blob: 7f7af44cf0d29f9bf5937d94b646f7aa33c4d26c [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"
Mark Slee31985722006-05-24 21:45:31 +000021
22/**
23 * Top level class representing an entire thrift program. A program consists
24 * fundamentally of the following:
25 *
26 * Typedefs
27 * Enumerations
Mark Slee30152872006-11-28 01:24:07 +000028 * Constants
Mark Slee31985722006-05-24 21:45:31 +000029 * Structs
Mark Slee9cb7c612006-09-01 22:17:45 +000030 * Exceptions
Mark Slee31985722006-05-24 21:45:31 +000031 * Services
32 *
Mark Sleef5377b32006-10-10 01:42:59 +000033 * The program module also contains the definitions of the base types.
34 *
Mark Slee31985722006-05-24 21:45:31 +000035 * @author Mark Slee <mcslee@facebook.com>
36 */
37class t_program {
38 public:
Mark Sleef0712dc2006-10-25 19:03:57 +000039 t_program(std::string path, std::string name) :
40 path_(path),
41 name_(name) {
42 scope_ = new t_scope();
Mark Slee31985722006-05-24 21:45:31 +000043 }
44
Mark Sleef0712dc2006-10-25 19:03:57 +000045 t_program(std::string path) :
46 path_(path) {
47 name_ = program_name(path);
48 scope_ = new t_scope();
Mark Slee31985722006-05-24 21:45:31 +000049 }
50
Mark Sleef0712dc2006-10-25 19:03:57 +000051 // Path accessor
52 const std::string& get_path() const { return path_; }
53
Mark Slee31985722006-05-24 21:45:31 +000054 // Name accessor
Mark Sleef0712dc2006-10-25 19:03:57 +000055 const std::string& get_name() const { return name_; }
Mark Slee31985722006-05-24 21:45:31 +000056
Mark Slee9cb7c612006-09-01 22:17:45 +000057 // Namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000058 const std::string& get_namespace() const { return namespace_; }
Mark Slee9cb7c612006-09-01 22:17:45 +000059
Mark Slee31985722006-05-24 21:45:31 +000060 // Accessors for program elements
Mark Slee9cb7c612006-09-01 22:17:45 +000061 const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; }
62 const std::vector<t_enum*>& get_enums() const { return enums_; }
Mark Slee30152872006-11-28 01:24:07 +000063 const std::vector<t_const*>& get_consts() const { return consts_; }
Mark Slee9cb7c612006-09-01 22:17:45 +000064 const std::vector<t_struct*>& get_structs() const { return structs_; }
65 const std::vector<t_struct*>& get_xceptions() const { return xceptions_; }
66 const std::vector<t_service*>& get_services() const { return services_; }
Mark Slee31985722006-05-24 21:45:31 +000067
Mark Sleef0712dc2006-10-25 19:03:57 +000068 // Program elements
69 void add_typedef (t_typedef* td) { typedefs_.push_back(td); }
70 void add_enum (t_enum* te) { enums_.push_back(te); }
Mark Slee30152872006-11-28 01:24:07 +000071 void add_const (t_const* tc) { consts_.push_back(tc); }
Mark Sleef0712dc2006-10-25 19:03:57 +000072 void add_struct (t_struct* ts) { structs_.push_back(ts); }
73 void add_xception (t_struct* tx) { xceptions_.push_back(tx); }
74 void add_service (t_service* ts) { services_.push_back(ts); }
Mark Slee31985722006-05-24 21:45:31 +000075
Mark Sleef0712dc2006-10-25 19:03:57 +000076 // Programs to include
77 const std::vector<t_program*>& get_includes() const { return includes_; }
Mark Sleee8540632006-05-30 09:24:40 +000078
Mark Sleef0712dc2006-10-25 19:03:57 +000079 // Scoping and namespacing
Mark Slee9cb7c612006-09-01 22:17:45 +000080 void set_namespace(std::string name) {
81 namespace_ = name;
82 }
83
Mark Sleef0712dc2006-10-25 19:03:57 +000084 // Scope accessor
85 t_scope* scope() {
86 return scope_;
Mark Sleee8540632006-05-30 09:24:40 +000087 }
Mark Sleef5377b32006-10-10 01:42:59 +000088
Mark Sleef0712dc2006-10-25 19:03:57 +000089 // Includes
90
91 void add_include(std::string path) {
92 includes_.push_back(new t_program(path));
Mark Sleee8540632006-05-30 09:24:40 +000093 }
Mark Sleef5377b32006-10-10 01:42:59 +000094
Mark Sleef0712dc2006-10-25 19:03:57 +000095 std::vector<t_program*>& get_includes() {
96 return includes_;
Mark Sleee8540632006-05-30 09:24:40 +000097 }
Mark Sleef5377b32006-10-10 01:42:59 +000098
Mark Sleef0712dc2006-10-25 19:03:57 +000099 // Language specific namespace / packaging
100
101 void set_cpp_namespace(std::string cpp_namespace) {
102 cpp_namespace_ = cpp_namespace;
Mark Slee9cb7c612006-09-01 22:17:45 +0000103 }
Mark Sleef5377b32006-10-10 01:42:59 +0000104
Mark Sleef0712dc2006-10-25 19:03:57 +0000105 const std::string& get_cpp_namespace() const {
106 return cpp_namespace_;
Mark Sleee8540632006-05-30 09:24:40 +0000107 }
108
Mark Sleef0712dc2006-10-25 19:03:57 +0000109 void add_cpp_include(std::string path) {
110 cpp_includes_.push_back(path);
111 }
112
113 const std::vector<std::string>& get_cpp_includes() {
114 return cpp_includes_;
115 }
116
Mark Sleee888b372007-01-12 01:06:24 +0000117 void set_php_namespace(std::string php_namespace) {
118 php_namespace_ = php_namespace;
119 }
120
121 const std::string& get_php_namespace() const {
122 return php_namespace_;
123 }
124
Mark Sleef0712dc2006-10-25 19:03:57 +0000125 void set_java_package(std::string java_package) {
126 java_package_ = java_package;
127 }
128
129 const std::string& get_java_package() const {
130 return java_package_;
131 }
132
Mark Slee0d9199e2007-01-31 02:08:30 +0000133 void set_xsd_namespace(std::string xsd_namespace) {
134 xsd_namespace_ = xsd_namespace;
135 }
136
137 const std::string& get_xsd_namespace() const {
138 return xsd_namespace_;
139 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000140
Mark Slee31985722006-05-24 21:45:31 +0000141 private:
Mark Sleef5377b32006-10-10 01:42:59 +0000142
Mark Sleef0712dc2006-10-25 19:03:57 +0000143 // File path
144 std::string path_;
Mark Slee9cb7c612006-09-01 22:17:45 +0000145
Mark Slee31985722006-05-24 21:45:31 +0000146 // Name
147 std::string name_;
148
Mark Slee9cb7c612006-09-01 22:17:45 +0000149 // Namespace
150 std::string namespace_;
151
Mark Sleef0712dc2006-10-25 19:03:57 +0000152 // Included programs
153 std::vector<t_program*> includes_;
Mark Sleee8540632006-05-30 09:24:40 +0000154
Mark Sleef0712dc2006-10-25 19:03:57 +0000155 // Identifier lookup scope
156 t_scope* scope_;
Mark Sleee8540632006-05-30 09:24:40 +0000157
Mark Sleef0712dc2006-10-25 19:03:57 +0000158 // Components to generate code for
159 std::vector<t_typedef*> typedefs_;
160 std::vector<t_enum*> enums_;
Mark Slee30152872006-11-28 01:24:07 +0000161 std::vector<t_const*> consts_;
Mark Sleef0712dc2006-10-25 19:03:57 +0000162 std::vector<t_struct*> structs_;
163 std::vector<t_struct*> xceptions_;
164 std::vector<t_service*> services_;
165
166 // C++ namespace
167 std::string cpp_namespace_;
168
169 // C++ extra includes
170 std::vector<std::string> cpp_includes_;
171
Mark Sleee888b372007-01-12 01:06:24 +0000172 // PHP namespace
173 std::string php_namespace_;
174
Mark Sleef0712dc2006-10-25 19:03:57 +0000175 // Java package
176 std::string java_package_;
177
Mark Slee0d9199e2007-01-31 02:08:30 +0000178 // XSD namespace
179 std::string xsd_namespace_;
180
Mark Slee31985722006-05-24 21:45:31 +0000181};
182
183#endif