cprover
dump_c_class.h
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 #ifndef CPROVER_GOTO_INSTRUMENT_DUMP_C_CLASS_H
13 #define CPROVER_GOTO_INSTRUMENT_DUMP_C_CLASS_H
14 
15 #include <set>
16 #include <string>
17 #include <memory> // unique_ptr
18 
19 #include <langapi/language.h>
20 #include <langapi/mode.h>
21 
23 
24 class dump_ct
25 {
26 public:
28  const goto_functionst &_goto_functions,
29  const bool use_system_headers,
30  const bool use_all_headers,
31  const bool include_harness,
32  const namespacet &_ns,
33  language_factoryt factory):
34  goto_functions(_goto_functions),
35  copied_symbol_table(_ns.get_symbol_table()),
37  language(factory()),
38  harness(include_harness),
39  system_symbols(use_system_headers)
40  {
41  system_symbols.set_use_all_headers(use_all_headers);
42  }
43 
44  virtual ~dump_ct()=default;
45 
46  void operator()(std::ostream &out);
47 
48 protected:
51  const namespacet ns;
52  std::unique_ptr<languaget> language;
53  const bool harness;
54 
55  typedef std::unordered_set<irep_idt> convertedt;
57 
58  std::set<std::string> system_headers;
59 
61 
62  typedef std::unordered_map<irep_idt, irep_idt> declared_enum_constants_mapt;
64 
66  {
68  std::string type_decl_str;
69  bool early;
70  std::unordered_set<irep_idt> dependencies;
71 
72  explicit typedef_infot(const irep_idt &name):
73  typedef_name(name),
74  type_decl_str(""),
75  early(false)
76  {
77  }
78  };
79  typedef std::map<irep_idt, typedef_infot> typedef_mapt;
81  typedef std::unordered_map<typet, irep_idt, irep_hash> typedef_typest;
83 
84  std::string type_to_string(const typet &type);
85  std::string expr_to_string(const exprt &expr);
86 
87  static std::string indent(const unsigned n)
88  {
89  return std::string(2*n, ' ');
90  }
91 
92  std::string make_decl(
93  const irep_idt &identifier,
94  const typet &type)
95  {
96  symbol_exprt sym(identifier, type);
97  code_declt d(sym);
98 
99  std::string d_str=expr_to_string(d);
100  assert(!d_str.empty());
101  assert(*d_str.rbegin()==';');
102 
103  return d_str.substr(0, d_str.size()-1);
104  }
105 
106  void collect_typedefs(const typet &type, bool early);
108  const typet &type,
109  bool early,
110  std::unordered_set<irep_idt> &dependencies);
111  void gather_global_typedefs();
112  void dump_typedefs(std::ostream &os) const;
113 
115  const symbolt &symbol,
116  std::ostream &os_body);
117  void convert_compound(
118  const typet &type,
119  const typet &unresolved,
120  bool recursive,
121  std::ostream &os);
122  void convert_compound(
123  const struct_union_typet &type,
124  const typet &unresolved,
125  bool recursive,
126  std::ostream &os);
128  const typet &type,
129  std::ostream &os);
130 
131  typedef std::unordered_map<irep_idt, code_declt> local_static_declst;
132 
134  const symbolt &symbol,
135  std::ostream &os,
136  local_static_declst &local_static_decls);
137 
139  const symbolt &symbol,
140  const bool skip_main,
141  std::ostream &os_decl,
142  std::ostream &os_body,
143  local_static_declst &local_static_decls);
144 
146  code_blockt &b,
147  const std::list<irep_idt> &local_static,
148  local_static_declst &local_static_decls,
149  std::list<irep_idt> &type_decls);
150 
152  code_blockt &b,
153  const std::list<irep_idt> &type_decls);
154 
155  void cleanup_expr(exprt &expr);
156  void cleanup_type(typet &type);
157  void cleanup_decl(
158  code_declt &decl,
159  std::list<irep_idt> &local_static,
160  std::list<irep_idt> &local_type_decls);
161  void cleanup_harness(code_blockt &b);
162 };
163 
164 #endif // CPROVER_GOTO_INSTRUMENT_DUMP_C_CLASS_H
The type of an expression.
Definition: type.h:22
const bool harness
Definition: dump_c_class.h:53
std::unique_ptr< languaget >(* language_factoryt)()
Definition: mode.h:29
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:637
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:1138
std::unordered_set< irep_idt > dependencies
Definition: dump_c_class.h:70
declared_enum_constants_mapt declared_enum_constants
Definition: dump_c_class.h:63
const namespacet ns
Definition: dump_c_class.h:51
std::string type_to_string(const typet &type)
Definition: dump_c.cpp:1382
std::unordered_map< irep_idt, code_declt > local_static_declst
Definition: dump_c_class.h:131
std::string expr_to_string(const exprt &expr)
Definition: dump_c.cpp:1391
std::map< irep_idt, typedef_infot > typedef_mapt
Definition: dump_c_class.h:79
Symbol table entry.This is a symbol in the symbol table, stored in an object of type symbol_tablet...
Definition: symbol.h:30
const goto_functionst & goto_functions
Definition: dump_c_class.h:49
convertedt converted_enum
Definition: dump_c_class.h:56
std::unordered_set< irep_idt > convertedt
Definition: dump_c_class.h:55
std::string make_decl(const irep_idt &identifier, const typet &type)
Definition: dump_c_class.h:92
dump_ct(const goto_functionst &_goto_functions, const bool use_system_headers, const bool use_all_headers, const bool include_harness, const namespacet &_ns, language_factoryt factory)
Definition: dump_c_class.h:27
typedef_typest typedef_types
Definition: dump_c_class.h:82
void insert_local_type_decls(code_blockt &b, const std::list< irep_idt > &type_decls)
Definition: dump_c.cpp:1172
A declaration of a local variable.
Definition: std_code.h:254
std::string type_decl_str
Definition: dump_c_class.h:68
void convert_compound(const typet &type, const typet &unresolved, bool recursive, std::ostream &os)
Definition: dump_c.cpp:311
void cleanup_decl(code_declt &decl, std::list< irep_idt > &local_static, std::list< irep_idt > &local_type_decls)
Definition: dump_c.cpp:587
The symbol table.
Definition: symbol_table.h:19
virtual ~dump_ct()=default
TO_BE_DOCUMENTED.
Definition: namespace.h:74
Abstract interface to support a programming language.
void gather_global_typedefs()
Find all global typdefs in the symbol table and store them in typedef_types.
Definition: dump_c.cpp:726
system_library_symbolst system_symbols
Definition: dump_c_class.h:60
dstringt has one field, an unsigned integer no which is an index into a static table of strings...
Definition: dstring.h:33
std::set< std::string > system_headers
Definition: dump_c_class.h:58
std::unique_ptr< languaget > language
Definition: dump_c_class.h:52
convertedt converted_global
Definition: dump_c_class.h:56
std::unordered_map< irep_idt, irep_idt > declared_enum_constants_mapt
Definition: dump_c_class.h:62
typedef_mapt typedef_map
Definition: dump_c_class.h:80
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:650
void convert_compound_declaration(const symbolt &symbol, std::ostream &os_body)
declare compound types
Definition: dump_c.cpp:297
Base type of C structs and unions, and C++ classes.
Definition: std_types.h:162
void cleanup_expr(exprt &expr)
Definition: dump_c.cpp:1209
std::unordered_map< typet, irep_idt, irep_hash > typedef_typest
Definition: dump_c_class.h:81
Base class for all expressions.
Definition: expr.h:42
void convert_compound_enum(const typet &type, std::ostream &os)
Definition: dump_c.cpp:544
void operator()(std::ostream &out)
Definition: dump_c.cpp:34
Sequential composition.
Definition: std_code.h:89
static std::string indent(const unsigned n)
Definition: dump_c_class.h:87
Expression to hold a symbol (variable)
Definition: std_expr.h:90
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:964
void dump_typedefs(std::ostream &os) const
Print all typedefs that are not covered via typedef struct xyz { ...
Definition: dump_c.cpp:757
typedef_infot(const irep_idt &name)
Definition: dump_c_class.h:72
convertedt converted_compound
Definition: dump_c_class.h:56
void cleanup_type(typet &type)
Definition: dump_c.cpp:1356
symbol_tablet copied_symbol_table
Definition: dump_c_class.h:50
void convert_global_variable(const symbolt &symbol, std::ostream &os, local_static_declst &local_static_decls)
Definition: dump_c.cpp:842
void cleanup_harness(code_blockt &b)
Replace CPROVER internal symbols in b by printable values and generate necessary declarations.
Definition: dump_c.cpp:910