| 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: |
| 16 | A() { } |
| 17 | ~A(); |
| 18 | }; |
| 19 | |
| 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 | |
| 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). |
| 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 { |
| 49 | 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. |
| 74 | void C::foo() {} |
| 75 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 129 | 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 | } |
| 134 | ~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 | |
| 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 | |
| 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 | |
| 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 | |
| 185 | namespace test2 { |
| 186 | // Just like dtor_in_second_nvbase, except put that in a vbase of a diamond. |
| 187 | |
| 188 | // C's dtor is in the non-primary base. |
| 189 | struct A { virtual void f(); }; |
| 190 | struct B { virtual ~B(); }; |
| 191 | struct C : A, B { virtual ~C(); int c; }; |
| 192 | |
| 193 | // Diamond hierarchy, with C as the shared vbase. |
| 194 | struct D : virtual C { int d; }; |
| 195 | struct E : virtual C { int e; }; |
| 196 | struct F : D, E { ~F(); int f; }; |
| 197 | |
| 198 | F::~F() { |
| 199 | // CHECK-LABEL: define dso_local x86_thiscallcc void @"??1F@test2@@UAE@XZ"(%"struct.test2::F"*{{[^,]*}}) |
| 200 | // Do an adjustment from C vbase subobject to F as though F was the |
| 201 | // complete type. |
| 202 | // CHECK: getelementptr inbounds i8, i8* %{{.*}}, i32 -20 |
| 203 | // CHECK: bitcast i8* %{{.*}} to %"struct.test2::F"* |
| 204 | // CHECK: store %"struct.test2::F"* |
| 205 | } |
| 206 | |
| 207 | void foo() { |
| 208 | F f; |
| 209 | } |
| 210 | // DTORS3-LABEL: define linkonce_odr dso_local x86_thiscallcc void @"??_DF@test2@@QAEXXZ"({{.*}} {{.*}} comdat |
| 211 | // Do an adjustment from C* to F*. |
| 212 | // DTORS3: getelementptr i8, i8* %{{.*}}, i32 20 |
| 213 | // DTORS3: bitcast i8* %{{.*}} to %"struct.test2::F"* |
| 214 | // DTORS3: call x86_thiscallcc void @"??1F@test2@@UAE@XZ" |
| 215 | // DTORS3: ret void |
| 216 | |
| 217 | } |
| 218 | |
| 219 | namespace constructors { |
| 220 | |
| 221 | struct A { |
| 222 | A() {} |
| 223 | }; |
| 224 | |
| 225 | struct B : A { |
| 226 | B(); |
| 227 | ~B(); |
| 228 | }; |
| 229 | |
| 230 | B::B() { |
| 231 | // CHECK: define dso_local x86_thiscallcc %"struct.constructors::B"* @"??0B@constructors@@QAE@XZ"(%"struct.constructors::B"* returned %this) |
| 232 | // CHECK: call x86_thiscallcc %"struct.constructors::A"* @"??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}}) |
| 233 | // CHECK: ret |
| 234 | } |
| 235 | |
| 236 | struct C : virtual A { |
| 237 | C(); |
| 238 | }; |
| 239 | |
| 240 | C::C() { |
| 241 | // CHECK: define dso_local x86_thiscallcc %"struct.constructors::C"* @"??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* returned %this, i32 %is_most_derived) |
| 242 | // TODO: make sure this works in the Release build too; |
| 243 | // CHECK: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4 |
| 244 | // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32, i32* %[[IS_MOST_DERIVED_VAR]] |
| 245 | // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0 |
| 246 | // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]] |
| 247 | // |
| 248 | // CHECK: [[INIT_VBASES]] |
| 249 | // CHECK-NEXT: %[[this_i8:.*]] = bitcast %"struct.constructors::C"* %{{.*}} to i8* |
| 250 | // CHECK-NEXT: %[[vbptr_off:.*]] = getelementptr inbounds i8, i8* %[[this_i8]], i32 0 |
| 251 | // CHECK-NEXT: %[[vbptr:.*]] = bitcast i8* %[[vbptr_off]] to i32** |
| 252 | // CHECK-NEXT: store i32* getelementptr inbounds ([2 x i32], [2 x i32]* @"??_8C@constructors@@7B@", i32 0, i32 0), i32** %[[vbptr]] |
| 253 | // CHECK-NEXT: bitcast %"struct.constructors::C"* %{{.*}} to i8* |
| 254 | // CHECK-NEXT: getelementptr inbounds i8, i8* %{{.*}}, i32 4 |
| 255 | // CHECK-NEXT: bitcast i8* %{{.*}} to %"struct.constructors::A"* |
| 256 | // CHECK-NEXT: call x86_thiscallcc %"struct.constructors::A"* @"??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}}) |
| 257 | // CHECK-NEXT: br label %[[SKIP_VBASES]] |
| 258 | // |
| 259 | // CHECK: [[SKIP_VBASES]] |
| 260 | // Class C does not define or override methods, so shouldn't change the vfptr. |
| 261 | // CHECK-NOT: @"??_7C@constructors@@6B@" |
| 262 | // CHECK: ret |
| 263 | } |
| 264 | |
| 265 | void create_C() { |
| 266 | C c; |
| 267 | // CHECK: define dso_local void @"?create_C@constructors@@YAXXZ"() |
| 268 | // CHECK: call x86_thiscallcc %"struct.constructors::C"* @"??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* %c, i32 1) |
| 269 | // CHECK: ret |
| 270 | } |
| 271 | |
| 272 | struct D : C { |
| 273 | D(); |
| 274 | }; |
| 275 | |
| 276 | D::D() { |
| 277 | // CHECK: define dso_local x86_thiscallcc %"struct.constructors::D"* @"??0D@constructors@@QAE@XZ"(%"struct.constructors::D"* returned %this, i32 %is_most_derived) unnamed_addr |
| 278 | // CHECK: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4 |
| 279 | // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32, i32* %[[IS_MOST_DERIVED_VAR]] |
| 280 | // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0 |
| 281 | // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]] |
| 282 | // |
| 283 | // CHECK: [[INIT_VBASES]] |
| 284 | // CHECK-NEXT: %[[this_i8:.*]] = bitcast %"struct.constructors::D"* %{{.*}} to i8* |
| 285 | // CHECK-NEXT: %[[vbptr_off:.*]] = getelementptr inbounds i8, i8* %[[this_i8]], i32 0 |
| 286 | // CHECK-NEXT: %[[vbptr:.*]] = bitcast i8* %[[vbptr_off]] to i32** |
| 287 | // CHECK-NEXT: store i32* getelementptr inbounds ([2 x i32], [2 x i32]* @"??_8D@constructors@@7B@", i32 0, i32 0), i32** %[[vbptr]] |
| 288 | // CHECK-NEXT: bitcast %"struct.constructors::D"* %{{.*}} to i8* |
| 289 | // CHECK-NEXT: getelementptr inbounds i8, i8* %{{.*}}, i32 4 |
| 290 | // CHECK-NEXT: bitcast i8* %{{.*}} to %"struct.constructors::A"* |
| 291 | // CHECK-NEXT: call x86_thiscallcc %"struct.constructors::A"* @"??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}}) |
| 292 | // CHECK-NEXT: br label %[[SKIP_VBASES]] |
| 293 | // |
| 294 | // CHECK: [[SKIP_VBASES]] |
| 295 | // CHECK: call x86_thiscallcc %"struct.constructors::C"* @"??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* %{{.*}}, i32 0) |
| 296 | // CHECK: ret |
| 297 | } |
| 298 | |
| 299 | struct E : virtual C { |
| 300 | E(); |
| 301 | }; |
| 302 | |
| 303 | E::E() { |
| 304 | // CHECK: define dso_local x86_thiscallcc %"struct.constructors::E"* @"??0E@constructors@@QAE@XZ"(%"struct.constructors::E"* returned %this, i32 %is_most_derived) unnamed_addr |
| 305 | // CHECK: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4 |
| 306 | // CHECK: %[[IS_MOST_DERIVED_VAL:.*]] = load i32, i32* %[[IS_MOST_DERIVED_VAR]] |
| 307 | // CHECK: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0 |
| 308 | // CHECK: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]] |
| 309 | // |
| 310 | // CHECK: [[INIT_VBASES]] |
| 311 | // CHECK-NEXT: %[[this_i8:.*]] = bitcast %"struct.constructors::E"* %{{.*}} to i8* |
| 312 | // CHECK-NEXT: %[[offs:.*]] = getelementptr inbounds i8, i8* %[[this_i8]], i32 0 |
| 313 | // CHECK-NEXT: %[[vbptr_E:.*]] = bitcast i8* %[[offs]] to i32** |
| 314 | // CHECK-NEXT: store i32* getelementptr inbounds ([3 x i32], [3 x i32]* @"??_8E@constructors@@7B01@@", i32 0, i32 0), i32** %[[vbptr_E]] |
| 315 | // CHECK-NEXT: %[[offs:.*]] = getelementptr inbounds i8, i8* %[[this_i8]], i32 4 |
| 316 | // CHECK-NEXT: %[[vbptr_C:.*]] = bitcast i8* %[[offs]] to i32** |
| 317 | // CHECK-NEXT: store i32* getelementptr inbounds ([2 x i32], [2 x i32]* @"??_8E@constructors@@7BC@1@@", i32 0, i32 0), i32** %[[vbptr_C]] |
| 318 | // CHECK-NEXT: bitcast %"struct.constructors::E"* %{{.*}} to i8* |
| 319 | // CHECK-NEXT: getelementptr inbounds i8, i8* %{{.*}}, i32 4 |
| 320 | // CHECK-NEXT: bitcast i8* %{{.*}} to %"struct.constructors::A"* |
| 321 | // CHECK-NEXT: call x86_thiscallcc %"struct.constructors::A"* @"??0A@constructors@@QAE@XZ"(%"struct.constructors::A"* %{{.*}}) |
| 322 | // CHECK: call x86_thiscallcc %"struct.constructors::C"* @"??0C@constructors@@QAE@XZ"(%"struct.constructors::C"* %{{.*}}, i32 0) |
| 323 | // CHECK-NEXT: br label %[[SKIP_VBASES]] |
| 324 | // |
| 325 | // CHECK: [[SKIP_VBASES]] |
| 326 | // CHECK: ret |
| 327 | } |
| 328 | |
| 329 | // PR16735 - even abstract classes should have a constructor emitted. |
| 330 | struct F { |
| 331 | F(); |
| 332 | virtual void f() = 0; |
| 333 | }; |
| 334 | |
| 335 | F::F() {} |
| 336 | // CHECK: define dso_local x86_thiscallcc %"struct.constructors::F"* @"??0F@constructors@@QAE@XZ" |
| 337 | |
| 338 | } // end namespace constructors |
| 339 | |
| 340 | namespace dtors { |
| 341 | |
| 342 | struct A { |
| 343 | ~A(); |
| 344 | }; |
| 345 | |
| 346 | void call_nv_complete(A *a) { |
| 347 | a->~A(); |
| 348 | // CHECK: define dso_local void @"?call_nv_complete@dtors@@YAXPAUA@1@@Z" |
| 349 | // CHECK: call x86_thiscallcc void @"??1A@dtors@@QAE@XZ" |
| 350 | // CHECK: ret |
| 351 | } |
| 352 | |
| 353 | // CHECK: declare dso_local x86_thiscallcc void @"??1A@dtors@@QAE@XZ" |
| 354 | |
| 355 | // Now try some virtual bases, where we need the complete dtor. |
| 356 | |
| 357 | struct B : virtual A { ~B(); }; |
| 358 | struct C : virtual A { ~C(); }; |
| 359 | struct D : B, C { ~D(); }; |
| 360 | |
| 361 | void call_vbase_complete(D *d) { |
| 362 | d->~D(); |
| 363 | // CHECK: define dso_local void @"?call_vbase_complete@dtors@@YAXPAUD@1@@Z" |
| 364 | // CHECK: call x86_thiscallcc void @"??_DD@dtors@@QAEXXZ"(%"struct.dtors::D"* %{{[^,]+}}) |
| 365 | // CHECK: ret |
| 366 | } |
| 367 | |
| 368 | // The complete dtor should call the base dtors for D and the vbase A (once). |
| 369 | // CHECK: define linkonce_odr dso_local x86_thiscallcc void @"??_DD@dtors@@QAEXXZ"({{.*}}) {{.*}} comdat |
| 370 | // CHECK-NOT: call |
| 371 | // CHECK: call x86_thiscallcc void @"??1D@dtors@@QAE@XZ" |
| 372 | // CHECK-NOT: call |
| 373 | // CHECK: call x86_thiscallcc void @"??1A@dtors@@QAE@XZ" |
| 374 | // CHECK-NOT: call |
| 375 | // CHECK: ret |
| 376 | |
| 377 | void destroy_d_complete() { |
| 378 | D d; |
| 379 | // CHECK: define dso_local void @"?destroy_d_complete@dtors@@YAXXZ" |
| 380 | // CHECK: call x86_thiscallcc void @"??_DD@dtors@@QAEXXZ"(%"struct.dtors::D"* %{{[^,]+}}) |
| 381 | // CHECK: ret |
| 382 | } |
| 383 | |
| 384 | // FIXME: Clang manually inlines the deletion, so we don't get a call to the |
| 385 | // deleting dtor (_G). The only way to call deleting dtors currently is through |
| 386 | // a vftable. |
| 387 | void call_nv_deleting_dtor(D *d) { |
| 388 | delete d; |
| 389 | // CHECK: define dso_local void @"?call_nv_deleting_dtor@dtors@@YAXPAUD@1@@Z" |
| 390 | // CHECK: call x86_thiscallcc void @"??_DD@dtors@@QAEXXZ"(%"struct.dtors::D"* %{{[^,]+}}) |
| 391 | // CHECK: call void @"??3@YAXPAX@Z" |
| 392 | // CHECK: ret |
| 393 | } |
| 394 | |
| 395 | } |
| 396 | |
| 397 | namespace test1 { |
| 398 | struct A { }; |
| 399 | struct B : virtual A { |
| 400 | B(int *a); |
| 401 | B(const char *a, ...); |
| 402 | __cdecl B(short *a); |
| 403 | }; |
| 404 | B::B(int *a) {} |
| 405 | B::B(const char *a, ...) {} |
| 406 | B::B(short *a) {} |
| 407 | // CHECK: define dso_local x86_thiscallcc %"struct.test1::B"* @"??0B@test1@@QAE@PAH@Z" |
| 408 | // CHECK: (%"struct.test1::B"* returned %this, i32* %a, i32 %is_most_derived) |
| 409 | // CHECK: define dso_local %"struct.test1::B"* @"??0B@test1@@QAA@PBDZZ" |
| 410 | // CHECK: (%"struct.test1::B"* returned %this, i32 %is_most_derived, i8* %a, ...) |
| 411 | // CHECK: define dso_local x86_thiscallcc %"struct.test1::B"* @"??0B@test1@@QAE@PAF@Z" |
| 412 | // CHECK: (%"struct.test1::B"* returned %this, i16* %a, i32 %is_most_derived) |
| 413 | |
| 414 | void construct_b() { |
| 415 | int a; |
| 416 | B b1(&a); |
| 417 | B b2("%d %d", 1, 2); |
| 418 | } |
| 419 | // CHECK-LABEL: define dso_local void @"?construct_b@test1@@YAXXZ"() |
| 420 | // CHECK: call x86_thiscallcc %"struct.test1::B"* @"??0B@test1@@QAE@PAH@Z" |
| 421 | // CHECK: (%"struct.test1::B"* {{.*}}, i32* {{.*}}, i32 1) |
| 422 | // CHECK: call %"struct.test1::B"* (%"struct.test1::B"*, i32, i8*, ...) @"??0B@test1@@QAA@PBDZZ" |
| 423 | // CHECK: (%"struct.test1::B"* {{.*}}, i32 1, i8* {{.*}}, i32 1, i32 2) |
| 424 | } |
| 425 | |
| 426 | namespace implicit_copy_vtable { |
| 427 | // This was a crash that only reproduced in ABIs without key functions. |
| 428 | struct ImplicitCopy { |
| 429 | // implicit copy ctor |
| 430 | virtual ~ImplicitCopy(); |
| 431 | }; |
| 432 | void CreateCopy(ImplicitCopy *a) { |
| 433 | new ImplicitCopy(*a); |
| 434 | } |
| 435 | // CHECK: store {{.*}} @"??_7ImplicitCopy@implicit_copy_vtable@@6B@" |
| 436 | |
| 437 | struct MoveOnly { |
| 438 | MoveOnly(MoveOnly &&o) = default; |
| 439 | virtual ~MoveOnly(); |
| 440 | }; |
| 441 | MoveOnly &&f(); |
| 442 | void g() { new MoveOnly(f()); } |
| 443 | // CHECK: store {{.*}} @"??_7MoveOnly@implicit_copy_vtable@@6B@" |
| 444 | } |
| 445 | |
| 446 | namespace delegating_ctor { |
| 447 | struct Y {}; |
| 448 | struct X : virtual Y { |
| 449 | X(int); |
| 450 | X(); |
| 451 | }; |
| 452 | X::X(int) : X() {} |
| 453 | } |
| 454 | // CHECK: define dso_local x86_thiscallcc %"struct.delegating_ctor::X"* @"??0X@delegating_ctor@@QAE@H@Z"( |
| 455 | // CHECK: %[[is_most_derived_addr:.*]] = alloca i32, align 4 |
| 456 | // CHECK: store i32 %is_most_derived, i32* %[[is_most_derived_addr]] |
| 457 | // CHECK: %[[is_most_derived:.*]] = load i32, i32* %[[is_most_derived_addr]] |
| 458 | // CHECK: call x86_thiscallcc {{.*}}* @"??0X@delegating_ctor@@QAE@XZ"({{.*}} i32 %[[is_most_derived]]) |
| 459 | |
| 460 | // Dtor thunks for classes in anonymous namespaces should be internal, not |
| 461 | // linkonce_odr. |
| 462 | namespace { |
| 463 | struct A { |
| 464 | virtual ~A() { } |
| 465 | }; |
| 466 | } |
| 467 | void *getA() { |
| 468 | return (void*)new A(); |
| 469 | } |
| 470 | // CHECK: define internal x86_thiscallcc i8* @"??_GA@?A0x{{[^@]*}}@@UAEPAXI@Z" |
| 471 | // CHECK: (%"struct.(anonymous namespace)::A"* %this, i32 %should_call_delete) |
| 472 | // CHECK: define internal x86_thiscallcc void @"??1A@?A0x{{[^@]*}}@@UAE@XZ" |
| 473 | // CHECK: (%"struct.(anonymous namespace)::A"* %this) |
| 474 | |
| 475 | // Check that we correctly transform __stdcall to __thiscall for ctors and |
| 476 | // dtors. |
| 477 | class G { |
| 478 | public: |
| 479 | __stdcall G() {}; |
| 480 | // DTORS4: define linkonce_odr dso_local x86_thiscallcc %class.G* @"??0G@@QAE@XZ" |
| 481 | __stdcall ~G() {}; |
| 482 | // DTORS4: define linkonce_odr dso_local x86_thiscallcc void @"??1G@@QAE@XZ" |
| 483 | }; |
| 484 | |
| 485 | extern void testG() { |
| 486 | G g; |
| 487 | } |
| 488 | |