ROSE 0.11.145.192
Classes | Functions
sg Namespace Reference

Description

This namespace contains template functions that operate on the ROSE AST.

Classes

struct  AncestorTypeFinder
 helper class for _ancestor More...
 
struct  ConditionalEnable
 
struct  ConditionalEnable< false >
 
struct  ConstLike
 projects the constness of T1 on T2 More...
 
struct  ConstLike< const T1, T2 >
 
struct  DefaultHandler
 struct DefaultHandler More...
 
struct  DefaultTraversalSuccessors
 
struct  DispatchHandler
 struct DispatchHandler More...
 
struct  DispatchHelper
 
struct  EnableConversion
 
struct  NotNull
 experimental class for returning non-null pointers More...
 
struct  TypeRecovery
 
struct  VisitDispatcher
 

Functions

void report_error (std::string desc, const char *file=nullptr, size_t ln=0)
 
void unexpected_node (const SgNode &n, const char *file=nullptr, size_t ln=0)
 
template<class T >
T & deref (T *ptr, const char *file=0, size_t ln=0)
 dereferences an object (= checked dereference in debug mode)
 
template<class T , class U , bool = EnableConversion<U*,T*>::value || EnableConversion<T*,U*>::value>
bool operator< (NotNull< T > lhs, NotNull< U > rhs)
 
template<class RoseVisitor >
std::remove_const< typenamestd::remove_reference< RoseVisitor >::type >::type _dispatch (RoseVisitor &&rv, SgNode *n)
 
template<class RoseVisitor >
std::remove_const< typenamestd::remove_reference< RoseVisitor >::type >::type dispatch (RoseVisitor &&rv, SgNode *n)
 uncovers the type of SgNode and passes it to an function "handle" in RoseVisitor.
 
template<class RoseVisitor >
std::remove_const< typenamestd::remove_reference< RoseVisitor >::type >::type dispatch (RoseVisitor &&rv, const SgNode *n)
 
template<class AncestorNode , class QualSgNode >
AncestorNode * _ancestor (QualSgNode &n)
 implements the ancestor search
 
template<class AncestorNode >
AncestorNode * ancestor (SgNode *n)
 finds an ancestor node with a given type
 
template<class AncestorNode >
const AncestorNode * ancestor (const SgNode *n)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<class AncestorNode >
AncestorNode & ancestor (SgNode &n)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<class AncestorNode >
const AncestorNode & ancestor (const SgNode &n)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<class SageNode >
auto ancestor_path (const SgNode &n) -> SageNode *
 
template<class SageNode , class... SageNodes>
auto ancestor_path (const SgNode &n) -> decltype(ancestor_path< SageNodes... >(n))
 
template<class SageNode , class SageChild >
void swap_child (SageNode &lhs, SageNode &rhs, SageChild *(SageNode::*getter)() const, void(SageNode::*setter)(SageChild *))
 swaps children (of equal kind) between two ancestor nodes of the same type
 
template<class SageParent , class SageChild >
void linkParentChild (SageParent &parent, SageChild &child, void(SageParent::*setter)(SageChild *))
 
template<class SageNode >
SageNode * assert_sage_type (SgNode *n, const char *f=0, size_t ln=0)
 asserts that n has type SageNode
 
template<class SageNode >
const SageNode * assert_sage_type (const SgNode *n, const char *f=0, size_t ln=0)
 asserts that n has type SageNode
 
template<class SageNode >
SageNode & assert_sage_type (SgNode &n, const char *f=0, size_t ln=0)
 asserts that n has type SageNode
 
template<class SageNode >
const SageNode & assert_sage_type (const SgNode &n, const char *f=0, size_t ln=0)
 asserts that n has type SageNode
 
template<class SageNode >
auto ancestorPath (const SgNode &n) -> SageNode *
 returns the last parent in an ancestor path
 
template<class SageNode , class... SageNodes>
auto ancestorPath (const SgNode &n) -> decltype(ancestorPath< SageNodes... >(n))
 returns the last parent in an ancestor path
 
template<class SageNode >
SageNode::base_node_type & asBaseType (SageNode &n)
 returns the same node n upcasted to its base type
 
template<class SageNode >
const SageNode::base_node_type & asBaseType (const SageNode &n)
 returns the same node n upcasted to its base type
 
template<class SageNode >
SageNode::base_node_type * asBaseType (SageNode *n)
 returns the same node n upcasted to its base type
 
template<class SageNode >
const SageNode::base_node_type * asBaseType (const SageNode *n)
 returns the same node n upcasted to its base type
 

Function Documentation

◆ deref()

template<class T >
T & sg::deref ( T *  ptr,
const char *  file = 0,
size_t  ln = 0 
)

dereferences an object (= checked dereference in debug mode)

Definition at line 99 of file sageGeneric.h.

◆ operator<()

