Skip to content

Commit 9ed3fe0

Browse files
committed
Change ModuleDecl::getImportedModules to take an option set
...in preparation for me adding a third kind of import, making the existing "All" kind a problem. NFC, except that I did rewrite the ClangModuleUnit implementation of getImportedModules to be simpler!
1 parent a62c87f commit 9ed3fe0

File tree

18 files changed

+97
-102
lines changed

18 files changed

+97
-102
lines changed

include/swift/AST/Module.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -419,18 +419,19 @@ class ModuleDecl : public DeclContext, public TypeDecl {
419419
SmallVectorImpl<AbstractFunctionDecl *> &results) const;
420420

421421
/// \sa getImportedModules
422-
enum class ImportFilter {
423-
All,
424-
Public,
425-
Private
422+
enum class ImportFilterKind {
423+
Public = 1 << 0,
424+
Private = 1 << 1
426425
};
426+
/// \sa getImportedModules
427+
using ImportFilter = OptionSet<ImportFilterKind>;
427428

428429
/// Looks up which modules are imported by this module.
429430
///
430431
/// \p filter controls whether public, private, or any imports are included
431432
/// in this list.
432433
void getImportedModules(SmallVectorImpl<ImportedModule> &imports,
433-
ImportFilter filter = ImportFilter::Public) const;
434+
ImportFilter filter = ImportFilterKind::Public) const;
434435

