Changeset e67a82d for src/AST/Pass.hpp


Ignore:
Timestamp:
Aug 20, 2020, 11:48:15 PM (4 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
d685cb0
Parents:
67ca73e (diff), 013b028 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

fix conflicts

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Pass.hpp

    r67ca73e re67a82d  
    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
    3739// Private prelude header, needed for some of the magic tricks this class pulls off
    3840#include "AST/Pass.proto.hpp"
     
    4648//
    4749// Several additional features are available through inheritance
    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
     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
    6467//-------------------------------------------------------------------------------------------------
    65 template< typename pass_t >
     68template< typename core_t >
    6669class Pass final : public ast::Visitor {
    6770public:
     71        using core_type = core_t;
     72        using type = Pass<core_t>;
     73
    6874        /// Forward any arguments to the pass constructor
    6975        /// Propagate 'this' if necessary
    7076        template< typename... Args >
    7177        Pass( Args &&... args)
    72                 : pass( std::forward<Args>( args )... )
     78                : core( std::forward<Args>( args )... )
    7379        {
    7480                // After the pass is constructed, check if it wants the have a pointer to the wrapping visitor
    75                 typedef Pass<pass_t> this_t;
    76                 this_t * const * visitor = __pass::visitor(pass, 0);
     81                type * const * visitor = __pass::visitor(core, 0);
    7782                if(visitor) {
    78                         *const_cast<this_t **>( visitor ) = this;
     83                        *const_cast<type **>( visitor ) = this;
    7984                }
    8085        }
     
    8287        virtual ~Pass() = default;
    8388
     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
    84102        /// Storage for the actual pass
    85         pass_t pass;
     103        core_t core;
    86104
    87105        /// Visit function declarations
     
    179197        const ast::TypeSubstitution * visit( const ast::TypeSubstitution     * ) override final;
    180198
    181         template<typename pass_type>
    182         friend void accept_all( std::list< ptr<Decl> > & decls, Pass<pass_type>& visitor );
     199        template<typename core_type>
     200        friend void accept_all( std::list< ptr<Decl> > & decls, Pass<core_type>& visitor );
    183201private:
    184202
    185         bool __visit_children() { __pass::bool_ref * ptr = __pass::visit_children(pass, 0); return ptr ? *ptr : true; }
     203        bool __visit_children() { __pass::bool_ref * ptr = __pass::visit_children(core, 0); return ptr ? *ptr : true; }
    186204
    187205private:
     
    202220        container_t< ptr<node_t> > call_accept( const container_t< ptr<node_t> > & container );
    203221
     222        /// Mutate forall-list, accounting for presence of type substitution map
     223        template<typename node_t>
     224        void mutate_forall( const node_t *& );
     225
    204226public:
    205227        /// Logic to call the accept and mutate the parent if needed, delegates call to accept
     
    210232        /// Internal RAII guard for symbol table features
    211233        struct guard_symtab {
    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;
     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;
    215237        };
    216238
    217239        /// Internal RAII guard for scope features
    218240        struct guard_scope {
    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;
     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;
    222253        };
    223254
     
    227258
    228259/// Apply a pass to an entire translation unit
    229 template<typename pass_t>
    230 void accept_all( std::list< ast::ptr<ast::Decl> > &, ast::Pass<pass_t> & visitor );
     260template<typename core_t>
     261void accept_all( std::list< ast::ptr<ast::Decl> > &, ast::Pass<core_t> & visitor );
    231262
    232263//-------------------------------------------------------------------------------------------------
     
    268299        };
    269300
    270         template< typename pass_t>
    271         friend auto __pass::at_cleanup( pass_t & pass, int ) -> decltype( &pass.at_cleanup );
     301        template< typename core_t>
     302        friend auto __pass::at_cleanup( core_t & core, int ) -> decltype( &core.at_cleanup );
    272303public:
    273304
     
    305336
    306337/// Used to get a pointer to the pass with its wrapped type
    307 template<typename pass_t>
     338template<typename core_t>
    308339struct WithVisitorRef {
    309         Pass<pass_t> * const visitor = nullptr;
     340        Pass<core_t> * const visitor = nullptr;
    310341};
    311342
     
    314345        SymbolTable symtab;
    315346};
     347
     348/// Use when the templated visitor needs to keep TypeInstType instances properly linked to TypeDecl
     349struct WithForallSubstitutor {
     350        ForallSubstitutionTable subs;
     351};
     352
    316353}
    317354
     
    321358extern struct PassVisitorStats {
    322359        size_t depth = 0;
    323         Stats::Counters::MaxCounter<double> * max = nullptr;
    324         Stats::Counters::AverageCounter<double> * avg = nullptr;
     360        Stats::Counters::MaxCounter<double> * max;
     361        Stats::Counters::AverageCounter<double> * avg;
    325362} pass_visitor_stats;
    326363}
Note: See TracChangeset for help on using the changeset viewer.