blob: 82f000b805de680f5a2fa86c1bbc3cc550f9b898 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Douglas Gregoreaebc752008-11-06 23:29:22 +00002class X { };
3
4X operator+(X, X);
5
6void f(X x) {
7 x = x + x;
8}
9
10struct Y;
11struct Z;
12
13struct Y {
14 Y(const Z&);
15};
16
17struct Z {
18 Z(const Y&);
19};
20
21Y operator+(Y, Y);
22bool operator-(Y, Y); // expected-note{{candidate function}}
23bool operator-(Z, Z); // expected-note{{candidate function}}
24
25void g(Y y, Z z) {
26 y = y + z;
27 bool b = y - z; // expected-error{{use of overloaded operator '-' is ambiguous; candidates are:}}
28}
29
Douglas Gregor96176b32008-11-18 23:14:02 +000030struct A {
Douglas Gregor33074752009-09-30 21:46:01 +000031 bool operator==(Z&); // expected-note 2{{candidate function}}
Douglas Gregor96176b32008-11-18 23:14:02 +000032};
Douglas Gregoreaebc752008-11-06 23:29:22 +000033
Douglas Gregor96176b32008-11-18 23:14:02 +000034A make_A();
35
Douglas Gregor33074752009-09-30 21:46:01 +000036bool operator==(A&, Z&); // expected-note 2{{candidate function}}
Douglas Gregor96176b32008-11-18 23:14:02 +000037
38void h(A a, const A ac, Z z) {
39 make_A() == z;
40 a == z; // expected-error{{use of overloaded operator '==' is ambiguous; candidates are:}}
John McCall7c2342d2010-03-10 11:27:22 +000041 ac == z; // expected-error{{invalid operands to binary expression ('A const' and 'Z')}}
Douglas Gregor96176b32008-11-18 23:14:02 +000042}
43
44struct B {
45 bool operator==(const B&) const;
46
47 void test(Z z) {
48 make_A() == z;
49 }
50};
Douglas Gregor447b69e2008-11-19 03:25:36 +000051
52enum Enum1 { };
53enum Enum2 { };
54
55struct E1 {
56 E1(Enum1) { }
57};
58
59struct E2 {
60 E2(Enum2);
61};
62
63// C++ [over.match.oper]p3 - enum restriction.
64float& operator==(E1, E2);
65
66void enum_test(Enum1 enum1, Enum2 enum2, E1 e1, E2 e2) {
67 float &f1 = (e1 == e2);
68 float &f2 = (enum1 == e2);
69 float &f3 = (e1 == enum2);
Douglas Gregor20093b42009-12-09 23:02:17 +000070 float &f4 = (enum1 == enum2); // expected-error{{non-const lvalue reference to type 'float' cannot bind to a temporary of type 'bool'}}
Douglas Gregor447b69e2008-11-19 03:25:36 +000071}
Douglas Gregor74253732008-11-19 15:42:04 +000072
Sebastian Redl644be852009-10-23 19:23:15 +000073// PR5244 - Argument-dependent lookup would include the two operators below,
74// which would break later assumptions and lead to a crash.
75class pr5244_foo
76{
77 pr5244_foo(int);
78 pr5244_foo(char);
79};
80
81bool operator==(const pr5244_foo& s1, const pr5244_foo& s2);
82bool operator==(char c, const pr5244_foo& s);
83
84enum pr5244_bar
85{
86 pr5244_BAR
87};
88
89class pr5244_baz
90{
John McCall7002f4c2010-04-09 19:03:51 +000091public:
Sebastian Redl644be852009-10-23 19:23:15 +000092 pr5244_bar quux;
93};
94
95void pr5244_barbaz()
96{
97 pr5244_baz quuux;
98 (void)(pr5244_BAR == quuux.quux);
99}
100
101
Douglas Gregor74253732008-11-19 15:42:04 +0000102
103struct PostInc {
104 PostInc operator++(int);
105 PostInc& operator++();
106};
107
108struct PostDec {
109 PostDec operator--(int);
110 PostDec& operator--();
111};
112
113void incdec_test(PostInc pi, PostDec pd) {
114 const PostInc& pi1 = pi++;
115 const PostDec& pd1 = pd--;
116 PostInc &pi2 = ++pi;
117 PostDec &pd2 = --pd;
118}
119
120struct SmartPtr {
121 int& operator*();
Douglas Gregor1ca50c32008-11-21 15:36:28 +0000122 long& operator*() const volatile;
Douglas Gregor74253732008-11-19 15:42:04 +0000123};
124
Douglas Gregor1ca50c32008-11-21 15:36:28 +0000125void test_smartptr(SmartPtr ptr, const SmartPtr cptr,
126 const volatile SmartPtr cvptr) {
Douglas Gregor74253732008-11-19 15:42:04 +0000127 int &ir = *ptr;
Douglas Gregor1ca50c32008-11-21 15:36:28 +0000128 long &lr = *cptr;
129 long &lr2 = *cvptr;
Douglas Gregor74253732008-11-19 15:42:04 +0000130}
Douglas Gregor337c6b92008-11-19 17:17:41 +0000131
132
133struct ArrayLike {
134 int& operator[](int);
135};
136
137void test_arraylike(ArrayLike a) {
138 int& ir = a[17];
139}
140
141struct SmartRef {
142 int* operator&();
143};
144
145void test_smartref(SmartRef r) {
146 int* ip = &r;
147}
148
149bool& operator,(X, Y);
150
151void test_comma(X x, Y y) {
152 bool& b1 = (x, y);
153 X& xr = (x, x);
154}
Douglas Gregorf9eb9052008-11-19 21:05:33 +0000155
Douglas Gregorf9eb9052008-11-19 21:05:33 +0000156struct Callable {
157 int& operator()(int, double = 2.71828); // expected-note{{candidate function}}
158 float& operator()(int, double, long, ...); // expected-note{{candidate function}}
Douglas Gregor518fda12009-01-13 05:10:00 +0000159
160 double& operator()(float); // expected-note{{candidate function}}
Douglas Gregorf9eb9052008-11-19 21:05:33 +0000161};
162
Douglas Gregor518fda12009-01-13 05:10:00 +0000163struct Callable2 {
164 int& operator()(int i = 0);
165 double& operator()(...) const;
166};
167
Douglas Gregor593564b2009-11-15 07:48:03 +0000168struct DerivesCallable : public Callable {
169};
170
171void test_callable(Callable c, Callable2 c2, const Callable2& c2c,
172 DerivesCallable dc) {
Douglas Gregorf9eb9052008-11-19 21:05:33 +0000173 int &ir = c(1);
174 float &fr = c(1, 3.14159, 17, 42);
175
John McCall7c2342d2010-03-10 11:27:22 +0000176 c(); // expected-error{{no matching function for call to object of type 'Callable'}}
Douglas Gregor518fda12009-01-13 05:10:00 +0000177
178 double &dr = c(1.0f);
179
180 int &ir2 = c2();
181 int &ir3 = c2(1);
182 double &fr2 = c2c();
Douglas Gregor593564b2009-11-15 07:48:03 +0000183
184 int &ir4 = dc(17);
185 double &fr3 = dc(3.14159f);
Douglas Gregorf9eb9052008-11-19 21:05:33 +0000186}
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000187
Douglas Gregor621b3932008-11-21 02:54:28 +0000188typedef float FLOAT;
189typedef int& INTREF;
190typedef INTREF Func1(FLOAT, double);
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000191typedef float& Func2(int, double);
192
193struct ConvertToFunc {
Douglas Gregor90073282010-01-11 19:36:35 +0000194 operator Func1*(); // expected-note 2{{conversion candidate of type 'INTREF (*)(FLOAT, double)'}}
195 operator Func2&(); // expected-note 2{{conversion candidate of type 'float &(&)(int, double)'}}
Douglas Gregor9ebae312008-11-19 22:59:19 +0000196 void operator()();
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000197};
198
Douglas Gregor90073282010-01-11 19:36:35 +0000199struct ConvertToFuncDerived : ConvertToFunc { };
200
201void test_funcptr_call(ConvertToFunc ctf, ConvertToFuncDerived ctfd) {
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000202 int &i1 = ctf(1.0f, 2.0);
Douglas Gregor90073282010-01-11 19:36:35 +0000203 float &f1 = ctf((short int)1, 1.0f);
John McCall7c2342d2010-03-10 11:27:22 +0000204 ctf((long int)17, 2.0); // expected-error{{call to object of type 'ConvertToFunc' is ambiguous}}
Douglas Gregor9ebae312008-11-19 22:59:19 +0000205 ctf();
Douglas Gregor90073282010-01-11 19:36:35 +0000206
207 int &i2 = ctfd(1.0f, 2.0);
208 float &f2 = ctfd((short int)1, 1.0f);
John McCall7c2342d2010-03-10 11:27:22 +0000209 ctfd((long int)17, 2.0); // expected-error{{call to object of type 'ConvertToFuncDerived' is ambiguous}}
Douglas Gregor90073282010-01-11 19:36:35 +0000210 ctfd();
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000211}
Douglas Gregor8ba10742008-11-20 16:27:02 +0000212
213struct HasMember {
214 int m;
215};
216
217struct Arrow1 {
218 HasMember* operator->();
219};
220
221struct Arrow2 {
222 Arrow1 operator->(); // expected-note{{candidate function}}
223};
224
225void test_arrow(Arrow1 a1, Arrow2 a2, const Arrow2 a3) {
226 int &i1 = a1->m;
227 int &i2 = a2->m;
Sebastian Redle4c452c2008-11-22 13:44:36 +0000228 a3->m; // expected-error{{no viable overloaded 'operator->'; candidate is}}
Douglas Gregor8ba10742008-11-20 16:27:02 +0000229}
Douglas Gregore63ef482009-01-13 00:11:19 +0000230
231struct CopyConBase {
232};
233
234struct CopyCon : public CopyConBase {
235 CopyCon(const CopyConBase &Base);
236
237 CopyCon(const CopyConBase *Base) {
238 *this = *Base;
239 }
240};
Douglas Gregorfa047642009-02-04 00:32:51 +0000241
242namespace N {
243 struct X { };
244}
245
246namespace M {
247 N::X operator+(N::X, N::X);
248}
249
250namespace M {
251 void test_X(N::X x) {
Douglas Gregorf680a0f2009-02-04 16:44:47 +0000252 (void)(x + x);
Douglas Gregorfa047642009-02-04 00:32:51 +0000253 }
254}
Douglas Gregor8a5ae242009-08-27 23:35:55 +0000255
256struct AA { bool operator!=(AA&); };
257struct BB : AA {};
258bool x(BB y, BB z) { return y != z; }
Fariborz Jahanian4a4e3452009-09-30 00:19:41 +0000259
260
261struct AX {
Fariborz Jahanian7a8233a2009-09-30 17:46:20 +0000262 AX& operator ->(); // expected-note {{declared at}}
Fariborz Jahanian4a4e3452009-09-30 00:19:41 +0000263 int b;
264};
265
266void m() {
267 AX a;
268 a->b = 0; // expected-error {{circular pointer delegation detected}}
269}
John McCallc4e83212009-09-30 01:01:30 +0000270
271struct CircA {
Fariborz Jahanian7a8233a2009-09-30 17:46:20 +0000272 struct CircB& operator->(); // expected-note {{declared at}}
John McCallc4e83212009-09-30 01:01:30 +0000273 int val;
274};
275struct CircB {
Fariborz Jahanian7a8233a2009-09-30 17:46:20 +0000276 struct CircC& operator->(); // expected-note {{declared at}}
John McCallc4e83212009-09-30 01:01:30 +0000277};
278struct CircC {
Fariborz Jahanian7a8233a2009-09-30 17:46:20 +0000279 struct CircA& operator->(); // expected-note {{declared at}}
John McCallc4e83212009-09-30 01:01:30 +0000280};
281
282void circ() {
283 CircA a;
284 a->val = 0; // expected-error {{circular pointer delegation detected}}
285}
Sebastian Redla65b5512009-11-05 16:36:20 +0000286
287// PR5360: Arrays should lead to built-in candidates for subscript.
288typedef enum {
289 LastReg = 23,
290} Register;
291class RegAlloc {
292 int getPriority(Register r) {
293 return usepri[r];
294 }
295 int usepri[LastReg + 1];
296};
Sebastian Redla9efada2009-11-18 20:39:26 +0000297
298// PR5546: Don't generate incorrect and ambiguous overloads for multi-level
299// arrays.
300namespace pr5546
301{
302 enum { X };
303 extern const char *const sMoveCommands[][2][2];
304 const char* a() { return sMoveCommands[X][0][0]; }
305 const char* b() { return (*(sMoveCommands+X))[0][0]; }
306}
Sebastian Redl275c2b42009-11-18 23:10:33 +0000307
308// PR5512 and its discussion
309namespace pr5512 {
310 struct Y {
311 operator short();
312 operator float();
313 };
314 void g_test(Y y) {
315 short s = 0;
316 // DR507, this should be ambiguous, but we special-case assignment
317 s = y;
318 // Note: DR507, this is ambiguous as specified
319 //s += y;
320 }
321
322 struct S {};
323 void operator +=(int&, S);
324 void f(S s) {
325 int i = 0;
326 i += s;
327 }
328
329 struct A {operator int();};
330 int a;
331 void b(A x) {
332 a += x;
333 }
334}
John McCall1eb3e102010-01-07 02:04:15 +0000335
336// PR5900
337namespace pr5900 {
338 struct NotAnArray {};
339 void test0() {
340 NotAnArray x;
341 x[0] = 0; // expected-error {{does not provide a subscript operator}}
342 }
343
344 struct NonConstArray {
345 int operator[](unsigned); // expected-note {{candidate}}
346 };
347 int test1() {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000348 const NonConstArray x = NonConstArray();
John McCall1eb3e102010-01-07 02:04:15 +0000349 return x[0]; // expected-error {{no viable overloaded operator[] for type}}
350 }
351
352 // Not really part of this PR, but implemented at the same time.
353 struct NotAFunction {};
354 void test2() {
355 NotAFunction x;
356 x(); // expected-error {{does not provide a call operator}}
357 }
358}