15
15
#include < util/c_types.h>
16
16
#include < util/find_symbols.h>
17
17
#include < util/mathematical_types.h>
18
+ #include < util/message.h>
18
19
#include < util/pointer_expr.h>
19
20
#include < util/pointer_offset_size.h>
20
21
#include < util/simplify_expr.h>
@@ -201,7 +202,8 @@ bool linkingt::detailed_conflict_report_rec(
201
202
bool conclusive = false ;
202
203
203
204
#ifdef DEBUG
204
- debug () << " <BEGIN DEPTH " << depth << " >" << eom;
205
+ messaget log {message_handler};
206
+ log .debug () << " <BEGIN DEPTH " << depth << " >" << messaget::eom;
205
207
#endif
206
208
207
209
std::string msg;
@@ -455,18 +457,23 @@ bool linkingt::detailed_conflict_report_rec(
455
457
456
458
if (conclusive && !msg.empty ())
457
459
{
458
- error () << ' \n ' ;
459
- error () << " reason for conflict at "
460
- << expr_to_string (irep_idt (), conflict_path) << " : " << msg << ' \n ' ;
461
-
462
- error () << ' \n ' ;
463
- error () << type_to_string_verbose (old_symbol, t1) << ' \n ' ;
464
- error () << type_to_string_verbose (new_symbol, t2) << ' \n ' ;
460
+ #ifndef DEBUG
461
+ messaget log {message_handler};
462
+ #endif
463
+ log .error () << ' \n ' ;
464
+ log .error () << " reason for conflict at "
465
+ << expr_to_string (irep_idt (), conflict_path) << " : " << msg
466
+ << ' \n ' ;
467
+
468
+ log .error () << ' \n ' ;
469
+ log .error () << type_to_string_verbose (old_symbol, t1) << ' \n ' ;
470
+ log .error () << type_to_string_verbose (new_symbol, t2) << ' \n '
471
+ << messaget::eom;
465
472
}
466
473
467
474
#ifdef DEBUG
468
- debug () << " <END DEPTH " << depth << " >" << eom;
469
- #endif
475
+ log . debug () << " <END DEPTH " << depth << " >" << messaget:: eom;
476
+ #endif
470
477
471
478
return conclusive;
472
479
}
@@ -476,37 +483,39 @@ void linkingt::link_error(
476
483
const symbolt &new_symbol,
477
484
const std::string &msg)
478
485
{
479
- error ().source_location =new_symbol.location ;
480
-
481
- error () << " error: " << msg << " '" << old_symbol.display_name () << " '"
482
- << ' \n ' ;
483
- error () << " old definition in module '" << old_symbol.module << " ' "
484
- << old_symbol.location << ' \n '
485
- << type_to_string_verbose (old_symbol) << ' \n ' ;
486
- error () << " new definition in module '" << new_symbol.module << " ' "
487
- << new_symbol.location << ' \n '
488
- << type_to_string_verbose (new_symbol) << eom;
486
+ messaget log {message_handler};
487
+ log .error ().source_location = new_symbol.location ;
488
+
489
+ log .error () << " error: " << msg << " '" << old_symbol.display_name () << " '"
490
+ << ' \n ' ;
491
+ log .error () << " old definition in module '" << old_symbol.module << " ' "
492
+ << old_symbol.location << ' \n '
493
+ << type_to_string_verbose (old_symbol) << ' \n ' ;
494
+ log .error () << " new definition in module '" << new_symbol.module << " ' "
495
+ << new_symbol.location << ' \n '
496
+ << type_to_string_verbose (new_symbol) << messaget::eom;
489
497
}
490
498
491
499
void linkingt::link_warning (
492
500
const symbolt &old_symbol,
493
501
const symbolt &new_symbol,
494
502
const std::string &msg)
495
503
{
496
- warning (). source_location =new_symbol. location ;
497
-
498
- warning () << " warning: " << msg << " \" "
499
- << old_symbol.display_name ()
500
- << " \" " << ' \n ' ;
501
- warning () << " old definition in module " << old_symbol.module << " "
502
- << old_symbol.location << ' \n '
503
- << type_to_string_verbose (old_symbol) << ' \n ' ;
504
- warning () << " new definition in module " << new_symbol.module << " "
505
- << new_symbol.location << ' \n '
506
- << type_to_string_verbose (new_symbol) << eom;
504
+ messaget log {message_handler} ;
505
+ log . warning (). source_location = new_symbol. location ;
506
+
507
+ log . warning () << " warning: " << msg << " \" " << old_symbol.display_name ()
508
+ << " \" " << ' \n ' ;
509
+ log . warning () << " old definition in module " << old_symbol.module << " "
510
+ << old_symbol.location << ' \n '
511
+ << type_to_string_verbose (old_symbol) << ' \n ' ;
512
+ log . warning () << " new definition in module " << new_symbol.module << " "
513
+ << new_symbol.location << ' \n '
514
+ << type_to_string_verbose (new_symbol) << messaget:: eom;
507
515
}
508
516
509
- irep_idt linkingt::rename (const irep_idt &id)
517
+ irep_idt
518
+ linkingt::rename (const symbol_table_baset &src_symbol_table, const irep_idt &id)
510
519
{
511
520
unsigned cnt=0 ;
512
521
@@ -738,11 +747,6 @@ void linkingt::duplicate_code_symbol(
738
747
{
739
748
warn_msg=" pointer parameter types differ between "
740
749
" declaration and definition" ;
741
- detailed_conflict_report (
742
- old_symbol,
743
- new_symbol,
744
- conflicts.front ().first ,
745
- conflicts.front ().second );
746
750
}
747
751
748
752
replace=new_symbol.value .is_not_nil ();
@@ -855,12 +859,12 @@ void linkingt::duplicate_code_symbol(
855
859
else if (old_symbol.type == new_symbol.type )
856
860
{
857
861
// keep the one in old_symbol -- libraries come last!
858
- debug (). source_location = new_symbol. location ;
859
-
860
- debug () << " function '" << old_symbol.name << " ' in module '"
861
- << new_symbol.module
862
- << " ' is shadowed by a definition in module '"
863
- << old_symbol.module << " '" << eom;
862
+ messaget log {message_handler} ;
863
+ log . debug (). source_location = new_symbol. location ;
864
+ log . debug () << " function '" << old_symbol.name << " ' in module '"
865
+ << new_symbol.module
866
+ << " ' is shadowed by a definition in module '"
867
+ << old_symbol.module << " '" << messaget:: eom;
864
868
}
865
869
else
866
870
link_error (
@@ -1146,16 +1150,18 @@ void linkingt::duplicate_object_symbol(
1146
1150
}
1147
1151
else
1148
1152
{
1149
- warning ().source_location =new_symbol.location ;
1150
-
1151
- warning () << " warning: conflicting initializers for"
1152
- << " variable \" " << old_symbol.name << " \"\n " ;
1153
- warning () << " using old value in module " << old_symbol.module << " "
1154
- << old_symbol.value .find_source_location () << ' \n '
1155
- << expr_to_string (old_symbol.name , tmp_old) << ' \n ' ;
1156
- warning () << " ignoring new value in module " << new_symbol.module << " "
1157
- << new_symbol.value .find_source_location () << ' \n '
1158
- << expr_to_string (new_symbol.name , tmp_new) << eom;
1153
+ messaget log {message_handler};
1154
+ log .warning ().source_location = new_symbol.location ;
1155
+
1156
+ log .warning () << " warning: conflicting initializers for"
1157
+ << " variable \" " << old_symbol.name << " \"\n " ;
1158
+ log .warning () << " using old value in module " << old_symbol.module
1159
+ << " " << old_symbol.value .find_source_location () << ' \n '
1160
+ << expr_to_string (old_symbol.name , tmp_old) << ' \n ' ;
1161
+ log .warning () << " ignoring new value in module " << new_symbol.module
1162
+ << " " << new_symbol.value .find_source_location () << ' \n '
1163
+ << expr_to_string (new_symbol.name , tmp_new)
1164
+ << messaget::eom;
1159
1165
}
1160
1166
}
1161
1167
}
@@ -1363,13 +1369,16 @@ bool linkingt::needs_renaming_type(
1363
1369
return true ; // different
1364
1370
}
1365
1371
1366
- void linkingt::do_type_dependencies (
1367
- std::unordered_set<irep_idt> &needs_to_be_renamed)
1372
+ static void do_type_dependencies (
1373
+ const symbol_table_baset &src_symbol_table,
1374
+ std::unordered_set<irep_idt> &needs_to_be_renamed,
1375
+ message_handlert &message_handler)
1368
1376
{
1369
1377
// Any type that uses a symbol that will be renamed also
1370
1378
// needs to be renamed, and so on, until saturation.
1371
1379
1372
- used_byt used_by;
1380
+ // X -> Y iff Y uses X for new symbol type IDs X and Y
1381
+ std::unordered_map<irep_idt, std::unordered_set<irep_idt>> used_by;
1373
1382
1374
1383
for (const auto &symbol_pair : src_symbol_table.symbols )
1375
1384
{
@@ -1401,14 +1410,16 @@ void linkingt::do_type_dependencies(
1401
1410
{
1402
1411
queue.push_back (dep);
1403
1412
#ifdef DEBUG
1404
- debug () << " LINKING: needs to be renamed (dependency): "
1405
- << dep << eom;
1406
- #endif
1413
+ messaget log {message_handler};
1414
+ log .debug () << " LINKING: needs to be renamed (dependency): " << dep
1415
+ << messaget::eom;
1416
+ #endif
1407
1417
}
1408
1418
}
1409
1419
}
1410
1420
1411
1421
std::unordered_map<irep_idt, irep_idt> linkingt::rename_symbols (
1422
+ const symbol_table_baset &src_symbol_table,
1412
1423
const std::unordered_set<irep_idt> &needs_to_be_renamed)
1413
1424
{
1414
1425
std::unordered_map<irep_idt, irep_idt> new_identifiers;
@@ -1423,13 +1434,14 @@ std::unordered_map<irep_idt, irep_idt> linkingt::rename_symbols(
1423
1434
if (new_symbol.is_type )
1424
1435
new_identifier=type_to_name (src_ns, id, new_symbol.type );
1425
1436
else
1426
- new_identifier= rename (id);
1437
+ new_identifier = rename (src_symbol_table, id);
1427
1438
1428
1439
new_identifiers.emplace (id, new_identifier);
1429
1440
1430
1441
#ifdef DEBUG
1431
- debug () << " LINKING: renaming " << id << " to "
1432
- << new_identifier << eom;
1442
+ messaget log {message_handler};
1443
+ log .debug () << " LINKING: renaming " << id << " to " << new_identifier
1444
+ << messaget::eom;
1433
1445
#endif
1434
1446
1435
1447
if (new_symbol.is_type )
@@ -1442,6 +1454,7 @@ std::unordered_map<irep_idt, irep_idt> linkingt::rename_symbols(
1442
1454
}
1443
1455
1444
1456
void linkingt::copy_symbols (
1457
+ const symbol_table_baset &src_symbol_table,
1445
1458
const std::unordered_map<irep_idt, irep_idt> &new_identifiers)
1446
1459
{
1447
1460
std::map<irep_idt, symbolt> src_symbols;
@@ -1506,8 +1519,11 @@ void linkingt::copy_symbols(
1506
1519
}
1507
1520
}
1508
1521
1509
- void linkingt::typecheck ( )
1522
+ bool linkingt::link ( const symbol_table_baset &src_symbol_table )
1510
1523
{
1524
+ const unsigned errors_before =
1525
+ message_handler.get_message_count (messaget::M_ERROR);
1526
+
1511
1527
// We do this in three phases. We first figure out which symbols need to
1512
1528
// be renamed, and then build the renaming, and finally apply this
1513
1529
// renaming in the second pass over the symbol table.
@@ -1527,28 +1543,31 @@ void linkingt::typecheck()
1527
1543
{
1528
1544
needs_to_be_renamed.insert (symbol_pair.first );
1529
1545
#ifdef DEBUG
1530
- debug () << " LINKING: needs to be renamed: " << symbol_pair.first << eom;
1531
- #endif
1546
+ messaget log {message_handler};
1547
+ log .debug () << " LINKING: needs to be renamed: " << symbol_pair.first
1548
+ << messaget::eom;
1549
+ #endif
1532
1550
}
1533
1551
}
1534
1552
1535
1553
// renaming types may trigger further renaming
1536
- do_type_dependencies (needs_to_be_renamed);
1554
+ do_type_dependencies (src_symbol_table, needs_to_be_renamed, message_handler );
1537
1555
1538
1556
// PHASE 2: actually rename them
1539
- auto new_identifiers = rename_symbols (needs_to_be_renamed);
1557
+ auto new_identifiers = rename_symbols (src_symbol_table, needs_to_be_renamed);
1540
1558
1541
1559
// PHASE 3: copy new symbols to main table
1542
- copy_symbols (new_identifiers);
1560
+ copy_symbols (src_symbol_table, new_identifiers);
1561
+
1562
+ return message_handler.get_message_count (messaget::M_ERROR) != errors_before;
1543
1563
}
1544
1564
1545
1565
bool linking (
1546
1566
symbol_table_baset &dest_symbol_table,
1547
1567
const symbol_table_baset &new_symbol_table,
1548
1568
message_handlert &message_handler)
1549
1569
{
1550
- linkingt linking (
1551
- dest_symbol_table, new_symbol_table, message_handler);
1570
+ linkingt linking (dest_symbol_table, message_handler);
1552
1571
1553
- return linking.typecheck_main ( );
1572
+ return linking.link (new_symbol_table );
1554
1573
}
0 commit comments