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