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