Use SuppressibleProtocolSet as InvertibleProtocolSet

Collapse the representations of "suppressible" and "invertible"
protocol sets. Only minor adjustments were required.
This commit is contained in:
Doug Gregor
2024-03-29 09:05:28 -07:00
parent 8b22079291
commit 79b78acdf6
13 changed files with 35 additions and 53 deletions

View File

@@ -19,6 +19,7 @@
#define SWIFT_ABI_SUPPRESSIBLEPROTOCOLS_H
#include <cstdint>
#include <initializer_list>
#include <iterator>
namespace swift {
@@ -30,6 +31,8 @@ enum class SuppressibleProtocolKind : uint8_t {
#include "swift/ABI/SuppressibleProtocols.def"
};
typedef SuppressibleProtocolKind InvertibleProtocolKind;
/// A set of suppressible protocols, whose bits correspond to the cases of
/// SuppressibleProtocolKind.
class SuppressibleProtocolSet {
@@ -47,6 +50,13 @@ public:
explicit constexpr SuppressibleProtocolSet(StorageType bits) : bits(bits) {}
constexpr SuppressibleProtocolSet() : bits(0) {}
SuppressibleProtocolSet(
std::initializer_list<SuppressibleProtocolKind> elements
) : bits(0) {
for (auto element : elements)
insert(element);
}
/// Retrieve the raw bits that describe this set.
StorageType rawBits() const { return bits; }
@@ -63,6 +73,12 @@ public:
bits = bits | getMask(kind);
}
/// Insert all of the suppressible protocols from the other set into this
/// one.
void insertAll(SuppressibleProtocolSet other) {
bits |= other.bits;
}
/// Remove the given suppressible protocol from the set.
void remove(SuppressibleProtocolKind kind) {
uint16_t mask = getMask(kind);

View File

@@ -1,30 +0,0 @@
//===--- InvertibleProtocolKind.h - -----------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This header declares the InvertibleProtocolKind enum and some
// related operations.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_AST_INVERTIBLEPROTOCOLKIND_H
#define SWIFT_AST_INVERTIBLEPROTOCOLKIND_H
#include <stdint.h>
#include <swift/ABI/SuppressibleProtocols.h>
namespace swift {
typedef SuppressibleProtocolKind InvertibleProtocolKind;
} // end namespace swift
#endif // SWIFT_AST_INVERTIBLEPROTOCOLKIND_H

View File

@@ -14,9 +14,6 @@
#define SWIFT_AST_KNOWNPROTOCOLS_H
#include "swift/ABI/SuppressibleProtocols.h"
#include "swift/Basic/InlineBitfield.h"
#include "swift/Basic/FixedBitSet.h"
#include "swift/AST/InvertibleProtocolKind.h"
#include "swift/Config.h"
namespace llvm {
@@ -53,14 +50,13 @@ llvm::StringRef getProtocolName(KnownProtocolKind kind);
enum : uint8_t {
// Use preprocessor trick to count all the invertible protocols.
#define INVERTIBLE_PROTOCOL_WITH_NAME(Id, Name) +1
#define SUPPRESSIBLE_PROTOCOL(Name, Bit) +1
/// The number of invertible protocols.
NumInvertibleProtocols =
#include "swift/AST/KnownProtocols.def"
#include "swift/ABI/SuppressibleProtocols.def"
};
using InvertibleProtocolSet = FixedBitSet<NumInvertibleProtocols,
InvertibleProtocolKind>;
using InvertibleProtocolSet = SuppressibleProtocolSet;
/// Maps a KnownProtocol to the set of InvertibleProtocols, if a mapping exists.
/// \returns None if the known protocol is not invertible.

View File

@@ -22,7 +22,7 @@
#include "swift/Basic/LLVM.h"
#include "swift/ABI/MetadataValues.h"
#include "swift/AST/InvertibleProtocolKind.h"
#include "swift/ABI/SuppressibleProtocols.h"
#include "swift/AST/LayoutConstraintKind.h"
#include "swift/AST/RequirementKind.h"
#include "swift/Basic/OptionSet.h"

View File

@@ -7774,7 +7774,7 @@ static void getSyntacticInheritanceClause(const ProtocolDecl *proto,
/*isPreconcurrency=*/false);
}
InvertibleProtocolSet inverses = InvertibleProtocolSet::full();
InvertibleProtocolSet inverses = InvertibleProtocolSet::allKnown();
for (auto *inherited : proto->getInheritedProtocols()) {
if (auto ip = inherited->getInvertibleProtocolKind()) {
@@ -7782,7 +7782,7 @@ static void getSyntacticInheritanceClause(const ProtocolDecl *proto,
continue;
}
for (auto ip : InvertibleProtocolSet::full()) {
for (auto ip : InvertibleProtocolSet::allKnown()) {
auto *proto = ctx.getProtocol(getKnownProtocolKind(ip));
if (inherited->inheritsFrom(proto))
inverses.remove(ip);

View File

@@ -1326,7 +1326,7 @@ static GenericSignature getPlaceholderGenericSignature(
auto type = genericParam->getDeclaredInterfaceType();
genericParams.push_back(type->castTo<GenericTypeParamType>());
for (auto ip : InvertibleProtocolSet::full()) {
for (auto ip : InvertibleProtocolSet::allKnown()) {
auto proto = ctx.getProtocol(getKnownProtocolKind(ip));
requirements.emplace_back(RequirementKind::Conformance, type,
proto->getDeclaredInterfaceType());

View File

@@ -661,7 +661,7 @@ Type GenericSignatureImpl::getUpperBound(Type type,
InvertibleProtocolSet inverses;
if (!superclass && !hasExplicitAnyObject) {
for (auto ip : InvertibleProtocolSet::full()) {
for (auto ip : InvertibleProtocolSet::allKnown()) {
auto *kp = ctx.getProtocol(::getKnownProtocolKind(ip));
if (!requiresProtocol(type, kp))
inverses.insert(ip);
@@ -1257,7 +1257,7 @@ void GenericSignatureImpl::getRequirementsWithInverses(
if (getSuperclassBound(gp) || getConcreteType(gp))
continue;
for (auto ip : InvertibleProtocolSet::full()) {
for (auto ip : InvertibleProtocolSet::allKnown()) {
auto *proto = ctx.getProtocol(getKnownProtocolKind(ip));
// If we can derive a conformance to this protocol, then don't add an
@@ -1297,7 +1297,7 @@ RequirementSignature RequirementSignature::getPlaceholderRequirementSignature(
inheritedProtos.push_back(inheritedProto);
}
for (auto ip : InvertibleProtocolSet::full()) {
for (auto ip : InvertibleProtocolSet::allKnown()) {
auto *otherProto = ctx.getProtocol(getKnownProtocolKind(ip));
inheritedProtos.push_back(otherProto);
}
@@ -1313,7 +1313,7 @@ RequirementSignature RequirementSignature::getPlaceholderRequirementSignature(
}
for (auto *assocTypeDecl : proto->getAssociatedTypeMembers()) {
for (auto ip : InvertibleProtocolSet::full()) {
for (auto ip : InvertibleProtocolSet::allKnown()) {
auto *otherProto = ctx.getProtocol(getKnownProtocolKind(ip));
requirements.emplace_back(RequirementKind::Conformance,
assocTypeDecl->getDeclaredInterfaceType(),
@@ -1349,7 +1349,7 @@ void RequirementSignature::getRequirementsWithInverses(
sig->getConcreteType(interfaceType))
return;
for (auto ip : InvertibleProtocolSet::full()) {
for (auto ip : InvertibleProtocolSet::allKnown()) {
auto *proto = ctx.getProtocol(getKnownProtocolKind(ip));
// If we can derive a conformance to this protocol, then don't add an

View File

@@ -3304,7 +3304,7 @@ InheritedProtocolsRequest::evaluate(Evaluator &evaluator,
}
if (!skipInverses) {
for (auto ip : InvertibleProtocolSet::full()) {
for (auto ip : InvertibleProtocolSet::allKnown()) {
// Unless the user wrote ~P in the syntactic inheritance clause, the
// semantic inherited list includes P.
if (!inverses.contains(ip))

View File

@@ -1087,7 +1087,7 @@ void NominalTypeDecl::prepareConformanceTable() const {
inverses.insert(InvertibleProtocolKind::Escapable);
bool hasSuppressedConformances = false;
for (auto ip : InvertibleProtocolSet::full()) {
for (auto ip : InvertibleProtocolSet::allKnown()) {
if (!inverses.contains(ip) ||
(isa<ClassDecl>(this) &&
!ctx.LangOpts.hasFeature(Feature::MoveOnlyClasses))) {
@@ -1281,7 +1281,7 @@ static SmallVector<ProtocolConformance *, 2> findSynthesizedConformances(
// Triggers synthesis of a possibly conditional conformance.
// For the unconditional ones, see NominalTypeDecl::prepareConformanceTable
for (auto ip : InvertibleProtocolSet::full())
for (auto ip : InvertibleProtocolSet::allKnown())
trySynthesize(getKnownProtocolKind(ip));
if (nominal->getASTContext().LangOpts.hasFeature(

View File

@@ -350,7 +350,7 @@ void InverseRequirement::expandDefaults(
ArrayRef<Type> gps,
SmallVectorImpl<StructuralRequirement> &result) {
for (auto gp : gps) {
for (auto ip : InvertibleProtocolSet::full()) {
for (auto ip : InvertibleProtocolSet::allKnown()) {
auto proto = ctx.getProtocol(getKnownProtocolKind(ip));
result.push_back({{RequirementKind::Conformance, gp,
proto->getDeclaredInterfaceType()},

View File

@@ -755,7 +755,7 @@ static GenericSignature getPlaceholderGenericSignature(
ASTContext &ctx, ArrayRef<GenericTypeParamType *> genericParams) {
SmallVector<Requirement, 2> requirements;
for (auto param : genericParams) {
for (auto ip : InvertibleProtocolSet::full()) {
for (auto ip : InvertibleProtocolSet::allKnown()) {
auto proto = ctx.getProtocol(getKnownProtocolKind(ip));
requirements.emplace_back(RequirementKind::Conformance, param,
proto->getDeclaredInterfaceType());

View File

@@ -281,7 +281,7 @@ bool TypeBase::allowsOwnership(const GenericSignatureImpl *sig) {
static void expandDefaults(SmallVectorImpl<ProtocolDecl *> &protocols,
InvertibleProtocolSet inverses,
ASTContext &ctx) {
for (auto ip : InvertibleProtocolSet::full()) {
for (auto ip : InvertibleProtocolSet::allKnown()) {
if (!inverses.contains(ip)) {
auto *proto = ctx.getProtocol(getKnownProtocolKind(ip));
protocols.push_back(proto);

View File

@@ -273,7 +273,7 @@ mangleProtocolForLLVMTypeName(ProtocolCompositionType *type) {
// don't start with a digit and don't need to be quoted.
Buffer << 'T';
bool isFirstItem = true;
InvertibleProtocolSet inverses = InvertibleProtocolSet::full();
InvertibleProtocolSet inverses = InvertibleProtocolSet::allKnown();
auto protocols = layout.getProtocols();
for (auto *proto : protocols) {
if (auto ip = proto->getInvertibleProtocolKind()) {