• 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 struct A { int a; void f(); virtual void vf1(); virtual void vf2(); };
8 struct B { int b; virtual void g(); };
9 struct C : B, A { };
10 
11 void (A::*pa)();
12 void (A::*volatile vpa)();
13 void (B::*pb)();
14 void (C::*pc)();
15 
16 // GLOBAL-LP64: @pa2 = global { i64, i64 } { i64 ptrtoint (void (%struct.A*)* @_ZN1A1fEv to i64), i64 0 }, align 8
17 void (A::*pa2)() = &A::f;
18 
19 // 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
21 void (A::*pa3)() = &A::vf1;
22 
23 // 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
25 void (A::*pa4)() = &A::vf2;
26 
27 // GLOBAL-LP64: @pc2 = global { i64, i64 } { i64 ptrtoint (void (%struct.A*)* @_ZN1A1fEv to i64), i64 16 }, align 8
28 void (C::*pc2)() = &C::f;
29 
30 // GLOBAL-LP64: @pc3 = global { i64, i64 } { i64 1, i64 0 }, align 8
31 void (A::*pc3)() = &A::vf1;
32 
f()33 void f() {
34   // CODE-LP64: store { i64, i64 } zeroinitializer, { i64, i64 }* @pa
35   pa = 0;
36 
37   // Is this okay?  What are LLVM's volatile semantics for structs?
38   // CODE-LP64: store volatile { i64, i64 } zeroinitializer, { i64, i64 }* @vpa
39   vpa = 0;
40 
41   // 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
46   pc = pa;
47 
48   // 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
53   pa = static_cast<void (A::*)()>(pc);
54 }
55 
f2()56 void f2() {
57   // CODE-LP64: store { i64, i64 } { i64 ptrtoint (void (%struct.A*)* @_ZN1A1fEv to i64), i64 0 }
58   void (A::*pa2)() = &A::f;
59 
60   // CODE-LP64: store { i64, i64 } { i64 1, i64 0 }
61   // CODE-LP32: store { i32, i32 } { i32 1, i32 0 }
62   void (A::*pa3)() = &A::vf1;
63 
64   // CODE-LP64: store { i64, i64 } { i64 9, i64 0 }
65   // CODE-LP32: store { i32, i32 } { i32 5, i32 0 }
66   void (A::*pa4)() = &A::vf2;
67 }
68 
f3(A * a,A & ar)69 void f3(A *a, A &ar) {
70   (a->*pa)();
71   (ar.*pa)();
72 }
73 
f4()74 bool f4() {
75   return pa;
76 }
77 
78 // PR5177
79 namespace 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 
B1PR5177::B188    B1() : pmf(&A::foo), pa(&a) {}
operator ()PR5177::B189    bool operator()() const { return (pa->*pmf)(new int); }
90   };
91 
bar(B1 b2)92   void bar(B1 b2) { while (b2()) ; }
93 }
94 
95 // PR5138
96 namespace 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 }
110 
111 // PR5593
112 namespace PR5593 {
113   struct A { };
114 
f(void (A::* f)())115   bool f(void (A::*f)()) {
116     return f && f;
117   }
118 }
119 
120 namespace PR5718 {
121   struct A { };
122 
f(void (A::* f)(),void (A::* g)())123   bool f(void (A::*f)(), void (A::*g)()) {
124     return f == g;
125   }
126 }
127 
128 namespace BoolMemberPointer {
129   struct A { };
130 
f(void (A::* f)())131   bool f(void (A::*f)()) {
132     return !f;
133   }
134 
g(void (A::* f)())135   bool g(void (A::*f)()) {
136     if (!!f)
137       return true;
138     return false;
139   }
140 }
141 
142 // PR5940
143 namespace PR5940 {
144   class foo {
145   public:
146     virtual void baz(void);
147   };
148 
baz(void)149   void foo::baz(void) {
150        void (foo::*ptr)(void) = &foo::baz;
151   }
152 }
153 
154 namespace MemberPointerImpCast {
155   struct A {
156     int x;
157   };
158   struct B : public A {
159   };
f(B * obj,void (A::* method)())160   void f(B* obj, void (A::*method)()) {
161     (obj->*method)();
162   }
163 }
164 
165 // PR6258
166 namespace PR6258 {
167 
168   struct A {
169     void f(bool);
170   };
171 
172   void (A::*pf)(bool) = &A::f;
173 
f()174   void f() {
175     void (A::*pf)(bool) = &A::f;
176   }
177 }
178 
179 // PR7027
180 namespace PR7027 {
181   struct X { void test( ); };
testX()182   void testX() { &X::test; }
183 }
184 
185 namespace 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 
190   // 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 }
196   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 }
203 
204 namespace test8 {
205   struct X { };
206   typedef int (X::*pmf)(int);
207 
208   // CHECK: {{define.*_ZN5test81fEv}}
f()209   pmf f() {
210     // CHECK: {{ret.*zeroinitializer}}
211     return pmf();
212   }
213 }
214 
215 namespace 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 
229   // CODE-LP64:    define void @_ZN5test94testEv(
230   // CODE-LP64:      alloca i32
231   // CODE-LP64-NEXT: ret void
test()232   void test() {
233     int x;
234     static S array[] = { (fooptr) &B::foo };
235   }
236 }
237 
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.
241 namespace 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 
256 
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
262   void (A::*a)() = &A::foo;
263 
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
267   void (B::*b)() = (void (B::*)()) &A::foo;
268 
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
272   void (C::*c)() = (void (C::*)()) (void (B::*)()) &A::foo;
273 
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
277   void (D::*d)() = (void (C::*)()) (void (B::*)()) &A::foo;
278 }
279 
280 namespace 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 }
290