Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame^] | 1 | // RUN: %clang_cc1 -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 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 15 | int B::*pbi; // expected-error {{expected a class or namespace}} \ |
| 16 | // expected-error{{does not point into a class}} |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 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}} |
Anders Carlsson | 8d4655d | 2009-06-30 00:06:57 +0000 | [diff] [blame] | 19 | int& A::*pdr; // expected-error {{'pdr' declared as a member pointer to a reference}} |
Sebastian Redl | 8edef7c | 2009-01-24 23:29:36 +0000 | [diff] [blame] | 20 | |
| 21 | void f() { |
| 22 | // This requires tentative parsing. |
| 23 | int (A::*pf)(int, int); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 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; |
Sebastian Redl | 9e5e4aa | 2009-01-26 19:54:48 +0000 | [diff] [blame] | 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 'struct A' to pointer to member of derived class 'struct F'}} expected-error {{incompatible type}} |
| 39 | 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] | 40 | |
| 41 | // Conversion to member of base. |
| 42 | pdi1 = pdid; // expected-error {{incompatible type assigning 'int struct D::*', expected 'int struct A::*'}} |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 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; |
Sebastian Redl | 8edef7c | 2009-01-24 23:29:36 +0000 | [diff] [blame] | 51 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 52 | |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 53 | struct TheBase |
| 54 | { |
| 55 | void d(); |
| 56 | }; |
| 57 | |
| 58 | struct HasMembers : TheBase |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 59 | { |
| 60 | int i; |
| 61 | void f(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 62 | |
| 63 | void g(); |
| 64 | void g(int); |
| 65 | static void g(double); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | namespace Fake |
| 69 | { |
| 70 | int i; |
| 71 | void f(); |
| 72 | } |
| 73 | |
| 74 | void g() { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 75 | HasMembers hm; |
| 76 | |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 77 | int HasMembers::*pmi = &HasMembers::i; |
| 78 | int *pni = &Fake::i; |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 79 | int *pmii = &hm.i; |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 80 | |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 81 | void (HasMembers::*pmf)() = &HasMembers::f; |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 82 | void (*pnf)() = &Fake::f; |
Eli Friedman | 9895d88 | 2009-04-28 17:59:09 +0000 | [diff] [blame] | 83 | &hm.f; // FIXME: needs diagnostic expected-warning{{result unused}} |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 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; |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 90 | } |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 91 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 92 | struct Incomplete; |
Sebastian Redl | 7878ffd | 2009-02-07 00:41:42 +0000 | [diff] [blame] | 93 | |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 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); |
Fariborz Jahanian | 27d4be5 | 2009-10-08 18:00:39 +0000 | [diff] [blame] | 102 | (void)&((&hm)->*pi); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 103 | |
| 104 | void (HasMembers::*pf)() = &HasMembers::f; |
| 105 | (hm.*pf)(); |
| 106 | (phm->*pf)(); |
Sebastian Redl | 7878ffd | 2009-02-07 00:41:42 +0000 | [diff] [blame] | 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 'struct HasMembers'}} |
| 109 | (void)(phm.*pi); // expected-error {{left hand operand to .* must be a class compatible with the right hand operand, but is 'struct 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 {{left hand operand to .* must be a class compatible with the right hand operand, but is 'struct F'}} |
| 120 | (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 *'}} |
| 121 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 122 | (void)(hm.*i); // expected-error {{pointer-to-member}} |
| 123 | (void)(phm->*i); // expected-error {{pointer-to-member}} |
Sebastian Redl | 7878ffd | 2009-02-07 00:41:42 +0000 | [diff] [blame] | 124 | |
| 125 | Incomplete *inc; |
| 126 | int Incomplete::*pii = 0; |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 127 | (void)(inc->*pii); // okay |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | struct OverloadsPtrMem |
| 131 | { |
| 132 | int operator ->*(const char *); |
| 133 | }; |
| 134 | |
| 135 | void i() { |
| 136 | OverloadsPtrMem m; |
| 137 | int foo = m->*"Awesome!"; |
| 138 | } |