blob: 610867aa61606513e0b237f594a4ba1fcd1a84e2 [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) :
41 path_(path),
42 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 Sleef0712dc2006-10-25 19:03:57 +000052 // Path accessor
53 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 Slee31985722006-05-24 21:45:31 +0000142 private:
Mark Sleef5377b32006-10-10 01:42:59 +0000143
Mark Sleef0712dc2006-10-25 19:03:57 +0000144 // File path
145 std::string path_;
Mark Slee9cb7c612006-09-01 22:17:45 +0000146
Mark Slee31985722006-05-24 21:45:31 +0000147 // Name
148 std::string name_;
149
Mark Slee9cb7c612006-09-01 22:17:45 +0000150 // Namespace
151 std::string namespace_;
152
Mark Sleef0712dc2006-10-25 19:03:57 +0000153 // Included programs
154 std::vector<t_program*> includes_;
Mark Sleee8540632006-05-30 09:24:40 +0000155
Mark Sleef0712dc2006-10-25 19:03:57 +0000156 // Identifier lookup scope
157 t_scope* scope_;
Mark Sleee8540632006-05-30 09:24:40 +0000158
Mark Sleef0712dc2006-10-25 19:03:57 +0000159 // Components to generate code for
160 std::vector<t_typedef*> typedefs_;
161 std::vector<t_enum*> enums_;
Mark Slee30152872006-11-28 01:24:07 +0000162 std::vector<t_const*> consts_;
Mark Sleef0712dc2006-10-25 19:03:57 +0000163 std::vector<t_struct*> structs_;
164 std::vector<t_struct*> xceptions_;
165 std::vector<t_service*> services_;
166
167 // C++ namespace
168 std::string cpp_namespace_;
169
170 // C++ extra includes
171 std::vector<std::string> cpp_includes_;
172
Mark Sleee888b372007-01-12 01:06:24 +0000173 // PHP namespace
174 std::string php_namespace_;
175
Mark Sleef0712dc2006-10-25 19:03:57 +0000176 // Java package
177 std::string java_package_;
178
Mark Slee0d9199e2007-01-31 02:08:30 +0000179 // XSD namespace
180 std::string xsd_namespace_;
181
Mark Slee31985722006-05-24 21:45:31 +0000182};
183
184#endif