1 // RUN: %clang_cc1 %s -emit-llvm -o - -triple=x86_64-unknown-unknown | FileCheck -check-prefix CODE-LP64 %s
2 // RUN: %clang_cc1 %s -emit-llvm -o - -triple=i386-unknown-unknown | FileCheck -check-prefix CODE-LP32 %s
3 // RUN: %clang_cc1 %s -emit-llvm -o - -triple=x86_64-unknown-unknown | FileCheck -check-prefix GLOBAL-LP64 %s
4 // RUN: %clang_cc1 %s -emit-llvm -o - -triple=i386-unknown-unknown | FileCheck -check-prefix GLOBAL-LP32 %s
5 // RUN: %clang_cc1 %s -emit-llvm -o - -triple=armv7-unknown-unknown | FileCheck -check-prefix GLOBAL-ARM %s
6
7 // PNaCl uses the same representation of method pointers as ARM.
8 // RUN: %clang_cc1 %s -emit-llvm -o - -triple=le32-unknown-nacl | FileCheck -check-prefix GLOBAL-ARM %s
9 // MIPS uses the same representation of method pointers as ARM.
10 // RUN: %clang_cc1 %s -emit-llvm -o - -triple=mips-unknown-linux-gnu | FileCheck -check-prefix GLOBAL-ARM %s
11
12 struct A { int a; void f(); virtual void vf1(); virtual void vf2(); };
13 struct B { int b; virtual void g(); };
14 struct C : B, A { };
15
16 void (A::*pa)();
17 void (A::*volatile vpa)();
18 void (B::*pb)();
19 void (C::*pc)();
20
21 // GLOBAL-LP64: @pa2 = global { i64, i64 } { i64 ptrtoint (void (%struct.A*)* @_ZN1A1fEv to i64), i64 0 }, align 8
22 void (A::*pa2)() = &A::f;
23
24 // GLOBAL-LP64: @pa3 = global { i64, i64 } { i64 1, i64 0 }, align 8
25 // GLOBAL-LP32: @pa3 = global { i32, i32 } { i32 1, i32 0 }, align 4
26 void (A::*pa3)() = &A::vf1;
27
28 // GLOBAL-LP64: @pa4 = global { i64, i64 } { i64 9, i64 0 }, align 8
29 // GLOBAL-LP32: @pa4 = global { i32, i32 } { i32 5, i32 0 }, align 4
30 void (A::*pa4)() = &A::vf2;
31
32 // GLOBAL-LP64: @pc2 = global { i64, i64 } { i64 ptrtoint (void (%struct.A*)* @_ZN1A1fEv to i64), i64 16 }, align 8
33 void (C::*pc2)() = &C::f;
34
35 // GLOBAL-LP64: @pc3 = global { i64, i64 } { i64 1, i64 0 }, align 8
36 void (A::*pc3)() = &A::vf1;
37
f()38 void f() {
39 // CODE-LP64: store { i64, i64 } zeroinitializer, { i64, i64 }* @pa
40 pa = 0;
41
42 // Is this okay? What are LLVM's volatile semantics for structs?
43 // CODE-LP64: store volatile { i64, i64 } zeroinitializer, { i64, i64 }* @vpa
44 vpa = 0;
45
46 // CODE-LP64: [[TMP:%.*]] = load { i64, i64 }, { i64, i64 }* @pa, align 8
47 // CODE-LP64: [[TMPADJ:%.*]] = extractvalue { i64, i64 } [[TMP]], 1
48 // CODE-LP64: [[ADJ:%.*]] = add nsw i64 [[TMPADJ]], 16
49 // CODE-LP64: [[RES:%.*]] = insertvalue { i64, i64 } [[TMP]], i64 [[ADJ]], 1
50 // CODE-LP64: store { i64, i64 } [[RES]], { i64, i64 }* @pc, align 8
51 pc = pa;
52
53 // CODE-LP64: [[TMP:%.*]] = load { i64, i64 }, { i64, i64 }* @pc, align 8
54 // CODE-LP64: [[TMPADJ:%.*]] = extractvalue { i64, i64 } [[TMP]], 1
55 // CODE-LP64: [[ADJ:%.*]] = sub nsw i64 [[TMPADJ]], 16
56 // CODE-LP64: [[RES:%.*]] = insertvalue { i64, i64 } [[TMP]], i64 [[ADJ]], 1
57 // CODE-LP64: store { i64, i64 } [[RES]], { i64, i64 }* @pa, align 8
58 pa = static_cast<void (A::*)()>(pc);
59 }
60
f2()61 void f2() {
62 // CODE-LP64: store { i64, i64 } { i64 ptrtoint (void (%struct.A*)* @_ZN1A1fEv to i64), i64 0 }
63 void (A::*pa2)() = &A::f;
64
65 // CODE-LP64: store { i64, i64 } { i64 1, i64 0 }
66 // CODE-LP32: store { i32, i32 } { i32 1, i32 0 }
67 void (A::*pa3)() = &A::vf1;
68
69 // CODE-LP64: store { i64, i64 } { i64 9, i64 0 }
70 // CODE-LP32: store { i32, i32 } { i32 5, i32 0 }
71 void (A::*pa4)() = &A::vf2;
72 }
73
f3(A * a,A & ar)74 void f3(A *a, A &ar) {
75 (a->*pa)();
76 (ar.*pa)();
77 }
78
f4()79 bool f4() {
80 return pa;
81 }
82
83 // PR5177
84 namespace PR5177 {
85 struct A {
86 bool foo(int*) const;
87 } a;
88
89 struct B1 {
90 bool (A::*pmf)(int*) const;
91 const A* pa;
92
B1PR5177::B193 B1() : pmf(&A::foo), pa(&a) {}
operator ()PR5177::B194 bool operator()() const { return (pa->*pmf)(new int); }
95 };
96
bar(B1 b2)97 void bar(B1 b2) { while (b2()) ; }
98 }
99
100 // PR5138
101 namespace PR5138 {
102 struct foo {
103 virtual void bar(foo *);
104 };
105
106 extern "C" {
107 void baz(foo *);
108 }
109
110 void (foo::*ptr1)(void *) = (void (foo::*)(void *))&foo::bar;
111 void (*ptr2)(void *) = (void (*)(void *))&baz;
112
113 void (foo::*ptr3)(void) = (void (foo::*)(void))&foo::bar;
114 }
115
116 // PR5593
117 namespace PR5593 {
118 struct A { };
119
f(void (A::* f)())120 bool f(void (A::*f)()) {
121 return f && f;
122 }
123 }
124
125 namespace PR5718 {
126 struct A { };
127
f(void (A::* f)(),void (A::* g)())128 bool f(void (A::*f)(), void (A::*g)()) {
129 return f == g;
130 }
131 }
132
133 namespace BoolMemberPointer {
134 struct A { };
135
f(void (A::* f)())136 bool f(void (A::*f)()) {
137 return !f;
138 }
139
g(void (A::* f)())140 bool g(void (A::*f)()) {
141 if (!!f)
142 return true;
143 return false;
144 }
145 }
146
147 // PR5940
148 namespace PR5940 {
149 class foo {
150 public:
151 virtual void baz(void);
152 };
153
baz(void)154 void foo::baz(void) {
155 void (foo::*ptr)(void) = &foo::baz;
156 }
157 }
158
159 namespace MemberPointerImpCast {
160 struct A {
161 int x;
162 };
163 struct B : public A {
164 };
f(B * obj,void (A::* method)())165 void f(B* obj, void (A::*method)()) {
166 (obj->*method)();
167 }
168 }
169
170 // PR6258
171 namespace PR6258 {
172
173 struct A {
174 void f(bool);
175 };
176
177 void (A::*pf)(bool) = &A::f;
178
f()179 void f() {
180 void (A::*pf)(bool) = &A::f;
181 }
182 }
183
184 // PR7027
185 namespace PR7027 {
186 struct X { void test( ); };
testX()187 void testX() { &X::test; }
188 }
189
190 namespace test7 {
191 struct A { void foo(); virtual void vfoo(); };
192 struct B { void foo(); virtual void vfoo(); };
193 struct C : A, B { void foo(); virtual void vfoo(); };
194
195 // GLOBAL-ARM: @_ZN5test74ptr0E = global {{.*}} { i32 ptrtoint ({{.*}}* @_ZN5test71A3fooEv to i32), i32 0 }
196 // GLOBAL-ARM: @_ZN5test74ptr1E = global {{.*}} { i32 ptrtoint ({{.*}}* @_ZN5test71B3fooEv to i32), i32 8 }
197 // GLOBAL-ARM: @_ZN5test74ptr2E = global {{.*}} { i32 ptrtoint ({{.*}}* @_ZN5test71C3fooEv to i32), i32 0 }
198 // GLOBAL-ARM: @_ZN5test74ptr3E = global {{.*}} { i32 0, i32 1 }
199 // GLOBAL-ARM: @_ZN5test74ptr4E = global {{.*}} { i32 0, i32 9 }
200 // GLOBAL-ARM: @_ZN5test74ptr5E = global {{.*}} { i32 0, i32 1 }
201 void (C::*ptr0)() = &A::foo;
202 void (C::*ptr1)() = &B::foo;
203 void (C::*ptr2)() = &C::foo;
204 void (C::*ptr3)() = &A::vfoo;
205 void (C::*ptr4)() = &B::vfoo;
206 void (C::*ptr5)() = &C::vfoo;
207 }
208
209 namespace test8 {
210 struct X { };
211 typedef int (X::*pmf)(int);
212
213 // CHECK: {{define.*_ZN5test81fEv}}
f()214 pmf f() {
215 // CHECK: {{ret.*zeroinitializer}}
216 return pmf();
217 }
218 }
219
220 namespace test9 {
221 struct A {
222 void foo();
223 };
224 struct B : A {
225 void foo();
226 };
227
228 typedef void (A::*fooptr)();
229
230 struct S {
231 fooptr p;
232 };
233
234 // CODE-LP64-LABEL: define void @_ZN5test94testEv(
235 // CODE-LP64: alloca i32
236 // CODE-LP64-NEXT: ret void
test()237 void test() {
238 int x;
239 static S array[] = { (fooptr) &B::foo };
240 }
241 }
242
243 // rdar://problem/10815683 - Verify that we can emit reinterprets of
244 // member pointers as constant initializers. For added trickiness,
245 // we also add some non-trivial adjustments.
246 namespace test10 {
247 struct A {
248 int nonEmpty;
249 void foo();
250 };
251 struct B : public A {
252 virtual void requireNonZeroAdjustment();
253 };
254 struct C {
255 int nonEmpty;
256 };
257 struct D : public C {
258 virtual void requireNonZeroAdjustment();
259 };
260
261
262 // It's not that the offsets are doubled on ARM, it's that they're left-shifted by 1.
263
264 // GLOBAL-LP64: @_ZN6test101aE = global { i64, i64 } { i64 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i64), i64 0 }, align 8
265 // GLOBAL-LP32: @_ZN6test101aE = global { i32, i32 } { i32 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i32), i32 0 }, align 4
266 // GLOBAL-ARM: @_ZN6test101aE = global { i32, i32 } { i32 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i32), i32 0 }, align 4
267 void (A::*a)() = &A::foo;
268
269 // GLOBAL-LP64: @_ZN6test101bE = global { i64, i64 } { i64 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i64), i64 8 }, align 8
270 // GLOBAL-LP32: @_ZN6test101bE = global { i32, i32 } { i32 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i32), i32 4 }, align 4
271 // GLOBAL-ARM: @_ZN6test101bE = global { i32, i32 } { i32 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i32), i32 8 }, align 4
272 void (B::*b)() = (void (B::*)()) &A::foo;
273
274 // GLOBAL-LP64: @_ZN6test101cE = global { i64, i64 } { i64 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i64), i64 8 }, align 8
275 // GLOBAL-LP32: @_ZN6test101cE = global { i32, i32 } { i32 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i32), i32 4 }, align 4
276 // GLOBAL-ARM: @_ZN6test101cE = global { i32, i32 } { i32 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i32), i32 8 }, align 4
277 void (C::*c)() = (void (C::*)()) (void (B::*)()) &A::foo;
278
279 // GLOBAL-LP64: @_ZN6test101dE = global { i64, i64 } { i64 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i64), i64 16 }, align 8
280 // GLOBAL-LP32: @_ZN6test101dE = global { i32, i32 } { i32 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i32), i32 8 }, align 4
281 // GLOBAL-ARM: @_ZN6test101dE = global { i32, i32 } { i32 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i32), i32 16 }, align 4
282 void (D::*d)() = (void (C::*)()) (void (B::*)()) &A::foo;
283 }
284
285 namespace test11 {
286 struct A { virtual void a(); };
287 struct B : A {};
288 struct C : B { virtual void a(); };
289 void (C::*x)() = &C::a;
290
291 // GLOBAL-LP64: @_ZN6test111xE = global { i64, i64 } { i64 1, i64 0 }
292 // GLOBAL-LP32: @_ZN6test111xE = global { i32, i32 } { i32 1, i32 0 }
293 // GLOBAL-ARM: @_ZN6test111xE = global { i32, i32 } { i32 0, i32 1 }
294 }
295