llvm-type_traits.h 8.6 KB

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