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