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