blob: bff49be7a3c4552569085cef7ecf86d64db770a7 [file] [log] [blame]
Charles Davis4e786dd2010-05-25 19:52:27 +00001//===----- CGCXXABI.h - Interface to C++ ABIs -------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Charles Davis4e786dd2010-05-25 19:52:27 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This provides an abstract class for C++ code generation. Concrete subclasses
10// of this implement code generation for specific C++ ABIs.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_LIB_CODEGEN_CGCXXABI_H
15#define LLVM_CLANG_LIB_CODEGEN_CGCXXABI_H
Charles Davis4e786dd2010-05-25 19:52:27 +000016
John McCall5d865c322010-08-31 07:33:07 +000017#include "CodeGenFunction.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Basic/LLVM.h"
John McCall5d865c322010-08-31 07:33:07 +000019
John McCall475999d2010-08-22 00:05:51 +000020namespace llvm {
Rafael Espindola14048092014-05-09 00:26:20 +000021class Constant;
22class Type;
23class Value;
Reid Klecknerfff8e7f2015-03-03 19:21:04 +000024class CallInst;
John McCall475999d2010-08-22 00:05:51 +000025}
26
Charles Davis4e786dd2010-05-25 19:52:27 +000027namespace clang {
Rafael Espindola14048092014-05-09 00:26:20 +000028class CastExpr;
29class CXXConstructorDecl;
30class CXXDestructorDecl;
31class CXXMethodDecl;
32class CXXRecordDecl;
33class FieldDecl;
34class MangleContext;
John McCall475999d2010-08-22 00:05:51 +000035
Charles Davis4e786dd2010-05-25 19:52:27 +000036namespace CodeGen {
John McCallb92ab1a2016-10-26 23:46:34 +000037class CGCallee;
Rafael Espindola14048092014-05-09 00:26:20 +000038class CodeGenFunction;
39class CodeGenModule;
Reid Kleckner10aa7702015-09-16 20:15:55 +000040struct CatchTypeInfo;
Charles Davis4e786dd2010-05-25 19:52:27 +000041
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000042/// Implements C++ ABI-specific code generation functions.
Charles Davis53c59df2010-08-16 03:33:14 +000043class CGCXXABI {
John McCalla1dee5302010-08-22 10:59:02 +000044protected:
45 CodeGenModule &CGM;
Ahmed Charlesb8984322014-03-07 20:03:18 +000046 std::unique_ptr<MangleContext> MangleCtx;
John McCalla1dee5302010-08-22 10:59:02 +000047
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000048 CGCXXABI(CodeGenModule &CGM)
49 : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {}
John McCalla1dee5302010-08-22 10:59:02 +000050
John McCall5d865c322010-08-31 07:33:07 +000051protected:
John McCall7f416cc2015-09-08 08:05:57 +000052 ImplicitParamDecl *getThisDecl(CodeGenFunction &CGF) {
Eli Friedman9fbeba02012-02-11 02:57:39 +000053 return CGF.CXXABIThisDecl;
John McCall5d865c322010-08-31 07:33:07 +000054 }
John McCall7f416cc2015-09-08 08:05:57 +000055 llvm::Value *getThisValue(CodeGenFunction &CGF) {
Eli Friedman9fbeba02012-02-11 02:57:39 +000056 return CGF.CXXABIThisValue;
John McCall5d865c322010-08-31 07:33:07 +000057 }
John McCall7f416cc2015-09-08 08:05:57 +000058 Address getThisAddress(CodeGenFunction &CGF) {
59 return Address(CGF.CXXABIThisValue, CGF.CXXABIThisAlignment);
60 }
John McCall5d865c322010-08-31 07:33:07 +000061
Reid Kleckner407e8b62013-03-22 19:02:54 +000062 /// Issue a diagnostic about unsupported features in the ABI.
63 void ErrorUnsupportedABI(CodeGenFunction &CGF, StringRef S);
64
65 /// Get a null value for unsupported member pointers.
66 llvm::Constant *GetBogusMemberPointer(QualType T);
67
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +000068 ImplicitParamDecl *&getStructorImplicitParamDecl(CodeGenFunction &CGF) {
69 return CGF.CXXStructorImplicitParamDecl;
70 }
71 llvm::Value *&getStructorImplicitParamValue(CodeGenFunction &CGF) {
72 return CGF.CXXStructorImplicitParamValue;
John McCall5d865c322010-08-31 07:33:07 +000073 }
74
Reid Kleckner06239e42017-11-16 19:09:36 +000075 /// Loads the incoming C++ this pointer as it was passed by the caller.
76 llvm::Value *loadIncomingCXXThis(CodeGenFunction &CGF);
77
78 void setCXXABIThisValue(CodeGenFunction &CGF, llvm::Value *ThisPtr);
John McCall5d865c322010-08-31 07:33:07 +000079
John McCall8ed55a52010-09-02 09:58:18 +000080 ASTContext &getContext() const { return CGM.getContext(); }
81
John McCallb91cd662012-05-01 05:23:51 +000082 virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType);
83 virtual bool requiresArrayCookie(const CXXNewExpr *E);
84
John McCall7f416cc2015-09-08 08:05:57 +000085 /// Determine whether there's something special about the rules of
86 /// the ABI tell us that 'this' is a complete object within the
87 /// given function. Obvious common logic like being defined on a
88 /// final class will have been taken care of by the caller.
89 virtual bool isThisCompleteObject(GlobalDecl GD) const = 0;
90
Charles Davis4e786dd2010-05-25 19:52:27 +000091public:
John McCalla1dee5302010-08-22 10:59:02 +000092
Anders Carlssone8ba4732010-11-28 17:50:09 +000093 virtual ~CGCXXABI();
Charles Davis4e786dd2010-05-25 19:52:27 +000094
95 /// Gets the mangle context.
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000096 MangleContext &getMangleContext() {
97 return *MangleCtx;
98 }
John McCall475999d2010-08-22 00:05:51 +000099
Stephen Lin9dc6eef2013-06-30 20:40:16 +0000100 /// Returns true if the given constructor or destructor is one of the
101 /// kinds that the ABI says returns 'this' (only applies when called
102 /// non-virtually for destructors).
103 ///
104 /// There currently is no way to indicate if a destructor returns 'this'
105 /// when called virtually, and code generation does not support the case.
Manman Ren01754612013-03-20 16:59:38 +0000106 virtual bool HasThisReturn(GlobalDecl GD) const { return false; }
107
David Majnemer0c0b6d92014-10-31 20:09:12 +0000108 virtual bool hasMostDerivedReturn(GlobalDecl GD) const { return false; }
109
Derek Schuff8179be42016-05-10 17:44:55 +0000110 /// Returns true if the target allows calling a function through a pointer
111 /// with a different signature than the actual function (or equivalently,
112 /// bitcasting a function or function pointer to a different function type).
113 /// In principle in the most general case this could depend on the target, the
114 /// calling convention, and the actual types of the arguments and return
115 /// value. Here it just means whether the signature mismatch could *ever* be
116 /// allowed; in other words, does the target do strict checking of signatures
117 /// for all calls.
118 virtual bool canCallMismatchedFunctionType() const { return true; }
119
Reid Kleckner40ca9132014-05-13 22:05:45 +0000120 /// If the C++ ABI requires the given type be returned in a particular way,
121 /// this method sets RetAI and returns true.
122 virtual bool classifyReturnType(CGFunctionInfo &FI) const = 0;
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000123
124 /// Specify how one should pass an argument of a record type.
125 enum RecordArgABI {
126 /// Pass it using the normal C aggregate rules for the ABI, potentially
127 /// introducing extra copies and passing some or all of it in registers.
128 RAA_Default = 0,
129
130 /// Pass it on the stack using its defined layout. The argument must be
131 /// evaluated directly into the correct stack position in the arguments area,
132 /// and the call machinery must not move it or introduce extra copies.
133 RAA_DirectInMemory,
134
135 /// Pass it as a pointer to temporary memory.
136 RAA_Indirect
137 };
138
139 /// Returns how an argument of the given record type should be passed.
140 virtual RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const = 0;
141
Reid Kleckner37abaca2014-05-09 22:46:15 +0000142 /// Returns true if the implicit 'sret' parameter comes after the implicit
143 /// 'this' parameter of C++ instance methods.
144 virtual bool isSRetParameterAfterThis() const { return false; }
145
John McCall7a9aac22010-08-23 01:21:21 +0000146 /// Find the LLVM type used to represent the given member pointer
147 /// type.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000148 virtual llvm::Type *
John McCall7a9aac22010-08-23 01:21:21 +0000149 ConvertMemberPointerType(const MemberPointerType *MPT);
150
151 /// Load a member function from an object and a member function
152 /// pointer. Apply the this-adjustment and set 'This' to the
153 /// adjusted value.
John McCallb92ab1a2016-10-26 23:46:34 +0000154 virtual CGCallee EmitLoadOfMemberFunctionPointer(
John McCall7f416cc2015-09-08 08:05:57 +0000155 CodeGenFunction &CGF, const Expr *E, Address This,
156 llvm::Value *&ThisPtrForCall, llvm::Value *MemPtr,
157 const MemberPointerType *MPT);
John McCalla8bbb822010-08-22 03:04:22 +0000158
John McCallc134eb52010-08-31 21:07:20 +0000159 /// Calculate an l-value from an object and a data member pointer.
David Majnemer2b0d66d2014-02-20 23:22:07 +0000160 virtual llvm::Value *
161 EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E,
John McCall7f416cc2015-09-08 08:05:57 +0000162 Address Base, llvm::Value *MemPtr,
David Majnemer2b0d66d2014-02-20 23:22:07 +0000163 const MemberPointerType *MPT);
John McCallc134eb52010-08-31 21:07:20 +0000164
John McCallc62bb392012-02-15 01:22:51 +0000165 /// Perform a derived-to-base, base-to-derived, or bitcast member
166 /// pointer conversion.
John McCall7a9aac22010-08-23 01:21:21 +0000167 virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
168 const CastExpr *E,
169 llvm::Value *Src);
John McCall84fa5102010-08-22 04:16:24 +0000170
John McCallc62bb392012-02-15 01:22:51 +0000171 /// Perform a derived-to-base, base-to-derived, or bitcast member
172 /// pointer conversion on a constant value.
173 virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
174 llvm::Constant *Src);
175
John McCall7a9aac22010-08-23 01:21:21 +0000176 /// Return true if the given member pointer can be zero-initialized
177 /// (in the C++ sense) with an LLVM zeroinitializer.
John McCall614dbdc2010-08-22 21:01:12 +0000178 virtual bool isZeroInitializable(const MemberPointerType *MPT);
John McCall84fa5102010-08-22 04:16:24 +0000179
David Majnemerb3e56542014-08-07 22:56:13 +0000180 /// Return whether or not a member pointers type is convertible to an IR type.
181 virtual bool isMemberPointerConvertible(const MemberPointerType *MPT) const {
182 return true;
183 }
184
John McCall7a9aac22010-08-23 01:21:21 +0000185 /// Create a null member pointer of the given type.
186 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
John McCall84fa5102010-08-22 04:16:24 +0000187
John McCall7a9aac22010-08-23 01:21:21 +0000188 /// Create a member pointer for the given method.
David Majnemere2be95b2015-06-23 07:31:01 +0000189 virtual llvm::Constant *EmitMemberFunctionPointer(const CXXMethodDecl *MD);
John McCall1c456c82010-08-22 06:43:33 +0000190
John McCall7a9aac22010-08-23 01:21:21 +0000191 /// Create a member pointer for the given field.
John McCallf3a88602011-02-03 08:15:49 +0000192 virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
193 CharUnits offset);
John McCall131d97d2010-08-22 08:30:07 +0000194
Richard Smithdafff942012-01-14 04:30:29 +0000195 /// Create a member pointer for the given member pointer constant.
196 virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
197
John McCall7a9aac22010-08-23 01:21:21 +0000198 /// Emit a comparison between two member pointers. Returns an i1.
John McCall131d97d2010-08-22 08:30:07 +0000199 virtual llvm::Value *
John McCall7a9aac22010-08-23 01:21:21 +0000200 EmitMemberPointerComparison(CodeGenFunction &CGF,
201 llvm::Value *L,
202 llvm::Value *R,
203 const MemberPointerType *MPT,
204 bool Inequality);
John McCall131d97d2010-08-22 08:30:07 +0000205
John McCall7a9aac22010-08-23 01:21:21 +0000206 /// Determine if a member pointer is non-null. Returns an i1.
John McCall131d97d2010-08-22 08:30:07 +0000207 virtual llvm::Value *
John McCall7a9aac22010-08-23 01:21:21 +0000208 EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
209 llvm::Value *MemPtr,
210 const MemberPointerType *MPT);
John McCall5d865c322010-08-31 07:33:07 +0000211
John McCallc62bb392012-02-15 01:22:51 +0000212protected:
213 /// A utility method for computing the offset required for the given
214 /// base-to-derived or derived-to-base member-pointer conversion.
215 /// Does not handle virtual conversions (in case we ever fully
216 /// support an ABI that allows this). Returns null if no adjustment
217 /// is required.
218 llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
219
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000220 /// Computes the non-virtual adjustment needed for a member pointer
Reid Kleckner452abac2013-05-09 21:01:17 +0000221 /// conversion along an inheritance path stored in an APValue. Unlike
222 /// getMemberPointerAdjustment(), the adjustment can be negative if the path
223 /// is from a derived type to a base type.
224 CharUnits getMemberPointerPathAdjustment(const APValue &MP);
225
John McCallc62bb392012-02-15 01:22:51 +0000226public:
David Majnemer0c0b6d92014-10-31 20:09:12 +0000227 virtual void emitVirtualObjectDelete(CodeGenFunction &CGF,
David Majnemer08681372014-11-01 07:37:17 +0000228 const CXXDeleteExpr *DE,
John McCall7f416cc2015-09-08 08:05:57 +0000229 Address Ptr, QualType ElementType,
David Majnemer0c0b6d92014-10-31 20:09:12 +0000230 const CXXDestructorDecl *Dtor) = 0;
David Majnemer442d0a22014-11-25 07:20:20 +0000231 virtual void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) = 0;
David Majnemer7c237072015-03-05 00:46:22 +0000232 virtual void emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) = 0;
David Majnemerba3e5ec2015-03-13 18:26:17 +0000233 virtual llvm::GlobalVariable *getThrowInfo(QualType T) { return nullptr; }
John McCall82fb8922012-09-25 10:10:39 +0000234
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000235 /// Determine whether it's possible to emit a vtable for \p RD, even
Piotr Padlewskid679d7e2015-09-15 00:37:06 +0000236 /// though we do not know that the vtable has been marked as used by semantic
237 /// analysis.
238 virtual bool canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const = 0;
Piotr Padlewskia68a7872015-07-24 04:04:49 +0000239
Reid Klecknerfff8e7f2015-03-03 19:21:04 +0000240 virtual void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C) = 0;
241
242 virtual llvm::CallInst *
243 emitTerminateForUnexpectedException(CodeGenFunction &CGF,
244 llvm::Value *Exn);
245
David Majnemer443250f2015-03-17 20:35:00 +0000246 virtual llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) = 0;
Reid Kleckner10aa7702015-09-16 20:15:55 +0000247 virtual CatchTypeInfo
David Majnemer37b417f2015-03-29 21:55:10 +0000248 getAddrOfCXXCatchHandlerType(QualType Ty, QualType CatchHandlerType) = 0;
Reid Kleckner10aa7702015-09-16 20:15:55 +0000249 virtual CatchTypeInfo getCatchAllTypeInfo();
David Majnemere2cb8d12014-07-07 06:20:47 +0000250
David Majnemer1162d252014-06-22 19:05:33 +0000251 virtual bool shouldTypeidBeNullChecked(bool IsDeref,
252 QualType SrcRecordTy) = 0;
253 virtual void EmitBadTypeidCall(CodeGenFunction &CGF) = 0;
254 virtual llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy,
John McCall7f416cc2015-09-08 08:05:57 +0000255 Address ThisPtr,
David Majnemer1162d252014-06-22 19:05:33 +0000256 llvm::Type *StdTypeInfoPtrTy) = 0;
257
258 virtual bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
259 QualType SrcRecordTy) = 0;
260
261 virtual llvm::Value *
John McCall7f416cc2015-09-08 08:05:57 +0000262 EmitDynamicCastCall(CodeGenFunction &CGF, Address Value,
David Majnemer1162d252014-06-22 19:05:33 +0000263 QualType SrcRecordTy, QualType DestTy,
264 QualType DestRecordTy, llvm::BasicBlock *CastEnd) = 0;
265
266 virtual llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +0000267 Address Value,
David Majnemer1162d252014-06-22 19:05:33 +0000268 QualType SrcRecordTy,
269 QualType DestTy) = 0;
270
271 virtual bool EmitBadCastCall(CodeGenFunction &CGF) = 0;
272
Reid Klecknerd8cbeec2013-05-29 18:02:47 +0000273 virtual llvm::Value *GetVirtualBaseClassOffset(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +0000274 Address This,
Reid Klecknerd8cbeec2013-05-29 18:02:47 +0000275 const CXXRecordDecl *ClassDecl,
276 const CXXRecordDecl *BaseClassDecl) = 0;
277
Reid Kleckner7810af02013-06-19 15:20:38 +0000278 virtual llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
279 const CXXRecordDecl *RD);
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000280
Timur Iskhodzhanovb6487322013-10-09 18:16:58 +0000281 /// Emit the code to initialize hidden members required
282 /// to handle virtual inheritance, if needed by the ABI.
283 virtual void
284 initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF,
285 const CXXRecordDecl *RD) {}
286
Timur Iskhodzhanov40f2fa92013-08-04 17:30:04 +0000287 /// Emit constructor variants required by this ABI.
288 virtual void EmitCXXConstructors(const CXXConstructorDecl *D) = 0;
289
George Burgess IVf203dbf2017-02-22 20:28:02 +0000290 /// Notes how many arguments were added to the beginning (Prefix) and ending
291 /// (Suffix) of an arg list.
292 ///
293 /// Note that Prefix actually refers to the number of args *after* the first
294 /// one: `this` arguments always come first.
295 struct AddedStructorArgs {
296 unsigned Prefix = 0;
297 unsigned Suffix = 0;
298 AddedStructorArgs() = default;
299 AddedStructorArgs(unsigned P, unsigned S) : Prefix(P), Suffix(S) {}
300 static AddedStructorArgs prefix(unsigned N) { return {N, 0}; }
301 static AddedStructorArgs suffix(unsigned N) { return {0, N}; }
302 };
303
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000304 /// Build the signature of the given constructor or destructor variant by
305 /// adding any required parameters. For convenience, ArgTys has been
306 /// initialized with the type of 'this'.
George Burgess IVf203dbf2017-02-22 20:28:02 +0000307 virtual AddedStructorArgs
Peter Collingbourned1c5b282019-03-22 23:05:10 +0000308 buildStructorSignature(GlobalDecl GD,
George Burgess IVf203dbf2017-02-22 20:28:02 +0000309 SmallVectorImpl<CanQualType> &ArgTys) = 0;
John McCall5d865c322010-08-31 07:33:07 +0000310
Reid Klecknere7de47e2013-07-22 13:51:44 +0000311 /// Returns true if the given destructor type should be emitted as a linkonce
312 /// delegating thunk, regardless of whether the dtor is defined in this TU or
313 /// not.
314 virtual bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor,
315 CXXDtorType DT) const = 0;
316
Reid Klecknerae9b0702018-03-16 19:40:50 +0000317 virtual void setCXXDestructorDLLStorage(llvm::GlobalValue *GV,
318 const CXXDestructorDecl *Dtor,
319 CXXDtorType DT) const;
320
321 virtual llvm::GlobalValue::LinkageTypes
322 getCXXDestructorLinkage(GVALinkage Linkage, const CXXDestructorDecl *Dtor,
323 CXXDtorType DT) const;
324
Reid Klecknere7de47e2013-07-22 13:51:44 +0000325 /// Emit destructor variants required by this ABI.
326 virtual void EmitCXXDestructors(const CXXDestructorDecl *D) = 0;
327
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000328 /// Get the type of the implicit "this" parameter used by a method. May return
329 /// zero if no specific type is applicable, e.g. if the ABI expects the "this"
330 /// parameter to point to some artificial offset in a complete object due to
331 /// vbases being reordered.
332 virtual const CXXRecordDecl *
333 getThisArgumentTypeForMethod(const CXXMethodDecl *MD) {
334 return MD->getParent();
335 }
336
337 /// Perform ABI-specific "this" argument adjustment required prior to
Timur Iskhodzhanovf1749422014-03-14 17:43:37 +0000338 /// a call of a virtual function.
339 /// The "VirtualCall" argument is true iff the call itself is virtual.
John McCall7f416cc2015-09-08 08:05:57 +0000340 virtual Address
Timur Iskhodzhanovf1749422014-03-14 17:43:37 +0000341 adjustThisArgumentForVirtualFunctionCall(CodeGenFunction &CGF, GlobalDecl GD,
John McCall7f416cc2015-09-08 08:05:57 +0000342 Address This, bool VirtualCall) {
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000343 return This;
344 }
345
Reid Kleckner89077a12013-12-17 19:46:40 +0000346 /// Build a parameter variable suitable for 'this'.
347 void buildThisParam(CodeGenFunction &CGF, FunctionArgList &Params);
348
349 /// Insert any ABI-specific implicit parameters into the parameter list for a
350 /// function. This generally involves extra data for constructors and
351 /// destructors.
John McCall5d865c322010-08-31 07:33:07 +0000352 ///
353 /// ABIs may also choose to override the return type, which has been
Stephen Lin9dc6eef2013-06-30 20:40:16 +0000354 /// initialized with the type of 'this' if HasThisReturn(CGF.CurGD) is true or
355 /// the formal return type of the function otherwise.
Reid Kleckner89077a12013-12-17 19:46:40 +0000356 virtual void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy,
357 FunctionArgList &Params) = 0;
John McCall5d865c322010-08-31 07:33:07 +0000358
Reid Kleckner0358cbf2016-07-01 02:41:25 +0000359 /// Get the ABI-specific "this" parameter adjustment to apply in the prologue
360 /// of a virtual function.
361 virtual CharUnits getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD) {
362 return CharUnits::Zero();
363 }
364
John McCall5d865c322010-08-31 07:33:07 +0000365 /// Emit the ABI-specific prolog for the function.
366 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
367
Reid Kleckner89077a12013-12-17 19:46:40 +0000368 /// Add any ABI-specific implicit arguments needed to call a constructor.
369 ///
George Burgess IVf203dbf2017-02-22 20:28:02 +0000370 /// \return The number of arguments added at the beginning and end of the
371 /// call, which is typically zero or one.
372 virtual AddedStructorArgs
Reid Kleckner89077a12013-12-17 19:46:40 +0000373 addImplicitConstructorArgs(CodeGenFunction &CGF, const CXXConstructorDecl *D,
374 CXXCtorType Type, bool ForVirtualBase,
375 bool Delegating, CallArgList &Args) = 0;
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000376
Reid Kleckner6fe771a2013-12-13 00:53:54 +0000377 /// Emit the destructor call.
378 virtual void EmitDestructorCall(CodeGenFunction &CGF,
379 const CXXDestructorDecl *DD, CXXDtorType Type,
380 bool ForVirtualBase, bool Delegating,
Marco Antognini88559632019-07-22 09:39:13 +0000381 Address This, QualType ThisTy) = 0;
Reid Kleckner6fe771a2013-12-13 00:53:54 +0000382
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000383 /// Emits the VTable definitions required for the given record type.
384 virtual void emitVTableDefinitions(CodeGenVTables &CGVT,
385 const CXXRecordDecl *RD) = 0;
386
Piotr Padlewskid679d7e2015-09-15 00:37:06 +0000387 /// Checks if ABI requires extra virtual offset for vtable field.
388 virtual bool
389 isVirtualOffsetNeededForVTableField(CodeGenFunction &CGF,
390 CodeGenFunction::VPtr Vptr) = 0;
391
Simon Pilgrim2c518802017-03-30 14:13:19 +0000392 /// Checks if ABI requires to initialize vptrs for given dynamic class.
Piotr Padlewskid679d7e2015-09-15 00:37:06 +0000393 virtual bool doStructorsInitializeVPtrs(const CXXRecordDecl *VTableClass) = 0;
394
395 /// Get the address point of the vtable for the given base subobject.
396 virtual llvm::Constant *
397 getVTableAddressPoint(BaseSubobject Base,
398 const CXXRecordDecl *VTableClass) = 0;
399
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000400 /// Get the address point of the vtable for the given base subobject while
Piotr Padlewskid679d7e2015-09-15 00:37:06 +0000401 /// building a constructor or a destructor.
402 virtual llvm::Value *
403 getVTableAddressPointInStructor(CodeGenFunction &CGF, const CXXRecordDecl *RD,
404 BaseSubobject Base,
405 const CXXRecordDecl *NearestVBase) = 0;
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000406
407 /// Get the address point of the vtable for the given base subobject while
408 /// building a constexpr.
409 virtual llvm::Constant *
410 getVTableAddressPointForConstExpr(BaseSubobject Base,
411 const CXXRecordDecl *VTableClass) = 0;
412
413 /// Get the address of the vtable for the given record decl which should be
414 /// used for the vptr at the given offset in RD.
415 virtual llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD,
416 CharUnits VPtrOffset) = 0;
417
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000418 /// Build a virtual function pointer in the ABI-specific way.
John McCall9831b842018-02-06 18:52:44 +0000419 virtual CGCallee getVirtualFunctionPointer(CodeGenFunction &CGF,
420 GlobalDecl GD, Address This,
421 llvm::Type *Ty,
422 SourceLocation Loc) = 0;
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000423
Marco Antognini88559632019-07-22 09:39:13 +0000424 using DeleteOrMemberCallExpr =
425 llvm::PointerUnion<const CXXDeleteExpr *, const CXXMemberCallExpr *>;
426
Timur Iskhodzhanovd6197112013-02-15 14:45:22 +0000427 /// Emit the ABI-specific virtual destructor call.
Marco Antognini88559632019-07-22 09:39:13 +0000428 virtual llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF,
429 const CXXDestructorDecl *Dtor,
430 CXXDtorType DtorType,
431 Address This,
432 DeleteOrMemberCallExpr E) = 0;
Timur Iskhodzhanovd6197112013-02-15 14:45:22 +0000433
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +0000434 virtual void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF,
435 GlobalDecl GD,
436 CallArgList &CallArgs) {}
437
Reid Kleckner7810af02013-06-19 15:20:38 +0000438 /// Emit any tables needed to implement virtual inheritance. For Itanium,
439 /// this emits virtual table tables. For the MSVC++ ABI, this emits virtual
440 /// base tables.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000441 virtual void emitVirtualInheritanceTables(const CXXRecordDecl *RD) = 0;
Reid Kleckner7810af02013-06-19 15:20:38 +0000442
Rafael Espindolab7350042018-03-01 00:35:47 +0000443 virtual bool exportThunk() = 0;
Hans Wennborgc94391d2014-06-06 20:04:01 +0000444 virtual void setThunkLinkage(llvm::Function *Thunk, bool ForVTable,
445 GlobalDecl GD, bool ReturnAdjustment) = 0;
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +0000446
Timur Iskhodzhanov02014322013-10-30 11:55:43 +0000447 virtual llvm::Value *performThisAdjustment(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +0000448 Address This,
Timur Iskhodzhanov02014322013-10-30 11:55:43 +0000449 const ThisAdjustment &TA) = 0;
450
451 virtual llvm::Value *performReturnAdjustment(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +0000452 Address Ret,
Timur Iskhodzhanov02014322013-10-30 11:55:43 +0000453 const ReturnAdjustment &RA) = 0;
454
John McCall5d865c322010-08-31 07:33:07 +0000455 virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
456 RValue RV, QualType ResultType);
John McCall8ed55a52010-09-02 09:58:18 +0000457
David Majnemer196ac332014-09-11 23:05:02 +0000458 virtual size_t getSrcArgforCopyCtor(const CXXConstructorDecl *,
459 FunctionArgList &Args) const = 0;
460
David Majnemer8671c6e2015-11-02 09:01:44 +0000461 /// Gets the offsets of all the virtual base pointers in a given class.
462 virtual std::vector<CharUnits> getVBPtrOffsets(const CXXRecordDecl *RD);
463
Joao Matos2ce88ef2012-07-17 17:10:11 +0000464 /// Gets the pure virtual member call function.
465 virtual StringRef GetPureVirtualCallName() = 0;
466
David Blaikieeb7d5982012-10-16 22:56:05 +0000467 /// Gets the deleted virtual member call name.
468 virtual StringRef GetDeletedVirtualCallName() = 0;
469
John McCall8ed55a52010-09-02 09:58:18 +0000470 /**************************** Array cookies ******************************/
471
472 /// Returns the extra size required in order to store the array
James Dennett41725122012-06-22 10:16:05 +0000473 /// cookie for the given new-expression. May return 0 to indicate that no
John McCall8ed55a52010-09-02 09:58:18 +0000474 /// array cookie is required.
475 ///
476 /// Several cases are filtered out before this method is called:
477 /// - non-array allocations never need a cookie
James Dennetta02e11f2012-06-20 00:57:15 +0000478 /// - calls to \::operator new(size_t, void*) never need a cookie
John McCall8ed55a52010-09-02 09:58:18 +0000479 ///
James Dennett41725122012-06-22 10:16:05 +0000480 /// \param expr - the new-expression being allocated.
John McCall284c48f2011-01-27 09:37:56 +0000481 virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
John McCall8ed55a52010-09-02 09:58:18 +0000482
483 /// Initialize the array cookie for the given allocation.
484 ///
485 /// \param NewPtr - a char* which is the presumed-non-null
486 /// return value of the allocation function
487 /// \param NumElements - the computed number of elements,
John McCallb91cd662012-05-01 05:23:51 +0000488 /// potentially collapsed from the multidimensional array case;
489 /// always a size_t
John McCall8ed55a52010-09-02 09:58:18 +0000490 /// \param ElementType - the base element allocated type,
491 /// i.e. the allocated type after stripping all array types
John McCall7f416cc2015-09-08 08:05:57 +0000492 virtual Address InitializeArrayCookie(CodeGenFunction &CGF,
493 Address NewPtr,
494 llvm::Value *NumElements,
495 const CXXNewExpr *expr,
496 QualType ElementType);
John McCall8ed55a52010-09-02 09:58:18 +0000497
498 /// Reads the array cookie associated with the given pointer,
499 /// if it has one.
500 ///
501 /// \param Ptr - a pointer to the first element in the array
502 /// \param ElementType - the base element type of elements of the array
503 /// \param NumElements - an out parameter which will be initialized
504 /// with the number of elements allocated, or zero if there is no
505 /// cookie
506 /// \param AllocPtr - an out parameter which will be initialized
507 /// with a char* pointing to the address returned by the allocation
508 /// function
509 /// \param CookieSize - an out parameter which will be initialized
510 /// with the size of the cookie, or zero if there is no cookie
John McCall7f416cc2015-09-08 08:05:57 +0000511 virtual void ReadArrayCookie(CodeGenFunction &CGF, Address Ptr,
John McCall284c48f2011-01-27 09:37:56 +0000512 const CXXDeleteExpr *expr,
John McCall8ed55a52010-09-02 09:58:18 +0000513 QualType ElementType, llvm::Value *&NumElements,
514 llvm::Value *&AllocPtr, CharUnits &CookieSize);
515
Peter Collingbourne66f82e62013-06-28 20:45:28 +0000516 /// Return whether the given global decl needs a VTT parameter.
517 virtual bool NeedsVTTParameter(GlobalDecl GD);
518
John McCallb91cd662012-05-01 05:23:51 +0000519protected:
520 /// Returns the extra size required in order to store the array
521 /// cookie for the given type. Assumes that an array cookie is
522 /// required.
523 virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
524
525 /// Reads the array cookie for an allocation which is known to have one.
526 /// This is called by the standard implementation of ReadArrayCookie.
527 ///
528 /// \param ptr - a pointer to the allocation made for an array, as a char*
529 /// \param cookieSize - the computed cookie size of an array
James Dennett6e5cffb2012-06-15 07:35:42 +0000530 ///
John McCallb91cd662012-05-01 05:23:51 +0000531 /// Other parameters are as above.
James Dennett6e5cffb2012-06-15 07:35:42 +0000532 ///
John McCallb91cd662012-05-01 05:23:51 +0000533 /// \return a size_t
John McCall7f416cc2015-09-08 08:05:57 +0000534 virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF, Address ptr,
John McCallb91cd662012-05-01 05:23:51 +0000535 CharUnits cookieSize);
536
537public:
538
John McCall68ff0372010-09-08 01:44:27 +0000539 /*************************** Static local guards ****************************/
540
John McCallcdf7ef52010-11-06 09:44:32 +0000541 /// Emits the guarded initializer and destructor setup for the given
542 /// variable, given that it couldn't be emitted as a constant.
Richard Smith6331c402012-02-13 22:16:19 +0000543 /// If \p PerformInit is false, the initialization has been folded to a
544 /// constant and should not be performed.
John McCallcdf7ef52010-11-06 09:44:32 +0000545 ///
546 /// The variable may be:
547 /// - a static local variable
548 /// - a static data member of a class template instantiation
549 virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
Reid Klecknerd8110b62013-09-10 20:14:30 +0000550 llvm::GlobalVariable *DeclPtr,
551 bool PerformInit) = 0;
John McCall68ff0372010-09-08 01:44:27 +0000552
John McCallc84ed6a2012-05-01 06:13:13 +0000553 /// Emit code to force the execution of a destructor during global
554 /// teardown. The default implementation of this uses atexit.
555 ///
David Majnemerb3341ea2014-10-05 05:05:40 +0000556 /// \param Dtor - a function taking a single pointer argument
557 /// \param Addr - a pointer to pass to the destructor function.
Richard Smithdbf74ba2013-04-14 23:01:42 +0000558 virtual void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
James Y Knightf7321542019-02-07 01:14:17 +0000559 llvm::FunctionCallee Dtor,
David Majnemerb3341ea2014-10-05 05:05:40 +0000560 llvm::Constant *Addr) = 0;
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000561
562 /*************************** thread_local initialization ********************/
563
564 /// Emits ABI-required functions necessary to initialize thread_local
565 /// variables in this translation unit.
566 ///
David Majnemerb3341ea2014-10-05 05:05:40 +0000567 /// \param CXXThreadLocals - The thread_local declarations in this translation
568 /// unit.
569 /// \param CXXThreadLocalInits - If this translation unit contains any
570 /// non-constant initialization or non-trivial destruction for
571 /// thread_local variables, a list of functions to perform the
572 /// initialization.
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000573 virtual void EmitThreadLocalInitFuncs(
Richard Smith5a99c492015-12-01 01:10:48 +0000574 CodeGenModule &CGM, ArrayRef<const VarDecl *> CXXThreadLocals,
David Majnemerb3341ea2014-10-05 05:05:40 +0000575 ArrayRef<llvm::Function *> CXXThreadLocalInits,
Richard Smith5a99c492015-12-01 01:10:48 +0000576 ArrayRef<const VarDecl *> CXXThreadLocalInitVars) = 0;
David Majnemerb3341ea2014-10-05 05:05:40 +0000577
578 // Determine if references to thread_local global variables can be made
579 // directly or require access through a thread wrapper function.
Richard Smith00223822019-09-12 20:00:24 +0000580 virtual bool usesThreadWrapperFunction(const VarDecl *VD) const = 0;
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000581
582 /// Emit a reference to a non-local thread_local variable (including
583 /// triggering the initialization of all thread_local variables in its
584 /// translation unit).
Richard Smith0f383742014-03-26 22:48:22 +0000585 virtual LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF,
586 const VarDecl *VD,
David Majnemerb3341ea2014-10-05 05:05:40 +0000587 QualType LValType) = 0;
Rafael Espindola91f68b42014-09-15 19:20:10 +0000588
589 /// Emit a single constructor/destructor with the given type from a C++
590 /// constructor Decl.
Peter Collingbourned1c5b282019-03-22 23:05:10 +0000591 virtual void emitCXXStructor(GlobalDecl GD) = 0;
Peter Collingbourne60108802017-12-13 21:53:04 +0000592
593 /// Load a vtable from This, an object of polymorphic type RD, or from one of
594 /// its virtual bases if it does not have its own vtable. Returns the vtable
595 /// and the class from which the vtable was loaded.
596 virtual std::pair<llvm::Value *, const CXXRecordDecl *>
597 LoadVTablePtr(CodeGenFunction &CGF, Address This,
598 const CXXRecordDecl *RD) = 0;
Charles Davis4e786dd2010-05-25 19:52:27 +0000599};
600
John McCall57625922013-01-25 23:36:14 +0000601// Create an instance of a C++ ABI class:
602
603/// Creates an Itanium-family ABI.
Charles Davis53c59df2010-08-16 03:33:14 +0000604CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
John McCall57625922013-01-25 23:36:14 +0000605
606/// Creates a Microsoft-family ABI.
Charles Davis53c59df2010-08-16 03:33:14 +0000607CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);
John McCall475999d2010-08-22 00:05:51 +0000608
Heejin Ahnc6479192018-05-31 22:18:13 +0000609struct CatchRetScope final : EHScopeStack::Cleanup {
610 llvm::CatchPadInst *CPI;
611
612 CatchRetScope(llvm::CatchPadInst *CPI) : CPI(CPI) {}
613
614 void Emit(CodeGenFunction &CGF, Flags flags) override {
615 llvm::BasicBlock *BB = CGF.createBasicBlock("catchret.dest");
616 CGF.Builder.CreateCatchRet(CPI, BB);
617 CGF.EmitBlock(BB);
618 }
619};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000620}
621}
Charles Davis4e786dd2010-05-25 19:52:27 +0000622
623#endif