#ifndef LLVM_ADT_POINTERUNION_H
#define LLVM_ADT_POINTERUNION_H
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/PointerLikeTypeTraits.h"
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
namespace llvm {
namespace pointer_union_detail {
constexpr int bitsRequired(unsigned n) {
return n > 1 ? 1 + bitsRequired((n + 1) / 2) : 0;
}
template <typename... Ts> constexpr int lowBitsAvailable() {
return std::min<int>({PointerLikeTypeTraits<Ts>::NumLowBitsAvailable...});
}
template <typename T, typename...> struct GetFirstType {
using type = T;
};
template <typename ...PTs> class PointerUnionUIntTraits {
public:
static inline void *getAsVoidPointer(void *P) { return P; }
static inline void *getFromVoidPointer(void *P) { return P; }
static constexpr int NumLowBitsAvailable = lowBitsAvailable<PTs...>();
};
template <typename Derived, typename ValTy, int I, typename ...Types>
class PointerUnionMembers;
template <typename Derived, typename ValTy, int I>
class PointerUnionMembers<Derived, ValTy, I> {
protected:
ValTy Val;
PointerUnionMembers() = default;
PointerUnionMembers(ValTy Val) : Val(Val) {}
friend struct PointerLikeTypeTraits<Derived>;
};
template <typename Derived, typename ValTy, int I, typename Type,
typename ...Types>
class PointerUnionMembers<Derived, ValTy, I, Type, Types...>
: public PointerUnionMembers<Derived, ValTy, I + 1, Types...> {
using Base = PointerUnionMembers<Derived, ValTy, I + 1, Types...>;
public:
using Base::Base;
PointerUnionMembers() = default;
PointerUnionMembers(Type V)
: Base(ValTy(const_cast<void *>(
PointerLikeTypeTraits<Type>::getAsVoidPointer(V)),
I)) {}
using Base::operator=;
Derived &operator=(Type V) {
this->Val = ValTy(
const_cast<void *>(PointerLikeTypeTraits<Type>::getAsVoidPointer(V)),
I);
return static_cast<Derived &>(*this);
};
};
}
template <typename... PTs> struct CastInfoPointerUnionImpl;
template <typename... PTs>
class PointerUnion
: public pointer_union_detail::PointerUnionMembers<
PointerUnion<PTs...>,
PointerIntPair<
void *, pointer_union_detail::bitsRequired(sizeof...(PTs)), int,
pointer_union_detail::PointerUnionUIntTraits<PTs...>>,
0, PTs...> {
static_assert(TypesAreDistinct<PTs...>::value,
"PointerUnion alternative types cannot be repeated");
using First = TypeAtIndex<0, PTs...>;
using Base = typename PointerUnion::PointerUnionMembers;
friend struct CastInfoPointerUnionImpl<PTs...>;
public:
PointerUnion() = default;
PointerUnion(std::nullptr_t) : PointerUnion() {}
using Base::Base;
bool isNull() const { return !this->Val.getPointer(); }
explicit operator bool() const { return !isNull(); }
template <typename T> inline bool is() const { return isa<T>(*this); }
template <typename T> inline T get() const {
assert(isa<T>(*this) && "Invalid accessor called");
return cast<T>(*this);
}
template <typename T> inline T dyn_cast() const {
return llvm::dyn_cast<T>(*this);
}
First const *getAddrOfPtr1() const {
return const_cast<PointerUnion *>(this)->getAddrOfPtr1();
}
First *getAddrOfPtr1() {
assert(is<First>() && "Val is not the first pointer");
assert(
PointerLikeTypeTraits<First>::getAsVoidPointer(get<First>()) ==
this->Val.getPointer() &&
"Can't get the address because PointerLikeTypeTraits changes the ptr");
return const_cast<First *>(
reinterpret_cast<const First *>(this->Val.getAddrOfPointer()));
}
const PointerUnion &operator=(std::nullptr_t) {
this->Val.initWithPointer(nullptr);
return *this;
}
using Base::operator=;
void *getOpaqueValue() const { return this->Val.getOpaqueValue(); }
static inline PointerUnion getFromOpaqueValue(void *VP) {
PointerUnion V;
V.Val = decltype(V.Val)::getFromOpaqueValue(VP);
return V;
}
};
template <typename ...PTs>
bool operator==(PointerUnion<PTs...> lhs, PointerUnion<PTs...> rhs) {
return lhs.getOpaqueValue() == rhs.getOpaqueValue();
}
template <typename ...PTs>
bool operator!=(PointerUnion<PTs...> lhs, PointerUnion<PTs...> rhs) {
return lhs.getOpaqueValue() != rhs.getOpaqueValue();
}
template <typename ...PTs>
bool operator<(PointerUnion<PTs...> lhs, PointerUnion<PTs...> rhs) {
return lhs.getOpaqueValue() < rhs.getOpaqueValue();
}
template <typename... PTs> struct CastInfoPointerUnionImpl {
using From = PointerUnion<PTs...>;
template <typename To> static inline bool isPossible(From &F) {
return F.Val.getInt() == FirstIndexOfType<To, PTs...>::value;
}
template <typename To> static To doCast(From &F) {
assert(isPossible<To>(F) && "cast to an incompatible type !");
return PointerLikeTypeTraits<To>::getFromVoidPointer(F.Val.getPointer());
}
};
template <typename To, typename... PTs>
struct CastInfo<To, PointerUnion<PTs...>>
: public DefaultDoCastIfPossible<To, PointerUnion<PTs...>,
CastInfo<To, PointerUnion<PTs...>>> {
using From = PointerUnion<PTs...>;
using Impl = CastInfoPointerUnionImpl<PTs...>;
static inline bool isPossible(From &f) {
return Impl::template isPossible<To>(f);
}
static To doCast(From &f) { return Impl::template doCast<To>(f); }
static inline To castFailed() { return To(); }
};
template <typename To, typename... PTs>
struct CastInfo<To, const PointerUnion<PTs...>>
: public ConstStrippingForwardingCast<To, const PointerUnion<PTs...>,
CastInfo<To, PointerUnion<PTs...>>> {
};
template <typename ...PTs>
struct PointerLikeTypeTraits<PointerUnion<PTs...>> {
static inline void *getAsVoidPointer(const PointerUnion<PTs...> &P) {
return P.getOpaqueValue();
}
static inline PointerUnion<PTs...> getFromVoidPointer(void *P) {
return PointerUnion<PTs...>::getFromOpaqueValue(P);
}
static constexpr int NumLowBitsAvailable = PointerLikeTypeTraits<decltype(
PointerUnion<PTs...>::Val)>::NumLowBitsAvailable;
};
template <typename ...PTs> struct DenseMapInfo<PointerUnion<PTs...>> {
using Union = PointerUnion<PTs...>;
using FirstInfo =
DenseMapInfo<typename pointer_union_detail::GetFirstType<PTs...>::type>;
static inline Union getEmptyKey() { return Union(FirstInfo::getEmptyKey()); }
static inline Union getTombstoneKey() {
return Union(FirstInfo::getTombstoneKey());
}
static unsigned getHashValue(const Union &UnionVal) {
intptr_t key = (intptr_t)UnionVal.getOpaqueValue();
return DenseMapInfo<intptr_t>::getHashValue(key);
}
static bool isEqual(const Union &LHS, const Union &RHS) {
return LHS == RHS;
}
};
}
#endif