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 | class bool_ref { |
---|
44 | public: |
---|
45 | bool_ref() = default; |
---|
46 | ~bool_ref() = default; |
---|
47 | |
---|
48 | operator bool() { return m_ref ? *m_ref : true; } |
---|
49 | bool operator=( bool val ) { assert(m_ref); return *m_ref = val; } |
---|
50 | |
---|
51 | private: |
---|
52 | |
---|
53 | friend class ChildrenGuard; |
---|
54 | |
---|
55 | bool * set( bool * val ) { |
---|
56 | bool * prev = m_ref; |
---|
57 | m_ref = val; |
---|
58 | return prev; |
---|
59 | } |
---|
60 | |
---|
61 | bool * m_ref = nullptr; |
---|
62 | }; |
---|
63 | |
---|
64 | class ChildrenGuard { |
---|
65 | public: |
---|
66 | |
---|
67 | ChildrenGuard( bool_ref * ref ) |
---|
68 | : m_val ( true ) |
---|
69 | , m_prev( ref ? ref->set( &m_val ) : nullptr ) |
---|
70 | , m_ref ( ref ) |
---|
71 | {} |
---|
72 | |
---|
73 | ~ChildrenGuard() { |
---|
74 | if( m_ref ) { |
---|
75 | m_ref->set( m_prev ); |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | operator bool() { return m_val; } |
---|
80 | |
---|
81 | private: |
---|
82 | bool m_val; |
---|
83 | bool * m_prev; |
---|
84 | bool_ref * m_ref; |
---|
85 | }; |
---|
86 | |
---|
87 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
88 | // Deep magic (a.k.a template meta programming) to make the templated visitor work |
---|
89 | // Basically the goal is to make 2 previsit_impl |
---|
90 | // 1 - Use when a pass implements a valid previsit. This uses overloading which means the any overload of |
---|
91 | // 'pass.previsit( node )' that compiles will be used for that node for that type |
---|
92 | // This requires that this option only compile for passes that actually define an appropriate visit. |
---|
93 | // SFINAE will make sure the compilation errors in this function don't halt the build. |
---|
94 | // See http://en.cppreference.com/w/cpp/language/sfinae for details on SFINAE |
---|
95 | // 2 - Since the first implementation might not be specilizable, the second implementation exists and does nothing. |
---|
96 | // This is needed only to eliminate the need for passes to specify any kind of handlers. |
---|
97 | // The second implementation only works because it has a lower priority. This is due to the bogus last parameter. |
---|
98 | // The second implementation takes a long while the first takes an int. Since the caller always passes an literal 0 |
---|
99 | // the first implementation takes priority in regards to overloading. |
---|
100 | // Mutator functions work along the same principal |
---|
101 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
102 | // Visit |
---|
103 | template<typename pass_type, typename node_type> |
---|
104 | static inline auto previsit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.previsit( node ), void() ) { |
---|
105 | pass.previsit( node ); |
---|
106 | } |
---|
107 | |
---|
108 | template<typename pass_type, typename node_type> |
---|
109 | static inline void previsit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {} |
---|
110 | |
---|
111 | |
---|
112 | template<typename pass_type, typename node_type> |
---|
113 | static inline auto postvisit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.postvisit( node ), void() ) { |
---|
114 | pass.postvisit( node ); |
---|
115 | } |
---|
116 | |
---|
117 | template<typename pass_type, typename node_type> |
---|
118 | static inline void postvisit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {} |
---|
119 | |
---|
120 | //--------------------------------------------------------- |
---|
121 | // Mutate |
---|
122 | template<typename pass_type, typename node_type> |
---|
123 | static inline auto premutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.premutate( node ), void() ) { |
---|
124 | return pass.premutate( node ); |
---|
125 | } |
---|
126 | |
---|
127 | template<typename pass_type, typename node_type> |
---|
128 | static inline void premutate_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {} |
---|
129 | |
---|
130 | |
---|
131 | template<typename return_type, typename pass_type, typename node_type> |
---|
132 | static inline auto postmutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.postmutate( node ) ) { |
---|
133 | return pass.postmutate( node ); |
---|
134 | } |
---|
135 | |
---|
136 | template<typename return_type, typename pass_type, typename node_type> |
---|
137 | static inline return_type postmutate_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) { return node; } |
---|
138 | |
---|
139 | //--------------------------------------------------------- |
---|
140 | // Begin/End scope |
---|
141 | template<typename pass_type> |
---|
142 | static inline auto begin_scope_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( pass.beginScope(), void() ) { |
---|
143 | pass.beginScope(); |
---|
144 | } |
---|
145 | |
---|
146 | template<typename pass_type> |
---|
147 | static inline void begin_scope_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) {} |
---|
148 | |
---|
149 | |
---|
150 | template<typename pass_type> |
---|
151 | static inline auto end_scope_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( pass.endScope(), void() ) { |
---|
152 | pass.endScope(); |
---|
153 | } |
---|
154 | |
---|
155 | template<typename pass_type> |
---|
156 | static inline void end_scope_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) {} |
---|
157 | |
---|
158 | //--------------------------------------------------------- |
---|
159 | // Fields |
---|
160 | #define FIELD_PTR( type, name ) \ |
---|
161 | template<typename pass_type> \ |
---|
162 | static inline auto name##_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( &pass.name ) { return &pass.name; } \ |
---|
163 | \ |
---|
164 | template<typename pass_type> \ |
---|
165 | static inline type * name##_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) { return nullptr;} \ |
---|
166 | |
---|
167 | FIELD_PTR( const TypeSubstitution *, env ) |
---|
168 | FIELD_PTR( std::list< Statement* >, stmtsToAddBefore ) |
---|
169 | FIELD_PTR( std::list< Statement* >, stmtsToAddAfter ) |
---|
170 | FIELD_PTR( std::list< Declaration* >, declsToAddBefore ) |
---|
171 | FIELD_PTR( std::list< Declaration* >, declsToAddAfter ) |
---|
172 | FIELD_PTR( bool_ref, visit_children ) |
---|
173 | FIELD_PTR( at_cleanup_t, at_cleanup ) |
---|
174 | FIELD_PTR( PassVisitor<pass_type> * const, visitor ) |
---|
175 | |
---|
176 | #undef FIELD_PTR |
---|
177 | |
---|
178 | //--------------------------------------------------------- |
---|
179 | // Indexer |
---|
180 | template<typename pass_type> |
---|
181 | static inline auto indexer_impl_enterScope( pass_type & pass, int ) -> decltype( pass.indexer.enterScope(), void() ) { |
---|
182 | pass.indexer.enterScope(); |
---|
183 | } |
---|
184 | |
---|
185 | template<typename pass_type> |
---|
186 | static inline auto indexer_impl_enterScope( pass_type &, long ) {} |
---|
187 | |
---|
188 | template<typename pass_type> |
---|
189 | static inline auto indexer_impl_leaveScope( pass_type & pass, int ) -> decltype( pass.indexer.leaveScope(), void() ) { |
---|
190 | pass.indexer.leaveScope(); |
---|
191 | } |
---|
192 | |
---|
193 | template<typename pass_type> |
---|
194 | static inline auto indexer_impl_leaveScope( pass_type &, long ) {} |
---|
195 | |
---|
196 | |
---|
197 | #define INDEXER_FUNC1( func, type ) \ |
---|
198 | template<typename pass_type> \ |
---|
199 | static inline auto indexer_impl_##func ( pass_type & pass, int, type arg ) -> decltype( pass.indexer.func( arg ), void() ) { \ |
---|
200 | pass.indexer.func( arg ); \ |
---|
201 | } \ |
---|
202 | \ |
---|
203 | template<typename pass_type> \ |
---|
204 | static inline void indexer_impl_##func ( pass_type &, long, type ) { } \ |
---|
205 | |
---|
206 | #define INDEXER_FUNC2( func, type1, type2 ) \ |
---|
207 | template<typename pass_type> \ |
---|
208 | static inline auto indexer_impl_##func ( pass_type & pass, int, type1 arg1, type2 arg2 ) -> decltype( pass.indexer.func( arg1, arg2 ), void() ) { \ |
---|
209 | pass.indexer.func( arg1, arg2 ); \ |
---|
210 | } \ |
---|
211 | \ |
---|
212 | template<typename pass_type> \ |
---|
213 | static inline void indexer_impl_##func ( pass_type &, long, type1, type2 ) { } |
---|
214 | |
---|
215 | |
---|
216 | INDEXER_FUNC1( addId , DeclarationWithType * ); |
---|
217 | INDEXER_FUNC1( addType , NamedTypeDecl * ); |
---|
218 | INDEXER_FUNC1( addStruct , StructDecl * ); |
---|
219 | INDEXER_FUNC1( addEnum , EnumDecl * ); |
---|
220 | INDEXER_FUNC1( addUnion , UnionDecl * ); |
---|
221 | INDEXER_FUNC1( addTrait , TraitDecl * ); |
---|
222 | INDEXER_FUNC2( addWith , std::list< Expression * > &, BaseSyntaxNode * ); |
---|
223 | |
---|
224 | #undef INDEXER_FUNC1 |
---|
225 | #undef INDEXER_FUNC2 |
---|
226 | |
---|
227 | template<typename pass_type> |
---|
228 | static inline auto indexer_impl_addStructFwd( pass_type & pass, int, StructDecl * decl ) -> decltype( pass.indexer.addStruct( decl ), void() ) { |
---|
229 | StructDecl * fwd = new StructDecl( decl->name ); |
---|
230 | cloneAll( decl->parameters, fwd->parameters ); |
---|
231 | pass.indexer.addStruct( fwd ); |
---|
232 | } |
---|
233 | |
---|
234 | template<typename pass_type> |
---|
235 | static inline auto indexer_impl_addStructFwd( pass_type &, long, StructDecl * ) {} |
---|
236 | |
---|
237 | template<typename pass_type> |
---|
238 | static inline auto indexer_impl_addUnionFwd( pass_type & pass, int, UnionDecl * decl ) -> decltype( pass.indexer.addUnion( decl ), void() ) { |
---|
239 | UnionDecl * fwd = new UnionDecl( decl->name ); |
---|
240 | cloneAll( decl->parameters, fwd->parameters ); |
---|
241 | pass.indexer.addUnion( fwd ); |
---|
242 | } |
---|
243 | |
---|
244 | template<typename pass_type> |
---|
245 | static inline auto indexer_impl_addUnionFwd( pass_type &, long, UnionDecl * ) {} |
---|
246 | |
---|
247 | template<typename pass_type> |
---|
248 | static inline auto indexer_impl_addStruct( pass_type & pass, int, const std::string & str ) -> decltype( pass.indexer.addStruct( str ), void() ) { |
---|
249 | if ( ! pass.indexer.lookupStruct( str ) ) { |
---|
250 | pass.indexer.addStruct( str ); |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | template<typename pass_type> |
---|
255 | static inline auto indexer_impl_addStruct( pass_type &, long, const std::string & ) {} |
---|
256 | |
---|
257 | template<typename pass_type> |
---|
258 | static inline auto indexer_impl_addUnion( pass_type & pass, int, const std::string & str ) -> decltype( pass.indexer.addUnion( str ), void() ) { |
---|
259 | if ( ! pass.indexer.lookupUnion( str ) ) { |
---|
260 | pass.indexer.addUnion( str ); |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | template<typename pass_type> |
---|
265 | static inline auto indexer_impl_addUnion( pass_type &, long, const std::string & ) {} |
---|