Skip to content

AST: Implement DenseMapInfo for AvailabilityDomain #78633

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
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
51 changes: 31 additions & 20 deletions include/swift/AST/AvailabilityDomain.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class AvailabilityDomain final {

private:
friend struct llvm::PointerLikeTypeTraits<AvailabilityDomain>;
friend struct llvm::DenseMapInfo<AvailabilityDomain>;

/// For a subset of domain kinds, all the information needed to represent the
/// domain can be encapsulated inline in this class.
Expand All @@ -57,7 +58,8 @@ class AvailabilityDomain final {
public:
using IntReprType = uint32_t;
enum : uintptr_t {
ReprBits = sizeof(IntReprType) * CHAR_BIT - 8,
SpareBits = 8,
ReprBits = sizeof(IntReprType) * CHAR_BIT - SpareBits,
KindShift = ReprBits - sizeof(Kind) * CHAR_BIT,
PlatformShift = KindShift - sizeof(PlatformKind) * CHAR_BIT,
};
Expand All @@ -83,7 +85,8 @@ class AvailabilityDomain final {
/// defined availability domains.
using ExternalDomain = void;

using InlineDomainPtr = llvm::PointerEmbeddedInt<uint32_t, InlineDomain::ReprBits>;
using InlineDomainPtr =
llvm::PointerEmbeddedInt<uint32_t, InlineDomain::ReprBits>;
using Storage = llvm::PointerUnion<ExternalDomain *, InlineDomainPtr>;
Storage storage;

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

AvailabilityDomain(void *opaque)
: storage(Storage::getFromOpaqueValue(opaque)) {};
AvailabilityDomain(Storage storage) : storage(storage) {};

static AvailabilityDomain fromOpaque(void *opaque) {
return AvailabilityDomain(Storage::getFromOpaqueValue(opaque));
}

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

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

bool operator==(const AvailabilityDomain &other) const {
if (getKind() != other.getKind())
return false;

switch (getKind()) {
case Kind::Universal:
case Kind::SwiftLanguage:
case Kind::PackageDescription:
// These availability domains are singletons.
return true;
case Kind::Platform:
return getPlatformKind() == other.getPlatformKind();
}
return storage.getOpaqueValue() == other.storage.getOpaqueValue();
}

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

namespace llvm {
using swift::AvailabilityDomain;

// An AvailabilityDomain is "pointer like".
template <typename T>
struct PointerLikeTypeTraits;
template <>
struct PointerLikeTypeTraits<swift::AvailabilityDomain> {
public:
static inline void *getAsVoidPointer(swift::AvailabilityDomain domain) {
static inline void *getAsVoidPointer(AvailabilityDomain domain) {
return domain.storage.getOpaqueValue();
}
static inline swift::AvailabilityDomain getFromVoidPointer(void *P) {
return swift::AvailabilityDomain(P);
return AvailabilityDomain::fromOpaque(P);
}
enum {
NumLowBitsAvailable = PointerLikeTypeTraits<
swift::AvailabilityDomain::Storage>::NumLowBitsAvailable
NumLowBitsAvailable =
PointerLikeTypeTraits<AvailabilityDomain::Storage>::NumLowBitsAvailable
};
};

template <>
struct DenseMapInfo<AvailabilityDomain> {
static inline AvailabilityDomain getEmptyKey() {
return DenseMapInfo<AvailabilityDomain::Storage>::getEmptyKey();
}
static inline AvailabilityDomain getTombstoneKey() {
return DenseMapInfo<AvailabilityDomain::Storage>::getTombstoneKey();
}
static bool isEqual(const AvailabilityDomain LHS,
const AvailabilityDomain RHS) {
return LHS == RHS;
}
};

} // end namespace llvm

#endif