1 //===- llvm/Support/PointerLikeTypeTraits.h - Pointer Traits ----*- 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 PointerLikeTypeTraits class. This allows data 11 // structures to reason about pointers and other things that are pointer sized. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_SUPPORT_POINTERLIKETYPETRAITS_H 16 #define LLVM_SUPPORT_POINTERLIKETYPETRAITS_H 17 18 #include "llvm/Support/DataTypes.h" 19 #include <type_traits> 20 21 namespace llvm { 22 23 /// A traits type that is used to handle pointer types and things that are just 24 /// wrappers for pointers as a uniform entity. 25 template <typename T> class PointerLikeTypeTraits { 26 // getAsVoidPointer 27 // getFromVoidPointer 28 // getNumLowBitsAvailable 29 }; 30 31 namespace detail { 32 /// A tiny meta function to compute the log2 of a compile time constant. 33 template <size_t N> 34 struct ConstantLog2 35 : std::integral_constant<size_t, ConstantLog2<N / 2>::value + 1> {}; 36 template <> struct ConstantLog2<1> : std::integral_constant<size_t, 0> {}; 37 } 38 39 // Provide PointerLikeTypeTraits for non-cvr pointers. 40 template <typename T> class PointerLikeTypeTraits<T *> { 41 public: 42 static inline void *getAsVoidPointer(T *P) { return P; } 43 static inline T *getFromVoidPointer(void *P) { return static_cast<T *>(P); } 44 45 enum { NumLowBitsAvailable = detail::ConstantLog2<alignof(T)>::value }; 46 }; 47 48 template <> class PointerLikeTypeTraits<void *> { 49 public: 50 static inline void *getAsVoidPointer(void *P) { return P; } 51 static inline void *getFromVoidPointer(void *P) { return P; } 52 53 /// Note, we assume here that void* is related to raw malloc'ed memory and 54 /// that malloc returns objects at least 4-byte aligned. However, this may be 55 /// wrong, or pointers may be from something other than malloc. In this case, 56 /// you should specify a real typed pointer or avoid this template. 57 /// 58 /// All clients should use assertions to do a run-time check to ensure that 59 /// this is actually true. 60 enum { NumLowBitsAvailable = 2 }; 61 }; 62 63 // Provide PointerLikeTypeTraits for const things. 64 template <typename T> class PointerLikeTypeTraits<const T> { 65 typedef PointerLikeTypeTraits<T> NonConst; 66 67 public: 68 static inline const void *getAsVoidPointer(const T P) { 69 return NonConst::getAsVoidPointer(P); 70 } 71 static inline const T getFromVoidPointer(const void *P) { 72 return NonConst::getFromVoidPointer(const_cast<void *>(P)); 73 } 74 enum { NumLowBitsAvailable = NonConst::NumLowBitsAvailable }; 75 }; 76 77 // Provide PointerLikeTypeTraits for const pointers. 78 template <typename T> class PointerLikeTypeTraits<const T *> { 79 typedef PointerLikeTypeTraits<T *> NonConst; 80 81 public: 82 static inline const void *getAsVoidPointer(const T *P) { 83 return NonConst::getAsVoidPointer(const_cast<T *>(P)); 84 } 85 static inline const T *getFromVoidPointer(const void *P) { 86 return NonConst::getFromVoidPointer(const_cast<void *>(P)); 87 } 88 enum { NumLowBitsAvailable = NonConst::NumLowBitsAvailable }; 89 }; 90 91 // Provide PointerLikeTypeTraits for uintptr_t. 92 template <> class PointerLikeTypeTraits<uintptr_t> { 93 public: 94 static inline void *getAsVoidPointer(uintptr_t P) { 95 return reinterpret_cast<void *>(P); 96 } 97 static inline uintptr_t getFromVoidPointer(void *P) { 98 return reinterpret_cast<uintptr_t>(P); 99 } 100 // No bits are available! 101 enum { NumLowBitsAvailable = 0 }; 102 }; 103 104 } // end namespace llvm 105 106 #endif 107