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