cprover
dump_c.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Dump Goto-Program as C/C++ Source
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #include "dump_c.h"
13 
14 #include <util/byte_operators.h>
15 #include <util/c_types.h>
16 #include <util/config.h>
17 #include <util/expr_initializer.h>
18 #include <util/find_symbols.h>
19 #include <util/get_base_name.h>
20 #include <util/invariant.h>
21 #include <util/replace_symbol.h>
22 #include <util/string_utils.h>
23 
24 #include <ansi-c/expr2c.h>
25 #include <cpp/expr2cpp.h>
26 
28 
29 #include "dump_c_class.h"
30 #include "goto_program2code.h"
31 
34 
41 
42 inline std::ostream &operator << (std::ostream &out, dump_ct &src)
43 {
44  src(out);
45  return out;
46 }
47 
48 void dump_ct::operator()(std::ostream &os)
49 {
50  std::stringstream func_decl_stream;
51  std::stringstream compound_body_stream;
52  std::stringstream global_var_stream;
53  std::stringstream global_decl_stream;
54  std::stringstream global_decl_header_stream;
55  std::stringstream func_body_stream;
56  local_static_declst local_static_decls;
57 
58  // add copies of struct types when ID_C_transparent_union is only
59  // annotated to parameter
60  symbol_tablet symbols_transparent;
61  for(const auto &named_symbol : copied_symbol_table.symbols)
62  {
63  const symbolt &symbol=named_symbol.second;
64 
65  if(symbol.type.id()!=ID_code)
66  continue;
67 
68  code_typet &code_type=to_code_type(
69  copied_symbol_table.get_writeable_ref(named_symbol.first).type);
70  code_typet::parameterst &parameters=code_type.parameters();
71 
72  for(code_typet::parameterst::iterator
73  it2=parameters.begin();
74  it2!=parameters.end();
75  ++it2)
76  {
77  typet &type=it2->type();
78 
79  if(type.id() == ID_union_tag && type.get_bool(ID_C_transparent_union))
80  {
81  symbolt new_type_sym =
82  ns.lookup(to_union_tag_type(type).get_identifier());
83 
84  new_type_sym.name=id2string(new_type_sym.name)+"$transparent";
85  new_type_sym.type.set(ID_C_transparent_union, true);
86 
87  // we might have it already, in which case this has no effect
88  symbols_transparent.add(new_type_sym);
89 
90  to_union_tag_type(type).set_identifier(new_type_sym.name);
91  type.remove(ID_C_transparent_union);
92  }
93  }
94  }
95  for(const auto &symbol_pair : symbols_transparent.symbols)
96  {
97  copied_symbol_table.add(symbol_pair.second);
98  }
99 
100  typedef std::unordered_map<irep_idt, unsigned> unique_tagst;
101  unique_tagst unique_tags;
102 
103  // add tags to anonymous union/struct/enum,
104  // and prepare lexicographic order
105  std::set<std::string> symbols_sorted;
106  for(const auto &named_symbol : copied_symbol_table.symbols)
107  {
108  symbolt &symbol = copied_symbol_table.get_writeable_ref(named_symbol.first);
109  bool tag_added=false;
110 
111  // TODO we could get rid of some of the ID_anonymous by looking up
112  // the origin symbol types in typedef_types and adjusting any other
113  // uses of ID_tag
114  if((symbol.type.id()==ID_union || symbol.type.id()==ID_struct) &&
115  symbol.type.get(ID_tag).empty())
116  {
117  PRECONDITION(symbol.is_type);
118  symbol.type.set(ID_tag, ID_anonymous);
119  tag_added=true;
120  }
121  else if(symbol.type.id()==ID_c_enum &&
122  symbol.type.find(ID_tag).get(ID_C_base_name).empty())
123  {
124  PRECONDITION(symbol.is_type);
125  symbol.type.add(ID_tag).set(ID_C_base_name, ID_anonymous);
126  tag_added=true;
127  }
128 
129  const std::string name_str=id2string(named_symbol.first);
130  if(symbol.is_type &&
131  (symbol.type.id()==ID_union ||
132  symbol.type.id()==ID_struct ||
133  symbol.type.id()==ID_c_enum))
134  {
135  std::string new_tag=symbol.type.id()==ID_c_enum?
136  symbol.type.find(ID_tag).get_string(ID_C_base_name):
137  symbol.type.get_string(ID_tag);
138 
139  std::string::size_type tag_pos=new_tag.rfind("tag-");
140  if(tag_pos!=std::string::npos)
141  new_tag.erase(0, tag_pos+4);
142  const std::string new_tag_base=new_tag;
143 
144  for(std::pair<unique_tagst::iterator, bool>
145  unique_entry=unique_tags.insert(std::make_pair(new_tag, 0));
146  !unique_entry.second;
147  unique_entry=unique_tags.insert(std::make_pair(new_tag, 0)))
148  {
149  new_tag=new_tag_base+"$"+
150  std::to_string(unique_entry.first->second);
151  ++(unique_entry.first->second);
152  }
153 
154  if(symbol.type.id()==ID_c_enum)
155  {
156  symbol.type.add(ID_tag).set(ID_C_base_name, new_tag);
157  symbol.base_name=new_tag;
158  }
159  else
160  symbol.type.set(ID_tag, new_tag);
161  }
162 
163  // we don't want to dump in full all definitions; in particular
164  // do not dump anonymous types that are defined in system headers
165  if((!tag_added || symbol.is_type) &&
167  symbol.name!=goto_functions.entry_point())
168  continue;
169 
170  bool inserted=symbols_sorted.insert(name_str).second;
171  CHECK_RETURN(inserted);
172  }
173 
175 
176  // collect all declarations we might need, include local static variables
177  bool skip_function_main=false;
178  std::vector<std::string> header_files;
179  for(std::set<std::string>::const_iterator
180  it=symbols_sorted.begin();
181  it!=symbols_sorted.end();
182  ++it)
183  {
184  const symbolt &symbol=ns.lookup(*it);
185  const irep_idt &type_id=symbol.type.id();
186 
187  if(symbol.is_type &&
188  symbol.location.get_function().empty() &&
189  (type_id==ID_struct ||
190  type_id==ID_union ||
191  type_id==ID_c_enum))
192  {
194  {
195  global_decl_stream << "// " << symbol.name << '\n';
196  global_decl_stream << "// " << symbol.location << '\n';
197 
198  std::string location_file =
199  get_base_name(id2string(symbol.location.get_file()), false);
200  // collect header the types are borrowed from
201  // expect header files to end in .h
202  if(
203  location_file.length() > 1 &&
204  location_file[location_file.length() - 1] == 'h')
205  {
206  std::vector<std::string>::iterator it =
207  find(header_files.begin(), header_files.end(), location_file);
208  if(it == header_files.end())
209  {
210  header_files.push_back(location_file);
211  global_decl_header_stream << "#include \"" << location_file
212  << "\"\n";
213  }
214  }
215 
216  if(type_id==ID_c_enum)
217  convert_compound_enum(symbol.type, global_decl_stream);
218  else if(type_id == ID_struct)
219  {
220  global_decl_stream << type_to_string(struct_tag_typet{symbol.name})
221  << ";\n\n";
222  }
223  else
224  {
225  global_decl_stream << type_to_string(union_tag_typet{symbol.name})
226  << ";\n\n";
227  }
228  }
229  }
230  else if(
231  symbol.is_static_lifetime && symbol.type.id() != ID_code &&
232  !symbol.type.get_bool(ID_C_do_not_dump))
234  symbol,
235  global_var_stream,
236  local_static_decls);
237  else if(symbol.type.id()==ID_code)
238  {
239  goto_functionst::function_mapt::const_iterator func_entry=
240  goto_functions.function_map.find(symbol.name);
241 
242  if(
243  !harness && func_entry != goto_functions.function_map.end() &&
244  func_entry->second.body_available() &&
245  (symbol.name == ID_main ||
246  (config.main.has_value() && symbol.name == config.main.value())))
247  {
248  skip_function_main=true;
249  }
250  }
251  }
252 
253  // function declarations and definitions
254  for(std::set<std::string>::const_iterator
255  it=symbols_sorted.begin();
256  it!=symbols_sorted.end();
257  ++it)
258  {
259  const symbolt &symbol=ns.lookup(*it);
260 
261  if(symbol.type.id()!=ID_code ||
262  symbol.is_type)
263  continue;
264 
266  symbol,
267  skip_function_main,
268  func_decl_stream,
269  func_body_stream,
270  local_static_decls);
271  }
272 
273  // (possibly modified) compound types
274  for(std::set<std::string>::const_iterator
275  it=symbols_sorted.begin();
276  it!=symbols_sorted.end();
277  ++it)
278  {
279  const symbolt &symbol=ns.lookup(*it);
280 
281  if(
282  symbol.is_type &&
283  (symbol.type.id() == ID_struct || symbol.type.id() == ID_union))
285  symbol,
286  compound_body_stream);
287  }
288 
289  // Dump the code to the target stream;
290  // the statements before to this point collect the code to dump!
291  for(std::set<std::string>::const_iterator
292  it=system_headers.begin();
293  it!=system_headers.end();
294  ++it)
295  os << "#include <" << *it << ">\n";
296  if(!system_headers.empty())
297  os << '\n';
298 
299  if(!global_decl_header_stream.str().empty() && dump_c_config.include_headers)
300  os << global_decl_header_stream.str() << '\n';
301 
302  if(global_var_stream.str().find("NULL")!=std::string::npos ||
303  func_body_stream.str().find("NULL")!=std::string::npos)
304  {
305  os << "#ifndef NULL\n"
306  << "#define NULL ((void*)0)\n"
307  << "#endif\n\n";
308  }
309  if(func_body_stream.str().find("FENCE")!=std::string::npos)
310  {
311  os << "#ifndef FENCE\n"
312  << "#define FENCE(x) ((void)0)\n"
313  << "#endif\n\n";
314  }
315  if(func_body_stream.str().find("IEEE_FLOAT_")!=std::string::npos)
316  {
317  os << "#ifndef IEEE_FLOAT_EQUAL\n"
318  << "#define IEEE_FLOAT_EQUAL(x,y) ((x)==(y))\n"
319  << "#endif\n"
320  << "#ifndef IEEE_FLOAT_NOTEQUAL\n"
321  << "#define IEEE_FLOAT_NOTEQUAL(x,y) ((x)!=(y))\n"
322  << "#endif\n\n";
323  }
324 
325  if(!global_decl_stream.str().empty() && dump_c_config.include_global_decls)
326  os << global_decl_stream.str() << '\n';
327 
329  dump_typedefs(os);
330 
331  if(!func_decl_stream.str().empty() && dump_c_config.include_function_decls)
332  os << func_decl_stream.str() << '\n';
333  if(!compound_body_stream.str().empty() && dump_c_config.include_compounds)
334  os << compound_body_stream.str() << '\n';
335  if(!global_var_stream.str().empty() && dump_c_config.include_global_vars)
336  os << global_var_stream.str() << '\n';
338  os << func_body_stream.str();
339 }
340 
343  const symbolt &symbol,
344  std::ostream &os_body)
345 {
346  if(
347  !symbol.location.get_function().empty() ||
348  symbol.type.get_bool(ID_C_do_not_dump))
349  {
350  return;
351  }
352 
353  // do compound type body
354  if(symbol.type.id() == ID_struct)
356  symbol.type,
357  struct_tag_typet(symbol.name),
359  os_body);
360  else if(symbol.type.id() == ID_union)
362  symbol.type,
363  union_tag_typet(symbol.name),
365  os_body);
366  else if(symbol.type.id() == ID_c_enum)
368  symbol.type,
369  c_enum_tag_typet(symbol.name),
371  os_body);
372 }
373 
375  const typet &type,
376  const typet &unresolved,
377  bool recursive,
378  std::ostream &os)
379 {
380  if(
381  type.id() == ID_c_enum_tag || type.id() == ID_struct_tag ||
382  type.id() == ID_union_tag)
383  {
384  const symbolt &symbol = ns.lookup(to_tag_type(type));
385  DATA_INVARIANT(symbol.is_type, "tag expected to be type symbol");
386 
388  convert_compound(symbol.type, unresolved, recursive, os);
389  }
390  else if(type.id()==ID_array || type.id()==ID_pointer)
391  {
392  if(!recursive)
393  return;
394 
395  convert_compound(type.subtype(), type.subtype(), recursive, os);
396 
397  // sizeof may contain a type symbol that has to be declared first
398  if(type.id()==ID_array)
399  {
400  find_symbols_sett syms;
401  find_non_pointer_type_symbols(to_array_type(type).size(), syms);
402 
403  for(find_symbols_sett::const_iterator
404  it=syms.begin();
405  it!=syms.end();
406  ++it)
407  {
408  const symbolt &type_symbol = ns.lookup(*it);
409  irep_idt tag_kind =
410  type_symbol.type.id() == ID_c_enum
411  ? ID_c_enum_tag
412  : (type_symbol.type.id() == ID_union ? ID_union_tag
413  : ID_struct_tag);
414  tag_typet s_type(tag_kind, *it);
415  convert_compound(s_type, s_type, recursive, os);
416  }
417  }
418  }
419  else if(type.id()==ID_struct || type.id()==ID_union)
420  convert_compound(to_struct_union_type(type), unresolved, recursive, os);
421  else if(type.id()==ID_c_enum)
422  convert_compound_enum(type, os);
423 }
424 
426  const struct_union_typet &type,
427  const typet &unresolved,
428  bool recursive,
429  std::ostream &os)
430 {
431  const irep_idt &name=type.get(ID_tag);
432 
433  if(!converted_compound.insert(name).second || type.get_bool(ID_C_do_not_dump))
434  return;
435 
436  // make sure typedef names used in the declaration are available
437  collect_typedefs(type, true);
438 
439  const irept &bases = type.find(ID_bases);
440  std::stringstream base_decls;
441  for(const auto &parent : bases.get_sub())
442  {
443  UNREACHABLE;
444  (void)parent;
445 #if 0
446  assert(parent.id() == ID_base);
447  assert(parent.get(ID_type) == ID_struct_tag);
448 
449  const irep_idt &base_id=
450  parent.find(ID_type).get(ID_identifier);
451  const irep_idt &renamed_base_id=global_renaming[base_id];
452  const symbolt &parsymb=ns.lookup(renamed_base_id);
453 
454  convert_compound_rec(parsymb.type, os);
455 
456  base_decls << id2string(renamed_base_id) +
457  (parent_it+1==bases.get_sub().end()?"":", ");
458 #endif
459  }
460 
461 #if 0
462  // for the constructor
463  string constructor_args;
464  string constructor_body;
465 
466  std::string component_name = id2string(renaming[compo.get(ID_name)]);
467  assert(component_name != "");
468 
469  if(it != struct_type.components().begin()) constructor_args += ", ";
470 
471  if(compo.type().id() == ID_pointer)
472  constructor_args += type_to_string(compo.type()) + component_name;
473  else
474  constructor_args += "const " + type_to_string(compo.type()) + "& " + component_name;
475 
476  constructor_body += indent + indent + "this->"+component_name + " = " + component_name + ";\n";
477 #endif
478 
479  std::stringstream struct_body;
480 
481  for(const auto &comp : type.components())
482  {
483  const typet &comp_type = comp.type();
484 
485  if(comp_type.id()==ID_code ||
486  comp.get_bool(ID_from_base) ||
487  comp.get_is_padding())
488  continue;
489 
490  const typet *non_array_type = &comp_type;
491  while(non_array_type->id()==ID_array)
492  non_array_type = &(non_array_type->subtype());
493 
494  if(recursive)
495  {
496  if(non_array_type->id()!=ID_pointer)
497  convert_compound(comp.type(), comp.type(), recursive, os);
498  else
499  collect_typedefs(comp.type(), true);
500  }
501 
502  irep_idt comp_name=comp.get_name();
503 
504  struct_body << indent(1) << "// " << comp_name << '\n';
505  struct_body << indent(1);
506 
507  // component names such as "main" would collide with other objects in the
508  // namespace
509  std::string fake_unique_name="NO/SUCH/NS::"+id2string(comp_name);
510  std::string s=make_decl(fake_unique_name, comp.type());
511  POSTCONDITION(s.find("NO/SUCH/NS")==std::string::npos);
512 
513  if(comp_type.id()==ID_c_bit_field &&
514  to_c_bit_field_type(comp_type).get_width()==0)
515  {
516  comp_name.clear();
517  s=type_to_string(comp_type);
518  }
519 
520  if(s.find(CPROVER_PREFIX "bitvector") == std::string::npos)
521  {
522  struct_body << s;
523  }
524  else if(comp_type.id()==ID_signedbv)
525  {
526  const signedbv_typet &t=to_signedbv_type(comp_type);
528  struct_body << "long long int " << comp_name
529  << " : " << t.get_width();
530  else if(mode == ID_cpp)
531  struct_body << "__signedbv<" << t.get_width() << "> "
532  << comp_name;
533  else
534  struct_body << s;
535  }
536  else if(comp_type.id()==ID_unsignedbv)
537  {
538  const unsignedbv_typet &t=to_unsignedbv_type(comp_type);
540  struct_body << "unsigned long long " << comp_name
541  << " : " << t.get_width();
542  else if(mode == ID_cpp)
543  struct_body << "__unsignedbv<" << t.get_width() << "> "
544  << comp_name;
545  else
546  struct_body << s;
547  }
548  else
549  UNREACHABLE;
550 
551  struct_body << ";\n";
552  }
553 
554  typet unresolved_clean=unresolved;
555  irep_idt typedef_str;
556  for(auto td_entry : typedef_types)
557  {
558  if(
559  td_entry.first.get(ID_identifier) == unresolved.get(ID_identifier) &&
560  (td_entry.first.source_location() == unresolved.source_location()))
561  {
562  unresolved_clean.remove(ID_C_typedef);
563  typedef_str = td_entry.second;
564  std::pair<typedef_mapt::iterator, bool> td_map_entry =
565  typedef_map.insert({typedef_str, typedef_infot(typedef_str)});
566  PRECONDITION(!td_map_entry.second);
567  if(!td_map_entry.first->second.early)
568  td_map_entry.first->second.type_decl_str.clear();
569  os << "typedef ";
570  break;
571  }
572  }
573 
574  os << type_to_string(unresolved_clean);
575  if(!base_decls.str().empty())
576  {
577  PRECONDITION(mode == ID_cpp);
578  os << ": " << base_decls.str();
579  }
580  os << '\n';
581  os << "{\n";
582  os << struct_body.str();
583 
584  /*
585  if(!struct_type.components().empty())
586  {
587  os << indent << name << "(){}\n";
588  os << indent << "explicit " << name
589  << "(" + constructor_args + ")\n";
590  os << indent << "{\n";
591  os << constructor_body;
592  os << indent << "}\n";
593  }
594  */
595 
596  os << "}";
597  if(type.get_bool(ID_C_transparent_union))
598  os << " __attribute__ ((__transparent_union__))";
599  if(type.get_bool(ID_C_packed))
600  os << " __attribute__ ((__packed__))";
601  if(!typedef_str.empty())
602  os << " " << typedef_str;
603  os << ";\n\n";
604 }
605 
607  const typet &type,
608  std::ostream &os)
609 {
610  PRECONDITION(type.id()==ID_c_enum);
611 
612  const irept &tag=type.find(ID_tag);
613  const irep_idt &name=tag.get(ID_C_base_name);
614 
615  if(tag.is_nil() ||
616  !converted_enum.insert(name).second)
617  return;
618 
619  c_enum_typet enum_type=to_c_enum_type(type);
620  c_enum_typet::memberst &members=
621  (c_enum_typet::memberst &)(enum_type.add(ID_body).get_sub());
622  for(c_enum_typet::memberst::iterator
623  it=members.begin();
624  it!=members.end();
625  ++it)
626  {
627  const irep_idt bn=it->get_base_name();
628 
629  if(declared_enum_constants.find(bn)!=
630  declared_enum_constants.end() ||
632  {
633  std::string new_bn=id2string(name)+"$$"+id2string(bn);
634  it->set_base_name(new_bn);
635  }
636 
638  std::make_pair(bn, it->get_base_name()));
639  }
640 
641  os << type_to_string(enum_type);
642 
643  if(enum_type.get_bool(ID_C_packed))
644  os << " __attribute__ ((__packed__))";
645 
646  os << ";\n\n";
647 }
648 
650  code_declt &decl,
651  std::list<irep_idt> &local_static,
652  std::list<irep_idt> &local_type_decls)
653 {
654  goto_programt tmp;
655  tmp.add(goto_programt::make_decl(decl.symbol()));
656 
657  if(optionalt<exprt> value = decl.initial_value())
658  {
659  decl.set_initial_value({});
660  tmp.add(goto_programt::make_assignment(decl.symbol(), std::move(*value)));
661  }
662 
664 
665  // goto_program2codet requires valid location numbers:
666  tmp.update();
667 
668  std::unordered_set<irep_idt> typedef_names;
669  for(const auto &td : typedef_map)
670  typedef_names.insert(td.first);
671 
672  code_blockt b;
673  goto_program2codet p2s(
674  irep_idt(),
675  tmp,
677  b,
678  local_static,
679  local_type_decls,
680  typedef_names,
682  p2s();
683 
684  POSTCONDITION(b.statements().size() == 1);
685  decl.swap(b.op0());
686 }
687 
693 void dump_ct::collect_typedefs(const typet &type, bool early)
694 {
695  std::unordered_set<irep_idt> deps;
696  collect_typedefs_rec(type, early, deps);
697 }
698 
707  const typet &type,
708  bool early,
709  std::unordered_set<irep_idt> &dependencies)
710 {
712  return;
713 
714  std::unordered_set<irep_idt> local_deps;
715 
716  if(type.id()==ID_code)
717  {
718  const code_typet &code_type=to_code_type(type);
719 
720  collect_typedefs_rec(code_type.return_type(), early, local_deps);
721  for(const auto &param : code_type.parameters())
722  collect_typedefs_rec(param.type(), early, local_deps);
723  }
724  else if(type.id()==ID_pointer || type.id()==ID_array)
725  {
726  collect_typedefs_rec(type.subtype(), early, local_deps);
727  }
728  else if(
729  type.id() == ID_c_enum_tag || type.id() == ID_struct_tag ||
730  type.id() == ID_union_tag)
731  {
732  const symbolt &symbol = ns.lookup(to_tag_type(type));
733  collect_typedefs_rec(symbol.type, early, local_deps);
734  }
735 
736  const irep_idt &typedef_str=type.get(ID_C_typedef);
737 
738  if(!typedef_str.empty())
739  {
740  std::pair<typedef_mapt::iterator, bool> entry=
741  typedef_map.insert({typedef_str, typedef_infot(typedef_str)});
742 
743  if(entry.second ||
744  (early && entry.first->second.type_decl_str.empty()))
745  {
746  if(typedef_str=="__gnuc_va_list" || typedef_str == "va_list")
747  {
748  system_headers.insert("stdarg.h");
749  early=false;
750  }
751  else
752  {
753  typet t=type;
754  t.remove(ID_C_typedef);
755 
756  std::ostringstream oss;
757  oss << "typedef " << make_decl(typedef_str, t) << ';';
758 
759  entry.first->second.type_decl_str=oss.str();
760  entry.first->second.dependencies=local_deps;
761  }
762  }
763 
764  if(early)
765  {
766  entry.first->second.early=true;
767 
768  for(const auto &d : local_deps)
769  {
770  auto td_entry=typedef_map.find(d);
771  PRECONDITION(td_entry!=typedef_map.end());
772  td_entry->second.early=true;
773  }
774  }
775 
776  dependencies.insert(typedef_str);
777  }
778 
779  dependencies.insert(local_deps.begin(), local_deps.end());
780 }
781 
784 {
785  // sort the symbols first to ensure deterministic replacement in
786  // typedef_types below as there could be redundant declarations
787  // typedef int x;
788  // typedef int y;
789  std::map<std::string, symbolt> symbols_sorted;
790  for(const auto &symbol_entry : copied_symbol_table.symbols)
791  symbols_sorted.insert(
792  {id2string(symbol_entry.first), symbol_entry.second});
793 
794  for(const auto &symbol_entry : symbols_sorted)
795  {
796  const symbolt &symbol=symbol_entry.second;
797 
798  if(symbol.is_macro && symbol.is_type &&
799  symbol.location.get_function().empty())
800  {
801  const irep_idt &typedef_str=symbol.type.get(ID_C_typedef);
802  PRECONDITION(!typedef_str.empty());
803  typedef_types[symbol.type]=typedef_str;
805  typedef_map.insert({typedef_str, typedef_infot(typedef_str)});
806  else
807  collect_typedefs(symbol.type, false);
808  }
809  }
810 }
811 
814 void dump_ct::dump_typedefs(std::ostream &os) const
815 {
816  // we need to compute a topological sort; we do so by picking all
817  // typedefs the dependencies of which have been emitted into to_insert
818  std::vector<typedef_infot> typedefs_sorted;
819  typedefs_sorted.reserve(typedef_map.size());
820 
821  // elements in to_insert are lexicographically sorted and ready for
822  // output
823  std::map<std::string, typedef_infot> to_insert;
824 
825  std::unordered_set<irep_idt> typedefs_done;
826  std::unordered_map<irep_idt, std::unordered_set<irep_idt>> forward_deps,
827  reverse_deps;
828 
829  for(const auto &td : typedef_map)
830  if(!td.second.type_decl_str.empty())
831  {
832  if(td.second.dependencies.empty())
833  // those can be dumped immediately
834  to_insert.insert({id2string(td.first), td.second});
835  else
836  {
837  // delay them until dependencies are dumped
838  forward_deps.insert({td.first, td.second.dependencies});
839  for(const auto &d : td.second.dependencies)
840  reverse_deps[d].insert(td.first);
841  }
842  }
843 
844  while(!to_insert.empty())
845  {
846  // the topologically next element (lexicographically ranked first
847  // among all the dependencies of which have been dumped)
848  typedef_infot t=to_insert.begin()->second;
849  to_insert.erase(to_insert.begin());
850  // move to the output queue
851  typedefs_sorted.push_back(t);
852 
853  // find any depending typedefs that are now valid, or at least
854  // reduce the remaining dependencies
855  auto r_it=reverse_deps.find(t.typedef_name);
856  if(r_it==reverse_deps.end())
857  continue;
858 
859  // reduce remaining dependencies
860  std::unordered_set<irep_idt> &r_deps = r_it->second;
861  for(std::unordered_set<irep_idt>::iterator it = r_deps.begin();
862  it != r_deps.end();) // no ++it
863  {
864  auto f_it=forward_deps.find(*it);
865  if(f_it==forward_deps.end()) // might be done already
866  {
867  it=r_deps.erase(it);
868  continue;
869  }
870 
871  // update dependencies
872  std::unordered_set<irep_idt> &f_deps = f_it->second;
873  PRECONDITION(!f_deps.empty());
874  PRECONDITION(f_deps.find(t.typedef_name)!=f_deps.end());
875  f_deps.erase(t.typedef_name);
876 
877  if(f_deps.empty()) // all depenencies done now!
878  {
879  const auto td_entry=typedef_map.find(*it);
880  PRECONDITION(td_entry!=typedef_map.end());
881  to_insert.insert({id2string(*it), td_entry->second});
882  forward_deps.erase(*it);
883  it=r_deps.erase(it);
884  }
885  else
886  ++it;
887  }
888  }
889 
890  POSTCONDITION(forward_deps.empty());
891 
892  for(const auto &td : typedefs_sorted)
893  os << td.type_decl_str << '\n';
894 
895  if(!typedefs_sorted.empty())
896  os << '\n';
897 }
898 
900  const symbolt &symbol,
901  std::ostream &os,
902  local_static_declst &local_static_decls)
903 {
904  const irep_idt &func=symbol.location.get_function();
905  if((func.empty() || symbol.is_extern || symbol.value.is_not_nil()) &&
906  !converted_global.insert(symbol.name).second)
907  return;
908 
909  code_declt d(symbol.symbol_expr());
910 
911  find_symbols_sett syms;
912  if(symbol.value.is_not_nil())
913  find_symbols_or_nexts(symbol.value, syms);
914 
915  // add a tentative declaration to cater for symbols in the initializer
916  // relying on it this symbol
917  if((func.empty() || symbol.is_extern) &&
918  (symbol.value.is_nil() || !syms.empty()))
919  {
920  os << "// " << symbol.name << '\n';
921  os << "// " << symbol.location << '\n';
922  os << expr_to_string(d) << '\n';
923  }
924 
925  if(!symbol.value.is_nil())
926  {
927  std::set<std::string> symbols_sorted;
928  for(find_symbols_sett::const_iterator
929  it=syms.begin();
930  it!=syms.end();
931  ++it)
932  {
933  bool inserted=symbols_sorted.insert(id2string(*it)).second;
934  CHECK_RETURN(inserted);
935  }
936 
937  for(std::set<std::string>::const_iterator
938  it=symbols_sorted.begin();
939  it!=symbols_sorted.end();
940  ++it)
941  {
942  const symbolt &sym=ns.lookup(*it);
943  if(!sym.is_type && sym.is_static_lifetime && sym.type.id()!=ID_code)
944  convert_global_variable(sym, os, local_static_decls);
945  }
946 
947  d.copy_to_operands(symbol.value);
948  }
949 
950  if(!func.empty() && !symbol.is_extern)
951  {
952  local_static_decls.emplace(symbol.name, d);
953  }
954  else if(!symbol.value.is_nil())
955  {
956  os << "// " << symbol.name << '\n';
957  os << "// " << symbol.location << '\n';
958 
959  std::list<irep_idt> empty_static, empty_types;
960  cleanup_decl(d, empty_static, empty_types);
961  CHECK_RETURN(empty_static.empty());
962  os << expr_to_string(d) << '\n';
963  }
964 }
965 
970 {
972  code_blockt decls;
973 
974  const symbolt *argc_sym=nullptr;
975  if(!ns.lookup("argc'", argc_sym))
976  {
977  symbol_exprt argc("argc", argc_sym->type);
978  replace.insert(argc_sym->symbol_expr(), argc);
979  code_declt d(argc);
980  decls.add(d);
981  }
982  const symbolt *argv_sym=nullptr;
983  if(!ns.lookup("argv'", argv_sym))
984  {
985  symbol_exprt argv("argv", argv_sym->type);
986  // replace argc' by argc in the type of argv['] to maintain type consistency
987  // while replacing
988  replace(argv);
989  replace.insert(symbol_exprt(argv_sym->name, argv.type()), argv);
990  code_declt d(argv);
991  decls.add(d);
992  }
993  const symbolt *return_sym=nullptr;
994  if(!ns.lookup("return'", return_sym))
995  {
996  symbol_exprt return_value("return_value", return_sym->type);
997  replace.insert(return_sym->symbol_expr(), return_value);
998  code_declt d(return_value);
999  decls.add(d);
1000  }
1001 
1002  for(auto &code : b.statements())
1003  {
1004  if(code.get_statement()==ID_function_call)
1005  {
1006  exprt &func=to_code_function_call(code).function();
1007  if(func.id()==ID_symbol)
1008  {
1009  symbol_exprt &s=to_symbol_expr(func);
1010  if(s.get_identifier()==ID_main)
1012  else if(s.get_identifier() == INITIALIZE_FUNCTION)
1013  continue;
1014  }
1015  }
1016 
1017  decls.add(code);
1018  }
1019 
1020  b.swap(decls);
1021  replace(b);
1022 }
1023 
1025  const symbolt &symbol,
1026  const bool skip_main,
1027  std::ostream &os_decl,
1028  std::ostream &os_body,
1029  local_static_declst &local_static_decls)
1030 {
1031  // don't dump artificial main
1032  if(skip_main && symbol.name==goto_functionst::entry_point())
1033  return;
1034 
1035  // convert the goto program back to code - this might change
1036  // the function type
1037  goto_functionst::function_mapt::const_iterator func_entry=
1038  goto_functions.function_map.find(symbol.name);
1039  if(func_entry!=goto_functions.function_map.end() &&
1040  func_entry->second.body_available())
1041  {
1042  code_blockt b;
1043  std::list<irep_idt> type_decls, local_static;
1044 
1045  std::unordered_set<irep_idt> typedef_names;
1046  for(const auto &td : typedef_map)
1047  typedef_names.insert(td.first);
1048 
1049  goto_program2codet p2s(
1050  symbol.name,
1051  func_entry->second.body,
1053  b,
1054  local_static,
1055  type_decls,
1056  typedef_names,
1057  system_headers);
1058  p2s();
1059 
1061  b,
1062  local_static,
1063  local_static_decls,
1064  type_decls);
1065 
1066  convertedt converted_c_bak(converted_compound);
1067  convertedt converted_e_bak(converted_enum);
1068 
1070  enum_constants_bak(declared_enum_constants);
1071 
1073  b,
1074  type_decls);
1075 
1076  converted_enum.swap(converted_e_bak);
1077  converted_compound.swap(converted_c_bak);
1078 
1079  if(harness && symbol.name==goto_functions.entry_point())
1080  cleanup_harness(b);
1081 
1082  os_body << "// " << symbol.name << '\n';
1083  os_body << "// " << symbol.location << '\n';
1084  if(symbol.name==goto_functions.entry_point())
1085  os_body << make_decl(ID_main, symbol.type) << '\n';
1086  else if(!harness || symbol.name!=ID_main)
1087  os_body << make_decl(symbol.name, symbol.type) << '\n';
1088  else if(harness && symbol.name==ID_main)
1089  {
1090  os_body << make_decl(CPROVER_PREFIX+id2string(symbol.name), symbol.type)
1091  << '\n';
1092  }
1093  os_body << expr_to_string(b);
1094  os_body << "\n\n";
1095 
1096  declared_enum_constants.swap(enum_constants_bak);
1097  }
1098 
1099  if(symbol.name!=goto_functionst::entry_point() &&
1100  symbol.name!=ID_main)
1101  {
1102  os_decl << "// " << symbol.name << '\n';
1103  os_decl << "// " << symbol.location << '\n';
1104  os_decl << make_decl(symbol.name, symbol.type) << ";\n";
1105  }
1106  else if(harness && symbol.name==ID_main)
1107  {
1108  os_decl << "// " << symbol.name << '\n';
1109  os_decl << "// " << symbol.location << '\n';
1110  os_decl << make_decl(CPROVER_PREFIX+id2string(symbol.name), symbol.type)
1111  << ";\n";
1112  }
1113 
1114  // make sure typedef names used in the function declaration are
1115  // available
1116  collect_typedefs(symbol.type, true);
1117 }
1118 
1120  const irep_idt &identifier,
1121  codet &root,
1122  code_blockt* &dest,
1123  exprt::operandst::iterator &before)
1124 {
1125  if(!root.has_operands())
1126  return false;
1127 
1128  code_blockt *our_dest=nullptr;
1129 
1130  exprt::operandst &operands=root.operands();
1131  exprt::operandst::iterator first_found=operands.end();
1132 
1133  Forall_expr(it, operands)
1134  {
1135  bool found=false;
1136 
1137  // be aware of the skip-carries-type hack
1138  if(it->id()==ID_code &&
1139  to_code(*it).get_statement()!=ID_skip)
1141  identifier,
1142  to_code(*it),
1143  our_dest,
1144  before);
1145  else
1146  {
1147  find_symbols_sett syms;
1148  find_type_and_expr_symbols(*it, syms);
1149 
1150  found=syms.find(identifier)!=syms.end();
1151  }
1152 
1153  if(!found)
1154  continue;
1155 
1156  if(!our_dest)
1157  {
1158  // first containing block
1159  if(root.get_statement()==ID_block)
1160  {
1161  dest=&(to_code_block(root));
1162  before=it;
1163  }
1164 
1165  return true;
1166  }
1167  else
1168  {
1169  // there is a containing block and this is the first operand
1170  // that contains identifier
1171  if(first_found==operands.end())
1172  first_found=it;
1173  // we have seen it already - pick the first occurrence in this
1174  // block
1175  else if(root.get_statement()==ID_block)
1176  {
1177  dest=&(to_code_block(root));
1178  before=first_found;
1179 
1180  return true;
1181  }
1182  // we have seen it already - outer block has to deal with this
1183  else
1184  return true;
1185  }
1186  }
1187 
1188  if(first_found!=operands.end())
1189  {
1190  dest=our_dest;
1191 
1192  return true;
1193  }
1194 
1195  return false;
1196 }
1197 
1199  code_blockt &b,
1200  const std::list<irep_idt> &local_static,
1201  local_static_declst &local_static_decls,
1202  std::list<irep_idt> &type_decls)
1203 {
1204  // look up last identifier first as its value may introduce the
1205  // other ones
1206  for(std::list<irep_idt>::const_reverse_iterator
1207  it=local_static.rbegin();
1208  it!=local_static.rend();
1209  ++it)
1210  {
1211  local_static_declst::const_iterator d_it=
1212  local_static_decls.find(*it);
1213  PRECONDITION(d_it!=local_static_decls.end());
1214 
1215  code_declt d=d_it->second;
1216  std::list<irep_idt> redundant;
1217  cleanup_decl(d, redundant, type_decls);
1218 
1219  code_blockt *dest_ptr=nullptr;
1220  exprt::operandst::iterator before=b.operands().end();
1221 
1222  // some use of static variables might be optimised out if it is
1223  // within an if(false) { ... } block
1224  if(find_block_position_rec(*it, b, dest_ptr, before))
1225  {
1226  CHECK_RETURN(dest_ptr!=nullptr);
1227  dest_ptr->operands().insert(before, d);
1228  }
1229  }
1230 }
1231 
1233  code_blockt &b,
1234  const std::list<irep_idt> &type_decls)
1235 {
1236  // look up last identifier first as its value may introduce the
1237  // other ones
1238  for(std::list<irep_idt>::const_reverse_iterator
1239  it=type_decls.rbegin();
1240  it!=type_decls.rend();
1241  ++it)
1242  {
1243  const typet &type=ns.lookup(*it).type;
1244  // feed through plain C to expr2c by ending and re-starting
1245  // a comment block ...
1246  std::ostringstream os_body;
1247  os_body << *it << " */\n";
1248  irep_idt tag_kind =
1249  type.id() == ID_c_enum
1250  ? ID_c_enum_tag
1251  : (type.id() == ID_union ? ID_union_tag : ID_struct_tag);
1252  convert_compound(type, tag_typet(tag_kind, *it), false, os_body);
1253  os_body << "/*";
1254 
1255  code_skipt skip;
1256  skip.add_source_location().set_comment(os_body.str());
1257  // another hack to ensure symbols inside types are seen
1258  skip.type()=type;
1259 
1260  code_blockt *dest_ptr=nullptr;
1261  exprt::operandst::iterator before=b.operands().end();
1262 
1263  // we might not find it in case a transparent union type cast
1264  // has been removed by cleanup operations
1265  if(find_block_position_rec(*it, b, dest_ptr, before))
1266  {
1267  CHECK_RETURN(dest_ptr!=nullptr);
1268  dest_ptr->operands().insert(before, skip);
1269  }
1270  }
1271 }
1272 
1274 {
1275  Forall_operands(it, expr)
1276  cleanup_expr(*it);
1277 
1278  cleanup_type(expr.type());
1279 
1280  if(expr.id()==ID_struct)
1281  {
1282  struct_typet type=
1283  to_struct_type(ns.follow(expr.type()));
1284 
1285  struct_union_typet::componentst old_components;
1286  old_components.swap(type.components());
1287 
1288  exprt::operandst old_ops;
1289  old_ops.swap(expr.operands());
1290 
1291  PRECONDITION(old_components.size()==old_ops.size());
1292  exprt::operandst::iterator o_it=old_ops.begin();
1293  for(const auto &old_comp : old_components)
1294  {
1295  const bool is_zero_bit_field =
1296  old_comp.type().id() == ID_c_bit_field &&
1297  to_c_bit_field_type(old_comp.type()).get_width() == 0;
1298 
1299  if(!old_comp.get_is_padding() && !is_zero_bit_field)
1300  {
1301  type.components().push_back(old_comp);
1302  expr.add_to_operands(std::move(*o_it));
1303  }
1304  ++o_it;
1305  }
1306  expr.type().swap(type);
1307  }
1308  else if(expr.id()==ID_union)
1309  {
1310  union_exprt &u=to_union_expr(expr);
1311  const union_typet &u_type_f=to_union_type(ns.follow(u.type()));
1312 
1313  if(!u.type().get_bool(ID_C_transparent_union) &&
1314  !u_type_f.get_bool(ID_C_transparent_union))
1315  {
1316  if(u_type_f.get_component(u.get_component_name()).get_is_padding())
1317  // we just use an empty struct to fake an empty union
1318  expr = struct_exprt({}, struct_typet());
1319  }
1320  // add a typecast for NULL
1321  else if(u.op().id()==ID_constant &&
1322  u.op().type().id()==ID_pointer &&
1323  u.op().type().subtype().id()==ID_empty &&
1324  (u.op().is_zero() ||
1325  to_constant_expr(u.op()).get_value()==ID_NULL))
1326  {
1327  const struct_union_typet::componentt &comp=
1328  u_type_f.get_component(u.get_component_name());
1329  const typet &u_op_type=comp.type();
1330  PRECONDITION(u_op_type.id()==ID_pointer);
1331 
1332  typecast_exprt tc(u.op(), u_op_type);
1333  expr.swap(tc);
1334  }
1335  else
1336  {
1337  exprt tmp;
1338  tmp.swap(u.op());
1339  expr.swap(tmp);
1340  }
1341  }
1342  else if(
1343  expr.id() == ID_typecast &&
1344  to_typecast_expr(expr).op().id() == ID_typecast &&
1345  expr.type() == to_typecast_expr(expr).op().type())
1346  {
1347  exprt tmp = to_typecast_expr(expr).op();
1348  expr.swap(tmp);
1349  }
1350  else if(expr.id()==ID_code &&
1351  to_code(expr).get_statement()==ID_function_call &&
1352  to_code_function_call(to_code(expr)).function().id()==ID_symbol)
1353  {
1355  const symbol_exprt &fn=to_symbol_expr(call.function());
1356  code_function_callt::argumentst &arguments=call.arguments();
1357 
1358  // don't edit function calls we might have introduced
1359  const symbolt *s;
1360  if(!ns.lookup(fn.get_identifier(), s))
1361  {
1362  const symbolt &fn_sym=ns.lookup(fn.get_identifier());
1363  const code_typet &code_type=to_code_type(fn_sym.type);
1364  const code_typet::parameterst &parameters=code_type.parameters();
1365 
1366  if(parameters.size()==arguments.size())
1367  {
1368  code_typet::parameterst::const_iterator it=parameters.begin();
1369  for(auto &argument : arguments)
1370  {
1371  const typet &type=ns.follow(it->type());
1372  if(type.id()==ID_union &&
1373  type.get_bool(ID_C_transparent_union))
1374  {
1375  if(argument.id() == ID_typecast && argument.type() == type)
1376  argument = to_typecast_expr(argument).op();
1377 
1378  // add a typecast for NULL or 0
1379  if(
1380  argument.id() == ID_constant &&
1381  (argument.is_zero() ||
1382  to_constant_expr(argument).get_value() == ID_NULL))
1383  {
1384  const typet &comp_type=
1385  to_union_type(type).components().front().type();
1386 
1387  if(comp_type.id()==ID_pointer)
1388  argument = typecast_exprt(argument, comp_type);
1389  }
1390  }
1391 
1392  ++it;
1393  }
1394  }
1395  }
1396  }
1397  else if(expr.id()==ID_constant &&
1398  expr.type().id()==ID_signedbv)
1399  {
1400  #if 0
1401  const irep_idt &cformat=expr.get(ID_C_cformat);
1402 
1403  if(!cformat.empty())
1404  {
1405  declared_enum_constants_mapt::const_iterator entry=
1406  declared_enum_constants.find(cformat);
1407 
1408  if(entry!=declared_enum_constants.end() &&
1409  entry->first!=entry->second)
1410  expr.set(ID_C_cformat, entry->second);
1411  else if(entry==declared_enum_constants.end() &&
1412  !std::isdigit(id2string(cformat)[0]))
1413  expr.remove(ID_C_cformat);
1414  }
1415  #endif
1416  }
1417  else if(
1418  expr.id() == ID_byte_update_little_endian ||
1419  expr.id() == ID_byte_update_big_endian)
1420  {
1421  const byte_update_exprt &bu = to_byte_update_expr(expr);
1422 
1423  if(bu.op().id() == ID_union && bu.offset().is_zero())
1424  {
1425  const union_exprt &union_expr = to_union_expr(bu.op());
1426  const union_typet &union_type =
1427  to_union_type(ns.follow(union_expr.type()));
1428 
1429  for(const auto &comp : union_type.components())
1430  {
1431  if(bu.value().type() == comp.type())
1432  {
1433  exprt member1{ID_member};
1434  member1.set(ID_component_name, union_expr.get_component_name());
1435  exprt designated_initializer1{ID_designated_initializer};
1436  designated_initializer1.add_to_operands(union_expr.op());
1437  designated_initializer1.add(ID_designator).move_to_sub(member1);
1438 
1439  exprt member2{ID_member};
1440  member2.set(ID_component_name, comp.get_name());
1441  exprt designated_initializer2{ID_designated_initializer};
1442  designated_initializer2.add_to_operands(bu.value());
1443  designated_initializer2.add(ID_designator).move_to_sub(member2);
1444 
1445  binary_exprt initializer_list{std::move(designated_initializer1),
1446  ID_initializer_list,
1447  std::move(designated_initializer2)};
1448  expr.swap(initializer_list);
1449 
1450  return;
1451  }
1452  }
1453  }
1454  else if(
1455  bu.op().id() == ID_side_effect &&
1456  to_side_effect_expr(bu.op()).get_statement() == ID_nondet &&
1457  ns.follow(bu.op().type()).id() == ID_union && bu.offset().is_zero())
1458  {
1459  const union_typet &union_type = to_union_type(ns.follow(bu.op().type()));
1460 
1461  for(const auto &comp : union_type.components())
1462  {
1463  if(bu.value().type() == comp.type())
1464  {
1465  union_exprt union_expr{comp.get_name(), bu.value(), bu.op().type()};
1466  expr.swap(union_expr);
1467 
1468  return;
1469  }
1470  }
1471  }
1472 
1473  optionalt<exprt> clean_init;
1474  if(
1475  ns.follow(bu.type()).id() == ID_union &&
1477  {
1478  clean_init = zero_initializer(bu.op().type(), source_locationt{}, ns)
1479  .value_or(nil_exprt{});
1480  if(clean_init->id() != ID_struct || clean_init->has_operands())
1481  cleanup_expr(*clean_init);
1482  }
1483 
1484  if(clean_init.has_value() && bu.op() == *clean_init)
1485  {
1486  const union_typet &union_type = to_union_type(ns.follow(bu.type()));
1487 
1488  for(const auto &comp : union_type.components())
1489  {
1490  if(bu.value().type() == comp.type())
1491  {
1492  union_exprt union_expr{comp.get_name(), bu.value(), bu.type()};
1493  expr.swap(union_expr);
1494 
1495  return;
1496  }
1497  }
1498 
1499  // we still haven't found a suitable component, so just ignore types and
1500  // build an initializer list without designators
1501  expr = unary_exprt{ID_initializer_list, bu.value()};
1502  }
1503  }
1504 }
1505 
1507 {
1508  for(typet &subtype : to_type_with_subtypes(type).subtypes())
1509  cleanup_type(subtype);
1510 
1511  if(type.id()==ID_code)
1512  {
1513  code_typet &code_type=to_code_type(type);
1514 
1515  cleanup_type(code_type.return_type());
1516 
1517  for(code_typet::parameterst::iterator
1518  it=code_type.parameters().begin();
1519  it!=code_type.parameters().end();
1520  ++it)
1521  cleanup_type(it->type());
1522  }
1523 
1524  if(type.id()==ID_array)
1525  cleanup_expr(to_array_type(type).size());
1526 
1527  POSTCONDITION(
1528  (type.id()!=ID_union && type.id()!=ID_struct) ||
1529  !type.get(ID_tag).empty());
1530 }
1531 
1533 {
1534  // future TODO: with C++20 we can actually use designated initializers as
1535  // commented out below
1536  static expr2c_configurationt configuration{
1537  /* .include_struct_padding_components = */ true,
1538  /* .print_struct_body_in_type = */ true,
1539  /* .include_array_size = */ true,
1540  /* .true_string = */ "1",
1541  /* .false_string = */ "0",
1542  /* .use_library_macros = */ true,
1543  /* .print_enum_int_value = */ false,
1544  /* .expand_typedef = */ false};
1545 
1546  return configuration;
1547 }
1548 
1549 std::string dump_ct::type_to_string(const typet &type)
1550 {
1551  std::string ret;
1552  typet t=type;
1553  cleanup_type(t);
1554 
1555  if(mode == ID_C)
1556  return type2c(t, ns, expr2c_configuration());
1557  else if(mode == ID_cpp)
1558  return type2cpp(t, ns);
1559  else
1560  UNREACHABLE;
1561 }
1562 
1563 std::string dump_ct::expr_to_string(const exprt &expr)
1564 {
1565  std::string ret;
1566  exprt e=expr;
1567  cleanup_expr(e);
1568 
1569  if(mode == ID_C)
1570  return expr2c(e, ns, expr2c_configuration());
1571  else if(mode == ID_cpp)
1572  return expr2cpp(e, ns);
1573  else
1574  UNREACHABLE;
1575 }
1576 
1577 void dump_c(
1578  const goto_functionst &src,
1579  const bool use_system_headers,
1580  const bool use_all_headers,
1581  const bool include_harness,
1582  const namespacet &ns,
1583  std::ostream &out)
1584 {
1585  dump_ct goto2c(
1586  src, use_system_headers, use_all_headers, include_harness, ns, ID_C);
1587  out << goto2c;
1588 }
1589 
1591  const goto_functionst &src,
1592  const bool use_system_headers,
1593  const bool use_all_headers,
1594  const bool include_harness,
1595  const namespacet &ns,
1596  std::ostream &out)
1597 {
1598  dump_ct goto2cpp(
1599  src, use_system_headers, use_all_headers, include_harness, ns, ID_cpp);
1600  out << goto2cpp;
1601 }
1602 
1603 static bool
1604 module_local_declaration(const symbolt &symbol, const std::string module)
1605 {
1606  std::string base_name =
1607  get_base_name(id2string(symbol.location.get_file()), true);
1608  std::string symbol_module = strip_string(id2string(symbol.module));
1609  return (base_name == module && symbol_module == module);
1610 }
1611 
1613  const goto_functionst &src,
1614  const bool use_system_headers,
1615  const bool use_all_headers,
1616  const bool include_harness,
1617  const namespacet &ns,
1618  const std::string module,
1619  std::ostream &out)
1620 {
1621  symbol_tablet symbol_table = ns.get_symbol_table();
1622  for(symbol_tablet::iteratort it = symbol_table.begin();
1623  it != symbol_table.end();
1624  it++)
1625  {
1626  symbolt &new_symbol = it.get_writeable_symbol();
1627  if(module_local_declaration(new_symbol, module))
1628  {
1629  new_symbol.type.set(ID_C_do_not_dump, 0);
1630  }
1631  else
1632  {
1633  new_symbol.type.set(ID_C_do_not_dump, 1);
1634  }
1635  }
1636 
1637  namespacet new_ns(symbol_table);
1638  dump_ct goto2c(
1639  src,
1640  use_system_headers,
1641  use_all_headers,
1642  include_harness,
1643  new_ns,
1644  ID_C,
1646  out << goto2c;
1647 }
to_union_tag_type
const union_tag_typet & to_union_tag_type(const typet &type)
Cast a typet to a union_tag_typet.
Definition: c_types.h:189
exprt::copy_to_operands
void copy_to_operands(const exprt &expr)
Copy the given argument to the end of exprt's operands.
Definition: expr.h:137
dump_ct::declared_enum_constants_mapt
std::unordered_map< irep_idt, irep_idt > declared_enum_constants_mapt
Definition: dump_c_class.h:165
UNREACHABLE
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:504
struct_union_typet::components
const componentst & components() const
Definition: std_types.h:141
symbol_table_baset::has_symbol
bool has_symbol(const irep_idt &name) const
Check whether a symbol exists in the symbol table.
Definition: symbol_table_base.h:87
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
to_union_type
const union_typet & to_union_type(const typet &type)
Cast a typet to a union_typet.
Definition: c_types.h:149
source_locationt::get_function
const irep_idt & get_function() const
Definition: source_location.h:56
to_unsignedbv_type
const unsignedbv_typet & to_unsignedbv_type(const typet &type)
Cast a typet to an unsignedbv_typet.
Definition: bitvector_types.h:191
get_base_name
std::string get_base_name(const std::string &in, bool strip_suffix)
cleans a filename from path and extension
Definition: get_base_name.cpp:16
code_blockt
A codet representing sequential composition of program statements.
Definition: std_code.h:170
dump_ct::collect_typedefs_rec
void collect_typedefs_rec(const typet &type, bool early, std::unordered_set< irep_idt > &dependencies)
Find any typedef names contained in the input type and store their declaration strings in typedef_map...
Definition: dump_c.cpp:706
Forall_expr
#define Forall_expr(it, expr)
Definition: expr.h:34
dump_c_configurationt::enable_include_headers
dump_c_configurationt enable_include_headers()
Definition: dump_c_class.h:99
symbol_tablet
The symbol table.
Definition: symbol_table.h:20
tag_typet::set_identifier
void set_identifier(const irep_idt &identifier)
Definition: std_types.h:399
typet::subtype
const typet & subtype() const
Definition: type.h:47
c_enum_typet
The type of C enums.
Definition: c_types.h:204
dump_c_configurationt::disable_include_global_vars
dump_c_configurationt disable_include_global_vars()
Definition: dump_c_class.h:81
dump_ct::indent
static std::string indent(const unsigned n)
Definition: dump_c_class.h:189
dump_ct::goto_functions
const goto_functionst & goto_functions
Definition: dump_c_class.h:151
source_locationt::set_comment
void set_comment(const irep_idt &comment)
Definition: source_location.h:141
dump_c_configurationt::default_configuration
static dump_c_configurationt default_configuration
The default used for dump-c and dump-cpp.
Definition: dump_c_class.h:52
find_type_and_expr_symbols
void find_type_and_expr_symbols(const exprt &src, find_symbols_sett &dest)
Definition: find_symbols.cpp:201
byte_update_exprt
Expression corresponding to op() where the bytes starting at position offset (given in number of byte...
Definition: byte_operators.h:81
Forall_operands
#define Forall_operands(it, expr)
Definition: expr.h:25
symbolt::is_macro
bool is_macro
Definition: symbol.h:61
dump_ct::ns
const namespacet ns
Definition: dump_c_class.h:153
codet::op0
exprt & op0()
Definition: expr.h:103
struct_union_typet::get_component
const componentt & get_component(const irep_idt &component_name) const
Get the reference to a component with given name.
Definition: std_types.cpp:53
to_struct_type
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:302
to_struct_union_type
const struct_union_typet & to_struct_union_type(const typet &type)
Cast a typet to a struct_union_typet.
Definition: std_types.h:208
CHECK_RETURN
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:496
string_utils.h
typet
The type of an expression, extends irept.
Definition: type.h:28
code_typet::parameterst
std::vector< parametert > parameterst
Definition: std_types.h:535
find_symbols_or_nexts
void find_symbols_or_nexts(const exprt &src, find_symbols_sett &dest)
Add to the set dest the sub-expressions of src with id ID_symbol or ID_next_symbol.
Definition: find_symbols.cpp:19
expr2c
std::string expr2c(const exprt &expr, const namespacet &ns)
dump_c_configurationt
Used for configuring the behaviour of dump_c.
Definition: dump_c_class.h:22
struct_union_typet
Base type for structs and unions.
Definition: std_types.h:56
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
find_non_pointer_type_symbols
void find_non_pointer_type_symbols(const exprt &src, find_symbols_sett &dest)
Definition: find_symbols.cpp:187
byte_update_exprt::offset
const exprt & offset() const
Definition: byte_operators.h:109
c_enum_typet::memberst
std::vector< c_enum_membert > memberst
Definition: c_types.h:240
irept::add
irept & add(const irep_namet &name)
Definition: irep.cpp:122
replace_symbol.h
to_c_enum_type
const c_enum_typet & to_c_enum_type(const typet &type)
Cast a typet to a c_enum_typet.
Definition: c_types.h:277
code_declt::initial_value
optionalt< exprt > initial_value() const
Returns the initial value to which the declared variable is initialized, or empty in the case where n...
Definition: std_code.h:427
irept::find
const irept & find(const irep_namet &name) const
Definition: irep.cpp:112
replace
void replace(const union_find_replacet &replace_map, string_not_contains_constraintt &constraint)
Definition: string_constraint.cpp:67
goto_programt::update
void update()
Update all indices.
Definition: goto_program.cpp:540
configt::main
optionalt< std::string > main
Definition: config.h:261
invariant.h
dump_c_configurationt::follow_compounds
bool follow_compounds
Define whether to follow compunds recursively.
Definition: dump_c_class.h:42
code_declt
A codet representing the declaration of a local variable.
Definition: std_code.h:402
union_exprt
Union constructor from single element.
Definition: std_expr.h:1516
goto_programt::make_end_function
static instructiont make_end_function(const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:941
goto_programt::add
targett add(instructiont &&instruction)
Adds a given instruction at the end.
Definition: goto_program.h:670
to_type_with_subtypes
const type_with_subtypest & to_type_with_subtypes(const typet &type)
Definition: type.h:198
dump_ct::convert_function_declaration
void convert_function_declaration(const symbolt &symbol, const bool skip_main, std::ostream &os_decl, std::ostream &os_body, local_static_declst &local_static_decls)
Definition: dump_c.cpp:1024
dump_ct::system_symbols
system_library_symbolst system_symbols
Definition: dump_c_class.h:163
exprt
Base class for all expressions.
Definition: expr.h:54
struct_union_typet::componentst
std::vector< componentt > componentst
Definition: std_types.h:134
unary_exprt
Generic base class for unary expressions.
Definition: std_expr.h:281
binary_exprt
A base class for binary expressions.
Definition: std_expr.h:550
to_union_expr
const union_exprt & to_union_expr(const exprt &expr)
Cast an exprt to a union_exprt.
Definition: std_expr.h:1562
symbolt::base_name
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
struct_tag_typet
A struct tag type, i.e., struct_typet with an identifier.
Definition: std_types.h:443
expr2cpp.h
dump_ct::convert_compound
void convert_compound(const typet &type, const typet &unresolved, bool recursive, std::ostream &os)
Definition: dump_c.cpp:374
irep_idt
dstringt irep_idt
Definition: irep.h:37
code_declt::set_initial_value
void set_initial_value(optionalt< exprt > initial_value)
Sets the value to which this declaration initializes the declared variable.
Definition: std_code.h:438
to_string
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
Definition: string_constraint.cpp:55
find_block_position_rec
static bool find_block_position_rec(const irep_idt &identifier, codet &root, code_blockt *&dest, exprt::operandst::iterator &before)
Definition: dump_c.cpp:1119
expr2cpp
std::string expr2cpp(const exprt &expr, const namespacet &ns)
Definition: expr2cpp.cpp:491
to_side_effect_expr
side_effect_exprt & to_side_effect_expr(exprt &expr)
Definition: std_code.h:1954
configt::ansi_c
struct configt::ansi_ct ansi_c
dump_c_configurationt::disable_include_function_bodies
dump_c_configurationt disable_include_function_bodies()
Definition: dump_c_class.h:63
expr2c_configuration
static expr2c_configurationt expr2c_configuration()
Definition: dump_c.cpp:1532
goto_functionst::function_map
function_mapt function_map
Definition: goto_functions.h:27
dump_ct::typedef_map
typedef_mapt typedef_map
Definition: dump_c_class.h:182
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:80
goto_programt::make_decl
static instructiont make_decl(const symbol_exprt &symbol, const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:904
dump_ct::insert_local_type_decls
void insert_local_type_decls(code_blockt &b, const std::list< irep_idt > &type_decls)
Definition: dump_c.cpp:1232
dump_ct::expr_to_string
std::string expr_to_string(const exprt &expr)
Definition: dump_c.cpp:1563
dump_ct::converted_compound
convertedt converted_compound
Definition: dump_c_class.h:159
goto_programt::make_assignment
static instructiont make_assignment(const code_assignt &_code, const source_locationt &l=source_locationt::nil())
Create an assignment instruction.
Definition: goto_program.h:1008
zero_initializer
optionalt< exprt > zero_initializer(const typet &type, const source_locationt &source_location, const namespacet &ns)
Create the equivalent of zero for type type.
Definition: expr_initializer.cpp:318
symbol_tablet::begin
virtual iteratort begin() override
Definition: symbol_table.h:114
union_tag_typet
A union tag type, i.e., union_typet with an identifier.
Definition: c_types.h:164
dump_ct::harness
const bool harness
Definition: dump_c_class.h:156
unsignedbv_typet
Fixed-width bit-vector with unsigned binary interpretation.
Definition: bitvector_types.h:159
dump_ct::system_headers
std::set< std::string > system_headers
Definition: dump_c_class.h:161
to_code
const codet & to_code(const exprt &expr)
Definition: std_code.h:155
dump_ct::make_decl
std::string make_decl(const irep_idt &identifier, const typet &type)
Definition: dump_c_class.h:194
struct_exprt
Struct constructor from list of elements.
Definition: std_expr.h:1582
dump_ct::dump_typedefs
void dump_typedefs(std::ostream &os) const
Print all typedefs that are not covered via typedef struct xyz { ...
Definition: dump_c.cpp:814
code_blockt::statements
code_operandst & statements()
Definition: std_code.h:178
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:92
type2c
std::string type2c(const typet &type, const namespacet &ns)
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:82
namespacet::lookup
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:141
dump_ct::declared_enum_constants
declared_enum_constants_mapt declared_enum_constants
Definition: dump_c_class.h:166
code_function_callt
codet representation of a function call statement.
Definition: std_code.h:1215
irept::get_bool
bool get_bool(const irep_namet &name) const
Definition: irep.cpp:64
irept::is_not_nil
bool is_not_nil() const
Definition: irep.h:391
dump_c_configurationt::include_typedefs
bool include_typedefs
Include the typedefs in the dump.
Definition: dump_c_class.h:33
operator<<
std::ostream & operator<<(std::ostream &out, dump_ct &src)
Definition: dump_c.cpp:42
strip_string
std::string strip_string(const std::string &s)
Remove all whitespace characters from either end of a string.
Definition: string_utils.cpp:21
dump_ct::converted_global
convertedt converted_global
Definition: dump_c_class.h:159
byte_operators.h
Expression classes for byte-level operators.
to_code_type
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:738
expr_initializer.h
Expression Initialization.
find_symbols.h
dump_ct::cleanup_type
void cleanup_type(typet &type)
Definition: dump_c.cpp:1506
to_tag_type
const tag_typet & to_tag_type(const typet &type)
Cast a typet to a tag_typet.
Definition: std_types.h:428
DATA_INVARIANT
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition: invariant.h:511
exprt::has_operands
bool has_operands() const
Return true if there is at least one operand.
Definition: expr.h:93
symbol_tablet::end
virtual iteratort end() override
Definition: symbol_table.h:118
symbol_table_baset::get_writeable_ref
symbolt & get_writeable_ref(const irep_idt &name)
Find a symbol in the symbol table for read-write access.
Definition: symbol_table_base.h:121
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:49
system_library_symbolst::is_type_internal
bool is_type_internal(const typet &type, std::set< std::string > &out_system_headers) const
Helper function to call is_symbol_internal_symbol on a nameless fake symbol with the given type,...
Definition: system_library_symbols.cpp:263
dump_ct::convert_compound_enum
void convert_compound_enum(const typet &type, std::ostream &os)
Definition: dump_c.cpp:606
dump_ct::cleanup_expr
void cleanup_expr(exprt &expr)
Definition: dump_c.cpp:1273
to_byte_update_expr
const byte_update_exprt & to_byte_update_expr(const exprt &expr)
Definition: byte_operators.h:120
type2cpp
std::string type2cpp(const typet &type, const namespacet &ns)
Definition: expr2cpp.cpp:498
dump_ct::local_static_declst
std::unordered_map< irep_idt, code_declt > local_static_declst
Definition: dump_c_class.h:233
expr2c.h
dump_c
void dump_c(const goto_functionst &src, const bool use_system_headers, const bool use_all_headers, const bool include_harness, const namespacet &ns, std::ostream &out)
Definition: dump_c.cpp:1577
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:464
typet::source_location
const source_locationt & source_location() const
Definition: type.h:71
symbol_exprt::get_identifier
const irep_idt & get_identifier() const
Definition: std_expr.h:109
nil_exprt
The NIL expression.
Definition: std_expr.h:2734
struct_union_typet::componentt::get_is_padding
bool get_is_padding() const
Definition: std_types.h:123
signedbv_typet
Fixed-width bit-vector with two's complement interpretation.
Definition: bitvector_types.h:208
dump_ct::convertedt
std::unordered_set< irep_idt > convertedt
Definition: dump_c_class.h:158
symbolt::symbol_expr
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition: symbol.cpp:122
module_local_declaration
static bool module_local_declaration(const symbolt &symbol, const std::string module)
Definition: dump_c.cpp:1604
INITIALIZE_FUNCTION
#define INITIALIZE_FUNCTION
Definition: static_lifetime_init.h:23
dump_c_type_header
void dump_c_type_header(const goto_functionst &src, const bool use_system_headers, const bool use_all_headers, const bool include_harness, const namespacet &ns, const std::string module, std::ostream &out)
Definition: dump_c.cpp:1612
dump_ct::cleanup_decl
void cleanup_decl(code_declt &decl, std::list< irep_idt > &local_static, std::list< irep_idt > &local_type_decls)
Definition: dump_c.cpp:649
configt::ansi_ct::long_long_int_width
std::size_t long_long_int_width
Definition: config.h:116
goto_program2code.h
Dump Goto-Program as C/C++ Source.
irept::swap
void swap(irept &irep)
Definition: irep.h:453
to_symbol_expr
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:189
code_typet
Base type of functions.
Definition: std_types.h:533
irept::is_nil
bool is_nil() const
Definition: irep.h:387
irept::id
const irep_idt & id() const
Definition: irep.h:407
dump_ct::insert_local_static_decls
void insert_local_static_decls(code_blockt &b, const std::list< irep_idt > &local_static, local_static_declst &local_static_decls, std::list< irep_idt > &type_decls)
Definition: dump_c.cpp:1198
irept::remove
void remove(const irep_namet &name)
Definition: irep.cpp:102
to_code_function_call
const code_function_callt & to_code_function_call(const codet &code)
Definition: std_code.h:1326
exprt::operandst
std::vector< exprt > operandst
Definition: expr.h:56
code_function_callt::argumentst
exprt::operandst argumentst
Definition: std_code.h:1224
dstringt::empty
bool empty() const
Definition: dstring.h:88
get_base_name.h
dump_c_configurationt::include_compounds
bool include_compounds
Include struct definitions in the dump.
Definition: dump_c_class.h:39
dump_ct::operator()
void operator()(std::ostream &out)
Definition: dump_c.cpp:48
code_blockt::add
void add(const codet &code)
Definition: std_code.h:208
union_typet
The union type.
Definition: c_types.h:112
tag_typet
A tag-based type, i.e., typet with an identifier.
Definition: std_types.h:390
unary_exprt::op
const exprt & op() const
Definition: std_expr.h:293
dump_c_configurationt::include_headers
bool include_headers
Include headers type declarations are borrowed from.
Definition: dump_c_class.h:45
code_typet::parameters
const parameterst & parameters() const
Definition: std_types.h:649
dump_c_configurationt::include_function_decls
bool include_function_decls
Include the function declarations in the dump.
Definition: dump_c_class.h:24
code_function_callt::arguments
argumentst & arguments()
Definition: std_code.h:1260
optionalt
nonstd::optional< T > optionalt
Definition: optional.h:35
code_declt::symbol
symbol_exprt & symbol()
Definition: std_code.h:408
side_effect_exprt::get_statement
const irep_idt & get_statement() const
Definition: std_code.h:1920
config
configt config
Definition: config.cpp:24
to_signedbv_type
const signedbv_typet & to_signedbv_type(const typet &type)
Cast a typet to a signedbv_typet.
Definition: bitvector_types.h:241
source_locationt
Definition: source_location.h:20
bitvector_typet::get_width
std::size_t get_width() const
Definition: std_types.h:837
union_exprt::get_component_name
irep_idt get_component_name() const
Definition: std_expr.h:1524
exprt::is_zero
bool is_zero() const
Return whether the expression is a constant representing 0.
Definition: expr.cpp:91
symbol_table_baset::add
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
Definition: symbol_table_base.cpp:18
struct_union_typet::componentt
Definition: std_types.h:63
to_c_bit_field_type
const c_bit_field_typet & to_c_bit_field_type(const typet &type)
Cast a typet to a c_bit_field_typet.
Definition: c_types.h:47
irept::get_string
const std::string & get_string(const irep_namet &name) const
Definition: irep.h:420
goto_functionst
A collection of goto functions.
Definition: goto_functions.h:23
dump_ct::mode
const irep_idt mode
Definition: dump_c_class.h:155
symbolt::value
exprt value
Initial value of symbol.
Definition: symbol.h:34
dump_c_configurationt::include_global_decls
bool include_global_decls
Include the global declarations in the dump.
Definition: dump_c_class.h:30
POSTCONDITION
#define POSTCONDITION(CONDITION)
Definition: invariant.h:480
namespacet::get_symbol_table
const symbol_table_baset & get_symbol_table() const
Return first symbol table registered with the namespace.
Definition: namespace.h:124
struct_typet
Structure type, corresponds to C style structs.
Definition: std_types.h:225
byte_update_exprt::value
const exprt & value() const
Definition: byte_operators.h:110
c_enum_tag_typet
C enum tag type, i.e., c_enum_typet with an identifier.
Definition: c_types.h:292
code_skipt
A codet representing a skip statement.
Definition: std_code.h:270
symbolt::is_extern
bool is_extern
Definition: symbol.h:66
find_symbols_sett
std::unordered_set< irep_idt > find_symbols_sett
Definition: find_symbols.h:21
dump_c_configurationt::disable_include_function_decls
dump_c_configurationt disable_include_function_decls()
Definition: dump_c_class.h:57
dump_c_configurationt::include_global_vars
bool include_global_vars
Include global variable definitions in the dump.
Definition: dump_c_class.h:36
namespace_baset::follow
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:52
symbol_table_baset::iteratort
Definition: symbol_table_base.h:155
irept::get
const irep_idt & get(const irep_namet &name) const
Definition: irep.cpp:51
symbolt::location
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
dump_c_configurationt::include_function_bodies
bool include_function_bodies
Include the functions in the dump.
Definition: dump_c_class.h:27
dump_ct::typedef_infot::typedef_name
irep_idt typedef_name
Definition: dump_c_class.h:170
dump_c_class.h
Dump Goto-Program as C/C++ Source.
symbolt
Symbol table entry.
Definition: symbol.h:28
irept::set
void set(const irep_namet &name, const irep_idt &value)
Definition: irep.h:431
dump_ct::copied_symbol_table
symbol_tablet copied_symbol_table
Definition: dump_c_class.h:152
to_typecast_expr
const typecast_exprt & to_typecast_expr(const exprt &expr)
Cast an exprt to a typecast_exprt.
Definition: std_expr.h:1814
dump_ct::typedef_infot
Definition: dump_c_class.h:169
symbol_table_baset::symbols
const symbolst & symbols
Read-only field, used to look up symbols given their names.
Definition: symbol_table_base.h:30
dump_ct::typedef_types
typedef_typest typedef_types
Definition: dump_c_class.h:184
symbolt::is_type
bool is_type
Definition: symbol.h:61
dump_ct::cleanup_harness
void cleanup_harness(code_blockt &b)
Replace CPROVER internal symbols in b by printable values and generate necessary declarations.
Definition: dump_c.cpp:969
goto_program2codet
Definition: goto_program2code.h:21
CPROVER_PREFIX
#define CPROVER_PREFIX
Definition: cprover_prefix.h:14
irept::get_sub
subt & get_sub()
Definition: irep.h:467
to_array_type
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition: std_types.h:807
exprt::add_to_operands
void add_to_operands(const exprt &expr)
Add the given argument to the end of exprt's operands.
Definition: expr.h:144
symbolt::is_static_lifetime
bool is_static_lifetime
Definition: symbol.h:65
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:74
config.h
dump_ct::convert_compound_declaration
void convert_compound_declaration(const symbolt &symbol, std::ostream &os_body)
declare compound types
Definition: dump_c.cpp:342
source_locationt::get_file
const irep_idt & get_file() const
Definition: source_location.h:36
system_library_symbolst::is_symbol_internal_symbol
bool is_symbol_internal_symbol(const symbolt &symbol, std::set< std::string > &out_system_headers) const
To find out if a symbol is an internal symbol.
Definition: system_library_symbols.cpp:277
code_typet::return_type
const typet & return_type() const
Definition: std_types.h:639
to_code_block
const code_blockt & to_code_block(const codet &code)
Definition: std_code.h:256
irept
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition: irep.h:383
dump_ct::dump_c_config
const dump_c_configurationt dump_c_config
Definition: dump_c_class.h:154
dump_c_configurationt::type_header_configuration
static dump_c_configurationt type_header_configuration
The config used for dump-c-type-header.
Definition: dump_c_class.h:55
dump_ct::convert_global_variable
void convert_global_variable(const symbolt &symbol, std::ostream &os, local_static_declst &local_static_decls)
Definition: dump_c.cpp:899
exprt::operands
operandst & operands()
Definition: expr.h:96
goto_functionst::entry_point
static irep_idt entry_point()
Get the identifier of the entry point to a goto model.
Definition: goto_functions.h:90
dump_ct::collect_typedefs
void collect_typedefs(const typet &type, bool early)
Find any typedef names contained in the input type and store their declaration strings in typedef_map...
Definition: dump_c.cpp:693
static_lifetime_init.h
exprt::add_source_location
source_locationt & add_source_location()
Definition: expr.h:243
expr2c_configurationt
Used for configuring the behaviour of expr2c and type2c.
Definition: expr2c.h:21
typecast_exprt
Semantic type conversion.
Definition: std_expr.h:1780
size_type
unsignedbv_typet size_type()
Definition: c_types.cpp:58
codet::get_statement
const irep_idt & get_statement() const
Definition: std_code.h:71
symbolt::module
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:43
dump_c.h
Dump C from Goto Program.
dump_ct
Definition: dump_c_class.h:107
constant_exprt::get_value
const irep_idt & get_value() const
Definition: std_expr.h:2675
exprt::source_location
const source_locationt & source_location() const
Definition: expr.h:238
byte_update_exprt::op
const exprt & op() const
Definition: byte_operators.h:108
c_types.h
symbol_exprt::set_identifier
void set_identifier(const irep_idt &identifier)
Definition: std_expr.h:104
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40
dump_cpp
void dump_cpp(const goto_functionst &src, const bool use_system_headers, const bool use_all_headers, const bool include_harness, const namespacet &ns, std::ostream &out)
Definition: dump_c.cpp:1590
dump_ct::type_to_string
std::string type_to_string(const typet &type)
Definition: dump_c.cpp:1549
code_function_callt::function
exprt & function()
Definition: std_code.h:1250
dump_ct::converted_enum
convertedt converted_enum
Definition: dump_c_class.h:159
dump_ct::gather_global_typedefs
void gather_global_typedefs()
Find all global typdefs in the symbol table and store them in typedef_types.
Definition: dump_c.cpp:783
replace_symbolt
Replace expression or type symbols by an expression or type, respectively.
Definition: replace_symbol.h:25
codet
Data structure for representing an arbitrary statement in a program.
Definition: std_code.h:35
to_constant_expr
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition: std_expr.h:2700