blob: 7cb0738ab2677ae11ec89d59d2edd25a9ce8252f [file] [log] [blame]
Mark Sleee9ce01c2007-05-16 02:29:53 +00001// 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 Sleef0712dc2006-10-25 19:03:57 +00007#ifndef T_SCOPE_H
8#define T_SCOPE_H
9
10#include <map>
11#include <string>
12
13#include "t_type.h"
14#include "t_service.h"
15
16/**
17 * This represents a variable scope used for looking up predefined types and
18 * services. Typically, a scope is associated with a t_program. Scopes are not
19 * used to determine code generation, but rather to resolve identifiers at
20 * parse time.
21 *
Mark Sleef0712dc2006-10-25 19:03:57 +000022 */
23class t_scope {
24 public:
25 t_scope() {}
26
27 void add_type(std::string name, t_type* type) {
28 types_[name] = type;
29 }
30
31 t_type* get_type(std::string name) {
32 return types_[name];
33 }
34
35 void add_service(std::string name, t_service* service) {
36 services_[name] = service;
37 }
38
39 t_service* get_service(std::string name) {
40 return services_[name];
41 }
42
Mark Sleed0767c52007-07-27 22:14:41 +000043 void add_constant(std::string name, t_const* constant) {
44 constants_[name] = constant;
45 }
46
47 t_const* get_constant(std::string name) {
48 return constants_[name];
49 }
50
Mark Sleef0712dc2006-10-25 19:03:57 +000051 void print() {
52 std::map<std::string, t_type*>::iterator iter;
53 for (iter = types_.begin(); iter != types_.end(); ++iter) {
54 printf("%s => %s\n",
55 iter->first.c_str(),
56 iter->second->get_name().c_str());
57 }
58 }
59
60 private:
David Reiss0c90f6f2008-02-06 22:18:40 +000061
Mark Sleef0712dc2006-10-25 19:03:57 +000062 // Map of names to types
63 std::map<std::string, t_type*> types_;
64
Mark Sleed0767c52007-07-27 22:14:41 +000065 // Map of names to constants
66 std::map<std::string, t_const*> constants_;
67
Mark Sleef0712dc2006-10-25 19:03:57 +000068 // Map of names to services
David Reiss0c90f6f2008-02-06 22:18:40 +000069 std::map<std::string, t_service*> services_;
70
Mark Sleef0712dc2006-10-25 19:03:57 +000071};
72
73#endif