[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / CodeGenCXX / member-function-pointers.cpp
blobf74263cd462157e0ea8ee33a215e03ca0285081f
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 // MIPS uses the same representation of method pointers as ARM.
8 // RUN: %clang_cc1 %s -emit-llvm -o - -triple=mips-unknown-linux-gnu | FileCheck -check-prefix GLOBAL-ARM %s
9 // WebAssembly uses the same representation of method pointers as ARM.
10 // RUN: %clang_cc1 %s -emit-llvm -o - -triple=wasm32-unknown-unknown | 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(); };
14 struct C : B, A { };
16 void (A::*pa)();
17 void (A::*volatile vpa)();
18 void (B::*pb)();
19 void (C::*pc)();
21 // GLOBAL-LP64: @pa2 ={{.*}} global { i64, i64 } { i64 ptrtoint (ptr @_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 (ptr @_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;
38 void f() {
39 // CODE-LP64: store { i64, i64 } zeroinitializer, ptr @pa
40 pa = 0;
42 // Is this okay? What are LLVM's volatile semantics for structs?
43 // CODE-LP64: store volatile { i64, i64 } zeroinitializer, ptr @vpa
44 vpa = 0;
46 // CODE-LP64: [[TMP:%.*]] = load { i64, i64 }, ptr @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]], ptr @pc, align 8
51 pc = pa;
53 // CODE-LP64: [[TMP:%.*]] = load { i64, i64 }, ptr @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]], ptr @pa, align 8
58 pa = static_cast<void (A::*)()>(pc);
61 void f2() {
62 // CODE-LP64: store { i64, i64 } { i64 ptrtoint (ptr @_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) {
75 (a->*pa)();
76 (ar.*pa)();
79 bool f4() {
80 return pa;
83 // PR5177
84 namespace PR5177 {
85 struct A {
86 bool foo(int*) const;
87 } a;
89 struct B1 {
90 bool (A::*pmf)(int*) const;
91 const A* pa;
93 B1() : pmf(&A::foo), pa(&a) {}
94 bool operator()() const { return (pa->*pmf)(new int); }
97 void bar(B1 b2) { while (b2()) ; }
100 // PR5138
101 namespace PR5138 {
102 struct foo {
103 virtual void bar(foo *);
106 extern "C" {
107 void baz(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;
116 // PR5593
117 namespace PR5593 {
118 struct A { };
120 bool f(void (A::*f)()) {
121 return f && f;
125 namespace PR5718 {
126 struct A { };
128 bool f(void (A::*f)(), void (A::*g)()) {
129 return f == g;
133 namespace BoolMemberPointer {
134 struct A { };
136 bool f(void (A::*f)()) {
137 return !f;
140 bool g(void (A::*f)()) {
141 if (!!f)
142 return true;
143 return false;
147 // PR5940
148 namespace PR5940 {
149 class foo {
150 public:
151 virtual void baz(void);
154 void foo::baz(void) {
155 void (foo::*ptr)(void) = &foo::baz;
159 namespace MemberPointerImpCast {
160 struct A {
161 int x;
163 struct B : public A {
165 void f(B* obj, void (A::*method)()) {
166 (obj->*method)();
170 // PR6258
171 namespace PR6258 {
173 struct A {
174 void f(bool);
177 void (A::*pf)(bool) = &A::f;
179 void f() {
180 void (A::*pf)(bool) = &A::f;
184 // PR7027
185 namespace PR7027 {
186 struct X { void test( ); };
187 void testX() { &X::test; }
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(); };
195 // GLOBAL-ARM: @_ZN5test74ptr0E ={{.*}} global {{.*}} { i32 ptrtoint (ptr @_ZN5test71A3fooEv to i32), i32 0 }
196 // GLOBAL-ARM: @_ZN5test74ptr1E ={{.*}} global {{.*}} { i32 ptrtoint (ptr @_ZN5test71B3fooEv to i32), i32 8 }
197 // GLOBAL-ARM: @_ZN5test74ptr2E ={{.*}} global {{.*}} { i32 ptrtoint (ptr @_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;
209 namespace test8 {
210 struct X { };
211 typedef int (X::*pmf)(int);
213 // CHECK: {{define.*_ZN5test81fEv}}
214 pmf f() {
215 // CHECK: {{ret.*zeroinitializer}}
216 return pmf();
220 namespace test9 {
221 struct A {
222 void foo();
224 struct B : A {
225 void foo();
228 typedef void (A::*fooptr)();
230 struct S {
231 fooptr p;
234 // CODE-LP64-LABEL: define{{.*}} void @_ZN5test94testEv(
235 // CODE-LP64: alloca i32
236 // CODE-LP64-NEXT: ret void
237 void test() {
238 int x;
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.
246 namespace test10 {
247 struct A {
248 int nonEmpty;
249 void foo();
251 struct B : public A {
252 virtual void requireNonZeroAdjustment();
254 struct C {
255 int nonEmpty;
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 (ptr @_ZN6test101A3fooEv to i64), i64 0 }, align 8
265 // GLOBAL-LP32: @_ZN6test101aE ={{.*}} global { i32, i32 } { i32 ptrtoint (ptr @_ZN6test101A3fooEv to i32), i32 0 }, align 4
266 // GLOBAL-ARM: @_ZN6test101aE ={{.*}} global { i32, i32 } { i32 ptrtoint (ptr @_ZN6test101A3fooEv to i32), i32 0 }, align 4
267 void (A::*a)() = &A::foo;
269 // GLOBAL-LP64: @_ZN6test101bE ={{.*}} global { i64, i64 } { i64 ptrtoint (ptr @_ZN6test101A3fooEv to i64), i64 8 }, align 8
270 // GLOBAL-LP32: @_ZN6test101bE ={{.*}} global { i32, i32 } { i32 ptrtoint (ptr @_ZN6test101A3fooEv to i32), i32 4 }, align 4
271 // GLOBAL-ARM: @_ZN6test101bE ={{.*}} global { i32, i32 } { i32 ptrtoint (ptr @_ZN6test101A3fooEv to i32), i32 8 }, align 4
272 void (B::*b)() = (void (B::*)()) &A::foo;
274 // GLOBAL-LP64: @_ZN6test101cE ={{.*}} global { i64, i64 } { i64 ptrtoint (ptr @_ZN6test101A3fooEv to i64), i64 8 }, align 8
275 // GLOBAL-LP32: @_ZN6test101cE ={{.*}} global { i32, i32 } { i32 ptrtoint (ptr @_ZN6test101A3fooEv to i32), i32 4 }, align 4
276 // GLOBAL-ARM: @_ZN6test101cE ={{.*}} global { i32, i32 } { i32 ptrtoint (ptr @_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 (ptr @_ZN6test101A3fooEv to i64), i64 16 }, align 8
280 // GLOBAL-LP32: @_ZN6test101dE ={{.*}} global { i32, i32 } { i32 ptrtoint (ptr @_ZN6test101A3fooEv to i32), i32 8 }, align 4
281 // GLOBAL-ARM: @_ZN6test101dE ={{.*}} global { i32, i32 } { i32 ptrtoint (ptr @_ZN6test101A3fooEv to i32), i32 16 }, align 4
282 void (D::*d)() = (void (C::*)()) (void (B::*)()) &A::foo;
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;
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 }