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