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
34namespace llvm {
35
36namespace 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
51template <typename T>
52struct 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
57public:
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.
65template <typename T>
66struct 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.
79template<typename T, typename U>
80struct isPodLike<std::pair<T, U> > {
81  static const bool value = isPodLike<T>::value && isPodLike<U>::value;
82};
83
84
85template <class T, T v>
86struct 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
93typedef integral_constant<bool, true> true_type;
94typedef integral_constant<bool, false> false_type;
95
96/// \brief Metafunction that determines whether the two given types are
97/// equivalent.
98template<typename T, typename U> struct is_same       : public false_type {};
99template<typename T>             struct is_same<T, T> : public true_type {};
100
101/// \brief Metafunction that removes const qualification from a type.
102template <typename T> struct remove_const          { typedef T type; };
103template <typename T> struct remove_const<const T> { typedef T type; };
104
105/// \brief Metafunction that removes volatile qualification from a type.
106template <typename T> struct remove_volatile             { typedef T type; };
107template <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.
111template <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.
116template <typename T> struct is_integral_impl           : false_type {};
117template <> struct is_integral_impl<         bool>      : true_type {};
118template <> struct is_integral_impl<         char>      : true_type {};
119template <> struct is_integral_impl<  signed char>      : true_type {};
120template <> struct is_integral_impl<unsigned char>      : true_type {};
121template <> struct is_integral_impl<         wchar_t>   : true_type {};
122template <> struct is_integral_impl<         short>     : true_type {};
123template <> struct is_integral_impl<unsigned short>     : true_type {};
124template <> struct is_integral_impl<         int>       : true_type {};
125template <> struct is_integral_impl<unsigned int>       : true_type {};
126template <> struct is_integral_impl<         long>      : true_type {};
127template <> struct is_integral_impl<unsigned long>      : true_type {};
128template <> struct is_integral_impl<         long long> : true_type {};
129template <> struct is_integral_impl<unsigned long long> : true_type {};
130
131/// \brief Metafunction that determines whether the given type is an integral
132/// type.
133template <typename T>
134struct is_integral : is_integral_impl<T> {};
135
136/// \brief Metafunction to remove reference from a type.
137template <typename T> struct remove_reference { typedef T type; };
138template <typename T> struct remove_reference<T&> { typedef T type; };
139
140/// \brief Metafunction that determines whether the given type is a pointer
141/// type.
142template <typename T> struct is_pointer : false_type {};
143template <typename T> struct is_pointer<T*> : true_type {};
144template <typename T> struct is_pointer<T* const> : true_type {};
145template <typename T> struct is_pointer<T* volatile> : true_type {};
146template <typename T> struct is_pointer<T* const volatile> : true_type {};
147
148/// \brief Metafunction that determines wheather the given type is a reference.
149template <typename T> struct is_reference : false_type {};
150template <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.
158template <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
169public:
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
178template<bool Cond, typename T = void>
179struct enable_if_c {
180  typedef T type;
181};
182
183template<typename T> struct enable_if_c<false, T> { };
184
185// enable_if - Enable/disable a template based on a metafunction
186template<typename Cond, typename T = void>
187struct enable_if : public enable_if_c<Cond::value, T> { };
188
189namespace 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.
196template<typename Base, typename Derived>
197struct 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].
205template <typename T> struct remove_pointer { typedef T type; };
206template <typename T> struct remove_pointer<T*> { typedef T type; };
207template <typename T> struct remove_pointer<T*const> { typedef T type; };
208template <typename T> struct remove_pointer<T*volatile> { typedef T type; };
209template <typename T> struct remove_pointer<T*const volatile> {
210    typedef T type; };
211
212// If T is a pointer, just return it. If it is not, return T&.
213template<typename T, typename Enable = void>
214struct add_lvalue_reference_if_not_pointer { typedef T &type; };
215
216template<typename T>
217struct add_lvalue_reference_if_not_pointer<T,
218                                     typename enable_if<is_pointer<T> >::type> {
219  typedef T type;
220};
221
222// If T is a pointer to X, return a pointer to const X. If it is not, return
223// const T.
224template<typename T, typename Enable = void>
225struct add_const_past_pointer { typedef const T type; };
226
227template<typename T>
228struct add_const_past_pointer<T, typename enable_if<is_pointer<T> >::type> {
229  typedef const typename remove_pointer<T>::type *type;
230};
231
232template <bool, typename T, typename F>
233struct conditional { typedef T type; };
234
235template <typename T, typename F>
236struct conditional<false, T, F> { typedef F type; };
237
238}
239
240#ifdef LLVM_DEFINED_HAS_FEATURE
241#undef __has_feature
242#endif
243
244#endif
245