00001
00002 #ifndef _SAGEFUNCTORS_H
00003 #define _SAGEFUNCTORS_H
00004
00011
00012 #include "sageInterface.h"
00013 #include "sageBuilder.h"
00014
00015 namespace sg
00016 {
00019 template <class SageNode>
00020 static inline
00021 SageNode* cloneNode(const SageNode* n)
00022 {
00023 if (!n) return 0;
00024
00025 return SageInterface::deepCopy(n);
00026 }
00027
00030 static inline
00031 void _append(SgExprListExp& container, SgExpression* elem)
00032 {
00033 SageInterface::appendExpression(&container, elem);
00034 }
00035
00037 static inline
00038 void _append(SgFunctionParameterList& container, SgInitializedName* elem)
00039 {
00040 SageInterface::appendArg(&container, elem);
00041 }
00042
00044 struct ScopeSetter
00045 {
00046 explicit
00047 ScopeSetter(SgScopeStatement& the_scope)
00048 : scope(the_scope)
00049 {}
00050
00051 template <class ScopedSageNode>
00052 void handle(ScopedSageNode* scopeElem) const
00053 {
00054 ROSE_ASSERT(scopeElem);
00055
00056 scopeElem->set_scope(&scope);
00057 }
00058
00059 void operator()(SgStatement* scopeElem) const { handle(scopeElem); }
00060 void operator()(SgInitializedName* scopeElem) const { handle(scopeElem); }
00061
00062 private:
00063 SgScopeStatement& scope;
00064 };
00065
00067 struct VarRefBuilder
00068 {
00069 explicit
00070 VarRefBuilder(SgScopeStatement& the_scope)
00071 : scope(the_scope)
00072 {}
00073
00074 SgVarRefExp* operator()(SgInitializedName* initName) const
00075 {
00076 return SageBuilder::buildVarRefExp(initName, &scope);
00077 }
00078
00079 private:
00080 SgScopeStatement& scope;
00081 };
00082
00084 struct InitNameCloner
00085 {
00086 InitNameCloner(SgDeclarationStatement& declaration, SgScopeStatement* enclosing_scope = 0)
00087 : decl(declaration), scope(enclosing_scope)
00088 {}
00089
00090 SgInitializedName* operator()(const SgInitializedName* orig) const
00091 {
00092 SgInitializer* copy_init = cloneNode(orig->get_initializer());
00093 SgInitializedName* res = SageBuilder::buildInitializedName(orig->get_name(), orig->get_type(), copy_init);
00094
00095 res->set_scope(scope);
00096
00097 return res;
00098 }
00099
00100 private:
00101 SgDeclarationStatement& decl;
00102 SgScopeStatement* scope;
00103 };
00104
00108 template <class SageSequenceContainer>
00109 struct SageInserter : std::iterator<std::output_iterator_tag, void, void, void, void>
00110 {
00111 typedef SageSequenceContainer Container;
00112
00113 Container& container;
00114
00115 explicit
00116 SageInserter(Container& cont)
00117 : container(cont)
00118 {}
00119
00120
00121 template <class SageElem>
00122 SageInserter& operator=(SageElem* elem)
00123 {
00124 _append(container, elem);
00125 return *this;
00126 }
00127
00128 SageInserter& operator*() { return *this; }
00129 SageInserter& operator++() { return *this; }
00130 SageInserter& operator++(int) { return *this; }
00131 };
00132
00135 template <class SageSequenceContainer>
00136 SageInserter<SageSequenceContainer>
00137 sage_inserter(SageSequenceContainer& cont)
00138 {
00139 return SageInserter<SageSequenceContainer>(cont);
00140 }
00141 }
00142
00143 #endif