xref: /minix3/external/bsd/llvm/dist/clang/test/SemaTemplate/instantiate-expr-4.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc // ---------------------------------------------------------------------
4*f4a2713aSLionel Sambuc // C++ Functional Casts
5*f4a2713aSLionel Sambuc // ---------------------------------------------------------------------
6*f4a2713aSLionel Sambuc template<int N>
7*f4a2713aSLionel Sambuc struct ValueInit0 {
fValueInit08*f4a2713aSLionel Sambuc   int f() {
9*f4a2713aSLionel Sambuc     return int();
10*f4a2713aSLionel Sambuc   }
11*f4a2713aSLionel Sambuc };
12*f4a2713aSLionel Sambuc 
13*f4a2713aSLionel Sambuc template struct ValueInit0<5>;
14*f4a2713aSLionel Sambuc 
15*f4a2713aSLionel Sambuc template<int N>
16*f4a2713aSLionel Sambuc struct FunctionalCast0 {
fFunctionalCast017*f4a2713aSLionel Sambuc   int f() {
18*f4a2713aSLionel Sambuc     return int(N);
19*f4a2713aSLionel Sambuc   }
20*f4a2713aSLionel Sambuc };
21*f4a2713aSLionel Sambuc 
22*f4a2713aSLionel Sambuc template struct FunctionalCast0<5>;
23*f4a2713aSLionel Sambuc 
24*f4a2713aSLionel Sambuc struct X { // expected-note 3 {{candidate constructor (the implicit copy constructor)}}
25*f4a2713aSLionel Sambuc   X(int, int); // expected-note 3 {{candidate constructor}}
26*f4a2713aSLionel Sambuc };
27*f4a2713aSLionel Sambuc 
28*f4a2713aSLionel Sambuc template<int N, int M>
29*f4a2713aSLionel Sambuc struct BuildTemporary0 {
fBuildTemporary030*f4a2713aSLionel Sambuc   X f() {
31*f4a2713aSLionel Sambuc     return X(N, M);
32*f4a2713aSLionel Sambuc   }
33*f4a2713aSLionel Sambuc };
34*f4a2713aSLionel Sambuc 
35*f4a2713aSLionel Sambuc template struct BuildTemporary0<5, 7>;
36*f4a2713aSLionel Sambuc 
37*f4a2713aSLionel Sambuc template<int N, int M>
38*f4a2713aSLionel Sambuc struct Temporaries0 {
fTemporaries039*f4a2713aSLionel Sambuc   void f() {
40*f4a2713aSLionel Sambuc     (void)X(N, M);
41*f4a2713aSLionel Sambuc   }
42*f4a2713aSLionel Sambuc };
43*f4a2713aSLionel Sambuc 
44*f4a2713aSLionel Sambuc template struct Temporaries0<5, 7>;
45*f4a2713aSLionel Sambuc 
46*f4a2713aSLionel Sambuc // Ensure that both the constructor and the destructor are instantiated by
47*f4a2713aSLionel Sambuc // checking for parse errors from each.
48*f4a2713aSLionel Sambuc template<int N> struct BadX {
BadXBadX49*f4a2713aSLionel Sambuc   BadX() { int a[-N]; } // expected-error {{array with a negative size}}
~BadXBadX50*f4a2713aSLionel Sambuc   ~BadX() { int a[-N]; } // expected-error {{array with a negative size}}
51*f4a2713aSLionel Sambuc };
52*f4a2713aSLionel Sambuc 
53*f4a2713aSLionel Sambuc template<int N>
54*f4a2713aSLionel Sambuc struct PR6671 {
fPR667155*f4a2713aSLionel Sambuc   void f() { (void)BadX<1>(); } // expected-note 2 {{instantiation}}
56*f4a2713aSLionel Sambuc };
57*f4a2713aSLionel Sambuc template struct PR6671<1>;
58*f4a2713aSLionel Sambuc 
59*f4a2713aSLionel Sambuc // ---------------------------------------------------------------------
60*f4a2713aSLionel Sambuc // new/delete expressions
61*f4a2713aSLionel Sambuc // ---------------------------------------------------------------------
62*f4a2713aSLionel Sambuc struct Y { };
63*f4a2713aSLionel Sambuc 
64*f4a2713aSLionel Sambuc template<typename T>
65*f4a2713aSLionel Sambuc struct New0 {
fNew066*f4a2713aSLionel Sambuc   T* f(bool x) {
67*f4a2713aSLionel Sambuc     if (x)
68*f4a2713aSLionel Sambuc       return new T; // expected-error{{no matching}}
69*f4a2713aSLionel Sambuc     else
70*f4a2713aSLionel Sambuc       return new T();
71*f4a2713aSLionel Sambuc   }
72*f4a2713aSLionel Sambuc };
73*f4a2713aSLionel Sambuc 
74*f4a2713aSLionel Sambuc template struct New0<int>;
75*f4a2713aSLionel Sambuc template struct New0<Y>;
76*f4a2713aSLionel Sambuc template struct New0<X>; // expected-note{{instantiation}}
77*f4a2713aSLionel Sambuc 
78*f4a2713aSLionel Sambuc template<typename T, typename Arg1>
79*f4a2713aSLionel Sambuc struct New1 {
fNew180*f4a2713aSLionel Sambuc   T* f(bool x, Arg1 a1) {
81*f4a2713aSLionel Sambuc     return new T(a1); // expected-error{{no matching}}
82*f4a2713aSLionel Sambuc   }
83*f4a2713aSLionel Sambuc };
84*f4a2713aSLionel Sambuc 
85*f4a2713aSLionel Sambuc template struct New1<int, float>;
86*f4a2713aSLionel Sambuc template struct New1<Y, Y>;
87*f4a2713aSLionel Sambuc template struct New1<X, Y>; // expected-note{{instantiation}}
88*f4a2713aSLionel Sambuc 
89*f4a2713aSLionel Sambuc template<typename T, typename Arg1, typename Arg2>
90*f4a2713aSLionel Sambuc struct New2 {
fNew291*f4a2713aSLionel Sambuc   T* f(bool x, Arg1 a1, Arg2 a2) {
92*f4a2713aSLionel Sambuc     return new T(a1, a2); // expected-error{{no matching}}
93*f4a2713aSLionel Sambuc   }
94*f4a2713aSLionel Sambuc };
95*f4a2713aSLionel Sambuc 
96*f4a2713aSLionel Sambuc template struct New2<X, int, float>;
97*f4a2713aSLionel Sambuc template struct New2<X, int, int*>; // expected-note{{instantiation}}
98*f4a2713aSLionel Sambuc // FIXME: template struct New2<int, int, float>;
99*f4a2713aSLionel Sambuc 
100*f4a2713aSLionel Sambuc // PR5833
101*f4a2713aSLionel Sambuc struct New3 {
102*f4a2713aSLionel Sambuc   New3();
103*f4a2713aSLionel Sambuc 
104*f4a2713aSLionel Sambuc   void *operator new[](__SIZE_TYPE__) __attribute__((unavailable)); // expected-note{{explicitly made unavailable}}
105*f4a2713aSLionel Sambuc };
106*f4a2713aSLionel Sambuc 
107*f4a2713aSLionel Sambuc template<class C>
object_creator()108*f4a2713aSLionel Sambuc void* object_creator() {
109*f4a2713aSLionel Sambuc   return new C(); // expected-error{{call to unavailable function 'operator new[]'}}
110*f4a2713aSLionel Sambuc }
111*f4a2713aSLionel Sambuc 
112*f4a2713aSLionel Sambuc template void *object_creator<New3[4]>(); // expected-note{{instantiation}}
113*f4a2713aSLionel Sambuc 
114*f4a2713aSLionel Sambuc template<typename T>
115*f4a2713aSLionel Sambuc struct Delete0 {
fDelete0116*f4a2713aSLionel Sambuc   void f(T t) {
117*f4a2713aSLionel Sambuc     delete t; // expected-error{{cannot delete}}
118*f4a2713aSLionel Sambuc     ::delete [] t; // expected-error{{cannot delete}}
119*f4a2713aSLionel Sambuc   }
120*f4a2713aSLionel Sambuc };
121*f4a2713aSLionel Sambuc 
122*f4a2713aSLionel Sambuc template struct Delete0<int*>;
123*f4a2713aSLionel Sambuc template struct Delete0<X*>;
124*f4a2713aSLionel Sambuc template struct Delete0<int>; // expected-note{{instantiation}}
125*f4a2713aSLionel Sambuc 
126*f4a2713aSLionel Sambuc namespace PR5755 {
127*f4a2713aSLionel Sambuc   template <class T>
Foo()128*f4a2713aSLionel Sambuc   void Foo() {
129*f4a2713aSLionel Sambuc     char* p = 0;
130*f4a2713aSLionel Sambuc     delete[] p;
131*f4a2713aSLionel Sambuc   }
132*f4a2713aSLionel Sambuc 
Test()133*f4a2713aSLionel Sambuc   void Test() {
134*f4a2713aSLionel Sambuc     Foo<int>();
135*f4a2713aSLionel Sambuc   }
136*f4a2713aSLionel Sambuc }
137*f4a2713aSLionel Sambuc 
138*f4a2713aSLionel Sambuc namespace PR10480 {
139*f4a2713aSLionel Sambuc   template<typename T>
140*f4a2713aSLionel Sambuc   struct X {
141*f4a2713aSLionel Sambuc     X();
~XPR10480::X142*f4a2713aSLionel Sambuc     ~X() {
143*f4a2713aSLionel Sambuc       T *ptr = 1; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}}
144*f4a2713aSLionel Sambuc     }
145*f4a2713aSLionel Sambuc   };
146*f4a2713aSLionel Sambuc 
147*f4a2713aSLionel Sambuc   template<typename T>
f()148*f4a2713aSLionel Sambuc   void f() {
149*f4a2713aSLionel Sambuc     new X<int>[1]; // expected-note{{in instantiation of member function 'PR10480::X<int>::~X' requested here}}
150*f4a2713aSLionel Sambuc   }
151*f4a2713aSLionel Sambuc 
152*f4a2713aSLionel Sambuc   template void f<int>();
153*f4a2713aSLionel Sambuc }
154*f4a2713aSLionel Sambuc 
155*f4a2713aSLionel Sambuc // ---------------------------------------------------------------------
156*f4a2713aSLionel Sambuc // throw expressions
157*f4a2713aSLionel Sambuc // ---------------------------------------------------------------------
158*f4a2713aSLionel Sambuc template<typename T>
159*f4a2713aSLionel Sambuc struct Throw1 {
fThrow1160*f4a2713aSLionel Sambuc   void f(T t) {
161*f4a2713aSLionel Sambuc     throw;
162*f4a2713aSLionel Sambuc     throw t; // expected-error{{incomplete type}}
163*f4a2713aSLionel Sambuc   }
164*f4a2713aSLionel Sambuc };
165*f4a2713aSLionel Sambuc 
166*f4a2713aSLionel Sambuc struct Incomplete; // expected-note 2{{forward}}
167*f4a2713aSLionel Sambuc 
168*f4a2713aSLionel Sambuc template struct Throw1<int>;
169*f4a2713aSLionel Sambuc template struct Throw1<int*>;
170*f4a2713aSLionel Sambuc template struct Throw1<Incomplete*>; // expected-note{{instantiation}}
171*f4a2713aSLionel Sambuc 
172*f4a2713aSLionel Sambuc // ---------------------------------------------------------------------
173*f4a2713aSLionel Sambuc // typeid expressions
174*f4a2713aSLionel Sambuc // ---------------------------------------------------------------------
175*f4a2713aSLionel Sambuc 
176*f4a2713aSLionel Sambuc namespace std {
177*f4a2713aSLionel Sambuc   class type_info;
178*f4a2713aSLionel Sambuc }
179*f4a2713aSLionel Sambuc 
180*f4a2713aSLionel Sambuc template<typename T>
181*f4a2713aSLionel Sambuc struct TypeId0 {
fTypeId0182*f4a2713aSLionel Sambuc   const std::type_info &f(T* ptr) {
183*f4a2713aSLionel Sambuc     if (ptr)
184*f4a2713aSLionel Sambuc       return typeid(ptr);
185*f4a2713aSLionel Sambuc     else
186*f4a2713aSLionel Sambuc       return typeid(T); // expected-error{{'typeid' of incomplete type 'Incomplete'}}
187*f4a2713aSLionel Sambuc   }
188*f4a2713aSLionel Sambuc };
189*f4a2713aSLionel Sambuc 
190*f4a2713aSLionel Sambuc struct Abstract {
191*f4a2713aSLionel Sambuc   virtual void f() = 0;
192*f4a2713aSLionel Sambuc };
193*f4a2713aSLionel Sambuc 
194*f4a2713aSLionel Sambuc template struct TypeId0<int>;
195*f4a2713aSLionel Sambuc template struct TypeId0<Incomplete>; // expected-note{{instantiation of member function}}
196*f4a2713aSLionel Sambuc template struct TypeId0<Abstract>;
197*f4a2713aSLionel Sambuc 
198*f4a2713aSLionel Sambuc // ---------------------------------------------------------------------
199*f4a2713aSLionel Sambuc // type traits
200*f4a2713aSLionel Sambuc // ---------------------------------------------------------------------
201*f4a2713aSLionel Sambuc template<typename T>
202*f4a2713aSLionel Sambuc struct is_pod {
203*f4a2713aSLionel Sambuc   static const bool value = __is_pod(T);
204*f4a2713aSLionel Sambuc };
205*f4a2713aSLionel Sambuc 
206*f4a2713aSLionel Sambuc static int is_pod0[is_pod<X>::value? -1 : 1];
207*f4a2713aSLionel Sambuc static int is_pod1[is_pod<Y>::value? 1 : -1];
208*f4a2713aSLionel Sambuc 
209*f4a2713aSLionel Sambuc // ---------------------------------------------------------------------
210*f4a2713aSLionel Sambuc // initializer lists
211*f4a2713aSLionel Sambuc // ---------------------------------------------------------------------
212*f4a2713aSLionel Sambuc template<typename T, typename Val1>
213*f4a2713aSLionel Sambuc struct InitList1 {
fInitList1214*f4a2713aSLionel Sambuc   void f(Val1 val1) {
215*f4a2713aSLionel Sambuc     T x = { val1 };
216*f4a2713aSLionel Sambuc   }
217*f4a2713aSLionel Sambuc };
218*f4a2713aSLionel Sambuc 
219*f4a2713aSLionel Sambuc struct APair {
220*f4a2713aSLionel Sambuc   int *x;
221*f4a2713aSLionel Sambuc   const float *y;
222*f4a2713aSLionel Sambuc };
223*f4a2713aSLionel Sambuc 
224*f4a2713aSLionel Sambuc template struct InitList1<int[1], float>;
225*f4a2713aSLionel Sambuc template struct InitList1<APair, int*>;
226*f4a2713aSLionel Sambuc 
227*f4a2713aSLionel Sambuc template<typename T, typename Val1, typename Val2>
228*f4a2713aSLionel Sambuc struct InitList2 {
fInitList2229*f4a2713aSLionel Sambuc   void f(Val1 val1, Val2 val2) {
230*f4a2713aSLionel Sambuc     T x = { val1, val2 }; // expected-error{{cannot initialize}}
231*f4a2713aSLionel Sambuc   }
232*f4a2713aSLionel Sambuc };
233*f4a2713aSLionel Sambuc 
234*f4a2713aSLionel Sambuc template struct InitList2<APair, int*, float*>;
235*f4a2713aSLionel Sambuc template struct InitList2<APair, int*, double*>; // expected-note{{instantiation}}
236*f4a2713aSLionel Sambuc 
237*f4a2713aSLionel Sambuc // ---------------------------------------------------------------------
238*f4a2713aSLionel Sambuc // member references
239*f4a2713aSLionel Sambuc // ---------------------------------------------------------------------
240*f4a2713aSLionel Sambuc template<typename T, typename Result>
241*f4a2713aSLionel Sambuc struct DotMemRef0 {
fDotMemRef0242*f4a2713aSLionel Sambuc   void f(T t) {
243*f4a2713aSLionel Sambuc     Result result = t.m; // expected-error{{non-const lvalue reference to type}}
244*f4a2713aSLionel Sambuc   }
245*f4a2713aSLionel Sambuc };
246*f4a2713aSLionel Sambuc 
247*f4a2713aSLionel Sambuc struct MemInt {
248*f4a2713aSLionel Sambuc   int m;
249*f4a2713aSLionel Sambuc };
250*f4a2713aSLionel Sambuc 
251*f4a2713aSLionel Sambuc struct InheritsMemInt : MemInt { };
252*f4a2713aSLionel Sambuc 
253*f4a2713aSLionel Sambuc struct MemIntFunc {
254*f4a2713aSLionel Sambuc   static int m(int);
255*f4a2713aSLionel Sambuc };
256*f4a2713aSLionel Sambuc 
257*f4a2713aSLionel Sambuc template struct DotMemRef0<MemInt, int&>;
258*f4a2713aSLionel Sambuc template struct DotMemRef0<InheritsMemInt, int&>;
259*f4a2713aSLionel Sambuc template struct DotMemRef0<MemIntFunc, int (*)(int)>;
260*f4a2713aSLionel Sambuc template struct DotMemRef0<MemInt, float&>; // expected-note{{instantiation}}
261*f4a2713aSLionel Sambuc 
262*f4a2713aSLionel Sambuc template<typename T, typename Result>
263*f4a2713aSLionel Sambuc struct ArrowMemRef0 {
fArrowMemRef0264*f4a2713aSLionel Sambuc   void f(T t) {
265*f4a2713aSLionel Sambuc     Result result = t->m; // expected-error 2{{non-const lvalue reference}}
266*f4a2713aSLionel Sambuc   }
267*f4a2713aSLionel Sambuc };
268*f4a2713aSLionel Sambuc 
269*f4a2713aSLionel Sambuc template<typename T>
270*f4a2713aSLionel Sambuc struct ArrowWrapper {
271*f4a2713aSLionel Sambuc   T operator->();
272*f4a2713aSLionel Sambuc };
273*f4a2713aSLionel Sambuc 
274*f4a2713aSLionel Sambuc template struct ArrowMemRef0<MemInt*, int&>;
275*f4a2713aSLionel Sambuc template struct ArrowMemRef0<InheritsMemInt*, int&>;
276*f4a2713aSLionel Sambuc template struct ArrowMemRef0<MemIntFunc*, int (*)(int)>;
277*f4a2713aSLionel Sambuc template struct ArrowMemRef0<MemInt*, float&>; // expected-note{{instantiation}}
278*f4a2713aSLionel Sambuc 
279*f4a2713aSLionel Sambuc template struct ArrowMemRef0<ArrowWrapper<MemInt*>, int&>;
280*f4a2713aSLionel Sambuc template struct ArrowMemRef0<ArrowWrapper<InheritsMemInt*>, int&>;
281*f4a2713aSLionel Sambuc template struct ArrowMemRef0<ArrowWrapper<MemIntFunc*>, int (*)(int)>;
282*f4a2713aSLionel Sambuc template struct ArrowMemRef0<ArrowWrapper<MemInt*>, float&>; // expected-note{{instantiation}}
283*f4a2713aSLionel Sambuc template struct ArrowMemRef0<ArrowWrapper<ArrowWrapper<MemInt*> >, int&>;
284*f4a2713aSLionel Sambuc 
285*f4a2713aSLionel Sambuc struct UnresolvedMemRefArray {
286*f4a2713aSLionel Sambuc   int f(int);
287*f4a2713aSLionel Sambuc   int f(char);
288*f4a2713aSLionel Sambuc };
289*f4a2713aSLionel Sambuc UnresolvedMemRefArray Arr[10];
UnresolvedMemRefArrayT(U u)290*f4a2713aSLionel Sambuc template<typename U> int UnresolvedMemRefArrayT(U u) {
291*f4a2713aSLionel Sambuc   return Arr->f(u);
292*f4a2713aSLionel Sambuc }
293*f4a2713aSLionel Sambuc template int UnresolvedMemRefArrayT<int>(int);
294*f4a2713aSLionel Sambuc 
295*f4a2713aSLionel Sambuc // FIXME: we should be able to return a MemInt without the reference!
296*f4a2713aSLionel Sambuc MemInt &createMemInt(int);
297*f4a2713aSLionel Sambuc 
298*f4a2713aSLionel Sambuc template<int N>
299*f4a2713aSLionel Sambuc struct NonDepMemberExpr0 {
fNonDepMemberExpr0300*f4a2713aSLionel Sambuc   void f() {
301*f4a2713aSLionel Sambuc     createMemInt(N).m = N;
302*f4a2713aSLionel Sambuc   }
303*f4a2713aSLionel Sambuc };
304*f4a2713aSLionel Sambuc 
305*f4a2713aSLionel Sambuc template struct NonDepMemberExpr0<0>;
306*f4a2713aSLionel Sambuc 
307*f4a2713aSLionel Sambuc template<typename T, typename Result>
308*f4a2713aSLionel Sambuc struct MemberFuncCall0 {
fMemberFuncCall0309*f4a2713aSLionel Sambuc   void f(T t) {
310*f4a2713aSLionel Sambuc     Result result = t.f();
311*f4a2713aSLionel Sambuc   }
312*f4a2713aSLionel Sambuc };
313*f4a2713aSLionel Sambuc 
314*f4a2713aSLionel Sambuc template<typename T>
315*f4a2713aSLionel Sambuc struct HasMemFunc0 {
316*f4a2713aSLionel Sambuc   T f();
317*f4a2713aSLionel Sambuc };
318*f4a2713aSLionel Sambuc 
319*f4a2713aSLionel Sambuc 
320*f4a2713aSLionel Sambuc template struct MemberFuncCall0<HasMemFunc0<int&>, const int&>;
321*f4a2713aSLionel Sambuc 
322*f4a2713aSLionel Sambuc template<typename Result>
323*f4a2713aSLionel Sambuc struct ThisMemberFuncCall0 {
324*f4a2713aSLionel Sambuc   Result g();
325*f4a2713aSLionel Sambuc 
fThisMemberFuncCall0326*f4a2713aSLionel Sambuc   void f() {
327*f4a2713aSLionel Sambuc     Result r1 = g();
328*f4a2713aSLionel Sambuc     Result r2 = this->g();
329*f4a2713aSLionel Sambuc   }
330*f4a2713aSLionel Sambuc };
331*f4a2713aSLionel Sambuc 
332*f4a2713aSLionel Sambuc template struct ThisMemberFuncCall0<int&>;
333*f4a2713aSLionel Sambuc 
334*f4a2713aSLionel Sambuc template<typename T>
335*f4a2713aSLionel Sambuc struct NonDepMemberCall0 {
fooNonDepMemberCall0336*f4a2713aSLionel Sambuc   void foo(HasMemFunc0<int&> x) {
337*f4a2713aSLionel Sambuc     T result = x.f(); // expected-error{{non-const lvalue reference}}
338*f4a2713aSLionel Sambuc   }
339*f4a2713aSLionel Sambuc };
340*f4a2713aSLionel Sambuc 
341*f4a2713aSLionel Sambuc template struct NonDepMemberCall0<int&>;
342*f4a2713aSLionel Sambuc template struct NonDepMemberCall0<const int&>;
343*f4a2713aSLionel Sambuc template struct NonDepMemberCall0<float&>; // expected-note{{instantiation}}
344*f4a2713aSLionel Sambuc 
345*f4a2713aSLionel Sambuc 
346*f4a2713aSLionel Sambuc template<typename T>
347*f4a2713aSLionel Sambuc struct QualifiedDeclRef0 {
fQualifiedDeclRef0348*f4a2713aSLionel Sambuc   T f() {
349*f4a2713aSLionel Sambuc     return is_pod<X>::value; // expected-error{{non-const lvalue reference to type 'int' cannot bind to a value of unrelated type 'const bool'}}
350*f4a2713aSLionel Sambuc   }
351*f4a2713aSLionel Sambuc };
352*f4a2713aSLionel Sambuc 
353*f4a2713aSLionel Sambuc template struct QualifiedDeclRef0<bool>;
354*f4a2713aSLionel Sambuc template struct QualifiedDeclRef0<int&>; // expected-note{{instantiation}}
355