1111// / there is a symbol with the same name already in the symbol table.
1212bool 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)
5363bool 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
0 commit comments