forked from diffblue/cbmc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexpr.h
380 lines (318 loc) · 10.6 KB
/
expr.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*******************************************************************\
Module:
Author: Daniel Kroening, [email protected]
\*******************************************************************/
#ifndef CPROVER_UTIL_EXPR_H
#define CPROVER_UTIL_EXPR_H
#include "as_const.h"
#include "type.h"
#include "validate_expressions.h"
#include "validate_types.h"
#include "validation_mode.h"
#include <functional>
#define forall_operands(it, expr) \
for(exprt::operandst::const_iterator \
it = as_const(expr).operands().begin(), \
it##_end = as_const(expr).operands().end(); \
it != it##_end; \
++it)
#define Forall_operands(it, expr) \
if((expr).has_operands()) /* NOLINT(readability/braces) */ \
for(exprt::operandst::iterator it=(expr).operands().begin(); \
it!=(expr).operands().end(); ++it)
#define forall_expr(it, expr) \
for(exprt::operandst::const_iterator it=(expr).begin(); \
it!=(expr).end(); ++it)
#define Forall_expr(it, expr) \
for(exprt::operandst::iterator it=(expr).begin(); \
it!=(expr).end(); ++it)
class depth_iteratort;
class const_depth_iteratort;
class const_unique_depth_iteratort;
/// Base class for all expressions.
/// Inherits from \ref irept and has operands (stored as unnamed children of
/// `irept`), and a type (which is a named sub with identifier `ID_type`).
/// The class contains functions to access and modify the operands, as well as
/// visitor utilities.
///
/// The example below shows the irept structure of the sum of integers
/// 3 and 5.
/// \dotfile expr.dot
/// Some context available here: \ref exprt_section
/// "exprt section in util module"
class exprt:public irept
{
public:
typedef std::vector<exprt> operandst;
// constructors
exprt() { }
explicit exprt(const irep_idt &_id):irept(_id) { }
exprt(irep_idt _id, typet _type)
: irept(std::move(_id), {{ID_type, std::move(_type)}}, {})
{
}
exprt(irep_idt _id, typet _type, operandst &&_operands)
: irept(
std::move(_id),
{{ID_type, std::move(_type)}},
std::move((irept::subt &&) _operands))
{
}
exprt(const irep_idt &id, typet type, source_locationt loc)
: exprt(id, std::move(type))
{
add_source_location() = std::move(loc);
}
/// Return the type of the expression
typet &type() { return static_cast<typet &>(add(ID_type)); }
const typet &type() const
{
return static_cast<const typet &>(find(ID_type));
}
/// Return true if there is at least one operand.
bool has_operands() const
{ return !operands().empty(); }
operandst &operands()
{ return (operandst &)get_sub(); }
const operandst &operands() const
{ return (const operandst &)get_sub(); }
/// Add the source location from \p location, if it is non-nil.
exprt &with_source_location(source_locationt location) &
{
if(location.is_not_nil())
add_source_location() = std::move(location);
return *this;
}
/// Add the source location from \p location, if it is non-nil.
exprt &&with_source_location(source_locationt location) &&
{
if(location.is_not_nil())
add_source_location() = std::move(location);
return std::move(*this);
}
/// Add the source location from \p other, if it has any.
exprt &with_source_location(const exprt &other) &
{
if(other.source_location().is_not_nil())
add_source_location() = other.source_location();
return *this;
}
/// Add the source location from \p other, if it has any.
exprt &&with_source_location(const exprt &other) &&
{
if(other.source_location().is_not_nil())
add_source_location() = other.source_location();
return std::move(*this);
}
protected:
exprt &op0()
{ return operands().front(); }
exprt &op1()
{ return operands()[1]; }
exprt &op2()
{ return operands()[2]; }
exprt &op3()
{ return operands()[3]; }
const exprt &op0() const
{ return operands().front(); }
const exprt &op1() const
{ return operands()[1]; }
const exprt &op2() const
{ return operands()[2]; }
const exprt &op3() const
{ return operands()[3]; }
public:
void reserve_operands(operandst::size_type n)
{ operands().reserve(n) ; }
/// Copy the given argument to the end of `exprt`'s operands.
/// \param expr: `exprt` to append to the operands
void copy_to_operands(const exprt &expr)
{
operands().push_back(expr);
}
/// Add the given argument to the end of `exprt`'s operands.
/// \param expr: `exprt` to append to the operands
void add_to_operands(const exprt &expr)
{
copy_to_operands(expr);
}
/// Add the given argument to the end of `exprt`'s operands.
/// \param expr: `exprt` to append to the operands
void add_to_operands(exprt &&expr)
{
operands().push_back(std::move(expr));
}
/// Add the given arguments to the end of `exprt`'s operands.
/// \param e1: first `exprt` to append to the operands
/// \param e2: second `exprt` to append to the operands
void add_to_operands(exprt &&e1, exprt &&e2)
{
operandst &op = operands();
#ifndef USE_LIST
op.reserve(op.size() + 2);
#endif
op.push_back(std::move(e1));
op.push_back(std::move(e2));
}
/// Add the given arguments to the end of `exprt`'s operands.
/// \param e1: first `exprt` to append to the operands
/// \param e2: second `exprt` to append to the operands
/// \param e3: third `exprt` to append to the operands
void add_to_operands(exprt &&e1, exprt &&e2, exprt &&e3)
{
operandst &op = operands();
#ifndef USE_LIST
op.reserve(op.size() + 3);
#endif
op.push_back(std::move(e1));
op.push_back(std::move(e2));
op.push_back(std::move(e3));
}
/// Return whether the expression is a constant.
/// \return True if is a constant, false otherwise
bool is_constant() const
{
return id() == ID_constant;
}
bool is_true() const;
bool is_false() const;
bool is_zero() const;
bool is_one() const;
/// Return whether the expression represents a Boolean.
/// \return True if is a Boolean, false otherwise.
bool is_boolean() const
{
return type().id() == ID_bool;
}
const source_locationt &find_source_location() const;
const source_locationt &source_location() const
{
return static_cast<const source_locationt &>(find(ID_C_source_location));
}
source_locationt &add_source_location()
{
return static_cast<source_locationt &>(add(ID_C_source_location));
}
void drop_source_location()
{
remove(ID_C_source_location);
}
/// Check that the expression is well-formed (shallow checks only, i.e.,
/// subexpressions and its type are not checked).
///
/// Subclasses may override this function to provide specific well-formedness
/// checks for the corresponding expressions.
///
/// The validation mode indicates whether well-formedness check failures are
/// reported via DATA_INVARIANT violations or exceptions.
static void check(const exprt &, const validation_modet)
{
}
/// Check that the expression is well-formed, assuming that its subexpressions
/// and type have all ready been checked for well-formedness.
///
/// Subclasses may override this function to provide specific well-formedness
/// checks for the corresponding expressions.
///
/// The validation mode indicates whether well-formedness check failures are
/// reported via DATA_INVARIANT violations or exceptions.
static void validate(
const exprt &expr,
const namespacet &,
const validation_modet vm = validation_modet::INVARIANT)
{
check_expr(expr, vm);
}
/// Check that the expression is well-formed (full check, including checks
/// of all subexpressions and the type)
///
/// Subclasses may override this function, though in most cases the provided
/// implementation should be sufficient.
///
/// The validation mode indicates whether well-formedness check failures are
/// reported via DATA_INVARIANT violations or exceptions.
static void validate_full(
const exprt &expr,
const namespacet &ns,
const validation_modet vm = validation_modet::INVARIANT)
{
// first check operands (if any)
for(const exprt &op : expr.operands())
{
validate_full_expr(op, ns, vm);
}
// type may be nil
const typet &t = expr.type();
validate_full_type(t, ns, vm);
validate_expr(expr, ns, vm);
}
protected:
exprt &add_expr(const irep_idt &name)
{
return static_cast<exprt &>(add(name));
}
const exprt &find_expr(const irep_idt &name) const
{
return static_cast<const exprt &>(find(name));
}
public:
/// These are pre-order traversal visitors, i.e.,
/// the visitor is executed on a node _before_ its children
/// have been visited.
void visit(class expr_visitort &visitor);
void visit(class const_expr_visitort &visitor) const;
void visit_pre(std::function<void(exprt &)>);
void visit_pre(std::function<void(const exprt &)>) const;
/// These are post-order traversal visitors, i.e.,
/// the visitor is executed on a node _after_ its children
/// have been visited.
void visit_post(std::function<void(exprt &)>);
void visit_post(std::function<void(const exprt &)>) const;
depth_iteratort depth_begin();
depth_iteratort depth_end();
const_depth_iteratort depth_begin() const;
const_depth_iteratort depth_end() const;
const_depth_iteratort depth_cbegin() const;
const_depth_iteratort depth_cend() const;
depth_iteratort depth_begin(std::function<exprt &()> mutate_root) const;
const_unique_depth_iteratort unique_depth_begin() const;
const_unique_depth_iteratort unique_depth_end() const;
const_unique_depth_iteratort unique_depth_cbegin() const;
const_unique_depth_iteratort unique_depth_cend() const;
};
/// Base class for all expressions. This protects low-level methods in
/// exprt that are not type safe. Depcrecated constructors are removed.
/// This API will eventually replace exprt.
class expr_protectedt : public exprt
{
protected:
// constructors
expr_protectedt(irep_idt _id, typet _type)
: exprt(std::move(_id), std::move(_type))
{
}
expr_protectedt(irep_idt _id, typet _type, operandst _operands)
: exprt(std::move(_id), std::move(_type), std::move(_operands))
{
}
// protect these low-level methods
using exprt::add;
using exprt::op0;
using exprt::op1;
using exprt::op2;
using exprt::op3;
using exprt::remove;
};
class expr_visitort
{
public:
virtual ~expr_visitort() { }
virtual void operator()(exprt &) { }
};
class const_expr_visitort
{
public:
virtual ~const_expr_visitort() { }
virtual void operator()(const exprt &) { }
};
#endif // CPROVER_UTIL_EXPR_H