Skip to content

Commit 9846c48

Browse files
committed
[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 705d120 commit 9846c48

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 @@ namespace llvm {
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
@@ -400,6 +400,13 @@ class CGCXXABI {
400400
CXXCtorType Type, bool ForVirtualBase,
401401
bool Delegating, CallArgList &Args);
402402

403+
/// Get the implicit (second) parameter that comes after the "this" pointer,
404+
/// or nullptr if there is isn't one.
405+
virtual llvm::Value *
406+
getCXXDestructorImplicitParam(CodeGenFunction &CGF,
407+
const CXXDestructorDecl *DD, CXXDtorType Type,
408+
bool ForVirtualBase, bool Delegating) = 0;
409+
403410
/// Emit the destructor call.
404411
virtual void EmitDestructorCall(CodeGenFunction &CGF,
405412
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
@@ -229,6 +229,12 @@ class ItaniumCXXABI : public CodeGen::CGCXXABI {
229229
bool ForVirtualBase,
230230
bool Delegating) override;
231231

232+
llvm::Value *getCXXDestructorImplicitParam(CodeGenFunction &CGF,
233+
const CXXDestructorDecl *DD,
234+
CXXDtorType Type,
235+
bool ForVirtualBase,
236+
bool Delegating) override;
237+
232238
void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD,
233239
CXXDtorType Type, bool ForVirtualBase,
234240
bool Delegating, Address This,
@@ -1717,13 +1723,21 @@ CGCXXABI::AddedStructorArgs ItaniumCXXABI::getImplicitConstructorArgs(
17171723
return AddedStructorArgs::prefix({{VTT, VTTTy}});
17181724
}
17191725

1726+
llvm::Value *ItaniumCXXABI::getCXXDestructorImplicitParam(
1727+
CodeGenFunction &CGF, const CXXDestructorDecl *DD, CXXDtorType Type,
1728+
bool ForVirtualBase, bool Delegating) {
1729+
GlobalDecl GD(DD, Type);
1730+
return CGF.GetVTTParameter(GD, ForVirtualBase, Delegating);
1731+
}
1732+
17201733
void ItaniumCXXABI::EmitDestructorCall(CodeGenFunction &CGF,
17211734
const CXXDestructorDecl *DD,
17221735
CXXDtorType Type, bool ForVirtualBase,
17231736
bool Delegating, Address This,
17241737
QualType ThisTy) {
17251738
GlobalDecl GD(DD, Type);
1726-
llvm::Value *VTT = CGF.GetVTTParameter(GD, ForVirtualBase, Delegating);
1739+
llvm::Value *VTT =
1740+
getCXXDestructorImplicitParam(CGF, DD, Type, ForVirtualBase, Delegating);
17271741
QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy);
17281742

17291743
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)