[296b2be] | 1 | #pragma once |
---|
[3268a58] | 2 | // IWYU pragma: private, include "PassVisitor.h" |
---|
[296b2be] | 3 | |
---|
[b73bd70] | 4 | template<typename pass_type> |
---|
| 5 | class PassVisitor; |
---|
| 6 | |
---|
[6e09f211] | 7 | typedef std::function<void( void * )> cleanup_func_t; |
---|
[62423350] | 8 | typedef std::function< void( cleanup_func_t, void * ) > at_cleanup_t; |
---|
[6e09f211] | 9 | |
---|
| 10 | class guard_value_impl { |
---|
| 11 | public: |
---|
[62423350] | 12 | guard_value_impl( at_cleanup_t * at_cleanup ) { |
---|
| 13 | if( at_cleanup ) { |
---|
| 14 | *at_cleanup = [this]( cleanup_func_t && func, void* val ) { |
---|
| 15 | push( std::move( func ), val ); |
---|
| 16 | }; |
---|
| 17 | } |
---|
| 18 | } |
---|
[6e09f211] | 19 | |
---|
| 20 | ~guard_value_impl() { |
---|
| 21 | while( !cleanups.empty() ) { |
---|
| 22 | auto& cleanup = cleanups.top(); |
---|
| 23 | cleanup.func( cleanup.val ); |
---|
| 24 | cleanups.pop(); |
---|
| 25 | } |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | void push( cleanup_func_t && func, void* val ) { |
---|
| 29 | cleanups.emplace( std::move(func), val ); |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | private: |
---|
| 33 | struct cleanup_t { |
---|
| 34 | cleanup_func_t func; |
---|
| 35 | void * val; |
---|
| 36 | |
---|
| 37 | cleanup_t( cleanup_func_t&& func, void * val ) : func(func), val(val) {} |
---|
| 38 | }; |
---|
| 39 | |
---|
| 40 | std::stack< cleanup_t > cleanups; |
---|
| 41 | }; |
---|
| 42 | |
---|
[b73bd70] | 43 | class bool_ref { |
---|
| 44 | public: |
---|
| 45 | bool_ref() = default; |
---|
| 46 | ~bool_ref() = default; |
---|
| 47 | |
---|
| 48 | operator bool() { return *m_ref; } |
---|
| 49 | bool operator=( bool val ) { return *m_ref = val; } |
---|
| 50 | |
---|
| 51 | private: |
---|
| 52 | |
---|
| 53 | template<typename pass> |
---|
| 54 | friend class PassVisitor; |
---|
| 55 | |
---|
| 56 | void set( bool & val ) { m_ref = &val; }; |
---|
| 57 | |
---|
| 58 | bool * m_ref; |
---|
| 59 | }; |
---|
| 60 | |
---|
[e0886db] | 61 | template< typename TreeType, typename VisitorType > |
---|
| 62 | inline void indexerScopedAccept( TreeType * tree, VisitorType & visitor ) { |
---|
[9dcb653] | 63 | auto guard = makeFuncGuard( |
---|
| 64 | [&visitor]() { visitor.indexerScopeEnter(); }, |
---|
| 65 | [&visitor]() { visitor.indexerScopeLeave(); } |
---|
| 66 | ); |
---|
[e0886db] | 67 | maybeAccept( tree, visitor ); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | template< typename TreeType, typename MutatorType > |
---|
| 71 | inline void indexerScopedMutate( TreeType *& tree, MutatorType & mutator ) { |
---|
[9dcb653] | 72 | auto guard = makeFuncGuard( |
---|
| 73 | [&mutator]() { mutator.indexerScopeEnter(); }, |
---|
| 74 | [&mutator]() { mutator.indexerScopeLeave(); } |
---|
| 75 | ); |
---|
[e0886db] | 76 | tree = maybeMutate( tree, mutator ); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | template< typename TreeType, typename MutatorType > |
---|
| 80 | inline void maybeMutateRef( TreeType *& tree, MutatorType & mutator ) { |
---|
| 81 | tree = maybeMutate( tree, mutator ); |
---|
| 82 | } |
---|
| 83 | |
---|
[296b2be] | 84 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
| 85 | // Deep magic (a.k.a template meta programming) to make the templated visitor work |
---|
| 86 | // Basically the goal is to make 2 previsit_impl |
---|
[2a7b3ca] | 87 | // 1 - Use when a pass implements a valid previsit. This uses overloading which means the any overload of |
---|
[296b2be] | 88 | // 'pass.previsit( node )' that compiles will be used for that node for that type |
---|
| 89 | // This requires that this option only compile for passes that actually define an appropriate visit. |
---|
| 90 | // SFINAE will make sure the compilation errors in this function don't halt the build. |
---|
| 91 | // See http://en.cppreference.com/w/cpp/language/sfinae for details on SFINAE |
---|
| 92 | // 2 - Since the first implementation might not be specilizable, the second implementation exists and does nothing. |
---|
| 93 | // This is needed only to eliminate the need for passes to specify any kind of handlers. |
---|
| 94 | // The second implementation only works because it has a lower priority. This is due to the bogus last parameter. |
---|
| 95 | // The second implementation takes a long while the first takes an int. Since the caller always passes an literal 0 |
---|
| 96 | // the first implementation takes priority in regards to overloading. |
---|
| 97 | // Mutator functions work along the same principal |
---|
| 98 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
| 99 | // Visit |
---|
| 100 | template<typename pass_type, typename node_type> |
---|
[d7dc824] | 101 | static inline auto previsit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.previsit( node ), void() ) { |
---|
[296b2be] | 102 | pass.previsit( node ); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | template<typename pass_type, typename node_type> |
---|
[b3c36f4] | 106 | static inline void previsit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {} |
---|
[296b2be] | 107 | |
---|
| 108 | |
---|
| 109 | template<typename pass_type, typename node_type> |
---|
[d7dc824] | 110 | static inline auto postvisit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.postvisit( node ), void() ) { |
---|
[296b2be] | 111 | pass.postvisit( node ); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | template<typename pass_type, typename node_type> |
---|
[b3c36f4] | 115 | static inline void postvisit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {} |
---|
[296b2be] | 116 | |
---|
[e0886db] | 117 | //--------------------------------------------------------- |
---|
[296b2be] | 118 | // Mutate |
---|
| 119 | template<typename pass_type, typename node_type> |
---|
[d7dc824] | 120 | static inline auto premutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.premutate( node ), void() ) { |
---|
[296b2be] | 121 | return pass.premutate( node ); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | template<typename pass_type, typename node_type> |
---|
[b3c36f4] | 125 | static inline void premutate_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {} |
---|
[296b2be] | 126 | |
---|
| 127 | |
---|
| 128 | template<typename return_type, typename pass_type, typename node_type> |
---|
[d7dc824] | 129 | static inline auto postmutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.postmutate( node ) ) { |
---|
[296b2be] | 130 | return pass.postmutate( node ); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | template<typename return_type, typename pass_type, typename node_type> |
---|
| 134 | static inline return_type postmutate_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) { return node; } |
---|
| 135 | |
---|
[e0886db] | 136 | //--------------------------------------------------------- |
---|
[296b2be] | 137 | // Begin/End scope |
---|
| 138 | template<typename pass_type> |
---|
[d7dc824] | 139 | static inline auto begin_scope_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( pass.beginScope(), void() ) { |
---|
[296b2be] | 140 | pass.beginScope(); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | template<typename pass_type> |
---|
| 144 | static inline void begin_scope_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) {} |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | template<typename pass_type> |
---|
[d7dc824] | 148 | static inline auto end_scope_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( pass.endScope(), void() ) { |
---|
[296b2be] | 149 | pass.endScope(); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | template<typename pass_type> |
---|
[3fb9a83] | 153 | static inline void end_scope_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) {} |
---|
| 154 | |
---|
[e0886db] | 155 | //--------------------------------------------------------- |
---|
[134322e] | 156 | // Fields |
---|
[b3c36f4] | 157 | #define FIELD_PTR( type, name ) \ |
---|
| 158 | template<typename pass_type> \ |
---|
[6e09f211] | 159 | static inline auto name##_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( &pass.name ) { return &pass.name; } \ |
---|
[b3c36f4] | 160 | \ |
---|
| 161 | template<typename pass_type> \ |
---|
[134322e] | 162 | static inline type * name##_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) { return nullptr;} \ |
---|
| 163 | |
---|
| 164 | FIELD_PTR( TypeSubstitution *, env ) |
---|
| 165 | FIELD_PTR( std::list< Statement* >, stmtsToAddBefore ) |
---|
[7b13aeb] | 166 | FIELD_PTR( std::list< Statement* >, stmtsToAddAfter ) |
---|
[35df560] | 167 | FIELD_PTR( std::list< Declaration* >, declsToAddBefore ) |
---|
| 168 | FIELD_PTR( std::list< Declaration* >, declsToAddAfter ) |
---|
[b73bd70] | 169 | FIELD_PTR( bool_ref, visit_children ) |
---|
| 170 | FIELD_PTR( at_cleanup_t, at_cleanup ) |
---|
[2a7b3ca] | 171 | FIELD_PTR( PassVisitor<pass_type> * const, visitor ) |
---|
[e0886db] | 172 | |
---|
| 173 | //--------------------------------------------------------- |
---|
| 174 | // Indexer |
---|
| 175 | template<typename pass_type> |
---|
| 176 | static inline auto indexer_impl_enterScope( pass_type & pass, int ) -> decltype( pass.indexer.enterScope(), void() ) { |
---|
[9dcb653] | 177 | pass.indexer.enterScope(); |
---|
[e0886db] | 178 | } |
---|
| 179 | |
---|
| 180 | template<typename pass_type> |
---|
[522363e] | 181 | static inline auto indexer_impl_enterScope( pass_type &, long ) {} |
---|
[e0886db] | 182 | |
---|
| 183 | template<typename pass_type> |
---|
| 184 | static inline auto indexer_impl_leaveScope( pass_type & pass, int ) -> decltype( pass.indexer.leaveScope(), void() ) { |
---|
[9dcb653] | 185 | pass.indexer.leaveScope(); |
---|
[e0886db] | 186 | } |
---|
| 187 | |
---|
| 188 | template<typename pass_type> |
---|
[522363e] | 189 | static inline auto indexer_impl_leaveScope( pass_type &, long ) {} |
---|
[e0886db] | 190 | |
---|
| 191 | |
---|
| 192 | #define INDEXER_FUNC( func, type ) \ |
---|
| 193 | template<typename pass_type> \ |
---|
| 194 | static inline auto indexer_impl_##func ( pass_type & pass, int, type arg ) -> decltype( pass.indexer.func( arg ), void() ) { \ |
---|
[9dcb653] | 195 | pass.indexer.func( arg ); \ |
---|
[e0886db] | 196 | } \ |
---|
| 197 | \ |
---|
| 198 | template<typename pass_type> \ |
---|
[522363e] | 199 | static inline void indexer_impl_##func ( pass_type &, long, type ) { } \ |
---|
[e0886db] | 200 | |
---|
| 201 | INDEXER_FUNC( addId , DeclarationWithType * ); |
---|
| 202 | INDEXER_FUNC( addType , NamedTypeDecl * ); |
---|
| 203 | INDEXER_FUNC( addStruct , StructDecl * ); |
---|
| 204 | INDEXER_FUNC( addEnum , EnumDecl * ); |
---|
| 205 | INDEXER_FUNC( addUnion , UnionDecl * ); |
---|
| 206 | INDEXER_FUNC( addTrait , TraitDecl * ); |
---|
| 207 | |
---|
| 208 | |
---|
| 209 | template<typename pass_type> |
---|
| 210 | static inline auto indexer_impl_addStructFwd( pass_type & pass, int, StructDecl * decl ) -> decltype( pass.indexer.addStruct( decl ), void() ) { |
---|
[9dcb653] | 211 | StructDecl * fwd = new StructDecl( decl->name ); |
---|
| 212 | cloneAll( decl->parameters, fwd->parameters ); |
---|
| 213 | pass.indexer.addStruct( fwd ); |
---|
[e0886db] | 214 | } |
---|
| 215 | |
---|
| 216 | template<typename pass_type> |
---|
[522363e] | 217 | static inline auto indexer_impl_addStructFwd( pass_type &, long, StructDecl * ) {} |
---|
[e0886db] | 218 | |
---|
| 219 | template<typename pass_type> |
---|
| 220 | static inline auto indexer_impl_addUnionFwd( pass_type & pass, int, UnionDecl * decl ) -> decltype( pass.indexer.addUnion( decl ), void() ) { |
---|
[9dcb653] | 221 | UnionDecl * fwd = new UnionDecl( decl->name ); |
---|
| 222 | cloneAll( decl->parameters, fwd->parameters ); |
---|
| 223 | pass.indexer.addUnion( fwd ); |
---|
[e0886db] | 224 | } |
---|
| 225 | |
---|
| 226 | template<typename pass_type> |
---|
[522363e] | 227 | static inline auto indexer_impl_addUnionFwd( pass_type &, long, UnionDecl * ) {} |
---|
[e0886db] | 228 | |
---|
| 229 | template<typename pass_type> |
---|
| 230 | static inline auto indexer_impl_addStruct( pass_type & pass, int, const std::string & str ) -> decltype( pass.indexer.addStruct( str ), void() ) { |
---|
[9dcb653] | 231 | if ( ! pass.indexer.lookupStruct( str ) ) { |
---|
| 232 | pass.indexer.addStruct( str ); |
---|
| 233 | } |
---|
[e0886db] | 234 | } |
---|
| 235 | |
---|
| 236 | template<typename pass_type> |
---|
[522363e] | 237 | static inline auto indexer_impl_addStruct( pass_type &, long, const std::string & ) {} |
---|
[e0886db] | 238 | |
---|
| 239 | template<typename pass_type> |
---|
| 240 | static inline auto indexer_impl_addUnion( pass_type & pass, int, const std::string & str ) -> decltype( pass.indexer.addUnion( str ), void() ) { |
---|
[9dcb653] | 241 | if ( ! pass.indexer.lookupUnion( str ) ) { |
---|
| 242 | pass.indexer.addUnion( str ); |
---|
| 243 | } |
---|
[e0886db] | 244 | } |
---|
| 245 | |
---|
| 246 | template<typename pass_type> |
---|
[522363e] | 247 | static inline auto indexer_impl_addUnion( pass_type &, long, const std::string & ) {} |
---|