Skip to content

Commit 95265a6

Browse files
Refactor substitute_array_access of array_exprt
1 parent ed3628e commit 95265a6

File tree

1 file changed

+11
-27
lines changed

1 file changed

+11
-27
lines changed

src/solvers/refinement/string_refinement.cpp

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,44 +1391,28 @@ static exprt substitute_array_access(
13911391
&symbol_generator)
13921392
{
13931393
const typet &char_type = array_expr.type().subtype();
1394+
const std::vector<exprt> &operands = array_expr.operands();
13941395

1395-
// Access to an empty array is undefined (non deterministic result)
1396-
if(array_expr.operands().empty())
1397-
return symbol_generator("out_of_bound_access", char_type);
1396+
exprt result = symbol_generator("out_of_bound_access", char_type);
13981397

1399-
const std::size_t last_index = array_expr.operands().size() - 1;
1400-
exprt ite = array_expr.operands().back();
1401-
1402-
if(ite.type() != char_type)
1403-
{
1404-
// We have to manually set the type for unknown values
1405-
INVARIANT(
1406-
ite.id() == ID_unknown,
1407-
string_refinement_invariantt(
1408-
"the last element can only have type char "
1409-
"or unknown, and it is not char type"));
1410-
ite.type() = char_type;
1411-
}
1412-
1413-
auto op_it = ++array_expr.operands().rbegin();
1414-
1415-
for(std::size_t i = last_index - 1; op_it != array_expr.operands().rend();
1416-
++op_it, --i)
1398+
for(std::size_t i = 0; i < operands.size(); ++i)
14171399
{
1418-
const equal_exprt equals(index, from_integer(i, java_int_type()));
1419-
if(op_it->type() != char_type)
1400+
// Go in reverse order so that smaller indexes appear first in the result
1401+
const std::size_t pos = operands.size() - 1 - i;
1402+
const equal_exprt equals(index, from_integer(pos, java_int_type()));
1403+
if(operands[pos].type() != char_type)
14201404
{
14211405
INVARIANT(
1422-
op_it->id() == ID_unknown,
1406+
operands[pos].id() == ID_unknown,
14231407
string_refinement_invariantt(
14241408
"elements can only have type char or "
14251409
"unknown, and it is not char type"));
1426-
ite = if_exprt(equals, exprt(ID_unknown, char_type), ite);
1410+
result = if_exprt(equals, exprt(ID_unknown, char_type), result);
14271411
}
14281412
else
1429-
ite = if_exprt(equals, *op_it, ite);
1413+
result = if_exprt(equals, operands[pos], result);
14301414
}
1431-
return ite;
1415+
return result;
14321416
}
14331417

14341418
static exprt substitute_array_access(

0 commit comments

Comments
 (0)