Skip to content

Commit 3b61921

Browse files
fixup! Refactoring of substitute array access
1 parent 95265a6 commit 3b61921

File tree

2 files changed

+33
-35
lines changed

2 files changed

+33
-35
lines changed

src/solvers/refinement/string_refinement.cpp

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,18 +1224,18 @@ void debug_model(
12241224

12251225
sparse_arrayt::sparse_arrayt(const with_exprt &expr)
12261226
{
1227-
auto ref = std::ref(static_cast<const exprt &>(expr));
1228-
while(can_cast_expr<with_exprt>(ref.get()))
1227+
auto ref_to_current_expr = std::ref(static_cast<const exprt &>(expr));
1228+
while(can_cast_expr<with_exprt>(ref_to_current_expr.get()))
12291229
{
1230-
const auto &with_expr = expr_dynamic_cast<with_exprt>(ref.get());
1230+
const auto &with_expr = expr_dynamic_cast<with_exprt>(ref_to_current_expr.get());
12311231
const auto current_index = numeric_cast_v<std::size_t>(with_expr.where());
12321232
entries.emplace_back(current_index, with_expr.new_value());
1233-
ref = with_expr.old();
1233+
ref_to_current_expr = with_expr.old();
12341234
}
12351235

12361236
// This function only handles 'with' and 'array_of' expressions
1237-
PRECONDITION(ref.get().id() == ID_array_of);
1238-
default_value = expr_dynamic_cast<array_of_exprt>(ref.get()).what();
1237+
PRECONDITION(ref_to_current_expr.get().id() == ID_array_of);
1238+
default_value = expr_dynamic_cast<array_of_exprt>(ref_to_current_expr.get()).what();
12391239
}
12401240

12411241
exprt sparse_arrayt::to_if_expression(const exprt &index) const
@@ -1456,6 +1456,25 @@ static exprt substitute_array_access(
14561456
UNREACHABLE;
14571457
}
14581458

1459+
/// Auxiliary function for substitute_array_access
1460+
/// Performs the same operation but modifies the argument instead of returning
1461+
/// the resulting expression.
1462+
static void substitute_array_access_in_place(
1463+
exprt &expr,
1464+
const std::function<symbol_exprt(const irep_idt &, const typet &)>
1465+
&symbol_generator,
1466+
const bool left_propagate)
1467+
{
1468+
if(const auto index_expr = expr_try_dynamic_cast<index_exprt>(expr))
1469+
{
1470+
expr =
1471+
substitute_array_access(*index_expr, symbol_generator, left_propagate);
1472+
}
1473+
1474+
for(auto &op : expr.operands())
1475+
substitute_array_access_in_place(op, symbol_generator, left_propagate);
1476+
}
1477+
14591478
/// Create an equivalent expression where array accesses and 'with' expressions
14601479
/// are replaced by 'if' expressions, in particular:
14611480
/// * for an array access `arr[index]`, where:
@@ -1477,35 +1496,14 @@ static exprt substitute_array_access(
14771496
/// expressions
14781497
/// \return an expression containing no array access, and a Boolean which is
14791498
/// true if the expression is unchanged
1480-
std::pair<exprt, bool> substitute_array_access(
1481-
const exprt &expr,
1499+
exprt substitute_array_access(
1500+
exprt expr,
14821501
const std::function<symbol_exprt(const irep_idt &, const typet &)>
14831502
&symbol_generator,
14841503
const bool left_propagate)
14851504
{
1486-
if(const auto index_expr = expr_try_dynamic_cast<index_exprt>(expr))
1487-
{
1488-
const exprt substituted =
1489-
substitute_array_access(*index_expr, symbol_generator, left_propagate);
1490-
return {substituted, false};
1491-
}
1492-
1493-
exprt::operandst operands;
1494-
bool unchanged = true;
1495-
for(auto &op : expr.operands())
1496-
{
1497-
std::pair<exprt, bool> pair =
1498-
substitute_array_access(op, symbol_generator, left_propagate);
1499-
unchanged = unchanged && pair.second;
1500-
operands.push_back(pair.first);
1501-
}
1502-
1503-
if(unchanged)
1504-
return {expr, true};
1505-
1506-
exprt copy(expr);
1507-
copy.operands() = std::move(operands);
1508-
return {copy, false};
1505+
substitute_array_access_in_place(expr, symbol_generator, left_propagate);
1506+
return expr;
15091507
}
15101508

15111509
/// Negates the constraint to be fed to a solver. The intended usage is to find
@@ -1735,7 +1733,7 @@ static std::pair<bool, std::vector<exprt>> check_axioms(
17351733

17361734
stream << indent << i << ".\n";
17371735
const exprt with_concretized_arrays =
1738-
substitute_array_access(negaxiom, gen_symbol, true).first;
1736+
substitute_array_access(negaxiom, gen_symbol, true);
17391737
debug_check_axioms_step(
17401738
stream, ns, axiom, axiom_in_model, negaxiom, with_concretized_arrays);
17411739

@@ -1787,7 +1785,7 @@ static std::pair<bool, std::vector<exprt>> check_axioms(
17871785

17881786
negaxiom = simplify_expr(negaxiom, ns);
17891787
const exprt with_concrete_arrays =
1790-
substitute_array_access(negaxiom, gen_symbol, true).first;
1788+
substitute_array_access(negaxiom, gen_symbol, true);
17911789

17921790
stream << indent << i << ".\n";
17931791
debug_check_axioms_step(

src/solvers/refinement/string_refinement.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ union_find_replacet string_identifiers_resolution_from_equations(
152152
messaget::mstreamt &stream);
153153

154154
// Declaration required for unit-test:
155-
std::pair<exprt, bool> substitute_array_access(
156-
const exprt &expr,
155+
exprt substitute_array_access(
156+
exprt expr,
157157
const std::function<symbol_exprt(const irep_idt &, const typet &)>
158158
&symbol_generator,
159159
const bool left_propagate);

0 commit comments

Comments
 (0)