forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscope.h
More file actions
185 lines (160 loc) · 5.21 KB
/
Copy pathscope.h
File metadata and controls
185 lines (160 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (c) 2015 PLUMgrid, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <map>
#include <string>
#include <vector>
#include <memory>
namespace ebpf {
namespace cc {
using std::string;
using std::vector;
using std::map;
using std::pair;
using std::unique_ptr;
class StateDeclStmtNode;
class VariableDeclStmtNode;
class TableDeclStmtNode;
class StructDeclStmtNode;
class FuncDeclStmtNode;
enum search_type { SCOPE_LOCAL, SCOPE_GLOBAL };
template <typename T>
class Scope {
public:
Scope() {}
Scope(Scope<T>* scope, int id) : parent_(scope), id_(id) {}
T* lookup(const string &name, bool search_local = true) {
return lookup(name, search_local ? SCOPE_LOCAL : SCOPE_GLOBAL);
}
T * lookup(const string &name, search_type stype) {
auto it = elems_.find(name);
if (it != elems_.end())
return it->second;
if (stype == SCOPE_LOCAL || !parent_)
return nullptr;
return parent_->lookup(name, stype);
}
void add(const string& name, T* n) {
elems_[name] = n;
elems_ordered_.push_back(n);
}
typename map<string, T*>::iterator begin() { return elems_.begin(); }
typename map<string, T*>::iterator end() { return elems_.end(); }
typename vector<T*>::iterator obegin() { return elems_ordered_.begin(); }
typename vector<T*>::iterator oend() { return elems_ordered_.end(); }
Scope<T> *parent_;
int id_;
map<string, T*> elems_;
vector<T*> elems_ordered_;
};
/**
* Hold the current stack of scope pointers. Lookups search upwards.
* Actual scope pointers are kept in the AST.
*/
class Scopes {
public:
typedef unique_ptr<Scopes> Ptr;
typedef Scope<StructDeclStmtNode> StructScope;
typedef Scope<StateDeclStmtNode> StateScope;
typedef Scope<VariableDeclStmtNode> VarScope;
typedef Scope<TableDeclStmtNode> TableScope;
typedef Scope<FuncDeclStmtNode> FuncScope;
Scopes() : var_id__(0), state_id_(0), var_id_(0),
current_var_scope_(nullptr), top_var_scope_(nullptr),
current_state_scope_(nullptr), top_state_scope_(nullptr),
top_struct_scope_(new StructScope(nullptr, 1)),
top_table_scope_(new TableScope(nullptr, 1)),
top_func_scope_(new FuncScope(nullptr, 1)) {}
~Scopes() {
delete top_func_scope_;
delete top_struct_scope_;
delete top_table_scope_;
delete top_state_scope_;
}
void push_var(VarScope *scope) {
if (scope == top_var_scope_)
return;
scope->parent_ = current_var_scope_;
current_var_scope_ = scope;
}
void pop_var() {
if (current_var_scope_ == top_var_scope_)
return;
VarScope *old = current_var_scope_;
current_var_scope_ = old->parent_;
old->parent_ = nullptr;
}
void push_state(StateScope *scope) {
if (scope == top_state_scope_)
return;
scope->parent_ = current_state_scope_;
current_state_scope_ = scope;
}
void pop_state() {
if (current_state_scope_ == top_state_scope_)
return;
StateScope *old = current_state_scope_;
current_state_scope_ = old->parent_;
old->parent_ = nullptr;
}
/// While building the AST, allocate a new scope
VarScope* enter_var_scope() {
current_var_scope_ = new VarScope(current_var_scope_, next_var_id());
if (!top_var_scope_) {
top_var_scope_ = current_var_scope_;
}
return current_var_scope_;
}
VarScope* exit_var_scope() {
current_var_scope_ = current_var_scope_->parent_;
return current_var_scope_;
}
StateScope* enter_state_scope() {
current_state_scope_ = new StateScope(current_state_scope_, next_state_id());
if (!top_state_scope_) {
top_state_scope_ = current_state_scope_;
}
return current_state_scope_;
}
StateScope* exit_state_scope() {
current_state_scope_ = current_state_scope_->parent_;
return current_state_scope_;
}
void set_current(VarScope* s) { current_var_scope_ = s; }
VarScope* current_var() const { return current_var_scope_; }
VarScope* top_var() const { return top_var_scope_; }
void set_current(StateScope* s) { current_state_scope_ = s; }
StateScope* current_state() const { return current_state_scope_; }
StateScope* top_state() const { return top_state_scope_; }
StructScope* top_struct() const { return top_struct_scope_; }
TableScope* top_table() const { return top_table_scope_; }
FuncScope* top_func() const { return top_func_scope_; }
int next_id() { return ++var_id__; }
int next_state_id() { return ++state_id_; }
int next_var_id() { return ++var_id_; }
int var_id__;
int state_id_;
int var_id_;
VarScope* current_var_scope_;
VarScope* top_var_scope_;
StateScope* current_state_scope_;
StateScope* top_state_scope_;
StructScope* top_struct_scope_;
TableScope* top_table_scope_;
FuncScope* top_func_scope_;
};
} // namespace cc
} // namespace ebpf