1 // RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple=x86_64-apple-darwin9 | FileCheck %s
2 
3 namespace 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 
35 template < bool condition, typename T = void >
36 struct enable_if { typedef T type; };
37 
38 template< typename T >
39 struct enable_if< false, T > {};
40 
41 // PR5876
42 namespace 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 
59   template< typename T >
60   void auto_(decltype(new auto(T()))) {
61   }
62 
63   template< typename T >
64   void scalar_(decltype(T(), int())) {
65   }
66 
67   // 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 
73   // CHECK-LABEL: define weak_odr void @_ZN5Casts8implicitILj4EEEvPN9enable_ifIXleT_Li4EEvE4typeE
74   template void implicit<4>(void*);
75   // CHECK-LABEL: define weak_odr void @_ZN5Casts6cstyleILj4EEEvPN9enable_ifIXleT_cvjLi4EEvE4typeE
76   template void cstyle<4>(void*);
77   // CHECK-LABEL: define weak_odr void @_ZN5Casts10functionalILj4EEEvPN9enable_ifIXleT_cvjLi4EEvE4typeE
78   template void functional<4>(void*);
79   // CHECK-LABEL: define weak_odr void @_ZN5Casts7static_ILj4EEEvPN9enable_ifIXleT_cvjLi4EEvE4typeE
80   template void static_<4>(void*);
81 
82   // CHECK-LABEL: define weak_odr void @_ZN5Casts1fILi6EEENS_1TIXT_EEEv
83   template T<6> f<6>();
84 
85   // CHECK-LABEL: define weak_odr void @_ZN5Casts5auto_IiEEvDTnw_DapicvT__EEE(
86   template void auto_<int>(int*);
87 
88   // CHECK-LABEL: define weak_odr void @_ZN5Casts7scalar_IiEEvDTcmcvT__Ecvi_EE(
89   template void scalar_<int>(int);
90 }
91 
92 namespace test1 {
93   short foo(short);
94   int foo(int);
95 
96   // CHECK-LABEL: define linkonce_odr signext i16 @_ZN5test11aIsEEDTcl3foocvT__EEES1_(
97   template <class T> auto a(T t) -> decltype(foo(T())) { return foo(t); }
98 
99   // CHECK-LABEL: 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 }
107 
108 namespace 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-LABEL: 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 }
151 
152 namespace 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-LABEL: 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 }
167 
168 namespace test4 {
169   struct X {
170     X(int);
171   };
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 
188   // CHECK: void @_ZN5test43tf2INS_1XEEEvDTnw_T_piilLi1EEEE
189   template void tf2<X>(X*);
190 
191   // CHECK: void @_ZN5test43tf3INS_1XEEEvDTnw_T_ilLi1EEE
192   template void tf3<X>(X*);
193 
194 }
195 
196 namespace test5 {
197   template <typename T> void a(decltype(noexcept(T()))) {}
198   template void a<int>(decltype(noexcept(int())));
199   // CHECK: void @_ZN5test51aIiEEvDTnxcvT__EE(
200 }
201