@@ -26,23 +26,22 @@ class expr_initializert
26
26
{
27
27
}
28
28
29
- exprt operator ()(
30
- const typet &type,
31
- const source_locationt &source_location)
29
+ optionalt<exprt>
30
+ operator ()(const typet &type, const source_locationt &source_location)
32
31
{
33
32
return expr_initializer_rec (type, source_location);
34
33
}
35
34
36
35
protected:
37
36
const namespacet &ns;
38
37
39
- exprt expr_initializer_rec (
38
+ optionalt< exprt> expr_initializer_rec (
40
39
const typet &type,
41
40
const source_locationt &source_location);
42
41
};
43
42
44
43
template <bool nondet>
45
- exprt expr_initializert<nondet>::expr_initializer_rec(
44
+ optionalt< exprt> expr_initializert<nondet>::expr_initializer_rec(
46
45
const typet &type,
47
46
const source_locationt &source_location)
48
47
{
@@ -103,12 +102,12 @@ exprt expr_initializert<nondet>::expr_initializer_rec(
103
102
result = side_effect_expr_nondett (type, source_location);
104
103
else
105
104
{
106
- exprt sub_zero =
105
+ auto sub_zero =
107
106
expr_initializer_rec (to_complex_type (type).subtype (), source_location);
108
- if (sub_zero.is_nil ())
109
- return nil_exprt () ;
107
+ if (! sub_zero.has_value ())
108
+ return {} ;
110
109
111
- result = complex_exprt (sub_zero, sub_zero, to_complex_type (type));
110
+ result = complex_exprt (* sub_zero, * sub_zero, to_complex_type (type));
112
111
}
113
112
114
113
result.add_source_location ()=source_location;
@@ -130,17 +129,16 @@ exprt expr_initializert<nondet>::expr_initializer_rec(
130
129
}
131
130
else
132
131
{
133
- exprt tmpval =
134
- expr_initializer_rec (array_type.subtype (), source_location);
135
- if (tmpval.is_nil ())
136
- return nil_exprt ();
132
+ auto tmpval = expr_initializer_rec (array_type.subtype (), source_location);
133
+ if (!tmpval.has_value ())
134
+ return {};
137
135
138
136
if (array_type.size ().id ()==ID_infinity)
139
137
{
140
138
if (nondet)
141
139
return side_effect_expr_nondett (type, source_location);
142
140
143
- array_of_exprt value (tmpval, array_type);
141
+ array_of_exprt value (* tmpval, array_type);
144
142
value.add_source_location ()=source_location;
145
143
return std::move (value);
146
144
}
@@ -151,14 +149,15 @@ exprt expr_initializert<nondet>::expr_initializer_rec(
151
149
if (nondet)
152
150
return side_effect_expr_nondett (type, source_location);
153
151
else
154
- return nil_exprt () ;
152
+ return {} ;
155
153
}
156
154
157
155
if (*array_size < 0 )
158
- return nil_exprt () ;
156
+ return {} ;
159
157
160
158
array_exprt value ({}, array_type);
161
- value.operands ().resize (numeric_cast_v<std::size_t >(*array_size), tmpval);
159
+ value.operands ().resize (
160
+ numeric_cast_v<std::size_t >(*array_size), *tmpval);
162
161
value.add_source_location ()=source_location;
163
162
return std::move (value);
164
163
}
@@ -167,18 +166,18 @@ exprt expr_initializert<nondet>::expr_initializer_rec(
167
166
{
168
167
const vector_typet &vector_type=to_vector_type (type);
169
168
170
- exprt tmpval = expr_initializer_rec (vector_type.subtype (), source_location);
171
- if (tmpval.is_nil ())
172
- return nil_exprt () ;
169
+ auto tmpval = expr_initializer_rec (vector_type.subtype (), source_location);
170
+ if (! tmpval.has_value ())
171
+ return {} ;
173
172
174
173
const mp_integer vector_size =
175
174
numeric_cast_v<mp_integer>(vector_type.size ());
176
175
177
176
if (vector_size < 0 )
178
- return nil_exprt () ;
177
+ return {} ;
179
178
180
179
vector_exprt value ({}, vector_type);
181
- value.operands ().resize (numeric_cast_v<std::size_t >(vector_size), tmpval);
180
+ value.operands ().resize (numeric_cast_v<std::size_t >(vector_size), * tmpval);
182
181
value.add_source_location ()=source_location;
183
182
184
183
return std::move (value);
@@ -202,11 +201,11 @@ exprt expr_initializert<nondet>::expr_initializer_rec(
202
201
}
203
202
else
204
203
{
205
- const exprt member = expr_initializer_rec (c.type (), source_location);
206
- if (member.is_nil ())
207
- return nil_exprt () ;
204
+ const auto member = expr_initializer_rec (c.type (), source_location);
205
+ if (! member.has_value ())
206
+ return {} ;
208
207
209
- value.add_to_operands (std::move (member));
208
+ value.add_to_operands (std::move (* member));
210
209
}
211
210
}
212
211
@@ -241,62 +240,78 @@ exprt expr_initializert<nondet>::expr_initializer_rec(
241
240
}
242
241
}
243
242
244
- union_exprt value (" " , nil_exprt (), type);
245
- value.add_source_location ()=source_location;
246
-
247
243
if (!found)
248
244
{
249
245
// stupid empty union
246
+ union_exprt value (" " , nil_exprt (), type);
247
+ value.add_source_location () = source_location;
248
+ return std::move (value);
250
249
}
251
250
else
252
251
{
253
- value.set_component_name (component.get_name ());
254
- value.op ()=
252
+ auto component_value =
255
253
expr_initializer_rec (component.type (), source_location);
256
- if (value.op ().is_nil ())
257
- return nil_exprt ();
258
- }
259
254
260
- return std::move (value);
255
+ if (!component_value.has_value ())
256
+ return {};
257
+
258
+ union_exprt value (component.get_name (), *component_value, type);
259
+ value.add_source_location () = source_location;
260
+
261
+ return std::move (value);
262
+ }
261
263
}
262
264
else if (type_id == ID_symbol_type)
263
265
{
264
- exprt result = expr_initializer_rec (ns.follow (type), source_location);
266
+ auto result = expr_initializer_rec (ns.follow (type), source_location);
267
+
268
+ if (!result.has_value ())
269
+ return {};
270
+
265
271
// we might have mangled the type for arrays, so keep that
266
272
if (type.id () != ID_array)
267
- result. type ()= type;
273
+ result-> type () = type;
268
274
269
- return result;
275
+ return * result;
270
276
}
271
277
else if (type_id==ID_c_enum_tag)
272
278
{
273
- exprt result = expr_initializer_rec (
279
+ auto result = expr_initializer_rec (
274
280
ns.follow_tag (to_c_enum_tag_type (type)), source_location);
275
281
282
+ if (!result.has_value ())
283
+ return {};
284
+
276
285
// use the tag type
277
- result. type () = type;
286
+ result-> type () = type;
278
287
279
- return result;
288
+ return * result;
280
289
}
281
290
else if (type_id==ID_struct_tag)
282
291
{
283
- exprt result = expr_initializer_rec (
292
+ auto result = expr_initializer_rec (
284
293
ns.follow_tag (to_struct_tag_type (type)), source_location);
285
294
295
+ if (!result.has_value ())
296
+ return {};
297
+
286
298
// use the tag type
287
- result. type () = type;
299
+ result-> type () = type;
288
300
289
- return result;
301
+ return * result;
290
302
}
291
303
else if (type_id==ID_union_tag)
292
304
{
293
- exprt result = expr_initializer_rec (
305
+ auto result = expr_initializer_rec (
294
306
ns.follow_tag (to_union_tag_type (type)), source_location);
295
307
308
+ if (!result.has_value ())
309
+ return {};
310
+
296
311
// use the tag type
297
- result. type () = type;
312
+ result-> type () = type;
298
313
299
- return result;
314
+ return * result;
300
315
}
301
316
else if (type_id==ID_string)
302
317
{
@@ -310,7 +325,7 @@ exprt expr_initializert<nondet>::expr_initializer_rec(
310
325
return result;
311
326
}
312
327
else
313
- return nil_exprt () ;
328
+ return {} ;
314
329
}
315
330
316
331
// / Create the equivalent of zero for type `type`.
@@ -324,12 +339,7 @@ optionalt<exprt> zero_initializer(
324
339
const source_locationt &source_location,
325
340
const namespacet &ns)
326
341
{
327
- expr_initializert<false > z_i (ns);
328
- const exprt result = z_i (type, source_location);
329
- if (result.is_nil ())
330
- return {};
331
- else
332
- return result;
342
+ return expr_initializert<false >(ns)(type, source_location);
333
343
}
334
344
335
345
// / Create a non-deterministic value for type `type`, with all subtypes
@@ -344,10 +354,5 @@ optionalt<exprt> nondet_initializer(
344
354
const source_locationt &source_location,
345
355
const namespacet &ns)
346
356
{
347
- expr_initializert<true > z_i (ns);
348
- const exprt result = z_i (type, source_location);
349
- if (result.is_nil ())
350
- return {};
351
- else
352
- return result;
357
+ return expr_initializert<true >(ns)(type, source_location);
353
358
}
0 commit comments