@@ -49,7 +49,6 @@ static symbolt &new_tmp_symbol(
49
49
50
50
class java_object_factoryt
51
51
{
52
- protected:
53
52
std::vector<const symbolt *> &symbols_created;
54
53
const source_locationt &loc;
55
54
std::unordered_set<irep_idt, irep_id_hash> recursion_set;
@@ -66,14 +65,13 @@ class java_object_factoryt
66
65
code_assignt get_null_assignment (
67
66
const exprt &expr,
68
67
const pointer_typet &ptr_type);
69
- protected:
70
- virtual void gen_pointer_target_init (
68
+
69
+ void gen_pointer_target_init (
71
70
code_blockt &assignments,
72
71
const exprt &expr,
73
72
const typet &target_type,
74
73
bool create_dynamic_objects,
75
74
update_in_placet update_in_place);
76
- protected:
77
75
78
76
void allocate_nondet_length_array (
79
77
code_blockt &assignments,
@@ -96,8 +94,6 @@ class java_object_factoryt
96
94
ns (_symbol_table)
97
95
{}
98
96
99
- virtual ~java_object_factoryt ()=default ;
100
-
101
97
exprt allocate_object (
102
98
code_blockt &assignments,
103
99
const exprt &,
@@ -109,7 +105,7 @@ class java_object_factoryt
109
105
const exprt &expr,
110
106
update_in_placet);
111
107
112
- virtual void gen_nondet_init (
108
+ void gen_nondet_init (
113
109
code_blockt &assignments,
114
110
const exprt &expr,
115
111
bool is_sub,
@@ -138,36 +134,11 @@ class java_object_factoryt
138
134
bool create_dynamic_objects,
139
135
const struct_typet &struct_type,
140
136
const update_in_placet &update_in_place);
141
- };
142
-
143
137
144
- class java_object_factory_with_randomt :java_object_factoryt
145
- {
146
- public:
147
- java_object_factory_with_randomt (
148
- std::vector<const symbolt *> &_symbols_created,
149
- const source_locationt &loc,
150
- bool _assume_non_null,
151
- size_t _max_nondet_array_length,
152
- symbol_tablet &_symbol_table):
153
- java_object_factoryt (
154
- _symbols_created,
155
- loc,
156
- _assume_non_null,
157
- _max_nondet_array_length,
158
- _symbol_table)
159
- {}
160
-
161
- void gen_nondet_init (
138
+ symbol_exprt gen_nondet_subtype_pointer_init (
162
139
code_blockt &assignments,
163
- const exprt &expr,
164
- bool is_sub,
165
- irep_idt class_identifier,
166
- bool skip_classid,
167
140
bool create_dynamic_objects,
168
- bool override ,
169
- const typet &override_type,
170
- update_in_placet update_in_place) override ;
141
+ const pointer_typet &substitute_pointer_type);
171
142
};
172
143
173
144
// / Generates code for allocating a dynamic object. This is used in
@@ -427,6 +398,27 @@ void java_object_factoryt::gen_nondet_pointer_init(
427
398
const pointer_typet &pointer_type,
428
399
const update_in_placet &update_in_place)
429
400
{
401
+ select_pointer_typet pointer_type_selector (ns);
402
+ const pointer_typet &replacement_pointer_type=
403
+ pointer_type_selector (pointer_type);
404
+
405
+ // If we are changing the pointer, we generate code for creating a pointer
406
+ // to the substituted type instead
407
+ if (replacement_pointer_type!=pointer_type)
408
+ {
409
+ const symbol_exprt real_pointer_symbol=gen_nondet_subtype_pointer_init (
410
+ assignments,
411
+ create_dynamic_objects,
412
+ replacement_pointer_type);
413
+
414
+ // Having created a pointer to object of type replacement_pointer_type
415
+ // we now assign it back to the original pointer with a cast
416
+ // from pointer_type to replacement_pointer_type
417
+ assignments.add (
418
+ code_assignt (expr, typecast_exprt (real_pointer_symbol, pointer_type)));
419
+ return ;
420
+ }
421
+
430
422
const typet &subtype=ns.follow (pointer_type.subtype ());
431
423
if (subtype.id ()==ID_struct)
432
424
{
@@ -532,6 +524,41 @@ void java_object_factoryt::gen_nondet_pointer_init(
532
524
}
533
525
}
534
526
527
+ // / Generate GOTO code to initalize the selected concrete type
528
+ // / A { ... } tmp_object;
529
+ // / A.x = NONDET ...
530
+ // / // non-det init of all the fields of A
531
+ // / A * p = &tmp_object
532
+ // / expr = (I *)p
533
+ // / \param assignments: the code to append to
534
+ // / \param create_dynamic_objects: if true, use malloc to allocate objects;
535
+ // / otherwise generate fresh static symbols.
536
+ // / \param replacement_pointer: The type of the pointer we actually want to
537
+ // / to create.
538
+ // / \return The symbol expression that corresponds to the pointer to object
539
+ // / created of the required type.
540
+ symbol_exprt java_object_factoryt::gen_nondet_subtype_pointer_init (
541
+ code_blockt &assignments,
542
+ bool create_dynamic_objects,
543
+ const pointer_typet &replacement_pointer)
544
+ {
545
+ symbolt new_symbol=new_tmp_symbol (symbol_table, loc, replacement_pointer);
546
+
547
+ // Generate a new object into this new symbol
548
+ gen_nondet_init (
549
+ assignments,
550
+ new_symbol.symbol_expr (),
551
+ false ,
552
+ " " ,
553
+ false ,
554
+ create_dynamic_objects,
555
+ false ,
556
+ typet (),
557
+ update_in_placet::NO_UPDATE_IN_PLACE);
558
+
559
+ return new_symbol.symbol_expr ();
560
+ }
561
+
535
562
// / Initialises an object tree rooted at `expr`, allocating child objects as
536
563
// / necessary and nondet-initialising their members, or if MUST_UPDATE_IN_PLACE
537
564
// / is set, re-initialising already-allocated objects.
@@ -907,7 +934,7 @@ exprt object_factory(
907
934
908
935
std::vector<const symbolt *> symbols_created;
909
936
symbols_created.push_back (main_symbol_ptr);
910
- java_object_factory_with_randomt state (
937
+ java_object_factoryt state (
911
938
symbols_created,
912
939
loc,
913
940
!allow_null,
@@ -970,7 +997,7 @@ void gen_nondet_init(
970
997
{
971
998
std::vector<const symbolt *> symbols_created;
972
999
973
- java_object_factory_with_randomt state (
1000
+ java_object_factoryt state (
974
1001
symbols_created,
975
1002
loc,
976
1003
assume_non_null,
@@ -999,69 +1026,3 @@ void gen_nondet_init(
999
1026
1000
1027
init_code.append (assignments);
1001
1028
}
1002
-
1003
-
1004
- void java_object_factory_with_randomt::gen_nondet_init (
1005
- code_blockt &assignments,
1006
- const exprt &expr,
1007
- bool is_sub,
1008
- irep_idt class_identifier,
1009
- bool skip_classid,
1010
- bool create_dynamic_objects,
1011
- bool override ,
1012
- const typet &override_type,
1013
- update_in_placet update_in_place)
1014
- {
1015
- const typet &type=
1016
- override ? ns.follow (override_type) : ns.follow (expr.type ());
1017
- typet real_type=type;
1018
-
1019
- if (type.id ()==ID_pointer && type.subtype ().id ()==ID_symbol)
1020
- {
1021
- const pointer_typet &pointer_type=to_pointer_type (type);
1022
- const namespacet ns (symbol_table);
1023
-
1024
- select_pointer_typet pointer_type_selector (ns);
1025
- const pointer_typet &replacement_pointer=
1026
- pointer_type_selector (pointer_type);
1027
-
1028
- if (replacement_pointer!=real_type)
1029
- {
1030
- // Generate GOTO code to initalize the selected concrete type
1031
- // A { ... } tmp_object;
1032
- // A.x = NONDET ...
1033
- // // non-det init of all the fields of A
1034
- // A * p = &tmp_object
1035
- // expr = (I *)p
1036
-
1037
- symbolt new_symbol=new_tmp_symbol (symbol_table, loc, replacement_pointer);
1038
-
1039
- // Generate a new object into this new symbol
1040
- gen_nondet_init (
1041
- assignments,
1042
- new_symbol.symbol_expr (),
1043
- is_sub,
1044
- class_identifier,
1045
- skip_classid,
1046
- create_dynamic_objects,
1047
- override ,
1048
- override_type,
1049
- update_in_placet::NO_UPDATE_IN_PLACE);
1050
-
1051
- assignments.add (
1052
- code_assignt (expr, typecast_exprt (new_symbol.symbol_expr (), type)));
1053
-
1054
- return ;
1055
- }
1056
- }
1057
- java_object_factoryt::gen_nondet_init (
1058
- assignments,
1059
- expr,
1060
- is_sub,
1061
- class_identifier,
1062
- skip_classid,
1063
- create_dynamic_objects,
1064
- override ,
1065
- override_type,
1066
- update_in_place);
1067
- }
0 commit comments