1 // RUN: %clang_cc1 -emit-llvm %s -o - -cxx-abi microsoft -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
7 namespace basic {
8
9 class A {
10 public:
A()11 A() { }
12 ~A();
13 };
14
no_constructor_destructor_infinite_recursion()15 void no_constructor_destructor_infinite_recursion() {
16 A a;
17
18 // CHECK: define linkonce_odr x86_thiscallcc %"class.basic::A"* @"\01??0A@basic@@QAE@XZ"(%"class.basic::A"* returned %this)
19 // 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]]
23 // CHECK-NEXT: }
24 }
25
~A()26 A::~A() {
27 // Make sure that the destructor doesn't call itself:
28 // CHECK: define {{.*}} @"\01??1A@basic@@QAE@XZ"
29 // CHECK-NOT: call void @"\01??1A@basic@@QAE@XZ"
30 // CHECK: ret
31 }
32
33 struct B {
34 B();
35 };
36
37 // Tests that we can define constructors outside the class (PR12784).
B()38 B::B() {
39 // CHECK: define x86_thiscallcc %"struct.basic::B"* @"\01??0B@basic@@QAE@XZ"(%"struct.basic::B"* returned %this)
40 // CHECK: ret
41 }
42
43 struct C {
~Cbasic::C44 virtual ~C() {
45 // DTORS: define linkonce_odr x86_thiscallcc void @"\01??_GC@basic@@UAEPAXI@Z"(%"struct.basic::C"* %this, i1 zeroext %should_call_delete)
46 // DTORS: %[[FROMBOOL:[0-9a-z]+]] = zext i1 %should_call_delete to i8
47 // 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]]
49 // DTORS: call x86_thiscallcc void @"\01??1C@basic@@UAE@XZ"(%"struct.basic::C"* %[[THIS:[0-9a-z]+]])
50 // 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]]
54 // DTORS-NEXT: %[[THIS_AS_VOID:[0-9a-z]+]] = bitcast %"struct.basic::C"* %[[THIS]] to i8*
55 // DTORS-NEXT: call void @"\01??3@YAXPAX@Z"(i8* %[[THIS_AS_VOID]])
56 // DTORS-NEXT: br label %[[CONTINUE_LABEL]]
57 //
58 // DTORS: [[CONTINUE_LABEL]]
59 // DTORS-NEXT: ret void
60 }
61 virtual void foo();
62 };
63
64 // Emits the vftable in the output.
foo()65 void C::foo() {}
66
check_vftable_offset()67 void check_vftable_offset() {
68 C c;
69 // The vftable pointer should point at the beginning of the vftable.
70 // 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]]
72 }
73
call_complete_dtor(C * obj_ptr)74 void 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)
83 // CHECK-NEXT: ret void
84 }
85
call_deleting_dtor(C * obj_ptr)86 void call_deleting_dtor(C *obj_ptr) {
87 // CHECK: define void @"\01?call_deleting_dtor@basic@@YAXPAUC@1@@Z"(%"struct.basic::C"* %obj_ptr)
88 delete obj_ptr;
89 // 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)
98 // CHECK: ret void
99 }
100
101 struct D {
102 static int foo();
103
Dbasic::D104 D() {
105 static int ctor_static = foo();
106 // CHECK that the static in the ctor gets mangled correctly:
107 // CHECK: @"\01?ctor_static@?1???0D@basic@@QAE@XZ@4HA"
108 }
~Dbasic::D109 ~D() {
110 static int dtor_static = foo();
111 // CHECK that the static in the dtor gets mangled correctly:
112 // CHECK: @"\01?dtor_static@?1???1D@basic@@QAE@XZ@4HA"
113 }
114 };
115
use_D()116 void use_D() { D c; }
117
118 } // end namespace basic
119
120
121 namespace constructors {
122
123 struct A {
Aconstructors::A124 A() {}
125 };
126
127 struct B : A {
128 B();
129 ~B();
130 };
131
B()132 B::B() {
133 // CHECK: define x86_thiscallcc %"struct.constructors::B"* @"\01??0B@constructors@@QAE@XZ"(%"struct.constructors::B"* returned %this)
134 // CHECK: call x86_thiscallcc %"struct.constructors::A"* @"\01??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}})
135 // CHECK: ret
136 }
137
138 struct C : virtual A {
139 C();
140 };
141
C()142 C::C() {
143 // CHECK: define x86_thiscallcc %"struct.constructors::C"* @"\01??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* returned %this, i32 %is_most_derived)
144 // 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]]
151 // 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]]
155 // 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
create_C()164 void 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
171 struct D : C {
172 D();
173 };
174
D()175 D::D() {
176 // CHECK: define x86_thiscallcc %"struct.constructors::D"* @"\01??0D@constructors@@QAE@XZ"(%"struct.constructors::D"* returned %this, i32 %is_most_derived) unnamed_addr
177 // 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]]
183 // 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]]
187 // 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
196 struct E : virtual C {
197 E();
198 };
199
E()200 E::E() {
201 // CHECK: define x86_thiscallcc %"struct.constructors::E"* @"\01??0E@constructors@@QAE@XZ"(%"struct.constructors::E"* returned %this, i32 %is_most_derived) unnamed_addr
202 // 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]]
208 // 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]]
215 // 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 // PR16735 - even abstract classes should have a constructor emitted.
225 struct F {
226 F();
227 virtual void f() = 0;
228 };
229
F()230 F::F() {}
231 // CHECK: define x86_thiscallcc %"struct.constructors::F"* @"\01??0F@constructors@@QAE@XZ"
232
233 } // end namespace constructors
234
235 namespace dtors {
236
237 struct A {
238 ~A();
239 };
240
call_nv_complete(A * a)241 void call_nv_complete(A *a) {
242 a->~A();
243 // CHECK: define void @"\01?call_nv_complete@dtors@@YAXPAUA@1@@Z"
244 // CHECK: call x86_thiscallcc void @"\01??1A@dtors@@QAE@XZ"
245 // CHECK: ret
246 }
247
248 // CHECK: declare x86_thiscallcc void @"\01??1A@dtors@@QAE@XZ"
249
250 // Now try some virtual bases, where we need the complete dtor.
251
252 struct B : virtual A { ~B(); };
253 struct C : virtual A { ~C(); };
254 struct D : B, C { ~D(); };
255
call_vbase_complete(D * d)256 void call_vbase_complete(D *d) {
257 d->~D();
258 // CHECK: define void @"\01?call_vbase_complete@dtors@@YAXPAUD@1@@Z"
259 // CHECK: call x86_thiscallcc void @"\01??_DD@dtors@@QAE@XZ"(%"struct.dtors::D"* %{{[^,]+}})
260 // CHECK: ret
261 }
262
263 // The complete dtor should call the base dtors for D and the vbase A (once).
264 // CHECK: define linkonce_odr x86_thiscallcc void @"\01??_DD@dtors@@QAE@XZ"
265 // CHECK-NOT: call
266 // CHECK: call x86_thiscallcc void @"\01??1D@dtors@@QAE@XZ"
267 // CHECK-NOT: call
268 // CHECK: call x86_thiscallcc void @"\01??1A@dtors@@QAE@XZ"
269 // CHECK-NOT: call
270 // CHECK: ret
271
destroy_d_complete()272 void destroy_d_complete() {
273 D d;
274 // CHECK: define void @"\01?destroy_d_complete@dtors@@YAXXZ"
275 // CHECK: call x86_thiscallcc void @"\01??_DD@dtors@@QAE@XZ"(%"struct.dtors::D"* %{{[^,]+}})
276 // CHECK: ret
277 }
278
279 // FIXME: Clang manually inlines the deletion, so we don't get a call to the
280 // deleting dtor (_G). The only way to call deleting dtors currently is through
281 // a vftable.
call_nv_deleting_dtor(D * d)282 void call_nv_deleting_dtor(D *d) {
283 delete d;
284 // CHECK: define void @"\01?call_nv_deleting_dtor@dtors@@YAXPAUD@1@@Z"
285 // CHECK: call x86_thiscallcc void @"\01??_DD@dtors@@QAE@XZ"(%"struct.dtors::D"* %{{[^,]+}})
286 // CHECK: call void @"\01??3@YAXPAX@Z"
287 // CHECK: ret
288 }
289
290 }
291