blob: 4e99294d309fdafd75a74bbd8b97b976742b252f [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
kholst76f2c882008-01-16 02:47:41 +000073 // Include prefix accessor
74 const std::string& get_include_prefix() const { return include_prefix_; }
75
Mark Slee31985722006-05-24 21:45:31 +000076 // Accessors for program elements
Mark Slee9cb7c612006-09-01 22:17:45 +000077 const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; }
78 const std::vector<t_enum*>& get_enums() const { return enums_; }
Mark Slee30152872006-11-28 01:24:07 +000079 const std::vector<t_const*>& get_consts() const { return consts_; }
Mark Slee9cb7c612006-09-01 22:17:45 +000080 const std::vector<t_struct*>& get_structs() const { return structs_; }
81 const std::vector<t_struct*>& get_xceptions() const { return xceptions_; }
Mark Slee1c4ced72008-01-14 23:04:43 +000082 const std::vector<t_struct*>& get_objects() const { return objects_; }
Mark Slee9cb7c612006-09-01 22:17:45 +000083 const std::vector<t_service*>& get_services() const { return services_; }
Mark Slee31985722006-05-24 21:45:31 +000084
Mark Sleef0712dc2006-10-25 19:03:57 +000085 // 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 Slee30152872006-11-28 01:24:07 +000088 void add_const (t_const* tc) { consts_.push_back(tc); }
Mark Slee1c4ced72008-01-14 23:04:43 +000089 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 Sleef0712dc2006-10-25 19:03:57 +000093 void add_service (t_service* ts) { services_.push_back(ts); }
Mark Slee31985722006-05-24 21:45:31 +000094
Mark Sleef0712dc2006-10-25 19:03:57 +000095 // Programs to include
96 const std::vector<t_program*>& get_includes() const { return includes_; }
Mark Sleee8540632006-05-30 09:24:40 +000097
dweatherford65b70752007-10-31 02:18:14 +000098 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 Sleef0712dc2006-10-25 19:03:57 +0000107 // Scoping and namespacing
Mark Slee9cb7c612006-09-01 22:17:45 +0000108 void set_namespace(std::string name) {
109 namespace_ = name;
110 }
111
Mark Sleef0712dc2006-10-25 19:03:57 +0000112 // Scope accessor
113 t_scope* scope() {
114 return scope_;
Mark Sleee8540632006-05-30 09:24:40 +0000115 }
Mark Sleef5377b32006-10-10 01:42:59 +0000116
Mark Sleef0712dc2006-10-25 19:03:57 +0000117 // Includes
118
kholst76f2c882008-01-16 02:47:41 +0000119 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 Sleee8540632006-05-30 09:24:40 +0000132 }
Mark Sleef5377b32006-10-10 01:42:59 +0000133
Mark Sleef0712dc2006-10-25 19:03:57 +0000134 std::vector<t_program*>& get_includes() {
135 return includes_;
Mark Sleee8540632006-05-30 09:24:40 +0000136 }
Mark Sleef5377b32006-10-10 01:42:59 +0000137
kholst76f2c882008-01-16 02:47:41 +0000138 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
David Reiss79eca142008-02-27 01:55:13 +0000148 // Language neutral namespace / packaging
149 void set_namespace(std::string language, std::string name_space) {
150 namespaces_[language] = name_space;
151 }
152
153 std::string get_namespace(std::string language) const {
154 std::map<std::string, std::string>::const_iterator iter = namespaces_.find(language);
155 if (iter == namespaces_.end()) {
156 return std::string();
157 }
158 return iter->second;
159 }
160
Mark Sleef0712dc2006-10-25 19:03:57 +0000161 // Language specific namespace / packaging
162
Mark Sleef0712dc2006-10-25 19:03:57 +0000163 void add_cpp_include(std::string path) {
164 cpp_includes_.push_back(path);
165 }
166
167 const std::vector<std::string>& get_cpp_includes() {
168 return cpp_includes_;
169 }
170
Mark Sleee888b372007-01-12 01:06:24 +0000171 void set_php_namespace(std::string php_namespace) {
172 php_namespace_ = php_namespace;
173 }
174
175 const std::string& get_php_namespace() const {
176 return php_namespace_;
177 }
178
Mark Slee0d9199e2007-01-31 02:08:30 +0000179 void set_xsd_namespace(std::string xsd_namespace) {
180 xsd_namespace_ = xsd_namespace;
181 }
182
183 const std::string& get_xsd_namespace() const {
184 return xsd_namespace_;
185 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000186
Mark Slee31985722006-05-24 21:45:31 +0000187 private:
Mark Sleef5377b32006-10-10 01:42:59 +0000188
Mark Sleef0712dc2006-10-25 19:03:57 +0000189 // File path
190 std::string path_;
Mark Slee9cb7c612006-09-01 22:17:45 +0000191
Mark Slee31985722006-05-24 21:45:31 +0000192 // Name
193 std::string name_;
194
dweatherford65b70752007-10-31 02:18:14 +0000195 // Output directory
196 std::string out_path_;
197
Mark Slee9cb7c612006-09-01 22:17:45 +0000198 // Namespace
199 std::string namespace_;
200
Mark Sleef0712dc2006-10-25 19:03:57 +0000201 // Included programs
202 std::vector<t_program*> includes_;
Mark Sleee8540632006-05-30 09:24:40 +0000203
kholst76f2c882008-01-16 02:47:41 +0000204 // Include prefix for this program, if any
205 std::string include_prefix_;
206
Mark Sleef0712dc2006-10-25 19:03:57 +0000207 // Identifier lookup scope
208 t_scope* scope_;
Mark Sleee8540632006-05-30 09:24:40 +0000209
Mark Sleef0712dc2006-10-25 19:03:57 +0000210 // Components to generate code for
211 std::vector<t_typedef*> typedefs_;
212 std::vector<t_enum*> enums_;
Mark Slee30152872006-11-28 01:24:07 +0000213 std::vector<t_const*> consts_;
Mark Slee1c4ced72008-01-14 23:04:43 +0000214 std::vector<t_struct*> objects_;
Mark Sleef0712dc2006-10-25 19:03:57 +0000215 std::vector<t_struct*> structs_;
216 std::vector<t_struct*> xceptions_;
217 std::vector<t_service*> services_;
218
David Reiss79eca142008-02-27 01:55:13 +0000219 // Dynamic namespaces
220 std::map<std::string, std::string> namespaces_;
221
Mark Sleef0712dc2006-10-25 19:03:57 +0000222 // C++ extra includes
223 std::vector<std::string> cpp_includes_;
224
Mark Sleee888b372007-01-12 01:06:24 +0000225 // PHP namespace
226 std::string php_namespace_;
227
Mark Slee0d9199e2007-01-31 02:08:30 +0000228 // XSD namespace
229 std::string xsd_namespace_;
230
Mark Slee31985722006-05-24 21:45:31 +0000231};
232
233#endif