1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2019 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // Pass.impl.hpp -- |
---|
8 | // |
---|
9 | // Author : Thierry Delisle |
---|
10 | // Created On : Thu May 09 15::37::05 2019 |
---|
11 | // Last Modified By : |
---|
12 | // Last Modified On : |
---|
13 | // Update Count : |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | // IWYU pragma: private, include "Pass.hpp" |
---|
18 | |
---|
19 | #include "Common/Stats/Heap.h" |
---|
20 | |
---|
21 | namespace ast { |
---|
22 | template<typename core_t> |
---|
23 | class Pass; |
---|
24 | |
---|
25 | struct TranslationUnit; |
---|
26 | |
---|
27 | struct PureVisitor; |
---|
28 | |
---|
29 | namespace __pass { |
---|
30 | typedef std::function<void( void * )> cleanup_func_t; |
---|
31 | typedef std::function<void( cleanup_func_t, void * )> at_cleanup_t; |
---|
32 | |
---|
33 | |
---|
34 | // boolean reference that may be null |
---|
35 | // either refers to a boolean value or is null and returns true |
---|
36 | class bool_ref { |
---|
37 | public: |
---|
38 | bool_ref() = default; |
---|
39 | ~bool_ref() = default; |
---|
40 | |
---|
41 | operator bool() { return m_ref ? *m_ref : true; } |
---|
42 | bool operator=( bool val ) { assert(m_ref); return *m_ref = val; } |
---|
43 | |
---|
44 | private: |
---|
45 | |
---|
46 | friend class visit_children_guard; |
---|
47 | |
---|
48 | bool * set( bool * val ) { |
---|
49 | bool * prev = m_ref; |
---|
50 | m_ref = val; |
---|
51 | return prev; |
---|
52 | } |
---|
53 | |
---|
54 | bool * m_ref = nullptr; |
---|
55 | }; |
---|
56 | |
---|
57 | // Implementation of the guard value |
---|
58 | // Created inside the visit scope |
---|
59 | class guard_value { |
---|
60 | public: |
---|
61 | /// Push onto the cleanup |
---|
62 | guard_value( at_cleanup_t * at_cleanup ) { |
---|
63 | if( at_cleanup ) { |
---|
64 | *at_cleanup = [this]( cleanup_func_t && func, void* val ) { |
---|
65 | push( std::move( func ), val ); |
---|
66 | }; |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | ~guard_value() { |
---|
71 | while( !cleanups.empty() ) { |
---|
72 | auto& cleanup = cleanups.top(); |
---|
73 | cleanup.func( cleanup.val ); |
---|
74 | cleanups.pop(); |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | void push( cleanup_func_t && func, void* val ) { |
---|
79 | cleanups.emplace( std::move(func), val ); |
---|
80 | } |
---|
81 | |
---|
82 | private: |
---|
83 | struct cleanup_t { |
---|
84 | cleanup_func_t func; |
---|
85 | void * val; |
---|
86 | |
---|
87 | cleanup_t( cleanup_func_t&& func, void * val ) : func(func), val(val) {} |
---|
88 | }; |
---|
89 | |
---|
90 | std::stack< cleanup_t, std::vector<cleanup_t> > cleanups; |
---|
91 | }; |
---|
92 | |
---|
93 | // Guard structure implementation for whether or not children should be visited |
---|
94 | class visit_children_guard { |
---|
95 | public: |
---|
96 | |
---|
97 | visit_children_guard( bool_ref * ref ) |
---|
98 | : m_val ( true ) |
---|
99 | , m_prev( ref ? ref->set( &m_val ) : nullptr ) |
---|
100 | , m_ref ( ref ) |
---|
101 | {} |
---|
102 | |
---|
103 | ~visit_children_guard() { |
---|
104 | if( m_ref ) { |
---|
105 | m_ref->set( m_prev ); |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | operator bool() { return m_val; } |
---|
110 | |
---|
111 | private: |
---|
112 | bool m_val; |
---|
113 | bool * m_prev; |
---|
114 | bool_ref * m_ref; |
---|
115 | }; |
---|
116 | |
---|
117 | /// "Short hand" to check if this is a valid previsit function |
---|
118 | /// Mostly used to make the static_assert look (and print) prettier |
---|
119 | template<typename core_t, typename node_t> |
---|
120 | struct is_valid_previsit { |
---|
121 | using ret_t = decltype( std::declval<core_t*>()->previsit( std::declval<const node_t *>() ) ); |
---|
122 | |
---|
123 | static constexpr bool value = std::is_void< ret_t >::value || |
---|
124 | std::is_base_of<const node_t, typename std::remove_pointer<ret_t>::type >::value; |
---|
125 | }; |
---|
126 | |
---|
127 | /// Used by previsit implementation |
---|
128 | /// We need to reassign the result to 'node', unless the function |
---|
129 | /// returns void, then we just leave 'node' unchanged |
---|
130 | template<bool is_void> |
---|
131 | struct __assign; |
---|
132 | |
---|
133 | template<> |
---|
134 | struct __assign<true> { |
---|
135 | template<typename core_t, typename node_t> |
---|
136 | static inline void result( core_t & core, const node_t * & node ) { |
---|
137 | core.previsit( node ); |
---|
138 | } |
---|
139 | }; |
---|
140 | |
---|
141 | template<> |
---|
142 | struct __assign<false> { |
---|
143 | template<typename core_t, typename node_t> |
---|
144 | static inline void result( core_t & core, const node_t * & node ) { |
---|
145 | node = core.previsit( node ); |
---|
146 | assertf(node, "Previsit must not return NULL"); |
---|
147 | } |
---|
148 | }; |
---|
149 | |
---|
150 | /// Used by postvisit implementation |
---|
151 | /// We need to return the result unless the function |
---|
152 | /// returns void, then we just return the original node |
---|
153 | template<bool is_void> |
---|
154 | struct __return; |
---|
155 | |
---|
156 | template<> |
---|
157 | struct __return<true> { |
---|
158 | template<typename core_t, typename node_t> |
---|
159 | static inline const node_t * result( core_t & core, const node_t * & node ) { |
---|
160 | core.postvisit( node ); |
---|
161 | return node; |
---|
162 | } |
---|
163 | }; |
---|
164 | |
---|
165 | template<> |
---|
166 | struct __return<false> { |
---|
167 | template<typename core_t, typename node_t> |
---|
168 | static inline auto result( core_t & core, const node_t * & node ) { |
---|
169 | return core.postvisit( node ); |
---|
170 | } |
---|
171 | }; |
---|
172 | |
---|
173 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
174 | // Deep magic (a.k.a template meta programming) to make the templated visitor work |
---|
175 | // Basically the goal is to make 2 previsit |
---|
176 | // 1 - Use when a pass implements a valid previsit. This uses overloading which means the any overload of |
---|
177 | // 'pass.previsit( node )' that compiles will be used for that node for that type |
---|
178 | // This requires that this option only compile for passes that actually define an appropriate visit. |
---|
179 | // SFINAE will make sure the compilation errors in this function don't halt the build. |
---|
180 | // See http://en.cppreference.com/w/cpp/language/sfinae for details on SFINAE |
---|
181 | // 2 - Since the first implementation might not be specilizable, the second implementation exists and does nothing. |
---|
182 | // This is needed only to eliminate the need for passes to specify any kind of handlers. |
---|
183 | // The second implementation only works because it has a lower priority. This is due to the bogus last parameter. |
---|
184 | // The second implementation takes a long while the first takes an int. Since the caller always passes an literal 0 |
---|
185 | // the first implementation takes priority in regards to overloading. |
---|
186 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
187 | // PreVisit : may mutate the pointer passed in if the node is mutated in the previsit call |
---|
188 | template<typename core_t, typename node_t> |
---|
189 | static inline auto previsit( core_t & core, const node_t * & node, int ) -> decltype( core.previsit( node ), void() ) { |
---|
190 | static_assert( |
---|
191 | is_valid_previsit<core_t, node_t>::value, |
---|
192 | "Previsit may not change the type of the node. It must return its paremeter or void." |
---|
193 | ); |
---|
194 | |
---|
195 | __assign< |
---|
196 | std::is_void< |
---|
197 | decltype( core.previsit( node ) ) |
---|
198 | >::value |
---|
199 | >::result( core, node ); |
---|
200 | } |
---|
201 | |
---|
202 | template<typename core_t, typename node_t> |
---|
203 | static inline auto previsit( core_t &, const node_t *, long ) {} |
---|
204 | |
---|
205 | // PostVisit : never mutates the passed pointer but may return a different node |
---|
206 | template<typename core_t, typename node_t> |
---|
207 | static inline auto postvisit( core_t & core, const node_t * node, int ) -> |
---|
208 | decltype( core.postvisit( node ), node->accept( *(Visitor*)nullptr ) ) |
---|
209 | { |
---|
210 | return __return< |
---|
211 | std::is_void< |
---|
212 | decltype( core.postvisit( node ) ) |
---|
213 | >::value |
---|
214 | >::result( core, node ); |
---|
215 | } |
---|
216 | |
---|
217 | template<typename core_t, typename node_t> |
---|
218 | static inline const node_t * postvisit( core_t &, const node_t * node, long ) { return node; } |
---|
219 | |
---|
220 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
221 | // Deep magic (a.k.a template meta programming) continued |
---|
222 | // To make the templated visitor be more expressive, we allow 'accessories' : classes/structs the implementation can inherit |
---|
223 | // from in order to get extra functionallity for example |
---|
224 | // class ErrorChecker : WithShortCircuiting { ... }; |
---|
225 | // Pass<ErrorChecker> checker; |
---|
226 | // this would define a pass that uses the templated visitor with the additionnal feature that it has short circuiting |
---|
227 | // Note that in all cases the accessories are not required but guarantee the requirements of the feature is matched |
---|
228 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
229 | // For several accessories, the feature is enabled by detecting that a specific field is present |
---|
230 | // Use a macro the encapsulate the logic of detecting a particular field |
---|
231 | // The type is not strictly enforced but does match the accessory |
---|
232 | #define FIELD_PTR( name, default_type ) \ |
---|
233 | template< typename core_t > \ |
---|
234 | static inline auto name( core_t & core, int ) -> decltype( &core.name ) { return &core.name; } \ |
---|
235 | \ |
---|
236 | template< typename core_t > \ |
---|
237 | static inline default_type * name( core_t &, long ) { return nullptr; } |
---|
238 | |
---|
239 | // List of fields and their expected types |
---|
240 | FIELD_PTR( typeSubs, const ast::TypeSubstitution * ) |
---|
241 | FIELD_PTR( stmtsToAddBefore, std::list< ast::ptr< ast::Stmt > > ) |
---|
242 | FIELD_PTR( stmtsToAddAfter , std::list< ast::ptr< ast::Stmt > > ) |
---|
243 | FIELD_PTR( declsToAddBefore, std::list< ast::ptr< ast::Decl > > ) |
---|
244 | FIELD_PTR( declsToAddAfter , std::list< ast::ptr< ast::Decl > > ) |
---|
245 | FIELD_PTR( visit_children, __pass::bool_ref ) |
---|
246 | FIELD_PTR( at_cleanup, __pass::at_cleanup_t ) |
---|
247 | FIELD_PTR( visitor, ast::Pass<core_t> * const ) |
---|
248 | |
---|
249 | // Remove the macro to make sure we don't clash |
---|
250 | #undef FIELD_PTR |
---|
251 | |
---|
252 | template< typename core_t > |
---|
253 | static inline auto beginTrace(core_t &, int) -> decltype( core_t::traceId, void() ) { |
---|
254 | // Stats::Heap::stacktrace_push(core_t::traceId); |
---|
255 | } |
---|
256 | |
---|
257 | template< typename core_t > |
---|
258 | static inline auto endTrace(core_t &, int) -> decltype( core_t::traceId, void() ) { |
---|
259 | // Stats::Heap::stacktrace_pop(); |
---|
260 | } |
---|
261 | |
---|
262 | template< typename core_t > |
---|
263 | static void beginTrace(core_t &, long) {} |
---|
264 | |
---|
265 | template< typename core_t > |
---|
266 | static void endTrace(core_t &, long) {} |
---|
267 | |
---|
268 | // Allows visitor to handle an error on top-level declarations, and possibly suppress the error. |
---|
269 | // If onError() returns false, the error will be ignored. By default, it returns true. |
---|
270 | |
---|
271 | template< typename core_t > |
---|
272 | static bool on_error (core_t &, ptr<Decl> &, long) { return true; } |
---|
273 | |
---|
274 | template< typename core_t > |
---|
275 | static auto on_error (core_t & core, ptr<Decl> & decl, int) -> decltype(core.on_error(decl)) { |
---|
276 | return core.on_error(decl); |
---|
277 | } |
---|
278 | |
---|
279 | // Another feature of the templated visitor is that it calls beginScope()/endScope() for compound statement. |
---|
280 | // All passes which have such functions are assumed desire this behaviour |
---|
281 | // detect it using the same strategy |
---|
282 | namespace scope { |
---|
283 | template<typename core_t> |
---|
284 | static inline auto enter( core_t & core, int ) -> decltype( core.beginScope(), void() ) { |
---|
285 | core.beginScope(); |
---|
286 | } |
---|
287 | |
---|
288 | template<typename core_t> |
---|
289 | static inline void enter( core_t &, long ) {} |
---|
290 | |
---|
291 | template<typename core_t> |
---|
292 | static inline auto leave( core_t & core, int ) -> decltype( core.endScope(), void() ) { |
---|
293 | core.endScope(); |
---|
294 | } |
---|
295 | |
---|
296 | template<typename core_t> |
---|
297 | static inline void leave( core_t &, long ) {} |
---|
298 | } // namespace scope |
---|
299 | |
---|
300 | // Certain passes desire an up to date symbol table automatically |
---|
301 | // detect the presence of a member name `symtab` and call all the members appropriately |
---|
302 | namespace symtab { |
---|
303 | // Some simple scoping rules |
---|
304 | template<typename core_t> |
---|
305 | static inline auto enter( core_t & core, int ) -> decltype( core.symtab, void() ) { |
---|
306 | core.symtab.enterScope(); |
---|
307 | } |
---|
308 | |
---|
309 | template<typename core_t> |
---|
310 | static inline auto enter( core_t &, long ) {} |
---|
311 | |
---|
312 | template<typename core_t> |
---|
313 | static inline auto leave( core_t & core, int ) -> decltype( core.symtab, void() ) { |
---|
314 | core.symtab.leaveScope(); |
---|
315 | } |
---|
316 | |
---|
317 | template<typename core_t> |
---|
318 | static inline auto leave( core_t &, long ) {} |
---|
319 | |
---|
320 | // The symbol table has 2 kind of functions mostly, 1 argument and 2 arguments |
---|
321 | // Create macro to condense these common patterns |
---|
322 | #define SYMTAB_FUNC1( func, type ) \ |
---|
323 | template<typename core_t> \ |
---|
324 | static inline auto func( core_t & core, int, type arg ) -> decltype( core.symtab.func( arg ), void() ) {\ |
---|
325 | core.symtab.func( arg ); \ |
---|
326 | } \ |
---|
327 | \ |
---|
328 | template<typename core_t> \ |
---|
329 | static inline void func( core_t &, long, type ) {} |
---|
330 | |
---|
331 | #define SYMTAB_FUNC2( func, type1, type2 ) \ |
---|
332 | template<typename core_t> \ |
---|
333 | static inline auto func( core_t & core, int, type1 arg1, type2 arg2 ) -> decltype( core.symtab.func( arg1, arg2 ), void () ) {\ |
---|
334 | core.symtab.func( arg1, arg2 ); \ |
---|
335 | } \ |
---|
336 | \ |
---|
337 | template<typename core_t> \ |
---|
338 | static inline void func( core_t &, long, type1, type2 ) {} |
---|
339 | |
---|
340 | SYMTAB_FUNC1( addId , const DeclWithType * ); |
---|
341 | SYMTAB_FUNC1( addType , const NamedTypeDecl * ); |
---|
342 | SYMTAB_FUNC1( addStruct , const StructDecl * ); |
---|
343 | SYMTAB_FUNC1( addEnum , const EnumDecl * ); |
---|
344 | SYMTAB_FUNC1( addUnion , const UnionDecl * ); |
---|
345 | SYMTAB_FUNC1( addTrait , const TraitDecl * ); |
---|
346 | SYMTAB_FUNC2( addWith , const std::vector< ptr<Expr> > &, const Decl * ); |
---|
347 | |
---|
348 | // A few extra functions have more complicated behaviour, they are hand written |
---|
349 | template<typename core_t> |
---|
350 | static inline auto addStructFwd( core_t & core, int, const ast::StructDecl * decl ) -> decltype( core.symtab.addStruct( decl ), void() ) { |
---|
351 | ast::StructDecl * fwd = new ast::StructDecl( decl->location, decl->name ); |
---|
352 | fwd->params = decl->params; |
---|
353 | core.symtab.addStruct( fwd ); |
---|
354 | } |
---|
355 | |
---|
356 | template<typename core_t> |
---|
357 | static inline void addStructFwd( core_t &, long, const ast::StructDecl * ) {} |
---|
358 | |
---|
359 | template<typename core_t> |
---|
360 | static inline auto addUnionFwd( core_t & core, int, const ast::UnionDecl * decl ) -> decltype( core.symtab.addUnion( decl ), void() ) { |
---|
361 | UnionDecl * fwd = new UnionDecl( decl->location, decl->name ); |
---|
362 | fwd->params = decl->params; |
---|
363 | core.symtab.addUnion( fwd ); |
---|
364 | } |
---|
365 | |
---|
366 | template<typename core_t> |
---|
367 | static inline void addUnionFwd( core_t &, long, const ast::UnionDecl * ) {} |
---|
368 | |
---|
369 | template<typename core_t> |
---|
370 | static inline auto addStruct( core_t & core, int, const std::string & str ) -> decltype( core.symtab.addStruct( str ), void() ) { |
---|
371 | if ( ! core.symtab.lookupStruct( str ) ) { |
---|
372 | core.symtab.addStruct( str ); |
---|
373 | } |
---|
374 | } |
---|
375 | |
---|
376 | template<typename core_t> |
---|
377 | static inline void addStruct( core_t &, long, const std::string & ) {} |
---|
378 | |
---|
379 | template<typename core_t> |
---|
380 | static inline auto addUnion( core_t & core, int, const std::string & str ) -> decltype( core.symtab.addUnion( str ), void() ) { |
---|
381 | if ( ! core.symtab.lookupUnion( str ) ) { |
---|
382 | core.symtab.addUnion( str ); |
---|
383 | } |
---|
384 | } |
---|
385 | |
---|
386 | template<typename core_t> |
---|
387 | static inline void addUnion( core_t &, long, const std::string & ) {} |
---|
388 | |
---|
389 | #undef SYMTAB_FUNC1 |
---|
390 | #undef SYMTAB_FUNC2 |
---|
391 | } // namespace symtab |
---|
392 | |
---|
393 | // Some passes need to mutate TypeDecl and properly update their pointing TypeInstType. |
---|
394 | // Detect the presence of a member name `subs` and call all members appropriately |
---|
395 | namespace forall { |
---|
396 | // Some simple scoping rules |
---|
397 | template<typename core_t> |
---|
398 | static inline auto enter( core_t & core, int, const ast::FunctionType * type ) |
---|
399 | -> decltype( core.subs, void() ) { |
---|
400 | if ( ! type->forall.empty() ) core.subs.beginScope(); |
---|
401 | } |
---|
402 | |
---|
403 | template<typename core_t> |
---|
404 | static inline auto enter( core_t &, long, const ast::FunctionType * ) {} |
---|
405 | |
---|
406 | template<typename core_t> |
---|
407 | static inline auto leave( core_t & core, int, const ast::FunctionType * type ) |
---|
408 | -> decltype( core.subs, void() ) { |
---|
409 | if ( ! type->forall.empty() ) { core.subs.endScope(); } |
---|
410 | } |
---|
411 | |
---|
412 | template<typename core_t> |
---|
413 | static inline auto leave( core_t &, long, const ast::FunctionType * ) {} |
---|
414 | |
---|
415 | // Replaces a TypeInstType's base TypeDecl according to the table |
---|
416 | template<typename core_t> |
---|
417 | static inline auto replace( core_t & core, int, const ast::TypeInstType *& inst ) |
---|
418 | -> decltype( core.subs, void() ) { |
---|
419 | inst = ast::mutate_field( |
---|
420 | inst, &ast::TypeInstType::base, core.subs.replace( inst->base ) ); |
---|
421 | } |
---|
422 | |
---|
423 | template<typename core_t> |
---|
424 | static inline auto replace( core_t &, long, const ast::TypeInstType *& ) {} |
---|
425 | |
---|
426 | } // namespace forall |
---|
427 | |
---|
428 | template<typename core_t> |
---|
429 | static inline auto get_result( core_t & core, char ) -> decltype( core.result() ) { |
---|
430 | return core.result(); |
---|
431 | } |
---|
432 | |
---|
433 | template<typename core_t> |
---|
434 | static inline auto get_result( core_t & core, int ) -> decltype( core.result ) { |
---|
435 | return core.result; |
---|
436 | } |
---|
437 | |
---|
438 | template<typename core_t> |
---|
439 | static inline void get_result( core_t &, long ) {} |
---|
440 | } // namespace __pass |
---|
441 | } // namespace ast |
---|