Home | History | Annotate | Download | only in Support
      1 //===- llvm/Support/type_traits.h - Simplfied type 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 provides a template class that determines if a type is a class or
     11 // not. The basic mechanism, based on using the pointer to member function of
     12 // a zero argument to a function was "boosted" from the boost type_traits
     13 // library. See http://www.boost.org/ for all the gory details.
     14 //
     15 //===----------------------------------------------------------------------===//
     16 
     17 #ifndef LLVM_SUPPORT_TYPE_TRAITS_H
     18 #define LLVM_SUPPORT_TYPE_TRAITS_H
     19 
     20 #include "llvm/Support/DataTypes.h"
     21 #include <cstddef>
     22 #include <utility>
     23 
     24 // This is actually the conforming implementation which works with abstract
     25 // classes.  However, enough compilers have trouble with it that most will use
     26 // the one in boost/type_traits/object_traits.hpp. This implementation actually
     27 // works with VC7.0, but other interactions seem to fail when we use it.
     28 
     29 namespace llvm {
     30 
     31 namespace dont_use
     32 {
     33     // These two functions should never be used. They are helpers to
     34     // the is_class template below. They cannot be located inside
     35     // is_class because doing so causes at least GCC to think that
     36     // the value of the "value" enumerator is not constant. Placing
     37     // them out here (for some strange reason) allows the sizeof
     38     // operator against them to magically be constant. This is
     39     // important to make the is_class<T>::value idiom zero cost. it
     40     // evaluates to a constant 1 or 0 depending on whether the
     41     // parameter T is a class or not (respectively).
     42     template<typename T> char is_class_helper(void(T::*)());
     43     template<typename T> double is_class_helper(...);
     44 }
     45 
     46 template <typename T>
     47 struct is_class
     48 {
     49   // is_class<> metafunction due to Paul Mensonides (leavings (at) attbi.com). For
     50   // more details:
     51   // http://groups.google.com/groups?hl=en&selm=000001c1cc83%24e154d5e0%247772e50c%40c161550a&rnum=1
     52  public:
     53     enum { value = sizeof(char) == sizeof(dont_use::is_class_helper<T>(0)) };
     54 };
     55 
     56 
     57 /// isPodLike - This is a type trait that is used to determine whether a given
     58 /// type can be copied around with memcpy instead of running ctors etc.
     59 template <typename T>
     60 struct isPodLike {
     61   // If we don't know anything else, we can (at least) assume that all non-class
     62   // types are PODs.
     63   static const bool value = !is_class<T>::value;
     64 };
     65 
     66 // std::pair's are pod-like if their elements are.
     67 template<typename T, typename U>
     68 struct isPodLike<std::pair<T, U> > {
     69   static const bool value = isPodLike<T>::value && isPodLike<U>::value;
     70 };
     71 
     72 
     73 template <class T, T v>
     74 struct integral_constant {
     75   typedef T value_type;
     76   static const value_type value = v;
     77   typedef integral_constant<T,v> type;
     78   operator value_type() { return value; }
     79 };
     80 
     81 typedef integral_constant<bool, true> true_type;
     82 typedef integral_constant<bool, false> false_type;
     83 
     84 /// \brief Metafunction that determines whether the two given types are
     85 /// equivalent.
     86 template<typename T, typename U> struct is_same       : public false_type {};
     87 template<typename T>             struct is_same<T, T> : public true_type {};
     88 
     89 /// \brief Metafunction that removes const qualification from a type.
     90 template <typename T> struct remove_const          { typedef T type; };
     91 template <typename T> struct remove_const<const T> { typedef T type; };
     92 
     93 /// \brief Metafunction that removes volatile qualification from a type.
     94 template <typename T> struct remove_volatile             { typedef T type; };
     95 template <typename T> struct remove_volatile<volatile T> { typedef T type; };
     96 
     97 /// \brief Metafunction that removes both const and volatile qualification from
     98 /// a type.
     99 template <typename T> struct remove_cv {
    100   typedef typename remove_const<typename remove_volatile<T>::type>::type type;
    101 };
    102 
    103 /// \brief Helper to implement is_integral metafunction.
    104 template <typename T> struct is_integral_impl           : false_type {};
    105 template <> struct is_integral_impl<         bool>      : true_type {};
    106 template <> struct is_integral_impl<         char>      : true_type {};
    107 template <> struct is_integral_impl<  signed char>      : true_type {};
    108 template <> struct is_integral_impl<unsigned char>      : true_type {};
    109 template <> struct is_integral_impl<         wchar_t>   : true_type {};
    110 template <> struct is_integral_impl<         short>     : true_type {};
    111 template <> struct is_integral_impl<unsigned short>     : true_type {};
    112 template <> struct is_integral_impl<         int>       : true_type {};
    113 template <> struct is_integral_impl<unsigned int>       : true_type {};
    114 template <> struct is_integral_impl<         long>      : true_type {};
    115 template <> struct is_integral_impl<unsigned long>      : true_type {};
    116 template <> struct is_integral_impl<         long long> : true_type {};
    117 template <> struct is_integral_impl<unsigned long long> : true_type {};
    118 
    119 /// \brief Metafunction that determines whether the given type is an integral
    120 /// type.
    121 template <typename T>
    122 struct is_integral : is_integral_impl<T> {};
    123 
    124 /// \brief Metafunction to remove reference from a type.
    125 template <typename T> struct remove_reference { typedef T type; };
    126 template <typename T> struct remove_reference<T&> { typedef T type; };
    127 
    128 /// \brief Metafunction that determines whether the given type is a pointer
    129 /// type.
    130 template <typename T> struct is_pointer : false_type {};
    131 template <typename T> struct is_pointer<T*> : true_type {};
    132 template <typename T> struct is_pointer<T* const> : true_type {};
    133 template <typename T> struct is_pointer<T* volatile> : true_type {};
    134 template <typename T> struct is_pointer<T* const volatile> : true_type {};
    135 
    136 /// \brief Metafunction that determines whether the given type is either an
    137 /// integral type or an enumeration type.
    138 ///
    139 /// Note that this accepts potentially more integral types than we whitelist
    140 /// above for is_integral because it is based on merely being convertible
    141 /// implicitly to an integral type.
    142 template <typename T> class is_integral_or_enum {
    143   // Provide an overload which can be called with anything implicitly
    144   // convertible to an unsigned long long. This should catch integer types and
    145   // enumeration types at least. We blacklist classes with conversion operators
    146   // below.
    147   static double check_int_convertible(unsigned long long);
    148   static char check_int_convertible(...);
    149 
    150   typedef typename remove_reference<T>::type UnderlyingT;
    151   static UnderlyingT &nonce_instance;
    152 
    153 public:
    154   enum {
    155     value = (!is_class<UnderlyingT>::value && !is_pointer<UnderlyingT>::value &&
    156              !is_same<UnderlyingT, float>::value &&
    157              !is_same<UnderlyingT, double>::value &&
    158              sizeof(char) != sizeof(check_int_convertible(nonce_instance)))
    159   };
    160 };
    161 
    162 // enable_if_c - Enable/disable a template based on a metafunction
    163 template<bool Cond, typename T = void>
    164 struct enable_if_c {
    165   typedef T type;
    166 };
    167 
    168 template<typename T> struct enable_if_c<false, T> { };
    169 
    170 // enable_if - Enable/disable a template based on a metafunction
    171 template<typename Cond, typename T = void>
    172 struct enable_if : public enable_if_c<Cond::value, T> { };
    173 
    174 namespace dont_use {
    175   template<typename Base> char base_of_helper(const volatile Base*);
    176   template<typename Base> double base_of_helper(...);
    177 }
    178 
    179 /// is_base_of - Metafunction to determine whether one type is a base class of
    180 /// (or identical to) another type.
    181 template<typename Base, typename Derived>
    182 struct is_base_of {
    183   static const bool value
    184     = is_class<Base>::value && is_class<Derived>::value &&
    185       sizeof(char) == sizeof(dont_use::base_of_helper<Base>((Derived*)0));
    186 };
    187 
    188 // remove_pointer - Metafunction to turn Foo* into Foo.  Defined in
    189 // C++0x [meta.trans.ptr].
    190 template <typename T> struct remove_pointer { typedef T type; };
    191 template <typename T> struct remove_pointer<T*> { typedef T type; };
    192 template <typename T> struct remove_pointer<T*const> { typedef T type; };
    193 template <typename T> struct remove_pointer<T*volatile> { typedef T type; };
    194 template <typename T> struct remove_pointer<T*const volatile> {
    195     typedef T type; };
    196 
    197 template <bool, typename T, typename F>
    198 struct conditional { typedef T type; };
    199 
    200 template <typename T, typename F>
    201 struct conditional<false, T, F> { typedef F type; };
    202 
    203 }
    204 
    205 #endif
    206