blob: 1a663f6e1ccaaed555ead8547a64d9734ad17e1f [file] [log] [blame]
Daniel Dunbard7d5f022009-03-24 02:24:46 +00001// RUN: clang-cc -fsyntax-only -verify %s
Sebastian Redlf30208a2009-01-24 21:16:55 +00002
3struct A {};
4enum B { Dummy };
5namespace C {}
Sebastian Redl4433aaf2009-01-25 19:43:20 +00006struct D : A {};
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +00007struct E : A {};
8struct F : D, E {};
9struct G : virtual D {};
Sebastian Redlf30208a2009-01-24 21:16:55 +000010
11int A::*pdi1;
12int (::A::*pdi2);
13int (A::*pfi)(int);
14
15int B::*pbi; // expected-error {{expected a class or namespace}}
16int C::*pci; // expected-error {{'pci' does not point into a class}}
17void A::*pdv; // expected-error {{'pdv' declared as a member pointer to void}}
18int& A::*pdr; // expected-error {{'pdr' declared as a pointer to a reference}}
Sebastian Redl8edef7c2009-01-24 23:29:36 +000019
20void f() {
21 // This requires tentative parsing.
22 int (A::*pf)(int, int);
Sebastian Redl4433aaf2009-01-25 19:43:20 +000023
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 Redl9e5e4aa2009-01-26 19:54:48 +000035
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 Redl21593ac2009-01-28 18:33:18 +000039
40 // Conversion to member of base.
41 pdi1 = pdid; // expected-error {{incompatible type assigning 'int struct D::*', expected 'int struct A::*'}}
Sebastian Redl8edef7c2009-01-24 23:29:36 +000042}
Sebastian Redlebc07d52009-02-03 20:19:35 +000043
Sebastian Redl33b399a2009-02-04 21:23:32 +000044struct TheBase
45{
46 void d();
47};
48
49struct HasMembers : TheBase
Sebastian Redlebc07d52009-02-03 20:19:35 +000050{
51 int i;
52 void f();
Sebastian Redl33b399a2009-02-04 21:23:32 +000053
54 void g();
55 void g(int);
56 static void g(double);
Sebastian Redlebc07d52009-02-03 20:19:35 +000057};
58
59namespace Fake
60{
61 int i;
62 void f();
63}
64
65void g() {
Sebastian Redl33b399a2009-02-04 21:23:32 +000066 HasMembers hm;
67
Sebastian Redlebc07d52009-02-03 20:19:35 +000068 int HasMembers::*pmi = &HasMembers::i;
69 int *pni = &Fake::i;
Sebastian Redl33b399a2009-02-04 21:23:32 +000070 int *pmii = &hm.i;
Sebastian Redlebc07d52009-02-03 20:19:35 +000071
Sebastian Redl33b399a2009-02-04 21:23:32 +000072 void (HasMembers::*pmf)() = &HasMembers::f;
Sebastian Redlebc07d52009-02-03 20:19:35 +000073 void (*pnf)() = &Fake::f;
Eli Friedman9895d882009-04-28 17:59:09 +000074 &hm.f; // FIXME: needs diagnostic expected-warning{{result unused}}
Sebastian Redl33b399a2009-02-04 21:23:32 +000075
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 Redlebc07d52009-02-03 20:19:35 +000081}
Sebastian Redl22460502009-02-07 00:15:38 +000082
Douglas Gregore7450f52009-03-24 19:52:54 +000083struct Incomplete; // expected-note{{forward declaration}}
Sebastian Redl7878ffd2009-02-07 00:41:42 +000084
Sebastian Redl22460502009-02-07 00:15:38 +000085void 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 Redl7878ffd2009-02-07 00:41:42 +000098
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 Gregore7450f52009-03-24 19:52:54 +0000113 (void)(hm.*i); // expected-error {{pointer-to-member}}
114 (void)(phm->*i); // expected-error {{pointer-to-member}}
Sebastian Redl7878ffd2009-02-07 00:41:42 +0000115
116 Incomplete *inc;
117 int Incomplete::*pii = 0;
Douglas Gregore7450f52009-03-24 19:52:54 +0000118 (void)inc->*pii; // expected-error {{right hand operand is a pointer to member of incomplete type 'struct Incomplete'}}
Sebastian Redl22460502009-02-07 00:15:38 +0000119}
120
121struct OverloadsPtrMem
122{
123 int operator ->*(const char *);
124};
125
126void i() {
127 OverloadsPtrMem m;
128 int foo = m->*"Awesome!";
129}