Skip to content

Commit 84b0433

Browse files
committed
[Scope map] Add a scope node for top-level code declarations.
1 parent 35888ee commit 84b0433

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

include/swift/AST/ASTScope.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class SourceFile;
4949
class Stmt;
5050
class StmtConditionElement;
5151
class SwitchStmt;
52+
class TopLevelCodeDecl;
5253
class WhileStmt;
5354

5455
/// Describes kind of scope that occurs within the AST.
@@ -104,6 +105,8 @@ enum class ASTScopeKind : uint8_t {
104105
Accessors,
105106
/// Scope for a closure.
106107
Closure,
108+
/// Scope for top-level code.
109+
TopLevelCode,
107110
};
108111

109112
class ASTScope {
@@ -235,6 +238,10 @@ class ASTScope {
235238

236239
/// The closure, for \c kind == ASTScopeKind::Closure.
237240
ClosureExpr *closure;
241+
242+
/// The top-level code declaration for
243+
/// \c kind == ASTScopeKind::TopLevelCodeDecl.
244+
TopLevelCodeDecl *topLevelCode;
238245
};
239246

240247
/// Child scopes, sorted by source range.
@@ -359,6 +366,11 @@ class ASTScope {
359366
this->closure = closure;
360367
}
361368

369+
ASTScope(const ASTScope *parent, TopLevelCodeDecl *topLevelCode)
370+
: ASTScope(ASTScopeKind::TopLevelCode, parent) {
371+
this->topLevelCode = topLevelCode;
372+
}
373+
362374
~ASTScope();
363375

364376
ASTScope(ASTScope &&) = delete;

lib/AST/ASTScope.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,12 @@ void ASTScope::expand() const {
465465
if (auto bodyChild = createIfNeeded(this, closure->getBody()))
466466
addChild(bodyChild);
467467
break;
468+
469+
case ASTScopeKind::TopLevelCode:
470+
/// Add a child for the body.
471+
if (auto bodyChild = createIfNeeded(this, topLevelCode->getBody()))
472+
addChild(bodyChild);
473+
break;
468474
}
469475

470476
// Enumerate any continuation scopes associated with this parent.
@@ -566,6 +572,7 @@ static bool parentDirectDescendedFromLocalDeclaration(const ASTScope *parent,
566572
case ASTScopeKind::ForStmt:
567573
case ASTScopeKind::ForStmtInitializer:
568574
case ASTScopeKind::Closure:
575+
case ASTScopeKind::TopLevelCode:
569576
// Not a direct descendant.
570577
return false;
571578
}
@@ -608,6 +615,7 @@ static bool parentDirectDescendedFromAbstractStorageDecl(
608615
case ASTScopeKind::ForStmt:
609616
case ASTScopeKind::ForStmtInitializer:
610617
case ASTScopeKind::Closure:
618+
case ASTScopeKind::TopLevelCode:
611619
// Not a direct descendant.
612620
return false;
613621
}
@@ -650,6 +658,7 @@ static bool parentDirectDescendedFromAbstractFunctionDecl(
650658
case ASTScopeKind::ForStmt:
651659
case ASTScopeKind::ForStmtInitializer:
652660
case ASTScopeKind::Closure:
661+
case ASTScopeKind::TopLevelCode:
653662
// Not a direct descendant.
654663
return false;
655664
}
@@ -735,14 +744,15 @@ ASTScope *ASTScope::createIfNeeded(const ASTScope *parent, Decl *decl) {
735744
case DeclKind::TopLevelCode: {
736745
// Drop top-level statements containing just an IfConfigStmt.
737746
// FIXME: The modeling of IfConfig is weird.
738-
auto braceStmt = cast<TopLevelCodeDecl>(decl)->getBody();
747+
auto topLevelCode = cast<TopLevelCodeDecl>(decl);
748+
auto braceStmt = topLevelCode->getBody();
739749
auto elements = braceStmt->getElements();
740750
if (elements.size() == 1 &&
741751
elements[0].is<Stmt *>() &&
742752
isa<IfConfigStmt>(elements[0].get<Stmt *>()))
743753
return nullptr;
744754

745-
return createIfNeeded(parent, braceStmt);
755+
return new (ctx) ASTScope(parent, topLevelCode);
746756
}
747757

748758
case DeclKind::Class:
@@ -1004,6 +1014,7 @@ bool ASTScope::isContinuationScope() const {
10041014
case ASTScopeKind::ForStmt:
10051015
case ASTScopeKind::ForStmtInitializer:
10061016
case ASTScopeKind::Closure:
1017+
case ASTScopeKind::TopLevelCode:
10071018
// These node kinds never have a viable continuation.
10081019
return false;
10091020

@@ -1046,6 +1057,7 @@ void ASTScope::enumerateContinuationScopes(
10461057
case ASTScopeKind::ForStmt:
10471058
case ASTScopeKind::ForStmtInitializer:
10481059
case ASTScopeKind::Closure:
1060+
case ASTScopeKind::TopLevelCode:
10491061
// These scopes are hard barriers; if we hit one, there is nothing to
10501062
// continue to.
10511063
return;
@@ -1141,6 +1153,9 @@ ASTContext &ASTScope::getASTContext() const {
11411153

11421154
case ASTScopeKind::Accessors:
11431155
return abstractStorageDecl->getASTContext();
1156+
1157+
case ASTScopeKind::TopLevelCode:
1158+
return static_cast<Decl *>(topLevelCode)->getASTContext();
11441159
}
11451160
}
11461161

@@ -1369,6 +1384,9 @@ SourceRange ASTScope::getSourceRangeImpl() const {
13691384
return SourceRange(closure->getInLoc(), closure->getEndLoc());
13701385

13711386
return closure->getSourceRange();
1387+
1388+
case ASTScopeKind::TopLevelCode:
1389+
return topLevelCode->getSourceRange();
13721390
}
13731391
}
13741392

@@ -1622,6 +1640,12 @@ void ASTScope::print(llvm::raw_ostream &out, unsigned level,
16221640
printAddress(closure);
16231641
printRange();
16241642
break;
1643+
1644+
case ASTScopeKind::TopLevelCode:
1645+
printScopeKind("TopLevelCode");
1646+
printAddress(topLevelCode);
1647+
printRange();
1648+
break;
16251649
}
16261650

16271651
// Was this scope expanded?

test/NameBinding/scope_map.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,10 @@ func closures() {
324324
// CHECK-EXPANDED-NEXT: {{^}} `-Closure {{.*}} [161:10 - 161:19] expanded
325325
// CHECK-EXPANDED-NEXT: {{^}} `-BraceStmt {{.*}} [161:10 - 161:19] expanded
326326

327-
// CHECK-EXPANDED: {{^}}`-BraceStmt {{.*}} [164:1 - 164:16] expanded
328-
// CHECK-EXPANDED-NEXT: {{^}} `-Closure {{.*}} [164:1 - 164:14] expanded
329-
// CHECK-EXPANDED-NEXT: {{^}} `-BraceStmt {{.*}} [164:1 - 164:14] expanded
327+
// CHECK-EXPANDED: `-TopLevelCode {{.*}} [164:1 - 164:16] expanded
328+
// CHECK-EXPANDED-NEXT: {{^}} `-BraceStmt {{.*}} [164:1 - 164:16] expanded
329+
// CHECK-EXPANDED-NEXT: {{^}} `-Closure {{.*}} [164:1 - 164:14] expanded
330+
// CHECK-EXPANDED-NEXT: {{^}} `-BraceStmt {{.*}} [164:1 - 164:14] expanded
330331

331332
// RUN: not %target-swift-frontend -dump-scope-maps 70:8,26:20 %s 2> %t.searches
332333
// RUN: %FileCheck -check-prefix CHECK-SEARCHES %s < %t.searches

0 commit comments

Comments
 (0)