Skip to content

Commit 73a1414

Browse files
authored
Merge pull request #6548 from tautschnig/linking-interface
Linking: do not modify source symbol table
2 parents 8c4d15b + 544fdf9 commit 73a1414

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

src/linking/linking.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ void linkingt::link_warning(
421421
<< type_to_string_verbose(new_symbol) << eom;
422422
}
423423

424-
irep_idt linkingt::rename(const irep_idt id)
424+
irep_idt linkingt::rename(const irep_idt &id)
425425
{
426426
unsigned cnt=0;
427427

@@ -1313,14 +1313,15 @@ void linkingt::do_type_dependencies(
13131313
}
13141314
}
13151315

1316-
void linkingt::rename_symbols(
1316+
std::unordered_map<irep_idt, irep_idt> linkingt::rename_symbols(
13171317
const std::unordered_set<irep_idt> &needs_to_be_renamed)
13181318
{
1319+
std::unordered_map<irep_idt, irep_idt> new_identifiers;
13191320
namespacet src_ns(src_symbol_table);
13201321

13211322
for(const irep_idt &id : needs_to_be_renamed)
13221323
{
1323-
symbolt &new_symbol = src_symbol_table.get_writeable_ref(id);
1324+
const symbolt &new_symbol = src_ns.lookup(id);
13241325

13251326
irep_idt new_identifier;
13261327

@@ -1329,21 +1330,24 @@ void linkingt::rename_symbols(
13291330
else
13301331
new_identifier=rename(id);
13311332

1332-
new_symbol.name=new_identifier;
1333+
new_identifiers.emplace(id, new_identifier);
13331334

1334-
#ifdef DEBUG
1335+
#ifdef DEBUG
13351336
debug() << "LINKING: renaming " << id << " to "
13361337
<< new_identifier << eom;
1337-
#endif
1338+
#endif
13381339

13391340
if(new_symbol.is_type)
13401341
rename_symbol.insert_type(id, new_identifier);
13411342
else
13421343
rename_symbol.insert_expr(id, new_identifier);
13431344
}
1345+
1346+
return new_identifiers;
13441347
}
13451348

1346-
void linkingt::copy_symbols()
1349+
void linkingt::copy_symbols(
1350+
const std::unordered_map<irep_idt, irep_idt> &new_identifiers)
13471351
{
13481352
std::map<irep_idt, symbolt> src_symbols;
13491353
// First apply the renaming
@@ -1353,7 +1357,10 @@ void linkingt::copy_symbols()
13531357
// apply the renaming
13541358
rename_symbol(symbol.type);
13551359
rename_symbol(symbol.value);
1356-
// Add to vector
1360+
auto it = new_identifiers.find(named_symbol.first);
1361+
if(it != new_identifiers.end())
1362+
symbol.name = it->second;
1363+
13571364
src_symbols.emplace(named_symbol.first, std::move(symbol));
13581365
}
13591366

@@ -1435,15 +1442,15 @@ void linkingt::typecheck()
14351442
do_type_dependencies(needs_to_be_renamed);
14361443

14371444
// PHASE 2: actually rename them
1438-
rename_symbols(needs_to_be_renamed);
1445+
auto new_identifiers = rename_symbols(needs_to_be_renamed);
14391446

14401447
// PHASE 3: copy new symbols to main table
1441-
copy_symbols();
1448+
copy_symbols(new_identifiers);
14421449
}
14431450

14441451
bool linking(
14451452
symbol_tablet &dest_symbol_table,
1446-
symbol_tablet &new_symbol_table,
1453+
const symbol_tablet &new_symbol_table,
14471454
message_handlert &message_handler)
14481455
{
14491456
linkingt linking(

src/linking/linking.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ Author: Daniel Kroening, [email protected]
1515
class message_handlert;
1616
class symbol_tablet;
1717

18-
// This merges the symbol table "new_symbol_table" into "dest_symbol_table",
19-
// applying appropriate renamings to symbols in "new_symbol_table"
20-
// when necessary.
21-
18+
/// Merges the symbol table \p new_symbol_table into \p dest_symbol_table,
19+
/// renaming symbols from \p new_symbol_table when necessary.
20+
/// \return True, iff linking failed with unresolvable conflicts.
2221
bool linking(
2322
symbol_tablet &dest_symbol_table,
24-
symbol_tablet &new_symbol_table,
23+
const symbol_tablet &new_symbol_table,
2524
message_handlert &message_handler);
2625

2726
#endif // CPROVER_LINKING_LINKING_H

src/linking/linking_class.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class linkingt:public typecheckt
3030
public:
3131
linkingt(
3232
symbol_table_baset &_main_symbol_table,
33-
symbol_table_baset &_src_symbol_table,
33+
const symbol_table_baset &_src_symbol_table,
3434
message_handlert &_message_handler)
3535
: typecheckt(_message_handler),
3636
main_symbol_table(_main_symbol_table),
@@ -65,8 +65,9 @@ class linkingt:public typecheckt
6565

6666
void do_type_dependencies(std::unordered_set<irep_idt> &);
6767

68-
void rename_symbols(const std::unordered_set<irep_idt> &needs_to_be_renamed);
69-
void copy_symbols();
68+
std::unordered_map<irep_idt, irep_idt>
69+
rename_symbols(const std::unordered_set<irep_idt> &needs_to_be_renamed);
70+
void copy_symbols(const std::unordered_map<irep_idt, irep_idt> &);
7071

7172
void duplicate_non_type_symbol(
7273
symbolt &old_symbol,
@@ -169,14 +170,14 @@ class linkingt:public typecheckt
169170
const struct_typet &new_type);
170171

171172
symbol_table_baset &main_symbol_table;
172-
symbol_table_baset &src_symbol_table;
173+
const symbol_table_baset &src_symbol_table;
173174

174175
namespacet ns;
175176

176177
// X -> Y iff Y uses X for new symbol type IDs X and Y
177178
typedef std::unordered_map<irep_idt, std::unordered_set<irep_idt>> used_byt;
178179

179-
irep_idt rename(irep_idt);
180+
irep_idt rename(const irep_idt &);
180181

181182
// the new IDs created by renaming
182183
std::unordered_set<irep_idt> renamed_ids;

0 commit comments

Comments
 (0)