Home | History | Annotate | Download | only in Support
      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> struct PointerLikeTypeTraits;
     26 
     27 namespace detail {
     28 /// A tiny meta function to compute the log2 of a compile time constant.
     29 template <size_t N>
     30 struct ConstantLog2
     31     : std::integral_constant<size_t, ConstantLog2<N / 2>::value + 1> {};
     32 template <> struct ConstantLog2<1> : std::integral_constant<size_t, 0> {};
     33 
     34 // Provide a trait to check if T is pointer-like.
     35 template <typename T, typename U = void> struct HasPointerLikeTypeTraits {
     36   static const bool value = false;
     37 };
     38 
     39 // sizeof(T) is valid only for a complete T.
     40 template <typename T> struct HasPointerLikeTypeTraits<
     41   T, decltype((sizeof(PointerLikeTypeTraits<T>) + sizeof(T)), void())> {
     42   static const bool value = true;
     43 };
     44 
     45 template <typename T> struct IsPointerLike {
     46   static const bool value = HasPointerLikeTypeTraits<T>::value;
     47 };
     48 
     49 template <typename T> struct IsPointerLike<T *> {
     50   static const bool value = true;
     51 };
     52 } // namespace detail
     53 
     54 // Provide PointerLikeTypeTraits for non-cvr pointers.
     55 template <typename T> struct PointerLikeTypeTraits<T *> {
     56   static inline void *getAsVoidPointer(T *P) { return P; }
     57   static inline T *getFromVoidPointer(void *P) { return static_cast<T *>(P); }
     58 
     59   enum { NumLowBitsAvailable = detail::ConstantLog2<alignof(T)>::value };
     60 };
     61 
     62 template <> struct PointerLikeTypeTraits<void *> {
     63   static inline void *getAsVoidPointer(void *P) { return P; }
     64   static inline void *getFromVoidPointer(void *P) { return P; }
     65 
     66   /// Note, we assume here that void* is related to raw malloc'ed memory and
     67   /// that malloc returns objects at least 4-byte aligned. However, this may be
     68   /// wrong, or pointers may be from something other than malloc. In this case,
     69   /// you should specify a real typed pointer or avoid this template.
     70   ///
     71   /// All clients should use assertions to do a run-time check to ensure that
     72   /// this is actually true.
     73   enum { NumLowBitsAvailable = 2 };
     74 };
     75 
     76 // Provide PointerLikeTypeTraits for const things.
     77 template <typename T> struct PointerLikeTypeTraits<const T> {
     78   typedef PointerLikeTypeTraits<T> NonConst;
     79 
     80   static inline const void *getAsVoidPointer(const T P) {
     81     return NonConst::getAsVoidPointer(P);
     82   }
     83   static inline const T getFromVoidPointer(const void *P) {
     84     return NonConst::getFromVoidPointer(const_cast<void *>(P));
     85   }
     86   enum { NumLowBitsAvailable = NonConst::NumLowBitsAvailable };
     87 };
     88 
     89 // Provide PointerLikeTypeTraits for const pointers.
     90 template <typename T> struct PointerLikeTypeTraits<const T *> {
     91   typedef PointerLikeTypeTraits<T *> NonConst;
     92 
     93   static inline const void *getAsVoidPointer(const T *P) {
     94     return NonConst::getAsVoidPointer(const_cast<T *>(P));
     95   }
     96   static inline const T *getFromVoidPointer(const void *P) {
     97     return NonConst::getFromVoidPointer(const_cast<void *>(P));
     98   }
     99   enum { NumLowBitsAvailable = NonConst::NumLowBitsAvailable };
    100 };
    101 
    102 // Provide PointerLikeTypeTraits for uintptr_t.
    103 template <> struct PointerLikeTypeTraits<uintptr_t> {
    104   static inline void *getAsVoidPointer(uintptr_t P) {
    105     return reinterpret_cast<void *>(P);
    106   }
    107   static inline uintptr_t getFromVoidPointer(void *P) {
    108     return reinterpret_cast<uintptr_t>(P);
    109   }
    110   // No bits are available!
    111   enum { NumLowBitsAvailable = 0 };
    112 };
    113 
    114 } // end namespace llvm
    115 
    116 #endif
    117