blob: f9d65f44f27780032911e6950324f7d687bd582b [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 Iskhodzhanov4b104062013-08-26 10:32:04 +00006//
7// RUN: %clang_cc1 -emit-llvm %s -o - -cxx-abi microsoft -triple=x86_64-pc-win32 -fno-rtti | FileCheck --check-prefix DTORS-X64 %s
Timur Iskhodzhanov85607912012-04-20 08:05:00 +00008
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +00009namespace basic {
10
Timur Iskhodzhanov85607912012-04-20 08:05:00 +000011class A {
12 public:
13 A() { }
Reid Klecknera4130ba2013-07-22 13:51:44 +000014 ~A();
Timur Iskhodzhanov85607912012-04-20 08:05:00 +000015};
16
Timur Iskhodzhanov6c9dccd2013-02-12 13:22:47 +000017void no_constructor_destructor_infinite_recursion() {
Timur Iskhodzhanov85607912012-04-20 08:05:00 +000018 A a;
19
Stephen Lin3b50e8d2013-06-30 20:40:16 +000020// 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 +000021// CHECK: [[THIS_ADDR:%[.0-9A-Z_a-z]+]] = alloca %"class.basic::A"*, align 4
22// CHECK-NEXT: store %"class.basic::A"* %this, %"class.basic::A"** [[THIS_ADDR]], align 4
23// CHECK-NEXT: [[T1:%[.0-9A-Z_a-z]+]] = load %"class.basic::A"** [[THIS_ADDR]]
24// CHECK-NEXT: ret %"class.basic::A"* [[T1]]
John McCallbd315742012-09-25 08:00:39 +000025// CHECK-NEXT: }
Reid Klecknera4130ba2013-07-22 13:51:44 +000026}
Timur Iskhodzhanov85607912012-04-20 08:05:00 +000027
Reid Klecknera4130ba2013-07-22 13:51:44 +000028A::~A() {
Timur Iskhodzhanov85607912012-04-20 08:05:00 +000029// Make sure that the destructor doesn't call itself:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000030// CHECK: define {{.*}} @"\01??1A@basic@@QAE@XZ"
31// CHECK-NOT: call void @"\01??1A@basic@@QAE@XZ"
Timur Iskhodzhanov85607912012-04-20 08:05:00 +000032// CHECK: ret
33}
John McCallbd315742012-09-25 08:00:39 +000034
Timur Iskhodzhanov649c7312013-01-21 13:02:41 +000035struct B {
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000036 B();
37};
38
39// Tests that we can define constructors outside the class (PR12784).
40B::B() {
Stephen Lin3b50e8d2013-06-30 20:40:16 +000041 // CHECK: define x86_thiscallcc %"struct.basic::B"* @"\01??0B@basic@@QAE@XZ"(%"struct.basic::B"* returned %this)
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000042 // CHECK: ret
43}
44
45struct C {
46 virtual ~C() {
Timur Iskhodzhanov1f71f392013-08-27 10:38:19 +000047// DTORS: define linkonce_odr x86_thiscallcc void @"\01??_GC@basic@@UAEPAXI@Z"(%"struct.basic::C"* %this, i32 %should_call_delete)
48// DTORS: store i32 %should_call_delete, i32* %[[SHOULD_DELETE_VAR:[0-9a-z._]+]], align 4
49// DTORS: %[[SHOULD_DELETE_VALUE:[0-9a-z._]+]] = load i32* %[[SHOULD_DELETE_VAR]]
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000050// DTORS: call x86_thiscallcc void @"\01??1C@basic@@UAE@XZ"(%"struct.basic::C"* %[[THIS:[0-9a-z]+]])
Timur Iskhodzhanov1f71f392013-08-27 10:38:19 +000051// DTORS-NEXT: %[[CONDITION:[0-9]+]] = icmp eq i32 %[[SHOULD_DELETE_VALUE]], 0
Timur Iskhodzhanov47453452013-02-13 12:14:25 +000052// DTORS-NEXT: br i1 %[[CONDITION]], label %[[CONTINUE_LABEL:[0-9a-z._]+]], label %[[CALL_DELETE_LABEL:[0-9a-z._]+]]
53//
54// DTORS: [[CALL_DELETE_LABEL]]
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000055// DTORS-NEXT: %[[THIS_AS_VOID:[0-9a-z]+]] = bitcast %"struct.basic::C"* %[[THIS]] to i8*
Reid Klecknera4130ba2013-07-22 13:51:44 +000056// DTORS-NEXT: call void @"\01??3@YAXPAX@Z"(i8* %[[THIS_AS_VOID]])
Timur Iskhodzhanov47453452013-02-13 12:14:25 +000057// DTORS-NEXT: br label %[[CONTINUE_LABEL]]
58//
59// DTORS: [[CONTINUE_LABEL]]
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000060// DTORS-NEXT: ret void
Timur Iskhodzhanov4b104062013-08-26 10:32:04 +000061
62// Check that we do the mangling correctly on x64.
63// DTORS-X64: @"\01??_GC@basic@@UEAAPEAXI@Z"
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000064 }
Timur Iskhodzhanov649c7312013-01-21 13:02:41 +000065 virtual void foo();
66};
67
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000068// Emits the vftable in the output.
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000069void C::foo() {}
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000070
Timur Iskhodzhanov649c7312013-01-21 13:02:41 +000071void check_vftable_offset() {
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000072 C c;
Timur Iskhodzhanov649c7312013-01-21 13:02:41 +000073// The vftable pointer should point at the beginning of the vftable.
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000074// CHECK: [[THIS_PTR:%[0-9]+]] = bitcast %"struct.basic::C"* {{.*}} to i8***
75// 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 +000076}
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000077
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000078void call_complete_dtor(C *obj_ptr) {
79// CHECK: define void @"\01?call_complete_dtor@basic@@YAXPAUC@1@@Z"(%"struct.basic::C"* %obj_ptr)
80 obj_ptr->~C();
81// CHECK: %[[OBJ_PTR_VALUE:.*]] = load %"struct.basic::C"** %{{.*}}, align 4
Timur Iskhodzhanov1f71f392013-08-27 10:38:19 +000082// CHECK-NEXT: %[[PVTABLE:.*]] = bitcast %"struct.basic::C"* %[[OBJ_PTR_VALUE]] to void (%"struct.basic::C"*, i32)***
83// CHECK-NEXT: %[[VTABLE:.*]] = load void (%"struct.basic::C"*, i32)*** %[[PVTABLE]]
84// CHECK-NEXT: %[[PVDTOR:.*]] = getelementptr inbounds void (%"struct.basic::C"*, i32)** %[[VTABLE]], i64 0
85// CHECK-NEXT: %[[VDTOR:.*]] = load void (%"struct.basic::C"*, i32)** %[[PVDTOR]]
86// CHECK-NEXT: call x86_thiscallcc void %[[VDTOR]](%"struct.basic::C"* %[[OBJ_PTR_VALUE]], i32 0)
Timur Iskhodzhanov0f9827f2013-02-15 14:45:22 +000087// CHECK-NEXT: ret void
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000088}
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000089
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000090void call_deleting_dtor(C *obj_ptr) {
91// CHECK: define void @"\01?call_deleting_dtor@basic@@YAXPAUC@1@@Z"(%"struct.basic::C"* %obj_ptr)
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +000092 delete obj_ptr;
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +000093// CHECK: %[[OBJ_PTR_VALUE:.*]] = load %"struct.basic::C"** %{{.*}}, align 4
94// CHECK: br i1 {{.*}}, label %[[DELETE_NULL:.*]], label %[[DELETE_NOTNULL:.*]]
95
96// CHECK: [[DELETE_NOTNULL]]
Timur Iskhodzhanov1f71f392013-08-27 10:38:19 +000097// CHECK-NEXT: %[[PVTABLE:.*]] = bitcast %"struct.basic::C"* %[[OBJ_PTR_VALUE]] to void (%"struct.basic::C"*, i32)***
98// CHECK-NEXT: %[[VTABLE:.*]] = load void (%"struct.basic::C"*, i32)*** %[[PVTABLE]]
99// CHECK-NEXT: %[[PVDTOR:.*]] = getelementptr inbounds void (%"struct.basic::C"*, i32)** %[[VTABLE]], i64 0
100// CHECK-NEXT: %[[VDTOR:.*]] = load void (%"struct.basic::C"*, i32)** %[[PVDTOR]]
101// CHECK-NEXT: call x86_thiscallcc void %[[VDTOR]](%"struct.basic::C"* %[[OBJ_PTR_VALUE]], i32 1)
Timur Iskhodzhanov0f9827f2013-02-15 14:45:22 +0000102// CHECK: ret void
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000103}
104
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000105struct D {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000106 static int foo();
107
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000108 D() {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000109 static int ctor_static = foo();
110 // CHECK that the static in the ctor gets mangled correctly:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000111 // CHECK: @"\01?ctor_static@?1???0D@basic@@QAE@XZ@4HA"
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000112 }
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000113 ~D() {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000114 static int dtor_static = foo();
115 // CHECK that the static in the dtor gets mangled correctly:
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000116 // CHECK: @"\01?dtor_static@?1???1D@basic@@QAE@XZ@4HA"
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000117 }
118};
119
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000120void use_D() { D c; }
Bill Wendling4e1125f2013-02-22 09:10:20 +0000121
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000122} // end namespace basic
123
124
125namespace constructors {
126
127struct A {
128 A() {}
129};
130
131struct B : A {
132 B();
133 ~B();
134};
135
136B::B() {
Stephen Lin3b50e8d2013-06-30 20:40:16 +0000137 // CHECK: define x86_thiscallcc %"struct.constructors::B"* @"\01??0B@constructors@@QAE@XZ"(%"struct.constructors::B"* returned %this)
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000138 // CHECK: call x86_thiscallcc %"struct.constructors::A"* @"\01??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}})
139 // CHECK: ret
140}
141
142struct C : virtual A {
143 C();
144};
145
146C::C() {
Stephen Lin3b50e8d2013-06-30 20:40:16 +0000147 // 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 +0000148 // TODO: make sure this works in the Release build too;
149 // CHECK: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4
150 // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32* %[[IS_MOST_DERIVED_VAR]]
151 // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
152 // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
153 //
154 // CHECK: [[INIT_VBASES]]
Reid Kleckner90633022013-06-19 15:20:38 +0000155 // CHECK-NEXT: %[[this_i8:.*]] = bitcast %"struct.constructors::C"* %{{.*}} to i8*
156 // CHECK-NEXT: %[[vbptr_off:.*]] = getelementptr inbounds i8* %[[this_i8]], i64 0
157 // CHECK-NEXT: %[[vbptr:.*]] = bitcast i8* %[[vbptr_off]] to [2 x i32]**
158 // CHECK-NEXT: store [2 x i32]* @"\01??_8C@constructors@@7B@", [2 x i32]** %[[vbptr]]
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000159 // CHECK-NEXT: bitcast %"struct.constructors::C"* %{{.*}} to %"struct.constructors::A"*
160 // CHECK-NEXT: call x86_thiscallcc %"struct.constructors::A"* @"\01??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}})
161 // CHECK-NEXT: br label %[[SKIP_VBASES]]
162 //
163 // CHECK: [[SKIP_VBASES]]
164 // CHECK: @"\01??_7C@constructors@@6B@"
165 // CHECK: ret
166}
167
168void create_C() {
169 C c;
170 // CHECK: define void @"\01?create_C@constructors@@YAXXZ"()
171 // CHECK: call x86_thiscallcc %"struct.constructors::C"* @"\01??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* %c, i32 1)
172 // CHECK: ret
173}
174
175struct D : C {
176 D();
177};
178
179D::D() {
Stephen Lin3b50e8d2013-06-30 20:40:16 +0000180 // 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 +0000181 // CHECK: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4
182 // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32* %[[IS_MOST_DERIVED_VAR]]
183 // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
184 // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
185 //
186 // CHECK: [[INIT_VBASES]]
Reid Kleckner90633022013-06-19 15:20:38 +0000187 // CHECK-NEXT: %[[this_i8:.*]] = bitcast %"struct.constructors::D"* %{{.*}} to i8*
188 // CHECK-NEXT: %[[vbptr_off:.*]] = getelementptr inbounds i8* %[[this_i8]], i64 0
189 // CHECK-NEXT: %[[vbptr:.*]] = bitcast i8* %[[vbptr_off]] to [2 x i32]**
190 // CHECK-NEXT: store [2 x i32]* @"\01??_8D@constructors@@7B@", [2 x i32]** %[[vbptr]]
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000191 // CHECK-NEXT: bitcast %"struct.constructors::D"* %{{.*}} to %"struct.constructors::A"*
192 // CHECK-NEXT: call x86_thiscallcc %"struct.constructors::A"* @"\01??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}})
193 // CHECK-NEXT: br label %[[SKIP_VBASES]]
194 //
195 // CHECK: [[SKIP_VBASES]]
196 // CHECK: call x86_thiscallcc %"struct.constructors::C"* @"\01??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* %{{.*}}, i32 0)
197 // CHECK: ret
198}
199
200struct E : virtual C {
201 E();
202};
203
204E::E() {
Stephen Lin3b50e8d2013-06-30 20:40:16 +0000205 // 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 +0000206 // CHECK: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4
207 // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32* %[[IS_MOST_DERIVED_VAR]]
208 // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
209 // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
210 //
211 // CHECK: [[INIT_VBASES]]
Reid Kleckner90633022013-06-19 15:20:38 +0000212 // CHECK-NEXT: %[[this_i8:.*]] = bitcast %"struct.constructors::E"* %{{.*}} to i8*
213 // CHECK-NEXT: %[[offs:.*]] = getelementptr inbounds i8* %[[this_i8]], i64 0
214 // CHECK-NEXT: %[[vbptr_E:.*]] = bitcast i8* %[[offs]] to [3 x i32]**
215 // CHECK-NEXT: store [3 x i32]* @"\01??_8E@constructors@@7B01@@", [3 x i32]** %[[vbptr_E]]
216 // CHECK-NEXT: %[[offs:.*]] = getelementptr inbounds i8* %[[this_i8]], i64 4
217 // CHECK-NEXT: %[[vbptr_C:.*]] = bitcast i8* %[[offs]] to [2 x i32]**
218 // CHECK-NEXT: store [2 x i32]* @"\01??_8E@constructors@@7BC@1@@", [2 x i32]** %[[vbptr_C]]
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000219 // CHECK-NEXT: bitcast %"struct.constructors::E"* %{{.*}} to %"struct.constructors::A"*
220 // CHECK-NEXT: call x86_thiscallcc %"struct.constructors::A"* @"\01??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}})
221 // CHECK: call x86_thiscallcc %"struct.constructors::C"* @"\01??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* %{{.*}}, i32 0)
222 // CHECK-NEXT: br label %[[SKIP_VBASES]]
223 //
224 // CHECK: [[SKIP_VBASES]]
225 // CHECK: ret
226}
227
Timur Iskhodzhanovbb1b7972013-08-04 17:30:04 +0000228// PR16735 - even abstract classes should have a constructor emitted.
229struct F {
230 F();
231 virtual void f() = 0;
232};
233
234F::F() {}
235// CHECK: define x86_thiscallcc %"struct.constructors::F"* @"\01??0F@constructors@@QAE@XZ"
236
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000237} // end namespace constructors
Reid Klecknera4130ba2013-07-22 13:51:44 +0000238
239namespace dtors {
240
241struct A {
242 ~A();
243};
244
245void call_nv_complete(A *a) {
246 a->~A();
247// CHECK: define void @"\01?call_nv_complete@dtors@@YAXPAUA@1@@Z"
248// CHECK: call x86_thiscallcc void @"\01??1A@dtors@@QAE@XZ"
249// CHECK: ret
250}
251
252// CHECK: declare x86_thiscallcc void @"\01??1A@dtors@@QAE@XZ"
253
254// Now try some virtual bases, where we need the complete dtor.
255
256struct B : virtual A { ~B(); };
257struct C : virtual A { ~C(); };
258struct D : B, C { ~D(); };
259
260void call_vbase_complete(D *d) {
261 d->~D();
262// CHECK: define void @"\01?call_vbase_complete@dtors@@YAXPAUD@1@@Z"
263// CHECK: call x86_thiscallcc void @"\01??_DD@dtors@@QAE@XZ"(%"struct.dtors::D"* %{{[^,]+}})
264// CHECK: ret
265}
266
267// The complete dtor should call the base dtors for D and the vbase A (once).
268// CHECK: define linkonce_odr x86_thiscallcc void @"\01??_DD@dtors@@QAE@XZ"
269// CHECK-NOT: call
270// CHECK: call x86_thiscallcc void @"\01??1D@dtors@@QAE@XZ"
271// CHECK-NOT: call
272// CHECK: call x86_thiscallcc void @"\01??1A@dtors@@QAE@XZ"
273// CHECK-NOT: call
274// CHECK: ret
275
276void destroy_d_complete() {
277 D d;
278// CHECK: define void @"\01?destroy_d_complete@dtors@@YAXXZ"
279// CHECK: call x86_thiscallcc void @"\01??_DD@dtors@@QAE@XZ"(%"struct.dtors::D"* %{{[^,]+}})
280// CHECK: ret
281}
282
283// FIXME: Clang manually inlines the deletion, so we don't get a call to the
284// deleting dtor (_G). The only way to call deleting dtors currently is through
285// a vftable.
286void call_nv_deleting_dtor(D *d) {
287 delete d;
288// CHECK: define void @"\01?call_nv_deleting_dtor@dtors@@YAXPAUD@1@@Z"
289// CHECK: call x86_thiscallcc void @"\01??_DD@dtors@@QAE@XZ"(%"struct.dtors::D"* %{{[^,]+}})
290// CHECK: call void @"\01??3@YAXPAX@Z"
291// CHECK: ret
292}
293
294}