xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGenCXX/pointers-to-data-members.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 %s -emit-llvm -o %t.ll -triple=x86_64-apple-darwin10
2f4a2713aSLionel Sambuc // RUN: FileCheck %s < %t.ll
3f4a2713aSLionel Sambuc // RUN: FileCheck -check-prefix=CHECK-GLOBAL %s < %t.ll
4f4a2713aSLionel Sambuc // RUN: %clang_cc1 %s -emit-llvm -o %t-opt.ll -triple=x86_64-apple-darwin10 -O3
5f4a2713aSLionel Sambuc // RUN: FileCheck --check-prefix=CHECK-O3 %s < %t-opt.ll
6f4a2713aSLionel Sambuc 
7f4a2713aSLionel Sambuc struct A { int a; int b; };
8f4a2713aSLionel Sambuc struct B { int b; };
9f4a2713aSLionel Sambuc struct C : B, A { };
10f4a2713aSLionel Sambuc 
11f4a2713aSLionel Sambuc // Zero init.
12f4a2713aSLionel Sambuc namespace ZeroInit {
13f4a2713aSLionel Sambuc   // CHECK-GLOBAL: @_ZN8ZeroInit1aE = global i64 -1
14f4a2713aSLionel Sambuc   int A::* a;
15f4a2713aSLionel Sambuc 
16f4a2713aSLionel Sambuc   // CHECK-GLOBAL: @_ZN8ZeroInit2aaE = global [2 x i64] [i64 -1, i64 -1]
17f4a2713aSLionel Sambuc   int A::* aa[2];
18f4a2713aSLionel Sambuc 
19f4a2713aSLionel Sambuc   // CHECK-GLOBAL: @_ZN8ZeroInit3aaaE = global [2 x [2 x i64]] {{\[}}[2 x i64] [i64 -1, i64 -1], [2 x i64] [i64 -1, i64 -1]]
20f4a2713aSLionel Sambuc   int A::* aaa[2][2];
21f4a2713aSLionel Sambuc 
22f4a2713aSLionel Sambuc   // CHECK-GLOBAL: @_ZN8ZeroInit1bE = global i64 -1,
23f4a2713aSLionel Sambuc   int A::* b = 0;
24f4a2713aSLionel Sambuc 
25f4a2713aSLionel Sambuc   // CHECK-GLOBAL: @_ZN8ZeroInit2saE = internal global %struct.anon { i64 -1 }
26f4a2713aSLionel Sambuc   struct {
27f4a2713aSLionel Sambuc     int A::*a;
28f4a2713aSLionel Sambuc   } sa;
test_sa()29f4a2713aSLionel Sambuc   void test_sa() { (void) sa; } // force emission
30f4a2713aSLionel Sambuc 
31f4a2713aSLionel Sambuc   // CHECK-GLOBAL: @_ZN8ZeroInit3ssaE = internal
32f4a2713aSLionel Sambuc   // CHECK-GLOBAL: [2 x i64] [i64 -1, i64 -1]
33f4a2713aSLionel Sambuc   struct {
34f4a2713aSLionel Sambuc     int A::*aa[2];
35f4a2713aSLionel Sambuc   } ssa[2];
test_ssa()36f4a2713aSLionel Sambuc   void test_ssa() { (void) ssa; }
37f4a2713aSLionel Sambuc 
38f4a2713aSLionel Sambuc   // CHECK-GLOBAL: @_ZN8ZeroInit2ssE = internal global %struct.anon.1 { %struct.anon.2 { i64 -1 } }
39f4a2713aSLionel Sambuc   struct {
40f4a2713aSLionel Sambuc     struct {
41f4a2713aSLionel Sambuc       int A::*pa;
42f4a2713aSLionel Sambuc     } s;
43f4a2713aSLionel Sambuc   } ss;
test_ss()44f4a2713aSLionel Sambuc   void test_ss() { (void) ss; }
45f4a2713aSLionel Sambuc 
46f4a2713aSLionel Sambuc   struct A {
47f4a2713aSLionel Sambuc     int A::*a;
48f4a2713aSLionel Sambuc     int b;
49f4a2713aSLionel Sambuc   };
50f4a2713aSLionel Sambuc 
51f4a2713aSLionel Sambuc   struct B {
52f4a2713aSLionel Sambuc     A a[10];
53f4a2713aSLionel Sambuc     char c;
54f4a2713aSLionel Sambuc     int B::*b;
55f4a2713aSLionel Sambuc   };
56f4a2713aSLionel Sambuc 
57f4a2713aSLionel Sambuc   struct C : A, B { int j; };
58*0a6a1f1dSLionel Sambuc   // CHECK-GLOBAL: @_ZN8ZeroInit1cE = global {{%.*}} <{ %"struct.ZeroInit::A" { i64 -1, i32 0 }, %"struct.ZeroInit::B" { [10 x %"struct.ZeroInit::A"] [%"struct.ZeroInit::A" { i64 -1, i32 0 }, %"struct.ZeroInit::A" { i64 -1, i32 0 }, %"struct.ZeroInit::A" { i64 -1, i32 0 }, %"struct.ZeroInit::A" { i64 -1, i32 0 }, %"struct.ZeroInit::A" { i64 -1, i32 0 }, %"struct.ZeroInit::A" { i64 -1, i32 0 }, %"struct.ZeroInit::A" { i64 -1, i32 0 }, %"struct.ZeroInit::A" { i64 -1, i32 0 }, %"struct.ZeroInit::A" { i64 -1, i32 0 }, %"struct.ZeroInit::A" { i64 -1, i32 0 }], i8 0, i64 -1 }, i32 0, [4 x i8] zeroinitializer }>, align 8
59f4a2713aSLionel Sambuc   C c;
60f4a2713aSLionel Sambuc }
61f4a2713aSLionel Sambuc 
62f4a2713aSLionel Sambuc // PR5674
63f4a2713aSLionel Sambuc namespace PR5674 {
64f4a2713aSLionel Sambuc   // CHECK-GLOBAL: @_ZN6PR56742pbE = global i64 4
65f4a2713aSLionel Sambuc   int A::*pb = &A::b;
66f4a2713aSLionel Sambuc }
67f4a2713aSLionel Sambuc 
68f4a2713aSLionel Sambuc // Casts.
69f4a2713aSLionel Sambuc namespace Casts {
70f4a2713aSLionel Sambuc 
71f4a2713aSLionel Sambuc int A::*pa;
72f4a2713aSLionel Sambuc int C::*pc;
73f4a2713aSLionel Sambuc 
f()74f4a2713aSLionel Sambuc void f() {
75f4a2713aSLionel Sambuc   // CHECK:      store i64 -1, i64* @_ZN5Casts2paE
76f4a2713aSLionel Sambuc   pa = 0;
77f4a2713aSLionel Sambuc 
78f4a2713aSLionel Sambuc   // CHECK-NEXT: [[TMP:%.*]] = load i64* @_ZN5Casts2paE, align 8
79f4a2713aSLionel Sambuc   // CHECK-NEXT: [[ADJ:%.*]] = add nsw i64 [[TMP]], 4
80f4a2713aSLionel Sambuc   // CHECK-NEXT: [[ISNULL:%.*]] = icmp eq i64 [[TMP]], -1
81f4a2713aSLionel Sambuc   // CHECK-NEXT: [[RES:%.*]] = select i1 [[ISNULL]], i64 [[TMP]], i64 [[ADJ]]
82f4a2713aSLionel Sambuc   // CHECK-NEXT: store i64 [[RES]], i64* @_ZN5Casts2pcE
83f4a2713aSLionel Sambuc   pc = pa;
84f4a2713aSLionel Sambuc 
85f4a2713aSLionel Sambuc   // CHECK-NEXT: [[TMP:%.*]] = load i64* @_ZN5Casts2pcE, align 8
86f4a2713aSLionel Sambuc   // CHECK-NEXT: [[ADJ:%.*]] = sub nsw i64 [[TMP]], 4
87f4a2713aSLionel Sambuc   // CHECK-NEXT: [[ISNULL:%.*]] = icmp eq i64 [[TMP]], -1
88f4a2713aSLionel Sambuc   // CHECK-NEXT: [[RES:%.*]] = select i1 [[ISNULL]], i64 [[TMP]], i64 [[ADJ]]
89f4a2713aSLionel Sambuc   // CHECK-NEXT: store i64 [[RES]], i64* @_ZN5Casts2paE
90f4a2713aSLionel Sambuc   pa = static_cast<int A::*>(pc);
91f4a2713aSLionel Sambuc }
92f4a2713aSLionel Sambuc 
93f4a2713aSLionel Sambuc }
94f4a2713aSLionel Sambuc 
95f4a2713aSLionel Sambuc // Comparisons
96f4a2713aSLionel Sambuc namespace Comparisons {
f()97f4a2713aSLionel Sambuc   void f() {
98f4a2713aSLionel Sambuc     int A::*a;
99f4a2713aSLionel Sambuc 
100f4a2713aSLionel Sambuc     // CHECK: icmp ne i64 {{.*}}, -1
101f4a2713aSLionel Sambuc     if (a) { }
102f4a2713aSLionel Sambuc 
103f4a2713aSLionel Sambuc     // CHECK: icmp ne i64 {{.*}}, -1
104f4a2713aSLionel Sambuc     if (a != 0) { }
105f4a2713aSLionel Sambuc 
106f4a2713aSLionel Sambuc     // CHECK: icmp ne i64 -1, {{.*}}
107f4a2713aSLionel Sambuc     if (0 != a) { }
108f4a2713aSLionel Sambuc 
109f4a2713aSLionel Sambuc     // CHECK: icmp eq i64 {{.*}}, -1
110f4a2713aSLionel Sambuc     if (a == 0) { }
111f4a2713aSLionel Sambuc 
112f4a2713aSLionel Sambuc     // CHECK: icmp eq i64 -1, {{.*}}
113f4a2713aSLionel Sambuc     if (0 == a) { }
114f4a2713aSLionel Sambuc   }
115f4a2713aSLionel Sambuc }
116f4a2713aSLionel Sambuc 
117f4a2713aSLionel Sambuc namespace ValueInit {
118f4a2713aSLionel Sambuc 
119f4a2713aSLionel Sambuc struct A {
120f4a2713aSLionel Sambuc   int A::*a;
121f4a2713aSLionel Sambuc 
122f4a2713aSLionel Sambuc   char c;
123f4a2713aSLionel Sambuc 
124f4a2713aSLionel Sambuc   A();
125f4a2713aSLionel Sambuc };
126f4a2713aSLionel Sambuc 
127f4a2713aSLionel Sambuc // CHECK-LABEL: define void @_ZN9ValueInit1AC2Ev(%"struct.ValueInit::A"* %this) unnamed_addr
128f4a2713aSLionel Sambuc // CHECK: store i64 -1, i64*
129f4a2713aSLionel Sambuc // CHECK: ret void
A()130f4a2713aSLionel Sambuc A::A() : a() {}
131f4a2713aSLionel Sambuc 
132f4a2713aSLionel Sambuc }
133f4a2713aSLionel Sambuc 
134f4a2713aSLionel Sambuc namespace PR7139 {
135f4a2713aSLionel Sambuc 
136f4a2713aSLionel Sambuc struct pair {
137f4a2713aSLionel Sambuc   int first;
138f4a2713aSLionel Sambuc   int second;
139f4a2713aSLionel Sambuc };
140f4a2713aSLionel Sambuc 
141f4a2713aSLionel Sambuc typedef int pair::*ptr_to_member_type;
142f4a2713aSLionel Sambuc 
143f4a2713aSLionel Sambuc struct ptr_to_member_struct {
144f4a2713aSLionel Sambuc   ptr_to_member_type data;
145f4a2713aSLionel Sambuc   int i;
146f4a2713aSLionel Sambuc };
147f4a2713aSLionel Sambuc 
148f4a2713aSLionel Sambuc struct A {
149f4a2713aSLionel Sambuc   ptr_to_member_struct a;
150f4a2713aSLionel Sambuc 
APR7139::A151f4a2713aSLionel Sambuc   A() : a() {}
152f4a2713aSLionel Sambuc };
153f4a2713aSLionel Sambuc 
154f4a2713aSLionel Sambuc // CHECK-O3: define zeroext i1 @_ZN6PR71395checkEv() [[NUW:#[0-9]+]]
check()155f4a2713aSLionel Sambuc bool check() {
156f4a2713aSLionel Sambuc   // CHECK-O3: ret i1 true
157f4a2713aSLionel Sambuc   return A().a.data == 0;
158f4a2713aSLionel Sambuc }
159f4a2713aSLionel Sambuc 
160f4a2713aSLionel Sambuc // CHECK-O3: define zeroext i1 @_ZN6PR71396check2Ev() [[NUW]]
check2()161f4a2713aSLionel Sambuc bool check2() {
162f4a2713aSLionel Sambuc   // CHECK-O3: ret i1 true
163f4a2713aSLionel Sambuc   return ptr_to_member_type() == 0;
164f4a2713aSLionel Sambuc }
165f4a2713aSLionel Sambuc 
166f4a2713aSLionel Sambuc }
167f4a2713aSLionel Sambuc 
168f4a2713aSLionel Sambuc namespace VirtualBases {
169f4a2713aSLionel Sambuc 
170f4a2713aSLionel Sambuc struct A {
171f4a2713aSLionel Sambuc   char c;
172f4a2713aSLionel Sambuc   int A::*i;
173f4a2713aSLionel Sambuc };
174f4a2713aSLionel Sambuc 
175f4a2713aSLionel Sambuc // CHECK-GLOBAL: @_ZN12VirtualBases1bE = global %"struct.VirtualBases::B" { i32 (...)** null, %"struct.VirtualBases::A" { i8 0, i64 -1 } }, align 8
176f4a2713aSLionel Sambuc struct B : virtual A { };
177f4a2713aSLionel Sambuc B b;
178f4a2713aSLionel Sambuc 
179f4a2713aSLionel Sambuc // CHECK-GLOBAL: @_ZN12VirtualBases1cE = global %"struct.VirtualBases::C" { i32 (...)** null, i64 -1, %"struct.VirtualBases::A" { i8 0, i64 -1 } }, align 8
180f4a2713aSLionel Sambuc struct C : virtual A { int A::*i; };
181f4a2713aSLionel Sambuc C c;
182f4a2713aSLionel Sambuc 
183f4a2713aSLionel Sambuc // CHECK-GLOBAL: @_ZN12VirtualBases1dE = global %"struct.VirtualBases::D" { %"struct.VirtualBases::C.base" { i32 (...)** null, i64 -1 }, i64 -1, %"struct.VirtualBases::A" { i8 0, i64 -1 } }, align 8
184f4a2713aSLionel Sambuc struct D : C { int A::*i; };
185f4a2713aSLionel Sambuc D d;
186f4a2713aSLionel Sambuc 
187f4a2713aSLionel Sambuc }
188f4a2713aSLionel Sambuc 
189f4a2713aSLionel Sambuc namespace Test1 {
190f4a2713aSLionel Sambuc 
191f4a2713aSLionel Sambuc // Don't crash when A contains a bit-field.
192f4a2713aSLionel Sambuc struct A {
193f4a2713aSLionel Sambuc   int A::* a;
194f4a2713aSLionel Sambuc   int b : 10;
195f4a2713aSLionel Sambuc };
196f4a2713aSLionel Sambuc A a;
197f4a2713aSLionel Sambuc 
198f4a2713aSLionel Sambuc }
199f4a2713aSLionel Sambuc 
200f4a2713aSLionel Sambuc namespace BoolPtrToMember {
201f4a2713aSLionel Sambuc   struct X {
202f4a2713aSLionel Sambuc     bool member;
203f4a2713aSLionel Sambuc   };
204f4a2713aSLionel Sambuc 
205*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: define dereferenceable({{[0-9]+}}) i8* @_ZN15BoolPtrToMember1fERNS_1XEMS0_b
f(X & x,bool X::* member)206f4a2713aSLionel Sambuc   bool &f(X &x, bool X::*member) {
207f4a2713aSLionel Sambuc     // CHECK: {{bitcast.* to i8\*}}
208f4a2713aSLionel Sambuc     // CHECK-NEXT: getelementptr inbounds i8*
209f4a2713aSLionel Sambuc     // CHECK-NEXT: ret i8*
210f4a2713aSLionel Sambuc     return x.*member;
211f4a2713aSLionel Sambuc   }
212f4a2713aSLionel Sambuc }
213f4a2713aSLionel Sambuc 
214f4a2713aSLionel Sambuc namespace PR8507 {
215f4a2713aSLionel Sambuc 
216f4a2713aSLionel Sambuc struct S;
f(S * p,double S::* pm)217f4a2713aSLionel Sambuc void f(S* p, double S::*pm) {
218f4a2713aSLionel Sambuc   if (0 < p->*pm) {
219f4a2713aSLionel Sambuc   }
220f4a2713aSLionel Sambuc }
221f4a2713aSLionel Sambuc 
222f4a2713aSLionel Sambuc }
223f4a2713aSLionel Sambuc 
224f4a2713aSLionel Sambuc namespace test4 {
225f4a2713aSLionel Sambuc   struct A             { int A_i; };
226f4a2713aSLionel Sambuc   struct B : virtual A { int A::*B_p; };
227f4a2713aSLionel Sambuc   struct C : virtual B { int    *C_p; };
228f4a2713aSLionel Sambuc   struct D :         C { int    *D_p; };
229f4a2713aSLionel Sambuc 
230f4a2713aSLionel Sambuc   // CHECK-GLOBAL: @_ZN5test41dE = global %"struct.test4::D" { %"struct.test4::C.base" zeroinitializer, i32* null, %"struct.test4::B.base" { i32 (...)** null, i64 -1 }, %"struct.test4::A" zeroinitializer }, align 8
231f4a2713aSLionel Sambuc   D d;
232f4a2713aSLionel Sambuc }
233f4a2713aSLionel Sambuc 
234f4a2713aSLionel Sambuc namespace PR11487 {
235f4a2713aSLionel Sambuc   union U
236f4a2713aSLionel Sambuc   {
237f4a2713aSLionel Sambuc     int U::* mptr;
238f4a2713aSLionel Sambuc     char x[16];
239f4a2713aSLionel Sambuc   } x;
240f4a2713aSLionel Sambuc   // CHECK-GLOBAL: @_ZN7PR114871xE = global %"union.PR11487::U" { i64 -1, [8 x i8] zeroinitializer }, align 8
241f4a2713aSLionel Sambuc 
242f4a2713aSLionel Sambuc }
243f4a2713aSLionel Sambuc 
244f4a2713aSLionel Sambuc namespace PR13097 {
245f4a2713aSLionel Sambuc   struct X { int x; X(const X&); };
246f4a2713aSLionel Sambuc   struct A {
247f4a2713aSLionel Sambuc     int qq;
248f4a2713aSLionel Sambuc       X x;
249f4a2713aSLionel Sambuc   };
250f4a2713aSLionel Sambuc   A f();
g()251f4a2713aSLionel Sambuc   X g() { return f().*&A::x; }
252f4a2713aSLionel Sambuc   // CHECK-LABEL: define void @_ZN7PR130971gEv
253f4a2713aSLionel Sambuc   // CHECK: call void @_ZN7PR130971fEv
254f4a2713aSLionel Sambuc   // CHECK-NOT: memcpy
255f4a2713aSLionel Sambuc   // CHECK: call void @_ZN7PR130971XC1ERKS0_
256f4a2713aSLionel Sambuc }
257f4a2713aSLionel Sambuc 
258*0a6a1f1dSLionel Sambuc namespace PR21089 {
259*0a6a1f1dSLionel Sambuc struct A {
260*0a6a1f1dSLionel Sambuc   bool : 1;
261*0a6a1f1dSLionel Sambuc   int A::*x;
262*0a6a1f1dSLionel Sambuc   bool y;
263*0a6a1f1dSLionel Sambuc   A();
264*0a6a1f1dSLionel Sambuc };
265*0a6a1f1dSLionel Sambuc struct B : A {
266*0a6a1f1dSLionel Sambuc };
267*0a6a1f1dSLionel Sambuc B b;
268*0a6a1f1dSLionel Sambuc // CHECK-GLOBAL: @_ZN7PR210891bE = global %"struct.PR21089::B" { %"struct.PR21089::A.base" <{ i8 0, [7 x i8] zeroinitializer, i64 -1, i8 0 }>, [7 x i8] zeroinitializer }, align 8
269*0a6a1f1dSLionel Sambuc }
270*0a6a1f1dSLionel Sambuc 
271*0a6a1f1dSLionel Sambuc namespace PR21282 {
272*0a6a1f1dSLionel Sambuc union U {
273*0a6a1f1dSLionel Sambuc   int U::*x;
274*0a6a1f1dSLionel Sambuc   long y[2];
275*0a6a1f1dSLionel Sambuc };
276*0a6a1f1dSLionel Sambuc U u;
277*0a6a1f1dSLionel Sambuc // CHECK-GLOBAL: @_ZN7PR212821uE = global %"union.PR21282::U" { i64 -1, [8 x i8] zeroinitializer }, align 8
278*0a6a1f1dSLionel Sambuc }
279*0a6a1f1dSLionel Sambuc 
280f4a2713aSLionel Sambuc // CHECK-O3: attributes [[NUW]] = { nounwind readnone{{.*}} }
281