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