cprover
expr.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #ifndef CPROVER_UTIL_EXPR_H
10 #define CPROVER_UTIL_EXPR_H
11 
12 #include "as_const.h"
13 #include "type.h"
14 #include "validate_expressions.h"
15 #include "validate_types.h"
16 #include "validation_mode.h"
17 
18 #define forall_operands(it, expr) \
19  for(exprt::operandst::const_iterator \
20  it = as_const(expr).operands().begin(), \
21  it##_end = as_const(expr).operands().end(); \
22  it != it##_end; \
23  ++it)
24 
25 #define Forall_operands(it, expr) \
26  if((expr).has_operands()) /* NOLINT(readability/braces) */ \
27  for(exprt::operandst::iterator it=(expr).operands().begin(); \
28  it!=(expr).operands().end(); ++it)
29 
30 #define forall_expr(it, expr) \
31  for(exprt::operandst::const_iterator it=(expr).begin(); \
32  it!=(expr).end(); ++it)
33 
34 #define Forall_expr(it, expr) \
35  for(exprt::operandst::iterator it=(expr).begin(); \
36  it!=(expr).end(); ++it)
37 
38 class depth_iteratort;
41 
53 class exprt:public irept
54 {
55 public:
56  typedef std::vector<exprt> operandst;
57 
58  // constructors
59  exprt() { }
60  explicit exprt(const irep_idt &_id):irept(_id) { }
61 
62  exprt(irep_idt _id, typet _type)
63  : irept(std::move(_id), {{ID_type, std::move(_type)}}, {})
64  {
65  }
66 
67  exprt(irep_idt _id, typet _type, operandst &&_operands)
68  : irept(
69  std::move(_id),
70  {{ID_type, std::move(_type)}},
71  std::move((irept::subt &&) _operands))
72  {
73  }
74 
76  : exprt(id, std::move(type))
77  {
78  add_source_location() = std::move(loc);
79  }
80 
82  typet &type() { return static_cast<typet &>(add(ID_type)); }
83  const typet &type() const
84  {
85  return static_cast<const typet &>(find(ID_type));
86  }
87 
90  std::size_t size() const;
91 
93  bool has_operands() const
94  { return !operands().empty(); }
95 
97  { return (operandst &)get_sub(); }
98 
99  const operandst &operands() const
100  { return (const operandst &)get_sub(); }
101 
102 protected:
104  { return operands().front(); }
105 
107  { return operands()[1]; }
108 
110  { return operands()[2]; }
111 
113  { return operands()[3]; }
114 
115  const exprt &op0() const
116  { return operands().front(); }
117 
118  const exprt &op1() const
119  { return operands()[1]; }
120 
121  const exprt &op2() const
122  { return operands()[2]; }
123 
124  const exprt &op3() const
125  { return operands()[3]; }
126 
127 public:
129  { operands().reserve(n) ; }
130 
133  void copy_to_operands(const exprt &expr)
134  {
135  operands().push_back(expr);
136  }
137 
140  void add_to_operands(const exprt &expr)
141  {
142  copy_to_operands(expr);
143  }
144 
147  void add_to_operands(exprt &&expr)
148  {
149  operands().push_back(std::move(expr));
150  }
151 
155  void copy_to_operands(const exprt &e1, const exprt &e2)
156  {
157  operandst &op = operands();
158  #ifndef USE_LIST
159  op.reserve(op.size() + 2);
160  #endif
161  op.push_back(e1);
162  op.push_back(e2);
163  }
164 
168  void add_to_operands(const exprt &e1, const exprt &e2)
169  {
170  copy_to_operands(e1, e2);
171  }
172 
176  void add_to_operands(exprt &&e1, exprt &&e2)
177  {
178  operandst &op = operands();
179  #ifndef USE_LIST
180  op.reserve(op.size() + 2);
181  #endif
182  op.push_back(std::move(e1));
183  op.push_back(std::move(e2));
184  }
185 
190  void add_to_operands(const exprt &e1, const exprt &e2, const exprt &e3)
191  {
192  copy_to_operands(e1, e2, e3);
193  }
194 
199  void copy_to_operands(const exprt &e1, const exprt &e2, const exprt &e3)
200  {
201  operandst &op = operands();
202  #ifndef USE_LIST
203  op.reserve(op.size() + 3);
204  #endif
205  op.push_back(e1);
206  op.push_back(e2);
207  op.push_back(e3);
208  }
209 
214  void add_to_operands(exprt &&e1, exprt &&e2, exprt &&e3)
215  {
216  operandst &op = operands();
217  #ifndef USE_LIST
218  op.reserve(op.size() + 3);
219  #endif
220  op.push_back(std::move(e1));
221  op.push_back(std::move(e2));
222  op.push_back(std::move(e3));
223  }
224 
225  bool is_constant() const;
226  bool is_true() const;
227  bool is_false() const;
228  bool is_zero() const;
229  bool is_one() const;
230  bool is_boolean() const;
231 
232  const source_locationt &find_source_location() const;
233 
235  {
236  return static_cast<const source_locationt &>(find(ID_C_source_location));
237  }
238 
240  {
241  return static_cast<source_locationt &>(add(ID_C_source_location));
242  }
243 
245  {
246  remove(ID_C_source_location);
247  }
248 
257  static void check(const exprt &, const validation_modet)
258  {
259  }
260 
269  static void validate(
270  const exprt &expr,
271  const namespacet &,
273  {
274  check_expr(expr, vm);
275  }
276 
285  static void validate_full(
286  const exprt &expr,
287  const namespacet &ns,
289  {
290  // first check operands (if any)
291  for(const exprt &op : expr.operands())
292  {
293  validate_full_expr(op, ns, vm);
294  }
295 
296  // type may be nil
297  const typet &t = expr.type();
298 
299  validate_full_type(t, ns, vm);
300 
301  validate_expr(expr, ns, vm);
302  }
303 
304 protected:
305  exprt &add_expr(const irep_idt &name)
306  {
307  return static_cast<exprt &>(add(name));
308  }
309 
310  const exprt &find_expr(const irep_idt &name) const
311  {
312  return static_cast<const exprt &>(find(name));
313  }
314 
315 public:
319  void visit(class expr_visitort &visitor);
320  void visit(class const_expr_visitort &visitor) const;
321  void visit_pre(std::function<void(exprt &)>);
322  void visit_pre(std::function<void(const exprt &)>) const;
323 
327  void visit_post(std::function<void(exprt &)>);
328  void visit_post(std::function<void(const exprt &)>) const;
329 
336  depth_iteratort depth_begin(std::function<exprt &()> mutate_root) const;
341 };
342 
346 class expr_protectedt : public exprt
347 {
348 protected:
349  // constructors
351  : exprt(std::move(_id), std::move(_type))
352  {
353  }
354 
355  expr_protectedt(irep_idt _id, typet _type, operandst _operands)
356  : exprt(std::move(_id), std::move(_type), std::move(_operands))
357  {
358  }
359 
360  // protect these low-level methods
361  using exprt::add;
362  using exprt::op0;
363  using exprt::op1;
364  using exprt::op2;
365  using exprt::op3;
366  using exprt::remove;
367 };
368 
370 {
371 public:
372  virtual ~expr_visitort() { }
373  virtual void operator()(exprt &) { }
374 };
375 
377 {
378 public:
379  virtual ~const_expr_visitort() { }
380  virtual void operator()(const exprt &) { }
381 };
382 
383 #endif // CPROVER_UTIL_EXPR_H
void validate_expr(const bswap_exprt &value)
unsignedbv_typet size_type()
Definition: c_types.cpp:58
virtual void operator()(const exprt &)
Definition: expr.h:380
virtual ~const_expr_visitort()
Definition: expr.h:379
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
Base class for all expressions.
Definition: expr.h:347
expr_protectedt(irep_idt _id, typet _type)
Definition: expr.h:350
expr_protectedt(irep_idt _id, typet _type, operandst _operands)
Definition: expr.h:355
virtual void operator()(exprt &)
Definition: expr.h:373
virtual ~expr_visitort()
Definition: expr.h:372
Base class for all expressions.
Definition: expr.h:54
void add_to_operands(const exprt &e1, const exprt &e2, const exprt &e3)
Add the given arguments to the end of exprt's operands.
Definition: expr.h:190
const source_locationt & find_source_location() const
Get a source_locationt from the expression or from its operands (non-recursively).
Definition: expr.cpp:179
std::vector< exprt > operandst
Definition: expr.h:56
void copy_to_operands(const exprt &e1, const exprt &e2, const exprt &e3)
Copy the given arguments to the end of exprt's operands.
Definition: expr.h:199
bool is_one() const
Return whether the expression is a constant representing 1.
Definition: expr.cpp:128
exprt & op3()
Definition: expr.h:112
bool has_operands() const
Return true if there is at least one operand.
Definition: expr.h:93
const exprt & op3() const
Definition: expr.h:124
void add_to_operands(exprt &&e1, exprt &&e2, exprt &&e3)
Add the given arguments to the end of exprt's operands.
Definition: expr.h:214
bool is_true() const
Return whether the expression is a constant representing true.
Definition: expr.cpp:47
exprt & op1()
Definition: expr.h:106
static void validate(const exprt &expr, const namespacet &, const validation_modet vm=validation_modet::INVARIANT)
Check that the expression is well-formed, assuming that its subexpressions and type have all ready be...
Definition: expr.h:269
void copy_to_operands(const exprt &expr)
Copy the given argument to the end of exprt's operands.
Definition: expr.h:133
exprt & add_expr(const irep_idt &name)
Definition: expr.h:305
const typet & type() const
Definition: expr.h:83
depth_iteratort depth_end()
Definition: expr.cpp:281
const_depth_iteratort depth_cend() const
Definition: expr.cpp:289
source_locationt & add_source_location()
Definition: expr.h:239
exprt()
Definition: expr.h:59
const_unique_depth_iteratort unique_depth_end() const
Definition: expr.cpp:298
bool is_boolean() const
Return whether the expression represents a Boolean.
Definition: expr.cpp:65
depth_iteratort depth_begin()
Definition: expr.cpp:279
const source_locationt & source_location() const
Definition: expr.h:234
const_unique_depth_iteratort unique_depth_cend() const
Definition: expr.cpp:302
void visit(class expr_visitort &visitor)
These are pre-order traversal visitors, i.e., the visitor is executed on a node before its children h...
Definition: expr.cpp:269
exprt & op2()
Definition: expr.h:109
bool is_false() const
Return whether the expression is a constant representing false.
Definition: expr.cpp:56
void reserve_operands(operandst::size_type n)
Definition: expr.h:128
static void check(const exprt &, const validation_modet)
Check that the expression is well-formed (shallow checks only, i.e., subexpressions and its type are ...
Definition: expr.h:257
bool is_zero() const
Return whether the expression is a constant representing 0.
Definition: expr.cpp:78
const exprt & op2() const
Definition: expr.h:121
void visit_pre(std::function< void(exprt &)>)
Definition: expr.cpp:259
typet & type()
Return the type of the expression.
Definition: expr.h:82
static void validate_full(const exprt &expr, const namespacet &ns, const validation_modet vm=validation_modet::INVARIANT)
Check that the expression is well-formed (full check, including checks of all subexpressions and the ...
Definition: expr.h:285
exprt(irep_idt _id, typet _type)
Definition: expr.h:62
exprt(irep_idt _id, typet _type, operandst &&_operands)
Definition: expr.h:67
bool is_constant() const
Return whether the expression is a constant.
Definition: expr.cpp:40
exprt & op0()
Definition: expr.h:103
const exprt & op0() const
Definition: expr.h:115
exprt(const irep_idt &_id)
Definition: expr.h:60
const_depth_iteratort depth_cbegin() const
Definition: expr.cpp:287
const operandst & operands() const
Definition: expr.h:99
operandst & operands()
Definition: expr.h:96
void visit_post(std::function< void(exprt &)>)
These are post-order traversal visitors, i.e., the visitor is executed on a node after its children h...
Definition: expr.cpp:230
const exprt & find_expr(const irep_idt &name) const
Definition: expr.h:310
void add_to_operands(exprt &&e1, exprt &&e2)
Add the given arguments to the end of exprt's operands.
Definition: expr.h:176
void add_to_operands(exprt &&expr)
Add the given argument to the end of exprt's operands.
Definition: expr.h:147
exprt(const irep_idt &id, typet type, source_locationt loc)
Definition: expr.h:75
void copy_to_operands(const exprt &e1, const exprt &e2)
Copy the given arguments to the end of exprt's operands.
Definition: expr.h:155
void add_to_operands(const exprt &e1, const exprt &e2)
Add the given arguments to the end of exprt's operands.
Definition: expr.h:168
void drop_source_location()
Definition: expr.h:244
const_unique_depth_iteratort unique_depth_cbegin() const
Definition: expr.cpp:300
std::size_t size() const
Amount of nodes this expression tree contains.
Definition: expr.cpp:26
const exprt & op1() const
Definition: expr.h:118
void add_to_operands(const exprt &expr)
Add the given argument to the end of exprt's operands.
Definition: expr.h:140
const_unique_depth_iteratort unique_depth_begin() const
Definition: expr.cpp:296
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition: irep.h:383
subt & get_sub()
Definition: irep.h:466
irept & add(const irep_namet &name)
Definition: irep.cpp:113
const irept & find(const irep_namet &name) const
Definition: irep.cpp:103
const irep_idt & id() const
Definition: irep.h:407
void remove(const irep_namet &name)
Definition: irep.cpp:93
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:92
The type of an expression, extends irept.
Definition: type.h:28
Defines typet, type_with_subtypet and type_with_subtypest.
void check_expr(const exprt &expr, const validation_modet vm)
Check that the given expression is well-formed (shallow checks only, i.e., subexpressions and its typ...
void validate_full_expr(const exprt &expr, const namespacet &ns, const validation_modet vm)
Check that the given expression is well-formed (full check, including checks of all subexpressions an...
void validate_full_type(const typet &type, const namespacet &ns, const validation_modet vm)
Check that the given type is well-formed (full check, including checks of subtypes)
validation_modet