Skip to content

Commit 9f4e933

Browse files
NathanJPhillipssmowton
authored andcommitted
Changed interface to symbol_tablet::insert
1 parent e35f2fc commit 9f4e933

10 files changed

+64
-77
lines changed

src/ansi-c/ansi_c_entry_point.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ bool generate_ansi_c_start_function(
478478
new_symbol.value.swap(init_code);
479479
new_symbol.mode=symbol.mode;
480480

481-
if(!symbol_table.insert(std::move(new_symbol)))
481+
if(!symbol_table.insert(std::move(new_symbol)).second)
482482
{
483483
messaget message;
484484
message.set_message_handler(message_handler);

src/cpp/cpp_typecheck_compound_type.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ void cpp_typecheckt::typecheck_compound_declarator(
536536
vt_symb_type.type.set(ID_name, vt_symb_type.name);
537537
vt_symb_type.is_type=true;
538538

539-
const bool failed=!symbol_table.insert(std::move(vt_symb_type));
539+
const bool failed=!symbol_table.insert(std::move(vt_symb_type)).second;
540540
assert(!failed);
541541

542542
// add a virtual-table pointer
@@ -612,7 +612,7 @@ void cpp_typecheckt::typecheck_compound_declarator(
612612
arg.set(ID_C_identifier, arg_symb.name);
613613

614614
// add the parameter to the symbol table
615-
const bool failed=!symbol_table.insert(std::move(arg_symb));
615+
const bool failed=!symbol_table.insert(std::move(arg_symb)).second;
616616
assert(!failed);
617617
}
618618

@@ -670,7 +670,7 @@ void cpp_typecheckt::typecheck_compound_declarator(
670670

671671
// add the function to the symbol table
672672
{
673-
const bool failed=!symbol_table.insert(std::move(func_symb));
673+
const bool failed=!symbol_table.insert(std::move(func_symb)).second;
674674
assert(!failed);
675675
}
676676

src/cpp/cpp_typecheck_namespace.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void cpp_typecheckt::convert(cpp_namespace_spect &namespace_spec)
7373
symbol.module=module;
7474
symbol.type=typet(ID_namespace);
7575

76-
if(!symbol_table.insert(std::move(symbol)))
76+
if(!symbol_table.insert(std::move(symbol)).second)
7777
{
7878
error().source_location=symbol.location;
7979
error() << "cpp_typecheckt::convert_namespace: symbol_table.move() failed"

src/cpp/cpp_typecheck_virtual_table.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void cpp_typecheckt::do_virtual_table(const symbolt &symbol)
9595
}
9696
vt_symb_var.value=values;
9797

98-
bool failed=!symbol_table.insert(std::move(vt_symb_var));
98+
bool failed=!symbol_table.insert(std::move(vt_symb_var)).second;
9999
assert(!failed);
100100
}
101101
}

src/java_bytecode/java_entry_point.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ bool generate_java_start_function(
668668
new_symbol.value.swap(init_code);
669669
new_symbol.mode=ID_java;
670670

671-
if(!symbol_table.insert(std::move(new_symbol)))
671+
if(!symbol_table.insert(std::move(new_symbol)).second)
672672
{
673673
message.error() << "failed to move main symbol" << messaget::eom;
674674
return true;

src/java_bytecode/java_utils.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,9 @@ void generate_class_stub(
8686
new_symbol.mode=ID_java;
8787
new_symbol.is_type=true;
8888

89-
optionalt<std::reference_wrapper<symbolt>> res=
90-
symbol_table.insert(std::move(new_symbol));
89+
std::pair<symbolt &, bool> res=symbol_table.insert(std::move(new_symbol));
9190

92-
if(!res)
91+
if(!res.second)
9392
{
9493
messaget message(message_handler);
9594
message.warning() <<
@@ -100,7 +99,7 @@ void generate_class_stub(
10099
else
101100
{
102101
// create the class identifier etc
103-
java_root_class(*res);
102+
java_root_class(res.first);
104103
}
105104
}
106105

src/jsil/jsil_entry_point.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ bool jsil_entry_point(
162162
new_symbol.type.swap(main_type);
163163
new_symbol.value.swap(init_code);
164164

165-
if(!symbol_table.insert(std::move(new_symbol)))
165+
if(!symbol_table.insert(std::move(new_symbol)).second)
166166
{
167167
messaget message;
168168
message.set_message_handler(message_handler);

src/util/fresh_symbol.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ symbolt &get_fresh_aux_symbol(
4040
{
4141
auxiliary_symbolt new_symbol;
4242

43-
optionalt<std::reference_wrapper<symbolt>> res;
4443
// Loop until find a name that doesn't clash with a non-auxilliary symbol
45-
while(!res)
44+
while(true)
4645
{
4746
// Distinguish local variables with the same name
4847
new_symbol.base_name=
@@ -54,7 +53,8 @@ symbolt &get_fresh_aux_symbol(
5453
new_symbol.type=type;
5554
new_symbol.location=source_location;
5655
new_symbol.mode=symbol_mode;
57-
res=symbol_table.insert(std::move(new_symbol));
56+
std::pair<symbolt &, bool> res=symbol_table.insert(std::move(new_symbol));
57+
if(res.second)
58+
return res.first;
5859
}
59-
return *res;
6060
}

src/util/symbol_table.cpp

Lines changed: 40 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,38 @@
1111
/// there is a symbol with the same name already in the symbol table.
1212
bool symbol_tablet::add(const symbolt &symbol)
1313
{
14-
std::pair<symbolst::iterator, bool> result=
15-
internal_symbols.emplace(symbol.name, symbol);
16-
if(!result.second)
17-
return true;
18-
add_base_and_module(result.first);
19-
return false;
14+
return !insert(symbol).second;
2015
}
2116

22-
/// Move a new symbol to the symbol table
23-
/// \remark: This is a nicer interface than move but achieves the same result
24-
/// \param symbol: The symbol to be added to the symbol table
25-
/// \return Returns an optional reference to the newly inserted symbol, without
26-
/// a value if a symbol with the same name already exists in the symbol table
27-
symbol_tablet::opt_symbol_reft symbol_tablet::insert(symbolt &&symbol)
17+
std::pair<symbolt &, bool> symbol_tablet::insert(symbolt symbol)
2818
{
2919
// Add the symbol to the table or retrieve existing symbol with the same name
3020
std::pair<symbolst::iterator, bool> result=
3121
internal_symbols.emplace(symbol.name, std::move(symbol));
32-
if(!result.second)
33-
return opt_symbol_reft();
34-
add_base_and_module(result.first);
35-
return std::ref(result.first->second);
22+
symbolt &new_symbol=result.first->second;
23+
if(result.second)
24+
{
25+
try
26+
{
27+
symbol_base_mapt::iterator base_result=
28+
internal_symbol_base_map.emplace(new_symbol.base_name, new_symbol.name);
29+
try
30+
{
31+
internal_symbol_module_map.emplace(new_symbol.module, new_symbol.name);
32+
}
33+
catch(...)
34+
{
35+
internal_symbol_base_map.erase(base_result);
36+
throw;
37+
}
38+
}
39+
catch(...)
40+
{
41+
internal_symbols.erase(result.first);
42+
throw;
43+
}
44+
}
45+
return std::make_pair(std::ref(new_symbol), result.second);
3646
}
3747

3848
/// Move a symbol into the symbol table. If there is already a symbol with the
@@ -53,49 +63,22 @@ symbol_tablet::opt_symbol_reft symbol_tablet::insert(symbolt &&symbol)
5363
bool symbol_tablet::move(symbolt &symbol, symbolt *&new_symbol)
5464
{
5565
// Add an empty symbol to the table or retrieve existing symbol with same name
56-
std::pair<symbolst::iterator, bool> result=
57-
internal_symbols.emplace(symbol.name, symbolt());
58-
59-
if(!result.second)
60-
{
61-
// Return the address of the symbol that already existed in the table
62-
new_symbol=&result.first->second;
63-
return true;
64-
}
65-
66-
// Move the provided symbol into the symbol table
67-
result.first->second.swap(symbol);
68-
69-
add_base_and_module(result.first);
70-
71-
// Return the address of the new symbol in the table
72-
new_symbol=&result.first->second;
73-
74-
return false;
75-
}
76-
77-
void symbol_tablet::add_base_and_module(symbolst::iterator added_symbol)
78-
{
79-
symbolt &symbol=added_symbol->second;
80-
try
81-
{
82-
symbol_base_mapt::iterator base_result=
83-
internal_symbol_base_map.emplace(symbol.base_name, symbol.name);
84-
try
85-
{
86-
internal_symbol_module_map.emplace(symbol.module, symbol.name);
87-
}
88-
catch(...)
89-
{
90-
internal_symbol_base_map.erase(base_result);
91-
throw;
92-
}
93-
}
94-
catch(...)
66+
symbolt temp_symbol;
67+
// This is not copying the symbol, this is passing the three required
68+
// parameters to insert (just in the symbol)
69+
temp_symbol.name=symbol.name;
70+
temp_symbol.base_name=symbol.base_name;
71+
temp_symbol.module=symbol.module;
72+
std::pair<symbolt &, bool> result=insert(std::move(temp_symbol));
73+
if(result.second)
9574
{
96-
internal_symbols.erase(added_symbol);
97-
throw;
75+
// Move the provided symbol into the symbol table, this can't be done
76+
// earlier
77+
result.first.swap(symbol);
9878
}
79+
// Return the address of the symbol in the table
80+
new_symbol=&result.first;
81+
return !result.second;
9982
}
10083

10184
/// Remove a symbol from the symbol table

src/util/symbol_table.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,17 @@ class symbol_tablet
106106
opt_symbol_reft get_writeable(const irep_idt &identifier);
107107

108108
bool add(const symbolt &symbol);
109-
opt_symbol_reft insert(symbolt &&symbol);
109+
/// Move or copy a new symbol to the symbol table
110+
/// \remark: This is a nicer interface than move and achieves the same
111+
/// result as both move and add
112+
/// \param symbol: The symbol to be added to the symbol table - can be
113+
/// moved or copied in
114+
/// \return Returns a reference to the newly inserted symbol or to the
115+
/// existing symbol if a symbol with the same name already exists in the
116+
/// symbol table, along with a bool that is true if a new symbol was inserted.
117+
std::pair<symbolt &, bool> insert(symbolt symbol);
110118
bool move(symbolt &symbol, symbolt *&new_symbol);
111-
private:
112-
void add_base_and_module(symbolst::iterator added_symbol);
113119

114-
public:
115120
void clear()
116121
{
117122
internal_symbols.clear();

0 commit comments

Comments
 (0)