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 Address
CGCXXABI::getThisAddress(CodeGenFunction
&CGF
) {
24 return CGF
.makeNaturalAddressForPointer(
25 CGF
.CXXABIThisValue
, CGF
.CXXABIThisDecl
->getType()->getPointeeType(),
26 CGF
.CXXABIThisAlignment
);
29 void CGCXXABI::ErrorUnsupportedABI(CodeGenFunction
&CGF
, StringRef S
) {
30 DiagnosticsEngine
&Diags
= CGF
.CGM
.getDiags();
31 unsigned DiagID
= Diags
.getCustomDiagID(DiagnosticsEngine::Error
,
32 "cannot yet compile %0 in this ABI");
33 Diags
.Report(CGF
.getContext().getFullLoc(CGF
.CurCodeDecl
->getLocation()),
38 llvm::Constant
*CGCXXABI::GetBogusMemberPointer(QualType T
) {
39 return llvm::Constant::getNullValue(CGM
.getTypes().ConvertType(T
));
43 CGCXXABI::ConvertMemberPointerType(const MemberPointerType
*MPT
) {
44 return CGM
.getTypes().ConvertType(CGM
.getContext().getPointerDiffType());
47 CGCallee
CGCXXABI::EmitLoadOfMemberFunctionPointer(
48 CodeGenFunction
&CGF
, const Expr
*E
, Address This
,
49 llvm::Value
*&ThisPtrForCall
,
50 llvm::Value
*MemPtr
, const MemberPointerType
*MPT
) {
51 ErrorUnsupportedABI(CGF
, "calls through member pointers");
54 cast
<CXXRecordDecl
>(MPT
->getClass()->castAs
<RecordType
>()->getDecl());
56 CGF
.getAsNaturalPointerTo(This
, CGF
.getContext().getRecordType(RD
));
57 const FunctionProtoType
*FPT
=
58 MPT
->getPointeeType()->getAs
<FunctionProtoType
>();
59 llvm::Constant
*FnPtr
= llvm::Constant::getNullValue(
60 llvm::PointerType::getUnqual(CGM
.getLLVMContext()));
61 return CGCallee::forDirect(FnPtr
, FPT
);
65 CGCXXABI::EmitMemberDataPointerAddress(CodeGenFunction
&CGF
, const Expr
*E
,
66 Address Base
, llvm::Value
*MemPtr
,
67 const MemberPointerType
*MPT
) {
68 ErrorUnsupportedABI(CGF
, "loads of member pointers");
70 llvm::PointerType::get(CGF
.getLLVMContext(), Base
.getAddressSpace());
71 return llvm::Constant::getNullValue(Ty
);
74 llvm::Value
*CGCXXABI::EmitMemberPointerConversion(CodeGenFunction
&CGF
,
77 ErrorUnsupportedABI(CGF
, "member function pointer conversions");
78 return GetBogusMemberPointer(E
->getType());
81 llvm::Constant
*CGCXXABI::EmitMemberPointerConversion(const CastExpr
*E
,
82 llvm::Constant
*Src
) {
83 return GetBogusMemberPointer(E
->getType());
87 CGCXXABI::EmitMemberPointerComparison(CodeGenFunction
&CGF
,
90 const MemberPointerType
*MPT
,
92 ErrorUnsupportedABI(CGF
, "member function pointer comparison");
93 return CGF
.Builder
.getFalse();
97 CGCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction
&CGF
,
99 const MemberPointerType
*MPT
) {
100 ErrorUnsupportedABI(CGF
, "member function pointer null testing");
101 return CGF
.Builder
.getFalse();
105 CGCXXABI::EmitNullMemberPointer(const MemberPointerType
*MPT
) {
106 return GetBogusMemberPointer(QualType(MPT
, 0));
109 llvm::Constant
*CGCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl
*MD
) {
110 return GetBogusMemberPointer(CGM
.getContext().getMemberPointerType(
111 MD
->getType(), MD
->getParent()->getTypeForDecl()));
114 llvm::Constant
*CGCXXABI::EmitMemberDataPointer(const MemberPointerType
*MPT
,
116 return GetBogusMemberPointer(QualType(MPT
, 0));
119 llvm::Constant
*CGCXXABI::EmitMemberPointer(const APValue
&MP
, QualType MPT
) {
120 return GetBogusMemberPointer(MPT
);
123 bool CGCXXABI::isZeroInitializable(const MemberPointerType
*MPT
) {
128 void CGCXXABI::buildThisParam(CodeGenFunction
&CGF
, FunctionArgList
¶ms
) {
129 const CXXMethodDecl
*MD
= cast
<CXXMethodDecl
>(CGF
.CurGD
.getDecl());
131 // FIXME: I'm not entirely sure I like using a fake decl just for code
132 // generation. Maybe we can come up with a better way?
134 ImplicitParamDecl::Create(CGM
.getContext(), nullptr, MD
->getLocation(),
135 &CGM
.getContext().Idents
.get("this"),
136 MD
->getThisType(), ImplicitParamKind::CXXThis
);
137 params
.push_back(ThisDecl
);
138 CGF
.CXXABIThisDecl
= ThisDecl
;
140 // Compute the presumed alignment of 'this', which basically comes
141 // down to whether we know it's a complete object or not.
142 auto &Layout
= CGF
.getContext().getASTRecordLayout(MD
->getParent());
143 if (MD
->getParent()->getNumVBases() == 0 || // avoid vcall in common case
144 MD
->getParent()->isEffectivelyFinal() ||
145 isThisCompleteObject(CGF
.CurGD
)) {
146 CGF
.CXXABIThisAlignment
= Layout
.getAlignment();
148 CGF
.CXXABIThisAlignment
= Layout
.getNonVirtualAlignment();
152 llvm::Value
*CGCXXABI::loadIncomingCXXThis(CodeGenFunction
&CGF
) {
153 return CGF
.Builder
.CreateLoad(CGF
.GetAddrOfLocalVar(getThisDecl(CGF
)),
157 void CGCXXABI::setCXXABIThisValue(CodeGenFunction
&CGF
, llvm::Value
*ThisPtr
) {
158 /// Initialize the 'this' slot.
159 assert(getThisDecl(CGF
) && "no 'this' variable for function");
160 CGF
.CXXABIThisValue
= ThisPtr
;
163 bool CGCXXABI::mayNeedDestruction(const VarDecl
*VD
) const {
164 if (VD
->needsDestruction(getContext()))
167 // If the variable has an incomplete class type (or array thereof), it
168 // might need destruction.
169 const Type
*T
= VD
->getType()->getBaseElementTypeUnsafe();
170 if (T
->getAs
<RecordType
>() && T
->isIncompleteType())
176 bool CGCXXABI::isEmittedWithConstantInitializer(
177 const VarDecl
*VD
, bool InspectInitForWeakDef
) const {
178 VD
= VD
->getMostRecentDecl();
179 if (VD
->hasAttr
<ConstInitAttr
>())
182 // All later checks examine the initializer specified on the variable. If
183 // the variable is weak, such examination would not be correct.
184 if (!InspectInitForWeakDef
&& (VD
->isWeak() || VD
->hasAttr
<SelectAnyAttr
>()))
187 const VarDecl
*InitDecl
= VD
->getInitializingDeclaration();
191 // If there's no initializer to run, this is constant initialization.
192 if (!InitDecl
->hasInit())
195 // If we have the only definition, we don't need a thread wrapper if we
196 // will emit the value as a constant.
197 if (isUniqueGVALinkage(getContext().GetGVALinkageForVariable(VD
)))
198 return !mayNeedDestruction(VD
) && InitDecl
->evaluateValue();
200 // Otherwise, we need a thread wrapper unless we know that every
201 // translation unit will emit the value as a constant. We rely on the
202 // variable being constant-initialized in every translation unit if it's
203 // constant-initialized in any translation unit, which isn't actually
204 // guaranteed by the standard but is necessary for sanity.
205 return InitDecl
->hasConstantInitialization();
208 void CGCXXABI::EmitReturnFromThunk(CodeGenFunction
&CGF
,
209 RValue RV
, QualType ResultType
) {
210 assert(!CGF
.hasAggregateEvaluationKind(ResultType
) &&
211 "cannot handle aggregates");
212 CGF
.EmitReturnOfRValue(RV
, ResultType
);
215 CharUnits
CGCXXABI::GetArrayCookieSize(const CXXNewExpr
*expr
) {
216 if (!requiresArrayCookie(expr
))
217 return CharUnits::Zero();
218 return getArrayCookieSizeImpl(expr
->getAllocatedType());
221 CharUnits
CGCXXABI::getArrayCookieSizeImpl(QualType elementType
) {
223 return CharUnits::Zero();
226 Address
CGCXXABI::InitializeArrayCookie(CodeGenFunction
&CGF
,
228 llvm::Value
*NumElements
,
229 const CXXNewExpr
*expr
,
230 QualType ElementType
) {
231 // Should never be called.
232 ErrorUnsupportedABI(CGF
, "array cookie initialization");
233 return Address::invalid();
236 bool CGCXXABI::requiresArrayCookie(const CXXDeleteExpr
*expr
,
237 QualType elementType
) {
238 // If the class's usual deallocation function takes two arguments,
239 // it needs a cookie.
240 if (expr
->doesUsualArrayDeleteWantSize())
243 return elementType
.isDestructedType();
246 bool CGCXXABI::requiresArrayCookie(const CXXNewExpr
*expr
) {
247 // If the class's usual deallocation function takes two arguments,
248 // it needs a cookie.
249 if (expr
->doesUsualArrayDeleteWantSize())
252 return expr
->getAllocatedType().isDestructedType();
255 void CGCXXABI::ReadArrayCookie(CodeGenFunction
&CGF
, Address ptr
,
256 const CXXDeleteExpr
*expr
, QualType eltTy
,
257 llvm::Value
*&numElements
,
258 llvm::Value
*&allocPtr
, CharUnits
&cookieSize
) {
259 // Derive a char* in the same address space as the pointer.
260 ptr
= ptr
.withElementType(CGF
.Int8Ty
);
262 // If we don't need an array cookie, bail out early.
263 if (!requiresArrayCookie(expr
, eltTy
)) {
264 allocPtr
= ptr
.emitRawPointer(CGF
);
265 numElements
= nullptr;
266 cookieSize
= CharUnits::Zero();
270 cookieSize
= getArrayCookieSizeImpl(eltTy
);
271 Address allocAddr
= CGF
.Builder
.CreateConstInBoundsByteGEP(ptr
, -cookieSize
);
272 allocPtr
= allocAddr
.emitRawPointer(CGF
);
273 numElements
= readArrayCookieImpl(CGF
, allocAddr
, cookieSize
);
276 llvm::Value
*CGCXXABI::readArrayCookieImpl(CodeGenFunction
&CGF
,
278 CharUnits cookieSize
) {
279 ErrorUnsupportedABI(CGF
, "reading a new[] cookie");
280 return llvm::ConstantInt::get(CGF
.SizeTy
, 0);
283 /// Returns the adjustment, in bytes, required for the given
284 /// member-pointer operation. Returns null if no adjustment is
286 llvm::Constant
*CGCXXABI::getMemberPointerAdjustment(const CastExpr
*E
) {
287 assert(E
->getCastKind() == CK_DerivedToBaseMemberPointer
||
288 E
->getCastKind() == CK_BaseToDerivedMemberPointer
);
290 QualType derivedType
;
291 if (E
->getCastKind() == CK_DerivedToBaseMemberPointer
)
292 derivedType
= E
->getSubExpr()->getType();
294 derivedType
= E
->getType();
296 const CXXRecordDecl
*derivedClass
=
297 derivedType
->castAs
<MemberPointerType
>()->getClass()->getAsCXXRecordDecl();
299 return CGM
.GetNonVirtualBaseClassOffset(derivedClass
,
305 CGCXXABI::EmitCtorCompleteObjectHandler(CodeGenFunction
&CGF
,
306 const CXXRecordDecl
*RD
) {
307 if (CGM
.getTarget().getCXXABI().hasConstructorVariants())
308 llvm_unreachable("shouldn't be called in this ABI");
310 ErrorUnsupportedABI(CGF
, "complete object detection in ctor");
314 void CGCXXABI::setCXXDestructorDLLStorage(llvm::GlobalValue
*GV
,
315 const CXXDestructorDecl
*Dtor
,
316 CXXDtorType DT
) const {
317 // Assume the base C++ ABI has no special rules for destructor variants.
318 CGM
.setDLLImportDLLExport(GV
, Dtor
);
321 llvm::GlobalValue::LinkageTypes
CGCXXABI::getCXXDestructorLinkage(
322 GVALinkage Linkage
, const CXXDestructorDecl
*Dtor
, CXXDtorType DT
) const {
323 // Delegate back to CGM by default.
324 return CGM
.getLLVMLinkageForDeclarator(Dtor
, Linkage
);
327 bool CGCXXABI::NeedsVTTParameter(GlobalDecl GD
) {
332 CGCXXABI::emitTerminateForUnexpectedException(CodeGenFunction
&CGF
,
334 // Just call std::terminate and ignore the violating exception.
335 return CGF
.EmitNounwindRuntimeCall(CGF
.CGM
.getTerminateFn());
338 CatchTypeInfo
CGCXXABI::getCatchAllTypeInfo() {
339 return CatchTypeInfo
{nullptr, 0};
342 std::vector
<CharUnits
> CGCXXABI::getVBPtrOffsets(const CXXRecordDecl
*RD
) {
343 return std::vector
<CharUnits
>();
346 CGCXXABI::AddedStructorArgCounts
CGCXXABI::addImplicitConstructorArgs(
347 CodeGenFunction
&CGF
, const CXXConstructorDecl
*D
, CXXCtorType Type
,
348 bool ForVirtualBase
, bool Delegating
, CallArgList
&Args
) {
349 AddedStructorArgs AddedArgs
=
350 getImplicitConstructorArgs(CGF
, D
, Type
, ForVirtualBase
, Delegating
);
351 for (size_t i
= 0; i
< AddedArgs
.Prefix
.size(); ++i
) {
352 Args
.insert(Args
.begin() + 1 + i
,
353 CallArg(RValue::get(AddedArgs
.Prefix
[i
].Value
),
354 AddedArgs
.Prefix
[i
].Type
));
356 for (const auto &arg
: AddedArgs
.Suffix
) {
357 Args
.add(RValue::get(arg
.Value
), arg
.Type
);
359 return AddedStructorArgCounts(AddedArgs
.Prefix
.size(),
360 AddedArgs
.Suffix
.size());