[296b2be] | 1 | #pragma once |
---|
| 2 | |
---|
[b73bd70] | 3 | template<typename pass_type> |
---|
| 4 | class PassVisitor; |
---|
| 5 | |
---|
[6e09f211] | 6 | typedef std::function<void( void * )> cleanup_func_t; |
---|
[62423350] | 7 | typedef std::function< void( cleanup_func_t, void * ) > at_cleanup_t; |
---|
[6e09f211] | 8 | |
---|
| 9 | class guard_value_impl { |
---|
| 10 | public: |
---|
[62423350] | 11 | guard_value_impl( at_cleanup_t * at_cleanup ) { |
---|
| 12 | if( at_cleanup ) { |
---|
| 13 | *at_cleanup = [this]( cleanup_func_t && func, void* val ) { |
---|
| 14 | push( std::move( func ), val ); |
---|
| 15 | }; |
---|
| 16 | } |
---|
| 17 | } |
---|
[6e09f211] | 18 | |
---|
| 19 | ~guard_value_impl() { |
---|
| 20 | while( !cleanups.empty() ) { |
---|
| 21 | auto& cleanup = cleanups.top(); |
---|
| 22 | cleanup.func( cleanup.val ); |
---|
| 23 | cleanups.pop(); |
---|
| 24 | } |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | void push( cleanup_func_t && func, void* val ) { |
---|
| 28 | cleanups.emplace( std::move(func), val ); |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | private: |
---|
| 32 | struct cleanup_t { |
---|
| 33 | cleanup_func_t func; |
---|
| 34 | void * val; |
---|
| 35 | |
---|
| 36 | cleanup_t( cleanup_func_t&& func, void * val ) : func(func), val(val) {} |
---|
| 37 | }; |
---|
| 38 | |
---|
| 39 | std::stack< cleanup_t > cleanups; |
---|
| 40 | }; |
---|
| 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 | |
---|
[296b2be] | 61 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
| 62 | // Deep magic (a.k.a template meta programming) to make the templated visitor work |
---|
| 63 | // Basically the goal is to make 2 previsit_impl |
---|
[2a7b3ca] | 64 | // 1 - Use when a pass implements a valid previsit. This uses overloading which means the any overload of |
---|
[296b2be] | 65 | // 'pass.previsit( node )' that compiles will be used for that node for that type |
---|
| 66 | // This requires that this option only compile for passes that actually define an appropriate visit. |
---|
| 67 | // SFINAE will make sure the compilation errors in this function don't halt the build. |
---|
| 68 | // See http://en.cppreference.com/w/cpp/language/sfinae for details on SFINAE |
---|
| 69 | // 2 - Since the first implementation might not be specilizable, the second implementation exists and does nothing. |
---|
| 70 | // This is needed only to eliminate the need for passes to specify any kind of handlers. |
---|
| 71 | // The second implementation only works because it has a lower priority. This is due to the bogus last parameter. |
---|
| 72 | // The second implementation takes a long while the first takes an int. Since the caller always passes an literal 0 |
---|
| 73 | // the first implementation takes priority in regards to overloading. |
---|
| 74 | // Mutator functions work along the same principal |
---|
| 75 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
| 76 | // Visit |
---|
| 77 | template<typename pass_type, typename node_type> |
---|
[d7dc824] | 78 | static inline auto previsit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.previsit( node ), void() ) { |
---|
[296b2be] | 79 | pass.previsit( node ); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | template<typename pass_type, typename node_type> |
---|
[b3c36f4] | 83 | static inline void previsit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {} |
---|
[296b2be] | 84 | |
---|
| 85 | |
---|
| 86 | template<typename pass_type, typename node_type> |
---|
[d7dc824] | 87 | static inline auto postvisit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.postvisit( node ), void() ) { |
---|
[296b2be] | 88 | pass.postvisit( node ); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | template<typename pass_type, typename node_type> |
---|
[b3c36f4] | 92 | static inline void postvisit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {} |
---|
[296b2be] | 93 | |
---|
| 94 | // Mutate |
---|
| 95 | template<typename pass_type, typename node_type> |
---|
[d7dc824] | 96 | static inline auto premutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.premutate( node ), void() ) { |
---|
[296b2be] | 97 | return pass.premutate( node ); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | template<typename pass_type, typename node_type> |
---|
[b3c36f4] | 101 | static inline void premutate_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {} |
---|
[296b2be] | 102 | |
---|
| 103 | |
---|
| 104 | template<typename return_type, typename pass_type, typename node_type> |
---|
[d7dc824] | 105 | static inline auto postmutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.postmutate( node ) ) { |
---|
[296b2be] | 106 | return pass.postmutate( node ); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | template<typename return_type, typename pass_type, typename node_type> |
---|
| 110 | static inline return_type postmutate_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) { return node; } |
---|
| 111 | |
---|
| 112 | // Begin/End scope |
---|
| 113 | template<typename pass_type> |
---|
[d7dc824] | 114 | static inline auto begin_scope_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( pass.beginScope(), void() ) { |
---|
[296b2be] | 115 | pass.beginScope(); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | template<typename pass_type> |
---|
| 119 | static inline void begin_scope_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) {} |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | template<typename pass_type> |
---|
[d7dc824] | 123 | static inline auto end_scope_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( pass.endScope(), void() ) { |
---|
[296b2be] | 124 | pass.endScope(); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | template<typename pass_type> |
---|
[3fb9a83] | 128 | static inline void end_scope_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) {} |
---|
| 129 | |
---|
[134322e] | 130 | // Fields |
---|
[b3c36f4] | 131 | #define FIELD_PTR( type, name ) \ |
---|
| 132 | template<typename pass_type> \ |
---|
[6e09f211] | 133 | static inline auto name##_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( &pass.name ) { return &pass.name; } \ |
---|
[b3c36f4] | 134 | \ |
---|
| 135 | template<typename pass_type> \ |
---|
[134322e] | 136 | static inline type * name##_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) { return nullptr;} \ |
---|
| 137 | |
---|
| 138 | FIELD_PTR( TypeSubstitution *, env ) |
---|
| 139 | FIELD_PTR( std::list< Statement* >, stmtsToAddBefore ) |
---|
[7b13aeb] | 140 | FIELD_PTR( std::list< Statement* >, stmtsToAddAfter ) |
---|
[35df560] | 141 | FIELD_PTR( std::list< Declaration* >, declsToAddBefore ) |
---|
| 142 | FIELD_PTR( std::list< Declaration* >, declsToAddAfter ) |
---|
[b73bd70] | 143 | FIELD_PTR( bool_ref, visit_children ) |
---|
| 144 | FIELD_PTR( at_cleanup_t, at_cleanup ) |
---|
[2a7b3ca] | 145 | FIELD_PTR( PassVisitor<pass_type> * const, visitor ) |
---|