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 
102  { return operands().front(); }
103 
105  { return operands()[1]; }
106 
108  { return operands()[2]; }
109 
111  { return operands()[3]; }
112 
113  const exprt &op0() const
114  { return operands().front(); }
115 
116  const exprt &op1() const
117  { return operands()[1]; }
118 
119  const exprt &op2() const
120  { return operands()[2]; }
121 
122  const exprt &op3() const
123  { return operands()[3]; }
124 
126  { operands().reserve(n) ; }
127 
128  DEPRECATED(SINCE(2018, 10, 1, "use add_to_operands(std::move(expr)) instead"))
129  void move_to_operands(exprt &expr);
130 
132  2018,
133  10,
134  1,
135  "use add_to_operands(std::move(e1), std::move(e2)) instead"))
136  void move_to_operands(exprt &e1, exprt &e2);
137 
139  2018,
140  10,
141  1,
142  "use add_to_operands(std::move(e1), std::move(e2), std::move(e3))"
143  "instead"))
144  void move_to_operands(exprt &e1, exprt &e2, exprt &e3);
145 
148  void copy_to_operands(const exprt &expr)
149  {
150  operands().push_back(expr);
151  }
152 
155  void add_to_operands(const exprt &expr)
156  {
157  copy_to_operands(expr);
158  }
159 
162  void add_to_operands(exprt &&expr)
163  {
164  operands().push_back(std::move(expr));
165  }
166 
170  void copy_to_operands(const exprt &e1, const exprt &e2)
171  {
172  operandst &op = operands();
173  #ifndef USE_LIST
174  op.reserve(op.size() + 2);
175  #endif
176  op.push_back(e1);
177  op.push_back(e2);
178  }
179 
183  void add_to_operands(const exprt &e1, const exprt &e2)
184  {
185  copy_to_operands(e1, e2);
186  }
187 
191  void add_to_operands(exprt &&e1, exprt &&e2)
192  {
193  operandst &op = operands();
194  #ifndef USE_LIST
195  op.reserve(op.size() + 2);
196  #endif
197  op.push_back(std::move(e1));
198  op.push_back(std::move(e2));
199  }
200 
205  void add_to_operands(const exprt &e1, const exprt &e2, const exprt &e3)
206  {
207  copy_to_operands(e1, e2, e3);
208  }
209 
214  void copy_to_operands(const exprt &e1, const exprt &e2, const exprt &e3)
215  {
216  operandst &op = operands();
217  #ifndef USE_LIST
218  op.reserve(op.size() + 3);
219  #endif
220  op.push_back(e1);
221  op.push_back(e2);
222  op.push_back(e3);
223  }
224 
229  void add_to_operands(exprt &&e1, exprt &&e2, exprt &&e3)
230  {
231  operandst &op = operands();
232  #ifndef USE_LIST
233  op.reserve(op.size() + 3);
234  #endif
235  op.push_back(std::move(e1));
236  op.push_back(std::move(e2));
237  op.push_back(std::move(e3));
238  }
239 
240  DEPRECATED(SINCE(2019, 5, 28, "use make_boolean_expr(value) instead"))
241  void make_bool(bool value);
242 
243  bool is_constant() const;
244  bool is_true() const;
245  bool is_false() const;
246  bool is_zero() const;
247  bool is_one() const;
248  bool is_boolean() const;
249 
250  const source_locationt &find_source_location() const;
251 
253  {
254  return static_cast<const source_locationt &>(find(ID_C_source_location));
255  }
256 
258  {
259  return static_cast<source_locationt &>(add(ID_C_source_location));
260  }
261 
263  {
264  remove(ID_C_source_location);
265  }
266 
275  static void check(const exprt &, const validation_modet)
276  {
277  }
278 
287  static void validate(
288  const exprt &expr,
289  const namespacet &,
291  {
292  check_expr(expr, vm);
293  }
294 
303  static void validate_full(
304  const exprt &expr,
305  const namespacet &ns,
307  {
308  // first check operands (if any)
309  for(const exprt &op : expr.operands())
310  {
311  validate_full_expr(op, ns, vm);
312  }
313 
314  // type may be nil
315  const typet &t = expr.type();
316 
317  validate_full_type(t, ns, vm);
318 
319  validate_expr(expr, ns, vm);
320  }
321 
322 protected:
323  exprt &add_expr(const irep_idt &name)
324  {
325  return static_cast<exprt &>(add(name));
326  }
327 
328  const exprt &find_expr(const irep_idt &name) const
329  {
330  return static_cast<const exprt &>(find(name));
331  }
332 
333 public:
337  void visit(class expr_visitort &visitor);
338  void visit(class const_expr_visitort &visitor) const;
339  void visit_pre(std::function<void(exprt &)>);
340  void visit_pre(std::function<void(const exprt &)>) const;
341 
345  void visit_post(std::function<void(exprt &)>);
346  void visit_post(std::function<void(const exprt &)>) const;
347 
354  depth_iteratort depth_begin(std::function<exprt &()> mutate_root) const;
359 };
360 
364 class expr_protectedt : public exprt
365 {
366 protected:
367  // constructors
369  : exprt(std::move(_id), std::move(_type))
370  {
371  }
372 
373  expr_protectedt(irep_idt _id, typet _type, operandst _operands)
374  : exprt(std::move(_id), std::move(_type), std::move(_operands))
375  {
376  }
377 
378  // protect these low-level methods
379  using exprt::add;
380  using exprt::make_bool;
381  using exprt::op0;
382  using exprt::op1;
383  using exprt::op2;
384  using exprt::op3;
385  using exprt::remove;
386 };
387 
389 {
390 public:
391  virtual ~expr_visitort() { }
392  virtual void operator()(exprt &) { }
393 };
394 
396 {
397 public:
398  virtual ~const_expr_visitort() { }
399  virtual void operator()(const exprt &) { }
400 };
401 
402 #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:148
exprt::op2
exprt & op2()
Definition: expr.h:107
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
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:284
exprt::op3
exprt & op3()
Definition: expr.h:110
exprt::find_expr
const exprt & find_expr(const irep_idt &name) const
Definition: expr.h:328
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:392
exprt::depth_begin
depth_iteratort depth_begin()
Definition: expr.cpp:331
const_expr_visitort::~const_expr_visitort
virtual ~const_expr_visitort()
Definition: expr.h:398
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:29
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:191
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:227
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:53
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:303
exprt::op0
exprt & op0()
Definition: expr.h:101
const_expr_visitort::operator()
virtual void operator()(const exprt &)
Definition: expr.h:399
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:170
exprt::add_to_operands
void add_to_operands(exprt &&expr)
Add the given argument to the end of exprt's operands.
Definition: expr.h:162
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:66
expr_visitort
Definition: expr.h:389
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:183
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:92
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:216
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:391
exprt::op1
exprt & op1()
Definition: expr.h:104
const_expr_visitort
Definition: expr.h:396
validation_modet
validation_modet
Definition: validation_mode.h:13
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:214
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:262
sharing_treet< irept, std::map< irep_namet, irept > >::subt
typename dt::subt subt
Definition: irep.h:182
source_locationt
Definition: source_location.h:20
exprt::is_zero
bool is_zero() const
Return whether the expression is a constant representing 0.
Definition: expr.cpp:132
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:205
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:275
DEPRECATED
#define DEPRECATED(msg)
Definition: deprecate.h:23
exprt::add_expr
exprt & add_expr(const irep_idt &name)
Definition: expr.h:323
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:229
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:373
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:155
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
loc
#define loc()
Definition: ansi_c_lex.yy.cpp:4684
irept
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition: irep.h:394
exprt::operands
operandst & operands()
Definition: expr.h:95
expr_protectedt::expr_protectedt
expr_protectedt(irep_idt _id, typet _type)
Definition: expr.h:368
exprt::add_source_location
source_locationt & add_source_location()
Definition: expr.h:257
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:287
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:125
exprt::source_location
const source_locationt & source_location() const
Definition: expr.h:252
expr_protectedt
Base class for all expressions.
Definition: expr.h:365
validation_modet::INVARIANT
@ INVARIANT