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 | dae6d3c | 2014-12-01 11:26:07 +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_; } |
| 103 | const std::map<std::string, std::string>& get_namespaces() const { return namespaces_; } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 104 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 105 | // Program elements |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 106 | void add_typedef(t_typedef* td) { typedefs_.push_back(td); } |
| 107 | void add_enum(t_enum* te) { enums_.push_back(te); } |
| 108 | void add_const(t_const* tc) { consts_.push_back(tc); } |
| 109 | void add_struct(t_struct* ts) { |
| 110 | objects_.push_back(ts); |
| 111 | structs_.push_back(ts); |
| 112 | } |
| 113 | void add_xception(t_struct* tx) { |
| 114 | objects_.push_back(tx); |
| 115 | xceptions_.push_back(tx); |
| 116 | } |
| 117 | void add_service(t_service* ts) { services_.push_back(ts); } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 118 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 119 | // Programs to include |
| 120 | const std::vector<t_program*>& get_includes() const { return includes_; } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 121 | |
Bryan Duxbury | bdca9f6 | 2011-03-01 19:53:07 +0000 | [diff] [blame] | 122 | void set_out_path(std::string out_path, bool out_path_is_absolute) { |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 123 | out_path_ = out_path; |
Bryan Duxbury | bdca9f6 | 2011-03-01 19:53:07 +0000 | [diff] [blame] | 124 | out_path_is_absolute_ = out_path_is_absolute; |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 125 | // Ensure that it ends with a trailing '/' (or '\' for windows machines) |
| 126 | char c = out_path_.at(out_path_.size() - 1); |
| 127 | if (!(c == '/' || c == '\\')) { |
| 128 | out_path_.push_back('/'); |
| 129 | } |
| 130 | } |
| 131 | |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 132 | // Typename collision detection |
| 133 | /** |
| 134 | * Search for typename collisions |
| 135 | * @param t the type to test for collisions |
| 136 | * @return true if a certain collision was found, otherwise false |
| 137 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 138 | bool is_unique_typename(t_type* t) { |
Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 139 | int occurrences = program_typename_count(this, t); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 140 | for (std::vector<t_program*>::iterator it = includes_.begin(); it != includes_.end(); ++it) { |
Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 141 | occurrences += program_typename_count(*it, t); |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 142 | } |
Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 143 | return 0 == occurrences; |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Search all type collections for duplicate typenames |
| 148 | * @param prog the program to search |
| 149 | * @param t the type to test for collisions |
| 150 | * @return the number of certain typename collisions |
| 151 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 152 | int program_typename_count(t_program* prog, t_type* t) { |
Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 153 | int occurrences = 0; |
| 154 | occurrences += collection_typename_count(prog, prog->typedefs_, t); |
| 155 | occurrences += collection_typename_count(prog, prog->enums_, t); |
| 156 | occurrences += collection_typename_count(prog, prog->objects_, t); |
| 157 | occurrences += collection_typename_count(prog, prog->services_, t); |
| 158 | return occurrences; |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Search a type collection for duplicate typenames |
| 163 | * @param prog the program to search |
| 164 | * @param type_collection the type collection to search |
| 165 | * @param t the type to test for collisions |
| 166 | * @return the number of certain typename collisions |
| 167 | */ |
| 168 | template <class T> |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 169 | int collection_typename_count(t_program* prog, T type_collection, t_type* t) { |
Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 170 | int occurrences = 0; |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 171 | for (typename T::iterator it = type_collection.begin(); it != type_collection.end(); ++it) |
| 172 | if (t != *it && 0 == t->get_name().compare((*it)->get_name()) && is_common_namespace(prog, t)) |
Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 173 | ++occurrences; |
| 174 | return occurrences; |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Determine whether identical typenames will collide based on namespaces. |
| 179 | * |
| 180 | * Because we do not know which languages the user will generate code for, |
| 181 | * collisions within programs (IDL files) having namespace declarations can be |
| 182 | * difficult to determine. Only guaranteed collisions return true (cause an error). |
| 183 | * Possible collisions involving explicit namespace declarations produce a warning. |
| 184 | * Other possible collisions go unreported. |
| 185 | * @param prog the program containing the preexisting typename |
| 186 | * @param t the type containing the typename match |
| 187 | * @return true if a collision within namespaces is found, otherwise false |
| 188 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 189 | bool is_common_namespace(t_program* prog, t_type* t) { |
| 190 | // Case 1: Typenames are in the same program [collision] |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 191 | if (prog == t->get_program()) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 192 | pwarning(1, |
| 193 | "Duplicate typename %s found in %s", |
| 194 | t->get_name().c_str(), |
| 195 | t->get_program()->get_name().c_str()); |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 196 | return true; |
| 197 | } |
| 198 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 199 | // Case 2: Both programs have identical namespace scope/name declarations [collision] |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 200 | bool match = true; |
| 201 | for (std::map<std::string, std::string>::iterator it = prog->namespaces_.begin(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 202 | it != prog->namespaces_.end(); |
| 203 | ++it) { |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 204 | if (0 == it->second.compare(t->get_program()->get_namespace(it->first))) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 205 | pwarning(1, |
| 206 | "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] | 207 | t->get_name().c_str(), |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 208 | t->get_program()->get_name().c_str(), |
| 209 | it->first.c_str(), |
| 210 | it->second.c_str(), |
| 211 | prog->get_name().c_str(), |
| 212 | it->first.c_str(), |
| 213 | it->second.c_str()); |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 214 | } else { |
| 215 | match = false; |
| 216 | } |
| 217 | } |
| 218 | 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] | 219 | it != t->get_program()->namespaces_.end(); |
| 220 | ++it) { |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 221 | if (0 == it->second.compare(prog->get_namespace(it->first))) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 222 | pwarning(1, |
| 223 | "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] | 224 | t->get_name().c_str(), |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 225 | t->get_program()->get_name().c_str(), |
| 226 | it->first.c_str(), |
| 227 | it->second.c_str(), |
| 228 | prog->get_name().c_str(), |
| 229 | it->first.c_str(), |
| 230 | it->second.c_str()); |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 231 | } else { |
| 232 | match = false; |
| 233 | } |
| 234 | } |
| 235 | if (0 == prog->namespaces_.size() && 0 == t->get_program()->namespaces_.size()) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 236 | pwarning(1, |
| 237 | "Duplicate typename %s found in %s and %s", |
| 238 | t->get_name().c_str(), |
| 239 | t->get_program()->get_name().c_str(), |
| 240 | prog->get_name().c_str()); |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 241 | } |
| 242 | return match; |
| 243 | } |
| 244 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 245 | // Scoping and namespacing |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 246 | void set_namespace(std::string name) { namespace_ = name; } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 247 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 248 | // Scope accessor |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 249 | t_scope* scope() const { return scope_; } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 250 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 251 | // Includes |
| 252 | |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 253 | void add_include(std::string path, std::string include_site) { |
| 254 | t_program* program = new t_program(path); |
| 255 | |
| 256 | // include prefix for this program is the site at which it was included |
| 257 | // (minus the filename) |
| 258 | std::string include_prefix; |
| 259 | std::string::size_type last_slash = std::string::npos; |
| 260 | if ((last_slash = include_site.rfind("/")) != std::string::npos) { |
| 261 | include_prefix = include_site.substr(0, last_slash); |
| 262 | } |
| 263 | |
| 264 | program->set_include_prefix(include_prefix); |
| 265 | includes_.push_back(program); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 266 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 267 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 268 | std::vector<t_program*>& get_includes() { return includes_; } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 269 | |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 270 | void set_include_prefix(std::string include_prefix) { |
| 271 | include_prefix_ = include_prefix; |
| 272 | |
| 273 | // this is intended to be a directory; add a trailing slash if necessary |
Jim King | 9de9b1f | 2015-04-30 16:03:34 -0400 | [diff] [blame] | 274 | std::string::size_type len = include_prefix_.size(); |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 275 | if (len > 0 && include_prefix_[len - 1] != '/') { |
| 276 | include_prefix_ += '/'; |
| 277 | } |
| 278 | } |
| 279 | |
David Reiss | 79eca14 | 2008-02-27 01:55:13 +0000 | [diff] [blame] | 280 | // Language neutral namespace / packaging |
| 281 | void set_namespace(std::string language, std::string name_space) { |
Bryan Duxbury | bbff4a8 | 2010-09-03 20:36:02 +0000 | [diff] [blame] | 282 | if (language != "*") { |
| 283 | size_t sub_index = language.find('.'); |
| 284 | std::string base_language = language.substr(0, sub_index); |
| 285 | std::string sub_namespace; |
Bryan Duxbury | 681f5ea | 2010-08-20 16:42:04 +0000 | [diff] [blame] | 286 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 287 | if (base_language == "smalltalk") { |
Bryan Duxbury | bbff4a8 | 2010-09-03 20:36:02 +0000 | [diff] [blame] | 288 | pwarning(1, "Namespace 'smalltalk' is deprecated. Use 'st' instead"); |
| 289 | base_language = "st"; |
| 290 | } |
Bryan Duxbury | 4d8a9cd | 2010-08-30 17:09:58 +0000 | [diff] [blame] | 291 | |
Bryan Duxbury | bbff4a8 | 2010-09-03 20:36:02 +0000 | [diff] [blame] | 292 | 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] | 293 | |
Bryan Duxbury | bbff4a8 | 2010-09-03 20:36:02 +0000 | [diff] [blame] | 294 | t_generator_registry::gen_map_t::iterator it; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 295 | it = my_copy.find(base_language); |
Bryan Duxbury | e0ac3ab | 2010-07-29 16:24:41 +0000 | [diff] [blame] | 296 | |
Bryan Duxbury | bbff4a8 | 2010-09-03 20:36:02 +0000 | [diff] [blame] | 297 | if (it == my_copy.end()) { |
Henrique Mendonca | ffb031d | 2012-09-24 18:36:16 +0000 | [diff] [blame] | 298 | std::string warning = "No generator named '" + base_language + "' could be found!"; |
| 299 | pwarning(1, warning.c_str()); |
| 300 | } else { |
| 301 | if (sub_index != std::string::npos) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 302 | std::string sub_namespace = language.substr(sub_index + 1); |
| 303 | if (!it->second->is_valid_namespace(sub_namespace)) { |
| 304 | std::string warning = base_language + " generator does not accept '" + sub_namespace |
| 305 | + "' as sub-namespace!"; |
Henrique Mendonça | 8ad13a3 | 2013-05-16 21:26:20 +0200 | [diff] [blame] | 306 | pwarning(1, warning.c_str()); |
Henrique Mendonca | ffb031d | 2012-09-24 18:36:16 +0000 | [diff] [blame] | 307 | } |
Bryan Duxbury | bbff4a8 | 2010-09-03 20:36:02 +0000 | [diff] [blame] | 308 | } |
Bryan Duxbury | 681f5ea | 2010-08-20 16:42:04 +0000 | [diff] [blame] | 309 | } |
David Reiss | e3ba349 | 2010-08-26 21:49:45 +0000 | [diff] [blame] | 310 | } |
Bryan Duxbury | 681f5ea | 2010-08-20 16:42:04 +0000 | [diff] [blame] | 311 | |
David Reiss | 79eca14 | 2008-02-27 01:55:13 +0000 | [diff] [blame] | 312 | namespaces_[language] = name_space; |
| 313 | } |
| 314 | |
| 315 | std::string get_namespace(std::string language) const { |
David Reiss | fb790d7 | 2010-09-02 16:41:45 +0000 | [diff] [blame] | 316 | std::map<std::string, std::string>::const_iterator iter; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 317 | if ((iter = namespaces_.find(language)) != namespaces_.end() |
| 318 | || (iter = namespaces_.find("*")) != namespaces_.end()) { |
David Reiss | fb790d7 | 2010-09-02 16:41:45 +0000 | [diff] [blame] | 319 | return iter->second; |
David Reiss | 79eca14 | 2008-02-27 01:55:13 +0000 | [diff] [blame] | 320 | } |
David Reiss | fb790d7 | 2010-09-02 16:41:45 +0000 | [diff] [blame] | 321 | return std::string(); |
David Reiss | 79eca14 | 2008-02-27 01:55:13 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 324 | // Language specific namespace / packaging |
| 325 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 326 | void add_cpp_include(std::string path) { cpp_includes_.push_back(path); } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 327 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 328 | const std::vector<std::string>& get_cpp_includes() { return cpp_includes_; } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 329 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 330 | void add_c_include(std::string path) { c_includes_.push_back(path); } |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 331 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 332 | const std::vector<std::string>& get_c_includes() { return c_includes_; } |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 333 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 334 | private: |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 335 | // File path |
| 336 | std::string path_; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 337 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 338 | // Name |
| 339 | std::string name_; |
| 340 | |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 341 | // Output directory |
| 342 | std::string out_path_; |
| 343 | |
Bryan Duxbury | bdca9f6 | 2011-03-01 19:53:07 +0000 | [diff] [blame] | 344 | // Output directory is absolute location for generated source (no gen-*) |
| 345 | bool out_path_is_absolute_; |
| 346 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 347 | // Namespace |
| 348 | std::string namespace_; |
| 349 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 350 | // Included programs |
| 351 | std::vector<t_program*> includes_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 352 | |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 353 | // Include prefix for this program, if any |
| 354 | std::string include_prefix_; |
| 355 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 356 | // Identifier lookup scope |
| 357 | t_scope* scope_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 358 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 359 | // Components to generate code for |
| 360 | std::vector<t_typedef*> typedefs_; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 361 | std::vector<t_enum*> enums_; |
| 362 | std::vector<t_const*> consts_; |
| 363 | std::vector<t_struct*> objects_; |
| 364 | std::vector<t_struct*> structs_; |
| 365 | std::vector<t_struct*> xceptions_; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 366 | std::vector<t_service*> services_; |
| 367 | |
David Reiss | 79eca14 | 2008-02-27 01:55:13 +0000 | [diff] [blame] | 368 | // Dynamic namespaces |
| 369 | std::map<std::string, std::string> namespaces_; |
| 370 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 371 | // C++ extra includes |
| 372 | std::vector<std::string> cpp_includes_; |
| 373 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 374 | // C extra includes |
| 375 | std::vector<std::string> c_includes_; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 376 | }; |
| 377 | |
| 378 | #endif |