Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 1 | #ifndef T_PROGRAM_H |
| 2 | #define T_PROGRAM_H |
| 3 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 4 | #include <map> |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 5 | #include <string> |
| 6 | #include <vector> |
| 7 | |
| 8 | #include "t_base_type.h" |
| 9 | #include "t_typedef.h" |
| 10 | #include "t_enum.h" |
| 11 | #include "t_struct.h" |
| 12 | #include "t_service.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 13 | #include "t_list.h" |
| 14 | #include "t_map.h" |
| 15 | #include "t_set.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * Top level class representing an entire thrift program. A program consists |
| 19 | * fundamentally of the following: |
| 20 | * |
| 21 | * Typedefs |
| 22 | * Enumerations |
| 23 | * Structs |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 24 | * Exceptions |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 25 | * Services |
| 26 | * |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 27 | * The program module also contains the definitions of the base types. |
| 28 | * |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 29 | * @author Mark Slee <mcslee@facebook.com> |
| 30 | */ |
| 31 | class t_program { |
| 32 | public: |
| 33 | t_program(std::string name) : |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 34 | name_(name), |
| 35 | namespace_() { |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 36 | type_void = new t_base_type("void", t_base_type::TYPE_VOID); |
| 37 | type_string = new t_base_type("string", t_base_type::TYPE_STRING); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 38 | type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 39 | type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE); |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 40 | type_i16 = new t_base_type("i16", t_base_type::TYPE_I16); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 41 | type_i32 = new t_base_type("i32", t_base_type::TYPE_I32); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 42 | type_i64 = new t_base_type("i64", t_base_type::TYPE_I64); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 43 | type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | ~t_program() { |
| 47 | delete type_string; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 48 | delete type_bool; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 49 | delete type_byte; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 50 | delete type_i16; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 51 | delete type_i32; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 52 | delete type_i64; |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 53 | delete type_double; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // Name accessor |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 57 | const std::string& get_name() const { |
| 58 | return name_; |
| 59 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 60 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 61 | // Namespace |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 62 | const std::string& get_namespace() const { |
| 63 | return namespace_; |
| 64 | } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 65 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 66 | // Accessors for program elements |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 67 | const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; } |
| 68 | const std::vector<t_enum*>& get_enums() const { return enums_; } |
| 69 | const std::vector<t_struct*>& get_structs() const { return structs_; } |
| 70 | const std::vector<t_struct*>& get_xceptions() const { return xceptions_; } |
| 71 | const std::vector<t_service*>& get_services() const { return services_; } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 72 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 73 | // Accessors for global types |
| 74 | t_type* get_void_type() const { return type_void; } |
| 75 | t_type* get_string_type() const { return type_string; } |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 76 | t_type* get_bool_type() const { return type_bool; } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 77 | t_type* get_byte_type() const { return type_byte; } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 78 | t_type* get_i16_type() const { return type_i16; } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 79 | t_type* get_i32_type() const { return type_i32; } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 80 | t_type* get_i64_type() const { return type_i64; } |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 81 | t_type* get_double_type() const { return type_double; } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 82 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 83 | // Custom data type lookup |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 84 | t_type* get_custom_type(std::string name) { |
| 85 | return custom_types_[name]; |
| 86 | } |
| 87 | |
| 88 | // New program element addition |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 89 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 90 | void set_namespace(std::string name) { |
| 91 | namespace_ = name; |
| 92 | } |
| 93 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 94 | void add_typedef(t_typedef* td) { |
| 95 | typedefs_.push_back(td); |
| 96 | add_custom_type(td->get_symbolic(), td); |
| 97 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 98 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 99 | void add_enum(t_enum* te) { |
| 100 | enums_.push_back(te); |
| 101 | add_custom_type(te->get_name(), te); |
| 102 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 103 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 104 | void add_struct(t_struct* ts) { |
| 105 | structs_.push_back(ts); |
| 106 | add_custom_type(ts->get_name(), ts); |
| 107 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 108 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 109 | void add_xception(t_struct* tx) { |
| 110 | xceptions_.push_back(tx); |
| 111 | add_custom_type(tx->get_name(), tx); |
| 112 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 113 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 114 | void add_service(t_service* ts) { |
| 115 | services_.push_back(ts); |
| 116 | } |
| 117 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 118 | private: |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 119 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 120 | // Add custom type for lookup |
| 121 | void add_custom_type(std::string name, t_type* type) { |
| 122 | custom_types_[name] = type; |
| 123 | } |
| 124 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 125 | // Name |
| 126 | std::string name_; |
| 127 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 128 | // Namespace |
| 129 | std::string namespace_; |
| 130 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 131 | // Components |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 132 | std::vector<t_typedef*> typedefs_; |
| 133 | std::vector<t_enum*> enums_; |
| 134 | std::vector<t_struct*> structs_; |
| 135 | std::vector<t_struct*> xceptions_; |
| 136 | std::vector<t_service*> services_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 137 | |
| 138 | // Type map |
| 139 | std::map<std::string, t_type*> custom_types_; |
| 140 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 141 | // Global base types |
| 142 | t_type* type_void; |
| 143 | t_type* type_string; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 144 | t_type* type_bool; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 145 | t_type* type_byte; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 146 | t_type* type_i16; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 147 | t_type* type_i32; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 148 | t_type* type_i64; |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 149 | t_type* type_double; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | #endif |