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