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 /// Issue a diagnostic about unsupported features in the ABI.
58 void ErrorUnsupportedABI(CodeGenFunction &CGF, StringRef S);
60 /// Get a null value for unsupported member pointers.
61 llvm::Constant *GetBogusMemberPointer(QualType T);
63 // FIXME: Every place that calls getVTT{Decl,Value} is something
64 // that needs to be abstracted properly.
65 ImplicitParamDecl *&getVTTDecl(CodeGenFunction &CGF) {
66 return CGF.CXXStructorImplicitParamDecl;
68 llvm::Value *&getVTTValue(CodeGenFunction &CGF) {
69 return CGF.CXXStructorImplicitParamValue;
72 ImplicitParamDecl *&getStructorImplicitParamDecl(CodeGenFunction &CGF) {
73 return CGF.CXXStructorImplicitParamDecl;
75 llvm::Value *&getStructorImplicitParamValue(CodeGenFunction &CGF) {
76 return CGF.CXXStructorImplicitParamValue;
79 /// Build a parameter variable suitable for 'this'.
80 void BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params);
82 /// Perform prolog initialization of the parameter variable suitable
83 /// for 'this' emitted by BuildThisParam.
84 void EmitThisParam(CodeGenFunction &CGF);
86 ASTContext &getContext() const { return CGM.getContext(); }
88 virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType);
89 virtual bool requiresArrayCookie(const CXXNewExpr *E);
95 /// Gets the mangle context.
96 MangleContext &getMangleContext() {
100 /// Returns true if the given instance method is one of the
101 /// kinds that the ABI says returns 'this'.
102 virtual bool HasThisReturn(GlobalDecl GD) const { return false; }
104 /// Returns true if the given record type should be returned indirectly.
105 virtual bool isReturnTypeIndirect(const CXXRecordDecl *RD) const = 0;
107 /// Specify how one should pass an argument of a record type.
109 /// Pass it using the normal C aggregate rules for the ABI, potentially
110 /// introducing extra copies and passing some or all of it in registers.
113 /// Pass it on the stack using its defined layout. The argument must be
114 /// evaluated directly into the correct stack position in the arguments area,
115 /// and the call machinery must not move it or introduce extra copies.
118 /// Pass it as a pointer to temporary memory.
122 /// Returns how an argument of the given record type should be passed.
123 virtual RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const = 0;
125 /// Find the LLVM type used to represent the given member pointer
128 ConvertMemberPointerType(const MemberPointerType *MPT);
130 /// Load a member function from an object and a member function
131 /// pointer. Apply the this-adjustment and set 'This' to the
133 virtual llvm::Value *
134 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
137 const MemberPointerType *MPT);
139 /// Calculate an l-value from an object and a data member pointer.
140 virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
143 const MemberPointerType *MPT);
145 /// Perform a derived-to-base, base-to-derived, or bitcast member
146 /// pointer conversion.
147 virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
151 /// Perform a derived-to-base, base-to-derived, or bitcast member
152 /// pointer conversion on a constant value.
153 virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
154 llvm::Constant *Src);
156 /// Return true if the given member pointer can be zero-initialized
157 /// (in the C++ sense) with an LLVM zeroinitializer.
158 virtual bool isZeroInitializable(const MemberPointerType *MPT);
160 /// Create a null member pointer of the given type.
161 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
163 /// Create a member pointer for the given method.
164 virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
166 /// Create a member pointer for the given field.
167 virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
170 /// Create a member pointer for the given member pointer constant.
171 virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
173 /// Emit a comparison between two member pointers. Returns an i1.
174 virtual llvm::Value *
175 EmitMemberPointerComparison(CodeGenFunction &CGF,
178 const MemberPointerType *MPT,
181 /// Determine if a member pointer is non-null. Returns an i1.
182 virtual llvm::Value *
183 EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
185 const MemberPointerType *MPT);
188 /// A utility method for computing the offset required for the given
189 /// base-to-derived or derived-to-base member-pointer conversion.
190 /// Does not handle virtual conversions (in case we ever fully
191 /// support an ABI that allows this). Returns null if no adjustment
193 llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
196 /// Adjust the given non-null pointer to an object of polymorphic
197 /// type to point to the complete object.
199 /// The IR type of the result should be a pointer but is otherwise
201 virtual llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
205 /// Build the signature of the given constructor variant by adding
206 /// any required parameters. For convenience, ResTy has been
207 /// initialized to 'void', and ArgTys has been initialized with the
208 /// type of 'this' (although this may be changed by the ABI) and
209 /// will have the formal parameters added to it afterwards.
211 /// If there are ever any ABIs where the implicit parameters are
212 /// intermixed with the formal parameters, we can address those
214 virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
217 SmallVectorImpl<CanQualType> &ArgTys) = 0;
219 virtual llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF);
221 /// Build the signature of the given destructor variant by adding
222 /// any required parameters. For convenience, ResTy has been
223 /// initialized to 'void' and ArgTys has been initialized with the
224 /// type of 'this' (although this may be changed by the ABI).
225 virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
228 SmallVectorImpl<CanQualType> &ArgTys) = 0;
230 /// Build the ABI-specific portion of the parameter list for a
231 /// function. This generally involves a 'this' parameter and
232 /// possibly some extra data for constructors and destructors.
234 /// ABIs may also choose to override the return type, which has been
235 /// initialized with the formal return type of the function.
236 virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
238 FunctionArgList &Params) = 0;
240 /// Emit the ABI-specific prolog for the function.
241 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
243 /// Emit the constructor call. Return the function that is called.
244 virtual llvm::Value *EmitConstructorCall(CodeGenFunction &CGF,
245 const CXXConstructorDecl *D,
246 CXXCtorType Type, bool ForVirtualBase,
249 CallExpr::const_arg_iterator ArgBeg,
250 CallExpr::const_arg_iterator ArgEnd) = 0;
252 /// Emit the ABI-specific virtual destructor call.
253 virtual RValue EmitVirtualDestructorCall(CodeGenFunction &CGF,
254 const CXXDestructorDecl *Dtor,
255 CXXDtorType DtorType,
256 SourceLocation CallLoc,
257 ReturnValueSlot ReturnValue,
258 llvm::Value *This) = 0;
260 virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
261 RValue RV, QualType ResultType);
263 /// Gets the pure virtual member call function.
264 virtual StringRef GetPureVirtualCallName() = 0;
266 /// Gets the deleted virtual member call name.
267 virtual StringRef GetDeletedVirtualCallName() = 0;
269 /**************************** Array cookies ******************************/
271 /// Returns the extra size required in order to store the array
272 /// cookie for the given new-expression. May return 0 to indicate that no
273 /// array cookie is required.
275 /// Several cases are filtered out before this method is called:
276 /// - non-array allocations never need a cookie
277 /// - calls to \::operator new(size_t, void*) never need a cookie
279 /// \param expr - the new-expression being allocated.
280 virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
282 /// Initialize the array cookie for the given allocation.
284 /// \param NewPtr - a char* which is the presumed-non-null
285 /// return value of the allocation function
286 /// \param NumElements - the computed number of elements,
287 /// potentially collapsed from the multidimensional array case;
289 /// \param ElementType - the base element allocated type,
290 /// i.e. the allocated type after stripping all array types
291 virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
293 llvm::Value *NumElements,
294 const CXXNewExpr *expr,
295 QualType ElementType);
297 /// Reads the array cookie associated with the given pointer,
300 /// \param Ptr - a pointer to the first element in the array
301 /// \param ElementType - the base element type of elements of the array
302 /// \param NumElements - an out parameter which will be initialized
303 /// with the number of elements allocated, or zero if there is no
305 /// \param AllocPtr - an out parameter which will be initialized
306 /// with a char* pointing to the address returned by the allocation
308 /// \param CookieSize - an out parameter which will be initialized
309 /// with the size of the cookie, or zero if there is no cookie
310 virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
311 const CXXDeleteExpr *expr,
312 QualType ElementType, llvm::Value *&NumElements,
313 llvm::Value *&AllocPtr, CharUnits &CookieSize);
316 /// Returns the extra size required in order to store the array
317 /// cookie for the given type. Assumes that an array cookie is
319 virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
321 /// Reads the array cookie for an allocation which is known to have one.
322 /// This is called by the standard implementation of ReadArrayCookie.
324 /// \param ptr - a pointer to the allocation made for an array, as a char*
325 /// \param cookieSize - the computed cookie size of an array
327 /// Other parameters are as above.
330 virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF,
332 CharUnits cookieSize);
336 /*************************** Static local guards ****************************/
338 /// Emits the guarded initializer and destructor setup for the given
339 /// variable, given that it couldn't be emitted as a constant.
340 /// If \p PerformInit is false, the initialization has been folded to a
341 /// constant and should not be performed.
343 /// The variable may be:
344 /// - a static local variable
345 /// - a static data member of a class template instantiation
346 virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
347 llvm::GlobalVariable *DeclPtr, bool PerformInit);
349 /// Emit code to force the execution of a destructor during global
350 /// teardown. The default implementation of this uses atexit.
352 /// \param dtor - a function taking a single pointer argument
353 /// \param addr - a pointer to pass to the destructor function.
354 virtual void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
355 llvm::Constant *dtor, llvm::Constant *addr);
357 /*************************** thread_local initialization ********************/
359 /// Emits ABI-required functions necessary to initialize thread_local
360 /// variables in this translation unit.
362 /// \param Decls The thread_local declarations in this translation unit.
363 /// \param InitFunc If this translation unit contains any non-constant
364 /// initialization or non-trivial destruction for thread_local
365 /// variables, a function to perform the initialization. Otherwise, 0.
366 virtual void EmitThreadLocalInitFuncs(
367 llvm::ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *> > Decls,
368 llvm::Function *InitFunc);
370 /// Emit a reference to a non-local thread_local variable (including
371 /// triggering the initialization of all thread_local variables in its
372 /// translation unit).
373 virtual LValue EmitThreadLocalDeclRefExpr(CodeGenFunction &CGF,
374 const DeclRefExpr *DRE);
377 // Create an instance of a C++ ABI class:
379 /// Creates an Itanium-family ABI.
380 CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
382 /// Creates a Microsoft-family ABI.
383 CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);