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