[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 | |
---|
| 43 | |
---|
[b73bd70] | 44 | class bool_ref { |
---|
| 45 | public: |
---|
| 46 | bool_ref() = default; |
---|
| 47 | ~bool_ref() = default; |
---|
| 48 | |
---|
| 49 | operator bool() { return *m_ref; } |
---|
| 50 | bool operator=( bool val ) { return *m_ref = val; } |
---|
| 51 | |
---|
| 52 | private: |
---|
| 53 | |
---|
| 54 | template<typename pass> |
---|
| 55 | friend class PassVisitor; |
---|
| 56 | |
---|
| 57 | void set( bool & val ) { m_ref = &val; }; |
---|
| 58 | |
---|
| 59 | bool * m_ref; |
---|
| 60 | }; |
---|
| 61 | |
---|
[296b2be] | 62 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
| 63 | // Deep magic (a.k.a template meta programming) to make the templated visitor work |
---|
| 64 | // Basically the goal is to make 2 previsit_impl |
---|
[2a7b3ca] | 65 | // 1 - Use when a pass implements a valid previsit. This uses overloading which means the any overload of |
---|
[296b2be] | 66 | // 'pass.previsit( node )' that compiles will be used for that node for that type |
---|
| 67 | // This requires that this option only compile for passes that actually define an appropriate visit. |
---|
| 68 | // SFINAE will make sure the compilation errors in this function don't halt the build. |
---|
| 69 | // See http://en.cppreference.com/w/cpp/language/sfinae for details on SFINAE |
---|
| 70 | // 2 - Since the first implementation might not be specilizable, the second implementation exists and does nothing. |
---|
| 71 | // This is needed only to eliminate the need for passes to specify any kind of handlers. |
---|
| 72 | // The second implementation only works because it has a lower priority. This is due to the bogus last parameter. |
---|
| 73 | // The second implementation takes a long while the first takes an int. Since the caller always passes an literal 0 |
---|
| 74 | // the first implementation takes priority in regards to overloading. |
---|
| 75 | // Mutator functions work along the same principal |
---|
| 76 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
| 77 | // Visit |
---|
| 78 | template<typename pass_type, typename node_type> |
---|
[d7dc824] | 79 | static inline auto previsit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.previsit( node ), void() ) { |
---|
[296b2be] | 80 | pass.previsit( node ); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | template<typename pass_type, typename node_type> |
---|
[b3c36f4] | 84 | static inline void previsit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {} |
---|
[296b2be] | 85 | |
---|
| 86 | |
---|
| 87 | template<typename pass_type, typename node_type> |
---|
[d7dc824] | 88 | static inline auto postvisit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.postvisit( node ), void() ) { |
---|
[296b2be] | 89 | pass.postvisit( node ); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | template<typename pass_type, typename node_type> |
---|
[b3c36f4] | 93 | static inline void postvisit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {} |
---|
[296b2be] | 94 | |
---|
| 95 | // Mutate |
---|
| 96 | template<typename pass_type, typename node_type> |
---|
[d7dc824] | 97 | static inline auto premutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.premutate( node ), void() ) { |
---|
[296b2be] | 98 | return pass.premutate( node ); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | template<typename pass_type, typename node_type> |
---|
[b3c36f4] | 102 | static inline void premutate_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {} |
---|
[296b2be] | 103 | |
---|
| 104 | |
---|
| 105 | template<typename return_type, typename pass_type, typename node_type> |
---|
[d7dc824] | 106 | static inline auto postmutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.postmutate( node ) ) { |
---|
[296b2be] | 107 | return pass.postmutate( node ); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | template<typename return_type, typename pass_type, typename node_type> |
---|
| 111 | static inline return_type postmutate_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) { return node; } |
---|
| 112 | |
---|
| 113 | // Begin/End scope |
---|
| 114 | template<typename pass_type> |
---|
[d7dc824] | 115 | static inline auto begin_scope_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( pass.beginScope(), void() ) { |
---|
[296b2be] | 116 | pass.beginScope(); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | template<typename pass_type> |
---|
| 120 | static inline void begin_scope_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) {} |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | template<typename pass_type> |
---|
[d7dc824] | 124 | static inline auto end_scope_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( pass.endScope(), void() ) { |
---|
[296b2be] | 125 | pass.endScope(); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | template<typename pass_type> |
---|
[3fb9a83] | 129 | static inline void end_scope_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) {} |
---|
| 130 | |
---|
[134322e] | 131 | // Fields |
---|
[b3c36f4] | 132 | #define FIELD_PTR( type, name ) \ |
---|
| 133 | template<typename pass_type> \ |
---|
[6e09f211] | 134 | static inline auto name##_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( &pass.name ) { return &pass.name; } \ |
---|
[b3c36f4] | 135 | \ |
---|
| 136 | template<typename pass_type> \ |
---|
[134322e] | 137 | static inline type * name##_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) { return nullptr;} \ |
---|
| 138 | |
---|
| 139 | FIELD_PTR( TypeSubstitution *, env ) |
---|
| 140 | FIELD_PTR( std::list< Statement* >, stmtsToAddBefore ) |
---|
[7b13aeb] | 141 | FIELD_PTR( std::list< Statement* >, stmtsToAddAfter ) |
---|
[35df560] | 142 | FIELD_PTR( std::list< Declaration* >, declsToAddBefore ) |
---|
| 143 | FIELD_PTR( std::list< Declaration* >, declsToAddAfter ) |
---|
[b73bd70] | 144 | FIELD_PTR( bool_ref, visit_children ) |
---|
| 145 | FIELD_PTR( at_cleanup_t, at_cleanup ) |
---|
[2a7b3ca] | 146 | FIELD_PTR( PassVisitor<pass_type> * const, visitor ) |
---|