Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/PassVisitor.h

    r4eb31f2b r6ca154b  
    1818// Templated visitor type
    1919// To use declare a PassVisitor< YOUR VISITOR TYPE >
    20 // The visitor type should specify the previsit/postvisit for types that are desired.
     20// The visitor type should specify the previsit/postvisit/premutate/postmutate for types that are desired.
     21// Note: previsit/postvisit/premutate/postmutate must be **public** members
     22//
     23// Several additional features are available through inheritance
     24// | WithTypeSubstitution - provides polymorphic TypeSubstitution * env for the current expression
     25// | WithStmtsToAdd       - provides the ability to insert statements before or after the current statement by adding new statements into
     26//                          stmtsToAddBefore or stmtsToAddAfter respectively.
     27// | WithShortCircuiting  - provides the ability to skip visiting child nodes; set visit_children to false in pre{visit,mutate} to skip visiting children
     28// | WithScopes           - provides the ability to save/restore data like a LIFO stack; to save, call GuardValue with the variable to save, the variable
     29//                          will automatically be restored to its previous value after the corresponding postvisit/postmutate teminates.
    2130//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    2231template< typename pass_type >
     
    2837        PassVisitor(Args &&... args)
    2938                : pass( std::forward<Args>( args )... )
    30         {}
     39        {
     40                typedef PassVisitor<pass_type> this_t;
     41                this_t * const * visitor = visitor_impl(pass, 0);
     42                if(visitor) {
     43                        *const_cast<this_t **>( visitor ) = this;
     44                }
     45        }
    3146
    3247        virtual ~PassVisitor() = default;
     
    86101        virtual void visit( ConstructorExpr * ctorExpr ) override final;
    87102        virtual void visit( CompoundLiteralExpr *compLitExpr ) override final;
    88         virtual void visit( UntypedValofExpr *valofExpr ) override final;
    89103        virtual void visit( RangeExpr *rangeExpr ) override final;
    90104        virtual void visit( UntypedTupleExpr *tupleExpr ) override final;
     
    172186        virtual Expression* mutate( ConstructorExpr *ctorExpr ) override final;
    173187        virtual Expression* mutate( CompoundLiteralExpr *compLitExpr ) override final;
    174         virtual Expression* mutate( UntypedValofExpr *valofExpr ) override final;
    175188        virtual Expression* mutate( RangeExpr *rangeExpr ) override final;
    176189        virtual Expression* mutate( UntypedTupleExpr *tupleExpr ) override final;
     
    207220
    208221private:
     222        template<typename pass_t> friend void acceptAll( std::list< Declaration* > &decls, PassVisitor< pass_t >& visitor );
     223        template<typename pass_t> friend void mutateAll( std::list< Declaration* > &decls, PassVisitor< pass_t >& visitor );
     224
    209225        template<typename node_type> void call_previsit ( node_type * node ) { previsit_impl ( pass, node, 0 ); }
    210226        template<typename node_type> void call_postvisit( node_type * node ) { postvisit_impl( pass, node, 0 ); }
     
    218234        void set_env( TypeSubstitution * env ) { set_env_impl( pass, env, 0); }
    219235
    220         void visitStatementList( std::list< Statement* > &statements );
     236        template< typename func_t >
     237        void handleStatementList( std::list< Statement * > & statements, func_t func );
     238        void visitStatementList ( std::list< Statement* > &statements );
    221239        void mutateStatementList( std::list< Statement* > &statements );
    222240
    223         Statement * visitStatement( Statement * stmt );
     241        template< typename func_t >
     242        Statement * handleStatement( Statement * stmt, func_t func );
     243        Statement * visitStatement ( Statement * stmt );
    224244        Statement * mutateStatement( Statement * stmt );
    225245
    226         void visitExpression( Expression * expr );
     246        template< typename func_t >
     247        Expression * handleExpression( Expression * expr, func_t func );
     248        Expression * visitExpression ( Expression * expr );
    227249        Expression * mutateExpression( Expression * expr );
    228250
     
    231253        std::list< Statement* > *       get_beforeStmts() { return stmtsToAddBefore_impl( pass, 0); }
    232254        std::list< Statement* > *       get_afterStmts () { return stmtsToAddAfter_impl ( pass, 0); }
    233         bool visit_children() { bool* skip = skip_children_impl(pass, 0); return ! (skip && *skip); }
    234         void reset_visit() { bool* skip = skip_children_impl(pass, 0); if(skip) *skip = false; }
     255        std::list< Declaration* > *     get_beforeDecls() { return declsToAddBefore_impl( pass, 0); }
     256        std::list< Declaration* > *     get_afterDecls () { return declsToAddAfter_impl ( pass, 0); }
     257
     258        void set_visit_children( bool& ref ) { bool_ref * ptr = visit_children_impl(pass, 0); if(ptr) ptr->set( ref ); }
    235259
    236260        guard_value_impl init_guard() {
     
    271295        std::list< Statement* > stmtsToAddAfter;
    272296};
    273 
    274297class WithShortCircuiting {
    275298protected:
     
    278301
    279302public:
    280         bool skip_children;
     303        bool_ref visit_children;
    281304};
    282305
     
    297320};
    298321
     322template<typename pass_type>
     323class WithVisitorRef {
     324protected:
     325        WithVisitorRef() = default;
     326        ~WithVisitorRef() = default;
     327
     328public:
     329        PassVisitor<pass_type> * const visitor;
     330};
    299331
    300332#include "PassVisitor.impl.h"
Note: See TracChangeset for help on using the changeset viewer.