-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathcpp_constructor.cpp
303 lines (243 loc) · 8.33 KB
/
cpp_constructor.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
/*******************************************************************\
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/pointer_expr.h>
/// \param source_location: source location for generated code
/// \param object: non-typechecked object
/// \param operands: non-typechecked operands
/// \return typechecked code
std::optional<codet> cpp_typecheckt::cpp_constructor(
const source_locationt &source_location,
const exprt &object,
const exprt::operandst &operands)
{
exprt object_tc=object;
typecheck_expr(object_tc);
elaborate_class_template(object_tc.type());
CHECK_RETURN(!is_reference(object_tc.type()));
if(object_tc.type().id() == ID_array)
{
// We allow only one operand and it must be tagged with '#array_ini'.
// Note that the operand is an array that is used for copy-initialization.
// In the general case, a program is not allowed to use this form of
// construct. This way of initializing an array is used internally only.
// The purpose of the tag #array_ini is to rule out ill-formed
// programs.
if(!operands.empty() && !operands.front().get_bool(ID_C_array_ini))
{
error().source_location=source_location;
error() << "bad array initializer" << eom;
throw 0;
}
DATA_INVARIANT(
operands.empty() || operands.size() == 1,
"array constructor must have at most one operand");
if(operands.empty() && cpp_is_pod(object_tc.type()))
return {};
const exprt &size_expr = to_array_type(object_tc.type()).size();
if(size_expr.id() == ID_infinity)
return {}; // don't initialize
exprt tmp_size=size_expr;
make_constant_index(tmp_size);
mp_integer s;
if(to_integer(to_constant_expr(tmp_size), s))
{
error().source_location=source_location;
error() << "array size '" << to_string(size_expr) << "' is not a constant"
<< eom;
throw 0;
}
/*if(cpp_is_pod(object_tc.type()))
{
code_expressiont new_code;
exprt op_tc=operands.front();
typecheck_expr(op_tc);
// Override constantness
object_tc.type().set("ID_C_constant", false);
object_tc.set("ID_C_lvalue", true);
side_effect_exprt assign(ID_assign);
assign.add_source_location()=source_location;
assign.copy_to_operands(object_tc, op_tc);
typecheck_side_effect_assignment(assign);
new_code.expression()=assign;
return new_code;
}
else*/
{
code_blockt new_code;
// for each element of the array, call the default constructor
for(mp_integer i=0; i < s; ++i)
{
exprt::operandst tmp_operands;
exprt constant = from_integer(i, c_index_type());
constant.add_source_location()=source_location;
index_exprt index = index_exprt(object_tc, constant);
index.add_source_location()=source_location;
if(!operands.empty())
{
index_exprt operand(operands.front(), constant);
operand.add_source_location()=source_location;
tmp_operands.push_back(operand);
}
auto i_code = cpp_constructor(source_location, index, tmp_operands);
if(i_code.has_value())
new_code.add(std::move(i_code.value()));
}
return std::move(new_code);
}
}
else if(cpp_is_pod(object_tc.type()))
{
exprt::operandst operands_tc=operands;
for(auto &op : operands_tc)
{
typecheck_expr(op);
add_implicit_dereference(op);
}
if(operands_tc.empty())
{
// a POD is NOT initialized
return {};
}
else if(operands_tc.size()==1)
{
// Override constantness
object_tc.type().set(ID_C_constant, false);
object_tc.set(ID_C_lvalue, true);
side_effect_expr_assignt assign(
object_tc, operands_tc.front(), typet(), source_location);
typecheck_side_effect_assignment(assign);
return code_expressiont(std::move(assign));
}
else
{
error().source_location=source_location;
error() << "initialization of POD requires one argument, "
"but got " << operands.size() << eom;
throw 0;
}
}
else if(object_tc.type().id() == ID_union_tag)
{
UNREACHABLE; // Todo: union
}
else if(object_tc.type().id() == ID_struct_tag)
{
exprt::operandst operands_tc=operands;
for(auto &op : operands_tc)
{
typecheck_expr(op);
add_implicit_dereference(op);
}
const struct_typet &struct_type =
follow_tag(to_struct_tag_type(object_tc.type()));
// set most-derived bits
code_blockt block;
for(const auto &component : struct_type.components())
{
if(component.get_base_name() != "@most_derived")
continue;
member_exprt member(object_tc, component.get_name(), bool_typet());
member.add_source_location()=source_location;
member.set(ID_C_lvalue, object_tc.get_bool(ID_C_lvalue));
exprt val=false_exprt();
if(!component.get_bool(ID_from_base))
val=true_exprt();
side_effect_expr_assignt assign(
std::move(member), std::move(val), typet(), source_location);
typecheck_side_effect_assignment(assign);
block.add(code_expressiont(std::move(assign)));
}
// enter struct scope
cpp_save_scopet save_scope(cpp_scopes);
cpp_scopes.set_scope(struct_type.get(ID_name));
// find name of constructor
const struct_typet::componentst &components=
struct_type.components();
irep_idt constructor_name;
for(const auto &c : components)
{
const typet &type = c.type();
if(
!c.get_bool(ID_from_base) && type.id() == ID_code &&
to_code_type(type).return_type().id() == ID_constructor)
{
constructor_name = c.get_base_name();
break;
}
}
INVARIANT(!constructor_name.empty(), "non-PODs should have a constructor");
side_effect_expr_function_callt function_call(
cpp_namet(constructor_name, source_location).as_expr(),
operands_tc,
uninitialized_typet(),
source_location);
typecheck_side_effect_function_call(function_call);
CHECK_RETURN(function_call.get(ID_statement) == ID_temporary_object);
exprt &initializer =
static_cast<exprt &>(function_call.add(ID_initializer));
DATA_INVARIANT(
initializer.id() == ID_code &&
initializer.get(ID_statement) == ID_expression,
"initializer must be expression statement");
auto &statement_expr = to_code_expression(to_code(initializer));
side_effect_expr_function_callt &func_ini =
to_side_effect_expr_function_call(statement_expr.expression());
exprt &tmp_this=func_ini.arguments().front();
DATA_INVARIANT(
to_address_of_expr(tmp_this).object().id() == ID_new_object,
"expected new_object operand in address_of expression");
tmp_this=address_of_exprt(object_tc);
const auto &initializer_code=to_code(initializer);
if(block.statements().empty())
return initializer_code;
else
{
block.add(initializer_code);
return std::move(block);
}
}
else
UNREACHABLE;
return {};
}
void cpp_typecheckt::new_temporary(
const source_locationt &source_location,
const typet &type,
const exprt::operandst &ops,
exprt &temporary)
{
// create temporary object
side_effect_exprt tmp_object_expr(ID_temporary_object, type, source_location);
tmp_object_expr.set(ID_mode, ID_cpp);
exprt new_object(ID_new_object);
new_object.add_source_location()=tmp_object_expr.source_location();
new_object.set(ID_C_lvalue, true);
new_object.type()=tmp_object_expr.type();
already_typechecked_exprt::make_already_typechecked(new_object);
auto new_code = cpp_constructor(source_location, new_object, ops);
if(new_code.has_value())
{
if(new_code->get_statement() == ID_assign)
tmp_object_expr.add_to_operands(std::move(new_code->op1()));
else
tmp_object_expr.add(ID_initializer) = *new_code;
}
temporary.swap(tmp_object_expr);
}
void cpp_typecheckt::new_temporary(
const source_locationt &source_location,
const typet &type,
const exprt &op,
exprt &temporary)
{
exprt::operandst ops;
ops.push_back(op);
new_temporary(source_location, type, ops, temporary);
}