123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- #pragma once
- #ifdef __GNUC__
- #pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wunused-parameter"
- #endif
- #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_if_present<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
- #ifdef __GNUC__
- #pragma GCC diagnostic pop
- #endif
|