Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 1 | //===----- 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 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 15 | #ifndef LLVM_CLANG_LIB_CODEGEN_CGCXXABI_H |
| 16 | #define LLVM_CLANG_LIB_CODEGEN_CGCXXABI_H |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 17 | |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 18 | #include "CodeGenFunction.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/Basic/LLVM.h" |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 20 | |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 21 | namespace llvm { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 22 | class Constant; |
| 23 | class Type; |
| 24 | class Value; |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 27 | namespace clang { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 28 | class CastExpr; |
| 29 | class CXXConstructorDecl; |
| 30 | class CXXDestructorDecl; |
| 31 | class CXXMethodDecl; |
| 32 | class CXXRecordDecl; |
| 33 | class FieldDecl; |
| 34 | class MangleContext; |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 35 | |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 36 | namespace CodeGen { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 37 | class CodeGenFunction; |
| 38 | class CodeGenModule; |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 39 | |
James Dennett | 3b2adf2 | 2012-06-15 07:35:42 +0000 | [diff] [blame] | 40 | /// \brief Implements C++ ABI-specific code generation functions. |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 41 | class CGCXXABI { |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 42 | protected: |
| 43 | CodeGenModule &CGM; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 44 | std::unique_ptr<MangleContext> MangleCtx; |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 45 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 46 | CGCXXABI(CodeGenModule &CGM) |
| 47 | : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {} |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 48 | |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 49 | protected: |
| 50 | ImplicitParamDecl *&getThisDecl(CodeGenFunction &CGF) { |
Eli Friedman | cec5ebd | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 51 | return CGF.CXXABIThisDecl; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 52 | } |
| 53 | llvm::Value *&getThisValue(CodeGenFunction &CGF) { |
Eli Friedman | cec5ebd | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 54 | return CGF.CXXABIThisValue; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 57 | /// Issue a diagnostic about unsupported features in the ABI. |
| 58 | void ErrorUnsupportedABI(CodeGenFunction &CGF, StringRef S); |
| 59 | |
| 60 | /// Get a null value for unsupported member pointers. |
| 61 | llvm::Constant *GetBogusMemberPointer(QualType T); |
| 62 | |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 63 | ImplicitParamDecl *&getStructorImplicitParamDecl(CodeGenFunction &CGF) { |
| 64 | return CGF.CXXStructorImplicitParamDecl; |
| 65 | } |
| 66 | llvm::Value *&getStructorImplicitParamValue(CodeGenFunction &CGF) { |
| 67 | return CGF.CXXStructorImplicitParamValue; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 68 | } |
| 69 | |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 70 | /// Perform prolog initialization of the parameter variable suitable |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 71 | /// for 'this' emitted by buildThisParam. |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 72 | void EmitThisParam(CodeGenFunction &CGF); |
| 73 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 74 | ASTContext &getContext() const { return CGM.getContext(); } |
| 75 | |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 76 | virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType); |
| 77 | virtual bool requiresArrayCookie(const CXXNewExpr *E); |
| 78 | |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 79 | public: |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 80 | |
Anders Carlsson | 1af610f | 2010-11-28 17:50:09 +0000 | [diff] [blame] | 81 | virtual ~CGCXXABI(); |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 82 | |
| 83 | /// Gets the mangle context. |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 84 | MangleContext &getMangleContext() { |
| 85 | return *MangleCtx; |
| 86 | } |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 87 | |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 88 | /// Returns true if the given constructor or destructor is one of the |
| 89 | /// kinds that the ABI says returns 'this' (only applies when called |
| 90 | /// non-virtually for destructors). |
| 91 | /// |
| 92 | /// There currently is no way to indicate if a destructor returns 'this' |
| 93 | /// when called virtually, and code generation does not support the case. |
Manman Ren | 63fd408 | 2013-03-20 16:59:38 +0000 | [diff] [blame] | 94 | virtual bool HasThisReturn(GlobalDecl GD) const { return false; } |
| 95 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 96 | virtual bool hasMostDerivedReturn(GlobalDecl GD) const { return false; } |
| 97 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 98 | /// If the C++ ABI requires the given type be returned in a particular way, |
| 99 | /// this method sets RetAI and returns true. |
| 100 | virtual bool classifyReturnType(CGFunctionInfo &FI) const = 0; |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 101 | |
| 102 | /// Specify how one should pass an argument of a record type. |
| 103 | enum RecordArgABI { |
| 104 | /// Pass it using the normal C aggregate rules for the ABI, potentially |
| 105 | /// introducing extra copies and passing some or all of it in registers. |
| 106 | RAA_Default = 0, |
| 107 | |
| 108 | /// Pass it on the stack using its defined layout. The argument must be |
| 109 | /// evaluated directly into the correct stack position in the arguments area, |
| 110 | /// and the call machinery must not move it or introduce extra copies. |
| 111 | RAA_DirectInMemory, |
| 112 | |
| 113 | /// Pass it as a pointer to temporary memory. |
| 114 | RAA_Indirect |
| 115 | }; |
| 116 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 117 | /// Returns true if C++ allows us to copy the memory of an object of type RD |
| 118 | /// when it is passed as an argument. |
| 119 | bool canCopyArgument(const CXXRecordDecl *RD) const; |
| 120 | |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 121 | /// Returns how an argument of the given record type should be passed. |
| 122 | virtual RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const = 0; |
| 123 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 124 | /// Returns true if the implicit 'sret' parameter comes after the implicit |
| 125 | /// 'this' parameter of C++ instance methods. |
| 126 | virtual bool isSRetParameterAfterThis() const { return false; } |
| 127 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 128 | /// Find the LLVM type used to represent the given member pointer |
| 129 | /// type. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 130 | virtual llvm::Type * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 131 | ConvertMemberPointerType(const MemberPointerType *MPT); |
| 132 | |
| 133 | /// Load a member function from an object and a member function |
| 134 | /// pointer. Apply the this-adjustment and set 'This' to the |
| 135 | /// adjusted value. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 136 | virtual llvm::Value *EmitLoadOfMemberFunctionPointer( |
| 137 | CodeGenFunction &CGF, const Expr *E, llvm::Value *&This, |
| 138 | llvm::Value *MemPtr, const MemberPointerType *MPT); |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 139 | |
John McCall | 6c2ab1d | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 140 | /// Calculate an l-value from an object and a data member pointer. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 141 | virtual llvm::Value * |
| 142 | EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E, |
| 143 | llvm::Value *Base, llvm::Value *MemPtr, |
| 144 | const MemberPointerType *MPT); |
John McCall | 6c2ab1d | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 145 | |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 146 | /// Perform a derived-to-base, base-to-derived, or bitcast member |
| 147 | /// pointer conversion. |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 148 | virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 149 | const CastExpr *E, |
| 150 | llvm::Value *Src); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 151 | |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 152 | /// Perform a derived-to-base, base-to-derived, or bitcast member |
| 153 | /// pointer conversion on a constant value. |
| 154 | virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E, |
| 155 | llvm::Constant *Src); |
| 156 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 157 | /// Return true if the given member pointer can be zero-initialized |
| 158 | /// (in the C++ sense) with an LLVM zeroinitializer. |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 159 | virtual bool isZeroInitializable(const MemberPointerType *MPT); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 160 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 161 | /// Return whether or not a member pointers type is convertible to an IR type. |
| 162 | virtual bool isMemberPointerConvertible(const MemberPointerType *MPT) const { |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | virtual bool isTypeInfoCalculable(QualType Ty) const { |
| 167 | return !Ty->isIncompleteType(); |
| 168 | } |
| 169 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 170 | /// Create a null member pointer of the given type. |
| 171 | virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 172 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 173 | /// Create a member pointer for the given method. |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 174 | virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 175 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 176 | /// Create a member pointer for the given field. |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 177 | virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT, |
| 178 | CharUnits offset); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 179 | |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 180 | /// Create a member pointer for the given member pointer constant. |
| 181 | virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT); |
| 182 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 183 | /// Emit a comparison between two member pointers. Returns an i1. |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 184 | virtual llvm::Value * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 185 | EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 186 | llvm::Value *L, |
| 187 | llvm::Value *R, |
| 188 | const MemberPointerType *MPT, |
| 189 | bool Inequality); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 190 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 191 | /// Determine if a member pointer is non-null. Returns an i1. |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 192 | virtual llvm::Value * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 193 | EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 194 | llvm::Value *MemPtr, |
| 195 | const MemberPointerType *MPT); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 196 | |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 197 | protected: |
| 198 | /// A utility method for computing the offset required for the given |
| 199 | /// base-to-derived or derived-to-base member-pointer conversion. |
| 200 | /// Does not handle virtual conversions (in case we ever fully |
| 201 | /// support an ABI that allows this). Returns null if no adjustment |
| 202 | /// is required. |
| 203 | llvm::Constant *getMemberPointerAdjustment(const CastExpr *E); |
| 204 | |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 205 | /// \brief Computes the non-virtual adjustment needed for a member pointer |
| 206 | /// conversion along an inheritance path stored in an APValue. Unlike |
| 207 | /// getMemberPointerAdjustment(), the adjustment can be negative if the path |
| 208 | /// is from a derived type to a base type. |
| 209 | CharUnits getMemberPointerPathAdjustment(const APValue &MP); |
| 210 | |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 211 | public: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 212 | virtual void emitVirtualObjectDelete(CodeGenFunction &CGF, |
| 213 | const CXXDeleteExpr *DE, |
| 214 | llvm::Value *Ptr, QualType ElementType, |
| 215 | const CXXDestructorDecl *Dtor) = 0; |
John McCall | ecd03b4 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 216 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 217 | virtual llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) = 0; |
| 218 | |
| 219 | virtual bool shouldTypeidBeNullChecked(bool IsDeref, |
| 220 | QualType SrcRecordTy) = 0; |
| 221 | virtual void EmitBadTypeidCall(CodeGenFunction &CGF) = 0; |
| 222 | virtual llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy, |
| 223 | llvm::Value *ThisPtr, |
| 224 | llvm::Type *StdTypeInfoPtrTy) = 0; |
| 225 | |
| 226 | virtual bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr, |
| 227 | QualType SrcRecordTy) = 0; |
| 228 | |
| 229 | virtual llvm::Value * |
| 230 | EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value, |
| 231 | QualType SrcRecordTy, QualType DestTy, |
| 232 | QualType DestRecordTy, llvm::BasicBlock *CastEnd) = 0; |
| 233 | |
| 234 | virtual llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF, |
| 235 | llvm::Value *Value, |
| 236 | QualType SrcRecordTy, |
| 237 | QualType DestTy) = 0; |
| 238 | |
| 239 | virtual bool EmitBadCastCall(CodeGenFunction &CGF) = 0; |
| 240 | |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 241 | virtual llvm::Value *GetVirtualBaseClassOffset(CodeGenFunction &CGF, |
| 242 | llvm::Value *This, |
| 243 | const CXXRecordDecl *ClassDecl, |
| 244 | const CXXRecordDecl *BaseClassDecl) = 0; |
| 245 | |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 246 | virtual llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, |
| 247 | const CXXRecordDecl *RD); |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 248 | |
Timur Iskhodzhanov | 5bd0d44 | 2013-10-09 18:16:58 +0000 | [diff] [blame] | 249 | /// Emit the code to initialize hidden members required |
| 250 | /// to handle virtual inheritance, if needed by the ABI. |
| 251 | virtual void |
| 252 | initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF, |
| 253 | const CXXRecordDecl *RD) {} |
| 254 | |
Timur Iskhodzhanov | bb1b797 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 255 | /// Emit constructor variants required by this ABI. |
| 256 | virtual void EmitCXXConstructors(const CXXConstructorDecl *D) = 0; |
| 257 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 258 | /// Build the signature of the given constructor or destructor variant by |
| 259 | /// adding any required parameters. For convenience, ArgTys has been |
| 260 | /// initialized with the type of 'this'. |
| 261 | virtual void buildStructorSignature(const CXXMethodDecl *MD, StructorType T, |
| 262 | SmallVectorImpl<CanQualType> &ArgTys) = 0; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 263 | |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 264 | /// Returns true if the given destructor type should be emitted as a linkonce |
| 265 | /// delegating thunk, regardless of whether the dtor is defined in this TU or |
| 266 | /// not. |
| 267 | virtual bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor, |
| 268 | CXXDtorType DT) const = 0; |
| 269 | |
| 270 | /// Emit destructor variants required by this ABI. |
| 271 | virtual void EmitCXXDestructors(const CXXDestructorDecl *D) = 0; |
| 272 | |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 273 | /// Get the type of the implicit "this" parameter used by a method. May return |
| 274 | /// zero if no specific type is applicable, e.g. if the ABI expects the "this" |
| 275 | /// parameter to point to some artificial offset in a complete object due to |
| 276 | /// vbases being reordered. |
| 277 | virtual const CXXRecordDecl * |
| 278 | getThisArgumentTypeForMethod(const CXXMethodDecl *MD) { |
| 279 | return MD->getParent(); |
| 280 | } |
| 281 | |
| 282 | /// Perform ABI-specific "this" argument adjustment required prior to |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 283 | /// a call of a virtual function. |
| 284 | /// The "VirtualCall" argument is true iff the call itself is virtual. |
| 285 | virtual llvm::Value * |
| 286 | adjustThisArgumentForVirtualFunctionCall(CodeGenFunction &CGF, GlobalDecl GD, |
| 287 | llvm::Value *This, |
| 288 | bool VirtualCall) { |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 289 | return This; |
| 290 | } |
| 291 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 292 | /// Build a parameter variable suitable for 'this'. |
| 293 | void buildThisParam(CodeGenFunction &CGF, FunctionArgList &Params); |
| 294 | |
| 295 | /// Insert any ABI-specific implicit parameters into the parameter list for a |
| 296 | /// function. This generally involves extra data for constructors and |
| 297 | /// destructors. |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 298 | /// |
| 299 | /// ABIs may also choose to override the return type, which has been |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 300 | /// initialized with the type of 'this' if HasThisReturn(CGF.CurGD) is true or |
| 301 | /// the formal return type of the function otherwise. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 302 | virtual void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy, |
| 303 | FunctionArgList &Params) = 0; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 304 | |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 305 | /// Perform ABI-specific "this" parameter adjustment in a virtual function |
| 306 | /// prologue. |
| 307 | virtual llvm::Value *adjustThisParameterInVirtualFunctionPrologue( |
| 308 | CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) { |
| 309 | return This; |
| 310 | } |
| 311 | |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 312 | /// Emit the ABI-specific prolog for the function. |
| 313 | virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0; |
| 314 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 315 | /// Add any ABI-specific implicit arguments needed to call a constructor. |
| 316 | /// |
| 317 | /// \return The number of args added to the call, which is typically zero or |
| 318 | /// one. |
| 319 | virtual unsigned |
| 320 | addImplicitConstructorArgs(CodeGenFunction &CGF, const CXXConstructorDecl *D, |
| 321 | CXXCtorType Type, bool ForVirtualBase, |
| 322 | bool Delegating, CallArgList &Args) = 0; |
| 323 | |
| 324 | /// Emit the destructor call. |
| 325 | virtual void EmitDestructorCall(CodeGenFunction &CGF, |
| 326 | const CXXDestructorDecl *DD, CXXDtorType Type, |
| 327 | bool ForVirtualBase, bool Delegating, |
| 328 | llvm::Value *This) = 0; |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 329 | |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 330 | /// Emits the VTable definitions required for the given record type. |
| 331 | virtual void emitVTableDefinitions(CodeGenVTables &CGVT, |
| 332 | const CXXRecordDecl *RD) = 0; |
| 333 | |
| 334 | /// Get the address point of the vtable for the given base subobject while |
| 335 | /// building a constructor or a destructor. On return, NeedsVirtualOffset |
| 336 | /// tells if a virtual base adjustment is needed in order to get the offset |
| 337 | /// of the base subobject. |
| 338 | virtual llvm::Value *getVTableAddressPointInStructor( |
| 339 | CodeGenFunction &CGF, const CXXRecordDecl *RD, BaseSubobject Base, |
| 340 | const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) = 0; |
| 341 | |
| 342 | /// Get the address point of the vtable for the given base subobject while |
| 343 | /// building a constexpr. |
| 344 | virtual llvm::Constant * |
| 345 | getVTableAddressPointForConstExpr(BaseSubobject Base, |
| 346 | const CXXRecordDecl *VTableClass) = 0; |
| 347 | |
| 348 | /// Get the address of the vtable for the given record decl which should be |
| 349 | /// used for the vptr at the given offset in RD. |
| 350 | virtual llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD, |
| 351 | CharUnits VPtrOffset) = 0; |
| 352 | |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 353 | /// Build a virtual function pointer in the ABI-specific way. |
| 354 | virtual llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF, |
| 355 | GlobalDecl GD, |
| 356 | llvm::Value *This, |
| 357 | llvm::Type *Ty) = 0; |
| 358 | |
Timur Iskhodzhanov | 0f9827f | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 359 | /// Emit the ABI-specific virtual destructor call. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 360 | virtual llvm::Value * |
| 361 | EmitVirtualDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, |
| 362 | CXXDtorType DtorType, llvm::Value *This, |
| 363 | const CXXMemberCallExpr *CE) = 0; |
Timur Iskhodzhanov | 0f9827f | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 364 | |
Timur Iskhodzhanov | 2cb17a0 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 365 | virtual void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF, |
| 366 | GlobalDecl GD, |
| 367 | CallArgList &CallArgs) {} |
| 368 | |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 369 | /// Emit any tables needed to implement virtual inheritance. For Itanium, |
| 370 | /// this emits virtual table tables. For the MSVC++ ABI, this emits virtual |
| 371 | /// base tables. |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 372 | virtual void emitVirtualInheritanceTables(const CXXRecordDecl *RD) = 0; |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 373 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 374 | virtual void setThunkLinkage(llvm::Function *Thunk, bool ForVTable, |
| 375 | GlobalDecl GD, bool ReturnAdjustment) = 0; |
Timur Iskhodzhanov | 2cb17a0 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 376 | |
Timur Iskhodzhanov | c70cc5d | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 377 | virtual llvm::Value *performThisAdjustment(CodeGenFunction &CGF, |
| 378 | llvm::Value *This, |
| 379 | const ThisAdjustment &TA) = 0; |
| 380 | |
| 381 | virtual llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, |
| 382 | llvm::Value *Ret, |
| 383 | const ReturnAdjustment &RA) = 0; |
| 384 | |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 385 | virtual void EmitReturnFromThunk(CodeGenFunction &CGF, |
| 386 | RValue RV, QualType ResultType); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 387 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 388 | virtual size_t getSrcArgforCopyCtor(const CXXConstructorDecl *, |
| 389 | FunctionArgList &Args) const = 0; |
| 390 | |
Joao Matos | 285baac | 2012-07-17 17:10:11 +0000 | [diff] [blame] | 391 | /// Gets the pure virtual member call function. |
| 392 | virtual StringRef GetPureVirtualCallName() = 0; |
| 393 | |
David Blaikie | 2eb9a95 | 2012-10-16 22:56:05 +0000 | [diff] [blame] | 394 | /// Gets the deleted virtual member call name. |
| 395 | virtual StringRef GetDeletedVirtualCallName() = 0; |
| 396 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 397 | /**************************** Array cookies ******************************/ |
| 398 | |
| 399 | /// Returns the extra size required in order to store the array |
James Dennett | 16ae9de | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 400 | /// cookie for the given new-expression. May return 0 to indicate that no |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 401 | /// array cookie is required. |
| 402 | /// |
| 403 | /// Several cases are filtered out before this method is called: |
| 404 | /// - non-array allocations never need a cookie |
James Dennett | 5900103 | 2012-06-20 00:57:15 +0000 | [diff] [blame] | 405 | /// - calls to \::operator new(size_t, void*) never need a cookie |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 406 | /// |
James Dennett | 16ae9de | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 407 | /// \param expr - the new-expression being allocated. |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 408 | virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 409 | |
| 410 | /// Initialize the array cookie for the given allocation. |
| 411 | /// |
| 412 | /// \param NewPtr - a char* which is the presumed-non-null |
| 413 | /// return value of the allocation function |
| 414 | /// \param NumElements - the computed number of elements, |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 415 | /// potentially collapsed from the multidimensional array case; |
| 416 | /// always a size_t |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 417 | /// \param ElementType - the base element allocated type, |
| 418 | /// i.e. the allocated type after stripping all array types |
| 419 | virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, |
| 420 | llvm::Value *NewPtr, |
| 421 | llvm::Value *NumElements, |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 422 | const CXXNewExpr *expr, |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 423 | QualType ElementType); |
| 424 | |
| 425 | /// Reads the array cookie associated with the given pointer, |
| 426 | /// if it has one. |
| 427 | /// |
| 428 | /// \param Ptr - a pointer to the first element in the array |
| 429 | /// \param ElementType - the base element type of elements of the array |
| 430 | /// \param NumElements - an out parameter which will be initialized |
| 431 | /// with the number of elements allocated, or zero if there is no |
| 432 | /// cookie |
| 433 | /// \param AllocPtr - an out parameter which will be initialized |
| 434 | /// with a char* pointing to the address returned by the allocation |
| 435 | /// function |
| 436 | /// \param CookieSize - an out parameter which will be initialized |
| 437 | /// with the size of the cookie, or zero if there is no cookie |
| 438 | virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr, |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 439 | const CXXDeleteExpr *expr, |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 440 | QualType ElementType, llvm::Value *&NumElements, |
| 441 | llvm::Value *&AllocPtr, CharUnits &CookieSize); |
| 442 | |
Peter Collingbourne | e1e35f7 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 443 | /// Return whether the given global decl needs a VTT parameter. |
| 444 | virtual bool NeedsVTTParameter(GlobalDecl GD); |
| 445 | |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 446 | protected: |
| 447 | /// Returns the extra size required in order to store the array |
| 448 | /// cookie for the given type. Assumes that an array cookie is |
| 449 | /// required. |
| 450 | virtual CharUnits getArrayCookieSizeImpl(QualType elementType); |
| 451 | |
| 452 | /// Reads the array cookie for an allocation which is known to have one. |
| 453 | /// This is called by the standard implementation of ReadArrayCookie. |
| 454 | /// |
| 455 | /// \param ptr - a pointer to the allocation made for an array, as a char* |
| 456 | /// \param cookieSize - the computed cookie size of an array |
James Dennett | 3b2adf2 | 2012-06-15 07:35:42 +0000 | [diff] [blame] | 457 | /// |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 458 | /// Other parameters are as above. |
James Dennett | 3b2adf2 | 2012-06-15 07:35:42 +0000 | [diff] [blame] | 459 | /// |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 460 | /// \return a size_t |
| 461 | virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF, |
| 462 | llvm::Value *ptr, |
| 463 | CharUnits cookieSize); |
| 464 | |
| 465 | public: |
| 466 | |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 467 | /*************************** Static local guards ****************************/ |
| 468 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 469 | /// Emits the guarded initializer and destructor setup for the given |
| 470 | /// variable, given that it couldn't be emitted as a constant. |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 471 | /// If \p PerformInit is false, the initialization has been folded to a |
| 472 | /// constant and should not be performed. |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 473 | /// |
| 474 | /// The variable may be: |
| 475 | /// - a static local variable |
| 476 | /// - a static data member of a class template instantiation |
| 477 | virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 478 | llvm::GlobalVariable *DeclPtr, |
| 479 | bool PerformInit) = 0; |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 480 | |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 481 | /// Emit code to force the execution of a destructor during global |
| 482 | /// teardown. The default implementation of this uses atexit. |
| 483 | /// |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 484 | /// \param Dtor - a function taking a single pointer argument |
| 485 | /// \param Addr - a pointer to pass to the destructor function. |
Richard Smith | 04e5176 | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 486 | virtual void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 487 | llvm::Constant *Dtor, |
| 488 | llvm::Constant *Addr) = 0; |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 489 | |
| 490 | /*************************** thread_local initialization ********************/ |
| 491 | |
| 492 | /// Emits ABI-required functions necessary to initialize thread_local |
| 493 | /// variables in this translation unit. |
| 494 | /// |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 495 | /// \param CXXThreadLocals - The thread_local declarations in this translation |
| 496 | /// unit. |
| 497 | /// \param CXXThreadLocalInits - If this translation unit contains any |
| 498 | /// non-constant initialization or non-trivial destruction for |
| 499 | /// thread_local variables, a list of functions to perform the |
| 500 | /// initialization. |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 501 | virtual void EmitThreadLocalInitFuncs( |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 502 | CodeGenModule &CGM, |
| 503 | ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>> |
| 504 | CXXThreadLocals, |
| 505 | ArrayRef<llvm::Function *> CXXThreadLocalInits, |
| 506 | ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) = 0; |
| 507 | |
| 508 | // Determine if references to thread_local global variables can be made |
| 509 | // directly or require access through a thread wrapper function. |
| 510 | virtual bool usesThreadWrapperFunction() const = 0; |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 511 | |
| 512 | /// Emit a reference to a non-local thread_local variable (including |
| 513 | /// triggering the initialization of all thread_local variables in its |
| 514 | /// translation unit). |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 515 | virtual LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, |
| 516 | const VarDecl *VD, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 517 | QualType LValType) = 0; |
| 518 | |
| 519 | /// Emit a single constructor/destructor with the given type from a C++ |
| 520 | /// constructor Decl. |
| 521 | virtual void emitCXXStructor(const CXXMethodDecl *MD, StructorType Type) = 0; |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 522 | }; |
| 523 | |
John McCall | 96fcde0 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 524 | // Create an instance of a C++ ABI class: |
| 525 | |
| 526 | /// Creates an Itanium-family ABI. |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 527 | CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM); |
John McCall | 96fcde0 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 528 | |
| 529 | /// Creates a Microsoft-family ABI. |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 530 | CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 531 | |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
| 535 | #endif |