forked from diffblue/cbmc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcpp_typecheck.cpp
343 lines (278 loc) · 8.76 KB
/
cpp_typecheck.cpp
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
/*******************************************************************\
Module: C++ Language Type Checking
Author: Daniel Kroening, [email protected]
\*******************************************************************/
/// \file
/// C++ Language Type Checking
#include "cpp_typecheck.h"
#include <util/pointer_expr.h>
#include <util/source_location.h>
#include <util/symbol_table.h>
#include <ansi-c/builtin_factory.h>
#include "cpp_declarator.h"
#include "cpp_util.h"
#include "expr2cpp.h"
void cpp_typecheckt::convert(cpp_itemt &item)
{
if(item.is_declaration())
convert(to_cpp_declaration(item));
else if(item.is_linkage_spec())
convert(item.get_linkage_spec());
else if(item.is_namespace_spec())
convert(item.get_namespace_spec());
else if(item.is_using())
convert(item.get_using());
else if(item.is_static_assert())
convert(item.get_static_assert());
else
{
error().source_location=item.source_location();
error() << "unknown parse-tree element: " << item.id() << eom;
throw 0;
}
}
/// typechecking main method
void cpp_typecheckt::typecheck()
{
// default linkage is "automatic"
current_linkage_spec=ID_auto;
for(auto &item : cpp_parse_tree.items)
convert(item);
static_and_dynamic_initialization();
typecheck_method_bodies();
do_not_typechecked();
clean_up();
}
const struct_typet &cpp_typecheckt::this_struct_type()
{
const exprt &this_expr=
cpp_scopes.current_scope().this_expr;
CHECK_RETURN(this_expr.is_not_nil());
CHECK_RETURN(this_expr.type().id() == ID_pointer);
const typet &t = follow(to_pointer_type(this_expr.type()).base_type());
CHECK_RETURN(t.id() == ID_struct);
return to_struct_type(t);
}
std::string cpp_typecheckt::to_string(const exprt &expr)
{
return expr2cpp(expr, *this);
}
std::string cpp_typecheckt::to_string(const typet &type)
{
return type2cpp(type, *this);
}
bool cpp_typecheck(
cpp_parse_treet &cpp_parse_tree,
symbol_table_baset &symbol_table,
const std::string &module,
message_handlert &message_handler)
{
cpp_typecheckt cpp_typecheck(
cpp_parse_tree, symbol_table, module, message_handler);
return cpp_typecheck.typecheck_main();
}
bool cpp_typecheck(
exprt &expr,
message_handlert &message_handler,
const namespacet &ns)
{
const unsigned errors_before=
message_handler.get_message_count(messaget::M_ERROR);
symbol_tablet symbol_table;
cpp_parse_treet cpp_parse_tree;
cpp_typecheckt cpp_typecheck(cpp_parse_tree, symbol_table,
ns.get_symbol_table(), "", message_handler);
try
{
cpp_typecheck.typecheck_expr(expr);
}
catch(int)
{
cpp_typecheck.error();
}
catch(const char *e)
{
cpp_typecheck.error() << e << messaget::eom;
}
catch(const std::string &e)
{
cpp_typecheck.error() << e << messaget::eom;
}
catch(const invalid_source_file_exceptiont &e)
{
cpp_typecheck.error().source_location = e.get_source_location();
cpp_typecheck.error() << e.get_reason() << messaget::eom;
}
return message_handler.get_message_count(messaget::M_ERROR)!=errors_before;
}
/// Initialization of static objects:
///
/// "Objects with static storage duration (3.7.1) shall be zero-initialized
/// (8.5) before any other initialization takes place. Zero-initialization
/// and initialization with a constant expression are collectively called
/// static initialization; all other initialization is dynamic
/// initialization. Objects of POD types (3.9) with static storage duration
/// initialized with constant expressions (5.19) shall be initialized before
/// any dynamic initialization takes place. Objects with static storage
/// duration defined in namespace scope in the same translation unit and
/// dynamically initialized shall be initialized in the order in which their
/// definition appears in the translation unit. [Note: 8.5.1 describes the
/// order in which aggregate members are initialized. The initialization
/// of local static objects is described in 6.7. ]"
void cpp_typecheckt::static_and_dynamic_initialization()
{
code_blockt init_block; // Dynamic Initialization Block
disable_access_control = true;
for(const irep_idt &d_it : dynamic_initializations)
{
symbolt &symbol = symbol_table.get_writeable_ref(d_it);
if(symbol.is_extern)
continue;
// PODs are always statically initialized
if(cpp_is_pod(symbol.type))
continue;
DATA_INVARIANT(symbol.is_static_lifetime, "should be static");
DATA_INVARIANT(!symbol.is_type, "should not be a type");
DATA_INVARIANT(symbol.type.id() != ID_code, "should not be code");
exprt symbol_expr=cpp_symbol_expr(symbol);
// initializer given?
if(symbol.value.is_not_nil())
{
// This will be a constructor call,
// which we execute.
init_block.add(to_code(symbol.value));
// Make it nil to get zero initialization by
// __CPROVER_initialize
symbol.value.make_nil();
}
else
{
// use default constructor
exprt::operandst ops;
auto call = cpp_constructor(symbol.location, symbol_expr, ops);
if(call.has_value())
init_block.add(call.value());
}
}
dynamic_initializations.clear();
// Create the dynamic initialization procedure
symbolt init_symbol{
"#cpp_dynamic_initialization#" + id2string(module),
code_typet({}, typet(ID_constructor)),
ID_cpp};
init_symbol.base_name="#cpp_dynamic_initialization#"+id2string(module);
init_symbol.value.swap(init_block);
init_symbol.module=module;
symbol_table.insert(std::move(init_symbol));
disable_access_control=false;
}
void cpp_typecheckt::do_not_typechecked()
{
bool cont;
do
{
cont = false;
for(auto it = symbol_table.begin(); it != symbol_table.end(); ++it)
{
const symbolt &symbol = it->second;
if(
symbol.value.id() == ID_cpp_not_typechecked &&
symbol.value.get_bool(ID_is_used))
{
DATA_INVARIANT(symbol.type.id() == ID_code, "must be code");
exprt value = symbol.value;
if(symbol.base_name=="operator=")
{
cpp_declaratort declarator;
declarator.add_source_location() = symbol.location;
default_assignop_value(
lookup(symbol.type.get(ID_C_member_name)), declarator);
value.swap(declarator.value());
cont=true;
}
else if(symbol.value.operands().size()==1)
{
value = to_unary_expr(symbol.value).op();
cont=true;
}
else
UNREACHABLE; // Don't know what to do!
symbolt &writable_symbol = it.get_writeable_symbol();
writable_symbol.value.swap(value);
convert_function(writable_symbol);
}
}
}
while(cont);
for(auto it = symbol_table.begin(); it != symbol_table.end(); ++it)
{
if(it->second.value.id() == ID_cpp_not_typechecked)
it.get_writeable_symbol().value.make_nil();
}
}
void cpp_typecheckt::clean_up()
{
auto it = symbol_table.begin();
while(it != symbol_table.end())
{
auto cur_it = it;
it++;
const symbolt &symbol=cur_it->second;
// erase templates and all member functions that have not been converted
if(
symbol.type.get_bool(ID_is_template) ||
deferred_typechecking.find(symbol.name) != deferred_typechecking.end())
{
symbol_table.erase(cur_it);
continue;
}
else if(symbol.type.id()==ID_struct ||
symbol.type.id()==ID_union)
{
// remove methods from 'components'
struct_union_typet &struct_union_type =
to_struct_union_type(cur_it.get_writeable_symbol().type);
const struct_union_typet::componentst &components=
struct_union_type.components();
struct_union_typet::componentst data_members;
data_members.reserve(components.size());
struct_union_typet::componentst &function_members=
(struct_union_typet::componentst &)
(struct_union_type.add(ID_methods).get_sub());
function_members.reserve(components.size());
for(const auto &compo_it : components)
{
if(compo_it.get_bool(ID_is_static) ||
compo_it.get_bool(ID_is_type))
{
// skip it
}
else if(compo_it.type().id()==ID_code)
{
function_members.push_back(compo_it);
}
else
{
data_members.push_back(compo_it);
}
}
struct_union_type.components().swap(data_members);
}
}
}
bool cpp_typecheckt::builtin_factory(const irep_idt &identifier)
{
return ::builtin_factory(
identifier, false, symbol_table, get_message_handler());
}
bool cpp_typecheckt::contains_cpp_name(const exprt &expr)
{
if(expr.id() == ID_cpp_name || expr.id() == ID_cpp_declaration)
return true;
for(const exprt &op : expr.operands())
{
if(contains_cpp_name(op))
return true;
}
return false;
}