Skip to content

Commit 63bf323

Browse files
author
Daniel Kroening
authored
Merge pull request #3063 from diffblue/cpp_scopet_lookup
cpp_scopet::lookup methods now return an id set
2 parents 2e84af4 + 89b2bb3 commit 63bf323

8 files changed

+80
-90
lines changed

src/cpp/cpp_declarator_converter.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,8 @@ symbolt &cpp_declarator_convertert::convert(
207207

208208
if(symbol.type.id()=="cpp-template-type")
209209
{
210-
cpp_scopet::id_sett id_set;
211-
212-
scope->lookup_identifier(
213-
symbol.name, cpp_idt::id_classt::TEMPLATE_PARAMETER, id_set);
210+
const auto id_set = scope->lookup_identifier(
211+
symbol.name, cpp_idt::id_classt::TEMPLATE_PARAMETER);
214212

215213
if(id_set.empty())
216214
{
@@ -496,10 +494,8 @@ symbolt &cpp_declarator_convertert::convert_new_symbol(
496494

497495
if(!is_code)
498496
{
499-
cpp_scopest::id_sett id_set;
500-
501-
cpp_typecheck.cpp_scopes.current_scope().lookup(
502-
base_name, cpp_scopet::SCOPE_ONLY, id_set);
497+
const auto id_set = cpp_typecheck.cpp_scopes.current_scope().lookup(
498+
base_name, cpp_scopet::SCOPE_ONLY);
503499

504500
for(cpp_scopest::id_sett::const_iterator
505501
id_it=id_set.begin();

src/cpp/cpp_instantiate_template.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ const symbolt &cpp_typecheckt::instantiate_template(
335335
{
336336
cpp_scopet &scope=cpp_scopes.get_scope(subscope_name);
337337

338-
cpp_scopet::id_sett id_set;
339-
scope.lookup(template_symbol.base_name, cpp_scopet::SCOPE_ONLY, id_set);
338+
const auto id_set =
339+
scope.lookup(template_symbol.base_name, cpp_scopet::SCOPE_ONLY);
340340

341341
if(id_set.size()==1)
342342
{

src/cpp/cpp_scope.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ std::ostream &operator << (std::ostream &out, cpp_scopet::lookup_kindt kind)
2626
return out;
2727
}
2828

29-
void cpp_scopet::lookup(
29+
void cpp_scopet::lookup_rec(
3030
const irep_idt &base_name,
3131
lookup_kindt kind,
3232
id_sett &id_set)
@@ -60,7 +60,7 @@ void cpp_scopet::lookup(
6060

6161
// Recursive call.
6262
// Note the different kind!
63-
other_scope.lookup(base_name, QUALIFIED, id_set);
63+
other_scope.lookup_rec(base_name, QUALIFIED, id_set);
6464
}
6565

6666
if(!id_set.empty())
@@ -76,20 +76,21 @@ void cpp_scopet::lookup(
7676

7777
// Recursive call.
7878
// Note the different kind!
79-
other_scope.lookup(base_name, QUALIFIED, id_set);
79+
other_scope.lookup_rec(base_name, QUALIFIED, id_set);
8080
}
8181

8282
if(kind==QUALIFIED)
8383
return; // done
84+
8485
if(!id_set.empty())
8586
return; // done
8687

8788
// ask parent, recursive call
8889
if(!is_root_scope())
89-
get_parent().lookup(base_name, kind, id_set);
90+
get_parent().lookup_rec(base_name, kind, id_set);
9091
}
9192

92-
void cpp_scopet::lookup(
93+
void cpp_scopet::lookup_rec(
9394
const irep_idt &base_name,
9495
lookup_kindt kind,
9596
cpp_idt::id_classt id_class,
@@ -139,7 +140,7 @@ void cpp_scopet::lookup(
139140

140141
// Recursive call.
141142
// Note the different kind!
142-
other_scope.lookup(base_name, QUALIFIED, id_class, id_set);
143+
other_scope.lookup_rec(base_name, QUALIFIED, id_class, id_set);
143144
}
144145

145146
if(!id_set.empty() && id_class != id_classt::TEMPLATE)
@@ -155,7 +156,7 @@ void cpp_scopet::lookup(
155156

156157
// Recursive call.
157158
// Note the different kind!
158-
other_scope.lookup(base_name, QUALIFIED, id_class, id_set);
159+
other_scope.lookup_rec(base_name, QUALIFIED, id_class, id_set);
159160
}
160161

161162
if(kind==QUALIFIED)
@@ -166,14 +167,15 @@ void cpp_scopet::lookup(
166167

167168
// ask parent, recursive call
168169
if(!is_root_scope())
169-
get_parent().lookup(base_name, kind, id_class, id_set);
170+
get_parent().lookup_rec(base_name, kind, id_class, id_set);
170171
}
171172

172-
void cpp_scopet::lookup_identifier(
173+
cpp_scopet::id_sett cpp_scopet::lookup_identifier(
173174
const irep_idt &identifier,
174-
cpp_idt::id_classt id_class,
175-
id_sett &id_set)
175+
cpp_idt::id_classt id_class)
176176
{
177+
id_sett id_set;
178+
177179
for(cpp_id_mapt::iterator n_it=sub.begin();
178180
n_it!=sub.end(); n_it++)
179181
{
@@ -195,6 +197,8 @@ void cpp_scopet::lookup_identifier(
195197
id_set.insert(&parent);
196198
}
197199
#endif
200+
201+
return id_set;
198202
}
199203

200204
cpp_scopet &cpp_scopet::new_scope(const irep_idt &new_scope_name)
@@ -208,10 +212,7 @@ cpp_scopet &cpp_scopet::new_scope(const irep_idt &new_scope_name)
208212
return (cpp_scopet &)id;
209213
}
210214

211-
212215
bool cpp_scopet::contains(const irep_idt &base_name)
213216
{
214-
id_sett id_set;
215-
lookup(base_name, SCOPE_ONLY, id_set);
216-
return !id_set.empty();
217+
return !lookup(base_name, SCOPE_ONLY).empty();
217218
}

src/cpp/cpp_scope.h

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,25 @@ class cpp_scopet:public cpp_idt
2929

3030
enum lookup_kindt { SCOPE_ONLY, QUALIFIED, RECURSIVE };
3131

32-
void lookup(
33-
const irep_idt &base_name,
34-
lookup_kindt kind,
35-
id_sett &id_set);
32+
id_sett lookup(const irep_idt &base_name, lookup_kindt kind)
33+
{
34+
id_sett result;
35+
lookup_rec(base_name, kind, result);
36+
return result;
37+
}
3638

37-
void lookup(
39+
id_sett lookup(
3840
const irep_idt &base_name,
3941
lookup_kindt kind,
40-
cpp_idt::id_classt id_class,
41-
id_sett &id_set);
42+
cpp_idt::id_classt id_class)
43+
{
44+
id_sett result;
45+
lookup_rec(base_name, kind, id_class, result);
46+
return result;
47+
}
4248

43-
void lookup_identifier(
44-
const irep_idt &identifier,
45-
cpp_idt::id_classt id_class,
46-
id_sett &id_set);
49+
id_sett
50+
lookup_identifier(const irep_idt &identifier, cpp_idt::id_classt id_class);
4751

4852
cpp_idt &insert(const irep_idt &_base_name)
4953
{
@@ -114,6 +118,15 @@ class cpp_scopet:public cpp_idt
114118
}
115119

116120
class cpp_scopet &new_scope(const irep_idt &new_scope_name);
121+
122+
protected:
123+
void lookup_rec(const irep_idt &base_name, lookup_kindt kind, id_sett &);
124+
125+
void lookup_rec(
126+
const irep_idt &base_name,
127+
lookup_kindt kind,
128+
cpp_idt::id_classt id_class,
129+
id_sett &);
117130
};
118131

119132
class cpp_root_scopet:public cpp_scopet

src/cpp/cpp_typecheck_compound_type.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ cpp_scopet &cpp_typecheckt::tag_scope(
105105
// Check if we have it already. If so, take it.
106106

107107
// we should only look for tags, but we don't
108-
cpp_scopet::id_sett id_set;
109-
cpp_scopes.current_scope().lookup(base_name, cpp_scopet::RECURSIVE, id_set);
108+
const auto id_set =
109+
cpp_scopes.current_scope().lookup(base_name, cpp_scopet::RECURSIVE);
110110

111111
for(const auto &id : id_set)
112112
if(id->is_class())
@@ -816,10 +816,8 @@ void cpp_typecheckt::put_compound_into_scope(
816816
else
817817
{
818818
// check if it's already there
819-
cpp_scopest::id_sett id_set;
820-
821-
cpp_scopes.current_scope().lookup(
822-
base_name, cpp_scopet::SCOPE_ONLY, id_set);
819+
const auto id_set =
820+
cpp_scopes.current_scope().lookup(base_name, cpp_scopet::SCOPE_ONLY);
823821

824822
for(const auto &id_it : id_set)
825823
{

src/cpp/cpp_typecheck_resolve.cpp

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -914,15 +914,12 @@ cpp_scopet &cpp_typecheck_resolvet::resolve_scope(
914914
template_args=to_cpp_template_args_non_tc(*pos);
915915
else if(pos->id()=="::")
916916
{
917-
cpp_scopest::id_sett id_set;
918-
919917
if(template_args.is_not_nil())
920918
{
921-
cpp_typecheck.cpp_scopes.current_scope().lookup(
919+
const auto id_set = cpp_typecheck.cpp_scopes.current_scope().lookup(
922920
final_base_name,
923-
recursive?cpp_scopet::RECURSIVE:cpp_scopet::QUALIFIED,
924-
cpp_idt::id_classt::TEMPLATE,
925-
id_set);
921+
recursive ? cpp_scopet::RECURSIVE : cpp_scopet::QUALIFIED,
922+
cpp_idt::id_classt::TEMPLATE);
926923

927924
#ifdef DEBUG
928925
std::cout << "S: "
@@ -946,10 +943,9 @@ cpp_scopet &cpp_typecheck_resolvet::resolve_scope(
946943
}
947944
else
948945
{
949-
cpp_typecheck.cpp_scopes.current_scope().lookup(
946+
auto id_set = cpp_typecheck.cpp_scopes.current_scope().lookup(
950947
final_base_name,
951-
recursive?cpp_scopet::RECURSIVE:cpp_scopet::QUALIFIED,
952-
id_set);
948+
recursive ? cpp_scopet::RECURSIVE : cpp_scopet::QUALIFIED);
953949

954950
filter_for_named_scopes(id_set);
955951

@@ -1259,12 +1255,8 @@ cpp_scopet &cpp_typecheck_resolvet::resolve_namespace(
12591255
const source_locationt &source_location=cpp_name.source_location();
12601256
bool qualified=cpp_name.is_qualified();
12611257

1262-
cpp_scopest::id_sett id_set;
1263-
1264-
cpp_typecheck.cpp_scopes.current_scope().lookup(
1265-
base_name,
1266-
qualified?cpp_scopet::QUALIFIED:cpp_scopet::RECURSIVE,
1267-
id_set);
1258+
auto id_set = cpp_typecheck.cpp_scopes.current_scope().lookup(
1259+
base_name, qualified ? cpp_scopet::QUALIFIED : cpp_scopet::RECURSIVE);
12681260

12691261
filter_for_namespaces(id_set);
12701262

@@ -1442,8 +1434,8 @@ exprt cpp_typecheck_resolvet::resolve(
14421434

14431435
if(template_args.is_nil())
14441436
{
1445-
cpp_typecheck.cpp_scopes.current_scope().lookup(
1446-
base_name, lookup_kind, id_set);
1437+
id_set =
1438+
cpp_typecheck.cpp_scopes.current_scope().lookup(base_name, lookup_kind);
14471439

14481440
if(id_set.empty() && !cpp_typecheck.builtin_factory(base_name))
14491441
{
@@ -1456,8 +1448,8 @@ exprt cpp_typecheck_resolvet::resolve(
14561448
}
14571449
}
14581450
else
1459-
cpp_typecheck.cpp_scopes.current_scope().lookup(
1460-
base_name, lookup_kind, cpp_idt::id_classt::TEMPLATE, id_set);
1451+
id_set = cpp_typecheck.cpp_scopes.current_scope().lookup(
1452+
base_name, lookup_kind, cpp_idt::id_classt::TEMPLATE);
14611453

14621454
// Argument-dependent name lookup
14631455
#if 0
@@ -1765,9 +1757,8 @@ void cpp_typecheck_resolvet::guess_template_args(
17651757
irep_idt base_name;
17661758
resolve_scope(cpp_name, base_name, template_args);
17671759

1768-
cpp_scopest::id_sett id_set;
1769-
cpp_typecheck.cpp_scopes.current_scope().lookup(
1770-
base_name, cpp_scopet::RECURSIVE, id_set);
1760+
const auto id_set = cpp_typecheck.cpp_scopes.current_scope().lookup(
1761+
base_name, cpp_scopet::RECURSIVE);
17711762

17721763
// alright, rummage through these
17731764
for(cpp_scopest::id_sett::const_iterator it=id_set.begin();
@@ -1855,9 +1846,8 @@ void cpp_typecheck_resolvet::guess_template_args(
18551846
cpp_template_args_non_tct template_args;
18561847
resolve_scope(cpp_name, base_name, template_args);
18571848

1858-
cpp_scopest::id_sett id_set;
1859-
cpp_typecheck.cpp_scopes.current_scope().lookup(
1860-
base_name, cpp_scopet::RECURSIVE, id_set);
1849+
const auto id_set = cpp_typecheck.cpp_scopes.current_scope().lookup(
1850+
base_name, cpp_scopet::RECURSIVE);
18611851

18621852
// alright, rummage through these
18631853
for(cpp_scopest::id_sett::const_iterator
@@ -2412,10 +2402,9 @@ void cpp_typecheck_resolvet::resolve_with_arguments(
24122402
if(final_type.id()!=ID_struct && final_type.id()!=ID_union)
24132403
continue;
24142404

2415-
cpp_scopest::id_sett tmp_set;
24162405
cpp_scopet &scope=
24172406
cpp_typecheck.cpp_scopes.get_scope(final_type.get(ID_name));
2418-
scope.lookup(base_name, cpp_scopet::SCOPE_ONLY, tmp_set);
2407+
const auto tmp_set = scope.lookup(base_name, cpp_scopet::SCOPE_ONLY);
24192408
id_set.insert(tmp_set.begin(), tmp_set.end());
24202409
}
24212410
}

src/cpp/cpp_typecheck_template.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,11 @@ void cpp_typecheckt::typecheck_class_template(
8383
// Check if the name is already used by a different template
8484
// in the same scope.
8585
{
86-
cpp_scopet::id_sett id_set;
87-
cpp_scopes.current_scope().lookup(
88-
base_name,
89-
cpp_scopet::SCOPE_ONLY,
90-
cpp_scopet::TEMPLATE,
91-
id_set);
86+
const auto id_set=
87+
cpp_scopes.current_scope().lookup(
88+
base_name,
89+
cpp_scopet::SCOPE_ONLY,
90+
cpp_scopet::TEMPLATE);
9291

9392
if(!id_set.empty())
9493
{
@@ -345,13 +344,10 @@ void cpp_typecheckt::typecheck_class_template_member(
345344
}
346345

347346
// let's find the class template this function template belongs to.
348-
cpp_scopet::id_sett id_set;
349-
350-
cpp_scopes.current_scope().lookup(
347+
const auto id_set = cpp_scopes.current_scope().lookup(
351348
cpp_name.get_sub().front().get(ID_identifier),
352-
cpp_scopet::SCOPE_ONLY, // look only in current scope
353-
cpp_scopet::id_classt::TEMPLATE, // must be template
354-
id_set);
349+
cpp_scopet::SCOPE_ONLY, // look only in current scope
350+
cpp_scopet::id_classt::TEMPLATE); // must be template
355351

356352
if(id_set.empty())
357353
{
@@ -539,9 +535,8 @@ void cpp_typecheckt::convert_class_template_specialization(
539535

540536
// get the template symbol
541537

542-
cpp_scopest::id_sett id_set;
543-
cpp_scopes.current_scope().lookup(
544-
base_name, cpp_scopet::SCOPE_ONLY, cpp_idt::id_classt::TEMPLATE, id_set);
538+
auto id_set = cpp_scopes.current_scope().lookup(
539+
base_name, cpp_scopet::SCOPE_ONLY, cpp_idt::id_classt::TEMPLATE);
545540

546541
// remove any specializations
547542
for(cpp_scopest::id_sett::iterator
@@ -656,9 +651,8 @@ void cpp_typecheckt::convert_template_function_or_member_specialization(
656651
std::string base_name=
657652
cpp_name.get_sub()[0].get(ID_identifier).c_str();
658653

659-
cpp_scopest::id_sett id_set;
660-
cpp_scopes.current_scope().lookup(
661-
base_name, cpp_scopet::SCOPE_ONLY, id_set);
654+
const auto id_set =
655+
cpp_scopes.current_scope().lookup(base_name, cpp_scopet::SCOPE_ONLY);
662656

663657
if(id_set.empty())
664658
{

src/cpp/cpp_typecheck_using.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ void cpp_typecheckt::convert(cpp_usingt &cpp_using)
2727
resolver.resolve_scope(cpp_using.name(), base_name, template_args);
2828

2929
bool qualified=cpp_using.name().is_qualified();
30-
cpp_scopest::id_sett id_set;
3130

32-
cpp_scopes.current_scope().lookup(
33-
base_name, qualified?cpp_scopet::QUALIFIED:cpp_scopet::RECURSIVE, id_set);
31+
const auto id_set = cpp_scopes.current_scope().lookup(
32+
base_name, qualified ? cpp_scopet::QUALIFIED : cpp_scopet::RECURSIVE);
3433

3534
bool using_directive=cpp_using.get_namespace();
3635

0 commit comments

Comments
 (0)