blob: e07afe20203140791054735ecb09f1ce6c7e48f5 [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:}}
41 ac == z; // expected-error{{invalid operands to binary expression ('struct A const' and 'struct Z')}}
42}
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{
91 pr5244_bar quux;
92};
93
94void pr5244_barbaz()
95{
96 pr5244_baz quuux;
97 (void)(pr5244_BAR == quuux.quux);
98}
99
100
Douglas Gregor74253732008-11-19 15:42:04 +0000101
102struct PostInc {
103 PostInc operator++(int);
104 PostInc& operator++();
105};
106
107struct PostDec {
108 PostDec operator--(int);
109 PostDec& operator--();
110};
111
112void incdec_test(PostInc pi, PostDec pd) {
113 const PostInc& pi1 = pi++;
114 const PostDec& pd1 = pd--;
115 PostInc &pi2 = ++pi;
116 PostDec &pd2 = --pd;
117}
118
119struct SmartPtr {
120 int& operator*();
Douglas Gregor1ca50c32008-11-21 15:36:28 +0000121 long& operator*() const volatile;
Douglas Gregor74253732008-11-19 15:42:04 +0000122};
123
Douglas Gregor1ca50c32008-11-21 15:36:28 +0000124void test_smartptr(SmartPtr ptr, const SmartPtr cptr,
125 const volatile SmartPtr cvptr) {
Douglas Gregor74253732008-11-19 15:42:04 +0000126 int &ir = *ptr;
Douglas Gregor1ca50c32008-11-21 15:36:28 +0000127 long &lr = *cptr;
128 long &lr2 = *cvptr;
Douglas Gregor74253732008-11-19 15:42:04 +0000129}
Douglas Gregor337c6b92008-11-19 17:17:41 +0000130
131
132struct ArrayLike {
133 int& operator[](int);
134};
135
136void test_arraylike(ArrayLike a) {
137 int& ir = a[17];
138}
139
140struct SmartRef {
141 int* operator&();
142};
143
144void test_smartref(SmartRef r) {
145 int* ip = &r;
146}
147
148bool& operator,(X, Y);
149
150void test_comma(X x, Y y) {
151 bool& b1 = (x, y);
152 X& xr = (x, x);
153}
Douglas Gregorf9eb9052008-11-19 21:05:33 +0000154
Douglas Gregorf9eb9052008-11-19 21:05:33 +0000155struct Callable {
156 int& operator()(int, double = 2.71828); // expected-note{{candidate function}}
157 float& operator()(int, double, long, ...); // expected-note{{candidate function}}
Douglas Gregor518fda12009-01-13 05:10:00 +0000158
159 double& operator()(float); // expected-note{{candidate function}}
Douglas Gregorf9eb9052008-11-19 21:05:33 +0000160};
161
Douglas Gregor518fda12009-01-13 05:10:00 +0000162struct Callable2 {
163 int& operator()(int i = 0);
164 double& operator()(...) const;
165};
166
Douglas Gregor593564b2009-11-15 07:48:03 +0000167struct DerivesCallable : public Callable {
168};
169
170void test_callable(Callable c, Callable2 c2, const Callable2& c2c,
171 DerivesCallable dc) {
Douglas Gregorf9eb9052008-11-19 21:05:33 +0000172 int &ir = c(1);
173 float &fr = c(1, 3.14159, 17, 42);
174
175 c(); // expected-error{{no matching function for call to object of type 'struct Callable'; candidates are:}}
Douglas Gregor518fda12009-01-13 05:10:00 +0000176
177 double &dr = c(1.0f);
178
179 int &ir2 = c2();
180 int &ir3 = c2(1);
181 double &fr2 = c2c();
Douglas Gregor593564b2009-11-15 07:48:03 +0000182
183 int &ir4 = dc(17);
184 double &fr3 = dc(3.14159f);
Douglas Gregorf9eb9052008-11-19 21:05:33 +0000185}
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000186
Douglas Gregor621b3932008-11-21 02:54:28 +0000187typedef float FLOAT;
188typedef int& INTREF;
189typedef INTREF Func1(FLOAT, double);
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000190typedef float& Func2(int, double);
191
192struct ConvertToFunc {
Douglas Gregor90073282010-01-11 19:36:35 +0000193 operator Func1*(); // expected-note 2{{conversion candidate of type 'INTREF (*)(FLOAT, double)'}}
194 operator Func2&(); // expected-note 2{{conversion candidate of type 'float &(&)(int, double)'}}
Douglas Gregor9ebae312008-11-19 22:59:19 +0000195 void operator()();
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000196};
197
Douglas Gregor90073282010-01-11 19:36:35 +0000198struct ConvertToFuncDerived : ConvertToFunc { };
199
200void test_funcptr_call(ConvertToFunc ctf, ConvertToFuncDerived ctfd) {
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000201 int &i1 = ctf(1.0f, 2.0);
Douglas Gregor90073282010-01-11 19:36:35 +0000202 float &f1 = ctf((short int)1, 1.0f);
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000203 ctf((long int)17, 2.0); // expected-error{{error: call to object of type 'struct ConvertToFunc' is ambiguous; candidates are:}}
Douglas Gregor9ebae312008-11-19 22:59:19 +0000204 ctf();
Douglas Gregor90073282010-01-11 19:36:35 +0000205
206 int &i2 = ctfd(1.0f, 2.0);
207 float &f2 = ctfd((short int)1, 1.0f);
208 ctfd((long int)17, 2.0); // expected-error{{error: call to object of type 'struct ConvertToFuncDerived' is ambiguous; candidates are:}}
209 ctfd();
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000210}
Douglas Gregor8ba10742008-11-20 16:27:02 +0000211
212struct HasMember {
213 int m;
214};
215
216struct Arrow1 {
217 HasMember* operator->();
218};
219
220struct Arrow2 {
221 Arrow1 operator->(); // expected-note{{candidate function}}
222};
223
224void test_arrow(Arrow1 a1, Arrow2 a2, const Arrow2 a3) {
225 int &i1 = a1->m;
226 int &i2 = a2->m;
Sebastian Redle4c452c2008-11-22 13:44:36 +0000227 a3->m; // expected-error{{no viable overloaded 'operator->'; candidate is}}
Douglas Gregor8ba10742008-11-20 16:27:02 +0000228}
Douglas Gregore63ef482009-01-13 00:11:19 +0000229
230struct CopyConBase {
231};
232
233struct CopyCon : public CopyConBase {
234 CopyCon(const CopyConBase &Base);
235
236 CopyCon(const CopyConBase *Base) {
237 *this = *Base;
238 }
239};
Douglas Gregorfa047642009-02-04 00:32:51 +0000240
241namespace N {
242 struct X { };
243}
244
245namespace M {
246 N::X operator+(N::X, N::X);
247}
248
249namespace M {
250 void test_X(N::X x) {
Douglas Gregorf680a0f2009-02-04 16:44:47 +0000251 (void)(x + x);
Douglas Gregorfa047642009-02-04 00:32:51 +0000252 }
253}
Douglas Gregor8a5ae242009-08-27 23:35:55 +0000254
255struct AA { bool operator!=(AA&); };
256struct BB : AA {};
257bool x(BB y, BB z) { return y != z; }
Fariborz Jahanian4a4e3452009-09-30 00:19:41 +0000258
259
260struct AX {
Fariborz Jahanian7a8233a2009-09-30 17:46:20 +0000261 AX& operator ->(); // expected-note {{declared at}}
Fariborz Jahanian4a4e3452009-09-30 00:19:41 +0000262 int b;
263};
264
265void m() {
266 AX a;
267 a->b = 0; // expected-error {{circular pointer delegation detected}}
268}
John McCallc4e83212009-09-30 01:01:30 +0000269
270struct CircA {
Fariborz Jahanian7a8233a2009-09-30 17:46:20 +0000271 struct CircB& operator->(); // expected-note {{declared at}}
John McCallc4e83212009-09-30 01:01:30 +0000272 int val;
273};
274struct CircB {
Fariborz Jahanian7a8233a2009-09-30 17:46:20 +0000275 struct CircC& operator->(); // expected-note {{declared at}}
John McCallc4e83212009-09-30 01:01:30 +0000276};
277struct CircC {
Fariborz Jahanian7a8233a2009-09-30 17:46:20 +0000278 struct CircA& operator->(); // expected-note {{declared at}}
John McCallc4e83212009-09-30 01:01:30 +0000279};
280
281void circ() {
282 CircA a;
283 a->val = 0; // expected-error {{circular pointer delegation detected}}
284}
Sebastian Redla65b5512009-11-05 16:36:20 +0000285
286// PR5360: Arrays should lead to built-in candidates for subscript.
287typedef enum {
288 LastReg = 23,
289} Register;
290class RegAlloc {
291 int getPriority(Register r) {
292 return usepri[r];
293 }
294 int usepri[LastReg + 1];
295};
Sebastian Redla9efada2009-11-18 20:39:26 +0000296
297// PR5546: Don't generate incorrect and ambiguous overloads for multi-level
298// arrays.
299namespace pr5546
300{
301 enum { X };
302 extern const char *const sMoveCommands[][2][2];
303 const char* a() { return sMoveCommands[X][0][0]; }
304 const char* b() { return (*(sMoveCommands+X))[0][0]; }
305}
Sebastian Redl275c2b42009-11-18 23:10:33 +0000306
307// PR5512 and its discussion
308namespace pr5512 {
309 struct Y {
310 operator short();
311 operator float();
312 };
313 void g_test(Y y) {
314 short s = 0;
315 // DR507, this should be ambiguous, but we special-case assignment
316 s = y;
317 // Note: DR507, this is ambiguous as specified
318 //s += y;
319 }
320
321 struct S {};
322 void operator +=(int&, S);
323 void f(S s) {
324 int i = 0;
325 i += s;
326 }
327
328 struct A {operator int();};
329 int a;
330 void b(A x) {
331 a += x;
332 }
333}
John McCall1eb3e102010-01-07 02:04:15 +0000334
335// PR5900
336namespace pr5900 {
337 struct NotAnArray {};
338 void test0() {
339 NotAnArray x;
340 x[0] = 0; // expected-error {{does not provide a subscript operator}}
341 }
342
343 struct NonConstArray {
344 int operator[](unsigned); // expected-note {{candidate}}
345 };
346 int test1() {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000347 const NonConstArray x = NonConstArray();
John McCall1eb3e102010-01-07 02:04:15 +0000348 return x[0]; // expected-error {{no viable overloaded operator[] for type}}
349 }
350
351 // Not really part of this PR, but implemented at the same time.
352 struct NotAFunction {};
353 void test2() {
354 NotAFunction x;
355 x(); // expected-error {{does not provide a call operator}}
356 }
357}