blob: 122e3256e2c806a5163b4a104138b57d1fe73280 [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>
25
26#include "t_type.h"
27#include "t_service.h"
28
29/**
30 * This represents a variable scope used for looking up predefined types and
31 * services. Typically, a scope is associated with a t_program. Scopes are not
32 * used to determine code generation, but rather to resolve identifiers at
33 * parse time.
34 *
Mark Sleef0712dc2006-10-25 19:03:57 +000035 */
36class t_scope {
37 public:
38 t_scope() {}
39
40 void add_type(std::string name, t_type* type) {
41 types_[name] = type;
42 }
43
44 t_type* get_type(std::string name) {
45 return types_[name];
46 }
47
48 void add_service(std::string name, t_service* service) {
49 services_[name] = service;
50 }
51
52 t_service* get_service(std::string name) {
53 return services_[name];
54 }
55
Mark Sleed0767c52007-07-27 22:14:41 +000056 void add_constant(std::string name, t_const* constant) {
57 constants_[name] = constant;
58 }
59
60 t_const* get_constant(std::string name) {
61 return constants_[name];
62 }
63
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) {
67 printf("%s => %s\n",
68 iter->first.c_str(),
69 iter->second->get_name().c_str());
70 }
71 }
72
73 private:
David Reiss0c90f6f2008-02-06 22:18:40 +000074
Mark Sleef0712dc2006-10-25 19:03:57 +000075 // Map of names to types
76 std::map<std::string, t_type*> types_;
77
Mark Sleed0767c52007-07-27 22:14:41 +000078 // Map of names to constants
79 std::map<std::string, t_const*> constants_;
80
Mark Sleef0712dc2006-10-25 19:03:57 +000081 // Map of names to services
David Reiss0c90f6f2008-02-06 22:18:40 +000082 std::map<std::string, t_service*> services_;
83
Mark Sleef0712dc2006-10-25 19:03:57 +000084};
85
86#endif