blob: 1e2ad1ec20637b5a2a47a1814b92257f1e649e34 [file] [log] [blame]
Mark Slee31985722006-05-24 21:45:31 +00001#ifndef T_PROGRAM_H
2#define T_PROGRAM_H
3
Mark Sleee8540632006-05-30 09:24:40 +00004#include <map>
Mark Slee31985722006-05-24 21:45:31 +00005#include <string>
6#include <vector>
7
Mark Sleef0712dc2006-10-25 19:03:57 +00008// For program_name()
9#include "main.h"
10
11#include "t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000012#include "t_base_type.h"
13#include "t_typedef.h"
14#include "t_enum.h"
Mark Slee30152872006-11-28 01:24:07 +000015#include "t_const.h"
Mark Slee31985722006-05-24 21:45:31 +000016#include "t_struct.h"
17#include "t_service.h"
Mark Sleee8540632006-05-30 09:24:40 +000018#include "t_list.h"
19#include "t_map.h"
20#include "t_set.h"
ccheeverf53b5cf2007-02-05 20:33:11 +000021//#include "t_doc.h"
Mark Slee31985722006-05-24 21:45:31 +000022
23/**
24 * Top level class representing an entire thrift program. A program consists
25 * fundamentally of the following:
26 *
27 * Typedefs
28 * Enumerations
Mark Slee30152872006-11-28 01:24:07 +000029 * Constants
Mark Slee31985722006-05-24 21:45:31 +000030 * Structs
Mark Slee9cb7c612006-09-01 22:17:45 +000031 * Exceptions
Mark Slee31985722006-05-24 21:45:31 +000032 * Services
33 *
Mark Sleef5377b32006-10-10 01:42:59 +000034 * The program module also contains the definitions of the base types.
35 *
Mark Slee31985722006-05-24 21:45:31 +000036 * @author Mark Slee <mcslee@facebook.com>
37 */
38class t_program {
39 public:
Mark Sleef0712dc2006-10-25 19:03:57 +000040 t_program(std::string path, std::string name) :
Mark Slee2c44d202007-05-16 02:18:07 +000041 path_(path),
Mark Sleef0712dc2006-10-25 19:03:57 +000042 name_(name) {
43 scope_ = new t_scope();
Mark Slee31985722006-05-24 21:45:31 +000044 }
45
Mark Sleef0712dc2006-10-25 19:03:57 +000046 t_program(std::string path) :
47 path_(path) {
48 name_ = program_name(path);
49 scope_ = new t_scope();
Mark Slee31985722006-05-24 21:45:31 +000050 }
51
Mark Slee2c44d202007-05-16 02:18:07 +000052 // Path accessor
Mark Sleef0712dc2006-10-25 19:03:57 +000053 const std::string& get_path() const { return path_; }
54
Mark Slee31985722006-05-24 21:45:31 +000055 // Name accessor
Mark Sleef0712dc2006-10-25 19:03:57 +000056 const std::string& get_name() const { return name_; }
Mark Slee31985722006-05-24 21:45:31 +000057
Mark Slee9cb7c612006-09-01 22:17:45 +000058 // Namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000059 const std::string& get_namespace() const { return namespace_; }
Mark Slee9cb7c612006-09-01 22:17:45 +000060
Mark Slee31985722006-05-24 21:45:31 +000061 // Accessors for program elements
Mark Slee9cb7c612006-09-01 22:17:45 +000062 const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; }
63 const std::vector<t_enum*>& get_enums() const { return enums_; }
Mark Slee30152872006-11-28 01:24:07 +000064 const std::vector<t_const*>& get_consts() const { return consts_; }
Mark Slee9cb7c612006-09-01 22:17:45 +000065 const std::vector<t_struct*>& get_structs() const { return structs_; }
66 const std::vector<t_struct*>& get_xceptions() const { return xceptions_; }
67 const std::vector<t_service*>& get_services() const { return services_; }
Mark Slee31985722006-05-24 21:45:31 +000068
Mark Sleef0712dc2006-10-25 19:03:57 +000069 // Program elements
70 void add_typedef (t_typedef* td) { typedefs_.push_back(td); }
71 void add_enum (t_enum* te) { enums_.push_back(te); }
Mark Slee30152872006-11-28 01:24:07 +000072 void add_const (t_const* tc) { consts_.push_back(tc); }
Mark Sleef0712dc2006-10-25 19:03:57 +000073 void add_struct (t_struct* ts) { structs_.push_back(ts); }
74 void add_xception (t_struct* tx) { xceptions_.push_back(tx); }
75 void add_service (t_service* ts) { services_.push_back(ts); }
Mark Slee31985722006-05-24 21:45:31 +000076
Mark Sleef0712dc2006-10-25 19:03:57 +000077 // Programs to include
78 const std::vector<t_program*>& get_includes() const { return includes_; }
Mark Sleee8540632006-05-30 09:24:40 +000079
Mark Sleef0712dc2006-10-25 19:03:57 +000080 // Scoping and namespacing
Mark Slee9cb7c612006-09-01 22:17:45 +000081 void set_namespace(std::string name) {
82 namespace_ = name;
83 }
84
Mark Sleef0712dc2006-10-25 19:03:57 +000085 // Scope accessor
86 t_scope* scope() {
87 return scope_;
Mark Sleee8540632006-05-30 09:24:40 +000088 }
Mark Sleef5377b32006-10-10 01:42:59 +000089
Mark Sleef0712dc2006-10-25 19:03:57 +000090 // Includes
91
92 void add_include(std::string path) {
93 includes_.push_back(new t_program(path));
Mark Sleee8540632006-05-30 09:24:40 +000094 }
Mark Sleef5377b32006-10-10 01:42:59 +000095
Mark Sleef0712dc2006-10-25 19:03:57 +000096 std::vector<t_program*>& get_includes() {
97 return includes_;
Mark Sleee8540632006-05-30 09:24:40 +000098 }
Mark Sleef5377b32006-10-10 01:42:59 +000099
Mark Sleef0712dc2006-10-25 19:03:57 +0000100 // Language specific namespace / packaging
101
102 void set_cpp_namespace(std::string cpp_namespace) {
103 cpp_namespace_ = cpp_namespace;
Mark Slee9cb7c612006-09-01 22:17:45 +0000104 }
Mark Sleef5377b32006-10-10 01:42:59 +0000105
Mark Sleef0712dc2006-10-25 19:03:57 +0000106 const std::string& get_cpp_namespace() const {
107 return cpp_namespace_;
Mark Sleee8540632006-05-30 09:24:40 +0000108 }
109
Mark Sleef0712dc2006-10-25 19:03:57 +0000110 void add_cpp_include(std::string path) {
111 cpp_includes_.push_back(path);
112 }
113
114 const std::vector<std::string>& get_cpp_includes() {
115 return cpp_includes_;
116 }
117
Mark Sleee888b372007-01-12 01:06:24 +0000118 void set_php_namespace(std::string php_namespace) {
119 php_namespace_ = php_namespace;
120 }
121
122 const std::string& get_php_namespace() const {
123 return php_namespace_;
124 }
125
Mark Sleef0712dc2006-10-25 19:03:57 +0000126 void set_java_package(std::string java_package) {
127 java_package_ = java_package;
128 }
129
130 const std::string& get_java_package() const {
131 return java_package_;
132 }
133
Mark Slee0d9199e2007-01-31 02:08:30 +0000134 void set_xsd_namespace(std::string xsd_namespace) {
135 xsd_namespace_ = xsd_namespace;
136 }
137
138 const std::string& get_xsd_namespace() const {
139 return xsd_namespace_;
140 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000141
Mark Slee2c44d202007-05-16 02:18:07 +0000142 void set_perl_namespace(std::string perl_namespace) {
143 perl_namespace_ = perl_namespace;
144 }
145
146 const std::string& get_perl_namespace() const {
147 return perl_namespace_;
148 }
149
Mark Slee31985722006-05-24 21:45:31 +0000150 private:
Mark Sleef5377b32006-10-10 01:42:59 +0000151
Mark Sleef0712dc2006-10-25 19:03:57 +0000152 // File path
153 std::string path_;
Mark Slee9cb7c612006-09-01 22:17:45 +0000154
Mark Slee31985722006-05-24 21:45:31 +0000155 // Name
156 std::string name_;
157
Mark Slee9cb7c612006-09-01 22:17:45 +0000158 // Namespace
159 std::string namespace_;
160
Mark Sleef0712dc2006-10-25 19:03:57 +0000161 // Included programs
162 std::vector<t_program*> includes_;
Mark Sleee8540632006-05-30 09:24:40 +0000163
Mark Sleef0712dc2006-10-25 19:03:57 +0000164 // Identifier lookup scope
165 t_scope* scope_;
Mark Sleee8540632006-05-30 09:24:40 +0000166
Mark Sleef0712dc2006-10-25 19:03:57 +0000167 // Components to generate code for
168 std::vector<t_typedef*> typedefs_;
169 std::vector<t_enum*> enums_;
Mark Slee30152872006-11-28 01:24:07 +0000170 std::vector<t_const*> consts_;
Mark Sleef0712dc2006-10-25 19:03:57 +0000171 std::vector<t_struct*> structs_;
172 std::vector<t_struct*> xceptions_;
173 std::vector<t_service*> services_;
174
175 // C++ namespace
176 std::string cpp_namespace_;
177
178 // C++ extra includes
179 std::vector<std::string> cpp_includes_;
180
Mark Sleee888b372007-01-12 01:06:24 +0000181 // PHP namespace
182 std::string php_namespace_;
183
Mark Sleef0712dc2006-10-25 19:03:57 +0000184 // Java package
185 std::string java_package_;
186
Mark Slee0d9199e2007-01-31 02:08:30 +0000187 // XSD namespace
188 std::string xsd_namespace_;
189
Mark Slee2c44d202007-05-16 02:18:07 +0000190 // Perl namespace
191 std::string perl_namespace_;
192
Mark Slee31985722006-05-24 21:45:31 +0000193};
194
195#endif