Skip to content

Commit a6c543e

Browse files
committed
AST: Implement DenseMapInfo for AvailabilityDomain.
Needed to use AvailabilityDomain as a key in an llvm::SmallDenseMap. NFC.
1 parent 4ae5685 commit a6c543e

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

include/swift/AST/AvailabilityDomain.h

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class AvailabilityDomain final {
4747

4848
private:
4949
friend struct llvm::PointerLikeTypeTraits<AvailabilityDomain>;
50+
friend struct llvm::DenseMapInfo<AvailabilityDomain>;
5051

5152
/// For a subset of domain kinds, all the information needed to represent the
5253
/// domain can be encapsulated inline in this class.
@@ -57,7 +58,8 @@ class AvailabilityDomain final {
5758
public:
5859
using IntReprType = uint32_t;
5960
enum : uintptr_t {
60-
ReprBits = sizeof(IntReprType) * CHAR_BIT - 8,
61+
SpareBits = 8,
62+
ReprBits = sizeof(IntReprType) * CHAR_BIT - SpareBits,
6163
KindShift = ReprBits - sizeof(Kind) * CHAR_BIT,
6264
PlatformShift = KindShift - sizeof(PlatformKind) * CHAR_BIT,
6365
};
@@ -83,7 +85,8 @@ class AvailabilityDomain final {
8385
/// defined availability domains.
8486
using ExternalDomain = void;
8587

86-
using InlineDomainPtr = llvm::PointerEmbeddedInt<uint32_t, InlineDomain::ReprBits>;
88+
using InlineDomainPtr =
89+
llvm::PointerEmbeddedInt<uint32_t, InlineDomain::ReprBits>;
8790
using Storage = llvm::PointerUnion<ExternalDomain *, InlineDomainPtr>;
8891
Storage storage;
8992

@@ -95,8 +98,11 @@ class AvailabilityDomain final {
9598
AvailabilityDomain(PlatformKind platform)
9699
: storage(InlineDomain(Kind::Platform, platform).asInteger()) {};
97100

98-
AvailabilityDomain(void *opaque)
99-
: storage(Storage::getFromOpaqueValue(opaque)) {};
101+
AvailabilityDomain(Storage storage) : storage(storage) {};
102+
103+
static AvailabilityDomain fromOpaque(void *opaque) {
104+
return AvailabilityDomain(Storage::getFromOpaqueValue(opaque));
105+
}
100106

101107
void *getOpaqueValue() const { return storage.getOpaqueValue(); }
102108

@@ -163,18 +169,7 @@ class AvailabilityDomain final {
163169
llvm::StringRef getNameForAttributePrinting() const;
164170

165171
bool operator==(const AvailabilityDomain &other) const {
166-
if (getKind() != other.getKind())
167-
return false;
168-
169-
switch (getKind()) {
170-
case Kind::Universal:
171-
case Kind::SwiftLanguage:
172-
case Kind::PackageDescription:
173-
// These availability domains are singletons.
174-
return true;
175-
case Kind::Platform:
176-
return getPlatformKind() == other.getPlatformKind();
177-
}
172+
return storage.getOpaqueValue() == other.storage.getOpaqueValue();
178173
}
179174

180175
bool operator!=(const AvailabilityDomain &other) const {
@@ -200,24 +195,40 @@ class AvailabilityDomain final {
200195
} // end namespace swift
201196

202197
namespace llvm {
198+
using swift::AvailabilityDomain;
199+
203200
// An AvailabilityDomain is "pointer like".
204201
template <typename T>
205202
struct PointerLikeTypeTraits;
206203
template <>
207204
struct PointerLikeTypeTraits<swift::AvailabilityDomain> {
208205
public:
209-
static inline void *getAsVoidPointer(swift::AvailabilityDomain domain) {
206+
static inline void *getAsVoidPointer(AvailabilityDomain domain) {
210207
return domain.storage.getOpaqueValue();
211208
}
212209
static inline swift::AvailabilityDomain getFromVoidPointer(void *P) {
213-
return swift::AvailabilityDomain(P);
210+
return AvailabilityDomain::fromOpaque(P);
214211
}
215212
enum {
216-
NumLowBitsAvailable = PointerLikeTypeTraits<
217-
swift::AvailabilityDomain::Storage>::NumLowBitsAvailable
213+
NumLowBitsAvailable =
214+
PointerLikeTypeTraits<AvailabilityDomain::Storage>::NumLowBitsAvailable
218215
};
219216
};
220217

218+
template <>
219+
struct DenseMapInfo<AvailabilityDomain> {
220+
static inline AvailabilityDomain getEmptyKey() {
221+
return DenseMapInfo<AvailabilityDomain::Storage>::getEmptyKey();
222+
}
223+
static inline AvailabilityDomain getTombstoneKey() {
224+
return DenseMapInfo<AvailabilityDomain::Storage>::getTombstoneKey();
225+
}
226+
static bool isEqual(const AvailabilityDomain LHS,
227+
const AvailabilityDomain RHS) {
228+
return LHS == RHS;
229+
}
230+
};
231+
221232
} // end namespace llvm
222233

223234
#endif

0 commit comments

Comments
 (0)