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" |
ccheever | f53b5cf | 2007-02-05 20:33:11 +0000 | [diff] [blame] | 41 | //#include "t_doc.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Top level class representing an entire thrift program. A program consists |
| 45 | * fundamentally of the following: |
| 46 | * |
| 47 | * Typedefs |
| 48 | * Enumerations |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 49 | * Constants |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 50 | * Structs |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 51 | * Exceptions |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 52 | * Services |
| 53 | * |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 54 | * The program module also contains the definitions of the base types. |
| 55 | * |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 56 | */ |
David Reiss | c2532a9 | 2007-07-30 23:46:11 +0000 | [diff] [blame] | 57 | class t_program : public t_doc { |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 58 | public: |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 59 | t_program(std::string path, std::string name) : |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 60 | path_(path), |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 61 | name_(name), |
| 62 | out_path_("./") { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 63 | scope_ = new t_scope(); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 66 | t_program(std::string path) : |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 67 | path_(path), |
| 68 | out_path_("./") { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 69 | name_ = program_name(path); |
| 70 | scope_ = new t_scope(); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 73 | // Path accessor |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 74 | const std::string& get_path() const { return path_; } |
| 75 | |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 76 | // Output path accessor |
| 77 | const std::string& get_out_path() const { return out_path_; } |
| 78 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 79 | // Name accessor |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 80 | const std::string& get_name() const { return name_; } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 81 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 82 | // Namespace |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 83 | const std::string& get_namespace() const { return namespace_; } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 84 | |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 85 | // Include prefix accessor |
| 86 | const std::string& get_include_prefix() const { return include_prefix_; } |
| 87 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 88 | // Accessors for program elements |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 89 | const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; } |
| 90 | const std::vector<t_enum*>& get_enums() const { return enums_; } |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 91 | const std::vector<t_const*>& get_consts() const { return consts_; } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 92 | const std::vector<t_struct*>& get_structs() const { return structs_; } |
| 93 | const std::vector<t_struct*>& get_xceptions() const { return xceptions_; } |
Mark Slee | 1c4ced7 | 2008-01-14 23:04:43 +0000 | [diff] [blame] | 94 | const std::vector<t_struct*>& get_objects() const { return objects_; } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 95 | const std::vector<t_service*>& get_services() const { return services_; } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 96 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 97 | // Program elements |
| 98 | void add_typedef (t_typedef* td) { typedefs_.push_back(td); } |
| 99 | void add_enum (t_enum* te) { enums_.push_back(te); } |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 100 | void add_const (t_const* tc) { consts_.push_back(tc); } |
Mark Slee | 1c4ced7 | 2008-01-14 23:04:43 +0000 | [diff] [blame] | 101 | void add_struct (t_struct* ts) { objects_.push_back(ts); |
| 102 | structs_.push_back(ts); } |
| 103 | void add_xception (t_struct* tx) { objects_.push_back(tx); |
| 104 | xceptions_.push_back(tx); } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 105 | void add_service (t_service* ts) { services_.push_back(ts); } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 106 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 107 | // Programs to include |
| 108 | const std::vector<t_program*>& get_includes() const { return includes_; } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 109 | |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 110 | void set_out_path(std::string out_path) { |
| 111 | out_path_ = out_path; |
| 112 | // Ensure that it ends with a trailing '/' (or '\' for windows machines) |
| 113 | char c = out_path_.at(out_path_.size() - 1); |
| 114 | if (!(c == '/' || c == '\\')) { |
| 115 | out_path_.push_back('/'); |
| 116 | } |
| 117 | } |
| 118 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 119 | // Scoping and namespacing |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 120 | void set_namespace(std::string name) { |
| 121 | namespace_ = name; |
| 122 | } |
| 123 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 124 | // Scope accessor |
| 125 | t_scope* scope() { |
| 126 | return scope_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 127 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 128 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 129 | // Includes |
| 130 | |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 131 | void add_include(std::string path, std::string include_site) { |
| 132 | t_program* program = new t_program(path); |
| 133 | |
| 134 | // include prefix for this program is the site at which it was included |
| 135 | // (minus the filename) |
| 136 | std::string include_prefix; |
| 137 | std::string::size_type last_slash = std::string::npos; |
| 138 | if ((last_slash = include_site.rfind("/")) != std::string::npos) { |
| 139 | include_prefix = include_site.substr(0, last_slash); |
| 140 | } |
| 141 | |
| 142 | program->set_include_prefix(include_prefix); |
| 143 | includes_.push_back(program); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 144 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 145 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 146 | std::vector<t_program*>& get_includes() { |
| 147 | return includes_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 148 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 149 | |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 150 | void set_include_prefix(std::string include_prefix) { |
| 151 | include_prefix_ = include_prefix; |
| 152 | |
| 153 | // this is intended to be a directory; add a trailing slash if necessary |
| 154 | int len = include_prefix_.size(); |
| 155 | if (len > 0 && include_prefix_[len - 1] != '/') { |
| 156 | include_prefix_ += '/'; |
| 157 | } |
| 158 | } |
| 159 | |
David Reiss | 79eca14 | 2008-02-27 01:55:13 +0000 | [diff] [blame] | 160 | // Language neutral namespace / packaging |
| 161 | void set_namespace(std::string language, std::string name_space) { |
| 162 | namespaces_[language] = name_space; |
| 163 | } |
| 164 | |
| 165 | std::string get_namespace(std::string language) const { |
| 166 | std::map<std::string, std::string>::const_iterator iter = namespaces_.find(language); |
| 167 | if (iter == namespaces_.end()) { |
| 168 | return std::string(); |
| 169 | } |
| 170 | return iter->second; |
| 171 | } |
| 172 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 173 | // Language specific namespace / packaging |
| 174 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 175 | void add_cpp_include(std::string path) { |
| 176 | cpp_includes_.push_back(path); |
| 177 | } |
| 178 | |
| 179 | const std::vector<std::string>& get_cpp_includes() { |
| 180 | return cpp_includes_; |
| 181 | } |
| 182 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 183 | private: |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 184 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 185 | // File path |
| 186 | std::string path_; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 187 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 188 | // Name |
| 189 | std::string name_; |
| 190 | |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 191 | // Output directory |
| 192 | std::string out_path_; |
| 193 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 194 | // Namespace |
| 195 | std::string namespace_; |
| 196 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 197 | // Included programs |
| 198 | std::vector<t_program*> includes_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 199 | |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 200 | // Include prefix for this program, if any |
| 201 | std::string include_prefix_; |
| 202 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 203 | // Identifier lookup scope |
| 204 | t_scope* scope_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 205 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 206 | // Components to generate code for |
| 207 | std::vector<t_typedef*> typedefs_; |
| 208 | std::vector<t_enum*> enums_; |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 209 | std::vector<t_const*> consts_; |
Mark Slee | 1c4ced7 | 2008-01-14 23:04:43 +0000 | [diff] [blame] | 210 | std::vector<t_struct*> objects_; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 211 | std::vector<t_struct*> structs_; |
| 212 | std::vector<t_struct*> xceptions_; |
| 213 | std::vector<t_service*> services_; |
| 214 | |
David Reiss | 79eca14 | 2008-02-27 01:55:13 +0000 | [diff] [blame] | 215 | // Dynamic namespaces |
| 216 | std::map<std::string, std::string> namespaces_; |
| 217 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 218 | // C++ extra includes |
| 219 | std::vector<std::string> cpp_includes_; |
| 220 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | #endif |