blob: 5acb94e5b15cb0a81c7479ba0fcfc9f572f4fc4a [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 Sleee9ce01c2007-05-16 02:29:53 +000019
Mark Sleef0712dc2006-10-25 19:03:57 +000020#ifndef T_SCOPE_H
21#define T_SCOPE_H
22
23#include <map>
24#include <string>
David Reissaca320d2010-08-31 16:51:26 +000025#include <sstream>
Mark Sleef0712dc2006-10-25 19:03:57 +000026
27#include "t_type.h"
28#include "t_service.h"
Bryan Duxbury2d804702009-12-18 19:41:11 +000029#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 Sleef0712dc2006-10-25 19:03:57 +000034
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 Sleef0712dc2006-10-25 19:03:57 +000041 */
42class t_scope {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010043public:
Mark Sleef0712dc2006-10-25 19:03:57 +000044 t_scope() {}
45
Konrad Grochowski16a23a62014-11-13 15:33:38 +010046 void add_type(std::string name, t_type* type) { types_[name] = type; }
Mark Sleef0712dc2006-10-25 19:03:57 +000047
Konrad Grochowski16a23a62014-11-13 15:33:38 +010048 t_type* get_type(std::string name) { return types_[name]; }
Mark Sleef0712dc2006-10-25 19:03:57 +000049
Konrad Grochowski16a23a62014-11-13 15:33:38 +010050 void add_service(std::string name, t_service* service) { services_[name] = service; }
Mark Sleef0712dc2006-10-25 19:03:57 +000051
Konrad Grochowski16a23a62014-11-13 15:33:38 +010052 t_service* get_service(std::string name) { return services_[name]; }
Mark Sleef0712dc2006-10-25 19:03:57 +000053
Mark Sleed0767c52007-07-27 22:14:41 +000054 void add_constant(std::string name, t_const* constant) {
Bryan Duxbury8fc413f2010-08-05 23:23:04 +000055 if (constants_.find(name) != constants_.end()) {
56 throw "Enum " + name + " is already defined!";
57 } else {
58 constants_[name] = constant;
59 }
Mark Sleed0767c52007-07-27 22:14:41 +000060 }
61
Konrad Grochowski16a23a62014-11-13 15:33:38 +010062 t_const* get_constant(std::string name) { return constants_[name]; }
Mark Sleed0767c52007-07-27 22:14:41 +000063
Mark Sleef0712dc2006-10-25 19:03:57 +000064 void print() {
65 std::map<std::string, t_type*>::iterator iter;
66 for (iter = types_.begin(); iter != types_.end(); ++iter) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010067 printf("%s => %s\n", iter->first.c_str(), iter->second->get_name().c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +000068 }
69 }
70
Bryan Duxbury2d804702009-12-18 19:41:11 +000071 void resolve_const_value(t_const_value* const_val, t_type* ttype) {
72 if (ttype->is_map()) {
73 const std::map<t_const_value*, t_const_value*>& map = const_val->get_map();
74 std::map<t_const_value*, t_const_value*>::const_iterator v_iter;
75 for (v_iter = map.begin(); v_iter != map.end(); ++v_iter) {
76 resolve_const_value(v_iter->first, ((t_map*)ttype)->get_key_type());
77 resolve_const_value(v_iter->second, ((t_map*)ttype)->get_val_type());
78 }
79 } else if (ttype->is_list() || ttype->is_set()) {
80 const std::vector<t_const_value*>& val = const_val->get_list();
81 std::vector<t_const_value*>::const_iterator v_iter;
82 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
83 resolve_const_value((*v_iter), ((t_list*)ttype)->get_elem_type());
84 }
85 } else if (ttype->is_struct()) {
86 t_struct* tstruct = (t_struct*)ttype;
87 const std::map<t_const_value*, t_const_value*>& map = const_val->get_map();
88 std::map<t_const_value*, t_const_value*>::const_iterator v_iter;
89 for (v_iter = map.begin(); v_iter != map.end(); ++v_iter) {
90 t_field* field = tstruct->get_field_by_name(v_iter->first->get_string());
Bryan Duxbury656683c2010-06-23 16:57:51 +000091 if (field == NULL) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010092 throw "No field named \"" + v_iter->first->get_string()
93 + "\" was found in struct of type \"" + tstruct->get_name() + "\"";
Bryan Duxbury656683c2010-06-23 16:57:51 +000094 }
Bryan Duxbury2d804702009-12-18 19:41:11 +000095 resolve_const_value(v_iter->second, field->get_type());
96 }
97 } else if (const_val->get_type() == t_const_value::CV_IDENTIFIER) {
98 if (ttype->is_enum()) {
99 const_val->set_enum((t_enum*)ttype);
100 } else {
101 t_const* constant = get_constant(const_val->get_identifier());
102 if (constant == NULL) {
103 throw "No enum value or constant found named \"" + const_val->get_identifier() + "\"!";
104 }
105
David Reiss41d30582010-10-05 16:39:29 +0000106 // Resolve typedefs to the underlying type
107 t_type* const_type = constant->get_type()->get_true_type();
108
109 if (const_type->is_base_type()) {
110 switch (((t_base_type*)const_type)->get_base()) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100111 case t_base_type::TYPE_I16:
112 case t_base_type::TYPE_I32:
113 case t_base_type::TYPE_I64:
114 case t_base_type::TYPE_BOOL:
Jens Geyer40c28d32015-10-20 23:13:02 +0200115 case t_base_type::TYPE_I8:
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100116 const_val->set_integer(constant->get_value()->get_integer());
117 break;
118 case t_base_type::TYPE_STRING:
119 const_val->set_string(constant->get_value()->get_string());
120 break;
121 case t_base_type::TYPE_DOUBLE:
122 const_val->set_double(constant->get_value()->get_double());
123 break;
124 case t_base_type::TYPE_VOID:
125 throw "Constants cannot be of type VOID";
Bryan Duxbury2d804702009-12-18 19:41:11 +0000126 }
David Reiss41d30582010-10-05 16:39:29 +0000127 } else if (const_type->is_map()) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000128 const std::map<t_const_value*, t_const_value*>& map = constant->get_value()->get_map();
129 std::map<t_const_value*, t_const_value*>::const_iterator v_iter;
130
131 const_val->set_map();
132 for (v_iter = map.begin(); v_iter != map.end(); ++v_iter) {
133 const_val->add_map(v_iter->first, v_iter->second);
134 }
David Reiss41d30582010-10-05 16:39:29 +0000135 } else if (const_type->is_list()) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000136 const std::vector<t_const_value*>& val = constant->get_value()->get_list();
137 std::vector<t_const_value*>::const_iterator v_iter;
138
139 const_val->set_list();
140 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
141 const_val->add_list(*v_iter);
142 }
143 }
144 }
145 } else if (ttype->is_enum()) {
146 // enum constant with non-identifier value. set the enum and find the
147 // value's name.
148 t_enum* tenum = (t_enum*)ttype;
149 t_enum_value* enum_value = tenum->get_constant_by_value(const_val->get_integer());
150 if (enum_value == NULL) {
David Reissaca320d2010-08-31 16:51:26 +0000151 std::ostringstream valstm;
152 valstm << const_val->get_integer();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100153 throw "Couldn't find a named value in enum " + tenum->get_name() + " for value "
154 + valstm.str();
Bryan Duxbury2d804702009-12-18 19:41:11 +0000155 }
Bryan Duxburycb0218f2010-09-12 15:22:21 +0000156 const_val->set_identifier(tenum->get_name() + "." + enum_value->get_name());
Bryan Duxbury2d804702009-12-18 19:41:11 +0000157 const_val->set_enum(tenum);
158 }
159 }
160
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100161private:
Mark Sleef0712dc2006-10-25 19:03:57 +0000162 // Map of names to types
163 std::map<std::string, t_type*> types_;
164
Mark Sleed0767c52007-07-27 22:14:41 +0000165 // Map of names to constants
166 std::map<std::string, t_const*> constants_;
167
Mark Sleef0712dc2006-10-25 19:03:57 +0000168 // Map of names to services
David Reiss0c90f6f2008-02-06 22:18:40 +0000169 std::map<std::string, t_service*> services_;
Mark Sleef0712dc2006-10-25 19:03:57 +0000170};
171
172#endif