1 //===- llvm/Support/PointerLikeTypeTraits.h - Pointer Traits ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the PointerLikeTypeTraits class. This allows data
11 // structures to reason about pointers and other things that are pointer sized.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_POINTERLIKETYPETRAITS_H
16 #define LLVM_SUPPORT_POINTERLIKETYPETRAITS_H
18 #include "llvm/Support/AlignOf.h"
19 #include "llvm/Support/DataTypes.h"
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 {
28 // getNumLowBitsAvailable
32 /// A tiny meta function to compute the log2 of a compile time constant.
35 : std::integral_constant<size_t, ConstantLog2<N / 2>::value + 1> {};
36 template <> struct ConstantLog2<1> : std::integral_constant<size_t, 0> {};
39 // Provide PointerLikeTypeTraits for non-cvr pointers.
40 template <typename T> class PointerLikeTypeTraits<T *> {
42 static inline void *getAsVoidPointer(T *P) { return P; }
43 static inline T *getFromVoidPointer(void *P) { return static_cast<T *>(P); }
46 NumLowBitsAvailable = detail::ConstantLog2<AlignOf<T>::Alignment>::value
50 template <> class PointerLikeTypeTraits<void *> {
52 static inline void *getAsVoidPointer(void *P) { return P; }
53 static inline void *getFromVoidPointer(void *P) { return P; }
55 /// Note, we assume here that void* is related to raw malloc'ed memory and
56 /// that malloc returns objects at least 4-byte aligned. However, this may be
57 /// wrong, or pointers may be from something other than malloc. In this case,
58 /// you should specify a real typed pointer or avoid this template.
60 /// All clients should use assertions to do a run-time check to ensure that
61 /// this is actually true.
62 enum { NumLowBitsAvailable = 2 };
65 // Provide PointerLikeTypeTraits for const pointers.
66 template <typename T> class PointerLikeTypeTraits<const T *> {
67 typedef PointerLikeTypeTraits<T *> NonConst;
70 static inline const void *getAsVoidPointer(const T *P) {
71 return NonConst::getAsVoidPointer(const_cast<T *>(P));
73 static inline const T *getFromVoidPointer(const void *P) {
74 return NonConst::getFromVoidPointer(const_cast<void *>(P));
76 enum { NumLowBitsAvailable = NonConst::NumLowBitsAvailable };
79 // Provide PointerLikeTypeTraits for uintptr_t.
80 template <> class PointerLikeTypeTraits<uintptr_t> {
82 static inline void *getAsVoidPointer(uintptr_t P) {
83 return reinterpret_cast<void *>(P);
85 static inline uintptr_t getFromVoidPointer(void *P) {
86 return reinterpret_cast<uintptr_t>(P);
88 // No bits are available!
89 enum { NumLowBitsAvailable = 0 };
92 } // end namespace llvm