Changeset 9cdfb4d0 for src/InitTweak
- Timestamp:
- Jan 22, 2018, 3:09:01 PM (8 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, stuck-waitfor-destruct, with_gc
- Children:
- e23d20b
- Parents:
- 326cd2b (diff), 4bf3b2b (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - Location:
- src/InitTweak
- Files:
-
- 3 edited
-
FixInit.cc (modified) (16 diffs)
-
GenInit.cc (modified) (8 diffs)
-
InitTweak.cc (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
src/InitTweak/FixInit.cc
r326cd2b r9cdfb4d0 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 ); -
src/InitTweak/GenInit.cc
r326cd2b r9cdfb4d0 30 30 #include "InitTweak.h" // for isConstExpr, InitExpander, checkIn... 31 31 #include "Parser/LinkageSpec.h" // for isOverridable, C 32 #include "ResolvExpr/Resolver.h" 32 33 #include "SymTab/Autogen.h" // for genImplicitCall, SizeType 33 34 #include "SymTab/Mangler.h" // for Mangler … … 40 41 #include "SynTree/Type.h" // for Type, ArrayType, Type::Qualifiers 41 42 #include "SynTree/Visitor.h" // for acceptAll, maybeAccept 43 #include "Tuples/Tuples.h" // for maybeImpure 42 44 43 45 namespace InitTweak { … … 89 91 }; 90 92 91 struct HoistArrayDimension final : public WithDeclsToAdd, public WithShortCircuiting, public WithGuards {93 struct HoistArrayDimension final : public WithDeclsToAdd, public WithShortCircuiting, public WithGuards, public WithIndexer { 92 94 /// hoist dimension from array types in object declaration so that it uses a single 93 95 /// const variable of type size_t, so that side effecting array dimensions are only … … 104 106 void premutate( FunctionType * ) { visit_children = false; } 105 107 108 // need this so that enumerators are added to the indexer, due to premutate(AggregateDecl *) 109 void premutate( EnumDecl * ) {} 110 106 111 void hoist( Type * type ); 107 112 … … 135 140 if ( varExpr->var == retVal ) return; 136 141 } 137 stmtsToAddBefore.push_back( genCtorDtor( "?{}", retVal, returnStmt->get_expr() ) ); 142 Statement * stmt = genCtorDtor( "?{}", retVal, returnStmt->expr ); 143 assertf( stmt, "ReturnFixer: genCtorDtor returned nullptr: %s / %s", toString( retVal ).c_str(), toString( returnStmt->expr ).c_str() ); 144 stmtsToAddBefore.push_back( stmt ); 138 145 139 146 // return the retVal object … … 178 185 if ( ! arrayType->get_dimension() ) return; // xxx - recursive call to hoist? 179 186 180 // don't need to hoist dimension if it's a constexpr - only need to if there's potential for side effects. 181 if ( isConstExpr( arrayType->get_dimension() ) ) return; 187 // need to resolve array dimensions in order to accurately determine if constexpr 188 ResolvExpr::findSingleExpression( arrayType->dimension, SymTab::SizeType->clone(), indexer ); 189 // array is variable-length when the dimension is not constexpr 190 arrayType->isVarLen = ! isConstExpr( arrayType->dimension ); 191 // don't need to hoist dimension if it's definitely pure - only need to if there's potential for side effects. 192 if ( ! Tuples::maybeImpure( arrayType->dimension ) ) return; 182 193 183 194 ObjectDecl * arrayDimension = new ObjectDecl( dimensionName.newName(), storageClasses, LinkageSpec::C, 0, SymTab::SizeType->clone(), new SingleInit( arrayType->get_dimension() ) ); … … 194 205 void HoistArrayDimension::premutate( FunctionDecl * ) { 195 206 GuardValue( inFunction ); 207 inFunction = true; 196 208 } 197 209 … … 220 232 Type * type = objDecl->get_type(); 221 233 while ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) { 234 // must always construct VLAs with an initializer, since this is an error in C 235 if ( at->isVarLen && objDecl->init ) return true; 222 236 type = at->get_base(); 223 237 } -
src/InitTweak/InitTweak.cc
r326cd2b r9cdfb4d0 564 564 void previsit( ConstantExpr * ) {} 565 565 566 void previsit( VariableExpr * varExpr ) { 567 visit_children = false; 568 569 if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( varExpr->result ) ) { 570 long long int value; 571 if ( inst->baseEnum->valueOf( varExpr->var, value ) ) { 572 // enumerators are const expr 573 return; 574 } 575 } 576 isConstExpr = false; 577 } 578 566 579 bool isConstExpr = true; 567 580 };
Note:
See TracChangeset
for help on using the changeset viewer.