Skip to content

Commit c35f161

Browse files
Make given_length_expr argument non-optional
This simplifies the logic in assign_array_from_json and makes its specification simpler.
1 parent e0dfd8b commit c35f161

File tree

1 file changed

+20
-26
lines changed

1 file changed

+20
-26
lines changed

jbmc/src/java_bytecode/assignments_from_json.cpp

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -458,55 +458,42 @@ static symbol_exprt nondet_length(
458458

459459
/// One of the cases in the recursive algorithm: the case where \p expr
460460
/// represents an array.
461-
/// The length of the array is given by a symbol: \p given_length_expr if it is
462-
/// specified (this will be the case when there are two or more reference-equal
463-
/// arrays), or a fresh symbol otherwise.
464-
/// If \p given_length_expr is specified, we assume that an array with this
465-
/// symbol as its length has already been allocated and that \p expr has been
466-
/// assigned to it.
467-
/// Either way, the length symbol stores a nondet integer, and we add
468-
/// constraints on this: if "nondetLength" is specified in \p json, then the
469-
/// number of elements specified in \p json should be the minimum length of the
470-
/// array. Otherwise the number of elements should be the exact length of the
471-
/// array.
461+
/// The length of the array is given by symbol \p given_length_expr.
462+
/// We assume that an array with this symbol as its length has already been
463+
/// allocated and that \p expr has been assigned to it.
464+
/// We add constraints on the length: if "nondetLength" is specified in \p json,
465+
/// then the number of elements specified in \p json should be less than or
466+
/// equal to the length of the array. Otherwise the number of elements should be
467+
/// the exact length of the array.
472468
/// For the assignment of the array elements, see
473469
/// \ref assign_array_data_component_from_json.
474470
/// For the overall algorithm, see \ref assign_from_json_rec.
475471
static void assign_array_from_json(
476472
const exprt &expr,
477473
const jsont &json,
478-
const optionalt<symbol_exprt> &given_length_expr,
474+
const exprt &given_length_expr,
479475
const optionalt<std::string> &type_from_array,
480476
object_creation_infot &info)
481477
{
482478
PRECONDITION(is_java_array_type(expr.type()));
483479
const auto &element_type =
484480
java_array_element_type(to_struct_tag_type(expr.type().subtype()));
485-
486-
const exprt length_expr = [&] {
487-
if(given_length_expr)
488-
return *given_length_expr;
489-
const symbol_exprt length =
490-
nondet_length(info.allocate_objects, info.block, info.loc);
491-
allocate_array(expr, length, info);
492-
return length;
493-
}();
494481
const json_arrayt json_array = get_untyped_array(json, element_type);
495482
const auto number_of_elements =
496483
from_integer(json_array.size(), java_int_type());
497484
info.block.add(code_assumet{
498-
binary_predicate_exprt{length_expr, ID_ge, number_of_elements}});
485+
binary_predicate_exprt{given_length_expr, ID_ge, number_of_elements}});
499486
if(has_nondet_length(json))
500487
{
501488
info.block.add(code_assumet{binary_predicate_exprt{
502-
length_expr,
489+
given_length_expr,
503490
ID_le,
504491
from_integer(info.max_user_array_length, java_int_type())}});
505492
}
506493
else
507494
{
508495
info.block.add(code_assumet{
509-
binary_predicate_exprt{length_expr, ID_le, number_of_elements}});
496+
binary_predicate_exprt{given_length_expr, ID_le, number_of_elements}});
510497
}
511498
assign_array_data_component_from_json(expr, json, type_from_array, info);
512499
}
@@ -784,8 +771,10 @@ static void assign_reference_from_json(
784771
{
785772
if(is_java_array_type(expr.type()))
786773
{
774+
INVARIANT(
775+
reference.array_length, "an array reference should store its length");
787776
assign_array_from_json(
788-
reference.expr, json, reference.array_length, type_from_array, info);
777+
reference.expr, json, *reference.array_length, type_from_array, info);
789778
}
790779
else
791780
assign_struct_from_json(dereference_exprt(reference.expr), json, info);
@@ -823,7 +812,12 @@ void assign_from_json_rec(
823812
assign_reference_from_json(expr, json, type_from_array, info);
824813
}
825814
else if(is_java_array_type(expr.type()))
826-
assign_array_from_json(expr, json, {}, type_from_array, info);
815+
{
816+
const exprt length =
817+
nondet_length(info.allocate_objects, info.block, info.loc);
818+
allocate_array(expr, length, info);
819+
assign_array_from_json(expr, json, length, type_from_array, info);
820+
}
827821
else if(
828822
const auto runtime_type =
829823
::runtime_type(json, type_from_array, info.symbol_table))

0 commit comments

Comments
 (0)