clang API Documentation
00001 //===----- CGCXXABI.h - Interface to C++ ABIs -------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This provides an abstract class for C++ code generation. Concrete subclasses 00011 // of this implement code generation for specific C++ ABIs. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_CLANG_LIB_CODEGEN_CGCXXABI_H 00016 #define LLVM_CLANG_LIB_CODEGEN_CGCXXABI_H 00017 00018 #include "CodeGenFunction.h" 00019 #include "clang/Basic/LLVM.h" 00020 00021 namespace llvm { 00022 class Constant; 00023 class Type; 00024 class Value; 00025 } 00026 00027 namespace clang { 00028 class CastExpr; 00029 class CXXConstructorDecl; 00030 class CXXDestructorDecl; 00031 class CXXMethodDecl; 00032 class CXXRecordDecl; 00033 class FieldDecl; 00034 class MangleContext; 00035 00036 namespace CodeGen { 00037 class CodeGenFunction; 00038 class CodeGenModule; 00039 00040 /// \brief Implements C++ ABI-specific code generation functions. 00041 class CGCXXABI { 00042 protected: 00043 CodeGenModule &CGM; 00044 std::unique_ptr<MangleContext> MangleCtx; 00045 00046 CGCXXABI(CodeGenModule &CGM) 00047 : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {} 00048 00049 protected: 00050 ImplicitParamDecl *&getThisDecl(CodeGenFunction &CGF) { 00051 return CGF.CXXABIThisDecl; 00052 } 00053 llvm::Value *&getThisValue(CodeGenFunction &CGF) { 00054 return CGF.CXXABIThisValue; 00055 } 00056 00057 /// Issue a diagnostic about unsupported features in the ABI. 00058 void ErrorUnsupportedABI(CodeGenFunction &CGF, StringRef S); 00059 00060 /// Get a null value for unsupported member pointers. 00061 llvm::Constant *GetBogusMemberPointer(QualType T); 00062 00063 ImplicitParamDecl *&getStructorImplicitParamDecl(CodeGenFunction &CGF) { 00064 return CGF.CXXStructorImplicitParamDecl; 00065 } 00066 llvm::Value *&getStructorImplicitParamValue(CodeGenFunction &CGF) { 00067 return CGF.CXXStructorImplicitParamValue; 00068 } 00069 00070 /// Perform prolog initialization of the parameter variable suitable 00071 /// for 'this' emitted by buildThisParam. 00072 void EmitThisParam(CodeGenFunction &CGF); 00073 00074 ASTContext &getContext() const { return CGM.getContext(); } 00075 00076 virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType); 00077 virtual bool requiresArrayCookie(const CXXNewExpr *E); 00078 00079 public: 00080 00081 virtual ~CGCXXABI(); 00082 00083 /// Gets the mangle context. 00084 MangleContext &getMangleContext() { 00085 return *MangleCtx; 00086 } 00087 00088 /// Returns true if the given constructor or destructor is one of the 00089 /// kinds that the ABI says returns 'this' (only applies when called 00090 /// non-virtually for destructors). 00091 /// 00092 /// There currently is no way to indicate if a destructor returns 'this' 00093 /// when called virtually, and code generation does not support the case. 00094 virtual bool HasThisReturn(GlobalDecl GD) const { return false; } 00095 00096 virtual bool hasMostDerivedReturn(GlobalDecl GD) const { return false; } 00097 00098 /// If the C++ ABI requires the given type be returned in a particular way, 00099 /// this method sets RetAI and returns true. 00100 virtual bool classifyReturnType(CGFunctionInfo &FI) const = 0; 00101 00102 /// Specify how one should pass an argument of a record type. 00103 enum RecordArgABI { 00104 /// Pass it using the normal C aggregate rules for the ABI, potentially 00105 /// introducing extra copies and passing some or all of it in registers. 00106 RAA_Default = 0, 00107 00108 /// Pass it on the stack using its defined layout. The argument must be 00109 /// evaluated directly into the correct stack position in the arguments area, 00110 /// and the call machinery must not move it or introduce extra copies. 00111 RAA_DirectInMemory, 00112 00113 /// Pass it as a pointer to temporary memory. 00114 RAA_Indirect 00115 }; 00116 00117 /// Returns true if C++ allows us to copy the memory of an object of type RD 00118 /// when it is passed as an argument. 00119 bool canCopyArgument(const CXXRecordDecl *RD) const; 00120 00121 /// Returns how an argument of the given record type should be passed. 00122 virtual RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const = 0; 00123 00124 /// Returns true if the implicit 'sret' parameter comes after the implicit 00125 /// 'this' parameter of C++ instance methods. 00126 virtual bool isSRetParameterAfterThis() const { return false; } 00127 00128 /// Find the LLVM type used to represent the given member pointer 00129 /// type. 00130 virtual llvm::Type * 00131 ConvertMemberPointerType(const MemberPointerType *MPT); 00132 00133 /// Load a member function from an object and a member function 00134 /// pointer. Apply the this-adjustment and set 'This' to the 00135 /// adjusted value. 00136 virtual llvm::Value *EmitLoadOfMemberFunctionPointer( 00137 CodeGenFunction &CGF, const Expr *E, llvm::Value *&This, 00138 llvm::Value *MemPtr, const MemberPointerType *MPT); 00139 00140 /// Calculate an l-value from an object and a data member pointer. 00141 virtual llvm::Value * 00142 EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E, 00143 llvm::Value *Base, llvm::Value *MemPtr, 00144 const MemberPointerType *MPT); 00145 00146 /// Perform a derived-to-base, base-to-derived, or bitcast member 00147 /// pointer conversion. 00148 virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, 00149 const CastExpr *E, 00150 llvm::Value *Src); 00151 00152 /// Perform a derived-to-base, base-to-derived, or bitcast member 00153 /// pointer conversion on a constant value. 00154 virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E, 00155 llvm::Constant *Src); 00156 00157 /// Return true if the given member pointer can be zero-initialized 00158 /// (in the C++ sense) with an LLVM zeroinitializer. 00159 virtual bool isZeroInitializable(const MemberPointerType *MPT); 00160 00161 /// Return whether or not a member pointers type is convertible to an IR type. 00162 virtual bool isMemberPointerConvertible(const MemberPointerType *MPT) const { 00163 return true; 00164 } 00165 00166 virtual bool isTypeInfoCalculable(QualType Ty) const { 00167 return !Ty->isIncompleteType(); 00168 } 00169 00170 /// Create a null member pointer of the given type. 00171 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT); 00172 00173 /// Create a member pointer for the given method. 00174 virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD); 00175 00176 /// Create a member pointer for the given field. 00177 virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT, 00178 CharUnits offset); 00179 00180 /// Create a member pointer for the given member pointer constant. 00181 virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT); 00182 00183 /// Emit a comparison between two member pointers. Returns an i1. 00184 virtual llvm::Value * 00185 EmitMemberPointerComparison(CodeGenFunction &CGF, 00186 llvm::Value *L, 00187 llvm::Value *R, 00188 const MemberPointerType *MPT, 00189 bool Inequality); 00190 00191 /// Determine if a member pointer is non-null. Returns an i1. 00192 virtual llvm::Value * 00193 EmitMemberPointerIsNotNull(CodeGenFunction &CGF, 00194 llvm::Value *MemPtr, 00195 const MemberPointerType *MPT); 00196 00197 protected: 00198 /// A utility method for computing the offset required for the given 00199 /// base-to-derived or derived-to-base member-pointer conversion. 00200 /// Does not handle virtual conversions (in case we ever fully 00201 /// support an ABI that allows this). Returns null if no adjustment 00202 /// is required. 00203 llvm::Constant *getMemberPointerAdjustment(const CastExpr *E); 00204 00205 /// \brief Computes the non-virtual adjustment needed for a member pointer 00206 /// conversion along an inheritance path stored in an APValue. Unlike 00207 /// getMemberPointerAdjustment(), the adjustment can be negative if the path 00208 /// is from a derived type to a base type. 00209 CharUnits getMemberPointerPathAdjustment(const APValue &MP); 00210 00211 public: 00212 virtual void emitVirtualObjectDelete(CodeGenFunction &CGF, 00213 const CXXDeleteExpr *DE, 00214 llvm::Value *Ptr, QualType ElementType, 00215 const CXXDestructorDecl *Dtor) = 0; 00216 00217 virtual llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) = 0; 00218 00219 virtual bool shouldTypeidBeNullChecked(bool IsDeref, 00220 QualType SrcRecordTy) = 0; 00221 virtual void EmitBadTypeidCall(CodeGenFunction &CGF) = 0; 00222 virtual llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy, 00223 llvm::Value *ThisPtr, 00224 llvm::Type *StdTypeInfoPtrTy) = 0; 00225 00226 virtual bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr, 00227 QualType SrcRecordTy) = 0; 00228 00229 virtual llvm::Value * 00230 EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value, 00231 QualType SrcRecordTy, QualType DestTy, 00232 QualType DestRecordTy, llvm::BasicBlock *CastEnd) = 0; 00233 00234 virtual llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF, 00235 llvm::Value *Value, 00236 QualType SrcRecordTy, 00237 QualType DestTy) = 0; 00238 00239 virtual bool EmitBadCastCall(CodeGenFunction &CGF) = 0; 00240 00241 virtual llvm::Value *GetVirtualBaseClassOffset(CodeGenFunction &CGF, 00242 llvm::Value *This, 00243 const CXXRecordDecl *ClassDecl, 00244 const CXXRecordDecl *BaseClassDecl) = 0; 00245 00246 virtual llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, 00247 const CXXRecordDecl *RD); 00248 00249 /// Emit the code to initialize hidden members required 00250 /// to handle virtual inheritance, if needed by the ABI. 00251 virtual void 00252 initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF, 00253 const CXXRecordDecl *RD) {} 00254 00255 /// Emit constructor variants required by this ABI. 00256 virtual void EmitCXXConstructors(const CXXConstructorDecl *D) = 0; 00257 00258 /// Build the signature of the given constructor or destructor variant by 00259 /// adding any required parameters. For convenience, ArgTys has been 00260 /// initialized with the type of 'this'. 00261 virtual void buildStructorSignature(const CXXMethodDecl *MD, StructorType T, 00262 SmallVectorImpl<CanQualType> &ArgTys) = 0; 00263 00264 /// Returns true if the given destructor type should be emitted as a linkonce 00265 /// delegating thunk, regardless of whether the dtor is defined in this TU or 00266 /// not. 00267 virtual bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor, 00268 CXXDtorType DT) const = 0; 00269 00270 /// Emit destructor variants required by this ABI. 00271 virtual void EmitCXXDestructors(const CXXDestructorDecl *D) = 0; 00272 00273 /// Get the type of the implicit "this" parameter used by a method. May return 00274 /// zero if no specific type is applicable, e.g. if the ABI expects the "this" 00275 /// parameter to point to some artificial offset in a complete object due to 00276 /// vbases being reordered. 00277 virtual const CXXRecordDecl * 00278 getThisArgumentTypeForMethod(const CXXMethodDecl *MD) { 00279 return MD->getParent(); 00280 } 00281 00282 /// Perform ABI-specific "this" argument adjustment required prior to 00283 /// a call of a virtual function. 00284 /// The "VirtualCall" argument is true iff the call itself is virtual. 00285 virtual llvm::Value * 00286 adjustThisArgumentForVirtualFunctionCall(CodeGenFunction &CGF, GlobalDecl GD, 00287 llvm::Value *This, 00288 bool VirtualCall) { 00289 return This; 00290 } 00291 00292 /// Build a parameter variable suitable for 'this'. 00293 void buildThisParam(CodeGenFunction &CGF, FunctionArgList &Params); 00294 00295 /// Insert any ABI-specific implicit parameters into the parameter list for a 00296 /// function. This generally involves extra data for constructors and 00297 /// destructors. 00298 /// 00299 /// ABIs may also choose to override the return type, which has been 00300 /// initialized with the type of 'this' if HasThisReturn(CGF.CurGD) is true or 00301 /// the formal return type of the function otherwise. 00302 virtual void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy, 00303 FunctionArgList &Params) = 0; 00304 00305 /// Perform ABI-specific "this" parameter adjustment in a virtual function 00306 /// prologue. 00307 virtual llvm::Value *adjustThisParameterInVirtualFunctionPrologue( 00308 CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) { 00309 return This; 00310 } 00311 00312 /// Emit the ABI-specific prolog for the function. 00313 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0; 00314 00315 /// Add any ABI-specific implicit arguments needed to call a constructor. 00316 /// 00317 /// \return The number of args added to the call, which is typically zero or 00318 /// one. 00319 virtual unsigned 00320 addImplicitConstructorArgs(CodeGenFunction &CGF, const CXXConstructorDecl *D, 00321 CXXCtorType Type, bool ForVirtualBase, 00322 bool Delegating, CallArgList &Args) = 0; 00323 00324 /// Emit the destructor call. 00325 virtual void EmitDestructorCall(CodeGenFunction &CGF, 00326 const CXXDestructorDecl *DD, CXXDtorType Type, 00327 bool ForVirtualBase, bool Delegating, 00328 llvm::Value *This) = 0; 00329 00330 /// Emits the VTable definitions required for the given record type. 00331 virtual void emitVTableDefinitions(CodeGenVTables &CGVT, 00332 const CXXRecordDecl *RD) = 0; 00333 00334 /// Get the address point of the vtable for the given base subobject while 00335 /// building a constructor or a destructor. On return, NeedsVirtualOffset 00336 /// tells if a virtual base adjustment is needed in order to get the offset 00337 /// of the base subobject. 00338 virtual llvm::Value *getVTableAddressPointInStructor( 00339 CodeGenFunction &CGF, const CXXRecordDecl *RD, BaseSubobject Base, 00340 const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) = 0; 00341 00342 /// Get the address point of the vtable for the given base subobject while 00343 /// building a constexpr. 00344 virtual llvm::Constant * 00345 getVTableAddressPointForConstExpr(BaseSubobject Base, 00346 const CXXRecordDecl *VTableClass) = 0; 00347 00348 /// Get the address of the vtable for the given record decl which should be 00349 /// used for the vptr at the given offset in RD. 00350 virtual llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD, 00351 CharUnits VPtrOffset) = 0; 00352 00353 /// Build a virtual function pointer in the ABI-specific way. 00354 virtual llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF, 00355 GlobalDecl GD, 00356 llvm::Value *This, 00357 llvm::Type *Ty) = 0; 00358 00359 /// Emit the ABI-specific virtual destructor call. 00360 virtual llvm::Value * 00361 EmitVirtualDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, 00362 CXXDtorType DtorType, llvm::Value *This, 00363 const CXXMemberCallExpr *CE) = 0; 00364 00365 virtual void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF, 00366 GlobalDecl GD, 00367 CallArgList &CallArgs) {} 00368 00369 /// Emit any tables needed to implement virtual inheritance. For Itanium, 00370 /// this emits virtual table tables. For the MSVC++ ABI, this emits virtual 00371 /// base tables. 00372 virtual void emitVirtualInheritanceTables(const CXXRecordDecl *RD) = 0; 00373 00374 virtual void setThunkLinkage(llvm::Function *Thunk, bool ForVTable, 00375 GlobalDecl GD, bool ReturnAdjustment) = 0; 00376 00377 virtual llvm::Value *performThisAdjustment(CodeGenFunction &CGF, 00378 llvm::Value *This, 00379 const ThisAdjustment &TA) = 0; 00380 00381 virtual llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, 00382 llvm::Value *Ret, 00383 const ReturnAdjustment &RA) = 0; 00384 00385 virtual void EmitReturnFromThunk(CodeGenFunction &CGF, 00386 RValue RV, QualType ResultType); 00387 00388 virtual size_t getSrcArgforCopyCtor(const CXXConstructorDecl *, 00389 FunctionArgList &Args) const = 0; 00390 00391 /// Gets the pure virtual member call function. 00392 virtual StringRef GetPureVirtualCallName() = 0; 00393 00394 /// Gets the deleted virtual member call name. 00395 virtual StringRef GetDeletedVirtualCallName() = 0; 00396 00397 /**************************** Array cookies ******************************/ 00398 00399 /// Returns the extra size required in order to store the array 00400 /// cookie for the given new-expression. May return 0 to indicate that no 00401 /// array cookie is required. 00402 /// 00403 /// Several cases are filtered out before this method is called: 00404 /// - non-array allocations never need a cookie 00405 /// - calls to \::operator new(size_t, void*) never need a cookie 00406 /// 00407 /// \param expr - the new-expression being allocated. 00408 virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr); 00409 00410 /// Initialize the array cookie for the given allocation. 00411 /// 00412 /// \param NewPtr - a char* which is the presumed-non-null 00413 /// return value of the allocation function 00414 /// \param NumElements - the computed number of elements, 00415 /// potentially collapsed from the multidimensional array case; 00416 /// always a size_t 00417 /// \param ElementType - the base element allocated type, 00418 /// i.e. the allocated type after stripping all array types 00419 virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, 00420 llvm::Value *NewPtr, 00421 llvm::Value *NumElements, 00422 const CXXNewExpr *expr, 00423 QualType ElementType); 00424 00425 /// Reads the array cookie associated with the given pointer, 00426 /// if it has one. 00427 /// 00428 /// \param Ptr - a pointer to the first element in the array 00429 /// \param ElementType - the base element type of elements of the array 00430 /// \param NumElements - an out parameter which will be initialized 00431 /// with the number of elements allocated, or zero if there is no 00432 /// cookie 00433 /// \param AllocPtr - an out parameter which will be initialized 00434 /// with a char* pointing to the address returned by the allocation 00435 /// function 00436 /// \param CookieSize - an out parameter which will be initialized 00437 /// with the size of the cookie, or zero if there is no cookie 00438 virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr, 00439 const CXXDeleteExpr *expr, 00440 QualType ElementType, llvm::Value *&NumElements, 00441 llvm::Value *&AllocPtr, CharUnits &CookieSize); 00442 00443 /// Return whether the given global decl needs a VTT parameter. 00444 virtual bool NeedsVTTParameter(GlobalDecl GD); 00445 00446 protected: 00447 /// Returns the extra size required in order to store the array 00448 /// cookie for the given type. Assumes that an array cookie is 00449 /// required. 00450 virtual CharUnits getArrayCookieSizeImpl(QualType elementType); 00451 00452 /// Reads the array cookie for an allocation which is known to have one. 00453 /// This is called by the standard implementation of ReadArrayCookie. 00454 /// 00455 /// \param ptr - a pointer to the allocation made for an array, as a char* 00456 /// \param cookieSize - the computed cookie size of an array 00457 /// 00458 /// Other parameters are as above. 00459 /// 00460 /// \return a size_t 00461 virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF, 00462 llvm::Value *ptr, 00463 CharUnits cookieSize); 00464 00465 public: 00466 00467 /*************************** Static local guards ****************************/ 00468 00469 /// Emits the guarded initializer and destructor setup for the given 00470 /// variable, given that it couldn't be emitted as a constant. 00471 /// If \p PerformInit is false, the initialization has been folded to a 00472 /// constant and should not be performed. 00473 /// 00474 /// The variable may be: 00475 /// - a static local variable 00476 /// - a static data member of a class template instantiation 00477 virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, 00478 llvm::GlobalVariable *DeclPtr, 00479 bool PerformInit) = 0; 00480 00481 /// Emit code to force the execution of a destructor during global 00482 /// teardown. The default implementation of this uses atexit. 00483 /// 00484 /// \param Dtor - a function taking a single pointer argument 00485 /// \param Addr - a pointer to pass to the destructor function. 00486 virtual void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D, 00487 llvm::Constant *Dtor, 00488 llvm::Constant *Addr) = 0; 00489 00490 /*************************** thread_local initialization ********************/ 00491 00492 /// Emits ABI-required functions necessary to initialize thread_local 00493 /// variables in this translation unit. 00494 /// 00495 /// \param CXXThreadLocals - The thread_local declarations in this translation 00496 /// unit. 00497 /// \param CXXThreadLocalInits - If this translation unit contains any 00498 /// non-constant initialization or non-trivial destruction for 00499 /// thread_local variables, a list of functions to perform the 00500 /// initialization. 00501 virtual void EmitThreadLocalInitFuncs( 00502 CodeGenModule &CGM, 00503 ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>> 00504 CXXThreadLocals, 00505 ArrayRef<llvm::Function *> CXXThreadLocalInits, 00506 ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) = 0; 00507 00508 // Determine if references to thread_local global variables can be made 00509 // directly or require access through a thread wrapper function. 00510 virtual bool usesThreadWrapperFunction() const = 0; 00511 00512 /// Emit a reference to a non-local thread_local variable (including 00513 /// triggering the initialization of all thread_local variables in its 00514 /// translation unit). 00515 virtual LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, 00516 const VarDecl *VD, 00517 QualType LValType) = 0; 00518 00519 /// Emit a single constructor/destructor with the given type from a C++ 00520 /// constructor Decl. 00521 virtual void emitCXXStructor(const CXXMethodDecl *MD, StructorType Type) = 0; 00522 }; 00523 00524 // Create an instance of a C++ ABI class: 00525 00526 /// Creates an Itanium-family ABI. 00527 CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM); 00528 00529 /// Creates a Microsoft-family ABI. 00530 CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM); 00531 00532 } 00533 } 00534 00535 #endif