CBMC
Loading...
Searching...
No Matches
expr.h
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: 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 "deprecate.h"
14#include "type.h"
16#include "validate_types.h"
17#include "validation_mode.h"
18
19#include <functional>
20
21#define forall_operands(it, expr) \
22 for(exprt::operandst::const_iterator \
23 it = as_const(expr).operands().begin(), \
24 it##_end = as_const(expr).operands().end(); \
25 it != it##_end; \
26 ++it)
27
28#define Forall_operands(it, expr) \
29 if((expr).has_operands()) /* NOLINT(readability/braces) */ \
30 for(exprt::operandst::iterator it=(expr).operands().begin(); \
31 it!=(expr).operands().end(); ++it)
32
33#define forall_expr(it, expr) \
34 for(exprt::operandst::const_iterator it=(expr).begin(); \
35 it!=(expr).end(); ++it)
36
37#define Forall_expr(it, expr) \
38 for(exprt::operandst::iterator it=(expr).begin(); \
39 it!=(expr).end(); ++it)
40
41class depth_iteratort;
44
56class exprt:public irept
57{
58public:
59 typedef std::vector<exprt> operandst;
60
61 // constructors
62 exprt() { }
63 explicit exprt(const irep_idt &_id):irept(_id) { }
64
66 : irept(std::move(_id), {{ID_type, std::move(_type)}}, {})
67 {
68 }
69
71 : irept(
72 std::move(_id),
73 {{ID_type, std::move(_type)}},
74 std::move((irept::subt &&) _operands))
75 {
76 }
77
79 : exprt(id, std::move(type))
80 {
81 add_source_location() = std::move(loc);
82 }
83
85 typet &type() { return static_cast<typet &>(add(ID_type)); }
86 const typet &type() const
87 {
88 return static_cast<const typet &>(find(ID_type));
89 }
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
103 {
104 if(location.is_not_nil())
105 add_source_location() = std::move(location);
106 return *this;
107 }
108
111 {
112 if(location.is_not_nil())
113 add_source_location() = std::move(location);
114 return std::move(*this);
115 }
116
119 {
120 if(other.source_location().is_not_nil())
121 add_source_location() = other.source_location();
122 return *this;
123 }
124
127 {
128 if(other.source_location().is_not_nil())
129 add_source_location() = other.source_location();
130 return std::move(*this);
131 }
132
133protected:
135 { return operands().front(); }
136
138 { return operands()[1]; }
139
141 { return operands()[2]; }
142
144 { return operands()[3]; }
145
146 const exprt &op0() const
147 { return operands().front(); }
148
149 const exprt &op1() const
150 { return operands()[1]; }
151
152 const exprt &op2() const
153 { return operands()[2]; }
154
155 const exprt &op3() const
156 { return operands()[3]; }
157
158public:
159 void reserve_operands(operandst::size_type n)
160 { operands().reserve(n) ; }
161
164 void copy_to_operands(const exprt &expr)
165 {
166 operands().push_back(expr);
167 }
168
171 void add_to_operands(const exprt &expr)
172 {
173 copy_to_operands(expr);
174 }
175
179 {
180 operands().push_back(std::move(expr));
181 }
182
187 {
188 operandst &op = operands();
189 #ifndef USE_LIST
190 op.reserve(op.size() + 2);
191 #endif
192 op.push_back(std::move(e1));
193 op.push_back(std::move(e2));
194 }
195
201 {
202 operandst &op = operands();
203 #ifndef USE_LIST
204 op.reserve(op.size() + 3);
205 #endif
206 op.push_back(std::move(e1));
207 op.push_back(std::move(e2));
208 op.push_back(std::move(e3));
209 }
210
213 bool is_constant() const
214 {
215 return id() == ID_constant;
216 }
217
218 DEPRECATED(SINCE(2025, 7, 5, "use expr == true instead"))
220 DEPRECATED(SINCE(2025, 7, 5, "use expr == false instead"))
221 bool is_false() const;
222 DEPRECATED(SINCE(2025, 7, 5, "use expr == 0 instead"))
223 bool is_zero() const;
224 DEPRECATED(SINCE(2025, 7, 5, "use expr == 1 instead"))
225 bool is_one() const;
226
229 bool is_boolean() const
230 {
231 return type().id() == ID_bool;
232 }
233
235
237 {
238 return static_cast<const source_locationt &>(find(ID_C_source_location));
239 }
240
245
250
259 static void check(const exprt &, const validation_modet)
260 {
261 }
262
271 static void validate(
272 const exprt &expr,
273 const namespacet &,
275 {
276 check_expr(expr, vm);
277 }
278
287 static void validate_full(
288 const exprt &expr,
289 const namespacet &ns,
291 {
292 // first check operands (if any)
293 for(const exprt &op : expr.operands())
294 {
295 validate_full_expr(op, ns, vm);
296 }
297
298 // type may be nil
299 const typet &t = expr.type();
300
301 validate_full_type(t, ns, vm);
302
303 validate_expr(expr, ns, vm);
304 }
305
306protected:
307 exprt &add_expr(const irep_idt &name)
308 {
309 return static_cast<exprt &>(add(name));
310 }
311
312 const exprt &find_expr(const irep_idt &name) const
313 {
314 return static_cast<const exprt &>(find(name));
315 }
316
317public:
321 void visit(class expr_visitort &visitor);
322 void visit(class const_expr_visitort &visitor) const;
323 void visit_pre(std::function<void(exprt &)>);
324 void visit_pre(std::function<void(const exprt &)>) const;
325
329 void visit_post(std::function<void(exprt &)>);
330 void visit_post(std::function<void(const exprt &)>) const;
331
338 depth_iteratort depth_begin(std::function<exprt &()> mutate_root) const;
343};
344
348class expr_protectedt : public exprt
349{
350protected:
351 // constructors
353 : exprt(std::move(_id), std::move(_type))
354 {
355 }
356
358 : exprt(std::move(_id), std::move(_type), std::move(_operands))
359 {
360 }
361
362 // protect these low-level methods
363 using exprt::add;
364 using exprt::op0;
365 using exprt::op1;
366 using exprt::op2;
367 using exprt::op3;
368 using exprt::remove;
369};
370
372{
373public:
374 virtual ~expr_visitort() { }
375 virtual void operator()(exprt &) { }
376};
377
379{
380public:
382 virtual void operator()(const exprt &) { }
383};
384
385#endif // CPROVER_UTIL_EXPR_H
void validate_expr(const shuffle_vector_exprt &value)
Definition c_expr.h:82
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
virtual void operator()(const exprt &)
Definition expr.h:382
virtual ~const_expr_visitort()
Definition expr.h:381
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:38
Base class for all expressions.
Definition expr.h:349
expr_protectedt(irep_idt _id, typet _type)
Definition expr.h:352
expr_protectedt(irep_idt _id, typet _type, operandst _operands)
Definition expr.h:357
virtual void operator()(exprt &)
Definition expr.h:375
virtual ~expr_visitort()
Definition expr.h:374
Base class for all expressions.
Definition expr.h:57
exprt & op3()
Definition expr.h:143
const source_locationt & find_source_location() const
Get a source_locationt from the expression or from its operands (non-recursively).
Definition expr.cpp:68
std::vector< exprt > operandst
Definition expr.h:59
const typet & type() const
Definition expr.h:86
bool is_one() const
Return whether the expression is a constant representing 1.
Definition expr.cpp:58
bool has_operands() const
Return true if there is at least one operand.
Definition expr.h:92
void add_to_operands(exprt &&e1, exprt &&e2, exprt &&e3)
Add the given arguments to the end of exprt's operands.
Definition expr.h:200
bool is_true() const
Return whether the expression is a constant representing true.
Definition expr.cpp:27
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:271
void copy_to_operands(const exprt &expr)
Copy the given argument to the end of exprt's operands.
Definition expr.h:164
depth_iteratort depth_end()
Definition expr.cpp:170
const_depth_iteratort depth_cend() const
Definition expr.cpp:178
exprt && with_source_location(source_locationt location) &&
Add the source location from location, if it is non-nil.
Definition expr.h:110
exprt()
Definition expr.h:62
const_unique_depth_iteratort unique_depth_end() const
Definition expr.cpp:187
bool is_boolean() const
Return whether the expression represents a Boolean.
Definition expr.h:229
depth_iteratort depth_begin()
Definition expr.cpp:168
const_unique_depth_iteratort unique_depth_cend() const
Definition expr.cpp:191
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:158
bool is_false() const
Return whether the expression is a constant representing false.
Definition expr.cpp:34
void reserve_operands(operandst::size_type n)
Definition expr.h:159
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:259
bool is_zero() const
Return whether the expression is a constant representing 0.
Definition expr.cpp:47
void visit_pre(std::function< void(exprt &)>)
Definition expr.cpp:148
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:287
exprt(irep_idt _id, typet _type)
Definition expr.h:65
exprt & add_expr(const irep_idt &name)
Definition expr.h:307
const exprt & op0() const
Definition expr.h:146
exprt & op0()
Definition expr.h:134
exprt(irep_idt _id, typet _type, operandst &&_operands)
Definition expr.h:70
exprt & op1()
Definition expr.h:137
bool is_constant() const
Return whether the expression is a constant.
Definition expr.h:213
exprt && with_source_location(const exprt &other) &&
Add the source location from other, if it has any.
Definition expr.h:126
typet & type()
Return the type of the expression.
Definition expr.h:85
exprt(const irep_idt &_id)
Definition expr.h:63
const_depth_iteratort depth_cbegin() const
Definition expr.cpp:176
const exprt & find_expr(const irep_idt &name) const
Definition expr.h:312
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:119
operandst & operands()
Definition expr.h:95
exprt & with_source_location(const exprt &other) &
Add the source location from other, if it has any.
Definition expr.h:118
const operandst & operands() const
Definition expr.h:98
void add_to_operands(exprt &&e1, exprt &&e2)
Add the given arguments to the end of exprt's operands.
Definition expr.h:186
void add_to_operands(exprt &&expr)
Add the given argument to the end of exprt's operands.
Definition expr.h:178
exprt(const irep_idt &id, typet type, source_locationt loc)
Definition expr.h:78
exprt & op2()
Definition expr.h:140
const source_locationt & source_location() const
Definition expr.h:236
source_locationt & add_source_location()
Definition expr.h:241
void drop_source_location()
Definition expr.h:246
const exprt & op3() const
Definition expr.h:155
const_unique_depth_iteratort unique_depth_cbegin() const
Definition expr.cpp:189
const exprt & op2() const
Definition expr.h:152
void add_to_operands(const exprt &expr)
Add the given argument to the end of exprt's operands.
Definition expr.h:171
const_unique_depth_iteratort unique_depth_begin() const
Definition expr.cpp:185
const exprt & op1() const
Definition expr.h:149
exprt & with_source_location(source_locationt location) &
Add the source location from location, if it is non-nil.
Definition expr.h:102
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition irep.h:364
const irept & find(const irep_idt &name) const
Definition irep.cpp:93
void remove(const irep_idt &name)
Definition irep.cpp:87
subt & get_sub()
Definition irep.h:448
const irep_idt & id() const
Definition irep.h:388
irept & add(const irep_idt &name)
Definition irep.cpp:103
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition namespace.h:91
The type of an expression, extends irept.
Definition type.h:29
#define SINCE(year, month, day, msg)
Definition deprecate.h:26
#define DEPRECATED(msg)
Definition deprecate.h:23
STL namespace.
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