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"
19 #include "clang/Basic/LLVM.h"
29 class CXXConstructorDecl;
30 class CXXDestructorDecl;
37 class CodeGenFunction;
40 /// \brief Implements C++ ABI-specific code generation functions.
44 OwningPtr<MangleContext> MangleCtx;
46 CGCXXABI(CodeGenModule &CGM)
47 : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {}
50 ImplicitParamDecl *&getThisDecl(CodeGenFunction &CGF) {
51 return CGF.CXXABIThisDecl;
53 llvm::Value *&getThisValue(CodeGenFunction &CGF) {
54 return CGF.CXXABIThisValue;
57 // FIXME: Every place that calls getVTT{Decl,Value} is something
58 // that needs to be abstracted properly.
59 ImplicitParamDecl *&getVTTDecl(CodeGenFunction &CGF) {
60 return CGF.CXXStructorImplicitParamDecl;
62 llvm::Value *&getVTTValue(CodeGenFunction &CGF) {
63 return CGF.CXXStructorImplicitParamValue;
66 ImplicitParamDecl *&getStructorImplicitParamDecl(CodeGenFunction &CGF) {
67 return CGF.CXXStructorImplicitParamDecl;
69 llvm::Value *&getStructorImplicitParamValue(CodeGenFunction &CGF) {
70 return CGF.CXXStructorImplicitParamValue;
73 /// Build a parameter variable suitable for 'this'.
74 void BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params);
76 /// Perform prolog initialization of the parameter variable suitable
77 /// for 'this' emitted by BuildThisParam.
78 void EmitThisParam(CodeGenFunction &CGF);
80 ASTContext &getContext() const { return CGM.getContext(); }
82 virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType);
83 virtual bool requiresArrayCookie(const CXXNewExpr *E);
89 /// Gets the mangle context.
90 MangleContext &getMangleContext() {
94 /// Find the LLVM type used to represent the given member pointer
97 ConvertMemberPointerType(const MemberPointerType *MPT);
99 /// Load a member function from an object and a member function
100 /// pointer. Apply the this-adjustment and set 'This' to the
102 virtual llvm::Value *
103 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
106 const MemberPointerType *MPT);
108 /// Calculate an l-value from an object and a data member pointer.
109 virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
112 const MemberPointerType *MPT);
114 /// Perform a derived-to-base, base-to-derived, or bitcast member
115 /// pointer conversion.
116 virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
120 /// Perform a derived-to-base, base-to-derived, or bitcast member
121 /// pointer conversion on a constant value.
122 virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
123 llvm::Constant *Src);
125 /// Return true if the given member pointer can be zero-initialized
126 /// (in the C++ sense) with an LLVM zeroinitializer.
127 virtual bool isZeroInitializable(const MemberPointerType *MPT);
129 /// Create a null member pointer of the given type.
130 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
132 /// Create a member pointer for the given method.
133 virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
135 /// Create a member pointer for the given field.
136 virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
139 /// Create a member pointer for the given member pointer constant.
140 virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
142 /// Emit a comparison between two member pointers. Returns an i1.
143 virtual llvm::Value *
144 EmitMemberPointerComparison(CodeGenFunction &CGF,
147 const MemberPointerType *MPT,
150 /// Determine if a member pointer is non-null. Returns an i1.
151 virtual llvm::Value *
152 EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
154 const MemberPointerType *MPT);
157 /// A utility method for computing the offset required for the given
158 /// base-to-derived or derived-to-base member-pointer conversion.
159 /// Does not handle virtual conversions (in case we ever fully
160 /// support an ABI that allows this). Returns null if no adjustment
162 llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
165 /// Adjust the given non-null pointer to an object of polymorphic
166 /// type to point to the complete object.
168 /// The IR type of the result should be a pointer but is otherwise
170 virtual llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
174 /// Build the signature of the given constructor variant by adding
175 /// any required parameters. For convenience, ResTy has been
176 /// initialized to 'void', and ArgTys has been initialized with the
177 /// type of 'this' (although this may be changed by the ABI) and
178 /// will have the formal parameters added to it afterwards.
180 /// If there are ever any ABIs where the implicit parameters are
181 /// intermixed with the formal parameters, we can address those
183 virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
186 SmallVectorImpl<CanQualType> &ArgTys) = 0;
188 /// Build the signature of the given destructor variant by adding
189 /// any required parameters. For convenience, ResTy has been
190 /// initialized to 'void' and ArgTys has been initialized with the
191 /// type of 'this' (although this may be changed by the ABI).
192 virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
195 SmallVectorImpl<CanQualType> &ArgTys) = 0;
197 /// Build the ABI-specific portion of the parameter list for a
198 /// function. This generally involves a 'this' parameter and
199 /// possibly some extra data for constructors and destructors.
201 /// ABIs may also choose to override the return type, which has been
202 /// initialized with the formal return type of the function.
203 virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
205 FunctionArgList &Params) = 0;
207 /// Emit the ABI-specific prolog for the function.
208 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
210 virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
211 RValue RV, QualType ResultType);
213 /// Gets the pure virtual member call function.
214 virtual StringRef GetPureVirtualCallName() = 0;
216 /// Gets the deleted virtual member call name.
217 virtual StringRef GetDeletedVirtualCallName() = 0;
219 /**************************** Array cookies ******************************/
221 /// Returns the extra size required in order to store the array
222 /// cookie for the given new-expression. May return 0 to indicate that no
223 /// array cookie is required.
225 /// Several cases are filtered out before this method is called:
226 /// - non-array allocations never need a cookie
227 /// - calls to \::operator new(size_t, void*) never need a cookie
229 /// \param expr - the new-expression being allocated.
230 virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
232 /// Initialize the array cookie for the given allocation.
234 /// \param NewPtr - a char* which is the presumed-non-null
235 /// return value of the allocation function
236 /// \param NumElements - the computed number of elements,
237 /// potentially collapsed from the multidimensional array case;
239 /// \param ElementType - the base element allocated type,
240 /// i.e. the allocated type after stripping all array types
241 virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
243 llvm::Value *NumElements,
244 const CXXNewExpr *expr,
245 QualType ElementType);
247 /// Reads the array cookie associated with the given pointer,
250 /// \param Ptr - a pointer to the first element in the array
251 /// \param ElementType - the base element type of elements of the array
252 /// \param NumElements - an out parameter which will be initialized
253 /// with the number of elements allocated, or zero if there is no
255 /// \param AllocPtr - an out parameter which will be initialized
256 /// with a char* pointing to the address returned by the allocation
258 /// \param CookieSize - an out parameter which will be initialized
259 /// with the size of the cookie, or zero if there is no cookie
260 virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
261 const CXXDeleteExpr *expr,
262 QualType ElementType, llvm::Value *&NumElements,
263 llvm::Value *&AllocPtr, CharUnits &CookieSize);
266 /// Returns the extra size required in order to store the array
267 /// cookie for the given type. Assumes that an array cookie is
269 virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
271 /// Reads the array cookie for an allocation which is known to have one.
272 /// This is called by the standard implementation of ReadArrayCookie.
274 /// \param ptr - a pointer to the allocation made for an array, as a char*
275 /// \param cookieSize - the computed cookie size of an array
277 /// Other parameters are as above.
280 virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF,
282 CharUnits cookieSize);
286 /*************************** Static local guards ****************************/
288 /// Emits the guarded initializer and destructor setup for the given
289 /// variable, given that it couldn't be emitted as a constant.
290 /// If \p PerformInit is false, the initialization has been folded to a
291 /// constant and should not be performed.
293 /// The variable may be:
294 /// - a static local variable
295 /// - a static data member of a class template instantiation
296 virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
297 llvm::GlobalVariable *DeclPtr, bool PerformInit);
299 /// Emit code to force the execution of a destructor during global
300 /// teardown. The default implementation of this uses atexit.
302 /// \param dtor - a function taking a single pointer argument
303 /// \param addr - a pointer to pass to the destructor function.
304 virtual void registerGlobalDtor(CodeGenFunction &CGF, llvm::Constant *dtor,
305 llvm::Constant *addr);
308 // Create an instance of a C++ ABI class:
310 /// Creates an Itanium-family ABI.
311 CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
313 /// Creates a Microsoft-family ABI.
314 CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);