Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Pass.hpp

    r0240cd69 r37cdd97  
    88//
    99// Author           : Thierry Delisle
    10 // Created On       : Thu May 09 15:37:05 2019
     10// Created On       : Thu May 09 15::37::05 2019
    1111// Last Modified By :
    1212// Last Modified On :
     
    3535#include "AST/SymbolTable.hpp"
    3636
    37 #include "AST/ForallSubstitutionTable.hpp"
    38 
    3937// Private prelude header, needed for some of the magic tricks this class pulls off
    4038#include "AST/Pass.proto.hpp"
     
    4846//
    4947// Several additional features are available through inheritance
    50 // | WithTypeSubstitution  - provides polymorphic const TypeSubstitution * env for the
    51 //                           current expression
    52 // | WithStmtsToAdd        - provides the ability to insert statements before or after the current
    53 //                           statement by adding new statements into stmtsToAddBefore or
    54 //                           stmtsToAddAfter respectively.
    55 // | WithDeclsToAdd        - provides the ability to insert declarations before or after the
    56 //                           current declarations by adding new DeclStmt into declsToAddBefore or
    57 //                           declsToAddAfter respectively.
    58 // | WithShortCircuiting   - provides the ability to skip visiting child nodes; set visit_children
    59 //                           to false in pre{visit,visit} to skip visiting children
    60 // | WithGuards            - provides the ability to save/restore data like a LIFO stack; to save,
    61 //                           call GuardValue with the variable to save, the variable will
    62 //                           automatically be restored to its previous value after the
    63 //                           corresponding postvisit/postmutate teminates.
    64 // | WithVisitorRef        - provides an pointer to the templated visitor wrapper
    65 // | WithSymbolTable       - provides symbol table functionality
    66 // | WithForallSubstitutor - maintains links between TypeInstType and TypeDecl under mutation
     48// | WithTypeSubstitution - provides polymorphic const TypeSubstitution * env for the
     49//                          current expression
     50// | WithStmtsToAdd       - provides the ability to insert statements before or after the current
     51//                          statement by adding new statements into stmtsToAddBefore or
     52//                          stmtsToAddAfter respectively.
     53// | WithDeclsToAdd       - provides the ability to insert declarations before or after the current
     54//                          declarations by adding new DeclStmt into declsToAddBefore or
     55//                          declsToAddAfter respectively.
     56// | WithShortCircuiting  - provides the ability to skip visiting child nodes; set visit_children
     57//                          to false in pre{visit,visit} to skip visiting children
     58// | WithGuards           - provides the ability to save/restore data like a LIFO stack; to save,
     59//                          call GuardValue with the variable to save, the variable will
     60//                          automatically be restored to its previous value after the corresponding
     61//                          postvisit/postmutate teminates.
     62// | WithVisitorRef       - provides an pointer to the templated visitor wrapper
     63// | WithSymbolTable      - provides symbol table functionality
    6764//-------------------------------------------------------------------------------------------------
    68 template< typename core_t >
     65template< typename pass_t >
    6966class Pass final : public ast::Visitor {
    7067public:
    71         using core_type = core_t;
    72         using type = Pass<core_t>;
    73 
    7468        /// Forward any arguments to the pass constructor
    7569        /// Propagate 'this' if necessary
    7670        template< typename... Args >
    7771        Pass( Args &&... args)
    78                 : core( std::forward<Args>( args )... )
     72                : pass( std::forward<Args>( args )... )
    7973        {
    8074                // After the pass is constructed, check if it wants the have a pointer to the wrapping visitor
    81                 type * const * visitor = __pass::visitor(core, 0);
     75                typedef Pass<pass_t> this_t;
     76                this_t * const * visitor = __pass::visitor(pass, 0);
    8277                if(visitor) {
    83                         *const_cast<type **>( visitor ) = this;
     78                        *const_cast<this_t **>( visitor ) = this;
    8479                }
    8580        }
     
    8782        virtual ~Pass() = default;
    8883
    89         /// Construct and run a pass on a translation unit.
    90         template< typename... Args >
    91         static void run( std::list< ptr<Decl> > & decls, Args &&... args ) {
    92                 Pass<core_t> visitor( std::forward<Args>( args )... );
    93                 accept_all( decls, visitor );
    94         }
    95 
    96         template< typename... Args >
    97         static void run( std::list< ptr<Decl> > & decls ) {
    98                 Pass<core_t> visitor;
    99                 accept_all( decls, visitor );
    100         }
    101 
    10284        /// Storage for the actual pass
    103         core_t core;
     85        pass_t pass;
    10486
    10587        /// Visit function declarations
     
    197179        const ast::TypeSubstitution * visit( const ast::TypeSubstitution     * ) override final;
    198180
    199         template<typename core_type>
    200         friend void accept_all( std::list< ptr<Decl> > & decls, Pass<core_type>& visitor );
     181        template<typename pass_type>
     182        friend void accept_all( std::list< ptr<Decl> > & decls, Pass<pass_type>& visitor );
    201183private:
    202184
    203         bool __visit_children() { __pass::bool_ref * ptr = __pass::visit_children(core, 0); return ptr ? *ptr : true; }
     185        bool __visit_children() { __pass::bool_ref * ptr = __pass::visit_children(pass, 0); return ptr ? *ptr : true; }
    204186
    205187private:
     
    220202        container_t< ptr<node_t> > call_accept( const container_t< ptr<node_t> > & container );
    221203
    222         /// Mutate forall-list, accounting for presence of type substitution map
    223         template<typename node_t>
    224         void mutate_forall( const node_t *& );
    225 
    226204public:
    227205        /// Logic to call the accept and mutate the parent if needed, delegates call to accept
     
    232210        /// Internal RAII guard for symbol table features
    233211        struct guard_symtab {
    234                 guard_symtab( Pass<core_t> & pass ): pass( pass ) { __pass::symtab::enter(pass.core, 0); }
    235                 ~guard_symtab()                                   { __pass::symtab::leave(pass.core, 0); }
    236                 Pass<core_t> & pass;
     212                guard_symtab( Pass<pass_t> & pass ): pass( pass ) { __pass::symtab::enter(pass, 0); }
     213                ~guard_symtab()                                   { __pass::symtab::leave(pass, 0); }
     214                Pass<pass_t> & pass;
    237215        };
    238216
    239217        /// Internal RAII guard for scope features
    240218        struct guard_scope {
    241                 guard_scope( Pass<core_t> & pass ): pass( pass ) { __pass::scope::enter(pass.core, 0); }
    242                 ~guard_scope()                                   { __pass::scope::leave(pass.core, 0); }
    243                 Pass<core_t> & pass;
    244         };
    245 
    246         /// Internal RAII guard for forall substitutions
    247         struct guard_forall_subs {
    248                 guard_forall_subs( Pass<core_t> & pass, const ParameterizedType * type )
    249                 : pass( pass ), type( type ) { __pass::forall::enter(pass.core, 0, type ); }
    250                 ~guard_forall_subs()         { __pass::forall::leave(pass.core, 0, type ); }
    251                 Pass<core_t> & pass;
    252                 const ParameterizedType * type;
     219                guard_scope( Pass<pass_t> & pass ): pass( pass ) { __pass::scope::enter(pass, 0); }
     220                ~guard_scope()                                   { __pass::scope::leave(pass, 0); }
     221                Pass<pass_t> & pass;
    253222        };
    254223
     
    258227
    259228/// Apply a pass to an entire translation unit
    260 template<typename core_t>
    261 void accept_all( std::list< ast::ptr<ast::Decl> > &, ast::Pass<core_t> & visitor );
     229template<typename pass_t>
     230void accept_all( std::list< ast::ptr<ast::Decl> > &, ast::Pass<pass_t> & visitor );
    262231
    263232//-------------------------------------------------------------------------------------------------
     
    299268        };
    300269
    301         template< typename core_t>
    302         friend auto __pass::at_cleanup( core_t & core, int ) -> decltype( &core.at_cleanup );
     270        template< typename pass_t>
     271        friend auto __pass::at_cleanup( pass_t & pass, int ) -> decltype( &pass.at_cleanup );
    303272public:
    304273
     
    336305
    337306/// Used to get a pointer to the pass with its wrapped type
    338 template<typename core_t>
     307template<typename pass_t>
    339308struct WithVisitorRef {
    340         Pass<core_t> * const visitor = nullptr;
     309        Pass<pass_t> * const visitor = nullptr;
    341310};
    342311
     
    345314        SymbolTable symtab;
    346315};
    347 
    348 /// Use when the templated visitor needs to keep TypeInstType instances properly linked to TypeDecl
    349 struct WithForallSubstitutor {
    350         ForallSubstitutionTable subs;
    351 };
    352 
    353316}
    354317
     
    358321extern struct PassVisitorStats {
    359322        size_t depth = 0;
    360         Stats::Counters::MaxCounter<double> * max;
    361         Stats::Counters::AverageCounter<double> * avg;
     323        Stats::Counters::MaxCounter<double> * max = nullptr;
     324        Stats::Counters::AverageCounter<double> * avg = nullptr;
    362325} pass_visitor_stats;
    363326}
Note: See TracChangeset for help on using the changeset viewer.