blob: 96a8a5cc379cae0228cc8fb8caf0eb559ecf7471 [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),
Bryan Duxburybdca9f62011-03-01 19:53:07 +000063 out_path_("./"),
64 out_path_is_absolute_(false) {
Mark Sleef0712dc2006-10-25 19:03:57 +000065 scope_ = new t_scope();
Mark Slee31985722006-05-24 21:45:31 +000066 }
67
Mark Sleef0712dc2006-10-25 19:03:57 +000068 t_program(std::string path) :
dweatherford65b70752007-10-31 02:18:14 +000069 path_(path),
Bryan Duxburybdca9f62011-03-01 19:53:07 +000070 out_path_("./"),
71 out_path_is_absolute_(false) {
Mark Sleef0712dc2006-10-25 19:03:57 +000072 name_ = program_name(path);
73 scope_ = new t_scope();
Mark Slee31985722006-05-24 21:45:31 +000074 }
75
Jens Geyer09b97c72013-08-04 13:39:09 +020076 ~t_program()
77 {
78 if(scope_)
79 {
80 delete scope_;
81 scope_ = NULL;
82 }
83 }
84
Mark Slee2c44d202007-05-16 02:18:07 +000085 // Path accessor
Mark Sleef0712dc2006-10-25 19:03:57 +000086 const std::string& get_path() const { return path_; }
87
dweatherford65b70752007-10-31 02:18:14 +000088 // Output path accessor
89 const std::string& get_out_path() const { return out_path_; }
90
Bryan Duxburybdca9f62011-03-01 19:53:07 +000091 // Create gen-* dir accessor
Jake Farrelld1fb1802013-03-23 21:35:03 -040092 const bool is_out_path_absolute() const { return out_path_is_absolute_; }
Bryan Duxburybdca9f62011-03-01 19:53:07 +000093
Mark Slee31985722006-05-24 21:45:31 +000094 // Name accessor
Mark Sleef0712dc2006-10-25 19:03:57 +000095 const std::string& get_name() const { return name_; }
Mark Slee31985722006-05-24 21:45:31 +000096
Mark Slee9cb7c612006-09-01 22:17:45 +000097 // Namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000098 const std::string& get_namespace() const { return namespace_; }
Mark Slee9cb7c612006-09-01 22:17:45 +000099
kholst76f2c882008-01-16 02:47:41 +0000100 // Include prefix accessor
101 const std::string& get_include_prefix() const { return include_prefix_; }
102
Mark Slee31985722006-05-24 21:45:31 +0000103 // Accessors for program elements
Mark Slee9cb7c612006-09-01 22:17:45 +0000104 const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; }
105 const std::vector<t_enum*>& get_enums() const { return enums_; }
Mark Slee30152872006-11-28 01:24:07 +0000106 const std::vector<t_const*>& get_consts() const { return consts_; }
Mark Slee9cb7c612006-09-01 22:17:45 +0000107 const std::vector<t_struct*>& get_structs() const { return structs_; }
108 const std::vector<t_struct*>& get_xceptions() const { return xceptions_; }
Mark Slee1c4ced72008-01-14 23:04:43 +0000109 const std::vector<t_struct*>& get_objects() const { return objects_; }
Mark Slee9cb7c612006-09-01 22:17:45 +0000110 const std::vector<t_service*>& get_services() const { return services_; }
Mark Slee31985722006-05-24 21:45:31 +0000111
Mark Sleef0712dc2006-10-25 19:03:57 +0000112 // Program elements
113 void add_typedef (t_typedef* td) { typedefs_.push_back(td); }
114 void add_enum (t_enum* te) { enums_.push_back(te); }
Mark Slee30152872006-11-28 01:24:07 +0000115 void add_const (t_const* tc) { consts_.push_back(tc); }
Mark Slee1c4ced72008-01-14 23:04:43 +0000116 void add_struct (t_struct* ts) { objects_.push_back(ts);
117 structs_.push_back(ts); }
118 void add_xception (t_struct* tx) { objects_.push_back(tx);
119 xceptions_.push_back(tx); }
Mark Sleef0712dc2006-10-25 19:03:57 +0000120 void add_service (t_service* ts) { services_.push_back(ts); }
Mark Slee31985722006-05-24 21:45:31 +0000121
Mark Sleef0712dc2006-10-25 19:03:57 +0000122 // Programs to include
123 const std::vector<t_program*>& get_includes() const { return includes_; }
Mark Sleee8540632006-05-30 09:24:40 +0000124
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000125 void set_out_path(std::string out_path, bool out_path_is_absolute) {
dweatherford65b70752007-10-31 02:18:14 +0000126 out_path_ = out_path;
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000127 out_path_is_absolute_ = out_path_is_absolute;
dweatherford65b70752007-10-31 02:18:14 +0000128 // Ensure that it ends with a trailing '/' (or '\' for windows machines)
129 char c = out_path_.at(out_path_.size() - 1);
130 if (!(c == '/' || c == '\\')) {
131 out_path_.push_back('/');
132 }
133 }
134
Roger Meierba406d32013-07-15 22:41:34 +0200135 // Typename collision detection
136 /**
137 * Search for typename collisions
138 * @param t the type to test for collisions
139 * @return true if a certain collision was found, otherwise false
140 */
141 bool is_unique_typename(t_type * t) {
142 int occurances = program_typename_count(this, t);
143 for (std::vector<t_program*>::iterator it = includes_.begin();
144 it != includes_.end(); ++it) {
145 occurances += program_typename_count(*it, t);
146 }
147 return 0 == occurances;
148 }
149
150 /**
151 * Search all type collections for duplicate typenames
152 * @param prog the program to search
153 * @param t the type to test for collisions
154 * @return the number of certain typename collisions
155 */
156 int program_typename_count(t_program * prog, t_type * t) {
157 int occurances = 0;
158 occurances += collection_typename_count(prog, prog->typedefs_, t);
159 occurances += collection_typename_count(prog, prog->enums_, t);
160 occurances += collection_typename_count(prog, prog->objects_, t);
161 occurances += collection_typename_count(prog, prog->services_, t);
162 return occurances;
163 }
164
165 /**
166 * Search a type collection for duplicate typenames
167 * @param prog the program to search
168 * @param type_collection the type collection to search
169 * @param t the type to test for collisions
170 * @return the number of certain typename collisions
171 */
172 template <class T>
173 int collection_typename_count(t_program * prog, T type_collection, t_type * t) {
174 int occurances = 0;
175 for (typename T::iterator it = type_collection.begin(); it != type_collection.end(); ++it)
176 if (t != *it && 0 == t->get_name().compare((*it)->get_name()) && is_common_namespace(prog, t))
177 ++occurances;
178 return occurances;
179 }
180
181 /**
182 * Determine whether identical typenames will collide based on namespaces.
183 *
184 * Because we do not know which languages the user will generate code for,
185 * collisions within programs (IDL files) having namespace declarations can be
186 * difficult to determine. Only guaranteed collisions return true (cause an error).
187 * Possible collisions involving explicit namespace declarations produce a warning.
188 * Other possible collisions go unreported.
189 * @param prog the program containing the preexisting typename
190 * @param t the type containing the typename match
191 * @return true if a collision within namespaces is found, otherwise false
192 */
193 bool is_common_namespace(t_program * prog, t_type * t) {
194 //Case 1: Typenames are in the same program [collision]
195 if (prog == t->get_program()) {
196 pwarning(1, "Duplicate typename %s found in %s",
197 t->get_name().c_str(), t->get_program()->get_name().c_str());
198 return true;
199 }
200
201 //Case 2: Both programs have identical namespace scope/name declarations [collision]
202 bool match = true;
203 for (std::map<std::string, std::string>::iterator it = prog->namespaces_.begin();
204 it != prog->namespaces_.end(); ++it) {
205 if (0 == it->second.compare(t->get_program()->get_namespace(it->first))) {
206 pwarning(1, "Duplicate typename %s found in %s,%s,%s and %s,%s,%s [file,scope,ns]",
207 t->get_name().c_str(),
208 t->get_program()->get_name().c_str(), it->first.c_str(), it->second.c_str(),
209 prog->get_name().c_str(), it->first.c_str(), it->second.c_str());
210 } else {
211 match = false;
212 }
213 }
214 for (std::map<std::string, std::string>::iterator it = t->get_program()->namespaces_.begin();
215 it != t->get_program()->namespaces_.end(); ++it) {
216 if (0 == it->second.compare(prog->get_namespace(it->first))) {
217 pwarning(1, "Duplicate typename %s found in %s,%s,%s and %s,%s,%s [file,scope,ns]",
218 t->get_name().c_str(),
219 t->get_program()->get_name().c_str(), it->first.c_str(), it->second.c_str(),
220 prog->get_name().c_str(), it->first.c_str(), it->second.c_str());
221 } else {
222 match = false;
223 }
224 }
225 if (0 == prog->namespaces_.size() && 0 == t->get_program()->namespaces_.size()) {
226 pwarning(1, "Duplicate typename %s found in %s and %s",
227 t->get_name().c_str(), t->get_program()->get_name().c_str(), prog->get_name().c_str());
228 }
229 return match;
230 }
231
Mark Sleef0712dc2006-10-25 19:03:57 +0000232 // Scoping and namespacing
Mark Slee9cb7c612006-09-01 22:17:45 +0000233 void set_namespace(std::string name) {
234 namespace_ = name;
235 }
236
Mark Sleef0712dc2006-10-25 19:03:57 +0000237 // Scope accessor
238 t_scope* scope() {
239 return scope_;
Mark Sleee8540632006-05-30 09:24:40 +0000240 }
Mark Sleef5377b32006-10-10 01:42:59 +0000241
Mark Sleef0712dc2006-10-25 19:03:57 +0000242 // Includes
243
kholst76f2c882008-01-16 02:47:41 +0000244 void add_include(std::string path, std::string include_site) {
245 t_program* program = new t_program(path);
246
247 // include prefix for this program is the site at which it was included
248 // (minus the filename)
249 std::string include_prefix;
250 std::string::size_type last_slash = std::string::npos;
251 if ((last_slash = include_site.rfind("/")) != std::string::npos) {
252 include_prefix = include_site.substr(0, last_slash);
253 }
254
255 program->set_include_prefix(include_prefix);
256 includes_.push_back(program);
Mark Sleee8540632006-05-30 09:24:40 +0000257 }
Mark Sleef5377b32006-10-10 01:42:59 +0000258
Mark Sleef0712dc2006-10-25 19:03:57 +0000259 std::vector<t_program*>& get_includes() {
260 return includes_;
Mark Sleee8540632006-05-30 09:24:40 +0000261 }
Mark Sleef5377b32006-10-10 01:42:59 +0000262
kholst76f2c882008-01-16 02:47:41 +0000263 void set_include_prefix(std::string include_prefix) {
264 include_prefix_ = include_prefix;
265
266 // this is intended to be a directory; add a trailing slash if necessary
267 int len = include_prefix_.size();
268 if (len > 0 && include_prefix_[len - 1] != '/') {
269 include_prefix_ += '/';
270 }
271 }
272
David Reiss79eca142008-02-27 01:55:13 +0000273 // Language neutral namespace / packaging
274 void set_namespace(std::string language, std::string name_space) {
Bryan Duxburybbff4a82010-09-03 20:36:02 +0000275 if (language != "*") {
276 size_t sub_index = language.find('.');
277 std::string base_language = language.substr(0, sub_index);
278 std::string sub_namespace;
Bryan Duxbury681f5ea2010-08-20 16:42:04 +0000279
Bryan Duxburybbff4a82010-09-03 20:36:02 +0000280 if(base_language == "smalltalk") {
281 pwarning(1, "Namespace 'smalltalk' is deprecated. Use 'st' instead");
282 base_language = "st";
283 }
Bryan Duxbury4d8a9cd2010-08-30 17:09:58 +0000284
Bryan Duxburybbff4a82010-09-03 20:36:02 +0000285 t_generator_registry::gen_map_t my_copy = t_generator_registry::get_generator_map();
Bryan Duxburye0ac3ab2010-07-29 16:24:41 +0000286
Bryan Duxburybbff4a82010-09-03 20:36:02 +0000287 t_generator_registry::gen_map_t::iterator it;
288 it=my_copy.find(base_language);
Bryan Duxburye0ac3ab2010-07-29 16:24:41 +0000289
Bryan Duxburybbff4a82010-09-03 20:36:02 +0000290 if (it == my_copy.end()) {
Henrique Mendoncaffb031d2012-09-24 18:36:16 +0000291 std::string warning = "No generator named '" + base_language + "' could be found!";
292 pwarning(1, warning.c_str());
293 } else {
294 if (sub_index != std::string::npos) {
295 std::string sub_namespace = language.substr(sub_index+1);
296 if ( ! it->second->is_valid_namespace(sub_namespace)) {
Henrique Mendonça8ad13a32013-05-16 21:26:20 +0200297 std::string warning = base_language + " generator does not accept '" + sub_namespace + "' as sub-namespace!";
298 pwarning(1, warning.c_str());
Henrique Mendoncaffb031d2012-09-24 18:36:16 +0000299 }
Bryan Duxburybbff4a82010-09-03 20:36:02 +0000300 }
Bryan Duxbury681f5ea2010-08-20 16:42:04 +0000301 }
David Reisse3ba3492010-08-26 21:49:45 +0000302 }
Bryan Duxbury681f5ea2010-08-20 16:42:04 +0000303
David Reiss79eca142008-02-27 01:55:13 +0000304 namespaces_[language] = name_space;
305 }
306
307 std::string get_namespace(std::string language) const {
David Reissfb790d72010-09-02 16:41:45 +0000308 std::map<std::string, std::string>::const_iterator iter;
309 if ((iter = namespaces_.find(language)) != namespaces_.end() ||
310 (iter = namespaces_.find("*" )) != namespaces_.end()) {
311 return iter->second;
David Reiss79eca142008-02-27 01:55:13 +0000312 }
David Reissfb790d72010-09-02 16:41:45 +0000313 return std::string();
David Reiss79eca142008-02-27 01:55:13 +0000314 }
315
Mark Sleef0712dc2006-10-25 19:03:57 +0000316 // Language specific namespace / packaging
317
Mark Sleef0712dc2006-10-25 19:03:57 +0000318 void add_cpp_include(std::string path) {
319 cpp_includes_.push_back(path);
320 }
321
322 const std::vector<std::string>& get_cpp_includes() {
323 return cpp_includes_;
324 }
325
Roger Meier213a6642010-10-27 12:30:11 +0000326 void add_c_include(std::string path) {
327 c_includes_.push_back(path);
328 }
329
330 const std::vector<std::string>& get_c_includes() {
331 return c_includes_;
332 }
333
Mark Slee31985722006-05-24 21:45:31 +0000334 private:
Mark Sleef5377b32006-10-10 01:42:59 +0000335
Mark Sleef0712dc2006-10-25 19:03:57 +0000336 // File path
337 std::string path_;
Mark Slee9cb7c612006-09-01 22:17:45 +0000338
Mark Slee31985722006-05-24 21:45:31 +0000339 // Name
340 std::string name_;
341
dweatherford65b70752007-10-31 02:18:14 +0000342 // Output directory
343 std::string out_path_;
344
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000345 // Output directory is absolute location for generated source (no gen-*)
346 bool out_path_is_absolute_;
347
Mark Slee9cb7c612006-09-01 22:17:45 +0000348 // Namespace
349 std::string namespace_;
350
Mark Sleef0712dc2006-10-25 19:03:57 +0000351 // Included programs
352 std::vector<t_program*> includes_;
Mark Sleee8540632006-05-30 09:24:40 +0000353
kholst76f2c882008-01-16 02:47:41 +0000354 // Include prefix for this program, if any
355 std::string include_prefix_;
356
Mark Sleef0712dc2006-10-25 19:03:57 +0000357 // Identifier lookup scope
358 t_scope* scope_;
Mark Sleee8540632006-05-30 09:24:40 +0000359
Mark Sleef0712dc2006-10-25 19:03:57 +0000360 // Components to generate code for
361 std::vector<t_typedef*> typedefs_;
362 std::vector<t_enum*> enums_;
Mark Slee30152872006-11-28 01:24:07 +0000363 std::vector<t_const*> consts_;
Mark Slee1c4ced72008-01-14 23:04:43 +0000364 std::vector<t_struct*> objects_;
Mark Sleef0712dc2006-10-25 19:03:57 +0000365 std::vector<t_struct*> structs_;
366 std::vector<t_struct*> xceptions_;
367 std::vector<t_service*> services_;
368
David Reiss79eca142008-02-27 01:55:13 +0000369 // Dynamic namespaces
370 std::map<std::string, std::string> namespaces_;
371
Mark Sleef0712dc2006-10-25 19:03:57 +0000372 // C++ extra includes
373 std::vector<std::string> cpp_includes_;
374
Roger Meier213a6642010-10-27 12:30:11 +0000375 // C extra includes
376 std::vector<std::string> c_includes_;
377
Mark Slee31985722006-05-24 21:45:31 +0000378};
379
380#endif