blob: e03956e572d8453106d6f4b0d476ffb92a977984 [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 *
22 * @author Mark Slee <mcslee@facebook.com>
23 */
24class t_scope {
25 public:
26 t_scope() {}
27
28 void add_type(std::string name, t_type* type) {
29 types_[name] = type;
30 }
31
32 t_type* get_type(std::string name) {
33 return types_[name];
34 }
35
36 void add_service(std::string name, t_service* service) {
37 services_[name] = service;
38 }
39
40 t_service* get_service(std::string name) {
41 return services_[name];
42 }
43
Mark Sleed0767c52007-07-27 22:14:41 +000044 void add_constant(std::string name, t_const* constant) {
45 constants_[name] = constant;
46 }
47
48 t_const* get_constant(std::string name) {
49 return constants_[name];
50 }
51
Mark Sleef0712dc2006-10-25 19:03:57 +000052 void print() {
53 std::map<std::string, t_type*>::iterator iter;
54 for (iter = types_.begin(); iter != types_.end(); ++iter) {
55 printf("%s => %s\n",
56 iter->first.c_str(),
57 iter->second->get_name().c_str());
58 }
59 }
60
61 private:
David Reiss0c90f6f2008-02-06 22:18:40 +000062
Mark Sleef0712dc2006-10-25 19:03:57 +000063 // Map of names to types
64 std::map<std::string, t_type*> types_;
65
Mark Sleed0767c52007-07-27 22:14:41 +000066 // Map of names to constants
67 std::map<std::string, t_const*> constants_;
68
Mark Sleef0712dc2006-10-25 19:03:57 +000069 // Map of names to services
David Reiss0c90f6f2008-02-06 22:18:40 +000070 std::map<std::string, t_service*> services_;
71
Mark Sleef0712dc2006-10-25 19:03:57 +000072};
73
74#endif