Skip to content

Commit 06682cf

Browse files
smowtontautschnig
authored andcommitted
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 a3da712 commit 06682cf

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/goto-programs/builtin_functions.cpp

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

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

710741
// zero-initialize the data
@@ -717,7 +748,7 @@ void goto_convertt::do_java_new_array(
717748
ns,
718749
get_message_handler());
719750
codet array_set(ID_array_set);
720-
array_set.copy_to_operands(data, zero_element);
751+
array_set.copy_to_operands(new_array_data_symbol, zero_element);
721752
goto_programt::targett t_d=dest.add_instruction(OTHER);
722753
t_d->code=array_set;
723754
t_d->source_location=location;

0 commit comments

Comments
 (0)