Skip to content

Commit 64eab5f

Browse files
zoecarvermemfrob
authored andcommitted
[CodeGen] Add public function to emit C++ destructor call.
Adds `CodeGen::getCXXDestructorImplicitParam`, to retrieve a C++ destructor's implicit parameter (after the "this" pointer) based on the ABI in the given CodeGenModule. This will allow other frontends (Swift, for example) to easily emit calls to object destructors with correct ABI semantics and calling convetions. This is needed for Swift C++ interop. Here's the corresponding Swift change: swiftlang/swift#32291 Differential Revision: https://reviews.llvm.org/D82392
1 parent fe77b58 commit 64eab5f

File tree

6 files changed

+60
-3
lines changed

6 files changed

+60
-3
lines changed

clang/include/clang/CodeGen/CodeGenABITypes.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525

2626
#include "clang/AST/CanonicalType.h"
2727
#include "clang/AST/Type.h"
28+
#include "clang/Basic/ABI.h"
2829
#include "clang/CodeGen/CGFunctionInfo.h"
30+
#include "llvm/IR/BasicBlock.h"
2931

3032
namespace llvm {
3133
class AttrBuilder;
@@ -40,6 +42,7 @@ class Type;
4042
namespace clang {
4143
class ASTContext;
4244
class CXXConstructorDecl;
45+
class CXXDestructorDecl;
4346
class CXXRecordDecl;
4447
class CXXMethodDecl;
4548
class CodeGenOptions;
@@ -90,6 +93,12 @@ const CGFunctionInfo &arrangeFreeFunctionCall(CodeGenModule &CGM,
9093
ImplicitCXXConstructorArgs
9194
getImplicitCXXConstructorArgs(CodeGenModule &CGM, const CXXConstructorDecl *D);
9295

96+
llvm::Value *
97+
getCXXDestructorImplicitParam(CodeGenModule &CGM, llvm::BasicBlock *InsertBlock,
98+
llvm::BasicBlock::iterator InsertPoint,
99+
const CXXDestructorDecl *D, CXXDtorType Type,
100+
bool ForVirtualBase, bool Delegating);
101+
93102
/// Returns null if the function type is incomplete and can't be lowered.
94103
llvm::FunctionType *convertFreeFunctionType(CodeGenModule &CGM,
95104
const FunctionDecl *FD);

clang/lib/CodeGen/ABIInfo.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ namespace clang {
2828

2929
namespace CodeGen {
3030
class ABIArgInfo;
31-
class Address;
3231
class CGCXXABI;
3332
class CGFunctionInfo;
3433
class CodeGenFunction;

clang/lib/CodeGen/CGCXXABI.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,13 @@ class CGCXXABI {
402402
CXXCtorType Type, bool ForVirtualBase,
403403
bool Delegating, CallArgList &Args);
404404

405+
/// Get the implicit (second) parameter that comes after the "this" pointer,
406+
/// or nullptr if there is isn't one.
407+
virtual llvm::Value *
408+
getCXXDestructorImplicitParam(CodeGenFunction &CGF,
409+
const CXXDestructorDecl *DD, CXXDtorType Type,
410+
bool ForVirtualBase, bool Delegating) = 0;
411+
405412
/// Emit the destructor call.
406413
virtual void EmitDestructorCall(CodeGenFunction &CGF,
407414
const CXXDestructorDecl *DD, CXXDtorType Type,

clang/lib/CodeGen/CodeGenABITypes.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,16 @@ unsigned CodeGen::getLLVMFieldNumber(CodeGenModule &CGM,
115115
const FieldDecl *FD) {
116116
return CGM.getTypes().getCGRecordLayout(RD).getLLVMFieldNo(FD);
117117
}
118+
119+
llvm::Value *CodeGen::getCXXDestructorImplicitParam(
120+
CodeGenModule &CGM, llvm::BasicBlock *InsertBlock,
121+
llvm::BasicBlock::iterator InsertPoint, const CXXDestructorDecl *D,
122+
CXXDtorType Type, bool ForVirtualBase, bool Delegating) {
123+
CodeGenFunction CGF(CGM, /*suppressNewContext=*/true);
124+
CGF.CurCodeDecl = D;
125+
CGF.CurFuncDecl = D;
126+
CGF.CurFn = InsertBlock->getParent();
127+
CGF.Builder.SetInsertPoint(InsertBlock, InsertPoint);
128+
return CGM.getCXXABI().getCXXDestructorImplicitParam(
129+
CGF, D, Type, ForVirtualBase, Delegating);
130+
}

clang/lib/CodeGen/ItaniumCXXABI.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,12 @@ class ItaniumCXXABI : public CodeGen::CGCXXABI {
228228
bool ForVirtualBase,
229229
bool Delegating) override;
230230

231+
llvm::Value *getCXXDestructorImplicitParam(CodeGenFunction &CGF,
232+
const CXXDestructorDecl *DD,
233+
CXXDtorType Type,
234+
bool ForVirtualBase,
235+
bool Delegating) override;
236+
231237
void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD,
232238
CXXDtorType Type, bool ForVirtualBase,
233239
bool Delegating, Address This,
@@ -1693,13 +1699,21 @@ CGCXXABI::AddedStructorArgs ItaniumCXXABI::getImplicitConstructorArgs(
16931699
return AddedStructorArgs::prefix({{VTT, VTTTy}});
16941700
}
16951701

1702+
llvm::Value *ItaniumCXXABI::getCXXDestructorImplicitParam(
1703+
CodeGenFunction &CGF, const CXXDestructorDecl *DD, CXXDtorType Type,
1704+
bool ForVirtualBase, bool Delegating) {
1705+
GlobalDecl GD(DD, Type);
1706+
return CGF.GetVTTParameter(GD, ForVirtualBase, Delegating);
1707+
}
1708+
16961709
void ItaniumCXXABI::EmitDestructorCall(CodeGenFunction &CGF,
16971710
const CXXDestructorDecl *DD,
16981711
CXXDtorType Type, bool ForVirtualBase,
16991712
bool Delegating, Address This,
17001713
QualType ThisTy) {
17011714
GlobalDecl GD(DD, Type);
1702-
llvm::Value *VTT = CGF.GetVTTParameter(GD, ForVirtualBase, Delegating);
1715+
llvm::Value *VTT =
1716+
getCXXDestructorImplicitParam(CGF, DD, Type, ForVirtualBase, Delegating);
17031717
QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy);
17041718

17051719
CGCallee Callee;

clang/lib/CodeGen/MicrosoftCXXABI.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,12 @@ class MicrosoftCXXABI : public CGCXXABI {
259259
bool ForVirtualBase,
260260
bool Delegating) override;
261261

262+
llvm::Value *getCXXDestructorImplicitParam(CodeGenFunction &CGF,
263+
const CXXDestructorDecl *DD,
264+
CXXDtorType Type,
265+
bool ForVirtualBase,
266+
bool Delegating) override;
267+
262268
void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD,
263269
CXXDtorType Type, bool ForVirtualBase,
264270
bool Delegating, Address This,
@@ -1577,6 +1583,12 @@ CGCXXABI::AddedStructorArgs MicrosoftCXXABI::getImplicitConstructorArgs(
15771583
return AddedStructorArgs::suffix({{MostDerivedArg, getContext().IntTy}});
15781584
}
15791585

1586+
llvm::Value *MicrosoftCXXABI::getCXXDestructorImplicitParam(
1587+
CodeGenFunction &CGF, const CXXDestructorDecl *DD, CXXDtorType Type,
1588+
bool ForVirtualBase, bool Delegating) {
1589+
return nullptr;
1590+
}
1591+
15801592
void MicrosoftCXXABI::EmitDestructorCall(CodeGenFunction &CGF,
15811593
const CXXDestructorDecl *DD,
15821594
CXXDtorType Type, bool ForVirtualBase,
@@ -1603,8 +1615,11 @@ void MicrosoftCXXABI::EmitDestructorCall(CodeGenFunction &CGF,
16031615
BaseDtorEndBB = EmitDtorCompleteObjectHandler(CGF);
16041616
}
16051617

1618+
llvm::Value *Implicit =
1619+
getCXXDestructorImplicitParam(CGF, DD, Type, ForVirtualBase,
1620+
Delegating); // = nullptr
16061621
CGF.EmitCXXDestructorCall(GD, Callee, This.getPointer(), ThisTy,
1607-
/*ImplicitParam=*/nullptr,
1622+
/*ImplicitParam=*/Implicit,
16081623
/*ImplicitParamTy=*/QualType(), nullptr);
16091624
if (BaseDtorEndBB) {
16101625
// Complete object handler should continue to be the remaining

0 commit comments

Comments
 (0)