Go to the documentation of this file.
3 #define _SAGEGENERIC_H 1
14 #include <type_traits>
21 #define WITH_BINARY_NODES 0
22 #define WITH_UNTYPED_NODES 0
29 #define SG_UNEXPECTED_NODE(X) (sg::unexpected_node(X, __FILE__, __LINE__))
30 #define SG_DEREF(X) (sg::deref(X, __FILE__, __LINE__))
31 #define SG_ASSERT_TYPE(SAGENODE, N) (sg::assert_sage_type<SAGENODE>(N, __FILE__, __LINE__))
32 #define SG_ERROR_IF(COND, MSG) (sg::report_error_if(COND, MSG, __FILE__, __LINE__))
45 void unused(
const T&) {}
48 template <
class T1,
class T2>
54 template <
class T1,
class T2>
57 typedef const T2 type;
64 template <
class T,
class E>
82 void report_error(std::string desc,
const char* file =
nullptr,
size_t ln = 0);
85 void unexpected_node(
const SgNode& n,
const char* file =
nullptr,
size_t ln = 0);
89 void report_error_if(
bool iserror,
const std::string& desc,
const char* file =
nullptr,
size_t ln = 0)
93 report_error(desc, file, ln);
98 T&
deref(T* ptr,
const char* file = 0,
size_t ln = 0)
100 report_error_if(!ptr,
"assertion failed: null dereference ", file, ln);
116 template <
class _ReturnType>
119 typedef _ReturnType ReturnType;
131 operator ReturnType()
const {
return res; }
142 template <
class SageNode>
144 SageNode& assume_sage_type(
SgNode& n)
146 return static_cast<SageNode&
>(n);
151 template <
class SageNode>
153 const SageNode& assume_sage_type(
const SgNode& n)
155 return static_cast<const SageNode&
>(n);
158 #define GEN_VISIT(X) \
159 void visit(X * n) { rv.handle(*n); }
161 template <
class RoseVisitor>
166 : rv(std::move(rosevisitor))
810 template <
class RoseVisitor>
812 typename std::remove_const<typename std::remove_reference<RoseVisitor>::type>::type
813 _dispatch(RoseVisitor&& rv,
SgNode* n)
815 typedef typename std::remove_reference<RoseVisitor>::type RoseVisitorNoref;
816 typedef typename std::remove_const<RoseVisitorNoref>::type RoseHandler;
821 std::is_lvalue_reference<RoseVisitor>()
825 return std::move(vis).rv;
897 template <
class RoseVisitor>
899 typename std::remove_const<typename std::remove_reference<RoseVisitor>::type>::type
903 return _dispatch(std::forward<RoseVisitor>(rv), n);
906 template <
class RoseVisitor>
908 typename std::remove_const<typename std::remove_reference<RoseVisitor>::type>::type
912 return _dispatch(std::forward<RoseVisitor>(rv),
const_cast<SgNode*
>(n));
924 template <
class SageNode>
927 void handle(SageNode&) {}
937 template <
class AncestorNode,
class QualSgNode>
941 typedef std::pair<AncestorNode*, QualSgNode*> Pair;
946 :
Base(), res(NULL, NULL)
953 void handle(QualSgNode& n) { res.second = n.get_parent(); }
954 void handle(AncestorNode& n) { res.first = &n; }
956 operator Pair()
const {
return res; }
961 template <
class AncestorNode,
class QualSgNode>
966 typename AncestorFinder::Pair res(NULL, n.get_parent());
968 while (res.second != NULL)
970 res = (
typename AncestorFinder::Pair)
sg::dispatch(AncestorFinder(), res.second);
987 template <
class AncestorNode>
990 if (n == NULL)
return NULL;
992 return _ancestor<AncestorNode>(*n);
996 template <
class AncestorNode>
999 if (n == NULL)
return NULL;
1001 return _ancestor<const AncestorNode>(*n);
1005 template <
class AncestorNode>
1008 AncestorNode* res = _ancestor<AncestorNode>(n);
1015 template <
class AncestorNode>
1018 const AncestorNode* res = _ancestor<const AncestorNode>(n);
1025 template <
class SageNode>
1026 struct TypeRecoveryHandler
1028 typedef typename ConstLike<SageNode, SgNode>::type SgBaseNode;
1030 TypeRecoveryHandler(
const char* f = 0,
size_t ln = 0)
1031 : res(NULL), loc(f), loc_ln(ln)
1034 TypeRecoveryHandler(TypeRecoveryHandler&&) =
default;
1035 TypeRecoveryHandler& operator=(TypeRecoveryHandler&&) =
default;
1037 operator SageNode* ()&& {
return res; }
1039 void handle(SgBaseNode& n) { unexpected_node(n, loc, loc_ln); }
1040 void handle(SageNode& n) { res = &n; }
1047 TypeRecoveryHandler() =
delete;
1048 TypeRecoveryHandler(
const TypeRecoveryHandler&) =
delete;
1049 TypeRecoveryHandler& operator=(
const TypeRecoveryHandler&) =
delete;
1061 template <
class SageNode>
1064 return sg::dispatch(TypeRecoveryHandler<SageNode>(f, ln), n);
1067 template <
class SageNode>
1070 return sg::dispatch(TypeRecoveryHandler<const SageNode>(f, ln), n);
1073 template <
class SageNode>
1076 return *
sg::dispatch(TypeRecoveryHandler<SageNode>(f, ln), &n);
1079 template <
class SageNode>
1082 return *
sg::dispatch(TypeRecoveryHandler<const SageNode>(f, ln), &n);
1101 void swap_parent(
void*,
void*) {}
1110 template <
class SageNode,
class SageChild>
1111 void swap_child(SageNode& lhs, SageNode& rhs, SageChild* (SageNode::*getter) ()
const,
void (SageNode::*setter) (SageChild*))
1113 SageChild* lhs_child = (lhs.*getter)();
1114 SageChild* rhs_child = (rhs.*getter)();
1115 ROSE_ASSERT(lhs_child && rhs_child);
1117 (lhs.*setter)(rhs_child);
1118 (rhs.*setter)(lhs_child);
1120 swap_parent(lhs_child, rhs_child);
1126 template <
class SageNode>
1129 typedef void (*TransformHandlerFn)(SageNode*);
1137 void handle(SageNode& n) { fn(&n); }
1139 TransformHandlerFn fn;
1144 template <
class SageNode>
1147 createTraversalFunction(
void (* fn)(SageNode*))
1157 template <
class GVisitor>
1172 GVisitor visitor() {
return gvisitor; }
1188 forAllNodes(F fn,
SgNode* root, AstSimpleProcessing::Order order = postorder)
1192 TraversalClass<F> tt(fn);
1195 tt.traverse(root, order);
1196 return tt.visitor();
1199 template <
class SageNode>
1202 forAllNodes(
void (*fn)(SageNode*),
SgNode* root, AstSimpleProcessing::Order order = postorder)
1204 forAllNodes(createTransformExecutor(fn), root, order);
1207 #if !defined(NDEBUG)
1209 std::string nodeType(
const SgNode& n)
1211 return typeid(n).
name();
1215 std::string nodeType(
const SgNode* n)
1217 if (n == NULL)
return "<null>";
1219 return nodeType(*n);
1223 template <
class GVisitor>
1228 : gvisitor(std::move(gv)), parent(p), cnt(0)
1231 void operator()(
SgNode* n)
1238 std::cerr <<
"succ(" << nodeType(parent) <<
", " << cnt <<
") is null" << std::endl;
1243 if (n != NULL) gvisitor =
sg::dispatch(std::move(gvisitor), n);
1246 operator GVisitor()&& {
return std::move(gvisitor); }
1254 template <
class GVisitor>
1257 dispatchHelper(GVisitor gv,
SgNode* parent = NULL)
1262 template <
class GVisitor>
1264 GVisitor traverseChildren(GVisitor gv,
SgNode& n)
1268 return std::for_each(successors.begin(), successors.end(), dispatchHelper(std::move(gv), &n));
1271 template <
class GVisitor>
1273 GVisitor traverseChildren(GVisitor gv,
SgNode* n)
1275 return traverseChildren(gv,
sg::deref(n));
1278 template <
class SageParent,
class SageChild>
1279 void linkParentChild(SageParent& parent, SageChild& child,
void (SageParent::*setter)(SageChild*))
1281 (parent.*setter)(&child);
1282 child.set_parent(&parent);
1288 template <
class SageNode>
1289 typename SageNode::base_node_type&
1292 template <
class SageNode>
1293 const typename SageNode::base_node_type&
1296 template <
class SageNode>
1297 typename SageNode::base_node_type*
1300 template <
class SageNode>
1301 const typename SageNode::base_node_type*
This class represents the notion of an expression or statement which has a position within the source...
This class represents a boolean value (expression value).
This class represents the base class of a number of IR nodes define modifiers within the C++ grammar.
This class represents the concept of a C++ sequence of catch statements.
This class represents a Fortran pointer assignment. It is not some weird compound assignment operator...
This class is intended to be a wrapper around SgStatements, allowing them to exist in scopes that onl...
This class represents a string type used for SgStringVal IR node.
This class represents the notion of a typedef declaration.
This class represents the GNU extension "statement expression" (thus is non-standard C and C++).
This class represents the concept of a variable name within the compiler (a shared container for the ...
This class represents the C++ throw expression (handled as a unary operator).
This class represents a lambda expression.
This class represents the concept of a C++ expression built from a class name.
This class represents the notion of an value (expression value).
This class represents the concept of a C++ namespace declaration.
This class represents a type for all functions.
This class represents the notion of an value (expression value).
This class represents the "&" operator (applied to any lvalue).
T & deref(T *ptr, const char *file=0, size_t ln=0)
dereferences an object (= checked dereference in debug mode)
This class represents the concept of a C and C++ case option (used within a switch statement).
This class represents the concept of an "if" construct.
This class represents the concept of the dynamic execution of a string, file, or code object....
This class represents the concept of a class definition in C++.
This class represents the concept of a C trinary conditional expression (e.g. "test ?...
This class represents the concept of a C or C++ default case within a switch statement.
This class represents the "this" operator (can be applied to any member data).
This class represents the concept of a namespace definition.
This class represents the concept of a do-while statement.
This class represents the concept of a class declaration statement. It includes the concept of an ins...
This class represents the concept of a for loop.
This class represents the notion of a unary operator. It is derived from a SgExpression because opera...
This class represents the definition (initialization) of a variable.
This class represents the concept of a generic call expression.
This class represents the variable refernece in expressions.
SageNode::base_node_type & asBaseType(SageNode &n)
returns the same node n upcasted to its base type
This class represents the rhs of a variable declaration which includes an optional assignment (e....
virtual std::vector< SgNode * > get_traversalSuccessorContainer()
container of pointers to AST successor nodes used in the traversal overridden in every class by gener...
This class represents the concept of a C and C++ expression list.
This class represents the concept of a namespace name within the compiler.
This class represents a source file for a project (which may contian many source files and or directo...
This class represents the base class for all types.
This class represents the concept of a C++ function call (which is an expression).
This class represents the concept of a do-while statement.
This class represents the base class of a numbr of IR nodes that don't otherwise fit into the existin...
This class represents the concept of a C++ call to the new operator.
This class represents the member function being called and must be assembled in the SgFunctionCall wi...
This class represents the concept of a switch.
This class represents the concept of a C or C++ continue statement.
virtual void accept(ROSE_VisitorPattern &visitor)
support for the classic visitor pattern done in GoF
AncestorNode * _ancestor(QualSgNode &n)
implements the ancestor search
This class represents the function being called and must be assembled in the SgFunctionCall with the ...
This class represents the concept of a block (not a basic block from control flow analysis).
projects the constness of T1 on T2
This class represents the notion of an n-ary boolean operation. This node is intended for use with Py...
std::remove_const< typename std::remove_reference< RoseVisitor >::type >::type dispatch(RoseVisitor &&rv, SgNode *n)
uncovers the type of SgNode and passes it to an function "handle" in RoseVisitor.
This class represents the function type table (stores all function types so that they can be shared i...
This class represents the concept of a name and a type. It may be renamed in the future to SgTypeSymb...
This class represents the concept of a member function declaration statement.
This class represents the concept of a function declaration statement.
This class is part of the older CC++ concept. It is not a part of C or C++ (this IR node is not used ...
This class represents the concept of a C++ call to the delete operator.
This class represents a directory within a projects file structure of files and directories.
This class is not used in ROSE, but is intended to represent a list of SgModifierTypes (similar to th...
This class represents the concept of a C++ using declaration.
This class represents a C99 complex type.
This class represents the notion of an expression. Expressions are derived from SgLocatedNodes,...
This class represents the numeric negation of a value. Not to be confused with SgSubtractOp.
This class represents the concept of an enum declaration.
This class represents the physical disequality (often called pointer disequality) operator for langua...
This class represents the notion of an n-ary comparison operation. This node is intended for use with...
This class represents the concept of an instantiation of member function template or a member functio...
helper class for _ancestor
This class represents the concept of a declaration list.
This class represents template argument within the use of a template to build an instantiation.
This class represents a default type used for some IR nodes (see below).
AncestorNode * ancestor(SgNode *n)
finds an ancestor node with a given type
Class for traversing the AST.
This class represents strings within the IR nodes.
This class represents the concept of a declaration statement.
This class represents the physical equality (often called pointer equality) operator for languages th...
This class represents the location of the code associated with the IR node in the original source cod...
This class represents the concept of a namespace definition.
This class represents a OLD concept of the structure require for qualified names when they were in th...
This class represents the concept of a template declaration.
This class represents the concept of a C Assembler statement (untested).
This class represents the notion of an value (expression value).
This class represents a list display.
This class represents the notion of a binary operator. It is derived from a SgExpression because oper...
This class represents the base class for all IR nodes within Sage III.
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
This class represents the concept of a class name within the compiler.
This class represents the concept of an instantiation of function template.
This class represents the concept of a class definition in C++.
This class represents the notion of an initializer for a variable declaration or expression in a func...
This class represents the concept of a C++ using directive.
This class represents the concept of a catch within a try-catch construct used in C++ exception handl...
This class represents the notion of an n-ary operator. This node is intended for use with Python.
This class represents the notion of a declared variable.
This class represents the concept of a C or C++ label statement.
This class represents modifiers for SgDeclaration (declaration statements).
This class represents the notion of an value (expression value).
This class represents the concept of a C++ template instantiation directive.
SgNode * get_parent() const
Access function for parent node.
void set_parent(SgNode *parent)
All nodes in the AST contain a reference to a parent node.
This class represents the concept of a scope in C++ (e.g. global scope, fuction scope,...
This namespace contains template functions that operate on the ROSE AST.
This class represents the symbol tables used in both SgScopeStatement and the SgFunctionTypeSymbolTab...
This class represents a list of associated typedefs for the SgType IR nodes which reference this list...
This class represents the conversion of an arbitrary expression to a string. This node is intended fo...
This class represents the concept of a C or C++ variable declaration.
This class represents the "sizeof()" operator (applied to any type).
This class represents the concept of a C++ namespace alias declaration statement.
This class represents the concept of a scope in C++ (e.g. global scope, fuction scope,...
SageNode * assert_sage_type(SgNode *n, const char *f=0, size_t ln=0)
asserts that n has type SageNode
This class represents the concept of a contructor initializer list (used in constructor (member funct...
This class represents a C99 complex type.
This class represents a source project, with a list of SgFile objects and global information about th...
void visit(SgNode *n)
this method is called at every traversed node.
This class represents modifiers specific to storage.
executes a functor for a specific node type
This class represents the notion of a statement.
This class represents the concept of a C or C++ goto statement.
This class represents an object used to initialize the unparsing.
This class was part of CC++ support from a long time ago.
This class represents the notion of an initializer for a variable declaration or expression in a func...
This class represents the concept of a C or C++ statement which contains a expression.
This class represents the notion of a break statement (typically used in a switch statment).
This class represents the variable declaration or variable initialization withn a for loop.
This class represents the concept of a name within the compiler.
This class represents the concept of a C Assembler statement (untested).
This class represents the concept of a C style extern "C" declaration. But such information (linkage)...
This class represents the concept of try statement within the try-catch support for exception handlin...
This class represents a OLD concept of the structure require for qualified names when they were in th...
const std::string & name(Id)
Returns the name for an attribute ID.
This class represents the concept of an instantiated class template.