template<class T , class U , bool = EnableConversion<U*,T*>::value || EnableConversion<T*,U*>::value>
bool sg::operator< ( NotNull< T >  lhs,
NotNull< U >  rhs 
)

Definition at line 169 of file sageGeneric.h.

◆ _dispatch()

template<class RoseVisitor >
std::remove_const< typenamestd::remove_reference< RoseVisitor >::type >::type sg::_dispatch ( RoseVisitor &&  rv,
SgNode n 
)
inline

Definition at line 1244 of file sageGeneric.h.

◆ dispatch() [1/2]

template<class RoseVisitor >
std::remove_const< typenamestd::remove_reference< RoseVisitor >::type >::type sg::dispatch ( RoseVisitor &&  rv,
SgNode n 
)
inline

uncovers the type of SgNode and passes it to an function "handle" in RoseVisitor.

which should be overloaded with every possible target node. After the traversal, RoseVisitor should contain the intended return data.

Template Parameters
RoseVisitor.The visitor that will be called back with the recovered type information. It must implement "handle." The handle function with the most suitable SgNode type will get invoked.
Parameters
rvan instance of a rose visitor; ie any class with a "handle" function. Note: rv is essentially passed by value (similar to STL's for_each).
nThe root of the tree to visit. SgNode
Returns
a copy of the RoseVisitor object, that will contain the intended return data.

The following code has two classes.

  • Counter counts the number of all expression and statement nodes. It implements handlers for SgNode (not interesting nodes), for SgExpression and SgStatement (to count the nodes).
  • Traversal inherits from ASTTraversal and contains a counter. The dispatch function is invoked using a Counter object and a pointer to an AST node. Since the counter object is passed by value we need to store back the result (similar to std::for_each).
    struct Counter
    {
    size_t expr;
    size_t decl;
    Counter() : expr(0), decl(0) {}
    void handle(const SgNode&) {}
    void handle(const SgExpression&) { ++expr; }
    void handle(const SgStatement&) { ++stmt; }
    };
    struct Traversal : ASTTraversal
    {
    Counter ctr;
    void visit(SgNode* n)
    {
    ctr = sg::dispatch(ctr, n);
    }
    void run(SgNode& root)
    {
    traverse(&root, preorder);
    std::cout << "Expr/Stmt ratio = " << ratio(ctr.expr, ctr.stmt) << std::endl;
    }
    static
    float ratio(float a, float b) { return a/b; }
    };
    This class represents the notion of an expression. Expressions are derived from SgLocatedNodes,...
    This class represents the base class for all IR nodes within Sage III.
    This class represents the notion of a statement.
    std::remove_const< typenamestd::remove_reference< RoseVisitor >::type >::type dispatch(RoseVisitor &&rv, SgNode *n)
    uncovers the type of SgNode and passes it to an function "handle" in RoseVisitor.

Definition at line 1331 of file sageGeneric.h.

Referenced by _ancestor(), ancestor_path(), ancestorPath(), assert_sage_type(), assert_sage_type(), assert_sage_type(), assert_sage_type(), and SageInterface::getDeclaredType().

◆ dispatch() [2/2]

template<class RoseVisitor >
std::remove_const< typenamestd::remove_reference< RoseVisitor >::type >::type sg::dispatch ( RoseVisitor &&  rv,
const SgNode n 
)
inline

Definition at line 1340 of file sageGeneric.h.

◆ _ancestor()

template<class AncestorNode , class QualSgNode >
AncestorNode * sg::_ancestor ( QualSgNode &  n)

implements the ancestor search

see comments for AncestorTypeFinder

Definition at line 1393 of file sageGeneric.h.

References dispatch().

◆ ancestor() [1/4]

template<class AncestorNode >
AncestorNode * sg::ancestor ( SgNode n)

finds an ancestor node with a given type

the function family comes in four variants:

  • SgNode* -> AncestorNode* ( result can be nullptr )
  • const SgNode* -> const AncestorNode* ( result can be nullptr )
  • SgNode& -> AncestorNode& ( assert(false) when an ancestor of the specified type cannot be found )
  • const SgNode& -> const AncestorNode& ( assert(false) when an ancestor of the specified type cannot be found )
    const SgStatement* enclosingStatement(const SgExpression* e) { return sg::ancestor<SgStatement>(e); }

Definition at line 1419 of file sageGeneric.h.

◆ ancestor() [2/4]

template<class AncestorNode >
const AncestorNode * sg::ancestor ( const SgNode n)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 1428 of file sageGeneric.h.

◆ ancestor() [3/4]

template<class AncestorNode >
AncestorNode & sg::ancestor ( SgNode n)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 1437 of file sageGeneric.h.

◆ ancestor() [4/4]

template<class AncestorNode >
const AncestorNode & sg::ancestor ( const SgNode n)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 1447 of file sageGeneric.h.

◆ assert_sage_type() [1/4]

template<class SageNode >
SageNode * sg::assert_sage_type ( SgNode n,
const char *  f = 0,
size_t  ln = 0 
)

asserts that n has type SageNode

the ROSE assert in the following example holds b/c assert_sage_type aborts if the input node is not a SgStatement

SgStatement* stmt = assert_sage_type<SgStatement>(expr.get_parent());
ROSE_ASSERT(stmt);

Definition at line 1497 of file sageGeneric.h.

References dispatch().

◆ assert_sage_type() [2/4]

template<class SageNode >
const SageNode * sg::assert_sage_type ( const SgNode n,
const char *  f = 0,
size_t  ln = 0 
)

asserts that n has type SageNode

the ROSE assert in the following example holds b/c assert_sage_type aborts if the input node is not a SgStatement

SgStatement* stmt = assert_sage_type<SgStatement>(expr.get_parent());
ROSE_ASSERT(stmt);

Definition at line 1503 of file sageGeneric.h.

References dispatch().

◆ assert_sage_type() [3/4]

template<class SageNode >
SageNode & sg::assert_sage_type ( SgNode n,
const char *  f = 0,
size_t  ln = 0 
)

asserts that n has type SageNode

the ROSE assert in the following example holds b/c assert_sage_type aborts if the input node is not a SgStatement

SgStatement* stmt = assert_sage_type<SgStatement>(expr.get_parent());
ROSE_ASSERT(stmt);

Definition at line 1509 of file sageGeneric.h.

References dispatch().

◆ assert_sage_type() [4/4]

template<class SageNode >
const SageNode & sg::assert_sage_type ( const SgNode n,
const char *  f = 0,
size_t  ln = 0 
)

asserts that n has type SageNode

the ROSE assert in the following example holds b/c assert_sage_type aborts if the input node is not a SgStatement

SgStatement* stmt = assert_sage_type<SgStatement>(expr.get_parent());
ROSE_ASSERT(stmt);

Definition at line 1515 of file sageGeneric.h.

References dispatch().

◆ ancestor_path() [1/2]

template<class SageNode >
auto sg::ancestor_path ( const SgNode n) -> SageNode*

Definition at line 1530 of file sageGeneric.h.

References dispatch(), and SgNode::get_parent().

◆ ancestor_path() [2/2]

template<class SageNode , class... SageNodes>
auto sg::ancestor_path ( const SgNode n) -> decltype(ancestor_path<SageNodes...>(n))

Definition at line 1537 of file sageGeneric.h.

◆ ancestorPath() [1/2]

template<class SageNode >
auto sg::ancestorPath ( const SgNode n) -> SageNode*

returns the last parent in an ancestor path

Definition at line 1551 of file sageGeneric.h.

References dispatch(), and SgNode::get_parent().

◆ ancestorPath() [2/2]

template<class SageNode , class... SageNodes>
auto sg::ancestorPath ( const SgNode n) -> decltype(ancestorPath<SageNodes...>(n))

returns the last parent in an ancestor path

Definition at line 1558 of file sageGeneric.h.

◆ swap_child()

template<class SageNode , class SageChild >
void sg::swap_child ( SageNode &  lhs,
SageNode &  rhs,
SageChild *(SageNode::*)() const  getter,
void(SageNode::*)(SageChild *)  setter 
)

swaps children (of equal kind) between two ancestor nodes of the same type

Template Parameters
SageNodethe parent node type
SageChildthe child node type
Parameters
lhsone parent node
rhsanother parent node
getterthe getter function to extract the child from lhs (and rhs)
setterthe setter function to store the child in lhs (and rhs)

Definition at line 1593 of file sageGeneric.h.

◆ linkParentChild()

template<class SageParent , class SageChild >
void sg::linkParentChild ( SageParent &  parent,
SageChild &  child,
void(SageParent::*)(SageChild *)  setter 
)

Definition at line 1659 of file sageGeneric.h.

◆ asBaseType() [1/4]

template<class SageNode >
SageNode::base_node_type & sg::asBaseType ( SageNode &  n)

returns the same node n upcasted to its base type

Note
useful for calling an overloaded function

Definition at line 1670 of file sageGeneric.h.

◆ asBaseType() [2/4]

template<class SageNode >
const SageNode::base_node_type & sg::asBaseType ( const SageNode &  n)

returns the same node n upcasted to its base type

Note
useful for calling an overloaded function

Definition at line 1674 of file sageGeneric.h.

◆ asBaseType() [3/4]

template<class SageNode >
SageNode::base_node_type * sg::asBaseType ( SageNode *  n)

returns the same node n upcasted to its base type

Note
useful for calling an overloaded function

Definition at line 1678 of file sageGeneric.h.

◆ asBaseType() [4/4]

template<class SageNode >
const SageNode::base_node_type * sg::asBaseType ( const SageNode *  n)

returns the same node n upcasted to its base type

Note
useful for calling an overloaded function

Definition at line 1682 of file sageGeneric.h.