@@ -383,21 +383,51 @@ class ScopeCreator final {
383
383
384
384
void addExprToScopeTree (Expr *expr, ASTScopeImpl *parent) {
385
385
// 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));
391
428
}
392
429
393
430
private:
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
-
401
431
// A safe way to discover this, without creating a circular request.
402
432
// Cannot call getAttachedPropertyWrappers.
403
433
static bool hasAttachedPropertyWrapper (VarDecl *vd) {
@@ -1171,7 +1201,7 @@ NO_NEW_INSERTION_POINT(CaptureListScope)
1171
1201
NO_NEW_INSERTION_POINT(CaseStmtScope)
1172
1202
NO_NEW_INSERTION_POINT(CaseLabelItemScope)
1173
1203
NO_NEW_INSERTION_POINT(CaseStmtBodyScope)
1174
- NO_NEW_INSERTION_POINT(ClosureBodyScope )
1204
+ NO_NEW_INSERTION_POINT(ClosureParametersScope )
1175
1205
NO_NEW_INSERTION_POINT(DefaultArgumentInitializerScope)
1176
1206
NO_NEW_INSERTION_POINT(DoStmtScope)
1177
1207
NO_NEW_INSERTION_POINT(DoCatchStmtScope)
@@ -1183,10 +1213,8 @@ NO_NEW_INSERTION_POINT(SubscriptDeclScope)
1183
1213
NO_NEW_INSERTION_POINT(SwitchStmtScope)
1184
1214
NO_NEW_INSERTION_POINT(VarDeclScope)
1185
1215
NO_NEW_INSERTION_POINT(WhileStmtScope)
1186
- NO_NEW_INSERTION_POINT(WholeClosureScope)
1187
1216
1188
1217
NO_EXPANSION(GenericParamScope)
1189
- NO_EXPANSION(ClosureParametersScope)
1190
1218
NO_EXPANSION(SpecializeAttributeScope)
1191
1219
NO_EXPANSION(DifferentiableAttributeScope)
1192
1220
NO_EXPANSION(ConditionalClausePatternUseScope)
@@ -1525,35 +1553,15 @@ void SubscriptDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
1525
1553
scopeCreator.addChildrenForAllLocalizableAccessorsInSourceOrder (sub, params);
1526
1554
}
1527
1555
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
-
1543
1556
void CaptureListScope::expandAScopeThatDoesNotCreateANewInsertionPoint (
1544
1557
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);
1554
1562
}
1555
1563
1556
- void ClosureBodyScope ::expandAScopeThatDoesNotCreateANewInsertionPoint (
1564
+ void ClosureParametersScope ::expandAScopeThatDoesNotCreateANewInsertionPoint (
1557
1565
ScopeCreator &scopeCreator) {
1558
1566
scopeCreator.addToScopeTree (closureExpr->getBody (), this );
1559
1567
}
@@ -1733,51 +1741,6 @@ bool ASTScopeImpl::isATypeDeclScope() const {
1733
1741
return pd && (isa<NominalTypeDecl>(pd) || isa<ExtensionDecl>(pd));
1734
1742
}
1735
1743
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
-
1781
1744
#pragma mark new operators
1782
1745
void *ASTScopeImpl::operator new (size_t bytes, const ASTContext &ctx,
1783
1746
unsigned alignment) {
@@ -1839,7 +1802,7 @@ GET_REFERRENT(VarDeclScope, getDecl())
1839
1802
GET_REFERRENT(GenericParamScope, paramList->getParams ()[index])
1840
1803
GET_REFERRENT(AbstractStmtScope, getStmt())
1841
1804
GET_REFERRENT(CaptureListScope, getExpr())
1842
- GET_REFERRENT(WholeClosureScope , getExpr())
1805
+ GET_REFERRENT(ClosureParametersScope , getExpr())
1843
1806
GET_REFERRENT(SpecializeAttributeScope, specializeAttr)
1844
1807
GET_REFERRENT(DifferentiableAttributeScope, differentiableAttr)
1845
1808
GET_REFERRENT(GenericTypeOrExtensionScope, portion->getReferrentOfScope (this ));
@@ -1980,13 +1943,6 @@ bool PatternEntryDeclScope::isCurrentIfWasExpanded() const {
1980
1943
return getPatternEntry ().getNumBoundVariables () == varCountWhenLastExpanded;
1981
1944
}
1982
1945
1983
- void WholeClosureScope::beCurrent () {
1984
- bodyWhenLastExpanded = closureExpr->getBody ();
1985
- }
1986
- bool WholeClosureScope::isCurrentIfWasExpanded () const {
1987
- return bodyWhenLastExpanded == closureExpr->getBody ();
1988
- }
1989
-
1990
1946
#pragma mark getParentOfASTAncestorScopesToBeRescued
1991
1947
NullablePtr<ASTScopeImpl>
1992
1948
ASTScopeImpl::getParentOfASTAncestorScopesToBeRescued () {
0 commit comments