CGCXXABI.h revision 3258abc2bad74e8bb1799d124bc4113c7234fa42
1//===----- CGCXXABI.h - Interface to C++ ABIs -------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This provides an abstract class for C++ code generation. Concrete subclasses 11// of this implement code generation for specific C++ ABIs. 12// 13//===----------------------------------------------------------------------===// 14 15#ifndef CLANG_CODEGEN_CXXABI_H 16#define CLANG_CODEGEN_CXXABI_H 17 18#include "CodeGenFunction.h" 19#include "clang/Basic/LLVM.h" 20 21namespace llvm { 22 class Constant; 23 class Type; 24 class Value; 25} 26 27namespace clang { 28 class CastExpr; 29 class CXXConstructorDecl; 30 class CXXDestructorDecl; 31 class CXXMethodDecl; 32 class CXXRecordDecl; 33 class FieldDecl; 34 class MangleContext; 35 36namespace CodeGen { 37 class CodeGenFunction; 38 class CodeGenModule; 39 40/// \brief Implements C++ ABI-specific code generation functions. 41class CGCXXABI { 42protected: 43 CodeGenModule &CGM; 44 OwningPtr<MangleContext> MangleCtx; 45 46 CGCXXABI(CodeGenModule &CGM) 47 : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {} 48 49protected: 50 ImplicitParamDecl *&getThisDecl(CodeGenFunction &CGF) { 51 return CGF.CXXABIThisDecl; 52 } 53 llvm::Value *&getThisValue(CodeGenFunction &CGF) { 54 return CGF.CXXABIThisValue; 55 } 56 57 /// Issue a diagnostic about unsupported features in the ABI. 58 void ErrorUnsupportedABI(CodeGenFunction &CGF, StringRef S); 59 60 /// Get a null value for unsupported member pointers. 61 llvm::Constant *GetBogusMemberPointer(QualType T); 62 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; 67 } 68 llvm::Value *&getVTTValue(CodeGenFunction &CGF) { 69 return CGF.CXXStructorImplicitParamValue; 70 } 71 72 ImplicitParamDecl *&getStructorImplicitParamDecl(CodeGenFunction &CGF) { 73 return CGF.CXXStructorImplicitParamDecl; 74 } 75 llvm::Value *&getStructorImplicitParamValue(CodeGenFunction &CGF) { 76 return CGF.CXXStructorImplicitParamValue; 77 } 78 79 /// Build a parameter variable suitable for 'this'. 80 void BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params); 81 82 /// Perform prolog initialization of the parameter variable suitable 83 /// for 'this' emitted by BuildThisParam. 84 void EmitThisParam(CodeGenFunction &CGF); 85 86 ASTContext &getContext() const { return CGM.getContext(); } 87 88 virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType); 89 virtual bool requiresArrayCookie(const CXXNewExpr *E); 90 91public: 92 93 virtual ~CGCXXABI(); 94 95 /// Gets the mangle context. 96 MangleContext &getMangleContext() { 97 return *MangleCtx; 98 } 99 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; } 103 104 /// Returns true if the given record type should be returned indirectly. 105 virtual bool isReturnTypeIndirect(const CXXRecordDecl *RD) const = 0; 106 107 /// Specify how one should pass an argument of a record type. 108 enum RecordArgABI { 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. 111 RAA_Default = 0, 112 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. 116 RAA_DirectInMemory, 117 118 /// Pass it as a pointer to temporary memory. 119 RAA_Indirect 120 }; 121 122 /// Returns how an argument of the given record type should be passed. 123 virtual RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const = 0; 124 125 /// Find the LLVM type used to represent the given member pointer 126 /// type. 127 virtual llvm::Type * 128 ConvertMemberPointerType(const MemberPointerType *MPT); 129 130 /// Load a member function from an object and a member function 131 /// pointer. Apply the this-adjustment and set 'This' to the 132 /// adjusted value. 133 virtual llvm::Value * 134 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, 135 llvm::Value *&This, 136 llvm::Value *MemPtr, 137 const MemberPointerType *MPT); 138 139 /// Calculate an l-value from an object and a data member pointer. 140 virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF, 141 llvm::Value *Base, 142 llvm::Value *MemPtr, 143 const MemberPointerType *MPT); 144 145 /// Perform a derived-to-base, base-to-derived, or bitcast member 146 /// pointer conversion. 147 virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, 148 const CastExpr *E, 149 llvm::Value *Src); 150 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); 155 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); 159 160 /// Create a null member pointer of the given type. 161 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT); 162 163 /// Create a member pointer for the given method. 164 virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD); 165 166 /// Create a member pointer for the given field. 167 virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT, 168 CharUnits offset); 169 170 /// Create a member pointer for the given member pointer constant. 171 virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT); 172 173 /// Emit a comparison between two member pointers. Returns an i1. 174 virtual llvm::Value * 175 EmitMemberPointerComparison(CodeGenFunction &CGF, 176 llvm::Value *L, 177 llvm::Value *R, 178 const MemberPointerType *MPT, 179 bool Inequality); 180 181 /// Determine if a member pointer is non-null. Returns an i1. 182 virtual llvm::Value * 183 EmitMemberPointerIsNotNull(CodeGenFunction &CGF, 184 llvm::Value *MemPtr, 185 const MemberPointerType *MPT); 186 187protected: 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 192 /// is required. 193 llvm::Constant *getMemberPointerAdjustment(const CastExpr *E); 194 195 /// \brief Computes the non-virtual adjustment needed for a member pointer 196 /// conversion along an inheritance path stored in an APValue. Unlike 197 /// getMemberPointerAdjustment(), the adjustment can be negative if the path 198 /// is from a derived type to a base type. 199 CharUnits getMemberPointerPathAdjustment(const APValue &MP); 200 201public: 202 /// Adjust the given non-null pointer to an object of polymorphic 203 /// type to point to the complete object. 204 /// 205 /// The IR type of the result should be a pointer but is otherwise 206 /// irrelevant. 207 virtual llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF, 208 llvm::Value *ptr, 209 QualType type) = 0; 210 211 virtual llvm::Value *GetVirtualBaseClassOffset(CodeGenFunction &CGF, 212 llvm::Value *This, 213 const CXXRecordDecl *ClassDecl, 214 const CXXRecordDecl *BaseClassDecl) = 0; 215 216 /// Build the signature of the given constructor variant by adding 217 /// any required parameters. For convenience, ResTy has been 218 /// initialized to 'void', and ArgTys has been initialized with the 219 /// type of 'this' (although this may be changed by the ABI) and 220 /// will have the formal parameters added to it afterwards. 221 /// 222 /// If there are ever any ABIs where the implicit parameters are 223 /// intermixed with the formal parameters, we can address those 224 /// then. 225 virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor, 226 CXXCtorType T, 227 CanQualType &ResTy, 228 SmallVectorImpl<CanQualType> &ArgTys) = 0; 229 230 virtual llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, 231 const CXXRecordDecl *RD); 232 233 /// Build the signature of the given destructor variant by adding 234 /// any required parameters. For convenience, ResTy has been 235 /// initialized to 'void' and ArgTys has been initialized with the 236 /// type of 'this' (although this may be changed by the ABI). 237 virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor, 238 CXXDtorType T, 239 CanQualType &ResTy, 240 SmallVectorImpl<CanQualType> &ArgTys) = 0; 241 242 /// Build the ABI-specific portion of the parameter list for a 243 /// function. This generally involves a 'this' parameter and 244 /// possibly some extra data for constructors and destructors. 245 /// 246 /// ABIs may also choose to override the return type, which has been 247 /// initialized with the formal return type of the function. 248 virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF, 249 QualType &ResTy, 250 FunctionArgList &Params) = 0; 251 252 /// Emit the ABI-specific prolog for the function. 253 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0; 254 255 /// Emit the constructor call. Return the function that is called. 256 virtual llvm::Value *EmitConstructorCall(CodeGenFunction &CGF, 257 const CXXConstructorDecl *D, 258 CXXCtorType Type, bool ForVirtualBase, 259 bool Delegating, 260 llvm::Value *This, 261 CallExpr::const_arg_iterator ArgBeg, 262 CallExpr::const_arg_iterator ArgEnd) = 0; 263 264 /// Emit the ABI-specific virtual destructor call. 265 virtual RValue EmitVirtualDestructorCall(CodeGenFunction &CGF, 266 const CXXDestructorDecl *Dtor, 267 CXXDtorType DtorType, 268 SourceLocation CallLoc, 269 ReturnValueSlot ReturnValue, 270 llvm::Value *This) = 0; 271 272 /// Emit any tables needed to implement virtual inheritance. For Itanium, 273 /// this emits virtual table tables. For the MSVC++ ABI, this emits virtual 274 /// base tables. 275 virtual void 276 EmitVirtualInheritanceTables(llvm::GlobalVariable::LinkageTypes Linkage, 277 const CXXRecordDecl *RD) = 0; 278 279 virtual void EmitReturnFromThunk(CodeGenFunction &CGF, 280 RValue RV, QualType ResultType); 281 282 /// Gets the pure virtual member call function. 283 virtual StringRef GetPureVirtualCallName() = 0; 284 285 /// Gets the deleted virtual member call name. 286 virtual StringRef GetDeletedVirtualCallName() = 0; 287 288 /**************************** Array cookies ******************************/ 289 290 /// Returns the extra size required in order to store the array 291 /// cookie for the given new-expression. May return 0 to indicate that no 292 /// array cookie is required. 293 /// 294 /// Several cases are filtered out before this method is called: 295 /// - non-array allocations never need a cookie 296 /// - calls to \::operator new(size_t, void*) never need a cookie 297 /// 298 /// \param expr - the new-expression being allocated. 299 virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr); 300 301 /// Initialize the array cookie for the given allocation. 302 /// 303 /// \param NewPtr - a char* which is the presumed-non-null 304 /// return value of the allocation function 305 /// \param NumElements - the computed number of elements, 306 /// potentially collapsed from the multidimensional array case; 307 /// always a size_t 308 /// \param ElementType - the base element allocated type, 309 /// i.e. the allocated type after stripping all array types 310 virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, 311 llvm::Value *NewPtr, 312 llvm::Value *NumElements, 313 const CXXNewExpr *expr, 314 QualType ElementType); 315 316 /// Reads the array cookie associated with the given pointer, 317 /// if it has one. 318 /// 319 /// \param Ptr - a pointer to the first element in the array 320 /// \param ElementType - the base element type of elements of the array 321 /// \param NumElements - an out parameter which will be initialized 322 /// with the number of elements allocated, or zero if there is no 323 /// cookie 324 /// \param AllocPtr - an out parameter which will be initialized 325 /// with a char* pointing to the address returned by the allocation 326 /// function 327 /// \param CookieSize - an out parameter which will be initialized 328 /// with the size of the cookie, or zero if there is no cookie 329 virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr, 330 const CXXDeleteExpr *expr, 331 QualType ElementType, llvm::Value *&NumElements, 332 llvm::Value *&AllocPtr, CharUnits &CookieSize); 333 334protected: 335 /// Returns the extra size required in order to store the array 336 /// cookie for the given type. Assumes that an array cookie is 337 /// required. 338 virtual CharUnits getArrayCookieSizeImpl(QualType elementType); 339 340 /// Reads the array cookie for an allocation which is known to have one. 341 /// This is called by the standard implementation of ReadArrayCookie. 342 /// 343 /// \param ptr - a pointer to the allocation made for an array, as a char* 344 /// \param cookieSize - the computed cookie size of an array 345 /// 346 /// Other parameters are as above. 347 /// 348 /// \return a size_t 349 virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF, 350 llvm::Value *ptr, 351 CharUnits cookieSize); 352 353public: 354 355 /*************************** Static local guards ****************************/ 356 357 /// Emits the guarded initializer and destructor setup for the given 358 /// variable, given that it couldn't be emitted as a constant. 359 /// If \p PerformInit is false, the initialization has been folded to a 360 /// constant and should not be performed. 361 /// 362 /// The variable may be: 363 /// - a static local variable 364 /// - a static data member of a class template instantiation 365 virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, 366 llvm::GlobalVariable *DeclPtr, bool PerformInit); 367 368 /// Emit code to force the execution of a destructor during global 369 /// teardown. The default implementation of this uses atexit. 370 /// 371 /// \param dtor - a function taking a single pointer argument 372 /// \param addr - a pointer to pass to the destructor function. 373 virtual void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D, 374 llvm::Constant *dtor, llvm::Constant *addr); 375 376 /*************************** thread_local initialization ********************/ 377 378 /// Emits ABI-required functions necessary to initialize thread_local 379 /// variables in this translation unit. 380 /// 381 /// \param Decls The thread_local declarations in this translation unit. 382 /// \param InitFunc If this translation unit contains any non-constant 383 /// initialization or non-trivial destruction for thread_local 384 /// variables, a function to perform the initialization. Otherwise, 0. 385 virtual void EmitThreadLocalInitFuncs( 386 llvm::ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *> > Decls, 387 llvm::Function *InitFunc); 388 389 /// Emit a reference to a non-local thread_local variable (including 390 /// triggering the initialization of all thread_local variables in its 391 /// translation unit). 392 virtual LValue EmitThreadLocalDeclRefExpr(CodeGenFunction &CGF, 393 const DeclRefExpr *DRE); 394}; 395 396// Create an instance of a C++ ABI class: 397 398/// Creates an Itanium-family ABI. 399CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM); 400 401/// Creates a Microsoft-family ABI. 402CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM); 403 404} 405} 406 407#endif 408