blob: 83426dc3a03c759838561610b028c3598e653b55 [file] [log] [blame]
Charles Davis4e786dd2010-05-25 19:52:27 +00001//===----- CGCXXABI.h - Interface to C++ ABIs -------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides an abstract class for C++ code generation. Concrete subclasses
11// of this implement code generation for specific C++ ABIs.
12//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000015#ifndef LLVM_CLANG_LIB_CODEGEN_CGCXXABI_H
16#define LLVM_CLANG_LIB_CODEGEN_CGCXXABI_H
Charles Davis4e786dd2010-05-25 19:52:27 +000017
John McCall5d865c322010-08-31 07:33:07 +000018#include "CodeGenFunction.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/Basic/LLVM.h"
John McCall5d865c322010-08-31 07:33:07 +000020
John McCall475999d2010-08-22 00:05:51 +000021namespace llvm {
Rafael Espindola14048092014-05-09 00:26:20 +000022class Constant;
23class Type;
24class Value;
Reid Klecknerfff8e7f2015-03-03 19:21:04 +000025class CallInst;
John McCall475999d2010-08-22 00:05:51 +000026}
27
Charles Davis4e786dd2010-05-25 19:52:27 +000028namespace clang {
Rafael Espindola14048092014-05-09 00:26:20 +000029class CastExpr;
30class CXXConstructorDecl;
31class CXXDestructorDecl;
32class CXXMethodDecl;
33class CXXRecordDecl;
34class FieldDecl;
35class MangleContext;
John McCall475999d2010-08-22 00:05:51 +000036
Charles Davis4e786dd2010-05-25 19:52:27 +000037namespace CodeGen {
John McCallb92ab1a2016-10-26 23:46:34 +000038class CGCallee;
Rafael Espindola14048092014-05-09 00:26:20 +000039class CodeGenFunction;
40class CodeGenModule;
Reid Kleckner10aa7702015-09-16 20:15:55 +000041struct CatchTypeInfo;
Charles Davis4e786dd2010-05-25 19:52:27 +000042
James Dennett6e5cffb2012-06-15 07:35:42 +000043/// \brief Implements C++ ABI-specific code generation functions.
Charles Davis53c59df2010-08-16 03:33:14 +000044class CGCXXABI {
John McCalla1dee5302010-08-22 10:59:02 +000045protected:
46 CodeGenModule &CGM;
Ahmed Charlesb8984322014-03-07 20:03:18 +000047 std::unique_ptr<MangleContext> MangleCtx;
John McCalla1dee5302010-08-22 10:59:02 +000048
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000049 CGCXXABI(CodeGenModule &CGM)
50 : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {}
John McCalla1dee5302010-08-22 10:59:02 +000051
John McCall5d865c322010-08-31 07:33:07 +000052protected:
John McCall7f416cc2015-09-08 08:05:57 +000053 ImplicitParamDecl *getThisDecl(CodeGenFunction &CGF) {
Eli Friedman9fbeba02012-02-11 02:57:39 +000054 return CGF.CXXABIThisDecl;
John McCall5d865c322010-08-31 07:33:07 +000055 }
John McCall7f416cc2015-09-08 08:05:57 +000056 llvm::Value *getThisValue(CodeGenFunction &CGF) {
Eli Friedman9fbeba02012-02-11 02:57:39 +000057 return CGF.CXXABIThisValue;
John McCall5d865c322010-08-31 07:33:07 +000058 }
John McCall7f416cc2015-09-08 08:05:57 +000059 Address getThisAddress(CodeGenFunction &CGF) {
60 return Address(CGF.CXXABIThisValue, CGF.CXXABIThisAlignment);
61 }
John McCall5d865c322010-08-31 07:33:07 +000062
Reid Kleckner407e8b62013-03-22 19:02:54 +000063 /// Issue a diagnostic about unsupported features in the ABI.
64 void ErrorUnsupportedABI(CodeGenFunction &CGF, StringRef S);
65
66 /// Get a null value for unsupported member pointers.
67 llvm::Constant *GetBogusMemberPointer(QualType T);
68
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +000069 ImplicitParamDecl *&getStructorImplicitParamDecl(CodeGenFunction &CGF) {
70 return CGF.CXXStructorImplicitParamDecl;
71 }
72 llvm::Value *&getStructorImplicitParamValue(CodeGenFunction &CGF) {
73 return CGF.CXXStructorImplicitParamValue;
John McCall5d865c322010-08-31 07:33:07 +000074 }
75
Reid Kleckner06239e42017-11-16 19:09:36 +000076 /// Loads the incoming C++ this pointer as it was passed by the caller.
77 llvm::Value *loadIncomingCXXThis(CodeGenFunction &CGF);
78
79 void setCXXABIThisValue(CodeGenFunction &CGF, llvm::Value *ThisPtr);
John McCall5d865c322010-08-31 07:33:07 +000080
John McCall8ed55a52010-09-02 09:58:18 +000081 ASTContext &getContext() const { return CGM.getContext(); }
82
John McCallb91cd662012-05-01 05:23:51 +000083 virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType);
84 virtual bool requiresArrayCookie(const CXXNewExpr *E);
85
John McCall7f416cc2015-09-08 08:05:57 +000086 /// Determine whether there's something special about the rules of
87 /// the ABI tell us that 'this' is a complete object within the
88 /// given function. Obvious common logic like being defined on a
89 /// final class will have been taken care of by the caller.
90 virtual bool isThisCompleteObject(GlobalDecl GD) const = 0;
91
Charles Davis4e786dd2010-05-25 19:52:27 +000092public:
John McCalla1dee5302010-08-22 10:59:02 +000093
Anders Carlssone8ba4732010-11-28 17:50:09 +000094 virtual ~CGCXXABI();
Charles Davis4e786dd2010-05-25 19:52:27 +000095
96 /// Gets the mangle context.
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000097 MangleContext &getMangleContext() {
98 return *MangleCtx;
99 }
John McCall475999d2010-08-22 00:05:51 +0000100
Stephen Lin9dc6eef2013-06-30 20:40:16 +0000101 /// Returns true if the given constructor or destructor is one of the
102 /// kinds that the ABI says returns 'this' (only applies when called
103 /// non-virtually for destructors).
104 ///
105 /// There currently is no way to indicate if a destructor returns 'this'
106 /// when called virtually, and code generation does not support the case.
Manman Ren01754612013-03-20 16:59:38 +0000107 virtual bool HasThisReturn(GlobalDecl GD) const { return false; }
108
David Majnemer0c0b6d92014-10-31 20:09:12 +0000109 virtual bool hasMostDerivedReturn(GlobalDecl GD) const { return false; }
110
Derek Schuff8179be42016-05-10 17:44:55 +0000111 /// Returns true if the target allows calling a function through a pointer
112 /// with a different signature than the actual function (or equivalently,
113 /// bitcasting a function or function pointer to a different function type).
114 /// In principle in the most general case this could depend on the target, the
115 /// calling convention, and the actual types of the arguments and return
116 /// value. Here it just means whether the signature mismatch could *ever* be
117 /// allowed; in other words, does the target do strict checking of signatures
118 /// for all calls.
119 virtual bool canCallMismatchedFunctionType() const { return true; }
120
Reid Kleckner40ca9132014-05-13 22:05:45 +0000121 /// If the C++ ABI requires the given type be returned in a particular way,
122 /// this method sets RetAI and returns true.
123 virtual bool classifyReturnType(CGFunctionInfo &FI) const = 0;
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000124
125 /// Specify how one should pass an argument of a record type.
126 enum RecordArgABI {
127 /// Pass it using the normal C aggregate rules for the ABI, potentially
128 /// introducing extra copies and passing some or all of it in registers.
129 RAA_Default = 0,
130
131 /// Pass it on the stack using its defined layout. The argument must be
132 /// evaluated directly into the correct stack position in the arguments area,
133 /// and the call machinery must not move it or introduce extra copies.
134 RAA_DirectInMemory,
135
136 /// Pass it as a pointer to temporary memory.
137 RAA_Indirect
138 };
139
Reid Klecknercf87e102014-05-14 16:02:09 +0000140 /// Returns true if C++ allows us to copy the memory of an object of type RD
141 /// when it is passed as an argument.
142 bool canCopyArgument(const CXXRecordDecl *RD) const;
143
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000144 /// Returns how an argument of the given record type should be passed.
145 virtual RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const = 0;
146
Reid Kleckner37abaca2014-05-09 22:46:15 +0000147 /// Returns true if the implicit 'sret' parameter comes after the implicit
148 /// 'this' parameter of C++ instance methods.
149 virtual bool isSRetParameterAfterThis() const { return false; }
150
John McCall7a9aac22010-08-23 01:21:21 +0000151 /// Find the LLVM type used to represent the given member pointer
152 /// type.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000153 virtual llvm::Type *
John McCall7a9aac22010-08-23 01:21:21 +0000154 ConvertMemberPointerType(const MemberPointerType *MPT);
155
156 /// Load a member function from an object and a member function
157 /// pointer. Apply the this-adjustment and set 'This' to the
158 /// adjusted value.
John McCallb92ab1a2016-10-26 23:46:34 +0000159 virtual CGCallee EmitLoadOfMemberFunctionPointer(
John McCall7f416cc2015-09-08 08:05:57 +0000160 CodeGenFunction &CGF, const Expr *E, Address This,
161 llvm::Value *&ThisPtrForCall, llvm::Value *MemPtr,
162 const MemberPointerType *MPT);
John McCalla8bbb822010-08-22 03:04:22 +0000163
John McCallc134eb52010-08-31 21:07:20 +0000164 /// Calculate an l-value from an object and a data member pointer.
David Majnemer2b0d66d2014-02-20 23:22:07 +0000165 virtual llvm::Value *
166 EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E,
John McCall7f416cc2015-09-08 08:05:57 +0000167 Address Base, llvm::Value *MemPtr,
David Majnemer2b0d66d2014-02-20 23:22:07 +0000168 const MemberPointerType *MPT);
John McCallc134eb52010-08-31 21:07:20 +0000169
John McCallc62bb392012-02-15 01:22:51 +0000170 /// Perform a derived-to-base, base-to-derived, or bitcast member
171 /// pointer conversion.
John McCall7a9aac22010-08-23 01:21:21 +0000172 virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
173 const CastExpr *E,
174 llvm::Value *Src);
John McCall84fa5102010-08-22 04:16:24 +0000175
John McCallc62bb392012-02-15 01:22:51 +0000176 /// Perform a derived-to-base, base-to-derived, or bitcast member
177 /// pointer conversion on a constant value.
178 virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
179 llvm::Constant *Src);
180
John McCall7a9aac22010-08-23 01:21:21 +0000181 /// Return true if the given member pointer can be zero-initialized
182 /// (in the C++ sense) with an LLVM zeroinitializer.
John McCall614dbdc2010-08-22 21:01:12 +0000183 virtual bool isZeroInitializable(const MemberPointerType *MPT);
John McCall84fa5102010-08-22 04:16:24 +0000184
David Majnemerb3e56542014-08-07 22:56:13 +0000185 /// Return whether or not a member pointers type is convertible to an IR type.
186 virtual bool isMemberPointerConvertible(const MemberPointerType *MPT) const {
187 return true;
188 }
189
John McCall7a9aac22010-08-23 01:21:21 +0000190 /// Create a null member pointer of the given type.
191 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
John McCall84fa5102010-08-22 04:16:24 +0000192
John McCall7a9aac22010-08-23 01:21:21 +0000193 /// Create a member pointer for the given method.
David Majnemere2be95b2015-06-23 07:31:01 +0000194 virtual llvm::Constant *EmitMemberFunctionPointer(const CXXMethodDecl *MD);
John McCall1c456c82010-08-22 06:43:33 +0000195
John McCall7a9aac22010-08-23 01:21:21 +0000196 /// Create a member pointer for the given field.
John McCallf3a88602011-02-03 08:15:49 +0000197 virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
198 CharUnits offset);
John McCall131d97d2010-08-22 08:30:07 +0000199
Richard Smithdafff942012-01-14 04:30:29 +0000200 /// Create a member pointer for the given member pointer constant.
201 virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
202
John McCall7a9aac22010-08-23 01:21:21 +0000203 /// Emit a comparison between two member pointers. Returns an i1.
John McCall131d97d2010-08-22 08:30:07 +0000204 virtual llvm::Value *
John McCall7a9aac22010-08-23 01:21:21 +0000205 EmitMemberPointerComparison(CodeGenFunction &CGF,
206 llvm::Value *L,
207 llvm::Value *R,
208 const MemberPointerType *MPT,
209 bool Inequality);
John McCall131d97d2010-08-22 08:30:07 +0000210
John McCall7a9aac22010-08-23 01:21:21 +0000211 /// Determine if a member pointer is non-null. Returns an i1.
John McCall131d97d2010-08-22 08:30:07 +0000212 virtual llvm::Value *
John McCall7a9aac22010-08-23 01:21:21 +0000213 EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
214 llvm::Value *MemPtr,
215 const MemberPointerType *MPT);
John McCall5d865c322010-08-31 07:33:07 +0000216
John McCallc62bb392012-02-15 01:22:51 +0000217protected:
218 /// A utility method for computing the offset required for the given
219 /// base-to-derived or derived-to-base member-pointer conversion.
220 /// Does not handle virtual conversions (in case we ever fully
221 /// support an ABI that allows this). Returns null if no adjustment
222 /// is required.
223 llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
224
Reid Kleckner452abac2013-05-09 21:01:17 +0000225 /// \brief Computes the non-virtual adjustment needed for a member pointer
226 /// conversion along an inheritance path stored in an APValue. Unlike
227 /// getMemberPointerAdjustment(), the adjustment can be negative if the path
228 /// is from a derived type to a base type.
229 CharUnits getMemberPointerPathAdjustment(const APValue &MP);
230
John McCallc62bb392012-02-15 01:22:51 +0000231public:
David Majnemer0c0b6d92014-10-31 20:09:12 +0000232 virtual void emitVirtualObjectDelete(CodeGenFunction &CGF,
David Majnemer08681372014-11-01 07:37:17 +0000233 const CXXDeleteExpr *DE,
John McCall7f416cc2015-09-08 08:05:57 +0000234 Address Ptr, QualType ElementType,
David Majnemer0c0b6d92014-10-31 20:09:12 +0000235 const CXXDestructorDecl *Dtor) = 0;
David Majnemer442d0a22014-11-25 07:20:20 +0000236 virtual void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) = 0;
David Majnemer7c237072015-03-05 00:46:22 +0000237 virtual void emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) = 0;
David Majnemerba3e5ec2015-03-13 18:26:17 +0000238 virtual llvm::GlobalVariable *getThrowInfo(QualType T) { return nullptr; }
John McCall82fb8922012-09-25 10:10:39 +0000239
Piotr Padlewskid679d7e2015-09-15 00:37:06 +0000240 /// \brief Determine whether it's possible to emit a vtable for \p RD, even
241 /// though we do not know that the vtable has been marked as used by semantic
242 /// analysis.
243 virtual bool canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const = 0;
Piotr Padlewskia68a7872015-07-24 04:04:49 +0000244
Reid Klecknerfff8e7f2015-03-03 19:21:04 +0000245 virtual void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C) = 0;
246
247 virtual llvm::CallInst *
248 emitTerminateForUnexpectedException(CodeGenFunction &CGF,
249 llvm::Value *Exn);
250
David Majnemer443250f2015-03-17 20:35:00 +0000251 virtual llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) = 0;
Reid Kleckner10aa7702015-09-16 20:15:55 +0000252 virtual CatchTypeInfo
David Majnemer37b417f2015-03-29 21:55:10 +0000253 getAddrOfCXXCatchHandlerType(QualType Ty, QualType CatchHandlerType) = 0;
Reid Kleckner10aa7702015-09-16 20:15:55 +0000254 virtual CatchTypeInfo getCatchAllTypeInfo();
David Majnemere2cb8d12014-07-07 06:20:47 +0000255
David Majnemer1162d252014-06-22 19:05:33 +0000256 virtual bool shouldTypeidBeNullChecked(bool IsDeref,
257 QualType SrcRecordTy) = 0;
258 virtual void EmitBadTypeidCall(CodeGenFunction &CGF) = 0;
259 virtual llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy,
John McCall7f416cc2015-09-08 08:05:57 +0000260 Address ThisPtr,
David Majnemer1162d252014-06-22 19:05:33 +0000261 llvm::Type *StdTypeInfoPtrTy) = 0;
262
263 virtual bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
264 QualType SrcRecordTy) = 0;
265
266 virtual llvm::Value *
John McCall7f416cc2015-09-08 08:05:57 +0000267 EmitDynamicCastCall(CodeGenFunction &CGF, Address Value,
David Majnemer1162d252014-06-22 19:05:33 +0000268 QualType SrcRecordTy, QualType DestTy,
269 QualType DestRecordTy, llvm::BasicBlock *CastEnd) = 0;
270
271 virtual llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +0000272 Address Value,
David Majnemer1162d252014-06-22 19:05:33 +0000273 QualType SrcRecordTy,
274 QualType DestTy) = 0;
275
276 virtual bool EmitBadCastCall(CodeGenFunction &CGF) = 0;
277
Reid Klecknerd8cbeec2013-05-29 18:02:47 +0000278 virtual llvm::Value *GetVirtualBaseClassOffset(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +0000279 Address This,
Reid Klecknerd8cbeec2013-05-29 18:02:47 +0000280 const CXXRecordDecl *ClassDecl,
281 const CXXRecordDecl *BaseClassDecl) = 0;
282
Reid Kleckner7810af02013-06-19 15:20:38 +0000283 virtual llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
284 const CXXRecordDecl *RD);
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000285
Timur Iskhodzhanovb6487322013-10-09 18:16:58 +0000286 /// Emit the code to initialize hidden members required
287 /// to handle virtual inheritance, if needed by the ABI.
288 virtual void
289 initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF,
290 const CXXRecordDecl *RD) {}
291
Timur Iskhodzhanov40f2fa92013-08-04 17:30:04 +0000292 /// Emit constructor variants required by this ABI.
293 virtual void EmitCXXConstructors(const CXXConstructorDecl *D) = 0;
294
George Burgess IVf203dbf2017-02-22 20:28:02 +0000295 /// Notes how many arguments were added to the beginning (Prefix) and ending
296 /// (Suffix) of an arg list.
297 ///
298 /// Note that Prefix actually refers to the number of args *after* the first
299 /// one: `this` arguments always come first.
300 struct AddedStructorArgs {
301 unsigned Prefix = 0;
302 unsigned Suffix = 0;
303 AddedStructorArgs() = default;
304 AddedStructorArgs(unsigned P, unsigned S) : Prefix(P), Suffix(S) {}
305 static AddedStructorArgs prefix(unsigned N) { return {N, 0}; }
306 static AddedStructorArgs suffix(unsigned N) { return {0, N}; }
307 };
308
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000309 /// Build the signature of the given constructor or destructor variant by
310 /// adding any required parameters. For convenience, ArgTys has been
311 /// initialized with the type of 'this'.
George Burgess IVf203dbf2017-02-22 20:28:02 +0000312 virtual AddedStructorArgs
313 buildStructorSignature(const CXXMethodDecl *MD, StructorType T,
314 SmallVectorImpl<CanQualType> &ArgTys) = 0;
John McCall5d865c322010-08-31 07:33:07 +0000315
Reid Klecknere7de47e2013-07-22 13:51:44 +0000316 /// Returns true if the given destructor type should be emitted as a linkonce
317 /// delegating thunk, regardless of whether the dtor is defined in this TU or
318 /// not.
319 virtual bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor,
320 CXXDtorType DT) const = 0;
321
322 /// Emit destructor variants required by this ABI.
323 virtual void EmitCXXDestructors(const CXXDestructorDecl *D) = 0;
324
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000325 /// Get the type of the implicit "this" parameter used by a method. May return
326 /// zero if no specific type is applicable, e.g. if the ABI expects the "this"
327 /// parameter to point to some artificial offset in a complete object due to
328 /// vbases being reordered.
329 virtual const CXXRecordDecl *
330 getThisArgumentTypeForMethod(const CXXMethodDecl *MD) {
331 return MD->getParent();
332 }
333
334 /// Perform ABI-specific "this" argument adjustment required prior to
Timur Iskhodzhanovf1749422014-03-14 17:43:37 +0000335 /// a call of a virtual function.
336 /// The "VirtualCall" argument is true iff the call itself is virtual.
John McCall7f416cc2015-09-08 08:05:57 +0000337 virtual Address
Timur Iskhodzhanovf1749422014-03-14 17:43:37 +0000338 adjustThisArgumentForVirtualFunctionCall(CodeGenFunction &CGF, GlobalDecl GD,
John McCall7f416cc2015-09-08 08:05:57 +0000339 Address This, bool VirtualCall) {
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000340 return This;
341 }
342
Reid Kleckner89077a12013-12-17 19:46:40 +0000343 /// Build a parameter variable suitable for 'this'.
344 void buildThisParam(CodeGenFunction &CGF, FunctionArgList &Params);
345
346 /// Insert any ABI-specific implicit parameters into the parameter list for a
347 /// function. This generally involves extra data for constructors and
348 /// destructors.
John McCall5d865c322010-08-31 07:33:07 +0000349 ///
350 /// ABIs may also choose to override the return type, which has been
Stephen Lin9dc6eef2013-06-30 20:40:16 +0000351 /// initialized with the type of 'this' if HasThisReturn(CGF.CurGD) is true or
352 /// the formal return type of the function otherwise.
Reid Kleckner89077a12013-12-17 19:46:40 +0000353 virtual void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy,
354 FunctionArgList &Params) = 0;
John McCall5d865c322010-08-31 07:33:07 +0000355
Reid Kleckner0358cbf2016-07-01 02:41:25 +0000356 /// Get the ABI-specific "this" parameter adjustment to apply in the prologue
357 /// of a virtual function.
358 virtual CharUnits getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD) {
359 return CharUnits::Zero();
360 }
361
John McCall5d865c322010-08-31 07:33:07 +0000362 /// Emit the ABI-specific prolog for the function.
363 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
364
Reid Kleckner89077a12013-12-17 19:46:40 +0000365 /// Add any ABI-specific implicit arguments needed to call a constructor.
366 ///
George Burgess IVf203dbf2017-02-22 20:28:02 +0000367 /// \return The number of arguments added at the beginning and end of the
368 /// call, which is typically zero or one.
369 virtual AddedStructorArgs
Reid Kleckner89077a12013-12-17 19:46:40 +0000370 addImplicitConstructorArgs(CodeGenFunction &CGF, const CXXConstructorDecl *D,
371 CXXCtorType Type, bool ForVirtualBase,
372 bool Delegating, CallArgList &Args) = 0;
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000373
Reid Kleckner6fe771a2013-12-13 00:53:54 +0000374 /// Emit the destructor call.
375 virtual void EmitDestructorCall(CodeGenFunction &CGF,
376 const CXXDestructorDecl *DD, CXXDtorType Type,
377 bool ForVirtualBase, bool Delegating,
John McCall7f416cc2015-09-08 08:05:57 +0000378 Address This) = 0;
Reid Kleckner6fe771a2013-12-13 00:53:54 +0000379
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000380 /// Emits the VTable definitions required for the given record type.
381 virtual void emitVTableDefinitions(CodeGenVTables &CGVT,
382 const CXXRecordDecl *RD) = 0;
383
Piotr Padlewskid679d7e2015-09-15 00:37:06 +0000384 /// Checks if ABI requires extra virtual offset for vtable field.
385 virtual bool
386 isVirtualOffsetNeededForVTableField(CodeGenFunction &CGF,
387 CodeGenFunction::VPtr Vptr) = 0;
388
Simon Pilgrim2c518802017-03-30 14:13:19 +0000389 /// Checks if ABI requires to initialize vptrs for given dynamic class.
Piotr Padlewskid679d7e2015-09-15 00:37:06 +0000390 virtual bool doStructorsInitializeVPtrs(const CXXRecordDecl *VTableClass) = 0;
391
392 /// Get the address point of the vtable for the given base subobject.
393 virtual llvm::Constant *
394 getVTableAddressPoint(BaseSubobject Base,
395 const CXXRecordDecl *VTableClass) = 0;
396
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000397 /// Get the address point of the vtable for the given base subobject while
Piotr Padlewskid679d7e2015-09-15 00:37:06 +0000398 /// building a constructor or a destructor.
399 virtual llvm::Value *
400 getVTableAddressPointInStructor(CodeGenFunction &CGF, const CXXRecordDecl *RD,
401 BaseSubobject Base,
402 const CXXRecordDecl *NearestVBase) = 0;
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000403
404 /// Get the address point of the vtable for the given base subobject while
405 /// building a constexpr.
406 virtual llvm::Constant *
407 getVTableAddressPointForConstExpr(BaseSubobject Base,
408 const CXXRecordDecl *VTableClass) = 0;
409
410 /// Get the address of the vtable for the given record decl which should be
411 /// used for the vptr at the given offset in RD.
412 virtual llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD,
413 CharUnits VPtrOffset) = 0;
414
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000415 /// Build a virtual function pointer in the ABI-specific way.
John McCallb92ab1a2016-10-26 23:46:34 +0000416 virtual CGCallee getVirtualFunctionPointer(CodeGenFunction &CGF,
417 GlobalDecl GD,
418 Address This,
419 llvm::Type *Ty,
420 SourceLocation Loc) = 0;
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000421
Timur Iskhodzhanovd6197112013-02-15 14:45:22 +0000422 /// Emit the ABI-specific virtual destructor call.
David Majnemer0c0b6d92014-10-31 20:09:12 +0000423 virtual llvm::Value *
424 EmitVirtualDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *Dtor,
John McCall7f416cc2015-09-08 08:05:57 +0000425 CXXDtorType DtorType, Address This,
David Majnemer0c0b6d92014-10-31 20:09:12 +0000426 const CXXMemberCallExpr *CE) = 0;
Timur Iskhodzhanovd6197112013-02-15 14:45:22 +0000427
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +0000428 virtual void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF,
429 GlobalDecl GD,
430 CallArgList &CallArgs) {}
431
Reid Kleckner7810af02013-06-19 15:20:38 +0000432 /// Emit any tables needed to implement virtual inheritance. For Itanium,
433 /// this emits virtual table tables. For the MSVC++ ABI, this emits virtual
434 /// base tables.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000435 virtual void emitVirtualInheritanceTables(const CXXRecordDecl *RD) = 0;
Reid Kleckner7810af02013-06-19 15:20:38 +0000436
Hans Wennborgc94391d2014-06-06 20:04:01 +0000437 virtual void setThunkLinkage(llvm::Function *Thunk, bool ForVTable,
438 GlobalDecl GD, bool ReturnAdjustment) = 0;
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +0000439
Timur Iskhodzhanov02014322013-10-30 11:55:43 +0000440 virtual llvm::Value *performThisAdjustment(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +0000441 Address This,
Timur Iskhodzhanov02014322013-10-30 11:55:43 +0000442 const ThisAdjustment &TA) = 0;
443
444 virtual llvm::Value *performReturnAdjustment(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +0000445 Address Ret,
Timur Iskhodzhanov02014322013-10-30 11:55:43 +0000446 const ReturnAdjustment &RA) = 0;
447
John McCall5d865c322010-08-31 07:33:07 +0000448 virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
449 RValue RV, QualType ResultType);
John McCall8ed55a52010-09-02 09:58:18 +0000450
David Majnemer196ac332014-09-11 23:05:02 +0000451 virtual size_t getSrcArgforCopyCtor(const CXXConstructorDecl *,
452 FunctionArgList &Args) const = 0;
453
David Majnemer8671c6e2015-11-02 09:01:44 +0000454 /// Gets the offsets of all the virtual base pointers in a given class.
455 virtual std::vector<CharUnits> getVBPtrOffsets(const CXXRecordDecl *RD);
456
Joao Matos2ce88ef2012-07-17 17:10:11 +0000457 /// Gets the pure virtual member call function.
458 virtual StringRef GetPureVirtualCallName() = 0;
459
David Blaikieeb7d5982012-10-16 22:56:05 +0000460 /// Gets the deleted virtual member call name.
461 virtual StringRef GetDeletedVirtualCallName() = 0;
462
John McCall8ed55a52010-09-02 09:58:18 +0000463 /**************************** Array cookies ******************************/
464
465 /// Returns the extra size required in order to store the array
James Dennett41725122012-06-22 10:16:05 +0000466 /// cookie for the given new-expression. May return 0 to indicate that no
John McCall8ed55a52010-09-02 09:58:18 +0000467 /// array cookie is required.
468 ///
469 /// Several cases are filtered out before this method is called:
470 /// - non-array allocations never need a cookie
James Dennetta02e11f2012-06-20 00:57:15 +0000471 /// - calls to \::operator new(size_t, void*) never need a cookie
John McCall8ed55a52010-09-02 09:58:18 +0000472 ///
James Dennett41725122012-06-22 10:16:05 +0000473 /// \param expr - the new-expression being allocated.
John McCall284c48f2011-01-27 09:37:56 +0000474 virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
John McCall8ed55a52010-09-02 09:58:18 +0000475
476 /// Initialize the array cookie for the given allocation.
477 ///
478 /// \param NewPtr - a char* which is the presumed-non-null
479 /// return value of the allocation function
480 /// \param NumElements - the computed number of elements,
John McCallb91cd662012-05-01 05:23:51 +0000481 /// potentially collapsed from the multidimensional array case;
482 /// always a size_t
John McCall8ed55a52010-09-02 09:58:18 +0000483 /// \param ElementType - the base element allocated type,
484 /// i.e. the allocated type after stripping all array types
John McCall7f416cc2015-09-08 08:05:57 +0000485 virtual Address InitializeArrayCookie(CodeGenFunction &CGF,
486 Address NewPtr,
487 llvm::Value *NumElements,
488 const CXXNewExpr *expr,
489 QualType ElementType);
John McCall8ed55a52010-09-02 09:58:18 +0000490
491 /// Reads the array cookie associated with the given pointer,
492 /// if it has one.
493 ///
494 /// \param Ptr - a pointer to the first element in the array
495 /// \param ElementType - the base element type of elements of the array
496 /// \param NumElements - an out parameter which will be initialized
497 /// with the number of elements allocated, or zero if there is no
498 /// cookie
499 /// \param AllocPtr - an out parameter which will be initialized
500 /// with a char* pointing to the address returned by the allocation
501 /// function
502 /// \param CookieSize - an out parameter which will be initialized
503 /// with the size of the cookie, or zero if there is no cookie
John McCall7f416cc2015-09-08 08:05:57 +0000504 virtual void ReadArrayCookie(CodeGenFunction &CGF, Address Ptr,
John McCall284c48f2011-01-27 09:37:56 +0000505 const CXXDeleteExpr *expr,
John McCall8ed55a52010-09-02 09:58:18 +0000506 QualType ElementType, llvm::Value *&NumElements,
507 llvm::Value *&AllocPtr, CharUnits &CookieSize);
508
Peter Collingbourne66f82e62013-06-28 20:45:28 +0000509 /// Return whether the given global decl needs a VTT parameter.
510 virtual bool NeedsVTTParameter(GlobalDecl GD);
511
John McCallb91cd662012-05-01 05:23:51 +0000512protected:
513 /// Returns the extra size required in order to store the array
514 /// cookie for the given type. Assumes that an array cookie is
515 /// required.
516 virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
517
518 /// Reads the array cookie for an allocation which is known to have one.
519 /// This is called by the standard implementation of ReadArrayCookie.
520 ///
521 /// \param ptr - a pointer to the allocation made for an array, as a char*
522 /// \param cookieSize - the computed cookie size of an array
James Dennett6e5cffb2012-06-15 07:35:42 +0000523 ///
John McCallb91cd662012-05-01 05:23:51 +0000524 /// Other parameters are as above.
James Dennett6e5cffb2012-06-15 07:35:42 +0000525 ///
John McCallb91cd662012-05-01 05:23:51 +0000526 /// \return a size_t
John McCall7f416cc2015-09-08 08:05:57 +0000527 virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF, Address ptr,
John McCallb91cd662012-05-01 05:23:51 +0000528 CharUnits cookieSize);
529
530public:
531
John McCall68ff0372010-09-08 01:44:27 +0000532 /*************************** Static local guards ****************************/
533
John McCallcdf7ef52010-11-06 09:44:32 +0000534 /// Emits the guarded initializer and destructor setup for the given
535 /// variable, given that it couldn't be emitted as a constant.
Richard Smith6331c402012-02-13 22:16:19 +0000536 /// If \p PerformInit is false, the initialization has been folded to a
537 /// constant and should not be performed.
John McCallcdf7ef52010-11-06 09:44:32 +0000538 ///
539 /// The variable may be:
540 /// - a static local variable
541 /// - a static data member of a class template instantiation
542 virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
Reid Klecknerd8110b62013-09-10 20:14:30 +0000543 llvm::GlobalVariable *DeclPtr,
544 bool PerformInit) = 0;
John McCall68ff0372010-09-08 01:44:27 +0000545
John McCallc84ed6a2012-05-01 06:13:13 +0000546 /// Emit code to force the execution of a destructor during global
547 /// teardown. The default implementation of this uses atexit.
548 ///
David Majnemerb3341ea2014-10-05 05:05:40 +0000549 /// \param Dtor - a function taking a single pointer argument
550 /// \param Addr - a pointer to pass to the destructor function.
Richard Smithdbf74ba2013-04-14 23:01:42 +0000551 virtual void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
David Majnemerb3341ea2014-10-05 05:05:40 +0000552 llvm::Constant *Dtor,
553 llvm::Constant *Addr) = 0;
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000554
555 /*************************** thread_local initialization ********************/
556
557 /// Emits ABI-required functions necessary to initialize thread_local
558 /// variables in this translation unit.
559 ///
David Majnemerb3341ea2014-10-05 05:05:40 +0000560 /// \param CXXThreadLocals - The thread_local declarations in this translation
561 /// unit.
562 /// \param CXXThreadLocalInits - If this translation unit contains any
563 /// non-constant initialization or non-trivial destruction for
564 /// thread_local variables, a list of functions to perform the
565 /// initialization.
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000566 virtual void EmitThreadLocalInitFuncs(
Richard Smith5a99c492015-12-01 01:10:48 +0000567 CodeGenModule &CGM, ArrayRef<const VarDecl *> CXXThreadLocals,
David Majnemerb3341ea2014-10-05 05:05:40 +0000568 ArrayRef<llvm::Function *> CXXThreadLocalInits,
Richard Smith5a99c492015-12-01 01:10:48 +0000569 ArrayRef<const VarDecl *> CXXThreadLocalInitVars) = 0;
David Majnemerb3341ea2014-10-05 05:05:40 +0000570
571 // Determine if references to thread_local global variables can be made
572 // directly or require access through a thread wrapper function.
573 virtual bool usesThreadWrapperFunction() const = 0;
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000574
575 /// Emit a reference to a non-local thread_local variable (including
576 /// triggering the initialization of all thread_local variables in its
577 /// translation unit).
Richard Smith0f383742014-03-26 22:48:22 +0000578 virtual LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF,
579 const VarDecl *VD,
David Majnemerb3341ea2014-10-05 05:05:40 +0000580 QualType LValType) = 0;
Rafael Espindola91f68b42014-09-15 19:20:10 +0000581
582 /// Emit a single constructor/destructor with the given type from a C++
583 /// constructor Decl.
584 virtual void emitCXXStructor(const CXXMethodDecl *MD, StructorType Type) = 0;
Peter Collingbourne60108802017-12-13 21:53:04 +0000585
586 /// Load a vtable from This, an object of polymorphic type RD, or from one of
587 /// its virtual bases if it does not have its own vtable. Returns the vtable
588 /// and the class from which the vtable was loaded.
589 virtual std::pair<llvm::Value *, const CXXRecordDecl *>
590 LoadVTablePtr(CodeGenFunction &CGF, Address This,
591 const CXXRecordDecl *RD) = 0;
Charles Davis4e786dd2010-05-25 19:52:27 +0000592};
593
John McCall57625922013-01-25 23:36:14 +0000594// Create an instance of a C++ ABI class:
595
596/// Creates an Itanium-family ABI.
Charles Davis53c59df2010-08-16 03:33:14 +0000597CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
John McCall57625922013-01-25 23:36:14 +0000598
599/// Creates a Microsoft-family ABI.
Charles Davis53c59df2010-08-16 03:33:14 +0000600CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);
John McCall475999d2010-08-22 00:05:51 +0000601
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000602}
603}
Charles Davis4e786dd2010-05-25 19:52:27 +0000604
605#endif