blob: 7762667d1aba3485d44ad4cec25d6d6ca7454a17 [file] [log] [blame]
Daniel Dunbard7d5f022009-03-24 02:24:46 +00001// RUN: clang-cc -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 Gregord249e1d1f2009-05-29 20:38:28 +000070 float &f4 = (enum1 == enum2); // expected-error{{non-const lvalue reference to type 'float' cannot be initialized with 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 {
John McCall54e14c42009-10-22 22:37:11 +0000193 operator Func1*(); // expected-note{{conversion candidate of type 'INTREF (*)(FLOAT, double)'}}
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000194 operator Func2&(); // expected-note{{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
198void test_funcptr_call(ConvertToFunc ctf) {
199 int &i1 = ctf(1.0f, 2.0);
200 float &f2 = ctf((short int)1, 1.0f);
201 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 +0000202 ctf();
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000203}
Douglas Gregor8ba10742008-11-20 16:27:02 +0000204
205struct HasMember {
206 int m;
207};
208
209struct Arrow1 {
210 HasMember* operator->();
211};
212
213struct Arrow2 {
214 Arrow1 operator->(); // expected-note{{candidate function}}
215};
216
217void test_arrow(Arrow1 a1, Arrow2 a2, const Arrow2 a3) {
218 int &i1 = a1->m;
219 int &i2 = a2->m;
Sebastian Redle4c452c2008-11-22 13:44:36 +0000220 a3->m; // expected-error{{no viable overloaded 'operator->'; candidate is}}
Douglas Gregor8ba10742008-11-20 16:27:02 +0000221}
Douglas Gregore63ef482009-01-13 00:11:19 +0000222
223struct CopyConBase {
224};
225
226struct CopyCon : public CopyConBase {
227 CopyCon(const CopyConBase &Base);
228
229 CopyCon(const CopyConBase *Base) {
230 *this = *Base;
231 }
232};
Douglas Gregorfa047642009-02-04 00:32:51 +0000233
234namespace N {
235 struct X { };
236}
237
238namespace M {
239 N::X operator+(N::X, N::X);
240}
241
242namespace M {
243 void test_X(N::X x) {
Douglas Gregorf680a0f2009-02-04 16:44:47 +0000244 (void)(x + x);
Douglas Gregorfa047642009-02-04 00:32:51 +0000245 }
246}
Douglas Gregor8a5ae242009-08-27 23:35:55 +0000247
248struct AA { bool operator!=(AA&); };
249struct BB : AA {};
250bool x(BB y, BB z) { return y != z; }
Fariborz Jahanian4a4e3452009-09-30 00:19:41 +0000251
252
253struct AX {
Fariborz Jahanian7a8233a2009-09-30 17:46:20 +0000254 AX& operator ->(); // expected-note {{declared at}}
Fariborz Jahanian4a4e3452009-09-30 00:19:41 +0000255 int b;
256};
257
258void m() {
259 AX a;
260 a->b = 0; // expected-error {{circular pointer delegation detected}}
261}
John McCallc4e83212009-09-30 01:01:30 +0000262
263struct CircA {
Fariborz Jahanian7a8233a2009-09-30 17:46:20 +0000264 struct CircB& operator->(); // expected-note {{declared at}}
John McCallc4e83212009-09-30 01:01:30 +0000265 int val;
266};
267struct CircB {
Fariborz Jahanian7a8233a2009-09-30 17:46:20 +0000268 struct CircC& operator->(); // expected-note {{declared at}}
John McCallc4e83212009-09-30 01:01:30 +0000269};
270struct CircC {
Fariborz Jahanian7a8233a2009-09-30 17:46:20 +0000271 struct CircA& operator->(); // expected-note {{declared at}}
John McCallc4e83212009-09-30 01:01:30 +0000272};
273
274void circ() {
275 CircA a;
276 a->val = 0; // expected-error {{circular pointer delegation detected}}
277}
Sebastian Redla65b5512009-11-05 16:36:20 +0000278
279// PR5360: Arrays should lead to built-in candidates for subscript.
280typedef enum {
281 LastReg = 23,
282} Register;
283class RegAlloc {
284 int getPriority(Register r) {
285 return usepri[r];
286 }
287 int usepri[LastReg + 1];
288};