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 constructor or destructor is one of the
101 /// kinds that the ABI says returns 'this' (only applies when called
102 /// non-virtually for destructors).
104 /// There currently is no way to indicate if a destructor returns 'this'
105 /// when called virtually, and code generation does not support the case.
106 virtual bool HasThisReturn(GlobalDecl GD) const { return false; }
108 /// Returns true if the given record type should be returned indirectly.
109 virtual bool isReturnTypeIndirect(const CXXRecordDecl *RD) const = 0;
111 /// Specify how one should pass an argument of a record type.
113 /// Pass it using the normal C aggregate rules for the ABI, potentially
114 /// introducing extra copies and passing some or all of it in registers.
117 /// Pass it on the stack using its defined layout. The argument must be
118 /// evaluated directly into the correct stack position in the arguments area,
119 /// and the call machinery must not move it or introduce extra copies.
122 /// Pass it as a pointer to temporary memory.
126 /// Returns how an argument of the given record type should be passed.
127 virtual RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const = 0;
129 /// Find the LLVM type used to represent the given member pointer
132 ConvertMemberPointerType(const MemberPointerType *MPT);
134 /// Load a member function from an object and a member function
135 /// pointer. Apply the this-adjustment and set 'This' to the
137 virtual llvm::Value *
138 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
141 const MemberPointerType *MPT);
143 /// Calculate an l-value from an object and a data member pointer.
144 virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
147 const MemberPointerType *MPT);
149 /// Perform a derived-to-base, base-to-derived, or bitcast member
150 /// pointer conversion.
151 virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
155 /// Perform a derived-to-base, base-to-derived, or bitcast member
156 /// pointer conversion on a constant value.
157 virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
158 llvm::Constant *Src);
160 /// Return true if the given member pointer can be zero-initialized
161 /// (in the C++ sense) with an LLVM zeroinitializer.
162 virtual bool isZeroInitializable(const MemberPointerType *MPT);
164 /// Create a null member pointer of the given type.
165 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
167 /// Create a member pointer for the given method.
168 virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
170 /// Create a member pointer for the given field.
171 virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
174 /// Create a member pointer for the given member pointer constant.
175 virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
177 /// Emit a comparison between two member pointers. Returns an i1.
178 virtual llvm::Value *
179 EmitMemberPointerComparison(CodeGenFunction &CGF,
182 const MemberPointerType *MPT,
185 /// Determine if a member pointer is non-null. Returns an i1.
186 virtual llvm::Value *
187 EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
189 const MemberPointerType *MPT);
192 /// A utility method for computing the offset required for the given
193 /// base-to-derived or derived-to-base member-pointer conversion.
194 /// Does not handle virtual conversions (in case we ever fully
195 /// support an ABI that allows this). Returns null if no adjustment
197 llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
199 /// \brief Computes the non-virtual adjustment needed for a member pointer
200 /// conversion along an inheritance path stored in an APValue. Unlike
201 /// getMemberPointerAdjustment(), the adjustment can be negative if the path
202 /// is from a derived type to a base type.
203 CharUnits getMemberPointerPathAdjustment(const APValue &MP);
206 /// Adjust the given non-null pointer to an object of polymorphic
207 /// type to point to the complete object.
209 /// The IR type of the result should be a pointer but is otherwise
211 virtual llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
215 virtual llvm::Value *GetVirtualBaseClassOffset(CodeGenFunction &CGF,
217 const CXXRecordDecl *ClassDecl,
218 const CXXRecordDecl *BaseClassDecl) = 0;
220 /// Build the signature of the given constructor variant by adding
221 /// any required parameters. For convenience, ArgTys has been initialized
222 /// with the type of 'this' and ResTy has been initialized with the type of
223 /// 'this' if HasThisReturn(GlobalDecl(Ctor, T)) is true or 'void' otherwise
224 /// (although both may be changed by the ABI).
226 /// If there are ever any ABIs where the implicit parameters are
227 /// intermixed with the formal parameters, we can address those
229 virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
232 SmallVectorImpl<CanQualType> &ArgTys) = 0;
234 virtual llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
235 const CXXRecordDecl *RD);
237 /// Emit the code to initialize hidden members required
238 /// to handle virtual inheritance, if needed by the ABI.
240 initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF,
241 const CXXRecordDecl *RD) {}
243 /// Emit constructor variants required by this ABI.
244 virtual void EmitCXXConstructors(const CXXConstructorDecl *D) = 0;
246 /// Build the signature of the given destructor variant by adding
247 /// any required parameters. For convenience, ArgTys has been initialized
248 /// with the type of 'this' and ResTy has been initialized with the type of
249 /// 'this' if HasThisReturn(GlobalDecl(Dtor, T)) is true or 'void' otherwise
250 /// (although both may be changed by the ABI).
251 virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
254 SmallVectorImpl<CanQualType> &ArgTys) = 0;
256 /// Returns true if the given destructor type should be emitted as a linkonce
257 /// delegating thunk, regardless of whether the dtor is defined in this TU or
259 virtual bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor,
260 CXXDtorType DT) const = 0;
262 /// Emit destructor variants required by this ABI.
263 virtual void EmitCXXDestructors(const CXXDestructorDecl *D) = 0;
265 /// Get the type of the implicit "this" parameter used by a method. May return
266 /// zero if no specific type is applicable, e.g. if the ABI expects the "this"
267 /// parameter to point to some artificial offset in a complete object due to
268 /// vbases being reordered.
269 virtual const CXXRecordDecl *
270 getThisArgumentTypeForMethod(const CXXMethodDecl *MD) {
271 return MD->getParent();
274 /// Perform ABI-specific "this" argument adjustment required prior to
275 /// a virtual function call.
276 virtual llvm::Value *adjustThisArgumentForVirtualCall(CodeGenFunction &CGF,
282 /// Build the ABI-specific portion of the parameter list for a
283 /// function. This generally involves a 'this' parameter and
284 /// possibly some extra data for constructors and destructors.
286 /// ABIs may also choose to override the return type, which has been
287 /// initialized with the type of 'this' if HasThisReturn(CGF.CurGD) is true or
288 /// the formal return type of the function otherwise.
289 virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
291 FunctionArgList &Params) = 0;
293 /// Perform ABI-specific "this" parameter adjustment in a virtual function
295 virtual llvm::Value *adjustThisParameterInVirtualFunctionPrologue(
296 CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) {
300 /// Emit the ABI-specific prolog for the function.
301 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
303 /// Emit the constructor call. Return the function that is called.
304 virtual void EmitConstructorCall(CodeGenFunction &CGF,
305 const CXXConstructorDecl *D,
307 bool ForVirtualBase, bool Delegating,
309 CallExpr::const_arg_iterator ArgBeg,
310 CallExpr::const_arg_iterator ArgEnd) = 0;
312 /// Emits the VTable definitions required for the given record type.
313 virtual void emitVTableDefinitions(CodeGenVTables &CGVT,
314 const CXXRecordDecl *RD) = 0;
316 /// Get the address point of the vtable for the given base subobject while
317 /// building a constructor or a destructor. On return, NeedsVirtualOffset
318 /// tells if a virtual base adjustment is needed in order to get the offset
319 /// of the base subobject.
320 virtual llvm::Value *getVTableAddressPointInStructor(
321 CodeGenFunction &CGF, const CXXRecordDecl *RD, BaseSubobject Base,
322 const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) = 0;
324 /// Get the address point of the vtable for the given base subobject while
325 /// building a constexpr.
326 virtual llvm::Constant *
327 getVTableAddressPointForConstExpr(BaseSubobject Base,
328 const CXXRecordDecl *VTableClass) = 0;
330 /// Get the address of the vtable for the given record decl which should be
331 /// used for the vptr at the given offset in RD.
332 virtual llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD,
333 CharUnits VPtrOffset) = 0;
335 /// Build a virtual function pointer in the ABI-specific way.
336 virtual llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF,
341 /// Emit the ABI-specific virtual destructor call.
342 virtual void EmitVirtualDestructorCall(CodeGenFunction &CGF,
343 const CXXDestructorDecl *Dtor,
344 CXXDtorType DtorType,
345 SourceLocation CallLoc,
346 llvm::Value *This) = 0;
348 virtual void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF,
350 CallArgList &CallArgs) {}
352 /// Emit any tables needed to implement virtual inheritance. For Itanium,
353 /// this emits virtual table tables. For the MSVC++ ABI, this emits virtual
355 virtual void emitVirtualInheritanceTables(const CXXRecordDecl *RD) = 0;
357 virtual void setThunkLinkage(llvm::Function *Thunk, bool ForVTable) = 0;
359 virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
360 RValue RV, QualType ResultType);
362 /// Gets the pure virtual member call function.
363 virtual StringRef GetPureVirtualCallName() = 0;
365 /// Gets the deleted virtual member call name.
366 virtual StringRef GetDeletedVirtualCallName() = 0;
368 /**************************** Array cookies ******************************/
370 /// Returns the extra size required in order to store the array
371 /// cookie for the given new-expression. May return 0 to indicate that no
372 /// array cookie is required.
374 /// Several cases are filtered out before this method is called:
375 /// - non-array allocations never need a cookie
376 /// - calls to \::operator new(size_t, void*) never need a cookie
378 /// \param expr - the new-expression being allocated.
379 virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
381 /// Initialize the array cookie for the given allocation.
383 /// \param NewPtr - a char* which is the presumed-non-null
384 /// return value of the allocation function
385 /// \param NumElements - the computed number of elements,
386 /// potentially collapsed from the multidimensional array case;
388 /// \param ElementType - the base element allocated type,
389 /// i.e. the allocated type after stripping all array types
390 virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
392 llvm::Value *NumElements,
393 const CXXNewExpr *expr,
394 QualType ElementType);
396 /// Reads the array cookie associated with the given pointer,
399 /// \param Ptr - a pointer to the first element in the array
400 /// \param ElementType - the base element type of elements of the array
401 /// \param NumElements - an out parameter which will be initialized
402 /// with the number of elements allocated, or zero if there is no
404 /// \param AllocPtr - an out parameter which will be initialized
405 /// with a char* pointing to the address returned by the allocation
407 /// \param CookieSize - an out parameter which will be initialized
408 /// with the size of the cookie, or zero if there is no cookie
409 virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
410 const CXXDeleteExpr *expr,
411 QualType ElementType, llvm::Value *&NumElements,
412 llvm::Value *&AllocPtr, CharUnits &CookieSize);
414 /// Return whether the given global decl needs a VTT parameter.
415 virtual bool NeedsVTTParameter(GlobalDecl GD);
418 /// Returns the extra size required in order to store the array
419 /// cookie for the given type. Assumes that an array cookie is
421 virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
423 /// Reads the array cookie for an allocation which is known to have one.
424 /// This is called by the standard implementation of ReadArrayCookie.
426 /// \param ptr - a pointer to the allocation made for an array, as a char*
427 /// \param cookieSize - the computed cookie size of an array
429 /// Other parameters are as above.
432 virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF,
434 CharUnits cookieSize);
438 /*************************** Static local guards ****************************/
440 /// Emits the guarded initializer and destructor setup for the given
441 /// variable, given that it couldn't be emitted as a constant.
442 /// If \p PerformInit is false, the initialization has been folded to a
443 /// constant and should not be performed.
445 /// The variable may be:
446 /// - a static local variable
447 /// - a static data member of a class template instantiation
448 virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
449 llvm::GlobalVariable *DeclPtr,
450 bool PerformInit) = 0;
452 /// Emit code to force the execution of a destructor during global
453 /// teardown. The default implementation of this uses atexit.
455 /// \param dtor - a function taking a single pointer argument
456 /// \param addr - a pointer to pass to the destructor function.
457 virtual void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
458 llvm::Constant *dtor, llvm::Constant *addr);
460 /*************************** thread_local initialization ********************/
462 /// Emits ABI-required functions necessary to initialize thread_local
463 /// variables in this translation unit.
465 /// \param Decls The thread_local declarations in this translation unit.
466 /// \param InitFunc If this translation unit contains any non-constant
467 /// initialization or non-trivial destruction for thread_local
468 /// variables, a function to perform the initialization. Otherwise, 0.
469 virtual void EmitThreadLocalInitFuncs(
470 llvm::ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *> > Decls,
471 llvm::Function *InitFunc);
473 /// Emit a reference to a non-local thread_local variable (including
474 /// triggering the initialization of all thread_local variables in its
475 /// translation unit).
476 virtual LValue EmitThreadLocalDeclRefExpr(CodeGenFunction &CGF,
477 const DeclRefExpr *DRE);
480 // Create an instance of a C++ ABI class:
482 /// Creates an Itanium-family ABI.
483 CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
485 /// Creates a Microsoft-family ABI.
486 CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);