Skip to content

Commit 2256b1f

Browse files
committed
AST: Introduce namelookup::getAllImports() to replace forAllVisibleModules()
1 parent 053c3c7 commit 2256b1f

File tree

13 files changed

+47
-141
lines changed

13 files changed

+47
-141
lines changed

include/swift/AST/ImportCache.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ImportSet final :
5151
public llvm::FoldingSetNode,
5252
private llvm::TrailingObjects<ImportSet, ModuleDecl::ImportedModule> {
5353
friend TrailingObjects;
54-
friend ImportCache;
54+
friend class ImportCache;
5555

5656
unsigned HasHeaderImportModule : 1;
5757
unsigned NumTopLevelImports : 31;
@@ -193,6 +193,8 @@ class alignas(ModuleDecl::ImportedModule) ImportCache {
193193
}
194194
};
195195

196+
ArrayRef<ModuleDecl::ImportedModule> getAllImports(const DeclContext *dc);
197+
196198
} // namespace namelookup
197199

198200
} // namespace swift

include/swift/AST/Module.h

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -519,45 +519,6 @@ class ModuleDecl : public DeclContext, public TypeDecl {
519519
/// shadowed clang module.
520520
void getDisplayDecls(SmallVectorImpl<Decl*> &results) const;
521521

522-
/// @{
523-
524-
/// Perform an action for every module visible from this module.
525-
///
526-
/// This only includes modules with at least one declaration visible: if two
527-
/// import access paths are incompatible, the indirect module will be skipped.
528-
/// Modules that can't be used for lookup (including Clang submodules at the
529-
/// time this comment was written) are also skipped under certain
530-
/// circumstances.
531-
///
532-
/// \param topLevelAccessPath If present, include the top-level module in the
533-
/// results, with the given access path.
534-
/// \param fn A callback of type bool(ImportedModule) or void(ImportedModule).
535-
/// Return \c false to abort iteration.
536-
///
537-
/// \return True if the traversal ran to completion, false if it ended early
538-
/// due to the callback.
539-
bool forAllVisibleModules(AccessPathTy topLevelAccessPath,
540-
llvm::function_ref<bool(ImportedModule)> fn) const;
541-
542-
bool forAllVisibleModules(AccessPathTy topLevelAccessPath,
543-
llvm::function_ref<void(ImportedModule)> fn) const {
544-
return forAllVisibleModules(topLevelAccessPath,
545-
[=](const ImportedModule &import) -> bool {
546-
fn(import);
547-
return true;
548-
});
549-
}
550-
551-
template <typename Fn>
552-
bool forAllVisibleModules(AccessPathTy topLevelAccessPath,
553-
Fn &&fn) const {
554-
using RetTy = typename std::result_of<Fn(ImportedModule)>::type;
555-
llvm::function_ref<RetTy(ImportedModule)> wrapped{std::forward<Fn>(fn)};
556-
return forAllVisibleModules(topLevelAccessPath, wrapped);
557-
}
558-
559-
/// @}
560-
561522
using LinkLibraryCallback = llvm::function_ref<void(LinkLibrary)>;
562523

563524
/// Generate the list of libraries needed to link this module, based on its
@@ -817,39 +778,6 @@ class FileUnit : public DeclContext {
817778
virtual void
818779
collectLinkLibraries(ModuleDecl::LinkLibraryCallback callback) const {}
819780

820-
/// @{
821-
822-
/// Perform an action for every module visible from this file.
823-
///
824-
/// \param fn A callback of type bool(ImportedModule) or void(ImportedModule).
825-
/// Return \c false to abort iteration.
826-
///
827-
/// \return True if the traversal ran to completion, false if it ended early
828-
/// due to the callback.
829-
bool
830-
forAllVisibleModules(
831-
llvm::function_ref<bool(ModuleDecl::ImportedModule)> fn) const;
832-
833-
bool
834-
forAllVisibleModules(
835-
llvm::function_ref<void(ModuleDecl::ImportedModule)> fn) const {
836-
return forAllVisibleModules([=](ModuleDecl::ImportedModule import) -> bool {
837-
fn(import);
838-
return true;
839-
});
840-
}
841-
842-
template <typename Fn>
843-
bool forAllVisibleModules(Fn &&fn) const {
844-
using RetTy = typename std::result_of<Fn(ModuleDecl::ImportedModule)>::type;
845-
llvm::function_ref<RetTy(ModuleDecl::ImportedModule)> wrapped{
846-
std::forward<Fn>(fn)
847-
};
848-
return forAllVisibleModules(wrapped);
849-
}
850-
851-
/// @}
852-
853781
/// True if this file contains the main class for the module.
854782
bool hasMainClass() const {
855783
return getMainClass();

include/swift/AST/ModuleNameLookup.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,6 @@ void lookupInModule(const DeclContext *moduleOrFile,
5959
NLKind lookupKind, ResolutionKind resolutionKind,
6060
const DeclContext *moduleScopeContext);
6161

62-
template <typename Fn>
63-
void forAllVisibleModules(const DeclContext *DC, const Fn &fn) {
64-
DeclContext *moduleScope = DC->getModuleScopeContext();
65-
if (auto file = dyn_cast<FileUnit>(moduleScope))
66-
file->forAllVisibleModules(fn);
67-
else
68-
cast<ModuleDecl>(moduleScope)
69-
->forAllVisibleModules(ModuleDecl::AccessPathTy(), fn);
70-
}
71-
7262
/// Performs a qualified lookup into the given module and, if necessary, its
7363
/// reexports, observing proper shadowing rules.
7464
void

lib/AST/ImportCache.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,10 @@ ImportCache::getAllAccessPathsNotShadowedBy(const ModuleDecl *mod,
291291
auto result = allocateArray(ctx, accessPaths);
292292
ShadowCache[key] = result;
293293
return result;
294-
};
294+
};
295+
296+
ArrayRef<ModuleDecl::ImportedModule>
297+
swift::namelookup::getAllImports(const DeclContext *dc) {
298+
return dc->getASTContext().getImportCache().getImportSet(dc)
299+
.getAllImports();
300+
}

lib/AST/Module.cpp

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,32 +1431,6 @@ forAllImportedModules(const ModuleDecl *topLevel,
14311431
return true;
14321432
}
14331433

1434-
bool
1435-
ModuleDecl::forAllVisibleModules(AccessPathTy thisPath,
1436-
llvm::function_ref<bool(ImportedModule)> fn) const {
1437-
return forAllImportedModules<true>(this, thisPath, fn);
1438-
}
1439-
1440-
bool FileUnit::forAllVisibleModules(
1441-
llvm::function_ref<bool(ModuleDecl::ImportedModule)> fn) const {
1442-
if (!getParentModule()->forAllVisibleModules(ModuleDecl::AccessPathTy(), fn))
1443-
return false;
1444-
1445-
if (auto SF = dyn_cast<SourceFile>(this)) {
1446-
// Handle privately visible modules as well.
1447-
ModuleDecl::ImportFilter importFilter;
1448-
importFilter |= ModuleDecl::ImportFilterKind::Private;
1449-
importFilter |= ModuleDecl::ImportFilterKind::ImplementationOnly;
1450-
SmallVector<ModuleDecl::ImportedModule, 4> imports;
1451-
SF->getImportedModules(imports, importFilter);
1452-
for (auto importPair : imports)
1453-
if (!importPair.second->forAllVisibleModules(importPair.first, fn))
1454-
return false;
1455-
}
1456-
1457-
return true;
1458-
}
1459-
14601434
void ModuleDecl::collectLinkLibraries(LinkLibraryCallback callback) const {
14611435
// FIXME: The proper way to do this depends on the decls used.
14621436
FORWARD(collectLinkLibraries, (callback));

lib/AST/NameLookup.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,9 +1706,9 @@ bool DeclContext::lookupAnyObject(DeclName member, NLOptions options,
17061706

17071707
// Collect all of the visible declarations.
17081708
SmallVector<ValueDecl *, 4> allDecls;
1709-
forAllVisibleModules(this, [&](ModuleDecl::ImportedModule import) {
1709+
for (auto import : namelookup::getAllImports(this)) {
17101710
import.second->lookupClassMember(import.first, member, allDecls);
1711-
});
1711+
}
17121712

17131713
// For each declaration whose context is not something we've
17141714
// already visited above, add it to the list of declarations.
@@ -1750,9 +1750,9 @@ void DeclContext::lookupAllObjCMethods(
17501750
ObjCSelector selector,
17511751
SmallVectorImpl<AbstractFunctionDecl *> &results) const {
17521752
// Collect all of the methods with this selector.
1753-
forAllVisibleModules(this, [&](ModuleDecl::ImportedModule import) {
1753+
for (auto import : namelookup::getAllImports(this)) {
17541754
import.second->lookupObjCMethods(selector, results);
1755-
});
1755+
}
17561756

17571757
// Filter out duplicates.
17581758
llvm::SmallPtrSet<AbstractFunctionDecl *, 8> visited;

lib/ClangImporter/ClangImporter.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/AST/ASTContext.h"
2323
#include "swift/AST/DiagnosticEngine.h"
2424
#include "swift/AST/DiagnosticsClangImporter.h"
25+
#include "swift/AST/ImportCache.h"
2526
#include "swift/AST/IRGenOptions.h"
2627
#include "swift/AST/LinkLibrary.h"
2728
#include "swift/AST/Module.h"
@@ -1684,7 +1685,7 @@ ModuleDecl *ClangImporter::Implementation::finishLoadingClangModule(
16841685
// FIXME: This forces the creation of wrapper modules for all imports as
16851686
// well, and may do unnecessary work.
16861687
cacheEntry.setInt(true);
1687-
result->forAllVisibleModules({}, [&](ModuleDecl::ImportedModule import) {});
1688+
(void) namelookup::getAllImports(result);
16881689
}
16891690
} else {
16901691
// Build the representation of the Clang module in Swift.
@@ -1704,7 +1705,7 @@ ModuleDecl *ClangImporter::Implementation::finishLoadingClangModule(
17041705
// Force load overlays for all imported modules.
17051706
// FIXME: This forces the creation of wrapper modules for all imports as
17061707
// well, and may do unnecessary work.
1707-
result->forAllVisibleModules({}, [](ModuleDecl::ImportedModule import) {});
1708+
(void) namelookup::getAllImports(result);
17081709
}
17091710

17101711
if (clangModule->isSubModule()) {

lib/ClangImporter/DWARFImporter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "ImporterImpl.h"
14+
#include "swift/AST/ImportCache.h"
1415
#include "swift/AST/Module.h"
1516

1617
using namespace swift;
@@ -118,7 +119,7 @@ ModuleDecl *ClangImporter::Implementation::loadModuleDWARF(
118119
decl->addFile(*wrapperUnit);
119120

120121
// Force load overlay modules for all imported modules.
121-
decl->forAllVisibleModules({}, [](ModuleDecl::ImportedModule import) {});
122+
(void) namelookup::getAllImports(decl);
122123

123124
// Register the module with the ASTContext so it is available for lookups.
124125
ModuleDecl *&loaded = SwiftContext.LoadedModules[name];

lib/IDE/CodeCompletion.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/AST/ASTPrinter.h"
1717
#include "swift/AST/ASTWalker.h"
1818
#include "swift/AST/Comment.h"
19+
#include "swift/AST/ImportCache.h"
1920
#include "swift/AST/Initializer.h"
2021
#include "swift/AST/GenericSignature.h"
2122
#include "swift/AST/LazyResolver.h"
@@ -3357,8 +3358,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
33573358
std::vector<OperatorDecl *> collectOperators() {
33583359
std::vector<OperatorDecl *> results;
33593360
assert(CurrDeclContext);
3360-
CurrDeclContext->getParentSourceFile()->forAllVisibleModules(
3361-
[&](ModuleDecl::ImportedModule import) {
3361+
for (auto import : namelookup::getAllImports(CurrDeclContext)) {
33623362
for (auto fileUnit : import.second->getFiles()) {
33633363
switch (fileUnit->getKind()) {
33643364
case FileUnitKind::Builtin:
@@ -3373,7 +3373,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
33733373
break;
33743374
}
33753375
}
3376-
});
3376+
}
33773377
return results;
33783378
}
33793379

@@ -3960,15 +3960,14 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
39603960
addPrecedenceGroupRef(PG);
39613961
}
39623962
}
3963-
CurrDeclContext->getParentSourceFile()
3964-
->forAllVisibleModules([&](ModuleDecl::ImportedModule Import) {
3963+
for (auto Import : namelookup::getAllImports(CurrDeclContext)) {
39653964
auto Module = Import.second;
39663965
if (Module == CurrModule)
3967-
return;
3966+
continue;
39683967

39693968
RequestedCachedResults.push_back(
39703969
RequestedResultsTy::fromModule(Module).onlyPrecedenceGroups());
3971-
});
3970+
}
39723971
}
39733972

39743973
void getPrecedenceGroupCompletions(SyntaxKind SK) {
@@ -5536,8 +5535,9 @@ void CodeCompletionCallbacksImpl::doneParsing() {
55365535

55375536
if (Request.TheModule) {
55385537
// FIXME: actually check imports.
5539-
const_cast<ModuleDecl*>(Request.TheModule)
5540-
->forAllVisibleModules({}, handleImport);
5538+
for (auto Import : namelookup::getAllImports(Request.TheModule)) {
5539+
handleImport(Import);
5540+
}
55415541
} else {
55425542
// Add results from current module.
55435543
Lookup.getToplevelCompletions(Request.OnlyTypes);
@@ -5552,9 +5552,8 @@ void CodeCompletionCallbacksImpl::doneParsing() {
55525552
SF->getImportedModules(Imports, ImportFilter);
55535553

55545554
for (auto Imported : Imports) {
5555-
ModuleDecl *TheModule = Imported.second;
5556-
ModuleDecl::AccessPathTy AccessPath = Imported.first;
5557-
TheModule->forAllVisibleModules(AccessPath, handleImport);
5555+
for (auto Import : namelookup::getAllImports(Imported.second))
5556+
handleImport(Import);
55585557
}
55595558
}
55605559
}

lib/Sema/LookupVisibleDecls.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "swift/AST/ASTContext.h"
1919
#include "swift/AST/GenericSignature.h"
2020
#include "swift/AST/GenericSignatureBuilder.h"
21+
#include "swift/AST/ImportCache.h"
2122
#include "swift/AST/Initializer.h"
2223
#include "swift/AST/LazyResolver.h"
2324
#include "swift/AST/ModuleNameLookup.h"
@@ -364,10 +365,9 @@ static void doDynamicLookup(VisibleDeclConsumer &Consumer,
364365

365366
DynamicLookupConsumer ConsumerWrapper(Consumer, LS, CurrDC);
366367

367-
CurrDC->getParentSourceFile()->forAllVisibleModules(
368-
[&](ModuleDecl::ImportedModule Import) {
369-
Import.second->lookupClassMembers(Import.first, ConsumerWrapper);
370-
});
368+
for (auto Import : namelookup::getAllImports(CurrDC)) {
369+
Import.second->lookupClassMembers(Import.first, ConsumerWrapper);
370+
}
371371
}
372372

373373
namespace {

lib/Sema/TypeCheckDeclObjC.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/AST/Decl.h"
2222
#include "swift/AST/ExistentialLayout.h"
2323
#include "swift/AST/ForeignErrorConvention.h"
24+
#include "swift/AST/ImportCache.h"
2425
#include "swift/AST/ParameterList.h"
2526
#include "swift/AST/TypeCheckRequests.h"
2627
#include "swift/Basic/StringExtras.h"
@@ -1795,10 +1796,12 @@ void swift::diagnoseAttrsRequiringFoundation(SourceFile &SF) {
17951796
return;
17961797
}
17971798

1798-
SF.forAllVisibleModules([&](ModuleDecl::ImportedModule import) {
1799-
if (import.second->getName() == Ctx.Id_Foundation)
1799+
for (auto import : namelookup::getAllImports(&SF)) {
1800+
if (import.second->getName() == Ctx.Id_Foundation) {
18001801
ImportsFoundationModule = true;
1801-
});
1802+
break;
1803+
}
1804+
}
18021805

18031806
if (ImportsFoundationModule)
18041807
return;

lib/Sema/TypeChecker.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "swift/AST/DiagnosticSuppression.h"
2828
#include "swift/AST/ExistentialLayout.h"
2929
#include "swift/AST/Identifier.h"
30+
#include "swift/AST/ImportCache.h"
3031
#include "swift/AST/Initializer.h"
3132
#include "swift/AST/ModuleLoader.h"
3233
#include "swift/AST/NameLookup.h"
@@ -249,7 +250,7 @@ static void bindExtensions(SourceFile &SF) {
249250

250251
// FIXME: The current source file needs to be handled specially, because of
251252
// private extensions.
252-
SF.forAllVisibleModules([&](ModuleDecl::ImportedModule import) {
253+
for (auto import : namelookup::getAllImports(&SF)) {
253254
// FIXME: Respect the access path?
254255
for (auto file : import.second->getFiles()) {
255256
auto SF = dyn_cast<SourceFile>(file);
@@ -262,7 +263,7 @@ static void bindExtensions(SourceFile &SF) {
262263
worklist.push_back(ED);
263264
}
264265
}
265-
});
266+
}
266267

267268
// Phase 2 - repeatedly go through the worklist and attempt to bind each
268269
// extension there, removing it from the worklist if we succeed.

0 commit comments

Comments
 (0)