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