ROSE  0.11.96.11
sageFunctors.h
Go to the documentation of this file.
1 
2 #ifndef _SAGEFUNCTORS_H
3 #define _SAGEFUNCTORS_H
4 
11 
12 #include "sageInterface.h"
13 #include "sageBuilder.h"
14 
15 namespace sg
16 {
19  template <class SageNode>
20  static inline
21  SageNode* cloneNode(const SageNode* n)
22  {
23  if (!n) return 0;
24 
25  return SageInterface::deepCopy(n);
26  }
27 
30  static inline
31  void _append(SgExprListExp& container, SgExpression* elem)
32  {
33  SageInterface::appendExpression(&container, elem);
34  }
35 
37  static inline
38  void _append(SgFunctionParameterList& container, SgInitializedName* elem)
39  {
40  SageInterface::appendArg(&container, elem);
41  }
42 
44  struct ScopeSetter
45  {
46  explicit
47  ScopeSetter(SgScopeStatement& the_scope)
48  : scope(the_scope)
49  {}
50 
51  template <class ScopedSageNode>
52  void handle(ScopedSageNode* scopeElem) const
53  {
54  ROSE_ASSERT(scopeElem);
55 
56  scopeElem->set_scope(&scope);
57  }
58 
59  void operator()(SgStatement* scopeElem) const { handle(scopeElem); }
60  void operator()(SgInitializedName* scopeElem) const { handle(scopeElem); }
61 
62  private:
63  SgScopeStatement& scope;
64  };
65 
68  {
69  explicit
71  : scope(the_scope)
72  {}
73 
74  SgVarRefExp* operator()(SgInitializedName* initName) const
75  {
76  return SageBuilder::buildVarRefExp(initName, &scope);
77  }
78 
79  private:
80  SgScopeStatement& scope;
81  };
82 
85  {
86  InitNameCloner(SgDeclarationStatement& declaration, SgScopeStatement* enclosing_scope = 0)
87  // DQ (3/25/2017): Remove to avoid Clang warning about unused private variable.
88  // : decl(declaration),
89  : scope(enclosing_scope)
90  {}
91 
92  SgInitializedName* operator()(const SgInitializedName* orig) const
93  {
94  SgInitializer* copy_init = cloneNode(orig->get_initializer());
95  SgInitializedName* res = SageBuilder::buildInitializedName(orig->get_name(), orig->get_type(), copy_init);
96 
97  res->set_scope(scope);
98 
99  return res;
100  }
101 
102  private:
103  // DQ (3/25/2017): Remove to avoid Clang warning about unused private variable.
104  // SgDeclarationStatement& decl;
105  SgScopeStatement* scope;
106  };
107 
111  template <class SageSequenceContainer>
112  struct SageInserter : std::iterator<std::output_iterator_tag, void, void, void, void>
113  {
114  typedef SageSequenceContainer Container;
115 
116  Container& container;
117 
118  explicit
119  SageInserter(Container& cont)
120  : container(cont)
121  {}
122 
123  // \todo SageElem should be derived form the container type
124  template <class SageElem>
125  SageInserter& operator=(SageElem* elem)
126  {
127  _append(container, elem);
128  return *this;
129  }
130 
131  SageInserter& operator*() { return *this; }
132  SageInserter& operator++() { return *this; }
133  SageInserter& operator++(int) { return *this; }
134  };
135 
138  template <class SageSequenceContainer>
140  sage_inserter(SageSequenceContainer& cont)
141  {
143  }
144 }
145 
146 #endif /* _SAGEFUNCTORS_H */
sg::ScopeSetter
Functor setting the scope of a sage node to a specified (at Functor construction time) scope.
Definition: sageFunctors.h:44
SageInterface::deepCopy
NodeType * deepCopy(const NodeType *subtree)
A template function for deep copying a subtree. It is also used to create deepcopy functions with spe...
Definition: sageInterface.h:523
sg::InitNameCloner
Functor copying an initialized name into a different scope.
Definition: sageFunctors.h:84
SgVarRefExp
This class represents the variable refernece in expressions.
Definition: Cxx_Grammar.h:272706
SgExprListExp
This class represents the concept of a C and C++ expression list.
Definition: Cxx_Grammar.h:270871
sg::SageInserter
Generic inserter for sage containers.
Definition: sageFunctors.h:112
sg::sage_inserter
SageInserter< SageSequenceContainer > sage_inserter(SageSequenceContainer &cont)
generates a SageInserter, adding elements at the end of a sequence
Definition: sageFunctors.h:140
SgExpression
This class represents the notion of an expression. Expressions are derived from SgLocatedNodes,...
Definition: Cxx_Grammar.h:229045
SgFunctionParameterList
This class represents the concept of a declaration list.
Definition: Cxx_Grammar.h:137786
SageInterface::appendExpression
ROSE_DLL_API void appendExpression(SgExprListExp *, SgExpression *)
Append an expression to a SgExprListExp, set the parent pointer also.
SgDeclarationStatement
This class represents the concept of a declaration statement.
Definition: Cxx_Grammar.h:136861
SageBuilder::buildVarRefExp
ROSE_DLL_API SgVarRefExp * buildVarRefExp(const SgName &name, SgScopeStatement *scope=NULL)
Build SgVarRefExp based on a variable's Sage name. It will lookup the name in the symbol table intern...
SageBuilder::buildInitializedName
ROSE_DLL_API SgInitializedName * buildInitializedName(const SgName &name, SgType *type, SgInitializer *init=NULL)
Initialized names are tricky, their scope vary depending on context, so scope and symbol information ...
SageInterface::appendArg
ROSE_DLL_API SgVariableSymbol * appendArg(SgFunctionParameterList *, SgInitializedName *)
Append an argument to SgFunctionParameterList, transparently set parent,scope, and symbols for argume...
SgInitializedName
This class represents the notion of a declared variable.
Definition: Cxx_Grammar.h:76851
sg
This namespace contains template functions that operate on the ROSE AST.
Definition: sageFunctors.h:15
sg::VarRefBuilder
Functor building a variable reference from an initialized name.
Definition: sageFunctors.h:67
SgScopeStatement
This class represents the concept of a scope in C++ (e.g. global scope, fuction scope,...
Definition: Cxx_Grammar.h:123553
SgStatement
This class represents the notion of a statement.
Definition: Cxx_Grammar.h:122747
SgInitializer
This class represents the notion of an initializer for a variable declaration or expression in a func...
Definition: Cxx_Grammar.h:294701