1 //===----- CGCXXABI.h - Interface to C++ ABIs -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This provides an abstract class for C++ code generation. Concrete subclasses
11 // of this implement code generation for specific C++ ABIs.
13 //===----------------------------------------------------------------------===//
15 #ifndef CLANG_CODEGEN_CXXABI_H
16 #define CLANG_CODEGEN_CXXABI_H
18 #include "clang/Basic/LLVM.h"
20 #include "CodeGenFunction.h"
30 class CXXConstructorDecl;
31 class CXXDestructorDecl;
38 class CodeGenFunction;
41 /// \brief Implements C++ ABI-specific code generation functions.
45 OwningPtr<MangleContext> MangleCtx;
47 CGCXXABI(CodeGenModule &CGM)
48 : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {}
51 ImplicitParamDecl *&getThisDecl(CodeGenFunction &CGF) {
52 return CGF.CXXABIThisDecl;
54 llvm::Value *&getThisValue(CodeGenFunction &CGF) {
55 return CGF.CXXABIThisValue;
58 ImplicitParamDecl *&getVTTDecl(CodeGenFunction &CGF) {
59 return CGF.CXXVTTDecl;
61 llvm::Value *&getVTTValue(CodeGenFunction &CGF) {
62 return CGF.CXXVTTValue;
65 /// Build a parameter variable suitable for 'this'.
66 void BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params);
68 /// Perform prolog initialization of the parameter variable suitable
69 /// for 'this' emitted by BuildThisParam.
70 void EmitThisParam(CodeGenFunction &CGF);
72 ASTContext &getContext() const { return CGM.getContext(); }
74 virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType);
75 virtual bool requiresArrayCookie(const CXXNewExpr *E);
81 /// Gets the mangle context.
82 MangleContext &getMangleContext() {
86 /// Find the LLVM type used to represent the given member pointer
89 ConvertMemberPointerType(const MemberPointerType *MPT);
91 /// Load a member function from an object and a member function
92 /// pointer. Apply the this-adjustment and set 'This' to the
95 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
98 const MemberPointerType *MPT);
100 /// Calculate an l-value from an object and a data member pointer.
101 virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
104 const MemberPointerType *MPT);
106 /// Perform a derived-to-base, base-to-derived, or bitcast member
107 /// pointer conversion.
108 virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
112 /// Perform a derived-to-base, base-to-derived, or bitcast member
113 /// pointer conversion on a constant value.
114 virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
115 llvm::Constant *Src);
117 /// Return true if the given member pointer can be zero-initialized
118 /// (in the C++ sense) with an LLVM zeroinitializer.
119 virtual bool isZeroInitializable(const MemberPointerType *MPT);
121 /// Create a null member pointer of the given type.
122 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
124 /// Create a member pointer for the given method.
125 virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
127 /// Create a member pointer for the given field.
128 virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
131 /// Create a member pointer for the given member pointer constant.
132 virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
134 /// Emit a comparison between two member pointers. Returns an i1.
135 virtual llvm::Value *
136 EmitMemberPointerComparison(CodeGenFunction &CGF,
139 const MemberPointerType *MPT,
142 /// Determine if a member pointer is non-null. Returns an i1.
143 virtual llvm::Value *
144 EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
146 const MemberPointerType *MPT);
149 /// A utility method for computing the offset required for the given
150 /// base-to-derived or derived-to-base member-pointer conversion.
151 /// Does not handle virtual conversions (in case we ever fully
152 /// support an ABI that allows this). Returns null if no adjustment
154 llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
157 /// Build the signature of the given constructor variant by adding
158 /// any required parameters. For convenience, ResTy has been
159 /// initialized to 'void', and ArgTys has been initialized with the
160 /// type of 'this' (although this may be changed by the ABI) and
161 /// will have the formal parameters added to it afterwards.
163 /// If there are ever any ABIs where the implicit parameters are
164 /// intermixed with the formal parameters, we can address those
166 virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
169 SmallVectorImpl<CanQualType> &ArgTys) = 0;
171 /// Build the signature of the given destructor variant by adding
172 /// any required parameters. For convenience, ResTy has been
173 /// initialized to 'void' and ArgTys has been initialized with the
174 /// type of 'this' (although this may be changed by the ABI).
175 virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
178 SmallVectorImpl<CanQualType> &ArgTys) = 0;
180 /// Build the ABI-specific portion of the parameter list for a
181 /// function. This generally involves a 'this' parameter and
182 /// possibly some extra data for constructors and destructors.
184 /// ABIs may also choose to override the return type, which has been
185 /// initialized with the formal return type of the function.
186 virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
188 FunctionArgList &Params) = 0;
190 /// Emit the ABI-specific prolog for the function.
191 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
193 virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
194 RValue RV, QualType ResultType);
196 /**************************** Array cookies ******************************/
198 /// Returns the extra size required in order to store the array
199 /// cookie for the given type. May return 0 to indicate that no
200 /// array cookie is required.
202 /// Several cases are filtered out before this method is called:
203 /// - non-array allocations never need a cookie
204 /// - calls to ::operator new(size_t, void*) never need a cookie
206 /// \param ElementType - the allocated type of the expression,
207 /// i.e. the pointee type of the expression result type
208 virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
210 /// Initialize the array cookie for the given allocation.
212 /// \param NewPtr - a char* which is the presumed-non-null
213 /// return value of the allocation function
214 /// \param NumElements - the computed number of elements,
215 /// potentially collapsed from the multidimensional array case;
217 /// \param ElementType - the base element allocated type,
218 /// i.e. the allocated type after stripping all array types
219 virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
221 llvm::Value *NumElements,
222 const CXXNewExpr *expr,
223 QualType ElementType);
225 /// Reads the array cookie associated with the given pointer,
228 /// \param Ptr - a pointer to the first element in the array
229 /// \param ElementType - the base element type of elements of the array
230 /// \param NumElements - an out parameter which will be initialized
231 /// with the number of elements allocated, or zero if there is no
233 /// \param AllocPtr - an out parameter which will be initialized
234 /// with a char* pointing to the address returned by the allocation
236 /// \param CookieSize - an out parameter which will be initialized
237 /// with the size of the cookie, or zero if there is no cookie
238 virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
239 const CXXDeleteExpr *expr,
240 QualType ElementType, llvm::Value *&NumElements,
241 llvm::Value *&AllocPtr, CharUnits &CookieSize);
244 /// Returns the extra size required in order to store the array
245 /// cookie for the given type. Assumes that an array cookie is
247 virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
249 /// Reads the array cookie for an allocation which is known to have one.
250 /// This is called by the standard implementation of ReadArrayCookie.
252 /// \param ptr - a pointer to the allocation made for an array, as a char*
253 /// \param cookieSize - the computed cookie size of an array
255 /// Other parameters are as above.
258 virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF,
260 CharUnits cookieSize);
264 /*************************** Static local guards ****************************/
266 /// Emits the guarded initializer and destructor setup for the given
267 /// variable, given that it couldn't be emitted as a constant.
268 /// If \p PerformInit is false, the initialization has been folded to a
269 /// constant and should not be performed.
271 /// The variable may be:
272 /// - a static local variable
273 /// - a static data member of a class template instantiation
274 virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
275 llvm::GlobalVariable *DeclPtr, bool PerformInit);
277 /// Emit code to force the execution of a destructor during global
278 /// teardown. The default implementation of this uses atexit.
280 /// \param dtor - a function taking a single pointer argument
281 /// \param addr - a pointer to pass to the destructor function.
282 virtual void registerGlobalDtor(CodeGenFunction &CGF, llvm::Constant *dtor,
283 llvm::Constant *addr);
286 /// Creates an instance of a C++ ABI class.
287 CGCXXABI *CreateARMCXXABI(CodeGenModule &CGM);
288 CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
289 CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);