source: src/AST/Pass.proto.hpp @ 77a2994

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

Added the ast::Pass::read utility. Converted two passes to use its two different uses.

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