Changes in / [653f2c7:ba912706]


Ignore:
Location:
src/Common
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/Common/PassVisitor.h

    r653f2c7 rba912706  
    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 visit_children to false in pre{visit,mutate} to skip visiting children
     27// | WithShortCircuiting  - provides the ability to skip visiting child nodes; set skip_children to true if 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 
    225222        template<typename node_type> void call_previsit ( node_type * node ) { previsit_impl ( pass, node, 0 ); }
    226223        template<typename node_type> void call_postvisit( node_type * node ) { postvisit_impl( pass, node, 0 ); }
     
    234231        void set_env( TypeSubstitution * env ) { set_env_impl( pass, env, 0); }
    235232
    236         template< typename func_t >
    237         void handleStatementList( std::list< Statement * > & statements, func_t func );
    238         void visitStatementList ( std::list< Statement* > &statements );
     233        void visitStatementList( std::list< Statement* > &statements );
    239234        void mutateStatementList( std::list< Statement* > &statements );
    240235
    241         template< typename func_t >
    242         Statement * handleStatement( Statement * stmt, func_t func );
    243         Statement * visitStatement ( Statement * stmt );
     236        Statement * visitStatement( Statement * stmt );
    244237        Statement * mutateStatement( Statement * stmt );
    245238
    246         template< typename func_t >
    247         Expression * handleExpression( Expression * expr, func_t func );
    248         Expression * visitExpression ( Expression * expr );
     239        void visitExpression( Expression * expr );
    249240        Expression * mutateExpression( Expression * expr );
    250241
  • src/Common/PassVisitor.impl.h

    r653f2c7 rba912706  
    4444}
    4545
    46 typedef std::list< Statement   * > StmtList_t;
    47 typedef std::list< Declaration * > DeclList_t;
    48 
    49 template<typename iterator_t>
    50 static 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 
    62 template< typename pass_type >
    63 static 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 
    82 template< typename pass_type >
    83 static 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 
    102 template< typename pass_type >
    103 template< typename func_t >
    104 void PassVisitor< pass_type >::handleStatementList( std::list< Statement * > & statements, func_t func ) {
     46typedef std::list< Statement * > StmtList_t;
     47
     48template< typename pass_type >
     49void PassVisitor< pass_type >::visitStatementList( std::list< Statement * > & statements ) {
    10550        SemanticError errors;
    10651
    10752        StmtList_t* beforeStmts = get_beforeStmts();
    10853        StmtList_t* afterStmts  = get_afterStmts();
    109         DeclList_t* beforeDecls = get_beforeDecls();
    110         DeclList_t* afterDecls  = get_afterDecls();
    11154
    11255        for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) {
    113 
    114                 if ( !empty( afterDecls ) ) { splice( std::inserter( statements, i ), afterDecls ); }
    11556                if ( !empty( afterStmts ) ) { statements.splice( i, *afterStmts ); }
    116 
    11757                try {
    118                         func( *i );
    119                         assert(( empty( beforeStmts ) && empty( afterStmts ))
    120                             || ( empty( beforeDecls ) && empty( afterDecls )) );
    121 
     58                        (*i)->accept( *this );
    12259                } catch ( SemanticError &e ) {
    12360                        errors.append( e );
    12461                }
    125 
    126                 if ( !empty( beforeDecls ) ) { splice( std::inserter( statements, i ), beforeDecls ); }
    12762                if ( !empty( beforeStmts ) ) { statements.splice( i, *beforeStmts ); }
    12863        }
    12964
    130         if ( !empty( afterDecls ) ) { splice( std::back_inserter( statements ), afterDecls); }
    13165        if ( !empty( afterStmts ) ) { statements.splice( statements.end(), *afterStmts ); }
    13266        if ( !errors.isEmpty() ) { throw errors; }
     
    13468
    13569template< typename pass_type >
    136 void PassVisitor< pass_type >::visitStatementList( std::list< Statement * > & statements ) {
    137         handleStatementList( statements, [this]( Statement * stmt) {
    138                 stmt->accept( *this );
    139         });
    140 }
    141 
    142 template< typename pass_type >
    14370void PassVisitor< pass_type >::mutateStatementList( std::list< Statement * > & statements ) {
    144         handleStatementList( statements, [this]( Statement *& stmt) {
    145                 stmt = stmt->acceptMutator( *this );
    146         });
    147 }
    148 
    149 
    150 template< typename pass_type >
    151 template< typename func_t >
    152 Statement * 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 );
     71        SemanticError errors;
    16172
    16273        StmtList_t* beforeStmts = get_beforeStmts();
    16374        StmtList_t* afterStmts  = get_afterStmts();
    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 )) );
     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
     90template< typename pass_type >
     91Statement * 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; }
    170103
    171104        CompoundStmt *compound = new CompoundStmt( noLabels );
    172         if( !empty(beforeDecls) ) { splice( std::back_inserter( compound->get_kids() ), beforeDecls ); }
     105        if( !empty(beforeStmts) ) { compound->get_kids().splice( compound->get_kids().end(), *beforeStmts ); }
     106        compound->get_kids().push_back( stmt );
     107        if( !empty(afterStmts) ) { compound->get_kids().splice( compound->get_kids().end(), *afterStmts ); }
     108        return compound;
     109}
     110
     111template< typename pass_type >
     112Statement * 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 );
    173126        if( !empty(beforeStmts) ) { compound->get_kids().splice( compound->get_kids().end(), *beforeStmts ); }
    174127        compound->get_kids().push_back( newStmt );
    175         if( !empty(afterDecls) ) { splice( std::back_inserter( compound->get_kids() ), afterDecls ); }
    176128        if( !empty(afterStmts) ) { compound->get_kids().splice( compound->get_kids().end(), *afterStmts ); }
    177129        return compound;
    178130}
    179131
    180 template< typename pass_type >
    181 Statement * PassVisitor< pass_type >::visitStatement( Statement * stmt ) {
    182         return handleStatement( stmt, [this]( Statement * stmt ) {
    183                 maybeAccept( stmt, *this );
    184                 return stmt;
    185         });
    186 }
    187 
    188 template< typename pass_type >
    189 Statement * PassVisitor< pass_type >::mutateStatement( Statement * stmt ) {
    190         return handleStatement( stmt, [this]( Statement * stmt ) {
    191                 return maybeMutate( stmt, *this );
    192         });
    193 }
    194 
    195 template< typename pass_type >
    196 template< typename func_t >
    197 Expression * PassVisitor< pass_type >::handleExpression( Expression * expr, func_t func ) {
    198         if( !expr ) return nullptr;
     132
     133
     134template< typename pass_type >
     135void PassVisitor< pass_type >::visitExpression( Expression * expr ) {
     136        if( !expr ) return;
    199137
    200138        auto env_ptr = get_env_ptr();
     
    202140                *env_ptr = expr->get_env();
    203141        }
    204 
    205         // should env be cloned (or moved) onto the result of the mutate?
    206         return func( expr );
    207 }
    208 
    209 template< typename pass_type >
    210 Expression * PassVisitor< pass_type >::visitExpression( Expression * expr ) {
    211         return handleExpression(expr, [this]( Expression * expr ) {
    212                 expr->accept( *this );
    213                 return expr;
    214         });             
     142        // xxx - should env be cloned (or moved) onto the result of the mutate?
     143        expr->accept( *this );
    215144}
    216145
    217146template< typename pass_type >
    218147Expression * PassVisitor< pass_type >::mutateExpression( Expression * expr ) {
    219         return handleExpression(expr, [this]( Expression * expr ) {
    220                 return expr->acceptMutator( *this );
    221         });
    222 }
     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
    223158
    224159//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     
    298233void PassVisitor< pass_type >::visit( ExprStmt * node ) {
    299234        VISIT_START( node );
     235        call_beginScope();
    300236
    301237        visitExpression( node->get_expr() );
    302238
     239        call_endScope();
    303240        VISIT_END( node );
    304241}
     
    313250}
    314251
    315 //--------------------------------------------------------------------------
    316 // AsmStmt
    317252template< typename pass_type >
    318253void PassVisitor< pass_type >::visit( AsmStmt * node ) {
    319254        VISIT_BODY( node );
    320 }
    321 
    322 template< typename pass_type >
    323 Statement * PassVisitor< pass_type >::mutate( AsmStmt * node ) {
    324         MUTATE_BODY( Statement, node );
    325255}
    326256
     
    372302
    373303//--------------------------------------------------------------------------
    374 // ForStmt
     304// WhileStmt
    375305template< typename pass_type >
    376306void PassVisitor< pass_type >::visit( ForStmt * node ) {
     
    441371}
    442372
    443 //--------------------------------------------------------------------------
    444 // BranchStmt
    445373template< typename pass_type >
    446374void PassVisitor< pass_type >::visit( BranchStmt * node ) {
    447375        VISIT_BODY( node );
    448 }
    449 
    450 template< typename pass_type >
    451 Statement * PassVisitor< pass_type >::mutate( BranchStmt * node ) {
    452         MUTATE_BODY( Statement, node );
    453376}
    454377
     
    494417        maybeAccept( node->get_block(), *this );
    495418        acceptAll( node->get_catchers(), *this );
    496         maybeAccept( node->get_finally(), *this );
    497419
    498420        VISIT_END( node );
     
    505427        node->set_block(  maybeMutate( node->get_block(), *this ) );
    506428        mutateAll( node->get_catchers(), *this );
    507         node->set_finally( maybeMutate( node->get_finally(), *this ) );
    508429
    509430        MUTATE_END( Statement, node );
     
    516437        VISIT_START( node );
    517438
     439        node->set_body( visitStatement( node->get_body() ) );
    518440        maybeAccept( node->get_decl(), *this );
    519         node->set_cond( visitExpression( node->get_cond() ) );
    520         node->set_body( visitStatement( node->get_body() ) );
    521441
    522442        VISIT_END( node );
     
    527447        MUTATE_START( node );
    528448
    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() ) );
     449        node->set_body(  mutateStatement( node->get_body() ) );
     450        node->set_decl(  maybeMutate( node->get_decl(), *this ) );
    532451
    533452        MUTATE_END( Statement, node );
     
    921840
    922841template< typename pass_type >
     842Statement * PassVisitor< pass_type >::mutate( AsmStmt * node ) {
     843        MUTATE_BODY( Statement, node );
     844}
     845
     846template< typename pass_type >
     847Statement * PassVisitor< pass_type >::mutate( BranchStmt * node ) {
     848        MUTATE_BODY( Statement, node );
     849}
     850
     851template< typename pass_type >
    923852Statement * PassVisitor< pass_type >::mutate( FinallyStmt * node ) {
    924853        MUTATE_BODY( Statement, node );
Note: See TracChangeset for help on using the changeset viewer.