IntegersSubsetMapping:
[oota-llvm.git] / include / llvm / Support / type_traits.h
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@attbi.com). For
55   // more details:
56   // http://groups.google.com/groups?hl=en&selm=000001c1cc83%24e154d5e0%247772e50c%40c161550a&rnum=1
57  public:
58     enum { value = sizeof(char) == sizeof(dont_use::is_class_helper<T>(0)) };
59 };
60   
61   
62 /// isPodLike - This is a type trait that is used to determine whether a given
63 /// type can be copied around with memcpy instead of running ctors etc.
64 template <typename T>
65 struct isPodLike {
66 #if __has_feature(is_trivially_copyable)
67   // If the compiler supports the is_trivially_copyable trait use it, as it
68   // matches the definition of isPodLike closely.
69   static const bool value = __is_trivially_copyable(T);
70 #else
71   // If we don't know anything else, we can (at least) assume that all non-class
72   // types are PODs.
73   static const bool value = !is_class<T>::value;
74 #endif
75 };
76
77 // std::pair's are pod-like if their elements are.
78 template<typename T, typename U>
79 struct isPodLike<std::pair<T, U> > {
80   static const bool value = isPodLike<T>::value && isPodLike<U>::value;
81 };
82   
83
84 template <class T, T v>
85 struct integral_constant {
86   typedef T value_type;
87   static const value_type value = v;
88   typedef integral_constant<T,v> type;
89   operator value_type() { return value; }
90 };
91
92 typedef integral_constant<bool, true> true_type;
93 typedef integral_constant<bool, false> false_type;
94
95 /// \brief Metafunction that determines whether the two given types are 
96 /// equivalent.
97 template<typename T, typename U> struct is_same       : public false_type {};
98 template<typename T>             struct is_same<T, T> : public true_type {};
99
100 /// \brief Metafunction that removes const qualification from a type.
101 template <typename T> struct remove_const          { typedef T type; };
102 template <typename T> struct remove_const<const T> { typedef T type; };
103
104 /// \brief Metafunction that removes volatile qualification from a type.
105 template <typename T> struct remove_volatile             { typedef T type; };
106 template <typename T> struct remove_volatile<volatile T> { typedef T type; };
107
108 /// \brief Metafunction that removes both const and volatile qualification from
109 /// a type.
110 template <typename T> struct remove_cv {
111   typedef typename remove_const<typename remove_volatile<T>::type>::type type;
112 };
113
114 /// \brief Helper to implement is_integral metafunction.
115 template <typename T> struct is_integral_impl           : false_type {};
116 template <> struct is_integral_impl<         bool>      : true_type {};
117 template <> struct is_integral_impl<         char>      : true_type {};
118 template <> struct is_integral_impl<  signed char>      : true_type {};
119 template <> struct is_integral_impl<unsigned char>      : true_type {};
120 template <> struct is_integral_impl<         wchar_t>   : true_type {};
121 template <> struct is_integral_impl<         short>     : true_type {};
122 template <> struct is_integral_impl<unsigned short>     : true_type {};
123 template <> struct is_integral_impl<         int>       : true_type {};
124 template <> struct is_integral_impl<unsigned int>       : true_type {};
125 template <> struct is_integral_impl<         long>      : true_type {};
126 template <> struct is_integral_impl<unsigned long>      : true_type {};
127 template <> struct is_integral_impl<         long long> : true_type {};
128 template <> struct is_integral_impl<unsigned long long> : true_type {};
129
130 /// \brief Metafunction that determines whether the given type is an integral
131 /// type.
132 template <typename T>
133 struct is_integral : is_integral_impl<T> {};
134
135 /// \brief Metafunction to remove reference from a type.
136 template <typename T> struct remove_reference { typedef T type; };
137 template <typename T> struct remove_reference<T&> { typedef T type; };
138
139 /// \brief Metafunction that determines whether the given type is a pointer
140 /// type.
141 template <typename T> struct is_pointer : false_type {};
142 template <typename T> struct is_pointer<T*> : true_type {};
143 template <typename T> struct is_pointer<T* const> : true_type {};
144 template <typename T> struct is_pointer<T* volatile> : true_type {};
145 template <typename T> struct is_pointer<T* const volatile> : true_type {};
146
147 /// \brief Metafunction that determines whether the given type is either an
148 /// integral type or an enumeration type.
149 ///
150 /// Note that this accepts potentially more integral types than we whitelist
151 /// above for is_integral because it is based on merely being convertible
152 /// implicitly to an integral type.
153 template <typename T> class is_integral_or_enum {
154   // Provide an overload which can be called with anything implicitly
155   // convertible to an unsigned long long. This should catch integer types and
156   // enumeration types at least. We blacklist classes with conversion operators
157   // below.
158   static double check_int_convertible(unsigned long long);
159   static char check_int_convertible(...);
160
161   typedef typename remove_reference<T>::type UnderlyingT;
162   static UnderlyingT &nonce_instance;
163
164 public:
165   enum {
166     value = (!is_class<UnderlyingT>::value && !is_pointer<UnderlyingT>::value &&
167              !is_same<UnderlyingT, float>::value &&
168              !is_same<UnderlyingT, double>::value &&
169              sizeof(char) != sizeof(check_int_convertible(nonce_instance)))
170   };
171 };
172
173 // enable_if_c - Enable/disable a template based on a metafunction
174 template<bool Cond, typename T = void>
175 struct enable_if_c {
176   typedef T type;
177 };
178
179 template<typename T> struct enable_if_c<false, T> { };
180   
181 // enable_if - Enable/disable a template based on a metafunction
182 template<typename Cond, typename T = void>
183 struct enable_if : public enable_if_c<Cond::value, T> { };
184
185 namespace dont_use {
186   template<typename Base> char base_of_helper(const volatile Base*);
187   template<typename Base> double base_of_helper(...);
188 }
189
190 /// is_base_of - Metafunction to determine whether one type is a base class of
191 /// (or identical to) another type.
192 template<typename Base, typename Derived>
193 struct is_base_of {
194   static const bool value 
195     = is_class<Base>::value && is_class<Derived>::value &&
196       sizeof(char) == sizeof(dont_use::base_of_helper<Base>((Derived*)0));
197 };
198
199 // remove_pointer - Metafunction to turn Foo* into Foo.  Defined in
200 // C++0x [meta.trans.ptr].
201 template <typename T> struct remove_pointer { typedef T type; };
202 template <typename T> struct remove_pointer<T*> { typedef T type; };
203 template <typename T> struct remove_pointer<T*const> { typedef T type; };
204 template <typename T> struct remove_pointer<T*volatile> { typedef T type; };
205 template <typename T> struct remove_pointer<T*const volatile> {
206     typedef T type; };
207
208 template <bool, typename T, typename F>
209 struct conditional { typedef T type; };
210
211 template <typename T, typename F>
212 struct conditional<false, T, F> { typedef F type; };
213
214 }
215
216 #ifdef LLVM_DEFINED_HAS_FEATURE
217 #undef __has_feature
218 #endif
219
220 #endif