@@ -213,14 +213,17 @@ class ScopeCreator final {
213213
214214 // / Given an array of ASTNodes or Decl pointers, add them
215215 // / Return the resultant insertionPoint
216+ // /
217+ // / \param endLoc The end location for any "scopes until the end" that
218+ // / we introduce here, such as PatternEntryDeclScope and GuardStmtScope
216219 ASTScopeImpl *
217220 addSiblingsToScopeTree (ASTScopeImpl *const insertionPoint,
218- ASTScopeImpl * const organicInsertionPoint ,
219- ArrayRef<ASTNode> nodesOrDeclsToAdd ) {
221+ ArrayRef<ASTNode> nodesOrDeclsToAdd ,
222+ Optional<SourceLoc> endLoc ) {
220223 auto *ip = insertionPoint;
221- for (auto nd : sortBySourceRange ( cull ( nodesOrDeclsToAdd)) ) {
224+ for (auto nd : nodesOrDeclsToAdd) {
222225 auto *const newIP =
223- addToScopeTreeAndReturnInsertionPoint (nd, ip).getPtrOr (ip);
226+ addToScopeTreeAndReturnInsertionPoint (nd, ip, endLoc ).getPtrOr (ip);
224227 ip = newIP;
225228 }
226229 return ip;
@@ -229,12 +232,16 @@ class ScopeCreator final {
229232public:
230233 // / For each of searching, call this unless the insertion point is needed
231234 void addToScopeTree (ASTNode n, ASTScopeImpl *parent) {
232- (void )addToScopeTreeAndReturnInsertionPoint (n, parent);
235+ (void )addToScopeTreeAndReturnInsertionPoint (n, parent, None );
233236 }
234237 // / Return new insertion point if the scope was not a duplicate
235238 // / For ease of searching, don't call unless insertion point is needed
239+ // /
240+ // / \param endLoc The end location for any "scopes until the end" that
241+ // / we introduce here, such as PatternEntryDeclScope and GuardStmtScope
236242 NullablePtr<ASTScopeImpl>
237- addToScopeTreeAndReturnInsertionPoint (ASTNode, ASTScopeImpl *parent);
243+ addToScopeTreeAndReturnInsertionPoint (ASTNode, ASTScopeImpl *parent,
244+ Optional<SourceLoc> endLoc);
238245
239246 bool isWorthTryingToCreateScopeFor (ASTNode n) const {
240247 if (!n)
@@ -419,9 +426,18 @@ class ScopeCreator final {
419426 void addChildrenForKnownAttributes (ValueDecl *decl,
420427 ASTScopeImpl *parent);
421428
422- public:
429+ // / Add PatternEntryDeclScopes for each pattern binding entry.
430+ // /
431+ // / Returns the new insertion point.
432+ // /
433+ // / \param endLoc Must be valid iff the pattern binding is in a local
434+ // / scope, in which case this is the last source location where the
435+ // / pattern bindings are going to be visible.
436+ NullablePtr<ASTScopeImpl>
437+ addPatternBindingToScopeTree (PatternBindingDecl *patternBinding,
438+ ASTScopeImpl *parent,
439+ Optional<SourceLoc> endLoc);
423440
424- private:
425441 // / Remove VarDecls because we'll find them when we expand the
426442 // / PatternBindingDecls. Remove EnunCases
427443 // / because they overlap EnumElements and AST includes the elements in the
@@ -463,7 +479,6 @@ class ScopeCreator final {
463479 return -1 == signum;
464480 }
465481
466- public:
467482 SWIFT_DEBUG_DUMP { print (llvm::errs ()); }
468483
469484 void print (raw_ostream &out) const {
@@ -545,7 +560,10 @@ class NodeAdder
545560 : public ASTVisitor<NodeAdder, NullablePtr<ASTScopeImpl>,
546561 NullablePtr<ASTScopeImpl>, NullablePtr<ASTScopeImpl>,
547562 void , void , void , ASTScopeImpl *, ScopeCreator &> {
563+ Optional<SourceLoc> endLoc;
564+
548565public:
566+ explicit NodeAdder (Optional<SourceLoc> endLoc) : endLoc(endLoc) {}
549567
550568#pragma mark ASTNodes that do not create scopes
551569
@@ -637,7 +655,9 @@ class NodeAdder
637655 // the deferred nodes.
638656 NullablePtr<ASTScopeImpl> visitGuardStmt (GuardStmt *e, ASTScopeImpl *p,
639657 ScopeCreator &scopeCreator) {
640- return scopeCreator.ifUniqueConstructExpandAndInsert <GuardStmtScope>(p, e);
658+ ASTScopeAssert (endLoc.hasValue (), " GuardStmt outside of a BraceStmt?" );
659+ return scopeCreator.ifUniqueConstructExpandAndInsert <GuardStmtScope>(
660+ p, e, *endLoc);
641661 }
642662 NullablePtr<ASTScopeImpl> visitTopLevelCodeDecl (TopLevelCodeDecl *d,
643663 ASTScopeImpl *p,
@@ -698,28 +718,8 @@ class NodeAdder
698718 visitPatternBindingDecl (PatternBindingDecl *patternBinding,
699719 ASTScopeImpl *parentScope,
700720 ScopeCreator &scopeCreator) {
701- if (auto *var = patternBinding->getSingleVar ())
702- scopeCreator.addChildrenForKnownAttributes (var, parentScope);
703-
704- auto *insertionPoint = parentScope;
705- for (auto i : range (patternBinding->getNumPatternEntries ())) {
706- bool isLocalBinding = false ;
707- if (auto *varDecl = patternBinding->getAnchoringVarDecl (i)) {
708- isLocalBinding = varDecl->getDeclContext ()->isLocalContext ();
709- }
710-
711- insertionPoint =
712- scopeCreator
713- .ifUniqueConstructExpandAndInsert <PatternEntryDeclScope>(
714- insertionPoint, patternBinding, i, isLocalBinding)
715- .getPtrOr (insertionPoint);
716-
717- ASTScopeAssert (isLocalBinding || insertionPoint == parentScope,
718- " Bindings at the top-level or members of types should "
719- " not change the insertion point" );
720- }
721-
722- return insertionPoint;
721+ return scopeCreator.addPatternBindingToScopeTree (
722+ patternBinding, parentScope, endLoc);
723723 }
724724
725725 NullablePtr<ASTScopeImpl> visitEnumElementDecl (EnumElementDecl *eed,
@@ -773,15 +773,18 @@ class NodeAdder
773773// NodeAdder
774774NullablePtr<ASTScopeImpl>
775775ScopeCreator::addToScopeTreeAndReturnInsertionPoint (ASTNode n,
776- ASTScopeImpl *parent) {
776+ ASTScopeImpl *parent,
777+ Optional<SourceLoc> endLoc) {
777778 if (!isWorthTryingToCreateScopeFor (n))
778779 return parent;
780+
781+ NodeAdder adder (endLoc);
779782 if (auto *p = n.dyn_cast <Decl *>())
780- return NodeAdder () .visit (p, parent, *this );
783+ return adder .visit (p, parent, *this );
781784 if (auto *p = n.dyn_cast <Expr *>())
782- return NodeAdder () .visit (p, parent, *this );
785+ return adder .visit (p, parent, *this );
783786 auto *p = n.get <Stmt *>();
784- return NodeAdder () .visit (p, parent, *this );
787+ return adder .visit (p, parent, *this );
785788}
786789
787790void ScopeCreator::addChildrenForAllLocalizableAccessorsInSourceOrder (
@@ -831,6 +834,41 @@ void ScopeCreator::addChildrenForKnownAttributes(ValueDecl *decl,
831834 }
832835}
833836
837+ NullablePtr<ASTScopeImpl>
838+ ScopeCreator::addPatternBindingToScopeTree (PatternBindingDecl *patternBinding,
839+ ASTScopeImpl *parentScope,
840+ Optional<SourceLoc> endLoc) {
841+ if (auto *var = patternBinding->getSingleVar ())
842+ addChildrenForKnownAttributes (var, parentScope);
843+
844+ auto *insertionPoint = parentScope;
845+ for (auto i : range (patternBinding->getNumPatternEntries ())) {
846+ bool isLocalBinding = false ;
847+ if (auto *varDecl = patternBinding->getAnchoringVarDecl (i)) {
848+ isLocalBinding = varDecl->getDeclContext ()->isLocalContext ();
849+ }
850+
851+ Optional<SourceLoc> endLocForBinding = None;
852+ if (isLocalBinding) {
853+ endLocForBinding = endLoc;
854+ ASTScopeAssert (endLoc.hasValue () && endLoc->isValid (),
855+ " PatternBindingDecl in local context outside of BraceStmt?" );
856+ }
857+
858+ insertionPoint =
859+ ifUniqueConstructExpandAndInsert<PatternEntryDeclScope>(
860+ insertionPoint, patternBinding, i,
861+ isLocalBinding, endLocForBinding)
862+ .getPtrOr (insertionPoint);
863+
864+ ASTScopeAssert (isLocalBinding || insertionPoint == parentScope,
865+ " Bindings at the top-level or members of types should "
866+ " not change the insertion point" );
867+ }
868+
869+ return insertionPoint;
870+ }
871+
834872#pragma mark creation helpers
835873
836874void ASTScopeImpl::addChild (ASTScopeImpl *child, ASTContext &ctx) {
@@ -950,7 +988,10 @@ ASTSourceFileScope::expandAScopeThatCreatesANewInsertionPoint(
950988 // Assume that decls are only added at the end, in source order
951989 std::vector<ASTNode> newNodes (decls.begin (), decls.end ());
952990 insertionPoint =
953- scopeCreator.addSiblingsToScopeTree (insertionPoint, this , newNodes);
991+ scopeCreator.addSiblingsToScopeTree (insertionPoint,
992+ scopeCreator.sortBySourceRange (
993+ scopeCreator.cull (newNodes)),
994+ None);
954995 // Too slow to perform all the time:
955996 // ASTScopeAssert(scopeCreator->containsAllDeclContextsFromAST(),
956997 // "ASTScope tree missed some DeclContexts or made some up");
@@ -991,7 +1032,7 @@ PatternEntryDeclScope::expandAScopeThatCreatesANewInsertionPoint(
9911032 " Original inits are always after the '='" );
9921033 scopeCreator
9931034 .constructExpandAndInsertUncheckable <PatternEntryInitializerScope>(
994- this , decl, patternEntryIndex, isLocalBinding );
1035+ this , decl, patternEntryIndex);
9951036 }
9961037
9971038 // Add accessors for the variables in this pattern.
@@ -1050,7 +1091,7 @@ GuardStmtScope::expandAScopeThatCreatesANewInsertionPoint(ScopeCreator &
10501091 auto *const lookupParentDiversionScope =
10511092 scopeCreator
10521093 .constructExpandAndInsertUncheckable <LookupParentDiversionScope>(
1053- this , conditionLookupParent, stmt->getEndLoc ());
1094+ this , conditionLookupParent, stmt->getEndLoc (), endLoc );
10541095 return {lookupParentDiversionScope,
10551096 " Succeeding code must be in scope of guard variables" };
10561097}
@@ -1068,7 +1109,11 @@ BraceStmtScope::expandAScopeThatCreatesANewInsertionPoint(
10681109 // TODO: remove the sort after fixing parser to create brace statement
10691110 // elements in source order
10701111 auto *insertionPoint =
1071- scopeCreator.addSiblingsToScopeTree (this , this , stmt->getElements ());
1112+ scopeCreator.addSiblingsToScopeTree (this ,
1113+ scopeCreator.sortBySourceRange (
1114+ scopeCreator.cull (
1115+ stmt->getElements ())),
1116+ stmt->getEndLoc ());
10721117 if (auto *s = scopeCreator.getASTContext ().Stats )
10731118 ++s->getFrontendCounters ().NumBraceStmtASTScopeExpansions ;
10741119 return {
@@ -1082,7 +1127,7 @@ TopLevelCodeScope::expandAScopeThatCreatesANewInsertionPoint(ScopeCreator &
10821127
10831128 if (auto *body =
10841129 scopeCreator
1085- .addToScopeTreeAndReturnInsertionPoint (decl->getBody (), this )
1130+ .addToScopeTreeAndReturnInsertionPoint (decl->getBody (), this , None )
10861131 .getPtrOrNull ())
10871132 return {body, " So next top level code scope and put its decls in its body "
10881133 " under a guard statement scope (etc) from the last top level "
@@ -1377,10 +1422,8 @@ ASTScopeImpl *LabeledConditionalStmtScope::createNestedConditionalClauseScopes(
13771422}
13781423
13791424AbstractPatternEntryScope::AbstractPatternEntryScope (
1380- PatternBindingDecl *declBeingScoped, unsigned entryIndex,
1381- bool isLocalBinding)
1382- : decl(declBeingScoped), patternEntryIndex(entryIndex),
1383- isLocalBinding(isLocalBinding) {
1425+ PatternBindingDecl *declBeingScoped, unsigned entryIndex)
1426+ : decl(declBeingScoped), patternEntryIndex(entryIndex) {
13841427 ASTScopeAssert (entryIndex < declBeingScoped->getPatternList ().size (),
13851428 " out of bounds" );
13861429}
@@ -1414,7 +1457,8 @@ void GenericTypeOrExtensionScope::expandBody(ScopeCreator &) {}
14141457
14151458void IterableTypeScope::expandBody (ScopeCreator &scopeCreator) {
14161459 auto nodes = asNodeVector (getIterableDeclContext ().get ()->getMembers ());
1417- scopeCreator.addSiblingsToScopeTree (this , this , nodes);
1460+ nodes = scopeCreator.sortBySourceRange (scopeCreator.cull (nodes));
1461+ scopeCreator.addSiblingsToScopeTree (this , nodes, None);
14181462 if (auto *s = scopeCreator.getASTContext ().Stats )
14191463 ++s->getFrontendCounters ().NumIterableTypeBodyASTScopeExpansions ;
14201464}
0 commit comments