PointerUnion.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 const_cast<PT1 *>(
  138. reinterpret_cast<const PT1 *>(Val.getAddrOfPointer()));
  139. }
  140. /// \brief Assignment from nullptr which just clears the union.
  141. const PointerUnion &operator=(std::nullptr_t) {
  142. Val.initWithPointer(nullptr);
  143. return *this;
  144. }
  145. /// Assignment operators - Allow assigning into this union from either
  146. /// pointer type, setting the discriminator to remember what it came from.
  147. const PointerUnion &operator=(const PT1 &RHS) {
  148. Val.initWithPointer(
  149. const_cast<void *>(PointerLikeTypeTraits<PT1>::getAsVoidPointer(RHS)));
  150. return *this;
  151. }
  152. const PointerUnion &operator=(const PT2 &RHS) {
  153. Val.setPointerAndInt(
  154. const_cast<void *>(PointerLikeTypeTraits<PT2>::getAsVoidPointer(RHS)),
  155. 1);
  156. return *this;
  157. }
  158. void *getOpaqueValue() const { return Val.getOpaqueValue(); }
  159. static inline PointerUnion getFromOpaqueValue(void *VP) {
  160. PointerUnion V;
  161. V.Val = ValTy::getFromOpaqueValue(VP);
  162. return V;
  163. }
  164. };
  165. template<typename PT1, typename PT2>
  166. static bool operator==(PointerUnion<PT1, PT2> lhs,
  167. PointerUnion<PT1, PT2> rhs) {
  168. return lhs.getOpaqueValue() == rhs.getOpaqueValue();
  169. }
  170. template<typename PT1, typename PT2>
  171. static bool operator!=(PointerUnion<PT1, PT2> lhs,
  172. PointerUnion<PT1, PT2> rhs) {
  173. return lhs.getOpaqueValue() != rhs.getOpaqueValue();
  174. }
  175. template<typename PT1, typename PT2>
  176. static bool operator<(PointerUnion<PT1, PT2> lhs,
  177. PointerUnion<PT1, PT2> rhs) {
  178. return lhs.getOpaqueValue() < rhs.getOpaqueValue();
  179. }
  180. // Teach SmallPtrSet that PointerUnion is "basically a pointer", that has
  181. // # low bits available = min(PT1bits,PT2bits)-1.
  182. template<typename PT1, typename PT2>
  183. class PointerLikeTypeTraits<PointerUnion<PT1, PT2> > {
  184. public:
  185. static inline void *
  186. getAsVoidPointer(const PointerUnion<PT1, PT2> &P) {
  187. return P.getOpaqueValue();
  188. }
  189. static inline PointerUnion<PT1, PT2>
  190. getFromVoidPointer(void *P) {
  191. return PointerUnion<PT1, PT2>::getFromOpaqueValue(P);
  192. }
  193. // The number of bits available are the min of the two pointer types.
  194. enum {
  195. NumLowBitsAvailable =
  196. PointerLikeTypeTraits<typename PointerUnion<PT1,PT2>::ValTy>
  197. ::NumLowBitsAvailable
  198. };
  199. };
  200. /// PointerUnion3 - This is a pointer union of three pointer types. See
  201. /// documentation for PointerUnion for usage.
  202. template <typename PT1, typename PT2, typename PT3>
  203. class PointerUnion3 {
  204. public:
  205. typedef PointerUnion<PT1, PT2> InnerUnion;
  206. typedef PointerUnion<InnerUnion, PT3> ValTy;
  207. private:
  208. ValTy Val;
  209. struct IsInnerUnion {
  210. ValTy Val;
  211. IsInnerUnion(ValTy val) : Val(val) { }
  212. template<typename T>
  213. int is() const {
  214. return Val.template is<InnerUnion>() &&
  215. Val.template get<InnerUnion>().template is<T>();
  216. }
  217. template<typename T>
  218. T get() const {
  219. return Val.template get<InnerUnion>().template get<T>();
  220. }
  221. };
  222. struct IsPT3 {
  223. ValTy Val;
  224. IsPT3(ValTy val) : Val(val) { }
  225. template<typename T>
  226. int is() const {
  227. return Val.template is<T>();
  228. }
  229. template<typename T>
  230. T get() const {
  231. return Val.template get<T>();
  232. }
  233. };
  234. public:
  235. PointerUnion3() {}
  236. PointerUnion3(PT1 V) {
  237. Val = InnerUnion(V);
  238. }
  239. PointerUnion3(PT2 V) {
  240. Val = InnerUnion(V);
  241. }
  242. PointerUnion3(PT3 V) {
  243. Val = V;
  244. }
  245. /// isNull - Return true if the pointer held in the union is null,
  246. /// regardless of which type it is.
  247. bool isNull() const { return Val.isNull(); }
  248. explicit operator bool() const { return !isNull(); }
  249. /// is<T>() return true if the Union currently holds the type matching T.
  250. template<typename T>
  251. int is() const {
  252. // If T is PT1/PT2 choose IsInnerUnion otherwise choose IsPT3.
  253. typedef typename
  254. ::llvm::PointerUnionTypeSelector<PT1, T, IsInnerUnion,
  255. ::llvm::PointerUnionTypeSelector<PT2, T, IsInnerUnion, IsPT3 >
  256. >::Return Ty;
  257. return Ty(Val).template is<T>();
  258. }
  259. /// get<T>() - Return the value of the specified pointer type. If the
  260. /// specified pointer type is incorrect, assert.
  261. template<typename T>
  262. T get() const {
  263. assert(is<T>() && "Invalid accessor called");
  264. // If T is PT1/PT2 choose IsInnerUnion otherwise choose IsPT3.
  265. typedef typename
  266. ::llvm::PointerUnionTypeSelector<PT1, T, IsInnerUnion,
  267. ::llvm::PointerUnionTypeSelector<PT2, T, IsInnerUnion, IsPT3 >
  268. >::Return Ty;
  269. return Ty(Val).template get<T>();
  270. }
  271. /// dyn_cast<T>() - If the current value is of the specified pointer type,
  272. /// return it, otherwise return null.
  273. template<typename T>
  274. T dyn_cast() const {
  275. if (is<T>()) return get<T>();
  276. return T();
  277. }
  278. /// \brief Assignment from nullptr which just clears the union.
  279. const PointerUnion3 &operator=(std::nullptr_t) {
  280. Val = nullptr;
  281. return *this;
  282. }
  283. /// Assignment operators - Allow assigning into this union from either
  284. /// pointer type, setting the discriminator to remember what it came from.
  285. const PointerUnion3 &operator=(const PT1 &RHS) {
  286. Val = InnerUnion(RHS);
  287. return *this;
  288. }
  289. const PointerUnion3 &operator=(const PT2 &RHS) {
  290. Val = InnerUnion(RHS);
  291. return *this;
  292. }
  293. const PointerUnion3 &operator=(const PT3 &RHS) {
  294. Val = RHS;
  295. return *this;
  296. }
  297. void *getOpaqueValue() const { return Val.getOpaqueValue(); }
  298. static inline PointerUnion3 getFromOpaqueValue(void *VP) {
  299. PointerUnion3 V;
  300. V.Val = ValTy::getFromOpaqueValue(VP);
  301. return V;
  302. }
  303. };
  304. // Teach SmallPtrSet that PointerUnion3 is "basically a pointer", that has
  305. // # low bits available = min(PT1bits,PT2bits,PT2bits)-2.
  306. template<typename PT1, typename PT2, typename PT3>
  307. class PointerLikeTypeTraits<PointerUnion3<PT1, PT2, PT3> > {
  308. public:
  309. static inline void *
  310. getAsVoidPointer(const PointerUnion3<PT1, PT2, PT3> &P) {
  311. return P.getOpaqueValue();
  312. }
  313. static inline PointerUnion3<PT1, PT2, PT3>
  314. getFromVoidPointer(void *P) {
  315. return PointerUnion3<PT1, PT2, PT3>::getFromOpaqueValue(P);
  316. }
  317. // The number of bits available are the min of the two pointer types.
  318. enum {
  319. NumLowBitsAvailable =
  320. PointerLikeTypeTraits<typename PointerUnion3<PT1, PT2, PT3>::ValTy>
  321. ::NumLowBitsAvailable
  322. };
  323. };
  324. /// PointerUnion4 - This is a pointer union of four pointer types. See
  325. /// documentation for PointerUnion for usage.
  326. template <typename PT1, typename PT2, typename PT3, typename PT4>
  327. class PointerUnion4 {
  328. public:
  329. typedef PointerUnion<PT1, PT2> InnerUnion1;
  330. typedef PointerUnion<PT3, PT4> InnerUnion2;
  331. typedef PointerUnion<InnerUnion1, InnerUnion2> ValTy;
  332. private:
  333. ValTy Val;
  334. public:
  335. PointerUnion4() {}
  336. PointerUnion4(PT1 V) {
  337. Val = InnerUnion1(V);
  338. }
  339. PointerUnion4(PT2 V) {
  340. Val = InnerUnion1(V);
  341. }
  342. PointerUnion4(PT3 V) {
  343. Val = InnerUnion2(V);
  344. }
  345. PointerUnion4(PT4 V) {
  346. Val = InnerUnion2(V);
  347. }
  348. /// isNull - Return true if the pointer held in the union is null,
  349. /// regardless of which type it is.
  350. bool isNull() const { return Val.isNull(); }
  351. explicit operator bool() const { return !isNull(); }
  352. /// is<T>() return true if the Union currently holds the type matching T.
  353. template<typename T>
  354. int is() const {
  355. // If T is PT1/PT2 choose InnerUnion1 otherwise choose InnerUnion2.
  356. typedef typename
  357. ::llvm::PointerUnionTypeSelector<PT1, T, InnerUnion1,
  358. ::llvm::PointerUnionTypeSelector<PT2, T, InnerUnion1, InnerUnion2 >
  359. >::Return Ty;
  360. return Val.template is<Ty>() &&
  361. Val.template get<Ty>().template is<T>();
  362. }
  363. /// get<T>() - Return the value of the specified pointer type. If the
  364. /// specified pointer type is incorrect, assert.
  365. template<typename T>
  366. T get() const {
  367. assert(is<T>() && "Invalid accessor called");
  368. // If T is PT1/PT2 choose InnerUnion1 otherwise choose InnerUnion2.
  369. typedef typename
  370. ::llvm::PointerUnionTypeSelector<PT1, T, InnerUnion1,
  371. ::llvm::PointerUnionTypeSelector<PT2, T, InnerUnion1, InnerUnion2 >
  372. >::Return Ty;
  373. return Val.template get<Ty>().template get<T>();
  374. }
  375. /// dyn_cast<T>() - If the current value is of the specified pointer type,
  376. /// return it, otherwise return null.
  377. template<typename T>
  378. T dyn_cast() const {
  379. if (is<T>()) return get<T>();
  380. return T();
  381. }
  382. /// \brief Assignment from nullptr which just clears the union.
  383. const PointerUnion4 &operator=(std::nullptr_t) {
  384. Val = nullptr;
  385. return *this;
  386. }
  387. /// Assignment operators - Allow assigning into this union from either
  388. /// pointer type, setting the discriminator to remember what it came from.
  389. const PointerUnion4 &operator=(const PT1 &RHS) {
  390. Val = InnerUnion1(RHS);
  391. return *this;
  392. }
  393. const PointerUnion4 &operator=(const PT2 &RHS) {
  394. Val = InnerUnion1(RHS);
  395. return *this;
  396. }
  397. const PointerUnion4 &operator=(const PT3 &RHS) {
  398. Val = InnerUnion2(RHS);
  399. return *this;
  400. }
  401. const PointerUnion4 &operator=(const PT4 &RHS) {
  402. Val = InnerUnion2(RHS);
  403. return *this;
  404. }
  405. void *getOpaqueValue() const { return Val.getOpaqueValue(); }
  406. static inline PointerUnion4 getFromOpaqueValue(void *VP) {
  407. PointerUnion4 V;
  408. V.Val = ValTy::getFromOpaqueValue(VP);
  409. return V;
  410. }
  411. };
  412. // Teach SmallPtrSet that PointerUnion4 is "basically a pointer", that has
  413. // # low bits available = min(PT1bits,PT2bits,PT2bits)-2.
  414. template<typename PT1, typename PT2, typename PT3, typename PT4>
  415. class PointerLikeTypeTraits<PointerUnion4<PT1, PT2, PT3, PT4> > {
  416. public:
  417. static inline void *
  418. getAsVoidPointer(const PointerUnion4<PT1, PT2, PT3, PT4> &P) {
  419. return P.getOpaqueValue();
  420. }
  421. static inline PointerUnion4<PT1, PT2, PT3, PT4>
  422. getFromVoidPointer(void *P) {
  423. return PointerUnion4<PT1, PT2, PT3, PT4>::getFromOpaqueValue(P);
  424. }
  425. // The number of bits available are the min of the two pointer types.
  426. enum {
  427. NumLowBitsAvailable =
  428. PointerLikeTypeTraits<typename PointerUnion4<PT1, PT2, PT3, PT4>::ValTy>
  429. ::NumLowBitsAvailable
  430. };
  431. };
  432. // Teach DenseMap how to use PointerUnions as keys.
  433. template<typename T, typename U>
  434. struct DenseMapInfo<PointerUnion<T, U> > {
  435. typedef PointerUnion<T, U> Pair;
  436. typedef DenseMapInfo<T> FirstInfo;
  437. typedef DenseMapInfo<U> SecondInfo;
  438. static inline Pair getEmptyKey() {
  439. return Pair(FirstInfo::getEmptyKey());
  440. }
  441. static inline Pair getTombstoneKey() {
  442. return Pair(FirstInfo::getTombstoneKey());
  443. }
  444. static unsigned getHashValue(const Pair &PairVal) {
  445. intptr_t key = (intptr_t)PairVal.getOpaqueValue();
  446. return DenseMapInfo<intptr_t>::getHashValue(key);
  447. }
  448. static bool isEqual(const Pair &LHS, const Pair &RHS) {
  449. return LHS.template is<T>() == RHS.template is<T>() &&
  450. (LHS.template is<T>() ?
  451. FirstInfo::isEqual(LHS.template get<T>(),
  452. RHS.template get<T>()) :
  453. SecondInfo::isEqual(LHS.template get<U>(),
  454. RHS.template get<U>()));
  455. }
  456. };
  457. }
  458. #endif