CBMC
smt_terms.h
Go to the documentation of this file.
1 // Author: Diffblue Ltd.
2 
3 #ifndef CPROVER_SOLVERS_SMT2_INCREMENTAL_SMT_TERMS_H
4 #define CPROVER_SOLVERS_SMT2_INCREMENTAL_SMT_TERMS_H
5 
6 #include <util/irep.h>
7 
12 
13 #include <functional>
14 #include <utility>
15 
16 class BigInt;
17 using mp_integer = BigInt;
18 
20 class smt_termt : protected irept, private smt_sortt::storert<smt_termt>
21 {
22 public:
23  // smt_termt does not support the notion of an empty / null state. Use
24  // optionalt<smt_termt> instead if an empty term is required.
25  smt_termt() = delete;
26 
27  using irept::pretty;
28 
29  bool operator==(const smt_termt &) const;
30  bool operator!=(const smt_termt &) const;
31 
32  const smt_sortt &get_sort() const;
33 
36 
42  template <typename derivedt>
43  class storert
44  {
45  protected:
46  storert();
47  static irept upcast(smt_termt term);
48  static const smt_termt &downcast(const irept &);
49  };
50 
51 protected:
52  smt_termt(irep_idt id, smt_sortt sort);
53 };
54 
55 template <typename derivedt>
57 {
58  static_assert(
59  std::is_base_of<irept, derivedt>::value &&
60  std::is_base_of<storert<derivedt>, derivedt>::value,
61  "Only irept based classes need to upcast smt_termt to store it.");
62 }
63 
64 template <typename derivedt>
66 {
67  return static_cast<irept &&>(std::move(term));
68 }
69 
70 template <typename derivedt>
72 {
73  return static_cast<const smt_termt &>(irep);
74 }
75 
77 {
78 public:
79  explicit smt_bool_literal_termt(bool value);
80  bool value() const;
81 };
82 
92  private smt_indext::storert<smt_identifier_termt>
93 {
94 public:
110  smt_sortt sort,
111  std::vector<smt_indext> indices = {});
112  irep_idt identifier() const;
113  std::vector<std::reference_wrapper<const smt_indext>> indices() const;
114 };
115 
117 {
118 public:
120  smt_bit_vector_constant_termt(const mp_integer &value, std::size_t bit_width);
121  mp_integer value() const;
122 
123  // This deliberately hides smt_termt::get_sort, because bit_vector terms
124  // always have bit_vector sorts. The same sort data is returned for both
125  // functions either way. Therefore this hiding is benign if the kind of sort
126  // is not important and useful for avoiding casts if the term is a known
127  // smt_bit_vector_constant_termt at compile time and the `bit_width()` is
128  // needed.
129  const smt_bit_vector_sortt &get_sort() const;
130 };
131 
133 {
134 private:
140  std::vector<smt_termt> arguments);
141 
142  // This is used to detect if \p functiont has an `indicies` member function.
143  // It will resolve to std::true_type if it does or std::false type otherwise.
144  template <class functiont, class = void>
145  struct has_indicest : std::false_type
146  {
147  };
148 
149  template <class functiont>
150  struct has_indicest<
151  functiont,
152  void_t<decltype(std::declval<functiont>().indices())>> : std::true_type
153  {
154  };
155 
157  template <class functiont>
158  static std::vector<smt_indext>
159  indices(const functiont &function, const std::false_type &has_indices)
160  {
161  return {};
162  }
163 
165  template <class functiont>
166  static std::vector<smt_indext>
167  indices(const functiont &function, const std::true_type &has_indices)
168  {
169  return function.indices();
170  }
171 
174  template <class functiont>
175  static std::vector<smt_indext> indices(const functiont &function)
176  {
177  return indices(function, has_indicest<functiont>{});
178  }
179 
180 public:
182  std::vector<std::reference_wrapper<const smt_termt>> arguments() const;
183 
184  template <typename functiont>
185  class factoryt
186  {
187  private:
188  functiont function;
189 
190  public:
191  template <typename... function_type_argument_typest>
192  explicit factoryt(function_type_argument_typest &&... arguments) noexcept
193  : function{std::forward<function_type_argument_typest>(arguments)...}
194  {
195  }
196 
197  template <typename... argument_typest>
199  operator()(argument_typest &&... arguments) const
200  {
201  function.validate(arguments...);
202  auto return_sort = function.return_sort(arguments...);
205  function.identifier(), std::move(return_sort), indices(function)},
206  {std::forward<argument_typest>(arguments)...}};
207  }
208 
209  template <typename... argument_typest>
211  validation(argument_typest &&... arguments) const
212  {
213  const auto validation_errors = function.validation_errors(arguments...);
214  if(!validation_errors.empty())
215  return response_or_errort<smt_termt>{validation_errors};
216  auto return_sort = function.return_sort(arguments...);
219  function.identifier(), std::move(return_sort), indices(function)},
220  {std::forward<argument_typest>(arguments)...}}};
221  }
222  };
223 };
224 
226 {
227 public:
229  std::vector<smt_identifier_termt> bound_variables,
231  std::vector<std::reference_wrapper<const smt_identifier_termt>>
232  bound_variables() const;
233  const smt_termt &predicate() const;
234 };
235 
237 {
238 public:
240  std::vector<smt_identifier_termt> bound_variables,
242  std::vector<std::reference_wrapper<const smt_identifier_termt>>
243  bound_variables() const;
244  const smt_termt &predicate() const;
245 };
246 
248 {
249 public:
250 #define TERM_ID(the_id) virtual void visit(const smt_##the_id##_termt &) = 0;
251 #include "smt_terms.def"
252 #undef TERM_ID
253 };
254 
255 #endif // CPROVER_SOLVERS_SMT2_INCREMENTAL_SMT_TERMS_H
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
smt_function_application_termt::indices
static std::vector< smt_indext > indices(const functiont &function)
Returns function.indices if functiont has an indices member function or returns an empty collection o...
Definition: smt_terms.h:175
smt_function_application_termt::factoryt::factoryt
factoryt(function_type_argument_typest &&... arguments) noexcept
Definition: smt_terms.h:192
mp_integer
BigInt mp_integer
Definition: smt_terms.h:17
response_or_errort
Holds either a valid parsed response or response sub-tree of type.
Definition: response_or_error.h:16
smt_function_application_termt::function_identifier
const smt_identifier_termt & function_identifier() const
Definition: smt_terms.cpp:141
smt_function_application_termt::indices
static std::vector< smt_indext > indices(const functiont &function, const std::false_type &has_indices)
Overload for when functiont does not have indices.
Definition: smt_terms.h:159
smt_identifier_termt
Stores identifiers in unescaped and unquoted form.
Definition: smt_terms.h:91
smt_identifier_termt::smt_identifier_termt
smt_identifier_termt(irep_idt identifier, smt_sortt sort, std::vector< smt_indext > indices={})
Constructs an identifier term with the given identifier and sort.
Definition: smt_terms.cpp:59
smt_function_application_termt::smt_function_application_termt
smt_function_application_termt(smt_identifier_termt function_identifier, std::vector< smt_termt > arguments)
Unchecked construction of function application terms.
Definition: smt_terms.cpp:127
irept::pretty
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
Definition: irep.cpp:495
response_or_error.h
smt_termt
Definition: smt_terms.h:20
smt_bit_vector_sortt
Definition: smt_sorts.h:83
smt_index.h
smt_forall_termt::smt_forall_termt
smt_forall_termt(std::vector< smt_identifier_termt > bound_variables, smt_termt predicate)
Definition: smt_terms.cpp:154
smt_termt::operator==
bool operator==(const smt_termt &) const
Definition: smt_terms.cpp:25
smt_sortt::storert
Class for adding the ability to up and down cast smt_sortt to and from irept.
Definition: smt_sorts.h:38
smt_bit_vector_constant_termt::value
mp_integer value() const
Definition: smt_terms.cpp:115
smt_bit_vector_constant_termt::smt_bit_vector_constant_termt
smt_bit_vector_constant_termt(const mp_integer &value, smt_bit_vector_sortt)
Definition: smt_terms.cpp:94
smt_forall_termt::predicate
const smt_termt & predicate() const
Definition: smt_terms.cpp:175
smt_bool_literal_termt::value
bool value() const
Definition: smt_terms.cpp:46
smt_termt::operator!=
bool operator!=(const smt_termt &) const
Definition: smt_terms.cpp:30
smt_sorts.h
smt_identifier_termt::indices
std::vector< std::reference_wrapper< const smt_indext > > indices() const
Definition: smt_terms.cpp:86
smt_termt::smt_termt
smt_termt()=delete
smt_termt::accept
void accept(smt_term_const_downcast_visitort &) const
Definition: smt_terms.cpp:235
smt_sortt
Definition: smt_sorts.h:17
smt_function_application_termt::factoryt::validation
response_or_errort< smt_termt > validation(argument_typest &&... arguments) const
Definition: smt_terms.h:211
smt_termt::storert::upcast
static irept upcast(smt_termt term)
Definition: smt_terms.h:65
smt_identifier_termt::identifier
irep_idt identifier() const
Definition: smt_terms.cpp:80
smt_termt::get_sort
const smt_sortt & get_sort() const
Definition: smt_terms.cpp:35
smt_forall_termt::bound_variables
std::vector< std::reference_wrapper< const smt_identifier_termt > > bound_variables() const
Definition: smt_terms.cpp:181
smt_exists_termt::smt_exists_termt
smt_exists_termt(std::vector< smt_identifier_termt > bound_variables, smt_termt predicate)
Definition: smt_terms.cpp:188
smt_exists_termt::predicate
const smt_termt & predicate() const
Definition: smt_terms.cpp:209
smt_termt::storert::storert
storert()
Definition: smt_terms.h:56
smt_function_application_termt::has_indicest
Definition: smt_terms.h:145
smt_forall_termt
Definition: smt_terms.h:225
smt_termt::storert::downcast
static const smt_termt & downcast(const irept &)
Definition: smt_terms.h:71
smt_function_application_termt::factoryt
Definition: smt_terms.h:185
smt_indext::storert
Class for adding the ability to up and down cast smt_indext to and from irept.
Definition: smt_index.h:36
smt_bool_literal_termt::smt_bool_literal_termt
smt_bool_literal_termt(bool value)
Definition: smt_terms.cpp:40
smt_termt::storert
Class for adding the ability to up and down cast smt_termt to and from irept.
Definition: smt_terms.h:43
smt_function_application_termt
Definition: smt_terms.h:132
type_traits.h
smt_function_application_termt::factoryt::operator()
smt_function_application_termt operator()(argument_typest &&... arguments) const
Definition: smt_terms.h:199
smt_bit_vector_constant_termt::get_sort
const smt_bit_vector_sortt & get_sort() const
Definition: smt_terms.cpp:120
void_t
typename detail::make_voidt< typest... >::type void_t
Definition: type_traits.h:35
smt_bool_literal_termt
Definition: smt_terms.h:76
irept
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition: irep.h:359
smt_function_application_termt::arguments
std::vector< std::reference_wrapper< const smt_termt > > arguments() const
Definition: smt_terms.cpp:147
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
smt_function_application_termt::indices
static std::vector< smt_indext > indices(const functiont &function, const std::true_type &has_indices)
Overload for when functiont has indices member function.
Definition: smt_terms.h:167
irep.h
smt_exists_termt::bound_variables
std::vector< std::reference_wrapper< const smt_identifier_termt > > bound_variables() const
Definition: smt_terms.cpp:215