blob: 420242fb34d0676a9da32fdbf144b8dbbe244992 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Mark Sleee9ce01c2007-05-16 02:29:53 +000019
Mark Slee31985722006-05-24 21:45:31 +000020#ifndef T_PROGRAM_H
21#define T_PROGRAM_H
22
Mark Sleee8540632006-05-30 09:24:40 +000023#include <map>
Mark Slee31985722006-05-24 21:45:31 +000024#include <string>
25#include <vector>
26
Mark Sleef0712dc2006-10-25 19:03:57 +000027// For program_name()
28#include "main.h"
29
David Reissc2532a92007-07-30 23:46:11 +000030#include "t_doc.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000031#include "t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000032#include "t_base_type.h"
33#include "t_typedef.h"
34#include "t_enum.h"
Mark Slee30152872006-11-28 01:24:07 +000035#include "t_const.h"
Mark Slee31985722006-05-24 21:45:31 +000036#include "t_struct.h"
37#include "t_service.h"
Mark Sleee8540632006-05-30 09:24:40 +000038#include "t_list.h"
39#include "t_map.h"
40#include "t_set.h"
Bryan Duxburye0ac3ab2010-07-29 16:24:41 +000041#include "generate/t_generator_registry.h"
ccheeverf53b5cf2007-02-05 20:33:11 +000042//#include "t_doc.h"
Mark Slee31985722006-05-24 21:45:31 +000043
44/**
45 * Top level class representing an entire thrift program. A program consists
46 * fundamentally of the following:
47 *
48 * Typedefs
49 * Enumerations
Mark Slee30152872006-11-28 01:24:07 +000050 * Constants
Mark Slee31985722006-05-24 21:45:31 +000051 * Structs
Mark Slee9cb7c612006-09-01 22:17:45 +000052 * Exceptions
Mark Slee31985722006-05-24 21:45:31 +000053 * Services
54 *
Mark Sleef5377b32006-10-10 01:42:59 +000055 * The program module also contains the definitions of the base types.
56 *
Mark Slee31985722006-05-24 21:45:31 +000057 */
David Reissc2532a92007-07-30 23:46:11 +000058class t_program : public t_doc {
Mark Slee31985722006-05-24 21:45:31 +000059 public:
Mark Sleef0712dc2006-10-25 19:03:57 +000060 t_program(std::string path, std::string name) :
Mark Slee2c44d202007-05-16 02:18:07 +000061 path_(path),
dweatherford65b70752007-10-31 02:18:14 +000062 name_(name),
63 out_path_("./") {
Mark Sleef0712dc2006-10-25 19:03:57 +000064 scope_ = new t_scope();
Mark Slee31985722006-05-24 21:45:31 +000065 }
66
Mark Sleef0712dc2006-10-25 19:03:57 +000067 t_program(std::string path) :
dweatherford65b70752007-10-31 02:18:14 +000068 path_(path),
69 out_path_("./") {
Mark Sleef0712dc2006-10-25 19:03:57 +000070 name_ = program_name(path);
71 scope_ = new t_scope();
Mark Slee31985722006-05-24 21:45:31 +000072 }
73
Mark Slee2c44d202007-05-16 02:18:07 +000074 // Path accessor
Mark Sleef0712dc2006-10-25 19:03:57 +000075 const std::string& get_path() const { return path_; }
76
dweatherford65b70752007-10-31 02:18:14 +000077 // Output path accessor
78 const std::string& get_out_path() const { return out_path_; }
79
Mark Slee31985722006-05-24 21:45:31 +000080 // Name accessor
Mark Sleef0712dc2006-10-25 19:03:57 +000081 const std::string& get_name() const { return name_; }
Mark Slee31985722006-05-24 21:45:31 +000082
Mark Slee9cb7c612006-09-01 22:17:45 +000083 // Namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000084 const std::string& get_namespace() const { return namespace_; }
Mark Slee9cb7c612006-09-01 22:17:45 +000085
kholst76f2c882008-01-16 02:47:41 +000086 // Include prefix accessor
87 const std::string& get_include_prefix() const { return include_prefix_; }
88
Mark Slee31985722006-05-24 21:45:31 +000089 // Accessors for program elements
Mark Slee9cb7c612006-09-01 22:17:45 +000090 const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; }
91 const std::vector<t_enum*>& get_enums() const { return enums_; }
Mark Slee30152872006-11-28 01:24:07 +000092 const std::vector<t_const*>& get_consts() const { return consts_; }
Mark Slee9cb7c612006-09-01 22:17:45 +000093 const std::vector<t_struct*>& get_structs() const { return structs_; }
94 const std::vector<t_struct*>& get_xceptions() const { return xceptions_; }
Mark Slee1c4ced72008-01-14 23:04:43 +000095 const std::vector<t_struct*>& get_objects() const { return objects_; }
Mark Slee9cb7c612006-09-01 22:17:45 +000096 const std::vector<t_service*>& get_services() const { return services_; }
Mark Slee31985722006-05-24 21:45:31 +000097
Mark Sleef0712dc2006-10-25 19:03:57 +000098 // Program elements
99 void add_typedef (t_typedef* td) { typedefs_.push_back(td); }
100 void add_enum (t_enum* te) { enums_.push_back(te); }
Mark Slee30152872006-11-28 01:24:07 +0000101 void add_const (t_const* tc) { consts_.push_back(tc); }
Mark Slee1c4ced72008-01-14 23:04:43 +0000102 void add_struct (t_struct* ts) { objects_.push_back(ts);
103 structs_.push_back(ts); }
104 void add_xception (t_struct* tx) { objects_.push_back(tx);
105 xceptions_.push_back(tx); }
Mark Sleef0712dc2006-10-25 19:03:57 +0000106 void add_service (t_service* ts) { services_.push_back(ts); }
Mark Slee31985722006-05-24 21:45:31 +0000107
Mark Sleef0712dc2006-10-25 19:03:57 +0000108 // Programs to include
109 const std::vector<t_program*>& get_includes() const { return includes_; }
Mark Sleee8540632006-05-30 09:24:40 +0000110
dweatherford65b70752007-10-31 02:18:14 +0000111 void set_out_path(std::string out_path) {
112 out_path_ = out_path;
113 // Ensure that it ends with a trailing '/' (or '\' for windows machines)
114 char c = out_path_.at(out_path_.size() - 1);
115 if (!(c == '/' || c == '\\')) {
116 out_path_.push_back('/');
117 }
118 }
119
Mark Sleef0712dc2006-10-25 19:03:57 +0000120 // Scoping and namespacing
Mark Slee9cb7c612006-09-01 22:17:45 +0000121 void set_namespace(std::string name) {
122 namespace_ = name;
123 }
124
Mark Sleef0712dc2006-10-25 19:03:57 +0000125 // Scope accessor
126 t_scope* scope() {
127 return scope_;
Mark Sleee8540632006-05-30 09:24:40 +0000128 }
Mark Sleef5377b32006-10-10 01:42:59 +0000129
Mark Sleef0712dc2006-10-25 19:03:57 +0000130 // Includes
131
kholst76f2c882008-01-16 02:47:41 +0000132 void add_include(std::string path, std::string include_site) {
133 t_program* program = new t_program(path);
134
135 // include prefix for this program is the site at which it was included
136 // (minus the filename)
137 std::string include_prefix;
138 std::string::size_type last_slash = std::string::npos;
139 if ((last_slash = include_site.rfind("/")) != std::string::npos) {
140 include_prefix = include_site.substr(0, last_slash);
141 }
142
143 program->set_include_prefix(include_prefix);
144 includes_.push_back(program);
Mark Sleee8540632006-05-30 09:24:40 +0000145 }
Mark Sleef5377b32006-10-10 01:42:59 +0000146
Mark Sleef0712dc2006-10-25 19:03:57 +0000147 std::vector<t_program*>& get_includes() {
148 return includes_;
Mark Sleee8540632006-05-30 09:24:40 +0000149 }
Mark Sleef5377b32006-10-10 01:42:59 +0000150
kholst76f2c882008-01-16 02:47:41 +0000151 void set_include_prefix(std::string include_prefix) {
152 include_prefix_ = include_prefix;
153
154 // this is intended to be a directory; add a trailing slash if necessary
155 int len = include_prefix_.size();
156 if (len > 0 && include_prefix_[len - 1] != '/') {
157 include_prefix_ += '/';
158 }
159 }
160
David Reiss79eca142008-02-27 01:55:13 +0000161 // Language neutral namespace / packaging
162 void set_namespace(std::string language, std::string name_space) {
Bryan Duxburye0ac3ab2010-07-29 16:24:41 +0000163 t_generator_registry::gen_map_t my_copy = t_generator_registry::get_generator_map();
164
165 t_generator_registry::gen_map_t::iterator it;
166 it=my_copy.find(language);
167
168 if (it == my_copy.end()) {
169 throw "No generator named '" + language + "' could be found!";
170 }
David Reiss79eca142008-02-27 01:55:13 +0000171 namespaces_[language] = name_space;
172 }
173
174 std::string get_namespace(std::string language) const {
175 std::map<std::string, std::string>::const_iterator iter = namespaces_.find(language);
176 if (iter == namespaces_.end()) {
177 return std::string();
178 }
179 return iter->second;
180 }
181
Mark Sleef0712dc2006-10-25 19:03:57 +0000182 // Language specific namespace / packaging
183
Mark Sleef0712dc2006-10-25 19:03:57 +0000184 void add_cpp_include(std::string path) {
185 cpp_includes_.push_back(path);
186 }
187
188 const std::vector<std::string>& get_cpp_includes() {
189 return cpp_includes_;
190 }
191
Mark Slee31985722006-05-24 21:45:31 +0000192 private:
Mark Sleef5377b32006-10-10 01:42:59 +0000193
Mark Sleef0712dc2006-10-25 19:03:57 +0000194 // File path
195 std::string path_;
Mark Slee9cb7c612006-09-01 22:17:45 +0000196
Mark Slee31985722006-05-24 21:45:31 +0000197 // Name
198 std::string name_;
199
dweatherford65b70752007-10-31 02:18:14 +0000200 // Output directory
201 std::string out_path_;
202
Mark Slee9cb7c612006-09-01 22:17:45 +0000203 // Namespace
204 std::string namespace_;
205
Mark Sleef0712dc2006-10-25 19:03:57 +0000206 // Included programs
207 std::vector<t_program*> includes_;
Mark Sleee8540632006-05-30 09:24:40 +0000208
kholst76f2c882008-01-16 02:47:41 +0000209 // Include prefix for this program, if any
210 std::string include_prefix_;
211
Mark Sleef0712dc2006-10-25 19:03:57 +0000212 // Identifier lookup scope
213 t_scope* scope_;
Mark Sleee8540632006-05-30 09:24:40 +0000214
Mark Sleef0712dc2006-10-25 19:03:57 +0000215 // Components to generate code for
216 std::vector<t_typedef*> typedefs_;
217 std::vector<t_enum*> enums_;
Mark Slee30152872006-11-28 01:24:07 +0000218 std::vector<t_const*> consts_;
Mark Slee1c4ced72008-01-14 23:04:43 +0000219 std::vector<t_struct*> objects_;
Mark Sleef0712dc2006-10-25 19:03:57 +0000220 std::vector<t_struct*> structs_;
221 std::vector<t_struct*> xceptions_;
222 std::vector<t_service*> services_;
223
David Reiss79eca142008-02-27 01:55:13 +0000224 // Dynamic namespaces
225 std::map<std::string, std::string> namespaces_;
226
Mark Sleef0712dc2006-10-25 19:03:57 +0000227 // C++ extra includes
228 std::vector<std::string> cpp_includes_;
229
Mark Slee31985722006-05-24 21:45:31 +0000230};
231
232#endif