blob: 8ae57b2cdb6a89ba757147b855493c7e556a2ca8 [file] [log] [blame]
John McCall2a7b09d2012-05-01 17:32:20 +00001// 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
Anders Carlsson7b699562009-09-29 03:38:56 +00006
Mark Seaborn21fe4502013-07-24 16:25:13 +00007// 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
Stephen Hines0e2c34f2015-03-23 12:09:02 -07009// 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
Mark Seaborn21fe4502013-07-24 16:25:13 +000011
Anders Carlssone8a81f72010-02-04 16:38:05 +000012struct A { int a; void f(); virtual void vf1(); virtual void vf2(); };
Anders Carlssonf57b4e42009-10-03 15:02:02 +000013struct B { int b; virtual void g(); };
Anders Carlsson7b699562009-09-29 03:38:56 +000014struct C : B, A { };
15
16void (A::*pa)();
17void (A::*volatile vpa)();
18void (B::*pb)();
19void (C::*pc)();
20
John McCall2a7b09d2012-05-01 17:32:20 +000021// GLOBAL-LP64: @pa2 = global { i64, i64 } { i64 ptrtoint (void (%struct.A*)* @_ZN1A1fEv to i64), i64 0 }, align 8
Anders Carlssonf57b4e42009-10-03 15:02:02 +000022void (A::*pa2)() = &A::f;
23
John McCall2a7b09d2012-05-01 17:32:20 +000024// 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
Anders Carlssone8a81f72010-02-04 16:38:05 +000026void (A::*pa3)() = &A::vf1;
27
John McCall2a7b09d2012-05-01 17:32:20 +000028// 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
Anders Carlssone8a81f72010-02-04 16:38:05 +000030void (A::*pa4)() = &A::vf2;
Anders Carlssonf57b4e42009-10-03 15:02:02 +000031
John McCall2a7b09d2012-05-01 17:32:20 +000032// GLOBAL-LP64: @pc2 = global { i64, i64 } { i64 ptrtoint (void (%struct.A*)* @_ZN1A1fEv to i64), i64 16 }, align 8
Anders Carlsson2c51f092009-10-03 15:13:22 +000033void (C::*pc2)() = &C::f;
34
John McCall2a7b09d2012-05-01 17:32:20 +000035// GLOBAL-LP64: @pc3 = global { i64, i64 } { i64 1, i64 0 }, align 8
Anders Carlssone8a81f72010-02-04 16:38:05 +000036void (A::*pc3)() = &A::vf1;
Anders Carlsson2c51f092009-10-03 15:13:22 +000037
Anders Carlsson7b699562009-09-29 03:38:56 +000038void f() {
John McCall2a7b09d2012-05-01 17:32:20 +000039 // CODE-LP64: store { i64, i64 } zeroinitializer, { i64, i64 }* @pa
Anders Carlsson7b699562009-09-29 03:38:56 +000040 pa = 0;
Anders Carlssonee383162009-10-03 14:39:13 +000041
John McCalld608cdb2010-08-22 10:59:02 +000042 // Is this okay? What are LLVM's volatile semantics for structs?
John McCall2a7b09d2012-05-01 17:32:20 +000043 // CODE-LP64: store volatile { i64, i64 } zeroinitializer, { i64, i64 }* @vpa
Anders Carlsson7b699562009-09-29 03:38:56 +000044 vpa = 0;
Anders Carlssonee383162009-10-03 14:39:13 +000045
John McCall2a7b09d2012-05-01 17:32:20 +000046 // 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
Anders Carlssonf57b4e42009-10-03 15:02:02 +000051 pc = pa;
Eli Friedman9b372742009-11-27 04:56:40 +000052
John McCall2a7b09d2012-05-01 17:32:20 +000053 // 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
Eli Friedman9b372742009-11-27 04:56:40 +000058 pa = static_cast<void (A::*)()>(pc);
Anders Carlsson7b699562009-09-29 03:38:56 +000059}
Anders Carlssona024d172009-10-03 15:43:24 +000060
61void f2() {
John McCall2a7b09d2012-05-01 17:32:20 +000062 // CODE-LP64: store { i64, i64 } { i64 ptrtoint (void (%struct.A*)* @_ZN1A1fEv to i64), i64 0 }
Anders Carlssona024d172009-10-03 15:43:24 +000063 void (A::*pa2)() = &A::f;
64
John McCall2a7b09d2012-05-01 17:32:20 +000065 // CODE-LP64: store { i64, i64 } { i64 1, i64 0 }
66 // CODE-LP32: store { i32, i32 } { i32 1, i32 0 }
Anders Carlssone8a81f72010-02-04 16:38:05 +000067 void (A::*pa3)() = &A::vf1;
68
John McCall2a7b09d2012-05-01 17:32:20 +000069 // CODE-LP64: store { i64, i64 } { i64 9, i64 0 }
70 // CODE-LP32: store { i32, i32 } { i32 5, i32 0 }
Anders Carlssone8a81f72010-02-04 16:38:05 +000071 void (A::*pa4)() = &A::vf2;
Anders Carlssona024d172009-10-03 15:43:24 +000072}
Anders Carlsson375c31c2009-10-03 19:43:08 +000073
74void f3(A *a, A &ar) {
75 (a->*pa)();
76 (ar.*pa)();
77}
Anders Carlsson3eea6352009-10-13 17:41:28 +000078
Anders Carlssonbc0e0782009-11-23 20:04:44 +000079bool f4() {
80 return pa;
81}
82
Anders Carlsson3eea6352009-10-13 17:41:28 +000083// PR5177
84namespace 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
93 B1() : pmf(&A::foo), pa(&a) {}
94 bool operator()() const { return (pa->*pmf)(new int); }
95 };
96
97 void bar(B1 b2) { while (b2()) ; }
98}
Anders Carlssonbb378cb2009-10-18 20:31:03 +000099
100// PR5138
101namespace 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}
Anders Carlssona4c98cd2009-11-23 21:47:44 +0000115
116// PR5593
117namespace PR5593 {
118 struct A { };
119
120 bool f(void (A::*f)()) {
121 return f && f;
122 }
123}
Eli Friedmanb81c7862009-12-11 07:36:43 +0000124
125namespace PR5718 {
126 struct A { };
127
128 bool f(void (A::*f)(), void (A::*g)()) {
129 return f == g;
130 }
131}
Eli Friedman3a173702009-12-11 09:26:29 +0000132
133namespace BoolMemberPointer {
134 struct A { };
135
136 bool f(void (A::*f)()) {
137 return !f;
138 }
139
140 bool g(void (A::*f)()) {
141 if (!!f)
142 return true;
143 return false;
144 }
145}
146
Anders Carlsson7af4ec72010-01-05 05:04:05 +0000147// PR5940
148namespace PR5940 {
149 class foo {
150 public:
151 virtual void baz(void);
152 };
153
154 void foo::baz(void) {
155 void (foo::*ptr)(void) = &foo::baz;
156 }
157}
Eli Friedman3005efe2010-01-16 00:00:48 +0000158
159namespace MemberPointerImpCast {
160 struct A {
161 int x;
162 };
163 struct B : public A {
164 };
165 void f(B* obj, void (A::*method)()) {
166 (obj->*method)();
167 }
168}
Anders Carlsson585fa682010-02-07 17:37:13 +0000169
170// PR6258
171namespace PR6258 {
172
173 struct A {
174 void f(bool);
175 };
176
177 void (A::*pf)(bool) = &A::f;
178
179 void f() {
180 void (A::*pf)(bool) = &A::f;
181 }
182}
Douglas Gregore174bd02010-05-03 20:00:27 +0000183
184// PR7027
185namespace PR7027 {
186 struct X { void test( ); };
187 void testX() { &X::test; }
188}
John McCall875ab102010-08-22 06:43:33 +0000189
190namespace 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
John McCall2a7b09d2012-05-01 17:32:20 +0000195 // 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 }
John McCall875ab102010-08-22 06:43:33 +0000201 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}
Douglas Gregordb68e282010-08-22 18:26:35 +0000208
209namespace test8 {
210 struct X { };
211 typedef int (X::*pmf)(int);
212
213 // CHECK: {{define.*_ZN5test81fEv}}
214 pmf f() {
215 // CHECK: {{ret.*zeroinitializer}}
216 return pmf();
217 }
218}
John McCall8bba1f42011-03-15 21:17:48 +0000219
220namespace 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
Stephen Lin93ab6bf2013-08-15 06:47:53 +0000234 // CODE-LP64-LABEL: define void @_ZN5test94testEv(
John McCall2a7b09d2012-05-01 17:32:20 +0000235 // CODE-LP64: alloca i32
236 // CODE-LP64-NEXT: ret void
John McCall8bba1f42011-03-15 21:17:48 +0000237 void test() {
238 int x;
239 static S array[] = { (fooptr) &B::foo };
240 }
241}
John McCall4d4e5c12012-02-15 01:22:51 +0000242
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.
246namespace 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
John McCall2a7b09d2012-05-01 17:32:20 +0000261
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
John McCall4d4e5c12012-02-15 01:22:51 +0000267 void (A::*a)() = &A::foo;
John McCall2a7b09d2012-05-01 17:32:20 +0000268
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
John McCall4d4e5c12012-02-15 01:22:51 +0000272 void (B::*b)() = (void (B::*)()) &A::foo;
John McCall2a7b09d2012-05-01 17:32:20 +0000273
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
John McCall4d4e5c12012-02-15 01:22:51 +0000277 void (C::*c)() = (void (C::*)()) (void (B::*)()) &A::foo;
John McCall2a7b09d2012-05-01 17:32:20 +0000278
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
John McCall4d4e5c12012-02-15 01:22:51 +0000282 void (D::*d)() = (void (C::*)()) (void (B::*)()) &A::foo;
283}
John McCall2a7b09d2012-05-01 17:32:20 +0000284
285namespace 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}