Skip to content

Commit 8e39f3e

Browse files
committed
[Scope map] Model the scopes of default arguments.
1 parent ed173d2 commit 8e39f3e

File tree

3 files changed

+85
-9
lines changed

3 files changed

+85
-9
lines changed

include/swift/AST/ASTScope.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class GuardStmt;
4343
class IfStmt;
4444
class IterableDeclContext;
4545
class LabeledConditionalStmt;
46+
class ParamDecl;
4647
class PatternBindingDecl;
4748
class RepeatWhileStmt;
4849
class SourceFile;
@@ -68,6 +69,8 @@ enum class ASTScopeKind : uint8_t {
6869
AbstractFunctionDecl,
6970
/// The parameters of a function/initializer/deinitializer.
7071
AbstractFunctionParams,
72+
/// The default argument for a parameter.
73+
DefaultArgument,
7174
/// A specific pattern binding.
7275
PatternBinding,
7376
/// The scope introduced for an initializer of a pattern binding.
@@ -173,6 +176,10 @@ class ASTScope {
173176
unsigned paramIndex;
174177
} abstractFunctionParams;
175178

179+
/// The parameter whose default argument is being described, i.e.,
180+
/// \c kind == ASTScopeKind::DefaultArgument.
181+
ParamDecl *parameter;
182+
176183
/// For \c kind == ASTScopeKind::PatternBinding,
177184
/// \c kind == ASTScopeKind::AfterPatternBinding, or
178185
/// \c kind == ASTScopeKind::PatternInitializer.
@@ -293,6 +300,11 @@ class ASTScope {
293300
this->abstractFunctionParams.paramIndex = paramIndex;
294301
}
295302

303+
ASTScope(const ASTScope *parent, ParamDecl *param)
304+
: ASTScope(ASTScopeKind::DefaultArgument, parent) {
305+
this->parameter = param;
306+
}
307+
296308
ASTScope(ASTScopeKind kind, const ASTScope *parent, PatternBindingDecl *decl,
297309
unsigned entry)
298310
: ASTScope(kind, parent) {

lib/AST/ASTScope.cpp

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ void ASTScope::expand() const {
210210
addChild(child);
211211
break;
212212

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+
213219
case ASTScopeKind::PatternBinding: {
214220
const auto &patternEntry =
215221
patternBinding.decl->getPatternList()[patternBinding.entry];
@@ -565,6 +571,7 @@ static bool parentDirectDescendedFromLocalDeclaration(const ASTScope *parent,
565571
const Decl *decl) {
566572
while (true) {
567573
switch (parent->getKind()) {
574+
case ASTScopeKind::Preexpanded:
568575
case ASTScopeKind::AbstractFunctionDecl:
569576
case ASTScopeKind::AbstractFunctionParams:
570577
case ASTScopeKind::GenericParams:
@@ -577,8 +584,8 @@ static bool parentDirectDescendedFromLocalDeclaration(const ASTScope *parent,
577584
case ASTScopeKind::LocalDeclaration:
578585
return (parent->getLocalDeclaration() == decl);
579586

580-
case ASTScopeKind::Preexpanded:
581587
case ASTScopeKind::SourceFile:
588+
case ASTScopeKind::DefaultArgument:
582589
case ASTScopeKind::PatternBinding:
583590
case ASTScopeKind::PatternInitializer:
584591
case ASTScopeKind::AfterPatternBinding:
@@ -610,6 +617,7 @@ static bool parentDirectDescendedFromAbstractStorageDecl(
610617
const AbstractStorageDecl *decl) {
611618
while (true) {
612619
switch (parent->getKind()) {
620+
case ASTScopeKind::Preexpanded:
613621
case ASTScopeKind::AbstractFunctionDecl:
614622
case ASTScopeKind::AbstractFunctionParams:
615623
case ASTScopeKind::GenericParams:
@@ -620,9 +628,9 @@ static bool parentDirectDescendedFromAbstractStorageDecl(
620628
case ASTScopeKind::Accessors:
621629
return (parent->getAbstractStorageDecl() == decl);
622630

623-
case ASTScopeKind::Preexpanded:
624631
case ASTScopeKind::SourceFile:
625632
case ASTScopeKind::TypeOrExtensionBody:
633+
case ASTScopeKind::DefaultArgument:
626634
case ASTScopeKind::LocalDeclaration:
627635
case ASTScopeKind::PatternBinding:
628636
case ASTScopeKind::PatternInitializer:
@@ -655,7 +663,9 @@ static bool parentDirectDescendedFromAbstractFunctionDecl(
655663
const AbstractFunctionDecl *decl) {
656664
while (true) {
657665
switch (parent->getKind()) {
666+
case ASTScopeKind::Preexpanded:
658667
case ASTScopeKind::AbstractFunctionParams:
668+
case ASTScopeKind::DefaultArgument:
659669
case ASTScopeKind::GenericParams:
660670
// Keep looking.
661671
parent = parent->getParent();
@@ -664,7 +674,6 @@ static bool parentDirectDescendedFromAbstractFunctionDecl(
664674
case ASTScopeKind::AbstractFunctionDecl:
665675
return (parent->getAbstractFunctionDecl() == decl);
666676

667-
case ASTScopeKind::Preexpanded:
668677
case ASTScopeKind::SourceFile:
669678
case ASTScopeKind::TypeOrExtensionBody:
670679
case ASTScopeKind::LocalDeclaration:
@@ -836,16 +845,37 @@ ASTScope *ASTScope::createIfNeeded(const ASTScope *parent, Decl *decl) {
836845
findNextParameter(parent->abstractFunctionParams.decl,
837846
parent->abstractFunctionParams.listIndex,
838847
parent->abstractFunctionParams.paramIndex);
848+
839849
} else if (abstractFunction->getParameterList(0)->size() > 0) {
840850
nextParameter = std::make_pair(0, 0);
841851
} else {
842852
nextParameter = findNextParameter(abstractFunction, 0, 0);
843853
}
844854

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+
}
849879

850880

851881
// Function body, if present.
@@ -1048,6 +1078,7 @@ bool ASTScope::isContinuationScope() const {
10481078
case ASTScopeKind::GenericParams:
10491079
case ASTScopeKind::AbstractFunctionDecl:
10501080
case ASTScopeKind::AbstractFunctionParams:
1081+
case ASTScopeKind::DefaultArgument:
10511082
case ASTScopeKind::PatternBinding:
10521083
case ASTScopeKind::PatternInitializer:
10531084
case ASTScopeKind::Accessors:
@@ -1096,6 +1127,7 @@ void ASTScope::enumerateContinuationScopes(
10961127
switch (continuation->getKind()) {
10971128
case ASTScopeKind::Preexpanded:
10981129
case ASTScopeKind::SourceFile:
1130+
case ASTScopeKind::DefaultArgument:
10991131
case ASTScopeKind::PatternInitializer:
11001132
case ASTScopeKind::IfStmt:
11011133
case ASTScopeKind::RepeatWhileStmt:
@@ -1180,6 +1212,9 @@ ASTContext &ASTScope::getASTContext() const {
11801212
case ASTScopeKind::AbstractFunctionParams:
11811213
return abstractFunctionParams.decl->getASTContext();
11821214

1215+
case ASTScopeKind::DefaultArgument:
1216+
return parameter->getASTContext();
1217+
11831218
case ASTScopeKind::PatternBinding:
11841219
case ASTScopeKind::PatternInitializer:
11851220
case ASTScopeKind::AfterPatternBinding:
@@ -1287,6 +1322,9 @@ SourceRange ASTScope::getSourceRangeImpl() const {
12871322
return SourceRange(param->getEndLoc(), endLoc);
12881323
}
12891324

1325+
case ASTScopeKind::DefaultArgument:
1326+
return parameter->getDefaultValue()->getSourceRange();
1327+
12901328
case ASTScopeKind::PatternBinding: {
12911329
const auto &patternEntry =
12921330
patternBinding.decl->getPatternList()[patternBinding.entry];
@@ -1599,6 +1637,12 @@ void ASTScope::print(llvm::raw_ostream &out, unsigned level,
15991637
printRange();
16001638
break;
16011639

1640+
case ASTScopeKind::DefaultArgument:
1641+
printScopeKind("DefaultArgument");
1642+
printAddress(parameter);
1643+
printRange();
1644+
break;
1645+
16021646
case ASTScopeKind::PatternBinding:
16031647
printScopeKind("PatternBinding");
16041648
printAddress(patternBinding.decl);

test/NameBinding/scope_map.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,17 @@ func closures() {
163163

164164
{ closures() }()
165165

166+
func defaultArguments(i: Int = 1,
167+
j: Int = { $0 + $1 }(1, 2)) {
168+
169+
func localWithDefaults(i: Int = 1,
170+
j: Int = { $0 + $1 }(1, 2)) {
171+
}
172+
173+
let a = i + j
174+
{ $0 }(a)
175+
}
176+
166177
// RUN: not %target-swift-frontend -dump-scope-maps expanded %s 2> %t.expanded
167178
// RUN: %FileCheck -check-prefix CHECK-EXPANDED %s < %t.expanded
168179

@@ -180,7 +191,8 @@ func closures() {
180191
// CHECK-EXPANDED-NEXT: -GenericParams {{.*}} param 1 [22:22 - 23:1] expanded
181192
// CHECK-EXPANDED-NEXT: -AbstractFunctionParams {{.*}} genericFunc0(t:u:i:) param 0:0 [22:28 - 23:1] expanded
182193
// CHECK-EXPANDED-NEXT: -AbstractFunctionParams {{.*}} genericFunc0(t:u:i:) param 0:1 [22:34 - 23:1] expanded
183-
// CHECK-EXPANDED-NEXT: -AbstractFunctionParams {{.*}} genericFunc0(t:u:i:) param 0:2 [22:46 - 23:1] expanded
194+
// CHECK-EXPANDED: |-DefaultArgument {{.*}} [22:46 - 22:46] expanded
195+
// CHECK-EXPANDED: `-AbstractFunctionParams {{.*}} genericFunc0(t:u:i:) param 0:2 [22:46 - 23:1] expanded
184196
// CHECK-EXPANDED-NEXT: -BraceStmt {{.*}} [22:50 - 23:1] expanded
185197
// CHECK-EXPANDED-NEXT: -TypeOrExtensionBody {{.*}} 'ContainsGenerics0' [25:25 - 31:1] expanded
186198
// CHECK-EXPANDED-NEXT: -AbstractFunctionDecl {{.*}} init(t:u:) [26:3 - 27:3] expanded
@@ -340,11 +352,19 @@ func closures() {
340352
// CHECK-EXPANDED-NEXT: {{^}} `-Closure {{.*}} [161:10 - 161:19] expanded
341353
// CHECK-EXPANDED-NEXT: {{^}} `-BraceStmt {{.*}} [161:10 - 161:19] expanded
342354

343-
// CHECK-EXPANDED: `-TopLevelCode {{.*}} [164:1 - 164:16] expanded
355+
// CHECK-EXPANDED: |-TopLevelCode {{.*}} [164:1 - 164:16] expanded
344356
// CHECK-EXPANDED-NEXT: {{^}} `-BraceStmt {{.*}} [164:1 - 164:16] expanded
345357
// CHECK-EXPANDED-NEXT: {{^}} `-Closure {{.*}} [164:1 - 164:14] expanded
346358
// CHECK-EXPANDED-NEXT: {{^}} `-BraceStmt {{.*}} [164:1 - 164:14] expanded
347359

360+
// CHECK-EXPANDED: -AbstractFunctionDecl {{.*}} defaultArguments(i:j:) [166:1 - 175:1] expanded
361+
// CHECK-EXPANDED: {{^}} |-DefaultArgument {{.*}} [166:32 - 166:32] expanded
362+
// CHECK-EXPANDED-NEXT: {{^}} `-AbstractFunctionParams {{.*}} defaultArguments(i:j:) param 0:0 [166:32 - 175:1] expanded
363+
// CHECK-EXPANDED: {{^}} |-DefaultArgument {{.*}} [167:32 - 167:48] expanded
364+
// CHECK-EXPANDED-NEXT: {{^}} `-Closure {{.*}} [167:32 - 167:42] expanded
365+
// CHECK-EXPANDED-NEXT: {{^}} `-BraceStmt {{.*}} [167:32 - 167:42] expanded
366+
// CHECK-EXPANDED-NEXT: {{^}} `-AbstractFunctionParams {{.*}} defaultArguments(i:j:) param 0:1 [167:48 - 175:1] expanded
367+
348368
// RUN: not %target-swift-frontend -dump-scope-maps 70:8,26:20 %s 2> %t.searches
349369
// RUN: %FileCheck -check-prefix CHECK-SEARCHES %s < %t.searches
350370

0 commit comments

Comments
 (0)