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 | |
David Reiss | c2532a9 | 2007-07-30 23:46:11 +0000 | [diff] [blame] | 17 | #include "t_doc.h" |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 18 | #include "t_scope.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 19 | #include "t_base_type.h" |
| 20 | #include "t_typedef.h" |
| 21 | #include "t_enum.h" |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 22 | #include "t_const.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 23 | #include "t_struct.h" |
| 24 | #include "t_service.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 25 | #include "t_list.h" |
| 26 | #include "t_map.h" |
| 27 | #include "t_set.h" |
ccheever | f53b5cf | 2007-02-05 20:33:11 +0000 | [diff] [blame] | 28 | //#include "t_doc.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Top level class representing an entire thrift program. A program consists |
| 32 | * fundamentally of the following: |
| 33 | * |
| 34 | * Typedefs |
| 35 | * Enumerations |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 36 | * Constants |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 37 | * Structs |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 38 | * Exceptions |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 39 | * Services |
| 40 | * |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 41 | * The program module also contains the definitions of the base types. |
| 42 | * |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 43 | * @author Mark Slee <mcslee@facebook.com> |
| 44 | */ |
David Reiss | c2532a9 | 2007-07-30 23:46:11 +0000 | [diff] [blame] | 45 | class t_program : public t_doc { |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 46 | public: |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 47 | t_program(std::string path, std::string name) : |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 48 | path_(path), |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 49 | name_(name), |
| 50 | out_path_("./") { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 51 | scope_ = new t_scope(); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 54 | t_program(std::string path) : |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 55 | path_(path), |
| 56 | out_path_("./") { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 57 | name_ = program_name(path); |
| 58 | scope_ = new t_scope(); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 61 | // Path accessor |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 62 | const std::string& get_path() const { return path_; } |
| 63 | |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 64 | // Output path accessor |
| 65 | const std::string& get_out_path() const { return out_path_; } |
| 66 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 67 | // Name accessor |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 68 | const std::string& get_name() const { return name_; } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 69 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 70 | // Namespace |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 71 | const std::string& get_namespace() const { return namespace_; } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 72 | |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 73 | // Include prefix accessor |
| 74 | const std::string& get_include_prefix() const { return include_prefix_; } |
| 75 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 76 | // Accessors for program elements |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 77 | const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; } |
| 78 | const std::vector<t_enum*>& get_enums() const { return enums_; } |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 79 | const std::vector<t_const*>& get_consts() const { return consts_; } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 80 | const std::vector<t_struct*>& get_structs() const { return structs_; } |
| 81 | const std::vector<t_struct*>& get_xceptions() const { return xceptions_; } |
Mark Slee | 1c4ced7 | 2008-01-14 23:04:43 +0000 | [diff] [blame] | 82 | const std::vector<t_struct*>& get_objects() const { return objects_; } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 83 | const std::vector<t_service*>& get_services() const { return services_; } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 84 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 85 | // Program elements |
| 86 | void add_typedef (t_typedef* td) { typedefs_.push_back(td); } |
| 87 | void add_enum (t_enum* te) { enums_.push_back(te); } |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 88 | void add_const (t_const* tc) { consts_.push_back(tc); } |
Mark Slee | 1c4ced7 | 2008-01-14 23:04:43 +0000 | [diff] [blame] | 89 | void add_struct (t_struct* ts) { objects_.push_back(ts); |
| 90 | structs_.push_back(ts); } |
| 91 | void add_xception (t_struct* tx) { objects_.push_back(tx); |
| 92 | xceptions_.push_back(tx); } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 93 | void add_service (t_service* ts) { services_.push_back(ts); } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 94 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 95 | // Programs to include |
| 96 | const std::vector<t_program*>& get_includes() const { return includes_; } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 97 | |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 98 | void set_out_path(std::string out_path) { |
| 99 | out_path_ = out_path; |
| 100 | // Ensure that it ends with a trailing '/' (or '\' for windows machines) |
| 101 | char c = out_path_.at(out_path_.size() - 1); |
| 102 | if (!(c == '/' || c == '\\')) { |
| 103 | out_path_.push_back('/'); |
| 104 | } |
| 105 | } |
| 106 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 107 | // Scoping and namespacing |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 108 | void set_namespace(std::string name) { |
| 109 | namespace_ = name; |
| 110 | } |
| 111 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 112 | // Scope accessor |
| 113 | t_scope* scope() { |
| 114 | return scope_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 115 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 116 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 117 | // Includes |
| 118 | |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 119 | void add_include(std::string path, std::string include_site) { |
| 120 | t_program* program = new t_program(path); |
| 121 | |
| 122 | // include prefix for this program is the site at which it was included |
| 123 | // (minus the filename) |
| 124 | std::string include_prefix; |
| 125 | std::string::size_type last_slash = std::string::npos; |
| 126 | if ((last_slash = include_site.rfind("/")) != std::string::npos) { |
| 127 | include_prefix = include_site.substr(0, last_slash); |
| 128 | } |
| 129 | |
| 130 | program->set_include_prefix(include_prefix); |
| 131 | includes_.push_back(program); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 132 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 133 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 134 | std::vector<t_program*>& get_includes() { |
| 135 | return includes_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 136 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 137 | |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 138 | void set_include_prefix(std::string include_prefix) { |
| 139 | include_prefix_ = include_prefix; |
| 140 | |
| 141 | // this is intended to be a directory; add a trailing slash if necessary |
| 142 | int len = include_prefix_.size(); |
| 143 | if (len > 0 && include_prefix_[len - 1] != '/') { |
| 144 | include_prefix_ += '/'; |
| 145 | } |
| 146 | } |
| 147 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 148 | // Language specific namespace / packaging |
| 149 | |
| 150 | void set_cpp_namespace(std::string cpp_namespace) { |
| 151 | cpp_namespace_ = cpp_namespace; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 152 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 153 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 154 | const std::string& get_cpp_namespace() const { |
| 155 | return cpp_namespace_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 158 | void add_cpp_include(std::string path) { |
| 159 | cpp_includes_.push_back(path); |
| 160 | } |
| 161 | |
| 162 | const std::vector<std::string>& get_cpp_includes() { |
| 163 | return cpp_includes_; |
| 164 | } |
| 165 | |
Mark Slee | e888b37 | 2007-01-12 01:06:24 +0000 | [diff] [blame] | 166 | void set_php_namespace(std::string php_namespace) { |
| 167 | php_namespace_ = php_namespace; |
| 168 | } |
| 169 | |
| 170 | const std::string& get_php_namespace() const { |
| 171 | return php_namespace_; |
| 172 | } |
| 173 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 174 | void set_java_package(std::string java_package) { |
| 175 | java_package_ = java_package; |
| 176 | } |
| 177 | |
| 178 | const std::string& get_java_package() const { |
| 179 | return java_package_; |
| 180 | } |
| 181 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 182 | void set_csharp_namespace(std::string csharp_namespace) { |
| 183 | csharp_namespace_ = csharp_namespace; |
| 184 | } |
| 185 | |
| 186 | const std::string& get_csharp_namespace() const { |
| 187 | return csharp_namespace_; |
| 188 | } |
| 189 | |
Mark Slee | 0d9199e | 2007-01-31 02:08:30 +0000 | [diff] [blame] | 190 | void set_xsd_namespace(std::string xsd_namespace) { |
| 191 | xsd_namespace_ = xsd_namespace; |
| 192 | } |
| 193 | |
| 194 | const std::string& get_xsd_namespace() const { |
| 195 | return xsd_namespace_; |
| 196 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 197 | |
Mark Slee | 58dfb4f | 2007-07-06 02:45:25 +0000 | [diff] [blame] | 198 | void set_ruby_namespace(std::string ruby_namespace) { |
| 199 | ruby_namespace_ = ruby_namespace; |
| 200 | } |
Mark Slee | 27ed6ec | 2007-08-16 01:26:31 +0000 | [diff] [blame] | 201 | |
Mark Slee | 58dfb4f | 2007-07-06 02:45:25 +0000 | [diff] [blame] | 202 | const std::string& get_ruby_namespace() const { |
| 203 | return ruby_namespace_; |
| 204 | } |
| 205 | |
David Reiss | c6fc329 | 2007-08-30 00:58:43 +0000 | [diff] [blame] | 206 | void set_py_module(std::string py_module) { |
| 207 | py_module_ = py_module; |
| 208 | } |
| 209 | |
| 210 | const std::string& get_py_module() const { |
| 211 | return py_module_; |
| 212 | } |
| 213 | |
Mark Slee | 27ed6ec | 2007-08-16 01:26:31 +0000 | [diff] [blame] | 214 | void set_perl_package(std::string perl_package) { |
| 215 | perl_package_ = perl_package; |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Mark Slee | 27ed6ec | 2007-08-16 01:26:31 +0000 | [diff] [blame] | 218 | const std::string& get_perl_package() const { |
| 219 | return perl_package_; |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Mark Slee | 7e9eea4 | 2007-09-10 21:00:23 +0000 | [diff] [blame] | 222 | void set_cocoa_prefix(std::string cocoa_prefix) { |
| 223 | cocoa_prefix_ = cocoa_prefix; |
| 224 | } |
| 225 | |
| 226 | const std::string& get_cocoa_prefix() const { |
| 227 | return cocoa_prefix_; |
| 228 | } |
| 229 | |
Mark Slee | bd58822 | 2007-11-21 08:43:35 +0000 | [diff] [blame] | 230 | void set_smalltalk_category(std::string smalltalk_category) { |
| 231 | smalltalk_category_ = smalltalk_category; |
| 232 | } |
| 233 | |
| 234 | const std::string& get_smalltalk_category() const { |
| 235 | return smalltalk_category_; |
| 236 | } |
| 237 | |
David Reiss | 15457c9 | 2007-12-14 07:03:03 +0000 | [diff] [blame] | 238 | void set_smalltalk_prefix(std::string smalltalk_prefix) { |
| 239 | smalltalk_prefix_ = smalltalk_prefix; |
| 240 | } |
Mark Slee | 1c4ced7 | 2008-01-14 23:04:43 +0000 | [diff] [blame] | 241 | |
David Reiss | 15457c9 | 2007-12-14 07:03:03 +0000 | [diff] [blame] | 242 | const std::string& get_smalltalk_prefix() const { |
| 243 | return smalltalk_prefix_; |
| 244 | } |
| 245 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 246 | private: |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 247 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 248 | // File path |
| 249 | std::string path_; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 250 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 251 | // Name |
| 252 | std::string name_; |
| 253 | |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 254 | // Output directory |
| 255 | std::string out_path_; |
| 256 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 257 | // Namespace |
| 258 | std::string namespace_; |
| 259 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 260 | // Included programs |
| 261 | std::vector<t_program*> includes_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 262 | |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 263 | // Include prefix for this program, if any |
| 264 | std::string include_prefix_; |
| 265 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 266 | // Identifier lookup scope |
| 267 | t_scope* scope_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 268 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 269 | // Components to generate code for |
| 270 | std::vector<t_typedef*> typedefs_; |
| 271 | std::vector<t_enum*> enums_; |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 272 | std::vector<t_const*> consts_; |
Mark Slee | 1c4ced7 | 2008-01-14 23:04:43 +0000 | [diff] [blame] | 273 | std::vector<t_struct*> objects_; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 274 | std::vector<t_struct*> structs_; |
| 275 | std::vector<t_struct*> xceptions_; |
| 276 | std::vector<t_service*> services_; |
| 277 | |
| 278 | // C++ namespace |
| 279 | std::string cpp_namespace_; |
| 280 | |
| 281 | // C++ extra includes |
| 282 | std::vector<std::string> cpp_includes_; |
| 283 | |
Mark Slee | e888b37 | 2007-01-12 01:06:24 +0000 | [diff] [blame] | 284 | // PHP namespace |
| 285 | std::string php_namespace_; |
| 286 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 287 | // Java package |
| 288 | std::string java_package_; |
| 289 | |
Mark Slee | 0d9199e | 2007-01-31 02:08:30 +0000 | [diff] [blame] | 290 | // XSD namespace |
| 291 | std::string xsd_namespace_; |
| 292 | |
Mark Slee | 58dfb4f | 2007-07-06 02:45:25 +0000 | [diff] [blame] | 293 | // Ruby namespace |
| 294 | std::string ruby_namespace_; |
| 295 | |
David Reiss | c6fc329 | 2007-08-30 00:58:43 +0000 | [diff] [blame] | 296 | // Python namespace |
| 297 | std::string py_module_; |
| 298 | |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 299 | // Perl namespace |
Mark Slee | 27ed6ec | 2007-08-16 01:26:31 +0000 | [diff] [blame] | 300 | std::string perl_package_; |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 301 | |
Mark Slee | 7e9eea4 | 2007-09-10 21:00:23 +0000 | [diff] [blame] | 302 | // Cocoa/Objective-C naming prefix |
| 303 | std::string cocoa_prefix_; |
| 304 | |
Mark Slee | bd58822 | 2007-11-21 08:43:35 +0000 | [diff] [blame] | 305 | // Smalltalk category |
| 306 | std::string smalltalk_category_; |
David Reiss | 15457c9 | 2007-12-14 07:03:03 +0000 | [diff] [blame] | 307 | // Smalltalk prefix |
| 308 | std::string smalltalk_prefix_; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 309 | |
| 310 | // C# namespace |
| 311 | std::string csharp_namespace_; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 312 | }; |
| 313 | |
| 314 | #endif |