]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Support/PointerLikeTypeTraits.h
Merge ^/head r306412 through r306905.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / Support / PointerLikeTypeTraits.h
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/AlignOf.h"
19 #include "llvm/Support/DataTypes.h"
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 {
46     NumLowBitsAvailable = detail::ConstantLog2<AlignOf<T>::Alignment>::value
47   };
48 };
49
50 template <> class PointerLikeTypeTraits<void *> {
51 public:
52   static inline void *getAsVoidPointer(void *P) { return P; }
53   static inline void *getFromVoidPointer(void *P) { return P; }
54
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.
59   ///
60   /// All clients should use assertions to do a run-time check to ensure that
61   /// this is actually true.
62   enum { NumLowBitsAvailable = 2 };
63 };
64
65 // Provide PointerLikeTypeTraits for const pointers.
66 template <typename T> class PointerLikeTypeTraits<const T *> {
67   typedef PointerLikeTypeTraits<T *> NonConst;
68
69 public:
70   static inline const void *getAsVoidPointer(const T *P) {
71     return NonConst::getAsVoidPointer(const_cast<T *>(P));
72   }
73   static inline const T *getFromVoidPointer(const void *P) {
74     return NonConst::getFromVoidPointer(const_cast<void *>(P));
75   }
76   enum { NumLowBitsAvailable = NonConst::NumLowBitsAvailable };
77 };
78
79 // Provide PointerLikeTypeTraits for uintptr_t.
80 template <> class PointerLikeTypeTraits<uintptr_t> {
81 public:
82   static inline void *getAsVoidPointer(uintptr_t P) {
83     return reinterpret_cast<void *>(P);
84   }
85   static inline uintptr_t getFromVoidPointer(void *P) {
86     return reinterpret_cast<uintptr_t>(P);
87   }
88   // No bits are available!
89   enum { NumLowBitsAvailable = 0 };
90 };
91
92 } // end namespace llvm
93
94 #endif