Mark Slee | e9ce01c | 2007-05-16 02:29:53 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 7 | #ifndef T_SCOPE_H |
| 8 | #define T_SCOPE_H |
| 9 | |
| 10 | #include <map> |
| 11 | #include <string> |
| 12 | |
| 13 | #include "t_type.h" |
| 14 | #include "t_service.h" |
| 15 | |
| 16 | /** |
| 17 | * This represents a variable scope used for looking up predefined types and |
| 18 | * services. Typically, a scope is associated with a t_program. Scopes are not |
| 19 | * used to determine code generation, but rather to resolve identifiers at |
| 20 | * parse time. |
| 21 | * |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 22 | */ |
| 23 | class t_scope { |
| 24 | public: |
| 25 | t_scope() {} |
| 26 | |
| 27 | void add_type(std::string name, t_type* type) { |
| 28 | types_[name] = type; |
| 29 | } |
| 30 | |
| 31 | t_type* get_type(std::string name) { |
| 32 | return types_[name]; |
| 33 | } |
| 34 | |
| 35 | void add_service(std::string name, t_service* service) { |
| 36 | services_[name] = service; |
| 37 | } |
| 38 | |
| 39 | t_service* get_service(std::string name) { |
| 40 | return services_[name]; |
| 41 | } |
| 42 | |
Mark Slee | d0767c5 | 2007-07-27 22:14:41 +0000 | [diff] [blame] | 43 | void add_constant(std::string name, t_const* constant) { |
| 44 | constants_[name] = constant; |
| 45 | } |
| 46 | |
| 47 | t_const* get_constant(std::string name) { |
| 48 | return constants_[name]; |
| 49 | } |
| 50 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 51 | void print() { |
| 52 | std::map<std::string, t_type*>::iterator iter; |
| 53 | for (iter = types_.begin(); iter != types_.end(); ++iter) { |
| 54 | printf("%s => %s\n", |
| 55 | iter->first.c_str(), |
| 56 | iter->second->get_name().c_str()); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | private: |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 61 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 62 | // Map of names to types |
| 63 | std::map<std::string, t_type*> types_; |
| 64 | |
Mark Slee | d0767c5 | 2007-07-27 22:14:41 +0000 | [diff] [blame] | 65 | // Map of names to constants |
| 66 | std::map<std::string, t_const*> constants_; |
| 67 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 68 | // Map of names to services |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 69 | std::map<std::string, t_service*> services_; |
| 70 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | #endif |