@@ -27,9 +27,19 @@ void replace_symbolt::insert(
27
27
old_expr.get_identifier (), new_expr));
28
28
}
29
29
30
- bool replace_symbolt::replace (
31
- exprt &dest,
32
- const bool replace_with_const) const
30
+ bool replace_symbolt::replace_symbol_expr (symbol_exprt &s) const
31
+ {
32
+ expr_mapt::const_iterator it = expr_map.find (s.get_identifier ());
33
+
34
+ if (it == expr_map.end ())
35
+ return true ;
36
+
37
+ static_cast <exprt &>(s) = it->second ;
38
+
39
+ return false ;
40
+ }
41
+
42
+ bool replace_symbolt::replace (exprt &dest) const
33
43
{
34
44
bool result=true ; // unchanged
35
45
@@ -49,14 +59,14 @@ bool replace_symbolt::replace(
49
59
{
50
60
member_exprt &me=to_member_expr (dest);
51
61
52
- if (!replace (me.struct_op (), replace_with_const)) // Could give non l-value.
62
+ if (!replace (me.struct_op ()))
53
63
result=false ;
54
64
}
55
65
else if (dest.id ()==ID_index)
56
66
{
57
67
index_exprt &ie=to_index_expr (dest);
58
68
59
- if (!replace (ie.array (), replace_with_const)) // Could give non l-value.
69
+ if (!replace (ie.array ()))
60
70
result=false ;
61
71
62
72
if (!replace (ie.index ()))
@@ -66,27 +76,13 @@ bool replace_symbolt::replace(
66
76
{
67
77
address_of_exprt &aoe=to_address_of_expr (dest);
68
78
69
- if (!replace (aoe.object (), false ))
79
+ if (!replace (aoe.object ()))
70
80
result=false ;
71
81
}
72
82
else if (dest.id ()==ID_symbol)
73
83
{
74
- const symbol_exprt &s=to_symbol_expr (dest);
75
-
76
- expr_mapt::const_iterator it=
77
- expr_map.find (s.get_identifier ());
78
-
79
- if (it!=expr_map.end ())
80
- {
81
- const exprt &e=it->second ;
82
-
83
- if (!replace_with_const && e.is_constant ()) // Would give non l-value.
84
- return true ;
85
-
86
- dest=e;
87
-
84
+ if (!replace_symbol_expr (to_symbol_expr (dest)))
88
85
return false ;
89
- }
90
86
}
91
87
else
92
88
{
@@ -252,3 +248,85 @@ void unchecked_replace_symbolt::insert(
252
248
{
253
249
expr_map.emplace (old_expr.get_identifier (), new_expr);
254
250
}
251
+
252
+ bool address_of_aware_replace_symbolt::replace (exprt &dest) const
253
+ {
254
+ const exprt &const_dest (dest);
255
+ if (!require_lvalue && const_dest.id () != ID_address_of)
256
+ return unchecked_replace_symbolt::replace (dest);
257
+
258
+ bool result=true ; // unchanged
259
+
260
+ // first look at type
261
+ if (have_to_replace (const_dest.type ()))
262
+ {
263
+ const set_require_lvalue_and_backupt backup (require_lvalue, false );
264
+ if (!unchecked_replace_symbolt::replace (dest.type ()))
265
+ result=false ;
266
+ }
267
+
268
+ // now do expression itself
269
+
270
+ if (!have_to_replace (dest))
271
+ return result;
272
+
273
+ if (dest.id ()==ID_index)
274
+ {
275
+ index_exprt &ie=to_index_expr (dest);
276
+
277
+ // Could yield non l-value.
278
+ if (!replace (ie.array ()))
279
+ result=false ;
280
+
281
+ const set_require_lvalue_and_backupt backup (require_lvalue, false );
282
+ if (!replace (ie.index ()))
283
+ result=false ;
284
+ }
285
+ else if (dest.id ()==ID_address_of)
286
+ {
287
+ address_of_exprt &aoe=to_address_of_expr (dest);
288
+
289
+ const set_require_lvalue_and_backupt backup (require_lvalue, true );
290
+ if (!replace (aoe.object ()))
291
+ result=false ;
292
+ }
293
+ else
294
+ {
295
+ if (!unchecked_replace_symbolt::replace (dest))
296
+ return false ;
297
+ }
298
+
299
+ const set_require_lvalue_and_backupt backup (require_lvalue, false );
300
+
301
+ const typet &c_sizeof_type =
302
+ static_cast <const typet&>(dest.find (ID_C_c_sizeof_type));
303
+ if (c_sizeof_type.is_not_nil () && have_to_replace (c_sizeof_type))
304
+ result &= unchecked_replace_symbolt::replace (
305
+ static_cast <typet&>(dest.add (ID_C_c_sizeof_type)));
306
+
307
+ const typet &va_arg_type =
308
+ static_cast <const typet&>(dest.find (ID_C_va_arg_type));
309
+ if (va_arg_type.is_not_nil () && have_to_replace (va_arg_type))
310
+ result &= unchecked_replace_symbolt::replace (
311
+ static_cast <typet&>(dest.add (ID_C_va_arg_type)));
312
+
313
+ return result;
314
+ }
315
+
316
+ bool address_of_aware_replace_symbolt::replace_symbol_expr (
317
+ symbol_exprt &s) const
318
+ {
319
+ expr_mapt::const_iterator it = expr_map.find (s.get_identifier ());
320
+
321
+ if (it == expr_map.end ())
322
+ return true ;
323
+
324
+ const exprt &e = it->second ;
325
+
326
+ if (require_lvalue && e.is_constant ()) // Would give non l-value.
327
+ return true ;
328
+
329
+ static_cast <exprt &>(s) = e;
330
+
331
+ return false ;
332
+ }
0 commit comments