1 | #pragma once |
---|
2 | |
---|
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 | |
---|
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> |
---|
51 | static inline auto previsit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.previsit( node ), void() ) { |
---|
52 | pass.previsit( node ); |
---|
53 | } |
---|
54 | |
---|
55 | template<typename pass_type, typename node_type> |
---|
56 | static inline void previsit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {} |
---|
57 | |
---|
58 | |
---|
59 | template<typename pass_type, typename node_type> |
---|
60 | static inline auto postvisit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.postvisit( node ), void() ) { |
---|
61 | pass.postvisit( node ); |
---|
62 | } |
---|
63 | |
---|
64 | template<typename pass_type, typename node_type> |
---|
65 | static inline void postvisit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {} |
---|
66 | |
---|
67 | // Mutate |
---|
68 | template<typename pass_type, typename node_type> |
---|
69 | static inline auto premutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.premutate( node ), void() ) { |
---|
70 | return pass.premutate( node ); |
---|
71 | } |
---|
72 | |
---|
73 | template<typename pass_type, typename node_type> |
---|
74 | static inline void premutate_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {} |
---|
75 | |
---|
76 | |
---|
77 | template<typename return_type, typename pass_type, typename node_type> |
---|
78 | static inline auto postmutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.postmutate( node ) ) { |
---|
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> |
---|
87 | static inline auto begin_scope_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( pass.beginScope(), void() ) { |
---|
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> |
---|
96 | static inline auto end_scope_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( pass.endScope(), void() ) { |
---|
97 | pass.endScope(); |
---|
98 | } |
---|
99 | |
---|
100 | template<typename pass_type> |
---|
101 | static inline void end_scope_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) {} |
---|
102 | |
---|
103 | // Fields |
---|
104 | #define FIELD_PTR( type, name ) \ |
---|
105 | template<typename pass_type> \ |
---|
106 | static inline auto name##_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( &pass.name ) { return &pass.name; } \ |
---|
107 | \ |
---|
108 | template<typename pass_type> \ |
---|
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 ) |
---|
113 | FIELD_PTR( std::list< Statement* >, stmtsToAddAfter ) |
---|
114 | FIELD_PTR( bool, skip_children ) |
---|
115 | FIELD_PTR( at_cleanup_t, at_cleanup ) |
---|