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