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 ImplicitParamDecl *&getVTTDecl(CodeGenFunction &CGF) {
58 return CGF.CXXVTTDecl;
60 llvm::Value *&getVTTValue(CodeGenFunction &CGF) {
61 return CGF.CXXVTTValue;
64 /// Build a parameter variable suitable for 'this'.
65 void BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params);
67 /// Perform prolog initialization of the parameter variable suitable
68 /// for 'this' emitted by BuildThisParam.
69 void EmitThisParam(CodeGenFunction &CGF);
71 ASTContext &getContext() const { return CGM.getContext(); }
73 virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType);
74 virtual bool requiresArrayCookie(const CXXNewExpr *E);
80 /// Gets the mangle context.
81 MangleContext &getMangleContext() {
85 /// Find the LLVM type used to represent the given member pointer
88 ConvertMemberPointerType(const MemberPointerType *MPT);
90 /// Load a member function from an object and a member function
91 /// pointer. Apply the this-adjustment and set 'This' to the
94 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
97 const MemberPointerType *MPT);
99 /// Calculate an l-value from an object and a data member pointer.
100 virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
103 const MemberPointerType *MPT);
105 /// Perform a derived-to-base, base-to-derived, or bitcast member
106 /// pointer conversion.
107 virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
111 /// Perform a derived-to-base, base-to-derived, or bitcast member
112 /// pointer conversion on a constant value.
113 virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
114 llvm::Constant *Src);
116 /// Return true if the given member pointer can be zero-initialized
117 /// (in the C++ sense) with an LLVM zeroinitializer.
118 virtual bool isZeroInitializable(const MemberPointerType *MPT);
120 /// Create a null member pointer of the given type.
121 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
123 /// Create a member pointer for the given method.
124 virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
126 /// Create a member pointer for the given field.
127 virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
130 /// Create a member pointer for the given member pointer constant.
131 virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
133 /// Emit a comparison between two member pointers. Returns an i1.
134 virtual llvm::Value *
135 EmitMemberPointerComparison(CodeGenFunction &CGF,
138 const MemberPointerType *MPT,
141 /// Determine if a member pointer is non-null. Returns an i1.
142 virtual llvm::Value *
143 EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
145 const MemberPointerType *MPT);
148 /// A utility method for computing the offset required for the given
149 /// base-to-derived or derived-to-base member-pointer conversion.
150 /// Does not handle virtual conversions (in case we ever fully
151 /// support an ABI that allows this). Returns null if no adjustment
153 llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
156 /// Adjust the given non-null pointer to an object of polymorphic
157 /// type to point to the complete object.
159 /// The IR type of the result should be a pointer but is otherwise
161 virtual llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
165 /// Build the signature of the given constructor variant by adding
166 /// any required parameters. For convenience, ResTy has been
167 /// initialized to 'void', and ArgTys has been initialized with the
168 /// type of 'this' (although this may be changed by the ABI) and
169 /// will have the formal parameters added to it afterwards.
171 /// If there are ever any ABIs where the implicit parameters are
172 /// intermixed with the formal parameters, we can address those
174 virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
177 SmallVectorImpl<CanQualType> &ArgTys) = 0;
179 /// Build the signature of the given destructor variant by adding
180 /// any required parameters. For convenience, ResTy has been
181 /// initialized to 'void' and ArgTys has been initialized with the
182 /// type of 'this' (although this may be changed by the ABI).
183 virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
186 SmallVectorImpl<CanQualType> &ArgTys) = 0;
188 /// Build the ABI-specific portion of the parameter list for a
189 /// function. This generally involves a 'this' parameter and
190 /// possibly some extra data for constructors and destructors.
192 /// ABIs may also choose to override the return type, which has been
193 /// initialized with the formal return type of the function.
194 virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
196 FunctionArgList &Params) = 0;
198 /// Emit the ABI-specific prolog for the function.
199 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
201 virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
202 RValue RV, QualType ResultType);
204 /// Gets the pure virtual member call function.
205 virtual StringRef GetPureVirtualCallName() = 0;
207 /// Gets the deleted virtual member call name.
208 virtual StringRef GetDeletedVirtualCallName() = 0;
210 /**************************** Array cookies ******************************/
212 /// Returns the extra size required in order to store the array
213 /// cookie for the given new-expression. May return 0 to indicate that no
214 /// array cookie is required.
216 /// Several cases are filtered out before this method is called:
217 /// - non-array allocations never need a cookie
218 /// - calls to \::operator new(size_t, void*) never need a cookie
220 /// \param expr - the new-expression being allocated.
221 virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
223 /// Initialize the array cookie for the given allocation.
225 /// \param NewPtr - a char* which is the presumed-non-null
226 /// return value of the allocation function
227 /// \param NumElements - the computed number of elements,
228 /// potentially collapsed from the multidimensional array case;
230 /// \param ElementType - the base element allocated type,
231 /// i.e. the allocated type after stripping all array types
232 virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
234 llvm::Value *NumElements,
235 const CXXNewExpr *expr,
236 QualType ElementType);
238 /// Reads the array cookie associated with the given pointer,
241 /// \param Ptr - a pointer to the first element in the array
242 /// \param ElementType - the base element type of elements of the array
243 /// \param NumElements - an out parameter which will be initialized
244 /// with the number of elements allocated, or zero if there is no
246 /// \param AllocPtr - an out parameter which will be initialized
247 /// with a char* pointing to the address returned by the allocation
249 /// \param CookieSize - an out parameter which will be initialized
250 /// with the size of the cookie, or zero if there is no cookie
251 virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
252 const CXXDeleteExpr *expr,
253 QualType ElementType, llvm::Value *&NumElements,
254 llvm::Value *&AllocPtr, CharUnits &CookieSize);
257 /// Returns the extra size required in order to store the array
258 /// cookie for the given type. Assumes that an array cookie is
260 virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
262 /// Reads the array cookie for an allocation which is known to have one.
263 /// This is called by the standard implementation of ReadArrayCookie.
265 /// \param ptr - a pointer to the allocation made for an array, as a char*
266 /// \param cookieSize - the computed cookie size of an array
268 /// Other parameters are as above.
271 virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF,
273 CharUnits cookieSize);
277 /*************************** Static local guards ****************************/
279 /// Emits the guarded initializer and destructor setup for the given
280 /// variable, given that it couldn't be emitted as a constant.
281 /// If \p PerformInit is false, the initialization has been folded to a
282 /// constant and should not be performed.
284 /// The variable may be:
285 /// - a static local variable
286 /// - a static data member of a class template instantiation
287 virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
288 llvm::GlobalVariable *DeclPtr, bool PerformInit);
290 /// Emit code to force the execution of a destructor during global
291 /// teardown. The default implementation of this uses atexit.
293 /// \param dtor - a function taking a single pointer argument
294 /// \param addr - a pointer to pass to the destructor function.
295 virtual void registerGlobalDtor(CodeGenFunction &CGF, llvm::Constant *dtor,
296 llvm::Constant *addr);
299 // Create an instance of a C++ ABI class:
301 /// Creates an Itanium-family ABI.
302 CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
304 /// Creates a Microsoft-family ABI.
305 CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);