Skip to content

Commit 03e1e3b

Browse files
committed
Lift nested type lookup fast-pathing up to FileUnit.
We use this to avoid circularity issues in serialization; we'd like to extend that to the Clang importer. This is only necessary because we can't look up a single member at a time, but it can still fix issues in the short term. This commit should have no effect on functionality.
1 parent 88aa00e commit 03e1e3b

File tree

6 files changed

+33
-19
lines changed

6 files changed

+33
-19
lines changed

include/swift/AST/Module.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,18 @@ class FileUnit : public DeclContext {
555555
return nullptr;
556556
}
557557

558+
/// Directly look for a nested type declared within this module inside the
559+
/// given nominal type (including any extensions).
560+
///
561+
/// This is a fast-path hack to avoid circular dependencies in deserialization
562+
/// and the Clang importer.
563+
///
564+
/// Private and fileprivate types should not be returned by this lookup.
565+
virtual TypeDecl *lookupNestedType(Identifier name,
566+
const NominalTypeDecl *parent) const {
567+
return nullptr;
568+
}
569+
558570
/// Find ValueDecls in the module and pass them to the given consumer object.
559571
///
560572
/// This does a simple local lookup, not recursively looking through imports.

include/swift/Serialization/ModuleFile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ class ModuleFile : public LazyMemberLoader {
603603

604604
/// Searches the module's nested type decls table for the given member of
605605
/// the given type.
606-
TypeDecl *lookupNestedType(Identifier name, const ValueDecl *parent);
606+
TypeDecl *lookupNestedType(Identifier name, const NominalTypeDecl *parent);
607607

608608
/// Searches the module's operators for one with the given name and fixity.
609609
///

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ class SerializedASTFile final : public LoadedFile {
132132

133133
virtual TypeDecl *lookupLocalType(StringRef MangledName) const override;
134134

135+
virtual TypeDecl *
136+
lookupNestedType(Identifier name,
137+
const NominalTypeDecl *parent) const override;
138+
135139
virtual OperatorDecl *lookupOperator(Identifier name,
136140
DeclKind fixity) const override;
137141

lib/Serialization/Deserialization.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,9 +1319,7 @@ ModuleFile::resolveCrossReference(ModuleDecl *baseModule, uint32_t pathLen) {
13191319
&blobData);
13201320
switch (recordID) {
13211321
case XREF_TYPE_PATH_PIECE: {
1322-
if (values.size() == 1) {
1323-
ModuleDecl *module = values.front()->getModuleContext();
1324-
1322+
if (values.size() == 1 && isa<NominalTypeDecl>(values.front())) {
13251323
// Fast path for nested types that avoids deserializing all
13261324
// members of the parent type.
13271325
IdentifierID IID;
@@ -1334,29 +1332,23 @@ ModuleFile::resolveCrossReference(ModuleDecl *baseModule, uint32_t pathLen) {
13341332
"If you're seeing a crash here, try passing "
13351333
"-Xfrontend -disable-serialization-nested-type-lookup-table"};
13361334

1335+
auto *baseType = cast<NominalTypeDecl>(values.front());
13371336
TypeDecl *nestedType = nullptr;
13381337
if (onlyInNominal) {
13391338
// Only look in the file containing the type itself.
13401339
const DeclContext *dc = values.front()->getDeclContext();
1341-
auto *serializedFile =
1342-
dyn_cast<SerializedASTFile>(dc->getModuleScopeContext());
1343-
if (serializedFile) {
1344-
nestedType =
1345-
serializedFile->File.lookupNestedType(memberName,
1346-
values.front());
1340+
auto *containingFile =
1341+
dyn_cast<FileUnit>(dc->getModuleScopeContext());
1342+
if (containingFile) {
1343+
nestedType = containingFile->lookupNestedType(memberName, baseType);
13471344
}
13481345
} else {
13491346
// Fault in extensions, then ask every serialized AST in the module.
1350-
(void)cast<NominalTypeDecl>(values.front())->getExtensions();
1351-
for (FileUnit *file : module->getFiles()) {
1347+
(void)baseType->getExtensions();
1348+
for (FileUnit *file : baseType->getModuleContext()->getFiles()) {
13521349
if (file == getFile())
13531350
continue;
1354-
auto *serializedFile = dyn_cast<SerializedASTFile>(file);
1355-
if (!serializedFile)
1356-
continue;
1357-
nestedType =
1358-
serializedFile->File.lookupNestedType(memberName,
1359-
values.front());
1351+
nestedType = file->lookupNestedType(memberName, baseType);
13601352
if (nestedType)
13611353
break;
13621354
}

lib/Serialization/ModuleFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,7 @@ TypeDecl *ModuleFile::lookupLocalType(StringRef MangledName) {
13631363
}
13641364

13651365
TypeDecl *ModuleFile::lookupNestedType(Identifier name,
1366-
const ValueDecl *parent) {
1366+
const NominalTypeDecl *parent) {
13671367
PrettyStackTraceModuleFile stackEntry(*this);
13681368

13691369
if (!NestedTypeDecls)

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,12 @@ TypeDecl *SerializedASTFile::lookupLocalType(llvm::StringRef MangledName) const{
525525
return File.lookupLocalType(MangledName);
526526
}
527527

528+
TypeDecl *
529+
SerializedASTFile::lookupNestedType(Identifier name,
530+
const NominalTypeDecl *parent) const {
531+
return File.lookupNestedType(name, parent);
532+
}
533+
528534
OperatorDecl *SerializedASTFile::lookupOperator(Identifier name,
529535
DeclKind fixity) const {
530536
return File.lookupOperator(name, fixity);

0 commit comments

Comments
 (0)