CGCXXABI.h revision 243830
1254721Semaste//===----- CGCXXABI.h - Interface to C++ ABIs -------------------*- C++ -*-===// 2254721Semaste// 3254721Semaste// The LLVM Compiler Infrastructure 4254721Semaste// 5254721Semaste// This file is distributed under the University of Illinois Open Source 6254721Semaste// License. See LICENSE.TXT for details. 7254721Semaste// 8254721Semaste//===----------------------------------------------------------------------===// 9254721Semaste// 10254721Semaste// This provides an abstract class for C++ code generation. Concrete subclasses 11254721Semaste// of this implement code generation for specific C++ ABIs. 12254721Semaste// 13254721Semaste//===----------------------------------------------------------------------===// 14254721Semaste 15254721Semaste#ifndef CLANG_CODEGEN_CXXABI_H 16254721Semaste#define CLANG_CODEGEN_CXXABI_H 17254721Semaste 18254721Semaste#include "clang/Basic/LLVM.h" 19254721Semaste 20254721Semaste#include "CodeGenFunction.h" 21254721Semaste 22254721Semastenamespace llvm { 23254721Semaste class Constant; 24254721Semaste class Type; 25254721Semaste class Value; 26254721Semaste} 27254721Semaste 28254721Semastenamespace clang { 29254721Semaste class CastExpr; 30254721Semaste class CXXConstructorDecl; 31254721Semaste class CXXDestructorDecl; 32254721Semaste class CXXMethodDecl; 33254721Semaste class CXXRecordDecl; 34254721Semaste class FieldDecl; 35254721Semaste class MangleContext; 36254721Semaste 37254721Semastenamespace CodeGen { 38254721Semaste class CodeGenFunction; 39254721Semaste class CodeGenModule; 40254721Semaste 41254721Semaste/// \brief Implements C++ ABI-specific code generation functions. 42254721Semasteclass CGCXXABI { 43254721Semasteprotected: 44254721Semaste CodeGenModule &CGM; 45254721Semaste OwningPtr<MangleContext> MangleCtx; 46254721Semaste 47254721Semaste CGCXXABI(CodeGenModule &CGM) 48254721Semaste : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {} 49254721Semaste 50254721Semasteprotected: 51254721Semaste ImplicitParamDecl *&getThisDecl(CodeGenFunction &CGF) { 52254721Semaste return CGF.CXXABIThisDecl; 53254721Semaste } 54254721Semaste llvm::Value *&getThisValue(CodeGenFunction &CGF) { 55254721Semaste return CGF.CXXABIThisValue; 56254721Semaste } 57254721Semaste 58254721Semaste ImplicitParamDecl *&getVTTDecl(CodeGenFunction &CGF) { 59254721Semaste return CGF.CXXVTTDecl; 60254721Semaste } 61254721Semaste llvm::Value *&getVTTValue(CodeGenFunction &CGF) { 62254721Semaste return CGF.CXXVTTValue; 63254721Semaste } 64254721Semaste 65254721Semaste /// Build a parameter variable suitable for 'this'. 66254721Semaste void BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params); 67254721Semaste 68254721Semaste /// Perform prolog initialization of the parameter variable suitable 69254721Semaste /// for 'this' emitted by BuildThisParam. 70254721Semaste void EmitThisParam(CodeGenFunction &CGF); 71254721Semaste 72254721Semaste ASTContext &getContext() const { return CGM.getContext(); } 73254721Semaste 74254721Semaste virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType); 75254721Semaste virtual bool requiresArrayCookie(const CXXNewExpr *E); 76254721Semaste 77254721Semastepublic: 78254721Semaste 79254721Semaste virtual ~CGCXXABI(); 80254721Semaste 81254721Semaste /// Gets the mangle context. 82254721Semaste MangleContext &getMangleContext() { 83254721Semaste return *MangleCtx; 84254721Semaste } 85254721Semaste 86254721Semaste /// Find the LLVM type used to represent the given member pointer 87254721Semaste /// type. 88254721Semaste virtual llvm::Type * 89254721Semaste ConvertMemberPointerType(const MemberPointerType *MPT); 90254721Semaste 91254721Semaste /// Load a member function from an object and a member function 92254721Semaste /// pointer. Apply the this-adjustment and set 'This' to the 93254721Semaste /// adjusted value. 94254721Semaste virtual llvm::Value * 95254721Semaste EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, 96269024Semaste llvm::Value *&This, 97254721Semaste llvm::Value *MemPtr, 98254721Semaste const MemberPointerType *MPT); 99254721Semaste 100254721Semaste /// Calculate an l-value from an object and a data member pointer. 101254721Semaste virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF, 102254721Semaste llvm::Value *Base, 103254721Semaste llvm::Value *MemPtr, 104254721Semaste const MemberPointerType *MPT); 105254721Semaste 106254721Semaste /// Perform a derived-to-base, base-to-derived, or bitcast member 107254721Semaste /// pointer conversion. 108254721Semaste virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, 109254721Semaste const CastExpr *E, 110254721Semaste llvm::Value *Src); 111 112 /// Perform a derived-to-base, base-to-derived, or bitcast member 113 /// pointer conversion on a constant value. 114 virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E, 115 llvm::Constant *Src); 116 117 /// Return true if the given member pointer can be zero-initialized 118 /// (in the C++ sense) with an LLVM zeroinitializer. 119 virtual bool isZeroInitializable(const MemberPointerType *MPT); 120 121 /// Create a null member pointer of the given type. 122 virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT); 123 124 /// Create a member pointer for the given method. 125 virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD); 126 127 /// Create a member pointer for the given field. 128 virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT, 129 CharUnits offset); 130 131 /// Create a member pointer for the given member pointer constant. 132 virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT); 133 134 /// Emit a comparison between two member pointers. Returns an i1. 135 virtual llvm::Value * 136 EmitMemberPointerComparison(CodeGenFunction &CGF, 137 llvm::Value *L, 138 llvm::Value *R, 139 const MemberPointerType *MPT, 140 bool Inequality); 141 142 /// Determine if a member pointer is non-null. Returns an i1. 143 virtual llvm::Value * 144 EmitMemberPointerIsNotNull(CodeGenFunction &CGF, 145 llvm::Value *MemPtr, 146 const MemberPointerType *MPT); 147 148protected: 149 /// A utility method for computing the offset required for the given 150 /// base-to-derived or derived-to-base member-pointer conversion. 151 /// Does not handle virtual conversions (in case we ever fully 152 /// support an ABI that allows this). Returns null if no adjustment 153 /// is required. 154 llvm::Constant *getMemberPointerAdjustment(const CastExpr *E); 155 156public: 157 /// Adjust the given non-null pointer to an object of polymorphic 158 /// type to point to the complete object. 159 /// 160 /// The IR type of the result should be a pointer but is otherwise 161 /// irrelevant. 162 virtual llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF, 163 llvm::Value *ptr, 164 QualType type) = 0; 165 166 /// Build the signature of the given constructor variant by adding 167 /// any required parameters. For convenience, ResTy has been 168 /// initialized to 'void', and ArgTys has been initialized with the 169 /// type of 'this' (although this may be changed by the ABI) and 170 /// will have the formal parameters added to it afterwards. 171 /// 172 /// If there are ever any ABIs where the implicit parameters are 173 /// intermixed with the formal parameters, we can address those 174 /// then. 175 virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor, 176 CXXCtorType T, 177 CanQualType &ResTy, 178 SmallVectorImpl<CanQualType> &ArgTys) = 0; 179 180 /// Build the signature of the given destructor variant by adding 181 /// any required parameters. For convenience, ResTy has been 182 /// initialized to 'void' and ArgTys has been initialized with the 183 /// type of 'this' (although this may be changed by the ABI). 184 virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor, 185 CXXDtorType T, 186 CanQualType &ResTy, 187 SmallVectorImpl<CanQualType> &ArgTys) = 0; 188 189 /// Build the ABI-specific portion of the parameter list for a 190 /// function. This generally involves a 'this' parameter and 191 /// possibly some extra data for constructors and destructors. 192 /// 193 /// ABIs may also choose to override the return type, which has been 194 /// initialized with the formal return type of the function. 195 virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF, 196 QualType &ResTy, 197 FunctionArgList &Params) = 0; 198 199 /// Emit the ABI-specific prolog for the function. 200 virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0; 201 202 virtual void EmitReturnFromThunk(CodeGenFunction &CGF, 203 RValue RV, QualType ResultType); 204 205 /// Gets the pure virtual member call function. 206 virtual StringRef GetPureVirtualCallName() = 0; 207 208 /// Gets the deleted virtual member call name. 209 virtual StringRef GetDeletedVirtualCallName() = 0; 210 211 /**************************** Array cookies ******************************/ 212 213 /// Returns the extra size required in order to store the array 214 /// cookie for the given new-expression. May return 0 to indicate that no 215 /// array cookie is required. 216 /// 217 /// Several cases are filtered out before this method is called: 218 /// - non-array allocations never need a cookie 219 /// - calls to \::operator new(size_t, void*) never need a cookie 220 /// 221 /// \param expr - the new-expression being allocated. 222 virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr); 223 224 /// Initialize the array cookie for the given allocation. 225 /// 226 /// \param NewPtr - a char* which is the presumed-non-null 227 /// return value of the allocation function 228 /// \param NumElements - the computed number of elements, 229 /// potentially collapsed from the multidimensional array case; 230 /// always a size_t 231 /// \param ElementType - the base element allocated type, 232 /// i.e. the allocated type after stripping all array types 233 virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, 234 llvm::Value *NewPtr, 235 llvm::Value *NumElements, 236 const CXXNewExpr *expr, 237 QualType ElementType); 238 239 /// Reads the array cookie associated with the given pointer, 240 /// if it has one. 241 /// 242 /// \param Ptr - a pointer to the first element in the array 243 /// \param ElementType - the base element type of elements of the array 244 /// \param NumElements - an out parameter which will be initialized 245 /// with the number of elements allocated, or zero if there is no 246 /// cookie 247 /// \param AllocPtr - an out parameter which will be initialized 248 /// with a char* pointing to the address returned by the allocation 249 /// function 250 /// \param CookieSize - an out parameter which will be initialized 251 /// with the size of the cookie, or zero if there is no cookie 252 virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr, 253 const CXXDeleteExpr *expr, 254 QualType ElementType, llvm::Value *&NumElements, 255 llvm::Value *&AllocPtr, CharUnits &CookieSize); 256 257protected: 258 /// Returns the extra size required in order to store the array 259 /// cookie for the given type. Assumes that an array cookie is 260 /// required. 261 virtual CharUnits getArrayCookieSizeImpl(QualType elementType); 262 263 /// Reads the array cookie for an allocation which is known to have one. 264 /// This is called by the standard implementation of ReadArrayCookie. 265 /// 266 /// \param ptr - a pointer to the allocation made for an array, as a char* 267 /// \param cookieSize - the computed cookie size of an array 268 /// 269 /// Other parameters are as above. 270 /// 271 /// \return a size_t 272 virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF, 273 llvm::Value *ptr, 274 CharUnits cookieSize); 275 276public: 277 278 /*************************** Static local guards ****************************/ 279 280 /// Emits the guarded initializer and destructor setup for the given 281 /// variable, given that it couldn't be emitted as a constant. 282 /// If \p PerformInit is false, the initialization has been folded to a 283 /// constant and should not be performed. 284 /// 285 /// The variable may be: 286 /// - a static local variable 287 /// - a static data member of a class template instantiation 288 virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, 289 llvm::GlobalVariable *DeclPtr, bool PerformInit); 290 291 /// Emit code to force the execution of a destructor during global 292 /// teardown. The default implementation of this uses atexit. 293 /// 294 /// \param dtor - a function taking a single pointer argument 295 /// \param addr - a pointer to pass to the destructor function. 296 virtual void registerGlobalDtor(CodeGenFunction &CGF, llvm::Constant *dtor, 297 llvm::Constant *addr); 298 299 /***************************** Virtual Tables *******************************/ 300 301 /// Generates and emits the virtual tables for a class. 302 virtual void EmitVTables(const CXXRecordDecl *Class) = 0; 303}; 304 305/// Creates an instance of a C++ ABI class. 306CGCXXABI *CreateARMCXXABI(CodeGenModule &CGM); 307CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM); 308CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM); 309 310} 311} 312 313#endif 314