Changeset 6ca154b for src/Common


Ignore:
Timestamp:
Jun 22, 2017, 4:32:15 PM (7 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
653f2c7
Parents:
186b398
Message:

PassVisitor? now supports declarations to add

Location:
src/Common
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/Common/PassVisitor.h

    r186b398 r6ca154b  
    2525// | WithStmtsToAdd       - provides the ability to insert statements before or after the current statement by adding new statements into
    2626//                          stmtsToAddBefore or stmtsToAddAfter respectively.
    27 // | WithShortCircuiting  - provides the ability to skip visiting child nodes; set skip_children to true if pre{visit,mutate} to skip visiting children
     27// | WithShortCircuiting  - provides the ability to skip visiting child nodes; set visit_children to false in pre{visit,mutate} to skip visiting children
    2828// | WithScopes           - provides the ability to save/restore data like a LIFO stack; to save, call GuardValue with the variable to save, the variable
    2929//                          will automatically be restored to its previous value after the corresponding postvisit/postmutate teminates.
     
    220220
    221221private:
     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
    222225        template<typename node_type> void call_previsit ( node_type * node ) { previsit_impl ( pass, node, 0 ); }
    223226        template<typename node_type> void call_postvisit( node_type * node ) { postvisit_impl( pass, node, 0 ); }
     
    231234        void set_env( TypeSubstitution * env ) { set_env_impl( pass, env, 0); }
    232235
    233         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 );
    234239        void mutateStatementList( std::list< Statement* > &statements );
    235240
    236         Statement * visitStatement( Statement * stmt );
     241        template< typename func_t >
     242        Statement * handleStatement( Statement * stmt, func_t func );
     243        Statement * visitStatement ( Statement * stmt );
    237244        Statement * mutateStatement( Statement * stmt );
    238245
    239         void visitExpression( Expression * expr );
     246        template< typename func_t >
     247        Expression * handleExpression( Expression * expr, func_t func );
     248        Expression * visitExpression ( Expression * expr );
    240249        Expression * mutateExpression( Expression * expr );
    241250
  • src/Common/PassVisitor.impl.h

    r186b398 r6ca154b  
    4444}
    4545
    46 typedef std::list< Statement * > StmtList_t;
    47 
    48 template< typename pass_type >
    49 void PassVisitor< pass_type >::visitStatementList( std::list< Statement * > & statements ) {
     46typedef std::list< Statement   * > StmtList_t;
     47typedef std::list< Declaration * > DeclList_t;
     48
     49template<typename iterator_t>
     50static inline void splice( iterator_t it, DeclList_t * decls ) {
     51        std::transform(
     52                decls->begin(),
     53                decls->end(),
     54                it,
     55                [](Declaration * decl) -> auto {
     56                        return new DeclStmt( noLabels, decl );
     57                }
     58        );
     59        decls->clear();
     60}
     61
     62template< typename pass_type >
     63static inline void acceptAll( std::list< Declaration* > &decls, PassVisitor< pass_type >& visitor ) {
     64
     65        DeclList_t* beforeDecls = visitor.get_beforeDecls();
     66        DeclList_t* afterDecls  = visitor.get_afterDecls();
     67
     68        for ( std::list< Declaration* >::iterator i = decls.begin(); ; ++i ) {
     69                // splice in new declarations after previous decl
     70                if ( !empty( afterDecls ) ) { decls.splice( i, *afterDecls ); }
     71
     72                if ( i == decls.end() ) break;
     73
     74                // run mutator on declaration
     75                maybeAccept( *i, visitor );
     76
     77                // splice in new declarations before current decl
     78                if ( !empty( beforeDecls ) ) { decls.splice( i, *beforeDecls ); }
     79        }
     80}
     81
     82template< typename pass_type >
     83static inline void mutateAll( std::list< Declaration* > &decls, PassVisitor< pass_type >& mutator ) {
     84
     85        DeclList_t* beforeDecls = mutator.get_beforeDecls();
     86        DeclList_t* afterDecls  = mutator.get_afterDecls();
     87
     88        for ( std::list< Declaration* >::iterator i = decls.begin(); ; ++i ) {
     89                // splice in new declarations after previous decl
     90                if ( !empty( afterDecls ) ) { decls.splice( i, *afterDecls ); }
     91
     92                if ( i == decls.end() ) break;
     93
     94                // run mutator on declaration
     95                *i = maybeMutate( *i, mutator );
     96
     97                // splice in new declarations before current decl
     98                if ( !empty( beforeDecls ) ) { decls.splice( i, *beforeDecls ); }
     99        }
     100}
     101
     102template< typename pass_type >
     103template< typename func_t >
     104void PassVisitor< pass_type >::handleStatementList( std::list< Statement * > & statements, func_t func ) {
    50105        SemanticError errors;
    51106
    52107        StmtList_t* beforeStmts = get_beforeStmts();
    53108        StmtList_t* afterStmts  = get_afterStmts();
     109        DeclList_t* beforeDecls = get_beforeDecls();
     110        DeclList_t* afterDecls  = get_afterDecls();
    54111
    55112        for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) {
     113
     114                if ( !empty( afterDecls ) ) { splice( std::inserter( statements, i ), afterDecls ); }
    56115                if ( !empty( afterStmts ) ) { statements.splice( i, *afterStmts ); }
     116
    57117                try {
    58                         (*i)->accept( *this );
     118                        func( *i );
     119                        assert(( empty( beforeStmts ) && empty( afterStmts ))
     120                            || ( empty( beforeDecls ) && empty( afterDecls )) );
     121
    59122                } catch ( SemanticError &e ) {
    60123                        errors.append( e );
    61124                }
     125
     126                if ( !empty( beforeDecls ) ) { splice( std::inserter( statements, i ), beforeDecls ); }
    62127                if ( !empty( beforeStmts ) ) { statements.splice( i, *beforeStmts ); }
    63128        }
    64129
     130        if ( !empty( afterDecls ) ) { splice( std::back_inserter( statements ), afterDecls); }
    65131        if ( !empty( afterStmts ) ) { statements.splice( statements.end(), *afterStmts ); }
    66132        if ( !errors.isEmpty() ) { throw errors; }
     
    68134
    69135template< typename pass_type >
     136void PassVisitor< pass_type >::visitStatementList( std::list< Statement * > & statements ) {
     137        handleStatementList( statements, [this]( Statement * stmt) {
     138                stmt->accept( *this );
     139        });
     140}
     141
     142template< typename pass_type >
    70143void PassVisitor< pass_type >::mutateStatementList( std::list< Statement * > & statements ) {
    71         SemanticError errors;
     144        handleStatementList( statements, [this]( Statement *& stmt) {
     145                stmt = stmt->acceptMutator( *this );
     146        });
     147}
     148
     149
     150template< typename pass_type >
     151template< typename func_t >
     152Statement * PassVisitor< pass_type >::handleStatement( Statement * stmt, func_t func ) {
     153        // don't want statements from outer CompoundStmts to be added to this CompoundStmt
     154        ValueGuardPtr< TypeSubstitution * >  oldEnv        ( get_env_ptr    () );
     155        ValueGuardPtr< DeclList_t >          oldBeforeDecls( get_beforeDecls() );
     156        ValueGuardPtr< DeclList_t >          oldAfterDecls ( get_afterDecls () );
     157        ValueGuardPtr< StmtList_t >          oldBeforeStmts( get_beforeStmts() );
     158        ValueGuardPtr< StmtList_t >          oldAfterStmts ( get_afterStmts () );
     159
     160        Statement *newStmt = func( stmt );
    72161
    73162        StmtList_t* beforeStmts = get_beforeStmts();
    74163        StmtList_t* afterStmts  = get_afterStmts();
    75 
    76         for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) {
    77                 if ( !empty( afterStmts ) ) { statements.splice( i, *afterStmts ); }
    78                 try {
    79                         *i = (*i)->acceptMutator( *this );
    80                 } catch ( SemanticError &e ) {
    81                         errors.append( e );
    82                 }
    83                 if ( !empty( beforeStmts ) ) { statements.splice( i, *beforeStmts ); }
    84         }
    85 
    86         if ( !empty( afterStmts ) ) { statements.splice( statements.end(), *afterStmts ); }
    87         if ( !errors.isEmpty() ) { throw errors; }
    88 }
    89 
    90 template< typename pass_type >
    91 Statement * PassVisitor< pass_type >::visitStatement( Statement * stmt ) {
    92         // don't want statements from outer CompoundStmts to be added to this CompoundStmt
    93         ValueGuardPtr< TypeSubstitution * >      oldEnv        ( get_env_ptr() );
    94         ValueGuardPtr< std::list< Statement* > > oldBeforeStmts( get_beforeStmts() );
    95         ValueGuardPtr< std::list< Statement* > > oldAfterStmts ( get_afterStmts () );
    96 
    97         maybeAccept( stmt, *this );
    98 
    99         StmtList_t* beforeStmts = get_beforeStmts();
    100         StmtList_t* afterStmts  = get_afterStmts();
    101 
    102         if( empty(beforeStmts) && empty(afterStmts) ) { return stmt; }
     164        DeclList_t* beforeDecls = get_beforeDecls();
     165        DeclList_t* afterDecls  = get_afterDecls();
     166
     167        if( empty(beforeStmts) && empty(afterStmts) && empty(beforeDecls) && empty(afterDecls) ) { return newStmt; }
     168        assert(( empty( beforeStmts ) && empty( afterStmts ))
     169            || ( empty( beforeDecls ) && empty( afterDecls )) );
    103170
    104171        CompoundStmt *compound = new CompoundStmt( noLabels );
     172        if( !empty(beforeDecls) ) { splice( std::back_inserter( compound->get_kids() ), beforeDecls ); }
    105173        if( !empty(beforeStmts) ) { compound->get_kids().splice( compound->get_kids().end(), *beforeStmts ); }
    106         compound->get_kids().push_back( stmt );
     174        compound->get_kids().push_back( newStmt );
     175        if( !empty(afterDecls) ) { splice( std::back_inserter( compound->get_kids() ), afterDecls ); }
    107176        if( !empty(afterStmts) ) { compound->get_kids().splice( compound->get_kids().end(), *afterStmts ); }
    108177        return compound;
     
    110179
    111180template< typename pass_type >
     181Statement * PassVisitor< pass_type >::visitStatement( Statement * stmt ) {
     182        return handleStatement( stmt, [this]( Statement * stmt ) {
     183                maybeAccept( stmt, *this );
     184                return stmt;
     185        });
     186}
     187
     188template< typename pass_type >
    112189Statement * PassVisitor< pass_type >::mutateStatement( Statement * stmt ) {
    113         // don't want statements from outer CompoundStmts to be added to this CompoundStmt
    114         ValueGuardPtr< TypeSubstitution * >      oldEnv        ( get_env_ptr() );
    115         ValueGuardPtr< std::list< Statement* > > oldBeforeStmts( get_beforeStmts() );
    116         ValueGuardPtr< std::list< Statement* > > oldAfterStmts ( get_afterStmts () );
    117 
    118         Statement *newStmt = maybeMutate( stmt, *this );
    119 
    120         StmtList_t* beforeStmts = get_beforeStmts();
    121         StmtList_t* afterStmts  = get_afterStmts();
    122 
    123         if( empty(beforeStmts) && empty(afterStmts) ) { return newStmt; }
    124 
    125         CompoundStmt *compound = new CompoundStmt( noLabels );
    126         if( !empty(beforeStmts) ) { compound->get_kids().splice( compound->get_kids().end(), *beforeStmts ); }
    127         compound->get_kids().push_back( newStmt );
    128         if( !empty(afterStmts) ) { compound->get_kids().splice( compound->get_kids().end(), *afterStmts ); }
    129         return compound;
    130 }
    131 
    132 
    133 
    134 template< typename pass_type >
    135 void PassVisitor< pass_type >::visitExpression( Expression * expr ) {
    136         if( !expr ) return;
     190        return handleStatement( stmt, [this]( Statement * stmt ) {
     191                return maybeMutate( stmt, *this );
     192        });
     193}
     194
     195template< typename pass_type >
     196template< typename func_t >
     197Expression * PassVisitor< pass_type >::handleExpression( Expression * expr, func_t func ) {
     198        if( !expr ) return nullptr;
    137199
    138200        auto env_ptr = get_env_ptr();
     
    140202                *env_ptr = expr->get_env();
    141203        }
    142         // xxx - should env be cloned (or moved) onto the result of the mutate?
    143         expr->accept( *this );
     204
     205        // should env be cloned (or moved) onto the result of the mutate?
     206        return func( expr );
     207}
     208
     209template< typename pass_type >
     210Expression * PassVisitor< pass_type >::visitExpression( Expression * expr ) {
     211        return handleExpression(expr, [this]( Expression * expr ) {
     212                expr->accept( *this );
     213                return expr;
     214        });             
    144215}
    145216
    146217template< typename pass_type >
    147218Expression * PassVisitor< pass_type >::mutateExpression( Expression * expr ) {
    148         if( !expr ) return nullptr;
    149 
    150         auto env_ptr = get_env_ptr();
    151         if ( env_ptr && expr->get_env() ) {
    152                 *env_ptr = expr->get_env();
    153         }
    154         // xxx - should env be cloned (or moved) onto the result of the mutate?
    155         return expr->acceptMutator( *this );
    156 }
    157 
     219        return handleExpression(expr, [this]( Expression * expr ) {
     220                return expr->acceptMutator( *this );
     221        });
     222}
    158223
    159224//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     
    233298void PassVisitor< pass_type >::visit( ExprStmt * node ) {
    234299        VISIT_START( node );
    235         call_beginScope();
    236300
    237301        visitExpression( node->get_expr() );
    238302
    239         call_endScope();
    240303        VISIT_END( node );
    241304}
     
    250313}
    251314
     315//--------------------------------------------------------------------------
     316// AsmStmt
    252317template< typename pass_type >
    253318void PassVisitor< pass_type >::visit( AsmStmt * node ) {
    254319        VISIT_BODY( node );
     320}
     321
     322template< typename pass_type >
     323Statement * PassVisitor< pass_type >::mutate( AsmStmt * node ) {
     324        MUTATE_BODY( Statement, node );
    255325}
    256326
     
    302372
    303373//--------------------------------------------------------------------------
    304 // WhileStmt
     374// ForStmt
    305375template< typename pass_type >
    306376void PassVisitor< pass_type >::visit( ForStmt * node ) {
     
    371441}
    372442
     443//--------------------------------------------------------------------------
     444// BranchStmt
    373445template< typename pass_type >
    374446void PassVisitor< pass_type >::visit( BranchStmt * node ) {
    375447        VISIT_BODY( node );
     448}
     449
     450template< typename pass_type >
     451Statement * PassVisitor< pass_type >::mutate( BranchStmt * node ) {
     452        MUTATE_BODY( Statement, node );
    376453}
    377454
     
    417494        maybeAccept( node->get_block(), *this );
    418495        acceptAll( node->get_catchers(), *this );
     496        maybeAccept( node->get_finally(), *this );
    419497
    420498        VISIT_END( node );
     
    427505        node->set_block(  maybeMutate( node->get_block(), *this ) );
    428506        mutateAll( node->get_catchers(), *this );
     507        node->set_finally( maybeMutate( node->get_finally(), *this ) );
    429508
    430509        MUTATE_END( Statement, node );
     
    437516        VISIT_START( node );
    438517
     518        maybeAccept( node->get_decl(), *this );
     519        node->set_cond( visitExpression( node->get_cond() ) );
    439520        node->set_body( visitStatement( node->get_body() ) );
    440         maybeAccept( node->get_decl(), *this );
    441521
    442522        VISIT_END( node );
     
    447527        MUTATE_START( node );
    448528
    449         node->set_body(  mutateStatement( node->get_body() ) );
    450         node->set_decl(  maybeMutate( node->get_decl(), *this ) );
     529        node->set_decl( maybeMutate( node->get_decl(), *this ) );
     530        node->set_cond( mutateExpression( node->get_cond() ) );
     531        node->set_body( mutateStatement( node->get_body() ) );
    451532
    452533        MUTATE_END( Statement, node );
     
    840921
    841922template< typename pass_type >
    842 Statement * PassVisitor< pass_type >::mutate( AsmStmt * node ) {
    843         MUTATE_BODY( Statement, node );
    844 }
    845 
    846 template< typename pass_type >
    847 Statement * PassVisitor< pass_type >::mutate( BranchStmt * node ) {
    848         MUTATE_BODY( Statement, node );
    849 }
    850 
    851 template< typename pass_type >
    852923Statement * PassVisitor< pass_type >::mutate( FinallyStmt * node ) {
    853924        MUTATE_BODY( Statement, node );
Note: See TracChangeset for help on using the changeset viewer.