Skip to content

Commit 7e5f8b2

Browse files
authored
Merge pull request #15453 from slavapestov/decl-checker-cleanup-part-3
Remove more usages of DeclChecker::IsFirstPass
2 parents f466e29 + 509d293 commit 7e5f8b2

22 files changed

+228
-282
lines changed

include/swift/AST/ASTContext.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,11 @@ class ASTContext {
533533
/// nested within it.
534534
void addExternalDecl(Decl *decl);
535535

536+
/// Add a declaration that was synthesized to a per-source file list if
537+
/// if is part of a source file, or the external declarations list if
538+
/// it is part of an imported type context.
539+
void addSynthesizedDecl(Decl *decl);
540+
536541
/// Add a cleanup function to be called when the ASTContext is deallocated.
537542
void addCleanup(std::function<void(void)> cleanup);
538543

include/swift/AST/Module.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,14 @@ class SourceFile final : public FileUnit {
854854
/// complete, we diagnose.
855855
llvm::SetVector<const DeclAttribute *> AttrsRequiringFoundation;
856856

857+
/// A set of synthesized declarations that need to be type checked.
858+
llvm::SmallVector<Decl *, 8> SynthesizedDecls;
859+
860+
/// We might perform type checking on the same source file more than once,
861+
/// if its the main file or a REPL instance, so keep track of the last
862+
/// checked synthesized declaration to avoid duplicating work.
863+
unsigned LastCheckedSynthesizedDecl = 0;
864+
857865
/// A mapping from Objective-C selectors to the methods that have
858866
/// those selectors.
859867
llvm::DenseMap<ObjCSelector, llvm::TinyPtrVector<AbstractFunctionDecl *>>

lib/AST/ASTContext.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,16 @@ void ASTContext::addExternalDecl(Decl *decl) {
13401340
ExternalDefinitions.insert(decl);
13411341
}
13421342

1343+
void ASTContext::addSynthesizedDecl(Decl *decl) {
1344+
auto *mod = cast<FileUnit>(decl->getDeclContext()->getModuleScopeContext());
1345+
if (mod->getKind() == FileUnitKind::ClangModule) {
1346+
ExternalDefinitions.insert(decl);
1347+
return;
1348+
}
1349+
1350+
cast<SourceFile>(mod)->SynthesizedDecls.push_back(decl);
1351+
}
1352+
13431353
void ASTContext::addCleanup(std::function<void(void)> cleanup) {
13441354
Impl.Cleanups.push_back(std::move(cleanup));
13451355
}

lib/ClangImporter/ClangImporter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1661,7 +1661,6 @@ ClangImporter::Implementation::Implementation(ASTContext &ctx,
16611661
nameImporter() {}
16621662

16631663
ClangImporter::Implementation::~Implementation() {
1664-
assert(NumCurrentImportingEntities == 0);
16651664
#ifndef NDEBUG
16661665
SwiftContext.SourceMgr.verifyAllBuffers();
16671666
#endif

lib/ClangImporter/ImportDecl.cpp

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ makeEnumRawValueConstructor(ClangImporter::Implementation &Impl,
502502
ctorDecl->setBody(body);
503503
ctorDecl->setBodyTypeCheckedIfPresent();
504504

505-
C.addExternalDecl(ctorDecl);
505+
Impl.registerExternalDecl(ctorDecl);
506506

507507
return ctorDecl;
508508
}
@@ -580,7 +580,7 @@ static AccessorDecl *makeEnumRawValueGetter(ClangImporter::Implementation &Impl,
580580

581581
getterDecl->setBody(body);
582582
getterDecl->setBodyTypeCheckedIfPresent();
583-
C.addExternalDecl(getterDecl);
583+
Impl.registerExternalDecl(getterDecl);
584584
return getterDecl;
585585
}
586586

@@ -663,7 +663,7 @@ static AccessorDecl *makeStructRawValueGetter(
663663
getterDecl->setBody(body);
664664
getterDecl->setBodyTypeCheckedIfPresent();
665665

666-
C.addExternalDecl(getterDecl);
666+
Impl.registerExternalDecl(getterDecl);
667667
return getterDecl;
668668
}
669669

@@ -830,7 +830,7 @@ makeIndirectFieldAccessors(ClangImporter::Implementation &Impl,
830830
/*implicit*/ true);
831831
getterDecl->setBody(body);
832832
getterDecl->getAttrs().add(new (C) TransparentAttr(/*implicit*/ true));
833-
C.addExternalDecl(getterDecl);
833+
Impl.registerExternalDecl(getterDecl);
834834
}
835835

836836
// Synthesize the setter body
@@ -855,7 +855,7 @@ makeIndirectFieldAccessors(ClangImporter::Implementation &Impl,
855855
/*implicit*/ true);
856856
setterDecl->setBody(body);
857857
setterDecl->getAttrs().add(new (C) TransparentAttr(/*implicit*/ true));
858-
C.addExternalDecl(setterDecl);
858+
Impl.registerExternalDecl(setterDecl);
859859
}
860860

861861
return { getterDecl, setterDecl };
@@ -916,7 +916,7 @@ makeUnionFieldAccessors(ClangImporter::Implementation &Impl,
916916
/*implicit*/ true);
917917
getterDecl->setBody(body);
918918
getterDecl->getAttrs().add(new (C) TransparentAttr(/*implicit*/ true));
919-
C.addExternalDecl(getterDecl);
919+
Impl.registerExternalDecl(getterDecl);
920920
}
921921

922922
// Synthesize the setter body
@@ -950,7 +950,7 @@ makeUnionFieldAccessors(ClangImporter::Implementation &Impl,
950950
/*implicit*/ true);
951951
setterDecl->setBody(body);
952952
setterDecl->getAttrs().add(new (C) TransparentAttr(/*implicit*/ true));
953-
C.addExternalDecl(setterDecl);
953+
Impl.registerExternalDecl(setterDecl);
954954
}
955955

