blob: f39852d984952a0e3841db67fc3b8f2c7d2ef60b [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
17#include "t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000018#include "t_base_type.h"
19#include "t_typedef.h"
20#include "t_enum.h"
Mark Slee30152872006-11-28 01:24:07 +000021#include "t_const.h"
Mark Slee31985722006-05-24 21:45:31 +000022#include "t_struct.h"
23#include "t_service.h"
Mark Sleee8540632006-05-30 09:24:40 +000024#include "t_list.h"
25#include "t_map.h"
26#include "t_set.h"
ccheeverf53b5cf2007-02-05 20:33:11 +000027//#include "t_doc.h"
Mark Slee31985722006-05-24 21:45:31 +000028
29/**
30 * Top level class representing an entire thrift program. A program consists
31 * fundamentally of the following:
32 *
33 * Typedefs
34 * Enumerations
Mark Slee30152872006-11-28 01:24:07 +000035 * Constants
Mark Slee31985722006-05-24 21:45:31 +000036 * Structs
Mark Slee9cb7c612006-09-01 22:17:45 +000037 * Exceptions
Mark Slee31985722006-05-24 21:45:31 +000038 * Services
39 *
Mark Sleef5377b32006-10-10 01:42:59 +000040 * The program module also contains the definitions of the base types.
41 *
Mark Slee31985722006-05-24 21:45:31 +000042 * @author Mark Slee <mcslee@facebook.com>
43 */
44class t_program {
45 public:
Mark Sleef0712dc2006-10-25 19:03:57 +000046 t_program(std::string path, std::string name) :
Mark Slee2c44d202007-05-16 02:18:07 +000047 path_(path),
Mark Sleef0712dc2006-10-25 19:03:57 +000048 name_(name) {
49 scope_ = new t_scope();
Mark Slee31985722006-05-24 21:45:31 +000050 }
51
Mark Sleef0712dc2006-10-25 19:03:57 +000052 t_program(std::string path) :
53 path_(path) {
54 name_ = program_name(path);
55 scope_ = new t_scope();
Mark Slee31985722006-05-24 21:45:31 +000056 }
57
Mark Slee2c44d202007-05-16 02:18:07 +000058 // Path accessor
Mark Sleef0712dc2006-10-25 19:03:57 +000059 const std::string& get_path() const { return path_; }
60
Mark Slee31985722006-05-24 21:45:31 +000061 // Name accessor
Mark Sleef0712dc2006-10-25 19:03:57 +000062 const std::string& get_name() const { return name_; }
Mark Slee31985722006-05-24 21:45:31 +000063
Mark Slee9cb7c612006-09-01 22:17:45 +000064 // Namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000065 const std::string& get_namespace() const { return namespace_; }
Mark Slee9cb7c612006-09-01 22:17:45 +000066
Mark Slee31985722006-05-24 21:45:31 +000067 // Accessors for program elements
Mark Slee9cb7c612006-09-01 22:17:45 +000068 const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; }
69 const std::vector<t_enum*>& get_enums() const { return enums_; }
Mark Slee30152872006-11-28 01:24:07 +000070 const std::vector<t_const*>& get_consts() const { return consts_; }
Mark Slee9cb7c612006-09-01 22:17:45 +000071 const std::vector<t_struct*>& get_structs() const { return structs_; }
72 const std::vector<t_struct*>& get_xceptions() const { return xceptions_; }
73 const std::vector<t_service*>& get_services() const { return services_; }
Mark Slee31985722006-05-24 21:45:31 +000074
Mark Sleef0712dc2006-10-25 19:03:57 +000075 // Program elements
76 void add_typedef (t_typedef* td) { typedefs_.push_back(td); }
77 void add_enum (t_enum* te) { enums_.push_back(te); }
Mark Slee30152872006-11-28 01:24:07 +000078 void add_const (t_const* tc) { consts_.push_back(tc); }
Mark Sleef0712dc2006-10-25 19:03:57 +000079 void add_struct (t_struct* ts) { structs_.push_back(ts); }
80 void add_xception (t_struct* tx) { xceptions_.push_back(tx); }
81 void add_service (t_service* ts) { services_.push_back(ts); }
Mark Slee31985722006-05-24 21:45:31 +000082
Mark Sleef0712dc2006-10-25 19:03:57 +000083 // Programs to include
84 const std::vector<t_program*>& get_includes() const { return includes_; }
Mark Sleee8540632006-05-30 09:24:40 +000085
Mark Sleef0712dc2006-10-25 19:03:57 +000086 // Scoping and namespacing
Mark Slee9cb7c612006-09-01 22:17:45 +000087 void set_namespace(std::string name) {
88 namespace_ = name;
89 }
90
Mark Sleef0712dc2006-10-25 19:03:57 +000091 // Scope accessor
92 t_scope* scope() {
93 return scope_;
Mark Sleee8540632006-05-30 09:24:40 +000094 }
Mark Sleef5377b32006-10-10 01:42:59 +000095
Mark Sleef0712dc2006-10-25 19:03:57 +000096 // Includes
97
98 void add_include(std::string path) {
99 includes_.push_back(new t_program(path));
Mark Sleee8540632006-05-30 09:24:40 +0000100 }
Mark Sleef5377b32006-10-10 01:42:59 +0000101
Mark Sleef0712dc2006-10-25 19:03:57 +0000102 std::vector<t_program*>& get_includes() {
103 return includes_;
Mark Sleee8540632006-05-30 09:24:40 +0000104 }
Mark Sleef5377b32006-10-10 01:42:59 +0000105
Mark Sleef0712dc2006-10-25 19:03:57 +0000106 // Language specific namespace / packaging
107
108 void set_cpp_namespace(std::string cpp_namespace) {
109 cpp_namespace_ = cpp_namespace;
Mark Slee9cb7c612006-09-01 22:17:45 +0000110 }
Mark Sleef5377b32006-10-10 01:42:59 +0000111
Mark Sleef0712dc2006-10-25 19:03:57 +0000112 const std::string& get_cpp_namespace() const {
113 return cpp_namespace_;
Mark Sleee8540632006-05-30 09:24:40 +0000114 }
115
Mark Sleef0712dc2006-10-25 19:03:57 +0000116 void add_cpp_include(std::string path) {
117 cpp_includes_.push_back(path);
118 }
119
120 const std::vector<std::string>& get_cpp_includes() {
121 return cpp_includes_;
122 }
123
Mark Sleee888b372007-01-12 01:06:24 +0000124 void set_php_namespace(std::string php_namespace) {
125 php_namespace_ = php_namespace;
126 }
127
128 const std::string& get_php_namespace() const {
129 return php_namespace_;
130 }
131
Mark Sleef0712dc2006-10-25 19:03:57 +0000132 void set_java_package(std::string java_package) {
133 java_package_ = java_package;
134 }
135
136 const std::string& get_java_package() const {
137 return java_package_;
138 }
139
Mark Slee0d9199e2007-01-31 02:08:30 +0000140 void set_xsd_namespace(std::string xsd_namespace) {
141 xsd_namespace_ = xsd_namespace;
142 }
143
144 const std::string& get_xsd_namespace() const {
145 return xsd_namespace_;
146 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000147
Mark Slee2c44d202007-05-16 02:18:07 +0000148 void set_perl_namespace(std::string perl_namespace) {
149 perl_namespace_ = perl_namespace;
150 }
151
152 const std::string& get_perl_namespace() const {
153 return perl_namespace_;
154 }
155
Mark Slee31985722006-05-24 21:45:31 +0000156 private:
Mark Sleef5377b32006-10-10 01:42:59 +0000157
Mark Sleef0712dc2006-10-25 19:03:57 +0000158 // File path
159 std::string path_;
Mark Slee9cb7c612006-09-01 22:17:45 +0000160
Mark Slee31985722006-05-24 21:45:31 +0000161 // Name
162 std::string name_;
163
Mark Slee9cb7c612006-09-01 22:17:45 +0000164 // Namespace
165 std::string namespace_;
166
Mark Sleef0712dc2006-10-25 19:03:57 +0000167 // Included programs
168 std::vector<t_program*> includes_;
Mark Sleee8540632006-05-30 09:24:40 +0000169
Mark Sleef0712dc2006-10-25 19:03:57 +0000170 // Identifier lookup scope
171 t_scope* scope_;
Mark Sleee8540632006-05-30 09:24:40 +0000172
Mark Sleef0712dc2006-10-25 19:03:57 +0000173 // Components to generate code for
174 std::vector<t_typedef*> typedefs_;
175 std::vector<t_enum*> enums_;
Mark Slee30152872006-11-28 01:24:07 +0000176 std::vector<t_const*> consts_;
Mark Sleef0712dc2006-10-25 19:03:57 +0000177 std::vector<t_struct*> structs_;
178 std::vector<t_struct*> xceptions_;
179 std::vector<t_service*> services_;
180
181 // C++ namespace
182 std::string cpp_namespace_;
183
184 // C++ extra includes
185 std::vector<std::string> cpp_includes_;
186
Mark Sleee888b372007-01-12 01:06:24 +0000187 // PHP namespace
188 std::string php_namespace_;
189
Mark Sleef0712dc2006-10-25 19:03:57 +0000190 // Java package
191 std::string java_package_;
192
Mark Slee0d9199e2007-01-31 02:08:30 +0000193 // XSD namespace
194 std::string xsd_namespace_;
195
Mark Slee2c44d202007-05-16 02:18:07 +0000196 // Perl namespace
197 std::string perl_namespace_;
198
Mark Slee31985722006-05-24 21:45:31 +0000199};
200
201#endif