Skip to content

pre/post traversal expression transformers #4891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/util/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,63 @@ void exprt::visit_post(std::function<void(const exprt &)> visitor) const
visit_post_template(visitor, this);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit message: "Transformers for expressions that offer an interface similar to that offered
by the goto-program API." There isn't enough context available to understand why what looks like a comparison of apples and oranges is useful to the reader.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

}

optionalt<exprt>
exprt::transform_pre(std::function<optionalt<exprt>(exprt)> visitor) const
{
auto visitor_result = visitor(*this);

// make a copy
exprt tmp = visitor_result.value_or(*this);

bool op_changed = false;

for(auto &op : tmp.operands()) // this breaks sharing of the copy
{
auto op_result = op.transform_pre(visitor);
if(op_result.has_value())
{
op = std::move(op_result.value());
op_changed = true;
}
}

if(op_changed)
return std::move(tmp);
else
return visitor_result;
}

optionalt<exprt>
exprt::transform_post(std::function<optionalt<exprt>(exprt)> visitor) const
{
// make a copy
exprt tmp = *this;

bool op_changed = false;

for(auto &op : tmp.operands()) // this breaks sharing of the copy
{
auto op_result = op.transform_post(visitor);
if(op_result.has_value())
{
op = std::move(op_result.value());
op_changed = true;
}
}

if(op_changed)
{
auto visitor_result = visitor(tmp);

if(visitor_result.has_value())
return std::move(visitor_result.value());
else
return std::move(tmp);
}
else
return visitor(*this);
}

template <typename T>
static void visit_pre_template(std::function<void(T &)> visitor, T *_expr)
{
Expand Down
2 changes: 2 additions & 0 deletions src/util/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,14 @@ class exprt:public irept
void visit(class const_expr_visitort &visitor) const;
void visit_pre(std::function<void(exprt &)>);
void visit_pre(std::function<void(const exprt &)>) const;
optionalt<exprt> transform_pre(std::function<optionalt<exprt>(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;
optionalt<exprt> transform_post(std::function<optionalt<exprt>(exprt)>) const;

depth_iteratort depth_begin();
depth_iteratort depth_end();
Expand Down