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/DataTypes.h"
22 /// A traits type that is used to handle pointer types and things that are just
23 /// wrappers for pointers as a uniform entity.
24 template <typename T> class PointerLikeTypeTraits {
27 // getNumLowBitsAvailable
30 // Provide PointerLikeTypeTraits for non-cvr pointers.
31 template <typename T> class PointerLikeTypeTraits<T *> {
33 static inline void *getAsVoidPointer(T *P) { return P; }
34 static inline T *getFromVoidPointer(void *P) { return static_cast<T *>(P); }
36 /// Note, we assume here that malloc returns objects at least 4-byte aligned.
37 /// However, this may be wrong, or pointers may be from something other than
38 /// malloc. In this case, you should specialize this template to reduce this.
40 /// All clients should use assertions to do a run-time check to ensure that
41 /// this is actually true.
42 enum { NumLowBitsAvailable = 2 };
45 // Provide PointerLikeTypeTraits for const pointers.
46 template <typename T> class PointerLikeTypeTraits<const T *> {
47 typedef PointerLikeTypeTraits<T *> NonConst;
50 static inline const void *getAsVoidPointer(const T *P) {
51 return NonConst::getAsVoidPointer(const_cast<T *>(P));
53 static inline const T *getFromVoidPointer(const void *P) {
54 return NonConst::getFromVoidPointer(const_cast<void *>(P));
56 enum { NumLowBitsAvailable = NonConst::NumLowBitsAvailable };
59 // Provide PointerLikeTypeTraits for uintptr_t.
60 template <> class PointerLikeTypeTraits<uintptr_t> {
62 static inline void *getAsVoidPointer(uintptr_t P) {
63 return reinterpret_cast<void *>(P);
65 static inline uintptr_t getFromVoidPointer(void *P) {
66 return reinterpret_cast<uintptr_t>(P);
68 // No bits are available!
69 enum { NumLowBitsAvailable = 0 };
72 } // end namespace llvm