@@ -465,6 +465,12 @@ void ASTScope::expand() const {
465
465
if (auto bodyChild = createIfNeeded (this , closure->getBody ()))
466
466
addChild (bodyChild);
467
467
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 ;
468
474
}
469
475
470
476
// Enumerate any continuation scopes associated with this parent.
@@ -566,6 +572,7 @@ static bool parentDirectDescendedFromLocalDeclaration(const ASTScope *parent,
566
572
case ASTScopeKind::ForStmt:
567
573
case ASTScopeKind::ForStmtInitializer:
568
574
case ASTScopeKind::Closure:
575
+ case ASTScopeKind::TopLevelCode:
569
576
// Not a direct descendant.
570
577
return false ;
571
578
}
@@ -608,6 +615,7 @@ static bool parentDirectDescendedFromAbstractStorageDecl(
608
615
case ASTScopeKind::ForStmt:
609
616
case ASTScopeKind::ForStmtInitializer:
610
617
case ASTScopeKind::Closure:
618
+ case ASTScopeKind::TopLevelCode:
611
619
// Not a direct descendant.
612
620
return false ;
613
621
}
@@ -650,6 +658,7 @@ static bool parentDirectDescendedFromAbstractFunctionDecl(
650
658
case ASTScopeKind::ForStmt:
651
659
case ASTScopeKind::ForStmtInitializer:
652
660
case ASTScopeKind::Closure:
661
+ case ASTScopeKind::TopLevelCode:
653
662
// Not a direct descendant.
654
663
return false ;
655
664
}
@@ -735,14 +744,15 @@ ASTScope *ASTScope::createIfNeeded(const ASTScope *parent, Decl *decl) {
735
744
case DeclKind::TopLevelCode: {
736
745
// Drop top-level statements containing just an IfConfigStmt.
737
746
// 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 ();
739
749
auto elements = braceStmt->getElements ();
740
750
if (elements.size () == 1 &&
741
751
elements[0 ].is <Stmt *>() &&
742
752
isa<IfConfigStmt>(elements[0 ].get <Stmt *>()))
743
753
return nullptr ;
744
754
745
- return createIfNeeded ( parent, braceStmt );
755
+ return new (ctx) ASTScope ( parent, topLevelCode );
746
756
}
747
757
748
758
case DeclKind::Class:
@@ -1004,6 +1014,7 @@ bool ASTScope::isContinuationScope() const {
1004
1014
case ASTScopeKind::ForStmt:
1005
1015
case ASTScopeKind::ForStmtInitializer:
1006
1016
case ASTScopeKind::Closure:
1017
+ case ASTScopeKind::TopLevelCode:
1007
1018
// These node kinds never have a viable continuation.
1008
1019
return false ;
1009
1020
@@ -1046,6 +1057,7 @@ void ASTScope::enumerateContinuationScopes(
1046
1057
case ASTScopeKind::ForStmt:
1047
1058
case ASTScopeKind::ForStmtInitializer:
1048
1059
case ASTScopeKind::Closure:
1060
+ case ASTScopeKind::TopLevelCode:
1049
1061
// These scopes are hard barriers; if we hit one, there is nothing to
1050
1062
// continue to.
1051
1063
return ;
@@ -1141,6 +1153,9 @@ ASTContext &ASTScope::getASTContext() const {
1141
1153
1142
1154
case ASTScopeKind::Accessors:
1143
1155
return abstractStorageDecl->getASTContext ();
1156
+
1157
+ case ASTScopeKind::TopLevelCode:
1158
+ return static_cast <Decl *>(topLevelCode)->getASTContext ();
1144
1159
}
1145
1160
}
1146
1161
@@ -1369,6 +1384,9 @@ SourceRange ASTScope::getSourceRangeImpl() const {
1369
1384
return SourceRange (closure->getInLoc (), closure->getEndLoc ());
1370
1385
1371
1386
return closure->getSourceRange ();
1387
+
1388
+ case ASTScopeKind::TopLevelCode:
1389
+ return topLevelCode->getSourceRange ();
1372
1390
}
1373
1391
}
1374
1392
@@ -1622,6 +1640,12 @@ void ASTScope::print(llvm::raw_ostream &out, unsigned level,
1622
1640
printAddress (closure);
1623
1641
printRange ();
1624
1642
break ;
1643
+
1644
+ case ASTScopeKind::TopLevelCode:
1645
+ printScopeKind (" TopLevelCode" );
1646
+ printAddress (topLevelCode);
1647
+ printRange ();
1648
+ break ;
1625
1649
}
1626
1650
1627
1651
// Was this scope expanded?
0 commit comments