blob: e0042de50e63d4f9386a97a54445d5a43f6ba0a4 [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
87template<typename T>
88struct Delete0 {
89 void f(T t) {
90 delete t; // expected-error{{cannot delete}}
91 ::delete [] t;
92 }
93};
94
95template struct Delete0<int*>;
96template struct Delete0<X*>;
97template struct Delete0<int>; // expected-note{{instantiation}}
Douglas Gregor42e5b502009-05-21 17:37:52 +000098
Douglas Gregora88cfbf2009-12-12 18:16:41 +000099namespace PR5755 {
100 template <class T>
101 void Foo() {
102 char* p = 0;
103 delete[] p;
104 }
105
106 void Test() {
107 Foo<int>();
108 }
109}
110
Douglas Gregor42e5b502009-05-21 17:37:52 +0000111// ---------------------------------------------------------------------
112// throw expressions
113// ---------------------------------------------------------------------
114template<typename T>
115struct Throw1 {
116 void f(T t) {
117 throw;
118 throw t; // expected-error{{incomplete type}}
119 }
120};
121
122struct Incomplete; // expected-note{{forward}}
123
124template struct Throw1<int>;
125template struct Throw1<int*>;
126template struct Throw1<Incomplete*>; // expected-note{{instantiation}}
127
Douglas Gregor12d0c302009-05-21 18:34:44 +0000128// ---------------------------------------------------------------------
129// typeid expressions
130// ---------------------------------------------------------------------
131
132// FIXME: This should really include <typeinfo>, but we don't have that yet.
133namespace std {
134 class type_info;
135}
136
137template<typename T>
138struct TypeId0 {
139 const std::type_info &f(T* ptr) {
140 if (ptr)
141 return typeid(ptr);
142 else
143 return typeid(T);
144 }
145};
146
147struct Abstract {
148 virtual void f() = 0;
149};
150
151template struct TypeId0<int>;
152template struct TypeId0<Incomplete>;
153template struct TypeId0<Abstract>;
Douglas Gregor36bb03b2009-05-21 18:55:48 +0000154
155// ---------------------------------------------------------------------
156// type traits
157// ---------------------------------------------------------------------
158template<typename T>
159struct is_pod {
160 static const bool value = __is_pod(T);
161};
162
163static const int is_pod0[is_pod<X>::value? -1 : 1];
164static const int is_pod1[is_pod<Y>::value? 1 : -1];
Douglas Gregorccb97f52009-05-21 21:38:12 +0000165
166// ---------------------------------------------------------------------
167// initializer lists
168// ---------------------------------------------------------------------
169template<typename T, typename Val1>
170struct InitList1 {
171 void f(Val1 val1) {
172 T x = { val1 };
173 }
174};
175
176struct APair {
177 int *x;
178 const float *y;
179};
180
181template struct InitList1<int[1], float>;
182template struct InitList1<APair, int*>;
183
184template<typename T, typename Val1, typename Val2>
185struct InitList2 {
186 void f(Val1 val1, Val2 val2) {
187 T x = { val1, val2 }; // expected-error{{incompatible}}
188 }
189};
190
191template struct InitList2<APair, int*, float*>;
192template struct InitList2<APair, int*, double*>; // expected-note{{instantiation}}
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000193
194// ---------------------------------------------------------------------
195// member references
196// ---------------------------------------------------------------------
197template<typename T, typename Result>
198struct DotMemRef0 {
199 void f(T t) {
Douglas Gregor20093b42009-12-09 23:02:17 +0000200 Result result = t.m; // expected-error{{non-const lvalue reference to type}}
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000201 }
202};
203
204struct MemInt {
205 int m;
206};
207
208struct InheritsMemInt : MemInt { };
209
210struct MemIntFunc {
211 static int m(int);
212};
213
214template struct DotMemRef0<MemInt, int&>;
215template struct DotMemRef0<InheritsMemInt, int&>;
216template struct DotMemRef0<MemIntFunc, int (*)(int)>;
217template struct DotMemRef0<MemInt, float&>; // expected-note{{instantiation}}
218
219template<typename T, typename Result>
220struct ArrowMemRef0 {
221 void f(T t) {
Douglas Gregor20093b42009-12-09 23:02:17 +0000222 Result result = t->m; // expected-error 2{{non-const lvalue reference}}
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000223 }
224};
225
226template<typename T>
227struct ArrowWrapper {
228 T operator->();
229};
230
231template struct ArrowMemRef0<MemInt*, int&>;
232template struct ArrowMemRef0<InheritsMemInt*, int&>;
233template struct ArrowMemRef0<MemIntFunc*, int (*)(int)>;
234template struct ArrowMemRef0<MemInt*, float&>; // expected-note{{instantiation}}
235
236template struct ArrowMemRef0<ArrowWrapper<MemInt*>, int&>;
237template struct ArrowMemRef0<ArrowWrapper<InheritsMemInt*>, int&>;
238template struct ArrowMemRef0<ArrowWrapper<MemIntFunc*>, int (*)(int)>;
239template struct ArrowMemRef0<ArrowWrapper<MemInt*>, float&>; // expected-note{{instantiation}}
240template struct ArrowMemRef0<ArrowWrapper<ArrowWrapper<MemInt*> >, int&>;
241
242// FIXME: we should be able to return a MemInt without the reference!
243MemInt &createMemInt(int);
244
245template<int N>
246struct NonDepMemberExpr0 {
247 void f() {
248 createMemInt(N).m = N;
249 }
250};
251
252template struct NonDepMemberExpr0<0>;
Douglas Gregor08d3e7c2009-05-22 21:26:58 +0000253
254template<typename T, typename Result>
255struct MemberFuncCall0 {
256 void f(T t) {
257 Result result = t.f();
258 }
259};
260
261template<typename T>
262struct HasMemFunc0 {
263 T f();
264};
265
266
267template struct MemberFuncCall0<HasMemFunc0<int&>, const int&>;
268
269template<typename Result>
270struct ThisMemberFuncCall0 {
271 Result g();
272
273 void f() {
274 Result r1 = g();
275 Result r2 = this->g();
276 }
277};
278
279template struct ThisMemberFuncCall0<int&>;
280
281template<typename T>
282struct NonDepMemberCall0 {
283 void foo(HasMemFunc0<int&> x) {
Douglas Gregor20093b42009-12-09 23:02:17 +0000284 T result = x.f(); // expected-error{{non-const lvalue reference}}
Douglas Gregor08d3e7c2009-05-22 21:26:58 +0000285 }
286};
287
288template struct NonDepMemberCall0<int&>;
289template struct NonDepMemberCall0<const int&>;
290template struct NonDepMemberCall0<float&>; // expected-note{{instantiation}}
Douglas Gregore30d0bd2009-05-22 23:47:06 +0000291
292
293template<typename T>
294struct QualifiedDeclRef0 {
295 T f() {
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000296 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 +0000297 }
298};
299
300template struct QualifiedDeclRef0<bool>;
301template struct QualifiedDeclRef0<int&>; // expected-note{{instantiation}}