Skip to content

Commit f192270

Browse files
committed
Add struct encoding support for symbol expressions
1 parent 112da4e commit f192270

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/solvers/smt2_incremental/struct_encoding.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,18 @@ typet struct_encodingt::encode(typet type) const
4040
}
4141
return type;
4242
}
43+
44+
exprt struct_encodingt::encode(exprt expr) const
45+
{
46+
std::queue<exprt *> work_queue;
47+
work_queue.push(&expr);
48+
while(!work_queue.empty())
49+
{
50+
exprt &current = *work_queue.front();
51+
work_queue.pop();
52+
current.type() = encode(current.type());
53+
for(auto &operand : current.operands())
54+
work_queue.push(&operand);
55+
}
56+
return expr;
57+
}

src/solvers/smt2_incremental/struct_encoding.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#ifndef CPROVER_SOLVERS_SMT2_INCREMENTAL_STRUCT_ENCODING_H
44
#define CPROVER_SOLVERS_SMT2_INCREMENTAL_STRUCT_ENCODING_H
55

6+
#include <util/expr.h> // For passing exprt by value. // IWYU pragma: keep
67
#include <util/type.h> // For passing `typet` by value. // IWYU pragma: keep
78

89
#include <memory>
@@ -19,6 +20,7 @@ class struct_encodingt final
1920
~struct_encodingt();
2021

2122
typet encode(typet type) const;
23+
exprt encode(exprt expr) const;
2224

2325
private:
2426
std::unique_ptr<boolbv_widtht> boolbv_width;

unit/solvers/smt2_incremental/struct_encoding.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,28 @@ TEST_CASE("struct encoding of types", "[core][smt2_incremental]")
6868
expected_encoded_array);
6969
}
7070
}
71+
72+
TEST_CASE("struct encoding of expressions", "[core][smt2_incremental]")
73+
{
74+
const struct_union_typet::componentst components{
75+
{"green", signedbv_typet{32}}, {"eggs", unsignedbv_typet{16}}};
76+
const struct_typet struct_type{components};
77+
const type_symbolt type_symbol{"my_structt", struct_type, ID_C};
78+
auto test = struct_encoding_test_environmentt::make();
79+
test.symbol_table.insert(type_symbol);
80+
const struct_tag_typet struct_tag{type_symbol.name};
81+
const symbolt struct_value_symbol{"my_struct", struct_tag, ID_C};
82+
test.symbol_table.insert(struct_value_symbol);
83+
const auto symbol_expr = struct_value_symbol.symbol_expr();
84+
const auto symbol_expr_as_bv = symbol_exprt{"my_struct", bv_typet{48}};
85+
SECTION("struct typed symbol expression")
86+
{
87+
REQUIRE(test.struct_encoding.encode(symbol_expr) == symbol_expr_as_bv);
88+
}
89+
SECTION("struct equality expression")
90+
{
91+
const auto struct_equal = equal_exprt{symbol_expr, symbol_expr};
92+
const auto bv_equal = equal_exprt{symbol_expr_as_bv, symbol_expr_as_bv};
93+
REQUIRE(test.struct_encoding.encode(struct_equal) == bv_equal);
94+
}
95+
}

0 commit comments

Comments
 (0)