Skip to content

index_exprt enforces array or vector type #7018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions src/ansi-c/expr2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1401,15 +1401,10 @@ std::string expr2ct::convert_unary_post(
return dest;
}

std::string expr2ct::convert_index(
const exprt &src,
unsigned precedence)
std::string expr2ct::convert_index(const binary_exprt &src, unsigned precedence)
{
if(src.operands().size()!=2)
return convert_norep(src, precedence);

unsigned p;
std::string op = convert_with_precedence(to_index_expr(src).array(), p);
std::string op = convert_with_precedence(src.op0(), p);

std::string dest;
if(precedence>p)
Expand All @@ -1419,7 +1414,7 @@ std::string expr2ct::convert_index(
dest+=')';

dest+='[';
dest += convert(to_index_expr(src).index());
dest += convert(src.op1());
dest+=']';

return dest;
Expand Down Expand Up @@ -3680,15 +3675,14 @@ std::string expr2ct::convert_with_precedence(
to_plus_expr(pointer).op0().type().id() == ID_pointer)
{
// Note that index[pointer] is legal C, but we avoid it nevertheless.
return convert(
index_exprt(to_plus_expr(pointer).op0(), to_plus_expr(pointer).op1()));
return convert_index(to_binary_expr(pointer), precedence = 16);
}
else
return convert_unary(to_unary_expr(src), "*", precedence = 15);
}

else if(src.id()==ID_index)
return convert_index(src, precedence=16);
return convert_index(to_binary_expr(src), precedence = 16);

else if(src.id()==ID_member)
return convert_member(to_member_expr(src), precedence=16);
Expand Down
3 changes: 1 addition & 2 deletions src/ansi-c/expr2c_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ class expr2ct
std::string convert_index_designator(
const exprt &src);

std::string convert_index(
const exprt &src, unsigned precedence);
std::string convert_index(const binary_exprt &, unsigned precedence);

std::string
convert_byte_extract(const byte_extract_exprt &, unsigned precedence);
Expand Down
7 changes: 7 additions & 0 deletions src/util/std_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,7 @@ inline notequal_exprt &to_notequal_expr(exprt &expr)
class index_exprt:public binary_exprt
{
public:
// _array must have either index or vector type.
// When _array has array_type, the type of _index
// must be array_type.index_type().
// This will eventually be enforced using a precondition.
Expand All @@ -1419,6 +1420,9 @@ class index_exprt:public binary_exprt
std::move(_index),
to_type_with_subtype(_array.type()).subtype())
{
const auto &array_op_type = _array.type();
PRECONDITION(
array_op_type.id() == ID_array || array_op_type.id() == ID_vector);
}

index_exprt(exprt _array, exprt _index, typet _type)
Expand All @@ -1428,6 +1432,9 @@ class index_exprt:public binary_exprt
std::move(_index),
std::move(_type))
{
const auto &array_op_type = array().type();
PRECONDITION(
array_op_type.id() == ID_array || array_op_type.id() == ID_vector);
}

exprt &array()
Expand Down
3 changes: 2 additions & 1 deletion unit/solvers/smt2_incremental/object_tracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ TEST_CASE("find_object_base_expression", "[core][smt2_incremental]")
TEST_CASE("Tracking object base expressions", "[core][smt2_incremental]")
{
const typet base_type = pointer_typet{signedbv_typet{16}, 18};
const symbol_exprt foo{"foo", base_type};
const symbol_exprt foo{
"foo", array_typet(base_type, from_integer(2, size_type()))};
const symbol_exprt bar{"bar", base_type};
const symbol_exprt qux{"qux", struct_typet{}};
const symbol_exprt index{"index", base_type};
Expand Down