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 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 8 | // For program_name() |
| 9 | #include "main.h" |
| 10 | |
| 11 | #include "t_scope.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 12 | #include "t_base_type.h" |
| 13 | #include "t_typedef.h" |
| 14 | #include "t_enum.h" |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 15 | #include "t_const.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 16 | #include "t_struct.h" |
| 17 | #include "t_service.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 18 | #include "t_list.h" |
| 19 | #include "t_map.h" |
| 20 | #include "t_set.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * Top level class representing an entire thrift program. A program consists |
| 24 | * fundamentally of the following: |
| 25 | * |
| 26 | * Typedefs |
| 27 | * Enumerations |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 28 | * Constants |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 29 | * Structs |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 30 | * Exceptions |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 31 | * Services |
| 32 | * |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 33 | * The program module also contains the definitions of the base types. |
| 34 | * |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 35 | * @author Mark Slee <mcslee@facebook.com> |
| 36 | */ |
| 37 | class t_program { |
| 38 | public: |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 39 | t_program(std::string path, std::string name) : |
| 40 | path_(path), |
| 41 | name_(name) { |
| 42 | scope_ = new t_scope(); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 45 | t_program(std::string path) : |
| 46 | path_(path) { |
| 47 | name_ = program_name(path); |
| 48 | scope_ = new t_scope(); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 51 | // Path accessor |
| 52 | const std::string& get_path() const { return path_; } |
| 53 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 54 | // Name accessor |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 55 | const std::string& get_name() const { return name_; } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 56 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 57 | // Namespace |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 58 | const std::string& get_namespace() const { return namespace_; } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 59 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 60 | // Accessors for program elements |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 61 | const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; } |
| 62 | const std::vector<t_enum*>& get_enums() const { return enums_; } |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 63 | const std::vector<t_const*>& get_consts() const { return consts_; } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 64 | const std::vector<t_struct*>& get_structs() const { return structs_; } |
| 65 | const std::vector<t_struct*>& get_xceptions() const { return xceptions_; } |
| 66 | const std::vector<t_service*>& get_services() const { return services_; } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 67 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 68 | // Program elements |
| 69 | void add_typedef (t_typedef* td) { typedefs_.push_back(td); } |
| 70 | void add_enum (t_enum* te) { enums_.push_back(te); } |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 71 | void add_const (t_const* tc) { consts_.push_back(tc); } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 72 | void add_struct (t_struct* ts) { structs_.push_back(ts); } |
| 73 | void add_xception (t_struct* tx) { xceptions_.push_back(tx); } |
| 74 | void add_service (t_service* ts) { services_.push_back(ts); } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 75 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 76 | // Programs to include |
| 77 | const std::vector<t_program*>& get_includes() const { return includes_; } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 78 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 79 | // Scoping and namespacing |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 80 | void set_namespace(std::string name) { |
| 81 | namespace_ = name; |
| 82 | } |
| 83 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 84 | // Scope accessor |
| 85 | t_scope* scope() { |
| 86 | return scope_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 87 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 88 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 89 | // Includes |
| 90 | |
| 91 | void add_include(std::string path) { |
| 92 | includes_.push_back(new t_program(path)); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 93 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 94 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 95 | std::vector<t_program*>& get_includes() { |
| 96 | return includes_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 97 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 98 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 99 | // Language specific namespace / packaging |
| 100 | |
| 101 | void set_cpp_namespace(std::string cpp_namespace) { |
| 102 | cpp_namespace_ = cpp_namespace; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 103 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 104 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 105 | const std::string& get_cpp_namespace() const { |
| 106 | return cpp_namespace_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 109 | void add_cpp_include(std::string path) { |
| 110 | cpp_includes_.push_back(path); |
| 111 | } |
| 112 | |
| 113 | const std::vector<std::string>& get_cpp_includes() { |
| 114 | return cpp_includes_; |
| 115 | } |
| 116 | |
Mark Slee | e888b37 | 2007-01-12 01:06:24 +0000 | [diff] [blame] | 117 | void set_php_namespace(std::string php_namespace) { |
| 118 | php_namespace_ = php_namespace; |
| 119 | } |
| 120 | |
| 121 | const std::string& get_php_namespace() const { |
| 122 | return php_namespace_; |
| 123 | } |
| 124 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 125 | void set_java_package(std::string java_package) { |
| 126 | java_package_ = java_package; |
| 127 | } |
| 128 | |
| 129 | const std::string& get_java_package() const { |
| 130 | return java_package_; |
| 131 | } |
| 132 | |
Mark Slee | 0d9199e | 2007-01-31 02:08:30 +0000 | [diff] [blame^] | 133 | void set_xsd_namespace(std::string xsd_namespace) { |
| 134 | xsd_namespace_ = xsd_namespace; |
| 135 | } |
| 136 | |
| 137 | const std::string& get_xsd_namespace() const { |
| 138 | return xsd_namespace_; |
| 139 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 140 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 141 | private: |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 142 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 143 | // File path |
| 144 | std::string path_; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 145 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 146 | // Name |
| 147 | std::string name_; |
| 148 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 149 | // Namespace |
| 150 | std::string namespace_; |
| 151 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 152 | // Included programs |
| 153 | std::vector<t_program*> includes_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 154 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 155 | // Identifier lookup scope |
| 156 | t_scope* scope_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 157 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 158 | // Components to generate code for |
| 159 | std::vector<t_typedef*> typedefs_; |
| 160 | std::vector<t_enum*> enums_; |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 161 | std::vector<t_const*> consts_; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 162 | std::vector<t_struct*> structs_; |
| 163 | std::vector<t_struct*> xceptions_; |
| 164 | std::vector<t_service*> services_; |
| 165 | |
| 166 | // C++ namespace |
| 167 | std::string cpp_namespace_; |
| 168 | |
| 169 | // C++ extra includes |
| 170 | std::vector<std::string> cpp_includes_; |
| 171 | |
Mark Slee | e888b37 | 2007-01-12 01:06:24 +0000 | [diff] [blame] | 172 | // PHP namespace |
| 173 | std::string php_namespace_; |
| 174 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 175 | // Java package |
| 176 | std::string java_package_; |
| 177 | |
Mark Slee | 0d9199e | 2007-01-31 02:08:30 +0000 | [diff] [blame^] | 178 | // XSD namespace |
| 179 | std::string xsd_namespace_; |
| 180 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | #endif |