microsoft-abi-structors.cpp revision 4b104067143cd97e954d279ddc42ed03bbe86a08
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// RUN: %clang_cc1 -emit-llvm %s -o - -cxx-abi microsoft -triple=x86_64-pc-win32 -fno-rtti | FileCheck --check-prefix DTORS-X64 %s
8
9namespace basic {
10
11class A {
12 public:
13  A() { }
14  ~A();
15};
16
17void no_constructor_destructor_infinite_recursion() {
18  A a;
19
20// CHECK:      define linkonce_odr x86_thiscallcc %"class.basic::A"* @"\01??0A@basic@@QAE@XZ"(%"class.basic::A"* returned %this)
21// 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]]
25// CHECK-NEXT: }
26}
27
28A::~A() {
29// Make sure that the destructor doesn't call itself:
30// CHECK: define {{.*}} @"\01??1A@basic@@QAE@XZ"
31// CHECK-NOT: call void @"\01??1A@basic@@QAE@XZ"
32// CHECK: ret
33}
34
35struct B {
36  B();
37};
38
39// Tests that we can define constructors outside the class (PR12784).
40B::B() {
41  // CHECK: define x86_thiscallcc %"struct.basic::B"* @"\01??0B@basic@@QAE@XZ"(%"struct.basic::B"* returned %this)
42  // CHECK: ret
43}
44
45struct C {
46  virtual ~C() {
47// DTORS:      define linkonce_odr x86_thiscallcc void @"\01??_GC@basic@@UAEPAXI@Z"(%"struct.basic::C"* %this, i1 zeroext %should_call_delete)
48// DTORS:        %[[FROMBOOL:[0-9a-z]+]] = zext i1 %should_call_delete to i8
49// DTORS-NEXT:   store i8 %[[FROMBOOL]], i8* %[[SHOULD_DELETE_VAR:[0-9a-z._]+]], align 1
50// DTORS:        %[[SHOULD_DELETE_VALUE:[0-9a-z._]+]] = load i8* %[[SHOULD_DELETE_VAR]]
51// DTORS:        call x86_thiscallcc void @"\01??1C@basic@@UAE@XZ"(%"struct.basic::C"* %[[THIS:[0-9a-z]+]])
52// DTORS-NEXT:   %[[CONDITION:[0-9]+]] = icmp eq i8 %[[SHOULD_DELETE_VALUE]], 0
53// DTORS-NEXT:   br i1 %[[CONDITION]], label %[[CONTINUE_LABEL:[0-9a-z._]+]], label %[[CALL_DELETE_LABEL:[0-9a-z._]+]]
54//
55// DTORS:      [[CALL_DELETE_LABEL]]
56// DTORS-NEXT:   %[[THIS_AS_VOID:[0-9a-z]+]] = bitcast %"struct.basic::C"* %[[THIS]] to i8*
57// DTORS-NEXT:   call void @"\01??3@YAXPAX@Z"(i8* %[[THIS_AS_VOID]])
58// DTORS-NEXT:   br label %[[CONTINUE_LABEL]]
59//
60// DTORS:      [[CONTINUE_LABEL]]
61// DTORS-NEXT:   ret void
62
63// Check that we do the mangling correctly on x64.
64// DTORS-X64:  @"\01??_GC@basic@@UEAAPEAXI@Z"
65  }
66  virtual void foo();
67};
68
69// Emits the vftable in the output.
70void C::foo() {}
71
72void check_vftable_offset() {
73  C c;
74// The vftable pointer should point at the beginning of the vftable.
75// CHECK: [[THIS_PTR:%[0-9]+]] = bitcast %"struct.basic::C"* {{.*}} to i8***
76// CHECK: store i8** getelementptr inbounds ([2 x i8*]* @"\01??_7C@basic@@6B@", i64 0, i64 0), i8*** [[THIS_PTR]]
77}
78
79void call_complete_dtor(C *obj_ptr) {
80// CHECK: define void @"\01?call_complete_dtor@basic@@YAXPAUC@1@@Z"(%"struct.basic::C"* %obj_ptr)
81  obj_ptr->~C();
82// CHECK: %[[OBJ_PTR_VALUE:.*]] = load %"struct.basic::C"** %{{.*}}, align 4
83// CHECK-NEXT: %[[PVTABLE:.*]] = bitcast %"struct.basic::C"* %[[OBJ_PTR_VALUE]] to void (%"struct.basic::C"*, i1)***
84// CHECK-NEXT: %[[VTABLE:.*]] = load void (%"struct.basic::C"*, i1)*** %[[PVTABLE]]
85// CHECK-NEXT: %[[PVDTOR:.*]] = getelementptr inbounds void (%"struct.basic::C"*, i1)** %[[VTABLE]], i64 0
86// CHECK-NEXT: %[[VDTOR:.*]] = load void (%"struct.basic::C"*, i1)** %[[PVDTOR]]
87// CHECK-NEXT: call x86_thiscallcc void %[[VDTOR]](%"struct.basic::C"* %[[OBJ_PTR_VALUE]], i1 zeroext false)
88// CHECK-NEXT: ret void
89}
90
91void call_deleting_dtor(C *obj_ptr) {
92// CHECK: define void @"\01?call_deleting_dtor@basic@@YAXPAUC@1@@Z"(%"struct.basic::C"* %obj_ptr)
93  delete obj_ptr;
94// CHECK:      %[[OBJ_PTR_VALUE:.*]] = load %"struct.basic::C"** %{{.*}}, align 4
95// CHECK:      br i1 {{.*}}, label %[[DELETE_NULL:.*]], label %[[DELETE_NOTNULL:.*]]
96
97// CHECK:      [[DELETE_NOTNULL]]
98// CHECK-NEXT:   %[[PVTABLE:.*]] = bitcast %"struct.basic::C"* %[[OBJ_PTR_VALUE]] to void (%"struct.basic::C"*, i1)***
99// CHECK-NEXT:   %[[VTABLE:.*]] = load void (%"struct.basic::C"*, i1)*** %[[PVTABLE]]
100// CHECK-NEXT:   %[[PVDTOR:.*]] = getelementptr inbounds void (%"struct.basic::C"*, i1)** %[[VTABLE]], i64 0
101// CHECK-NEXT:   %[[VDTOR:.*]] = load void (%"struct.basic::C"*, i1)** %[[PVDTOR]]
102// CHECK-NEXT:   call x86_thiscallcc void %[[VDTOR]](%"struct.basic::C"* %[[OBJ_PTR_VALUE]], i1 zeroext true)
103// CHECK:      ret void
104}
105
106struct D {
107  static int foo();
108
109  D() {
110    static int ctor_static = foo();
111    // CHECK that the static in the ctor gets mangled correctly:
112    // CHECK: @"\01?ctor_static@?1???0D@basic@@QAE@XZ@4HA"
113  }
114  ~D() {
115    static int dtor_static = foo();
116    // CHECK that the static in the dtor gets mangled correctly:
117    // CHECK: @"\01?dtor_static@?1???1D@basic@@QAE@XZ@4HA"
118  }
119};
120
121void use_D() { D c; }
122
123} // end namespace basic
124
125
126namespace constructors {
127
128struct A {
129  A() {}
130};
131
132struct B : A {
133  B();
134  ~B();
135};
136
137B::B() {
138  // CHECK: define x86_thiscallcc %"struct.constructors::B"* @"\01??0B@constructors@@QAE@XZ"(%"struct.constructors::B"* returned %this)
139  // CHECK: call x86_thiscallcc %"struct.constructors::A"* @"\01??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}})
140  // CHECK: ret
141}
142
143struct C : virtual A {
144  C();
145};
146
147C::C() {
148  // CHECK: define x86_thiscallcc %"struct.constructors::C"* @"\01??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* returned %this, i32 %is_most_derived)
149  // TODO: make sure this works in the Release build too;
150  // CHECK: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4
151  // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32* %[[IS_MOST_DERIVED_VAR]]
152  // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
153  // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
154  //
155  // CHECK: [[INIT_VBASES]]
156  // CHECK-NEXT: %[[this_i8:.*]] = bitcast %"struct.constructors::C"* %{{.*}} to i8*
157  // CHECK-NEXT: %[[vbptr_off:.*]] = getelementptr inbounds i8* %[[this_i8]], i64 0
158  // CHECK-NEXT: %[[vbptr:.*]] = bitcast i8* %[[vbptr_off]] to [2 x i32]**
159  // CHECK-NEXT: store [2 x i32]* @"\01??_8C@constructors@@7B@", [2 x i32]** %[[vbptr]]
160  // CHECK-NEXT: bitcast %"struct.constructors::C"* %{{.*}} to %"struct.constructors::A"*
161  // CHECK-NEXT: call x86_thiscallcc %"struct.constructors::A"* @"\01??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}})
162  // CHECK-NEXT: br label %[[SKIP_VBASES]]
163  //
164  // CHECK: [[SKIP_VBASES]]
165  // CHECK: @"\01??_7C@constructors@@6B@"
166  // CHECK: ret
167}
168
169void create_C() {
170  C c;
171  // CHECK: define void @"\01?create_C@constructors@@YAXXZ"()
172  // CHECK: call x86_thiscallcc %"struct.constructors::C"* @"\01??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* %c, i32 1)
173  // CHECK: ret
174}
175
176struct D : C {
177  D();
178};
179
180D::D() {
181  // CHECK: define x86_thiscallcc %"struct.constructors::D"* @"\01??0D@constructors@@QAE@XZ"(%"struct.constructors::D"* returned %this, i32 %is_most_derived) unnamed_addr
182  // CHECK: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4
183  // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32* %[[IS_MOST_DERIVED_VAR]]
184  // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
185  // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
186  //
187  // CHECK: [[INIT_VBASES]]
188  // CHECK-NEXT: %[[this_i8:.*]] = bitcast %"struct.constructors::D"* %{{.*}} to i8*
189  // CHECK-NEXT: %[[vbptr_off:.*]] = getelementptr inbounds i8* %[[this_i8]], i64 0
190  // CHECK-NEXT: %[[vbptr:.*]] = bitcast i8* %[[vbptr_off]] to [2 x i32]**
191  // CHECK-NEXT: store [2 x i32]* @"\01??_8D@constructors@@7B@", [2 x i32]** %[[vbptr]]
192  // CHECK-NEXT: bitcast %"struct.constructors::D"* %{{.*}} to %"struct.constructors::A"*
193  // CHECK-NEXT: call x86_thiscallcc %"struct.constructors::A"* @"\01??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}})
194  // CHECK-NEXT: br label %[[SKIP_VBASES]]
195  //
196  // CHECK: [[SKIP_VBASES]]
197  // CHECK: call x86_thiscallcc %"struct.constructors::C"* @"\01??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* %{{.*}}, i32 0)
198  // CHECK: ret
199}
200
201struct E : virtual C {
202  E();
203};
204
205E::E() {
206  // CHECK: define x86_thiscallcc %"struct.constructors::E"* @"\01??0E@constructors@@QAE@XZ"(%"struct.constructors::E"* returned %this, i32 %is_most_derived) unnamed_addr
207  // CHECK: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4
208  // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32* %[[IS_MOST_DERIVED_VAR]]
209  // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
210  // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
211  //
212  // CHECK: [[INIT_VBASES]]
213  // CHECK-NEXT: %[[this_i8:.*]] = bitcast %"struct.constructors::E"* %{{.*}} to i8*
214  // CHECK-NEXT: %[[offs:.*]] = getelementptr inbounds i8* %[[this_i8]], i64 0
215  // CHECK-NEXT: %[[vbptr_E:.*]] = bitcast i8* %[[offs]] to [3 x i32]**
216  // CHECK-NEXT: store [3 x i32]* @"\01??_8E@constructors@@7B01@@", [3 x i32]** %[[vbptr_E]]
217  // CHECK-NEXT: %[[offs:.*]] = getelementptr inbounds i8* %[[this_i8]], i64 4
218  // CHECK-NEXT: %[[vbptr_C:.*]] = bitcast i8* %[[offs]] to [2 x i32]**
219  // CHECK-NEXT: store [2 x i32]* @"\01??_8E@constructors@@7BC@1@@", [2 x i32]** %[[vbptr_C]]
220  // CHECK-NEXT: bitcast %"struct.constructors::E"* %{{.*}} to %"struct.constructors::A"*
221  // CHECK-NEXT: call x86_thiscallcc %"struct.constructors::A"* @"\01??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}})
222  // CHECK: call x86_thiscallcc %"struct.constructors::C"* @"\01??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* %{{.*}}, i32 0)
223  // CHECK-NEXT: br label %[[SKIP_VBASES]]
224  //
225  // CHECK: [[SKIP_VBASES]]
226  // CHECK: ret
227}
228
229// PR16735 - even abstract classes should have a constructor emitted.
230struct F {
231  F();
232  virtual void f() = 0;
233};
234
235F::F() {}
236// CHECK: define x86_thiscallcc %"struct.constructors::F"* @"\01??0F@constructors@@QAE@XZ"
237
238} // end namespace constructors
239
240namespace dtors {
241
242struct A {
243  ~A();
244};
245
246void call_nv_complete(A *a) {
247  a->~A();
248// CHECK: define void @"\01?call_nv_complete@dtors@@YAXPAUA@1@@Z"
249// CHECK: call x86_thiscallcc void @"\01??1A@dtors@@QAE@XZ"
250// CHECK: ret
251}
252
253// CHECK: declare x86_thiscallcc void @"\01??1A@dtors@@QAE@XZ"
254
255// Now try some virtual bases, where we need the complete dtor.
256
257struct B : virtual A { ~B(); };
258struct C : virtual A { ~C(); };
259struct D : B, C { ~D(); };
260
261void call_vbase_complete(D *d) {
262  d->~D();
263// CHECK: define void @"\01?call_vbase_complete@dtors@@YAXPAUD@1@@Z"
264// CHECK: call x86_thiscallcc void @"\01??_DD@dtors@@QAE@XZ"(%"struct.dtors::D"* %{{[^,]+}})
265// CHECK: ret
266}
267
268// The complete dtor should call the base dtors for D and the vbase A (once).
269// CHECK: define linkonce_odr x86_thiscallcc void @"\01??_DD@dtors@@QAE@XZ"
270// CHECK-NOT: call
271// CHECK: call x86_thiscallcc void @"\01??1D@dtors@@QAE@XZ"
272// CHECK-NOT: call
273// CHECK: call x86_thiscallcc void @"\01??1A@dtors@@QAE@XZ"
274// CHECK-NOT: call
275// CHECK: ret
276
277void destroy_d_complete() {
278  D d;
279// CHECK: define void @"\01?destroy_d_complete@dtors@@YAXXZ"
280// CHECK: call x86_thiscallcc void @"\01??_DD@dtors@@QAE@XZ"(%"struct.dtors::D"* %{{[^,]+}})
281// CHECK: ret
282}
283
284// FIXME: Clang manually inlines the deletion, so we don't get a call to the
285// deleting dtor (_G).  The only way to call deleting dtors currently is through
286// a vftable.
287void call_nv_deleting_dtor(D *d) {
288  delete d;
289// CHECK: define void @"\01?call_nv_deleting_dtor@dtors@@YAXPAUD@1@@Z"
290// CHECK: call x86_thiscallcc void @"\01??_DD@dtors@@QAE@XZ"(%"struct.dtors::D"* %{{[^,]+}})
291// CHECK: call void @"\01??3@YAXPAX@Z"
292// CHECK: ret
293}
294
295}
296