CBMC
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/bitvector_types.h>
5 #include <util/pointer_expr.h>
6 #include <util/std_expr.h>
7 #include <util/std_types.h>
8 #include <util/type.h>
9 
12 
14 {
15 private:
18 
21  {
22  }
23 
24  void visit(const smt_bool_literal_termt &bool_literal) override
25  {
26  INVARIANT(
28  "Bool terms may only be used to construct bool typed expressions.");
29  result = bool_literal.value() ? (exprt)true_exprt{} : false_exprt{};
30  }
31 
32  void visit(const smt_identifier_termt &identifier_term) override
33  {
34  INVARIANT(
35  false, "Unexpected conversion of identifier to value expression.");
36  }
37 
38  void visit(const smt_bit_vector_constant_termt &bit_vector_constant) override
39  {
40  const auto sort_width = bit_vector_constant.get_sort().bit_width();
41  if(
42  const auto pointer_type =
43  type_try_dynamic_cast<pointer_typet>(type_to_construct))
44  {
45  INVARIANT(
46  pointer_type->get_width() == sort_width,
47  "Width of smt bit vector term must match the width of pointer type.");
48  if(bit_vector_constant.value() == 0)
49  {
51  }
52  else
53  {
54  // The reason we are manually constructing a constant_exprt here is a
55  // limitation in the design of `from_integer`, which only allows it to
56  // be used with pointer values of 0 (null pointers).
58  integer2bvrep(bit_vector_constant.value(), sort_width),
59  *pointer_type};
60  }
61  return;
62  }
63  if(
64  const auto bitvector_type =
65  type_try_dynamic_cast<bitvector_typet>(type_to_construct))
66  {
67  INVARIANT(
68  bitvector_type->get_width() == sort_width,
69  "Width of smt bit vector term must match the width of bit vector "
70  "type.");
71  result = from_integer(bit_vector_constant.value(), type_to_construct);
72  return;
73  }
74 
75  INVARIANT(
76  false,
77  "construct_value_expr_from_smt for bit vector should not be applied to "
78  "unsupported type " +
80  }
81 
82  void
83  visit(const smt_function_application_termt &function_application) override
84  {
85  INVARIANT(
86  false,
87  "Unexpected conversion of function application to value expression.");
88  }
89 
90  void visit(const smt_forall_termt &forall) override
91  {
92  INVARIANT(
93  false, "Unexpected conversion of forall quantifier to value expression.");
94  }
95 
96  void visit(const smt_exists_termt &exists) override
97  {
98  INVARIANT(
99  false, "Unexpected conversion of exists quantifier to value expression.");
100  }
101 
102 public:
106  static exprt make(const smt_termt &value_term, const typet &type_to_construct)
107  {
109  value_term.accept(factory);
110  INVARIANT(factory.result.has_value(), "Factory must result in expr value.");
111  return *factory.result;
112  }
113 };
114 
116  const smt_termt &value_term,
117  const typet &type_to_construct)
118 {
119  return value_expr_from_smt_factoryt::make(value_term, type_to_construct);
120 }
exists
mini_bddt exists(const mini_bddt &u, const unsigned var)
Definition: miniBDD.cpp:556
smt_identifier_termt
Stores identifiers in unescaped and unquoted form.
Definition: smt_terms.h:91
arith_tools.h
typet
The type of an expression, extends irept.
Definition: type.h:28
irept::pretty
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
Definition: irep.cpp:495
smt_termt
Definition: smt_terms.h:20
exprt
Base class for all expressions.
Definition: expr.h:55
bool_typet
The Boolean type.
Definition: std_types.h:35
smt_bit_vector_constant_termt::value
mp_integer value() const
Definition: smt_terms.cpp:115
value_expr_from_smt_factoryt::result
optionalt< exprt > result
Definition: construct_value_expr_from_smt.cpp:17
smt_bool_literal_termt::value
bool value() const
Definition: smt_terms.cpp:46
type.h
smt_bit_vector_sortt::bit_width
std::size_t bit_width() const
Definition: smt_sorts.cpp:60
value_expr_from_smt_factoryt::visit
void visit(const smt_bit_vector_constant_termt &bit_vector_constant) override
Definition: construct_value_expr_from_smt.cpp:38
construct_value_expr_from_smt.h
integer2bvrep
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...
Definition: arith_tools.cpp:380
null_pointer_exprt
The null pointer constant.
Definition: pointer_expr.h:734
smt_termt::accept
void accept(smt_term_const_downcast_visitort &) const
Definition: smt_terms.cpp:235
bitvector_types.h
std_types.h
pointer_expr.h
construct_value_expr_from_smt
exprt construct_value_expr_from_smt(const smt_termt &value_term, const typet &type_to_construct)
Given a value_term and a type_to_construct, this function constructs a value exprt with a value based...
Definition: construct_value_expr_from_smt.cpp:115
pointer_type
pointer_typet pointer_type(const typet &subtype)
Definition: c_types.cpp:253
false_exprt
The Boolean constant false.
Definition: std_expr.h:3016
optionalt
nonstd::optional< T > optionalt
Definition: optional.h:35
smt_forall_termt
Definition: smt_terms.h:225
value_expr_from_smt_factoryt::make
static exprt make(const smt_termt &value_term, const typet &type_to_construct)
This function is complete the external interface to this class.
Definition: construct_value_expr_from_smt.cpp:106
bitvector_typet::get_width
std::size_t get_width() const
Definition: std_types.h:876
smt_function_application_termt
Definition: smt_terms.h:132
value_expr_from_smt_factoryt::visit
void visit(const smt_forall_termt &forall) override
Definition: construct_value_expr_from_smt.cpp:90
value_expr_from_smt_factoryt
Definition: construct_value_expr_from_smt.cpp:13
value_expr_from_smt_factoryt::visit
void visit(const smt_exists_termt &exists) override
Definition: construct_value_expr_from_smt.cpp:96
value_expr_from_smt_factoryt::visit
void visit(const smt_bool_literal_termt &bool_literal) override
Definition: construct_value_expr_from_smt.cpp:24
from_integer
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
Definition: arith_tools.cpp:100
value_expr_from_smt_factoryt::value_expr_from_smt_factoryt
value_expr_from_smt_factoryt(const typet &type_to_construct)
Definition: construct_value_expr_from_smt.cpp:19
smt_bit_vector_constant_termt::get_sort
const smt_bit_vector_sortt & get_sort() const
Definition: smt_terms.cpp:120
smt_bool_literal_termt
Definition: smt_terms.h:76
smt_exists_termt
Definition: smt_terms.h:236
smt_term_const_downcast_visitort
Definition: smt_terms.h:247
smt_bit_vector_constant_termt
Definition: smt_terms.h:116
value_expr_from_smt_factoryt::visit
void visit(const smt_function_application_termt &function_application) override
Definition: construct_value_expr_from_smt.cpp:83
true_exprt
The Boolean constant true.
Definition: std_expr.h:3007
constant_exprt
A constant literal expression.
Definition: std_expr.h:2941
smt_terms.h
std_expr.h
value_expr_from_smt_factoryt::visit
void visit(const smt_identifier_termt &identifier_term) override
Definition: construct_value_expr_from_smt.cpp:32
validation_modet::INVARIANT
@ INVARIANT
value_expr_from_smt_factoryt::type_to_construct
const typet & type_to_construct
Definition: construct_value_expr_from_smt.cpp:16