CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
construct_value_expr_from_smt.cpp
Go to the documentation of this file.
1// Author: Diffblue Ltd.
2
3#include <util/arith_tools.h>
4#include <util/c_types.h>
5#include <util/namespace.h>
6#include <util/pointer_expr.h>
7#include <util/std_expr.h>
8#include <util/std_types.h>
9#include <util/type.h>
10
13
15{
16private:
18 const namespacet &ns;
19 std::optional<exprt> result;
20
27
29 {
32 "Bool terms may only be used to construct bool typed expressions.");
34 }
35
37 {
39 false, "Unexpected conversion of identifier to value expression.");
40 }
41
43 {
44 const auto sort_width = bit_vector_constant.get_sort().bit_width();
45 if(
46 const auto pointer_type =
48 {
51 "Width of smt bit vector term must match the width of pointer type.");
52 if(bit_vector_constant.value() == 0)
53 {
55 }
56 else
57 {
58 // The reason we are manually constructing a constant_exprt here is a
59 // limitation in the design of `from_integer`, which only allows it to
60 // be used with pointer values of 0 (null pointers).
64 }
65 return;
66 }
67 if(
68 const auto bitvector_type =
70 {
72 bitvector_type->get_width() == sort_width,
73 "Width of smt bit vector term must match the width of bit vector "
74 "type.");
76 return;
77 }
78 if(
79 const auto c_enum_tag_type =
81 {
82 const c_enum_typet &real_type = ns.follow_tag(*c_enum_tag_type);
84 to_bitvector_type(real_type.underlying_type()).get_width() ==
86 "Width of smt bit vector term must match the width of bit vector "
87 "underlying type of the original c_enum type.");
89 result->type() = type_to_construct;
90 return;
91 }
92
94 false,
95 "construct_value_expr_from_smt for bit vector should not be applied to "
96 "unsupported type " +
97 type_to_construct.pretty());
98 }
99
100 void
101 visit(const smt_function_application_termt &function_application) override
102 {
103 INVARIANT(
104 false,
105 "Unexpected conversion of function application to value expression.");
106 }
107
108 void visit(const smt_forall_termt &forall) override
109 {
110 INVARIANT(
111 false, "Unexpected conversion of forall quantifier to value expression.");
112 }
113
114 void visit(const smt_exists_termt &exists) override
115 {
116 INVARIANT(
117 false, "Unexpected conversion of exists quantifier to value expression.");
118 }
119
120public:
124 static exprt make(
125 const smt_termt &value_term,
127 const namespacet &ns)
128 {
130 value_term.accept(factory);
131 INVARIANT(factory.result.has_value(), "Factory must result in expr value.");
132 return *factory.result;
133 }
134};
135
137 const smt_termt &value_term,
138 const typet &type_to_construct,
139 const namespacet &ns)
140{
141 return value_expr_from_smt_factoryt::make(value_term, type_to_construct, ns);
142}
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
irep_idt integer2bvrep(const mp_integer &src, std::size_t width)
convert an integer to bit-vector representation with given width This uses two's complement for negat...
const bitvector_typet & to_bitvector_type(const typet &type)
Cast a typet to a bitvector_typet.
pointer_typet pointer_type(const typet &subtype)
Definition c_types.cpp:235
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
std::size_t get_width() const
Definition std_types.h:925
The Boolean type.
Definition std_types.h:36
The type of C enums.
Definition c_types.h:239
A constant literal expression.
Definition std_expr.h:3117
Base class for all expressions.
Definition expr.h:56
The Boolean constant false.
Definition std_expr.h:3199
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition namespace.h:91
The null pointer constant.
Stores identifiers in unescaped and unquoted form.
Definition smt_terms.h:93
The Boolean constant true.
Definition std_expr.h:3190
The type of an expression, extends irept.
Definition type.h:29
void visit(const smt_bool_literal_termt &bool_literal) override
void visit(const smt_function_application_termt &function_application) override
void visit(const smt_identifier_termt &identifier_term) override
static exprt make(const smt_termt &value_term, const typet &type_to_construct, const namespacet &ns)
This function is complete the external interface to this class.
void visit(const smt_forall_termt &forall) override
void visit(const smt_bit_vector_constant_termt &bit_vector_constant) override
void visit(const smt_exists_termt &exists) override
value_expr_from_smt_factoryt(const typet &type_to_construct, const namespacet &ns)
exprt construct_value_expr_from_smt(const smt_termt &value_term, const typet &type_to_construct, const namespacet &ns)
Given a value_term and a type_to_construct, this function constructs a value exprt with a value based...
mini_bddt exists(const mini_bddt &u, const unsigned var)
Definition miniBDD.cpp:556
API to expression classes for Pointers.
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition invariant.h:423
API to expression classes.
Pre-defined types.
Defines typet, type_with_subtypet and type_with_subtypest.