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