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;
37 class CodeGenFunction;
41 /// Implements C++ ABI-specific code generation functions.
46 CGCXXABI(CodeGenModule &CGM) : CGM(CGM) {}
49 ImplicitParamDecl *&getThisDecl(CodeGenFunction &CGF) {
50 return CGF.CXXThisDecl;
52 llvm::Value *&getThisValue(CodeGenFunction &CGF) {
53 return CGF.CXXThisValue;
56 ImplicitParamDecl *&getVTTDecl(CodeGenFunction &CGF) {
57 return CGF.CXXVTTDecl;
59 llvm::Value *&getVTTValue(CodeGenFunction &CGF) {
60 return CGF.CXXVTTValue;
63 /// Build a parameter variable suitable for 'this'.
64 void BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params);
66 /// Perform prolog initialization of the parameter variable suitable
67 /// for 'this' emitted by BuildThisParam.
68 void EmitThisParam(CodeGenFunction &CGF);
70 ASTContext &getContext() const { return CGM.getContext(); }
72 virtual void _anchor();
76 virtual ~CGCXXABI() {}
78 /// Gets the mangle context.
79 virtual MangleContext &getMangleContext() = 0;
81 /// Find the LLVM type used to represent the given member pointer
83 virtual const llvm::Type *
84 ConvertMemberPointerType(const MemberPointerType *MPT);
86 /// Load a member function from an object and a member function
87 /// pointer. Apply the this-adjustment and set 'This' to the
90 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
93 const MemberPointerType *MPT);
95 /// Calculate an l-value from an object and a data member pointer.
96 virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
99 const MemberPointerType *MPT);
101 /// Perform a derived-to-base or base-to-derived member pointer
103 virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
107 /// Perform a derived-to-base or base-to-derived member pointer
108 /// conversion on a constant member pointer.
109 virtual llvm::Constant *EmitMemberPointerConversion(llvm::Constant *C,
112 /// Return true if the given member pointer can be zero-initialized
113 /// (in the C++ sense) with an LLVM zeroinitializer.
114 virtual bool isZeroInitializable(const MemberPointerType *MPT);
116 /// Create a null member pointer of the given type.
117 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
119 /// Create a member pointer for the given method.
120 virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
122 /// Create a member pointer for the given field.
123 virtual llvm::Constant *EmitMemberPointer(const FieldDecl *FD);
125 /// Emit a comparison between two member pointers. Returns an i1.
126 virtual llvm::Value *
127 EmitMemberPointerComparison(CodeGenFunction &CGF,
130 const MemberPointerType *MPT,
133 /// Determine if a member pointer is non-null. Returns an i1.
134 virtual llvm::Value *
135 EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
137 const MemberPointerType *MPT);
139 /// Build the signature of the given constructor variant by adding
140 /// any required parameters. For convenience, ResTy has been
141 /// initialized to 'void', and ArgTys has been initialized with the
142 /// type of 'this' (although this may be changed by the ABI) and
143 /// will have the formal parameters added to it afterwards.
145 /// If there are ever any ABIs where the implicit parameters are
146 /// intermixed with the formal parameters, we can address those
148 virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
151 llvm::SmallVectorImpl<CanQualType> &ArgTys) = 0;
153 /// Build the signature of the given destructor variant by adding
154 /// any required parameters. For convenience, ResTy has been
155 /// initialized to 'void' and ArgTys has been initialized with the
156 /// type of 'this' (although this may be changed by the ABI).
157 virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
160 llvm::SmallVectorImpl<CanQualType> &ArgTys) = 0;
162 /// Build the ABI-specific portion of the parameter list for a
163 /// function. This generally involves a 'this' parameter and
164 /// possibly some extra data for constructors and destructors.
166 /// ABIs may also choose to override the return type, which has been
167 /// initialized with the formal return type of the function.
168 virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
170 FunctionArgList &Params) = 0;
172 /// Emit the ABI-specific prolog for the function.
173 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
175 virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
176 RValue RV, QualType ResultType);
178 /**************************** Array cookies ******************************/
180 /// Returns the extra size required in order to store the array
181 /// cookie for the given type. May return 0 to indicate that no
182 /// array cookie is required.
184 /// Several cases are filtered out before this method is called:
185 /// - non-array allocations never need a cookie
186 /// - calls to ::operator new(size_t, void*) never need a cookie
188 /// \param ElementType - the allocated type of the expression,
189 /// i.e. the pointee type of the expression result type
190 virtual CharUnits GetArrayCookieSize(QualType ElementType);
192 /// Initialize the array cookie for the given allocation.
194 /// \param NewPtr - a char* which is the presumed-non-null
195 /// return value of the allocation function
196 /// \param NumElements - the computed number of elements,
197 /// potentially collapsed from the multidimensional array case
198 /// \param ElementType - the base element allocated type,
199 /// i.e. the allocated type after stripping all array types
200 virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
202 llvm::Value *NumElements,
203 QualType ElementType);
205 /// Reads the array cookie associated with the given pointer,
208 /// \param Ptr - a pointer to the first element in the array
209 /// \param ElementType - the base element type of elements of the array
210 /// \param NumElements - an out parameter which will be initialized
211 /// with the number of elements allocated, or zero if there is no
213 /// \param AllocPtr - an out parameter which will be initialized
214 /// with a char* pointing to the address returned by the allocation
216 /// \param CookieSize - an out parameter which will be initialized
217 /// with the size of the cookie, or zero if there is no cookie
218 virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
219 QualType ElementType, llvm::Value *&NumElements,
220 llvm::Value *&AllocPtr, CharUnits &CookieSize);
222 /*************************** Static local guards ****************************/
224 /// Emits the initializer and destructor setup for the given static
225 /// local variable, given that it's reachable and couldn't be
226 /// emitted as a constant.
227 virtual void EmitStaticLocalInit(CodeGenFunction &CGF, const VarDecl &D,
228 llvm::GlobalVariable *DeclPtr);
232 /// Creates an instance of a C++ ABI class.
233 CGCXXABI *CreateARMCXXABI(CodeGenModule &CGM);
234 CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
235 CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);