ROSE 0.11.145.192
Enumerations | Functions
Rose::AST::Traversal Namespace Reference

Description

Abstract syntax tree traversals.

Enumerations

enum class  Order {
  PRE = 0x01 ,
  POST = 0x02
}
 Order that visitor is called for node w.r.t. More...
 

Functions

template<class T , class Functor >
void forward (SgNode *ast, Functor functor)
 Traverse an AST in a forward direction.
 
template<class T , class Functor >
void forwardPre (SgNode *ast, Functor functor)
 Traverse an AST in a forward direction with pre-visit only.
 
template<class T , class Functor >
void forwardPost (SgNode *ast, Functor functor)
 Traverse an AST in a forward direction with post-visit only.
 
template<class T , class Functor >
void reverse (SgNode *ast, Functor functor)
 Traverse an AST in the reverse direction.
 
template<class T , class Functor >
void reversePre (SgNode *ast, Functor functor)
 Traverse an AST in the reverse with pre-visit only.
 
template<class T , class Functor >
void reversePost (SgNode *ast, Functor functor)
 Traverse an AST in the reverse with post-visit only.
 
template<class T , class Functor >
T * findParentSatisying (SgNode *ast, Functor functor)
 Reverse traversal that finds the first node satisfying the predicate.
 
template<class T >
T * findParentTyped (SgNode *ast)
 Find the closest parent of specified type.
 
template<class T >
std::vector< T * > findDescendantsTyped (SgNode *ast)
 Find all descendants of specified type.
 

Enumeration Type Documentation

◆ Order

enum class Rose::AST::Traversal::Order
strong

Order that visitor is called for node w.r.t.

children.

Enumerator
PRE 

Visitor is called before visiting the node's children.

POST 

Visitor is called after visiting the node's children.

Definition at line 19 of file AST/Traversal.h.

Function Documentation

◆ forward()

template<class T , class Functor >
void Rose::AST::Traversal::forward ( SgNode ast,
Functor  functor 
)

Traverse an AST in a forward direction.

This function traverses the specified ast by following the child edges of the tree. It calls specified functor at each node along the way that can be dynamically cast to a T pointer, and again when backtracking. In other words, when called for node n, this traversal invokes the functor with the arguments n and Order::PRE, then it recursively visits each child of n, then it invokes the visitor a second time with arguments n and Order::POST. The node argument of the functor is of type T*.

Example: Here's an example comparing the original AstPrePostProcessing to this newer function:

// old code from prePostTraversal.C tutorial
#include <sage3basic.h> // recursively about 600,000 lines of ROSE code plus various STL headers.
// Define the visitor
class PreAndPostOrderTraversal: public AstPrePostProcessing {
public:
void preOrderVisit(SgNode *node) override {
if (isSgForStatement(node))
std::cout <<"entering `for` loop...\n";
}
void postOrderVisit(SgNode *node) override {
if (isSgForStatement(node))
std::cout <<"leaving `for` loop...\n";
}
};
void debugForLoop(SgNode *node) {
if (node) {
PreAndPostOrderTraversal exampleTraversal;
exampleTraversal.traverse(node);
}
}
This class represents the base class for all IR nodes within Sage III.

Here's the corresponding code for the newer style traversals:

// New code replacing prePostTraversal.C tutorial
#include <Rose/AST/Traversal.h> // recursively includes a few hundred lines of code
void debugForLoop(SgNode *node) {
Rose::AST::Traversal::forward<SgForStatement>(node, [](SgForStatement*, Rose::AST::Traversal::Order order) {
std::cout <<(Rose::AST::Traversal::Order::PRE == order ? "entering" : "leaving") <<" for loop...\n";
});
}
This class represents the concept of a for loop.
Order
Order that visitor is called for node w.r.t.
@ PRE
Visitor is called before visiting the node's children.

Definition at line 183 of file AST/Traversal.h.

◆ forwardPre()

template<class T , class Functor >
void Rose::AST::Traversal::forwardPre ( SgNode ast,
Functor  functor 
)

Traverse an AST in a forward direction with pre-visit only.

This function is similar to the plain forward traversal but instead of calling the functor both before and after visiting a node's children, this traversal calls the functor only before visiting the children. The functor is called with one argument: a non-null pointer to the node being visited, and this argument is of type T*.

Definition at line 193 of file AST/Traversal.h.

◆ forwardPost()

template<class T , class Functor >
void Rose::AST::Traversal::forwardPost ( SgNode ast,
Functor  functor 
)

Traverse an AST in a forward direction with post-visit only.

This function is similar to the plain forward traversal but instead of calling the functor both before and after visiting a node's children, this traversal calls the functor only after visiting the children. The functor is called with one argument: a non-null pointer to the node being visited, and this argument is of type T*.

Definition at line 203 of file AST/Traversal.h.

◆ reverse()

template<class T , class Functor >
void Rose::AST::Traversal::reverse ( SgNode ast,
Functor  functor 
)

Traverse an AST in the reverse direction.

Traverses the specified AST following the child-to-parent edges. The functor is called for each node before and after its parent is visited. The functor is called with two arguments, the node pointer and an indication of when the functor is being called. The second argument being Order::PRE before the node's parent is visited, and Order::POST after the parent is visited.

Definition at line 214 of file AST/Traversal.h.

◆ reversePre()

template<class T , class Functor >
void Rose::AST::Traversal::reversePre ( SgNode ast,
Functor  functor 
)

Traverse an AST in the reverse with pre-visit only.

This function is similar to the plain reverse traversal, but only calls the functor before visiting the node's parents. The functor takes only one argument: a pointer to the node being visited.

Definition at line 223 of file AST/Traversal.h.

◆ reversePost()

template<class T , class Functor >
void Rose::AST::Traversal::reversePost ( SgNode ast,
Functor  functor 
)

Traverse an AST in the reverse with post-visit only.

This function is similar to the plain reverse traversal, but only calls the functor after visiting the node's parents. The functor takes only one argument: a pointer to the node being visited.

Definition at line 232 of file AST/Traversal.h.

◆ findParentSatisying()

template<class T , class Functor >
T * Rose::AST::Traversal::findParentSatisying ( SgNode ast,
Functor  functor 
)

Reverse traversal that finds the first node satisfying the predicate.

This is a reverse traversal starting at the specified node and following child-to-parent edges to find the first node having dynamic type T and for which the user-specified predicate returns true. The predicate takes one argument: a pointer to a node. The return value might be the same node as the argument. If the argument is null or if no satisfying node is found, then this function returns null.

Definition at line 244 of file AST/Traversal.h.

◆ findParentTyped()

template<class T >
T * Rose::AST::Traversal::findParentTyped ( SgNode ast)

Find the closest parent of specified type.

Traverses the specified AST and returns a pointer to the closest parent of the specified type. The return value might be the argument itself. If the argument is null or if no such node type can be found, then this function returns null.

Definition at line 256 of file AST/Traversal.h.

◆ findDescendantsTyped()

template<class T >
std::vector< T * > Rose::AST::Traversal::findDescendantsTyped ( SgNode ast)

Find all descendants of specified type.

Traverses the specified AST in depth-first pre-order and builds a vector that points to all the nodes of the tree that are of the specified type. All returned pointers are non-null. The specified root of the AST might be one of the pointers returned.

Definition at line 269 of file AST/Traversal.h.