LLVM  4.0.0
PointerLikeTypeTraits.h
Go to the documentation of this file.
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>
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 pointers.
64 template <typename T> class PointerLikeTypeTraits<const T *> {
66 
67 public:
68  static inline const void *getAsVoidPointer(const T *P) {
69  return NonConst::getAsVoidPointer(const_cast<T *>(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 uintptr_t.
78 template <> class PointerLikeTypeTraits<uintptr_t> {
79 public:
80  static inline void *getAsVoidPointer(uintptr_t P) {
81  return reinterpret_cast<void *>(P);
82  }
83  static inline uintptr_t getFromVoidPointer(void *P) {
84  return reinterpret_cast<uintptr_t>(P);
85  }
86  // No bits are available!
87  enum { NumLowBitsAvailable = 0 };
88 };
89 
90 } // end namespace llvm
91 
92 #endif
A traits type that is used to handle pointer types and things that are just wrappers for pointers as ...
A tiny meta function to compute the log2 of a compile time constant.
#define P(N)
static const T * getFromVoidPointer(const void *P)
aarch64 promote const
static const void * getAsVoidPointer(const T *P)