blob: 24be897b0578fcb9461251de5cb8a842edd3d55d [file] [log] [blame]
Reid Klecknera4130ba2013-07-22 13:51:44 +00001// RUN: %clang_cc1 -emit-llvm %s -o - -cxx-abi microsoft -triple=i386-pc-win32 -fno-rtti > %t
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00002// RUN: FileCheck %s < %t
Reid Klecknera4130ba2013-07-22 13:51:44 +00003// vftables are emitted very late, so do another pass to try to keep the checks
4// in source order.
5// RUN: FileCheck --check-prefix DTORS %s < %t
Timur Iskhodzhanov85607912012-04-20 08:05:00 +00006
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +00007namespace basic {
8
Timur Iskhodzhanov85607912012-04-20 08:05:00 +00009class A {
10 public:
11 A() { }
Reid Klecknera4130ba2013-07-22 13:51:44 +000012 ~A();
Timur Iskhodzhanov85607912012-04-20 08:05:00 +000013};
14
Timur Iskhodzhanov6c9dccd2013-02-12 13:22:47 +000015void no_constructor_destructor_infinite_recursion() {
Timur Iskhodzhanov85607912012-04-20 08:05:00 +000016 A a;
17
Stephen Lin3b50e8d2013-06-30 20:40:16 +000018// CHECK: define linkonce_odr x86_thiscallcc %"class.basic::A"* @"\01??0A@basic@@QAE@XZ"(%"class.basic::A"* returned %this)
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000019// CHECK: [[THIS_ADDR:%[.0-9A-Z_a-z]+]] = alloca %"class.basic::A"*, align 4
20// CHECK-NEXT: store %"class.basic::A"* %this, %"class.basic::A"** [[THIS_ADDR]], align 4
21// CHECK-NEXT: [[T1:%[.0-9A-Z_a-z]+]] = load %"class.basic::A"** [[THIS_ADDR]]
22// CHECK-NEXT: ret %"class.basic::A"* [[T1]]
John McCallbd315742012-09-25 08:00:39 +000023// CHECK-NEXT: }
Reid Klecknera4130ba2013-07-22 13:51:44 +000024}
Timur Iskhodzhanov85607912012-04-20 08:05:00 +000025
Reid Klecknera4130ba2013-07-22 13:51:44 +000026A::~A() {
Timur Iskhodzhanov85607912012-04-20 08:05:00 +000027// Make sure that the destructor doesn't call itself:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000028// CHECK: define {{.*}} @"\01??1A@basic@@QAE@XZ"
29// CHECK-NOT: call void @"\01??1A@basic@@QAE@XZ"
Timur Iskhodzhanov85607912012-04-20 08:05:00 +000030// CHECK: ret
31}
John McCallbd315742012-09-25 08:00:39 +000032
Timur Iskhodzhanov649c7312013-01-21 13:02:41 +000033struct B {
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000034 B();
35};
36
37// Tests that we can define constructors outside the class (PR12784).
38B::B() {
Stephen Lin3b50e8d2013-06-30 20:40:16 +000039 // CHECK: define x86_thiscallcc %"struct.basic::B"* @"\01??0B@basic@@QAE@XZ"(%"struct.basic::B"* returned %this)
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000040 // CHECK: ret
41}
42
43struct C {
44 virtual ~C() {
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000045// DTORS: define linkonce_odr x86_thiscallcc void @"\01??_GC@basic@@UAEPAXI@Z"(%"struct.basic::C"* %this, i1 zeroext %should_call_delete)
Timur Iskhodzhanov47453452013-02-13 12:14:25 +000046// DTORS: %[[FROMBOOL:[0-9a-z]+]] = zext i1 %should_call_delete to i8
Timur Iskhodzhanovba8fa0c2013-02-13 12:24:19 +000047// DTORS-NEXT: store i8 %[[FROMBOOL]], i8* %[[SHOULD_DELETE_VAR:[0-9a-z._]+]], align 1
48// DTORS: %[[SHOULD_DELETE_VALUE:[0-9a-z._]+]] = load i8* %[[SHOULD_DELETE_VAR]]
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000049// DTORS: call x86_thiscallcc void @"\01??1C@basic@@UAE@XZ"(%"struct.basic::C"* %[[THIS:[0-9a-z]+]])
Timur Iskhodzhanov47453452013-02-13 12:14:25 +000050// DTORS-NEXT: %[[CONDITION:[0-9]+]] = icmp eq i8 %[[SHOULD_DELETE_VALUE]], 0
51// DTORS-NEXT: br i1 %[[CONDITION]], label %[[CONTINUE_LABEL:[0-9a-z._]+]], label %[[CALL_DELETE_LABEL:[0-9a-z._]+]]
52//
53// DTORS: [[CALL_DELETE_LABEL]]
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000054// DTORS-NEXT: %[[THIS_AS_VOID:[0-9a-z]+]] = bitcast %"struct.basic::C"* %[[THIS]] to i8*
Reid Klecknera4130ba2013-07-22 13:51:44 +000055// DTORS-NEXT: call void @"\01??3@YAXPAX@Z"(i8* %[[THIS_AS_VOID]])
Timur Iskhodzhanov47453452013-02-13 12:14:25 +000056// DTORS-NEXT: br label %[[CONTINUE_LABEL]]
57//
58// DTORS: [[CONTINUE_LABEL]]
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000059// DTORS-NEXT: ret void
60 }
Timur Iskhodzhanov649c7312013-01-21 13:02:41 +000061 virtual void foo();
62};
63
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000064// Emits the vftable in the output.
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000065void C::foo() {}
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000066
Timur Iskhodzhanov649c7312013-01-21 13:02:41 +000067void check_vftable_offset() {
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000068 C c;
Timur Iskhodzhanov649c7312013-01-21 13:02:41 +000069// The vftable pointer should point at the beginning of the vftable.
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000070// CHECK: [[THIS_PTR:%[0-9]+]] = bitcast %"struct.basic::C"* {{.*}} to i8***
71// CHECK: store i8** getelementptr inbounds ([2 x i8*]* @"\01??_7C@basic@@6B@", i64 0, i64 0), i8*** [[THIS_PTR]]
Timur Iskhodzhanov649c7312013-01-21 13:02:41 +000072}
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000073
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000074void call_complete_dtor(C *obj_ptr) {
75// CHECK: define void @"\01?call_complete_dtor@basic@@YAXPAUC@1@@Z"(%"struct.basic::C"* %obj_ptr)
76 obj_ptr->~C();
77// CHECK: %[[OBJ_PTR_VALUE:.*]] = load %"struct.basic::C"** %{{.*}}, align 4
78// CHECK-NEXT: %[[PVTABLE:.*]] = bitcast %"struct.basic::C"* %[[OBJ_PTR_VALUE]] to void (%"struct.basic::C"*, i1)***
79// CHECK-NEXT: %[[VTABLE:.*]] = load void (%"struct.basic::C"*, i1)*** %[[PVTABLE]]
80// CHECK-NEXT: %[[PVDTOR:.*]] = getelementptr inbounds void (%"struct.basic::C"*, i1)** %[[VTABLE]], i64 0
81// CHECK-NEXT: %[[VDTOR:.*]] = load void (%"struct.basic::C"*, i1)** %[[PVDTOR]]
82// CHECK-NEXT: call x86_thiscallcc void %[[VDTOR]](%"struct.basic::C"* %[[OBJ_PTR_VALUE]], i1 zeroext false)
Timur Iskhodzhanov0f9827f2013-02-15 14:45:22 +000083// CHECK-NEXT: ret void
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000084}
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000085
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000086void call_deleting_dtor(C *obj_ptr) {
87// CHECK: define void @"\01?call_deleting_dtor@basic@@YAXPAUC@1@@Z"(%"struct.basic::C"* %obj_ptr)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000088 delete obj_ptr;
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000089// CHECK: %[[OBJ_PTR_VALUE:.*]] = load %"struct.basic::C"** %{{.*}}, align 4
90// CHECK: br i1 {{.*}}, label %[[DELETE_NULL:.*]], label %[[DELETE_NOTNULL:.*]]
91
92// CHECK: [[DELETE_NOTNULL]]
93// CHECK-NEXT: %[[PVTABLE:.*]] = bitcast %"struct.basic::C"* %[[OBJ_PTR_VALUE]] to void (%"struct.basic::C"*, i1)***
94// CHECK-NEXT: %[[VTABLE:.*]] = load void (%"struct.basic::C"*, i1)*** %[[PVTABLE]]
95// CHECK-NEXT: %[[PVDTOR:.*]] = getelementptr inbounds void (%"struct.basic::C"*, i1)** %[[VTABLE]], i64 0
96// CHECK-NEXT: %[[VDTOR:.*]] = load void (%"struct.basic::C"*, i1)** %[[PVDTOR]]
97// CHECK-NEXT: call x86_thiscallcc void %[[VDTOR]](%"struct.basic::C"* %[[OBJ_PTR_VALUE]], i1 zeroext true)
Timur Iskhodzhanov0f9827f2013-02-15 14:45:22 +000098// CHECK: ret void
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000099}
100
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000101struct D {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000102 static int foo();
103
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000104 D() {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000105 static int ctor_static = foo();
106 // CHECK that the static in the ctor gets mangled correctly:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000107 // CHECK: @"\01?ctor_static@?1???0D@basic@@QAE@XZ@4HA"
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000108 }
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000109 ~D() {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000110 static int dtor_static = foo();
111 // CHECK that the static in the dtor gets mangled correctly:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000112 // CHECK: @"\01?dtor_static@?1???1D@basic@@QAE@XZ@4HA"
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000113 }
114};
115
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000116void use_D() { D c; }
Bill Wendling4e1125f2013-02-22 09:10:20 +0000117
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000118} // end namespace basic
119
120
121namespace constructors {
122
123struct A {
124 A() {}
125};
126
127struct B : A {
128 B();
129 ~B();
130};
131
132B::B() {
Stephen Lin3b50e8d2013-06-30 20:40:16 +0000133 // CHECK: define x86_thiscallcc %"struct.constructors::B"* @"\01??0B@constructors@@QAE@XZ"(%"struct.constructors::B"* returned %this)
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000134 // CHECK: call x86_thiscallcc %"struct.constructors::A"* @"\01??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}})
135 // CHECK: ret
136}
137
138struct C : virtual A {
139 C();
140};
141
142C::C() {
Stephen Lin3b50e8d2013-06-30 20:40:16 +0000143 // CHECK: define x86_thiscallcc %"struct.constructors::C"* @"\01??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* returned %this, i32 %is_most_derived)
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000144 // TODO: make sure this works in the Release build too;
145 // CHECK: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4
146 // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32* %[[IS_MOST_DERIVED_VAR]]
147 // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
148 // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
149 //
150 // CHECK: [[INIT_VBASES]]
Reid Kleckner90633022013-06-19 15:20:38 +0000151 // CHECK-NEXT: %[[this_i8:.*]] = bitcast %"struct.constructors::C"* %{{.*}} to i8*
152 // CHECK-NEXT: %[[vbptr_off:.*]] = getelementptr inbounds i8* %[[this_i8]], i64 0
153 // CHECK-NEXT: %[[vbptr:.*]] = bitcast i8* %[[vbptr_off]] to [2 x i32]**
154 // CHECK-NEXT: store [2 x i32]* @"\01??_8C@constructors@@7B@", [2 x i32]** %[[vbptr]]
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000155 // CHECK-NEXT: bitcast %"struct.constructors::C"* %{{.*}} to %"struct.constructors::A"*
156 // CHECK-NEXT: call x86_thiscallcc %"struct.constructors::A"* @"\01??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}})
157 // CHECK-NEXT: br label %[[SKIP_VBASES]]
158 //
159 // CHECK: [[SKIP_VBASES]]
160 // CHECK: @"\01??_7C@constructors@@6B@"
161 // CHECK: ret
162}
163
164void create_C() {
165 C c;
166 // CHECK: define void @"\01?create_C@constructors@@YAXXZ"()
167 // CHECK: call x86_thiscallcc %"struct.constructors::C"* @"\01??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* %c, i32 1)
168 // CHECK: ret
169}
170
171struct D : C {
172 D();
173};
174
175D::D() {
Stephen Lin3b50e8d2013-06-30 20:40:16 +0000176 // CHECK: define x86_thiscallcc %"struct.constructors::D"* @"\01??0D@constructors@@QAE@XZ"(%"struct.constructors::D"* returned %this, i32 %is_most_derived) unnamed_addr
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000177 // CHECK: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4
178 // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32* %[[IS_MOST_DERIVED_VAR]]
179 // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
180 // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
181 //
182 // CHECK: [[INIT_VBASES]]
Reid Kleckner90633022013-06-19 15:20:38 +0000183 // CHECK-NEXT: %[[this_i8:.*]] = bitcast %"struct.constructors::D"* %{{.*}} to i8*
184 // CHECK-NEXT: %[[vbptr_off:.*]] = getelementptr inbounds i8* %[[this_i8]], i64 0
185 // CHECK-NEXT: %[[vbptr:.*]] = bitcast i8* %[[vbptr_off]] to [2 x i32]**
186 // CHECK-NEXT: store [2 x i32]* @"\01??_8D@constructors@@7B@", [2 x i32]** %[[vbptr]]
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000187 // CHECK-NEXT: bitcast %"struct.constructors::D"* %{{.*}} to %"struct.constructors::A"*
188 // CHECK-NEXT: call x86_thiscallcc %"struct.constructors::A"* @"\01??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}})
189 // CHECK-NEXT: br label %[[SKIP_VBASES]]
190 //
191 // CHECK: [[SKIP_VBASES]]
192 // CHECK: call x86_thiscallcc %"struct.constructors::C"* @"\01??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* %{{.*}}, i32 0)
193 // CHECK: ret
194}
195
196struct E : virtual C {
197 E();
198};
199
200E::E() {
Stephen Lin3b50e8d2013-06-30 20:40:16 +0000201 // CHECK: define x86_thiscallcc %"struct.constructors::E"* @"\01??0E@constructors@@QAE@XZ"(%"struct.constructors::E"* returned %this, i32 %is_most_derived) unnamed_addr
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000202 // CHECK: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4
203 // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32* %[[IS_MOST_DERIVED_VAR]]
204 // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
205 // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
206 //
207 // CHECK: [[INIT_VBASES]]
Reid Kleckner90633022013-06-19 15:20:38 +0000208 // CHECK-NEXT: %[[this_i8:.*]] = bitcast %"struct.constructors::E"* %{{.*}} to i8*
209 // CHECK-NEXT: %[[offs:.*]] = getelementptr inbounds i8* %[[this_i8]], i64 0
210 // CHECK-NEXT: %[[vbptr_E:.*]] = bitcast i8* %[[offs]] to [3 x i32]**
211 // CHECK-NEXT: store [3 x i32]* @"\01??_8E@constructors@@7B01@@", [3 x i32]** %[[vbptr_E]]
212 // CHECK-NEXT: %[[offs:.*]] = getelementptr inbounds i8* %[[this_i8]], i64 4
213 // CHECK-NEXT: %[[vbptr_C:.*]] = bitcast i8* %[[offs]] to [2 x i32]**
214 // CHECK-NEXT: store [2 x i32]* @"\01??_8E@constructors@@7BC@1@@", [2 x i32]** %[[vbptr_C]]
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000215 // CHECK-NEXT: bitcast %"struct.constructors::E"* %{{.*}} to %"struct.constructors::A"*
216 // CHECK-NEXT: call x86_thiscallcc %"struct.constructors::A"* @"\01??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}})
217 // CHECK: call x86_thiscallcc %"struct.constructors::C"* @"\01??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* %{{.*}}, i32 0)
218 // CHECK-NEXT: br label %[[SKIP_VBASES]]
219 //
220 // CHECK: [[SKIP_VBASES]]
221 // CHECK: ret
222}
223
224} // end namespace constructors
Reid Klecknera4130ba2013-07-22 13:51:44 +0000225
226namespace dtors {
227
228struct A {
229 ~A();
230};
231
232void call_nv_complete(A *a) {
233 a->~A();
234// CHECK: define void @"\01?call_nv_complete@dtors@@YAXPAUA@1@@Z"
235// CHECK: call x86_thiscallcc void @"\01??1A@dtors@@QAE@XZ"
236// CHECK: ret
237}
238
239// CHECK: declare x86_thiscallcc void @"\01??1A@dtors@@QAE@XZ"
240
241// Now try some virtual bases, where we need the complete dtor.
242
243struct B : virtual A { ~B(); };
244struct C : virtual A { ~C(); };
245struct D : B, C { ~D(); };
246
247void call_vbase_complete(D *d) {
248 d->~D();
249// CHECK: define void @"\01?call_vbase_complete@dtors@@YAXPAUD@1@@Z"
250// CHECK: call x86_thiscallcc void @"\01??_DD@dtors@@QAE@XZ"(%"struct.dtors::D"* %{{[^,]+}})
251// CHECK: ret
252}
253
254// The complete dtor should call the base dtors for D and the vbase A (once).
255// CHECK: define linkonce_odr x86_thiscallcc void @"\01??_DD@dtors@@QAE@XZ"
256// CHECK-NOT: call
257// CHECK: call x86_thiscallcc void @"\01??1D@dtors@@QAE@XZ"
258// CHECK-NOT: call
259// CHECK: call x86_thiscallcc void @"\01??1A@dtors@@QAE@XZ"
260// CHECK-NOT: call
261// CHECK: ret
262
263void destroy_d_complete() {
264 D d;
265// CHECK: define void @"\01?destroy_d_complete@dtors@@YAXXZ"
266// CHECK: call x86_thiscallcc void @"\01??_DD@dtors@@QAE@XZ"(%"struct.dtors::D"* %{{[^,]+}})
267// CHECK: ret
268}
269
270// FIXME: Clang manually inlines the deletion, so we don't get a call to the
271// deleting dtor (_G). The only way to call deleting dtors currently is through
272// a vftable.
273void call_nv_deleting_dtor(D *d) {
274 delete d;
275// CHECK: define void @"\01?call_nv_deleting_dtor@dtors@@YAXPAUD@1@@Z"
276// CHECK: call x86_thiscallcc void @"\01??_DD@dtors@@QAE@XZ"(%"struct.dtors::D"* %{{[^,]+}})
277// CHECK: call void @"\01??3@YAXPAX@Z"
278// CHECK: ret
279}
280
281}