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
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
12 struct A { int a; void f(); virtual void vf1(); virtual void vf2(); };
13 struct B { int b; virtual void g(); };
17 void (A::*volatile vpa)();
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;
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;
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;
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;
35 // GLOBAL-LP64: @pc3 = global { i64, i64 } { i64 1, i64 0 }, align 8
36 void (A::*pc3)() = &A::vf1;
39 // CODE-LP64: store { i64, i64 } zeroinitializer, { i64, i64 }* @pa
42 // Is this okay? What are LLVM's volatile semantics for structs?
43 // CODE-LP64: store volatile { i64, i64 } zeroinitializer, { i64, i64 }* @vpa
46 // CODE-LP64: [[TMP:%.*]] = load { 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
53 // CODE-LP64: [[TMP:%.*]] = load { 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);
62 // CODE-LP64: store { i64, i64 } { i64 ptrtoint (void (%struct.A*)* @_ZN1A1fEv to i64), i64 0 }
63 void (A::*pa2)() = &A::f;
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;
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;
74 void f3(A *a, A &ar) {
90 bool (A::*pmf)(int*) const;
93 B1() : pmf(&A::foo), pa(&a) {}
94 bool operator()() const { return (pa->*pmf)(new int); }
97 void bar(B1 b2) { while (b2()) ; }
103 virtual void bar(foo *);
110 void (foo::*ptr1)(void *) = (void (foo::*)(void *))&foo::bar;
111 void (*ptr2)(void *) = (void (*)(void *))&baz;
113 void (foo::*ptr3)(void) = (void (foo::*)(void))&foo::bar;
120 bool f(void (A::*f)()) {
128 bool f(void (A::*f)(), void (A::*g)()) {
133 namespace BoolMemberPointer {
136 bool f(void (A::*f)()) {
140 bool g(void (A::*f)()) {
151 virtual void baz(void);
154 void foo::baz(void) {
155 void (foo::*ptr)(void) = &foo::baz;
159 namespace MemberPointerImpCast {
163 struct B : public A {
165 void f(B* obj, void (A::*method)()) {
177 void (A::*pf)(bool) = &A::f;
180 void (A::*pf)(bool) = &A::f;
186 struct X { void test( ); };
187 void testX() { &X::test; }
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(); };
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;
211 typedef int (X::*pmf)(int);
213 // CHECK: {{define.*_ZN5test81fEv}}
215 // CHECK: {{ret.*zeroinitializer}}
228 typedef void (A::*fooptr)();
234 // CODE-LP64-LABEL: define void @_ZN5test94testEv(
235 // CODE-LP64: alloca i32
236 // CODE-LP64-NEXT: ret void
239 static S array[] = { (fooptr) &B::foo };
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.
251 struct B : public A {
252 virtual void requireNonZeroAdjustment();
257 struct D : public C {
258 virtual void requireNonZeroAdjustment();
262 // It's not that the offsets are doubled on ARM, it's that they're left-shifted by 1.
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;
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;
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;
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;
286 struct A { virtual void a(); };
288 struct C : B { virtual void a(); };
289 void (C::*x)() = &C::a;
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 }