@@ -1138,12 +1138,20 @@ codet java_string_library_preprocesst::make_assign_function_from_call(
1138
1138
}
1139
1139
1140
1140
// / Used to provide our own implementation of the
1141
- // / java.lang.String.toCharArray:()[C function.
1141
+ // / ` java.lang.String.toCharArray:()[C` function.
1142
1142
// / \param type: type of the function called
1143
1143
// / \param loc: location in the source
1144
1144
// / \param symbol_table: the symbol table
1145
- // / \return Code corresponding to > return_tmp0 = malloc(array[char]); >
1146
- // / return_tmp0->data=&((s->data)[0]) > return_tmp0->length=s->length
1145
+ // / \return Code corresponding to
1146
+ // / ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1147
+ // / lhs = new java::array[char]
1148
+ // / string_expr = {length=this->length, content=*(this->data)}
1149
+ // / data = new char[]
1150
+ // / *data = string_expr.content
1151
+ // / lhs->data = &data[0]
1152
+ // / lhs->length = string_expr.length
1153
+ // / return lhs
1154
+ // / ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1147
1155
codet java_string_library_preprocesst::make_string_to_char_array_code (
1148
1156
const code_typet &type,
1149
1157
const source_locationt &loc,
@@ -1154,29 +1162,37 @@ codet java_string_library_preprocesst::make_string_to_char_array_code(
1154
1162
const code_typet::parametert &p=type.parameters ()[0 ];
1155
1163
symbol_exprt string_argument (p.get_identifier (), p.type ());
1156
1164
assert (implements_java_char_sequence (string_argument.type ()));
1157
- dereference_exprt deref (string_argument, string_argument.type ().subtype ());
1158
1165
1159
- // lhs <- malloc(array[char])
1160
- exprt lhs=fresh_array (type.return_type (), loc, symbol_table);
1161
- allocate_dynamic_object_with_decl (
1162
- lhs, lhs.type ().subtype (), symbol_table, loc, code);
1166
+ // lhs = new java::array[char]
1167
+ exprt lhs=allocate_fresh_array (
1168
+ type.return_type (), loc, symbol_table, code);
1163
1169
1164
- // first_element_address is `&((string_argument->data)[0])`
1165
- exprt data=get_data (deref, symbol_table);
1170
+ // string_expr = {this->length, this->data}
1171
+ string_exprt string_expr=fresh_string_expr (loc, symbol_table, code);
1172
+ code.add (code_assign_java_string_to_string_expr (
1173
+ string_expr, string_argument, symbol_table));
1174
+ exprt string_expr_sym=fresh_string_expr_symbol (
1175
+ loc, symbol_table, code);
1176
+ code.add (code_assignt (string_expr_sym, string_expr));
1177
+
1178
+ // data = new char[]
1179
+ exprt data=allocate_fresh_array (
1180
+ pointer_typet (string_expr.content ().type ()), loc, symbol_table, code);
1181
+
1182
+ // *data = string_expr.content
1166
1183
dereference_exprt deref_data (data, data.type ().subtype ());
1167
- exprt first_index=from_integer (0 , string_length_type ());
1168
- index_exprt first_element (deref_data, first_index, java_char_type ());
1169
- address_of_exprt first_element_address (first_element);
1184
+ code.add (code_assignt (deref_data, string_expr.content ()));
1170
1185
1171
- // lhs->data <- &((string_argument-> data) [0])
1186
+ // lhs->data = & data[0]
1172
1187
dereference_exprt deref_lhs (lhs, lhs.type ().subtype ());
1173
1188
exprt lhs_data=get_data (deref_lhs, symbol_table);
1174
- code.add (code_assignt (lhs_data, first_element_address));
1189
+ index_exprt first_elt (
1190
+ deref_data, from_integer (0 , java_int_type ()), java_char_type ());
1191
+ code.add (code_assignt (lhs_data, address_of_exprt (first_elt)));
1175
1192
1176
- // lhs->length <- s->length
1177
- exprt rhs_length=get_length (deref, symbol_table);
1193
+ // lhs->length = string_expr.length
1178
1194
exprt lhs_length=get_length (deref_lhs, symbol_table);
1179
- code.add (code_assignt (lhs_length, rhs_length ));
1195
+ code.add (code_assignt (lhs_length, string_expr. length () ));
1180
1196
1181
1197
// return lhs
1182
1198
code.add (code_returnt (lhs));
0 commit comments