blob: 2d7a883526dbbded8ca2c101960d361b350bc8bb [file] [log] [blame]
Richard Smith762bb9d2011-10-13 22:29:44 +00001// RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple=x86_64-apple-darwin9 | FileCheck %s
Douglas Gregor46287c72010-01-29 16:37:09 +00002
Sebastian Redlb76ffc52012-02-25 20:51:07 +00003namespace std {
4 typedef decltype(sizeof(int)) size_t;
5
6 // libc++'s implementation
7 template <class _E>
8 class initializer_list
9 {
10 const _E* __begin_;
11 size_t __size_;
12
13 initializer_list(const _E* __b, size_t __s)
14 : __begin_(__b),
15 __size_(__s)
16 {}
17
18 public:
19 typedef _E value_type;
20 typedef const _E& reference;
21 typedef const _E& const_reference;
22 typedef size_t size_type;
23
24 typedef const _E* iterator;
25 typedef const _E* const_iterator;
26
27 initializer_list() : __begin_(nullptr), __size_(0) {}
28
29 size_t size() const {return __size_;}
30 const _E* begin() const {return __begin_;}
31 const _E* end() const {return __begin_ + __size_;}
32 };
33}
34
Douglas Gregor46287c72010-01-29 16:37:09 +000035template < bool condition, typename T = void >
36struct enable_if { typedef T type; };
37
38template< typename T >
39struct enable_if< false, T > {};
40
41// PR5876
42namespace Casts {
43 template< unsigned O >
44 void implicit(typename enable_if< O <= 4 >::type* = 0) {
45 }
46
47 template< unsigned O >
48 void cstyle(typename enable_if< O <= (unsigned)4 >::type* = 0) {
49 }
50
51 template< unsigned O >
52 void functional(typename enable_if< O <= unsigned(4) >::type* = 0) {
53 }
54
55 template< unsigned O >
56 void static_(typename enable_if< O <= static_cast<unsigned>(4) >::type* = 0) {
57 }
58
Richard Smith967ecd32011-02-21 20:10:02 +000059 template< typename T >
60 void auto_(decltype(new auto(T()))) {
61 }
62
Richard Smith41576d42012-02-06 02:54:51 +000063 template< typename T >
64 void scalar_(decltype(T(), int())) {
65 }
66
Douglas Gregor46287c72010-01-29 16:37:09 +000067 // FIXME: Test const_cast, reinterpret_cast, dynamic_cast, which are
68 // a bit harder to use in template arguments.
69 template <unsigned N> struct T {};
70
71 template <int N> T<N> f() { return T<N>(); }
72
Douglas Gregor8f51a4f2010-03-13 18:23:07 +000073 // CHECK: define weak_odr void @_ZN5Casts8implicitILj4EEEvPN9enable_ifIXleT_Li4EEvE4typeE
Douglas Gregor46287c72010-01-29 16:37:09 +000074 template void implicit<4>(void*);
Douglas Gregor8f51a4f2010-03-13 18:23:07 +000075 // CHECK: define weak_odr void @_ZN5Casts6cstyleILj4EEEvPN9enable_ifIXleT_cvjLi4EEvE4typeE
Douglas Gregor46287c72010-01-29 16:37:09 +000076 template void cstyle<4>(void*);
Douglas Gregor8f51a4f2010-03-13 18:23:07 +000077 // CHECK: define weak_odr void @_ZN5Casts10functionalILj4EEEvPN9enable_ifIXleT_cvjLi4EEvE4typeE
Douglas Gregor46287c72010-01-29 16:37:09 +000078 template void functional<4>(void*);
Douglas Gregor8f51a4f2010-03-13 18:23:07 +000079 // CHECK: define weak_odr void @_ZN5Casts7static_ILj4EEEvPN9enable_ifIXleT_cvjLi4EEvE4typeE
Douglas Gregor46287c72010-01-29 16:37:09 +000080 template void static_<4>(void*);
81
Chris Lattner117e3f42010-07-30 04:02:24 +000082 // CHECK: define weak_odr void @_ZN5Casts1fILi6EEENS_1TIXT_EEEv
Douglas Gregor46287c72010-01-29 16:37:09 +000083 template T<6> f<6>();
Richard Smith967ecd32011-02-21 20:10:02 +000084
85 // CHECK: define weak_odr void @_ZN5Casts5auto_IiEEvDTnw_DapicvT__EEE(
86 template void auto_<int>(int*);
Richard Smith41576d42012-02-06 02:54:51 +000087
88 // CHECK: define weak_odr void @_ZN5Casts7scalar_IiEEvDTcmcvT__Ecvi_EE(
89 template void scalar_<int>(int);
Douglas Gregor46287c72010-01-29 16:37:09 +000090}
John McCall5a7e6f72011-04-28 02:52:03 +000091
92namespace test1 {
93 short foo(short);
94 int foo(int);
95
96 // CHECK: define linkonce_odr signext i16 @_ZN5test11aIsEEDTcl3foocvT__EEES1_(
97 template <class T> auto a(T t) -> decltype(foo(T())) { return foo(t); }
98
99 // CHECK: define linkonce_odr signext i16 @_ZN5test11bIsEEDTcp3foocvT__EEES1_(
100 template <class T> auto b(T t) -> decltype((foo)(T())) { return (foo)(t); }
101
102 void test(short s) {
103 a(s);
104 b(s);
105 }
106}
John McCallfb44de92011-05-01 22:35:37 +0000107
108namespace test2 {
109 template <class T> void a(T x, decltype(x()) y) {}
110 template <class T> auto b(T x) -> decltype(x()) { return x(); }
111 template <class T> void c(T x, void (*p)(decltype(x()))) {}
112 template <class T> void d(T x, auto (*p)() -> decltype(x())) {}
113 template <class T> void e(auto (*p)(T y) -> decltype(y())) {}
114 template <class T> void f(void (*p)(T x, decltype(x()) y)) {}
115 template <class T> void g(T x, decltype(x()) y) {
116 static decltype(x()) variable;
117 variable = 0;
118 }
119 template <class T> void h(T x, decltype((decltype(x())(*)()) 0) y) {}
120 template <class T> void i(decltype((auto (*)(T x) -> decltype(x())) 0) y) {}
121
122 float foo();
123 void bar(float);
124 float baz(float(*)());
125 void fred(float(*)(), float);
126
127 // CHECK: define void @_ZN5test211instantiateEv
128 void instantiate() {
129 // CHECK: call void @_ZN5test21aIPFfvEEEvT_DTclfL0p_EE(
130 a(foo, 0.0f);
131 // CHECK: call float @_ZN5test21bIPFfvEEEDTclfp_EET_(
132 (void) b(foo);
133 // CHECK: call void @_ZN5test21cIPFfvEEEvT_PFvDTclfL1p_EEE(
134 c(foo, bar);
135 // CHECK: call void @_ZN5test21dIPFfvEEEvT_PFDTclfL0p_EEvE(
136 d(foo, foo);
137 // CHECK: call void @_ZN5test21eIPFfvEEEvPFDTclfp_EET_E(
138 e(baz);
139 // CHECK: call void @_ZN5test21fIPFfvEEEvPFvT_DTclfL0p_EEE(
140 f(fred);
141 // CHECK: call void @_ZN5test21gIPFfvEEEvT_DTclfL0p_EE(
142 g(foo, 0.0f);
143 // CHECK: call void @_ZN5test21hIPFfvEEEvT_DTcvPFDTclfL0p_EEvELi0EE(
144 h(foo, foo);
145 // CHECK: call void @_ZN5test21iIPFfvEEEvDTcvPFDTclfp_EET_ELi0EE(
146 i<float(*)()>(baz);
147 }
148
149 // CHECK: store float {{.*}}, float* @_ZZN5test21gIPFfvEEEvT_DTclfL0p_EEE8variable,
150}
Douglas Gregor63f62df2011-06-05 05:27:58 +0000151
152namespace test3 {
153 template <class T, class U> void a(T x, U y, decltype(x.*y) z) {}
154
155 struct X {
156 int *member;
157 };
158
159 // CHECK: define void @_ZN5test311instantiateEv
160 void instantiate() {
161 X x;
162 int *ip;
163 // CHECK: call void @_ZN5test31aINS_1XEMS1_PiEEvT_T0_DTdsfL0p_fL0p0_E
164 a(x, &X::member, ip);
165 }
166}
Sebastian Redlb76ffc52012-02-25 20:51:07 +0000167
168namespace test4 {
169 struct X {
170 X(int);
Sebastian Redlb76ffc52012-02-25 20:51:07 +0000171 };
172
173 template <typename T>
174 void tf1(decltype(new T(1)) p)
175 {}
176
177 template <typename T>
178 void tf2(decltype(new T({1})) p)
179 {}
180
181 template <typename T>
182 void tf3(decltype(new T{1}) p)
183 {}
184
185 // CHECK: void @_ZN5test43tf1INS_1XEEEvDTnw_T_piLi1EEE
186 template void tf1<X>(X*);
187
Sebastian Redlfaf4ef62012-02-25 22:59:28 +0000188 // CHECK: void @_ZN5test43tf2INS_1XEEEvDTnw_T_piilLi1EEEE
189 template void tf2<X>(X*);
Sebastian Redlb76ffc52012-02-25 20:51:07 +0000190
Sebastian Redlfaf4ef62012-02-25 22:59:28 +0000191 // CHECK: void @_ZN5test43tf3INS_1XEEEvDTnw_T_ilLi1EEE
Sebastian Redlb76ffc52012-02-25 20:51:07 +0000192 template void tf3<X>(X*);
John McCall9653ab52012-09-25 09:10:17 +0000193
194}
195
196namespace test5 {
197 template <typename T> void a(decltype(noexcept(T()))) {}
198 template void a<int>(decltype(noexcept(int())));
199 // CHECK: void @_ZN5test51aIiEEvDTnxcvT__EE(
Sebastian Redlb76ffc52012-02-25 20:51:07 +0000200}