constructor-init.cpp revision bcc3e660a1fdc19722157ef3e2f133418856ca3d
1// RUN: %clang_cc1 -triple x86_64-apple-darwin10 %s -emit-llvm -o - | FileCheck %s
2
3extern "C" int printf(...);
4
5struct M {
6  M() { printf("M()\n"); }
7  M(int i) { iM = i; printf("M(%d)\n", i); }
8  int iM;
9  void MPR() {printf("iM = %d\n", iM); };
10};
11
12struct P {
13  P() { printf("P()\n"); }
14  P(int i) { iP = i; printf("P(%d)\n", i); }
15  int iP;
16  void PPR() {printf("iP = %d\n", iP); };
17};
18
19struct Q {
20  Q() { printf("Q()\n"); }
21  Q(int i) { iQ = i; printf("Q(%d)\n", i); }
22  int iQ;
23  void QPR() {printf("iQ = %d\n", iQ); };
24};
25
26struct N : M , P, Q {
27  N() : f1(1.314), P(2000), ld(00.1234+f1), M(1000), Q(3000),
28        d1(3.4567), i1(1234), m1(100) { printf("N()\n"); }
29  M m1;
30  M m2;
31  float f1;
32  int i1;
33  float d1;
34  void PR() {
35    printf("f1 = %f d1 = %f i1 = %d ld = %f \n", f1,d1,i1, ld);
36    MPR();
37    PPR();
38    QPR();
39    printf("iQ = %d\n", iQ);
40    printf("iP = %d\n", iP);
41    printf("iM = %d\n", iM);
42    // FIXME. We don't yet support this syntax.
43    // printf("iQ = %d\n", (*this).iQ);
44    printf("iQ = %d\n", this->iQ);
45    printf("iP = %d\n", this->iP);
46    printf("iM = %d\n", this->iM);
47  }
48  float ld;
49  float ff;
50  M arr_m[3];
51  P arr_p[1][3];
52  Q arr_q[2][3][4];
53};
54
55int main() {
56  M m1;
57
58  N n1;
59  n1.PR();
60}
61
62// PR5826
63template <class T> struct A {
64  A() {}
65  A(int) {}
66  A(const A&) {}
67  ~A() {}
68  operator int() {return 0;}
69};
70
71// CHECK: define void @_Z1fv()
72void f() {
73  // CHECK: call void @_ZN1AIsEC1Ei
74  A<short> a4 = 97;
75
76  // CHECK-NEXT: store i32 17
77  int i = 17;
78
79  // CHECK-NEXT: call void @_ZN1AIsED1Ev
80  // CHECK-NOT: call void @_ZN1AIsED1Ev
81  // CHECK: ret void
82}
83
84// Make sure we initialize the vtable pointer if it's required by a
85// base initializer.
86namespace InitVTable {
87  struct A { A(int); };
88  struct B : A {
89    virtual int foo();
90    B();
91    B(int);
92  };
93
94  // CHECK: define void @_ZN10InitVTable1BC2Ev(%"struct.InitVTable::B"* %this) unnamed_addr
95  // CHECK:      [[T0:%.*]] = bitcast [[B:%.*]]* [[THIS:%.*]] to i8***
96  // CHECK-NEXT: store i8** getelementptr inbounds ([3 x i8*]* @_ZTVN10InitVTable1BE, i64 0, i64 2), i8*** [[T0]]
97  // CHECK:      [[VTBL:%.*]] = load i32 ([[B]]*)*** {{%.*}}
98  // CHECK-NEXT: [[FNP:%.*]] = getelementptr inbounds i32 ([[B]]*)** [[VTBL]], i64 0
99  // CHECK-NEXT: [[FN:%.*]] = load i32 ([[B]]*)** [[FNP]]
100  // CHECK-NEXT: [[ARG:%.*]] = call i32 [[FN]]([[B]]* [[THIS]])
101  // CHECK-NEXT: call void @_ZN10InitVTable1AC2Ei({{.*}}* {{%.*}}, i32 [[ARG]])
102  // CHECK-NEXT: [[T0:%.*]] = bitcast [[B]]* [[THIS]] to i8***
103  // CHECK-NEXT: store i8** getelementptr inbounds ([3 x i8*]* @_ZTVN10InitVTable1BE, i64 0, i64 2), i8*** [[T0]]
104  // CHECK-NEXT: ret void
105  B::B() : A(foo()) {}
106
107  // CHECK: define void @_ZN10InitVTable1BC2Ei(%"struct.InitVTable::B"* %this, i32 %x) unnamed_addr
108  // CHECK:      [[ARG:%.*]] = add nsw i32 {{%.*}}, 5
109  // CHECK-NEXT: call void @_ZN10InitVTable1AC2Ei({{.*}}* {{%.*}}, i32 [[ARG]])
110  // CHECK-NEXT: [[T0:%.*]] = bitcast [[B]]* {{%.*}} to i8***
111  // CHECK-NEXT: store i8** getelementptr inbounds ([3 x i8*]* @_ZTVN10InitVTable1BE, i64 0, i64 2), i8*** [[T0]]
112  // CHECK-NEXT: ret void
113  B::B(int x) : A(x + 5) {}
114}
115
116template<typename T>
117struct X {
118  X(const X &);
119
120  T *start;
121  T *end;
122};
123
124template<typename T> struct X;
125
126// Make sure that the instantiated constructor initializes start and
127// end properly.
128// CHECK: define linkonce_odr void @_ZN1XIiEC2ERKS0_(%struct.X* %this, %struct.X* %other) unnamed_addr
129// CHECK: {{store.*null}}
130// CHECK: {{store.*null}}
131// CHECK: ret
132template<typename T>
133X<T>::X(const X &other) : start(0), end(0) { }
134
135X<int> get_X(X<int> x) { return x; }
136
137namespace rdar9694300 {
138  struct X {
139    int x;
140  };
141
142  // CHECK: define void @_ZN11rdar96943001fEv
143  void f() {
144    // CHECK: alloca
145    X x;
146    // CHECK-NEXT: [[I:%.*]] = alloca i32
147    // CHECK-NEXT: store i32 17, i32* [[I]]
148    int i = 17;
149    // CHECK-NEXT: ret void
150  }
151}
152