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