Skip to content

Commit 29e2edb

Browse files
authored
Merge pull request swiftlang#24361 from slavapestov/top-level-code-cleanup
Sema: Clean up top level code handling a little
2 parents a86609a + c81e525 commit 29e2edb

File tree

5 files changed

+9
-29
lines changed

5 files changed

+9
-29
lines changed

lib/Sema/PCMacro.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -651,10 +651,10 @@ void swift::performPCMacro(SourceFile &SF, TopLevelContext &TLC) {
651651
ExpressionFinder(TopLevelContext &TLC) : TLC(TLC) {}
652652

653653
bool walkToDeclPre(Decl *D) override {
654+
ASTContext &ctx = D->getASTContext();
654655
if (auto *FD = dyn_cast<AbstractFunctionDecl>(D)) {
655656
if (!FD->isImplicit()) {
656657
if (FD->getBody()) {
657-
ASTContext &ctx = FD->getASTContext();
658658
Instrumenter I(ctx, FD, TmpNameIndex);
659659
I.transformDecl(FD);
660660
return false;
@@ -663,15 +663,13 @@ void swift::performPCMacro(SourceFile &SF, TopLevelContext &TLC) {
663663
} else if (auto *TLCD = dyn_cast<TopLevelCodeDecl>(D)) {
664664
if (!TLCD->isImplicit()) {
665665
if (BraceStmt *Body = TLCD->getBody()) {
666-
ASTContext &ctx = static_cast<Decl *>(TLCD)->getASTContext();
667666
Instrumenter I(ctx, TLCD, TmpNameIndex);
668667
BraceStmt *NewBody = I.transformBraceStmt(Body, true);
669668
if (NewBody != Body) {
670669
TLCD->setBody(NewBody);
671670
TypeChecker &TC = TypeChecker::createForContext(ctx);
672671
TC.checkTopLevelErrorHandling(TLCD);
673-
TC.contextualizeTopLevelCode(TLC,
674-
SmallVector<Decl *, 1>(1, TLCD));
672+
TC.contextualizeTopLevelCode(TLC, TLCD);
675673
}
676674
return false;
677675
}

lib/Sema/TypeCheckREPL.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,9 @@ void TypeChecker::processREPLTopLevel(SourceFile &SF, TopLevelContext &TLC,
453453
else if (auto *D = Entry.dyn_cast<Decl*>())
454454
if (auto *PBD = dyn_cast<PatternBindingDecl>(D))
455455
RC.processREPLTopLevelPatternBinding(PBD);
456+
457+
contextualizeTopLevelCode(TLC, TLCD);
456458
}
457459

458-
contextualizeTopLevelCode(TLC, llvm::makeArrayRef(SF.Decls).slice(FirstDecl));
459460
SF.clearLookupCache();
460461
}
461-

lib/Sema/TypeCheckStmt.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,6 @@ namespace {
8080
unsigned nextDiscriminator = 0)
8181
: ParentDC(parent), NextDiscriminator(nextDiscriminator) {}
8282

83-
/// Change the context we're contextualizing to. This is
84-
/// basically only reasonable when processing all the different
85-
/// top-level code declarations.
86-
void setContext(TopLevelCodeDecl *parent) {
87-
ParentDC = parent;
88-
}
89-
9083
bool hasAutoClosures() const {
9184
return NextDiscriminator != 0;
9285
}
@@ -238,15 +231,10 @@ bool TypeChecker::contextualizeInitializer(Initializer *DC, Expr *E) {
238231
}
239232

240233
void TypeChecker::contextualizeTopLevelCode(TopLevelContext &TLC,
241-
ArrayRef<Decl*> topLevel) {
234+
TopLevelCodeDecl *TLCD) {
242235
unsigned nextDiscriminator = TLC.NextAutoClosureDiscriminator;
243-
ContextualizeClosures CC(nullptr, nextDiscriminator);
244-
for (auto decl : topLevel) {
245-
auto topLevelCode = dyn_cast<TopLevelCodeDecl>(decl);
246-
if (!topLevelCode) continue;
247-
CC.setContext(topLevelCode);
248-
topLevelCode->getBody()->walk(CC);
249-
}
236+
ContextualizeClosures CC(TLCD, nextDiscriminator);
237+
TLCD->getBody()->walk(CC);
250238
assert(nextDiscriminator == TLC.NextAutoClosureDiscriminator &&
251239
"reentrant/concurrent invocation of contextualizeTopLevelCode?");
252240
TLC.NextAutoClosureDiscriminator = CC.NextDiscriminator;

lib/Sema/TypeChecker.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -508,22 +508,16 @@ void swift::performTypeChecking(SourceFile &SF, TopLevelContext &TLC,
508508
checkBridgedFunctions(TC.Context);
509509

510510
// Type check the top-level elements of the source file.
511-
bool hasTopLevelCode = false;
512511
for (auto D : llvm::makeArrayRef(SF.Decls).slice(StartElem)) {
513512
if (auto *TLCD = dyn_cast<TopLevelCodeDecl>(D)) {
514-
hasTopLevelCode = true;
515513
// Immediately perform global name-binding etc.
516514
TC.typeCheckTopLevelCodeDecl(TLCD);
515+
TC.contextualizeTopLevelCode(TLC, TLCD);
517516
} else {
518517
TC.typeCheckDecl(D);
519518
}
520519
}
521520

522-
if (hasTopLevelCode) {
523-
TC.contextualizeTopLevelCode(TLC,
524-
llvm::makeArrayRef(SF.Decls).slice(StartElem));
525-
}
526-
527521
// If we're in REPL mode, inject temporary result variables and other stuff
528522
// that the REPL needs to synthesize.
529523
if (SF.Kind == SourceFileKind::REPL && !Ctx.hadError())

lib/Sema/TypeChecker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1479,7 +1479,7 @@ class TypeChecker final : public LazyResolver {
14791479
/// \returns true if any closures were found
14801480
static bool contextualizeInitializer(Initializer *DC, Expr *init);
14811481
static void contextualizeTopLevelCode(TopLevelContext &TLC,
1482-
ArrayRef<Decl*> topLevelDecls);
1482+
TopLevelCodeDecl *TLCD);
14831483

14841484
/// Return the type-of-reference of the given value.
14851485
///

0 commit comments

Comments
 (0)