Changeset c0714bf for src/InitTweak
- Timestamp:
- Sep 15, 2017, 8:07:51 PM (7 years ago)
- 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:
- 33a25f9
- Parents:
- eada3cf
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/InitTweak/FixInit.cc
reada3cf rc0714bf 70 70 namespace InitTweak { 71 71 namespace { 72 typedef std::unordered_map< Expression *, TypeSubstitution * > EnvMap;73 72 typedef std::unordered_map< int, int > UnqCount; 74 73 75 class InsertImplicitCalls : public WithTypeSubstitution { 76 public: 74 struct InsertImplicitCalls : public WithTypeSubstitution { 77 75 /// wrap function application expressions as ImplicitCopyCtorExpr nodes so that it is easy to identify which 78 76 /// function calls need their parameters to be copy constructed 79 static void insert( std::list< Declaration * > & translationUnit, EnvMap & envMap ); 80 81 InsertImplicitCalls( EnvMap & envMap ) : envMap( envMap ) {} 77 static void insert( std::list< Declaration * > & translationUnit ); 82 78 83 79 Expression * postmutate( ApplicationExpr * appExpr ); 84 void premutate( StmtExpr * stmtExpr );85 86 // collects environments for relevant nodes87 EnvMap & envMap;88 80 }; 89 81 90 class ResolveCopyCtors final : public SymTab::Indexer { 91 public: 82 struct ResolveCopyCtors final : public WithIndexer, public WithShortCircuiting, public WithTypeSubstitution { 92 83 /// generate temporary ObjectDecls for each argument and return value of each ImplicitCopyCtorExpr, 93 84 /// generate/resolve copy construction expressions for each, and generate/resolve destructors for both 94 85 /// arguments and return value temporaries 95 static void resolveImplicitCalls( std::list< Declaration * > & translationUnit, const EnvMap & envMap, UnqCount & unqCount ); 96 97 typedef SymTab::Indexer Parent; 98 using Parent::visit; 99 100 ResolveCopyCtors( const EnvMap & envMap, UnqCount & unqCount ) : envMap( envMap ), unqCount( unqCount ) {} 101 102 virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr ) override; 103 virtual void visit( UniqueExpr * unqExpr ) override; 104 virtual void visit( StmtExpr * stmtExpr ) override; 86 static void resolveImplicitCalls( std::list< Declaration * > & translationUnit, UnqCount & unqCount ); 87 88 ResolveCopyCtors( UnqCount & unqCount ) : unqCount( unqCount ) {} 89 90 void postvisit( ImplicitCopyCtorExpr * impCpCtorExpr ); 91 void postvisit( StmtExpr * stmtExpr ); 92 void previsit( UniqueExpr * unqExpr ); 93 void postvisit( UniqueExpr * unqExpr ); 105 94 106 95 /// create and resolve ctor/dtor expression: fname(var, [cpArg]) … … 111 100 void destructRet( ObjectDecl * ret, ImplicitCopyCtorExpr * impCpCtorExpr ); 112 101 113 TypeSubstitution * env;114 const EnvMap & envMap;115 102 UnqCount & unqCount; // count the number of times each unique expr ID appears 103 std::unordered_set< int > vars; 116 104 }; 117 105 … … 292 280 InitTweak::fixGlobalInit( translationUnit, filename, inLibrary ); 293 281 294 EnvMap envMap;295 282 UnqCount unqCount; 296 283 297 InsertImplicitCalls::insert( translationUnit , envMap);298 ResolveCopyCtors::resolveImplicitCalls( translationUnit, envMap,unqCount );284 InsertImplicitCalls::insert( translationUnit ); 285 ResolveCopyCtors::resolveImplicitCalls( translationUnit, unqCount ); 299 286 InsertDtors::insert( translationUnit ); 300 287 FixInit::fixInitializers( translationUnit ); … … 315 302 316 303 namespace { 317 void InsertImplicitCalls::insert( std::list< Declaration * > & translationUnit , EnvMap & envMap) {318 PassVisitor<InsertImplicitCalls> inserter ( envMap );304 void InsertImplicitCalls::insert( std::list< Declaration * > & translationUnit ) { 305 PassVisitor<InsertImplicitCalls> inserter; 319 306 mutateAll( translationUnit, inserter ); 320 307 } 321 308 322 void ResolveCopyCtors::resolveImplicitCalls( std::list< Declaration * > & translationUnit, const EnvMap & envMap,UnqCount & unqCount ) {323 ResolveCopyCtors resolver( envMap,unqCount );309 void ResolveCopyCtors::resolveImplicitCalls( std::list< Declaration * > & translationUnit, UnqCount & unqCount ) { 310 PassVisitor<ResolveCopyCtors> resolver( unqCount ); 324 311 acceptAll( translationUnit, resolver ); 325 312 } … … 395 382 // wrap each function call so that it is easy to identify nodes that have to be copy constructed 396 383 ImplicitCopyCtorExpr * expr = new ImplicitCopyCtorExpr( appExpr ); 397 // save the type substitution into the envMap so that it is easy to find.384 // Move the type substitution to the new top-level, if it is attached to the appExpr. 398 385 // Ensure it is not deleted with the ImplicitCopyCtorExpr by removing it before deletion. 399 386 // The substitution is needed to obtain the type of temporary variables so that copy constructor 400 // calls can be resolved. Normally this is what PolyMutator is for, but the pass that resolves 401 // copy constructor calls must be an Indexer. We could alternatively make a PolyIndexer which 402 // saves the environment, or compute the types of temporaries here, but it's much simpler to 403 // save the environment here, and more cohesive to compute temporary variables and resolve copy 404 // constructor calls together. 387 // calls can be resolved. 405 388 assert( env ); 406 envMap[expr] = env;389 std::swap( expr->env, appExpr->env ); 407 390 return expr; 408 }409 410 void InsertImplicitCalls::premutate( StmtExpr * stmtExpr ) {411 assert( env );412 envMap[stmtExpr] = env;413 391 } 414 392 … … 428 406 // (VariableExpr and already resolved expression) 429 407 CP_CTOR_PRINT( std::cerr << "ResolvingCtorDtor " << untyped << std::endl; ) 430 Expression * resolved = ResolvExpr::findVoidExpression( untyped, *this);408 Expression * resolved = ResolvExpr::findVoidExpression( untyped, indexer ); 431 409 assert( resolved ); 432 410 if ( resolved->get_env() ) { … … 477 455 } 478 456 479 void ResolveCopyCtors:: visit( ImplicitCopyCtorExpr *impCpCtorExpr ) {457 void ResolveCopyCtors::postvisit( ImplicitCopyCtorExpr *impCpCtorExpr ) { 480 458 CP_CTOR_PRINT( std::cerr << "ResolveCopyCtors: " << impCpCtorExpr << std::endl; ) 481 Parent::visit( impCpCtorExpr );482 env = envMap.at(impCpCtorExpr);483 assert( env );484 459 485 460 ApplicationExpr * appExpr = impCpCtorExpr->get_callExpr(); … … 510 485 } 511 486 512 void ResolveCopyCtors::visit( StmtExpr * stmtExpr ) { 513 Parent::visit( stmtExpr ); 514 env = envMap.at(stmtExpr); 487 void ResolveCopyCtors::postvisit( StmtExpr * stmtExpr ) { 488 assert( env ); 515 489 assert( stmtExpr->get_result() ); 516 490 Type * result = stmtExpr->get_result(); … … 534 508 stmtExpr->get_dtors().push_front( makeCtorDtor( "^?{}", ret ) ); 535 509 } // if 536 537 } 538 539 void ResolveCopyCtors::visit( UniqueExpr * unqExpr ) { 540 static std::unordered_set< int > vars; 510 } 511 512 void ResolveCopyCtors::previsit( UniqueExpr * unqExpr ) { 541 513 unqCount[ unqExpr->get_id() ]++; // count the number of unique expressions for each ID 542 514 if ( vars.count( unqExpr->get_id() ) ) { 543 515 // xxx - hack to prevent double-handling of unique exprs, otherwise too many temporary variables and destructors are generated 516 visit_children = false; 517 } 518 } 519 520 void ResolveCopyCtors::postvisit( UniqueExpr * unqExpr ) { 521 if ( vars.count( unqExpr->get_id() ) ) { 522 // xxx - hack to prevent double-handling of unique exprs, otherwise too many temporary variables and destructors are generated 544 523 return; 545 524 } 546 525 547 Parent::visit( unqExpr );548 526 // it should never be necessary to wrap a void-returning expression in a UniqueExpr - if this assumption changes, this needs to be rethought 549 527 assert( unqExpr->get_result() ); … … 582 560 583 561 // xxx - update to work with multiple return values 584 ObjectDecl * returnDecl = returnDecls.empty() ? NULL: returnDecls.front();562 ObjectDecl * returnDecl = returnDecls.empty() ? nullptr : returnDecls.front(); 585 563 Expression * callExpr = impCpCtorExpr->get_callExpr(); 586 564 … … 591 569 tempDecls.clear(); 592 570 returnDecls.clear(); 593 impCpCtorExpr->set_callExpr( NULL ); 594 impCpCtorExpr->set_env( NULL ); 571 impCpCtorExpr->set_callExpr( nullptr ); 572 std::swap( impCpCtorExpr->env, callExpr->env ); 573 assert( impCpCtorExpr->env == nullptr ); 595 574 delete impCpCtorExpr; 596 575
Note: See TracChangeset
for help on using the changeset viewer.