-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathcpp_typecheck_initializer.cpp
More file actions
340 lines (276 loc) · 8.98 KB
/
cpp_typecheck_initializer.cpp
File metadata and controls
340 lines (276 loc) · 8.98 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
/*******************************************************************\
Module: C++ Language Type Checking
Author: Daniel Kroening, [email protected]
\*******************************************************************/
/// \file
/// C++ Language Type Checking
#include "cpp_typecheck.h"
#include <util/arith_tools.h>
#include <util/c_types.h>
#include <util/expr_initializer.h>
#include <util/pointer_expr.h>
#include <util/pointer_offset_size.h>
#include "cpp_convert_type.h"
#include "cpp_typecheck_fargs.h"
/// Initialize an object with a value
void cpp_typecheckt::convert_initializer(symbolt &symbol)
{
// this is needed for template arguments that are types
if(symbol.is_type)
{
if(symbol.value.is_nil())
return;
if(symbol.value.id()!=ID_type)
{
error().source_location=symbol.location;
error() << "expected type as initializer for '" << symbol.base_name << "'"
<< eom;
throw 0;
}
typecheck_type(symbol.value.type());
return;
}
// do we have an initializer?
if(symbol.value.is_nil())
{
// do we need one?
if(is_reference(symbol.type))
{
error().source_location=symbol.location;
error() << "'" << symbol.base_name
<< "' is declared as reference but is not initialized" << eom;
throw 0;
}
// done
return;
}
// we do have an initializer
if(is_reference(symbol.type))
{
typecheck_expr(symbol.value);
if(has_auto(symbol.type))
{
cpp_convert_auto(symbol.type, symbol.value.type(), get_message_handler());
typecheck_type(symbol.type);
implicit_typecast(symbol.value, symbol.type);
}
reference_initializer(symbol.value, to_reference_type(symbol.type));
}
else if(cpp_is_pod(symbol.type))
{
if(
symbol.type.id() == ID_pointer &&
to_pointer_type(symbol.type).base_type().id() == ID_code &&
symbol.value.id() == ID_address_of &&
to_address_of_expr(symbol.value).object().id() == ID_cpp_name)
{
// initialization of a function pointer with
// the address of a function: use pointer type information
// for the sake of overload resolution
cpp_typecheck_fargst fargs;
fargs.in_use = true;
const code_typet &code_type =
to_code_type(to_pointer_type(symbol.type).base_type());
for(const auto ¶meter : code_type.parameters())
{
exprt new_object(ID_new_object, parameter.type());
new_object.set(ID_C_lvalue, true);
if(parameter.get_this())
{
fargs.has_object = true;
new_object.type() = to_pointer_type(parameter.type()).base_type();
}
fargs.operands.push_back(new_object);
}
exprt resolved_expr = resolve(
to_cpp_name(
static_cast<irept &>(to_address_of_expr(symbol.value).object())),
cpp_typecheck_resolvet::wantt::BOTH,
fargs);
DATA_INVARIANT(
to_pointer_type(symbol.type).base_type() == resolved_expr.type(),
"symbol type must match");
if(resolved_expr.id()==ID_symbol)
{
symbol.value=
address_of_exprt(resolved_expr);
}
else if(resolved_expr.id()==ID_member)
{
symbol.value =
address_of_exprt(
lookup(resolved_expr.get(ID_component_name)).symbol_expr());
symbol.value.type().add(ID_to_member) =
to_member_expr(resolved_expr).compound().type();
}
else
UNREACHABLE;
if(symbol.type != symbol.value.type())
{
error().source_location=symbol.location;
error() << "conversion from '" << to_string(symbol.value.type())
<< "' to '" << to_string(symbol.type) << "' " << eom;
throw 0;
}
return;
}
typecheck_expr(symbol.value);
if(symbol.value.type().find(ID_to_member).is_not_nil())
symbol.type.add(ID_to_member) = symbol.value.type().find(ID_to_member);
if(symbol.value.id()==ID_initializer_list ||
symbol.value.id()==ID_string_constant)
{
do_initializer(symbol.value, symbol.type, true);
if(symbol.type.find(ID_size).is_nil())
symbol.type=symbol.value.type();
}
else if(has_auto(symbol.type))
{
cpp_convert_auto(symbol.type, symbol.value.type(), get_message_handler());
typecheck_type(symbol.type);
implicit_typecast(symbol.value, symbol.type);
}
else
implicit_typecast(symbol.value, symbol.type);
#if 0
simplify_exprt simplify(*this);
exprt tmp_value = symbol.value;
if(!simplify.simplify(tmp_value))
symbol.value.swap(tmp_value);
#endif
}
else
{
// we need a constructor
symbol_exprt expr_symbol(symbol.name, symbol.type);
already_typechecked_exprt::make_already_typechecked(expr_symbol);
exprt::operandst ops;
ops.push_back(symbol.value);
auto constructor =
cpp_constructor(symbol.value.source_location(), expr_symbol, ops);
if(constructor.has_value())
symbol.value = constructor.value();
else
symbol.value = nil_exprt();
}
}
void cpp_typecheckt::zero_initializer(
const exprt &object,
const typet &type,
const source_locationt &source_location,
exprt::operandst &ops)
{
if(type.id() == ID_struct_tag)
{
const auto &struct_type = follow_tag(to_struct_tag_type(type));
if(struct_type.is_incomplete())
{
error().source_location = source_location;
error() << "cannot zero-initialize incomplete struct" << eom;
throw 0;
}
for(const auto &component : struct_type.components())
{
if(component.type().id()==ID_code)
continue;
if(component.get_bool(ID_is_type))
continue;
if(component.get_bool(ID_is_static))
continue;
member_exprt member(object, component.get_name(), component.type());
// recursive call
zero_initializer(member, component.type(), source_location, ops);
}
}
else if(
type.id() == ID_array && !cpp_is_pod(to_array_type(type).element_type()))
{
const array_typet &array_type=to_array_type(type);
const exprt &size_expr=array_type.size();
if(size_expr.id()==ID_infinity)
return; // don't initialize
const mp_integer size =
numeric_cast_v<mp_integer>(to_constant_expr(size_expr));
CHECK_RETURN(size>=0);
exprt::operandst empty_operands;
for(mp_integer i=0; i<size; ++i)
{
index_exprt index(
object, from_integer(i, c_index_type()), array_type.element_type());
zero_initializer(index, array_type.element_type(), source_location, ops);
}
}
else if(type.id() == ID_union_tag)
{
const auto &union_type = follow_tag(to_union_tag_type(type));
if(union_type.is_incomplete())
{
error().source_location = source_location;
error() << "cannot zero-initialize incomplete union" << eom;
throw 0;
}
// Select the largest component for zero-initialization
mp_integer max_comp_size=0;
union_typet::componentt comp;
for(const auto &component : union_type.components())
{
DATA_INVARIANT(component.type().is_not_nil(), "missing component type");
if(component.type().id()==ID_code)
continue;
auto component_size_opt = size_of_expr(component.type(), *this);
const auto size_int =
numeric_cast<mp_integer>(component_size_opt.value_or(nil_exprt()));
if(size_int.has_value())
{
if(*size_int > max_comp_size)
{
max_comp_size = *size_int;
comp=component;
}
}
}
if(max_comp_size>0)
{
const cpp_namet cpp_name(comp.get_base_name(), source_location);
exprt member(ID_member);
member.copy_to_operands(object);
member.set(ID_component_cpp_name, cpp_name);
zero_initializer(member, comp.type(), source_location, ops);
}
}
else if(type.id() == ID_c_enum_tag)
{
const unsignedbv_typet enum_type(
to_bitvector_type(follow_tag(to_c_enum_tag_type(type)).underlying_type())
.get_width());
exprt zero =
typecast_exprt::conditional_cast(from_integer(0, enum_type), type);
already_typechecked_exprt::make_already_typechecked(zero);
code_frontend_assignt assign;
assign.lhs()=object;
assign.rhs()=zero;
assign.add_source_location()=source_location;
typecheck_expr(assign.lhs());
assign.lhs().type().set(ID_C_constant, false);
already_typechecked_exprt::make_already_typechecked(assign.lhs());
typecheck_code(assign);
ops.push_back(assign);
}
else
{
const auto value = ::zero_initializer(type, source_location, *this);
if(!value.has_value())
{
error().source_location = source_location;
error() << "cannot zero-initialize '" << to_string(type) << "'" << eom;
throw 0;
}
code_frontend_assignt assign(object, *value);
assign.add_source_location()=source_location;
typecheck_expr(assign.lhs());
assign.lhs().type().set(ID_C_constant, false);
already_typechecked_exprt::make_already_typechecked(assign.lhs());
typecheck_code(assign);
ops.push_back(assign);
}
}