Skip to content

Commit b89d99c

Browse files
Set the array length of reference when it is assigned
We know the number of elements of the reference array only when it is assigned so we replace the reference.array_length field at that point.
1 parent 5e92c9b commit b89d99c

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

jbmc/src/java_bytecode/assignments_from_json.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,11 @@ nondet_length(allocate_objectst &allocate, source_locationt loc)
448448
/// For the assignment of the array elements, see
449449
/// \ref assign_array_data_component_from_json.
450450
/// For the overall algorithm, see \ref assign_from_json_rec.
451-
static code_with_references_listt assign_det_length_array_from_json(
451+
/// \return code for the assignment and length of the created array.
452+
static std::pair<code_with_references_listt, exprt>
453+
assign_det_length_array_from_json(
452454
const exprt &expr,
453455
const jsont &json,
454-
const exprt &given_length_expr,
455456
const optionalt<std::string> &type_from_array,
456457
object_creation_infot &info)
457458
{
@@ -461,12 +462,9 @@ static code_with_references_listt assign_det_length_array_from_json(
461462
const json_arrayt json_array = get_untyped_array(json, element_type);
462463
const auto number_of_elements =
463464
from_integer(json_array.size(), java_int_type());
464-
code_with_references_listt result;
465-
result.add(code_without_referencest{
466-
code_assumet{equal_exprt{given_length_expr, number_of_elements}}});
467-
result.append(
468-
assign_array_data_component_from_json(expr, json, type_from_array, info));
469-
return result;
465+
return {
466+
assign_array_data_component_from_json(expr, json, type_from_array, info),
467+
number_of_elements};
470468
}
471469

472470
/// One of the cases in the recursive algorithm: the case where \p expr
@@ -479,6 +477,7 @@ static code_with_references_listt assign_det_length_array_from_json(
479477
/// For the assignment of the array elements, see
480478
/// \ref assign_array_data_component_from_json.
481479
/// For the overall algorithm, see \ref assign_from_json_rec.
480+
/// \return code for the assignment and length of the created array.
482481
static code_with_references_listt assign_nondet_length_array_from_json(
483482
const exprt &array,
484483
const jsont &json,
@@ -491,8 +490,7 @@ static code_with_references_listt assign_nondet_length_array_from_json(
491490
java_array_element_type(to_struct_tag_type(array.type().subtype()));
492491
const json_arrayt json_array = get_untyped_array(json, element_type);
493492
code_with_references_listt result;
494-
const auto number_of_elements =
495-
from_integer(json_array.size(), java_int_type());
493+
exprt number_of_elements = from_integer(json_array.size(), java_int_type());
496494
result.add(code_without_referencest{code_assumet{and_exprt{
497495
binary_predicate_exprt{given_length_expr, ID_ge, number_of_elements},
498496
binary_predicate_exprt{
@@ -823,12 +821,10 @@ static code_with_references_listt assign_reference_from_json(
823821
}
824822
else
825823
{
826-
result.append(assign_det_length_array_from_json(
827-
reference.expr,
828-
json,
829-
*reference.array_length,
830-
type_from_array,
831-
info));
824+
auto code_length_pair = assign_det_length_array_from_json(
825+
reference.expr, json, type_from_array, info);
826+
result.append(std::move(code_length_pair.first));
827+
reference.array_length = std::move(code_length_pair.second);
832828
}
833829
}
834830
else

0 commit comments

Comments
 (0)