blob: 84b54b67d315d8aa0188649f38fe90a203c83e05 [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
Anders Carlssone8a81f72010-02-04 16:38:05 +00007struct A { int a; void f(); virtual void vf1(); virtual void vf2(); };
Anders Carlssonf57b4e42009-10-03 15:02:02 +00008struct B { int b; virtual void g(); };
Anders Carlsson7b699562009-09-29 03:38:56 +00009struct C : B, A { };
10
11void (A::*pa)();
12void (A::*volatile vpa)();
13void (B::*pb)();
14void (C::*pc)();
15
John McCall2a7b09d2012-05-01 17:32:20 +000016// 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 +000017void (A::*pa2)() = &A::f;
18
John McCall2a7b09d2012-05-01 17:32:20 +000019// GLOBAL-LP64: @pa3 = global { i64, i64 } { i64 1, i64 0 }, align 8
20// GLOBAL-LP32: @pa3 = global { i32, i32 } { i32 1, i32 0 }, align 4
Anders Carlssone8a81f72010-02-04 16:38:05 +000021void (A::*pa3)() = &A::vf1;
22
John McCall2a7b09d2012-05-01 17:32:20 +000023// GLOBAL-LP64: @pa4 = global { i64, i64 } { i64 9, i64 0 }, align 8
24// GLOBAL-LP32: @pa4 = global { i32, i32 } { i32 5, i32 0 }, align 4
Anders Carlssone8a81f72010-02-04 16:38:05 +000025void (A::*pa4)() = &A::vf2;
Anders Carlssonf57b4e42009-10-03 15:02:02 +000026
John McCall2a7b09d2012-05-01 17:32:20 +000027// 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 +000028void (C::*pc2)() = &C::f;
29
John McCall2a7b09d2012-05-01 17:32:20 +000030// GLOBAL-LP64: @pc3 = global { i64, i64 } { i64 1, i64 0 }, align 8
Anders Carlssone8a81f72010-02-04 16:38:05 +000031void (A::*pc3)() = &A::vf1;
Anders Carlsson2c51f092009-10-03 15:13:22 +000032
Anders Carlsson7b699562009-09-29 03:38:56 +000033void f() {
John McCall2a7b09d2012-05-01 17:32:20 +000034 // CODE-LP64: store { i64, i64 } zeroinitializer, { i64, i64 }* @pa
Anders Carlsson7b699562009-09-29 03:38:56 +000035 pa = 0;
Anders Carlssonee383162009-10-03 14:39:13 +000036
John McCalld608cdb2010-08-22 10:59:02 +000037 // Is this okay? What are LLVM's volatile semantics for structs?
John McCall2a7b09d2012-05-01 17:32:20 +000038 // CODE-LP64: store volatile { i64, i64 } zeroinitializer, { i64, i64 }* @vpa
Anders Carlsson7b699562009-09-29 03:38:56 +000039 vpa = 0;
Anders Carlssonee383162009-10-03 14:39:13 +000040
John McCall2a7b09d2012-05-01 17:32:20 +000041 // CODE-LP64: [[TMP:%.*]] = load { i64, i64 }* @pa, align 8
42 // CODE-LP64: [[TMPADJ:%.*]] = extractvalue { i64, i64 } [[TMP]], 1
43 // CODE-LP64: [[ADJ:%.*]] = add nsw i64 [[TMPADJ]], 16
44 // CODE-LP64: [[RES:%.*]] = insertvalue { i64, i64 } [[TMP]], i64 [[ADJ]], 1
45 // CODE-LP64: store { i64, i64 } [[RES]], { i64, i64 }* @pc, align 8
Anders Carlssonf57b4e42009-10-03 15:02:02 +000046 pc = pa;
Eli Friedman9b372742009-11-27 04:56:40 +000047
John McCall2a7b09d2012-05-01 17:32:20 +000048 // CODE-LP64: [[TMP:%.*]] = load { i64, i64 }* @pc, align 8
49 // CODE-LP64: [[TMPADJ:%.*]] = extractvalue { i64, i64 } [[TMP]], 1
50 // CODE-LP64: [[ADJ:%.*]] = sub nsw i64 [[TMPADJ]], 16
51 // CODE-LP64: [[RES:%.*]] = insertvalue { i64, i64 } [[TMP]], i64 [[ADJ]], 1
52 // CODE-LP64: store { i64, i64 } [[RES]], { i64, i64 }* @pa, align 8
Eli Friedman9b372742009-11-27 04:56:40 +000053 pa = static_cast<void (A::*)()>(pc);
Anders Carlsson7b699562009-09-29 03:38:56 +000054}
Anders Carlssona024d172009-10-03 15:43:24 +000055
56void f2() {
John McCall2a7b09d2012-05-01 17:32:20 +000057 // CODE-LP64: store { i64, i64 } { i64 ptrtoint (void (%struct.A*)* @_ZN1A1fEv to i64), i64 0 }
Anders Carlssona024d172009-10-03 15:43:24 +000058 void (A::*pa2)() = &A::f;
59
John McCall2a7b09d2012-05-01 17:32:20 +000060 // CODE-LP64: store { i64, i64 } { i64 1, i64 0 }
61 // CODE-LP32: store { i32, i32 } { i32 1, i32 0 }
Anders Carlssone8a81f72010-02-04 16:38:05 +000062 void (A::*pa3)() = &A::vf1;
63
John McCall2a7b09d2012-05-01 17:32:20 +000064 // CODE-LP64: store { i64, i64 } { i64 9, i64 0 }
65 // CODE-LP32: store { i32, i32 } { i32 5, i32 0 }
Anders Carlssone8a81f72010-02-04 16:38:05 +000066 void (A::*pa4)() = &A::vf2;
Anders Carlssona024d172009-10-03 15:43:24 +000067}
Anders Carlsson375c31c2009-10-03 19:43:08 +000068
69void f3(A *a, A &ar) {
70 (a->*pa)();
71 (ar.*pa)();
72}
Anders Carlsson3eea6352009-10-13 17:41:28 +000073
Anders Carlssonbc0e0782009-11-23 20:04:44 +000074bool f4() {
75 return pa;
76}
77
Anders Carlsson3eea6352009-10-13 17:41:28 +000078// PR5177
79namespace PR5177 {
80 struct A {
81 bool foo(int*) const;
82 } a;
83
84 struct B1 {
85 bool (A::*pmf)(int*) const;
86 const A* pa;
87
88 B1() : pmf(&A::foo), pa(&a) {}
89 bool operator()() const { return (pa->*pmf)(new int); }
90 };
91
92 void bar(B1 b2) { while (b2()) ; }
93}
Anders Carlssonbb378cb2009-10-18 20:31:03 +000094
95// PR5138
96namespace PR5138 {
97 struct foo {
98 virtual void bar(foo *);
99 };
100
101 extern "C" {
102 void baz(foo *);
103 }
104
105 void (foo::*ptr1)(void *) = (void (foo::*)(void *))&foo::bar;
106 void (*ptr2)(void *) = (void (*)(void *))&baz;
107
108 void (foo::*ptr3)(void) = (void (foo::*)(void))&foo::bar;
109}
Anders Carlssona4c98cd2009-11-23 21:47:44 +0000110
111// PR5593
112namespace PR5593 {
113 struct A { };
114
115 bool f(void (A::*f)()) {
116 return f && f;
117 }
118}
Eli Friedmanb81c7862009-12-11 07:36:43 +0000119
120namespace PR5718 {
121 struct A { };
122
123 bool f(void (A::*f)(), void (A::*g)()) {
124 return f == g;
125 }
126}
Eli Friedman3a173702009-12-11 09:26:29 +0000127
128namespace BoolMemberPointer {
129 struct A { };
130
131 bool f(void (A::*f)()) {
132 return !f;
133 }
134
135 bool g(void (A::*f)()) {
136 if (!!f)
137 return true;
138 return false;
139 }
140}
141
Anders Carlsson7af4ec72010-01-05 05:04:05 +0000142// PR5940
143namespace PR5940 {
144 class foo {
145 public:
146 virtual void baz(void);
147 };
148
149 void foo::baz(void) {
150 void (foo::*ptr)(void) = &foo::baz;
151 }
152}
Eli Friedman3005efe2010-01-16 00:00:48 +0000153
154namespace MemberPointerImpCast {
155 struct A {
156 int x;
157 };
158 struct B : public A {
159 };
160 void f(B* obj, void (A::*method)()) {
161 (obj->*method)();
162 }
163}
Anders Carlsson585fa682010-02-07 17:37:13 +0000164
165// PR6258
166namespace PR6258 {
167
168 struct A {
169 void f(bool);
170 };
171
172 void (A::*pf)(bool) = &A::f;
173
174 void f() {
175 void (A::*pf)(bool) = &A::f;
176 }
177}
Douglas Gregore174bd02010-05-03 20:00:27 +0000178
179// PR7027
180namespace PR7027 {
181 struct X { void test( ); };
182 void testX() { &X::test; }
183}
John McCall875ab102010-08-22 06:43:33 +0000184
185namespace test7 {
186 struct A { void foo(); virtual void vfoo(); };
187 struct B { void foo(); virtual void vfoo(); };
188 struct C : A, B { void foo(); virtual void vfoo(); };
189
John McCall2a7b09d2012-05-01 17:32:20 +0000190 // GLOBAL-ARM: @_ZN5test74ptr0E = global {{.*}} { i32 ptrtoint ({{.*}}* @_ZN5test71A3fooEv to i32), i32 0 }
191 // GLOBAL-ARM: @_ZN5test74ptr1E = global {{.*}} { i32 ptrtoint ({{.*}}* @_ZN5test71B3fooEv to i32), i32 8 }
192 // GLOBAL-ARM: @_ZN5test74ptr2E = global {{.*}} { i32 ptrtoint ({{.*}}* @_ZN5test71C3fooEv to i32), i32 0 }
193 // GLOBAL-ARM: @_ZN5test74ptr3E = global {{.*}} { i32 0, i32 1 }
194 // GLOBAL-ARM: @_ZN5test74ptr4E = global {{.*}} { i32 0, i32 9 }
195 // GLOBAL-ARM: @_ZN5test74ptr5E = global {{.*}} { i32 0, i32 1 }
John McCall875ab102010-08-22 06:43:33 +0000196 void (C::*ptr0)() = &A::foo;
197 void (C::*ptr1)() = &B::foo;
198 void (C::*ptr2)() = &C::foo;
199 void (C::*ptr3)() = &A::vfoo;
200 void (C::*ptr4)() = &B::vfoo;
201 void (C::*ptr5)() = &C::vfoo;
202}
Douglas Gregordb68e282010-08-22 18:26:35 +0000203
204namespace test8 {
205 struct X { };
206 typedef int (X::*pmf)(int);
207
208 // CHECK: {{define.*_ZN5test81fEv}}
209 pmf f() {
210 // CHECK: {{ret.*zeroinitializer}}
211 return pmf();
212 }
213}
John McCall8bba1f42011-03-15 21:17:48 +0000214
215namespace test9 {
216 struct A {
217 void foo();
218 };
219 struct B : A {
220 void foo();
221 };
222
223 typedef void (A::*fooptr)();
224
225 struct S {
226 fooptr p;
227 };
228
John McCall2a7b09d2012-05-01 17:32:20 +0000229 // CODE-LP64: define void @_ZN5test94testEv(
230 // CODE-LP64: alloca i32
231 // CODE-LP64-NEXT: ret void
John McCall8bba1f42011-03-15 21:17:48 +0000232 void test() {
233 int x;
234 static S array[] = { (fooptr) &B::foo };
235 }
236}
John McCall4d4e5c12012-02-15 01:22:51 +0000237
238// rdar://problem/10815683 - Verify that we can emit reinterprets of
239// member pointers as constant initializers. For added trickiness,
240// we also add some non-trivial adjustments.
241namespace test10 {
242 struct A {
243 int nonEmpty;
244 void foo();
245 };
246 struct B : public A {
247 virtual void requireNonZeroAdjustment();
248 };
249 struct C {
250 int nonEmpty;
251 };
252 struct D : public C {
253 virtual void requireNonZeroAdjustment();
254 };
255
John McCall2a7b09d2012-05-01 17:32:20 +0000256
257// It's not that the offsets are doubled on ARM, it's that they're left-shifted by 1.
258
259// GLOBAL-LP64: @_ZN6test101aE = global { i64, i64 } { i64 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i64), i64 0 }, align 8
260// GLOBAL-LP32: @_ZN6test101aE = global { i32, i32 } { i32 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i32), i32 0 }, align 4
261// 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 +0000262 void (A::*a)() = &A::foo;
John McCall2a7b09d2012-05-01 17:32:20 +0000263
264// GLOBAL-LP64: @_ZN6test101bE = global { i64, i64 } { i64 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i64), i64 8 }, align 8
265// GLOBAL-LP32: @_ZN6test101bE = global { i32, i32 } { i32 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i32), i32 4 }, align 4
266// 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 +0000267 void (B::*b)() = (void (B::*)()) &A::foo;
John McCall2a7b09d2012-05-01 17:32:20 +0000268
269// GLOBAL-LP64: @_ZN6test101cE = global { i64, i64 } { i64 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i64), i64 8 }, align 8
270// GLOBAL-LP32: @_ZN6test101cE = global { i32, i32 } { i32 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i32), i32 4 }, align 4
271// 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 +0000272 void (C::*c)() = (void (C::*)()) (void (B::*)()) &A::foo;
John McCall2a7b09d2012-05-01 17:32:20 +0000273
274// GLOBAL-LP64: @_ZN6test101dE = global { i64, i64 } { i64 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i64), i64 16 }, align 8
275// GLOBAL-LP32: @_ZN6test101dE = global { i32, i32 } { i32 ptrtoint (void (%"struct.test10::A"*)* @_ZN6test101A3fooEv to i32), i32 8 }, align 4
276// 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 +0000277 void (D::*d)() = (void (C::*)()) (void (B::*)()) &A::foo;
278}
John McCall2a7b09d2012-05-01 17:32:20 +0000279
280namespace test11 {
281 struct A { virtual void a(); };
282 struct B : A {};
283 struct C : B { virtual void a(); };
284 void (C::*x)() = &C::a;
285
286 // GLOBAL-LP64: @_ZN6test111xE = global { i64, i64 } { i64 1, i64 0 }
287 // GLOBAL-LP32: @_ZN6test111xE = global { i32, i32 } { i32 1, i32 0 }
288 // GLOBAL-ARM: @_ZN6test111xE = global { i32, i32 } { i32 0, i32 1 }
289}