pointers-to-data-members.cpp revision 1fb0caaa7bef765b85972274e3b434af2572c141
1// RUN: %clang_cc1 %s -emit-llvm -o - -triple=x86_64-apple-darwin10 | FileCheck %s
2// RUN: %clang_cc1 %s -emit-llvm -o - -triple=x86_64-apple-darwin10 -O3 | FileCheck --check-prefix=CHECK-O3 %s
3struct A { int a; int b; };
4struct B { int b; };
5struct C : B, A { };
6
7// Zero init.
8namespace ZeroInit {
9  // CHECK: @_ZN8ZeroInit1aE = global i64 -1
10  int A::* a;
11
12  // CHECK: @_ZN8ZeroInit2aaE = global [2 x i64] [i64 -1, i64 -1]
13  int A::* aa[2];
14
15  // CHECK: @_ZN8ZeroInit3aaaE = global [2 x [2 x i64]] {{\[}}[2 x i64] [i64 -1, i64 -1], [2 x i64] [i64 -1, i64 -1]]
16  int A::* aaa[2][2];
17
18  // CHECK: @_ZN8ZeroInit1bE = global i64 -1,
19  int A::* b = 0;
20
21  // CHECK: @_ZN8ZeroInit2saE = internal global %struct.anon { i64 -1 }
22  struct {
23    int A::*a;
24  } sa;
25  void test_sa() { (void) sa; } // force emission
26
27  // CHECK: @_ZN8ZeroInit3ssaE = internal
28  // CHECK: [2 x i64] [i64 -1, i64 -1]
29  struct {
30    int A::*aa[2];
31  } ssa[2];
32  void test_ssa() { (void) ssa; }
33
34  // CHECK: @_ZN8ZeroInit2ssE = internal global %1 { %struct.anon { i64 -1 } }
35  struct {
36    struct {
37      int A::*pa;
38    } s;
39  } ss;
40  void test_ss() { (void) ss; }
41
42  struct A {
43    int A::*a;
44    int b;
45  };
46
47  struct B {
48    A a[10];
49    char c;
50    int B::*b;
51  };
52
53  struct C : A, B { int j; };
54  // CHECK: @_ZN8ZeroInit1cE = global %"struct.ZeroInit::C" { [16 x i8] c"\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00", [176 x i8] c"\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF", i32 0, [4 x i8] zeroinitializer }
55  C c;
56}
57
58// PR5674
59namespace PR5674 {
60  // CHECK: @_ZN6PR56742pbE = global i64 4
61  int A::*pb = &A::b;
62}
63
64// Casts.
65namespace Casts {
66
67int A::*pa;
68int C::*pc;
69
70void f() {
71  // CHECK:      store i64 -1, i64* @_ZN5Casts2paE
72  pa = 0;
73
74  // CHECK-NEXT: [[TMP:%.*]] = load i64* @_ZN5Casts2paE, align 8
75  // CHECK-NEXT: [[ADJ:%.*]] = add nsw i64 [[TMP]], 4
76  // CHECK-NEXT: [[ISNULL:%.*]] = icmp eq i64 [[TMP]], -1
77  // CHECK-NEXT: [[RES:%.*]] = select i1 [[ISNULL]], i64 [[TMP]], i64 [[ADJ]]
78  // CHECK-NEXT: store i64 [[RES]], i64* @_ZN5Casts2pcE
79  pc = pa;
80
81  // CHECK-NEXT: [[TMP:%.*]] = load i64* @_ZN5Casts2pcE, align 8
82  // CHECK-NEXT: [[ADJ:%.*]] = sub nsw i64 [[TMP]], 4
83  // CHECK-NEXT: [[ISNULL:%.*]] = icmp eq i64 [[TMP]], -1
84  // CHECK-NEXT: [[RES:%.*]] = select i1 [[ISNULL]], i64 [[TMP]], i64 [[ADJ]]
85  // CHECK-NEXT: store i64 [[RES]], i64* @_ZN5Casts2paE
86  pa = static_cast<int A::*>(pc);
87}
88
89}
90
91// Comparisons
92namespace Comparisons {
93  void f() {
94    int A::*a;
95
96    // CHECK: icmp ne i64 {{.*}}, -1
97    if (a) { }
98
99    // CHECK: icmp ne i64 {{.*}}, -1
100    if (a != 0) { }
101
102    // CHECK: icmp ne i64 -1, {{.*}}
103    if (0 != a) { }
104
105    // CHECK: icmp eq i64 {{.*}}, -1
106    if (a == 0) { }
107
108    // CHECK: icmp eq i64 -1, {{.*}}
109    if (0 == a) { }
110  }
111}
112
113namespace ValueInit {
114
115struct A {
116  int A::*a;
117
118  char c;
119
120  A();
121};
122
123// CHECK: define void @_ZN9ValueInit1AC2Ev
124// CHECK: store i64 -1, i64*
125// CHECK: ret void
126A::A() : a() {}
127
128}
129
130namespace PR7139 {
131
132struct pair {
133  int first;
134  int second;
135};
136
137typedef int pair::*ptr_to_member_type;
138
139struct ptr_to_member_struct {
140  ptr_to_member_type data;
141  int i;
142};
143
144struct A {
145  ptr_to_member_struct a;
146
147  A() : a() {}
148};
149
150// CHECK-O3: define zeroext i1 @_ZN6PR71395checkEv() nounwind readnone
151bool check() {
152  // CHECK-O3: ret i1 true
153  return A().a.data == 0;
154}
155
156// CHECK-O3: define zeroext i1 @_ZN6PR71396check2Ev() nounwind readnone
157bool check2() {
158  // CHECK-O3: ret i1 true
159  return ptr_to_member_type() == 0;
160}
161
162}
163
164namespace VirtualBases {
165
166struct A {
167  char c;
168  int A::*i;
169};
170
171// FIXME: A::i should be initialized to -1 here.
172struct B : virtual A { };
173B b;
174
175// FIXME: A::i should be initialized to -1 here.
176struct C : virtual A { int A::*i; };
177C c;
178
179// FIXME: C::A::i should be initialized to -1 here.
180struct D : C { int A::*i; };
181D d;
182
183}
184
185namespace Test1 {
186
187// Don't crash when A contains a bit-field.
188struct A {
189  int A::* a;
190  int b : 10;
191};
192A a;
193
194}
195
196namespace BoolPtrToMember {
197  struct X {
198    bool member;
199  };
200
201  // CHECK: define i8* @_ZN15BoolPtrToMember1fERNS_1XEMS0_b
202  bool &f(X &x, bool X::*member) {
203    // CHECK: {{bitcast.* to i8\*}}
204    // CHECK-NEXT: getelementptr inbounds i8*
205    // CHECK-NEXT: ret i8*
206    return x.*member;
207  }
208}
209