David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Slee | e9ce01c | 2007-05-16 02:29:53 +0000 | [diff] [blame] | 19 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 20 | #ifndef T_PROGRAM_H |
| 21 | #define T_PROGRAM_H |
| 22 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 23 | #include <map> |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 27 | // For program_name() |
| 28 | #include "main.h" |
| 29 | |
David Reiss | c2532a9 | 2007-07-30 23:46:11 +0000 | [diff] [blame] | 30 | #include "t_doc.h" |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 31 | #include "t_scope.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 32 | #include "t_base_type.h" |
| 33 | #include "t_typedef.h" |
| 34 | #include "t_enum.h" |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 35 | #include "t_const.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 36 | #include "t_struct.h" |
| 37 | #include "t_service.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 38 | #include "t_list.h" |
| 39 | #include "t_map.h" |
| 40 | #include "t_set.h" |
Bryan Duxbury | e0ac3ab | 2010-07-29 16:24:41 +0000 | [diff] [blame] | 41 | #include "generate/t_generator_registry.h" |
ccheever | f53b5cf | 2007-02-05 20:33:11 +0000 | [diff] [blame] | 42 | //#include "t_doc.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Top level class representing an entire thrift program. A program consists |
| 46 | * fundamentally of the following: |
| 47 | * |
| 48 | * Typedefs |
| 49 | * Enumerations |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 50 | * Constants |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 51 | * Structs |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 52 | * Exceptions |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 53 | * Services |
| 54 | * |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 55 | * The program module also contains the definitions of the base types. |
| 56 | * |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 57 | */ |
David Reiss | c2532a9 | 2007-07-30 23:46:11 +0000 | [diff] [blame] | 58 | class t_program : public t_doc { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 59 | public: |
| 60 | t_program(std::string path, std::string name) |
| 61 | : path_(path), name_(name), out_path_("./"), out_path_is_absolute_(false) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 62 | scope_ = new t_scope(); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 65 | t_program(std::string path) : path_(path), out_path_("./"), out_path_is_absolute_(false) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 66 | name_ = program_name(path); |
| 67 | scope_ = new t_scope(); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 70 | ~t_program() { |
| 71 | if (scope_) { |
| 72 | delete scope_; |
| 73 | scope_ = NULL; |
| 74 | } |
Roger Meier | 4f4b15b | 2014-11-05 16:51:04 +0100 | [diff] [blame] | 75 | } |
Jens Geyer | 09b97c7 | 2013-08-04 13:39:09 +0200 | [diff] [blame] | 76 | |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 77 | // Path accessor |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 78 | const std::string& get_path() const { return path_; } |
| 79 | |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 80 | // Output path accessor |
| 81 | const std::string& get_out_path() const { return out_path_; } |
| 82 | |
Bryan Duxbury | bdca9f6 | 2011-03-01 19:53:07 +0000 | [diff] [blame] | 83 | // Create gen-* dir accessor |
Roger Meier | a70986f | 2014-10-21 23:01:36 +0200 | [diff] [blame] | 84 | bool is_out_path_absolute() const { return out_path_is_absolute_; } |
Bryan Duxbury | bdca9f6 | 2011-03-01 19:53:07 +0000 | [diff] [blame] | 85 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 86 | // Name accessor |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 87 | const std::string& get_name() const { return name_; } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 88 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 89 | // Namespace |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 90 | const std::string& get_namespace() const { return namespace_; } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 91 | |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 92 | // Include prefix accessor |
| 93 | const std::string& get_include_prefix() const { return include_prefix_; } |
| 94 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 95 | // Accessors for program elements |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 96 | 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 Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 103 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 104 | // Program elements |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 105 | 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 Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 117 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 118 | // Programs to include |
| 119 | const std::vector<t_program*>& get_includes() const { return includes_; } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 120 | |
Bryan Duxbury | bdca9f6 | 2011-03-01 19:53:07 +0000 | [diff] [blame] | 121 | void set_out_path(std::string out_path, bool out_path_is_absolute) { |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 122 | out_path_ = out_path; |
Bryan Duxbury | bdca9f6 | 2011-03-01 19:53:07 +0000 | [diff] [blame] | 123 | out_path_is_absolute_ = out_path_is_absolute; |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 124 | // 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 Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 131 | // 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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 137 | bool is_unique_typename(t_type* t) { |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 138 | int occurances = program_typename_count(this, t); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 139 | for (std::vector<t_program*>::iterator it = includes_.begin(); it != includes_.end(); ++it) { |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 140 | 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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 151 | int program_typename_count(t_program* prog, t_type* t) { |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 152 | 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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 168 | int collection_typename_count(t_program* prog, T type_collection, t_type* t) { |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 169 | 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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 188 | bool is_common_namespace(t_program* prog, t_type* t) { |
| 189 | // Case 1: Typenames are in the same program [collision] |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 190 | if (prog == t->get_program()) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 191 | pwarning(1, |
| 192 | "Duplicate typename %s found in %s", |
| 193 | t->get_name().c_str(), |
| 194 | t->get_program()->get_name().c_str()); |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 195 | return true; |
| 196 | } |
| 197 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 198 | // Case 2: Both programs have identical namespace scope/name declarations [collision] |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 199 | bool match = true; |
| 200 | for (std::map<std::string, std::string>::iterator it = prog->namespaces_.begin(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 201 | it != prog->namespaces_.end(); |
| 202 | ++it) { |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 203 | if (0 == it->second.compare(t->get_program()->get_namespace(it->first))) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 204 | pwarning(1, |
| 205 | "Duplicate typename %s found in %s,%s,%s and %s,%s,%s [file,scope,ns]", |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 206 | t->get_name().c_str(), |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 207 | 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 Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 213 | } else { |
| 214 | match = false; |
| 215 | } |
| 216 | } |
| 217 | for (std::map<std::string, std::string>::iterator it = t->get_program()->namespaces_.begin(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 218 | it != t->get_program()->namespaces_.end(); |
| 219 | ++it) { |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 220 | if (0 == it->second.compare(prog->get_namespace(it->first))) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 221 | pwarning(1, |
| 222 | "Duplicate typename %s found in %s,%s,%s and %s,%s,%s [file,scope,ns]", |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 223 | t->get_name().c_str(), |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 224 | 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 Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 230 | } else { |
| 231 | match = false; |
| 232 | } |
| 233 | } |
| 234 | if (0 == prog->namespaces_.size() && 0 == t->get_program()->namespaces_.size()) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 235 | 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 Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 240 | } |
| 241 | return match; |
| 242 | } |
| 243 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 244 | // Scoping and namespacing |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 245 | void set_namespace(std::string name) { namespace_ = name; } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 246 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 247 | // Scope accessor |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 248 | t_scope* scope() const { return scope_; } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 249 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 250 | // Includes |
| 251 | |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 252 | 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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 265 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 266 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 267 | std::vector<t_program*>& get_includes() { return includes_; } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 268 | |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 269 | 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 Reiss | 79eca14 | 2008-02-27 01:55:13 +0000 | [diff] [blame] | 279 | // Language neutral namespace / packaging |
| 280 | void set_namespace(std::string language, std::string name_space) { |
Bryan Duxbury | bbff4a8 | 2010-09-03 20:36:02 +0000 | [diff] [blame] | 281 | 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 Duxbury | 681f5ea | 2010-08-20 16:42:04 +0000 | [diff] [blame] | 285 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 286 | if (base_language == "smalltalk") { |
Bryan Duxbury | bbff4a8 | 2010-09-03 20:36:02 +0000 | [diff] [blame] | 287 | pwarning(1, "Namespace 'smalltalk' is deprecated. Use 'st' instead"); |
| 288 | base_language = "st"; |
| 289 | } |
Bryan Duxbury | 4d8a9cd | 2010-08-30 17:09:58 +0000 | [diff] [blame] | 290 | |
Bryan Duxbury | bbff4a8 | 2010-09-03 20:36:02 +0000 | [diff] [blame] | 291 | t_generator_registry::gen_map_t my_copy = t_generator_registry::get_generator_map(); |
Bryan Duxbury | e0ac3ab | 2010-07-29 16:24:41 +0000 | [diff] [blame] | 292 | |
Bryan Duxbury | bbff4a8 | 2010-09-03 20:36:02 +0000 | [diff] [blame] | 293 | t_generator_registry::gen_map_t::iterator it; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 294 | it = my_copy.find(base_language); |
Bryan Duxbury | e0ac3ab | 2010-07-29 16:24:41 +0000 | [diff] [blame] | 295 | |
Bryan Duxbury | bbff4a8 | 2010-09-03 20:36:02 +0000 | [diff] [blame] | 296 | if (it == my_copy.end()) { |
Henrique Mendonca | ffb031d | 2012-09-24 18:36:16 +0000 | [diff] [blame] | 297 | 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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 301 | 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ça | 8ad13a3 | 2013-05-16 21:26:20 +0200 | [diff] [blame] | 305 | pwarning(1, warning.c_str()); |
Henrique Mendonca | ffb031d | 2012-09-24 18:36:16 +0000 | [diff] [blame] | 306 | } |
Bryan Duxbury | bbff4a8 | 2010-09-03 20:36:02 +0000 | [diff] [blame] | 307 | } |
Bryan Duxbury | 681f5ea | 2010-08-20 16:42:04 +0000 | [diff] [blame] | 308 | } |
David Reiss | e3ba349 | 2010-08-26 21:49:45 +0000 | [diff] [blame] | 309 | } |
Bryan Duxbury | 681f5ea | 2010-08-20 16:42:04 +0000 | [diff] [blame] | 310 | |
David Reiss | 79eca14 | 2008-02-27 01:55:13 +0000 | [diff] [blame] | 311 | namespaces_[language] = name_space; |
| 312 | } |
| 313 | |
| 314 | std::string get_namespace(std::string language) const { |
David Reiss | fb790d7 | 2010-09-02 16:41:45 +0000 | [diff] [blame] | 315 | std::map<std::string, std::string>::const_iterator iter; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 316 | if ((iter = namespaces_.find(language)) != namespaces_.end() |
| 317 | || (iter = namespaces_.find("*")) != namespaces_.end()) { |
David Reiss | fb790d7 | 2010-09-02 16:41:45 +0000 | [diff] [blame] | 318 | return iter->second; |
David Reiss | 79eca14 | 2008-02-27 01:55:13 +0000 | [diff] [blame] | 319 | } |
David Reiss | fb790d7 | 2010-09-02 16:41:45 +0000 | [diff] [blame] | 320 | return std::string(); |
David Reiss | 79eca14 | 2008-02-27 01:55:13 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 323 | // Language specific namespace / packaging |
| 324 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 325 | void add_cpp_include(std::string path) { cpp_includes_.push_back(path); } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 326 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 327 | const std::vector<std::string>& get_cpp_includes() { return cpp_includes_; } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 328 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 329 | void add_c_include(std::string path) { c_includes_.push_back(path); } |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 330 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 331 | const std::vector<std::string>& get_c_includes() { return c_includes_; } |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 332 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 333 | private: |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 334 | // File path |
| 335 | std::string path_; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 336 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 337 | // Name |
| 338 | std::string name_; |
| 339 | |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 340 | // Output directory |
| 341 | std::string out_path_; |
| 342 | |
Bryan Duxbury | bdca9f6 | 2011-03-01 19:53:07 +0000 | [diff] [blame] | 343 | // Output directory is absolute location for generated source (no gen-*) |
| 344 | bool out_path_is_absolute_; |
| 345 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 346 | // Namespace |
| 347 | std::string namespace_; |
| 348 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 349 | // Included programs |
| 350 | std::vector<t_program*> includes_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 351 | |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 352 | // Include prefix for this program, if any |
| 353 | std::string include_prefix_; |
| 354 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 355 | // Identifier lookup scope |
| 356 | t_scope* scope_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 357 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 358 | // Components to generate code for |
| 359 | std::vector<t_typedef*> typedefs_; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 360 | 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 Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 365 | std::vector<t_service*> services_; |
| 366 | |
David Reiss | 79eca14 | 2008-02-27 01:55:13 +0000 | [diff] [blame] | 367 | // Dynamic namespaces |
| 368 | std::map<std::string, std::string> namespaces_; |
| 369 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 370 | // C++ extra includes |
| 371 | std::vector<std::string> cpp_includes_; |
| 372 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 373 | // C extra includes |
| 374 | std::vector<std::string> c_includes_; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 375 | }; |
| 376 | |
| 377 | #endif |