Skip to content

Commit f0132e0

Browse files
slavapestovjrose-apple
authored andcommitted
Merge pull request swiftlang#16206 from slavapestov/serialization-cleanup
Serialization cleanup (cherry picked from commit af87582)
1 parent a3d8289 commit f0132e0

File tree

14 files changed

+113
-151
lines changed

14 files changed

+113
-151
lines changed

include/swift/AST/Module.h

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -408,46 +408,14 @@ class ModuleDecl : public DeclContext, public TypeDecl {
408408
///
409409
/// \param topLevelAccessPath If present, include the top-level module in the
410410
/// results, with the given access path.
411-
/// \param includePrivateTopLevelImports If true, imports listed in all
412-
/// file units within this module are traversed. Otherwise (the
413-
/// default), only re-exported imports are traversed.
414411
/// \param fn A callback of type bool(ImportedModule) or void(ImportedModule).
415412
/// Return \c false to abort iteration.
416413
///
417414
/// \return True if the traversal ran to completion, false if it ended early
418415
/// due to the callback.
419416
bool forAllVisibleModules(AccessPathTy topLevelAccessPath,
420-
bool includePrivateTopLevelImports,
421417
llvm::function_ref<bool(ImportedModule)> fn);
422418

423-
bool forAllVisibleModules(AccessPathTy topLevelAccessPath,
424-
bool includePrivateTopLevelImports,
425-
llvm::function_ref<void(ImportedModule)> fn) {
426-
return forAllVisibleModules(topLevelAccessPath,
427-
includePrivateTopLevelImports,
428-
[=](const ImportedModule &import) -> bool {
429-
fn(import);
430-
return true;
431-
});
432-
}
433-
434-
template <typename Fn>
435-
bool forAllVisibleModules(AccessPathTy topLevelAccessPath,
436-
bool includePrivateTopLevelImports,
437-
Fn &&fn) {
438-
using RetTy = typename std::result_of<Fn(ImportedModule)>::type;
439-
llvm::function_ref<RetTy(ImportedModule)> wrapped{std::forward<Fn>(fn)};
440-
return forAllVisibleModules(topLevelAccessPath,
441-
includePrivateTopLevelImports,
442-
wrapped);
443-
}
444-
445-
template <typename Fn>
446-
bool forAllVisibleModules(AccessPathTy topLevelAccessPath, Fn &&fn) {
447-
return forAllVisibleModules(topLevelAccessPath, false,
448-
std::forward<Fn>(fn));
449-
}
450-
451419
/// @}
452420

453421
using LinkLibraryCallback = llvm::function_ref<void(LinkLibrary)>;
@@ -695,23 +663,6 @@ class FileUnit : public DeclContext {
695663
bool
696664
forAllVisibleModules(llvm::function_ref<bool(ModuleDecl::ImportedModule)> fn);
697665

698-
bool
699-
forAllVisibleModules(llvm::function_ref<void(ModuleDecl::ImportedModule)> fn) {
700-
return forAllVisibleModules([=](ModuleDecl::ImportedModule import) -> bool {
701-
fn(import);
702-
return true;
703-
});
704-
}
705-
706-
template <typename Fn>
707-
bool forAllVisibleModules(Fn &&fn) {
708-
using RetTy = typename std::result_of<Fn(ModuleDecl::ImportedModule)>::type;
709-
llvm::function_ref<RetTy(ModuleDecl::ImportedModule)> wrapped{
710-
std::forward<Fn>(fn)
711-
};
712-
return forAllVisibleModules(wrapped);
713-
}
714-
715666
/// @}
716667

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

include/swift/Serialization/ModuleFile.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,6 @@ class ModuleFile
156156
bool isHeader() const { return IsHeader; }
157157
bool isScoped() const { return IsScoped; }
158158

159-
void forceExported() { IsExported = true; }
160-
161159
std::string getPrettyPrintedPath() const;
162160
};
163161

@@ -399,12 +397,6 @@ class ModuleFile
399397
/// Whether this module file comes from a framework.
400398
unsigned IsFramework : 1;
401399

402-
/// THIS SETTING IS OBSOLETE BUT IS STILL USED BY OLDER MODULES.
403-
///
404-
/// Whether this module has a shadowed module that's part of its public
405-
/// interface.
406-
unsigned HasUnderlyingModule : 1;
407-
408400
/// Whether or not ImportDecls is valid.
409401
unsigned ComputedImportDecls : 1;
410402

include/swift/Serialization/ModuleFormat.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ namespace input_block {
583583
LINK_LIBRARY,
584584
IMPORTED_HEADER,
585585
IMPORTED_HEADER_CONTENTS,
586-
MODULE_FLAGS,
586+
MODULE_FLAGS, // [unused]
587587
SEARCH_PATH
588588
};
589589

@@ -616,11 +616,6 @@ namespace input_block {
616616
BCBlob
617617
>;
618618

619-
using ModuleFlagsLayout = BCRecordLayout<
620-
MODULE_FLAGS,
621-
BCFixed<1> // has underlying module? [[UNUSED]]
622-
>;
623-
624619
using SearchPathLayout = BCRecordLayout<
625620
SEARCH_PATH,
626621
BCFixed<1>, // framework?

lib/AST/ASTContext.cpp

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

23542355
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 & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,40 +1127,25 @@ bool ModuleDecl::isSystemModule() const {
11271127
return false;
11281128
}
11291129

1130-
template<bool respectVisibility, typename Callback>
1131-
static bool forAllImportedModules(ModuleDecl *topLevel,
1132-
ModuleDecl::AccessPathTy thisPath,
1133-
bool includePrivateTopLevelImports,
1134-
const Callback &fn) {
1135-
using ImportedModule = ModuleDecl::ImportedModule;
1136-
using AccessPathTy = ModuleDecl::AccessPathTy;
1137-
1130+
bool ModuleDecl::forAllVisibleModules(AccessPathTy thisPath,
1131+
llvm::function_ref<bool(ImportedModule)> fn) {
11381132
llvm::SmallSet<ImportedModule, 32, ModuleDecl::OrderImportedModules> visited;
11391133
SmallVector<ImportedModule, 32> stack;
11401134

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);
1135+
getImportedModules(stack, ModuleDecl::ImportFilter::Public);
11481136

11491137
// 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));
1138+
stack.push_back(ImportedModule(thisPath, this));
11541139

11551140
while (!stack.empty()) {
11561141
auto next = stack.pop_back_val();
11571142

11581143
// Filter any whole-module imports, and skip specific-decl imports if the
11591144
// 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)) {
1145+
if (next.first.empty())
1146+
next.first = thisPath;
1147+
else if (!thisPath.empty() &&
1148+
!ModuleDecl::isSameAccessPath(next.first, thisPath)) {
11641149
// If we ever allow importing non-top-level decls, it's possible the rule
11651150
// above isn't what we want.
11661151
assert(next.first.size() == 1 && "import of non-top-level decl");
@@ -1173,22 +1158,12 @@ static bool forAllImportedModules(ModuleDecl *topLevel,
11731158
if (!fn(next))
11741159
return false;
11751160

1176-
if (respectVisibility)
1177-
next.second->getImportedModulesForLookup(stack);
1178-
else
1179-
next.second->getImportedModules(stack, filter);
1161+
next.second->getImportedModulesForLookup(stack);
11801162
}
11811163

11821164
return true;
11831165
}
11841166

1185-
bool ModuleDecl::forAllVisibleModules(AccessPathTy thisPath,
1186-
bool includePrivateTopLevelImports,
1187-
llvm::function_ref<bool(ImportedModule)> fn) {
1188-
return forAllImportedModules<true>(this, thisPath,
1189-
includePrivateTopLevelImports, fn);
1190-
}
1191-
11921167
bool FileUnit::forAllVisibleModules(
11931168
llvm::function_ref<bool(ModuleDecl::ImportedModule)> fn) {
11941169
if (!getParentModule()->forAllVisibleModules(ModuleDecl::AccessPathTy(), fn))
@@ -1215,13 +1190,15 @@ void ModuleDecl::collectLinkLibraries(LinkLibraryCallback callback) {
12151190
void
12161191
SourceFile::collectLinkLibraries(ModuleDecl::LinkLibraryCallback callback) const {
12171192

1218-
const_cast<SourceFile *>(this)->forAllVisibleModules([&](swift::ModuleDecl::ImportedModule import) {
1219-
swift::ModuleDecl *next = import.second;
1220-
if (next->getName() == getParentModule()->getName())
1221-
return;
1193+
const_cast<SourceFile *>(this)->forAllVisibleModules(
1194+
[&](swift::ModuleDecl::ImportedModule import) {
1195+
swift::ModuleDecl *next = import.second;
1196+
if (next->getName() == getParentModule()->getName())
1197+
return true;
12221198

1223-
next->collectLinkLibraries(callback);
1224-
});
1199+
next->collectLinkLibraries(callback);
1200+
return true;
1201+
});
12251202
}
12261203

12271204
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: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,7 +1577,9 @@ ModuleDecl *ClangImporter::Implementation::finishLoadingClangModule(
15771577
// FIXME: This forces the creation of wrapper modules for all imports as
15781578
// well, and may do unnecessary work.
15791579
cacheEntry.setInt(true);
1580-
result->forAllVisibleModules({}, [&](ModuleDecl::ImportedModule import) {});
1580+
result->forAllVisibleModules({}, [&](ModuleDecl::ImportedModule import) {
1581+
return true;
1582+
});
15811583
}
15821584
} else {
15831585
// Build the representation of the Clang module in Swift.
@@ -1597,7 +1599,9 @@ ModuleDecl *ClangImporter::Implementation::finishLoadingClangModule(
15971599
// Force load adapter modules for all imported modules.
15981600
// FIXME: This forces the creation of wrapper modules for all imports as
15991601
// well, and may do unnecessary work.
1600-
result->forAllVisibleModules({}, [](ModuleDecl::ImportedModule import) {});
1602+
result->forAllVisibleModules({}, [](ModuleDecl::ImportedModule import) {
1603+
return true;
1604+
});
16011605
}
16021606

16031607
if (clangModule->isSubModule()) {
@@ -3042,43 +3046,70 @@ ModuleDecl *ClangModuleUnit::getAdapterModule() const {
30423046
void ClangModuleUnit::getImportedModules(
30433047
SmallVectorImpl<ModuleDecl::ImportedModule> &imports,
30443048
ModuleDecl::ImportFilter filter) const {
3045-
if (filter != ModuleDecl::ImportFilter::Public)
3049+
switch (filter) {
3050+
case ModuleDecl::ImportFilter::All:
3051+
case ModuleDecl::ImportFilter::Private:
30463052
imports.push_back({ModuleDecl::AccessPathTy(), owner.getStdlibModule()});
3053+
break;
3054+
case ModuleDecl::ImportFilter::Public:
3055+
break;
3056+
}
30473057

30483058
SmallVector<clang::Module *, 8> imported;
30493059
if (!clangModule) {
30503060
// This is the special "imported headers" module.
3051-
if (filter != ModuleDecl::ImportFilter::Private) {
3061+
switch (filter) {
3062+
case ModuleDecl::ImportFilter::All:
3063+
case ModuleDecl::ImportFilter::Public:
30523064
imported.append(owner.ImportedHeaderExports.begin(),
30533065
owner.ImportedHeaderExports.end());
3066+
break;
3067+
3068+
case ModuleDecl::ImportFilter::Private:
3069+
break;
30543070
}
30553071

30563072
} else {
30573073
clangModule->getExportedModules(imported);
3058-
if (filter != ModuleDecl::ImportFilter::Public) {
3059-
if (filter == ModuleDecl::ImportFilter::All) {
3060-
llvm::SmallPtrSet<clang::Module *, 8> knownModules;
3061-
imported.append(clangModule->Imports.begin(), clangModule->Imports.end());
3062-
imported.erase(std::remove_if(imported.begin(), imported.end(),
3063-
[&](clang::Module *mod) -> bool {
3064-
return !knownModules.insert(mod).second;
3065-
}),
3066-
imported.end());
3067-
} else {
3068-
llvm::SmallPtrSet<clang::Module *, 8> knownModules(imported.begin(),
3069-
imported.end());
3070-
SmallVector<clang::Module *, 8> privateImports;
3071-
std::copy_if(clangModule->Imports.begin(), clangModule->Imports.end(),
3072-
std::back_inserter(privateImports), [&](clang::Module *mod) {
3073-
return knownModules.count(mod) == 0;
3074-
});
3075-
imported.swap(privateImports);
3076-
}
3074+
3075+
switch (filter) {
3076+
case ModuleDecl::ImportFilter::All: {
3077+
llvm::SmallPtrSet<clang::Module *, 8> knownModules;
3078+
imported.append(clangModule->Imports.begin(), clangModule->Imports.end());
3079+
imported.erase(std::remove_if(imported.begin(), imported.end(),
3080+
[&](clang::Module *mod) -> bool {
3081+
return !knownModules.insert(mod).second;
3082+
}),
3083+
imported.end());
30773084

30783085
// FIXME: The parent module isn't exactly a private import, but it is
30793086
// needed for link dependencies.
30803087
if (clangModule->Parent)
30813088
imported.push_back(clangModule->Parent);
3089+
3090+
break;
3091+
}
3092+
3093+
case ModuleDecl::ImportFilter::Private: {
3094+
llvm::SmallPtrSet<clang::Module *, 8> knownModules(imported.begin(),
3095+
imported.end());
3096+
SmallVector<clang::Module *, 8> privateImports;
3097+
std::copy_if(clangModule->Imports.begin(), clangModule->Imports.end(),
3098+
std::back_inserter(privateImports), [&](clang::Module *mod) {
3099+
return knownModules.count(mod) == 0;
3100+
});
3101+
imported.swap(privateImports);
3102+
3103+
// FIXME: The parent module isn't exactly a private import, but it is
3104+
// needed for link dependencies.
3105+
if (clangModule->Parent)
3106+
imported.push_back(clangModule->Parent);
3107+
3108+
break;
3109+
}
3110+
3111+
case ModuleDecl::ImportFilter::Public:
3112+
break;
30823113
}
30833114
}
30843115

lib/IDE/CodeCompletion.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3334,6 +3334,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
33343334
break;
33353335
}
33363336
}
3337+
3338+
return true;
33373339
});
33383340
return results;
33393341
}
@@ -5607,7 +5609,11 @@ void CodeCompletionCallbacksImpl::doneParsing() {
56075609

56085610
// FIXME: actually check imports.
56095611
const_cast<ModuleDecl*>(Request.TheModule)
5610-
->forAllVisibleModules({}, handleImport);
5612+
->forAllVisibleModules({},
5613+
[&](ModuleDecl::ImportedModule Import) {
5614+
handleImport(Import);
5615+
return true;
5616+
});
56115617
} else {
56125618
// Add results from current module.
56135619
Lookup.getToplevelCompletions(Request.OnlyTypes);
@@ -5621,7 +5627,11 @@ void CodeCompletionCallbacksImpl::doneParsing() {
56215627
for (auto Imported : Imports) {
56225628
ModuleDecl *TheModule = Imported.second;
56235629
ModuleDecl::AccessPathTy AccessPath = Imported.first;
5624-
TheModule->forAllVisibleModules(AccessPath, handleImport);
5630+
TheModule->forAllVisibleModules(AccessPath,
5631+
[&](ModuleDecl::ImportedModule Import) {
5632+
handleImport(Import);
5633+
return true;
5634+
});
56255635
}
56265636
}
56275637
Lookup.RequestedCachedResults.reset();

0 commit comments

Comments
 (0)