Skip to content

Commit a6fb421

Browse files
authored
Merge pull request #71330 from tshortli/import-resolution-underlying-clang-module-cache
AST: Cache underlying clang module during import resolution
2 parents 3881aca + e056c73 commit a6fb421

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

include/swift/AST/SourceFile.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,13 @@ class SourceFile final : public FileUnit {
406406
/// resolution.
407407
void setImports(ArrayRef<AttributedImport<ImportedModule>> imports);
408408

409+
/// Set the imported underlying clang module for this source file. This gets
410+
/// called by import resolution.
411+
void setImportedUnderlyingModule(ModuleDecl *module) {
412+
assert(!ImportedUnderlyingModule && "underlying module already set");
413+
ImportedUnderlyingModule = module;
414+
}
415+
409416
/// Whether the given import has used @preconcurrency.
410417
bool hasImportUsedPreconcurrency(
411418
AttributedImport<ImportedModule> import) const;

lib/AST/Module.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2566,20 +2566,6 @@ void
25662566
SourceFile::setImports(ArrayRef<AttributedImport<ImportedModule>> imports) {
25672567
assert(!Imports && "Already computed imports");
25682568
Imports = getASTContext().AllocateCopy(imports);
2569-
2570-
// Find and cache the import of the underlying module, if present.
2571-
auto parentModuleName = getParentModule()->getName();
2572-
for (auto import : imports) {
2573-
if (!import.options.contains(ImportFlags::Exported))
2574-
continue;
2575-
2576-
auto importedModule = import.module.importedModule;
2577-
if (importedModule->getName() == parentModuleName &&
2578-
importedModule->findUnderlyingClangModule()) {
2579-
ImportedUnderlyingModule = import.module.importedModule;
2580-
break;
2581-
}
2582-
}
25832569
}
25842570

25852571
bool SourceFile::hasImportUsedPreconcurrency(

lib/Sema/ImportResolution.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ class ImportResolver final : public DeclVisitor<ImportResolver> {
177177
/// much, much smaller than \c crossImportableModules.
178178
SmallVector<AttributedImport<ImportedModule>, 16> crossImportDeclaringModules;
179179

180+
/// The underlying clang module of the source file's parent module, if
181+
/// imported.
182+
ModuleDecl *underlyingClangModule = nullptr;
183+
180184
/// The index of the next module in \c visibleModules that should be
181185
/// cross-imported.
182186
size_t nextModuleToCrossImport = 0;
@@ -193,6 +197,10 @@ class ImportResolver final : public DeclVisitor<ImportResolver> {
193197
return boundImports;
194198
}
195199

200+
/// Retrieve the underlying clang module which will be cached if it was loaded
201+
/// when resolving imports.
202+
ModuleDecl *getUnderlyingClangModule() const { return underlyingClangModule; }
203+
196204
private:
197205
// We only need to visit import decls.
198206
void visitImportDecl(ImportDecl *ID);
@@ -287,6 +295,7 @@ void swift::performImportResolution(SourceFile &SF) {
287295
resolver.visit(D);
288296

289297
SF.setImports(resolver.getFinishedImports());
298+
SF.setImportedUnderlyingModule(resolver.getUnderlyingClangModule());
290299

291300
SF.ASTStage = SourceFile::ImportsResolved;
292301
verify(SF);
@@ -396,8 +405,10 @@ ImportResolver::getModule(ImportPath::Module modulePath) {
396405
// for clang overlays as well.
397406
if (ctx.getRealModuleName(moduleID.Item) == loadingModule->getName() &&
398407
modulePath.size() == 1) {
399-
if (auto importer = ctx.getClangModuleLoader())
400-
return importer->loadModule(moduleID.Loc, modulePath);
408+
if (auto importer = ctx.getClangModuleLoader()) {
409+
underlyingClangModule = importer->loadModule(moduleID.Loc, modulePath);
410+
return underlyingClangModule;
411+
}
401412
return nullptr;
402413
}
403414

0 commit comments

Comments
 (0)