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 ImplicitParamDecl *&getStructorImplicitParamDecl(CodeGenFunction &CGF) {
64 return CGF.CXXStructorImplicitParamDecl;
66 llvm::Value *&getStructorImplicitParamValue(CodeGenFunction &CGF) {
67 return CGF.CXXStructorImplicitParamValue;
70 /// Build a parameter variable suitable for 'this'.
71 void BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params);
73 /// Perform prolog initialization of the parameter variable suitable
74 /// for 'this' emitted by BuildThisParam.
75 void EmitThisParam(CodeGenFunction &CGF);
77 ASTContext &getContext() const { return CGM.getContext(); }
79 virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType);
80 virtual bool requiresArrayCookie(const CXXNewExpr *E);
86 /// Gets the mangle context.
87 MangleContext &getMangleContext() {
91 /// Returns true if the given constructor or destructor is one of the
92 /// kinds that the ABI says returns 'this' (only applies when called
93 /// non-virtually for destructors).
95 /// There currently is no way to indicate if a destructor returns 'this'
96 /// when called virtually, and code generation does not support the case.
97 virtual bool HasThisReturn(GlobalDecl GD) const { return false; }
99 /// Returns true if the given record type should be returned indirectly.
100 virtual bool isReturnTypeIndirect(const CXXRecordDecl *RD) const = 0;
102 /// Specify how one should pass an argument of a record type.
104 /// Pass it using the normal C aggregate rules for the ABI, potentially
105 /// introducing extra copies and passing some or all of it in registers.
108 /// Pass it on the stack using its defined layout. The argument must be
109 /// evaluated directly into the correct stack position in the arguments area,
110 /// and the call machinery must not move it or introduce extra copies.
113 /// Pass it as a pointer to temporary memory.
117 /// Returns how an argument of the given record type should be passed.
118 virtual RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const = 0;
120 /// Find the LLVM type used to represent the given member pointer
123 ConvertMemberPointerType(const MemberPointerType *MPT);
125 /// Load a member function from an object and a member function
126 /// pointer. Apply the this-adjustment and set 'This' to the
128 virtual llvm::Value *
129 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
132 const MemberPointerType *MPT);
134 /// Calculate an l-value from an object and a data member pointer.
135 virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
138 const MemberPointerType *MPT);
140 /// Perform a derived-to-base, base-to-derived, or bitcast member
141 /// pointer conversion.
142 virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
146 /// Perform a derived-to-base, base-to-derived, or bitcast member
147 /// pointer conversion on a constant value.
148 virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
149 llvm::Constant *Src);
151 /// Return true if the given member pointer can be zero-initialized
152 /// (in the C++ sense) with an LLVM zeroinitializer.
153 virtual bool isZeroInitializable(const MemberPointerType *MPT);
155 /// Create a null member pointer of the given type.
156 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
158 /// Create a member pointer for the given method.
159 virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
161 /// Create a member pointer for the given field.
162 virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
165 /// Create a member pointer for the given member pointer constant.
166 virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
168 /// Emit a comparison between two member pointers. Returns an i1.
169 virtual llvm::Value *
170 EmitMemberPointerComparison(CodeGenFunction &CGF,
173 const MemberPointerType *MPT,
176 /// Determine if a member pointer is non-null. Returns an i1.
177 virtual llvm::Value *
178 EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
180 const MemberPointerType *MPT);
183 /// A utility method for computing the offset required for the given
184 /// base-to-derived or derived-to-base member-pointer conversion.
185 /// Does not handle virtual conversions (in case we ever fully
186 /// support an ABI that allows this). Returns null if no adjustment
188 llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
190 /// \brief Computes the non-virtual adjustment needed for a member pointer
191 /// conversion along an inheritance path stored in an APValue. Unlike
192 /// getMemberPointerAdjustment(), the adjustment can be negative if the path
193 /// is from a derived type to a base type.
194 CharUnits getMemberPointerPathAdjustment(const APValue &MP);
197 /// Adjust the given non-null pointer to an object of polymorphic
198 /// type to point to the complete object.
200 /// The IR type of the result should be a pointer but is otherwise
202 virtual llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
206 virtual llvm::Value *GetVirtualBaseClassOffset(CodeGenFunction &CGF,
208 const CXXRecordDecl *ClassDecl,
209 const CXXRecordDecl *BaseClassDecl) = 0;
211 /// Build the signature of the given constructor variant by adding
212 /// any required parameters. For convenience, ArgTys has been initialized
213 /// with the type of 'this' and ResTy has been initialized with the type of
214 /// 'this' if HasThisReturn(GlobalDecl(Ctor, T)) is true or 'void' otherwise
215 /// (although both may be changed by the ABI).
217 /// If there are ever any ABIs where the implicit parameters are
218 /// intermixed with the formal parameters, we can address those
220 virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
223 SmallVectorImpl<CanQualType> &ArgTys) = 0;
225 virtual llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
226 const CXXRecordDecl *RD);
228 /// Emit the code to initialize hidden members required
229 /// to handle virtual inheritance, if needed by the ABI.
231 initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF,
232 const CXXRecordDecl *RD) {}
234 /// Emit constructor variants required by this ABI.
235 virtual void EmitCXXConstructors(const CXXConstructorDecl *D) = 0;
237 /// Build the signature of the given destructor variant by adding
238 /// any required parameters. For convenience, ArgTys has been initialized
239 /// with the type of 'this' and ResTy has been initialized with the type of
240 /// 'this' if HasThisReturn(GlobalDecl(Dtor, T)) is true or 'void' otherwise
241 /// (although both may be changed by the ABI).
242 virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
245 SmallVectorImpl<CanQualType> &ArgTys) = 0;
247 /// Returns true if the given destructor type should be emitted as a linkonce
248 /// delegating thunk, regardless of whether the dtor is defined in this TU or
250 virtual bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor,
251 CXXDtorType DT) const = 0;
253 /// Emit destructor variants required by this ABI.
254 virtual void EmitCXXDestructors(const CXXDestructorDecl *D) = 0;
256 /// Get the type of the implicit "this" parameter used by a method. May return
257 /// zero if no specific type is applicable, e.g. if the ABI expects the "this"
258 /// parameter to point to some artificial offset in a complete object due to
259 /// vbases being reordered.
260 virtual const CXXRecordDecl *
261 getThisArgumentTypeForMethod(const CXXMethodDecl *MD) {
262 return MD->getParent();
265 /// Perform ABI-specific "this" argument adjustment required prior to
266 /// a virtual function call.
267 virtual llvm::Value *adjustThisArgumentForVirtualCall(CodeGenFunction &CGF,
273 /// Build the ABI-specific portion of the parameter list for a
274 /// function. This generally involves a 'this' parameter and
275 /// possibly some extra data for constructors and destructors.
277 /// ABIs may also choose to override the return type, which has been
278 /// initialized with the type of 'this' if HasThisReturn(CGF.CurGD) is true or
279 /// the formal return type of the function otherwise.
280 virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
282 FunctionArgList &Params) = 0;
284 /// Perform ABI-specific "this" parameter adjustment in a virtual function
286 virtual llvm::Value *adjustThisParameterInVirtualFunctionPrologue(
287 CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) {
291 /// Emit the ABI-specific prolog for the function.
292 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
294 /// Emit the constructor call. Return the function that is called.
295 virtual void EmitConstructorCall(CodeGenFunction &CGF,
296 const CXXConstructorDecl *D,
298 bool ForVirtualBase, bool Delegating,
300 CallExpr::const_arg_iterator ArgBeg,
301 CallExpr::const_arg_iterator ArgEnd) = 0;
303 /// Emits the VTable definitions required for the given record type.
304 virtual void emitVTableDefinitions(CodeGenVTables &CGVT,
305 const CXXRecordDecl *RD) = 0;
307 /// Get the address point of the vtable for the given base subobject while
308 /// building a constructor or a destructor. On return, NeedsVirtualOffset
309 /// tells if a virtual base adjustment is needed in order to get the offset
310 /// of the base subobject.
311 virtual llvm::Value *getVTableAddressPointInStructor(
312 CodeGenFunction &CGF, const CXXRecordDecl *RD, BaseSubobject Base,
313 const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) = 0;
315 /// Get the address point of the vtable for the given base subobject while
316 /// building a constexpr.
317 virtual llvm::Constant *
318 getVTableAddressPointForConstExpr(BaseSubobject Base,
319 const CXXRecordDecl *VTableClass) = 0;
321 /// Get the address of the vtable for the given record decl which should be
322 /// used for the vptr at the given offset in RD.
323 virtual llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD,
324 CharUnits VPtrOffset) = 0;
326 /// Build a virtual function pointer in the ABI-specific way.
327 virtual llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF,
332 /// Emit the ABI-specific virtual destructor call.
333 virtual void EmitVirtualDestructorCall(CodeGenFunction &CGF,
334 const CXXDestructorDecl *Dtor,
335 CXXDtorType DtorType,
336 SourceLocation CallLoc,
337 llvm::Value *This) = 0;
339 virtual void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF,
341 CallArgList &CallArgs) {}
343 /// Emit any tables needed to implement virtual inheritance. For Itanium,
344 /// this emits virtual table tables. For the MSVC++ ABI, this emits virtual
346 virtual void emitVirtualInheritanceTables(const CXXRecordDecl *RD) = 0;
348 virtual void setThunkLinkage(llvm::Function *Thunk, bool ForVTable) = 0;
350 virtual llvm::Value *performThisAdjustment(CodeGenFunction &CGF,
352 const ThisAdjustment &TA) = 0;
354 virtual llvm::Value *performReturnAdjustment(CodeGenFunction &CGF,
356 const ReturnAdjustment &RA) = 0;
358 virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
359 RValue RV, QualType ResultType);
361 /// Gets the pure virtual member call function.
362 virtual StringRef GetPureVirtualCallName() = 0;
364 /// Gets the deleted virtual member call name.
365 virtual StringRef GetDeletedVirtualCallName() = 0;
367 /// \brief Returns true iff static data members that are initialized in the
368 /// class definition should have linkonce linkage.
369 virtual bool isInlineInitializedStaticDataMemberLinkOnce() { return false; }
371 /**************************** Array cookies ******************************/
373 /// Returns the extra size required in order to store the array
374 /// cookie for the given new-expression. May return 0 to indicate that no
375 /// array cookie is required.
377 /// Several cases are filtered out before this method is called:
378 /// - non-array allocations never need a cookie
379 /// - calls to \::operator new(size_t, void*) never need a cookie
381 /// \param expr - the new-expression being allocated.
382 virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
384 /// Initialize the array cookie for the given allocation.
386 /// \param NewPtr - a char* which is the presumed-non-null
387 /// return value of the allocation function
388 /// \param NumElements - the computed number of elements,
389 /// potentially collapsed from the multidimensional array case;
391 /// \param ElementType - the base element allocated type,
392 /// i.e. the allocated type after stripping all array types
393 virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
395 llvm::Value *NumElements,
396 const CXXNewExpr *expr,
397 QualType ElementType);
399 /// Reads the array cookie associated with the given pointer,
402 /// \param Ptr - a pointer to the first element in the array
403 /// \param ElementType - the base element type of elements of the array
404 /// \param NumElements - an out parameter which will be initialized
405 /// with the number of elements allocated, or zero if there is no
407 /// \param AllocPtr - an out parameter which will be initialized
408 /// with a char* pointing to the address returned by the allocation
410 /// \param CookieSize - an out parameter which will be initialized
411 /// with the size of the cookie, or zero if there is no cookie
412 virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
413 const CXXDeleteExpr *expr,
414 QualType ElementType, llvm::Value *&NumElements,
415 llvm::Value *&AllocPtr, CharUnits &CookieSize);
417 /// Return whether the given global decl needs a VTT parameter.
418 virtual bool NeedsVTTParameter(GlobalDecl GD);
421 /// Returns the extra size required in order to store the array
422 /// cookie for the given type. Assumes that an array cookie is
424 virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
426 /// Reads the array cookie for an allocation which is known to have one.
427 /// This is called by the standard implementation of ReadArrayCookie.
429 /// \param ptr - a pointer to the allocation made for an array, as a char*
430 /// \param cookieSize - the computed cookie size of an array
432 /// Other parameters are as above.
435 virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF,
437 CharUnits cookieSize);
441 /*************************** Static local guards ****************************/
443 /// Emits the guarded initializer and destructor setup for the given
444 /// variable, given that it couldn't be emitted as a constant.
445 /// If \p PerformInit is false, the initialization has been folded to a
446 /// constant and should not be performed.
448 /// The variable may be:
449 /// - a static local variable
450 /// - a static data member of a class template instantiation
451 virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
452 llvm::GlobalVariable *DeclPtr,
453 bool PerformInit) = 0;
455 /// Emit code to force the execution of a destructor during global
456 /// teardown. The default implementation of this uses atexit.
458 /// \param dtor - a function taking a single pointer argument
459 /// \param addr - a pointer to pass to the destructor function.
460 virtual void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
461 llvm::Constant *dtor, llvm::Constant *addr);
463 /*************************** thread_local initialization ********************/
465 /// Emits ABI-required functions necessary to initialize thread_local
466 /// variables in this translation unit.
468 /// \param Decls The thread_local declarations in this translation unit.
469 /// \param InitFunc If this translation unit contains any non-constant
470 /// initialization or non-trivial destruction for thread_local
471 /// variables, a function to perform the initialization. Otherwise, 0.
472 virtual void EmitThreadLocalInitFuncs(
473 llvm::ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *> > Decls,
474 llvm::Function *InitFunc);
476 /// Emit a reference to a non-local thread_local variable (including
477 /// triggering the initialization of all thread_local variables in its
478 /// translation unit).
479 virtual LValue EmitThreadLocalDeclRefExpr(CodeGenFunction &CGF,
480 const DeclRefExpr *DRE);
483 // Create an instance of a C++ ABI class:
485 /// Creates an Itanium-family ABI.
486 CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
488 /// Creates a Microsoft-family ABI.
489 CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);