blob: b3c08085a695fbb9833fe85df01a84af72b4b229 [file] [log] [blame]
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +00001// RUN: %clang_cc1 -fsyntax-only -fshow-overloads=best -verify %s
Douglas Gregoreb8f3062008-11-12 17:17:38 +00002struct yes;
3struct no;
4
5struct Short {
6 operator short();
7};
8
9struct Long {
10 operator long();
11};
12
Douglas Gregor447b69e2008-11-19 03:25:36 +000013enum E1 { };
14struct Enum1 {
15 operator E1();
16};
17
18enum E2 { };
19struct Enum2 {
20 operator E2();
21};
22
Douglas Gregor20b3e992009-08-24 17:42:35 +000023
24struct X {
25 void f();
26};
27
28typedef void (X::*pmf)();
29struct Xpmf {
30 operator pmf();
31};
32
Douglas Gregoreb8f3062008-11-12 17:17:38 +000033yes& islong(long);
Douglas Gregor447b69e2008-11-19 03:25:36 +000034yes& islong(unsigned long); // FIXME: shouldn't be needed
Douglas Gregoreb8f3062008-11-12 17:17:38 +000035no& islong(int);
36
Douglas Gregor20b3e992009-08-24 17:42:35 +000037void f(Short s, Long l, Enum1 e1, Enum2 e2, Xpmf pmf) {
Douglas Gregor74253732008-11-19 15:42:04 +000038 // C++ [over.built]p8
39 int i1 = +e1;
40 int i2 = -e2;
41
42 // C++ [over.built]p10:
43 int i3 = ~s;
44 bool b1 = !s;
45
Douglas Gregoreb8f3062008-11-12 17:17:38 +000046 // C++ [over.built]p12
47 (void)static_cast<yes&>(islong(s + l));
48 (void)static_cast<no&>(islong(s + s));
49
Douglas Gregor20b3e992009-08-24 17:42:35 +000050 // C++ [over.built]p16
51 (void)(pmf == &X::f);
52 (void)(pmf == 0);
53
Douglas Gregoreb8f3062008-11-12 17:17:38 +000054 // C++ [over.built]p17
55 (void)static_cast<yes&>(islong(s % l));
56 (void)static_cast<yes&>(islong(l << s));
57 (void)static_cast<no&>(islong(s << l));
Douglas Gregor447b69e2008-11-19 03:25:36 +000058 (void)static_cast<yes&>(islong(e1 % l));
59 // FIXME: should pass (void)static_cast<no&>(islong(e1 % e2));
Douglas Gregoreb8f3062008-11-12 17:17:38 +000060}
61
John McCall220ccbf2010-01-13 00:25:19 +000062struct ShortRef { // expected-note{{candidate function (the implicit copy assignment operator)}}
Douglas Gregoreb8f3062008-11-12 17:17:38 +000063 operator short&();
64};
65
66struct LongRef {
67 operator volatile long&();
68};
69
John McCall220ccbf2010-01-13 00:25:19 +000070struct XpmfRef { // expected-note{{candidate function (the implicit copy assignment operator)}}
Douglas Gregor20b3e992009-08-24 17:42:35 +000071 operator pmf&();
72};
73
74struct E2Ref {
75 operator E2&();
76};
77
78void g(ShortRef sr, LongRef lr, E2Ref e2_ref, XpmfRef pmf_ref) {
Douglas Gregor74253732008-11-19 15:42:04 +000079 // C++ [over.built]p3
80 short s1 = sr++;
81
82 // C++ [over.built]p3
83 long l1 = lr--;
84
Douglas Gregoreb8f3062008-11-12 17:17:38 +000085 // C++ [over.built]p18
86 short& sr1 = (sr *= lr);
87 volatile long& lr1 = (lr *= sr);
88
Douglas Gregor20b3e992009-08-24 17:42:35 +000089 // C++ [over.built]p20:
90 E2 e2r2;
91 e2r2 = e2_ref;
92
93 pmf &pmr = (pmf_ref = &X::f); // expected-error{{no viable overloaded '='}}
94 pmf pmr2;
95 pmr2 = pmf_ref;
96
Douglas Gregoreb8f3062008-11-12 17:17:38 +000097 // C++ [over.built]p22
98 short& sr2 = (sr %= lr);
99 volatile long& lr2 = (lr <<= sr);
100
101 bool b1 = (sr && lr) || (sr || lr);
102}
103
104struct VolatileIntPtr {
105 operator int volatile *();
106};
107
108struct ConstIntPtr {
109 operator int const *();
110};
111
Douglas Gregor74253732008-11-19 15:42:04 +0000112struct VolatileIntPtrRef {
113 operator int volatile *&();
114};
115
116struct ConstIntPtrRef {
117 operator int const *&();
118};
119
120void test_with_ptrs(VolatileIntPtr vip, ConstIntPtr cip, ShortRef sr,
121 VolatileIntPtrRef vipr, ConstIntPtrRef cipr) {
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000122 const int& cir1 = cip[sr];
123 const int& cir2 = sr[cip];
124 volatile int& vir1 = vip[sr];
125 volatile int& vir2 = sr[vip];
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000126 bool b1 = (vip == cip);
127 long p1 = vip - cip;
Douglas Gregor74253732008-11-19 15:42:04 +0000128
129 // C++ [over.built]p5:
130 int volatile *vip1 = vipr++;
131 int const *cip1 = cipr++;
132 int volatile *&vipr1 = ++vipr;
133 int const *&cipr1 = --cipr;
134
135 // C++ [over.built]p6:
136 int volatile &ivr = *vip;
137
138 // C++ [over.built]p8:
139 int volatile *vip2 = +vip;
140 int i1 = +sr;
141 int i2 = -sr;
Douglas Gregor337c6b92008-11-19 17:17:41 +0000142
143 // C++ [over.built]p13:
144 int volatile &ivr2 = vip[17];
145 int const &icr2 = 17[cip];
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000146}
Douglas Gregor74253732008-11-19 15:42:04 +0000147
Douglas Gregor88b4bf22009-01-13 00:52:54 +0000148// C++ [over.match.open]p4
149
150void test_assign_restrictions(ShortRef& sr) {
Sebastian Redl8593c782009-05-21 11:50:50 +0000151 sr = (short)0; // expected-error{{no viable overloaded '='}}
Douglas Gregor88b4bf22009-01-13 00:52:54 +0000152}
Douglas Gregor652371a2009-10-21 22:01:30 +0000153
154struct Base { };
155struct Derived1 : Base { };
156struct Derived2 : Base { };
157
158template<typename T>
159struct ConvertibleToPtrOf {
160 operator T*();
161};
162
163bool test_with_base_ptrs(ConvertibleToPtrOf<Derived1> d1,
164 ConvertibleToPtrOf<Derived2> d2) {
165 return d1 == d2; // expected-error{{invalid operands}}
166}
167
168// DR425
169struct A {
170 template< typename T > operator T() const;
171};
172
173void test_dr425(A a) {
174 // FIXME: lots of candidates here!
175 (void)(1.0f * a); // expected-error{{ambiguous}} \
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +0000176 // expected-note 4{{candidate}} \
177 // expected-note {{remaining 77 candidates omitted; pass -fshow-overloads=all to show them}}
Douglas Gregor652371a2009-10-21 22:01:30 +0000178}
Fariborz Jahaniand411b3f2009-11-09 21:02:05 +0000179
180// pr5432
181enum e {X};
182
183const int a[][2] = {{1}};
184
185int test_pr5432() {
186 return a[X][X];
187}
188
Anders Carlssona8a1e3d2009-11-14 21:26:41 +0000189void f() {
190 (void)__extension__(A());
191}
Douglas Gregorccd47132010-06-08 21:03:17 +0000192
193namespace PR7319 {
194 typedef enum { Enum1, Enum2, Enum3 } MyEnum;
195
196 template<typename X> bool operator>(const X &inX1, const X &inX2);
197
198 void f() {
199 MyEnum e1, e2;
200 if (e1 > e2) {}
201 }
202}
Douglas Gregorfec56e72010-11-03 17:00:07 +0000203
204namespace PR8477 {
205 struct Foo {
206 operator bool();
207 operator const char *();
208 };
209
210 bool doit() {
211 Foo foo;
212 long long zero = 0;
213 (void)(foo + zero);
214 (void)(foo - zero);
215 (void)(zero + foo);
216 (void)(zero[foo]);
217 (void)(foo - foo); // expected-error{{use of overloaded operator '-' is ambiguous}} \
218 // expected-note 4{{built-in candidate operator-}} \
219 // expected-note{{candidates omitted}}
220 return foo[zero] == zero;
221 }
222}
Douglas Gregor2fdc5e82011-01-05 00:13:17 +0000223
224namespace PR7851 {
225 struct X {
226 operator const void *() const;
227 operator void *();
228
229 operator const unsigned *() const;
230 operator unsigned *();
231 };
232
233 void f() {
234 X x;
235 x[0] = 1;
236 *x = 0;
237 (void)(x - x);
238 }
239}