CBMC
Loading...
Searching...
No Matches
expr.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Expression Representation
4
5Author: Daniel Kroening, kroening@kroening.com
6 Joel Allred, joel.allred@diffblue.com
7
8\*******************************************************************/
9
12
13#include "arith_tools.h"
14#include "bitvector_types.h"
15#include "expr_iterator.h"
16#include "expr_util.h"
17#include "fixedbv.h"
18#include "ieee_float.h"
19#include "rational.h"
20#include "rational_tools.h"
21#include "std_expr.h"
22
23#include <stack>
24
27bool exprt::is_true() const
28{
29 return *this == true;
30}
31
34bool exprt::is_false() const
35{
36 return *this == false;
37}
38
47bool exprt::is_zero() const
48{
49 return *this == 0;
50}
51
58bool exprt::is_one() const
59{
60 return *this == 1;
61}
62
69{
71
72 if(l.is_not_nil())
73 return l;
74
75 for(const auto &op : operands())
76 {
77 const source_locationt &op_l = op.find_source_location();
78 if(op_l.is_not_nil())
79 return op_l;
80 }
81
82 return source_locationt::nil();
83}
84
85template <typename T>
86void visit_post_template(std::function<void(T &)> visitor, T *_expr)
87{
88 struct stack_entryt
89 {
90 T *e;
91 bool operands_pushed;
92 explicit stack_entryt(T *_e) : e(_e), operands_pushed(false)
93 {
94 }
95 };
96
97 std::stack<stack_entryt> stack;
98
99 stack.emplace(_expr);
100
101 while(!stack.empty())
102 {
103 auto &top = stack.top();
104 if(top.operands_pushed)
105 {
106 visitor(*top.e);
107 stack.pop();
108 }
109 else
110 {
111 // do modification of 'top' before pushing in case 'top' isn't stable
112 top.operands_pushed = true;
113 for(auto &op : top.e->operands())
114 stack.emplace(&op);
115 }
116 }
117}
118
119void exprt::visit_post(std::function<void(exprt &)> visitor)
120{
122}
123
124void exprt::visit_post(std::function<void(const exprt &)> visitor) const
125{
127}
128
129template <typename T>
130static void visit_pre_template(std::function<void(T &)> visitor, T *_expr)
131{
132 std::stack<T *> stack;
133
134 stack.push(_expr);
135
136 while(!stack.empty())
137 {
138 T &expr = *stack.top();
139 stack.pop();
140
141 visitor(expr);
142
143 for(auto &op : expr.operands())
144 stack.push(&op);
145 }
146}
147
148void exprt::visit_pre(std::function<void(exprt &)> visitor)
149{
151}
152
153void exprt::visit_pre(std::function<void(const exprt &)> visitor) const
154{
156}
157
159{
160 visit_pre([&visitor](exprt &e) { visitor(e); });
161}
162
164{
165 visit_pre([&visitor](const exprt &e) { visitor(e); });
166}
167
180depth_iteratort exprt::depth_begin(std::function<exprt &()> mutate_root) const
181{
182 return depth_iteratort(*this, std::move(mutate_root));
183}
184
Pre-defined bitvector types.
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
ait()
Definition ai.h:565
Base class for all expressions.
Definition expr.h:57
const source_locationt & find_source_location() const
Get a source_locationt from the expression or from its operands (non-recursively).
Definition expr.cpp:68
bool is_one() const
Return whether the expression is a constant representing 1.
Definition expr.cpp:58
bool is_true() const
Return whether the expression is a constant representing true.
Definition expr.cpp:27
depth_iteratort depth_end()
Definition expr.cpp:170
const_depth_iteratort depth_cend() const
Definition expr.cpp:178
const_unique_depth_iteratort unique_depth_end() const
Definition expr.cpp:187
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
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
const_depth_iteratort depth_cbegin() const
Definition expr.cpp:176
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
const source_locationt & source_location() const
Definition expr.h:236
const_unique_depth_iteratort unique_depth_cbegin() const
Definition expr.cpp:189
const_unique_depth_iteratort unique_depth_begin() const
Definition expr.cpp:185
bool is_not_nil() const
Definition irep.h:372
static const source_locationt & nil()
void visit_post_template(std::function< void(T &)> visitor, T *_expr)
Definition expr.cpp:86
static void visit_pre_template(std::function< void(T &)> visitor, T *_expr)
Definition expr.cpp:130
Forward depth-first search iterators These iterators' copy operations are expensive,...
Deprecated expression utility functions.
API to expression classes.