@@ -184,6 +184,13 @@ void ASTScope::expand() const {
184
184
break ;
185
185
}
186
186
187
+ case ASTScopeKind::ExtensionGenericParams: {
188
+ // Create a child node.
189
+ if (ASTScope *child = createIfNeeded (this , extension))
190
+ addChild (child);
191
+ break ;
192
+ }
193
+
187
194
case ASTScopeKind::TypeOrExtensionBody:
188
195
for (auto member : iterableDeclContext->getMembers ()) {
189
196
// Create a child node for this declaration.
@@ -228,18 +235,35 @@ void ASTScope::expand() const {
228
235
patternBinding.decl ->getPatternList ()[patternBinding.entry ];
229
236
230
237
// Create a child for the initializer, if present.
238
+ ASTScope *initChild = nullptr ;
231
239
if (patternEntry.getInit () &&
232
- patternEntry.getInit ()->getSourceRange ().isValid ())
233
- addChild (new (ctx) ASTScope (ASTScopeKind::PatternInitializer, this ,
234
- patternBinding.decl , patternBinding.entry ));
240
+ patternEntry.getInit ()->getSourceRange ().isValid ()) {
241
+ initChild = new (ctx) ASTScope (ASTScopeKind::PatternInitializer, this ,
242
+ patternBinding.decl , patternBinding.entry );
243
+ }
235
244
236
245
// Create children for the accessors of any variables in the pattern that
237
246
// have them.
238
247
patternEntry.getPattern ()->forEachVariable ([&](VarDecl *var) {
239
- if (hasAccessors (var))
248
+ if (hasAccessors (var)) {
249
+ // If there is an initializer child that precedes this node (the
250
+ // normal case), add teh initializer child first.
251
+ if (initChild &&
252
+ ctx.SourceMgr .isBeforeInBuffer (
253
+ patternBinding.decl ->getInit (patternBinding.entry )->getEndLoc (),
254
+ var->getBracesRange ().Start )) {
255
+ addChild (initChild);
256
+ initChild = nullptr ;
257
+ }
258
+
240
259
addChild (new (ctx) ASTScope (this , var));
260
+ }
241
261
});
242
262
263
+ // If an initializer child remains, add it now.
264
+ if (initChild)
265
+ addChild (initChild);
266
+
243
267
// If the pattern binding is in a local context, we nest the remaining
244
268
// pattern bindings.
245
269
if (patternBinding.decl ->getDeclContext ()->isLocalContext ()) {
@@ -583,6 +607,7 @@ static bool parentDirectDescendedFromLocalDeclaration(const ASTScope *parent,
583
607
case ASTScopeKind::AbstractFunctionDecl:
584
608
case ASTScopeKind::AbstractFunctionParams:
585
609
case ASTScopeKind::GenericParams:
610
+ case ASTScopeKind::ExtensionGenericParams:
586
611
case ASTScopeKind::TypeOrExtensionBody:
587
612
case ASTScopeKind::Accessors:
588
613
// Keep looking.
@@ -638,6 +663,7 @@ static bool parentDirectDescendedFromAbstractStorageDecl(
638
663
return (parent->getAbstractStorageDecl () == decl);
639
664
640
665
case ASTScopeKind::SourceFile:
666
+ case ASTScopeKind::ExtensionGenericParams:
641
667
case ASTScopeKind::TypeOrExtensionBody:
642
668
case ASTScopeKind::DefaultArgument:
643
669
case ASTScopeKind::AbstractFunctionBody:
@@ -686,6 +712,7 @@ static bool parentDirectDescendedFromAbstractFunctionDecl(
686
712
return (parent->getAbstractFunctionDecl () == decl);
687
713
688
714
case ASTScopeKind::SourceFile:
715
+ case ASTScopeKind::ExtensionGenericParams:
689
716
case ASTScopeKind::TypeOrExtensionBody:
690
717
case ASTScopeKind::LocalDeclaration:
691
718
case ASTScopeKind::PatternBinding:
@@ -791,8 +818,17 @@ ASTScope *ASTScope::createIfNeeded(const ASTScope *parent, Decl *decl) {
791
818
// Always handled by a pattern-binding declaration.
792
819
return nullptr ;
793
820
794
- case DeclKind::Extension:
795
- return new (ctx) ASTScope (parent, cast<ExtensionDecl>(decl));
821
+ case DeclKind::Extension: {
822
+ auto ext = cast<ExtensionDecl>(decl);
823
+
824
+ // If we already have a scope of the (possible) generic parameters,
825
+ // add the body.
826
+ if (parent->getKind () == ASTScopeKind::ExtensionGenericParams)
827
+ return new (ctx) ASTScope (parent, cast<IterableDeclContext>(ext));
828
+
829
+ // Otherwise, form the extension's generic parameters scope.
830
+ return new (ctx) ASTScope (parent, ext);
831
+ }
796
832
797
833
case DeclKind::TopLevelCode: {
798
834
// Drop top-level statements containing just an IfConfigStmt.
@@ -808,6 +844,10 @@ ASTScope *ASTScope::createIfNeeded(const ASTScope *parent, Decl *decl) {
808
844
return new (ctx) ASTScope (parent, topLevelCode);
809
845
}
810
846
847
+ case DeclKind::Protocol:
848
+ cast<ProtocolDecl>(decl)->createGenericParamsIfMissing ();
849
+ SWIFT_FALLTHROUGH;
850
+
811
851
case DeclKind::Class:
812
852
case DeclKind::Enum:
813
853
case DeclKind::Struct: {
@@ -821,9 +861,6 @@ ASTScope *ASTScope::createIfNeeded(const ASTScope *parent, Decl *decl) {
821
861
return new (ctx) ASTScope (parent, nominal);
822
862
}
823
863
824
- case DeclKind::Protocol:
825
- return new (ctx) ASTScope (parent, cast<ProtocolDecl>(decl));
826
-
827
864
case DeclKind::TypeAlias: {
828
865
// If we have a generic typealias and our parent isn't describing our
829
866
// generic parameters, build the generic parameter scope.
@@ -1089,6 +1126,7 @@ bool ASTScope::isContinuationScope() const {
1089
1126
switch (getKind ()) {
1090
1127
case ASTScopeKind::Preexpanded:
1091
1128
case ASTScopeKind::SourceFile:
1129
+ case ASTScopeKind::ExtensionGenericParams:
1092
1130
case ASTScopeKind::TypeOrExtensionBody:
1093
1131
case ASTScopeKind::GenericParams:
1094
1132
case ASTScopeKind::AbstractFunctionDecl:
@@ -1143,6 +1181,7 @@ void ASTScope::enumerateContinuationScopes(
1143
1181
switch (continuation->getKind ()) {
1144
1182
case ASTScopeKind::Preexpanded:
1145
1183
case ASTScopeKind::SourceFile:
1184
+ case ASTScopeKind::ExtensionGenericParams:
1146
1185
case ASTScopeKind::DefaultArgument:
1147
1186
case ASTScopeKind::AbstractFunctionBody:
1148
1187
case ASTScopeKind::PatternInitializer:
@@ -1217,6 +1256,9 @@ ASTContext &ASTScope::getASTContext() const {
1217
1256
case ASTScopeKind::SourceFile:
1218
1257
return sourceFile.file ->getASTContext ();
1219
1258
1259
+ case ASTScopeKind::ExtensionGenericParams:
1260
+ return extension->getASTContext ();
1261
+
1220
1262
case ASTScopeKind::TypeOrExtensionBody:
1221
1263
return getParent ()->getASTContext ();
1222
1264
@@ -1287,13 +1329,27 @@ SourceRange ASTScope::getSourceRangeImpl() const {
1287
1329
1288
1330
return SourceRange ();
1289
1331
1332
+ case ASTScopeKind::ExtensionGenericParams: {
1333
+ // The generic parameters of an extension are available from the trailing
1334
+ // 'where' (if present) or from the start of the body.
1335
+ SourceLoc startLoc;
1336
+ if (auto trailingWhere = extension->getTrailingWhereClause ())
1337
+ startLoc = trailingWhere->getWhereLoc ();
1338
+ else
1339
+ startLoc = extension->getBraces ().Start ;
1340
+
1341
+ return SourceRange (startLoc, extension->getEndLoc ());
1342
+ }
1343
+
1290
1344
case ASTScopeKind::TypeOrExtensionBody:
1291
1345
if (auto ext = dyn_cast<ExtensionDecl>(iterableDeclContext))
1292
1346
return ext->getBraces ();
1293
1347
1294
1348
return cast<NominalTypeDecl>(iterableDeclContext)->getBraces ();
1295
1349
1296
1350
case ASTScopeKind::GenericParams:
1351
+ // Explicitly-written generic parameters are in scope following their
1352
+ // definition.
1297
1353
return SourceRange (genericParams.params ->getParams ()[genericParams.index ]
1298
1354
->getEndLoc (),
1299
1355
genericParams.decl ->getEndLoc ());
@@ -1595,6 +1651,7 @@ DeclContext *ASTScope::getDeclContext() const {
1595
1651
case ASTScopeKind::TopLevelCode:
1596
1652
return topLevelCode;
1597
1653
1654
+ case ASTScopeKind::ExtensionGenericParams:
1598
1655
case ASTScopeKind::GenericParams:
1599
1656
case ASTScopeKind::AbstractFunctionParams:
1600
1657
case ASTScopeKind::PatternBinding:
@@ -1658,6 +1715,21 @@ SmallVector<ValueDecl *, 4> ASTScope::getLocalBindings() const {
1658
1715
// No local declarations.
1659
1716
break ;
1660
1717
1718
+ case ASTScopeKind::ExtensionGenericParams:
1719
+ // Bind this extension, if we haven't done so already.
1720
+ if (!extension->getExtendedType ())
1721
+ if (auto resolver = extension->getASTContext ().getLazyResolver ())
1722
+ resolver->bindExtension (extension);
1723
+
1724
+ // If there are generic parameters, add them.
1725
+ for (auto genericParams = extension->getGenericParams ();
1726
+ genericParams;
1727
+ genericParams = genericParams->getOuterParameters ()) {
1728
+ for (auto param : genericParams->getParams ())
1729
+ result.push_back (param);
1730
+ }
1731
+ break ;
1732
+
1661
1733
case ASTScopeKind::GenericParams:
1662
1734
result.push_back (genericParams.params ->getParams ()[genericParams.index ]);
1663
1735
break ;
@@ -1765,6 +1837,18 @@ void ASTScope::print(llvm::raw_ostream &out, unsigned level,
1765
1837
printRange ();
1766
1838
break ;
1767
1839
1840
+ case ASTScopeKind::ExtensionGenericParams:
1841
+ printScopeKind (" ExtensionGenericParams" );
1842
+ printAddress (extension);
1843
+ out << " extension of '" ;
1844
+ if (auto typeRepr = extension->getExtendedTypeLoc ().getTypeRepr ())
1845
+ typeRepr->print (out);
1846
+ else
1847
+ extension->getExtendedType ()->print (out);
1848
+ out << " '" ;
1849
+ printRange ();
1850
+ break ;
1851
+
1768
1852
case ASTScopeKind::TypeOrExtensionBody: {
1769
1853
printScopeKind (" TypeOrExtensionBody" );
1770
1854
if (auto ext = dyn_cast<ExtensionDecl>(iterableDeclContext)) {
0 commit comments