Skip to content

Commit 926f095

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 5a7cabd commit 926f095

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

jbmc/src/java_bytecode/assignments_from_json.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -445,10 +445,11 @@ nondet_length(allocate_objectst &allocate, source_locationt loc)
445445
/// For the assignment of the array elements, see
446446
/// \ref assign_array_data_component_from_json.
447447
/// For the overall algorithm, see \ref assign_from_json_rec.
448-
static code_with_references_listt assign_det_length_array_from_json(
448+
/// \return code for the assignment and length of the created array.
449+
static std::pair<code_with_references_listt, exprt>
450+
assign_det_length_array_from_json(
449451
const exprt &expr,
450452
const jsont &json,
451-
const exprt &given_length_expr,
452453
const optionalt<std::string> &type_from_array,
453454
object_creation_infot &info)
454455
{
@@ -458,11 +459,9 @@ static code_with_references_listt assign_det_length_array_from_json(
458459
const json_arrayt json_array = get_untyped_array(json, element_type);
459460
const auto number_of_elements =
460461
from_integer(json_array.size(), java_int_type());
461-
code_with_references_listt result;
462-
result.add(code_assumet{equal_exprt{given_length_expr, number_of_elements}});
463-
result.append(
464-
assign_array_data_component_from_json(expr, json, type_from_array, info));
465-
return result;
462+
return {
463+
assign_array_data_component_from_json(expr, json, type_from_array, info),
464+
number_of_elements};
466465
}
467466

468467
/// One of the cases in the recursive algorithm: the case where \p expr
@@ -475,6 +474,7 @@ static code_with_references_listt assign_det_length_array_from_json(
475474
/// For the assignment of the array elements, see
476475
/// \ref assign_array_data_component_from_json.
477476
/// For the overall algorithm, see \ref assign_from_json_rec.
477+
/// \return code for the assignment and length of the created array.
478478
static code_with_references_listt assign_nondet_length_array_from_json(
479479
const exprt &array,
480480
const jsont &json,
@@ -487,8 +487,7 @@ static code_with_references_listt assign_nondet_length_array_from_json(
487487
java_array_element_type(to_struct_tag_type(array.type().subtype()));
488488
const json_arrayt json_array = get_untyped_array(json, element_type);
489489
code_with_references_listt result;
490-
const auto number_of_elements =
491-
from_integer(json_array.size(), java_int_type());
490+
exprt number_of_elements = from_integer(json_array.size(), java_int_type());
492491
result.add(code_assumet{and_exprt{
493492
binary_predicate_exprt{given_length_expr, ID_ge, number_of_elements},
494493
binary_predicate_exprt{
@@ -814,12 +813,10 @@ static code_with_references_listt assign_reference_from_json(
814813
}
815814
else
816815
{
817-
result.append(assign_det_length_array_from_json(
818-
reference.expr,
819-
json,
820-
*reference.array_length,
821-
type_from_array,
822-
info));
816+
auto code_length_pair = assign_det_length_array_from_json(
817+
reference.expr, json, type_from_array, info);
818+
result.append(std::move(code_length_pair.first));
819+
reference.array_length = std::move(code_length_pair.second);
823820
}
824821
}
825822
else

0 commit comments

Comments
 (0)