CGCXXABI.h revision 234353
1208600Srdivacky//===----- CGCXXABI.h - Interface to C++ ABIs -------------------*- C++ -*-===// 2208600Srdivacky// 3208600Srdivacky// The LLVM Compiler Infrastructure 4208600Srdivacky// 5208600Srdivacky// This file is distributed under the University of Illinois Open Source 6208600Srdivacky// License. See LICENSE.TXT for details. 7208600Srdivacky// 8208600Srdivacky//===----------------------------------------------------------------------===// 9208600Srdivacky// 10208600Srdivacky// This provides an abstract class for C++ code generation. Concrete subclasses 11208600Srdivacky// of this implement code generation for specific C++ ABIs. 12208600Srdivacky// 13208600Srdivacky//===----------------------------------------------------------------------===// 14208600Srdivacky 15208600Srdivacky#ifndef CLANG_CODEGEN_CXXABI_H 16208600Srdivacky#define CLANG_CODEGEN_CXXABI_H 17208600Srdivacky 18226633Sdim#include "clang/Basic/LLVM.h" 19226633Sdim 20212904Sdim#include "CodeGenFunction.h" 21212904Sdim 22212904Sdimnamespace llvm { 23212904Sdim class Constant; 24212904Sdim class Type; 25212904Sdim class Value; 26212904Sdim} 27212904Sdim 28208600Srdivackynamespace clang { 29212904Sdim class CastExpr; 30212904Sdim class CXXConstructorDecl; 31212904Sdim class CXXDestructorDecl; 32212904Sdim class CXXMethodDecl; 33212904Sdim class CXXRecordDecl; 34212904Sdim class FieldDecl; 35218893Sdim class MangleContext; 36212904Sdim 37208600Srdivackynamespace CodeGen { 38212904Sdim class CodeGenFunction; 39208600Srdivacky class CodeGenModule; 40208600Srdivacky 41208600Srdivacky/// Implements C++ ABI-specific code generation functions. 42212904Sdimclass CGCXXABI { 43212904Sdimprotected: 44212904Sdim CodeGenModule &CGM; 45234353Sdim OwningPtr<MangleContext> MangleCtx; 46212904Sdim 47218893Sdim CGCXXABI(CodeGenModule &CGM) 48218893Sdim : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {} 49212904Sdim 50212904Sdimprotected: 51212904Sdim ImplicitParamDecl *&getThisDecl(CodeGenFunction &CGF) { 52234353Sdim return CGF.CXXABIThisDecl; 53212904Sdim } 54212904Sdim llvm::Value *&getThisValue(CodeGenFunction &CGF) { 55234353Sdim return CGF.CXXABIThisValue; 56212904Sdim } 57212904Sdim 58212904Sdim ImplicitParamDecl *&getVTTDecl(CodeGenFunction &CGF) { 59212904Sdim return CGF.CXXVTTDecl; 60212904Sdim } 61212904Sdim llvm::Value *&getVTTValue(CodeGenFunction &CGF) { 62212904Sdim return CGF.CXXVTTValue; 63212904Sdim } 64212904Sdim 65212904Sdim /// Build a parameter variable suitable for 'this'. 66212904Sdim void BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params); 67212904Sdim 68212904Sdim /// Perform prolog initialization of the parameter variable suitable 69212904Sdim /// for 'this' emitted by BuildThisParam. 70212904Sdim void EmitThisParam(CodeGenFunction &CGF); 71212904Sdim 72212904Sdim ASTContext &getContext() const { return CGM.getContext(); } 73212904Sdim 74208600Srdivackypublic: 75208600Srdivacky 76212904Sdim virtual ~CGCXXABI(); 77212904Sdim 78208600Srdivacky /// Gets the mangle context. 79218893Sdim MangleContext &getMangleContext() { 80218893Sdim return *MangleCtx; 81218893Sdim } 82212904Sdim 83212904Sdim /// Find the LLVM type used to represent the given member pointer 84212904Sdim /// type. 85224145Sdim virtual llvm::Type * 86212904Sdim ConvertMemberPointerType(const MemberPointerType *MPT); 87212904Sdim 88212904Sdim /// Load a member function from an object and a member function 89212904Sdim /// pointer. Apply the this-adjustment and set 'This' to the 90212904Sdim /// adjusted value. 91212904Sdim virtual llvm::Value * 92212904Sdim EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, 93212904Sdim llvm::Value *&This, 94212904Sdim llvm::Value *MemPtr, 95212904Sdim const MemberPointerType *MPT); 96212904Sdim 97212904Sdim /// Calculate an l-value from an object and a data member pointer. 98212904Sdim virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF, 99212904Sdim llvm::Value *Base, 100212904Sdim llvm::Value *MemPtr, 101212904Sdim const MemberPointerType *MPT); 102212904Sdim 103234353Sdim /// Perform a derived-to-base, base-to-derived, or bitcast member 104234353Sdim /// pointer conversion. 105212904Sdim virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, 106212904Sdim const CastExpr *E, 107212904Sdim llvm::Value *Src); 108212904Sdim 109234353Sdim /// Perform a derived-to-base, base-to-derived, or bitcast member 110234353Sdim /// pointer conversion on a constant value. 111234353Sdim virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E, 112234353Sdim llvm::Constant *Src); 113212904Sdim 114212904Sdim /// Return true if the given member pointer can be zero-initialized 115212904Sdim /// (in the C++ sense) with an LLVM zeroinitializer. 116212904Sdim virtual bool isZeroInitializable(const MemberPointerType *MPT); 117212904Sdim 118212904Sdim /// Create a null member pointer of the given type. 119212904Sdim virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT); 120212904Sdim 121212904Sdim /// Create a member pointer for the given method. 122212904Sdim virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD); 123212904Sdim 124212904Sdim /// Create a member pointer for the given field. 125218893Sdim virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT, 126218893Sdim CharUnits offset); 127212904Sdim 128234353Sdim /// Create a member pointer for the given member pointer constant. 129234353Sdim virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT); 130234353Sdim 131212904Sdim /// Emit a comparison between two member pointers. Returns an i1. 132212904Sdim virtual llvm::Value * 133212904Sdim EmitMemberPointerComparison(CodeGenFunction &CGF, 134212904Sdim llvm::Value *L, 135212904Sdim llvm::Value *R, 136212904Sdim const MemberPointerType *MPT, 137212904Sdim bool Inequality); 138212904Sdim 139212904Sdim /// Determine if a member pointer is non-null. Returns an i1. 140212904Sdim virtual llvm::Value * 141212904Sdim EmitMemberPointerIsNotNull(CodeGenFunction &CGF, 142212904Sdim llvm::Value *MemPtr, 143212904Sdim const MemberPointerType *MPT); 144212904Sdim 145234353Sdimprotected: 146234353Sdim /// A utility method for computing the offset required for the given 147234353Sdim /// base-to-derived or derived-to-base member-pointer conversion. 148234353Sdim /// Does not handle virtual conversions (in case we ever fully 149234353Sdim /// support an ABI that allows this). Returns null if no adjustment 150234353Sdim /// is required. 151234353Sdim llvm::Constant *getMemberPointerAdjustment(const CastExpr *E); 152234353Sdim 153234353Sdimpublic: 154212904Sdim /// Build the signature of the given constructor variant by adding 155212904Sdim /// any required parameters. For convenience, ResTy has been 156212904Sdim /// initialized to 'void', and ArgTys has been initialized with the 157212904Sdim /// type of 'this' (although this may be changed by the ABI) and 158212904Sdim /// will have the formal parameters added to it afterwards. 159212904Sdim /// 160212904Sdim /// If there are ever any ABIs where the implicit parameters are 161212904Sdim /// intermixed with the formal parameters, we can address those 162212904Sdim /// then. 163212904Sdim virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor, 164212904Sdim CXXCtorType T, 165212904Sdim CanQualType &ResTy, 166226633Sdim SmallVectorImpl<CanQualType> &ArgTys) = 0; 167212904Sdim 168212904Sdim /// Build the signature of the given destructor variant by adding 169212904Sdim /// any required parameters. For convenience, ResTy has been 170212904Sdim /// initialized to 'void' and ArgTys has been initialized with the 171212904Sdim /// type of 'this' (although this may be changed by the ABI). 172212904Sdim virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor, 173212904Sdim CXXDtorType T, 174212904Sdim CanQualType &ResTy, 175226633Sdim SmallVectorImpl<CanQualType> &ArgTys) = 0; 176212904Sdim 177212904Sdim /// Build the ABI-specific portion of the parameter list for a 178212904Sdim /// function. This generally involves a 'this' parameter and 179212904Sdim /// possibly some extra data for constructors and destructors. 180212904Sdim /// 181212904Sdim /// ABIs may also choose to override the return type, which has been 182212904Sdim /// initialized with the formal return type of the function. 183212904Sdim virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF, 184212904Sdim QualType &ResTy, 185212904Sdim FunctionArgList &Params) = 0; 186212904Sdim 187212904Sdim /// Emit the ABI-specific prolog for the function. 188212904Sdim virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0; 189212904Sdim 190212904Sdim virtual void EmitReturnFromThunk(CodeGenFunction &CGF, 191212904Sdim RValue RV, QualType ResultType); 192212904Sdim 193212904Sdim /**************************** Array cookies ******************************/ 194212904Sdim 195212904Sdim /// Returns the extra size required in order to store the array 196212904Sdim /// cookie for the given type. May return 0 to indicate that no 197212904Sdim /// array cookie is required. 198212904Sdim /// 199212904Sdim /// Several cases are filtered out before this method is called: 200212904Sdim /// - non-array allocations never need a cookie 201212904Sdim /// - calls to ::operator new(size_t, void*) never need a cookie 202212904Sdim /// 203212904Sdim /// \param ElementType - the allocated type of the expression, 204212904Sdim /// i.e. the pointee type of the expression result type 205218893Sdim virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr); 206212904Sdim 207212904Sdim /// Initialize the array cookie for the given allocation. 208212904Sdim /// 209212904Sdim /// \param NewPtr - a char* which is the presumed-non-null 210212904Sdim /// return value of the allocation function 211212904Sdim /// \param NumElements - the computed number of elements, 212212904Sdim /// potentially collapsed from the multidimensional array case 213212904Sdim /// \param ElementType - the base element allocated type, 214212904Sdim /// i.e. the allocated type after stripping all array types 215212904Sdim virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, 216212904Sdim llvm::Value *NewPtr, 217212904Sdim llvm::Value *NumElements, 218218893Sdim const CXXNewExpr *expr, 219212904Sdim QualType ElementType); 220212904Sdim 221212904Sdim /// Reads the array cookie associated with the given pointer, 222212904Sdim /// if it has one. 223212904Sdim /// 224212904Sdim /// \param Ptr - a pointer to the first element in the array 225212904Sdim /// \param ElementType - the base element type of elements of the array 226212904Sdim /// \param NumElements - an out parameter which will be initialized 227212904Sdim /// with the number of elements allocated, or zero if there is no 228212904Sdim /// cookie 229212904Sdim /// \param AllocPtr - an out parameter which will be initialized 230212904Sdim /// with a char* pointing to the address returned by the allocation 231212904Sdim /// function 232212904Sdim /// \param CookieSize - an out parameter which will be initialized 233212904Sdim /// with the size of the cookie, or zero if there is no cookie 234212904Sdim virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr, 235218893Sdim const CXXDeleteExpr *expr, 236212904Sdim QualType ElementType, llvm::Value *&NumElements, 237212904Sdim llvm::Value *&AllocPtr, CharUnits &CookieSize); 238212904Sdim 239218893Sdim /*************************** Static local guards ****************************/ 240218893Sdim 241218893Sdim /// Emits the guarded initializer and destructor setup for the given 242218893Sdim /// variable, given that it couldn't be emitted as a constant. 243234353Sdim /// If \p PerformInit is false, the initialization has been folded to a 244234353Sdim /// constant and should not be performed. 245218893Sdim /// 246218893Sdim /// The variable may be: 247218893Sdim /// - a static local variable 248218893Sdim /// - a static data member of a class template instantiation 249218893Sdim virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, 250234353Sdim llvm::GlobalVariable *DeclPtr, bool PerformInit); 251218893Sdim 252208600Srdivacky}; 253208600Srdivacky 254208600Srdivacky/// Creates an instance of a C++ ABI class. 255212904SdimCGCXXABI *CreateARMCXXABI(CodeGenModule &CGM); 256212904SdimCGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM); 257212904SdimCGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM); 258212904Sdim 259208600Srdivacky} 260208600Srdivacky} 261208600Srdivacky 262208600Srdivacky#endif 263