Skip to content

Commit d5462fb

Browse files
committed
index_exprt enforces array or vector type
The constructors of index_exprt now require that the array operand either has array or vector type.
1 parent 3c69aa1 commit d5462fb

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

src/ansi-c/expr2c.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,15 +1401,10 @@ std::string expr2ct::convert_unary_post(
14011401
return dest;
14021402
}
14031403

1404-
std::string expr2ct::convert_index(
1405-
const exprt &src,
1406-
unsigned precedence)
1404+
std::string expr2ct::convert_index(const binary_exprt &src, unsigned precedence)
14071405
{
1408-
if(src.operands().size()!=2)
1409-
return convert_norep(src, precedence);
1410-
14111406
unsigned p;
1412-
std::string op = convert_with_precedence(to_index_expr(src).array(), p);
1407+
std::string op = convert_with_precedence(src.op0(), p);
14131408

14141409
std::string dest;
14151410
if(precedence>p)
@@ -1419,7 +1414,7 @@ std::string expr2ct::convert_index(
14191414
dest+=')';
14201415

14211416
dest+='[';
1422-
dest += convert(to_index_expr(src).index());
1417+
dest += convert(src.op1());
14231418
dest+=']';
14241419

14251420
return dest;
@@ -3680,15 +3675,14 @@ std::string expr2ct::convert_with_precedence(
36803675
to_plus_expr(pointer).op0().type().id() == ID_pointer)
36813676
{
36823677
// Note that index[pointer] is legal C, but we avoid it nevertheless.
3683-
return convert(
3684-
index_exprt(to_plus_expr(pointer).op0(), to_plus_expr(pointer).op1()));
3678+
return convert_index(to_binary_expr(pointer), precedence = 16);
36853679
}
36863680
else
36873681
return convert_unary(to_unary_expr(src), "*", precedence = 15);
36883682
}
36893683

36903684
else if(src.id()==ID_index)
3691-
return convert_index(src, precedence=16);
3685+
return convert_index(to_binary_expr(src), precedence = 16);
36923686

36933687
else if(src.id()==ID_member)
36943688
return convert_member(to_member_expr(src), precedence=16);

src/ansi-c/expr2c_class.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ class expr2ct
157157
std::string convert_index_designator(
158158
const exprt &src);
159159

160-
std::string convert_index(
161-
const exprt &src, unsigned precedence);
160+
std::string convert_index(const binary_exprt &, unsigned precedence);
162161

163162
std::string
164163
convert_byte_extract(const byte_extract_exprt &, unsigned precedence);

src/util/std_expr.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,6 +1409,7 @@ inline notequal_exprt &to_notequal_expr(exprt &expr)
14091409
class index_exprt:public binary_exprt
14101410
{
14111411
public:
1412+
// _array must have either index or vector type.
14121413
// When _array has array_type, the type of _index
14131414
// must be array_type.index_type().
14141415
// This will eventually be enforced using a precondition.
@@ -1419,6 +1420,9 @@ class index_exprt:public binary_exprt
14191420
std::move(_index),
14201421
to_type_with_subtype(_array.type()).subtype())
14211422
{
1423+
const auto &array_op_type = _array.type();
1424+
PRECONDITION(
1425+
array_op_type.id() == ID_array || array_op_type.id() == ID_vector);
14221426
}
14231427

14241428
index_exprt(exprt _array, exprt _index, typet _type)
@@ -1428,6 +1432,9 @@ class index_exprt:public binary_exprt
14281432
std::move(_index),
14291433
std::move(_type))
14301434
{
1435+
const auto &array_op_type = array().type();
1436+
PRECONDITION(
1437+
array_op_type.id() == ID_array || array_op_type.id() == ID_vector);
14311438
}
14321439

14331440
exprt &array()

unit/solvers/smt2_incremental/object_tracking.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ TEST_CASE("find_object_base_expression", "[core][smt2_incremental]")
9393
TEST_CASE("Tracking object base expressions", "[core][smt2_incremental]")
9494
{
9595
const typet base_type = pointer_typet{signedbv_typet{16}, 18};
96-
const symbol_exprt foo{"foo", base_type};
96+
const symbol_exprt foo{
97+
"foo", array_typet(base_type, from_integer(2, size_type()))};
9798
const symbol_exprt bar{"bar", base_type};
9899
const symbol_exprt qux{"qux", struct_typet{}};
99100
const symbol_exprt index{"index", base_type};

0 commit comments

Comments
 (0)