Changes in / [653f2c7:ba912706]
- Location:
- src/Common
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Common/PassVisitor.h
r653f2c7 rba912706 25 25 // | WithStmtsToAdd - provides the ability to insert statements before or after the current statement by adding new statements into 26 26 // stmtsToAddBefore or stmtsToAddAfter respectively. 27 // | WithShortCircuiting - provides the ability to skip visiting child nodes; set visit_children to false inpre{visit,mutate} to skip visiting children27 // | WithShortCircuiting - provides the ability to skip visiting child nodes; set skip_children to true if pre{visit,mutate} to skip visiting children 28 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 29 // will automatically be restored to its previous value after the corresponding postvisit/postmutate teminates. … … 220 220 221 221 private: 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 225 222 template<typename node_type> void call_previsit ( node_type * node ) { previsit_impl ( pass, node, 0 ); } 226 223 template<typename node_type> void call_postvisit( node_type * node ) { postvisit_impl( pass, node, 0 ); } … … 234 231 void set_env( TypeSubstitution * env ) { set_env_impl( pass, env, 0); } 235 232 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 ); 239 234 void mutateStatementList( std::list< Statement* > &statements ); 240 235 241 template< typename func_t > 242 Statement * handleStatement( Statement * stmt, func_t func ); 243 Statement * visitStatement ( Statement * stmt ); 236 Statement * visitStatement( Statement * stmt ); 244 237 Statement * mutateStatement( Statement * stmt ); 245 238 246 template< typename func_t > 247 Expression * handleExpression( Expression * expr, func_t func ); 248 Expression * visitExpression ( Expression * expr ); 239 void visitExpression( Expression * expr ); 249 240 Expression * mutateExpression( Expression * expr ); 250 241 -
src/Common/PassVisitor.impl.h
r653f2c7 rba912706 44 44 } 45 45 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 ) { 46 typedef std::list< Statement * > StmtList_t; 47 48 template< typename pass_type > 49 void PassVisitor< pass_type >::visitStatementList( std::list< Statement * > & statements ) { 105 50 SemanticError errors; 106 51 107 52 StmtList_t* beforeStmts = get_beforeStmts(); 108 53 StmtList_t* afterStmts = get_afterStmts(); 109 DeclList_t* beforeDecls = get_beforeDecls();110 DeclList_t* afterDecls = get_afterDecls();111 54 112 55 for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) { 113 114 if ( !empty( afterDecls ) ) { splice( std::inserter( statements, i ), afterDecls ); }115 56 if ( !empty( afterStmts ) ) { statements.splice( i, *afterStmts ); } 116 117 57 try { 118 func( *i ); 119 assert(( empty( beforeStmts ) && empty( afterStmts )) 120 || ( empty( beforeDecls ) && empty( afterDecls )) ); 121 58 (*i)->accept( *this ); 122 59 } catch ( SemanticError &e ) { 123 60 errors.append( e ); 124 61 } 125 126 if ( !empty( beforeDecls ) ) { splice( std::inserter( statements, i ), beforeDecls ); }127 62 if ( !empty( beforeStmts ) ) { statements.splice( i, *beforeStmts ); } 128 63 } 129 64 130 if ( !empty( afterDecls ) ) { splice( std::back_inserter( statements ), afterDecls); }131 65 if ( !empty( afterStmts ) ) { statements.splice( statements.end(), *afterStmts ); } 132 66 if ( !errors.isEmpty() ) { throw errors; } … … 134 68 135 69 template< 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 >143 70 void 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; 161 72 162 73 StmtList_t* beforeStmts = get_beforeStmts(); 163 74 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 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; } 170 103 171 104 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 111 template< typename pass_type > 112 Statement * 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 ); 173 126 if( !empty(beforeStmts) ) { compound->get_kids().splice( compound->get_kids().end(), *beforeStmts ); } 174 127 compound->get_kids().push_back( newStmt ); 175 if( !empty(afterDecls) ) { splice( std::back_inserter( compound->get_kids() ), afterDecls ); }176 128 if( !empty(afterStmts) ) { compound->get_kids().splice( compound->get_kids().end(), *afterStmts ); } 177 129 return compound; 178 130 } 179 131 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 134 template< typename pass_type > 135 void PassVisitor< pass_type >::visitExpression( Expression * expr ) { 136 if( !expr ) return; 199 137 200 138 auto env_ptr = get_env_ptr(); … … 202 140 *env_ptr = expr->get_env(); 203 141 } 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 ); 215 144 } 216 145 217 146 template< typename pass_type > 218 147 Expression * 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 223 158 224 159 //------------------------------------------------------------------------------------------------------------------------------------------------------------------------ … … 298 233 void PassVisitor< pass_type >::visit( ExprStmt * node ) { 299 234 VISIT_START( node ); 235 call_beginScope(); 300 236 301 237 visitExpression( node->get_expr() ); 302 238 239 call_endScope(); 303 240 VISIT_END( node ); 304 241 } … … 313 250 } 314 251 315 //--------------------------------------------------------------------------316 // AsmStmt317 252 template< typename pass_type > 318 253 void PassVisitor< pass_type >::visit( AsmStmt * node ) { 319 254 VISIT_BODY( node ); 320 }321 322 template< typename pass_type >323 Statement * PassVisitor< pass_type >::mutate( AsmStmt * node ) {324 MUTATE_BODY( Statement, node );325 255 } 326 256 … … 372 302 373 303 //-------------------------------------------------------------------------- 374 // ForStmt304 // WhileStmt 375 305 template< typename pass_type > 376 306 void PassVisitor< pass_type >::visit( ForStmt * node ) { … … 441 371 } 442 372 443 //--------------------------------------------------------------------------444 // BranchStmt445 373 template< typename pass_type > 446 374 void PassVisitor< pass_type >::visit( BranchStmt * node ) { 447 375 VISIT_BODY( node ); 448 }449 450 template< typename pass_type >451 Statement * PassVisitor< pass_type >::mutate( BranchStmt * node ) {452 MUTATE_BODY( Statement, node );453 376 } 454 377 … … 494 417 maybeAccept( node->get_block(), *this ); 495 418 acceptAll( node->get_catchers(), *this ); 496 maybeAccept( node->get_finally(), *this );497 419 498 420 VISIT_END( node ); … … 505 427 node->set_block( maybeMutate( node->get_block(), *this ) ); 506 428 mutateAll( node->get_catchers(), *this ); 507 node->set_finally( maybeMutate( node->get_finally(), *this ) );508 429 509 430 MUTATE_END( Statement, node ); … … 516 437 VISIT_START( node ); 517 438 439 node->set_body( visitStatement( node->get_body() ) ); 518 440 maybeAccept( node->get_decl(), *this ); 519 node->set_cond( visitExpression( node->get_cond() ) );520 node->set_body( visitStatement( node->get_body() ) );521 441 522 442 VISIT_END( node ); … … 527 447 MUTATE_START( node ); 528 448 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 ) ); 532 451 533 452 MUTATE_END( Statement, node ); … … 921 840 922 841 template< 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 > 923 852 Statement * PassVisitor< pass_type >::mutate( FinallyStmt * node ) { 924 853 MUTATE_BODY( Statement, node );
Note: See TracChangeset
for help on using the changeset viewer.