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 { |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 59 | public: |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 60 | t_program(std::string path, std::string name) : |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 61 | path_(path), |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 62 | name_(name), |
Bryan Duxbury | bdca9f6 | 2011-03-01 19:53:07 +0000 | [diff] [blame] | 63 | out_path_("./"), |
| 64 | out_path_is_absolute_(false) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 65 | scope_ = new t_scope(); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 68 | t_program(std::string path) : |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 69 | path_(path), |
Bryan Duxbury | bdca9f6 | 2011-03-01 19:53:07 +0000 | [diff] [blame] | 70 | out_path_("./"), |
| 71 | out_path_is_absolute_(false) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 72 | name_ = program_name(path); |
| 73 | scope_ = new t_scope(); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 76 | // Path accessor |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 77 | const std::string& get_path() const { return path_; } |
| 78 | |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 79 | // Output path accessor |
| 80 | const std::string& get_out_path() const { return out_path_; } |
| 81 | |
Bryan Duxbury | bdca9f6 | 2011-03-01 19:53:07 +0000 | [diff] [blame] | 82 | // Create gen-* dir accessor |
Jake Farrell | d1fb180 | 2013-03-23 21:35:03 -0400 | [diff] [blame] | 83 | const bool is_out_path_absolute() const { return out_path_is_absolute_; } |
Bryan Duxbury | bdca9f6 | 2011-03-01 19:53:07 +0000 | [diff] [blame] | 84 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 85 | // Name accessor |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 86 | const std::string& get_name() const { return name_; } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 87 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 88 | // Namespace |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 89 | const std::string& get_namespace() const { return namespace_; } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 90 | |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 91 | // Include prefix accessor |
| 92 | const std::string& get_include_prefix() const { return include_prefix_; } |
| 93 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 94 | // Accessors for program elements |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 95 | const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; } |
| 96 | const std::vector<t_enum*>& get_enums() const { return enums_; } |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 97 | const std::vector<t_const*>& get_consts() const { return consts_; } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 98 | const std::vector<t_struct*>& get_structs() const { return structs_; } |
| 99 | const std::vector<t_struct*>& get_xceptions() const { return xceptions_; } |
Mark Slee | 1c4ced7 | 2008-01-14 23:04:43 +0000 | [diff] [blame] | 100 | const std::vector<t_struct*>& get_objects() const { return objects_; } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 101 | const std::vector<t_service*>& get_services() const { return services_; } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 102 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 103 | // Program elements |
| 104 | void add_typedef (t_typedef* td) { typedefs_.push_back(td); } |
| 105 | void add_enum (t_enum* te) { enums_.push_back(te); } |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 106 | void add_const (t_const* tc) { consts_.push_back(tc); } |
Mark Slee | 1c4ced7 | 2008-01-14 23:04:43 +0000 | [diff] [blame] | 107 | void add_struct (t_struct* ts) { objects_.push_back(ts); |
| 108 | structs_.push_back(ts); } |
| 109 | void add_xception (t_struct* tx) { objects_.push_back(tx); |
| 110 | xceptions_.push_back(tx); } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 111 | void add_service (t_service* ts) { services_.push_back(ts); } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 112 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 113 | // Programs to include |
| 114 | const std::vector<t_program*>& get_includes() const { return includes_; } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 115 | |
Bryan Duxbury | bdca9f6 | 2011-03-01 19:53:07 +0000 | [diff] [blame] | 116 | void set_out_path(std::string out_path, bool out_path_is_absolute) { |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 117 | out_path_ = out_path; |
Bryan Duxbury | bdca9f6 | 2011-03-01 19:53:07 +0000 | [diff] [blame] | 118 | out_path_is_absolute_ = out_path_is_absolute; |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 119 | // Ensure that it ends with a trailing '/' (or '\' for windows machines) |
| 120 | char c = out_path_.at(out_path_.size() - 1); |
| 121 | if (!(c == '/' || c == '\\')) { |
| 122 | out_path_.push_back('/'); |
| 123 | } |
| 124 | } |
| 125 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 126 | // Scoping and namespacing |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 127 | void set_namespace(std::string name) { |
| 128 | namespace_ = name; |
| 129 | } |
| 130 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 131 | // Scope accessor |
| 132 | t_scope* scope() { |
| 133 | return scope_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 134 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 135 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 136 | // Includes |
| 137 | |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 138 | void add_include(std::string path, std::string include_site) { |
| 139 | t_program* program = new t_program(path); |
| 140 | |
| 141 | // include prefix for this program is the site at which it was included |
| 142 | // (minus the filename) |
| 143 | std::string include_prefix; |
| 144 | std::string::size_type last_slash = std::string::npos; |
| 145 | if ((last_slash = include_site.rfind("/")) != std::string::npos) { |
| 146 | include_prefix = include_site.substr(0, last_slash); |
| 147 | } |
| 148 | |
| 149 | program->set_include_prefix(include_prefix); |
| 150 | includes_.push_back(program); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 151 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 152 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 153 | std::vector<t_program*>& get_includes() { |
| 154 | return includes_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 155 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 156 | |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 157 | void set_include_prefix(std::string include_prefix) { |
| 158 | include_prefix_ = include_prefix; |
| 159 | |
| 160 | // this is intended to be a directory; add a trailing slash if necessary |
| 161 | int len = include_prefix_.size(); |
| 162 | if (len > 0 && include_prefix_[len - 1] != '/') { |
| 163 | include_prefix_ += '/'; |
| 164 | } |
| 165 | } |
| 166 | |
David Reiss | 79eca14 | 2008-02-27 01:55:13 +0000 | [diff] [blame] | 167 | // Language neutral namespace / packaging |
| 168 | void set_namespace(std::string language, std::string name_space) { |
Bryan Duxbury | bbff4a8 | 2010-09-03 20:36:02 +0000 | [diff] [blame] | 169 | if (language != "*") { |
| 170 | size_t sub_index = language.find('.'); |
| 171 | std::string base_language = language.substr(0, sub_index); |
| 172 | std::string sub_namespace; |
Bryan Duxbury | 681f5ea | 2010-08-20 16:42:04 +0000 | [diff] [blame] | 173 | |
Bryan Duxbury | bbff4a8 | 2010-09-03 20:36:02 +0000 | [diff] [blame] | 174 | if(base_language == "smalltalk") { |
| 175 | pwarning(1, "Namespace 'smalltalk' is deprecated. Use 'st' instead"); |
| 176 | base_language = "st"; |
| 177 | } |
Bryan Duxbury | 4d8a9cd | 2010-08-30 17:09:58 +0000 | [diff] [blame] | 178 | |
Bryan Duxbury | bbff4a8 | 2010-09-03 20:36:02 +0000 | [diff] [blame] | 179 | 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] | 180 | |
Bryan Duxbury | bbff4a8 | 2010-09-03 20:36:02 +0000 | [diff] [blame] | 181 | t_generator_registry::gen_map_t::iterator it; |
| 182 | it=my_copy.find(base_language); |
Bryan Duxbury | e0ac3ab | 2010-07-29 16:24:41 +0000 | [diff] [blame] | 183 | |
Bryan Duxbury | bbff4a8 | 2010-09-03 20:36:02 +0000 | [diff] [blame] | 184 | if (it == my_copy.end()) { |
Henrique Mendonca | ffb031d | 2012-09-24 18:36:16 +0000 | [diff] [blame] | 185 | std::string warning = "No generator named '" + base_language + "' could be found!"; |
| 186 | pwarning(1, warning.c_str()); |
| 187 | } else { |
| 188 | if (sub_index != std::string::npos) { |
| 189 | std::string sub_namespace = language.substr(sub_index+1); |
| 190 | if ( ! it->second->is_valid_namespace(sub_namespace)) { |
Henrique Mendonça | 8ad13a3 | 2013-05-16 21:26:20 +0200 | [diff] [blame] | 191 | std::string warning = base_language + " generator does not accept '" + sub_namespace + "' as sub-namespace!"; |
| 192 | pwarning(1, warning.c_str()); |
Henrique Mendonca | ffb031d | 2012-09-24 18:36:16 +0000 | [diff] [blame] | 193 | } |
Bryan Duxbury | bbff4a8 | 2010-09-03 20:36:02 +0000 | [diff] [blame] | 194 | } |
Bryan Duxbury | 681f5ea | 2010-08-20 16:42:04 +0000 | [diff] [blame] | 195 | } |
David Reiss | e3ba349 | 2010-08-26 21:49:45 +0000 | [diff] [blame] | 196 | } |
Bryan Duxbury | 681f5ea | 2010-08-20 16:42:04 +0000 | [diff] [blame] | 197 | |
David Reiss | 79eca14 | 2008-02-27 01:55:13 +0000 | [diff] [blame] | 198 | namespaces_[language] = name_space; |
| 199 | } |
| 200 | |
| 201 | std::string get_namespace(std::string language) const { |
David Reiss | fb790d7 | 2010-09-02 16:41:45 +0000 | [diff] [blame] | 202 | std::map<std::string, std::string>::const_iterator iter; |
| 203 | if ((iter = namespaces_.find(language)) != namespaces_.end() || |
| 204 | (iter = namespaces_.find("*" )) != namespaces_.end()) { |
| 205 | return iter->second; |
David Reiss | 79eca14 | 2008-02-27 01:55:13 +0000 | [diff] [blame] | 206 | } |
David Reiss | fb790d7 | 2010-09-02 16:41:45 +0000 | [diff] [blame] | 207 | return std::string(); |
David Reiss | 79eca14 | 2008-02-27 01:55:13 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 210 | // Language specific namespace / packaging |
| 211 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 212 | void add_cpp_include(std::string path) { |
| 213 | cpp_includes_.push_back(path); |
| 214 | } |
| 215 | |
| 216 | const std::vector<std::string>& get_cpp_includes() { |
| 217 | return cpp_includes_; |
| 218 | } |
| 219 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 220 | void add_c_include(std::string path) { |
| 221 | c_includes_.push_back(path); |
| 222 | } |
| 223 | |
| 224 | const std::vector<std::string>& get_c_includes() { |
| 225 | return c_includes_; |
| 226 | } |
| 227 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 228 | private: |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 229 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 230 | // File path |
| 231 | std::string path_; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 232 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 233 | // Name |
| 234 | std::string name_; |
| 235 | |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 236 | // Output directory |
| 237 | std::string out_path_; |
| 238 | |
Bryan Duxbury | bdca9f6 | 2011-03-01 19:53:07 +0000 | [diff] [blame] | 239 | // Output directory is absolute location for generated source (no gen-*) |
| 240 | bool out_path_is_absolute_; |
| 241 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 242 | // Namespace |
| 243 | std::string namespace_; |
| 244 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 245 | // Included programs |
| 246 | std::vector<t_program*> includes_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 247 | |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 248 | // Include prefix for this program, if any |
| 249 | std::string include_prefix_; |
| 250 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 251 | // Identifier lookup scope |
| 252 | t_scope* scope_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 253 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 254 | // Components to generate code for |
| 255 | std::vector<t_typedef*> typedefs_; |
| 256 | std::vector<t_enum*> enums_; |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 257 | std::vector<t_const*> consts_; |
Mark Slee | 1c4ced7 | 2008-01-14 23:04:43 +0000 | [diff] [blame] | 258 | std::vector<t_struct*> objects_; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 259 | std::vector<t_struct*> structs_; |
| 260 | std::vector<t_struct*> xceptions_; |
| 261 | std::vector<t_service*> services_; |
| 262 | |
David Reiss | 79eca14 | 2008-02-27 01:55:13 +0000 | [diff] [blame] | 263 | // Dynamic namespaces |
| 264 | std::map<std::string, std::string> namespaces_; |
| 265 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 266 | // C++ extra includes |
| 267 | std::vector<std::string> cpp_includes_; |
| 268 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 269 | // C extra includes |
| 270 | std::vector<std::string> c_includes_; |
| 271 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 272 | }; |
| 273 | |
| 274 | #endif |