1 //===- llvm/Support/type_traits.h - Simplfied type traits -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_SUPPORT_TYPE_TRAITS_H
18 #define LLVM_SUPPORT_TYPE_TRAITS_H
20 #include "llvm/Support/DataTypes.h"
25 #define LLVM_DEFINED_HAS_FEATURE
26 #define __has_feature(x) 0
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.
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(...);
54 // is_class<> metafunction due to Paul Mensonides (leavings@attbi.com). For
56 // http://groups.google.com/groups?hl=en&selm=000001c1cc83%24e154d5e0%247772e50c%40c161550a&rnum=1
58 static const bool value =
59 sizeof(char) == sizeof(dont_use::is_class_helper<T>(0));
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.
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);
72 // If we don't know anything else, we can (at least) assume that all non-class
74 static const bool value = !is_class<T>::value;
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;
85 template <class T, T v>
86 struct integral_constant {
88 static const value_type value = v;
89 typedef integral_constant<T,v> type;
90 operator value_type() { return value; }
93 typedef integral_constant<bool, true> true_type;
94 typedef integral_constant<bool, false> false_type;
96 /// \brief Metafunction that determines whether the two given types are
98 template<typename T, typename U> struct is_same : public false_type {};
99 template<typename T> struct is_same<T, T> : public true_type {};
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; };
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; };
109 /// \brief Metafunction that removes both const and volatile qualification from
111 template <typename T> struct remove_cv {
112 typedef typename remove_const<typename remove_volatile<T>::type>::type type;
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 {};
131 /// \brief Metafunction that determines whether the given type is an integral
133 template <typename T>
134 struct is_integral : is_integral_impl<T> {};
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; };
140 /// \brief Metafunction that determines whether the given type is a pointer
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 {};
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 {};
152 /// \brief Metafunction that determines whether the given type is either an
153 /// integral type or an enumeration type.
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
163 static double check_int_convertible(unsigned long long);
164 static char check_int_convertible(...);
166 typedef typename remove_reference<T>::type UnderlyingT;
167 static UnderlyingT &nonce_instance;
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)));
177 // enable_if_c - Enable/disable a template based on a metafunction
178 template<bool Cond, typename T = void>
183 template<typename T> struct enable_if_c<false, T> { };
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> { };
190 template<typename Base> char base_of_helper(const volatile Base*);
191 template<typename Base> double base_of_helper(...);
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>
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));
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> {
212 // If T is a pointer, just return it. If it is not, return T&.
213 template<typename T, typename Enable = void>
214 struct add_lvalue_reference_if_not_pointer { typedef T &type; };
217 struct add_lvalue_reference_if_not_pointer<T,
218 typename enable_if<is_pointer<T> >::type> {
222 // If T is a pointer to X, return a pointer to const X. If it is not, return
224 template<typename T, typename Enable = void>
225 struct add_const_past_pointer { typedef const T type; };
228 struct add_const_past_pointer<T, typename enable_if<is_pointer<T> >::type> {
229 typedef const typename remove_pointer<T>::type *type;
232 template <bool, typename T, typename F>
233 struct conditional { typedef T type; };
235 template <typename T, typename F>
236 struct conditional<false, T, F> { typedef F type; };
240 #ifdef LLVM_DEFINED_HAS_FEATURE