956956
return { getterDecl, setterDecl };
@@ -7742,36 +7742,12 @@ ClangImporter::Implementation::importDeclImpl(const clang::NamedDecl *ClangDecl,
77427742
}
77437743

77447744
void ClangImporter::Implementation::startedImportingEntity() {
7745-
++NumCurrentImportingEntities;
77467745
++NumTotalImportedEntities;
77477746
// FIXME: (transitional) increment the redundant "always-on" counter.
77487747
if (SwiftContext.Stats)
77497748
SwiftContext.Stats->getFrontendCounters().NumTotalClangImportedEntities++;
77507749
}
77517750

7752-
void ClangImporter::Implementation::finishedImportingEntity() {
7753-
assert(NumCurrentImportingEntities &&
7754-
"finishedImportingEntity not paired with startedImportingEntity");
7755-
if (NumCurrentImportingEntities == 1) {
7756-
// We decrease NumCurrentImportingEntities only after pending actions
7757-
// are finished, to avoid recursively re-calling finishPendingActions().
7758-
finishPendingActions();
7759-
}
7760-
--NumCurrentImportingEntities;
7761-
}
7762-
7763-
void ClangImporter::Implementation::finishPendingActions() {
7764-
if (RegisteredExternalDecls.empty())
7765-
return;
7766-
7767-
if (!hasFinishedTypeChecking()) {
7768-
for (auto *D : RegisteredExternalDecls)
7769-
SwiftContext.addExternalDecl(D);
7770-
}
7771-
7772-
RegisteredExternalDecls.clear();
7773-
}
7774-
77757751
/// Look up associated type requirements in the conforming type.
77767752
static void finishTypeWitnesses(
77777753
NormalProtocolConformance *conformance) {
@@ -7982,7 +7958,7 @@ Decl *ClangImporter::Implementation::importDeclAndCacheImpl(
79827958
bool TypedefIsSuperfluous = false;
79837959
bool HadForwardDeclaration = false;
79847960

7985-
ImportingEntityRAII ImportingEntity(*this);
7961+
startedImportingEntity();
79867962
Decl *Result = importDeclImpl(ClangDecl, version, TypedefIsSuperfluous,
79877963
HadForwardDeclaration);
79887964
if (!Result)
@@ -8479,7 +8455,7 @@ void ClangImporter::Implementation::loadAllMembersIntoExtension(
84798455
return;
84808456

84818457
// Get ready to actually load the members.
8482-
ImportingEntityRAII Importing(*this);
8458+
startedImportingEntity();
84838459

84848460
// Load the members.
84858461
for (auto entry : table->lookupGlobalsAsMembers(effectiveClangContext)) {
@@ -8579,13 +8555,11 @@ void ClangImporter::Implementation::loadAllMembersOfObjcContainer(
85798555
loadMembersOfBaseImportedFromClang(ext);
85808556
}
85818557

8582-
ImportingEntityRAII Importing(*this);
8558+
startedImportingEntity();
85838559

85848560
SmallVector<Decl *, 16> members;
85858561
collectMembersToAdd(objcContainer, D, DC, members);
85868562

8587-
// Add the members now, before ~ImportingEntityRAII does work that might
8588-
// involve them.
85898563
for (auto member : members) {
85908564
IDC->addMember(member);
85918565
}

lib/ClangImporter/ImportMacro.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,8 @@ ValueDecl *ClangImporter::Implementation::importMacro(Identifier name,
672672
known->second.push_back({macro, nullptr});
673673
}
674674

675-
ImportingEntityRAII ImportingEntity(*this);
675+
startedImportingEntity();
676+
676677
// We haven't tried to import this macro yet. Do so now, and cache the
677678
// result.
678679

lib/ClangImporter/ImporterImpl.h

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -557,11 +557,6 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
557557
/// Records those modules that we have looked up.
558558
llvm::DenseMap<Identifier, ModuleDecl *> checkedModules;
559559

560-
/// External Decls that we have imported but not passed to the ASTContext yet.
561-
SmallVector<Decl *, 4> RegisteredExternalDecls;
562-
563-
unsigned NumCurrentImportingEntities = 0;
564-
565560
/// Mapping from delayed conformance IDs to the set of delayed
566561
/// protocol conformances.
567562
llvm::DenseMap<unsigned, SmallVector<ProtocolConformance *, 4>>
@@ -576,19 +571,6 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
576571
ImportedProtocols;
577572

578573
void startedImportingEntity();
579-
void finishedImportingEntity();
580-
void finishPendingActions();
581-
582-
struct ImportingEntityRAII {
583-
Implementation &Impl;
584-
585-
ImportingEntityRAII(Implementation &Impl) : Impl(Impl) {
586-
Impl.startedImportingEntity();
587-
}
588-
~ImportingEntityRAII() {
589-
Impl.finishedImportingEntity();
590-
}
591-
};
592574

593575
public:
594576
importer::PlatformAvailability platformAvailability;
@@ -624,7 +606,8 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
624606

625607
public:
626608
void registerExternalDecl(Decl *D) {
627-
RegisteredExternalDecls.push_back(D);
609+
if (!hasFinishedTypeChecking())
610+
SwiftContext.addExternalDecl(D);
628611
}
629612

630613
void recordImplicitUnwrapForDecl(Decl *decl, bool isIUO) {

lib/Sema/CodeSynthesis.cpp

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,6 @@ static bool needsDynamicMaterializeForSet(AbstractStorageDecl *storage) {
243243
return storage->isDynamic() || storage->hasClangNode();
244244
}
245245

246-
// True if a generated accessor needs to be registered as an external decl.
247-
bool needsToBeRegisteredAsExternalDecl(AbstractStorageDecl *storage) {
248-
// Either the storage itself was imported from Clang...
249-
if (storage->hasClangNode())
250-
return true;
251-
252-
// ...or it was synthesized into an imported context.
253-
const DeclContext *dc = storage->getDeclContext();
254-
return isa<ClangModuleUnit>(dc->getModuleScopeContext());
255-
}
256-
257246
/// Mark the accessor as transparent if we can.
258247
///
259248
/// If the storage is inside a fixed-layout nominal type, we can mark the
@@ -396,12 +385,9 @@ createMaterializeForSetPrototype(AbstractStorageDecl *storage,
396385
maybeMarkTransparent(materializeForSet, storage, TC);
397386

398387
AvailabilityInference::applyInferredAvailableAttrs(materializeForSet,
399-
asAvailableAs, ctx);
388+
asAvailableAs, ctx);
400389

401-
// If the property came from ObjC, we need to register this as an external
402-
// definition to be compiled.
403-
if (needsToBeRegisteredAsExternalDecl(storage))
404-
TC.Context.addExternalDecl(materializeForSet);
390+
TC.Context.addSynthesizedDecl(materializeForSet);
405391
TC.DeclsToFinalize.insert(materializeForSet);
406392

407393
return materializeForSet;
@@ -734,9 +720,7 @@ static void synthesizeTrivialGetter(AccessorDecl *getter,
734720
SourceLoc loc = storage->getLoc();
735721
getter->setBody(BraceStmt::create(ctx, loc, returnStmt, loc, true));
736722

737-
// Register the accessor as an external decl if the storage was imported.
738-
if (needsToBeRegisteredAsExternalDecl(storage))
739-
TC.Context.addExternalDecl(getter);
723+
TC.Context.addSynthesizedDecl(getter);
740724
TC.DeclsToFinalize.insert(getter);
741725
}
742726

@@ -754,9 +738,7 @@ static void synthesizeTrivialSetter(AccessorDecl *setter,
754738
setterBody, TC);
755739
setter->setBody(BraceStmt::create(ctx, loc, setterBody, loc, true));
756740

757-
// Register the accessor as an external decl if the storage was imported.
758-
if (needsToBeRegisteredAsExternalDecl(storage))
759-
TC.Context.addExternalDecl(setter);
741+
TC.Context.addSynthesizedDecl(setter);
760742
TC.DeclsToFinalize.insert(setter);
761743
}
762744

lib/Sema/DerivedConformanceCodable.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -769,11 +769,7 @@ static FuncDecl *deriveEncodable_encode(TypeChecker &tc, Decl *parentDecl,
769769
encodeDecl->setValidationStarted();
770770
encodeDecl->setAccess(target->getFormalAccess());
771771

772-
// If the type was not imported, the derived conformance is either from the
773-
// type itself or an extension, in which case we will emit the declaration
774-
// normally.
775-
if (target->hasClangNode())
776-
tc.Context.addExternalDecl(encodeDecl);
772+
tc.Context.addSynthesizedDecl(encodeDecl);
777773

778774
target->addMember(encodeDecl);
779775
return encodeDecl;
@@ -1112,11 +1108,7 @@ static ValueDecl *deriveDecodable_init(TypeChecker &tc, Decl *parentDecl,
11121108
initDecl->setInitializerInterfaceType(initializerType);
11131109
initDecl->setAccess(target->getFormalAccess());
11141110

1115-
// If the type was not imported, the derived conformance is either from the
1116-
// type itself or an extension, in which case we will emit the declaration
1117-
// normally.
1118-
if (target->hasClangNode())
1119-
tc.Context.addExternalDecl(initDecl);
1111+
tc.Context.addSynthesizedDecl(initDecl);
11201112

11211113
target->addMember(initDecl);
11221114
return initDecl;

lib/Sema/DerivedConformanceCodingKey.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,7 @@ static ValueDecl *deriveInitDecl(TypeChecker &tc, Decl *parentDecl,
183183
initDecl->setAccess(enumDecl->getFormalAccess());
184184
initDecl->setValidationStarted();
185185

186-
// If the enum was not imported, the derived conformance is either from the
187-
// enum itself or an extension, in which case we will emit the declaration
188-
// normally.
189-
if (enumDecl->hasClangNode())
190-
tc.Context.addExternalDecl(initDecl);
186+
tc.Context.addSynthesizedDecl(initDecl);
191187

192188
cast<IterableDeclContext>(parentDecl)->addMember(initDecl);
193189
return initDecl;

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -661,11 +661,7 @@ deriveEquatable_eq(TypeChecker &tc, Decl *parentDecl, NominalTypeDecl *typeDecl,
661661
eqDecl->copyFormalAccessFrom(typeDecl);
662662
eqDecl->setValidationStarted();
663663

664-
// If the enum was not imported, the derived conformance is either from the
665-
// enum itself or an extension, in which case we will emit the declaration
666-
// normally.
667-
if (typeDecl->hasClangNode())
668-
tc.Context.addExternalDecl(eqDecl);
664+
tc.Context.addSynthesizedDecl(eqDecl);
669665

670666
// Add the operator to the parent scope.
671667
cast<IterableDeclContext>(parentDecl)->addMember(eqDecl);
@@ -1054,12 +1050,6 @@ deriveHashable_hashValue(TypeChecker &tc, Decl *parentDecl,
10541050
getterDecl->setValidationStarted();
10551051
getterDecl->copyFormalAccessFrom(typeDecl);
10561052

1057-
// If the enum was not imported, the derived conformance is either from the
1058-
// enum itself or an extension, in which case we will emit the declaration
1059-
// normally.
1060-
if (typeDecl->hasClangNode())
1061-
tc.Context.addExternalDecl(getterDecl);
1062-
10631053
// Finish creating the property.
10641054
hashValueDecl->setImplicit();
10651055
hashValueDecl->setInterfaceType(intType);
@@ -1081,6 +1071,9 @@ deriveHashable_hashValue(TypeChecker &tc, Decl *parentDecl,
10811071
parentDC);
10821072
patDecl->setImplicit();
10831073

1074+
tc.Context.addSynthesizedDecl(hashValueDecl);
1075+
tc.Context.addSynthesizedDecl(getterDecl);
1076+
10841077
auto dc = cast<IterableDeclContext>(parentDecl);
10851078
dc->addMember(getterDecl);
10861079
dc->addMember(hashValueDecl);

lib/Sema/DerivedConformanceRawRepresentable.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,7 @@ static ConstructorDecl *deriveRawRepresentable_init(TypeChecker &tc,
346346
initDecl->copyFormalAccessFrom(enumDecl);
347347
initDecl->setValidationStarted();
348348

349-
// If the enum was not imported, the derived conformance is either from the
350-
// enum itself or an extension, in which case we will emit the declaration
351-
// normally.
352-
if (enumDecl->hasClangNode())
353-
tc.Context.addExternalDecl(initDecl);
349+
tc.Context.addSynthesizedDecl(initDecl);
354350

355351
cast<IterableDeclContext>(parentDecl)->addMember(initDecl);
356352
return initDecl;

0 commit comments

Comments
 (0)