blob: 51157aaebe3bb1da85edaa678becfac7f7d28f3b [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 {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010059public:
60 t_program(std::string path, std::string name)
61 : path_(path), name_(name), out_path_("./"), out_path_is_absolute_(false) {
Mark Sleef0712dc2006-10-25 19:03:57 +000062 scope_ = new t_scope();
Mark Slee31985722006-05-24 21:45:31 +000063 }
64
Konrad Grochowski16a23a62014-11-13 15:33:38 +010065 t_program(std::string path) : path_(path), out_path_("./"), out_path_is_absolute_(false) {
Mark Sleef0712dc2006-10-25 19:03:57 +000066 name_ = program_name(path);
67 scope_ = new t_scope();
Mark Slee31985722006-05-24 21:45:31 +000068 }
69
Konrad Grochowski16a23a62014-11-13 15:33:38 +010070 ~t_program() {
71 if (scope_) {
72 delete scope_;
73 scope_ = NULL;
74 }
Roger Meier4f4b15b2014-11-05 16:51:04 +010075 }
Jens Geyer09b97c72013-08-04 13:39:09 +020076
Mark Slee2c44d202007-05-16 02:18:07 +000077 // Path accessor
Mark Sleef0712dc2006-10-25 19:03:57 +000078 const std::string& get_path() const { return path_; }
79
dweatherford65b70752007-10-31 02:18:14 +000080 // Output path accessor
81 const std::string& get_out_path() const { return out_path_; }
82
Bryan Duxburybdca9f62011-03-01 19:53:07 +000083 // Create gen-* dir accessor
Roger Meiera70986f2014-10-21 23:01:36 +020084 bool is_out_path_absolute() const { return out_path_is_absolute_; }
Bryan Duxburybdca9f62011-03-01 19:53:07 +000085
Mark Slee31985722006-05-24 21:45:31 +000086 // Name accessor
Mark Sleef0712dc2006-10-25 19:03:57 +000087 const std::string& get_name() const { return name_; }
Mark Slee31985722006-05-24 21:45:31 +000088
Mark Slee9cb7c612006-09-01 22:17:45 +000089 // Namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000090 const std::string& get_namespace() const { return namespace_; }
Mark Slee9cb7c612006-09-01 22:17:45 +000091
kholst76f2c882008-01-16 02:47:41 +000092 // Include prefix accessor
93 const std::string& get_include_prefix() const { return include_prefix_; }
94
Mark Slee31985722006-05-24 21:45:31 +000095 // Accessors for program elements
Konrad Grochowski16a23a62014-11-13 15:33:38 +010096 const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; }
97 const std::vector<t_enum*>& get_enums() const { return enums_; }
98 const std::vector<t_const*>& get_consts() const { return consts_; }
99 const std::vector<t_struct*>& get_structs() const { return structs_; }
100 const std::vector<t_struct*>& get_xceptions() const { return xceptions_; }
101 const std::vector<t_struct*>& get_objects() const { return objects_; }
102 const std::vector<t_service*>& get_services() const { return services_; }
Mark Slee31985722006-05-24 21:45:31 +0000103
Mark Sleef0712dc2006-10-25 19:03:57 +0000104 // Program elements
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100105 void add_typedef(t_typedef* td) { typedefs_.push_back(td); }
106 void add_enum(t_enum* te) { enums_.push_back(te); }
107 void add_const(t_const* tc) { consts_.push_back(tc); }
108 void add_struct(t_struct* ts) {
109 objects_.push_back(ts);
110 structs_.push_back(ts);
111 }
112 void add_xception(t_struct* tx) {
113 objects_.push_back(tx);
114 xceptions_.push_back(tx);
115 }
116 void add_service(t_service* ts) { services_.push_back(ts); }
Mark Slee31985722006-05-24 21:45:31 +0000117
Mark Sleef0712dc2006-10-25 19:03:57 +0000118 // Programs to include
119 const std::vector<t_program*>& get_includes() const { return includes_; }
Mark Sleee8540632006-05-30 09:24:40 +0000120
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000121 void set_out_path(std::string out_path, bool out_path_is_absolute) {
dweatherford65b70752007-10-31 02:18:14 +0000122 out_path_ = out_path;
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000123 out_path_is_absolute_ = out_path_is_absolute;
dweatherford65b70752007-10-31 02:18:14 +0000124 // Ensure that it ends with a trailing '/' (or '\' for windows machines)
125 char c = out_path_.at(out_path_.size() - 1);
126 if (!(c == '/' || c == '\\')) {
127 out_path_.push_back('/');
128 }
129 }
130
Roger Meierba406d32013-07-15 22:41:34 +0200131 // Typename collision detection
132 /**
133 * Search for typename collisions
134 * @param t the type to test for collisions
135 * @return true if a certain collision was found, otherwise false
136 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100137 bool is_unique_typename(t_type* t) {
Roger Meierba406d32013-07-15 22:41:34 +0200138 int occurances = program_typename_count(this, t);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100139 for (std::vector<t_program*>::iterator it = includes_.begin(); it != includes_.end(); ++it) {
Roger Meierba406d32013-07-15 22:41:34 +0200140 occurances += program_typename_count(*it, t);
141 }
142 return 0 == occurances;
143 }
144
145 /**
146 * Search all type collections for duplicate typenames
147 * @param prog the program to search
148 * @param t the type to test for collisions
149 * @return the number of certain typename collisions
150 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100151 int program_typename_count(t_program* prog, t_type* t) {
Roger Meierba406d32013-07-15 22:41:34 +0200152 int occurances = 0;
153 occurances += collection_typename_count(prog, prog->typedefs_, t);
154 occurances += collection_typename_count(prog, prog->enums_, t);
155 occurances += collection_typename_count(prog, prog->objects_, t);
156 occurances += collection_typename_count(prog, prog->services_, t);
157 return occurances;
158 }
159
160 /**
161 * Search a type collection for duplicate typenames
162 * @param prog the program to search
163 * @param type_collection the type collection to search
164 * @param t the type to test for collisions
165 * @return the number of certain typename collisions
166 */
167 template <class T>
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100168 int collection_typename_count(t_program* prog, T type_collection, t_type* t) {
Roger Meierba406d32013-07-15 22:41:34 +0200169 int occurances = 0;
170 for (typename T::iterator it = type_collection.begin(); it != type_collection.end(); ++it)
171 if (t != *it && 0 == t->get_name().compare((*it)->get_name()) && is_common_namespace(prog, t))
172 ++occurances;
173 return occurances;
174 }
175
176 /**
177 * Determine whether identical typenames will collide based on namespaces.
178 *
179 * Because we do not know which languages the user will generate code for,
180 * collisions within programs (IDL files) having namespace declarations can be
181 * difficult to determine. Only guaranteed collisions return true (cause an error).
182 * Possible collisions involving explicit namespace declarations produce a warning.
183 * Other possible collisions go unreported.
184 * @param prog the program containing the preexisting typename
185 * @param t the type containing the typename match
186 * @return true if a collision within namespaces is found, otherwise false
187 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100188 bool is_common_namespace(t_program* prog, t_type* t) {
189 // Case 1: Typenames are in the same program [collision]
Roger Meierba406d32013-07-15 22:41:34 +0200190 if (prog == t->get_program()) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100191 pwarning(1,
192 "Duplicate typename %s found in %s",
193 t->get_name().c_str(),
194 t->get_program()->get_name().c_str());
Roger Meierba406d32013-07-15 22:41:34 +0200195 return true;
196 }
197
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100198 // Case 2: Both programs have identical namespace scope/name declarations [collision]
Roger Meierba406d32013-07-15 22:41:34 +0200199 bool match = true;
200 for (std::map<std::string, std::string>::iterator it = prog->namespaces_.begin();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100201 it != prog->namespaces_.end();
202 ++it) {
Roger Meierba406d32013-07-15 22:41:34 +0200203 if (0 == it->second.compare(t->get_program()->get_namespace(it->first))) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100204 pwarning(1,
205 "Duplicate typename %s found in %s,%s,%s and %s,%s,%s [file,scope,ns]",
Roger Meierba406d32013-07-15 22:41:34 +0200206 t->get_name().c_str(),
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100207 t->get_program()->get_name().c_str(),
208 it->first.c_str(),
209 it->second.c_str(),
210 prog->get_name().c_str(),
211 it->first.c_str(),
212 it->second.c_str());
Roger Meierba406d32013-07-15 22:41:34 +0200213 } else {
214 match = false;
215 }
216 }
217 for (std::map<std::string, std::string>::iterator it = t->get_program()->namespaces_.begin();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100218 it != t->get_program()->namespaces_.end();
219 ++it) {
Roger Meierba406d32013-07-15 22:41:34 +0200220 if (0 == it->second.compare(prog->get_namespace(it->first))) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100221 pwarning(1,
222 "Duplicate typename %s found in %s,%s,%s and %s,%s,%s [file,scope,ns]",
Roger Meierba406d32013-07-15 22:41:34 +0200223 t->get_name().c_str(),
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100224 t->get_program()->get_name().c_str(),
225 it->first.c_str(),
226 it->second.c_str(),
227 prog->get_name().c_str(),
228 it->first.c_str(),
229 it->second.c_str());
Roger Meierba406d32013-07-15 22:41:34 +0200230 } else {
231 match = false;
232 }
233 }
234 if (0 == prog->namespaces_.size() && 0 == t->get_program()->namespaces_.size()) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100235 pwarning(1,
236 "Duplicate typename %s found in %s and %s",
237 t->get_name().c_str(),
238 t->get_program()->get_name().c_str(),
239 prog->get_name().c_str());
Roger Meierba406d32013-07-15 22:41:34 +0200240 }
241 return match;
242 }
243
Mark Sleef0712dc2006-10-25 19:03:57 +0000244 // Scoping and namespacing
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100245 void set_namespace(std::string name) { namespace_ = name; }
Mark Slee9cb7c612006-09-01 22:17:45 +0000246
Mark Sleef0712dc2006-10-25 19:03:57 +0000247 // Scope accessor
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100248 t_scope* scope() const { return scope_; }
Mark Sleef5377b32006-10-10 01:42:59 +0000249
Mark Sleef0712dc2006-10-25 19:03:57 +0000250 // Includes
251
kholst76f2c882008-01-16 02:47:41 +0000252 void add_include(std::string path, std::string include_site) {
253 t_program* program = new t_program(path);
254
255 // include prefix for this program is the site at which it was included
256 // (minus the filename)
257 std::string include_prefix;
258 std::string::size_type last_slash = std::string::npos;
259 if ((last_slash = include_site.rfind("/")) != std::string::npos) {
260 include_prefix = include_site.substr(0, last_slash);
261 }
262
263 program->set_include_prefix(include_prefix);
264 includes_.push_back(program);
Mark Sleee8540632006-05-30 09:24:40 +0000265 }
Mark Sleef5377b32006-10-10 01:42:59 +0000266
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100267 std::vector<t_program*>& get_includes() { return includes_; }
Mark Sleef5377b32006-10-10 01:42:59 +0000268
kholst76f2c882008-01-16 02:47:41 +0000269 void set_include_prefix(std::string include_prefix) {
270 include_prefix_ = include_prefix;
271
272 // this is intended to be a directory; add a trailing slash if necessary
273 int len = include_prefix_.size();
274 if (len > 0 && include_prefix_[len - 1] != '/') {
275 include_prefix_ += '/';
276 }
277 }
278
David Reiss79eca142008-02-27 01:55:13 +0000279 // Language neutral namespace / packaging
280 void set_namespace(std::string language, std::string name_space) {
Bryan Duxburybbff4a82010-09-03 20:36:02 +0000281 if (language != "*") {
282 size_t sub_index = language.find('.');
283 std::string base_language = language.substr(0, sub_index);
284 std::string sub_namespace;
Bryan Duxbury681f5ea2010-08-20 16:42:04 +0000285
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100286 if (base_language == "smalltalk") {
Bryan Duxburybbff4a82010-09-03 20:36:02 +0000287 pwarning(1, "Namespace 'smalltalk' is deprecated. Use 'st' instead");
288 base_language = "st";
289 }
Bryan Duxbury4d8a9cd2010-08-30 17:09:58 +0000290
Bryan Duxburybbff4a82010-09-03 20:36:02 +0000291 t_generator_registry::gen_map_t my_copy = t_generator_registry::get_generator_map();
Bryan Duxburye0ac3ab2010-07-29 16:24:41 +0000292
Bryan Duxburybbff4a82010-09-03 20:36:02 +0000293 t_generator_registry::gen_map_t::iterator it;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100294 it = my_copy.find(base_language);
Bryan Duxburye0ac3ab2010-07-29 16:24:41 +0000295
Bryan Duxburybbff4a82010-09-03 20:36:02 +0000296 if (it == my_copy.end()) {
Henrique Mendoncaffb031d2012-09-24 18:36:16 +0000297 std::string warning = "No generator named '" + base_language + "' could be found!";
298 pwarning(1, warning.c_str());
299 } else {
300 if (sub_index != std::string::npos) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100301 std::string sub_namespace = language.substr(sub_index + 1);
302 if (!it->second->is_valid_namespace(sub_namespace)) {
303 std::string warning = base_language + " generator does not accept '" + sub_namespace
304 + "' as sub-namespace!";
Henrique Mendonça8ad13a32013-05-16 21:26:20 +0200305 pwarning(1, warning.c_str());
Henrique Mendoncaffb031d2012-09-24 18:36:16 +0000306 }
Bryan Duxburybbff4a82010-09-03 20:36:02 +0000307 }
Bryan Duxbury681f5ea2010-08-20 16:42:04 +0000308 }
David Reisse3ba3492010-08-26 21:49:45 +0000309 }
Bryan Duxbury681f5ea2010-08-20 16:42:04 +0000310
David Reiss79eca142008-02-27 01:55:13 +0000311 namespaces_[language] = name_space;
312 }
313
314 std::string get_namespace(std::string language) const {
David Reissfb790d72010-09-02 16:41:45 +0000315 std::map<std::string, std::string>::const_iterator iter;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100316 if ((iter = namespaces_.find(language)) != namespaces_.end()
317 || (iter = namespaces_.find("*")) != namespaces_.end()) {
David Reissfb790d72010-09-02 16:41:45 +0000318 return iter->second;
David Reiss79eca142008-02-27 01:55:13 +0000319 }
David Reissfb790d72010-09-02 16:41:45 +0000320 return std::string();
David Reiss79eca142008-02-27 01:55:13 +0000321 }
322
Mark Sleef0712dc2006-10-25 19:03:57 +0000323 // Language specific namespace / packaging
324
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100325 void add_cpp_include(std::string path) { cpp_includes_.push_back(path); }
Mark Sleef0712dc2006-10-25 19:03:57 +0000326
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100327 const std::vector<std::string>& get_cpp_includes() { return cpp_includes_; }
Mark Sleef0712dc2006-10-25 19:03:57 +0000328
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100329 void add_c_include(std::string path) { c_includes_.push_back(path); }
Roger Meier213a6642010-10-27 12:30:11 +0000330
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100331 const std::vector<std::string>& get_c_includes() { return c_includes_; }
Roger Meier213a6642010-10-27 12:30:11 +0000332
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100333private:
Mark Sleef0712dc2006-10-25 19:03:57 +0000334 // File path
335 std::string path_;
Mark Slee9cb7c612006-09-01 22:17:45 +0000336
Mark Slee31985722006-05-24 21:45:31 +0000337 // Name
338 std::string name_;
339
dweatherford65b70752007-10-31 02:18:14 +0000340 // Output directory
341 std::string out_path_;
342
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000343 // Output directory is absolute location for generated source (no gen-*)
344 bool out_path_is_absolute_;
345
Mark Slee9cb7c612006-09-01 22:17:45 +0000346 // Namespace
347 std::string namespace_;
348
Mark Sleef0712dc2006-10-25 19:03:57 +0000349 // Included programs
350 std::vector<t_program*> includes_;
Mark Sleee8540632006-05-30 09:24:40 +0000351
kholst76f2c882008-01-16 02:47:41 +0000352 // Include prefix for this program, if any
353 std::string include_prefix_;
354
Mark Sleef0712dc2006-10-25 19:03:57 +0000355 // Identifier lookup scope
356 t_scope* scope_;
Mark Sleee8540632006-05-30 09:24:40 +0000357
Mark Sleef0712dc2006-10-25 19:03:57 +0000358 // Components to generate code for
359 std::vector<t_typedef*> typedefs_;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100360 std::vector<t_enum*> enums_;
361 std::vector<t_const*> consts_;
362 std::vector<t_struct*> objects_;
363 std::vector<t_struct*> structs_;
364 std::vector<t_struct*> xceptions_;
Mark Sleef0712dc2006-10-25 19:03:57 +0000365 std::vector<t_service*> services_;
366
David Reiss79eca142008-02-27 01:55:13 +0000367 // Dynamic namespaces
368 std::map<std::string, std::string> namespaces_;
369
Mark Sleef0712dc2006-10-25 19:03:57 +0000370 // C++ extra includes
371 std::vector<std::string> cpp_includes_;
372
Roger Meier213a6642010-10-27 12:30:11 +0000373 // C extra includes
374 std::vector<std::string> c_includes_;
Mark Slee31985722006-05-24 21:45:31 +0000375};
376
377#endif