Skip to content

Give OptionSet an initializer_list constructor #32447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions include/swift/Basic/OptionSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <type_traits>
#include <cstdint>
#include <initializer_list>

namespace swift {

Expand Down Expand Up @@ -58,6 +59,10 @@ class OptionSet {
/// Create an option set with only the given option set.
constexpr OptionSet(Flags flag) : Storage(static_cast<StorageType>(flag)) {}

/// Create an option set containing the given options.
constexpr OptionSet(std::initializer_list<Flags> flags)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constructor cannot be constexpr unless combineFlags is constexpr.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, of course. Done.

I'm surprised the compiler didn't complain about this?

: Storage(combineFlags(flags)) {}

/// Create an option set from raw storage.
explicit constexpr OptionSet(StorageType storage) : Storage(storage) {}

Expand Down Expand Up @@ -136,6 +141,14 @@ class OptionSet {

static void _checkResultTypeOperatorOr(...) {}

static constexpr StorageType
combineFlags(const std::initializer_list<Flags> &flags) {
OptionSet result;
for (Flags flag : flags)
result |= flag;
return result.Storage;
}

static_assert(!std::is_same<decltype(_checkResultTypeOperatorOr(Flags())),
Flags>::value,
"operator| should produce an OptionSet");
Expand Down
18 changes: 8 additions & 10 deletions lib/AST/ImportCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,10 @@ ImportSet &ImportCache::getImportSet(const DeclContext *dc) {
ModuleDecl::ImportedModule{ModuleDecl::AccessPathTy(), mod});

if (file) {
ModuleDecl::ImportFilter importFilter;
importFilter |= ModuleDecl::ImportFilterKind::Private;
importFilter |= ModuleDecl::ImportFilterKind::ImplementationOnly;
importFilter |= ModuleDecl::ImportFilterKind::SPIAccessControl;
file->getImportedModules(imports, importFilter);
file->getImportedModules(imports,
{ModuleDecl::ImportFilterKind::Private,
ModuleDecl::ImportFilterKind::ImplementationOnly,
ModuleDecl::ImportFilterKind::SPIAccessControl});
}

auto &result = getImportSet(ctx, imports);
Expand Down Expand Up @@ -265,11 +264,10 @@ ImportCache::getAllAccessPathsNotShadowedBy(const ModuleDecl *mod,
ModuleDecl::ImportedModule{ModuleDecl::AccessPathTy(), currentMod});

if (auto *file = dyn_cast<FileUnit>(dc)) {
ModuleDecl::ImportFilter importFilter;
importFilter |= ModuleDecl::ImportFilterKind::Private;
importFilter |= ModuleDecl::ImportFilterKind::ImplementationOnly;
importFilter |= ModuleDecl::ImportFilterKind::SPIAccessControl;
file->getImportedModules(stack, importFilter);
file->getImportedModules(stack,
{ModuleDecl::ImportFilterKind::Private,
ModuleDecl::ImportFilterKind::ImplementationOnly,
ModuleDecl::ImportFilterKind::SPIAccessControl});
}

SmallVector<ModuleDecl::AccessPathTy, 4> accessPaths;
Expand Down
10 changes: 4 additions & 6 deletions lib/Frontend/ModuleInterfaceSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,11 @@ static void printImports(raw_ostream &out,
ModuleDecl *M) {
// FIXME: This is very similar to what's in Serializer::writeInputBlock, but
// it's not obvious what higher-level optimization would be factored out here.
ModuleDecl::ImportFilter allImportFilter;
allImportFilter |= ModuleDecl::ImportFilterKind::Public;
allImportFilter |= ModuleDecl::ImportFilterKind::Private;
allImportFilter |= ModuleDecl::ImportFilterKind::SPIAccessControl;

SmallVector<ModuleDecl::ImportedModule, 8> allImports;
M->getImportedModules(allImports, allImportFilter);
M->getImportedModules(allImports,
{ModuleDecl::ImportFilterKind::Public,
ModuleDecl::ImportFilterKind::Private,
ModuleDecl::ImportFilterKind::SPIAccessControl});
ModuleDecl::removeDuplicateImports(allImports);
diagnoseScopedImports(M->getASTContext().Diags, allImports);

Expand Down
11 changes: 4 additions & 7 deletions lib/FrontendTool/ImportedModules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,12 @@ bool swift::emitImportedModules(ASTContext &Context, ModuleDecl *mainModule,
StringRef implicitHeaderPath = opts.ImplicitObjCHeaderPath;
if (!implicitHeaderPath.empty()) {
if (!clangImporter->importBridgingHeader(implicitHeaderPath, mainModule)) {
ModuleDecl::ImportFilter importFilter;
importFilter |= ModuleDecl::ImportFilterKind::Public;
importFilter |= ModuleDecl::ImportFilterKind::Private;
importFilter |= ModuleDecl::ImportFilterKind::ImplementationOnly;
importFilter |= ModuleDecl::ImportFilterKind::SPIAccessControl;

SmallVector<ModuleDecl::ImportedModule, 16> imported;
clangImporter->getImportedHeaderModule()->getImportedModules(
imported, importFilter);
imported, {ModuleDecl::ImportFilterKind::Public,
ModuleDecl::ImportFilterKind::Private,
ModuleDecl::ImportFilterKind::ImplementationOnly,
ModuleDecl::ImportFilterKind::SPIAccessControl});

for (auto IM : imported) {
if (auto clangModule = IM.importedModule->findUnderlyingClangModule())
Expand Down
21 changes: 9 additions & 12 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2081,15 +2081,13 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
}

void collectImportedModules(llvm::StringSet<> &ImportedModules) {
ModuleDecl::ImportFilter ImportFilter;
ImportFilter |= ModuleDecl::ImportFilterKind::Public;
ImportFilter |= ModuleDecl::ImportFilterKind::Private;
ImportFilter |= ModuleDecl::ImportFilterKind::ImplementationOnly;

SmallVector<ModuleDecl::ImportedModule, 16> Imported;
SmallVector<ModuleDecl::ImportedModule, 16> FurtherImported;
CurrDeclContext->getParentSourceFile()->getImportedModules(Imported,
ImportFilter);
CurrDeclContext->getParentSourceFile()->getImportedModules(
Imported,
{ModuleDecl::ImportFilterKind::Public,
ModuleDecl::ImportFilterKind::Private,
ModuleDecl::ImportFilterKind::ImplementationOnly});
while (!Imported.empty()) {
ModuleDecl *MD = Imported.back().importedModule;
Imported.pop_back();
Expand Down Expand Up @@ -6323,13 +6321,12 @@ void CodeCompletionCallbacksImpl::doneParsing() {
Lookup.addModuleName(curModule);

// Add results for all imported modules.
ModuleDecl::ImportFilter ImportFilter;
ImportFilter |= ModuleDecl::ImportFilterKind::Public;
ImportFilter |= ModuleDecl::ImportFilterKind::Private;
ImportFilter |= ModuleDecl::ImportFilterKind::ImplementationOnly;
SmallVector<ModuleDecl::ImportedModule, 4> Imports;
auto *SF = CurDeclContext->getParentSourceFile();
SF->getImportedModules(Imports, ImportFilter);
SF->getImportedModules(
Imports, {ModuleDecl::ImportFilterKind::Public,
ModuleDecl::ImportFilterKind::Private,
ModuleDecl::ImportFilterKind::ImplementationOnly});

for (auto Imported : Imports) {
for (auto Import : namelookup::getAllImports(Imported.importedModule))
Expand Down
9 changes: 4 additions & 5 deletions lib/IRGen/IRGenDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1832,12 +1832,11 @@ void IRGenDebugInfoImpl::finalize() {

// Get the list of imported modules (which may actually be different
// from all ImportDecls).
ModuleDecl::ImportFilter ImportFilter;
ImportFilter |= ModuleDecl::ImportFilterKind::Public;
ImportFilter |= ModuleDecl::ImportFilterKind::Private;
ImportFilter |= ModuleDecl::ImportFilterKind::ImplementationOnly;
SmallVector<ModuleDecl::ImportedModule, 8> ModuleWideImports;
IGM.getSwiftModule()->getImportedModules(ModuleWideImports, ImportFilter);
IGM.getSwiftModule()->getImportedModules(
ModuleWideImports, {ModuleDecl::ImportFilterKind::Public,
ModuleDecl::ImportFilterKind::Private,
ModuleDecl::ImportFilterKind::ImplementationOnly});
for (auto M : ModuleWideImports)
if (!ImportedModules.count(M.importedModule))
DBuilder.createImportedModule(MainFile, getOrCreateModule(M), MainFile,
Expand Down
8 changes: 4 additions & 4 deletions lib/Index/Index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ class SourceFileOrModule {

void
getImportedModules(SmallVectorImpl<ModuleDecl::ImportedModule> &Modules) const {
ModuleDecl::ImportFilter ImportFilter;
ImportFilter |= ModuleDecl::ImportFilterKind::Public;
ImportFilter |= ModuleDecl::ImportFilterKind::Private;
ImportFilter |= ModuleDecl::ImportFilterKind::ImplementationOnly;
constexpr ModuleDecl::ImportFilter ImportFilter = {
ModuleDecl::ImportFilterKind::Public,
ModuleDecl::ImportFilterKind::Private,
ModuleDecl::ImportFilterKind::ImplementationOnly};

if (auto *SF = SFOrMod.dyn_cast<SourceFile *>()) {
SF->getImportedModules(Modules, ImportFilter);
Expand Down
16 changes: 6 additions & 10 deletions lib/Index/IndexRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,11 +580,9 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
unitWriter.addRecordFile(recordFile, *FE, isSystemModule, mod);
}

ModuleDecl::ImportFilter importFilter;
importFilter |= ModuleDecl::ImportFilterKind::Public;
importFilter |= ModuleDecl::ImportFilterKind::Private;
SmallVector<ModuleDecl::ImportedModule, 8> imports;
module->getImportedModules(imports, importFilter);
module->getImportedModules(imports, {ModuleDecl::ImportFilterKind::Public,
ModuleDecl::ImportFilterKind::Private});
StringScratchSpace moduleNameScratch;
addModuleDependencies(imports, indexStorePath, indexSystemModules, skipStdlib,
targetTriple, clangCI, diags, unitWriter,
Expand Down Expand Up @@ -621,13 +619,11 @@ recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
getModuleInfoFromOpaqueModule);

// Module dependencies.
ModuleDecl::ImportFilter importFilter;
importFilter |= ModuleDecl::ImportFilterKind::Public;
importFilter |= ModuleDecl::ImportFilterKind::Private;
importFilter |= ModuleDecl::ImportFilterKind::ImplementationOnly;

SmallVector<ModuleDecl::ImportedModule, 8> imports;
primarySourceFile->getImportedModules(imports, importFilter);
primarySourceFile->getImportedModules(
imports, {ModuleDecl::ImportFilterKind::Public,
ModuleDecl::ImportFilterKind::Private,
ModuleDecl::ImportFilterKind::ImplementationOnly});
StringScratchSpace moduleNameScratch;
addModuleDependencies(imports, indexStorePath, indexSystemModules, skipStdlib,
targetTriple, clangCI, diags, unitWriter,
Expand Down
11 changes: 5 additions & 6 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1015,13 +1015,12 @@ void Serializer::writeInputBlock(const SerializationOptions &options) {
if (!options.ModuleInterface.empty())
ModuleInterface.emit(ScratchRecord, options.ModuleInterface);

ModuleDecl::ImportFilter allImportFilter;
allImportFilter |= ModuleDecl::ImportFilterKind::Public;
allImportFilter |= ModuleDecl::ImportFilterKind::Private;
allImportFilter |= ModuleDecl::ImportFilterKind::ImplementationOnly;
allImportFilter |= ModuleDecl::ImportFilterKind::SPIAccessControl;
SmallVector<ModuleDecl::ImportedModule, 8> allImports;
M->getImportedModules(allImports, allImportFilter);
M->getImportedModules(allImports,
{ModuleDecl::ImportFilterKind::Public,
ModuleDecl::ImportFilterKind::Private,
ModuleDecl::ImportFilterKind::ImplementationOnly,
ModuleDecl::ImportFilterKind::SPIAccessControl});
ModuleDecl::removeDuplicateImports(allImports);

// Collect the public and private imports as a subset so that we can
Expand Down
7 changes: 2 additions & 5 deletions lib/Serialization/SerializedModuleLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1085,12 +1085,9 @@ void SerializedASTFile::getImportedModules(

void SerializedASTFile::collectLinkLibrariesFromImports(
ModuleDecl::LinkLibraryCallback callback) const {
ModuleDecl::ImportFilter ImportFilter;
ImportFilter |= ModuleDecl::ImportFilterKind::Public;
ImportFilter |= ModuleDecl::ImportFilterKind::Private;

llvm::SmallVector<ModuleDecl::ImportedModule, 8> Imports;
File.getImportedModules(Imports, ImportFilter);
File.getImportedModules(Imports, {ModuleDecl::ImportFilterKind::Public,
ModuleDecl::ImportFilterKind::Private});

for (auto Import : Imports)
Import.importedModule->collectLinkLibraries(callback);
Expand Down
7 changes: 3 additions & 4 deletions tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,11 @@ ImportDepth::ImportDepth(ASTContext &context,

// Private imports from this module.
// FIXME: only the private imports from the current source file.
ModuleDecl::ImportFilter importFilter;
importFilter |= ModuleDecl::ImportFilterKind::Private;
importFilter |= ModuleDecl::ImportFilterKind::ImplementationOnly;
// FIXME: ImportFilterKind::ShadowedBySeparateOverlay?
SmallVector<ModuleDecl::ImportedModule, 16> mainImports;
main->getImportedModules(mainImports, importFilter);
main->getImportedModules(mainImports,
{ModuleDecl::ImportFilterKind::Private,
ModuleDecl::ImportFilterKind::ImplementationOnly});
for (auto &import : mainImports) {
uint8_t depth = 1;
if (auxImports.count(import.importedModule->getName().str()))
Expand Down
6 changes: 3 additions & 3 deletions tools/SourceKit/lib/SwiftLang/SwiftASTManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -907,9 +907,9 @@ static void collectModuleDependencies(ModuleDecl *TopMod,

auto ClangModuleLoader = TopMod->getASTContext().getClangModuleLoader();

ModuleDecl::ImportFilter ImportFilter;
ImportFilter |= ModuleDecl::ImportFilterKind::Public;
ImportFilter |= ModuleDecl::ImportFilterKind::Private;
ModuleDecl::ImportFilter ImportFilter = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't work because ImportFilter is modified below.

ModuleDecl::ImportFilterKind::Public,
ModuleDecl::ImportFilterKind::Private};
if (Visited.empty()) {
// Only collect implementation-only dependencies from the main module.
ImportFilter |= ModuleDecl::ImportFilterKind::ImplementationOnly;
Expand Down