AST: Optimize collectLinkLibraries()

SourceFile::collectLinkLibraries() did not depend on the source file,
so let's move this logic up into ModuleDecl::collectLinkLibraries().
This commit is contained in:
Slava Pestov
2024-07-10 18:55:00 -04:00
parent eca5a1c342
commit 403bb98451
5 changed files with 76 additions and 60 deletions

View File

@@ -2135,12 +2135,23 @@ bool ModuleDecl::registerEntryPointFile(
}
void ModuleDecl::collectLinkLibraries(LinkLibraryCallback callback) const {
// FIXME: The proper way to do this depends on the decls used.
FORWARD(collectLinkLibraries, (callback));
}
bool hasSourceFile = false;
for (auto *file : getFiles()) {
if (isa<SourceFile>(file)) {
hasSourceFile = true;
} else {
file->collectLinkLibraries(callback);
}
if (auto *synth = file->getSynthesizedFile()) {
synth->collectLinkLibraries(callback);
}
}
if (!hasSourceFile)
return;
void
SourceFile::collectLinkLibraries(ModuleDecl::LinkLibraryCallback callback) const {
llvm::SmallDenseSet<ModuleDecl *, 32> visited;
SmallVector<ImportedModule, 32> stack;
@@ -2148,17 +2159,15 @@ SourceFile::collectLinkLibraries(ModuleDecl::LinkLibraryCallback callback) const
ModuleDecl::ImportFilterKind::Exported,
ModuleDecl::ImportFilterKind::Default};
auto *topLevel = getParentModule();
ModuleDecl::ImportFilter topLevelFilter = filter;
topLevelFilter |= ModuleDecl::ImportFilterKind::ImplementationOnly;
topLevelFilter |= ModuleDecl::ImportFilterKind::InternalOrBelow;
topLevelFilter |= ModuleDecl::ImportFilterKind::PackageOnly,
topLevelFilter |= ModuleDecl::ImportFilterKind::SPIOnly;
topLevel->getImportedModules(stack, topLevelFilter);
getImportedModules(stack, topLevelFilter);
// Make sure the top-level module is first; we want pre-order-ish traversal.
stack.emplace_back(ImportPath::Access(), topLevel);
stack.emplace_back(ImportPath::Access(), const_cast<ModuleDecl *>(this));
while (!stack.empty()) {
auto next = stack.pop_back_val().importedModule;
@@ -2166,7 +2175,7 @@ SourceFile::collectLinkLibraries(ModuleDecl::LinkLibraryCallback callback) const
if (!visited.insert(next).second)
continue;
if (next->getName() != getParentModule()->getName()) {
if (next->getName() != getName()) {
next->collectLinkLibraries(callback);
}
@@ -2174,6 +2183,9 @@ SourceFile::collectLinkLibraries(ModuleDecl::LinkLibraryCallback callback) const
}
}
void
SourceFile::collectLinkLibraries(ModuleDecl::LinkLibraryCallback callback) const {}
bool ModuleDecl::walk(ASTWalker &Walker) {
llvm::SaveAndRestore<ASTWalker::ParentTy> SAR(Walker.Parent, this);
for (auto SF : getFiles())