blob: 62528e6f2e3d87bd553841284e287a0115baf5b3 [file] [log] [blame]
Mark Sleee9ce01c2007-05-16 02:29:53 +00001// 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 Slee31985722006-05-24 21:45:31 +00007#ifndef T_PROGRAM_H
8#define T_PROGRAM_H
9
Mark Sleee8540632006-05-30 09:24:40 +000010#include <map>
Mark Slee31985722006-05-24 21:45:31 +000011#include <string>
12#include <vector>
13
Mark Sleef0712dc2006-10-25 19:03:57 +000014// For program_name()
15#include "main.h"
16
David Reissc2532a92007-07-30 23:46:11 +000017#include "t_doc.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000018#include "t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000019#include "t_base_type.h"
20#include "t_typedef.h"
21#include "t_enum.h"
Mark Slee30152872006-11-28 01:24:07 +000022#include "t_const.h"
Mark Slee31985722006-05-24 21:45:31 +000023#include "t_struct.h"
24#include "t_service.h"
Mark Sleee8540632006-05-30 09:24:40 +000025#include "t_list.h"
26#include "t_map.h"
27#include "t_set.h"
ccheeverf53b5cf2007-02-05 20:33:11 +000028//#include "t_doc.h"
Mark Slee31985722006-05-24 21:45:31 +000029
30/**
31 * Top level class representing an entire thrift program. A program consists
32 * fundamentally of the following:
33 *
34 * Typedefs
35 * Enumerations
Mark Slee30152872006-11-28 01:24:07 +000036 * Constants
Mark Slee31985722006-05-24 21:45:31 +000037 * Structs
Mark Slee9cb7c612006-09-01 22:17:45 +000038 * Exceptions
Mark Slee31985722006-05-24 21:45:31 +000039 * Services
40 *
Mark Sleef5377b32006-10-10 01:42:59 +000041 * The program module also contains the definitions of the base types.
42 *
Mark Slee31985722006-05-24 21:45:31 +000043 * @author Mark Slee <mcslee@facebook.com>
44 */
David Reissc2532a92007-07-30 23:46:11 +000045class t_program : public t_doc {
Mark Slee31985722006-05-24 21:45:31 +000046 public:
Mark Sleef0712dc2006-10-25 19:03:57 +000047 t_program(std::string path, std::string name) :
Mark Slee2c44d202007-05-16 02:18:07 +000048 path_(path),
dweatherford65b70752007-10-31 02:18:14 +000049 name_(name),
50 out_path_("./") {
Mark Sleef0712dc2006-10-25 19:03:57 +000051 scope_ = new t_scope();
Mark Slee31985722006-05-24 21:45:31 +000052 }
53
Mark Sleef0712dc2006-10-25 19:03:57 +000054 t_program(std::string path) :
dweatherford65b70752007-10-31 02:18:14 +000055 path_(path),
56 out_path_("./") {
Mark Sleef0712dc2006-10-25 19:03:57 +000057 name_ = program_name(path);
58 scope_ = new t_scope();
Mark Slee31985722006-05-24 21:45:31 +000059 }
60
Mark Slee2c44d202007-05-16 02:18:07 +000061 // Path accessor
Mark Sleef0712dc2006-10-25 19:03:57 +000062 const std::string& get_path() const { return path_; }
63
dweatherford65b70752007-10-31 02:18:14 +000064 // Output path accessor
65 const std::string& get_out_path() const { return out_path_; }
66
Mark Slee31985722006-05-24 21:45:31 +000067 // Name accessor
Mark Sleef0712dc2006-10-25 19:03:57 +000068 const std::string& get_name() const { return name_; }
Mark Slee31985722006-05-24 21:45:31 +000069
Mark Slee9cb7c612006-09-01 22:17:45 +000070 // Namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000071 const std::string& get_namespace() const { return namespace_; }
Mark Slee9cb7c612006-09-01 22:17:45 +000072
Mark Slee31985722006-05-24 21:45:31 +000073 // Accessors for program elements
Mark Slee9cb7c612006-09-01 22:17:45 +000074 const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; }
75 const std::vector<t_enum*>& get_enums() const { return enums_; }
Mark Slee30152872006-11-28 01:24:07 +000076 const std::vector<t_const*>& get_consts() const { return consts_; }
Mark Slee9cb7c612006-09-01 22:17:45 +000077 const std::vector<t_struct*>& get_structs() const { return structs_; }
78 const std::vector<t_struct*>& get_xceptions() const { return xceptions_; }
Mark Slee1c4ced72008-01-14 23:04:43 +000079 const std::vector<t_struct*>& get_objects() const { return objects_; }
Mark Slee9cb7c612006-09-01 22:17:45 +000080 const std::vector<t_service*>& get_services() const { return services_; }
Mark Slee31985722006-05-24 21:45:31 +000081
Mark Sleef0712dc2006-10-25 19:03:57 +000082 // Program elements
83 void add_typedef (t_typedef* td) { typedefs_.push_back(td); }
84 void add_enum (t_enum* te) { enums_.push_back(te); }
Mark Slee30152872006-11-28 01:24:07 +000085 void add_const (t_const* tc) { consts_.push_back(tc); }
Mark Slee1c4ced72008-01-14 23:04:43 +000086 void add_struct (t_struct* ts) { objects_.push_back(ts);
87 structs_.push_back(ts); }
88 void add_xception (t_struct* tx) { objects_.push_back(tx);
89 xceptions_.push_back(tx); }
Mark Sleef0712dc2006-10-25 19:03:57 +000090 void add_service (t_service* ts) { services_.push_back(ts); }
Mark Slee31985722006-05-24 21:45:31 +000091
Mark Sleef0712dc2006-10-25 19:03:57 +000092 // Programs to include
93 const std::vector<t_program*>& get_includes() const { return includes_; }
Mark Sleee8540632006-05-30 09:24:40 +000094
dweatherford65b70752007-10-31 02:18:14 +000095 void set_out_path(std::string out_path) {
96 out_path_ = out_path;
97 // Ensure that it ends with a trailing '/' (or '\' for windows machines)
98 char c = out_path_.at(out_path_.size() - 1);
99 if (!(c == '/' || c == '\\')) {
100 out_path_.push_back('/');
101 }
102 }
103
Mark Sleef0712dc2006-10-25 19:03:57 +0000104 // Scoping and namespacing
Mark Slee9cb7c612006-09-01 22:17:45 +0000105 void set_namespace(std::string name) {
106 namespace_ = name;
107 }
108
Mark Sleef0712dc2006-10-25 19:03:57 +0000109 // Scope accessor
110 t_scope* scope() {
111 return scope_;
Mark Sleee8540632006-05-30 09:24:40 +0000112 }
Mark Sleef5377b32006-10-10 01:42:59 +0000113
Mark Sleef0712dc2006-10-25 19:03:57 +0000114 // Includes
115
116 void add_include(std::string path) {
117 includes_.push_back(new t_program(path));
Mark Sleee8540632006-05-30 09:24:40 +0000118 }
Mark Sleef5377b32006-10-10 01:42:59 +0000119
Mark Sleef0712dc2006-10-25 19:03:57 +0000120 std::vector<t_program*>& get_includes() {
121 return includes_;
Mark Sleee8540632006-05-30 09:24:40 +0000122 }
Mark Sleef5377b32006-10-10 01:42:59 +0000123
Mark Sleef0712dc2006-10-25 19:03:57 +0000124 // Language specific namespace / packaging
125
126 void set_cpp_namespace(std::string cpp_namespace) {
127 cpp_namespace_ = cpp_namespace;
Mark Slee9cb7c612006-09-01 22:17:45 +0000128 }
Mark Sleef5377b32006-10-10 01:42:59 +0000129
Mark Sleef0712dc2006-10-25 19:03:57 +0000130 const std::string& get_cpp_namespace() const {
131 return cpp_namespace_;
Mark Sleee8540632006-05-30 09:24:40 +0000132 }
133
Mark Sleef0712dc2006-10-25 19:03:57 +0000134 void add_cpp_include(std::string path) {
135 cpp_includes_.push_back(path);
136 }
137
138 const std::vector<std::string>& get_cpp_includes() {
139 return cpp_includes_;
140 }
141
Mark Sleee888b372007-01-12 01:06:24 +0000142 void set_php_namespace(std::string php_namespace) {
143 php_namespace_ = php_namespace;
144 }
145
146 const std::string& get_php_namespace() const {
147 return php_namespace_;
148 }
149
Mark Sleef0712dc2006-10-25 19:03:57 +0000150 void set_java_package(std::string java_package) {
151 java_package_ = java_package;
152 }
153
154 const std::string& get_java_package() const {
155 return java_package_;
156 }
157
David Reiss7f42bcf2008-01-11 20:59:12 +0000158 void set_csharp_namespace(std::string csharp_namespace) {
159 csharp_namespace_ = csharp_namespace;
160 }
161
162 const std::string& get_csharp_namespace() const {
163 return csharp_namespace_;
164 }
165
Mark Slee0d9199e2007-01-31 02:08:30 +0000166 void set_xsd_namespace(std::string xsd_namespace) {
167 xsd_namespace_ = xsd_namespace;
168 }
169
170 const std::string& get_xsd_namespace() const {
171 return xsd_namespace_;
172 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000173
Mark Slee58dfb4f2007-07-06 02:45:25 +0000174 void set_ruby_namespace(std::string ruby_namespace) {
175 ruby_namespace_ = ruby_namespace;
176 }
Mark Slee27ed6ec2007-08-16 01:26:31 +0000177
Mark Slee58dfb4f2007-07-06 02:45:25 +0000178 const std::string& get_ruby_namespace() const {
179 return ruby_namespace_;
180 }
181
David Reissc6fc3292007-08-30 00:58:43 +0000182 void set_py_module(std::string py_module) {
183 py_module_ = py_module;
184 }
185
186 const std::string& get_py_module() const {
187 return py_module_;
188 }
189
Mark Slee27ed6ec2007-08-16 01:26:31 +0000190 void set_perl_package(std::string perl_package) {
191 perl_package_ = perl_package;
Mark Slee2c44d202007-05-16 02:18:07 +0000192 }
193
Mark Slee27ed6ec2007-08-16 01:26:31 +0000194 const std::string& get_perl_package() const {
195 return perl_package_;
Mark Slee2c44d202007-05-16 02:18:07 +0000196 }
197
Mark Slee7e9eea42007-09-10 21:00:23 +0000198 void set_cocoa_prefix(std::string cocoa_prefix) {
199 cocoa_prefix_ = cocoa_prefix;
200 }
201
202 const std::string& get_cocoa_prefix() const {
203 return cocoa_prefix_;
204 }
205
Mark Sleebd588222007-11-21 08:43:35 +0000206 void set_smalltalk_category(std::string smalltalk_category) {
207 smalltalk_category_ = smalltalk_category;
208 }
209
210 const std::string& get_smalltalk_category() const {
211 return smalltalk_category_;
212 }
213
David Reiss15457c92007-12-14 07:03:03 +0000214 void set_smalltalk_prefix(std::string smalltalk_prefix) {
215 smalltalk_prefix_ = smalltalk_prefix;
216 }
Mark Slee1c4ced72008-01-14 23:04:43 +0000217
David Reiss15457c92007-12-14 07:03:03 +0000218 const std::string& get_smalltalk_prefix() const {
219 return smalltalk_prefix_;
220 }
221
Mark Slee31985722006-05-24 21:45:31 +0000222 private:
Mark Sleef5377b32006-10-10 01:42:59 +0000223
Mark Sleef0712dc2006-10-25 19:03:57 +0000224 // File path
225 std::string path_;
Mark Slee9cb7c612006-09-01 22:17:45 +0000226
Mark Slee31985722006-05-24 21:45:31 +0000227 // Name
228 std::string name_;
229
dweatherford65b70752007-10-31 02:18:14 +0000230 // Output directory
231 std::string out_path_;
232
Mark Slee9cb7c612006-09-01 22:17:45 +0000233 // Namespace
234 std::string namespace_;
235
Mark Sleef0712dc2006-10-25 19:03:57 +0000236 // Included programs
237 std::vector<t_program*> includes_;
Mark Sleee8540632006-05-30 09:24:40 +0000238
Mark Sleef0712dc2006-10-25 19:03:57 +0000239 // Identifier lookup scope
240 t_scope* scope_;
Mark Sleee8540632006-05-30 09:24:40 +0000241
Mark Sleef0712dc2006-10-25 19:03:57 +0000242 // Components to generate code for
243 std::vector<t_typedef*> typedefs_;
244 std::vector<t_enum*> enums_;
Mark Slee30152872006-11-28 01:24:07 +0000245 std::vector<t_const*> consts_;
Mark Slee1c4ced72008-01-14 23:04:43 +0000246 std::vector<t_struct*> objects_;
Mark Sleef0712dc2006-10-25 19:03:57 +0000247 std::vector<t_struct*> structs_;
248 std::vector<t_struct*> xceptions_;
249 std::vector<t_service*> services_;
250
251 // C++ namespace
252 std::string cpp_namespace_;
253
254 // C++ extra includes
255 std::vector<std::string> cpp_includes_;
256
Mark Sleee888b372007-01-12 01:06:24 +0000257 // PHP namespace
258 std::string php_namespace_;
259
Mark Sleef0712dc2006-10-25 19:03:57 +0000260 // Java package
261 std::string java_package_;
262
Mark Slee0d9199e2007-01-31 02:08:30 +0000263 // XSD namespace
264 std::string xsd_namespace_;
265
Mark Slee58dfb4f2007-07-06 02:45:25 +0000266 // Ruby namespace
267 std::string ruby_namespace_;
268
David Reissc6fc3292007-08-30 00:58:43 +0000269 // Python namespace
270 std::string py_module_;
271
Mark Slee2c44d202007-05-16 02:18:07 +0000272 // Perl namespace
Mark Slee27ed6ec2007-08-16 01:26:31 +0000273 std::string perl_package_;
Mark Slee2c44d202007-05-16 02:18:07 +0000274
Mark Slee7e9eea42007-09-10 21:00:23 +0000275 // Cocoa/Objective-C naming prefix
276 std::string cocoa_prefix_;
277
Mark Sleebd588222007-11-21 08:43:35 +0000278 // Smalltalk category
279 std::string smalltalk_category_;
David Reiss15457c92007-12-14 07:03:03 +0000280 // Smalltalk prefix
281 std::string smalltalk_prefix_;
David Reiss7f42bcf2008-01-11 20:59:12 +0000282
283 // C# namespace
284 std::string csharp_namespace_;
Mark Slee31985722006-05-24 21:45:31 +0000285};
286
287#endif