-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathcpp_scope.cpp
204 lines (162 loc) · 4.93 KB
/
cpp_scope.cpp
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*******************************************************************\
Module: C++ Language Type Checking
Author: Daniel Kroening, [email protected]
\*******************************************************************/
/// \file
/// C++ Language Type Checking
#include "cpp_scope.h"
std::ostream &operator << (std::ostream &out, cpp_scopet::lookup_kindt kind)
{
switch(kind)
{
case cpp_scopet::QUALIFIED: return out << "QUALIFIED";
case cpp_scopet::SCOPE_ONLY: return out << "SCOPE_ONLY";
case cpp_scopet::RECURSIVE: return out << "RECURSIVE";
default: UNREACHABLE;
}
return out;
}
void cpp_scopet::lookup_rec(
const irep_idt &base_name_to_lookup,
lookup_kindt kind,
id_sett &id_set)
{
cpp_id_mapt::iterator lower_it = sub.lower_bound(base_name_to_lookup);
if(lower_it!=sub.end())
{
cpp_id_mapt::iterator upper_it = sub.upper_bound(base_name_to_lookup);
for(cpp_id_mapt::iterator n_it=lower_it;
n_it!=upper_it; n_it++)
id_set.insert(&n_it->second);
}
if(base_name == base_name_to_lookup)
id_set.insert(this);
if(kind==SCOPE_ONLY)
return; // done
// using scopes
for(const auto &s_ptr : using_scopes)
{
cpp_scopet &other_scope = static_cast<cpp_scopet &>(*s_ptr);
// Recursive call.
// Note the different kind!
other_scope.lookup_rec(base_name_to_lookup, QUALIFIED, id_set);
}
if(!id_set.empty())
return; // done, upwards scopes are hidden
// secondary scopes
for(const auto &s_ptr : secondary_scopes)
{
cpp_scopet &other_scope = static_cast<cpp_scopet &>(*s_ptr);
// Recursive call.
// Note the different kind!
other_scope.lookup_rec(base_name_to_lookup, QUALIFIED, id_set);
}
if(kind==QUALIFIED)
return; // done
if(!id_set.empty())
return; // done
// ask parent, recursive call
if(!is_root_scope())
get_parent().lookup_rec(base_name_to_lookup, kind, id_set);
}
void cpp_scopet::lookup_rec(
const irep_idt &base_name_to_lookup,
lookup_kindt kind,
cpp_idt::id_classt identifier_class,
id_sett &id_set)
{
// we have a hack to do full search in case we
// are looking for templates!
#if 0
std::cout << "B: " << base_name_to_lookup << '\n';
std::cout << "K: " << kind << '\n';
std::cout << "I: " << identifier_class << '\n';
std::cout << "THIS: " << base_name << " " << identifier_class
<< " " << this->identifier << '\n';
#endif
cpp_id_mapt::iterator lower_it = sub.lower_bound(base_name_to_lookup);
if(lower_it!=sub.end())
{
cpp_id_mapt::iterator upper_it = sub.upper_bound(base_name_to_lookup);
for(cpp_id_mapt::iterator n_it=lower_it;
n_it!=upper_it; n_it++)
{
if(n_it->second.id_class == identifier_class)
id_set.insert(&n_it->second);
}
}
if(base_name == base_name_to_lookup && id_class == identifier_class)
id_set.insert(this);
if(kind==SCOPE_ONLY)
return; // done
// using scopes
for(const auto &s_ptr : using_scopes)
{
cpp_scopet &other_scope = static_cast<cpp_scopet &>(*s_ptr);
// Recursive call.
// Note the different kind!
other_scope.lookup_rec(
base_name_to_lookup, QUALIFIED, identifier_class, id_set);
}
if(!id_set.empty() && identifier_class != id_classt::TEMPLATE)
return; // done, upwards scopes are hidden
// secondary scopes
for(const auto &s_ptr : secondary_scopes)
{
cpp_scopet &other_scope = static_cast<cpp_scopet &>(*s_ptr);
// Recursive call.
// Note the different kind!
other_scope.lookup_rec(
base_name_to_lookup, QUALIFIED, identifier_class, id_set);
}
if(kind==QUALIFIED)
return; // done
if(!id_set.empty() && identifier_class != id_classt::TEMPLATE)
return; // done, upwards scopes are hidden
// ask parent, recursive call
if(!is_root_scope())
get_parent().lookup_rec(
base_name_to_lookup, kind, identifier_class, id_set);
}
cpp_scopet::id_sett cpp_scopet::lookup_identifier(
const irep_idt &id,
cpp_idt::id_classt identifier_class)
{
id_sett id_set;
for(cpp_id_mapt::iterator n_it=sub.begin();
n_it!=sub.end(); n_it++)
{
if(
n_it->second.identifier == id &&
n_it->second.id_class == identifier_class)
{
id_set.insert(&n_it->second);
}
}
if(identifier == id && id_class == identifier_class)
id_set.insert(this);
#if 0
for(std::size_t i=0; i<parents_size(); i++)
{
cpp_idt &parent= get_parent(i);
if(parent.identifier == id
&& parent.id_class == identifier_class)
id_set.insert(&parent);
}
#endif
return id_set;
}
cpp_scopet &cpp_scopet::new_scope(const irep_idt &new_scope_name)
{
cpp_idt &id=insert(new_scope_name);
id.identifier=prefix+id2string(new_scope_name);
id.prefix=prefix+id2string(new_scope_name)+"::";
id.this_expr=this_expr;
id.class_identifier=class_identifier;
id.is_scope=true;
return (cpp_scopet &)id;
}
bool cpp_scopet::contains(const irep_idt &base_name_to_lookup)
{
return !lookup(base_name_to_lookup, SCOPE_ONLY).empty();
}