source: src/AST/Pass.proto.hpp @ 293dc1c

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 293dc1c was 293dc1c, checked in by Andrew Beach <ajbeach@…>, 3 years ago

TranslationUnit? is now used at the top-level of the new-ast passes.

  • Property mode set to 100644
File size: 15.1 KB
Line 
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
21namespace ast {
22template<typename core_t>
23class Pass;
24
25class TranslationUnit;
26
27struct PureVisitor;
28
29namespace __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( ((core_t*)nullptr)->previsit( (const node_t *)nullptr ) );
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        // Another feature of the templated visitor is that it calls beginScope()/endScope() for compound statement.
269        // All passes which have such functions are assumed desire this behaviour
270        // detect it using the same strategy
271        namespace scope {
272                template<typename core_t>
273                static inline auto enter( core_t & core, int ) -> decltype( core.beginScope(), void() ) {
274                        core.beginScope();
275                }
276
277                template<typename core_t>
278                static inline void enter( core_t &, long ) {}
279
280                template<typename core_t>
281                static inline auto leave( core_t & core, int ) -> decltype( core.endScope(), void() ) {
282                        core.endScope();
283                }
284
285                template<typename core_t>
286                static inline void leave( core_t &, long ) {}
287        } // namespace scope
288
289        // Certain passes desire an up to date symbol table automatically
290        // detect the presence of a member name `symtab` and call all the members appropriately
291        namespace symtab {
292                // Some simple scoping rules
293                template<typename core_t>
294                static inline auto enter( core_t & core, int ) -> decltype( core.symtab, void() ) {
295                        core.symtab.enterScope();
296                }
297
298                template<typename core_t>
299                static inline auto enter( core_t &, long ) {}
300
301                template<typename core_t>
302                static inline auto leave( core_t & core, int ) -> decltype( core.symtab, void() ) {
303                        core.symtab.leaveScope();
304                }
305
306                template<typename core_t>
307                static inline auto leave( core_t &, long ) {}
308
309                // The symbol table has 2 kind of functions mostly, 1 argument and 2 arguments
310                // Create macro to condense these common patterns
311                #define SYMTAB_FUNC1( func, type ) \
312                template<typename core_t> \
313                static inline auto func( core_t & core, int, type arg ) -> decltype( core.symtab.func( arg ), void() ) {\
314                        core.symtab.func( arg ); \
315                } \
316                \
317                template<typename core_t> \
318                static inline void func( core_t &, long, type ) {}
319
320                #define SYMTAB_FUNC2( func, type1, type2 ) \
321                template<typename core_t> \
322                static inline auto func( core_t & core, int, type1 arg1, type2 arg2 ) -> decltype( core.symtab.func( arg1, arg2 ), void () ) {\
323                        core.symtab.func( arg1, arg2 ); \
324                } \
325                        \
326                template<typename core_t> \
327                static inline void func( core_t &, long, type1, type2 ) {}
328
329                SYMTAB_FUNC1( addId     , const DeclWithType *  );
330                SYMTAB_FUNC1( addType   , const NamedTypeDecl * );
331                SYMTAB_FUNC1( addStruct , const StructDecl *    );
332                SYMTAB_FUNC1( addEnum   , const EnumDecl *      );
333                SYMTAB_FUNC1( addUnion  , const UnionDecl *     );
334                SYMTAB_FUNC1( addTrait  , const TraitDecl *     );
335                SYMTAB_FUNC2( addWith   , const std::vector< ptr<Expr> > &, const Decl * );
336
337                // A few extra functions have more complicated behaviour, they are hand written
338                template<typename core_t>
339                static inline auto addStructFwd( core_t & core, int, const ast::StructDecl * decl ) -> decltype( core.symtab.addStruct( decl ), void() ) {
340                        ast::StructDecl * fwd = new ast::StructDecl( decl->location, decl->name );
341                        fwd->params = decl->params;
342                        core.symtab.addStruct( fwd );
343                }
344
345                template<typename core_t>
346                static inline void addStructFwd( core_t &, long, const ast::StructDecl * ) {}
347
348                template<typename core_t>
349                static inline auto addUnionFwd( core_t & core, int, const ast::UnionDecl * decl ) -> decltype( core.symtab.addUnion( decl ), void() ) {
350                        UnionDecl * fwd = new UnionDecl( decl->location, decl->name );
351                        fwd->params = decl->params;
352                        core.symtab.addUnion( fwd );
353                }
354
355                template<typename core_t>
356                static inline void addUnionFwd( core_t &, long, const ast::UnionDecl * ) {}
357
358                template<typename core_t>
359                static inline auto addStruct( core_t & core, int, const std::string & str ) -> decltype( core.symtab.addStruct( str ), void() ) {
360                        if ( ! core.symtab.lookupStruct( str ) ) {
361                                core.symtab.addStruct( str );
362                        }
363                }
364
365                template<typename core_t>
366                static inline void addStruct( core_t &, long, const std::string & ) {}
367
368                template<typename core_t>
369                static inline auto addUnion( core_t & core, int, const std::string & str ) -> decltype( core.symtab.addUnion( str ), void() ) {
370                        if ( ! core.symtab.lookupUnion( str ) ) {
371                                core.symtab.addUnion( str );
372                        }
373                }
374
375                template<typename core_t>
376                static inline void addUnion( core_t &, long, const std::string & ) {}
377
378                #undef SYMTAB_FUNC1
379                #undef SYMTAB_FUNC2
380        } // namespace symtab
381
382        // Some passes need to mutate TypeDecl and properly update their pointing TypeInstType.
383        // Detect the presence of a member name `subs` and call all members appropriately
384        namespace forall {
385                // Some simple scoping rules
386                template<typename core_t>
387                static inline auto enter( core_t & core, int, const ast::ParameterizedType * type )
388                -> decltype( core.subs, void() ) {
389                        if ( ! type->forall.empty() ) core.subs.beginScope();
390                }
391
392                template<typename core_t>
393                static inline auto enter( core_t &, long, const ast::ParameterizedType * ) {}
394
395                template<typename core_t>
396                static inline auto leave( core_t & core, int, const ast::ParameterizedType * type )
397                -> decltype( core.subs, void() ) {
398                        if ( ! type->forall.empty() ) { core.subs.endScope(); }
399                }
400
401                template<typename core_t>
402                static inline auto leave( core_t &, long, const ast::ParameterizedType * ) {}
403
404                // Get the substitution table, if present
405                template<typename core_t>
406                static inline auto subs( core_t & core, int ) -> decltype( &core.subs ) {
407                        return &core.subs;
408                }
409
410                template<typename core_t>
411                static inline ast::ForallSubstitutionTable * subs( core_t &, long ) { return nullptr; }
412
413                // Replaces a TypeInstType's base TypeDecl according to the table
414                template<typename core_t>
415                static inline auto replace( core_t & core, int, const ast::TypeInstType *& inst )
416                -> decltype( core.subs, void() ) {
417                        inst = ast::mutate_field(
418                                inst, &ast::TypeInstType::base, core.subs.replace( inst->base ) );
419                }
420
421                template<typename core_t>
422                static inline auto replace( core_t &, long, const ast::TypeInstType *& ) {}
423
424        } // namespace forall
425
426        template<typename core_t>
427        static inline auto get_result( core_t & core, char ) -> decltype( core.result() ) {
428                return core.result();
429        }
430
431        template<typename core_t>
432        static inline auto get_result( core_t & core, int ) -> decltype( core.result ) {
433                return core.result;
434        }
435
436        template<typename core_t>
437        static inline void get_result( core_t &, long ) {}
438} // namespace __pass
439} // namespace ast
Note: See TracBrowser for help on using the repository browser.