@@ -692,7 +692,20 @@ void goto_convertt::do_java_new_array(
692
692
deref,
693
693
struct_type.components ()[2 ].get_name (),
694
694
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);
696
709
697
710
// The instruction may specify a (hopefully small) upper bound on the
698
711
// array size, in which case we allocate a fixed-length array that may
@@ -704,8 +717,26 @@ void goto_convertt::do_java_new_array(
704
717
data_cpp_new_expr.set (ID_size, rhs.op0 ());
705
718
else
706
719
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
+
707
735
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);
709
740
t_p->source_location =location;
710
741
711
742
// zero-initialize the data
@@ -718,7 +749,7 @@ void goto_convertt::do_java_new_array(
718
749
ns,
719
750
get_message_handler ());
720
751
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);
722
753
goto_programt::targett t_d=dest.add_instruction (OTHER);
723
754
t_d->code =array_set;
724
755
t_d->source_location =location;
0 commit comments