Skip to content

Commit ad412cb

Browse files
committed
[NFC] AST: Define "repressible" known protocol.
This is the subset of suppressible protocols that are not invertible. This commit doesn't add any such protocols.
1 parent 8e7f246 commit ad412cb

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

include/swift/AST/KnownProtocols.def

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@
4949
PROTOCOL_WITH_NAME(Id, Name)
5050
#endif
5151

52+
/// \def REPRESSIBLE_PROTOCOL_WITH_NAME(id, name)
53+
/// \param id the internal "enum name" of this protocol
54+
/// \param name a string literal with the external name of this protocol
55+
#ifndef REPRESSIBLE_PROTOCOL_WITH_NAME
56+
#define REPRESSIBLE_PROTOCOL_WITH_NAME(Id, Name) \
57+
PROTOCOL_WITH_NAME(Id, Name)
58+
#endif
59+
60+
#define REPRESSIBLE_PROTOCOL(name) REPRESSIBLE_PROTOCOL_WITH_NAME(name, #name)
61+
#define REPRESSIBLE_PROTOCOL_(name) REPRESSIBLE_PROTOCOL_WITH_NAME(name, "_" #name)
5262

5363
#define PROTOCOL(name) PROTOCOL_WITH_NAME(name, #name)
5464
#define PROTOCOL_(name) PROTOCOL_WITH_NAME(name, "_" #name)
@@ -169,6 +179,9 @@ BUILTIN_EXPRESSIBLE_BY_LITERAL_PROTOCOL_(ExpressibleByBuiltinUnicodeScalarLitera
169179
#undef EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME
170180
#undef BUILTIN_EXPRESSIBLE_BY_LITERAL_PROTOCOL_
171181
#undef BUILTIN_EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME
182+
#undef REPRESSIBLE_PROTOCOL_WITH_NAME
183+
#undef REPRESSIBLE_PROTOCOL
184+
#undef REPRESSIBLE_PROTOCOL_
172185
#undef INVERTIBLE_PROTOCOL_WITH_NAME
173186
#undef PROTOCOL
174187
#undef PROTOCOL_

include/swift/AST/KnownProtocols.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ enum class KnownProtocolKind : uint8_t {
2828
#include "swift/AST/KnownProtocols.def"
2929
};
3030

31+
enum class RepressibleProtocolKind : uint8_t {
32+
#define REPRESSIBLE_PROTOCOL_WITH_NAME(Id, Name) Id,
33+
#include "swift/AST/KnownProtocols.def"
34+
};
35+
3136
enum : uint8_t {
3237
// This uses a preprocessor trick to count all the protocols. The enum value
3338
// expression below expands to "+1+1+1...". (Note that the first plus
@@ -44,6 +49,14 @@ enum : unsigned { NumKnownProtocolKindBits =
4449
/// Retrieve the name of the given known protocol.
4550
llvm::StringRef getProtocolName(KnownProtocolKind kind);
4651

52+
std::optional<RepressibleProtocolKind>
53+
getRepressibleProtocolKind(KnownProtocolKind kp);
54+
55+
KnownProtocolKind getKnownProtocolKind(RepressibleProtocolKind ip);
56+
57+
void simple_display(llvm::raw_ostream &out,
58+
const RepressibleProtocolKind &value);
59+
4760
/// MARK: Invertible protocols
4861
///
4962
/// The invertible protocols are a subset of the known protocols.
@@ -71,4 +84,27 @@ void simple_display(llvm::raw_ostream &out,
7184

7285
} // end namespace swift
7386

87+
namespace llvm {
88+
template <typename T, typename Enable>
89+
struct DenseMapInfo;
90+
template <>
91+
struct DenseMapInfo<swift::RepressibleProtocolKind> {
92+
using RepressibleProtocolKind = swift::RepressibleProtocolKind;
93+
using Impl = DenseMapInfo<uint8_t>;
94+
static inline RepressibleProtocolKind getEmptyKey() {
95+
return (RepressibleProtocolKind)Impl::getEmptyKey();
96+
}
97+
static inline RepressibleProtocolKind getTombstoneKey() {
98+
return (RepressibleProtocolKind)Impl::getTombstoneKey();
99+
}
100+
static unsigned getHashValue(const RepressibleProtocolKind &Val) {
101+
return Impl::getHashValue((uint8_t)Val);
102+
}
103+
static bool isEqual(const RepressibleProtocolKind &LHS,
104+
const RepressibleProtocolKind &RHS) {
105+
return LHS == RHS;
106+
}
107+
};
108+
} // namespace llvm
109+
74110
#endif

lib/AST/ASTContext.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,33 @@ void swift::simple_display(llvm::raw_ostream &out,
130130
out << getProtocolName(getKnownProtocolKind(value));
131131
}
132132

133+
std::optional<RepressibleProtocolKind>
134+
swift::getRepressibleProtocolKind(KnownProtocolKind kp) {
135+
switch (kp) {
136+
#define REPRESSIBLE_PROTOCOL_WITH_NAME(Id, Name) \
137+
case KnownProtocolKind::Id: \
138+
return RepressibleProtocolKind::Id;
139+
#include "swift/AST/KnownProtocols.def"
140+
default:
141+
return std::nullopt;
142+
}
143+
}
144+
145+
/// Returns the KnownProtocolKind corresponding to an RepressibleProtocolKind.
146+
KnownProtocolKind swift::getKnownProtocolKind(RepressibleProtocolKind ip) {
147+
switch (ip) {
148+
#define REPRESSIBLE_PROTOCOL_WITH_NAME(Id, Name) \
149+
case RepressibleProtocolKind::Id: \
150+
return KnownProtocolKind::Id;
151+
#include "swift/AST/KnownProtocols.def"
152+
}
153+
}
154+
155+
void swift::simple_display(llvm::raw_ostream &out,
156+
const RepressibleProtocolKind &value) {
157+
out << getProtocolName(getKnownProtocolKind(value));
158+
}
159+
133160
// Metadata stores a 16-bit field for invertible protocols. Trigger a build
134161
// error when we assign the 15th bit so we can think about what to do.
135162
#define INVERTIBLE_PROTOCOL(Name, Bit) \

0 commit comments

Comments
 (0)