| 1 | #pragma once
 | 
|---|
| 2 | 
 | 
|---|
| 3 | //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 
|---|
| 4 | // Deep magic (a.k.a template meta programming) to make the templated visitor work
 | 
|---|
| 5 | // Basically the goal is to make 2 previsit_impl
 | 
|---|
| 6 | // 1 - Use when a pass implements a valid previsit. This uses overloading which means the any overload of 
 | 
|---|
| 7 | //     'pass.previsit( node )' that compiles will be used for that node for that type
 | 
|---|
| 8 | //     This requires that this option only compile for passes that actually define an appropriate visit.
 | 
|---|
| 9 | //     SFINAE will make sure the compilation errors in this function don't halt the build.
 | 
|---|
| 10 | //     See http://en.cppreference.com/w/cpp/language/sfinae for details on SFINAE
 | 
|---|
| 11 | // 2 - Since the first implementation might not be specilizable, the second implementation exists and does nothing.
 | 
|---|
| 12 | //     This is needed only to eliminate the need for passes to specify any kind of handlers.
 | 
|---|
| 13 | //     The second implementation only works because it has a lower priority. This is due to the bogus last parameter.
 | 
|---|
| 14 | //     The second implementation takes a long while the first takes an int. Since the caller always passes an literal 0
 | 
|---|
| 15 | //     the first implementation takes priority in regards to overloading.
 | 
|---|
| 16 | // Mutator functions work along the same principal
 | 
|---|
| 17 | //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 
|---|
| 18 | // Visit
 | 
|---|
| 19 | template<typename pass_type, typename node_type>
 | 
|---|
| 20 | static inline auto previsit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) ->decltype( pass.previsit( node ), void() ) {
 | 
|---|
| 21 |         pass.previsit( node );
 | 
|---|
| 22 | }
 | 
|---|
| 23 | 
 | 
|---|
| 24 | template<typename pass_type, typename node_type>
 | 
|---|
| 25 | static inline void previsit_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) {}
 | 
|---|
| 26 | 
 | 
|---|
| 27 | 
 | 
|---|
| 28 | template<typename pass_type, typename node_type>
 | 
|---|
| 29 | static inline auto postvisit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) ->decltype( pass.postvisit( node ), void() ) {
 | 
|---|
| 30 |         pass.postvisit( node );
 | 
|---|
| 31 | }
 | 
|---|
| 32 | 
 | 
|---|
| 33 | template<typename pass_type, typename node_type>
 | 
|---|
| 34 | static inline void postvisit_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) {}
 | 
|---|
| 35 | 
 | 
|---|
| 36 | // Mutate
 | 
|---|
| 37 | template<typename pass_type, typename node_type>
 | 
|---|
| 38 | static inline auto premutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) ->decltype( pass.premutate( node ), void() ) {
 | 
|---|
| 39 |         return pass.premutate( node );
 | 
|---|
| 40 | }
 | 
|---|
| 41 | 
 | 
|---|
| 42 | template<typename pass_type, typename node_type>
 | 
|---|
| 43 | static inline void premutate_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) {}
 | 
|---|
| 44 | 
 | 
|---|
| 45 | 
 | 
|---|
| 46 | template<typename return_type, typename pass_type, typename node_type>
 | 
|---|
| 47 | static inline auto postmutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) ->decltype( pass.postmutate( node ) ) {
 | 
|---|
| 48 |         return pass.postmutate( node );
 | 
|---|
| 49 | }
 | 
|---|
| 50 | 
 | 
|---|
| 51 | template<typename return_type, typename pass_type, typename node_type>
 | 
|---|
| 52 | static inline return_type postmutate_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) { return node; }
 | 
|---|
| 53 | 
 | 
|---|
| 54 | // Begin/End scope
 | 
|---|
| 55 | template<typename pass_type>
 | 
|---|
| 56 | static inline auto begin_scope_impl( pass_type& pass, __attribute__((unused)) int unused ) ->decltype( pass.beginScope(), void() ) {
 | 
|---|
| 57 |         pass.beginScope();
 | 
|---|
| 58 | }
 | 
|---|
| 59 | 
 | 
|---|
| 60 | template<typename pass_type>
 | 
|---|
| 61 | static inline void begin_scope_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) {}
 | 
|---|
| 62 | 
 | 
|---|
| 63 | 
 | 
|---|
| 64 | template<typename pass_type>
 | 
|---|
| 65 | static inline auto end_scope_impl( pass_type& pass, __attribute__((unused)) int unused ) ->decltype( pass.endScope(), void() ) {
 | 
|---|
| 66 |         pass.endScope();
 | 
|---|
| 67 | }
 | 
|---|
| 68 | 
 | 
|---|
| 69 | template<typename pass_type>
 | 
|---|
| 70 | static inline void end_scope_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) {}
 | 
|---|
| 71 | 
 | 
|---|
| 72 | // Fields
 | 
|---|
| 73 | #define FIELD_PTR( type, name )                                                                                                                  \
 | 
|---|
| 74 | template<typename pass_type>                                                                                                                     \
 | 
|---|
| 75 | static inline auto name##_impl( pass_type& pass, __attribute__((unused)) int unused ) ->decltype( &pass.name ) { return &pass.name; }          \
 | 
|---|
| 76 |                                                                                                                                                  \
 | 
|---|
| 77 | template<typename pass_type>                                                                                                                     \
 | 
|---|
| 78 | static inline type * name##_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) { return nullptr;}    \
 | 
|---|
| 79 | 
 | 
|---|
| 80 | FIELD_PTR( TypeSubstitution *, env )
 | 
|---|
| 81 | FIELD_PTR( std::list< Statement* >, stmtsToAddBefore )
 | 
|---|
| 82 | FIELD_PTR( std::list< Statement* >, stmtsToAddAfter  )
 | 
|---|