Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/PassVisitor.impl.h

    r6ca154b raf5c204a  
    44        __attribute__((unused))                   \
    55        const auto & guard = init_guard();        \
    6         bool visit_children = true;               \
    7         set_visit_children( visit_children );   \
    86        call_previsit( node );                    \
    9         if( visit_children ) {                    \
     7        if( visit_children() ) {                  \
    108
    119#define VISIT_END( node )                       \
    1210        }                                         \
     11        reset_visit();                            \
    1312        call_postvisit( node );                   \
    1413
     
    1615        __attribute__((unused))                   \
    1716        const auto & guard = init_guard();        \
    18         bool visit_children = true;               \
    19         set_visit_children( visit_children );   \
    2017        call_premutate( node );                   \
    21         if( visit_children ) {                    \
     18        if( visit_children() ) {                  \
    2219
    2320#define MUTATE_END( type, node )                \
    2421        }                                         \
     22        reset_visit();                            \
    2523        return call_postmutate< type * >( node ); \
    2624
     
    4442}
    4543
    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 ) {
     44typedef std::list< Statement * > StmtList_t;
     45
     46template< typename pass_type >
     47void PassVisitor< pass_type >::visitStatementList( std::list< Statement * > & statements ) {
    10548        SemanticError errors;
    10649
    10750        StmtList_t* beforeStmts = get_beforeStmts();
    10851        StmtList_t* afterStmts  = get_afterStmts();
    109         DeclList_t* beforeDecls = get_beforeDecls();
    110         DeclList_t* afterDecls  = get_afterDecls();
    11152
    11253        for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) {
    113 
    114                 if ( !empty( afterDecls ) ) { splice( std::inserter( statements, i ), afterDecls ); }
    11554                if ( !empty( afterStmts ) ) { statements.splice( i, *afterStmts ); }
    116 
    11755                try {
    118                         func( *i );
    119                         assert(( empty( beforeStmts ) && empty( afterStmts ))
    120                             || ( empty( beforeDecls ) && empty( afterDecls )) );
    121 
     56                        (*i)->accept( *this );
    12257                } catch ( SemanticError &e ) {
    12358                        errors.append( e );
    12459                }
    125 
    126                 if ( !empty( beforeDecls ) ) { splice( std::inserter( statements, i ), beforeDecls ); }
    12760                if ( !empty( beforeStmts ) ) { statements.splice( i, *beforeStmts ); }
    12861        }
    12962
    130         if ( !empty( afterDecls ) ) { splice( std::back_inserter( statements ), afterDecls); }
    13163        if ( !empty( afterStmts ) ) { statements.splice( statements.end(), *afterStmts ); }
    13264        if ( !errors.isEmpty() ) { throw errors; }
     
    13466
    13567template< 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 >
    14368void 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 );
     69        SemanticError errors;
    16170
    16271        StmtList_t* beforeStmts = get_beforeStmts();
    16372        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 )) );
     73
     74        for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) {
     75                if ( !empty( afterStmts ) ) { statements.splice( i, *afterStmts ); }
     76                try {
     77                        *i = (*i)->acceptMutator( *this );
     78                } catch ( SemanticError &e ) {
     79                        errors.append( e );
     80                }
     81                if ( !empty( beforeStmts ) ) { statements.splice( i, *beforeStmts ); }
     82        }
     83
     84        if ( !empty( afterStmts ) ) { statements.splice( statements.end(), *afterStmts ); }
     85        if ( !errors.isEmpty() ) { throw errors; }
     86}
     87
     88template< typename pass_type >
     89Statement * PassVisitor< pass_type >::visitStatement( Statement * stmt ) {
     90        // don't want statements from outer CompoundStmts to be added to this CompoundStmt
     91        ValueGuardPtr< TypeSubstitution * >      oldEnv        ( get_env_ptr() );
     92        ValueGuardPtr< std::list< Statement* > > oldBeforeStmts( get_beforeStmts() );
     93        ValueGuardPtr< std::list< Statement* > > oldAfterStmts ( get_afterStmts () );
     94
     95        maybeAccept( stmt, *this );
     96
     97        StmtList_t* beforeStmts = get_beforeStmts();
     98        StmtList_t* afterStmts  = get_afterStmts();
     99
     100        if( empty(beforeStmts) && empty(afterStmts) ) { return stmt; }
    170101
    171102        CompoundStmt *compound = new CompoundStmt( noLabels );
    172         if( !empty(beforeDecls) ) { splice( std::back_inserter( compound->get_kids() ), beforeDecls ); }
     103        if( !empty(beforeStmts) ) { compound->get_kids().splice( compound->get_kids().end(), *beforeStmts ); }
     104        compound->get_kids().push_back( stmt );
     105        if( !empty(afterStmts) ) { compound->get_kids().splice( compound->get_kids().end(), *afterStmts ); }
     106        return compound;
     107}
     108
     109template< typename pass_type >
     110Statement * PassVisitor< pass_type >::mutateStatement( Statement * stmt ) {
     111        // don't want statements from outer CompoundStmts to be added to this CompoundStmt
     112        ValueGuardPtr< TypeSubstitution * >      oldEnv        ( get_env_ptr() );
     113        ValueGuardPtr< std::list< Statement* > > oldBeforeStmts( get_beforeStmts() );
     114        ValueGuardPtr< std::list< Statement* > > oldAfterStmts ( get_afterStmts () );
     115
     116        Statement *newStmt = maybeMutate( stmt, *this );
     117
     118        StmtList_t* beforeStmts = get_beforeStmts();
     119        StmtList_t* afterStmts  = get_afterStmts();
     120
     121        if( empty(beforeStmts) && empty(afterStmts) ) { return newStmt; }
     122
     123        CompoundStmt *compound = new CompoundStmt( noLabels );
    173124        if( !empty(beforeStmts) ) { compound->get_kids().splice( compound->get_kids().end(), *beforeStmts ); }
    174125        compound->get_kids().push_back( newStmt );
    175         if( !empty(afterDecls) ) { splice( std::back_inserter( compound->get_kids() ), afterDecls ); }
    176126        if( !empty(afterStmts) ) { compound->get_kids().splice( compound->get_kids().end(), *afterStmts ); }
    177127        return compound;
    178128}
    179129
    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;
     130
     131
     132template< typename pass_type >
     133void PassVisitor< pass_type >::visitExpression( Expression * expr ) {
     134        if( !expr ) return;
    199135
    200136        auto env_ptr = get_env_ptr();
     
    202138                *env_ptr = expr->get_env();
    203139        }
    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         });             
     140        // xxx - should env be cloned (or moved) onto the result of the mutate?
     141        expr->accept( *this );
    215142}
    216143
    217144template< typename pass_type >
    218145Expression * PassVisitor< pass_type >::mutateExpression( Expression * expr ) {
    219         return handleExpression(expr, [this]( Expression * expr ) {
    220                 return expr->acceptMutator( *this );
    221         });
    222 }
     146        if( !expr ) return nullptr;
     147
     148        auto env_ptr = get_env_ptr();
     149        if ( env_ptr && expr->get_env() ) {
     150                *env_ptr = expr->get_env();
     151        }
     152        // xxx - should env be cloned (or moved) onto the result of the mutate?
     153        return expr->acceptMutator( *this );
     154}
     155
    223156
    224157//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     
    298231void PassVisitor< pass_type >::visit( ExprStmt * node ) {
    299232        VISIT_START( node );
     233        call_beginScope();
    300234
    301235        visitExpression( node->get_expr() );
    302236
     237        call_endScope();
    303238        VISIT_END( node );
    304239}
     
    313248}
    314249
    315 //--------------------------------------------------------------------------
    316 // AsmStmt
    317250template< typename pass_type >
    318251void PassVisitor< pass_type >::visit( AsmStmt * node ) {
    319252        VISIT_BODY( node );
    320 }
    321 
    322 template< typename pass_type >
    323 Statement * PassVisitor< pass_type >::mutate( AsmStmt * node ) {
    324         MUTATE_BODY( Statement, node );
    325253}
    326254
     
    372300
    373301//--------------------------------------------------------------------------
    374 // ForStmt
     302// WhileStmt
    375303template< typename pass_type >
    376304void PassVisitor< pass_type >::visit( ForStmt * node ) {
     
    420348
    421349//--------------------------------------------------------------------------
    422 // CaseStmt
     350// SwitchStmt
    423351template< typename pass_type >
    424352void PassVisitor< pass_type >::visit( CaseStmt * node ) {
     
    441369}
    442370
    443 //--------------------------------------------------------------------------
    444 // BranchStmt
    445371template< typename pass_type >
    446372void PassVisitor< pass_type >::visit( BranchStmt * node ) {
    447373        VISIT_BODY( node );
    448 }
    449 
    450 template< typename pass_type >
    451 Statement * PassVisitor< pass_type >::mutate( BranchStmt * node ) {
    452         MUTATE_BODY( Statement, node );
    453374}
    454375
     
    494415        maybeAccept( node->get_block(), *this );
    495416        acceptAll( node->get_catchers(), *this );
    496         maybeAccept( node->get_finally(), *this );
    497417
    498418        VISIT_END( node );
     
    505425        node->set_block(  maybeMutate( node->get_block(), *this ) );
    506426        mutateAll( node->get_catchers(), *this );
    507         node->set_finally( maybeMutate( node->get_finally(), *this ) );
    508427
    509428        MUTATE_END( Statement, node );
     
    516435        VISIT_START( node );
    517436
     437        node->set_body( visitStatement( node->get_body() ) );
    518438        maybeAccept( node->get_decl(), *this );
    519         node->set_cond( visitExpression( node->get_cond() ) );
    520         node->set_body( visitStatement( node->get_body() ) );
    521439
    522440        VISIT_END( node );
     
    527445        MUTATE_START( node );
    528446
    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() ) );
     447        node->set_body(  mutateStatement( node->get_body() ) );
     448        node->set_decl(  maybeMutate( node->get_decl(), *this ) );
    532449
    533450        MUTATE_END( Statement, node );
     
    921838
    922839template< typename pass_type >
     840Statement * PassVisitor< pass_type >::mutate( AsmStmt * node ) {
     841        MUTATE_BODY( Statement, node );
     842}
     843
     844template< typename pass_type >
     845Statement * PassVisitor< pass_type >::mutate( BranchStmt * node ) {
     846        MUTATE_BODY( Statement, node );
     847}
     848
     849template< typename pass_type >
    923850Statement * PassVisitor< pass_type >::mutate( FinallyStmt * node ) {
    924851        MUTATE_BODY( Statement, node );
Note: See TracChangeset for help on using the changeset viewer.