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 "CodeGenFunction.h"
25 template <class T> class SmallVectorImpl;
30 class CXXConstructorDecl;
31 class CXXDestructorDecl;
38 class CodeGenFunction;
41 /// Implements C++ ABI-specific code generation functions.
45 llvm::OwningPtr<MangleContext> MangleCtx;
47 CGCXXABI(CodeGenModule &CGM)
48 : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {}
51 ImplicitParamDecl *&getThisDecl(CodeGenFunction &CGF) {
52 return CGF.CXXThisDecl;
54 llvm::Value *&getThisValue(CodeGenFunction &CGF) {
55 return CGF.CXXThisValue;
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(); }
78 /// Gets the mangle context.
79 MangleContext &getMangleContext() {
83 /// Find the LLVM type used to represent the given member pointer
85 virtual const llvm::Type *
86 ConvertMemberPointerType(const MemberPointerType *MPT);
88 /// Load a member function from an object and a member function
89 /// pointer. Apply the this-adjustment and set 'This' to the
92 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
95 const MemberPointerType *MPT);
97 /// Calculate an l-value from an object and a data member pointer.
98 virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
101 const MemberPointerType *MPT);
103 /// Perform a derived-to-base or base-to-derived member pointer
105 virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
109 /// Perform a derived-to-base or base-to-derived member pointer
110 /// conversion on a constant member pointer.
111 virtual llvm::Constant *EmitMemberPointerConversion(llvm::Constant *C,
114 /// Return true if the given member pointer can be zero-initialized
115 /// (in the C++ sense) with an LLVM zeroinitializer.
116 virtual bool isZeroInitializable(const MemberPointerType *MPT);
118 /// Create a null member pointer of the given type.
119 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
121 /// Create a member pointer for the given method.
122 virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
124 /// Create a member pointer for the given field.
125 virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
128 /// Emit a comparison between two member pointers. Returns an i1.
129 virtual llvm::Value *
130 EmitMemberPointerComparison(CodeGenFunction &CGF,
133 const MemberPointerType *MPT,
136 /// Determine if a member pointer is non-null. Returns an i1.
137 virtual llvm::Value *
138 EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
140 const MemberPointerType *MPT);
142 /// Build the signature of the given constructor variant by adding
143 /// any required parameters. For convenience, ResTy has been
144 /// initialized to 'void', and ArgTys has been initialized with the
145 /// type of 'this' (although this may be changed by the ABI) and
146 /// will have the formal parameters added to it afterwards.
148 /// If there are ever any ABIs where the implicit parameters are
149 /// intermixed with the formal parameters, we can address those
151 virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
154 llvm::SmallVectorImpl<CanQualType> &ArgTys) = 0;
156 /// Build the signature of the given destructor variant by adding
157 /// any required parameters. For convenience, ResTy has been
158 /// initialized to 'void' and ArgTys has been initialized with the
159 /// type of 'this' (although this may be changed by the ABI).
160 virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
163 llvm::SmallVectorImpl<CanQualType> &ArgTys) = 0;
165 /// Build the ABI-specific portion of the parameter list for a
166 /// function. This generally involves a 'this' parameter and
167 /// possibly some extra data for constructors and destructors.
169 /// ABIs may also choose to override the return type, which has been
170 /// initialized with the formal return type of the function.
171 virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
173 FunctionArgList &Params) = 0;
175 /// Emit the ABI-specific prolog for the function.
176 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
178 virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
179 RValue RV, QualType ResultType);
181 /**************************** Array cookies ******************************/
183 /// Returns the extra size required in order to store the array
184 /// cookie for the given type. May return 0 to indicate that no
185 /// array cookie is required.
187 /// Several cases are filtered out before this method is called:
188 /// - non-array allocations never need a cookie
189 /// - calls to ::operator new(size_t, void*) never need a cookie
191 /// \param ElementType - the allocated type of the expression,
192 /// i.e. the pointee type of the expression result type
193 virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
195 /// Initialize the array cookie for the given allocation.
197 /// \param NewPtr - a char* which is the presumed-non-null
198 /// return value of the allocation function
199 /// \param NumElements - the computed number of elements,
200 /// potentially collapsed from the multidimensional array case
201 /// \param ElementType - the base element allocated type,
202 /// i.e. the allocated type after stripping all array types
203 virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
205 llvm::Value *NumElements,
206 const CXXNewExpr *expr,
207 QualType ElementType);
209 /// Reads the array cookie associated with the given pointer,
212 /// \param Ptr - a pointer to the first element in the array
213 /// \param ElementType - the base element type of elements of the array
214 /// \param NumElements - an out parameter which will be initialized
215 /// with the number of elements allocated, or zero if there is no
217 /// \param AllocPtr - an out parameter which will be initialized
218 /// with a char* pointing to the address returned by the allocation
220 /// \param CookieSize - an out parameter which will be initialized
221 /// with the size of the cookie, or zero if there is no cookie
222 virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
223 const CXXDeleteExpr *expr,
224 QualType ElementType, llvm::Value *&NumElements,
225 llvm::Value *&AllocPtr, CharUnits &CookieSize);
227 /*************************** Static local guards ****************************/
229 /// Emits the guarded initializer and destructor setup for the given
230 /// variable, given that it couldn't be emitted as a constant.
232 /// The variable may be:
233 /// - a static local variable
234 /// - a static data member of a class template instantiation
235 virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
236 llvm::GlobalVariable *DeclPtr);
240 /// Creates an instance of a C++ ABI class.
241 CGCXXABI *CreateARMCXXABI(CodeGenModule &CGM);
242 CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
243 CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);