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