[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 | |
---|
[3c398b6] | 48 | operator bool() { return m_ref ? *m_ref : true; } |
---|
[4990812] | 49 | bool operator=( bool val ) { assert(m_ref); return *m_ref = val; } |
---|
[b73bd70] | 50 | |
---|
| 51 | private: |
---|
| 52 | |
---|
[3c398b6] | 53 | friend class ChildrenGuard; |
---|
[b73bd70] | 54 | |
---|
[4990812] | 55 | bool * set( bool * val ) { |
---|
[3c398b6] | 56 | bool * prev = m_ref; |
---|
[4990812] | 57 | m_ref = val; |
---|
[3c398b6] | 58 | return prev; |
---|
| 59 | } |
---|
[b73bd70] | 60 | |
---|
[3c398b6] | 61 | bool * m_ref = nullptr; |
---|
[b73bd70] | 62 | }; |
---|
| 63 | |
---|
[3c398b6] | 64 | class ChildrenGuard { |
---|
| 65 | public: |
---|
| 66 | |
---|
| 67 | ChildrenGuard( bool_ref * ref ) |
---|
| 68 | : m_val ( true ) |
---|
[4990812] | 69 | , m_prev( ref ? ref->set( &m_val ) : nullptr ) |
---|
[3c398b6] | 70 | , m_ref ( ref ) |
---|
| 71 | {} |
---|
| 72 | |
---|
| 73 | ~ChildrenGuard() { |
---|
| 74 | if( m_ref ) { |
---|
[4990812] | 75 | m_ref->set( m_prev ); |
---|
[3c398b6] | 76 | } |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | operator bool() { return m_val; } |
---|
| 80 | |
---|
| 81 | private: |
---|
| 82 | bool m_val; |
---|
| 83 | bool * m_prev; |
---|
| 84 | bool_ref * m_ref; |
---|
| 85 | }; |
---|
[e0886db] | 86 | |
---|
[296b2be] | 87 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
| 88 | // Deep magic (a.k.a template meta programming) to make the templated visitor work |
---|
| 89 | // Basically the goal is to make 2 previsit_impl |
---|
[2a7b3ca] | 90 | // 1 - Use when a pass implements a valid previsit. This uses overloading which means the any overload of |
---|
[296b2be] | 91 | // 'pass.previsit( node )' that compiles will be used for that node for that type |
---|
| 92 | // This requires that this option only compile for passes that actually define an appropriate visit. |
---|
| 93 | // SFINAE will make sure the compilation errors in this function don't halt the build. |
---|
| 94 | // See http://en.cppreference.com/w/cpp/language/sfinae for details on SFINAE |
---|
| 95 | // 2 - Since the first implementation might not be specilizable, the second implementation exists and does nothing. |
---|
| 96 | // This is needed only to eliminate the need for passes to specify any kind of handlers. |
---|
| 97 | // The second implementation only works because it has a lower priority. This is due to the bogus last parameter. |
---|
| 98 | // The second implementation takes a long while the first takes an int. Since the caller always passes an literal 0 |
---|
| 99 | // the first implementation takes priority in regards to overloading. |
---|
| 100 | // Mutator functions work along the same principal |
---|
| 101 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
| 102 | // Visit |
---|
| 103 | template<typename pass_type, typename node_type> |
---|
[d7dc824] | 104 | static inline auto previsit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.previsit( node ), void() ) { |
---|
[296b2be] | 105 | pass.previsit( node ); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | template<typename pass_type, typename node_type> |
---|
[b3c36f4] | 109 | static inline void previsit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {} |
---|
[296b2be] | 110 | |
---|
| 111 | |
---|
| 112 | template<typename pass_type, typename node_type> |
---|
[d7dc824] | 113 | static inline auto postvisit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.postvisit( node ), void() ) { |
---|
[296b2be] | 114 | pass.postvisit( node ); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | template<typename pass_type, typename node_type> |
---|
[b3c36f4] | 118 | static inline void postvisit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {} |
---|
[296b2be] | 119 | |
---|
[7870799] | 120 | template<typename pass_type, typename node_type> |
---|
| 121 | static inline auto previsit_impl( pass_type& pass, const node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.previsit( node ), void() ) { |
---|
| 122 | pass.previsit( node ); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | template<typename pass_type, typename node_type> |
---|
| 126 | static inline void previsit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) const node_type * node, __attribute__((unused)) long unused ) {} |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | template<typename pass_type, typename node_type> |
---|
| 130 | static inline auto postvisit_impl( pass_type& pass, const node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.postvisit( node ), void() ) { |
---|
| 131 | pass.postvisit( node ); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | template<typename pass_type, typename node_type> |
---|
| 135 | static inline void postvisit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) const node_type * node, __attribute__((unused)) long unused ) {} |
---|
| 136 | |
---|
[e0886db] | 137 | //--------------------------------------------------------- |
---|
[296b2be] | 138 | // Mutate |
---|
| 139 | template<typename pass_type, typename node_type> |
---|
[d7dc824] | 140 | static inline auto premutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.premutate( node ), void() ) { |
---|
[296b2be] | 141 | return pass.premutate( node ); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | template<typename pass_type, typename node_type> |
---|
[b3c36f4] | 145 | static inline void premutate_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {} |
---|
[296b2be] | 146 | |
---|
| 147 | |
---|
| 148 | template<typename return_type, typename pass_type, typename node_type> |
---|
[d7dc824] | 149 | static inline auto postmutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.postmutate( node ) ) { |
---|
[296b2be] | 150 | return pass.postmutate( node ); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | template<typename return_type, typename pass_type, typename node_type> |
---|
| 154 | static inline return_type postmutate_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) { return node; } |
---|
| 155 | |
---|
[e0886db] | 156 | //--------------------------------------------------------- |
---|
[296b2be] | 157 | // Begin/End scope |
---|
| 158 | template<typename pass_type> |
---|
[d7dc824] | 159 | static inline auto begin_scope_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( pass.beginScope(), void() ) { |
---|
[296b2be] | 160 | pass.beginScope(); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | template<typename pass_type> |
---|
| 164 | static inline void begin_scope_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) {} |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | template<typename pass_type> |
---|
[d7dc824] | 168 | static inline auto end_scope_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( pass.endScope(), void() ) { |
---|
[296b2be] | 169 | pass.endScope(); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | template<typename pass_type> |
---|
[3fb9a83] | 173 | static inline void end_scope_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) {} |
---|
| 174 | |
---|
[e0886db] | 175 | //--------------------------------------------------------- |
---|
[134322e] | 176 | // Fields |
---|
[b3c36f4] | 177 | #define FIELD_PTR( type, name ) \ |
---|
| 178 | template<typename pass_type> \ |
---|
[6e09f211] | 179 | static inline auto name##_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( &pass.name ) { return &pass.name; } \ |
---|
[b3c36f4] | 180 | \ |
---|
| 181 | template<typename pass_type> \ |
---|
[134322e] | 182 | static inline type * name##_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) { return nullptr;} \ |
---|
| 183 | |
---|
[02fdb8e] | 184 | FIELD_PTR( const TypeSubstitution *, env ) |
---|
[134322e] | 185 | FIELD_PTR( std::list< Statement* >, stmtsToAddBefore ) |
---|
[7b13aeb] | 186 | FIELD_PTR( std::list< Statement* >, stmtsToAddAfter ) |
---|
[35df560] | 187 | FIELD_PTR( std::list< Declaration* >, declsToAddBefore ) |
---|
| 188 | FIELD_PTR( std::list< Declaration* >, declsToAddAfter ) |
---|
[b73bd70] | 189 | FIELD_PTR( bool_ref, visit_children ) |
---|
| 190 | FIELD_PTR( at_cleanup_t, at_cleanup ) |
---|
[2a7b3ca] | 191 | FIELD_PTR( PassVisitor<pass_type> * const, visitor ) |
---|
[e0886db] | 192 | |
---|
[6a625de] | 193 | #undef FIELD_PTR |
---|
| 194 | |
---|
[e0886db] | 195 | //--------------------------------------------------------- |
---|
| 196 | // Indexer |
---|
| 197 | template<typename pass_type> |
---|
| 198 | static inline auto indexer_impl_enterScope( pass_type & pass, int ) -> decltype( pass.indexer.enterScope(), void() ) { |
---|
[9dcb653] | 199 | pass.indexer.enterScope(); |
---|
[e0886db] | 200 | } |
---|
| 201 | |
---|
| 202 | template<typename pass_type> |
---|
[522363e] | 203 | static inline auto indexer_impl_enterScope( pass_type &, long ) {} |
---|
[e0886db] | 204 | |
---|
| 205 | template<typename pass_type> |
---|
| 206 | static inline auto indexer_impl_leaveScope( pass_type & pass, int ) -> decltype( pass.indexer.leaveScope(), void() ) { |
---|
[9dcb653] | 207 | pass.indexer.leaveScope(); |
---|
[e0886db] | 208 | } |
---|
| 209 | |
---|
| 210 | template<typename pass_type> |
---|
[522363e] | 211 | static inline auto indexer_impl_leaveScope( pass_type &, long ) {} |
---|
[e0886db] | 212 | |
---|
| 213 | |
---|
[0ac366b] | 214 | #define INDEXER_FUNC1( func, type ) \ |
---|
[e0886db] | 215 | template<typename pass_type> \ |
---|
| 216 | static inline auto indexer_impl_##func ( pass_type & pass, int, type arg ) -> decltype( pass.indexer.func( arg ), void() ) { \ |
---|
[9dcb653] | 217 | pass.indexer.func( arg ); \ |
---|
[e0886db] | 218 | } \ |
---|
| 219 | template<typename pass_type> \ |
---|
[7870799] | 220 | static inline void indexer_impl_##func ( pass_type &, long, type ) { } |
---|
[e0886db] | 221 | |
---|
[0ac366b] | 222 | #define INDEXER_FUNC2( func, type1, type2 ) \ |
---|
| 223 | template<typename pass_type> \ |
---|
| 224 | static inline auto indexer_impl_##func ( pass_type & pass, int, type1 arg1, type2 arg2 ) -> decltype( pass.indexer.func( arg1, arg2 ), void() ) { \ |
---|
| 225 | pass.indexer.func( arg1, arg2 ); \ |
---|
| 226 | } \ |
---|
| 227 | template<typename pass_type> \ |
---|
| 228 | static inline void indexer_impl_##func ( pass_type &, long, type1, type2 ) { } |
---|
| 229 | |
---|
| 230 | |
---|
[e3d7f9f] | 231 | INDEXER_FUNC1( addId , const DeclarationWithType * ); |
---|
| 232 | INDEXER_FUNC1( addType , const NamedTypeDecl * ); |
---|
| 233 | INDEXER_FUNC1( addStruct , const StructDecl * ); |
---|
| 234 | INDEXER_FUNC1( addEnum , const EnumDecl * ); |
---|
| 235 | INDEXER_FUNC1( addUnion , const UnionDecl * ); |
---|
| 236 | INDEXER_FUNC1( addTrait , const TraitDecl * ); |
---|
[e67991f] | 237 | INDEXER_FUNC2( addWith , const std::list< Expression * > &, const Declaration * ); |
---|
[e0886db] | 238 | |
---|
[37e3af4] | 239 | #undef INDEXER_FUNC1 |
---|
| 240 | #undef INDEXER_FUNC2 |
---|
[e0886db] | 241 | |
---|
| 242 | template<typename pass_type> |
---|
[e3d7f9f] | 243 | static inline auto indexer_impl_addStructFwd( pass_type & pass, int, const StructDecl * decl ) -> decltype( pass.indexer.addStruct( decl ), void() ) { |
---|
[9dcb653] | 244 | StructDecl * fwd = new StructDecl( decl->name ); |
---|
| 245 | cloneAll( decl->parameters, fwd->parameters ); |
---|
| 246 | pass.indexer.addStruct( fwd ); |
---|
[e0886db] | 247 | } |
---|
| 248 | |
---|
| 249 | template<typename pass_type> |
---|
[7870799] | 250 | static inline auto indexer_impl_addStructFwd( pass_type &, long, const StructDecl * ) {} |
---|
[e0886db] | 251 | |
---|
| 252 | template<typename pass_type> |
---|
[e3d7f9f] | 253 | static inline auto indexer_impl_addUnionFwd( pass_type & pass, int, const UnionDecl * decl ) -> decltype( pass.indexer.addUnion( decl ), void() ) { |
---|
[9dcb653] | 254 | UnionDecl * fwd = new UnionDecl( decl->name ); |
---|
| 255 | cloneAll( decl->parameters, fwd->parameters ); |
---|
| 256 | pass.indexer.addUnion( fwd ); |
---|
[e0886db] | 257 | } |
---|
| 258 | |
---|
| 259 | template<typename pass_type> |
---|
[7870799] | 260 | static inline auto indexer_impl_addUnionFwd( pass_type &, long, const UnionDecl * ) {} |
---|
[e0886db] | 261 | |
---|
| 262 | template<typename pass_type> |
---|
| 263 | static inline auto indexer_impl_addStruct( pass_type & pass, int, const std::string & str ) -> decltype( pass.indexer.addStruct( str ), void() ) { |
---|
[9dcb653] | 264 | if ( ! pass.indexer.lookupStruct( str ) ) { |
---|
| 265 | pass.indexer.addStruct( str ); |
---|
| 266 | } |
---|
[e0886db] | 267 | } |
---|
| 268 | |
---|
| 269 | template<typename pass_type> |
---|
[522363e] | 270 | static inline auto indexer_impl_addStruct( pass_type &, long, const std::string & ) {} |
---|
[e0886db] | 271 | |
---|
| 272 | template<typename pass_type> |
---|
| 273 | static inline auto indexer_impl_addUnion( pass_type & pass, int, const std::string & str ) -> decltype( pass.indexer.addUnion( str ), void() ) { |
---|
[9dcb653] | 274 | if ( ! pass.indexer.lookupUnion( str ) ) { |
---|
| 275 | pass.indexer.addUnion( str ); |
---|
| 276 | } |
---|
[e0886db] | 277 | } |
---|
| 278 | |
---|
| 279 | template<typename pass_type> |
---|
[522363e] | 280 | static inline auto indexer_impl_addUnion( pass_type &, long, const std::string & ) {} |
---|