Skip to content

Commit 3956fc0

Browse files
committed
AST: Fold forAllImportedModules() into collectLinkLibraries()
1 parent 2256b1f commit 3956fc0

File tree

1 file changed

+26
-63
lines changed

1 file changed

+26
-63
lines changed

lib/AST/Module.cpp

Lines changed: 26 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,84 +1375,47 @@ bool ModuleDecl::registerEntryPointFile(FileUnit *file, SourceLoc diagLoc,
13751375
return true;
13761376
}
13771377

1378-
template<bool respectVisibility>
1379-
static bool
1380-
forAllImportedModules(const ModuleDecl *topLevel,
1381-
ModuleDecl::AccessPathTy thisPath,
1382-
llvm::function_ref<bool(ModuleDecl::ImportedModule)> fn) {
1383-
using ImportedModule = ModuleDecl::ImportedModule;
1384-
using AccessPathTy = ModuleDecl::AccessPathTy;
1385-
1386-
llvm::SmallSet<ImportedModule, 32, ModuleDecl::OrderImportedModules> visited;
1387-
SmallVector<ImportedModule, 32> stack;
1378+
void ModuleDecl::collectLinkLibraries(LinkLibraryCallback callback) const {
1379+
// FIXME: The proper way to do this depends on the decls used.
1380+
FORWARD(collectLinkLibraries, (callback));
1381+
}
1382+
1383+
void
1384+
SourceFile::collectLinkLibraries(ModuleDecl::LinkLibraryCallback callback) const {
1385+
llvm::SmallDenseSet<ModuleDecl *, 32> visited;
1386+
SmallVector<ModuleDecl::ImportedModule, 32> stack;
13881387

13891388
ModuleDecl::ImportFilter filter = ModuleDecl::ImportFilterKind::Public;
1390-
if (!respectVisibility)
1391-
filter |= ModuleDecl::ImportFilterKind::Private;
1389+
filter |= ModuleDecl::ImportFilterKind::Private;
1390+
1391+
auto *topLevel = getParentModule();
13921392

13931393
ModuleDecl::ImportFilter topLevelFilter = filter;
1394-
if (!respectVisibility)
1395-
topLevelFilter |= ModuleDecl::ImportFilterKind::ImplementationOnly;
1394+
topLevelFilter |= ModuleDecl::ImportFilterKind::ImplementationOnly;
13961395
topLevel->getImportedModules(stack, topLevelFilter);
13971396

13981397
// Make sure the top-level module is first; we want pre-order-ish traversal.
1399-
AccessPathTy overridingPath;
1400-
if (respectVisibility)
1401-
overridingPath = thisPath;
1402-
stack.emplace_back(overridingPath, const_cast<ModuleDecl *>(topLevel));
1398+
stack.emplace_back(ModuleDecl::AccessPathTy(),
1399+
const_cast<ModuleDecl *>(topLevel));
14031400

14041401
while (!stack.empty()) {
1405-
auto next = stack.pop_back_val();
1406-
1407-
// Filter any whole-module imports, and skip specific-decl imports if the
1408-
// import path doesn't match exactly.
1409-
if (next.first.empty() || !respectVisibility)
1410-
next.first = overridingPath;
1411-
else if (!overridingPath.empty() &&
1412-
!ModuleDecl::isSameAccessPath(next.first, overridingPath)) {
1413-
// If we ever allow importing non-top-level decls, it's possible the rule
1414-
// above isn't what we want.
1415-
assert(next.first.size() == 1 && "import of non-top-level decl");
1416-
continue;
1417-
}
1402+
auto next = stack.pop_back_val().second;
14181403

14191404
if (!visited.insert(next).second)
14201405
continue;
14211406

1422-
if (!fn(next))
1423-
return false;
1424-
1425-
if (respectVisibility)
1426-
next.second->getImportedModulesForLookup(stack);
1427-
else
1428-
next.second->getImportedModules(stack, filter);
1429-
}
1407+
if (next->getName() != getParentModule()->getName()) {
1408+
// Hack: Assume other REPL files already have their libraries linked.
1409+
if (!next->getFiles().empty())
1410+
if (auto *nextSource = dyn_cast<SourceFile>(next->getFiles().front()))
1411+
if (nextSource->Kind == SourceFileKind::REPL)
1412+
continue;
14301413

1431-
return true;
1432-
}
1433-
1434-
void ModuleDecl::collectLinkLibraries(LinkLibraryCallback callback) const {
1435-
// FIXME: The proper way to do this depends on the decls used.
1436-
FORWARD(collectLinkLibraries, (callback));
1437-
}
1438-
1439-
void
1440-
SourceFile::collectLinkLibraries(ModuleDecl::LinkLibraryCallback callback) const {
1441-
forAllImportedModules<false>(getParentModule(), /*thisPath*/{},
1442-
[=](ModuleDecl::ImportedModule import) -> bool {
1443-
swift::ModuleDecl *next = import.second;
1444-
if (next->getName() == getParentModule()->getName())
1445-
return true;
1446-
1447-
// Hack: Assume other REPL files already have their libraries linked.
1448-
if (!next->getFiles().empty())
1449-
if (auto *nextSource = dyn_cast<SourceFile>(next->getFiles().front()))
1450-
if (nextSource->Kind == SourceFileKind::REPL)
1451-
return true;
1414+
next->collectLinkLibraries(callback);
1415+
}
14521416

1453-
next->collectLinkLibraries(callback);
1454-
return true;
1455-
});
1417+
next->getImportedModules(stack, filter);
1418+
}
14561419
}
14571420

14581421
bool ModuleDecl::walk(ASTWalker &Walker) {

0 commit comments

Comments
 (0)