@@ -13,6 +13,7 @@ Author: Diffblue Ltd.
13
13
14
14
#include < ansi-c/c_object_factory_parameters.h>
15
15
16
+ #include < util/allocate_objects.h>
16
17
#include < util/arith_tools.h>
17
18
#include < util/c_types.h>
18
19
#include < util/fresh_symbol.h>
@@ -26,39 +27,48 @@ Author: Diffblue Ltd.
26
27
27
28
class symbol_factoryt
28
29
{
29
- std::vector<const symbolt *> &symbols_created;
30
30
symbol_tablet &symbol_table;
31
31
const source_locationt &loc;
32
32
namespacet ns;
33
33
const c_object_factory_parameterst &object_factory_params;
34
34
35
+ allocate_objectst allocate_objects;
36
+
35
37
typedef std::set<irep_idt> recursion_sett;
36
38
37
39
public:
38
40
symbol_factoryt (
39
- std::vector<const symbolt *> &_symbols_created,
40
41
symbol_tablet &_symbol_table,
41
42
const source_locationt &loc,
42
43
const c_object_factory_parameterst &object_factory_params)
43
- : symbols_created(_symbols_created),
44
- symbol_table (_symbol_table),
44
+ : symbol_table(_symbol_table),
45
45
loc (loc),
46
46
ns(_symbol_table),
47
- object_factory_params(object_factory_params)
47
+ object_factory_params(object_factory_params),
48
+ allocate_objects(ID_C, loc, loc.get_function(), symbol_table)
48
49
{}
49
50
50
- exprt allocate_object (
51
- code_blockt &assignments,
52
- const exprt &target_expr,
53
- const typet &allocate_type,
54
- const bool static_lifetime);
55
-
56
51
void gen_nondet_init (
57
52
code_blockt &assignments,
58
53
const exprt &expr,
59
54
const std::size_t depth = 0 ,
60
55
recursion_sett recursion_set = recursion_sett());
61
56
57
+ void add_created_symbol (const symbolt *symbol_ptr)
58
+ {
59
+ allocate_objects.add_created_symbol (symbol_ptr);
60
+ }
61
+
62
+ void declare_created_symbols (code_blockt &init_code)
63
+ {
64
+ allocate_objects.declare_created_symbols (init_code);
65
+ }
66
+
67
+ void mark_created_symbols_as_input (code_blockt &init_code)
68
+ {
69
+ allocate_objects.mark_created_symbols_as_input (init_code);
70
+ }
71
+
62
72
private:
63
73
// / Generate initialisation code for each array element
64
74
// / \param assignments: The code block to add code to
@@ -73,49 +83,6 @@ class symbol_factoryt
73
83
const recursion_sett &recursion_set);
74
84
};
75
85
76
- // / Create a symbol for a pointer to point to
77
- // / \param assignments: The code block to add code to
78
- // / \param target_expr: The expression which we are allocating a symbol for
79
- // / \param allocate_type: The type to use for the symbol. If this doesn't match
80
- // / target_expr then a cast will be used for the assignment
81
- // / \param static_lifetime: Whether the symbol created should have static
82
- // / lifetime
83
- // / \return Returns the address of the allocated symbol
84
- exprt symbol_factoryt::allocate_object (
85
- code_blockt &assignments,
86
- const exprt &target_expr,
87
- const typet &allocate_type,
88
- const bool static_lifetime)
89
- {
90
- symbolt &aux_symbol = get_fresh_aux_symbol (
91
- allocate_type,
92
- id2string (loc.get_function ()),
93
- " tmp" ,
94
- loc,
95
- ID_C,
96
- symbol_table);
97
- aux_symbol.is_static_lifetime = static_lifetime;
98
- symbols_created.push_back (&aux_symbol);
99
-
100
- const typet &allocate_type_resolved=ns.follow (allocate_type);
101
- const typet &target_type=ns.follow (target_expr.type ().subtype ());
102
- bool cast_needed=allocate_type_resolved!=target_type;
103
-
104
- exprt aoe=address_of_exprt (aux_symbol.symbol_expr ());
105
- if (cast_needed)
106
- {
107
- aoe=typecast_exprt (aoe, target_expr.type ());
108
- }
109
-
110
- // Add the following code to assignments:
111
- // <target_expr> = &tmp$<temporary_counter>
112
- code_assignt assign (target_expr, aoe);
113
- assign.add_source_location ()=loc;
114
- assignments.add (std::move (assign));
115
-
116
- return aoe;
117
- }
118
-
119
86
// / Creates a nondet for expr, including calling itself recursively to make
120
87
// / appropriate symbols to point to if expr is a pointer.
121
88
// / \param assignments: The code block to add code to
@@ -155,17 +122,9 @@ void symbol_factoryt::gen_nondet_init(
155
122
156
123
code_blockt non_null_inst;
157
124
158
- exprt allocated=allocate_object (non_null_inst, expr, subtype, false );
125
+ exprt init_expr = allocate_objects.allocate_automatic_local_object (
126
+ non_null_inst, expr, subtype);
159
127
160
- exprt init_expr;
161
- if (allocated.id ()==ID_address_of)
162
- {
163
- init_expr=allocated.op0 ();
164
- }
165
- else
166
- {
167
- init_expr=dereference_exprt (allocated, allocated.type ().subtype ());
168
- }
169
128
gen_nondet_init (non_null_inst, init_expr, depth + 1 , recursion_set);
170
129
171
130
if (depth < object_factory_params.min_null_tree_depth )
@@ -288,39 +247,16 @@ symbol_exprt c_nondet_symbol_factory(
288
247
bool moving_symbol_failed=symbol_table.move (main_symbol, main_symbol_ptr);
289
248
CHECK_RETURN (!moving_symbol_failed);
290
249
291
- std::vector<const symbolt *> symbols_created;
292
- symbols_created.push_back (main_symbol_ptr);
293
-
294
- symbol_factoryt state (
295
- symbols_created, symbol_table, loc, object_factory_parameters);
250
+ symbol_factoryt state (symbol_table, loc, object_factory_parameters);
296
251
code_blockt assignments;
297
252
state.gen_nondet_init (assignments, main_symbol_expr);
298
253
299
- // Add the following code to init_code for each symbol that's been created:
300
- // <type> <identifier>;
301
- for (const symbolt * const symbol_ptr : symbols_created)
302
- {
303
- code_declt decl (symbol_ptr->symbol_expr ());
304
- decl.add_source_location ()=loc;
305
- init_code.add (std::move (decl));
306
- }
254
+ state.add_created_symbol (main_symbol_ptr);
255
+ state.declare_created_symbols (init_code);
307
256
308
257
init_code.append (assignments);
309
258
310
- // Add the following code to init_code for each symbol that's been created:
311
- // INPUT("<identifier>", <identifier>);
312
- for (symbolt const *symbol_ptr : symbols_created)
313
- {
314
- codet input_code (ID_input);
315
- input_code.operands ().resize (2 );
316
- input_code.op0 ()=
317
- address_of_exprt (index_exprt (
318
- string_constantt (symbol_ptr->base_name ),
319
- from_integer (0 , index_type ())));
320
- input_code.op1 ()=symbol_ptr->symbol_expr ();
321
- input_code.add_source_location ()=loc;
322
- init_code.add (std::move (input_code));
323
- }
259
+ state.mark_created_symbols_as_input (init_code);
324
260
325
261
return main_symbol_expr;
326
262
}
0 commit comments