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 | * |
| 22 | * @author Mark Slee <mcslee@facebook.com> |
| 23 | */ |
| 24 | class t_scope { |
| 25 | public: |
| 26 | t_scope() {} |
| 27 | |
| 28 | void add_type(std::string name, t_type* type) { |
| 29 | types_[name] = type; |
| 30 | } |
| 31 | |
| 32 | t_type* get_type(std::string name) { |
| 33 | return types_[name]; |
| 34 | } |
| 35 | |
| 36 | void add_service(std::string name, t_service* service) { |
| 37 | services_[name] = service; |
| 38 | } |
| 39 | |
| 40 | t_service* get_service(std::string name) { |
| 41 | return services_[name]; |
| 42 | } |
| 43 | |
Mark Slee | d0767c5 | 2007-07-27 22:14:41 +0000 | [diff] [blame] | 44 | void add_constant(std::string name, t_const* constant) { |
| 45 | constants_[name] = constant; |
| 46 | } |
| 47 | |
| 48 | t_const* get_constant(std::string name) { |
| 49 | return constants_[name]; |
| 50 | } |
| 51 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 52 | void print() { |
| 53 | std::map<std::string, t_type*>::iterator iter; |
| 54 | for (iter = types_.begin(); iter != types_.end(); ++iter) { |
| 55 | printf("%s => %s\n", |
| 56 | iter->first.c_str(), |
| 57 | iter->second->get_name().c_str()); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | private: |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 62 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 63 | // Map of names to types |
| 64 | std::map<std::string, t_type*> types_; |
| 65 | |
Mark Slee | d0767c5 | 2007-07-27 22:14:41 +0000 | [diff] [blame] | 66 | // Map of names to constants |
| 67 | std::map<std::string, t_const*> constants_; |
| 68 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 69 | // Map of names to services |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 70 | std::map<std::string, t_service*> services_; |
| 71 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | #endif |