PointerUnion.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. //===- llvm/ADT/PointerUnion.h - Discriminated Union of 2 Ptrs --*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the PointerUnion class, which is a discriminated union of
  11. // pointer types.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_ADT_POINTERUNION_H
  15. #define LLVM_ADT_POINTERUNION_H
  16. #include "llvm/ADT/DenseMapInfo.h"
  17. #include "llvm/ADT/PointerIntPair.h"
  18. #include "llvm/Support/Compiler.h"
  19. namespace llvm {
  20. template <typename T>
  21. struct PointerUnionTypeSelectorReturn {
  22. typedef T Return;
  23. };
  24. /// \brief Get a type based on whether two types are the same or not. For:
  25. /// @code
  26. /// typedef typename PointerUnionTypeSelector<T1, T2, EQ, NE>::Return Ret;
  27. /// @endcode
  28. /// Ret will be EQ type if T1 is same as T2 or NE type otherwise.
  29. template <typename T1, typename T2, typename RET_EQ, typename RET_NE>
  30. struct PointerUnionTypeSelector {
  31. typedef typename PointerUnionTypeSelectorReturn<RET_NE>::Return Return;
  32. };
  33. template <typename T, typename RET_EQ, typename RET_NE>
  34. struct PointerUnionTypeSelector<T, T, RET_EQ, RET_NE> {
  35. typedef typename PointerUnionTypeSelectorReturn<RET_EQ>::Return Return;
  36. };
  37. template <typename T1, typename T2, typename RET_EQ, typename RET_NE>
  38. struct PointerUnionTypeSelectorReturn<
  39. PointerUnionTypeSelector<T1, T2, RET_EQ, RET_NE> > {
  40. typedef typename PointerUnionTypeSelector<T1, T2, RET_EQ, RET_NE>::Return
  41. Return;
  42. };
  43. /// Provide PointerLikeTypeTraits for void* that is used by PointerUnion
  44. /// for the two template arguments.
  45. template <typename PT1, typename PT2>
  46. class PointerUnionUIntTraits {
  47. public:
  48. static inline void *getAsVoidPointer(void *P) { return P; }
  49. static inline void *getFromVoidPointer(void *P) { return P; }
  50. enum {
  51. PT1BitsAv = (int)(PointerLikeTypeTraits<PT1>::NumLowBitsAvailable),
  52. PT2BitsAv = (int)(PointerLikeTypeTraits<PT2>::NumLowBitsAvailable),
  53. NumLowBitsAvailable = PT1BitsAv < PT2BitsAv ? PT1BitsAv : PT2BitsAv
  54. };
  55. };
  56. /// PointerUnion - This implements a discriminated union of two pointer types,
  57. /// and keeps the discriminator bit-mangled into the low bits of the pointer.
  58. /// This allows the implementation to be extremely efficient in space, but
  59. /// permits a very natural and type-safe API.
  60. ///
  61. /// Common use patterns would be something like this:
  62. /// PointerUnion<int*, float*> P;
  63. /// P = (int*)0;
  64. /// printf("%d %d", P.is<int*>(), P.is<float*>()); // prints "1 0"
  65. /// X = P.get<int*>(); // ok.
  66. /// Y = P.get<float*>(); // runtime assertion failure.
  67. /// Z = P.get<double*>(); // compile time failure.
  68. /// P = (float*)0;
  69. /// Y = P.get<float*>(); // ok.
  70. /// X = P.get<int*>(); // runtime assertion failure.
  71. template <typename PT1, typename PT2>
  72. class PointerUnion {
  73. public:
  74. typedef PointerIntPair<void*, 1, bool,
  75. PointerUnionUIntTraits<PT1,PT2> > ValTy;
  76. private:
  77. ValTy Val;
  78. struct IsPT1 {
  79. static const int Num = 0;
  80. };
  81. struct IsPT2 {
  82. static const int Num = 1;
  83. };
  84. template <typename T>
  85. struct UNION_DOESNT_CONTAIN_TYPE { };
  86. public:
  87. PointerUnion() {}
  88. PointerUnion(PT1 V) : Val(
  89. const_cast<void *>(PointerLikeTypeTraits<PT1>::getAsVoidPointer(V))) {
  90. }
  91. PointerUnion(PT2 V) : Val(
  92. const_cast<void *>(PointerLikeTypeTraits<PT2>::getAsVoidPointer(V)), 1) {
  93. }
  94. /// isNull - Return true if the pointer held in the union is null,
  95. /// regardless of which type it is.
  96. bool isNull() const {
  97. // Convert from the void* to one of the pointer types, to make sure that
  98. // we recursively strip off low bits if we have a nested PointerUnion.
  99. return !PointerLikeTypeTraits<PT1>::getFromVoidPointer(Val.getPointer());
  100. }
  101. explicit operator bool() const { return !isNull(); }
  102. /// is<T>() return true if the Union currently holds the type matching T.
  103. template<typename T>
  104. int is() const {
  105. typedef typename
  106. ::llvm::PointerUnionTypeSelector<PT1, T, IsPT1,
  107. ::llvm::PointerUnionTypeSelector<PT2, T, IsPT2,
  108. UNION_DOESNT_CONTAIN_TYPE<T> > >::Return Ty;
  109. int TyNo = Ty::Num;
  110. return static_cast<int>(Val.getInt()) == TyNo;
  111. }
  112. /// get<T>() - Return the value of the specified pointer type. If the
  113. /// specified pointer type is incorrect, assert.
  114. template<typename T>
  115. T get() const {
  116. assert(is<T>() && "Invalid accessor called");
  117. return PointerLikeTypeTraits<T>::getFromVoidPointer(Val.getPointer());
  118. }
  119. /// dyn_cast<T>() - If the current value is of the specified pointer type,
  120. /// return it, otherwise return null.
  121. template<typename T>
  122. T dyn_cast() const {
  123. if (is<T>()) return get<T>();
  124. return T();
  125. }
  126. /// \brief If the union is set to the first pointer type get an address
  127. /// pointing to it.
  128. PT1 const *getAddrOfPtr1() const {
  129. return const_cast<PointerUnion *>(this)->getAddrOfPtr1();
  130. }
  131. /// \brief If the union is set to the first pointer type get an address
  132. /// pointing to it.
  133. PT1 *getAddrOfPtr1() {
  134. assert(is<PT1>() && "Val is not the first pointer");
  135. assert(get<PT1>() == Val.getPointer() &&
  136. "Can't get the address because PointerLikeTypeTraits changes the ptr");
  137. return (PT1 *)Val.getAddrOfPointer();
  138. }
  139. /// \brief Assignment from nullptr which just clears the union.
  140. const PointerUnion &operator=(std::nullptr_t) {
  141. Val.initWithPointer(nullptr);
  142. return *this;
  143. }
  144. /// Assignment operators - Allow assigning into this union from either
  145. /// pointer type, setting the discriminator to remember what it came from.
  146. const PointerUnion &operator=(const PT1 &RHS) {
  147. Val.initWithPointer(
  148. const_cast<void *>(PointerLikeTypeTraits<PT1>::getAsVoidPointer(RHS)));
  149. return *this;
  150. }
  151. const PointerUnion &operator=(const PT2 &RHS) {
  152. Val.setPointerAndInt(
  153. const_cast<void *>(PointerLikeTypeTraits<PT2>::getAsVoidPointer(RHS)),
  154. 1);
  155. return *this;
  156. }
  157. void *getOpaqueValue() const { return Val.getOpaqueValue(); }
  158. static inline PointerUnion getFromOpaqueValue(void *VP) {
  159. PointerUnion V;
  160. V.Val = ValTy::getFromOpaqueValue(VP);
  161. return V;
  162. }
  163. };
  164. template<typename PT1, typename PT2>
  165. static bool operator==(PointerUnion<PT1, PT2> lhs,
  166. PointerUnion<PT1, PT2> rhs) {
  167. return lhs.getOpaqueValue() == rhs.getOpaqueValue();
  168. }
  169. template<typename PT1, typename PT2>
  170. static bool operator!=(PointerUnion<PT1, PT2> lhs,
  171. PointerUnion<PT1, PT2> rhs) {
  172. return lhs.getOpaqueValue() != rhs.getOpaqueValue();
  173. }
  174. template<typename PT1, typename PT2>
  175. static bool operator<(PointerUnion<PT1, PT2> lhs,
  176. PointerUnion<PT1, PT2> rhs) {
  177. return lhs.getOpaqueValue() < rhs.getOpaqueValue();
  178. }
  179. // Teach SmallPtrSet that PointerUnion is "basically a pointer", that has
  180. // # low bits available = min(PT1bits,PT2bits)-1.
  181. template<typename PT1, typename PT2>
  182. class PointerLikeTypeTraits<PointerUnion<PT1, PT2> > {
  183. public:
  184. static inline void *
  185. getAsVoidPointer(const PointerUnion<PT1, PT2> &P) {
  186. return P.getOpaqueValue();
  187. }
  188. static inline PointerUnion<PT1, PT2>
  189. getFromVoidPointer(void *P) {
  190. return PointerUnion<PT1, PT2>::getFromOpaqueValue(P);
  191. }
  192. // The number of bits available are the min of the two pointer types.
  193. enum {
  194. NumLowBitsAvailable =
  195. PointerLikeTypeTraits<typename PointerUnion<PT1,PT2>::ValTy>
  196. ::NumLowBitsAvailable
  197. };
  198. };
  199. /// PointerUnion3 - This is a pointer union of three pointer types. See
  200. /// documentation for PointerUnion for usage.
  201. template <typename PT1, typename PT2, typename PT3>
  202. class PointerUnion3 {
  203. public:
  204. typedef PointerUnion<PT1, PT2> InnerUnion;
  205. typedef PointerUnion<InnerUnion, PT3> ValTy;
  206. private:
  207. ValTy Val;
  208. struct IsInnerUnion {
  209. ValTy Val;
  210. IsInnerUnion(ValTy val) : Val(val) { }
  211. template<typename T>
  212. int is() const {
  213. return Val.template is<InnerUnion>() &&
  214. Val.template get<InnerUnion>().template is<T>();
  215. }
  216. template<typename T>
  217. T get() const {
  218. return Val.template get<InnerUnion>().template get<T>();
  219. }
  220. };
  221. struct IsPT3 {
  222. ValTy Val;
  223. IsPT3(ValTy val) : Val(val) { }
  224. template<typename T>
  225. int is() const {
  226. return Val.template is<T>();
  227. }
  228. template<typename T>
  229. T get() const {
  230. return Val.template get<T>();
  231. }
  232. };
  233. public:
  234. PointerUnion3() {}
  235. PointerUnion3(PT1 V) {
  236. Val = InnerUnion(V);
  237. }
  238. PointerUnion3(PT2 V) {
  239. Val = InnerUnion(V);
  240. }
  241. PointerUnion3(PT3 V) {
  242. Val = V;
  243. }
  244. /// isNull - Return true if the pointer held in the union is null,
  245. /// regardless of which type it is.
  246. bool isNull() const { return Val.isNull(); }
  247. explicit operator bool() const { return !isNull(); }
  248. /// is<T>() return true if the Union currently holds the type matching T.
  249. template<typename T>
  250. int is() const {
  251. // If T is PT1/PT2 choose IsInnerUnion otherwise choose IsPT3.
  252. typedef typename
  253. ::llvm::PointerUnionTypeSelector<PT1, T, IsInnerUnion,
  254. ::llvm::PointerUnionTypeSelector<PT2, T, IsInnerUnion, IsPT3 >
  255. >::Return Ty;
  256. return Ty(Val).template is<T>();
  257. }
  258. /// get<T>() - Return the value of the specified pointer type. If the
  259. /// specified pointer type is incorrect, assert.
  260. template<typename T>
  261. T get() const {
  262. assert(is<T>() && "Invalid accessor called");
  263. // If T is PT1/PT2 choose IsInnerUnion otherwise choose IsPT3.
  264. typedef typename
  265. ::llvm::PointerUnionTypeSelector<PT1, T, IsInnerUnion,
  266. ::llvm::PointerUnionTypeSelector<PT2, T, IsInnerUnion, IsPT3 >
  267. >::Return Ty;
  268. return Ty(Val).template get<T>();
  269. }
  270. /// dyn_cast<T>() - If the current value is of the specified pointer type,
  271. /// return it, otherwise return null.
  272. template<typename T>
  273. T dyn_cast() const {
  274. if (is<T>()) return get<T>();
  275. return T();
  276. }
  277. /// \brief Assignment from nullptr which just clears the union.
  278. const PointerUnion3 &operator=(std::nullptr_t) {
  279. Val = nullptr;
  280. return *this;
  281. }
  282. /// Assignment operators - Allow assigning into this union from either
  283. /// pointer type, setting the discriminator to remember what it came from.
  284. const PointerUnion3 &operator=(const PT1 &RHS) {
  285. Val = InnerUnion(RHS);
  286. return *this;
  287. }
  288. const PointerUnion3 &operator=(const PT2 &RHS) {
  289. Val = InnerUnion(RHS);
  290. return *this;
  291. }
  292. const PointerUnion3 &operator=(const PT3 &RHS) {
  293. Val = RHS;
  294. return *this;
  295. }
  296. void *getOpaqueValue() const { return Val.getOpaqueValue(); }
  297. static inline PointerUnion3 getFromOpaqueValue(void *VP) {
  298. PointerUnion3 V;
  299. V.Val = ValTy::getFromOpaqueValue(VP);
  300. return V;
  301. }
  302. };
  303. // Teach SmallPtrSet that PointerUnion3 is "basically a pointer", that has
  304. // # low bits available = min(PT1bits,PT2bits,PT2bits)-2.
  305. template<typename PT1, typename PT2, typename PT3>
  306. class PointerLikeTypeTraits<PointerUnion3<PT1, PT2, PT3> > {
  307. public:
  308. static inline void *
  309. getAsVoidPointer(const PointerUnion3<PT1, PT2, PT3> &P) {
  310. return P.getOpaqueValue();
  311. }
  312. static inline PointerUnion3<PT1, PT2, PT3>
  313. getFromVoidPointer(void *P) {
  314. return PointerUnion3<PT1, PT2, PT3>::getFromOpaqueValue(P);
  315. }
  316. // The number of bits available are the min of the two pointer types.
  317. enum {
  318. NumLowBitsAvailable =
  319. PointerLikeTypeTraits<typename PointerUnion3<PT1, PT2, PT3>::ValTy>
  320. ::NumLowBitsAvailable
  321. };
  322. };
  323. /// PointerUnion4 - This is a pointer union of four pointer types. See
  324. /// documentation for PointerUnion for usage.
  325. template <typename PT1, typename PT2, typename PT3, typename PT4>
  326. class PointerUnion4 {
  327. public:
  328. typedef PointerUnion<PT1, PT2> InnerUnion1;
  329. typedef PointerUnion<PT3, PT4> InnerUnion2;
  330. typedef PointerUnion<InnerUnion1, InnerUnion2> ValTy;
  331. private:
  332. ValTy Val;
  333. public:
  334. PointerUnion4() {}
  335. PointerUnion4(PT1 V) {
  336. Val = InnerUnion1(V);
  337. }
  338. PointerUnion4(PT2 V) {
  339. Val = InnerUnion1(V);
  340. }
  341. PointerUnion4(PT3 V) {
  342. Val = InnerUnion2(V);
  343. }
  344. PointerUnion4(PT4 V) {
  345. Val = InnerUnion2(V);
  346. }
  347. /// isNull - Return true if the pointer held in the union is null,
  348. /// regardless of which type it is.
  349. bool isNull() const { return Val.isNull(); }
  350. explicit operator bool() const { return !isNull(); }
  351. /// is<T>() return true if the Union currently holds the type matching T.
  352. template<typename T>
  353. int is() const {
  354. // If T is PT1/PT2 choose InnerUnion1 otherwise choose InnerUnion2.
  355. typedef typename
  356. ::llvm::PointerUnionTypeSelector<PT1, T, InnerUnion1,
  357. ::llvm::PointerUnionTypeSelector<PT2, T, InnerUnion1, InnerUnion2 >
  358. >::Return Ty;
  359. return Val.template is<Ty>() &&
  360. Val.template get<Ty>().template is<T>();
  361. }
  362. /// get<T>() - Return the value of the specified pointer type. If the
  363. /// specified pointer type is incorrect, assert.
  364. template<typename T>
  365. T get() const {
  366. assert(is<T>() && "Invalid accessor called");
  367. // If T is PT1/PT2 choose InnerUnion1 otherwise choose InnerUnion2.
  368. typedef typename
  369. ::llvm::PointerUnionTypeSelector<PT1, T, InnerUnion1,
  370. ::llvm::PointerUnionTypeSelector<PT2, T, InnerUnion1, InnerUnion2 >
  371. >::Return Ty;
  372. return Val.template get<Ty>().template get<T>();
  373. }
  374. /// dyn_cast<T>() - If the current value is of the specified pointer type,
  375. /// return it, otherwise return null.
  376. template<typename T>
  377. T dyn_cast() const {
  378. if (is<T>()) return get<T>();
  379. return T();
  380. }
  381. /// \brief Assignment from nullptr which just clears the union.
  382. const PointerUnion4 &operator=(std::nullptr_t) {
  383. Val = nullptr;
  384. return *this;
  385. }
  386. /// Assignment operators - Allow assigning into this union from either
  387. /// pointer type, setting the discriminator to remember what it came from.
  388. const PointerUnion4 &operator=(const PT1 &RHS) {
  389. Val = InnerUnion1(RHS);
  390. return *this;
  391. }
  392. const PointerUnion4 &operator=(const PT2 &RHS) {
  393. Val = InnerUnion1(RHS);
  394. return *this;
  395. }
  396. const PointerUnion4 &operator=(const PT3 &RHS) {
  397. Val = InnerUnion2(RHS);
  398. return *this;
  399. }
  400. const PointerUnion4 &operator=(const PT4 &RHS) {
  401. Val = InnerUnion2(RHS);
  402. return *this;
  403. }
  404. void *getOpaqueValue() const { return Val.getOpaqueValue(); }
  405. static inline PointerUnion4 getFromOpaqueValue(void *VP) {
  406. PointerUnion4 V;
  407. V.Val = ValTy::getFromOpaqueValue(VP);
  408. return V;
  409. }
  410. };
  411. // Teach SmallPtrSet that PointerUnion4 is "basically a pointer", that has
  412. // # low bits available = min(PT1bits,PT2bits,PT2bits)-2.
  413. template<typename PT1, typename PT2, typename PT3, typename PT4>
  414. class PointerLikeTypeTraits<PointerUnion4<PT1, PT2, PT3, PT4> > {
  415. public:
  416. static inline void *
  417. getAsVoidPointer(const PointerUnion4<PT1, PT2, PT3, PT4> &P) {
  418. return P.getOpaqueValue();
  419. }
  420. static inline PointerUnion4<PT1, PT2, PT3, PT4>
  421. getFromVoidPointer(void *P) {
  422. return PointerUnion4<PT1, PT2, PT3, PT4>::getFromOpaqueValue(P);
  423. }
  424. // The number of bits available are the min of the two pointer types.
  425. enum {
  426. NumLowBitsAvailable =
  427. PointerLikeTypeTraits<typename PointerUnion4<PT1, PT2, PT3, PT4>::ValTy>
  428. ::NumLowBitsAvailable
  429. };
  430. };
  431. // Teach DenseMap how to use PointerUnions as keys.
  432. template<typename T, typename U>
  433. struct DenseMapInfo<PointerUnion<T, U> > {
  434. typedef PointerUnion<T, U> Pair;
  435. typedef DenseMapInfo<T> FirstInfo;
  436. typedef DenseMapInfo<U> SecondInfo;
  437. static inline Pair getEmptyKey() {
  438. return Pair(FirstInfo::getEmptyKey());
  439. }
  440. static inline Pair getTombstoneKey() {
  441. return Pair(FirstInfo::getTombstoneKey());
  442. }
  443. static unsigned getHashValue(const Pair &PairVal) {
  444. intptr_t key = (intptr_t)PairVal.getOpaqueValue();
  445. return DenseMapInfo<intptr_t>::getHashValue(key);
  446. }
  447. static bool isEqual(const Pair &LHS, const Pair &RHS) {
  448. return LHS.template is<T>() == RHS.template is<T>() &&
  449. (LHS.template is<T>() ?
  450. FirstInfo::isEqual(LHS.template get<T>(),
  451. RHS.template get<T>()) :
  452. SecondInfo::isEqual(LHS.template get<U>(),
  453. RHS.template get<U>()));
  454. }
  455. };
  456. }
  457. #endif