@@ -31,8 +31,12 @@ ASTScope::ASTScope(const ASTScope *parent, ArrayRef<ASTScope *> children)
31
31
: ASTScope(ASTScopeKind::Preexpanded, parent) {
32
32
assert (children.size () > 1 && " Don't use this without multiple nodes" );
33
33
34
- // Add child nodes.
35
- storedChildren.append (children.begin (), children.end ());
34
+ // Add child nodes, reparenting them to this node.
35
+ storedChildren.reserve (children.size ());
36
+ for (auto child : children ) {
37
+ child->parentAndExpanded .setPointer (this );
38
+ storedChildren.push_back (child);
39
+ }
36
40
37
41
// Note that this node has already been expanded.
38
42
parentAndExpanded.setInt (true );
@@ -206,22 +210,40 @@ void ASTScope::expand() const {
206
210
addChild (child);
207
211
break ;
208
212
209
- case ASTScopeKind::AfterPatternBinding : {
213
+ case ASTScopeKind::PatternBinding : {
210
214
const auto &patternEntry =
211
215
patternBinding.decl ->getPatternList ()[patternBinding.entry ];
212
216
213
- // Create a child for the initializer expression, if present.
214
- if (auto child = createIfNeeded (this , patternEntry.getInit ()))
215
- addChild (child);
217
+ // Create a child for the initializer, if present.
218
+ if (patternEntry.getInit ())
219
+ addChild (new (ctx) ASTScope (ASTScopeKind::PatternInitializer, this ,
220
+ patternBinding.decl , patternBinding.entry ));
216
221
217
- // Create children for the variables in the pattern, if needed.
222
+ // Create children for the accessors of any variables in the pattern that
223
+ // have them.
218
224
patternEntry.getPattern ()->forEachVariable ([&](VarDecl *var) {
219
- if (hasAccessors (var)) {
220
- if (auto varChild = createIfNeeded (this , var))
221
- addChild (varChild);
222
- }
225
+ if (hasAccessors (var))
226
+ addChild (new (ctx) ASTScope (this , var));
223
227
});
224
228
229
+ // If the pattern binding is in a local context, we nest the remaining
230
+ // pattern bindings.
231
+ if (patternBinding.decl ->getDeclContext ()->isLocalContext ()) {
232
+ addChild (new (ctx) ASTScope (ASTScopeKind::AfterPatternBinding, this ,
233
+ patternBinding.decl , patternBinding.entry ));
234
+ }
235
+ break ;
236
+ }
237
+
238
+ case ASTScopeKind::PatternInitializer:
239
+ // Create a child for the initializer expression.
240
+ if (auto child =
241
+ createIfNeeded (this ,
242
+ patternBinding.decl ->getInit (patternBinding.entry )))
243
+ addChild (child);
244
+ break ;
245
+
246
+ case ASTScopeKind::AfterPatternBinding: {
225
247
// Create a child for the next pattern binding.
226
248
if (auto child = createIfNeeded (this , patternBinding.decl ))
227
249
addChild (child);
@@ -557,6 +579,8 @@ static bool parentDirectDescendedFromLocalDeclaration(const ASTScope *parent,
557
579
558
580
case ASTScopeKind::Preexpanded:
559
581
case ASTScopeKind::SourceFile:
582
+ case ASTScopeKind::PatternBinding:
583
+ case ASTScopeKind::PatternInitializer:
560
584
case ASTScopeKind::AfterPatternBinding:
561
585
case ASTScopeKind::BraceStmt:
562
586
case ASTScopeKind::ConditionalClause:
@@ -600,6 +624,8 @@ static bool parentDirectDescendedFromAbstractStorageDecl(
600
624
case ASTScopeKind::SourceFile:
601
625
case ASTScopeKind::TypeOrExtensionBody:
602
626
case ASTScopeKind::LocalDeclaration:
627
+ case ASTScopeKind::PatternBinding:
628
+ case ASTScopeKind::PatternInitializer:
603
629
case ASTScopeKind::AfterPatternBinding:
604
630
case ASTScopeKind::BraceStmt:
605
631
case ASTScopeKind::ConditionalClause:
@@ -642,6 +668,8 @@ static bool parentDirectDescendedFromAbstractFunctionDecl(
642
668
case ASTScopeKind::SourceFile:
643
669
case ASTScopeKind::TypeOrExtensionBody:
644
670
case ASTScopeKind::LocalDeclaration:
671
+ case ASTScopeKind::PatternBinding:
672
+ case ASTScopeKind::PatternInitializer:
645
673
case ASTScopeKind::AfterPatternBinding:
646
674
case ASTScopeKind::Accessors:
647
675
case ASTScopeKind::BraceStmt:
@@ -738,6 +766,10 @@ ASTScope *ASTScope::createIfNeeded(const ASTScope *parent, Decl *decl) {
738
766
// These declarations do not introduce scopes.
739
767
return nullptr ;
740
768
769
+ case DeclKind::Var:
770
+ // Always handled by a pattern-binding declaration.
771
+ return nullptr ;
772
+
741
773
case DeclKind::Extension:
742
774
return new (ctx) ASTScope (parent, cast<ExtensionDecl>(decl));
743
775
@@ -821,24 +853,40 @@ ASTScope *ASTScope::createIfNeeded(const ASTScope *parent, Decl *decl) {
821
853
}
822
854
823
855
case DeclKind::PatternBinding: {
824
- // Only pattern bindings in local scope introduce scopes.
825
- if (!inLocalContext) return nullptr ;
826
-
827
- // If there are no bindings, we're done.
828
856
auto patternBinding = cast<PatternBindingDecl>(decl);
829
857
830
- // Find the next pattern binding.
831
- unsigned entry = (parent->getKind () == ASTScopeKind::AfterPatternBinding &&
832
- parent->patternBinding .decl == decl)
833
- ? parent->patternBinding .entry + 1
834
- : 0 ;
835
- if (entry < patternBinding->getPatternList ().size ())
836
- return new (ctx) ASTScope (parent, patternBinding, entry);
858
+ // Within a local context, bindings nest.
859
+ if (inLocalContext) {
860
+ // Find the next pattern binding.
861
+ unsigned entry = (parent->getKind () == ASTScopeKind::AfterPatternBinding&&
862
+ parent->patternBinding .decl == decl)
863
+ ? parent->patternBinding .entry + 1
864
+ : 0 ;
865
+ if (entry < patternBinding->getPatternList ().size ())
866
+ return new (ctx) ASTScope (ASTScopeKind::PatternBinding, parent,
867
+ patternBinding, entry);
837
868
838
- return nullptr ;
869
+ return nullptr ;
870
+ }
871
+
872
+ // Elsewhere, explode out the bindings because they're independent.
873
+
874
+ // Handle a single binding directly.
875
+ if (patternBinding->getNumPatternEntries () == 1 )
876
+ return new (ctx) ASTScope (ASTScopeKind::PatternBinding, parent,
877
+ patternBinding, 0 );
878
+
879
+
880
+ // Pre-expand when there are multiple bindings.
881
+ SmallVector<ASTScope *, 4 > bindings;
882
+ for (auto entry : range (patternBinding->getNumPatternEntries ())) {
883
+ bindings.push_back (new (ctx) ASTScope (ASTScopeKind::PatternBinding,
884
+ parent, patternBinding, entry));
885
+ }
886
+
887
+ return new (ctx) ASTScope (parent, bindings);
839
888
}
840
889
841
- case DeclKind::Var:
842
890
case DeclKind::Subscript: {
843
891
auto asd = cast<AbstractStorageDecl>(decl);
844
892
if (hasAccessors (asd))
@@ -1000,6 +1048,8 @@ bool ASTScope::isContinuationScope() const {
1000
1048
case ASTScopeKind::GenericParams:
1001
1049
case ASTScopeKind::AbstractFunctionDecl:
1002
1050
case ASTScopeKind::AbstractFunctionParams:
1051
+ case ASTScopeKind::PatternBinding:
1052
+ case ASTScopeKind::PatternInitializer:
1003
1053
case ASTScopeKind::Accessors:
1004
1054
case ASTScopeKind::BraceStmt:
1005
1055
case ASTScopeKind::IfStmt:
@@ -1046,6 +1096,7 @@ void ASTScope::enumerateContinuationScopes(
1046
1096
switch (continuation->getKind ()) {
1047
1097
case ASTScopeKind::Preexpanded:
1048
1098
case ASTScopeKind::SourceFile:
1099
+ case ASTScopeKind::PatternInitializer:
1049
1100
case ASTScopeKind::IfStmt:
1050
1101
case ASTScopeKind::RepeatWhileStmt:
1051
1102
case ASTScopeKind::ForEachStmt:
@@ -1074,6 +1125,7 @@ void ASTScope::enumerateContinuationScopes(
1074
1125
case ASTScopeKind::AbstractFunctionDecl:
1075
1126
case ASTScopeKind::AbstractFunctionParams:
1076
1127
case ASTScopeKind::GenericParams:
1128
+ case ASTScopeKind::PatternBinding:
1077
1129
case ASTScopeKind::AfterPatternBinding:
1078
1130
case ASTScopeKind::LocalDeclaration:
1079
1131
case ASTScopeKind::ConditionalClause:
@@ -1128,6 +1180,8 @@ ASTContext &ASTScope::getASTContext() const {
1128
1180
case ASTScopeKind::AbstractFunctionParams:
1129
1181
return abstractFunctionParams.decl ->getASTContext ();
1130
1182
1183
+ case ASTScopeKind::PatternBinding:
1184
+ case ASTScopeKind::PatternInitializer:
1131
1185
case ASTScopeKind::AfterPatternBinding:
1132
1186
return patternBinding.decl ->getASTContext ();
1133
1187
@@ -1230,23 +1284,35 @@ SourceRange ASTScope::getSourceRangeImpl() const {
1230
1284
auto param = abstractFunctionParams.decl ->getParameterList (
1231
1285
abstractFunctionParams.listIndex )
1232
1286
->get (abstractFunctionParams.paramIndex );
1233
- // FIXME: getLocForEndOfToken... but I can't use it here.
1234
1287
return SourceRange (param->getEndLoc (), endLoc);
1235
1288
}
1236
1289
1290
+ case ASTScopeKind::PatternBinding: {
1291
+ const auto &patternEntry =
1292
+ patternBinding.decl ->getPatternList ()[patternBinding.entry ];
1293
+
1294
+ SourceRange range = patternEntry.getSourceRange ();
1295
+
1296
+ // Local pattern bindings cover the entire binding + continuation.
1297
+ if (patternBinding.decl ->getDeclContext ()->isLocalContext ()) {
1298
+ range.End = getParent ()->getSourceRange ().End ;
1299
+ }
1300
+
1301
+ return range;
1302
+ }
1303
+
1304
+ case ASTScopeKind::PatternInitializer:
1305
+ return patternBinding.decl ->getInit (patternBinding.entry )->getSourceRange ();
1306
+
1237
1307
case ASTScopeKind::AfterPatternBinding: {
1238
1308
const auto &patternEntry =
1239
1309
patternBinding.decl ->getPatternList ()[patternBinding.entry ];
1240
- // The scope of the binding begins after the initializer (if there is one);
1241
- // other, after the pattern itself.
1242
- SourceLoc startLoc = patternEntry.getOrigInitRange ().End ;
1243
- if (startLoc.isInvalid ())
1244
- startLoc = patternEntry.getPattern ()->getSourceRange ().End ;
1310
+ // The scope of the binding begins at the end of the binding.
1311
+ SourceLoc startLoc = patternEntry.getSourceRange ().End ;
1245
1312
1246
1313
// And extends to the end of the parent range.
1247
1314
return SourceRange (startLoc, getParent ()->getSourceRange ().End );
1248
1315
}
1249
-
1250
1316
case ASTScopeKind::BraceStmt:
1251
1317
// The brace statements that represent closures start their scope at the
1252
1318
// 'in' keyword, when present.
@@ -1533,6 +1599,20 @@ void ASTScope::print(llvm::raw_ostream &out, unsigned level,
1533
1599
printRange ();
1534
1600
break ;
1535
1601
1602
+ case ASTScopeKind::PatternBinding:
1603
+ printScopeKind (" PatternBinding" );
1604
+ printAddress (patternBinding.decl );
1605
+ out << " entry " << patternBinding.entry ;
1606
+ printRange ();
1607
+ break ;
1608
+
1609
+ case ASTScopeKind::PatternInitializer:
1610
+ printScopeKind (" PatternInitializer" );
1611
+ printAddress (patternBinding.decl );
1612
+ out << " entry " << patternBinding.entry ;
1613
+ printRange ();
1614
+ break ;
1615
+
1536
1616
case ASTScopeKind::AfterPatternBinding:
1537
1617
printScopeKind (" AfterPatternBinding" );
1538
1618
printAddress (patternBinding.decl );
0 commit comments