| Charles Davis | 4e786dd | 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 | |
| 15 | #ifndef CLANG_CODEGEN_CXXABI_H |
| 16 | #define CLANG_CODEGEN_CXXABI_H |
| 17 | |
| John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 18 | #include "CodeGenFunction.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/Basic/LLVM.h" |
| John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 20 | |
| John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 21 | namespace llvm { |
| John McCall | 84fa510 | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 22 | class Constant; |
| John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 23 | class Type; |
| John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 24 | class Value; |
| 25 | } |
| 26 | |
| Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 27 | namespace clang { |
| John McCall | a8bbb82 | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 28 | class CastExpr; |
| John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 29 | class CXXConstructorDecl; |
| 30 | class CXXDestructorDecl; |
| John McCall | 1c456c8 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 31 | class CXXMethodDecl; |
| John McCall | 84fa510 | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 32 | class CXXRecordDecl; |
| John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 33 | class FieldDecl; |
| Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 34 | class MangleContext; |
| John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 35 | |
| Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 36 | namespace CodeGen { |
| John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 37 | class CodeGenFunction; |
| Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 38 | class CodeGenModule; |
| Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 39 | |
| James Dennett | 6e5cffb | 2012-06-15 07:35:42 +0000 | [diff] [blame] | 40 | /// \brief Implements C++ ABI-specific code generation functions. |
| Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 41 | class CGCXXABI { |
| John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 42 | protected: |
| 43 | CodeGenModule &CGM; |
| Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 44 | OwningPtr<MangleContext> MangleCtx; |
| John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 45 | |
| Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 46 | CGCXXABI(CodeGenModule &CGM) |
| 47 | : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {} |
| John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 48 | |
| John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 49 | protected: |
| 50 | ImplicitParamDecl *&getThisDecl(CodeGenFunction &CGF) { |
| Eli Friedman | 9fbeba0 | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 51 | return CGF.CXXABIThisDecl; |
| John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 52 | } |
| 53 | llvm::Value *&getThisValue(CodeGenFunction &CGF) { |
| Eli Friedman | 9fbeba0 | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 54 | return CGF.CXXABIThisValue; |
| John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| Reid Kleckner | 407e8b6 | 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 | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 63 | // FIXME: Every place that calls getVTT{Decl,Value} is something |
| 64 | // that needs to be abstracted properly. |
| John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 65 | ImplicitParamDecl *&getVTTDecl(CodeGenFunction &CGF) { |
| Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 66 | return CGF.CXXStructorImplicitParamDecl; |
| John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 67 | } |
| 68 | llvm::Value *&getVTTValue(CodeGenFunction &CGF) { |
| Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 69 | return CGF.CXXStructorImplicitParamValue; |
| 70 | } |
| 71 | |
| 72 | ImplicitParamDecl *&getStructorImplicitParamDecl(CodeGenFunction &CGF) { |
| 73 | return CGF.CXXStructorImplicitParamDecl; |
| 74 | } |
| 75 | llvm::Value *&getStructorImplicitParamValue(CodeGenFunction &CGF) { |
| 76 | return CGF.CXXStructorImplicitParamValue; |
| John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /// Build a parameter variable suitable for 'this'. |
| 80 | void BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params); |
| 81 | |
| 82 | /// Perform prolog initialization of the parameter variable suitable |
| 83 | /// for 'this' emitted by BuildThisParam. |
| 84 | void EmitThisParam(CodeGenFunction &CGF); |
| 85 | |
| John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 86 | ASTContext &getContext() const { return CGM.getContext(); } |
| 87 | |
| John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 88 | virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType); |
| 89 | virtual bool requiresArrayCookie(const CXXNewExpr *E); |
| 90 | |
| Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 91 | public: |
| John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 92 | |
| Anders Carlsson | e8ba473 | 2010-11-28 17:50:09 +0000 | [diff] [blame] | 93 | virtual ~CGCXXABI(); |
| Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 94 | |
| 95 | /// Gets the mangle context. |
| Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 96 | MangleContext &getMangleContext() { |
| 97 | return *MangleCtx; |
| 98 | } |
| John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 99 | |
| Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 100 | /// 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 Ren | 0175461 | 2013-03-20 16:59:38 +0000 | [diff] [blame] | 106 | virtual bool HasThisReturn(GlobalDecl GD) const { return false; } |
| 107 | |
| Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 108 | /// Returns true if the given record type should be returned indirectly. |
| 109 | virtual bool isReturnTypeIndirect(const CXXRecordDecl *RD) const = 0; |
| 110 | |
| 111 | /// Specify how one should pass an argument of a record type. |
| 112 | enum RecordArgABI { |
| 113 | /// Pass it using the normal C aggregate rules for the ABI, potentially |
| 114 | /// introducing extra copies and passing some or all of it in registers. |
| 115 | RAA_Default = 0, |
| 116 | |
| 117 | /// Pass it on the stack using its defined layout. The argument must be |
| 118 | /// evaluated directly into the correct stack position in the arguments area, |
| 119 | /// and the call machinery must not move it or introduce extra copies. |
| 120 | RAA_DirectInMemory, |
| 121 | |
| 122 | /// Pass it as a pointer to temporary memory. |
| 123 | RAA_Indirect |
| 124 | }; |
| 125 | |
| 126 | /// Returns how an argument of the given record type should be passed. |
| 127 | virtual RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const = 0; |
| 128 | |
| John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 129 | /// Find the LLVM type used to represent the given member pointer |
| 130 | /// type. |
| Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 131 | virtual llvm::Type * |
| John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 132 | ConvertMemberPointerType(const MemberPointerType *MPT); |
| 133 | |
| 134 | /// Load a member function from an object and a member function |
| 135 | /// pointer. Apply the this-adjustment and set 'This' to the |
| 136 | /// adjusted value. |
| John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 137 | virtual llvm::Value * |
| 138 | EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 139 | llvm::Value *&This, |
| 140 | llvm::Value *MemPtr, |
| 141 | const MemberPointerType *MPT); |
| John McCall | a8bbb82 | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 142 | |
| John McCall | c134eb5 | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 143 | /// Calculate an l-value from an object and a data member pointer. |
| 144 | virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF, |
| 145 | llvm::Value *Base, |
| 146 | llvm::Value *MemPtr, |
| 147 | const MemberPointerType *MPT); |
| 148 | |
| John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 149 | /// Perform a derived-to-base, base-to-derived, or bitcast member |
| 150 | /// pointer conversion. |
| John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 151 | virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 152 | const CastExpr *E, |
| 153 | llvm::Value *Src); |
| John McCall | 84fa510 | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 154 | |
| John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 155 | /// Perform a derived-to-base, base-to-derived, or bitcast member |
| 156 | /// pointer conversion on a constant value. |
| 157 | virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E, |
| 158 | llvm::Constant *Src); |
| 159 | |
| John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 160 | /// Return true if the given member pointer can be zero-initialized |
| 161 | /// (in the C++ sense) with an LLVM zeroinitializer. |
| John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 162 | virtual bool isZeroInitializable(const MemberPointerType *MPT); |
| John McCall | 84fa510 | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 163 | |
| John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 164 | /// Create a null member pointer of the given type. |
| 165 | virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT); |
| John McCall | 84fa510 | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 166 | |
| John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 167 | /// Create a member pointer for the given method. |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 168 | virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD); |
| John McCall | 1c456c8 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 169 | |
| John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 170 | /// Create a member pointer for the given field. |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 171 | virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT, |
| 172 | CharUnits offset); |
| John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 173 | |
| Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 174 | /// Create a member pointer for the given member pointer constant. |
| 175 | virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT); |
| 176 | |
| John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 177 | /// Emit a comparison between two member pointers. Returns an i1. |
| John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 178 | virtual llvm::Value * |
| John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 179 | EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 180 | llvm::Value *L, |
| 181 | llvm::Value *R, |
| 182 | const MemberPointerType *MPT, |
| 183 | bool Inequality); |
| John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 184 | |
| John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 185 | /// Determine if a member pointer is non-null. Returns an i1. |
| John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 186 | virtual llvm::Value * |
| John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 187 | EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 188 | llvm::Value *MemPtr, |
| 189 | const MemberPointerType *MPT); |
| John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 190 | |
| John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 191 | protected: |
| 192 | /// A utility method for computing the offset required for the given |
| 193 | /// base-to-derived or derived-to-base member-pointer conversion. |
| 194 | /// Does not handle virtual conversions (in case we ever fully |
| 195 | /// support an ABI that allows this). Returns null if no adjustment |
| 196 | /// is required. |
| 197 | llvm::Constant *getMemberPointerAdjustment(const CastExpr *E); |
| 198 | |
| Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 199 | /// \brief Computes the non-virtual adjustment needed for a member pointer |
| 200 | /// conversion along an inheritance path stored in an APValue. Unlike |
| 201 | /// getMemberPointerAdjustment(), the adjustment can be negative if the path |
| 202 | /// is from a derived type to a base type. |
| 203 | CharUnits getMemberPointerPathAdjustment(const APValue &MP); |
| 204 | |
| John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 205 | public: |
| John McCall | 82fb892 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 206 | /// Adjust the given non-null pointer to an object of polymorphic |
| 207 | /// type to point to the complete object. |
| 208 | /// |
| 209 | /// The IR type of the result should be a pointer but is otherwise |
| 210 | /// irrelevant. |
| 211 | virtual llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF, |
| 212 | llvm::Value *ptr, |
| 213 | QualType type) = 0; |
| 214 | |
| Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 215 | virtual llvm::Value *GetVirtualBaseClassOffset(CodeGenFunction &CGF, |
| 216 | llvm::Value *This, |
| 217 | const CXXRecordDecl *ClassDecl, |
| 218 | const CXXRecordDecl *BaseClassDecl) = 0; |
| 219 | |
| John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 220 | /// Build the signature of the given constructor variant by adding |
| Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 221 | /// any required parameters. For convenience, ArgTys has been initialized |
| 222 | /// with the type of 'this' and ResTy has been initialized with the type of |
| 223 | /// 'this' if HasThisReturn(GlobalDecl(Ctor, T)) is true or 'void' otherwise |
| 224 | /// (although both may be changed by the ABI). |
| John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 225 | /// |
| 226 | /// If there are ever any ABIs where the implicit parameters are |
| 227 | /// intermixed with the formal parameters, we can address those |
| 228 | /// then. |
| 229 | virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor, |
| 230 | CXXCtorType T, |
| 231 | CanQualType &ResTy, |
| Chris Lattner | 01cf8db | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 232 | SmallVectorImpl<CanQualType> &ArgTys) = 0; |
| John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 233 | |
| Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 234 | virtual llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, |
| 235 | const CXXRecordDecl *RD); |
| Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 236 | |
| Timur Iskhodzhanov | b648732 | 2013-10-09 18:16:58 +0000 | [diff] [blame] | 237 | /// Emit the code to initialize hidden members required |
| 238 | /// to handle virtual inheritance, if needed by the ABI. |
| 239 | virtual void |
| 240 | initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF, |
| 241 | const CXXRecordDecl *RD) {} |
| 242 | |
| Timur Iskhodzhanov | 40f2fa9 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 243 | /// Emit constructor variants required by this ABI. |
| 244 | virtual void EmitCXXConstructors(const CXXConstructorDecl *D) = 0; |
| 245 | |
| John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 246 | /// Build the signature of the given destructor variant by adding |
| Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 247 | /// any required parameters. For convenience, ArgTys has been initialized |
| 248 | /// with the type of 'this' and ResTy has been initialized with the type of |
| 249 | /// 'this' if HasThisReturn(GlobalDecl(Dtor, T)) is true or 'void' otherwise |
| 250 | /// (although both may be changed by the ABI). |
| John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 251 | virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor, |
| 252 | CXXDtorType T, |
| 253 | CanQualType &ResTy, |
| Chris Lattner | 01cf8db | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 254 | SmallVectorImpl<CanQualType> &ArgTys) = 0; |
| John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 255 | |
| Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 256 | /// Returns true if the given destructor type should be emitted as a linkonce |
| 257 | /// delegating thunk, regardless of whether the dtor is defined in this TU or |
| 258 | /// not. |
| 259 | virtual bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor, |
| 260 | CXXDtorType DT) const = 0; |
| 261 | |
| 262 | /// Emit destructor variants required by this ABI. |
| 263 | virtual void EmitCXXDestructors(const CXXDestructorDecl *D) = 0; |
| 264 | |
| Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 265 | /// Get the type of the implicit "this" parameter used by a method. May return |
| 266 | /// zero if no specific type is applicable, e.g. if the ABI expects the "this" |
| 267 | /// parameter to point to some artificial offset in a complete object due to |
| 268 | /// vbases being reordered. |
| 269 | virtual const CXXRecordDecl * |
| 270 | getThisArgumentTypeForMethod(const CXXMethodDecl *MD) { |
| 271 | return MD->getParent(); |
| 272 | } |
| 273 | |
| 274 | /// Perform ABI-specific "this" argument adjustment required prior to |
| 275 | /// a virtual function call. |
| 276 | virtual llvm::Value *adjustThisArgumentForVirtualCall(CodeGenFunction &CGF, |
| 277 | GlobalDecl GD, |
| 278 | llvm::Value *This) { |
| 279 | return This; |
| 280 | } |
| 281 | |
| John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 282 | /// Build the ABI-specific portion of the parameter list for a |
| 283 | /// function. This generally involves a 'this' parameter and |
| 284 | /// possibly some extra data for constructors and destructors. |
| 285 | /// |
| 286 | /// ABIs may also choose to override the return type, which has been |
| Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 287 | /// initialized with the type of 'this' if HasThisReturn(CGF.CurGD) is true or |
| 288 | /// the formal return type of the function otherwise. |
| John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 289 | virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF, |
| 290 | QualType &ResTy, |
| 291 | FunctionArgList &Params) = 0; |
| 292 | |
| Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 293 | /// Perform ABI-specific "this" parameter adjustment in a virtual function |
| 294 | /// prologue. |
| 295 | virtual llvm::Value *adjustThisParameterInVirtualFunctionPrologue( |
| 296 | CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) { |
| 297 | return This; |
| 298 | } |
| 299 | |
| John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 300 | /// Emit the ABI-specific prolog for the function. |
| 301 | virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0; |
| 302 | |
| Manman Ren | 0175461 | 2013-03-20 16:59:38 +0000 | [diff] [blame] | 303 | /// Emit the constructor call. Return the function that is called. |
| Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 304 | virtual void EmitConstructorCall(CodeGenFunction &CGF, |
| Stephen Lin | c467c87 | 2013-06-19 18:10:35 +0000 | [diff] [blame] | 305 | const CXXConstructorDecl *D, |
| Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 306 | CXXCtorType Type, |
| 307 | bool ForVirtualBase, bool Delegating, |
| Stephen Lin | c467c87 | 2013-06-19 18:10:35 +0000 | [diff] [blame] | 308 | llvm::Value *This, |
| 309 | CallExpr::const_arg_iterator ArgBeg, |
| 310 | CallExpr::const_arg_iterator ArgEnd) = 0; |
| Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 311 | |
| Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 312 | /// Emits the VTable definitions required for the given record type. |
| 313 | virtual void emitVTableDefinitions(CodeGenVTables &CGVT, |
| 314 | const CXXRecordDecl *RD) = 0; |
| 315 | |
| 316 | /// Get the address point of the vtable for the given base subobject while |
| 317 | /// building a constructor or a destructor. On return, NeedsVirtualOffset |
| 318 | /// tells if a virtual base adjustment is needed in order to get the offset |
| 319 | /// of the base subobject. |
| 320 | virtual llvm::Value *getVTableAddressPointInStructor( |
| 321 | CodeGenFunction &CGF, const CXXRecordDecl *RD, BaseSubobject Base, |
| 322 | const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) = 0; |
| 323 | |
| 324 | /// Get the address point of the vtable for the given base subobject while |
| 325 | /// building a constexpr. |
| 326 | virtual llvm::Constant * |
| 327 | getVTableAddressPointForConstExpr(BaseSubobject Base, |
| 328 | const CXXRecordDecl *VTableClass) = 0; |
| 329 | |
| 330 | /// Get the address of the vtable for the given record decl which should be |
| 331 | /// used for the vptr at the given offset in RD. |
| 332 | virtual llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD, |
| 333 | CharUnits VPtrOffset) = 0; |
| 334 | |
| Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 335 | /// Build a virtual function pointer in the ABI-specific way. |
| 336 | virtual llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF, |
| 337 | GlobalDecl GD, |
| 338 | llvm::Value *This, |
| 339 | llvm::Type *Ty) = 0; |
| 340 | |
| Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 341 | /// Emit the ABI-specific virtual destructor call. |
| Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 342 | virtual void EmitVirtualDestructorCall(CodeGenFunction &CGF, |
| 343 | const CXXDestructorDecl *Dtor, |
| 344 | CXXDtorType DtorType, |
| 345 | SourceLocation CallLoc, |
| 346 | llvm::Value *This) = 0; |
| Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 347 | |
| Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 348 | virtual void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF, |
| 349 | GlobalDecl GD, |
| 350 | CallArgList &CallArgs) {} |
| 351 | |
| Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 352 | /// Emit any tables needed to implement virtual inheritance. For Itanium, |
| 353 | /// this emits virtual table tables. For the MSVC++ ABI, this emits virtual |
| 354 | /// base tables. |
| Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 355 | virtual void emitVirtualInheritanceTables(const CXXRecordDecl *RD) = 0; |
| Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 356 | |
| Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 357 | virtual void setThunkLinkage(llvm::Function *Thunk, bool ForVTable) = 0; |
| 358 | |
| Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 359 | virtual llvm::Value *performThisAdjustment(CodeGenFunction &CGF, |
| 360 | llvm::Value *This, |
| 361 | const ThisAdjustment &TA) = 0; |
| 362 | |
| 363 | virtual llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, |
| 364 | llvm::Value *Ret, |
| 365 | const ReturnAdjustment &RA) = 0; |
| 366 | |
| John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 367 | virtual void EmitReturnFromThunk(CodeGenFunction &CGF, |
| 368 | RValue RV, QualType ResultType); |
| John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 369 | |
| Joao Matos | 2ce88ef | 2012-07-17 17:10:11 +0000 | [diff] [blame] | 370 | /// Gets the pure virtual member call function. |
| 371 | virtual StringRef GetPureVirtualCallName() = 0; |
| 372 | |
| David Blaikie | eb7d598 | 2012-10-16 22:56:05 +0000 | [diff] [blame] | 373 | /// Gets the deleted virtual member call name. |
| 374 | virtual StringRef GetDeletedVirtualCallName() = 0; |
| 375 | |
| John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 376 | /**************************** Array cookies ******************************/ |
| 377 | |
| 378 | /// Returns the extra size required in order to store the array |
| James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 379 | /// cookie for the given new-expression. May return 0 to indicate that no |
| John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 380 | /// array cookie is required. |
| 381 | /// |
| 382 | /// Several cases are filtered out before this method is called: |
| 383 | /// - non-array allocations never need a cookie |
| James Dennett | a02e11f | 2012-06-20 00:57:15 +0000 | [diff] [blame] | 384 | /// - calls to \::operator new(size_t, void*) never need a cookie |
| John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 385 | /// |
| James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 386 | /// \param expr - the new-expression being allocated. |
| John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 387 | virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr); |
| John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 388 | |
| 389 | /// Initialize the array cookie for the given allocation. |
| 390 | /// |
| 391 | /// \param NewPtr - a char* which is the presumed-non-null |
| 392 | /// return value of the allocation function |
| 393 | /// \param NumElements - the computed number of elements, |
| John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 394 | /// potentially collapsed from the multidimensional array case; |
| 395 | /// always a size_t |
| John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 396 | /// \param ElementType - the base element allocated type, |
| 397 | /// i.e. the allocated type after stripping all array types |
| 398 | virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, |
| 399 | llvm::Value *NewPtr, |
| 400 | llvm::Value *NumElements, |
| John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 401 | const CXXNewExpr *expr, |
| John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 402 | QualType ElementType); |
| 403 | |
| 404 | /// Reads the array cookie associated with the given pointer, |
| 405 | /// if it has one. |
| 406 | /// |
| 407 | /// \param Ptr - a pointer to the first element in the array |
| 408 | /// \param ElementType - the base element type of elements of the array |
| 409 | /// \param NumElements - an out parameter which will be initialized |
| 410 | /// with the number of elements allocated, or zero if there is no |
| 411 | /// cookie |
| 412 | /// \param AllocPtr - an out parameter which will be initialized |
| 413 | /// with a char* pointing to the address returned by the allocation |
| 414 | /// function |
| 415 | /// \param CookieSize - an out parameter which will be initialized |
| 416 | /// with the size of the cookie, or zero if there is no cookie |
| 417 | virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr, |
| John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 418 | const CXXDeleteExpr *expr, |
| John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 419 | QualType ElementType, llvm::Value *&NumElements, |
| 420 | llvm::Value *&AllocPtr, CharUnits &CookieSize); |
| 421 | |
| Peter Collingbourne | 66f82e6 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 422 | /// Return whether the given global decl needs a VTT parameter. |
| 423 | virtual bool NeedsVTTParameter(GlobalDecl GD); |
| 424 | |
| John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 425 | protected: |
| 426 | /// Returns the extra size required in order to store the array |
| 427 | /// cookie for the given type. Assumes that an array cookie is |
| 428 | /// required. |
| 429 | virtual CharUnits getArrayCookieSizeImpl(QualType elementType); |
| 430 | |
| 431 | /// Reads the array cookie for an allocation which is known to have one. |
| 432 | /// This is called by the standard implementation of ReadArrayCookie. |
| 433 | /// |
| 434 | /// \param ptr - a pointer to the allocation made for an array, as a char* |
| 435 | /// \param cookieSize - the computed cookie size of an array |
| James Dennett | 6e5cffb | 2012-06-15 07:35:42 +0000 | [diff] [blame] | 436 | /// |
| John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 437 | /// Other parameters are as above. |
| James Dennett | 6e5cffb | 2012-06-15 07:35:42 +0000 | [diff] [blame] | 438 | /// |
| John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 439 | /// \return a size_t |
| 440 | virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF, |
| 441 | llvm::Value *ptr, |
| 442 | CharUnits cookieSize); |
| 443 | |
| 444 | public: |
| 445 | |
| John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 446 | /*************************** Static local guards ****************************/ |
| 447 | |
| John McCall | cdf7ef5 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 448 | /// Emits the guarded initializer and destructor setup for the given |
| 449 | /// variable, given that it couldn't be emitted as a constant. |
| Richard Smith | 6331c40 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 450 | /// If \p PerformInit is false, the initialization has been folded to a |
| 451 | /// constant and should not be performed. |
| John McCall | cdf7ef5 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 452 | /// |
| 453 | /// The variable may be: |
| 454 | /// - a static local variable |
| 455 | /// - a static data member of a class template instantiation |
| 456 | virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, |
| Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 457 | llvm::GlobalVariable *DeclPtr, |
| 458 | bool PerformInit) = 0; |
| John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 459 | |
| John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 460 | /// Emit code to force the execution of a destructor during global |
| 461 | /// teardown. The default implementation of this uses atexit. |
| 462 | /// |
| 463 | /// \param dtor - a function taking a single pointer argument |
| 464 | /// \param addr - a pointer to pass to the destructor function. |
| Richard Smith | dbf74ba | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 465 | virtual void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D, |
| 466 | llvm::Constant *dtor, llvm::Constant *addr); |
| Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 467 | |
| 468 | /*************************** thread_local initialization ********************/ |
| 469 | |
| 470 | /// Emits ABI-required functions necessary to initialize thread_local |
| 471 | /// variables in this translation unit. |
| 472 | /// |
| 473 | /// \param Decls The thread_local declarations in this translation unit. |
| 474 | /// \param InitFunc If this translation unit contains any non-constant |
| 475 | /// initialization or non-trivial destruction for thread_local |
| 476 | /// variables, a function to perform the initialization. Otherwise, 0. |
| 477 | virtual void EmitThreadLocalInitFuncs( |
| 478 | llvm::ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *> > Decls, |
| 479 | llvm::Function *InitFunc); |
| 480 | |
| 481 | /// Emit a reference to a non-local thread_local variable (including |
| 482 | /// triggering the initialization of all thread_local variables in its |
| 483 | /// translation unit). |
| 484 | virtual LValue EmitThreadLocalDeclRefExpr(CodeGenFunction &CGF, |
| 485 | const DeclRefExpr *DRE); |
| Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 486 | }; |
| 487 | |
| John McCall | 5762592 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 488 | // Create an instance of a C++ ABI class: |
| 489 | |
| 490 | /// Creates an Itanium-family ABI. |
| Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 491 | CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM); |
| John McCall | 5762592 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 492 | |
| 493 | /// Creates a Microsoft-family ABI. |
| Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 494 | CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM); |
| John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 495 | |
| Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
| 499 | #endif |