Skip to content

Commit 2c79f51

Browse files
author
thk123
committed
Removed extra code and just combined into gen_nondet_pointer_init
Since the code had to go in the same file and meant in multiple places would have to construct a different class, I concluded that this half way refactoring was a step back. I think it is more confusing to follow without really being any easier to merge. The changes between the class and the master branch version introduced by this change are highly isolated (a since if statement inside one function).
1 parent 78a4e05 commit 2c79f51

File tree

1 file changed

+63
-102
lines changed

1 file changed

+63
-102
lines changed

src/java_bytecode/java_object_factory.cpp

+63-102
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ static symbolt &new_tmp_symbol(
4949

5050
class java_object_factoryt
5151
{
52-
protected:
5352
std::vector<const symbolt *> &symbols_created;
5453
const source_locationt &loc;
5554
std::unordered_set<irep_idt, irep_id_hash> recursion_set;
@@ -66,14 +65,13 @@ class java_object_factoryt
6665
code_assignt get_null_assignment(
6766
const exprt &expr,
6867
const pointer_typet &ptr_type);
69-
protected:
70-
virtual void gen_pointer_target_init(
68+
69+
void gen_pointer_target_init(
7170
code_blockt &assignments,
7271
const exprt &expr,
7372
const typet &target_type,
7473
bool create_dynamic_objects,
7574
update_in_placet update_in_place);
76-
protected:
7775

7876
void allocate_nondet_length_array(
7977
code_blockt &assignments,
@@ -96,8 +94,6 @@ class java_object_factoryt
9694
ns(_symbol_table)
9795
{}
9896

99-
virtual ~java_object_factoryt()=default;
100-
10197
exprt allocate_object(
10298
code_blockt &assignments,
10399
const exprt &,
@@ -109,7 +105,7 @@ class java_object_factoryt
109105
const exprt &expr,
110106
update_in_placet);
111107

112-
virtual void gen_nondet_init(
108+
void gen_nondet_init(
113109
code_blockt &assignments,
114110
const exprt &expr,
115111
bool is_sub,
@@ -138,36 +134,11 @@ class java_object_factoryt
138134
bool create_dynamic_objects,
139135
const struct_typet &struct_type,
140136
const update_in_placet &update_in_place);
141-
};
142-
143137

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(
162139
code_blockt &assignments,
163-
const exprt &expr,
164-
bool is_sub,
165-
irep_idt class_identifier,
166-
bool skip_classid,
167140
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);
171142
};
172143

173144
/// Generates code for allocating a dynamic object. This is used in
@@ -427,6 +398,27 @@ void java_object_factoryt::gen_nondet_pointer_init(
427398
const pointer_typet &pointer_type,
428399
const update_in_placet &update_in_place)
429400
{
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+
430422
const typet &subtype=ns.follow(pointer_type.subtype());
431423
if(subtype.id()==ID_struct)
432424
{
@@ -532,6 +524,41 @@ void java_object_factoryt::gen_nondet_pointer_init(
532524
}
533525
}
534526

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+
535562
/// Initialises an object tree rooted at `expr`, allocating child objects as
536563
/// necessary and nondet-initialising their members, or if MUST_UPDATE_IN_PLACE
537564
/// is set, re-initialising already-allocated objects.
@@ -907,7 +934,7 @@ exprt object_factory(
907934

908935
std::vector<const symbolt *> symbols_created;
909936
symbols_created.push_back(main_symbol_ptr);
910-
java_object_factory_with_randomt state(
937+
java_object_factoryt state(
911938
symbols_created,
912939
loc,
913940
!allow_null,
@@ -970,7 +997,7 @@ void gen_nondet_init(
970997
{
971998
std::vector<const symbolt *> symbols_created;
972999

973-
java_object_factory_with_randomt state(
1000+
java_object_factoryt state(
9741001
symbols_created,
9751002
loc,
9761003
assume_non_null,
@@ -999,69 +1026,3 @@ void gen_nondet_init(
9991026

10001027
init_code.append(assignments);
10011028
}
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

Comments
 (0)