blob: 83823f69c6373be2878fe0c6e2aa1054dd511a4b [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -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
John McCall9ba61662010-02-26 08:45:28 +000015int B::*pbi; // expected-error {{expected a class or namespace}}
Sebastian Redlf30208a2009-01-24 21:16:55 +000016int C::*pci; // expected-error {{'pci' does not point into a class}}
17void A::*pdv; // expected-error {{'pdv' declared as a member pointer to void}}
Anders Carlsson8d4655d2009-06-30 00:06:57 +000018int& A::*pdr; // expected-error {{'pdr' declared as a member 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.
John McCall7c2342d2010-03-10 11:27:22 +000037 int F::*pdif = pdi1; // expected-error {{ambiguous conversion from pointer to member of base class 'A' to pointer to member of derived class 'F':}}
38 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}}
Sebastian Redl21593ac2009-01-28 18:33:18 +000039
40 // Conversion to member of base.
Douglas Gregord4eea832010-04-09 00:35:39 +000041 pdi1 = pdid; // expected-error {{assigning to 'int A::*' from incompatible type 'int D::*'}}
Douglas Gregor20b3e992009-08-24 17:42:35 +000042
43 // Comparisons
44 int (A::*pf2)(int, int);
45 int (D::*pf3)(int, int) = 0;
46 bool b1 = (pf == pf2); (void)b1;
47 bool b2 = (pf != pf2); (void)b2;
48 bool b3 = (pf == pf3); (void)b3;
49 bool b4 = (pf != 0); (void)b4;
Sebastian Redl8edef7c2009-01-24 23:29:36 +000050}
Sebastian Redlebc07d52009-02-03 20:19:35 +000051
Sebastian Redl33b399a2009-02-04 21:23:32 +000052struct TheBase
53{
54 void d();
55};
56
57struct HasMembers : TheBase
Sebastian Redlebc07d52009-02-03 20:19:35 +000058{
59 int i;
60 void f();
Sebastian Redl33b399a2009-02-04 21:23:32 +000061
62 void g();
63 void g(int);
64 static void g(double);
Sebastian Redlebc07d52009-02-03 20:19:35 +000065};
66
67namespace Fake
68{
69 int i;
70 void f();
71}
72
73void g() {
Sebastian Redl33b399a2009-02-04 21:23:32 +000074 HasMembers hm;
75
Sebastian Redlebc07d52009-02-03 20:19:35 +000076 int HasMembers::*pmi = &HasMembers::i;
77 int *pni = &Fake::i;
Sebastian Redl33b399a2009-02-04 21:23:32 +000078 int *pmii = &hm.i;
Sebastian Redlebc07d52009-02-03 20:19:35 +000079
Sebastian Redl33b399a2009-02-04 21:23:32 +000080 void (HasMembers::*pmf)() = &HasMembers::f;
Sebastian Redlebc07d52009-02-03 20:19:35 +000081 void (*pnf)() = &Fake::f;
John McCall9c72c602010-08-27 09:08:28 +000082 &hm.f; // expected-error {{cannot create a non-constant pointer to member function}}
Sebastian Redl33b399a2009-02-04 21:23:32 +000083
84 void (HasMembers::*pmgv)() = &HasMembers::g;
85 void (HasMembers::*pmgi)(int) = &HasMembers::g;
86 void (*pmgd)(double) = &HasMembers::g;
87
88 void (HasMembers::*pmd)() = &HasMembers::d;
Sebastian Redlebc07d52009-02-03 20:19:35 +000089}
Sebastian Redl22460502009-02-07 00:15:38 +000090
Sebastian Redl59fc2692010-04-10 10:14:54 +000091struct Incomplete; // expected-note {{forward declaration}}
Sebastian Redl7878ffd2009-02-07 00:41:42 +000092
Sebastian Redl22460502009-02-07 00:15:38 +000093void h() {
94 HasMembers hm, *phm = &hm;
95
96 int HasMembers::*pi = &HasMembers::i;
97 hm.*pi = 0;
98 int i = phm->*pi;
99 (void)&(hm.*pi);
100 (void)&(phm->*pi);
Fariborz Jahanian27d4be52009-10-08 18:00:39 +0000101 (void)&((&hm)->*pi);
Sebastian Redl22460502009-02-07 00:15:38 +0000102
103 void (HasMembers::*pf)() = &HasMembers::f;
104 (hm.*pf)();
105 (phm->*pf)();
Sebastian Redl7878ffd2009-02-07 00:41:42 +0000106
John McCall7c2342d2010-03-10 11:27:22 +0000107 (void)(hm->*pi); // expected-error {{left hand operand to ->* must be a pointer to class compatible with the right hand operand, but is 'HasMembers'}}
108 (void)(phm.*pi); // expected-error {{left hand operand to .* must be a class compatible with the right hand operand, but is 'HasMembers *'}}
Sebastian Redl7878ffd2009-02-07 00:41:42 +0000109 (void)(i.*pi); // expected-error {{left hand operand to .* must be a class compatible with the right hand operand, but is 'int'}}
110 int *ptr;
111 (void)(ptr->*pi); // expected-error {{left hand operand to ->* must be a pointer to class compatible with the right hand operand, but is 'int *'}}
112
113 int A::*pai = 0;
114 D d, *pd = &d;
115 (void)(d.*pai);
116 (void)(pd->*pai);
117 F f, *ptrf = &f;
John McCall7c2342d2010-03-10 11:27:22 +0000118 (void)(f.*pai); // expected-error {{left hand operand to .* must be a class compatible with the right hand operand, but is 'F'}}
119 (void)(ptrf->*pai); // expected-error {{left hand operand to ->* must be a pointer to class compatible with the right hand operand, but is 'F *'}}
Sebastian Redl7878ffd2009-02-07 00:41:42 +0000120
Douglas Gregore7450f52009-03-24 19:52:54 +0000121 (void)(hm.*i); // expected-error {{pointer-to-member}}
122 (void)(phm->*i); // expected-error {{pointer-to-member}}
Sebastian Redl7878ffd2009-02-07 00:41:42 +0000123
124 Incomplete *inc;
125 int Incomplete::*pii = 0;
Sebastian Redl59fc2692010-04-10 10:14:54 +0000126 (void)(inc->*pii); // expected-error {{pointer into incomplete}}
Sebastian Redl22460502009-02-07 00:15:38 +0000127}
128
129struct OverloadsPtrMem
130{
131 int operator ->*(const char *);
132};
133
134void i() {
135 OverloadsPtrMem m;
136 int foo = m->*"Awesome!";
137}
Sebastian Redle27d87f2010-01-11 15:56:56 +0000138
139namespace pr5985 {
140 struct c {
141 void h();
142 void f() {
143 void (c::*p)();
144 p = &h; // expected-error {{must explicitly qualify}}
John McCall9c72c602010-08-27 09:08:28 +0000145 p = &this->h; // expected-error {{cannot create a non-constant pointer to member function}}
146 p = &(*this).h; // expected-error {{cannot create a non-constant pointer to member function}}
Sebastian Redle27d87f2010-01-11 15:56:56 +0000147 }
148 };
149}
Sebastian Redl17e1d352010-04-23 17:18:26 +0000150
151namespace pr6783 {
152 struct Base {};
153 struct X; // expected-note {{forward declaration}}
154
155 int test1(int Base::* p2m, X* object)
156 {
157 return object->*p2m; // expected-error {{left hand operand to ->*}}
158 }
159}
Douglas Gregore0d5fe22010-05-21 20:29:55 +0000160
161namespace PR7176 {
162 namespace base
163 {
164 struct Process
165 { };
166 struct Continuous : Process
167 {
168 bool cond();
169 };
170 }
171
172 typedef bool( base::Process::*Condition )();
173
174 void m()
175 { (void)(Condition) &base::Continuous::cond; }
176}
John McCall9c72c602010-08-27 09:08:28 +0000177
178namespace rdar8358512 {
179 // We can't call this with an overload set because we're not allowed
180 // to look into overload sets unless the parameter has some kind of
181 // function type.
182 template <class F> void bind(F f); // expected-note 6 {{candidate template ignored}}
183 template <class F, class T> void bindmem(F (T::*f)()); // expected-note 4 {{candidate template ignored}}
184 template <class F> void bindfn(F (*f)()); // expected-note 4 {{candidate template ignored}}
185
186 struct A {
187 void member();
188
189 void nonstat();
190 void nonstat(int);
191
192 void mixed();
193 static void mixed(int);
194
195 static void stat();
196 static void stat(int);
197
198 template <typename T> struct Test0 {
199 void test() {
200 bind(&nonstat); // expected-error {{no matching function for call}}
201 bind(&A::nonstat); // expected-error {{no matching function for call}}
202
203 bind(&mixed); // expected-error {{no matching function for call}}
204 bind(&A::mixed); // expected-error {{no matching function for call}}
205
206 bind(&stat); // expected-error {{no matching function for call}}
207 bind(&A::stat); // expected-error {{no matching function for call}}
208 }
209 };
210
211 template <typename T> struct Test1 {
212 void test() {
213 bindmem(&nonstat); // expected-error {{no matching function for call}}
214 bindmem(&A::nonstat);
215
216 bindmem(&mixed); // expected-error {{no matching function for call}}
217 bindmem(&A::mixed);
218
219 bindmem(&stat); // expected-error {{no matching function for call}}
220 bindmem(&A::stat); // expected-error {{no matching function for call}}
221 }
222 };
223
224 template <typename T> struct Test2 {
225 void test() {
226 bindfn(&nonstat); // expected-error {{no matching function for call}}
227 bindfn(&A::nonstat); // expected-error {{no matching function for call}}
228
229 bindfn(&mixed); // expected-error {{no matching function for call}}
230 bindfn(&A::mixed); // expected-error {{no matching function for call}}
231
232 bindfn(&stat);
233 bindfn(&A::stat);
234 }
235 };
236 };
237}