]> git.proxmox.com Git - rustc.git/blob - src/llvm/tools/clang/test/SemaCXX/overloaded-builtin-operators.cpp
Imported Upstream version 0.6
[rustc.git] / src / llvm / tools / clang / test / SemaCXX / overloaded-builtin-operators.cpp
1 // RUN: %clang_cc1 -fsyntax-only -fshow-overloads=best -verify %s
2 // REQUIRES: LP64
3
4 struct yes;
5 struct no;
6
7 struct Short {
8 operator short();
9 };
10
11 struct Long {
12 operator long();
13 };
14
15 enum E1 { };
16 struct Enum1 {
17 operator E1();
18 };
19
20 enum E2 { };
21 struct Enum2 {
22 operator E2();
23 };
24
25
26 struct X {
27 void f();
28 };
29
30 typedef void (X::*pmf)();
31 struct Xpmf {
32 operator pmf();
33 };
34
35 yes& islong(long);
36 yes& islong(unsigned long); // FIXME: shouldn't be needed
37 no& islong(int);
38
39 void f(Short s, Long l, Enum1 e1, Enum2 e2, Xpmf pmf) {
40 // C++ [over.built]p8
41 int i1 = +e1;
42 int i2 = -e2;
43
44 // C++ [over.built]p10:
45 int i3 = ~s;
46 bool b1 = !s;
47
48 // C++ [over.built]p12
49 (void)static_cast<yes&>(islong(s + l));
50 (void)static_cast<no&>(islong(s + s));
51
52 // C++ [over.built]p16
53 (void)(pmf == &X::f);
54 (void)(pmf == 0);
55
56 // C++ [over.built]p17
57 (void)static_cast<yes&>(islong(s % l));
58 (void)static_cast<yes&>(islong(l << s));
59 (void)static_cast<no&>(islong(s << l));
60 (void)static_cast<yes&>(islong(e1 % l));
61 // FIXME: should pass (void)static_cast<no&>(islong(e1 % e2));
62 }
63
64 struct ShortRef { // expected-note{{candidate function (the implicit copy assignment operator)}}
65 operator short&();
66 };
67
68 struct LongRef {
69 operator volatile long&();
70 };
71
72 struct XpmfRef { // expected-note{{candidate function (the implicit copy assignment operator)}}
73 operator pmf&();
74 };
75
76 struct E2Ref {
77 operator E2&();
78 };
79
80 void g(ShortRef sr, LongRef lr, E2Ref e2_ref, XpmfRef pmf_ref) {
81 // C++ [over.built]p3
82 short s1 = sr++;
83
84 // C++ [over.built]p3
85 long l1 = lr--;
86
87 // C++ [over.built]p18
88 short& sr1 = (sr *= lr);
89 volatile long& lr1 = (lr *= sr);
90
91 // C++ [over.built]p20:
92 E2 e2r2;
93 e2r2 = e2_ref;
94
95 pmf &pmr = (pmf_ref = &X::f); // expected-error{{no viable overloaded '='}}
96 pmf pmr2;
97 pmr2 = pmf_ref;
98
99 // C++ [over.built]p22
100 short& sr2 = (sr %= lr);
101 volatile long& lr2 = (lr <<= sr);
102
103 bool b1 = (sr && lr) || (sr || lr);
104 }
105
106 struct VolatileIntPtr {
107 operator int volatile *();
108 };
109
110 struct ConstIntPtr {
111 operator int const *();
112 };
113
114 struct VolatileIntPtrRef {
115 operator int volatile *&();
116 };
117
118 struct ConstIntPtrRef {
119 operator int const *&();
120 };
121
122 void test_with_ptrs(VolatileIntPtr vip, ConstIntPtr cip, ShortRef sr,
123 VolatileIntPtrRef vipr, ConstIntPtrRef cipr) {
124 const int& cir1 = cip[sr];
125 const int& cir2 = sr[cip];
126 volatile int& vir1 = vip[sr];
127 volatile int& vir2 = sr[vip];
128 bool b1 = (vip == cip);
129 long p1 = vip - cip;
130
131 // C++ [over.built]p5:
132 int volatile *vip1 = vipr++;
133 int const *cip1 = cipr++;
134 int volatile *&vipr1 = ++vipr;
135 int const *&cipr1 = --cipr;
136
137 // C++ [over.built]p6:
138 int volatile &ivr = *vip;
139
140 // C++ [over.built]p8:
141 int volatile *vip2 = +vip;
142 int i1 = +sr;
143 int i2 = -sr;
144
145 // C++ [over.built]p13:
146 int volatile &ivr2 = vip[17];
147 int const &icr2 = 17[cip];
148 }
149
150 // C++ [over.match.open]p4
151
152 void test_assign_restrictions(ShortRef& sr) {
153 sr = (short)0; // expected-error{{no viable overloaded '='}}
154 }
155
156 struct Base { };
157 struct Derived1 : Base { };
158 struct Derived2 : Base { };
159
160 template<typename T>
161 struct ConvertibleToPtrOf {
162 operator T*();
163 };
164
165 bool test_with_base_ptrs(ConvertibleToPtrOf<Derived1> d1,
166 ConvertibleToPtrOf<Derived2> d2) {
167 return d1 == d2; // expected-error{{invalid operands}}
168 }
169
170 // DR425
171 struct A {
172 template< typename T > operator T() const;
173 };
174
175 void test_dr425(A a) {
176 // FIXME: lots of candidates here!
177 (void)(1.0f * a); // expected-error{{ambiguous}} \
178 // expected-note 4{{candidate}} \
179 // expected-note {{remaining 117 candidates omitted; pass -fshow-overloads=all to show them}}
180 }
181
182 // pr5432
183 enum e {X};
184
185 const int a[][2] = {{1}};
186
187 int test_pr5432() {
188 return a[X][X];
189 }
190
191 void f() {
192 (void)__extension__(A());
193 }
194
195 namespace PR7319 {
196 typedef enum { Enum1, Enum2, Enum3 } MyEnum;
197
198 template<typename X> bool operator>(const X &inX1, const X &inX2);
199
200 void f() {
201 MyEnum e1, e2;
202 if (e1 > e2) {}
203 }
204 }
205
206 namespace PR8477 {
207 struct Foo {
208 operator bool();
209 operator const char *();
210 };
211
212 bool doit() {
213 Foo foo;
214 long long zero = 0;
215 (void)(foo + zero);
216 (void)(foo - zero);
217 (void)(zero + foo);
218 (void)(zero[foo]);
219 (void)(foo - foo); // expected-error{{use of overloaded operator '-' is ambiguous}} \
220 // expected-note 4{{built-in candidate operator-}} \
221 // expected-note{{candidates omitted}}
222 return foo[zero] == zero;
223 }
224 }
225
226 namespace PR7851 {
227 struct X {
228 operator const void *() const;
229 operator void *();
230
231 operator const unsigned *() const;
232 operator unsigned *();
233 };
234
235 void f() {
236 X x;
237 x[0] = 1;
238 *x = 0;
239 (void)(x - x);
240 }
241 }
242
243 namespace PR12854 {
244 enum { size = 1 };
245 void plus_equals() {
246 int* __restrict py;
247 py += size;
248 }
249
250 struct RestrictInt {
251 operator int* __restrict &();
252 };
253
254 void user_conversions(RestrictInt ri) {
255 ++ri;
256 --ri;
257 ri++;
258 ri--;
259 }
260 }
261
262 namespace PR12964 {
263 struct X { operator __int128() const; } x;
264 bool a = x == __int128(0);
265 bool b = x == 0;
266
267 struct Y { operator unsigned __int128() const; } y;
268 bool c = y == __int128(0);
269 bool d = y == 0;
270
271 bool e = x == y;
272 }