@@ -2550,37 +2550,21 @@ SingleValueStmtExpr *SingleValueStmtExpr::createWithWrappedBranches(
25502550 if (!BS)
25512551 continue ;
25522552
2553- if (auto *S = BS->getSingleActiveStatement ()) {
2554- if (mustBeExpr) {
2555- // If this must be an expression, we can eagerly wrap any exhaustive if,
2556- // switch, and do statement.
2557- if (auto *IS = dyn_cast<IfStmt>(S)) {
2558- if (!IS->isSyntacticallyExhaustive ())
2559- continue ;
2560- } else if (auto *DCS = dyn_cast<DoCatchStmt>(S)) {
2561- if (!ctx.LangOpts .hasFeature (Feature::DoExpressions))
2562- continue ;
2563- if (!DCS->isSyntacticallyExhaustive ())
2564- continue ;
2565- } else if (isa<DoStmt>(S)) {
2566- if (!ctx.LangOpts .hasFeature (Feature::DoExpressions))
2567- continue ;
2568- } else if (!isa<SwitchStmt>(S)) {
2569- continue ;
2570- }
2571- } else {
2572- // Otherwise do the semantic checking to verify that we can wrap the
2573- // branch.
2574- if (!S->mayProduceSingleValue (ctx))
2575- continue ;
2576- }
2577- BS->setLastElement (SingleValueStmtExpr::createWithWrappedBranches (
2578- ctx, S, DC, mustBeExpr));
2579- }
2553+ // Check to see if we can wrap an implicit last element of the brace.
2554+ if (!isLastElementImplicitResult (BS, ctx, mustBeExpr))
2555+ continue ;
2556+
2557+ auto &result = BS->getElements ().back ();
2558+ assert (result.is <Expr *>() || result.is <Stmt *>());
25802559
2581- // Wrap single expression branches in an implicit 'then <expr>'.
2582- if (auto *E = BS->getSingleActiveExpression ())
2583- BS->setLastElement (ThenStmt::createImplicit (ctx, E));
2560+ // Wrap a statement in a SingleValueStmtExpr.
2561+ if (auto *S = result.dyn_cast <Stmt *>()) {
2562+ result = SingleValueStmtExpr::createWithWrappedBranches (ctx, S, DC,
2563+ mustBeExpr);
2564+ }
2565+ // Wrap an expression in an implicit 'then <expr>'.
2566+ if (auto *E = result.dyn_cast <Expr *>())
2567+ result = ThenStmt::createImplicit (ctx, E);
25842568 }
25852569 return SVE;
25862570}
@@ -2630,6 +2614,50 @@ SingleValueStmtExpr::tryDigOutSingleValueStmtExpr(Expr *E) {
26302614 return finder.FoundSVE ;
26312615}
26322616
2617+ bool SingleValueStmtExpr::isLastElementImplicitResult (
2618+ BraceStmt *BS, ASTContext &ctx, bool mustBeSingleValueStmt) {
2619+ if (BS->empty ())
2620+ return false ;
2621+
2622+ // We must either be allowing implicit last expressions, or we must have a
2623+ // single active element, which is guaranteed to be the last element.
2624+ if (!ctx.LangOpts .hasFeature (Feature::ImplicitLastExprResults) &&
2625+ !BS->getSingleActiveElement ()) {
2626+ return false ;
2627+ }
2628+ auto elt = BS->getLastElement ();
2629+
2630+ // Expressions are always valid.
2631+ if (elt.is <Expr *>())
2632+ return true ;
2633+
2634+ if (auto *S = elt.dyn_cast <Stmt *>()) {
2635+ if (mustBeSingleValueStmt) {
2636+ // If this must be a SingleValueStmtExpr, we can eagerly take any
2637+ // exhaustive if, switch, and do statement.
2638+ if (auto *IS = dyn_cast<IfStmt>(S))
2639+ return IS->isSyntacticallyExhaustive ();
2640+
2641+ // Guaranteed to be exhaustive.
2642+ if (isa<SwitchStmt>(S))
2643+ return true ;
2644+
2645+ if (ctx.LangOpts .hasFeature (Feature::DoExpressions)) {
2646+ if (auto *DCS = dyn_cast<DoCatchStmt>(S))
2647+ return DCS->isSyntacticallyExhaustive ();
2648+
2649+ if (isa<DoStmt>(S))
2650+ return true ;
2651+ }
2652+ return false ;
2653+ }
2654+ // Otherwise do the semantic checking to verify the statement produces
2655+ // a single value.
2656+ return S->mayProduceSingleValue (ctx).isValid ();
2657+ }
2658+ return false ;
2659+ }
2660+
26332661ThenStmt *SingleValueStmtExpr::getThenStmtFrom (BraceStmt *BS) {
26342662 if (BS->empty ())
26352663 return nullptr ;
0 commit comments