1 // RUN: %clang_cc1 -emit-llvm %s -o - -cxx-abi microsoft -triple=i386-pc-win32 -fno-rtti > %t 2>&1
2 // RUN: FileCheck %s < %t
3 // Using a different check prefix as the inline destructors might be placed
4 // anywhere in the output.
5 // RUN: FileCheck --check-prefix=DTORS %s < %t
6 
7 namespace basic {
8 
9 class A {
10  public:
11   A() { }
12   ~A() { }
13 };
14 
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"* %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 // Make sure that the destructor doesn't call itself:
26 // CHECK: define {{.*}} @"\01??1A@basic@@QAE@XZ"
27 // CHECK-NOT: call void @"\01??1A@basic@@QAE@XZ"
28 // CHECK: ret
29 }
30 
31 struct B {
32   B();
33 };
34 
35 // Tests that we can define constructors outside the class (PR12784).
36 B::B() {
37   // CHECK: define x86_thiscallcc %"struct.basic::B"* @"\01??0B@basic@@QAE@XZ"(%"struct.basic::B"* %this)
38   // CHECK: ret
39 }
40 
41 struct C {
42   virtual ~C() {
43 // Complete destructor first:
44 // DTORS: define {{.*}} x86_thiscallcc void @"\01??1C@basic@@UAE@XZ"(%"struct.basic::C"* %this)
45 
46 // Then, the scalar deleting destructor (used in the vtable):
47 // FIXME: add a test that verifies that the out-of-line scalar deleting
48 // destructor is linkonce_odr too.
49 // DTORS:      define linkonce_odr x86_thiscallcc void @"\01??_GC@basic@@UAEPAXI@Z"(%"struct.basic::C"* %this, i1 zeroext %should_call_delete)
50 // DTORS:        %[[FROMBOOL:[0-9a-z]+]] = zext i1 %should_call_delete to i8
51 // DTORS-NEXT:   store i8 %[[FROMBOOL]], i8* %[[SHOULD_DELETE_VAR:[0-9a-z._]+]], align 1
52 // DTORS:        %[[SHOULD_DELETE_VALUE:[0-9a-z._]+]] = load i8* %[[SHOULD_DELETE_VAR]]
53 // DTORS:        call x86_thiscallcc void @"\01??1C@basic@@UAE@XZ"(%"struct.basic::C"* %[[THIS:[0-9a-z]+]])
54 // DTORS-NEXT:   %[[CONDITION:[0-9]+]] = icmp eq i8 %[[SHOULD_DELETE_VALUE]], 0
55 // DTORS-NEXT:   br i1 %[[CONDITION]], label %[[CONTINUE_LABEL:[0-9a-z._]+]], label %[[CALL_DELETE_LABEL:[0-9a-z._]+]]
56 //
57 // DTORS:      [[CALL_DELETE_LABEL]]
58 // DTORS-NEXT:   %[[THIS_AS_VOID:[0-9a-z]+]] = bitcast %"struct.basic::C"* %[[THIS]] to i8*
59 // DTORS-NEXT:   call void @"\01??3@YAXPAX@Z"(i8* %[[THIS_AS_VOID]]) [[NUW:#[0-9]+]]
60 // DTORS-NEXT:   br label %[[CONTINUE_LABEL]]
61 //
62 // DTORS:      [[CONTINUE_LABEL]]
63 // DTORS-NEXT:   ret void
64   }
65   virtual void foo();
66 };
67 
68 // Emits the vftable in the output.
69 void C::foo() {}
70 
71 void check_vftable_offset() {
72   C c;
73 // The vftable pointer should point at the beginning of the vftable.
74 // 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]]
76 }
77 
78 void 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
82 // CHECK-NEXT: %[[PVTABLE:.*]] = bitcast %"struct.basic::C"* %[[OBJ_PTR_VALUE]] to void (%"struct.basic::C"*, i1)***
83 // CHECK-NEXT: %[[VTABLE:.*]] = load void (%"struct.basic::C"*, i1)*** %[[PVTABLE]]
84 // CHECK-NEXT: %[[PVDTOR:.*]] = getelementptr inbounds void (%"struct.basic::C"*, i1)** %[[VTABLE]], i64 0
85 // CHECK-NEXT: %[[VDTOR:.*]] = load void (%"struct.basic::C"*, i1)** %[[PVDTOR]]
86 // CHECK-NEXT: call x86_thiscallcc void %[[VDTOR]](%"struct.basic::C"* %[[OBJ_PTR_VALUE]], i1 zeroext false)
87 // CHECK-NEXT: ret void
88 }
89 
90 void call_deleting_dtor(C *obj_ptr) {
91 // CHECK: define void @"\01?call_deleting_dtor@basic@@YAXPAUC@1@@Z"(%"struct.basic::C"* %obj_ptr)
92   delete obj_ptr;
93 // 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]]
97 // CHECK-NEXT:   %[[PVTABLE:.*]] = bitcast %"struct.basic::C"* %[[OBJ_PTR_VALUE]] to void (%"struct.basic::C"*, i1)***
98 // CHECK-NEXT:   %[[VTABLE:.*]] = load void (%"struct.basic::C"*, i1)*** %[[PVTABLE]]
99 // CHECK-NEXT:   %[[PVDTOR:.*]] = getelementptr inbounds void (%"struct.basic::C"*, i1)** %[[VTABLE]], i64 0
100 // CHECK-NEXT:   %[[VDTOR:.*]] = load void (%"struct.basic::C"*, i1)** %[[PVDTOR]]
101 // CHECK-NEXT:   call x86_thiscallcc void %[[VDTOR]](%"struct.basic::C"* %[[OBJ_PTR_VALUE]], i1 zeroext true)
102 // CHECK:      ret void
103 }
104 
105 struct D {
106   static int foo();
107 
108   D() {
109     static int ctor_static = foo();
110     // CHECK that the static in the ctor gets mangled correctly:
111     // CHECK: @"\01?ctor_static@?1???0D@basic@@QAE@XZ@4HA"
112   }
113   ~D() {
114     static int dtor_static = foo();
115     // CHECK that the static in the dtor gets mangled correctly:
116     // CHECK: @"\01?dtor_static@?1???1D@basic@@QAE@XZ@4HA"
117   }
118 };
119 
120 void use_D() { D c; }
121 
122 // DTORS: attributes [[NUW]] = { nounwind{{.*}} }
123 
124 } // end namespace basic
125 
126 
127 namespace constructors {
128 
129 struct A {
130   A() {}
131 };
132 
133 struct B : A {
134   B();
135   ~B();
136 };
137 
138 B::B() {
139   // CHECK: define x86_thiscallcc %"struct.constructors::B"* @"\01??0B@constructors@@QAE@XZ"(%"struct.constructors::B"* %this)
140   // CHECK: call x86_thiscallcc %"struct.constructors::A"* @"\01??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}})
141   // CHECK: ret
142 }
143 
144 struct C : virtual A {
145   C();
146 };
147 
148 C::C() {
149   // CHECK: define x86_thiscallcc %"struct.constructors::C"* @"\01??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* %this, i32 %is_most_derived)
150   // TODO: make sure this works in the Release build too;
151   // CHECK: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4
152   // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32* %[[IS_MOST_DERIVED_VAR]]
153   // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
154   // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
155   //
156   // CHECK: [[INIT_VBASES]]
157   // CHECK-NEXT: bitcast %"struct.constructors::C"* %{{.*}} to %"struct.constructors::A"*
158   // CHECK-NEXT: call x86_thiscallcc %"struct.constructors::A"* @"\01??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}})
159   // CHECK-NEXT: br label %[[SKIP_VBASES]]
160   //
161   // CHECK: [[SKIP_VBASES]]
162   // CHECK: @"\01??_7C@constructors@@6B@"
163   // CHECK: ret
164 }
165 
166 void create_C() {
167   C c;
168   // CHECK: define void @"\01?create_C@constructors@@YAXXZ"()
169   // CHECK: call x86_thiscallcc %"struct.constructors::C"* @"\01??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* %c, i32 1)
170   // CHECK: ret
171 }
172 
173 struct D : C {
174   D();
175 };
176 
177 D::D() {
178   // CHECK: define x86_thiscallcc %"struct.constructors::D"* @"\01??0D@constructors@@QAE@XZ"(%"struct.constructors::D"* %this, i32 %is_most_derived) unnamed_addr
179   // CHECK: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4
180   // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32* %[[IS_MOST_DERIVED_VAR]]
181   // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
182   // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
183   //
184   // CHECK: [[INIT_VBASES]]
185   // CHECK-NEXT: bitcast %"struct.constructors::D"* %{{.*}} to %"struct.constructors::A"*
186   // CHECK-NEXT: call x86_thiscallcc %"struct.constructors::A"* @"\01??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}})
187   // CHECK-NEXT: br label %[[SKIP_VBASES]]
188   //
189   // CHECK: [[SKIP_VBASES]]
190   // CHECK: call x86_thiscallcc %"struct.constructors::C"* @"\01??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* %{{.*}}, i32 0)
191   // CHECK: ret
192 }
193 
194 struct E : virtual C {
195   E();
196 };
197 
198 E::E() {
199   // CHECK: define x86_thiscallcc %"struct.constructors::E"* @"\01??0E@constructors@@QAE@XZ"(%"struct.constructors::E"* %this, i32 %is_most_derived) unnamed_addr
200   // CHECK: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4
201   // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32* %[[IS_MOST_DERIVED_VAR]]
202   // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
203   // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
204   //
205   // CHECK: [[INIT_VBASES]]
206   // CHECK-NEXT: bitcast %"struct.constructors::E"* %{{.*}} to %"struct.constructors::A"*
207   // CHECK-NEXT: call x86_thiscallcc %"struct.constructors::A"* @"\01??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}})
208   // CHECK: call x86_thiscallcc %"struct.constructors::C"* @"\01??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* %{{.*}}, i32 0)
209   // CHECK-NEXT: br label %[[SKIP_VBASES]]
210   //
211   // CHECK: [[SKIP_VBASES]]
212   // CHECK: ret
213 }
214 
215 } // end namespace constructors
216