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 /// Returns true if the given instance method is one of the
95 /// kinds that the ABI says returns 'this'.
96 virtual bool HasThisReturn(GlobalDecl GD) const { return false; }
98 /// Find the LLVM type used to represent the given member pointer
101 ConvertMemberPointerType(const MemberPointerType *MPT);
103 /// Load a member function from an object and a member function
104 /// pointer. Apply the this-adjustment and set 'This' to the
106 virtual llvm::Value *
107 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
110 const MemberPointerType *MPT);
112 /// Calculate an l-value from an object and a data member pointer.
113 virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
116 const MemberPointerType *MPT);
118 /// Perform a derived-to-base, base-to-derived, or bitcast member
119 /// pointer conversion.
120 virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
124 /// Perform a derived-to-base, base-to-derived, or bitcast member
125 /// pointer conversion on a constant value.
126 virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
127 llvm::Constant *Src);
129 /// Return true if the given member pointer can be zero-initialized
130 /// (in the C++ sense) with an LLVM zeroinitializer.
131 virtual bool isZeroInitializable(const MemberPointerType *MPT);
133 /// Create a null member pointer of the given type.
134 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
136 /// Create a member pointer for the given method.
137 virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
139 /// Create a member pointer for the given field.
140 virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
143 /// Create a member pointer for the given member pointer constant.
144 virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
146 /// Emit a comparison between two member pointers. Returns an i1.
147 virtual llvm::Value *
148 EmitMemberPointerComparison(CodeGenFunction &CGF,
151 const MemberPointerType *MPT,
154 /// Determine if a member pointer is non-null. Returns an i1.
155 virtual llvm::Value *
156 EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
158 const MemberPointerType *MPT);
161 /// A utility method for computing the offset required for the given
162 /// base-to-derived or derived-to-base member-pointer conversion.
163 /// Does not handle virtual conversions (in case we ever fully
164 /// support an ABI that allows this). Returns null if no adjustment
166 llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
169 /// Adjust the given non-null pointer to an object of polymorphic
170 /// type to point to the complete object.
172 /// The IR type of the result should be a pointer but is otherwise
174 virtual llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
178 /// Build the signature of the given constructor variant by adding
179 /// any required parameters. For convenience, ResTy has been
180 /// initialized to 'void', and ArgTys has been initialized with the
181 /// type of 'this' (although this may be changed by the ABI) and
182 /// will have the formal parameters added to it afterwards.
184 /// If there are ever any ABIs where the implicit parameters are
185 /// intermixed with the formal parameters, we can address those
187 virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
190 SmallVectorImpl<CanQualType> &ArgTys) = 0;
192 virtual llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF);
194 /// Build the signature of the given destructor variant by adding
195 /// any required parameters. For convenience, ResTy has been
196 /// initialized to 'void' and ArgTys has been initialized with the
197 /// type of 'this' (although this may be changed by the ABI).
198 virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
201 SmallVectorImpl<CanQualType> &ArgTys) = 0;
203 /// Build the ABI-specific portion of the parameter list for a
204 /// function. This generally involves a 'this' parameter and
205 /// possibly some extra data for constructors and destructors.
207 /// ABIs may also choose to override the return type, which has been
208 /// initialized with the formal return type of the function.
209 virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
211 FunctionArgList &Params) = 0;
213 /// Emit the ABI-specific prolog for the function.
214 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
216 /// Emit the constructor call. Return the function that is called.
217 virtual llvm::Value *EmitConstructorCall(CodeGenFunction &CGF,
218 const CXXConstructorDecl *D,
219 CXXCtorType Type, bool ForVirtualBase,
222 CallExpr::const_arg_iterator ArgBeg,
223 CallExpr::const_arg_iterator ArgEnd) = 0;
225 /// Emit the ABI-specific virtual destructor call.
226 virtual RValue EmitVirtualDestructorCall(CodeGenFunction &CGF,
227 const CXXDestructorDecl *Dtor,
228 CXXDtorType DtorType,
229 SourceLocation CallLoc,
230 ReturnValueSlot ReturnValue,
231 llvm::Value *This) = 0;
233 virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
234 RValue RV, QualType ResultType);
236 /// Gets the pure virtual member call function.
237 virtual StringRef GetPureVirtualCallName() = 0;
239 /// Gets the deleted virtual member call name.
240 virtual StringRef GetDeletedVirtualCallName() = 0;
242 /**************************** Array cookies ******************************/
244 /// Returns the extra size required in order to store the array
245 /// cookie for the given new-expression. May return 0 to indicate that no
246 /// array cookie is required.
248 /// Several cases are filtered out before this method is called:
249 /// - non-array allocations never need a cookie
250 /// - calls to \::operator new(size_t, void*) never need a cookie
252 /// \param expr - the new-expression being allocated.
253 virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
255 /// Initialize the array cookie for the given allocation.
257 /// \param NewPtr - a char* which is the presumed-non-null
258 /// return value of the allocation function
259 /// \param NumElements - the computed number of elements,
260 /// potentially collapsed from the multidimensional array case;
262 /// \param ElementType - the base element allocated type,
263 /// i.e. the allocated type after stripping all array types
264 virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
266 llvm::Value *NumElements,
267 const CXXNewExpr *expr,
268 QualType ElementType);
270 /// Reads the array cookie associated with the given pointer,
273 /// \param Ptr - a pointer to the first element in the array
274 /// \param ElementType - the base element type of elements of the array
275 /// \param NumElements - an out parameter which will be initialized
276 /// with the number of elements allocated, or zero if there is no
278 /// \param AllocPtr - an out parameter which will be initialized
279 /// with a char* pointing to the address returned by the allocation
281 /// \param CookieSize - an out parameter which will be initialized
282 /// with the size of the cookie, or zero if there is no cookie
283 virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
284 const CXXDeleteExpr *expr,
285 QualType ElementType, llvm::Value *&NumElements,
286 llvm::Value *&AllocPtr, CharUnits &CookieSize);
289 /// Returns the extra size required in order to store the array
290 /// cookie for the given type. Assumes that an array cookie is
292 virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
294 /// Reads the array cookie for an allocation which is known to have one.
295 /// This is called by the standard implementation of ReadArrayCookie.
297 /// \param ptr - a pointer to the allocation made for an array, as a char*
298 /// \param cookieSize - the computed cookie size of an array
300 /// Other parameters are as above.
303 virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF,
305 CharUnits cookieSize);
309 /*************************** Static local guards ****************************/
311 /// Emits the guarded initializer and destructor setup for the given
312 /// variable, given that it couldn't be emitted as a constant.
313 /// If \p PerformInit is false, the initialization has been folded to a
314 /// constant and should not be performed.
316 /// The variable may be:
317 /// - a static local variable
318 /// - a static data member of a class template instantiation
319 virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
320 llvm::GlobalVariable *DeclPtr, bool PerformInit);
322 /// Emit code to force the execution of a destructor during global
323 /// teardown. The default implementation of this uses atexit.
325 /// \param dtor - a function taking a single pointer argument
326 /// \param addr - a pointer to pass to the destructor function.
327 virtual void registerGlobalDtor(CodeGenFunction &CGF, llvm::Constant *dtor,
328 llvm::Constant *addr);
331 // Create an instance of a C++ ABI class:
333 /// Creates an Itanium-family ABI.
334 CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
336 /// Creates a Microsoft-family ABI.
337 CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);