Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -fsyntax-only -verify %s |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2 | |
| 3 | struct A {}; |
| 4 | enum B { Dummy }; |
| 5 | namespace C {} |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 6 | struct D : A {}; |
Sebastian Redl | 9e5e4aa | 2009-01-26 19:54:48 +0000 | [diff] [blame] | 7 | struct E : A {}; |
| 8 | struct F : D, E {}; |
| 9 | struct G : virtual D {}; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 10 | |
| 11 | int A::*pdi1; |
| 12 | int (::A::*pdi2); |
| 13 | int (A::*pfi)(int); |
| 14 | |
| 15 | int B::*pbi; // expected-error {{expected a class or namespace}} |
| 16 | int C::*pci; // expected-error {{'pci' does not point into a class}} |
| 17 | void A::*pdv; // expected-error {{'pdv' declared as a member pointer to void}} |
| 18 | int& A::*pdr; // expected-error {{'pdr' declared as a pointer to a reference}} |
Sebastian Redl | 8edef7c | 2009-01-24 23:29:36 +0000 | [diff] [blame] | 19 | |
| 20 | void f() { |
| 21 | // This requires tentative parsing. |
| 22 | int (A::*pf)(int, int); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 23 | |
| 24 | // Implicit conversion to bool. |
| 25 | bool b = pdi1; |
| 26 | b = pfi; |
| 27 | |
| 28 | // Conversion from null pointer constant. |
| 29 | pf = 0; |
| 30 | pf = __null; |
| 31 | |
| 32 | // Conversion to member of derived. |
| 33 | int D::*pdid = pdi1; |
| 34 | pdid = pdi2; |
Sebastian Redl | 9e5e4aa | 2009-01-26 19:54:48 +0000 | [diff] [blame] | 35 | |
| 36 | // Fail conversion due to ambiguity and virtuality. |
| 37 | int F::*pdif = pdi1; // expected-error {{ambiguous conversion from pointer to member of base class 'struct A' to pointer to member of derived class 'struct F'}} expected-error {{incompatible type}} |
| 38 | int G::*pdig = pdi1; // expected-error {{conversion from pointer to member of class 'struct A' to pointer to member of class 'struct G' via virtual base 'struct D' is not allowed}} expected-error {{incompatible type}} |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 39 | |
| 40 | // Conversion to member of base. |
| 41 | pdi1 = pdid; // expected-error {{incompatible type assigning 'int struct D::*', expected 'int struct A::*'}} |
Sebastian Redl | 8edef7c | 2009-01-24 23:29:36 +0000 | [diff] [blame] | 42 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 43 | |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 44 | struct TheBase |
| 45 | { |
| 46 | void d(); |
| 47 | }; |
| 48 | |
| 49 | struct HasMembers : TheBase |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 50 | { |
| 51 | int i; |
| 52 | void f(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 53 | |
| 54 | void g(); |
| 55 | void g(int); |
| 56 | static void g(double); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | namespace Fake |
| 60 | { |
| 61 | int i; |
| 62 | void f(); |
| 63 | } |
| 64 | |
| 65 | void g() { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 66 | HasMembers hm; |
| 67 | |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 68 | int HasMembers::*pmi = &HasMembers::i; |
| 69 | int *pni = &Fake::i; |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 70 | int *pmii = &hm.i; |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 71 | |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 72 | void (HasMembers::*pmf)() = &HasMembers::f; |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 73 | void (*pnf)() = &Fake::f; |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 74 | &hm.f; // expected-error {{address expression must be an lvalue or a function designator}} |
| 75 | |
| 76 | void (HasMembers::*pmgv)() = &HasMembers::g; |
| 77 | void (HasMembers::*pmgi)(int) = &HasMembers::g; |
| 78 | void (*pmgd)(double) = &HasMembers::g; |
| 79 | |
| 80 | void (HasMembers::*pmd)() = &HasMembers::d; |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 81 | } |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 82 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame^] | 83 | struct Incomplete; // expected-note{{forward declaration}} |
Sebastian Redl | 7878ffd | 2009-02-07 00:41:42 +0000 | [diff] [blame] | 84 | |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 85 | void h() { |
| 86 | HasMembers hm, *phm = &hm; |
| 87 | |
| 88 | int HasMembers::*pi = &HasMembers::i; |
| 89 | hm.*pi = 0; |
| 90 | int i = phm->*pi; |
| 91 | (void)&(hm.*pi); |
| 92 | (void)&(phm->*pi); |
| 93 | (void)&((&hm)->*pi); // expected-error {{address expression must be an lvalue or a function designator}} |
| 94 | |
| 95 | void (HasMembers::*pf)() = &HasMembers::f; |
| 96 | (hm.*pf)(); |
| 97 | (phm->*pf)(); |
Sebastian Redl | 7878ffd | 2009-02-07 00:41:42 +0000 | [diff] [blame] | 98 | |
| 99 | (void)(hm->*pi); // expected-error {{left hand operand to ->* must be a pointer to class compatible with the right hand operand, but is 'struct HasMembers'}} |
| 100 | (void)(phm.*pi); // expected-error {{left hand operand to .* must be a class compatible with the right hand operand, but is 'struct HasMembers *'}} |
| 101 | (void)(i.*pi); // expected-error {{left hand operand to .* must be a class compatible with the right hand operand, but is 'int'}} |
| 102 | int *ptr; |
| 103 | (void)(ptr->*pi); // expected-error {{left hand operand to ->* must be a pointer to class compatible with the right hand operand, but is 'int *'}} |
| 104 | |
| 105 | int A::*pai = 0; |
| 106 | D d, *pd = &d; |
| 107 | (void)(d.*pai); |
| 108 | (void)(pd->*pai); |
| 109 | F f, *ptrf = &f; |
| 110 | (void)(f.*pai); // expected-error {{left hand operand to .* must be a class compatible with the right hand operand, but is 'struct F'}} |
| 111 | (void)(ptrf->*pai); // expected-error {{left hand operand to ->* must be a pointer to class compatible with the right hand operand, but is 'struct F *'}} |
| 112 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame^] | 113 | (void)(hm.*i); // expected-error {{pointer-to-member}} |
| 114 | (void)(phm->*i); // expected-error {{pointer-to-member}} |
Sebastian Redl | 7878ffd | 2009-02-07 00:41:42 +0000 | [diff] [blame] | 115 | |
| 116 | Incomplete *inc; |
| 117 | int Incomplete::*pii = 0; |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame^] | 118 | (void)inc->*pii; // expected-error {{right hand operand is a pointer to member of incomplete type 'struct Incomplete'}} |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | struct OverloadsPtrMem |
| 122 | { |
| 123 | int operator ->*(const char *); |
| 124 | }; |
| 125 | |
| 126 | void i() { |
| 127 | OverloadsPtrMem m; |
| 128 | int foo = m->*"Awesome!"; |
| 129 | } |