@@ -818,6 +818,9 @@ refined_string_exprt java_string_library_preprocesst::string_expr_of_function(
818
818
// / \param rhs_array: pointer to the array to assign
819
819
// / \param rhs_length: length of the array to assign
820
820
// / \param symbol_table: symbol table
821
+ // / \param is_constructor: the assignment happens in the context of a
822
+ // / constructor, so the whole object should be initialised, not just its
823
+ // / length and data fields.
821
824
// / \return return the following code:
822
825
// / ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
823
826
// / lhs = { {Object}, length=rhs_length, data=rhs_array }
@@ -826,41 +829,55 @@ codet java_string_library_preprocesst::code_assign_components_to_java_string(
826
829
const exprt &lhs,
827
830
const exprt &rhs_array,
828
831
const exprt &rhs_length,
829
- const symbol_table_baset &symbol_table)
832
+ const symbol_table_baset &symbol_table,
833
+ bool is_constructor)
830
834
{
831
835
PRECONDITION (implements_java_char_sequence_pointer (lhs.type ()));
832
836
dereference_exprt deref=checked_dereference (lhs, lhs.type ().subtype ());
833
837
834
- // A String has a field Object with @clsid = String
835
- const symbolt &jlo_symbol=*symbol_table.lookup (" java::java.lang.Object" );
836
- const struct_typet &jlo_struct=to_struct_type (jlo_symbol.type );
837
- struct_exprt jlo_init (jlo_struct);
838
- irep_idt clsid = get_tag (lhs.type ().subtype ());
839
- java_root_class_init (jlo_init, jlo_struct, clsid);
840
-
841
- struct_exprt struct_rhs (deref.type ());
842
- struct_rhs.copy_to_operands (jlo_init);
843
- struct_rhs.copy_to_operands (rhs_length);
844
- struct_rhs.copy_to_operands (rhs_array);
845
- return code_assignt (
846
- checked_dereference (lhs, lhs.type ().subtype ()), struct_rhs);
838
+ if (is_constructor)
839
+ {
840
+ // A String has a field Object with @clsid = String
841
+ const symbolt &jlo_symbol = *symbol_table.lookup (" java::java.lang.Object" );
842
+ const struct_typet &jlo_struct = to_struct_type (jlo_symbol.type );
843
+ struct_exprt jlo_init (jlo_struct);
844
+ irep_idt clsid = get_tag (lhs.type ().subtype ());
845
+ java_root_class_init (jlo_init, jlo_struct, clsid);
846
+
847
+ struct_exprt struct_rhs (deref.type ());
848
+ struct_rhs.copy_to_operands (jlo_init);
849
+ struct_rhs.copy_to_operands (rhs_length);
850
+ struct_rhs.copy_to_operands (rhs_array);
851
+ return code_assignt (
852
+ checked_dereference (lhs, lhs.type ().subtype ()), struct_rhs);
853
+ }
854
+ else
855
+ {
856
+ return code_blockt (
857
+ {code_assignt (get_length (deref, symbol_table), rhs_length),
858
+ code_assignt (get_data (deref, symbol_table), rhs_array)});
859
+ }
847
860
}
848
861
849
862
// / Produce code for an assignemnt of a string expr to a Java string.
850
863
// / \param lhs: an expression representing a java string
851
864
// / \param rhs: a string expression
852
865
// / \param symbol_table: symbol table
866
+ // / \param is_constructor: the assignment happens in the context of a
867
+ // / constructor, so the whole object should be initialised, not just its
868
+ // / length and data fields.
853
869
// / \return return the following code:
854
870
// / ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
855
871
// / lhs = { {Object}, length=rhs.length, data=rhs.data }
856
872
// / ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
857
873
codet java_string_library_preprocesst::code_assign_string_expr_to_java_string (
858
874
const exprt &lhs,
859
875
const refined_string_exprt &rhs,
860
- const symbol_table_baset &symbol_table)
876
+ const symbol_table_baset &symbol_table,
877
+ bool is_constructor)
861
878
{
862
879
return code_assign_components_to_java_string (
863
- lhs, rhs.content (), rhs.length (), symbol_table);
880
+ lhs, rhs.content (), rhs.length (), symbol_table, is_constructor );
864
881
}
865
882
866
883
// / \param lhs: a string expression
@@ -1044,14 +1061,14 @@ code_blockt java_string_library_preprocesst::make_float_to_string_code(
1044
1061
1045
1062
// We do not check the condition of the first element in the list as it
1046
1063
// will be reached only if all other conditions are not satisfied.
1047
- codet tmp_code= code_assign_string_expr_to_java_string (
1048
- str, string_expr_list[0 ], symbol_table);
1064
+ codet tmp_code = code_assign_string_expr_to_java_string (
1065
+ str, string_expr_list[0 ], symbol_table, true );
1049
1066
for (std::size_t i=1 ; i<condition_list.size (); i++)
1050
1067
{
1051
1068
code_ifthenelset ife;
1052
1069
ife.cond ()=condition_list[i];
1053
1070
ife.then_case () = code_assign_string_expr_to_java_string (
1054
- str, string_expr_list[i], symbol_table);
1071
+ str, string_expr_list[i], symbol_table, true );
1055
1072
ife.else_case ()=tmp_code;
1056
1073
tmp_code=ife;
1057
1074
}
@@ -1067,9 +1084,8 @@ code_blockt java_string_library_preprocesst::make_float_to_string_code(
1067
1084
// / \param type: the type of the function call
1068
1085
// / \param loc: location in program
1069
1086
// / \param symbol_table: the symbol table to populate
1070
- // / \param ignore_first_arg: boolean flag telling that the first argument should
1071
- // / not be part of the arguments of the call (but only used to be assigned the
1072
- // / result)
1087
+ // / \param is_constructor: the function being made is a constructor, so the
1088
+ // / whole object should be initialised, not just its length and data fields.
1073
1089
// / \return code for the `String.<init>(args)` function:
1074
1090
// /
1075
1091
// / cprover_string_length = fun(arg).length;
@@ -1083,14 +1099,14 @@ code_blockt java_string_library_preprocesst::make_init_function_from_call(
1083
1099
const java_method_typet &type,
1084
1100
const source_locationt &loc,
1085
1101
symbol_table_baset &symbol_table,
1086
- bool ignore_first_arg )
1102
+ bool is_constructor )
1087
1103
{
1088
1104
java_method_typet::parameterst params = type.parameters ();
1089
1105
1090
1106
// The first parameter is the object to be initialized
1091
1107
PRECONDITION (!params.empty ());
1092
1108
const symbol_exprt arg_this (params[0 ].get_identifier (), params[0 ].type ());
1093
- if (ignore_first_arg )
1109
+ if (is_constructor )
1094
1110
params.erase (params.begin ());
1095
1111
1096
1112
// Holder for output code
@@ -1105,7 +1121,8 @@ code_blockt java_string_library_preprocesst::make_init_function_from_call(
1105
1121
1106
1122
// arg_this <- string_expr
1107
1123
code.add (
1108
- code_assign_string_expr_to_java_string (arg_this, string_expr, symbol_table),
1124
+ code_assign_string_expr_to_java_string (
1125
+ arg_this, string_expr, symbol_table, is_constructor),
1109
1126
loc);
1110
1127
1111
1128
return code;
@@ -1466,7 +1483,7 @@ code_blockt java_string_library_preprocesst::make_string_format_code(
1466
1483
type.return_type (), loc, function_id, symbol_table, code);
1467
1484
code.add (
1468
1485
code_assign_string_expr_to_java_string (
1469
- java_string, string_expr, symbol_table),
1486
+ java_string, string_expr, symbol_table, true ),
1470
1487
loc);
1471
1488
code.add (code_returnt (java_string), loc);
1472
1489
return code;
@@ -1535,7 +1552,8 @@ code_blockt java_string_library_preprocesst::make_object_get_class_code(
1535
1552
exprt string1 = allocate_fresh_string (
1536
1553
string_ptr_type, loc, function_id, symbol_table, code);
1537
1554
code.add (
1538
- code_assign_string_expr_to_java_string (string1, string_expr1, symbol_table),
1555
+ code_assign_string_expr_to_java_string (
1556
+ string1, string_expr1, symbol_table, true ),
1539
1557
loc);
1540
1558
1541
1559
// > class1 = Class.forName(string1)
@@ -1615,7 +1633,8 @@ java_string_library_preprocesst::make_string_returning_function_from_call(
1615
1633
exprt str = allocate_fresh_string (
1616
1634
type.return_type (), loc, function_name, symbol_table, code);
1617
1635
code.add (
1618
- code_assign_string_expr_to_java_string (str, string_expr, symbol_table),
1636
+ code_assign_string_expr_to_java_string (
1637
+ str, string_expr, symbol_table, true ),
1619
1638
loc);
1620
1639
1621
1640
// Return value
@@ -1659,7 +1678,8 @@ code_blockt java_string_library_preprocesst::make_copy_string_code(
1659
1678
exprt str = allocate_fresh_string (
1660
1679
type.return_type (), loc, function_id, symbol_table, code);
1661
1680
code.add (
1662
- code_assign_string_expr_to_java_string (str, string_expr, symbol_table),
1681
+ code_assign_string_expr_to_java_string (
1682
+ str, string_expr, symbol_table, true ),
1663
1683
loc);
1664
1684
1665
1685
// Return value
@@ -1702,7 +1722,8 @@ code_blockt java_string_library_preprocesst::make_copy_constructor_code(
1702
1722
// Assign string_expr to `this` object
1703
1723
symbol_exprt arg_this (params[0 ].get_identifier (), params[0 ].type ());
1704
1724
code.add (
1705
- code_assign_string_expr_to_java_string (arg_this, string_expr, symbol_table),
1725
+ code_assign_string_expr_to_java_string (
1726
+ arg_this, string_expr, symbol_table, true ),
1706
1727
loc);
1707
1728
1708
1729
return code;
0 commit comments