1 // RUN: %clang_cc1 -emit-llvm -fno-rtti %s -std=c++11 -o - -mconstructor-aliases -triple=i386-pc-win32 -fno-rtti > %t
2 // RUN: FileCheck %s < %t
3 // 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
6 // RUN: FileCheck --check-prefix DTORS2 %s < %t
7 // RUN: FileCheck --check-prefix DTORS3 %s < %t
8 // RUN: FileCheck --check-prefix DTORS4 %s < %t
9 //
10 // RUN: %clang_cc1 -emit-llvm %s -o - -mconstructor-aliases -triple=x86_64-pc-win32 -fno-rtti -std=c++11 | FileCheck --check-prefix DTORS-X64 %s
11 
12 namespace basic {
13 
14 class A {
15  public:
A()16   A() { }
17   ~A();
18 };
19 
no_constructor_destructor_infinite_recursion()20 void no_constructor_destructor_infinite_recursion() {
21   A a;
22 
23 // CHECK:      define linkonce_odr dso_local x86_thiscallcc %"class.basic::A"* @"??0A@basic@@QAE@XZ"(%"class.basic::A"* {{[^,]*}} returned {{[^,]*}} %this) {{.*}} comdat {{.*}} {
24 // CHECK:        [[THIS_ADDR:%[.0-9A-Z_a-z]+]] = alloca %"class.basic::A"*, align 4
25 // CHECK-NEXT:   store %"class.basic::A"* %this, %"class.basic::A"** [[THIS_ADDR]], align 4
26 // CHECK-NEXT:   [[T1:%[.0-9A-Z_a-z]+]] = load %"class.basic::A"*, %"class.basic::A"** [[THIS_ADDR]]
27 // CHECK-NEXT:   ret %"class.basic::A"* [[T1]]
28 // CHECK-NEXT: }
29 }
30 
~A()31 A::~A() {
32 // Make sure that the destructor doesn't call itself:
33 // CHECK: define {{.*}} @"??1A@basic@@QAE@XZ"
34 // CHECK-NOT: call void @"??1A@basic@@QAE@XZ"
35 // CHECK: ret
36 }
37 
38 struct B {
39   B();
40 };
41 
42 // Tests that we can define constructors outside the class (PR12784).
B()43 B::B() {
44   // CHECK: define dso_local x86_thiscallcc %"struct.basic::B"* @"??0B@basic@@QAE@XZ"(%"struct.basic::B"* {{[^,]*}} returned {{[^,]*}} %this)
45   // CHECK: ret
46 }
47 
48 struct C {
~Cbasic::C49   virtual ~C() {
50 // DTORS:      define linkonce_odr dso_local x86_thiscallcc i8* @"??_GC@basic@@UAEPAXI@Z"(%"struct.basic::C"* {{[^,]*}} %this, i32 %should_call_delete) {{.*}} comdat {{.*}} {
51 // DTORS:        store i32 %should_call_delete, i32* %[[SHOULD_DELETE_VAR:[0-9a-z._]+]], align 4
52 // DTORS:        store i8* %{{.*}}, i8** %[[RETVAL:[0-9a-z._]+]]
53 // DTORS:        %[[SHOULD_DELETE_VALUE:[0-9a-z._]+]] = load i32, i32* %[[SHOULD_DELETE_VAR]]
54 // DTORS:        call x86_thiscallcc void @"??1C@basic@@UAE@XZ"(%"struct.basic::C"* {{[^,]*}} %[[THIS:[0-9a-z]+]])
55 // DTORS-NEXT:   %[[CONDITION:[0-9]+]] = icmp eq i32 %[[SHOULD_DELETE_VALUE]], 0
56 // DTORS-NEXT:   br i1 %[[CONDITION]], label %[[CONTINUE_LABEL:[0-9a-z._]+]], label %[[CALL_DELETE_LABEL:[0-9a-z._]+]]
57 //
58 // DTORS:      [[CALL_DELETE_LABEL]]
59 // DTORS-NEXT:   %[[THIS_AS_VOID:[0-9a-z]+]] = bitcast %"struct.basic::C"* %[[THIS]] to i8*
60 // DTORS-NEXT:   call void @"??3@YAXPAX@Z"(i8* %[[THIS_AS_VOID]])
61 // DTORS-NEXT:   br label %[[CONTINUE_LABEL]]
62 //
63 // DTORS:      [[CONTINUE_LABEL]]
64 // DTORS-NEXT:   %[[RET:.*]] = load i8*, i8** %[[RETVAL]]
65 // DTORS-NEXT:   ret i8* %[[RET]]
66 
67 // Check that we do the mangling correctly on x64.
68 // DTORS-X64:  @"??_GC@basic@@UEAAPEAXI@Z"
69   }
70   virtual void foo();
71 };
72 
73 // Emits the vftable in the output.
foo()74 void C::foo() {}
75 
check_vftable_offset()76 void check_vftable_offset() {
77   C c;
78 // The vftable pointer should point at the beginning of the vftable.
79 // CHECK: [[THIS_PTR:%[0-9]+]] = bitcast %"struct.basic::C"* {{.*}} to i32 (...)***
80 // CHECK: store i32 (...)** bitcast ({ [2 x i8*] }* @"??_7C@basic@@6B@" to i32 (...)**), i32 (...)*** [[THIS_PTR]]
81 }
82 
call_complete_dtor(C * obj_ptr)83 void call_complete_dtor(C *obj_ptr) {
84 // CHECK: define dso_local void @"?call_complete_dtor@basic@@YAXPAUC@1@@Z"(%"struct.basic::C"* %obj_ptr)
85   obj_ptr->~C();
86 // CHECK: %[[OBJ_PTR_VALUE:.*]] = load %"struct.basic::C"*, %"struct.basic::C"** %{{.*}}, align 4
87 // CHECK-NEXT: %[[PVTABLE:.*]] = bitcast %"struct.basic::C"* %[[OBJ_PTR_VALUE]] to i8* (%"struct.basic::C"*, i32)***
88 // CHECK-NEXT: %[[VTABLE:.*]] = load i8* (%"struct.basic::C"*, i32)**, i8* (%"struct.basic::C"*, i32)*** %[[PVTABLE]]
89 // CHECK-NEXT: %[[PVDTOR:.*]] = getelementptr inbounds i8* (%"struct.basic::C"*, i32)*, i8* (%"struct.basic::C"*, i32)** %[[VTABLE]], i64 0
90 // CHECK-NEXT: %[[VDTOR:.*]] = load i8* (%"struct.basic::C"*, i32)*, i8* (%"struct.basic::C"*, i32)** %[[PVDTOR]]
91 // CHECK-NEXT: call x86_thiscallcc i8* %[[VDTOR]](%"struct.basic::C"* {{[^,]*}} %[[OBJ_PTR_VALUE]], i32 0)
92 // CHECK-NEXT: ret void
93 }
94 
call_deleting_dtor(C * obj_ptr)95 void call_deleting_dtor(C *obj_ptr) {
96 // CHECK: define dso_local void @"?call_deleting_dtor@basic@@YAXPAUC@1@@Z"(%"struct.basic::C"* %obj_ptr)
97   delete obj_ptr;
98 // CHECK:      %[[OBJ_PTR_VALUE:.*]] = load %"struct.basic::C"*, %"struct.basic::C"** %{{.*}}, align 4
99 // CHECK:      br i1 {{.*}}, label %[[DELETE_NULL:.*]], label %[[DELETE_NOTNULL:.*]]
100 
101 // CHECK:      [[DELETE_NOTNULL]]
102 // CHECK-NEXT:   %[[PVTABLE:.*]] = bitcast %"struct.basic::C"* %[[OBJ_PTR_VALUE]] to i8* (%"struct.basic::C"*, i32)***
103 // CHECK-NEXT:   %[[VTABLE:.*]] = load i8* (%"struct.basic::C"*, i32)**, i8* (%"struct.basic::C"*, i32)*** %[[PVTABLE]]
104 // CHECK-NEXT:   %[[PVDTOR:.*]] = getelementptr inbounds i8* (%"struct.basic::C"*, i32)*, i8* (%"struct.basic::C"*, i32)** %[[VTABLE]], i64 0
105 // CHECK-NEXT:   %[[VDTOR:.*]] = load i8* (%"struct.basic::C"*, i32)*, i8* (%"struct.basic::C"*, i32)** %[[PVDTOR]]
106 // CHECK-NEXT:   call x86_thiscallcc i8* %[[VDTOR]](%"struct.basic::C"* {{[^,]*}} %[[OBJ_PTR_VALUE]], i32 1)
107 // CHECK:      ret void
108 }
109 
call_deleting_dtor_and_global_delete(C * obj_ptr)110 void call_deleting_dtor_and_global_delete(C *obj_ptr) {
111 // CHECK: define dso_local void @"?call_deleting_dtor_and_global_delete@basic@@YAXPAUC@1@@Z"(%"struct.basic::C"* %obj_ptr)
112   ::delete obj_ptr;
113 // CHECK:      %[[OBJ_PTR_VALUE:.*]] = load %"struct.basic::C"*, %"struct.basic::C"** %{{.*}}, align 4
114 // CHECK:      br i1 {{.*}}, label %[[DELETE_NULL:.*]], label %[[DELETE_NOTNULL:.*]]
115 
116 // CHECK:      [[DELETE_NOTNULL]]
117 // CHECK-NEXT:   %[[PVTABLE:.*]] = bitcast %"struct.basic::C"* %[[OBJ_PTR_VALUE]] to i8* (%"struct.basic::C"*, i32)***
118 // CHECK-NEXT:   %[[VTABLE:.*]] = load i8* (%"struct.basic::C"*, i32)**, i8* (%"struct.basic::C"*, i32)*** %[[PVTABLE]]
119 // CHECK-NEXT:   %[[PVDTOR:.*]] = getelementptr inbounds i8* (%"struct.basic::C"*, i32)*, i8* (%"struct.basic::C"*, i32)** %[[VTABLE]], i64 0
120 // CHECK-NEXT:   %[[VDTOR:.*]] = load i8* (%"struct.basic::C"*, i32)*, i8* (%"struct.basic::C"*, i32)** %[[PVDTOR]]
121 // CHECK-NEXT:   %[[CALL:.*]] = call x86_thiscallcc i8* %[[VDTOR]](%"struct.basic::C"* {{[^,]*}} %[[OBJ_PTR_VALUE]], i32 0)
122 // CHECK-NEXT:   call void @"??3@YAXPAX@Z"(i8* %[[CALL]])
123 // CHECK:      ret void
124 }
125 
126 struct D {
127   static int foo();
128 
Dbasic::D129   D() {
130     static int ctor_static = foo();
131     // CHECK that the static in the ctor gets mangled correctly:
132     // CHECK: @"?ctor_static@?1???0D@basic@@QAE@XZ@4HA"
133   }
~Dbasic::D134   ~D() {
135     static int dtor_static = foo();
136     // CHECK that the static in the dtor gets mangled correctly:
137     // CHECK: @"?dtor_static@?1???1D@basic@@QAE@XZ@4HA"
138   }
139 };
140 
use_D()141 void use_D() { D c; }
142 
143 } // end namespace basic
144 
145 namespace dtor_in_second_nvbase {
146 
147 struct A {
148   virtual void f();  // A needs vftable to be primary.
149 };
150 struct B {
151   virtual ~B();
152 };
153 struct C : A, B {
154   virtual ~C();
155 };
156 
~C()157 C::~C() {
158 // CHECK-LABEL: define dso_local x86_thiscallcc void @"??1C@dtor_in_second_nvbase@@UAE@XZ"
159 // CHECK:       (%"struct.dtor_in_second_nvbase::C"* {{[^,]*}} %this)
160 //      No this adjustment!
161 // CHECK-NOT: getelementptr
162 // CHECK:   load %"struct.dtor_in_second_nvbase::C"*, %"struct.dtor_in_second_nvbase::C"** %{{.*}}
163 //      Now we this-adjust before calling ~B.
164 // CHECK:   bitcast %"struct.dtor_in_second_nvbase::C"* %{{.*}} to i8*
165 // CHECK:   getelementptr inbounds i8, i8* %{{.*}}, i32 4
166 // CHECK:   bitcast i8* %{{.*}} to %"struct.dtor_in_second_nvbase::B"*
167 // CHECK:   call x86_thiscallcc void @"??1B@dtor_in_second_nvbase@@UAE@XZ"
168 // CHECK:       (%"struct.dtor_in_second_nvbase::B"* {{[^,]*}} %{{.*}})
169 // CHECK:   ret void
170 }
171 
foo()172 void foo() {
173   C c;
174 }
175 // DTORS2-LABEL: define linkonce_odr dso_local x86_thiscallcc i8* @"??_EC@dtor_in_second_nvbase@@W3AEPAXI@Z"
176 // DTORS2:       (%"struct.dtor_in_second_nvbase::C"* %this, i32 %should_call_delete)
177 //      Do an adjustment from B* to C*.
178 // DTORS2:   getelementptr i8, i8* %{{.*}}, i32 -4
179 // DTORS2:   bitcast i8* %{{.*}} to %"struct.dtor_in_second_nvbase::C"*
180 // DTORS2:   %[[CALL:.*]] = tail call x86_thiscallcc i8* @"??_GC@dtor_in_second_nvbase@@UAEPAXI@Z"
181 // DTORS2:   ret i8* %[[CALL]]
182 }
183 
184 namespace test2 {
185 // Just like dtor_in_second_nvbase, except put that in a vbase of a diamond.
186 
187 // C's dtor is in the non-primary base.
188 struct A { virtual void f(); };
189 struct B { virtual ~B(); };
190 struct C : A, B { virtual ~C(); int c; };
191 
192 // Diamond hierarchy, with C as the shared vbase.
193 struct D : virtual C { int d; };
194 struct E : virtual C { int e; };
195 struct F : D, E { ~F(); int f; };
196 
~F()197 F::~F() {
198 // CHECK-LABEL: define dso_local x86_thiscallcc void @"??1F@test2@@UAE@XZ"(%"struct.test2::F"*{{[^,]*}})
199 //      Do an adjustment from C vbase subobject to F as though F was the
200 //      complete type.
201 // CHECK:   getelementptr inbounds i8, i8* %{{.*}}, i32 -20
202 // CHECK:   bitcast i8* %{{.*}} to %"struct.test2::F"*
203 // CHECK:   store %"struct.test2::F"*
204 }
205 
foo()206 void foo() {
207   F f;
208 }
209 // DTORS3-LABEL: define linkonce_odr dso_local x86_thiscallcc void @"??_DF@test2@@QAEXXZ"({{.*}} {{.*}} comdat
210 //      Do an adjustment from C* to F*.
211 // DTORS3:   getelementptr i8, i8* %{{.*}}, i32 20
212 // DTORS3:   bitcast i8* %{{.*}} to %"struct.test2::F"*
213 // DTORS3:   call x86_thiscallcc void @"??1F@test2@@UAE@XZ"
214 // DTORS3:   ret void
215 
216 }
217 
218 namespace constructors {
219 
220 struct A {
Aconstructors::A221   A() {}
222 };
223 
224 struct B : A {
225   B();
226   ~B();
227 };
228 
B()229 B::B() {
230   // CHECK: define dso_local x86_thiscallcc %"struct.constructors::B"* @"??0B@constructors@@QAE@XZ"(%"struct.constructors::B"* {{[^,]*}} returned {{[^,]*}} %this)
231   // CHECK: call x86_thiscallcc %"struct.constructors::A"* @"??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* {{[^,]*}} %{{.*}})
232   // CHECK: ret
233 }
234 
235 struct C : virtual A {
236   C();
237 };
238 
C()239 C::C() {
240   // CHECK: define dso_local x86_thiscallcc %"struct.constructors::C"* @"??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* {{[^,]*}} returned {{[^,]*}} %this, i32 %is_most_derived)
241   // TODO: make sure this works in the Release build too;
242   // CHECK: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4
243   // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32, i32* %[[IS_MOST_DERIVED_VAR]]
244   // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
245   // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
246   //
247   // CHECK: [[INIT_VBASES]]
248   // CHECK-NEXT: %[[this_i8:.*]] = bitcast %"struct.constructors::C"* %{{.*}} to i8*
249   // CHECK-NEXT: %[[vbptr_off:.*]] = getelementptr inbounds i8, i8* %[[this_i8]], i32 0
250   // CHECK-NEXT: %[[vbptr:.*]] = bitcast i8* %[[vbptr_off]] to i32**
251   // CHECK-NEXT: store i32* getelementptr inbounds ([2 x i32], [2 x i32]* @"??_8C@constructors@@7B@", i32 0, i32 0), i32** %[[vbptr]]
252   // CHECK-NEXT: bitcast %"struct.constructors::C"* %{{.*}} to i8*
253   // CHECK-NEXT: getelementptr inbounds i8, i8* %{{.*}}, i32 4
254   // CHECK-NEXT: bitcast i8* %{{.*}} to %"struct.constructors::A"*
255   // CHECK-NEXT: call x86_thiscallcc %"struct.constructors::A"* @"??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* {{[^,]*}} %{{.*}})
256   // CHECK-NEXT: br label %[[SKIP_VBASES]]
257   //
258   // CHECK: [[SKIP_VBASES]]
259   // Class C does not define or override methods, so shouldn't change the vfptr.
260   // CHECK-NOT: @"??_7C@constructors@@6B@"
261   // CHECK: ret
262 }
263 
create_C()264 void create_C() {
265   C c;
266   // CHECK: define dso_local void @"?create_C@constructors@@YAXXZ"()
267   // CHECK: call x86_thiscallcc %"struct.constructors::C"* @"??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* {{[^,]*}} %c, i32 1)
268   // CHECK: ret
269 }
270 
271 struct D : C {
272   D();
273 };
274 
D()275 D::D() {
276   // CHECK: define dso_local x86_thiscallcc %"struct.constructors::D"* @"??0D@constructors@@QAE@XZ"(%"struct.constructors::D"* {{[^,]*}} returned {{[^,]*}} %this, i32 %is_most_derived) unnamed_addr
277   // CHECK: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4
278   // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32, i32* %[[IS_MOST_DERIVED_VAR]]
279   // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
280   // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
281   //
282   // CHECK: [[INIT_VBASES]]
283   // CHECK-NEXT: %[[this_i8:.*]] = bitcast %"struct.constructors::D"* %{{.*}} to i8*
284   // CHECK-NEXT: %[[vbptr_off:.*]] = getelementptr inbounds i8, i8* %[[this_i8]], i32 0
285   // CHECK-NEXT: %[[vbptr:.*]] = bitcast i8* %[[vbptr_off]] to i32**
286   // CHECK-NEXT: store i32* getelementptr inbounds ([2 x i32], [2 x i32]* @"??_8D@constructors@@7B@", i32 0, i32 0), i32** %[[vbptr]]
287   // CHECK-NEXT: bitcast %"struct.constructors::D"* %{{.*}} to i8*
288   // CHECK-NEXT: getelementptr inbounds i8, i8* %{{.*}}, i32 4
289   // CHECK-NEXT: bitcast i8* %{{.*}} to %"struct.constructors::A"*
290   // CHECK-NEXT: call x86_thiscallcc %"struct.constructors::A"* @"??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* {{[^,]*}} %{{.*}})
291   // CHECK-NEXT: br label %[[SKIP_VBASES]]
292   //
293   // CHECK: [[SKIP_VBASES]]
294   // CHECK: call x86_thiscallcc %"struct.constructors::C"* @"??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* {{[^,]*}} %{{.*}}, i32 0)
295   // CHECK: ret
296 }
297 
298 struct E : virtual C {
299   E();
300 };
301 
E()302 E::E() {
303   // CHECK: define dso_local x86_thiscallcc %"struct.constructors::E"* @"??0E@constructors@@QAE@XZ"(%"struct.constructors::E"* {{[^,]*}} returned {{[^,]*}} %this, i32 %is_most_derived) unnamed_addr
304   // CHECK: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4
305   // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32, i32* %[[IS_MOST_DERIVED_VAR]]
306   // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
307   // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
308   //
309   // CHECK: [[INIT_VBASES]]
310   // CHECK-NEXT: %[[this_i8:.*]] = bitcast %"struct.constructors::E"* %{{.*}} to i8*
311   // CHECK-NEXT: %[[offs:.*]] = getelementptr inbounds i8, i8* %[[this_i8]], i32 0
312   // CHECK-NEXT: %[[vbptr_E:.*]] = bitcast i8* %[[offs]] to i32**
313   // CHECK-NEXT: store i32* getelementptr inbounds ([3 x i32], [3 x i32]* @"??_8E@constructors@@7B01@@", i32 0, i32 0), i32** %[[vbptr_E]]
314   // CHECK-NEXT: %[[offs:.*]] = getelementptr inbounds i8, i8* %[[this_i8]], i32 4
315   // CHECK-NEXT: %[[vbptr_C:.*]] = bitcast i8* %[[offs]] to i32**
316   // CHECK-NEXT: store i32* getelementptr inbounds ([2 x i32], [2 x i32]* @"??_8E@constructors@@7BC@1@@", i32 0, i32 0), i32** %[[vbptr_C]]
317   // CHECK-NEXT: bitcast %"struct.constructors::E"* %{{.*}} to i8*
318   // CHECK-NEXT: getelementptr inbounds i8, i8* %{{.*}}, i32 4
319   // CHECK-NEXT: bitcast i8* %{{.*}} to %"struct.constructors::A"*
320   // CHECK-NEXT: call x86_thiscallcc %"struct.constructors::A"* @"??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* {{[^,]*}} %{{.*}})
321   // CHECK: call x86_thiscallcc %"struct.constructors::C"* @"??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* {{[^,]*}} %{{.*}}, i32 0)
322   // CHECK-NEXT: br label %[[SKIP_VBASES]]
323   //
324   // CHECK: [[SKIP_VBASES]]
325   // CHECK: ret
326 }
327 
328 // PR16735 - even abstract classes should have a constructor emitted.
329 struct F {
330   F();
331   virtual void f() = 0;
332 };
333 
F()334 F::F() {}
335 // CHECK: define dso_local x86_thiscallcc %"struct.constructors::F"* @"??0F@constructors@@QAE@XZ"
336 
337 } // end namespace constructors
338 
339 namespace dtors {
340 
341 struct A {
342   ~A();
343 };
344 
call_nv_complete(A * a)345 void call_nv_complete(A *a) {
346   a->~A();
347 // CHECK: define dso_local void @"?call_nv_complete@dtors@@YAXPAUA@1@@Z"
348 // CHECK: call x86_thiscallcc void @"??1A@dtors@@QAE@XZ"
349 // CHECK: ret
350 }
351 
352 // CHECK: declare dso_local x86_thiscallcc void @"??1A@dtors@@QAE@XZ"
353 
354 // Now try some virtual bases, where we need the complete dtor.
355 
356 struct B : virtual A { ~B(); };
357 struct C : virtual A { ~C(); };
358 struct D : B, C { ~D(); };
359 
call_vbase_complete(D * d)360 void call_vbase_complete(D *d) {
361   d->~D();
362 // CHECK: define dso_local void @"?call_vbase_complete@dtors@@YAXPAUD@1@@Z"
363 // CHECK: call x86_thiscallcc void @"??_DD@dtors@@QAEXXZ"(%"struct.dtors::D"* {{[^,]*}} %{{[^,]+}})
364 // CHECK: ret
365 }
366 
367 // The complete dtor should call the base dtors for D and the vbase A (once).
368 // CHECK: define linkonce_odr dso_local x86_thiscallcc void @"??_DD@dtors@@QAEXXZ"({{.*}}) {{.*}} comdat
369 // CHECK-NOT: call
370 // CHECK: call x86_thiscallcc void @"??1D@dtors@@QAE@XZ"
371 // CHECK-NOT: call
372 // CHECK: call x86_thiscallcc void @"??1A@dtors@@QAE@XZ"
373 // CHECK-NOT: call
374 // CHECK: ret
375 
destroy_d_complete()376 void destroy_d_complete() {
377   D d;
378 // CHECK: define dso_local void @"?destroy_d_complete@dtors@@YAXXZ"
379 // CHECK: call x86_thiscallcc void @"??_DD@dtors@@QAEXXZ"(%"struct.dtors::D"* {{[^,]*}} %{{[^,]+}})
380 // CHECK: ret
381 }
382 
383 // FIXME: Clang manually inlines the deletion, so we don't get a call to the
384 // deleting dtor (_G).  The only way to call deleting dtors currently is through
385 // a vftable.
call_nv_deleting_dtor(D * d)386 void call_nv_deleting_dtor(D *d) {
387   delete d;
388 // CHECK: define dso_local void @"?call_nv_deleting_dtor@dtors@@YAXPAUD@1@@Z"
389 // CHECK: call x86_thiscallcc void @"??_DD@dtors@@QAEXXZ"(%"struct.dtors::D"* {{[^,]*}} %{{[^,]+}})
390 // CHECK: call void @"??3@YAXPAX@Z"
391 // CHECK: ret
392 }
393 
394 }
395 
396 namespace test1 {
397 struct A { };
398 struct B : virtual A {
399   B(int *a);
400   B(const char *a, ...);
401   __cdecl B(short *a);
402 };
B(int * a)403 B::B(int *a) {}
B(const char * a,...)404 B::B(const char *a, ...) {}
B(short * a)405 B::B(short *a) {}
406 // CHECK: define dso_local x86_thiscallcc %"struct.test1::B"* @"??0B@test1@@QAE@PAH@Z"
407 // CHECK:               (%"struct.test1::B"* {{[^,]*}} returned {{[^,]*}} %this, i32* %a, i32 %is_most_derived)
408 // CHECK: define dso_local %"struct.test1::B"* @"??0B@test1@@QAA@PBDZZ"
409 // CHECK:               (%"struct.test1::B"* {{[^,]*}} returned {{[^,]*}} %this, i32 %is_most_derived, i8* %a, ...)
410 // CHECK: define dso_local x86_thiscallcc %"struct.test1::B"* @"??0B@test1@@QAE@PAF@Z"
411 // CHECK:               (%"struct.test1::B"* {{[^,]*}} returned {{[^,]*}} %this, i16* %a, i32 %is_most_derived)
412 
construct_b()413 void construct_b() {
414   int a;
415   B b1(&a);
416   B b2("%d %d", 1, 2);
417 }
418 // CHECK-LABEL: define dso_local void @"?construct_b@test1@@YAXXZ"()
419 // CHECK: call x86_thiscallcc %"struct.test1::B"* @"??0B@test1@@QAE@PAH@Z"
420 // CHECK:               (%"struct.test1::B"* {{.*}}, i32* {{.*}}, i32 1)
421 // CHECK: call %"struct.test1::B"* (%"struct.test1::B"*, i32, i8*, ...) @"??0B@test1@@QAA@PBDZZ"
422 // CHECK:               (%"struct.test1::B"* {{.*}}, i32 1, i8* {{.*}}, i32 1, i32 2)
423 }
424 
425 namespace implicit_copy_vtable {
426 // This was a crash that only reproduced in ABIs without key functions.
427 struct ImplicitCopy {
428   // implicit copy ctor
429   virtual ~ImplicitCopy();
430 };
CreateCopy(ImplicitCopy * a)431 void CreateCopy(ImplicitCopy *a) {
432   new ImplicitCopy(*a);
433 }
434 // CHECK: store {{.*}} @"??_7ImplicitCopy@implicit_copy_vtable@@6B@"
435 
436 struct MoveOnly {
437   MoveOnly(MoveOnly &&o) = default;
438   virtual ~MoveOnly();
439 };
440 MoveOnly &&f();
g()441 void g() { new MoveOnly(f()); }
442 // CHECK: store {{.*}} @"??_7MoveOnly@implicit_copy_vtable@@6B@"
443 }
444 
445 namespace delegating_ctor {
446 struct Y {};
447 struct X : virtual Y {
448   X(int);
449   X();
450 };
X(int)451 X::X(int) : X() {}
452 }
453 // CHECK: define dso_local x86_thiscallcc %"struct.delegating_ctor::X"* @"??0X@delegating_ctor@@QAE@H@Z"(
454 // CHECK:  %[[is_most_derived_addr:.*]] = alloca i32, align 4
455 // CHECK:  store i32 %is_most_derived, i32* %[[is_most_derived_addr]]
456 // CHECK:  %[[is_most_derived:.*]] = load i32, i32* %[[is_most_derived_addr]]
457 // CHECK:  call x86_thiscallcc {{.*}}* @"??0X@delegating_ctor@@QAE@XZ"({{.*}} i32 %[[is_most_derived]])
458 
459 // Dtor thunks for classes in anonymous namespaces should be internal, not
460 // linkonce_odr.
461 namespace {
462 struct A {
~A__anon69b92c3d0111::A463   virtual ~A() { }
464 };
465 }
getA()466 void *getA() {
467   return (void*)new A();
468 }
469 // CHECK: define internal x86_thiscallcc i8* @"??_GA@?A0x{{[^@]*}}@@UAEPAXI@Z"
470 // CHECK:               (%"struct.(anonymous namespace)::A"* {{[^,]*}} %this, i32 %should_call_delete)
471 // CHECK: define internal x86_thiscallcc void @"??1A@?A0x{{[^@]*}}@@UAE@XZ"
472 // CHECK:               (%"struct.(anonymous namespace)::A"* {{[^,]*}} %this)
473 
474 // Check that we correctly transform __stdcall to __thiscall for ctors and
475 // dtors.
476 class G {
477  public:
G()478   __stdcall G() {};
479 // DTORS4: define linkonce_odr dso_local x86_thiscallcc %class.G* @"??0G@@QAE@XZ"
~G()480   __stdcall ~G() {};
481 // DTORS4: define linkonce_odr dso_local x86_thiscallcc void @"??1G@@QAE@XZ"
482 };
483 
testG()484 extern void testG() {
485   G g;
486 }
487