Skip to content

Commit e0dfd8b

Browse files
Extract a nondet_length function from assign_array
This makes the assign_array_from_json a bit simpler. This is an intermediary commit towards removing the optional argument from assign_array_from_json.
1 parent db2ef9d commit e0dfd8b

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

jbmc/src/java_bytecode/assignments_from_json.cpp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,25 @@ static void allocate_array(
437437
info.block.add(assign);
438438
}
439439

440+
/// Declare a non-deterministic length expression
441+
/// \param[out] allocate: allocation functor
442+
/// \param[out] code: code block to which code will get appended
443+
/// \param loc: location for the created code
444+
/// \return the length expression that has been non-deterministically created
445+
static symbol_exprt nondet_length(
446+
allocate_objectst &allocate,
447+
code_blockt &code,
448+
source_locationt loc)
449+
{
450+
const symbol_exprt length_expr = allocate.allocate_automatic_local_object(
451+
java_int_type(), "user_specified_array_length");
452+
code.add(code_assignt{
453+
length_expr, side_effect_expr_nondett{java_int_type(), std::move(loc)}});
454+
code.add(code_assumet{binary_predicate_exprt{
455+
length_expr, ID_ge, from_integer(0, java_int_type())}});
456+
return length_expr;
457+
}
458+
440459
/// One of the cases in the recursive algorithm: the case where \p expr
441460
/// represents an array.
442461
/// The length of the array is given by a symbol: \p given_length_expr if it is
@@ -464,19 +483,14 @@ static void assign_array_from_json(
464483
const auto &element_type =
465484
java_array_element_type(to_struct_tag_type(expr.type().subtype()));
466485

467-
exprt length_expr;
468-
if(given_length_expr)
469-
length_expr = *given_length_expr;
470-
else
471-
{
472-
length_expr = info.allocate_objects.allocate_automatic_local_object(
473-
java_int_type(), "user_specified_array_length");
474-
info.block.add(code_assignt{
475-
length_expr, side_effect_expr_nondett{java_int_type(), info.loc}});
476-
info.block.add(code_assumet{binary_predicate_exprt{
477-
length_expr, ID_ge, from_integer(0, java_int_type())}});
478-
allocate_array(expr, length_expr, info);
479-
}
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+
}();
480494
const json_arrayt json_array = get_untyped_array(json, element_type);
481495
const auto number_of_elements =
482496
from_integer(json_array.size(), java_int_type());

0 commit comments

Comments
 (0)