[ADT] Unbreak PointerEmbeddedInt build with MSVC.
[oota-llvm.git] / include / llvm / ADT / PointerEmbeddedInt.h
1 //===- llvm/ADT/PointerEmbeddedInt.h ----------------------------*- 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 #ifndef LLVM_ADT_POINTEREMBEDDEDINT_H
11 #define LLVM_ADT_POINTEREMBEDDEDINT_H
12
13 #include "llvm/ADT/DenseMapInfo.h"
14 #include "llvm/Support/PointerLikeTypeTraits.h"
15 #include <climits>
16
17 namespace llvm {
18
19 /// Utility to embed an integer into a pointer-like type. This is specifically
20 /// intended to allow embedding integers where fewer bits are required than
21 /// exist in a pointer, and the integer can participate in abstractions along
22 /// side other pointer-like types. For example it can be placed into a \c
23 /// PointerSumType or \c PointerUnion.
24 ///
25 /// Note that much like pointers, an integer value of zero has special utility
26 /// due to boolean conversions. For example, a non-null value can be tested for
27 /// in the above abstractions without testing the particular active member.
28 /// Also, the default constructed value zero initializes the integer.
29 template <typename IntT, int Bits = sizeof(IntT) * CHAR_BIT>
30 class PointerEmbeddedInt {
31   uintptr_t Value;
32
33   static_assert(Bits < sizeof(PointerEmbeddedInt::Value) * CHAR_BIT,
34                 "Cannot embed more bits than we have in a pointer!");
35
36   enum : uintptr_t {
37     // We shift as many zeros into the value as we can while preserving the
38     // number of bits desired for the integer.
39     Shift = sizeof(PointerEmbeddedInt::Value) * CHAR_BIT - Bits,
40
41     // We also want to be able to mask out the preserved bits for asserts.
42     Mask = static_cast<uintptr_t>(-1) << Bits
43   };
44
45   friend class PointerLikeTypeTraits<PointerEmbeddedInt>;
46
47   explicit PointerEmbeddedInt(uintptr_t Value) : Value(Value) {}
48
49 public:
50   PointerEmbeddedInt() : Value(0) {}
51
52   PointerEmbeddedInt(IntT I) : Value(static_cast<uintptr_t>(I) << Shift) {
53     assert((I & Mask) == 0 && "Integer has bits outside those preserved!");
54   }
55
56   PointerEmbeddedInt &operator=(IntT I) {
57     assert((I & Mask) == 0 && "Integer has bits outside those preserved!");
58     Value = static_cast<uintptr_t>(I) << Shift;
59   }
60
61   // Note that this imilict conversion additionally allows all of the basic
62   // comparison operators to work transparently, etc.
63   operator IntT() const { return static_cast<IntT>(Value >> Shift); }
64 };
65
66 // Provide pointer like traits to support use with pointer unions and sum
67 // types.
68 template <typename IntT, int Bits>
69 class PointerLikeTypeTraits<PointerEmbeddedInt<IntT, Bits>> {
70   typedef PointerEmbeddedInt<IntT, Bits> T;
71
72 public:
73   static inline void *getAsVoidPointer(const T &P) {
74     return reinterpret_cast<void *>(P.Value);
75   }
76   static inline T getFromVoidPointer(void *P) {
77     return T(reinterpret_cast<uintptr_t>(P));
78   }
79   static inline T getFromVoidPointer(const void *P) {
80     return T(reinterpret_cast<uintptr_t>(P));
81   }
82
83   enum { NumLowBitsAvailable = T::Shift };
84 };
85
86 // Teach DenseMap how to use PointerEmbeddedInt objects as keys if the Int type
87 // itself can be a key.
88 template <typename IntT, int Bits>
89 struct DenseMapInfo<PointerEmbeddedInt<IntT, Bits>> {
90   typedef PointerEmbeddedInt<IntT, Bits> T;
91
92   typedef DenseMapInfo<IntT> IntInfo;
93
94   static inline T getEmptyKey() { return IntInfo::getEmptyKey(); }
95   static inline T getTombstoneKey() { return IntInfo::getTombstoneKey(); }
96   static unsigned getHashValue(const T &Arg) {
97     return IntInfo::getHashValue(Arg);
98   }
99   static bool isEqual(const T &LHS, const T &RHS) { return LHS == RHS; }
100 };
101 }
102
103 #endif