1 //===----- CGCXXABI.cpp - Interface to C++ ABIs ---------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This provides an abstract class for C++ code generation. Concrete subclasses
10 // of this implement code generation for specific C++ ABIs.
12 //===----------------------------------------------------------------------===//
15 #include "CGCleanup.h"
16 #include "clang/AST/Attr.h"
18 using namespace clang
;
19 using namespace CodeGen
;
21 CGCXXABI::~CGCXXABI() { }
23 void CGCXXABI::ErrorUnsupportedABI(CodeGenFunction
&CGF
, StringRef S
) {
24 DiagnosticsEngine
&Diags
= CGF
.CGM
.getDiags();
25 unsigned DiagID
= Diags
.getCustomDiagID(DiagnosticsEngine::Error
,
26 "cannot yet compile %0 in this ABI");
27 Diags
.Report(CGF
.getContext().getFullLoc(CGF
.CurCodeDecl
->getLocation()),
32 llvm::Constant
*CGCXXABI::GetBogusMemberPointer(QualType T
) {
33 return llvm::Constant::getNullValue(CGM
.getTypes().ConvertType(T
));
37 CGCXXABI::ConvertMemberPointerType(const MemberPointerType
*MPT
) {
38 return CGM
.getTypes().ConvertType(CGM
.getContext().getPointerDiffType());
41 CGCallee
CGCXXABI::EmitLoadOfMemberFunctionPointer(
42 CodeGenFunction
&CGF
, const Expr
*E
, Address This
,
43 llvm::Value
*&ThisPtrForCall
,
44 llvm::Value
*MemPtr
, const MemberPointerType
*MPT
) {
45 ErrorUnsupportedABI(CGF
, "calls through member pointers");
47 ThisPtrForCall
= This
.getPointer();
48 const auto *FPT
= MPT
->getPointeeType()->castAs
<FunctionProtoType
>();
50 cast
<CXXRecordDecl
>(MPT
->getClass()->castAs
<RecordType
>()->getDecl());
51 llvm::FunctionType
*FTy
= CGM
.getTypes().GetFunctionType(
52 CGM
.getTypes().arrangeCXXMethodType(RD
, FPT
, /*FD=*/nullptr));
53 llvm::Constant
*FnPtr
= llvm::Constant::getNullValue(FTy
->getPointerTo());
54 return CGCallee::forDirect(FnPtr
, FPT
);
58 CGCXXABI::EmitMemberDataPointerAddress(CodeGenFunction
&CGF
, const Expr
*E
,
59 Address Base
, llvm::Value
*MemPtr
,
60 const MemberPointerType
*MPT
) {
61 ErrorUnsupportedABI(CGF
, "loads of member pointers");
62 llvm::Type
*Ty
= CGF
.ConvertType(MPT
->getPointeeType())
63 ->getPointerTo(Base
.getAddressSpace());
64 return llvm::Constant::getNullValue(Ty
);
67 llvm::Value
*CGCXXABI::EmitMemberPointerConversion(CodeGenFunction
&CGF
,
70 ErrorUnsupportedABI(CGF
, "member function pointer conversions");
71 return GetBogusMemberPointer(E
->getType());
74 llvm::Constant
*CGCXXABI::EmitMemberPointerConversion(const CastExpr
*E
,
75 llvm::Constant
*Src
) {
76 return GetBogusMemberPointer(E
->getType());
80 CGCXXABI::EmitMemberPointerComparison(CodeGenFunction
&CGF
,
83 const MemberPointerType
*MPT
,
85 ErrorUnsupportedABI(CGF
, "member function pointer comparison");
86 return CGF
.Builder
.getFalse();
90 CGCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction
&CGF
,
92 const MemberPointerType
*MPT
) {
93 ErrorUnsupportedABI(CGF
, "member function pointer null testing");
94 return CGF
.Builder
.getFalse();
98 CGCXXABI::EmitNullMemberPointer(const MemberPointerType
*MPT
) {
99 return GetBogusMemberPointer(QualType(MPT
, 0));
102 llvm::Constant
*CGCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl
*MD
) {
103 return GetBogusMemberPointer(CGM
.getContext().getMemberPointerType(
104 MD
->getType(), MD
->getParent()->getTypeForDecl()));
107 llvm::Constant
*CGCXXABI::EmitMemberDataPointer(const MemberPointerType
*MPT
,
109 return GetBogusMemberPointer(QualType(MPT
, 0));
112 llvm::Constant
*CGCXXABI::EmitMemberPointer(const APValue
&MP
, QualType MPT
) {
113 return GetBogusMemberPointer(MPT
);
116 bool CGCXXABI::isZeroInitializable(const MemberPointerType
*MPT
) {
121 void CGCXXABI::buildThisParam(CodeGenFunction
&CGF
, FunctionArgList
¶ms
) {
122 const CXXMethodDecl
*MD
= cast
<CXXMethodDecl
>(CGF
.CurGD
.getDecl());
124 // FIXME: I'm not entirely sure I like using a fake decl just for code
125 // generation. Maybe we can come up with a better way?
126 auto *ThisDecl
= ImplicitParamDecl::Create(
127 CGM
.getContext(), nullptr, MD
->getLocation(),
128 &CGM
.getContext().Idents
.get("this"), MD
->getThisType(),
129 ImplicitParamDecl::CXXThis
);
130 params
.push_back(ThisDecl
);
131 CGF
.CXXABIThisDecl
= ThisDecl
;
133 // Compute the presumed alignment of 'this', which basically comes
134 // down to whether we know it's a complete object or not.
135 auto &Layout
= CGF
.getContext().getASTRecordLayout(MD
->getParent());
136 if (MD
->getParent()->getNumVBases() == 0 || // avoid vcall in common case
137 MD
->getParent()->isEffectivelyFinal() ||
138 isThisCompleteObject(CGF
.CurGD
)) {
139 CGF
.CXXABIThisAlignment
= Layout
.getAlignment();
141 CGF
.CXXABIThisAlignment
= Layout
.getNonVirtualAlignment();
145 llvm::Value
*CGCXXABI::loadIncomingCXXThis(CodeGenFunction
&CGF
) {
146 return CGF
.Builder
.CreateLoad(CGF
.GetAddrOfLocalVar(getThisDecl(CGF
)),
150 void CGCXXABI::setCXXABIThisValue(CodeGenFunction
&CGF
, llvm::Value
*ThisPtr
) {
151 /// Initialize the 'this' slot.
152 assert(getThisDecl(CGF
) && "no 'this' variable for function");
153 CGF
.CXXABIThisValue
= ThisPtr
;
156 bool CGCXXABI::mayNeedDestruction(const VarDecl
*VD
) const {
157 if (VD
->needsDestruction(getContext()))
160 // If the variable has an incomplete class type (or array thereof), it
161 // might need destruction.
162 const Type
*T
= VD
->getType()->getBaseElementTypeUnsafe();
163 if (T
->getAs
<RecordType
>() && T
->isIncompleteType())
169 bool CGCXXABI::isEmittedWithConstantInitializer(
170 const VarDecl
*VD
, bool InspectInitForWeakDef
) const {
171 VD
= VD
->getMostRecentDecl();
172 if (VD
->hasAttr
<ConstInitAttr
>())
175 // All later checks examine the initializer specified on the variable. If
176 // the variable is weak, such examination would not be correct.
177 if (!InspectInitForWeakDef
&& (VD
->isWeak() || VD
->hasAttr
<SelectAnyAttr
>()))
180 const VarDecl
*InitDecl
= VD
->getInitializingDeclaration();
184 // If there's no initializer to run, this is constant initialization.
185 if (!InitDecl
->hasInit())
188 // If we have the only definition, we don't need a thread wrapper if we
189 // will emit the value as a constant.
190 if (isUniqueGVALinkage(getContext().GetGVALinkageForVariable(VD
)))
191 return !mayNeedDestruction(VD
) && InitDecl
->evaluateValue();
193 // Otherwise, we need a thread wrapper unless we know that every
194 // translation unit will emit the value as a constant. We rely on the
195 // variable being constant-initialized in every translation unit if it's
196 // constant-initialized in any translation unit, which isn't actually
197 // guaranteed by the standard but is necessary for sanity.
198 return InitDecl
->hasConstantInitialization();
201 void CGCXXABI::EmitReturnFromThunk(CodeGenFunction
&CGF
,
202 RValue RV
, QualType ResultType
) {
203 assert(!CGF
.hasAggregateEvaluationKind(ResultType
) &&
204 "cannot handle aggregates");
205 CGF
.EmitReturnOfRValue(RV
, ResultType
);
208 CharUnits
CGCXXABI::GetArrayCookieSize(const CXXNewExpr
*expr
) {
209 if (!requiresArrayCookie(expr
))
210 return CharUnits::Zero();
211 return getArrayCookieSizeImpl(expr
->getAllocatedType());
214 CharUnits
CGCXXABI::getArrayCookieSizeImpl(QualType elementType
) {
216 return CharUnits::Zero();
219 Address
CGCXXABI::InitializeArrayCookie(CodeGenFunction
&CGF
,
221 llvm::Value
*NumElements
,
222 const CXXNewExpr
*expr
,
223 QualType ElementType
) {
224 // Should never be called.
225 ErrorUnsupportedABI(CGF
, "array cookie initialization");
226 return Address::invalid();
229 bool CGCXXABI::requiresArrayCookie(const CXXDeleteExpr
*expr
,
230 QualType elementType
) {
231 // If the class's usual deallocation function takes two arguments,
232 // it needs a cookie.
233 if (expr
->doesUsualArrayDeleteWantSize())
236 return elementType
.isDestructedType();
239 bool CGCXXABI::requiresArrayCookie(const CXXNewExpr
*expr
) {
240 // If the class's usual deallocation function takes two arguments,
241 // it needs a cookie.
242 if (expr
->doesUsualArrayDeleteWantSize())
245 return expr
->getAllocatedType().isDestructedType();
248 void CGCXXABI::ReadArrayCookie(CodeGenFunction
&CGF
, Address ptr
,
249 const CXXDeleteExpr
*expr
, QualType eltTy
,
250 llvm::Value
*&numElements
,
251 llvm::Value
*&allocPtr
, CharUnits
&cookieSize
) {
252 // Derive a char* in the same address space as the pointer.
253 ptr
= CGF
.Builder
.CreateElementBitCast(ptr
, CGF
.Int8Ty
);
255 // If we don't need an array cookie, bail out early.
256 if (!requiresArrayCookie(expr
, eltTy
)) {
257 allocPtr
= ptr
.getPointer();
258 numElements
= nullptr;
259 cookieSize
= CharUnits::Zero();
263 cookieSize
= getArrayCookieSizeImpl(eltTy
);
265 CGF
.Builder
.CreateConstInBoundsByteGEP(ptr
, -cookieSize
);
266 allocPtr
= allocAddr
.getPointer();
267 numElements
= readArrayCookieImpl(CGF
, allocAddr
, cookieSize
);
270 llvm::Value
*CGCXXABI::readArrayCookieImpl(CodeGenFunction
&CGF
,
272 CharUnits cookieSize
) {
273 ErrorUnsupportedABI(CGF
, "reading a new[] cookie");
274 return llvm::ConstantInt::get(CGF
.SizeTy
, 0);
277 /// Returns the adjustment, in bytes, required for the given
278 /// member-pointer operation. Returns null if no adjustment is
280 llvm::Constant
*CGCXXABI::getMemberPointerAdjustment(const CastExpr
*E
) {
281 assert(E
->getCastKind() == CK_DerivedToBaseMemberPointer
||
282 E
->getCastKind() == CK_BaseToDerivedMemberPointer
);
284 QualType derivedType
;
285 if (E
->getCastKind() == CK_DerivedToBaseMemberPointer
)
286 derivedType
= E
->getSubExpr()->getType();
288 derivedType
= E
->getType();
290 const CXXRecordDecl
*derivedClass
=
291 derivedType
->castAs
<MemberPointerType
>()->getClass()->getAsCXXRecordDecl();
293 return CGM
.GetNonVirtualBaseClassOffset(derivedClass
,
299 CGCXXABI::EmitCtorCompleteObjectHandler(CodeGenFunction
&CGF
,
300 const CXXRecordDecl
*RD
) {
301 if (CGM
.getTarget().getCXXABI().hasConstructorVariants())
302 llvm_unreachable("shouldn't be called in this ABI");
304 ErrorUnsupportedABI(CGF
, "complete object detection in ctor");
308 void CGCXXABI::setCXXDestructorDLLStorage(llvm::GlobalValue
*GV
,
309 const CXXDestructorDecl
*Dtor
,
310 CXXDtorType DT
) const {
311 // Assume the base C++ ABI has no special rules for destructor variants.
312 CGM
.setDLLImportDLLExport(GV
, Dtor
);
315 llvm::GlobalValue::LinkageTypes
CGCXXABI::getCXXDestructorLinkage(
316 GVALinkage Linkage
, const CXXDestructorDecl
*Dtor
, CXXDtorType DT
) const {
317 // Delegate back to CGM by default.
318 return CGM
.getLLVMLinkageForDeclarator(Dtor
, Linkage
,
319 /*IsConstantVariable=*/false);
322 bool CGCXXABI::NeedsVTTParameter(GlobalDecl GD
) {
327 CGCXXABI::emitTerminateForUnexpectedException(CodeGenFunction
&CGF
,
329 // Just call std::terminate and ignore the violating exception.
330 return CGF
.EmitNounwindRuntimeCall(CGF
.CGM
.getTerminateFn());
333 CatchTypeInfo
CGCXXABI::getCatchAllTypeInfo() {
334 return CatchTypeInfo
{nullptr, 0};
337 std::vector
<CharUnits
> CGCXXABI::getVBPtrOffsets(const CXXRecordDecl
*RD
) {
338 return std::vector
<CharUnits
>();
341 CGCXXABI::AddedStructorArgCounts
CGCXXABI::addImplicitConstructorArgs(
342 CodeGenFunction
&CGF
, const CXXConstructorDecl
*D
, CXXCtorType Type
,
343 bool ForVirtualBase
, bool Delegating
, CallArgList
&Args
) {
344 AddedStructorArgs AddedArgs
=
345 getImplicitConstructorArgs(CGF
, D
, Type
, ForVirtualBase
, Delegating
);
346 for (size_t i
= 0; i
< AddedArgs
.Prefix
.size(); ++i
) {
347 Args
.insert(Args
.begin() + 1 + i
,
348 CallArg(RValue::get(AddedArgs
.Prefix
[i
].Value
),
349 AddedArgs
.Prefix
[i
].Type
));
351 for (const auto &arg
: AddedArgs
.Suffix
) {
352 Args
.add(RValue::get(arg
.Value
), arg
.Type
);
354 return AddedStructorArgCounts(AddedArgs
.Prefix
.size(),
355 AddedArgs
.Suffix
.size());