Skip to content

Commit a9d802b

Browse files
NathanJPhillipssmowton
authored andcommitted
Made lookup return optional
This was requested in review and removes a lot of double map lookups (has_symbol followed by lookup)
1 parent d1d502f commit a9d802b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+194
-164
lines changed

src/ansi-c/ansi_c_entry_point.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void record_function_outputs(
7272
codet output(ID_output);
7373
output.operands().resize(2);
7474

75-
const symbolt &return_symbol=symbol_table.lookup("return'");
75+
const symbolt &return_symbol=*symbol_table.lookup("return'");
7676

7777
output.op0()=
7878
address_of_exprt(

src/ansi-c/c_typecheck_base.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ void c_typecheck_baset::typecheck_symbol(symbolt &symbol)
121121
throw 0;
122122
}
123123

124-
symbolt & existing_symbol=symbol_table.get_writeable(symbol.name);
124+
symbolt & existing_symbol=*symbol_table.get_writeable(symbol.name);
125125
if(symbol.is_type)
126126
typecheck_redefinition_type(existing_symbol, symbol);
127127
else
@@ -733,7 +733,7 @@ void c_typecheck_baset::typecheck_declaration(
733733
// add code contract (if any); we typecheck this after the
734734
// function body done above, so as to have parameter symbols
735735
// available
736-
symbolt &new_symbol=symbol_table.get_writeable(identifier);
736+
symbolt &new_symbol=*symbol_table.get_writeable(identifier);
737737

738738
typecheck_spec_expr(contract, ID_C_spec_requires);
739739

src/ansi-c/c_typecheck_type.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ void c_typecheck_baset::typecheck_type(typet &type)
208208
{
209209
const irep_idt &tag_name=
210210
to_c_enum_tag_type(type.subtype()).get_identifier();
211-
symbol_table.get_writeable(tag_name).type.subtype()=result;
211+
symbol_table.get_writeable(tag_name)->get().type.subtype()=result;
212212
}
213213

214214
type=result;
@@ -782,7 +782,7 @@ void c_typecheck_baset::typecheck_compound_type(struct_union_typet &type)
782782
type.set(ID_tag, base_name);
783783

784784
typecheck_compound_body(type);
785-
symbol_table.get_writeable(s_it->first).type.swap(type);
785+
symbol_table.get_writeable(s_it->first)->get().type.swap(type);
786786
}
787787
}
788788
else if(have_body)
@@ -1220,7 +1220,7 @@ void c_typecheck_baset::typecheck_c_enum_type(typet &type)
12201220
{
12211221
// Ok, overwrite the type in the symbol table.
12221222
// This gives us the members and the subtype.
1223-
symbol_table.get_writeable(symbol.name).type=enum_tag_symbol.type;
1223+
symbol_table.get_writeable(symbol.name)->get().type=enum_tag_symbol.type;
12241224
}
12251225
else if(symbol.type.id()==ID_c_enum)
12261226
{

src/cpp/cpp_declarator_converter.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ symbolt &cpp_declarator_convertert::convert(
9898
}
9999

100100
// try static first
101-
if(!cpp_typecheck.symbol_table.has_symbol(final_identifier))
101+
symbol_tablet::opt_symbol_reft maybe_symbol=
102+
cpp_typecheck.symbol_table.get_writeable(final_identifier);
103+
104+
if(!maybe_symbol)
102105
{
103106
// adjust type if it's a non-static member function
104107
if(final_type.id()==ID_code)
@@ -108,7 +111,8 @@ symbolt &cpp_declarator_convertert::convert(
108111
get_final_identifier();
109112

110113
// try again
111-
if(!cpp_typecheck.symbol_table.has_symbol(final_identifier))
114+
maybe_symbol=cpp_typecheck.symbol_table.get_writeable(final_identifier);
115+
if(!maybe_symbol)
112116
{
113117
cpp_typecheck.error().source_location=
114118
declarator.name().source_location();
@@ -120,7 +124,7 @@ symbolt &cpp_declarator_convertert::convert(
120124
}
121125
}
122126

123-
symbolt &symbol=cpp_typecheck.symbol_table.get_writeable(final_identifier);
127+
symbolt &symbol=*maybe_symbol;
124128

125129
combine_types(declarator.name().source_location(), final_type, symbol);
126130
enforce_rules(symbol);
@@ -187,10 +191,11 @@ symbolt &cpp_declarator_convertert::convert(
187191
}
188192

189193
// already there?
190-
if(!cpp_typecheck.symbol_table.has_symbol(final_identifier))
194+
symbol_tablet::opt_symbol_reft maybe_symbol=
195+
cpp_typecheck.symbol_table.get_writeable(final_identifier);
196+
if(!maybe_symbol)
191197
return convert_new_symbol(storage_spec, member_spec, declarator);
192-
193-
symbolt &symbol=cpp_typecheck.symbol_table.get_writeable(final_identifier);
198+
symbolt &symbol=*maybe_symbol;
194199

195200
if(!storage_spec.is_extern())
196201
symbol.is_extern = false;

src/cpp/cpp_instantiate_template.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ const symbolt &cpp_typecheckt::instantiate_template(
372372
// been instantiated using these arguments
373373
{
374374
// need non-const handle on template symbol
375-
symbolt &s=symbol_table.get_writeable(template_symbol.name);
375+
symbolt &s=*symbol_table.get_writeable(template_symbol.name);
376376
irept &instantiated_with=s.value.add("instantiated_with");
377377
instantiated_with.get_sub().push_back(specialization_template_args);
378378
}
@@ -451,7 +451,7 @@ const symbolt &cpp_typecheckt::instantiate_template(
451451

452452
if(is_template_method)
453453
{
454-
symbolt &symb=symbol_table.get_writeable(class_name);
454+
symbolt &symb=*symbol_table.get_writeable(class_name);
455455

456456
assert(new_decl.declarators().size() == 1);
457457

src/cpp/cpp_typecheck.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void cpp_typecheckt::static_and_dynamic_initialization()
153153

154154
for(const irep_idt &d_it : dynamic_initializations)
155155
{
156-
const symbolt &symbol=symbol_table.lookup(d_it);
156+
const symbolt &symbol=*symbol_table.lookup(d_it);
157157

158158
if(symbol.is_extern)
159159
continue;
@@ -178,7 +178,7 @@ void cpp_typecheckt::static_and_dynamic_initialization()
178178

179179
// Make it nil to get zero initialization by
180180
// __CPROVER_initialize
181-
symbol_table.get_writeable(d_it).value.make_nil();
181+
symbol_table.get_writeable(d_it)->get().value.make_nil();
182182
}
183183
else
184184
{
@@ -231,7 +231,7 @@ void cpp_typecheckt::do_not_typechecked()
231231
symbol.value.get_bool("is_used"))
232232
{
233233
assert(symbol.type.id()==ID_code);
234-
symbolt &symbol=symbol_table.get_writeable(named_symbol.first);
234+
symbolt &symbol=*symbol_table.get_writeable(named_symbol.first);
235235

236236
if(symbol.base_name=="operator=")
237237
{
@@ -260,7 +260,7 @@ void cpp_typecheckt::do_not_typechecked()
260260
for(const auto &named_symbol : symbol_table.symbols)
261261
{
262262
if(named_symbol.second.value.id()=="cpp_not_typechecked")
263-
symbol_table.get_writeable(named_symbol.first).value.make_nil();
263+
symbol_table.get_writeable(named_symbol.first)->get().value.make_nil();
264264
}
265265
}
266266

@@ -285,8 +285,8 @@ void cpp_typecheckt::clean_up()
285285
symbol.type.id()==ID_union)
286286
{
287287
// remove methods from 'components'
288-
struct_union_typet &struct_union_type=
289-
to_struct_union_type(symbol_table.get_writeable(cur_it->first).type);
288+
struct_union_typet &struct_union_type=to_struct_union_type(
289+
symbol_table.get_writeable(cur_it->first)->get().type);
290290

291291
const struct_union_typet::componentst &components=
292292
struct_union_type.components();

src/cpp/cpp_typecheck_compound_type.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,19 @@ void cpp_typecheckt::typecheck_compound_type(
159159

160160
// check if we have it already
161161

162-
if(symbol_table.has_symbol(symbol_name))
162+
symbol_tablet::opt_const_symbol_reft maybe_symbol=
163+
symbol_table.lookup(symbol_name);
164+
if(maybe_symbol)
163165
{
164166
// we do!
165-
const symbolt &symbol=symbol_table.lookup(symbol_name);
167+
const symbolt &symbol=*maybe_symbol;
166168

167169
if(has_body)
168170
{
169171
if(symbol.type.id()=="incomplete_"+type.id_string())
170172
{
171173
// a previously incomplete struct/union becomes complete
172-
symbolt &symbol=symbol_table.get_writeable(symbol_name);
174+
symbolt &symbol=*symbol_table.get_writeable(symbol_name);
173175
symbol.type.swap(type);
174176
typecheck_compound_body(symbol);
175177
}
@@ -551,7 +553,7 @@ void cpp_typecheckt::typecheck_compound_declarator(
551553
put_compound_into_scope(compo);
552554
}
553555

554-
typet &vt=symbol_table.get_writeable(vt_name).type;
556+
typet &vt=symbol_table.get_writeable(vt_name)->get().type;
555557
INVARIANT(vt.id()==ID_struct, "Virtual tables must be stored as struct");
556558
struct_typet &virtual_table=to_struct_type(vt);
557559

@@ -1408,7 +1410,7 @@ void cpp_typecheckt::convert_anon_struct_union_member(
14081410
struct_typet::componentst &components)
14091411
{
14101412
symbolt &struct_union_symbol=
1411-
symbol_table.get_writeable(follow(declaration.type()).get(ID_name));
1413+
*symbol_table.get_writeable(follow(declaration.type()).get(ID_name));
14121414

14131415
if(declaration.storage_spec().is_static() ||
14141416
declaration.storage_spec().is_mutable())

src/cpp/cpp_typecheck_declaration.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void cpp_typecheckt::convert_anonymous_union(
7575

7676
// do scoping
7777
symbolt union_symbol=
78-
symbol_table.get_writeable(follow(symbol.type).get(ID_name));
78+
*symbol_table.get_writeable(follow(symbol.type).get(ID_name));
7979
const irept::subt &components=union_symbol.type.add(ID_components).get_sub();
8080

8181
forall_irep(it, components)
@@ -105,7 +105,7 @@ void cpp_typecheckt::convert_anonymous_union(
105105
id.is_member=true;
106106
}
107107

108-
symbol_table.get_writeable(union_symbol.name).type.set(
108+
symbol_table.get_writeable(union_symbol.name)->get().type.set(
109109
"#unnamed_object", symbol.base_name);
110110

111111
code.swap(new_code);

src/cpp/cpp_typecheck_expr.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,8 @@ void cpp_typecheckt::typecheck_expr_member(
12441244
assert(it!=symbol_table.symbols.end());
12451245

12461246
if(it->second.value.id()=="cpp_not_typechecked")
1247-
symbol_table.get_writeable(component_name).value.set("is_used", true);
1247+
symbol_table.get_writeable(component_name)->get()
1248+
.value.set("is_used", true);
12481249
}
12491250
}
12501251

@@ -2202,7 +2203,7 @@ void cpp_typecheckt::typecheck_side_effect_function_call(
22022203
type.id()==ID_code &&
22032204
type.find(ID_return_type).id()==ID_destructor)
22042205
{
2205-
add_method_body(&symbol_table.get_writeable(it->get(ID_name)));
2206+
add_method_body(&symbol_table.get_writeable(it->get(ID_name))->get());
22062207
break;
22072208
}
22082209
}
@@ -2371,7 +2372,7 @@ void cpp_typecheckt::typecheck_method_application(
23712372
member_expr.swap(expr.function());
23722373

23732374
const symbolt &symbol=lookup(member_expr.get(ID_component_name));
2374-
add_method_body(&symbol_table.get_writeable(symbol.name));
2375+
add_method_body(&symbol_table.get_writeable(symbol.name)->get());
23752376

23762377
// build new function expression
23772378
exprt new_function(cpp_symbol_expr(symbol));
@@ -2413,7 +2414,7 @@ void cpp_typecheckt::typecheck_method_application(
24132414
if(symbol.value.id()=="cpp_not_typechecked" &&
24142415
!symbol.value.get_bool("is_used"))
24152416
{
2416-
symbol_table.get_writeable(symbol.name).value.set("is_used", true);
2417+
symbol_table.get_writeable(symbol.name)->get().value.set("is_used", true);
24172418
}
24182419
}
24192420

@@ -2682,7 +2683,7 @@ void cpp_typecheckt::typecheck_expr_function_identifier(exprt &expr)
26822683
assert(it != symbol_table.symbols.end());
26832684

26842685
if(it->second.value.id()=="cpp_not_typechecked")
2685-
symbol_table.get_writeable(it->first).value.set("is_used", true);
2686+
symbol_table.get_writeable(it->first)->get().value.set("is_used", true);
26862687
}
26872688

26882689
c_typecheck_baset::typecheck_expr_function_identifier(expr);

src/cpp/cpp_typecheck_template.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,12 @@ void cpp_typecheckt::typecheck_class_template(
107107

108108
// check if we have it already
109109

110-
if(symbol_table.has_symbol(symbol_name))
110+
symbol_tablet::opt_symbol_reft maybe_symbol=
111+
symbol_table.get_writeable(symbol_name);
112+
if(maybe_symbol)
111113
{
112114
// there already
113-
auto &previous_symbol=symbol_table.get_writeable(symbol_name);
115+
symbolt &previous_symbol=*maybe_symbol;
114116
cpp_declarationt &previous_declaration=
115117
to_cpp_declaration(previous_symbol.type);
116118

@@ -263,7 +265,7 @@ void cpp_typecheckt::typecheck_function_template(
263265

264266
if(has_value)
265267
{
266-
symbol_table.get_writeable(symbol_name).type.swap(declaration);
268+
symbol_table.get_writeable(symbol_name)->get().type.swap(declaration);
267269
cpp_scopes.id_map[symbol_name]=&template_scope;
268270
}
269271

@@ -382,7 +384,7 @@ void cpp_typecheckt::typecheck_class_template_member(
382384

383385
const cpp_idt &cpp_id=**(id_set.begin());
384386
symbolt &template_symbol=
385-
symbol_table.get_writeable(cpp_id.identifier);
387+
*symbol_table.get_writeable(cpp_id.identifier);
386388

387389
exprt &template_methods=static_cast<exprt &>(
388390
template_symbol.value.add("template_methods"));

0 commit comments

Comments
 (0)