David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | e9ce01c | 2007-05-16 02:29:53 +0000 | [diff] [blame] | 19 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 20 | #ifndef T_SCOPE_H |
| 21 | #define T_SCOPE_H |
| 22 | |
| 23 | #include <map> |
| 24 | #include <string> |
| 25 | |
| 26 | #include "t_type.h" |
| 27 | #include "t_service.h" |
| 28 | |
| 29 | /** |
| 30 | * This represents a variable scope used for looking up predefined types and |
| 31 | * services. Typically, a scope is associated with a t_program. Scopes are not |
| 32 | * used to determine code generation, but rather to resolve identifiers at |
| 33 | * parse time. |
| 34 | * |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 35 | */ |
| 36 | class t_scope { |
| 37 | public: |
| 38 | t_scope() {} |
| 39 | |
| 40 | void add_type(std::string name, t_type* type) { |
| 41 | types_[name] = type; |
| 42 | } |
| 43 | |
| 44 | t_type* get_type(std::string name) { |
| 45 | return types_[name]; |
| 46 | } |
| 47 | |
| 48 | void add_service(std::string name, t_service* service) { |
| 49 | services_[name] = service; |
| 50 | } |
| 51 | |
| 52 | t_service* get_service(std::string name) { |
| 53 | return services_[name]; |
| 54 | } |
| 55 | |
Mark Slee | d0767c5 | 2007-07-27 22:14:41 +0000 | [diff] [blame] | 56 | void add_constant(std::string name, t_const* constant) { |
| 57 | constants_[name] = constant; |
| 58 | } |
| 59 | |
| 60 | t_const* get_constant(std::string name) { |
| 61 | return constants_[name]; |
| 62 | } |
| 63 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 64 | void print() { |
| 65 | std::map<std::string, t_type*>::iterator iter; |
| 66 | for (iter = types_.begin(); iter != types_.end(); ++iter) { |
| 67 | printf("%s => %s\n", |
| 68 | iter->first.c_str(), |
| 69 | iter->second->get_name().c_str()); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | private: |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 74 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 75 | // Map of names to types |
| 76 | std::map<std::string, t_type*> types_; |
| 77 | |
Mark Slee | d0767c5 | 2007-07-27 22:14:41 +0000 | [diff] [blame] | 78 | // Map of names to constants |
| 79 | std::map<std::string, t_const*> constants_; |
| 80 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 81 | // Map of names to services |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 82 | std::map<std::string, t_service*> services_; |
| 83 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | #endif |