Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f195d3a

Browse files
smowtontautschnig
authored andcommittedAug 7, 2017
Preserve true array type in Java -> GOTO conversion. Fixes #184.
Previously all object array allocations became a generic void** when a Java-new operator was lowered into a CPP-new one. Now the type is preserved and is therefore available in the symbol table to build an appropriate source-level type during testcase generation.
1 parent 3139e3f commit f195d3a

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed
 

‎src/goto-programs/builtin_functions.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,20 @@ void goto_convertt::do_java_new_array(
692692
deref,
693693
struct_type.components()[2].get_name(),
694694
struct_type.components()[2].type());
695-
side_effect_exprt data_cpp_new_expr(ID_cpp_new_array, data.type());
695+
696+
// Allocate a (struct realtype**) instead of a (void**) if possible.
697+
const irept &given_element_type=object_type.find(ID_C_element_type);
698+
typet allocate_data_type;
699+
exprt cast_data_member;
700+
if(given_element_type.is_not_nil())
701+
{
702+
allocate_data_type=
703+
pointer_typet(static_cast<const typet &>(given_element_type));
704+
}
705+
else
706+
allocate_data_type=data.type();
707+
708+
side_effect_exprt data_cpp_new_expr(ID_cpp_new_array, allocate_data_type);
696709

697710
// The instruction may specify a (hopefully small) upper bound on the
698711
// array size, in which case we allocate a fixed-length array that may
@@ -704,8 +717,26 @@ void goto_convertt::do_java_new_array(
704717
data_cpp_new_expr.set(ID_size, rhs.op0());
705718
else
706719
data_cpp_new_expr.set(ID_size, size_bound);
720+
721+
// Must directly assign the new array to a temporary
722+
// because goto-symex will notice `x=side_effect_exprt` but not
723+
// `x=typecast_exprt(side_effect_exprt(...))`
724+
symbol_exprt new_array_data_symbol=
725+
new_tmp_symbol(
726+
data_cpp_new_expr.type(),
727+
"new_array_data",
728+
dest,
729+
location)
730+
.symbol_expr();
731+
goto_programt::targett t_p2=dest.add_instruction(ASSIGN);
732+
t_p2->code=code_assignt(new_array_data_symbol, data_cpp_new_expr);
733+
t_p2->source_location=location;
734+
707735
goto_programt::targett t_p=dest.add_instruction(ASSIGN);
708-
t_p->code=code_assignt(data, data_cpp_new_expr);
736+
exprt cast_cpp_new=new_array_data_symbol;
737+
if(cast_cpp_new.type()!=data.type())
738+
cast_cpp_new=typecast_exprt(cast_cpp_new, data.type());
739+
t_p->code=code_assignt(data, cast_cpp_new);
709740
t_p->source_location=location;
710741

711742
// zero-initialize the data
@@ -718,7 +749,7 @@ void goto_convertt::do_java_new_array(
718749
ns,
719750
get_message_handler());
720751
codet array_set(ID_array_set);
721-
array_set.copy_to_operands(data, zero_element);
752+
array_set.copy_to_operands(new_array_data_symbol, zero_element);
722753
goto_programt::targett t_d=dest.add_instruction(OTHER);
723754
t_d->code=array_set;
724755
t_d->source_location=location;

0 commit comments

Comments
 (0)
Please sign in to comment.