1// RUN: %clang_cc1 -triple x86_64-apple-darwin10 %s -emit-llvm -o - | FileCheck %s
2
3struct Member { int x; Member(); Member(int); Member(const Member &); };
4struct VBase { int x; VBase(); VBase(int); VBase(const VBase &); };
5
6struct ValueClass {
7  ValueClass(int x, int y) : x(x), y(y) {}
8  int x;
9  int y;
10}; // subject to ABI trickery
11
12
13
14/* Test basic functionality. */
15struct A {
16  A(struct Undeclared &);
17  A(ValueClass);
18  Member mem;
19};
20
21A::A(struct Undeclared &ref) : mem(0) {}
22
23// Check that delegation works.
24// CHECK: define void @_ZN1AC1ER10Undeclared(%struct.A* %this, %struct.Undeclared* %ref) unnamed_addr
25// CHECK: call void @_ZN1AC2ER10Undeclared(
26
27// CHECK: define void @_ZN1AC2ER10Undeclared(%struct.A* %this, %struct.Undeclared* %ref) unnamed_addr
28// CHECK: call void @_ZN6MemberC1Ei(
29
30A::A(ValueClass v) : mem(v.y - v.x) {}
31
32// CHECK: define void @_ZN1AC1E10ValueClass(%struct.A* %this, i64 %v.coerce) unnamed_addr
33// CHECK: call void @_ZN1AC2E10ValueClass(
34
35// CHECK: define void @_ZN1AC2E10ValueClass(%struct.A* %this, i64 %v.coerce) unnamed_addr
36// CHECK: call void @_ZN6MemberC1Ei(
37
38
39/* Test that things work for inheritance. */
40struct B : A {
41  B(struct Undeclared &);
42  Member mem;
43};
44
45B::B(struct Undeclared &ref) : A(ref), mem(1) {}
46
47// CHECK: define void @_ZN1BC1ER10Undeclared(%struct.B* %this, %struct.Undeclared* %ref) unnamed_addr
48// CHECK: call void @_ZN1BC2ER10Undeclared(
49
50// CHECK: define void @_ZN1BC2ER10Undeclared(%struct.B* %this, %struct.Undeclared* %ref) unnamed_addr
51// CHECK: call void @_ZN1AC2ER10Undeclared(
52// CHECK: call void @_ZN6MemberC1Ei(
53
54
55
56/* Test that the delegation optimization is disabled for classes with
57   virtual bases (for now).  This is necessary because a vbase
58   initializer could access one of the parameter variables by
59   reference.  That's a solvable problem, but let's not solve it right
60   now. */
61struct C : virtual A {
62  C(int);
63  Member mem;
64};
65C::C(int x) : A(ValueClass(x, x+1)), mem(x * x) {}
66
67// CHECK: define void @_ZN1CC1Ei(%struct.C* %this, i32 %x) unnamed_addr
68// CHECK: call void @_ZN10ValueClassC1Eii(
69// CHECK: call void @_ZN1AC2E10ValueClass(
70// CHECK: call void @_ZN6MemberC1Ei(
71
72// CHECK: define void @_ZN1CC2Ei(%struct.C* %this, i8** %vtt, i32 %x) unnamed_addr
73// CHECK: call void @_ZN6MemberC1Ei(
74
75
76
77/* Test that the delegation optimization is disabled for varargs
78   constructors. */
79struct D : A {
80  D(int, ...);
81  Member mem;
82};
83
84D::D(int x, ...) : A(ValueClass(x, x+1)), mem(x*x) {}
85
86// CHECK: define void @_ZN1DC1Eiz(%struct.D* %this, i32 %x, ...) unnamed_addr
87// CHECK: call void @_ZN10ValueClassC1Eii(
88// CHECK: call void @_ZN1AC2E10ValueClass(
89// CHECK: call void @_ZN6MemberC1Ei(
90
91// CHECK: define void @_ZN1DC2Eiz(%struct.D* %this, i32 %x, ...) unnamed_addr
92// CHECK: call void @_ZN10ValueClassC1Eii(
93// CHECK: call void @_ZN1AC2E10ValueClass(
94// CHECK: call void @_ZN6MemberC1Ei(
95
96
97// PR6622:  this shouldn't crash
98namespace test0 {
99  struct A {};
100  struct B : virtual A { int x; };
101  struct C : B {};
102
103  void test(C &in) {
104    C tmp = in;
105  }
106}
107
108namespace test1 {
109  struct A { A(); void *ptr; };
110  struct B { B(); int x; A a[0]; };
111  B::B() {}
112  // CHECK:    define void @_ZN5test11BC2Ev(
113  // CHECK:      [[THIS:%.*]] = load [[B:%.*]]**
114  // CHECK-NEXT: ret void
115}
116