Skip to content

Commit a19b7b3

Browse files
committed
Add encode struct_exprt as field concatenation
1 parent f192270 commit a19b7b3

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/solvers/smt2_incremental/struct_encoding.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "struct_encoding.h"
44

5+
#include <util/bitvector_expr.h>
56
#include <util/bitvector_types.h>
67
#include <util/make_unique.h>
78

@@ -41,6 +42,11 @@ typet struct_encodingt::encode(typet type) const
4142
return type;
4243
}
4344

45+
static exprt encode(const struct_exprt &struct_expr)
46+
{
47+
return concatenation_exprt{struct_expr.operands(), struct_expr.type()};
48+
}
49+
4450
exprt struct_encodingt::encode(exprt expr) const
4551
{
4652
std::queue<exprt *> work_queue;
@@ -50,6 +56,8 @@ exprt struct_encodingt::encode(exprt expr) const
5056
exprt &current = *work_queue.front();
5157
work_queue.pop();
5258
current.type() = encode(current.type());
59+
if(const auto struct_expr = expr_try_dynamic_cast<struct_exprt>(current))
60+
current = ::encode(*struct_expr);
5361
for(auto &operand : current.operands())
5462
work_queue.push(&operand);
5563
}

unit/solvers/smt2_incremental/struct_encoding.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Author: Diffblue Ltd.
22

33
#include <util/arith_tools.h>
4+
#include <util/bitvector_expr.h>
45
#include <util/bitvector_types.h>
56
#include <util/namespace.h>
67
#include <util/symbol_table.h>
@@ -71,9 +72,9 @@ TEST_CASE("struct encoding of types", "[core][smt2_incremental]")
7172

7273
TEST_CASE("struct encoding of expressions", "[core][smt2_incremental]")
7374
{
74-
const struct_union_typet::componentst components{
75+
const struct_union_typet::componentst component_types{
7576
{"green", signedbv_typet{32}}, {"eggs", unsignedbv_typet{16}}};
76-
const struct_typet struct_type{components};
77+
const struct_typet struct_type{component_types};
7778
const type_symbolt type_symbol{"my_structt", struct_type, ID_C};
7879
auto test = struct_encoding_test_environmentt::make();
7980
test.symbol_table.insert(type_symbol);
@@ -92,4 +93,15 @@ TEST_CASE("struct encoding of expressions", "[core][smt2_incremental]")
9293
const auto bv_equal = equal_exprt{symbol_expr_as_bv, symbol_expr_as_bv};
9394
REQUIRE(test.struct_encoding.encode(struct_equal) == bv_equal);
9495
}
96+
SECTION("expression for a struct from list of components")
97+
{
98+
const symbolt green_ham{"ham", signedbv_typet{32}, ID_C};
99+
test.symbol_table.insert(green_ham);
100+
const auto forty_two = from_integer(42, unsignedbv_typet{16});
101+
const exprt::operandst components{green_ham.symbol_expr(), forty_two};
102+
const struct_exprt struct_expr{components, struct_tag};
103+
const concatenation_exprt expected_result{
104+
{green_ham.symbol_expr(), forty_two}, bv_typet{48}};
105+
REQUIRE(test.struct_encoding.encode(struct_expr) == expected_result);
106+
}
95107
}

0 commit comments

Comments
 (0)