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 "deprecate.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  if((expr).has_operands()) /* NOLINT(readability/braces) */ \
20  for(exprt::operandst::const_iterator it=(expr).operands().begin(), \
21  it##_end=(expr).operands().end(); \
22  it!=it##_end; ++it)
23 
24 #define Forall_operands(it, expr) \
25  if((expr).has_operands()) /* NOLINT(readability/braces) */ \
26  for(exprt::operandst::iterator it=(expr).operands().begin(); \
27  it!=(expr).operands().end(); ++it)
28 
29 #define forall_expr(it, expr) \
30  for(exprt::operandst::const_iterator it=(expr).begin(); \
31  it!=(expr).end(); ++it)
32 
33 #define Forall_expr(it, expr) \
34  for(exprt::operandst::iterator it=(expr).begin(); \
35  it!=(expr).end(); ++it)
36 
37 class depth_iteratort;
40 
52 class exprt:public irept
53 {
54 public:
55  typedef std::vector<exprt> operandst;
56 
57  // constructors
58  exprt() { }
59  explicit exprt(const irep_idt &_id):irept(_id) { }
60 
61  exprt(irep_idt _id, typet _type)
62  : irept(std::move(_id), {{ID_type, std::move(_type)}}, {})
63  {
64  }
65 
66  exprt(irep_idt _id, typet _type, operandst &&_operands)
67  : irept(
68  std::move(_id),
69  {{ID_type, std::move(_type)}},
70  std::move((irept::subt &&) _operands))
71  {
72  }
73 
75  : exprt(id, std::move(type))
76  {
77  add_source_location() = std::move(loc);
78  }
79 
81  typet &type() { return static_cast<typet &>(add(ID_type)); }
82  const typet &type() const
83  {
84  return static_cast<const typet &>(find(ID_type));
85  }
86 
89  std::size_t size() const;
90 
92  bool has_operands() const
93  { return !operands().empty(); }
94 
96  { return (operandst &)get_sub(); }
97 
98  const operandst &operands() const
99  { return (const operandst &)get_sub(); }
100 
101 protected:
103  { return operands().front(); }
104 
106  { return operands()[1]; }
107 
109  { return operands()[2]; }
110 
112  { return operands()[3]; }
113 
114  const exprt &op0() const
115  { return operands().front(); }
116 
117  const exprt &op1() const
118  { return operands()[1]; }
119 
120  const exprt &op2() const
121  { return operands()[2]; }
122 
123  const exprt &op3() const
124  { return operands()[3]; }
125 
126 public:
128  { operands().reserve(n) ; }
129 
130  DEPRECATED(SINCE(2018, 10, 1, "use add_to_operands(std::move(expr)) instead"))
131  void move_to_operands(exprt &expr);
132 
134  2018,
135  10,
136  1,
137  "use add_to_operands(std::move(e1), std::move(e2)) instead"))
138  void move_to_operands(exprt &e1, exprt &e2);
139 
141  2018,
142  10,
143  1,
144  "use add_to_operands(std::move(e1), std::move(e2), std::move(e3))"
145  "instead"))
146  void move_to_operands(exprt &e1, exprt &e2, exprt &e3);
147 
150  void copy_to_operands(const exprt &expr)
151  {
152  operands().push_back(expr);
153  }
154 
157  void add_to_operands(const exprt &expr)
158  {
159  copy_to_operands(expr);
160  }
161 
164  void add_to_operands(exprt &&expr)
165  {
166  operands().push_back(std::move(expr));
167  }
168 
172  void copy_to_operands(const exprt &e1, const exprt &e2)
173  {
174  operandst &op = operands();
175  #ifndef USE_LIST
176  op.reserve(op.size() + 2);
177  #endif
178  op.push_back(e1);
179  op.push_back(e2);
180  }
181 
185  void add_to_operands(const exprt &e1, const exprt &e2)
186  {
187  copy_to_operands(e1, e2);
188  }
189 
193  void add_to_operands(exprt &&e1, exprt &&e2)
194  {
195  operandst &op = operands();
196  #ifndef USE_LIST
197  op.reserve(op.size() + 2);
198  #endif
199  op.push_back(std::move(e1));
200  op.push_back(std::move(e2));
201  }
202 
207  void add_to_operands(const exprt &e1, const exprt &e2, const exprt &e3)
208  {
209  copy_to_operands(e1, e2, e3);
210  }
211 
216  void copy_to_operands(const exprt &e1, const exprt &e2, const exprt &e3)
217  {
218  operandst &op = operands();
219  #ifndef USE_LIST
220  op.reserve(op.size() + 3);
221  #endif
222  op.push_back(e1);
223  op.push_back(e2);
224  op.push_back(e3);
225  }
226 
231  void add_to_operands(exprt &&e1, exprt &&e2, exprt &&e3)
232  {
233  operandst &op = operands();
234  #ifndef USE_LIST
235  op.reserve(op.size() + 3);
236  #endif
237  op.push_back(std::move(e1));
238  op.push_back(std::move(e2));
239  op.push_back(std::move(e3));
240  }
241 
242  DEPRECATED(SINCE(2019, 5, 28, "use make_boolean_expr(value) instead"))
243  void make_bool(bool value);
244 
245  bool is_constant() const;
246  bool is_true() const;
247  bool is_false() const;
248  bool is_zero() const;
249  bool is_one() const;
250  bool is_boolean() const;
251 
252  const source_locationt &find_source_location() const;
253 
255  {
256  return static_cast<const source_locationt &>(find(ID_C_source_location));
257  }
258 
260  {
261  return static_cast<source_locationt &>(add(ID_C_source_location));
262  }
263 
265  {
266  remove(ID_C_source_location);
267  }
268 
277  static void check(const exprt &, const validation_modet)
278  {
279  }
280 
289  static void validate(
290  const exprt &expr,
291  const namespacet &,
293  {
294  check_expr(expr, vm);
295  }
296 
305  static void validate_full(
306  const exprt &expr,
307  const namespacet &ns,
309  {
310  // first check operands (if any)
311  for(const exprt &op : expr.operands())
312  {
313  validate_full_expr(op, ns, vm);
314  }
315 
316  // type may be nil
317  const typet &t = expr.type();
318 
319  validate_full_type(t, ns, vm);
320 
321  validate_expr(expr, ns, vm);
322  }
323 
324 protected:
325  exprt &add_expr(const irep_idt &name)
326  {
327  return static_cast<exprt &>(add(name));
328  }
329 
330  const exprt &find_expr(const irep_idt &name) const
331  {
332  return static_cast<const exprt &>(find(name));
333  }
334 
335 public:
339  void visit(class expr_visitort &visitor);
340  void visit(class const_expr_visitort &visitor) const;
341  void visit_pre(std::function<void(exprt &)>);
342  void visit_pre(std::function<void(const exprt &)>) const;
343 
347  void visit_post(std::function<void(exprt &)>);
348  void visit_post(std::function<void(const exprt &)>) const;
349 
356  depth_iteratort depth_begin(std::function<exprt &()> mutate_root) const;
361 };
362 
366 class expr_protectedt : public exprt
367 {
368 protected:
369  // constructors
371  : exprt(std::move(_id), std::move(_type))
372  {
373  }
374 
375  expr_protectedt(irep_idt _id, typet _type, operandst _operands)
376  : exprt(std::move(_id), std::move(_type), std::move(_operands))
377  {
378  }
379 
380  // protect these low-level methods
381  using exprt::add;
382  using exprt::make_bool;
383  using exprt::op0;
384  using exprt::op1;
385  using exprt::op2;
386  using exprt::op3;
387  using exprt::remove;
388 };
389 
391 {
392 public:
393  virtual ~expr_visitort() { }
394  virtual void operator()(exprt &) { }
395 };
396 
398 {
399 public:
400  virtual ~const_expr_visitort() { }
401  virtual void operator()(const exprt &) { }
402 };
403 
404 #endif // CPROVER_UTIL_EXPR_H
exprt::copy_to_operands
void copy_to_operands(const exprt &expr)
Copy the given argument to the end of exprt's operands.
Definition: expr.h:150
exprt::op2
exprt & op2()
Definition: expr.h:108
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
exprt::move_to_operands
void move_to_operands(exprt &expr)
Move the given argument to the end of exprt's operands.
Definition: expr.cpp:41
exprt::depth_cbegin
const_depth_iteratort depth_cbegin() const
Definition: expr.cpp:339
const_unique_depth_iteratort
Definition: expr_iterator.h:285
exprt::op3
exprt & op3()
Definition: expr.h:111
exprt::find_expr
const exprt & find_expr(const irep_idt &name) const
Definition: expr.h:330
validate_full_type
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)
Definition: validate_types.cpp:103
expr_visitort::operator()
virtual void operator()(exprt &)
Definition: expr.h:394
exprt::depth_begin
depth_iteratort depth_begin()
Definition: expr.cpp:331
const_expr_visitort::~const_expr_visitort
virtual ~const_expr_visitort()
Definition: expr.h:400
exprt::size
std::size_t size() const
Amount of nodes this expression tree contains.
Definition: expr.cpp:26
typet
The type of an expression, extends irept.
Definition: type.h:28
check_expr
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...
Definition: validate_expressions.cpp:67
exprt::add_to_operands
void add_to_operands(exprt &&e1, exprt &&e2)
Add the given arguments to the end of exprt's operands.
Definition: expr.h:193
irept::add
irept & add(const irep_namet &name)
Definition: irep.cpp:113
irept::find
const irept & find(const irep_namet &name) const
Definition: irep.cpp:103
depth_iteratort
Definition: expr_iterator.h:228
exprt::make_bool
void make_bool(bool value)
Replace the expression by a Boolean expression representing value.
Definition: expr.cpp:111
exprt::exprt
exprt()
Definition: expr.h:58
exprt
Base class for all expressions.
Definition: expr.h:52
exprt::exprt
exprt(const irep_idt &_id)
Definition: expr.h:59
exprt::validate_full
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:305
exprt::op0
exprt & op0()
Definition: expr.h:102
const_expr_visitort::operator()
virtual void operator()(const exprt &)
Definition: expr.h:401
exprt::is_true
bool is_true() const
Return whether the expression is a constant representing true.
Definition: expr.cpp:92
exprt::copy_to_operands
void copy_to_operands(const exprt &e1, const exprt &e2)
Copy the given arguments to the end of exprt's operands.
Definition: expr.h:172
exprt::add_to_operands
void add_to_operands(exprt &&expr)
Add the given argument to the end of exprt's operands.
Definition: expr.h:164
exprt::visit
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:321
validate_expr
void validate_expr(const byte_extract_exprt &value)
Definition: byte_operators.h:69
expr_visitort
Definition: expr.h:390
exprt::is_false
bool is_false() const
Return whether the expression is a constant representing false.
Definition: expr.cpp:101
type.h
exprt::add_to_operands
void add_to_operands(const exprt &e1, const exprt &e2)
Add the given arguments to the end of exprt's operands.
Definition: expr.h:185
exprt::exprt
exprt(const irep_idt &id, typet type, source_locationt loc)
Definition: expr.h:74
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:91
exprt::operands
const operandst & operands() const
Definition: expr.h:98
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:81
const_depth_iteratort
Definition: expr_iterator.h:217
exprt::has_operands
bool has_operands() const
Return true if there is at least one operand.
Definition: expr.h:92
deprecate.h
exprt::visit_post
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:282
exprt::find_source_location
const source_locationt & find_source_location() const
Get a source_locationt from the expression or from its operands (non-recursively).
Definition: expr.cpp:231
expr_visitort::~expr_visitort
virtual ~expr_visitort()
Definition: expr.h:393
exprt::op1
exprt & op1()
Definition: expr.h:105
const_expr_visitort
Definition: expr.h:397
validation_modet
validation_modet
Definition: validation_mode.h:12
irept::id
const irep_idt & id() const
Definition: irep.h:418
irept::remove
void remove(const irep_namet &name)
Definition: irep.cpp:93
exprt::operandst
std::vector< exprt > operandst
Definition: expr.h:55
validation_mode.h
exprt::copy_to_operands
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:216
exprt::type
const typet & type() const
Definition: expr.h:82
SINCE
#define SINCE(year, month, day, msg)
Definition: deprecate.h:26
exprt::drop_source_location
void drop_source_location()
Definition: expr.h:264
sharing_treet< irept, std::map< irep_namet, irept > >::subt
typename dt::subt subt
Definition: irep.h:182
source_locationt
Definition: source_location.h:19
exprt::is_zero
bool is_zero() const
Return whether the expression is a constant representing 0.
Definition: expr.cpp:132
exprt::op1
const exprt & op1() const
Definition: expr.h:117
exprt::add_to_operands
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:207
exprt::check
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:277
exprt::op0
const exprt & op0() const
Definition: expr.h:114
DEPRECATED
#define DEPRECATED(msg)
Definition: deprecate.h:23
exprt::add_expr
exprt & add_expr(const irep_idt &name)
Definition: expr.h:325
exprt::add_to_operands
void add_to_operands(exprt &&e1, exprt &&e2, exprt &&e3)
Add the given arguments to the end of exprt's operands.
Definition: expr.h:231
exprt::depth_cend
const_depth_iteratort depth_cend() const
Definition: expr.cpp:341
exprt::is_constant
bool is_constant() const
Return whether the expression is a constant.
Definition: expr.cpp:85
expr_protectedt::expr_protectedt
expr_protectedt(irep_idt _id, typet _type, operandst _operands)
Definition: expr.h:375
irept::get_sub
subt & get_sub()
Definition: irep.h:477
exprt::unique_depth_cend
const_unique_depth_iteratort unique_depth_cend() const
Definition: expr.cpp:354
exprt::visit_pre
void visit_pre(std::function< void(exprt &)>)
Definition: expr.cpp:311
exprt::add_to_operands
void add_to_operands(const exprt &expr)
Add the given argument to the end of exprt's operands.
Definition: expr.h:157
validate_full_expr
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...
Definition: validate_expressions.cpp:96
exprt::exprt
exprt(irep_idt _id, typet _type, operandst &&_operands)
Definition: expr.h:66
validate_expressions.h
exprt::is_one
bool is_one() const
Return whether the expression is a constant representing 1.
Definition: expr.cpp:182
irept
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition: irep.h:381
exprt::operands
operandst & operands()
Definition: expr.h:95
expr_protectedt::expr_protectedt
expr_protectedt(irep_idt _id, typet _type)
Definition: expr.h:370
exprt::add_source_location
source_locationt & add_source_location()
Definition: expr.h:259
size_type
unsignedbv_typet size_type()
Definition: c_types.cpp:58
exprt::unique_depth_begin
const_unique_depth_iteratort unique_depth_begin() const
Definition: expr.cpp:348
validate_types.h
exprt::validate
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:289
exprt::unique_depth_cbegin
const_unique_depth_iteratort unique_depth_cbegin() const
Definition: expr.cpp:352
exprt::unique_depth_end
const_unique_depth_iteratort unique_depth_end() const
Definition: expr.cpp:350
exprt::depth_end
depth_iteratort depth_end()
Definition: expr.cpp:333
exprt::is_boolean
bool is_boolean() const
Return whether the expression represents a Boolean.
Definition: expr.cpp:119
exprt::exprt
exprt(irep_idt _id, typet _type)
Definition: expr.h:61
exprt::reserve_operands
void reserve_operands(operandst::size_type n)
Definition: expr.h:127
exprt::source_location
const source_locationt & source_location() const
Definition: expr.h:254
exprt::op3
const exprt & op3() const
Definition: expr.h:123
expr_protectedt
Base class for all expressions.
Definition: expr.h:366
validation_modet::INVARIANT
exprt::op2
const exprt & op2() const
Definition: expr.h:120