Skip to content

[cxx-interop] Fix lookup of class template instantiations #58543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4330,9 +4330,15 @@ ClangDirectLookupRequest::evaluate(Evaluator &evaluator,
if (auto spec = dyn_cast<clang::ClassTemplateSpecializationDecl>(clangDecl))
return lookupInClassTemplateSpecialization(ctx, spec, desc.name);

auto *clangModule =
getClangOwningModule(clangDecl, clangDecl->getASTContext());
auto *lookupTable = ctx.getClangModuleLoader()->findLookupTable(clangModule);
SwiftLookupTable *lookupTable = nullptr;
if (isa<clang::NamespaceDecl>(clangDecl)) {
// DeclContext of a namespace imported into Swift is the __ObjC module.
lookupTable = ctx.getClangModuleLoader()->findLookupTable(nullptr);
} else {
auto *clangModule =
getClangOwningModule(clangDecl, clangDecl->getASTContext());
lookupTable = ctx.getClangModuleLoader()->findLookupTable(clangModule);
}

auto foundDecls = lookupTable->lookup(
SerializedSwiftName(desc.name.getBaseName()), EffectiveClangContext());
Expand Down
24 changes: 24 additions & 0 deletions lib/ClangImporter/SwiftLookupTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1922,6 +1922,30 @@ void importer::addEntryToLookupTable(SwiftLookupTable &table,
}
}

// Class template instantiations are imported lazily, however, the lookup
// table must include their mangled name (__CxxTemplateInst...) to make it
// possible to find these decls during deserialization. For any C++ typedef
// that defines a name for a class template instantiation (e.g. std::string),
// import the mangled name of this instantiation, and add it to the table.
if (auto typedefNameDecl = dyn_cast<clang::TypedefNameDecl>(named)) {
auto underlyingDecl = typedefNameDecl->getUnderlyingType()->getAsTagDecl();

if (auto specializationDecl =
dyn_cast_or_null<clang::ClassTemplateSpecializationDecl>(
underlyingDecl)) {
auto name = nameImporter.importName(specializationDecl, currentVersion);

// Avoid adding duplicate entries into the table.
auto existingEntries =
table.lookup(DeclBaseName(name.getDeclName().getBaseName()),
name.getEffectiveContext());
if (existingEntries.empty()) {
table.addEntry(name.getDeclName(), specializationDecl,
name.getEffectiveContext());
}
}
}

// Walk the members of any context that can have nested members.
if (isa<clang::TagDecl>(named) || isa<clang::ObjCInterfaceDecl>(named) ||
isa<clang::ObjCProtocolDecl>(named) ||
Expand Down