@@ -210,6 +210,12 @@ void ASTScope::expand() const {
210
210
addChild (child);
211
211
break ;
212
212
213
+ case ASTScopeKind::DefaultArgument:
214
+ // Create a child for the default argument expression.
215
+ if (auto child = createIfNeeded (this , parameter->getDefaultValue ()))
216
+ addChild (child);
217
+ break ;
218
+
213
219
case ASTScopeKind::PatternBinding: {
214
220
const auto &patternEntry =
215
221
patternBinding.decl ->getPatternList ()[patternBinding.entry ];
@@ -565,6 +571,7 @@ static bool parentDirectDescendedFromLocalDeclaration(const ASTScope *parent,
565
571
const Decl *decl) {
566
572
while (true ) {
567
573
switch (parent->getKind ()) {
574
+ case ASTScopeKind::Preexpanded:
568
575
case ASTScopeKind::AbstractFunctionDecl:
569
576
case ASTScopeKind::AbstractFunctionParams:
570
577
case ASTScopeKind::GenericParams:
@@ -577,8 +584,8 @@ static bool parentDirectDescendedFromLocalDeclaration(const ASTScope *parent,
577
584
case ASTScopeKind::LocalDeclaration:
578
585
return (parent->getLocalDeclaration () == decl);
579
586
580
- case ASTScopeKind::Preexpanded:
581
587
case ASTScopeKind::SourceFile:
588
+ case ASTScopeKind::DefaultArgument:
582
589
case ASTScopeKind::PatternBinding:
583
590
case ASTScopeKind::PatternInitializer:
584
591
case ASTScopeKind::AfterPatternBinding:
@@ -610,6 +617,7 @@ static bool parentDirectDescendedFromAbstractStorageDecl(
610
617
const AbstractStorageDecl *decl) {
611
618
while (true ) {
612
619
switch (parent->getKind ()) {
620
+ case ASTScopeKind::Preexpanded:
613
621
case ASTScopeKind::AbstractFunctionDecl:
614
622
case ASTScopeKind::AbstractFunctionParams:
615
623
case ASTScopeKind::GenericParams:
@@ -620,9 +628,9 @@ static bool parentDirectDescendedFromAbstractStorageDecl(
620
628
case ASTScopeKind::Accessors:
621
629
return (parent->getAbstractStorageDecl () == decl);
622
630
623
- case ASTScopeKind::Preexpanded:
624
631
case ASTScopeKind::SourceFile:
625
632
case ASTScopeKind::TypeOrExtensionBody:
633
+ case ASTScopeKind::DefaultArgument:
626
634
case ASTScopeKind::LocalDeclaration:
627
635
case ASTScopeKind::PatternBinding:
628
636
case ASTScopeKind::PatternInitializer:
@@ -655,7 +663,9 @@ static bool parentDirectDescendedFromAbstractFunctionDecl(
655
663
const AbstractFunctionDecl *decl) {
656
664
while (true ) {
657
665
switch (parent->getKind ()) {
666
+ case ASTScopeKind::Preexpanded:
658
667
case ASTScopeKind::AbstractFunctionParams:
668
+ case ASTScopeKind::DefaultArgument:
659
669
case ASTScopeKind::GenericParams:
660
670
// Keep looking.
661
671
parent = parent->getParent ();
@@ -664,7 +674,6 @@ static bool parentDirectDescendedFromAbstractFunctionDecl(
664
674
case ASTScopeKind::AbstractFunctionDecl:
665
675
return (parent->getAbstractFunctionDecl () == decl);
666
676
667
- case ASTScopeKind::Preexpanded:
668
677
case ASTScopeKind::SourceFile:
669
678
case ASTScopeKind::TypeOrExtensionBody:
670
679
case ASTScopeKind::LocalDeclaration:
@@ -836,16 +845,37 @@ ASTScope *ASTScope::createIfNeeded(const ASTScope *parent, Decl *decl) {
836
845
findNextParameter (parent->abstractFunctionParams .decl ,
837
846
parent->abstractFunctionParams .listIndex ,
838
847
parent->abstractFunctionParams .paramIndex );
848
+
839
849
} else if (abstractFunction->getParameterList (0 )->size () > 0 ) {
840
850
nextParameter = std::make_pair (0 , 0 );
841
851
} else {
842
852
nextParameter = findNextParameter (abstractFunction, 0 , 0 );
843
853
}
844
854
845
- // If there is another parameter to visit, do so now.
846
- if (nextParameter)
847
- return new (ctx) ASTScope (parent, abstractFunction, nextParameter->first ,
848
- nextParameter->second );
855
+ if (nextParameter) {
856
+ // Dig out the actual parameter.
857
+ ParamDecl *currentParam =
858
+ abstractFunction->getParameterList (nextParameter->first )
859
+ ->get (nextParameter->second );
860
+
861
+ // Determine whether there is a default argument.
862
+ ASTScope *defaultArgumentScope = nullptr ;
863
+ if (currentParam->getDefaultValue ())
864
+ defaultArgumentScope = new (ctx) ASTScope (parent, currentParam);
865
+
866
+ // If there is another parameter to visit, do so now.
867
+ ASTScope *afterParamScope = new (ctx) ASTScope (parent, abstractFunction,
868
+ nextParameter->first ,
869
+ nextParameter->second );
870
+
871
+ // If we have a default argument, use a pre-expanded node.
872
+ if (defaultArgumentScope) {
873
+ ASTScope *children[2 ] = { defaultArgumentScope, afterParamScope };
874
+ return new (ctx) ASTScope (parent, children);
875
+ }
876
+
877
+ return afterParamScope;
878
+ }
849
879
850
880
851
881
// Function body, if present.
@@ -1048,6 +1078,7 @@ bool ASTScope::isContinuationScope() const {
1048
1078
case ASTScopeKind::GenericParams:
1049
1079
case ASTScopeKind::AbstractFunctionDecl:
1050
1080
case ASTScopeKind::AbstractFunctionParams:
1081
+ case ASTScopeKind::DefaultArgument:
1051
1082
case ASTScopeKind::PatternBinding:
1052
1083
case ASTScopeKind::PatternInitializer:
1053
1084
case ASTScopeKind::Accessors:
@@ -1096,6 +1127,7 @@ void ASTScope::enumerateContinuationScopes(
1096
1127
switch (continuation->getKind ()) {
1097
1128
case ASTScopeKind::Preexpanded:
1098
1129
case ASTScopeKind::SourceFile:
1130
+ case ASTScopeKind::DefaultArgument:
1099
1131
case ASTScopeKind::PatternInitializer:
1100
1132
case ASTScopeKind::IfStmt:
1101
1133
case ASTScopeKind::RepeatWhileStmt:
@@ -1180,6 +1212,9 @@ ASTContext &ASTScope::getASTContext() const {
1180
1212
case ASTScopeKind::AbstractFunctionParams:
1181
1213
return abstractFunctionParams.decl ->getASTContext ();
1182
1214
1215
+ case ASTScopeKind::DefaultArgument:
1216
+ return parameter->getASTContext ();
1217
+
1183
1218
case ASTScopeKind::PatternBinding:
1184
1219
case ASTScopeKind::PatternInitializer:
1185
1220
case ASTScopeKind::AfterPatternBinding:
@@ -1287,6 +1322,9 @@ SourceRange ASTScope::getSourceRangeImpl() const {
1287
1322
return SourceRange (param->getEndLoc (), endLoc);
1288
1323
}
1289
1324
1325
+ case ASTScopeKind::DefaultArgument:
1326
+ return parameter->getDefaultValue ()->getSourceRange ();
1327
+
1290
1328
case ASTScopeKind::PatternBinding: {
1291
1329
const auto &patternEntry =
1292
1330
patternBinding.decl ->getPatternList ()[patternBinding.entry ];
@@ -1599,6 +1637,12 @@ void ASTScope::print(llvm::raw_ostream &out, unsigned level,
1599
1637
printRange ();
1600
1638
break ;
1601
1639
1640
+ case ASTScopeKind::DefaultArgument:
1641
+ printScopeKind (" DefaultArgument" );
1642
+ printAddress (parameter);
1643
+ printRange ();
1644
+ break ;
1645
+
1602
1646
case ASTScopeKind::PatternBinding:
1603
1647
printScopeKind (" PatternBinding" );
1604
1648
printAddress (patternBinding.decl );
0 commit comments