435436
/// Looks up which modules are imported by this module, ignoring any that
436437
/// won't contain top-level decls.
@@ -758,7 +759,7 @@ class FileUnit : public DeclContext {
758759
/// \see ModuleDecl::getImportedModulesForLookup
759760
virtual void getImportedModulesForLookup(
760761
SmallVectorImpl<ModuleDecl::ImportedModule> &imports) const {
761-
return getImportedModules(imports, ModuleDecl::ImportFilter::Public);
762+
return getImportedModules(imports, ModuleDecl::ImportFilterKind::Public);
762763
}
763764

764765
/// Generates the list of libraries needed to link this file, based on its

lib/AST/Module.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,17 +1021,12 @@ SourceFile::getImportedModules(SmallVectorImpl<ModuleDecl::ImportedModule> &modu
10211021
ModuleDecl::ImportFilter filter) const {
10221022
assert(ASTStage >= Parsed || Kind == SourceFileKind::SIL);
10231023
for (auto desc : Imports) {
1024-
switch (filter) {
1025-
case ModuleDecl::ImportFilter::All:
1026-
break;
1027-
case ModuleDecl::ImportFilter::Public:
1028-
if (!desc.importOptions.contains(ImportFlags::Exported))
1024+
if (desc.importOptions.contains(ImportFlags::Exported)) {
1025+
if (!filter.contains(ModuleDecl::ImportFilterKind::Public))
10291026
continue;
1030-
break;
1031-
case ModuleDecl::ImportFilter::Private:
1032-
if (desc.importOptions.contains(ImportFlags::Exported))
1027+
} else {
1028+
if (!filter.contains(ModuleDecl::ImportFilterKind::Private))
10331029
continue;
1034-
break;
10351030
}
10361031

10371032
modules.push_back(desc.module);
@@ -1277,8 +1272,9 @@ forAllImportedModules(ModuleDecl *topLevel, ModuleDecl::AccessPathTy thisPath,
12771272
llvm::SmallSet<ImportedModule, 32, ModuleDecl::OrderImportedModules> visited;
12781273
SmallVector<ImportedModule, 32> stack;
12791274

1280-
auto filter = respectVisibility ? ModuleDecl::ImportFilter::Public
1281-
: ModuleDecl::ImportFilter::All;
1275+
ModuleDecl::ImportFilter filter = ModuleDecl::ImportFilterKind::Public;
1276+
if (!respectVisibility)
1277+
filter |= ModuleDecl::ImportFilterKind::Private;
12821278
topLevel->getImportedModules(stack, filter);
12831279

12841280
// Make sure the top-level module is first; we want pre-order-ish traversal.
@@ -1332,7 +1328,7 @@ bool FileUnit::forAllVisibleModules(
13321328
// Handle privately visible modules as well.
13331329
// FIXME: Should this apply to all FileUnits?
13341330
SmallVector<ModuleDecl::ImportedModule, 4> imports;
1335-
SF->getImportedModules(imports, ModuleDecl::ImportFilter::Private);
1331+
SF->getImportedModules(imports, ModuleDecl::ImportFilterKind::Private);
13361332
for (auto importPair : imports)
13371333
if (!importPair.second->forAllVisibleModules(importPair.first, fn))
13381334
return false;

lib/AST/UnqualifiedLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ void UnqualifiedLookupFactory::addImportedResults(DeclContext *const dc) {
10451045
// Add private imports to the extra search list.
10461046
SmallVector<ModuleDecl::ImportedModule, 8> extraImports;
10471047
if (auto FU = dyn_cast<FileUnit>(dc))
1048-
FU->getImportedModules(extraImports, ModuleDecl::ImportFilter::Private);
1048+
FU->getImportedModules(extraImports, ModuleDecl::ImportFilterKind::Private);
10491049

10501050
using namespace namelookup;
10511051
SmallVector<ValueDecl *, 8> CurModuleResults;

lib/ClangImporter/ClangImporter.cpp

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3203,71 +3203,38 @@ ModuleDecl *ClangModuleUnit::getAdapterModule() const {
32033203
void ClangModuleUnit::getImportedModules(
32043204
SmallVectorImpl<ModuleDecl::ImportedModule> &imports,
32053205
ModuleDecl::ImportFilter filter) const {
3206-
switch (filter) {
3207-
case ModuleDecl::ImportFilter::All:
3208-
case ModuleDecl::ImportFilter::Private:
3206+
if (filter.contains(ModuleDecl::ImportFilterKind::Private))
32093207
if (auto stdlib = owner.getStdlibModule())
32103208
imports.push_back({ModuleDecl::AccessPathTy(), stdlib});
3211-
break;
3212-
case ModuleDecl::ImportFilter::Public:
3213-
break;
3214-
}
32153209

32163210
SmallVector<clang::Module *, 8> imported;
32173211
if (!clangModule) {
32183212
// This is the special "imported headers" module.
3219-
switch (filter) {
3220-
case ModuleDecl::ImportFilter::All:
3221-
case ModuleDecl::ImportFilter::Public:
3213+
if (filter.contains(ModuleDecl::ImportFilterKind::Public)) {
32223214
imported.append(owner.ImportedHeaderExports.begin(),
32233215
owner.ImportedHeaderExports.end());
3224-
break;
3225-
3226-
case ModuleDecl::ImportFilter::Private:
3227-
break;
32283216
}
32293217

32303218
} else {
32313219
clangModule->getExportedModules(imported);
32323220

3233-
switch (filter) {
3234-
case ModuleDecl::ImportFilter::All: {
3235-
llvm::SmallPtrSet<clang::Module *, 8> knownModules;
3236-
imported.append(clangModule->Imports.begin(), clangModule->Imports.end());
3237-
imported.erase(std::remove_if(imported.begin(), imported.end(),
3238-
[&](clang::Module *mod) -> bool {
3239-
return !knownModules.insert(mod).second;
3240-
}),
3241-
imported.end());
3242-
3243-
// FIXME: The parent module isn't exactly a private import, but it is
3244-
// needed for link dependencies.
3245-
if (clangModule->Parent)
3246-
imported.push_back(clangModule->Parent);
3247-
3248-
break;
3249-
}
3250-
3251-
case ModuleDecl::ImportFilter::Private: {
3221+
if (filter.contains(ModuleDecl::ImportFilterKind::Private)) {
3222+
// Copy in any modules that are imported but not exported.
32523223
llvm::SmallPtrSet<clang::Module *, 8> knownModules(imported.begin(),
32533224
imported.end());
3254-
SmallVector<clang::Module *, 8> privateImports;
3255-
std::copy_if(clangModule->Imports.begin(), clangModule->Imports.end(),
3256-
std::back_inserter(privateImports), [&](clang::Module *mod) {
3257-
return knownModules.count(mod) == 0;
3258-
});
3259-
imported.swap(privateImports);
3225+
if (!filter.contains(ModuleDecl::ImportFilterKind::Public)) {
3226+
// Remove the exported ones now that we're done with them.
3227+
imported.clear();
3228+
}
3229+
llvm::copy_if(clangModule->Imports, std::back_inserter(imported),
3230+
[&](clang::Module *mod) {
3231+
return !knownModules.insert(mod).second;
3232+
});
32603233

32613234
// FIXME: The parent module isn't exactly a private import, but it is
32623235
// needed for link dependencies.
32633236
if (clangModule->Parent)
32643237
imported.push_back(clangModule->Parent);
3265-
3266-
break;
3267-
}
3268-
3269-
case ModuleDecl::ImportFilter::Public:
3270-
break;
32713238
}
32723239
}
32733240

lib/Frontend/ParseableInterfaceSupport.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,19 @@ llvm::Regex swift::getSwiftInterfaceModuleFlagsRegex() {
8686
static void printImports(raw_ostream &out, ModuleDecl *M) {
8787
// FIXME: This is very similar to what's in Serializer::writeInputBlock, but
8888
// it's not obvious what higher-level optimization would be factored out here.
89+
ModuleDecl::ImportFilter allImportFilter;
90+
allImportFilter |= ModuleDecl::ImportFilterKind::Public;
91+
allImportFilter |= ModuleDecl::ImportFilterKind::Private;
92+
8993
SmallVector<ModuleDecl::ImportedModule, 8> allImports;
90-
M->getImportedModules(allImports, ModuleDecl::ImportFilter::All);
94+
M->getImportedModules(allImports, allImportFilter);
9195
ModuleDecl::removeDuplicateImports(allImports);
9296
diagnoseScopedImports(M->getASTContext().Diags, allImports);
9397

9498
// Collect the public imports as a subset so that we can mark them with
9599
// '@_exported'.
96100
SmallVector<ModuleDecl::ImportedModule, 8> publicImports;
97-
M->getImportedModules(publicImports, ModuleDecl::ImportFilter::Public);
101+
M->getImportedModules(publicImports, ModuleDecl::ImportFilterKind::Public);
98102
llvm::SmallSet<ModuleDecl::ImportedModule, 8,
99103
ModuleDecl::OrderImportedModules> publicImportSet;
100104
publicImportSet.insert(publicImports.begin(), publicImports.end());

lib/FrontendTool/ImportedModules.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,13 @@ bool swift::emitImportedModules(ASTContext &Context, ModuleDecl *mainModule,
7979
StringRef implicitHeaderPath = opts.ImplicitObjCHeaderPath;
8080
if (!implicitHeaderPath.empty()) {
8181
if (!clangImporter->importBridgingHeader(implicitHeaderPath, mainModule)) {
82+
ModuleDecl::ImportFilter importFilter;
83+
importFilter |= ModuleDecl::ImportFilterKind::Public;
84+
importFilter |= ModuleDecl::ImportFilterKind::Private;
85+
8286
SmallVector<ModuleDecl::ImportedModule, 16> imported;
8387
clangImporter->getImportedHeaderModule()->getImportedModules(
84-
imported, ModuleDecl::ImportFilter::All);
88+
imported, importFilter);
8589

8690
for (auto IM : imported) {
8791
if (auto clangModule = IM.second->findUnderlyingClangModule())

lib/IDE/CodeCompletion.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,17 +1707,22 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
17071707
}
17081708

17091709
void collectImportedModules(llvm::StringSet<> &ImportedModules) {
1710+
ModuleDecl::ImportFilter ImportFilter;
1711+
ImportFilter |= ModuleDecl::ImportFilterKind::Public;
1712+
ImportFilter |= ModuleDecl::ImportFilterKind::Private;
1713+
17101714
SmallVector<ModuleDecl::ImportedModule, 16> Imported;
17111715
SmallVector<ModuleDecl::ImportedModule, 16> FurtherImported;
17121716
CurrDeclContext->getParentSourceFile()->getImportedModules(Imported,
1713-
ModuleDecl::ImportFilter::All);
1717+
ImportFilter);
17141718
while (!Imported.empty()) {
17151719
ModuleDecl *MD = Imported.back().second;
17161720
Imported.pop_back();
17171721
if (!ImportedModules.insert(MD->getNameStr()).second)
17181722
continue;
17191723
FurtherImported.clear();
1720-
MD->getImportedModules(FurtherImported, ModuleDecl::ImportFilter::Public);
1724+
MD->getImportedModules(FurtherImported,
1725+
ModuleDecl::ImportFilterKind::Public);
17211726
Imported.append(FurtherImported.begin(), FurtherImported.end());
17221727
for (auto SubMod : FurtherImported) {
17231728
Imported.push_back(SubMod);
@@ -5331,9 +5336,12 @@ void CodeCompletionCallbacksImpl::doneParsing() {
53315336
Lookup.getToplevelCompletions(Request.OnlyTypes);
53325337

53335338
// Add results for all imported modules.
5339+
ModuleDecl::ImportFilter ImportFilter;
5340+
ImportFilter |= ModuleDecl::ImportFilterKind::Public;
5341+
ImportFilter |= ModuleDecl::ImportFilterKind::Private;
53345342
SmallVector<ModuleDecl::ImportedModule, 4> Imports;
53355343
auto *SF = CurDeclContext->getParentSourceFile();
5336-
SF->getImportedModules(Imports, ModuleDecl::ImportFilter::All);
5344+
SF->getImportedModules(Imports, ImportFilter);
53375345

53385346
for (auto Imported : Imports) {
53395347
ModuleDecl *TheModule = Imported.second;

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,9 +1741,11 @@ void IRGenDebugInfoImpl::finalize() {
17411741

17421742
// Get the list of imported modules (which may actually be different
17431743
// from all ImportDecls).
1744+
ModuleDecl::ImportFilter ImportFilter;
1745+
ImportFilter |= ModuleDecl::ImportFilterKind::Public;
1746+
ImportFilter |= ModuleDecl::ImportFilterKind::Private;
17441747
SmallVector<ModuleDecl::ImportedModule, 8> ModuleWideImports;
1745-
IGM.getSwiftModule()->getImportedModules(ModuleWideImports,
1746-
ModuleDecl::ImportFilter::All);
1748+
IGM.getSwiftModule()->getImportedModules(ModuleWideImports, ImportFilter);
17471749
for (auto M : ModuleWideImports)
17481750
if (!ImportedModules.count(M.second))
17491751
DBuilder.createImportedModule(MainFile, getOrCreateModule(M), MainFile,

lib/Immediate/REPL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ typeCheckREPLInput(ModuleDecl *MostRecentModule, StringRef Name,
176176

177177
SmallVector<ModuleDecl::ImportedModule, 8> Imports;
178178
MostRecentModule->getImportedModules(Imports,
179-
ModuleDecl::ImportFilter::Private);
179+
ModuleDecl::ImportFilterKind::Private);
180180
if (!Imports.empty()) {
181181
SmallVector<SourceFile::ImportedModuleDesc, 8> ImportsWithOptions;
182182
for (auto Import : Imports) {

lib/Index/Index.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,14 @@ class SourceFileOrModule {
120120

121121
void
122122
getImportedModules(SmallVectorImpl<ModuleDecl::ImportedModule> &Modules) const {
123+
ModuleDecl::ImportFilter ImportFilter;
124+
ImportFilter |= ModuleDecl::ImportFilterKind::Public;
125+
ImportFilter |= ModuleDecl::ImportFilterKind::Private;
126+
123127
if (auto *SF = SFOrMod.dyn_cast<SourceFile *>()) {
124-
SF->getImportedModules(Modules, ModuleDecl::ImportFilter::All);
128+
SF->getImportedModules(Modules, ImportFilter);
125129
} else {
126-
SFOrMod.get<ModuleDecl *>()->getImportedModules(Modules,
127-
ModuleDecl::ImportFilter::All);
130+
SFOrMod.get<ModuleDecl *>()->getImportedModules(Modules, ImportFilter);
128131
}
129132
}
130133
};
@@ -1536,8 +1539,11 @@ void IndexSwiftASTWalker::collectRecursiveModuleImports(
15361539
return;
15371540
}
15381541

1542+
ModuleDecl::ImportFilter ImportFilter;
1543+
ImportFilter |= ModuleDecl::ImportFilterKind::Public;
1544+
ImportFilter |= ModuleDecl::ImportFilterKind::Private;
15391545
SmallVector<ModuleDecl::ImportedModule, 8> Imports;
1540-
TopMod.getImportedModules(Imports, ModuleDecl::ImportFilter::All);
1546+
TopMod.getImportedModules(Imports);
15411547

15421548
for (auto Import : Imports) {
15431549
collectRecursiveModuleImports(*Import.second, Visited);

0 commit comments

Comments
 (0)