blob: 99bbbf7a162c0f8229e65d0264453a770287af87 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Douglas Gregord94546a2009-05-20 21:38:11 +00002
3// ---------------------------------------------------------------------
4// C++ Functional Casts
5// ---------------------------------------------------------------------
6template<int N>
7struct ValueInit0 {
8 int f() {
9 return int();
10 }
11};
12
13template struct ValueInit0<5>;
14
15template<int N>
16struct FunctionalCast0 {
17 int f() {
18 return int(N);
19 }
20};
21
22template struct FunctionalCast0<5>;
23
Douglas Gregor99a2e602009-12-16 01:38:02 +000024struct X { // expected-note 3 {{candidate function}}
25 X(int, int); // expected-note 3 {{candidate function}}
Douglas Gregord94546a2009-05-20 21:38:11 +000026};
27
28template<int N, int M>
29struct BuildTemporary0 {
30 X f() {
31 return X(N, M);
32 }
33};
34
35template struct BuildTemporary0<5, 7>;
Douglas Gregore06274d2009-05-20 21:51:01 +000036
37template<int N, int M>
38struct Temporaries0 {
39 void f() {
40 (void)X(N, M);
41 }
42};
43
44template struct Temporaries0<5, 7>;
Douglas Gregor3433cf72009-05-21 00:00:09 +000045
46// ---------------------------------------------------------------------
Douglas Gregord0c02672009-05-21 17:21:12 +000047// new/delete expressions
Douglas Gregor3433cf72009-05-21 00:00:09 +000048// ---------------------------------------------------------------------
49struct Y { };
50
51template<typename T>
52struct New0 {
53 T* f(bool x) {
54 if (x)
55 return new T; // expected-error{{no matching}}
56 else
57 return new T();
58 }
59};
60
61template struct New0<int>;
62template struct New0<Y>;
63template struct New0<X>; // expected-note{{instantiation}}
64
65template<typename T, typename Arg1>
66struct New1 {
67 T* f(bool x, Arg1 a1) {
68 return new T(a1); // expected-error{{no matching}}
69 }
70};
71
72template struct New1<int, float>;
73template struct New1<Y, Y>;
74template struct New1<X, Y>; // expected-note{{instantiation}}
75
76template<typename T, typename Arg1, typename Arg2>
77struct New2 {
78 T* f(bool x, Arg1 a1, Arg2 a2) {
79 return new T(a1, a2); // expected-error{{no matching}}
80 }
81};
82
83template struct New2<X, int, float>;
84template struct New2<X, int, int*>; // expected-note{{instantiation}}
85// FIXME: template struct New2<int, int, float>;
Douglas Gregord0c02672009-05-21 17:21:12 +000086
Douglas Gregor5b5ad842009-12-22 17:13:37 +000087// PR5833
88struct New3 {
89 New3();
90
91 void *operator new[](__SIZE_TYPE__) __attribute__((unavailable)); // expected-note{{explicitly made unavailable}}
92};
93
94template<class C>
95void* object_creator() {
96 return new C(); // expected-error{{call to unavailable function 'operator new[]'}}
97}
98
99template void *object_creator<New3[4]>(); // expected-note{{instantiation}}
100
Douglas Gregord0c02672009-05-21 17:21:12 +0000101template<typename T>
102struct Delete0 {
103 void f(T t) {
104 delete t; // expected-error{{cannot delete}}
105 ::delete [] t;
106 }
107};
108
109template struct Delete0<int*>;
110template struct Delete0<X*>;
111template struct Delete0<int>; // expected-note{{instantiation}}
Douglas Gregor42e5b502009-05-21 17:37:52 +0000112
Douglas Gregora88cfbf2009-12-12 18:16:41 +0000113namespace PR5755 {
114 template <class T>
115 void Foo() {
116 char* p = 0;
117 delete[] p;
118 }
119
120 void Test() {
121 Foo<int>();
122 }
123}
124
Douglas Gregor42e5b502009-05-21 17:37:52 +0000125// ---------------------------------------------------------------------
126// throw expressions
127// ---------------------------------------------------------------------
128template<typename T>
129struct Throw1 {
130 void f(T t) {
131 throw;
132 throw t; // expected-error{{incomplete type}}
133 }
134};
135
136struct Incomplete; // expected-note{{forward}}
137
138template struct Throw1<int>;
139template struct Throw1<int*>;
140template struct Throw1<Incomplete*>; // expected-note{{instantiation}}
141
Douglas Gregor12d0c302009-05-21 18:34:44 +0000142// ---------------------------------------------------------------------
143// typeid expressions
144// ---------------------------------------------------------------------
145
146// FIXME: This should really include <typeinfo>, but we don't have that yet.
147namespace std {
148 class type_info;
149}
150
151template<typename T>
152struct TypeId0 {
153 const std::type_info &f(T* ptr) {
154 if (ptr)
155 return typeid(ptr);
156 else
157 return typeid(T);
158 }
159};
160
161struct Abstract {
162 virtual void f() = 0;
163};
164
165template struct TypeId0<int>;
166template struct TypeId0<Incomplete>;
167template struct TypeId0<Abstract>;
Douglas Gregor36bb03b2009-05-21 18:55:48 +0000168
169// ---------------------------------------------------------------------
170// type traits
171// ---------------------------------------------------------------------
172template<typename T>
173struct is_pod {
174 static const bool value = __is_pod(T);
175};
176
177static const int is_pod0[is_pod<X>::value? -1 : 1];
178static const int is_pod1[is_pod<Y>::value? 1 : -1];
Douglas Gregorccb97f52009-05-21 21:38:12 +0000179
180// ---------------------------------------------------------------------
181// initializer lists
182// ---------------------------------------------------------------------
183template<typename T, typename Val1>
184struct InitList1 {
185 void f(Val1 val1) {
186 T x = { val1 };
187 }
188};
189
190struct APair {
191 int *x;
192 const float *y;
193};
194
195template struct InitList1<int[1], float>;
196template struct InitList1<APair, int*>;
197
198template<typename T, typename Val1, typename Val2>
199struct InitList2 {
200 void f(Val1 val1, Val2 val2) {
201 T x = { val1, val2 }; // expected-error{{incompatible}}
202 }
203};
204
205template struct InitList2<APair, int*, float*>;
206template struct InitList2<APair, int*, double*>; // expected-note{{instantiation}}
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000207
208// ---------------------------------------------------------------------
209// member references
210// ---------------------------------------------------------------------
211template<typename T, typename Result>
212struct DotMemRef0 {
213 void f(T t) {
Douglas Gregor20093b42009-12-09 23:02:17 +0000214 Result result = t.m; // expected-error{{non-const lvalue reference to type}}
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000215 }
216};
217
218struct MemInt {
219 int m;
220};
221
222struct InheritsMemInt : MemInt { };
223
224struct MemIntFunc {
225 static int m(int);
226};
227
228template struct DotMemRef0<MemInt, int&>;
229template struct DotMemRef0<InheritsMemInt, int&>;
230template struct DotMemRef0<MemIntFunc, int (*)(int)>;
231template struct DotMemRef0<MemInt, float&>; // expected-note{{instantiation}}
232
233template<typename T, typename Result>
234struct ArrowMemRef0 {
235 void f(T t) {
Douglas Gregor20093b42009-12-09 23:02:17 +0000236 Result result = t->m; // expected-error 2{{non-const lvalue reference}}
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000237 }
238};
239
240template<typename T>
241struct ArrowWrapper {
242 T operator->();
243};
244
245template struct ArrowMemRef0<MemInt*, int&>;
246template struct ArrowMemRef0<InheritsMemInt*, int&>;
247template struct ArrowMemRef0<MemIntFunc*, int (*)(int)>;
248template struct ArrowMemRef0<MemInt*, float&>; // expected-note{{instantiation}}
249
250template struct ArrowMemRef0<ArrowWrapper<MemInt*>, int&>;
251template struct ArrowMemRef0<ArrowWrapper<InheritsMemInt*>, int&>;
252template struct ArrowMemRef0<ArrowWrapper<MemIntFunc*>, int (*)(int)>;
253template struct ArrowMemRef0<ArrowWrapper<MemInt*>, float&>; // expected-note{{instantiation}}
254template struct ArrowMemRef0<ArrowWrapper<ArrowWrapper<MemInt*> >, int&>;
255
256// FIXME: we should be able to return a MemInt without the reference!
257MemInt &createMemInt(int);
258
259template<int N>
260struct NonDepMemberExpr0 {
261 void f() {
262 createMemInt(N).m = N;
263 }
264};
265
266template struct NonDepMemberExpr0<0>;
Douglas Gregor08d3e7c2009-05-22 21:26:58 +0000267
268template<typename T, typename Result>
269struct MemberFuncCall0 {
270 void f(T t) {
271 Result result = t.f();
272 }
273};
274
275template<typename T>
276struct HasMemFunc0 {
277 T f();
278};
279
280
281template struct MemberFuncCall0<HasMemFunc0<int&>, const int&>;
282
283template<typename Result>
284struct ThisMemberFuncCall0 {
285 Result g();
286
287 void f() {
288 Result r1 = g();
289 Result r2 = this->g();
290 }
291};
292
293template struct ThisMemberFuncCall0<int&>;
294
295template<typename T>
296struct NonDepMemberCall0 {
297 void foo(HasMemFunc0<int&> x) {
Douglas Gregor20093b42009-12-09 23:02:17 +0000298 T result = x.f(); // expected-error{{non-const lvalue reference}}
Douglas Gregor08d3e7c2009-05-22 21:26:58 +0000299 }
300};
301
302template struct NonDepMemberCall0<int&>;
303template struct NonDepMemberCall0<const int&>;
304template struct NonDepMemberCall0<float&>; // expected-note{{instantiation}}
Douglas Gregore30d0bd2009-05-22 23:47:06 +0000305
306
307template<typename T>
308struct QualifiedDeclRef0 {
309 T f() {
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000310 return is_pod<X>::value; // expected-error{{non-const lvalue reference to type 'int' cannot bind to a value of unrelated type 'bool const'}}
Douglas Gregore30d0bd2009-05-22 23:47:06 +0000311 }
312};
313
314template struct QualifiedDeclRef0<bool>;
315template struct QualifiedDeclRef0<int&>; // expected-note{{instantiation}}