xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/member-pointer.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc struct A {};
4*f4a2713aSLionel Sambuc enum B { Dummy };
5*f4a2713aSLionel Sambuc namespace C {}
6*f4a2713aSLionel Sambuc struct D : A {};
7*f4a2713aSLionel Sambuc struct E : A {};
8*f4a2713aSLionel Sambuc struct F : D, E {};
9*f4a2713aSLionel Sambuc struct G : virtual D {};
10*f4a2713aSLionel Sambuc 
11*f4a2713aSLionel Sambuc int A::*pdi1;
12*f4a2713aSLionel Sambuc int (::A::*pdi2);
13*f4a2713aSLionel Sambuc int (A::*pfi)(int);
14*f4a2713aSLionel Sambuc 
15*f4a2713aSLionel Sambuc int B::*pbi; // expected-error {{expected a class or namespace}}
16*f4a2713aSLionel Sambuc int C::*pci; // expected-error {{'pci' does not point into a class}}
17*f4a2713aSLionel Sambuc void A::*pdv; // expected-error {{'pdv' declared as a member pointer to void}}
18*f4a2713aSLionel Sambuc int& A::*pdr; // expected-error {{'pdr' declared as a member pointer to a reference}}
19*f4a2713aSLionel Sambuc 
20*f4a2713aSLionel Sambuc void f() {
21*f4a2713aSLionel Sambuc   // This requires tentative parsing.
22*f4a2713aSLionel Sambuc   int (A::*pf)(int, int);
23*f4a2713aSLionel Sambuc 
24*f4a2713aSLionel Sambuc   // Implicit conversion to bool.
25*f4a2713aSLionel Sambuc   bool b = pdi1;
26*f4a2713aSLionel Sambuc   b = pfi;
27*f4a2713aSLionel Sambuc 
28*f4a2713aSLionel Sambuc   // Conversion from null pointer constant.
29*f4a2713aSLionel Sambuc   pf = 0;
30*f4a2713aSLionel Sambuc   pf = __null;
31*f4a2713aSLionel Sambuc 
32*f4a2713aSLionel Sambuc   // Conversion to member of derived.
33*f4a2713aSLionel Sambuc   int D::*pdid = pdi1;
34*f4a2713aSLionel Sambuc   pdid = pdi2;
35*f4a2713aSLionel Sambuc 
36*f4a2713aSLionel Sambuc   // Fail conversion due to ambiguity and virtuality.
37*f4a2713aSLionel Sambuc   int F::*pdif = pdi1; // expected-error {{ambiguous conversion from pointer to member of base class 'A' to pointer to member of derived class 'F':}}
38*f4a2713aSLionel Sambuc   int G::*pdig = pdi1; // expected-error {{conversion from pointer to member of class 'A' to pointer to member of class 'G' via virtual base 'D' is not allowed}}
39*f4a2713aSLionel Sambuc 
40*f4a2713aSLionel Sambuc   // Conversion to member of base.
41*f4a2713aSLionel Sambuc   pdi1 = pdid; // expected-error {{assigning to 'int A::*' from incompatible type 'int D::*'}}
42*f4a2713aSLionel Sambuc 
43*f4a2713aSLionel Sambuc   // Comparisons
44*f4a2713aSLionel Sambuc   int (A::*pf2)(int, int);
45*f4a2713aSLionel Sambuc   int (D::*pf3)(int, int) = 0;
46*f4a2713aSLionel Sambuc   bool b1 = (pf == pf2); (void)b1;
47*f4a2713aSLionel Sambuc   bool b2 = (pf != pf2); (void)b2;
48*f4a2713aSLionel Sambuc   bool b3 = (pf == pf3); (void)b3;
49*f4a2713aSLionel Sambuc   bool b4 = (pf != 0); (void)b4;
50*f4a2713aSLionel Sambuc }
51*f4a2713aSLionel Sambuc 
52*f4a2713aSLionel Sambuc struct TheBase
53*f4a2713aSLionel Sambuc {
54*f4a2713aSLionel Sambuc   void d();
55*f4a2713aSLionel Sambuc };
56*f4a2713aSLionel Sambuc 
57*f4a2713aSLionel Sambuc struct HasMembers : TheBase
58*f4a2713aSLionel Sambuc {
59*f4a2713aSLionel Sambuc   int i;
60*f4a2713aSLionel Sambuc   void f();
61*f4a2713aSLionel Sambuc 
62*f4a2713aSLionel Sambuc   void g();
63*f4a2713aSLionel Sambuc   void g(int);
64*f4a2713aSLionel Sambuc   static void g(double);
65*f4a2713aSLionel Sambuc };
66*f4a2713aSLionel Sambuc 
67*f4a2713aSLionel Sambuc namespace Fake
68*f4a2713aSLionel Sambuc {
69*f4a2713aSLionel Sambuc   int i;
70*f4a2713aSLionel Sambuc   void f();
71*f4a2713aSLionel Sambuc }
72*f4a2713aSLionel Sambuc 
73*f4a2713aSLionel Sambuc void g() {
74*f4a2713aSLionel Sambuc   HasMembers hm;
75*f4a2713aSLionel Sambuc 
76*f4a2713aSLionel Sambuc   int HasMembers::*pmi = &HasMembers::i;
77*f4a2713aSLionel Sambuc   int *pni = &Fake::i;
78*f4a2713aSLionel Sambuc   int *pmii = &hm.i;
79*f4a2713aSLionel Sambuc 
80*f4a2713aSLionel Sambuc   void (HasMembers::*pmf)() = &HasMembers::f;
81*f4a2713aSLionel Sambuc   void (*pnf)() = &Fake::f;
82*f4a2713aSLionel Sambuc   &hm.f; // expected-error {{cannot create a non-constant pointer to member function}}
83*f4a2713aSLionel Sambuc 
84*f4a2713aSLionel Sambuc   void (HasMembers::*pmgv)() = &HasMembers::g;
85*f4a2713aSLionel Sambuc   void (HasMembers::*pmgi)(int) = &HasMembers::g;
86*f4a2713aSLionel Sambuc   void (*pmgd)(double) = &HasMembers::g;
87*f4a2713aSLionel Sambuc 
88*f4a2713aSLionel Sambuc   void (HasMembers::*pmd)() = &HasMembers::d;
89*f4a2713aSLionel Sambuc }
90*f4a2713aSLionel Sambuc 
91*f4a2713aSLionel Sambuc struct Incomplete;
92*f4a2713aSLionel Sambuc 
93*f4a2713aSLionel Sambuc void h() {
94*f4a2713aSLionel Sambuc   HasMembers hm, *phm = &hm;
95*f4a2713aSLionel Sambuc 
96*f4a2713aSLionel Sambuc   int HasMembers::*pi = &HasMembers::i;
97*f4a2713aSLionel Sambuc   hm.*pi = 0;
98*f4a2713aSLionel Sambuc   int i = phm->*pi;
99*f4a2713aSLionel Sambuc   (void)&(hm.*pi);
100*f4a2713aSLionel Sambuc   (void)&(phm->*pi);
101*f4a2713aSLionel Sambuc   (void)&((&hm)->*pi);
102*f4a2713aSLionel Sambuc 
103*f4a2713aSLionel Sambuc   void (HasMembers::*pf)() = &HasMembers::f;
104*f4a2713aSLionel Sambuc   (hm.*pf)();
105*f4a2713aSLionel Sambuc   (phm->*pf)();
106*f4a2713aSLionel Sambuc 
107*f4a2713aSLionel Sambuc   (void)(hm->*pi); // expected-error {{left hand operand to ->* must be a pointer to class compatible with the right hand operand, but is 'HasMembers'}}
108*f4a2713aSLionel Sambuc   (void)(phm.*pi); // expected-error {{left hand operand to .* must be a class compatible with the right hand operand, but is 'HasMembers *'}}
109*f4a2713aSLionel Sambuc   (void)(i.*pi); // expected-error {{left hand operand to .* must be a class compatible with the right hand operand, but is 'int'}}
110*f4a2713aSLionel Sambuc   int *ptr;
111*f4a2713aSLionel Sambuc   (void)(ptr->*pi); // expected-error {{left hand operand to ->* must be a pointer to class compatible with the right hand operand, but is 'int *'}}
112*f4a2713aSLionel Sambuc 
113*f4a2713aSLionel Sambuc   int A::*pai = 0;
114*f4a2713aSLionel Sambuc   D d, *pd = &d;
115*f4a2713aSLionel Sambuc   (void)(d.*pai);
116*f4a2713aSLionel Sambuc   (void)(pd->*pai);
117*f4a2713aSLionel Sambuc   F f, *ptrf = &f;
118*f4a2713aSLionel Sambuc   (void)(f.*pai); // expected-error {{left hand operand to .* must be a class compatible with the right hand operand, but is 'F'}}
119*f4a2713aSLionel Sambuc   (void)(ptrf->*pai); // expected-error {{left hand operand to ->* must be a pointer to class compatible with the right hand operand, but is 'F *'}}
120*f4a2713aSLionel Sambuc 
121*f4a2713aSLionel Sambuc   (void)(hm.*i); // expected-error {{pointer-to-member}}
122*f4a2713aSLionel Sambuc   (void)(phm->*i); // expected-error {{pointer-to-member}}
123*f4a2713aSLionel Sambuc 
124*f4a2713aSLionel Sambuc   // Okay
125*f4a2713aSLionel Sambuc   Incomplete *inc;
126*f4a2713aSLionel Sambuc   int Incomplete::*pii = 0;
127*f4a2713aSLionel Sambuc   (void)(inc->*pii);
128*f4a2713aSLionel Sambuc }
129*f4a2713aSLionel Sambuc 
130*f4a2713aSLionel Sambuc struct OverloadsPtrMem
131*f4a2713aSLionel Sambuc {
132*f4a2713aSLionel Sambuc   int operator ->*(const char *);
133*f4a2713aSLionel Sambuc };
134*f4a2713aSLionel Sambuc 
135*f4a2713aSLionel Sambuc void i() {
136*f4a2713aSLionel Sambuc   OverloadsPtrMem m;
137*f4a2713aSLionel Sambuc   int foo = m->*"Awesome!";
138*f4a2713aSLionel Sambuc }
139*f4a2713aSLionel Sambuc 
140*f4a2713aSLionel Sambuc namespace pr5985 {
141*f4a2713aSLionel Sambuc   struct c {
142*f4a2713aSLionel Sambuc     void h();
143*f4a2713aSLionel Sambuc     void f() {
144*f4a2713aSLionel Sambuc       void (c::*p)();
145*f4a2713aSLionel Sambuc       p = &h; // expected-error {{must explicitly qualify}}
146*f4a2713aSLionel Sambuc       p = &this->h; // expected-error {{cannot create a non-constant pointer to member function}}
147*f4a2713aSLionel Sambuc       p = &(*this).h; // expected-error {{cannot create a non-constant pointer to member function}}
148*f4a2713aSLionel Sambuc     }
149*f4a2713aSLionel Sambuc   };
150*f4a2713aSLionel Sambuc }
151*f4a2713aSLionel Sambuc 
152*f4a2713aSLionel Sambuc namespace pr6783 {
153*f4a2713aSLionel Sambuc   struct Base {};
154*f4a2713aSLionel Sambuc   struct X; // expected-note {{forward declaration}}
155*f4a2713aSLionel Sambuc 
156*f4a2713aSLionel Sambuc   int test1(int Base::* p2m, X* object)
157*f4a2713aSLionel Sambuc   {
158*f4a2713aSLionel Sambuc     return object->*p2m; // expected-error {{left hand operand to ->*}}
159*f4a2713aSLionel Sambuc   }
160*f4a2713aSLionel Sambuc }
161*f4a2713aSLionel Sambuc 
162*f4a2713aSLionel Sambuc namespace PR7176 {
163*f4a2713aSLionel Sambuc   namespace base
164*f4a2713aSLionel Sambuc   {
165*f4a2713aSLionel Sambuc     struct Process
166*f4a2713aSLionel Sambuc     { };
167*f4a2713aSLionel Sambuc     struct Continuous : Process
168*f4a2713aSLionel Sambuc     {
169*f4a2713aSLionel Sambuc       bool cond();
170*f4a2713aSLionel Sambuc     };
171*f4a2713aSLionel Sambuc   }
172*f4a2713aSLionel Sambuc 
173*f4a2713aSLionel Sambuc   typedef bool( base::Process::*Condition )();
174*f4a2713aSLionel Sambuc 
175*f4a2713aSLionel Sambuc   void m()
176*f4a2713aSLionel Sambuc   { (void)(Condition) &base::Continuous::cond; }
177*f4a2713aSLionel Sambuc }
178*f4a2713aSLionel Sambuc 
179*f4a2713aSLionel Sambuc namespace rdar8358512 {
180*f4a2713aSLionel Sambuc   // We can't call this with an overload set because we're not allowed
181*f4a2713aSLionel Sambuc   // to look into overload sets unless the parameter has some kind of
182*f4a2713aSLionel Sambuc   // function type.
183*f4a2713aSLionel Sambuc   template <class F> void bind(F f); // expected-note 12 {{candidate template ignored}}
184*f4a2713aSLionel Sambuc   template <class F, class T> void bindmem(F (T::*f)()); // expected-note 4 {{candidate template ignored}}
185*f4a2713aSLionel Sambuc   template <class F> void bindfn(F (*f)()); // expected-note 4 {{candidate template ignored}}
186*f4a2713aSLionel Sambuc 
187*f4a2713aSLionel Sambuc   struct A {
188*f4a2713aSLionel Sambuc     void nonstat();
189*f4a2713aSLionel Sambuc     void nonstat(int);
190*f4a2713aSLionel Sambuc 
191*f4a2713aSLionel Sambuc     void mixed();
192*f4a2713aSLionel Sambuc     static void mixed(int);
193*f4a2713aSLionel Sambuc 
194*f4a2713aSLionel Sambuc     static void stat();
195*f4a2713aSLionel Sambuc     static void stat(int);
196*f4a2713aSLionel Sambuc 
197*f4a2713aSLionel Sambuc     template <typename T> struct Test0 {
198*f4a2713aSLionel Sambuc       void test() {
199*f4a2713aSLionel Sambuc         bind(&nonstat); // expected-error {{no matching function for call}}
200*f4a2713aSLionel Sambuc         bind(&A::nonstat); // expected-error {{no matching function for call}}
201*f4a2713aSLionel Sambuc 
202*f4a2713aSLionel Sambuc         bind(&mixed); // expected-error {{no matching function for call}}
203*f4a2713aSLionel Sambuc         bind(&A::mixed); // expected-error {{no matching function for call}}
204*f4a2713aSLionel Sambuc 
205*f4a2713aSLionel Sambuc         bind(&stat); // expected-error {{no matching function for call}}
206*f4a2713aSLionel Sambuc         bind(&A::stat); // expected-error {{no matching function for call}}
207*f4a2713aSLionel Sambuc       }
208*f4a2713aSLionel Sambuc     };
209*f4a2713aSLionel Sambuc 
210*f4a2713aSLionel Sambuc     template <typename T> struct Test1 {
211*f4a2713aSLionel Sambuc       void test() {
212*f4a2713aSLionel Sambuc         bindmem(&nonstat); // expected-error {{no matching function for call}}
213*f4a2713aSLionel Sambuc         bindmem(&A::nonstat);
214*f4a2713aSLionel Sambuc 
215*f4a2713aSLionel Sambuc         bindmem(&mixed); // expected-error {{no matching function for call}}
216*f4a2713aSLionel Sambuc         bindmem(&A::mixed);
217*f4a2713aSLionel Sambuc 
218*f4a2713aSLionel Sambuc         bindmem(&stat); // expected-error {{no matching function for call}}
219*f4a2713aSLionel Sambuc         bindmem(&A::stat); // expected-error {{no matching function for call}}
220*f4a2713aSLionel Sambuc       }
221*f4a2713aSLionel Sambuc     };
222*f4a2713aSLionel Sambuc 
223*f4a2713aSLionel Sambuc     template <typename T> struct Test2 {
224*f4a2713aSLionel Sambuc       void test() {
225*f4a2713aSLionel Sambuc         bindfn(&nonstat); // expected-error {{no matching function for call}}
226*f4a2713aSLionel Sambuc         bindfn(&A::nonstat); // expected-error {{no matching function for call}}
227*f4a2713aSLionel Sambuc 
228*f4a2713aSLionel Sambuc         bindfn(&mixed); // expected-error {{no matching function for call}}
229*f4a2713aSLionel Sambuc         bindfn(&A::mixed); // expected-error {{no matching function for call}}
230*f4a2713aSLionel Sambuc 
231*f4a2713aSLionel Sambuc         bindfn(&stat);
232*f4a2713aSLionel Sambuc         bindfn(&A::stat);
233*f4a2713aSLionel Sambuc       }
234*f4a2713aSLionel Sambuc     };
235*f4a2713aSLionel Sambuc   };
236*f4a2713aSLionel Sambuc 
237*f4a2713aSLionel Sambuc   template <class T> class B {
238*f4a2713aSLionel Sambuc     void nonstat();
239*f4a2713aSLionel Sambuc     void nonstat(int);
240*f4a2713aSLionel Sambuc 
241*f4a2713aSLionel Sambuc     void mixed();
242*f4a2713aSLionel Sambuc     static void mixed(int);
243*f4a2713aSLionel Sambuc 
244*f4a2713aSLionel Sambuc     static void stat();
245*f4a2713aSLionel Sambuc     static void stat(int);
246*f4a2713aSLionel Sambuc 
247*f4a2713aSLionel Sambuc     // None of these can be diagnosed yet, because the arguments are
248*f4a2713aSLionel Sambuc     // still dependent.
249*f4a2713aSLionel Sambuc     void test0a() {
250*f4a2713aSLionel Sambuc       bind(&nonstat);
251*f4a2713aSLionel Sambuc       bind(&B::nonstat);
252*f4a2713aSLionel Sambuc 
253*f4a2713aSLionel Sambuc       bind(&mixed);
254*f4a2713aSLionel Sambuc       bind(&B::mixed);
255*f4a2713aSLionel Sambuc 
256*f4a2713aSLionel Sambuc       bind(&stat);
257*f4a2713aSLionel Sambuc       bind(&B::stat);
258*f4a2713aSLionel Sambuc     }
259*f4a2713aSLionel Sambuc 
260*f4a2713aSLionel Sambuc     void test0b() {
261*f4a2713aSLionel Sambuc       bind(&nonstat); // expected-error {{no matching function for call}}
262*f4a2713aSLionel Sambuc       bind(&B::nonstat); // expected-error {{no matching function for call}}
263*f4a2713aSLionel Sambuc 
264*f4a2713aSLionel Sambuc       bind(&mixed); // expected-error {{no matching function for call}}
265*f4a2713aSLionel Sambuc       bind(&B::mixed); // expected-error {{no matching function for call}}
266*f4a2713aSLionel Sambuc 
267*f4a2713aSLionel Sambuc       bind(&stat); // expected-error {{no matching function for call}}
268*f4a2713aSLionel Sambuc       bind(&B::stat); // expected-error {{no matching function for call}}
269*f4a2713aSLionel Sambuc     }
270*f4a2713aSLionel Sambuc   };
271*f4a2713aSLionel Sambuc 
272*f4a2713aSLionel Sambuc   template void B<int>::test0b(); // expected-note {{in instantiation}}
273*f4a2713aSLionel Sambuc }
274*f4a2713aSLionel Sambuc 
275*f4a2713aSLionel Sambuc namespace PR9973 {
276*f4a2713aSLionel Sambuc   template<class R, class T> struct dm
277*f4a2713aSLionel Sambuc   {
278*f4a2713aSLionel Sambuc     typedef R T::*F;
279*f4a2713aSLionel Sambuc     F f_;
280*f4a2713aSLionel Sambuc     template<class U> int & call(U u)
281*f4a2713aSLionel Sambuc     { return u->*f_; } // expected-error{{reference to non-static member function must be called; did you mean to call it with no arguments?}} expected-error {{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}
282*f4a2713aSLionel Sambuc 
283*f4a2713aSLionel Sambuc     template<class U> int operator()(U u)
284*f4a2713aSLionel Sambuc     { call(u); } // expected-note{{in instantiation of}}
285*f4a2713aSLionel Sambuc   };
286*f4a2713aSLionel Sambuc 
287*f4a2713aSLionel Sambuc   template<class R, class T>
288*f4a2713aSLionel Sambuc   dm<R, T> mem_fn(R T::*) ;
289*f4a2713aSLionel Sambuc 
290*f4a2713aSLionel Sambuc   struct test
291*f4a2713aSLionel Sambuc   { int nullary_v(); };
292*f4a2713aSLionel Sambuc 
293*f4a2713aSLionel Sambuc   void f()
294*f4a2713aSLionel Sambuc   {
295*f4a2713aSLionel Sambuc     test* t;
296*f4a2713aSLionel Sambuc     mem_fn(&test::nullary_v)(t); // expected-note{{in instantiation of}}
297*f4a2713aSLionel Sambuc   }
298*f4a2713aSLionel Sambuc }
299*f4a2713aSLionel Sambuc 
300*f4a2713aSLionel Sambuc namespace test8 {
301*f4a2713aSLionel Sambuc   struct A { int foo; };
302*f4a2713aSLionel Sambuc   int test1() {
303*f4a2713aSLionel Sambuc     // Verify that we perform (and check) an lvalue conversion on the operands here.
304*f4a2713aSLionel Sambuc     return (*((A**) 0)) // expected-warning {{indirection of non-volatile null pointer will be deleted}} expected-note {{consider}}
305*f4a2713aSLionel Sambuc              ->**(int A::**) 0; // expected-warning {{indirection of non-volatile null pointer will be deleted}} expected-note {{consider}}
306*f4a2713aSLionel Sambuc   }
307*f4a2713aSLionel Sambuc 
308*f4a2713aSLionel Sambuc   int test2() {
309*f4a2713aSLionel Sambuc     // Verify that we perform (and check) an lvalue conversion on the operands here.
310*f4a2713aSLionel Sambuc     // TODO: the .* should itself warn about being a dereference of null.
311*f4a2713aSLionel Sambuc     return (*((A*) 0))
312*f4a2713aSLionel Sambuc              .**(int A::**) 0; // expected-warning {{indirection of non-volatile null pointer will be deleted}} expected-note {{consider}}
313*f4a2713aSLionel Sambuc   }
314*f4a2713aSLionel Sambuc }
315