Changes in src/InitTweak/FixInit.cc [ba3706f:b3fc977]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/InitTweak/FixInit.cc
rba3706f rb3fc977 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // FixInit. h--7 // FixInit.cc -- 8 8 // 9 9 // Author : Rob Schluntz … … 197 197 }; 198 198 199 struct GenStructMemberCalls final : public WithGuards, public WithShortCircuiting, public WithIndexer {199 struct GenStructMemberCalls final : public WithGuards, public WithShortCircuiting, public WithIndexer, public WithVisitorRef<GenStructMemberCalls> { 200 200 /// generate default/copy ctor and dtor calls for user-defined struct ctor/dtors 201 201 /// for any member that is missing a corresponding ctor/dtor call. … … 203 203 static void generate( std::list< Declaration * > & translationUnit ); 204 204 205 void previsit( FunctionDecl * funcDecl ); 206 void postvisit( FunctionDecl * funcDecl ); 207 208 void previsit( MemberExpr * memberExpr ); 209 void previsit( ApplicationExpr * appExpr ); 205 void premutate( FunctionDecl * funcDecl ); 206 DeclarationWithType * postmutate( FunctionDecl * funcDecl ); 207 208 void premutate( MemberExpr * memberExpr ); 209 void premutate( ApplicationExpr * appExpr ); 210 211 /// Note: this post mutate used to be in a separate visitor. If this pass breaks, one place to examine is whether it is 212 /// okay for this part of the recursion to occur alongside the rest. 213 Expression * postmutate( UntypedExpr * expr ); 210 214 211 215 SemanticError errors; … … 220 224 bool isCtor = false; // true if current function is a constructor 221 225 StructDecl * structDecl = nullptr; 222 };223 224 // very simple resolver-like mutator class - used to225 // resolve UntypedExprs that are found within newly226 // generated constructor/destructor calls227 class MutatingResolver final : public Mutator {228 public:229 MutatingResolver( SymTab::Indexer & indexer ) : indexer( indexer ) {}230 231 using Mutator::mutate;232 virtual DeclarationWithType* mutate( ObjectDecl *objectDecl ) override;233 virtual Expression* mutate( UntypedExpr *untypedExpr ) override;234 235 private:236 SymTab::Indexer & indexer;237 226 }; 238 227 … … 315 304 void GenStructMemberCalls::generate( std::list< Declaration * > & translationUnit ) { 316 305 PassVisitor<GenStructMemberCalls> warner; 317 acceptAll( translationUnit, warner );306 mutateAll( translationUnit, warner ); 318 307 } 319 308 … … 365 354 // arrays are not copy constructed, so this should always be an ExprStmt 366 355 ImplicitCtorDtorStmt * stmt = genCtorDtor( fname, var, cpArg ); 367 ExprStmt * exprStmt = strict_dynamic_cast< ExprStmt * >( stmt->get_callStmt() ); 356 assertf( stmt, "ResolveCopyCtors: genCtorDtor returned nullptr: %s / %s / %s", fname.c_str(), toString( var ).c_str(), toString( cpArg ).c_str() ); 357 ExprStmt * exprStmt = strict_dynamic_cast< ExprStmt * >( stmt->callStmt ); 368 358 Expression * resolved = exprStmt->expr; 369 359 exprStmt->expr = nullptr; // take ownership of expr … … 382 372 } // if 383 373 delete stmt; 374 if ( TupleAssignExpr * assign = dynamic_cast< TupleAssignExpr * >( resolved ) ) { 375 // fix newly generated StmtExpr 376 postvisit( assign->stmtExpr ); 377 } 384 378 return resolved; 385 379 } … … 475 469 static UniqueName retNamer("_tmp_stmtexpr_ret"); 476 470 477 // create variable that will hold the result of the stmt expr478 471 result = result->clone(); 479 472 env->apply( result ); 473 if ( ! InitTweak::isConstructable( result ) ) { 474 delete result; 475 return; 476 } 477 478 // create variable that will hold the result of the stmt expr 480 479 ObjectDecl * ret = ObjectDecl::newObject( retNamer.newName(), result, nullptr ); 481 ret-> get_type()->set_const( false );482 stmtExpr-> get_returnDecls().push_front( ret );480 ret->type->set_const( false ); 481 stmtExpr->returnDecls.push_front( ret ); 483 482 484 483 // must have a non-empty body, otherwise it wouldn't have a result 485 CompoundStmt * body = stmtExpr-> get_statements();484 CompoundStmt * body = stmtExpr->statements; 486 485 assert( ! body->get_kids().empty() ); 487 486 // must be an ExprStmt, otherwise it wouldn't have a result 488 487 ExprStmt * last = strict_dynamic_cast< ExprStmt * >( body->get_kids().back() ); 489 last-> set_expr( makeCtorDtor( "?{}", ret, last->get_expr()) );490 491 stmtExpr-> get_dtors().push_front( makeCtorDtor( "^?{}", ret ) );488 last->expr = makeCtorDtor( "?{}", ret, last->get_expr() ); 489 490 stmtExpr->dtors.push_front( makeCtorDtor( "^?{}", ret ) ); 492 491 } // if 493 492 } … … 590 589 // to the outer context, rather than inside of the statement expression. 591 590 visit_children = false; 592 std::list< Statement * > & stmts = stmtExpr-> get_statements()->get_kids();591 std::list< Statement * > & stmts = stmtExpr->statements->get_kids(); 593 592 for ( Statement *& stmt : stmts ) { 594 593 stmt = stmt->acceptMutator( *visitor ); 595 594 } // for 596 assert( stmtExpr-> get_result());597 Type * result = stmtExpr-> get_result();595 assert( stmtExpr->result ); 596 Type * result = stmtExpr->result; 598 597 if ( ! result->isVoid() ) { 599 for ( ObjectDecl * obj : stmtExpr-> get_returnDecls()) {598 for ( ObjectDecl * obj : stmtExpr->returnDecls ) { 600 599 stmtsToAddBefore.push_back( new DeclStmt( obj ) ); 601 600 } // for 602 601 // add destructors after current statement 603 for ( Expression * dtor : stmtExpr-> get_dtors()) {602 for ( Expression * dtor : stmtExpr->dtors ) { 604 603 stmtsToAddAfter.push_back( new ExprStmt( dtor ) ); 605 604 } // for 606 605 // must have a non-empty body, otherwise it wouldn't have a result 607 606 assert( ! stmts.empty() ); 608 assert( ! stmtExpr->get_returnDecls().empty() ); 609 stmts.push_back( new ExprStmt( new VariableExpr( stmtExpr->get_returnDecls().front() ) ) ); 610 stmtExpr->get_returnDecls().clear(); 611 stmtExpr->get_dtors().clear(); 612 } 613 assert( stmtExpr->get_returnDecls().empty() ); 614 assert( stmtExpr->get_dtors().empty() ); 607 assertf( ! stmtExpr->returnDecls.empty() || stmtExpr->dtors.empty(), "StmtExpr returns non-void, but no return decls: %s", toString( stmtExpr ).c_str() ); 608 // if there is a return decl, add a use as the last statement; will not have return decl on non-constructable returns 609 if ( ! stmtExpr->returnDecls.empty() ) { 610 stmts.push_back( new ExprStmt( new VariableExpr( stmtExpr->returnDecls.front() ) ) ); 611 } 612 stmtExpr->returnDecls.clear(); 613 stmtExpr->dtors.clear(); 614 } 615 assert( stmtExpr->returnDecls.empty() ); 616 assert( stmtExpr->dtors.empty() ); 615 617 } 616 618 … … 937 939 } 938 940 939 void GenStructMemberCalls::pre visit( FunctionDecl * funcDecl ) {941 void GenStructMemberCalls::premutate( FunctionDecl * funcDecl ) { 940 942 GuardValue( function ); 941 943 GuardValue( unhandled ); … … 971 973 } 972 974 973 void GenStructMemberCalls::postvisit( FunctionDecl * funcDecl ) {975 DeclarationWithType * GenStructMemberCalls::postmutate( FunctionDecl * funcDecl ) { 974 976 // remove the unhandled objects from usedUninit, because a call is inserted 975 977 // to handle them - only objects that are later constructed are used uninitialized. … … 1025 1027 Statement * callStmt = stmt.front(); 1026 1028 1027 MutatingResolver resolver( indexer );1028 1029 try { 1029 callStmt->acceptMutator( resolver );1030 callStmt->acceptMutator( *visitor ); 1030 1031 if ( isCtor ) { 1031 1032 function->get_statements()->push_front( callStmt ); … … 1043 1044 throw errors; 1044 1045 } 1046 return funcDecl; 1045 1047 } 1046 1048 … … 1068 1070 } 1069 1071 1070 void GenStructMemberCalls::pre visit( ApplicationExpr * appExpr ) {1072 void GenStructMemberCalls::premutate( ApplicationExpr * appExpr ) { 1071 1073 if ( ! checkWarnings( function ) ) { 1072 1074 visit_children = false; … … 1093 1095 } 1094 1096 1095 void GenStructMemberCalls::pre visit( MemberExpr * memberExpr ) {1097 void GenStructMemberCalls::premutate( MemberExpr * memberExpr ) { 1096 1098 if ( ! checkWarnings( function ) || ! isCtor ) { 1097 1099 visit_children = false; … … 1121 1123 } 1122 1124 1123 DeclarationWithType * MutatingResolver::mutate( ObjectDecl * objectDecl ) { 1124 // add object to the indexer assumes that there will be no name collisions 1125 // in generated code. If this changes, add mutate methods for entities with 1126 // scope and call {enter,leave}Scope explicitly. 1127 indexer.addId( objectDecl ); 1128 return objectDecl; 1129 } 1130 1131 Expression * MutatingResolver::mutate( UntypedExpr * untypedExpr ) { 1125 Expression * GenStructMemberCalls::postmutate( UntypedExpr * untypedExpr ) { 1132 1126 Expression * newExpr = untypedExpr; 1133 1127 ResolvExpr::findVoidExpression( newExpr, indexer );
Note:
See TracChangeset
for help on using the changeset viewer.