Skip to content

Commit 0c32c54

Browse files
committed
AST: Simplify ModuleDecl::forAllVisibleModules()
1 parent 6d14372 commit 0c32c54

File tree

9 files changed

+43
-85
lines changed

9 files changed

+43
-85
lines changed

include/swift/AST/Module.h

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -400,46 +400,14 @@ class ModuleDecl : public DeclContext, public TypeDecl {
400400
///
401401
/// \param topLevelAccessPath If present, include the top-level module in the
402402
/// results, with the given access path.
403-
/// \param includePrivateTopLevelImports If true, imports listed in all
404-
/// file units within this module are traversed. Otherwise (the
405-
/// default), only re-exported imports are traversed.
406403
/// \param fn A callback of type bool(ImportedModule) or void(ImportedModule).
407404
/// Return \c false to abort iteration.
408405
///
409406
/// \return True if the traversal ran to completion, false if it ended early
410407
/// due to the callback.
411408
bool forAllVisibleModules(AccessPathTy topLevelAccessPath,
412-
bool includePrivateTopLevelImports,
413409
llvm::function_ref<bool(ImportedModule)> fn);
414410

415-
bool forAllVisibleModules(AccessPathTy topLevelAccessPath,
416-
bool includePrivateTopLevelImports,
417-
llvm::function_ref<void(ImportedModule)> fn) {
418-
return forAllVisibleModules(topLevelAccessPath,
419-
includePrivateTopLevelImports,
420-
[=](const ImportedModule &import) -> bool {
421-
fn(import);
422-
return true;
423-
});
424-
}
425-
426-
template <typename Fn>
427-
bool forAllVisibleModules(AccessPathTy topLevelAccessPath,
428-
bool includePrivateTopLevelImports,
429-
Fn &&fn) {
430-
using RetTy = typename std::result_of<Fn(ImportedModule)>::type;
431-
llvm::function_ref<RetTy(ImportedModule)> wrapped{std::forward<Fn>(fn)};
432-
return forAllVisibleModules(topLevelAccessPath,
433-
includePrivateTopLevelImports,
434-
wrapped);
435-
}
436-
437-
template <typename Fn>
438-
bool forAllVisibleModules(AccessPathTy topLevelAccessPath, Fn &&fn) {
439-
return forAllVisibleModules(topLevelAccessPath, false,
440-
std::forward<Fn>(fn));
441-
}
442-
443411
/// @}
444412

445413
using LinkLibraryCallback = llvm::function_ref<void(LinkLibrary)>;
@@ -687,23 +655,6 @@ class FileUnit : public DeclContext {
687655
bool
688656
forAllVisibleModules(llvm::function_ref<bool(ModuleDecl::ImportedModule)> fn);
689657

690-
bool
691-
forAllVisibleModules(llvm::function_ref<void(ModuleDecl::ImportedModule)> fn) {
692-
return forAllVisibleModules([=](ModuleDecl::ImportedModule import) -> bool {
693-
fn(import);
694-
return true;
695-
});
696-
}
697-
698-
template <typename Fn>
699-
bool forAllVisibleModules(Fn &&fn) {
700-
using RetTy = typename std::result_of<Fn(ModuleDecl::ImportedModule)>::type;
701-
llvm::function_ref<RetTy(ModuleDecl::ImportedModule)> wrapped{
702-
std::forward<Fn>(fn)
703-
};
704-
return forAllVisibleModules(wrapped);
705-
}
706-
707658
/// @}
708659

709660
/// True if this file contains the main class for the module.

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,6 +2348,7 @@ void ASTContext::diagnoseAttrsRequiringFoundation(SourceFile &SF) {
23482348
SF.forAllVisibleModules([&](ModuleDecl::ImportedModule import) {
23492349
if (import.second->getName() == Id_Foundation)
23502350
ImportsFoundationModule = true;
2351+
return true;
23512352
});
23522353

23532354
if (ImportsFoundationModule)

lib/AST/LookupVisibleDecls.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ static void doDynamicLookup(VisibleDeclConsumer &Consumer,
387387
CurrDC->getParentSourceFile()->forAllVisibleModules(
388388
[&](ModuleDecl::ImportedModule Import) {
389389
Import.second->lookupClassMembers(Import.first, ConsumerWrapper);
390+
return true;
390391
});
391392
}
392393

lib/AST/Module.cpp

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,40 +1127,28 @@ bool ModuleDecl::isSystemModule() const {
11271127
return false;
11281128
}
11291129

1130-
template<bool respectVisibility, typename Callback>
11311130
static bool forAllImportedModules(ModuleDecl *topLevel,
11321131
ModuleDecl::AccessPathTy thisPath,
1133-
bool includePrivateTopLevelImports,
1134-
const Callback &fn) {
1132+
llvm::function_ref<bool(ModuleDecl::ImportedModule)> fn) {
11351133
using ImportedModule = ModuleDecl::ImportedModule;
1136-
using AccessPathTy = ModuleDecl::AccessPathTy;
11371134

11381135
llvm::SmallSet<ImportedModule, 32, ModuleDecl::OrderImportedModules> visited;
11391136
SmallVector<ImportedModule, 32> stack;
11401137

1141-
// Even if we're processing the top-level module like any other, we may
1142-
// still want to include non-exported modules.
1143-
ModuleDecl::ImportFilter filter = respectVisibility ? ModuleDecl::ImportFilter::Public
1144-
: ModuleDecl::ImportFilter::All;
1145-
ModuleDecl::ImportFilter topLevelFilter =
1146-
includePrivateTopLevelImports ? ModuleDecl::ImportFilter::All : filter;
1147-
topLevel->getImportedModules(stack, topLevelFilter);
1138+
topLevel->getImportedModules(stack, ModuleDecl::ImportFilter::Public);
11481139

11491140
// Make sure the top-level module is first; we want pre-order-ish traversal.
1150-
AccessPathTy overridingPath;
1151-
if (respectVisibility)
1152-
overridingPath = thisPath;
1153-
stack.push_back(ImportedModule(overridingPath, topLevel));
1141+
stack.push_back(ImportedModule(thisPath, topLevel));
11541142

11551143
while (!stack.empty()) {
11561144
auto next = stack.pop_back_val();
11571145

11581146
// Filter any whole-module imports, and skip specific-decl imports if the
11591147
// import path doesn't match exactly.
1160-
if (next.first.empty() || !respectVisibility)
1161-
next.first = overridingPath;
1162-
else if (!overridingPath.empty() &&
1163-
!ModuleDecl::isSameAccessPath(next.first, overridingPath)) {
1148+
if (next.first.empty())
1149+
next.first = thisPath;
1150+
else if (!thisPath.empty() &&
1151+
!ModuleDecl::isSameAccessPath(next.first, thisPath)) {
11641152
// If we ever allow importing non-top-level decls, it's possible the rule
11651153
// above isn't what we want.
11661154
assert(next.first.size() == 1 && "import of non-top-level decl");
@@ -1173,20 +1161,15 @@ static bool forAllImportedModules(ModuleDecl *topLevel,
11731161
if (!fn(next))
11741162
return false;
11751163

1176-
if (respectVisibility)
1177-
next.second->getImportedModulesForLookup(stack);
1178-
else
1179-
next.second->getImportedModules(stack, filter);
1164+
next.second->getImportedModulesForLookup(stack);
11801165
}
11811166

11821167
return true;
11831168
}
11841169

11851170
bool ModuleDecl::forAllVisibleModules(AccessPathTy thisPath,
1186-
bool includePrivateTopLevelImports,
11871171
llvm::function_ref<bool(ImportedModule)> fn) {
1188-
return forAllImportedModules<true>(this, thisPath,
1189-
includePrivateTopLevelImports, fn);
1172+
return forAllImportedModules(this, thisPath, fn);
11901173
}
11911174

11921175
bool FileUnit::forAllVisibleModules(
@@ -1215,13 +1198,15 @@ void ModuleDecl::collectLinkLibraries(LinkLibraryCallback callback) {
12151198
void
12161199
SourceFile::collectLinkLibraries(ModuleDecl::LinkLibraryCallback callback) const {
12171200

1218-
const_cast<SourceFile *>(this)->forAllVisibleModules([&](swift::ModuleDecl::ImportedModule import) {
1219-
swift::ModuleDecl *next = import.second;
1220-
if (next->getName() == getParentModule()->getName())
1221-
return;
1201+
const_cast<SourceFile *>(this)->forAllVisibleModules(
1202+
[&](swift::ModuleDecl::ImportedModule import) {
1203+
swift::ModuleDecl *next = import.second;
1204+
if (next->getName() == getParentModule()->getName())
1205+
return true;
12221206

1223-
next->collectLinkLibraries(callback);
1224-
});
1207+
next->collectLinkLibraries(callback);
1208+
return true;
1209+
});
12251210
}
12261211

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

lib/AST/NameLookup.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,6 +1865,7 @@ bool DeclContext::lookupQualified(Type type,
18651865
SmallVector<ValueDecl *, 4> allDecls;
18661866
forAllVisibleModules(this, [&](ModuleDecl::ImportedModule import) {
18671867
import.second->lookupClassMember(import.first, member, allDecls);
1868+
return true;
18681869
});
18691870

18701871
// For each declaration whose context is not something we've
@@ -1927,6 +1928,7 @@ void DeclContext::lookupAllObjCMethods(
19271928
// Collect all of the methods with this selector.
19281929
forAllVisibleModules(this, [&](ModuleDecl::ImportedModule import) {
19291930
import.second->lookupObjCMethods(selector, results);
1931+
return true;
19301932
});
19311933

19321934
// Filter out duplicates.

lib/ClangImporter/ClangImporter.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,7 +1576,9 @@ ModuleDecl *ClangImporter::Implementation::finishLoadingClangModule(
15761576
// FIXME: This forces the creation of wrapper modules for all imports as
15771577
// well, and may do unnecessary work.
15781578
cacheEntry.setInt(true);
1579-
result->forAllVisibleModules({}, [&](ModuleDecl::ImportedModule import) {});
1579+
result->forAllVisibleModules({}, [&](ModuleDecl::ImportedModule import) {
1580+
return true;
1581+
});
15801582
}
15811583
} else {
15821584
// Build the representation of the Clang module in Swift.
@@ -1595,7 +1597,9 @@ ModuleDecl *ClangImporter::Implementation::finishLoadingClangModule(
15951597
// Force load adapter modules for all imported modules.
15961598
// FIXME: This forces the creation of wrapper modules for all imports as
15971599
// well, and may do unnecessary work.
1598-
result->forAllVisibleModules({}, [](ModuleDecl::ImportedModule import) {});
1600+
result->forAllVisibleModules({}, [](ModuleDecl::ImportedModule import) {
1601+
return true;
1602+
});
15991603
}
16001604

16011605
if (clangModule->isSubModule()) {

lib/IDE/CodeCompletion.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3329,6 +3329,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
33293329
break;
33303330
}
33313331
}
3332+
3333+
return true;
33323334
});
33333335
return results;
33343336
}
@@ -5602,7 +5604,11 @@ void CodeCompletionCallbacksImpl::doneParsing() {
56025604

56035605
// FIXME: actually check imports.
56045606
const_cast<ModuleDecl*>(Request.TheModule)
5605-
->forAllVisibleModules({}, handleImport);
5607+
->forAllVisibleModules({},
5608+
[&](ModuleDecl::ImportedModule Import) {
5609+
handleImport(Import);
5610+
return true;
5611+
});
56065612
} else {
56075613
// Add results from current module.
56085614
Lookup.getToplevelCompletions(Request.OnlyTypes);
@@ -5616,7 +5622,11 @@ void CodeCompletionCallbacksImpl::doneParsing() {
56165622
for (auto Imported : Imports) {
56175623
ModuleDecl *TheModule = Imported.second;
56185624
ModuleDecl::AccessPathTy AccessPath = Imported.first;
5619-
TheModule->forAllVisibleModules(AccessPath, handleImport);
5625+
TheModule->forAllVisibleModules(AccessPath,
5626+
[&](ModuleDecl::ImportedModule Import) {
5627+
handleImport(Import);
5628+
return true;
5629+
});
56205630
}
56215631
}
56225632
Lookup.RequestedCachedResults.reset();

lib/Sema/TypeChecker.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,8 @@ void swift::performTypeChecking(SourceFile &SF, TopLevelContext &TLC,
650650
bindExtensionDecl(ED, TC);
651651
}
652652
}
653+
654+
return true;
653655
});
654656

655657
// Type check the top-level elements of the source file.

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,6 +2449,8 @@ static int doPrintModuleImports(const CompilerInvocation &InitInvok,
24492449
llvm::outs() << " (Clang)";
24502450
llvm::outs() << "\n";
24512451
}
2452+
2453+
return true;
24522454
});
24532455
}
24542456

0 commit comments

Comments
 (0)