@@ -383,21 +383,51 @@ class ScopeCreator final {
383383
384384 void addExprToScopeTree (Expr *expr, ASTScopeImpl *parent) {
385385 // Use the ASTWalker to find buried captures and closures
386- forEachClosureIn (expr, [&](NullablePtr<CaptureListExpr> captureList,
387- ClosureExpr *closureExpr) {
388- ifUniqueConstructExpandAndInsert<WholeClosureScope>(parent, closureExpr,
389- captureList);
390- });
386+ ASTScopeAssert (expr,
387+ " If looking for closures, must have an expression to search." );
388+
389+ // / AST walker that finds top-level closures in an expression.
390+ class ClosureFinder : public ASTWalker {
391+ ScopeCreator &scopeCreator;
392+ ASTScopeImpl *parent;
393+
394+ public:
395+ ClosureFinder (ScopeCreator &scopeCreator, ASTScopeImpl *parent)
396+ : scopeCreator(scopeCreator), parent(parent) {}
397+
398+ std::pair<bool , Expr *> walkToExprPre (Expr *E) override {
399+ if (auto *closure = dyn_cast<ClosureExpr>(E)) {
400+ scopeCreator
401+ .ifUniqueConstructExpandAndInsert <ClosureParametersScope>(
402+ parent, closure);
403+ return {false , E};
404+ }
405+ if (auto *capture = dyn_cast<CaptureListExpr>(E)) {
406+ scopeCreator
407+ .ifUniqueConstructExpandAndInsert <CaptureListScope>(
408+ parent, capture);
409+ return {false , E};
410+ }
411+ return {true , E};
412+ }
413+ std::pair<bool , Stmt *> walkToStmtPre (Stmt *S) override {
414+ if (isa<BraceStmt>(S)) { // closures hidden in here
415+ return {true , S};
416+ }
417+ return {false , S};
418+ }
419+ std::pair<bool , Pattern *> walkToPatternPre (Pattern *P) override {
420+ return {false , P};
421+ }
422+ bool walkToDeclPre (Decl *D) override { return false ; }
423+ bool walkToTypeReprPre (TypeRepr *T) override { return false ; }
424+ bool walkToParameterListPre (ParameterList *PL) override { return false ; }
425+ };
426+
427+ expr->walk (ClosureFinder (*this , parent));
391428 }
392429
393430private:
394- // / Find all of the (non-nested) closures (and associated capture lists)
395- // / referenced within this expression.
396- void forEachClosureIn (
397- Expr *expr,
398- function_ref<void (NullablePtr<CaptureListExpr>, ClosureExpr *)>
399- foundClosure);
400-
401431 // A safe way to discover this, without creating a circular request.
402432 // Cannot call getAttachedPropertyWrappers.
403433 static bool hasAttachedPropertyWrapper (VarDecl *vd) {
@@ -1171,7 +1201,7 @@ NO_NEW_INSERTION_POINT(CaptureListScope)
11711201NO_NEW_INSERTION_POINT(CaseStmtScope)
11721202NO_NEW_INSERTION_POINT(CaseLabelItemScope)
11731203NO_NEW_INSERTION_POINT(CaseStmtBodyScope)
1174- NO_NEW_INSERTION_POINT(ClosureBodyScope )
1204+ NO_NEW_INSERTION_POINT(ClosureParametersScope )
11751205NO_NEW_INSERTION_POINT(DefaultArgumentInitializerScope)
11761206NO_NEW_INSERTION_POINT(DoStmtScope)
11771207NO_NEW_INSERTION_POINT(DoCatchStmtScope)
@@ -1183,10 +1213,8 @@ NO_NEW_INSERTION_POINT(SubscriptDeclScope)
11831213NO_NEW_INSERTION_POINT(SwitchStmtScope)
11841214NO_NEW_INSERTION_POINT(VarDeclScope)
11851215NO_NEW_INSERTION_POINT(WhileStmtScope)
1186- NO_NEW_INSERTION_POINT(WholeClosureScope)
11871216
11881217NO_EXPANSION(GenericParamScope)
1189- NO_EXPANSION(ClosureParametersScope)
11901218NO_EXPANSION(SpecializeAttributeScope)
11911219NO_EXPANSION(DifferentiableAttributeScope)
11921220NO_EXPANSION(ConditionalClausePatternUseScope)
@@ -1525,35 +1553,15 @@ void SubscriptDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
15251553 scopeCreator.addChildrenForAllLocalizableAccessorsInSourceOrder (sub, params);
15261554}
15271555
1528- void WholeClosureScope::expandAScopeThatDoesNotCreateANewInsertionPoint (
1529- ScopeCreator &scopeCreator) {
1530- if (auto *cl = captureList.getPtrOrNull ())
1531- scopeCreator.ensureUniqueThenConstructExpandAndInsert <CaptureListScope>(
1532- this , cl);
1533- ASTScopeImpl *bodyParent = this ;
1534- if (closureExpr->getInLoc ().isValid ())
1535- bodyParent =
1536- scopeCreator
1537- .constructExpandAndInsertUncheckable <ClosureParametersScope>(
1538- this , closureExpr, captureList);
1539- scopeCreator.constructExpandAndInsertUncheckable <ClosureBodyScope>(
1540- bodyParent, closureExpr, captureList);
1541- }
1542-
15431556void CaptureListScope::expandAScopeThatDoesNotCreateANewInsertionPoint (
15441557 ScopeCreator &scopeCreator) {
1545- // Patterns here are implicit, so need to dig out the intializers
1546- for (const CaptureListEntry &captureListEntry : expr->getCaptureList ()) {
1547- for (unsigned patternEntryIndex = 0 ;
1548- patternEntryIndex < captureListEntry.Init ->getNumPatternEntries ();
1549- ++patternEntryIndex) {
1550- Expr *init = captureListEntry.Init ->getInit (patternEntryIndex);
1551- scopeCreator.addExprToScopeTree (init, this );
1552- }
1553- }
1558+ auto *closureExpr = expr->getClosureBody ();
1559+ scopeCreator
1560+ .ifUniqueConstructExpandAndInsert <ClosureParametersScope>(
1561+ this , closureExpr);
15541562}
15551563
1556- void ClosureBodyScope ::expandAScopeThatDoesNotCreateANewInsertionPoint (
1564+ void ClosureParametersScope ::expandAScopeThatDoesNotCreateANewInsertionPoint (
15571565 ScopeCreator &scopeCreator) {
15581566 scopeCreator.addToScopeTree (closureExpr->getBody (), this );
15591567}
@@ -1733,51 +1741,6 @@ bool ASTScopeImpl::isATypeDeclScope() const {
17331741 return pd && (isa<NominalTypeDecl>(pd) || isa<ExtensionDecl>(pd));
17341742}
17351743
1736- void ScopeCreator::forEachClosureIn (
1737- Expr *expr, function_ref<void (NullablePtr<CaptureListExpr>, ClosureExpr *)>
1738- foundClosure) {
1739- ASTScopeAssert (expr,
1740- " If looking for closures, must have an expression to search." );
1741-
1742- // / AST walker that finds top-level closures in an expression.
1743- class ClosureFinder : public ASTWalker {
1744- function_ref<void (NullablePtr<CaptureListExpr>, ClosureExpr *)>
1745- foundClosure;
1746-
1747- public:
1748- ClosureFinder (
1749- function_ref<void (NullablePtr<CaptureListExpr>, ClosureExpr *)>
1750- foundClosure)
1751- : foundClosure(foundClosure) {}
1752-
1753- std::pair<bool , Expr *> walkToExprPre (Expr *E) override {
1754- if (auto *closure = dyn_cast<ClosureExpr>(E)) {
1755- foundClosure (nullptr , closure);
1756- return {false , E};
1757- }
1758- if (auto *capture = dyn_cast<CaptureListExpr>(E)) {
1759- foundClosure (capture, capture->getClosureBody ());
1760- return {false , E};
1761- }
1762- return {true , E};
1763- }
1764- std::pair<bool , Stmt *> walkToStmtPre (Stmt *S) override {
1765- if (isa<BraceStmt>(S)) { // closures hidden in here
1766- return {true , S};
1767- }
1768- return {false , S};
1769- }
1770- std::pair<bool , Pattern *> walkToPatternPre (Pattern *P) override {
1771- return {false , P};
1772- }
1773- bool walkToDeclPre (Decl *D) override { return false ; }
1774- bool walkToTypeReprPre (TypeRepr *T) override { return false ; }
1775- bool walkToParameterListPre (ParameterList *PL) override { return false ; }
1776- };
1777-
1778- expr->walk (ClosureFinder (foundClosure));
1779- }
1780-
17811744#pragma mark new operators
17821745void *ASTScopeImpl::operator new (size_t bytes, const ASTContext &ctx,
17831746 unsigned alignment) {
@@ -1839,7 +1802,7 @@ GET_REFERRENT(VarDeclScope, getDecl())
18391802GET_REFERRENT(GenericParamScope, paramList->getParams ()[index])
18401803GET_REFERRENT(AbstractStmtScope, getStmt())
18411804GET_REFERRENT(CaptureListScope, getExpr())
1842- GET_REFERRENT(WholeClosureScope , getExpr())
1805+ GET_REFERRENT(ClosureParametersScope , getExpr())
18431806GET_REFERRENT(SpecializeAttributeScope, specializeAttr)
18441807GET_REFERRENT(DifferentiableAttributeScope, differentiableAttr)
18451808GET_REFERRENT(GenericTypeOrExtensionScope, portion->getReferrentOfScope (this ));
@@ -1980,13 +1943,6 @@ bool PatternEntryDeclScope::isCurrentIfWasExpanded() const {
19801943 return getPatternEntry ().getNumBoundVariables () == varCountWhenLastExpanded;
19811944}
19821945
1983- void WholeClosureScope::beCurrent () {
1984- bodyWhenLastExpanded = closureExpr->getBody ();
1985- }
1986- bool WholeClosureScope::isCurrentIfWasExpanded () const {
1987- return bodyWhenLastExpanded == closureExpr->getBody ();
1988- }
1989-
19901946#pragma mark getParentOfASTAncestorScopesToBeRescued
19911947NullablePtr<ASTScopeImpl>
19921948ASTScopeImpl::getParentOfASTAncestorScopesToBeRescued () {
0 commit comments