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