blob: 23a7b0009f1fe3dbb398a83a54558ddca757eb23 [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
9
Anders Carlssone8a81f72010-02-04 16:38:05 +000010struct A { int a; void f(); virtual void vf1(); virtual void vf2(); };
Anders Carlssonf57b4e42009-10-03 15:02:02 +000011struct B { int b; virtual void g(); };
Anders Carlsson7b699562009-09-29 03:38:56 +000012struct C : B, A { };
13
14void (A::*pa)();
15void (A::*volatile vpa)();
16void (B::*pb)();
17void (C::*pc)();
18
John McCall2a7b09d2012-05-01 17:32:20 +000019// 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 +000020void (A::*pa2)() = &A::f;
21
John McCall2a7b09d2012-05-01 17:32:20 +000022// GLOBAL-LP64: @pa3 = global { i64, i64 } { i64 1, i64 0 }, align 8
23// GLOBAL-LP32: @pa3 = global { i32, i32 } { i32 1, i32 0 }, align 4
Anders Carlssone8a81f72010-02-04 16:38:05 +000024void (A::*pa3)() = &A::vf1;
25
John McCall2a7b09d2012-05-01 17:32:20 +000026// GLOBAL-LP64: @pa4 = global { i64, i64 } { i64 9, i64 0 }, align 8
27// GLOBAL-LP32: @pa4 = global { i32, i32 } { i32 5, i32 0 }, align 4
Anders Carlssone8a81f72010-02-04 16:38:05 +000028void (A::*pa4)() = &A::vf2;
Anders Carlssonf57b4e42009-10-03 15:02:02 +000029
John McCall2a7b09d2012-05-01 17:32:20 +000030// 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 +000031void (C::*pc2)() = &C::f;
32
John McCall2a7b09d2012-05-01 17:32:20 +000033// GLOBAL-LP64: @pc3 = global { i64, i64 } { i64 1, i64 0 }, align 8
Anders Carlssone8a81f72010-02-04 16:38:05 +000034void (A::*pc3)() = &A::vf1;
Anders Carlsson2c51f092009-10-03 15:13:22 +000035
Anders Carlsson7b699562009-09-29 03:38:56 +000036void f() {
John McCall2a7b09d2012-05-01 17:32:20 +000037 // CODE-LP64: store { i64, i64 } zeroinitializer, { i64, i64 }* @pa
Anders Carlsson7b699562009-09-29 03:38:56 +000038 pa = 0;
Anders Carlssonee383162009-10-03 14:39:13 +000039
John McCalld608cdb2010-08-22 10:59:02 +000040 // Is this okay? What are LLVM's volatile semantics for structs?
John McCall2a7b09d2012-05-01 17:32:20 +000041 // CODE-LP64: store volatile { i64, i64 } zeroinitializer, { i64, i64 }* @vpa
Anders Carlsson7b699562009-09-29 03:38:56 +000042 vpa = 0;
Anders Carlssonee383162009-10-03 14:39:13 +000043
John McCall2a7b09d2012-05-01 17:32:20 +000044 // CODE-LP64: [[TMP:%.*]] = load { i64, i64 }* @pa, align 8
45 // CODE-LP64: [[TMPADJ:%.*]] = extractvalue { i64, i64 } [[TMP]], 1
46 // CODE-LP64: [[ADJ:%.*]] = add nsw i64 [[TMPADJ]], 16
47 // CODE-LP64: [[RES:%.*]] = insertvalue { i64, i64 } [[TMP]], i64 [[ADJ]], 1
48 // CODE-LP64: store { i64, i64 } [[RES]], { i64, i64 }* @pc, align 8
Anders Carlssonf57b4e42009-10-03 15:02:02 +000049 pc = pa;
Eli Friedman9b372742009-11-27 04:56:40 +000050
John McCall2a7b09d2012-05-01 17:32:20 +000051 // CODE-LP64: [[TMP:%.*]] = load { i64, i64 }* @pc, align 8
52 // CODE-LP64: [[TMPADJ:%.*]] = extractvalue { i64, i64 } [[TMP]], 1
53 // CODE-LP64: [[ADJ:%.*]] = sub nsw i64 [[TMPADJ]], 16
54 // CODE-LP64: [[RES:%.*]] = insertvalue { i64, i64 } [[TMP]], i64 [[ADJ]], 1
55 // CODE-LP64: store { i64, i64 } [[RES]], { i64, i64 }* @pa, align 8
Eli Friedman9b372742009-11-27 04:56:40 +000056 pa = static_cast<void (A::*)()>(pc);
Anders Carlsson7b699562009-09-29 03:38:56 +000057}
Anders Carlssona024d172009-10-03 15:43:24 +000058
59void f2() {
John McCall2a7b09d2012-05-01 17:32:20 +000060 // CODE-LP64: store { i64, i64 } { i64 ptrtoint (void (%struct.A*)* @_ZN1A1fEv to i64), i64 0 }
Anders Carlssona024d172009-10-03 15:43:24 +000061 void (A::*pa2)() = &A::f;
62
John McCall2a7b09d2012-05-01 17:32:20 +000063 // CODE-LP64: store { i64, i64 } { i64 1, i64 0 }
64 // CODE-LP32: store { i32, i32 } { i32 1, i32 0 }
Anders Carlssone8a81f72010-02-04 16:38:05 +000065 void (A::*pa3)() = &A::vf1;
66
John McCall2a7b09d2012-05-01 17:32:20 +000067 // CODE-LP64: store { i64, i64 } { i64 9, i64 0 }
68 // CODE-LP32: store { i32, i32 } { i32 5, i32 0 }
Anders Carlssone8a81f72010-02-04 16:38:05 +000069 void (A::*pa4)() = &A::vf2;
Anders Carlssona024d172009-10-03 15:43:24 +000070}
Anders Carlsson375c31c2009-10-03 19:43:08 +000071
72void f3(A *a, A &ar) {
73 (a->*pa)();
74 (ar.*pa)();
75}
Anders Carlsson3eea6352009-10-13 17:41:28 +000076
Anders Carlssonbc0e0782009-11-23 20:04:44 +000077bool f4() {
78 return pa;
79}
80
Anders Carlsson3eea6352009-10-13 17:41:28 +000081// PR5177
82namespace PR5177 {
83 struct A {
84 bool foo(int*) const;
85 } a;
86
87 struct B1 {
88 bool (A::*pmf)(int*) const;
89 const A* pa;
90
91 B1() : pmf(&A::foo), pa(&a) {}
92 bool operator()() const { return (pa->*pmf)(new int); }
93 };
94
95 void bar(B1 b2) { while (b2()) ; }
96}
Anders Carlssonbb378cb2009-10-18 20:31:03 +000097
98// PR5138
99namespace PR5138 {
100 struct foo {
101 virtual void bar(foo *);
102 };
103
104 extern "C" {
105 void baz(foo *);
106 }
107
108 void (foo::*ptr1)(void *) = (void (foo::*)(void *))&foo::bar;
109 void (*ptr2)(void *) = (void (*)(void *))&baz;
110
111 void (foo::*ptr3)(void) = (void (foo::*)(void))&foo::bar;
112}
Anders Carlssona4c98cd2009-11-23 21:47:44 +0000113
114// PR5593
115namespace PR5593 {
116 struct A { };
117
118 bool f(void (A::*f)()) {
119 return f && f;
120 }
121}
Eli Friedmanb81c7862009-12-11 07:36:43 +0000122
123namespace PR5718 {
124 struct A { };
125
126 bool f(void (A::*f)(), void (A::*g)()) {
127 return f == g;
128 }
129}
Eli Friedman3a173702009-12-11 09:26:29 +0000130
131namespace BoolMemberPointer {
132 struct A { };
133
134 bool f(void (A::*f)()) {
135 return !f;
136 }
137
138 bool g(void (A::*f)()) {
139 if (!!f)
140 return true;
141 return false;
142 }
143}
144
Anders Carlsson7af4ec72010-01-05 05:04:05 +0000145// PR5940
146namespace PR5940 {
147 class foo {
148 public:
149 virtual void baz(void);
150 };
151
152 void foo::baz(void) {
153 void (foo::*ptr)(void) = &foo::baz;
154 }
155}
Eli Friedman3005efe2010-01-16 00:00:48 +0000156
157namespace MemberPointerImpCast {
158 struct A {
159 int x;
160 };
161 struct B : public A {
162 };
163 void f(B* obj, void (A::*method)()) {
164 (obj->*method)();
165 }
166}
Anders Carlsson585fa682010-02-07 17:37:13 +0000167
168// PR6258
169namespace PR6258 {
170
171 struct A {
172 void f(bool);
173 };
174
175 void (A::*pf)(bool) = &A::f;
176
177 void f() {
178 void (A::*pf)(bool) = &A::f;
179 }
180}
Douglas Gregore174bd02010-05-03 20:00:27 +0000181
182// PR7027
183namespace PR7027 {
184 struct X { void test( ); };
185 void testX() { &X::test; }
186}
John McCall875ab102010-08-22 06:43:33 +0000187
188namespace test7 {
189 struct A { void foo(); virtual void vfoo(); };
190 struct B { void foo(); virtual void vfoo(); };
191 struct C : A, B { void foo(); virtual void vfoo(); };
192
John McCall2a7b09d2012-05-01 17:32:20 +0000193 // GLOBAL-ARM: @_ZN5test74ptr0E = global {{.*}} { i32 ptrtoint ({{.*}}* @_ZN5test71A3fooEv to i32), i32 0 }
194 // GLOBAL-ARM: @_ZN5test74ptr1E = global {{.*}} { i32 ptrtoint ({{.*}}* @_ZN5test71B3fooEv to i32), i32 8 }
195 // GLOBAL-ARM: @_ZN5test74ptr2E = global {{.*}} { i32 ptrtoint ({{.*}}* @_ZN5test71C3fooEv to i32), i32 0 }
196 // GLOBAL-ARM: @_ZN5test74ptr3E = global {{.*}} { i32 0, i32 1 }
197 // GLOBAL-ARM: @_ZN5test74ptr4E = global {{.*}} { i32 0, i32 9 }
198 // GLOBAL-ARM: @_ZN5test74ptr5E = global {{.*}} { i32 0, i32 1 }
John McCall875ab102010-08-22 06:43:33 +0000199 void (C::*ptr0)() = &A::foo;
200 void (C::*ptr1)() = &B::foo;
201 void (C::*ptr2)() = &C::foo;
202 void (C::*ptr3)() = &A::vfoo;
203 void (C::*ptr4)() = &B::vfoo;
204 void (C::*ptr5)() = &C::vfoo;
205}
Douglas Gregordb68e282010-08-22 18:26:35 +0000206
207namespace test8 {
208 struct X { };
209 typedef int (X::*pmf)(int);
210
211 // CHECK: {{define.*_ZN5test81fEv}}
212 pmf f() {
213 // CHECK: {{ret.*zeroinitializer}}
214 return pmf();
215 }
216}
John McCall8bba1f42011-03-15 21:17:48 +0000217
218namespace test9 {
219 struct A {
220 void foo();
221 };
222 struct B : A {
223 void foo();
224 };
225
226 typedef void (A::*fooptr)();
227
228 struct S {
229 fooptr p;
230 };
231
John McCall2a7b09d2012-05-01 17:32:20 +0000232 // CODE-LP64: define void @_ZN5test94testEv(
233 // CODE-LP64: alloca i32
234 // CODE-LP64-NEXT: ret void
John McCall8bba1f42011-03-15 21:17:48 +0000235 void test() {
236 int x;
237 static S array[] = { (fooptr) &B::foo };
238 }
239}
John McCall4d4e5c12012-02-15 01:22:51 +0000240
241// rdar://problem/10815683 - Verify that we can emit reinterprets of
242// member pointers as constant initializers. For added trickiness,
243// we also add some non-trivial adjustments.
244namespace test10 {
245 struct A {
246 int nonEmpty;
247 void foo();
248 };
249 struct B : public A {
250 virtual void requireNonZeroAdjustment();
251 };
252 struct C {
253 int nonEmpty;
254 };
255 struct D : public C {
256 virtual void requireNonZeroAdjustment();
257 };
258
John McCall2a7b09d2012-05-01 17:32:20 +0000259
260// It's not that the offsets are doubled on ARM, it's that they're left-shifted by 1.
261
262// GLOBAL-LP64: @_ZN6test101aE = global { i64, i64 } { i64 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i64), i64 0 }, align 8
263// GLOBAL-LP32: @_ZN6test101aE = global { i32, i32 } { i32 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i32), i32 0 }, align 4
264// 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 +0000265 void (A::*a)() = &A::foo;
John McCall2a7b09d2012-05-01 17:32:20 +0000266
267// GLOBAL-LP64: @_ZN6test101bE = global { i64, i64 } { i64 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i64), i64 8 }, align 8
268// GLOBAL-LP32: @_ZN6test101bE = global { i32, i32 } { i32 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i32), i32 4 }, align 4
269// 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 +0000270 void (B::*b)() = (void (B::*)()) &A::foo;
John McCall2a7b09d2012-05-01 17:32:20 +0000271
272// GLOBAL-LP64: @_ZN6test101cE = global { i64, i64 } { i64 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i64), i64 8 }, align 8
273// GLOBAL-LP32: @_ZN6test101cE = global { i32, i32 } { i32 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i32), i32 4 }, align 4
274// 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 +0000275 void (C::*c)() = (void (C::*)()) (void (B::*)()) &A::foo;
John McCall2a7b09d2012-05-01 17:32:20 +0000276
277// GLOBAL-LP64: @_ZN6test101dE = global { i64, i64 } { i64 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i64), i64 16 }, align 8
278// GLOBAL-LP32: @_ZN6test101dE = global { i32, i32 } { i32 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i32), i32 8 }, align 4
279// 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 +0000280 void (D::*d)() = (void (C::*)()) (void (B::*)()) &A::foo;
281}
John McCall2a7b09d2012-05-01 17:32:20 +0000282
283namespace test11 {
284 struct A { virtual void a(); };
285 struct B : A {};
286 struct C : B { virtual void a(); };
287 void (C::*x)() = &C::a;
288
289 // GLOBAL-LP64: @_ZN6test111xE = global { i64, i64 } { i64 1, i64 0 }
290 // GLOBAL-LP32: @_ZN6test111xE = global { i32, i32 } { i32 1, i32 0 }
291 // GLOBAL-ARM: @_ZN6test111xE = global { i32, i32 } { i32 0, i32 1 }
292}