-
Notifications
You must be signed in to change notification settings - Fork 300
Expand file tree
/
Copy pathdump_c.cpp
More file actions
1738 lines (1515 loc) · 49.4 KB
/
Copy pathdump_c.cpp
File metadata and controls
1738 lines (1515 loc) · 49.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************\
Module: Dump Goto-Program as C/C++ Source
Author: Daniel Kroening, kroening@kroening.com
\*******************************************************************/
/// \file
/// Dump Goto-Program as C/C++ Source
#include "dump_c.h"
#include "dump_c_class.h"
#include <util/byte_operators.h>
#include <util/c_types.h>
#include <util/config.h>
#include <util/expr_initializer.h>
#include <util/expr_util.h>
#include <util/find_symbols.h>
#include <util/get_base_name.h>
#include <util/invariant.h>
#include <util/prefix.h>
#include <util/replace_symbol.h>
#include <util/string_utils.h>
#include <ansi-c/expr2c.h>
#include <ansi-c/type2name.h>
#include <cpp/cpp_type2name.h>
#include <cpp/expr2cpp.h>
#include <linking/static_lifetime_init.h>
#include "goto_program2code.h"
static std::string clean_identifier(const irep_idt &id)
{
std::string result;
result.reserve(id2string(id).size());
for(auto c : id2string(id))
{
if(c >= '0' && c <= '9')
result += c;
else if(c >= 'A' && c <= 'Z')
result += c;
else if(c >= 'a' && c <= 'z')
result += c;
else if(c == '_' || c == '$')
result += c;
else
result += "_" + std::to_string(c);
}
return result;
}
dump_c_configurationt dump_c_configurationt::default_configuration =
dump_c_configurationt();
dump_c_configurationt dump_c_configurationt::type_header_configuration =
dump_c_configurationt()
.disable_include_function_decls()
.disable_include_function_bodies()
.disable_include_global_vars()
.enable_include_headers();
inline std::ostream &operator << (std::ostream &out, dump_ct &src)
{
src(out);
return out;
}
void dump_ct::operator()(std::ostream &os)
{
std::stringstream func_decl_stream;
std::stringstream compound_body_stream;
std::stringstream global_var_stream;
std::stringstream global_decl_stream;
std::stringstream global_decl_header_stream;
std::stringstream func_body_stream;
local_static_declst local_static_decls;
// add copies of struct types when ID_C_transparent_union is only
// annotated to parameter
symbol_tablet additional_symbols;
for(auto it = copied_symbol_table.begin(); it != copied_symbol_table.end();
++it)
{
const symbolt &symbol = it->second;
if(
(symbol.type.id() == ID_union || symbol.type.id() == ID_struct) &&
!symbol.is_type)
{
std::string tag_name;
if(mode == ID_C)
tag_name = "tag-" + type2name(symbol.type, ns);
else if(mode == ID_cpp)
tag_name = "tag-" + cpp_type2name(symbol.type);
else
UNREACHABLE;
type_symbolt ts{tag_name, symbol.type, symbol.mode};
typet &type = it.get_writeable_symbol().type;
if(ts.type.id() == ID_union)
type = union_tag_typet{ts.name};
else
type = struct_tag_typet{ts.name};
additional_symbols.add(ts);
}
if(symbol.type.id()!=ID_code)
continue;
code_typet &code_type = to_code_type(it.get_writeable_symbol().type);
code_typet::parameterst ¶meters=code_type.parameters();
for(code_typet::parameterst::iterator
it2=parameters.begin();
it2!=parameters.end();
++it2)
{
typet &type=it2->type();
if(type.id() == ID_union_tag && type.get_bool(ID_C_transparent_union))
{
symbolt new_type_sym =
ns.lookup(to_union_tag_type(type).get_identifier());
new_type_sym.name=id2string(new_type_sym.name)+"$transparent";
new_type_sym.type.set(ID_C_transparent_union, true);
// we might have it already, in which case this has no effect
additional_symbols.add(new_type_sym);
to_union_tag_type(type).set_identifier(new_type_sym.name);
type.remove(ID_C_transparent_union);
}
}
}
for(const auto &symbol_pair : additional_symbols.symbols)
{
copied_symbol_table.add(symbol_pair.second);
}
typedef std::unordered_map<irep_idt, unsigned> unique_tagst;
unique_tagst unique_tags;
// add tags to anonymous union/struct/enum,
// and prepare lexicographic order
std::set<std::string> symbols_sorted;
for(auto it = copied_symbol_table.begin(); it != copied_symbol_table.end();
++it)
{
symbolt &symbol = it.get_writeable_symbol();
bool tag_added=false;
// TODO we could get rid of some of the ID_anonymous by looking up
// the origin symbol types in typedef_types and adjusting any other
// uses of ID_tag
if((symbol.type.id()==ID_union || symbol.type.id()==ID_struct) &&
symbol.type.get(ID_tag).empty())
{
PRECONDITION(symbol.is_type);
symbol.type.set(ID_tag, ID_anonymous);
tag_added=true;
}
else if(symbol.type.id()==ID_c_enum &&
symbol.type.find(ID_tag).get(ID_C_base_name).empty())
{
PRECONDITION(symbol.is_type);
symbol.type.add(ID_tag).set(ID_C_base_name, ID_anonymous);
tag_added=true;
}
const std::string name_str = id2string(it->first);
if(symbol.is_type &&
(symbol.type.id()==ID_union ||
symbol.type.id()==ID_struct ||
symbol.type.id()==ID_c_enum))
{
std::string new_tag=symbol.type.id()==ID_c_enum?
symbol.type.find(ID_tag).get_string(ID_C_base_name):
symbol.type.get_string(ID_tag);
std::string::size_type tag_pos=new_tag.rfind("tag-");
if(tag_pos!=std::string::npos)
new_tag.erase(0, tag_pos+4);
const std::string new_tag_base=new_tag;
for(std::pair<unique_tagst::iterator, bool>
unique_entry=unique_tags.insert(std::make_pair(new_tag, 0));
!unique_entry.second;
unique_entry=unique_tags.insert(std::make_pair(new_tag, 0)))
{
new_tag=new_tag_base+"$"+
std::to_string(unique_entry.first->second);
++(unique_entry.first->second);
}
if(symbol.type.id()==ID_c_enum)
{
symbol.type.add(ID_tag).set(ID_C_base_name, new_tag);
symbol.base_name=new_tag;
}
else
symbol.type.set(ID_tag, new_tag);
}
// we don't want to dump in full all definitions; in particular
// do not dump anonymous types that are defined in system headers
if(
(!tag_added || symbol.is_type) &&
system_symbols.is_symbol_internal_symbol(symbol, system_headers) &&
symbol.name != goto_functions.entry_point() &&
symbol.name != CPROVER_PREFIX "arg_string") // model for argc/argv
{
continue;
}
bool inserted=symbols_sorted.insert(name_str).second;
CHECK_RETURN(inserted);
}
gather_global_typedefs();
// collect all declarations we might need, include local static variables
bool skip_function_main=false;
std::vector<std::string> header_files;
for(std::set<std::string>::const_iterator
it=symbols_sorted.begin();
it!=symbols_sorted.end();
++it)
{
const symbolt &symbol=ns.lookup(*it);
const irep_idt &type_id=symbol.type.id();
if(symbol.is_type &&
symbol.location.get_function().empty() &&
(type_id==ID_struct ||
type_id==ID_union ||
type_id==ID_c_enum))
{
if(!system_symbols.is_symbol_internal_symbol(symbol, system_headers))
{
global_decl_stream << "// " << symbol.name << '\n';
global_decl_stream << "// " << symbol.location << '\n';
std::string location_file =
get_base_name(id2string(symbol.location.get_file()), false);
// collect header the types are borrowed from
// expect header files to end in .h
if(
location_file.length() > 1 &&
location_file[location_file.length() - 1] == 'h')
{
std::vector<std::string>::iterator it =
find(header_files.begin(), header_files.end(), location_file);
if(it == header_files.end())
{
header_files.push_back(location_file);
global_decl_header_stream << "#include \"" << location_file
<< "\"\n";
}
}
if(type_id==ID_c_enum)
convert_compound_enum(symbol.type, global_decl_stream);
else if(type_id == ID_struct)
{
global_decl_stream << type_to_string(struct_tag_typet{symbol.name})
<< ";\n\n";
}
else
{
global_decl_stream << type_to_string(union_tag_typet{symbol.name})
<< ";\n\n";
}
}
}
else if(
symbol.is_static_lifetime && symbol.type.id() != ID_code &&
!symbol.type.get_bool(ID_C_do_not_dump))
convert_global_variable(
symbol,
global_var_stream,
local_static_decls);
else if(symbol.type.id()==ID_code)
{
goto_functionst::function_mapt::const_iterator func_entry=
goto_functions.function_map.find(symbol.name);
if(
!harness && func_entry != goto_functions.function_map.end() &&
func_entry->second.body_available() &&
(symbol.name == ID_main ||
(config.main.has_value() && symbol.name == config.main.value())))
{
skip_function_main=true;
}
}
}
// function declarations and definitions
for(std::set<std::string>::const_iterator
it=symbols_sorted.begin();
it!=symbols_sorted.end();
++it)
{
const symbolt &symbol=ns.lookup(*it);
if(symbol.type.id()!=ID_code ||
symbol.is_type)
continue;
convert_function_declaration(
symbol,
skip_function_main,
func_decl_stream,
func_body_stream,
local_static_decls);
}
// (possibly modified) compound types
for(std::set<std::string>::const_iterator
it=symbols_sorted.begin();
it!=symbols_sorted.end();
++it)
{
const symbolt &symbol=ns.lookup(*it);
if(
symbol.is_type &&
(symbol.type.id() == ID_struct || symbol.type.id() == ID_union) &&
!to_struct_union_type(symbol.type).is_incomplete())
{
convert_compound_declaration(
symbol,
compound_body_stream);
}
}
// Dump the code to the target stream;
// the statements before to this point collect the code to dump!
for(std::set<std::string>::const_iterator
it=system_headers.begin();
it!=system_headers.end();
++it)
os << "#include <" << *it << ">\n";
if(!system_headers.empty())
os << '\n';
if(!global_decl_header_stream.str().empty() && dump_c_config.include_headers)
os << global_decl_header_stream.str() << '\n';
if(global_var_stream.str().find("NULL")!=std::string::npos ||
func_body_stream.str().find("NULL")!=std::string::npos)
{
os << "#ifndef NULL\n"
<< "#define NULL ((void*)0)\n"
<< "#endif\n\n";
}
if(func_body_stream.str().find("FENCE")!=std::string::npos)
{
os << "#ifndef FENCE\n"
<< "#define FENCE(x) ((void)0)\n"
<< "#endif\n\n";
}
if(func_body_stream.str().find("IEEE_FLOAT_")!=std::string::npos)
{
os << "#ifndef IEEE_FLOAT_EQUAL\n"
<< "#define IEEE_FLOAT_EQUAL(x,y) ((x)==(y))\n"
<< "#endif\n"
<< "#ifndef IEEE_FLOAT_NOTEQUAL\n"
<< "#define IEEE_FLOAT_NOTEQUAL(x,y) ((x)!=(y))\n"
<< "#endif\n\n";
}
if(!global_decl_stream.str().empty() && dump_c_config.include_global_decls)
os << global_decl_stream.str() << '\n';
if(dump_c_config.include_typedefs)
dump_typedefs(os);
if(!func_decl_stream.str().empty() && dump_c_config.include_function_decls)
os << func_decl_stream.str() << '\n';
if(!compound_body_stream.str().empty() && dump_c_config.include_compounds)
os << compound_body_stream.str() << '\n';
if(!global_var_stream.str().empty() && dump_c_config.include_global_vars)
os << global_var_stream.str() << '\n';
if(dump_c_config.include_function_bodies)
os << func_body_stream.str();
}
/// declare compound types
void dump_ct::convert_compound_declaration(
const symbolt &symbol,
std::ostream &os_body)
{
if(
!symbol.location.get_function().empty() ||
symbol.type.get_bool(ID_C_do_not_dump))
{
return;
}
// do compound type body
if(symbol.type.id() == ID_struct)
convert_compound(
symbol.type,
struct_tag_typet(symbol.name),
dump_c_config.follow_compounds,
os_body);
else if(symbol.type.id() == ID_union)
convert_compound(
symbol.type,
union_tag_typet(symbol.name),
dump_c_config.follow_compounds,
os_body);
else if(symbol.type.id() == ID_c_enum)
convert_compound(
symbol.type,
c_enum_tag_typet(symbol.name),
dump_c_config.follow_compounds,
os_body);
}
void dump_ct::convert_compound(
const typet &type,
const typet &unresolved,
bool recursive,
std::ostream &os)
{
if(
type.id() == ID_c_enum_tag || type.id() == ID_struct_tag ||
type.id() == ID_union_tag)
{
const symbolt &symbol = ns.lookup(to_tag_type(type));
DATA_INVARIANT(symbol.is_type, "tag expected to be type symbol");
if(!system_symbols.is_symbol_internal_symbol(symbol, system_headers))
convert_compound(symbol.type, unresolved, recursive, os);
}
else if(type.id()==ID_array || type.id()==ID_pointer)
{
if(!recursive)
return;
convert_compound(
to_type_with_subtype(type).subtype(),
to_type_with_subtype(type).subtype(),
recursive,
os);
// sizeof may contain a type symbol that has to be declared first
if(type.id()==ID_array)
{
find_symbols_sett syms;
find_non_pointer_type_symbols(to_array_type(type).size(), syms);
for(find_symbols_sett::const_iterator
it=syms.begin();
it!=syms.end();
++it)
{
const symbolt &type_symbol = ns.lookup(*it);
irep_idt tag_kind =
type_symbol.type.id() == ID_c_enum
? ID_c_enum_tag
: (type_symbol.type.id() == ID_union ? ID_union_tag
: ID_struct_tag);
tag_typet s_type(tag_kind, *it);
convert_compound(s_type, s_type, recursive, os);
}
}
}
else if(type.id()==ID_struct || type.id()==ID_union)
convert_compound(to_struct_union_type(type), unresolved, recursive, os);
else if(type.id()==ID_c_enum)
convert_compound_enum(type, os);
}
void dump_ct::convert_compound(
const struct_union_typet &type,
const typet &unresolved,
bool recursive,
std::ostream &os)
{
const irep_idt &name=type.get(ID_tag);
if(!converted_compound.insert(name).second || type.get_bool(ID_C_do_not_dump))
return;
// make sure typedef names used in the declaration are available
collect_typedefs(type, true);
const irept &bases = type.find(ID_bases);
std::stringstream base_decls;
for(const auto &parent : bases.get_sub())
{
UNREACHABLE;
(void)parent;
#if 0
assert(parent.id() == ID_base);
assert(parent.get(ID_type) == ID_struct_tag);
const irep_idt &base_id=
parent.find(ID_type).get(ID_identifier);
const irep_idt &renamed_base_id=global_renaming[base_id];
const symbolt &parsymb=ns.lookup(renamed_base_id);
convert_compound_rec(parsymb.type, os);
base_decls << id2string(renamed_base_id) +
(parent_it+1==bases.get_sub().end()?"":", ");
#endif
}
#if 0
// for the constructor
string constructor_args;
string constructor_body;
std::string component_name = id2string(renaming[compo.get(ID_name)]);
assert(component_name != "");
if(it != struct_type.components().begin()) constructor_args += ", ";
if(compo.type().id() == ID_pointer)
constructor_args += type_to_string(compo.type()) + component_name;
else
constructor_args += "const " + type_to_string(compo.type()) + "& " + component_name;
constructor_body += indent + indent + "this->"+component_name + " = " + component_name + ";\n";
#endif
std::stringstream struct_body;
for(const auto &comp : type.components())
{
const typet &comp_type = comp.type();
DATA_INVARIANT(
comp_type.id() != ID_code, "struct member must not be of code type");
if(comp.get_bool(ID_from_base) || comp.get_is_padding())
continue;
const typet *non_array_type = &comp_type;
while(non_array_type->id()==ID_array)
non_array_type = &(to_array_type(*non_array_type).element_type());
bool is_anon =
can_cast_type<tag_typet>(comp.type()) &&
has_prefix(
id2string(to_tag_type(comp.type()).get_identifier()), "tag-#anon");
if(recursive)
{
if(non_array_type->id() != ID_pointer && !is_anon)
convert_compound(comp.type(), comp.type(), recursive, os);
else
collect_typedefs(comp.type(), true);
}
struct_body << indent(1) << "// " << comp.get_name() << '\n';
struct_body << indent(1);
irep_idt comp_name = clean_identifier(comp.get_name());
// component names such as "main" would collide with other objects in the
// namespace
std::string fake_unique_name="NO/SUCH/NS::"+id2string(comp_name);
typet comp_type_to_use = comp.type();
if(is_anon)
{
comp_type_to_use =
(comp.type().id() == ID_struct_tag || comp.type().id() == ID_union_tag)
? ns.follow_tag(to_struct_or_union_tag_type(comp.type()))
: comp.type();
comp_type_to_use.remove(ID_tag);
if(
recursive && (comp_type_to_use.id() == ID_struct ||
comp_type_to_use.id() == ID_union))
{
const auto &sub_comps =
to_struct_union_type(comp_type_to_use).components();
for(const auto &sc : sub_comps)
convert_compound(sc.type(), sc.type(), recursive, os);
}
}
std::string s = make_decl(fake_unique_name, comp_type_to_use);
POSTCONDITION(s.find("NO/SUCH/NS")==std::string::npos);
if(comp_type.id()==ID_c_bit_field &&
to_c_bit_field_type(comp_type).get_width()==0)
{
comp_name.clear();
s=type_to_string(comp_type);
}
if(s.find(CPROVER_PREFIX "bitvector") == std::string::npos)
{
struct_body << s;
}
else if(comp_type.id()==ID_signedbv)
{
const signedbv_typet &t=to_signedbv_type(comp_type);
if(t.get_width()<=config.ansi_c.long_long_int_width)
struct_body << "long long int " << comp_name
<< " : " << t.get_width();
else if(mode == ID_cpp)
struct_body << "__signedbv<" << t.get_width() << "> "
<< comp_name;
else
struct_body << s;
}
else if(comp_type.id()==ID_unsignedbv)
{
const unsignedbv_typet &t=to_unsignedbv_type(comp_type);
if(t.get_width()<=config.ansi_c.long_long_int_width)
struct_body << "unsigned long long " << comp_name
<< " : " << t.get_width();
else if(mode == ID_cpp)
struct_body << "__unsignedbv<" << t.get_width() << "> "
<< comp_name;
else
struct_body << s;
}
else
UNREACHABLE;
struct_body << ";\n";
}
typet unresolved_clean=unresolved;
irep_idt typedef_str;
for(auto td_entry : typedef_types)
{
if(
td_entry.first.get(ID_identifier) == unresolved.get(ID_identifier) &&
(td_entry.first.source_location() == unresolved.source_location()))
{
unresolved_clean.remove(ID_C_typedef);
typedef_str = td_entry.second;
std::pair<typedef_mapt::iterator, bool> td_map_entry =
typedef_map.insert({typedef_str, typedef_infot(typedef_str)});
PRECONDITION(!td_map_entry.second);
if(!td_map_entry.first->second.early)
td_map_entry.first->second.type_decl_str.clear();
os << "typedef ";
break;
}
}
os << type_to_string(unresolved_clean);
if(!base_decls.str().empty())
{
PRECONDITION(mode == ID_cpp);
os << ": " << base_decls.str();
}
os << '\n';
os << "{\n";
os << struct_body.str();
/*
if(!struct_type.components().empty())
{
os << indent << name << "(){}\n";
os << indent << "explicit " << name
<< "(" + constructor_args + ")\n";
os << indent << "{\n";
os << constructor_body;
os << indent << "}\n";
}
*/
os << "}";
if(type.get_bool(ID_C_transparent_union))
os << " __attribute__ ((__transparent_union__))";
if(type.get_bool(ID_C_packed))
os << " __attribute__ ((__packed__))";
if(!typedef_str.empty())
os << " " << typedef_str;
os << ";\n\n";
}
void dump_ct::convert_compound_enum(
const typet &type,
std::ostream &os)
{
PRECONDITION(type.id()==ID_c_enum);
const irept &tag=type.find(ID_tag);
const irep_idt &name=tag.get(ID_C_base_name);
if(tag.is_nil() ||
!converted_enum.insert(name).second)
return;
c_enum_typet enum_type=to_c_enum_type(type);
c_enum_typet::memberst &members=
(c_enum_typet::memberst &)(enum_type.add(ID_body).get_sub());
for(c_enum_typet::memberst::iterator
it=members.begin();
it!=members.end();
++it)
{
const irep_idt bn=it->get_base_name();
if(declared_enum_constants.find(bn)!=
declared_enum_constants.end() ||
copied_symbol_table.has_symbol(bn))
{
std::string new_bn=id2string(name)+"$$"+id2string(bn);
it->set_base_name(new_bn);
}
declared_enum_constants.insert(
std::make_pair(bn, it->get_base_name()));
}
os << type_to_string(enum_type);
if(enum_type.get_bool(ID_C_packed))
os << " __attribute__ ((__packed__))";
os << ";\n\n";
}
void dump_ct::cleanup_decl(
code_frontend_declt &decl,
std::list<irep_idt> &local_static,
std::list<irep_idt> &local_type_decls)
{
goto_programt tmp;
tmp.add(goto_programt::make_decl(decl.symbol()));
if(std::optional<exprt> value = decl.initial_value())
{
decl.set_initial_value({});
tmp.add(goto_programt::make_assignment(decl.symbol(), std::move(*value)));
}
tmp.add(goto_programt::make_end_function());
// goto_program2codet requires valid location numbers:
tmp.update();
std::unordered_set<irep_idt> typedef_names;
for(const auto &td : typedef_map)
typedef_names.insert(td.first);
code_blockt b;
goto_program2codet p2s(
irep_idt(),
tmp,
copied_symbol_table,
b,
local_static,
local_type_decls,
typedef_names,
system_headers);
p2s();
POSTCONDITION(b.statements().size() == 1);
decl.swap(b.op0());
}
/// Find any typedef names contained in the input type and store their
/// declaration strings in typedef_map for eventual output.
/// \param type: type to inspect for ID_C_typedef entry
/// \param early: set to true to enforce that typedef is dumped before any
/// function declarations or struct definitions
void dump_ct::collect_typedefs(const typet &type, bool early)
{
std::unordered_set<irep_idt> deps;
collect_typedefs_rec(type, early, deps);
}
/// Find any typedef names contained in the input type and store their
/// declaration strings in typedef_map for eventual output.
/// \param type: type to inspect for ID_C_typedef entry
/// \param early: set to true to enforce that typedef is dumped before any
/// function declarations or struct definitions
/// \param [out] dependencies: typedefs used in the declaration of a given
/// typedef
void dump_ct::collect_typedefs_rec(
const typet &type,
bool early,
std::unordered_set<irep_idt> &dependencies)
{
std::unordered_set<irep_idt> local_deps;
if(type.id()==ID_code)
{
const code_typet &code_type=to_code_type(type);
collect_typedefs_rec(code_type.return_type(), early, local_deps);
for(const auto ¶m : code_type.parameters())
collect_typedefs_rec(param.type(), early, local_deps);
}
else if(type.id()==ID_pointer || type.id()==ID_array)
{
collect_typedefs_rec(
to_type_with_subtype(type).subtype(), early, local_deps);
}
else if(
type.id() == ID_c_enum_tag || type.id() == ID_struct_tag ||
type.id() == ID_union_tag)
{
const symbolt &symbol = ns.lookup(to_tag_type(type));
collect_typedefs_rec(symbol.type, early, local_deps);
}
const irep_idt &typedef_str=type.get(ID_C_typedef);
if(!typedef_str.empty())
{
std::pair<typedef_mapt::iterator, bool> entry=
typedef_map.insert({typedef_str, typedef_infot(typedef_str)});
if(entry.second ||
(early && entry.first->second.type_decl_str.empty()))
{
if(typedef_str=="__gnuc_va_list" || typedef_str == "va_list")
{
system_headers.insert("stdarg.h");
early=false;
}
else
{
typet t=type;
t.remove(ID_C_typedef);
std::ostringstream oss;
oss << "typedef " << make_decl(typedef_str, t) << ';';
entry.first->second.type_decl_str=oss.str();
entry.first->second.dependencies=local_deps;
}
}
if(early)
{
entry.first->second.early=true;
for(const auto &d : local_deps)
{
auto td_entry=typedef_map.find(d);
PRECONDITION(td_entry!=typedef_map.end());
td_entry->second.early=true;
}
}
dependencies.insert(typedef_str);
}
dependencies.insert(local_deps.begin(), local_deps.end());
}
/// Find all global typdefs in the symbol table and store them in typedef_types
void dump_ct::gather_global_typedefs()
{
// sort the symbols first to ensure deterministic replacement in
// typedef_types below as there could be redundant declarations
// typedef int x;
// typedef int y;
std::map<std::string, symbolt> symbols_sorted;
for(const auto &symbol_entry : copied_symbol_table.symbols)
symbols_sorted.insert(
{id2string(symbol_entry.first), symbol_entry.second});
for(const auto &symbol_entry : symbols_sorted)
{
const symbolt &symbol=symbol_entry.second;
if(symbol.is_macro && symbol.is_type &&
symbol.location.get_function().empty())
{
const irep_idt &typedef_str=symbol.type.get(ID_C_typedef);
PRECONDITION(!typedef_str.empty());
typedef_types[symbol.type]=typedef_str;
if(system_symbols.is_symbol_internal_symbol(symbol, system_headers))
typedef_map.insert({typedef_str, typedef_infot(typedef_str)});
else
collect_typedefs(symbol.type, false);
}
}
}
/// Print all typedefs that are not covered via typedef struct xyz { ... } name;
/// \param [out] os: output stream
void dump_ct::dump_typedefs(std::ostream &os) const
{
// we need to compute a topological sort; we do so by picking all
// typedefs the dependencies of which have been emitted into to_insert
std::vector<typedef_infot> typedefs_sorted;
typedefs_sorted.reserve(typedef_map.size());
// elements in to_insert are lexicographically sorted and ready for
// output
std::map<std::string, typedef_infot> to_insert;
std::unordered_set<irep_idt> typedefs_done;
std::unordered_map<irep_idt, std::unordered_set<irep_idt>> forward_deps,
reverse_deps;
for(const auto &td : typedef_map)
if(!td.second.type_decl_str.empty())
{
if(td.second.dependencies.empty())
// those can be dumped immediately
to_insert.insert({id2string(td.first), td.second});
else
{
// delay them until dependencies are dumped
forward_deps.insert({td.first, td.second.dependencies});
for(const auto &d : td.second.dependencies)
reverse_deps[d].insert(td.first);
}
}
while(!to_insert.empty())
{
// the topologically next element (lexicographically ranked first
// among all the dependencies of which have been dumped)
typedef_infot t=to_insert.begin()->second;
to_insert.erase(to_insert.begin());
// move to the output queue
typedefs_sorted.push_back(t);
// find any depending typedefs that are now valid, or at least
// reduce the remaining dependencies
auto r_it=reverse_deps.find(t.typedef_name);
if(r_it==reverse_deps.end())
continue;
// reduce remaining dependencies
std::unordered_set<irep_idt> &r_deps = r_it->second;
for(std::unordered_set<irep_idt>::iterator it = r_deps.begin();
it != r_deps.end();) // no ++it
{
auto f_it=forward_deps.find(*it);
if(f_it==forward_deps.end()) // might be done already
{
it=r_deps.erase(it);
continue;
}
// update dependencies
std::unordered_set<irep_idt> &f_deps = f_it->second;
PRECONDITION(!f_deps.empty());
PRECONDITION(f_deps.find(t.typedef_name)!=f_deps.end());
f_deps.erase(t.typedef_name);
if(f_deps.empty()) // all depenencies done now!
{
const auto td_entry=typedef_map.find(*it);
PRECONDITION(td_entry!=typedef_map.end());
to_insert.insert({id2string(*it), td_entry->second});
forward_deps.erase(*it);
it=r_deps.erase(it);
}
else
++it;
}
}
POSTCONDITION(forward_deps.empty());
for(const auto &td : typedefs_sorted)
os << td.type_decl_str << '\n';
if(!typedefs_sorted.empty())
os << '\n';
}
void dump_ct::convert_global_variable(
const symbolt &symbol,
std::ostream &os,
local_static_declst &local_static_decls)
{
const irep_idt &func=symbol.location.get_function();
if((func.empty() || symbol.is_extern || symbol.value.is_not_nil()) &&
!converted_global.insert(symbol.name).second)
return;
code_frontend_declt d(symbol.symbol_expr());
find_symbols_sett syms = find_symbol_identifiers(symbol.value);
// add a tentative declaration to cater for symbols in the initializer
// relying on it this symbol
if((func.empty() || symbol.is_extern) &&
(symbol.value.is_nil() || !syms.empty()))
{
os << "// " << symbol.name << '\n';
os << "// " << symbol.location << '\n';
os << expr_to_string(d) << '\n';
}