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 | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 20 | #ifndef T_SCOPE_H |
| 21 | #define T_SCOPE_H |
| 22 | |
| 23 | #include <map> |
| 24 | #include <string> |
| 25 | |
Bryan Duxbury | 2d80470 | 2009-12-18 19:41:11 +0000 | [diff] [blame^] | 26 | #include <boost/lexical_cast.hpp> |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 27 | #include "t_type.h" |
| 28 | #include "t_service.h" |
Bryan Duxbury | 2d80470 | 2009-12-18 19:41:11 +0000 | [diff] [blame^] | 29 | #include "t_const.h" |
| 30 | #include "t_const_value.h" |
| 31 | #include "t_base_type.h" |
| 32 | #include "t_map.h" |
| 33 | #include "t_list.h" |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * This represents a variable scope used for looking up predefined types and |
| 37 | * services. Typically, a scope is associated with a t_program. Scopes are not |
| 38 | * used to determine code generation, but rather to resolve identifiers at |
| 39 | * parse time. |
| 40 | * |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 41 | */ |
| 42 | class t_scope { |
| 43 | public: |
| 44 | t_scope() {} |
| 45 | |
| 46 | void add_type(std::string name, t_type* type) { |
| 47 | types_[name] = type; |
| 48 | } |
| 49 | |
| 50 | t_type* get_type(std::string name) { |
| 51 | return types_[name]; |
| 52 | } |
| 53 | |
| 54 | void add_service(std::string name, t_service* service) { |
| 55 | services_[name] = service; |
| 56 | } |
| 57 | |
| 58 | t_service* get_service(std::string name) { |
| 59 | return services_[name]; |
| 60 | } |
| 61 | |
Mark Slee | d0767c5 | 2007-07-27 22:14:41 +0000 | [diff] [blame] | 62 | void add_constant(std::string name, t_const* constant) { |
| 63 | constants_[name] = constant; |
| 64 | } |
| 65 | |
| 66 | t_const* get_constant(std::string name) { |
| 67 | return constants_[name]; |
| 68 | } |
| 69 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 70 | void print() { |
| 71 | std::map<std::string, t_type*>::iterator iter; |
| 72 | for (iter = types_.begin(); iter != types_.end(); ++iter) { |
| 73 | printf("%s => %s\n", |
| 74 | iter->first.c_str(), |
| 75 | iter->second->get_name().c_str()); |
| 76 | } |
| 77 | } |
| 78 | |
Bryan Duxbury | 2d80470 | 2009-12-18 19:41:11 +0000 | [diff] [blame^] | 79 | void resolve_const_value(t_const_value* const_val, t_type* ttype) { |
| 80 | if (ttype->is_map()) { |
| 81 | const std::map<t_const_value*, t_const_value*>& map = const_val->get_map(); |
| 82 | std::map<t_const_value*, t_const_value*>::const_iterator v_iter; |
| 83 | for (v_iter = map.begin(); v_iter != map.end(); ++v_iter) { |
| 84 | resolve_const_value(v_iter->first, ((t_map*)ttype)->get_key_type()); |
| 85 | resolve_const_value(v_iter->second, ((t_map*)ttype)->get_val_type()); |
| 86 | } |
| 87 | } else if (ttype->is_list() || ttype->is_set()) { |
| 88 | const std::vector<t_const_value*>& val = const_val->get_list(); |
| 89 | std::vector<t_const_value*>::const_iterator v_iter; |
| 90 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { |
| 91 | resolve_const_value((*v_iter), ((t_list*)ttype)->get_elem_type()); |
| 92 | } |
| 93 | } else if (ttype->is_struct()) { |
| 94 | t_struct* tstruct = (t_struct*)ttype; |
| 95 | const std::map<t_const_value*, t_const_value*>& map = const_val->get_map(); |
| 96 | std::map<t_const_value*, t_const_value*>::const_iterator v_iter; |
| 97 | for (v_iter = map.begin(); v_iter != map.end(); ++v_iter) { |
| 98 | t_field* field = tstruct->get_field_by_name(v_iter->first->get_string()); |
| 99 | resolve_const_value(v_iter->second, field->get_type()); |
| 100 | } |
| 101 | } else if (const_val->get_type() == t_const_value::CV_IDENTIFIER) { |
| 102 | if (ttype->is_enum()) { |
| 103 | const_val->set_enum((t_enum*)ttype); |
| 104 | } else { |
| 105 | t_const* constant = get_constant(const_val->get_identifier()); |
| 106 | if (constant == NULL) { |
| 107 | throw "No enum value or constant found named \"" + const_val->get_identifier() + "\"!"; |
| 108 | } |
| 109 | |
| 110 | if (constant->get_type()->is_base_type()) { |
| 111 | switch (((t_base_type*)constant->get_type())->get_base()) { |
| 112 | case t_base_type::TYPE_I16: |
| 113 | case t_base_type::TYPE_I32: |
| 114 | case t_base_type::TYPE_I64: |
| 115 | case t_base_type::TYPE_BOOL: |
| 116 | case t_base_type::TYPE_BYTE: |
| 117 | const_val->set_integer(constant->get_value()->get_integer()); |
| 118 | break; |
| 119 | case t_base_type::TYPE_STRING: |
| 120 | const_val->set_string(constant->get_value()->get_string()); |
| 121 | break; |
| 122 | case t_base_type::TYPE_DOUBLE: |
| 123 | const_val->set_double(constant->get_value()->get_double()); |
| 124 | break; |
| 125 | case t_base_type::TYPE_VOID: |
| 126 | throw "Constants cannot be of type VOID"; |
| 127 | } |
| 128 | } else if (constant->get_type()->is_map()) { |
| 129 | const std::map<t_const_value*, t_const_value*>& map = constant->get_value()->get_map(); |
| 130 | std::map<t_const_value*, t_const_value*>::const_iterator v_iter; |
| 131 | |
| 132 | const_val->set_map(); |
| 133 | for (v_iter = map.begin(); v_iter != map.end(); ++v_iter) { |
| 134 | const_val->add_map(v_iter->first, v_iter->second); |
| 135 | } |
| 136 | } else if (constant->get_type()->is_list()) { |
| 137 | const std::vector<t_const_value*>& val = constant->get_value()->get_list(); |
| 138 | std::vector<t_const_value*>::const_iterator v_iter; |
| 139 | |
| 140 | const_val->set_list(); |
| 141 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { |
| 142 | const_val->add_list(*v_iter); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } else if (ttype->is_enum()) { |
| 147 | // enum constant with non-identifier value. set the enum and find the |
| 148 | // value's name. |
| 149 | t_enum* tenum = (t_enum*)ttype; |
| 150 | t_enum_value* enum_value = tenum->get_constant_by_value(const_val->get_integer()); |
| 151 | if (enum_value == NULL) { |
| 152 | throw "Couldn't find a named value in enum " + tenum->get_name() + " for value " + boost::lexical_cast<std::string>(const_val->get_integer()); |
| 153 | } |
| 154 | const_val->set_identifier(enum_value->get_name()); |
| 155 | const_val->set_enum(tenum); |
| 156 | } |
| 157 | } |
| 158 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 159 | private: |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 160 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 161 | // Map of names to types |
| 162 | std::map<std::string, t_type*> types_; |
| 163 | |
Mark Slee | d0767c5 | 2007-07-27 22:14:41 +0000 | [diff] [blame] | 164 | // Map of names to constants |
| 165 | std::map<std::string, t_const*> constants_; |
| 166 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 167 | // Map of names to services |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 168 | std::map<std::string, t_service*> services_; |
| 169 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | #endif |