Richard Smith | 762bb9d | 2011-10-13 22:29:44 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple=x86_64-apple-darwin9 | FileCheck %s |
Douglas Gregor | 46287c7 | 2010-01-29 16:37:09 +0000 | [diff] [blame] | 2 | |
| 3 | template < bool condition, typename T = void > |
| 4 | struct enable_if { typedef T type; }; |
| 5 | |
| 6 | template< typename T > |
| 7 | struct enable_if< false, T > {}; |
| 8 | |
| 9 | // PR5876 |
| 10 | namespace Casts { |
| 11 | template< unsigned O > |
| 12 | void implicit(typename enable_if< O <= 4 >::type* = 0) { |
| 13 | } |
| 14 | |
| 15 | template< unsigned O > |
| 16 | void cstyle(typename enable_if< O <= (unsigned)4 >::type* = 0) { |
| 17 | } |
| 18 | |
| 19 | template< unsigned O > |
| 20 | void functional(typename enable_if< O <= unsigned(4) >::type* = 0) { |
| 21 | } |
| 22 | |
| 23 | template< unsigned O > |
| 24 | void static_(typename enable_if< O <= static_cast<unsigned>(4) >::type* = 0) { |
| 25 | } |
| 26 | |
Richard Smith | 967ecd3 | 2011-02-21 20:10:02 +0000 | [diff] [blame] | 27 | template< typename T > |
| 28 | void auto_(decltype(new auto(T()))) { |
| 29 | } |
| 30 | |
Richard Smith | 41576d4 | 2012-02-06 02:54:51 +0000 | [diff] [blame^] | 31 | template< typename T > |
| 32 | void scalar_(decltype(T(), int())) { |
| 33 | } |
| 34 | |
Douglas Gregor | 46287c7 | 2010-01-29 16:37:09 +0000 | [diff] [blame] | 35 | // FIXME: Test const_cast, reinterpret_cast, dynamic_cast, which are |
| 36 | // a bit harder to use in template arguments. |
| 37 | template <unsigned N> struct T {}; |
| 38 | |
| 39 | template <int N> T<N> f() { return T<N>(); } |
| 40 | |
Douglas Gregor | 8f51a4f | 2010-03-13 18:23:07 +0000 | [diff] [blame] | 41 | // CHECK: define weak_odr void @_ZN5Casts8implicitILj4EEEvPN9enable_ifIXleT_Li4EEvE4typeE |
Douglas Gregor | 46287c7 | 2010-01-29 16:37:09 +0000 | [diff] [blame] | 42 | template void implicit<4>(void*); |
Douglas Gregor | 8f51a4f | 2010-03-13 18:23:07 +0000 | [diff] [blame] | 43 | // CHECK: define weak_odr void @_ZN5Casts6cstyleILj4EEEvPN9enable_ifIXleT_cvjLi4EEvE4typeE |
Douglas Gregor | 46287c7 | 2010-01-29 16:37:09 +0000 | [diff] [blame] | 44 | template void cstyle<4>(void*); |
Douglas Gregor | 8f51a4f | 2010-03-13 18:23:07 +0000 | [diff] [blame] | 45 | // CHECK: define weak_odr void @_ZN5Casts10functionalILj4EEEvPN9enable_ifIXleT_cvjLi4EEvE4typeE |
Douglas Gregor | 46287c7 | 2010-01-29 16:37:09 +0000 | [diff] [blame] | 46 | template void functional<4>(void*); |
Douglas Gregor | 8f51a4f | 2010-03-13 18:23:07 +0000 | [diff] [blame] | 47 | // CHECK: define weak_odr void @_ZN5Casts7static_ILj4EEEvPN9enable_ifIXleT_cvjLi4EEvE4typeE |
Douglas Gregor | 46287c7 | 2010-01-29 16:37:09 +0000 | [diff] [blame] | 48 | template void static_<4>(void*); |
| 49 | |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 50 | // CHECK: define weak_odr void @_ZN5Casts1fILi6EEENS_1TIXT_EEEv |
Douglas Gregor | 46287c7 | 2010-01-29 16:37:09 +0000 | [diff] [blame] | 51 | template T<6> f<6>(); |
Richard Smith | 967ecd3 | 2011-02-21 20:10:02 +0000 | [diff] [blame] | 52 | |
| 53 | // CHECK: define weak_odr void @_ZN5Casts5auto_IiEEvDTnw_DapicvT__EEE( |
| 54 | template void auto_<int>(int*); |
Richard Smith | 41576d4 | 2012-02-06 02:54:51 +0000 | [diff] [blame^] | 55 | |
| 56 | // CHECK: define weak_odr void @_ZN5Casts7scalar_IiEEvDTcmcvT__Ecvi_EE( |
| 57 | template void scalar_<int>(int); |
Douglas Gregor | 46287c7 | 2010-01-29 16:37:09 +0000 | [diff] [blame] | 58 | } |
John McCall | 5a7e6f7 | 2011-04-28 02:52:03 +0000 | [diff] [blame] | 59 | |
| 60 | namespace test1 { |
| 61 | short foo(short); |
| 62 | int foo(int); |
| 63 | |
| 64 | // CHECK: define linkonce_odr signext i16 @_ZN5test11aIsEEDTcl3foocvT__EEES1_( |
| 65 | template <class T> auto a(T t) -> decltype(foo(T())) { return foo(t); } |
| 66 | |
| 67 | // CHECK: define linkonce_odr signext i16 @_ZN5test11bIsEEDTcp3foocvT__EEES1_( |
| 68 | template <class T> auto b(T t) -> decltype((foo)(T())) { return (foo)(t); } |
| 69 | |
| 70 | void test(short s) { |
| 71 | a(s); |
| 72 | b(s); |
| 73 | } |
| 74 | } |
John McCall | fb44de9 | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 75 | |
| 76 | namespace test2 { |
| 77 | template <class T> void a(T x, decltype(x()) y) {} |
| 78 | template <class T> auto b(T x) -> decltype(x()) { return x(); } |
| 79 | template <class T> void c(T x, void (*p)(decltype(x()))) {} |
| 80 | template <class T> void d(T x, auto (*p)() -> decltype(x())) {} |
| 81 | template <class T> void e(auto (*p)(T y) -> decltype(y())) {} |
| 82 | template <class T> void f(void (*p)(T x, decltype(x()) y)) {} |
| 83 | template <class T> void g(T x, decltype(x()) y) { |
| 84 | static decltype(x()) variable; |
| 85 | variable = 0; |
| 86 | } |
| 87 | template <class T> void h(T x, decltype((decltype(x())(*)()) 0) y) {} |
| 88 | template <class T> void i(decltype((auto (*)(T x) -> decltype(x())) 0) y) {} |
| 89 | |
| 90 | float foo(); |
| 91 | void bar(float); |
| 92 | float baz(float(*)()); |
| 93 | void fred(float(*)(), float); |
| 94 | |
| 95 | // CHECK: define void @_ZN5test211instantiateEv |
| 96 | void instantiate() { |
| 97 | // CHECK: call void @_ZN5test21aIPFfvEEEvT_DTclfL0p_EE( |
| 98 | a(foo, 0.0f); |
| 99 | // CHECK: call float @_ZN5test21bIPFfvEEEDTclfp_EET_( |
| 100 | (void) b(foo); |
| 101 | // CHECK: call void @_ZN5test21cIPFfvEEEvT_PFvDTclfL1p_EEE( |
| 102 | c(foo, bar); |
| 103 | // CHECK: call void @_ZN5test21dIPFfvEEEvT_PFDTclfL0p_EEvE( |
| 104 | d(foo, foo); |
| 105 | // CHECK: call void @_ZN5test21eIPFfvEEEvPFDTclfp_EET_E( |
| 106 | e(baz); |
| 107 | // CHECK: call void @_ZN5test21fIPFfvEEEvPFvT_DTclfL0p_EEE( |
| 108 | f(fred); |
| 109 | // CHECK: call void @_ZN5test21gIPFfvEEEvT_DTclfL0p_EE( |
| 110 | g(foo, 0.0f); |
| 111 | // CHECK: call void @_ZN5test21hIPFfvEEEvT_DTcvPFDTclfL0p_EEvELi0EE( |
| 112 | h(foo, foo); |
| 113 | // CHECK: call void @_ZN5test21iIPFfvEEEvDTcvPFDTclfp_EET_ELi0EE( |
| 114 | i<float(*)()>(baz); |
| 115 | } |
| 116 | |
| 117 | // CHECK: store float {{.*}}, float* @_ZZN5test21gIPFfvEEEvT_DTclfL0p_EEE8variable, |
| 118 | } |
Douglas Gregor | 63f62df | 2011-06-05 05:27:58 +0000 | [diff] [blame] | 119 | |
| 120 | namespace test3 { |
| 121 | template <class T, class U> void a(T x, U y, decltype(x.*y) z) {} |
| 122 | |
| 123 | struct X { |
| 124 | int *member; |
| 125 | }; |
| 126 | |
| 127 | // CHECK: define void @_ZN5test311instantiateEv |
| 128 | void instantiate() { |
| 129 | X x; |
| 130 | int *ip; |
| 131 | // CHECK: call void @_ZN5test31aINS_1XEMS1_PiEEvT_T0_DTdsfL0p_fL0p0_E |
| 132 | a(x, &X::member, ip); |
| 133 | } |
| 134 | } |