clang API Documentation

CGCXXABI.h
Go to the documentation of this file.
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 CLANG_CODEGEN_CXXABI_H
00016 #define CLANG_CODEGEN_CXXABI_H
00017 
00018 #include "clang/Basic/LLVM.h"
00019 
00020 #include "CodeGenFunction.h"
00021 
00022 namespace llvm {
00023   class Constant;
00024   class Type;
00025   class Value;
00026 }
00027 
00028 namespace clang {
00029   class CastExpr;
00030   class CXXConstructorDecl;
00031   class CXXDestructorDecl;
00032   class CXXMethodDecl;
00033   class CXXRecordDecl;
00034   class FieldDecl;
00035   class MangleContext;
00036 
00037 namespace CodeGen {
00038   class CodeGenFunction;
00039   class CodeGenModule;
00040 
00041 /// Implements C++ ABI-specific code generation functions.
00042 class CGCXXABI {
00043 protected:
00044   CodeGenModule &CGM;
00045   OwningPtr<MangleContext> MangleCtx;
00046 
00047   CGCXXABI(CodeGenModule &CGM)
00048     : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {}
00049 
00050 protected:
00051   ImplicitParamDecl *&getThisDecl(CodeGenFunction &CGF) {
00052     return CGF.CXXABIThisDecl;
00053   }
00054   llvm::Value *&getThisValue(CodeGenFunction &CGF) {
00055     return CGF.CXXABIThisValue;
00056   }
00057 
00058   ImplicitParamDecl *&getVTTDecl(CodeGenFunction &CGF) {
00059     return CGF.CXXVTTDecl;
00060   }
00061   llvm::Value *&getVTTValue(CodeGenFunction &CGF) {
00062     return CGF.CXXVTTValue;
00063   }
00064 
00065   /// Build a parameter variable suitable for 'this'.
00066   void BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params);
00067 
00068   /// Perform prolog initialization of the parameter variable suitable
00069   /// for 'this' emitted by BuildThisParam.
00070   void EmitThisParam(CodeGenFunction &CGF);
00071 
00072   ASTContext &getContext() const { return CGM.getContext(); }
00073 
00074   virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType);
00075   virtual bool requiresArrayCookie(const CXXNewExpr *E);
00076 
00077 public:
00078 
00079   virtual ~CGCXXABI();
00080 
00081   /// Gets the mangle context.
00082   MangleContext &getMangleContext() {
00083     return *MangleCtx;
00084   }
00085 
00086   /// Find the LLVM type used to represent the given member pointer
00087   /// type.
00088   virtual llvm::Type *
00089   ConvertMemberPointerType(const MemberPointerType *MPT);
00090 
00091   /// Load a member function from an object and a member function
00092   /// pointer.  Apply the this-adjustment and set 'This' to the
00093   /// adjusted value.
00094   virtual llvm::Value *
00095   EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
00096                                   llvm::Value *&This,
00097                                   llvm::Value *MemPtr,
00098                                   const MemberPointerType *MPT);
00099 
00100   /// Calculate an l-value from an object and a data member pointer.
00101   virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
00102                                                     llvm::Value *Base,
00103                                                     llvm::Value *MemPtr,
00104                                             const MemberPointerType *MPT);
00105 
00106   /// Perform a derived-to-base, base-to-derived, or bitcast member
00107   /// pointer conversion.
00108   virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
00109                                                    const CastExpr *E,
00110                                                    llvm::Value *Src);
00111 
00112   /// Perform a derived-to-base, base-to-derived, or bitcast member
00113   /// pointer conversion on a constant value.
00114   virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
00115                                                       llvm::Constant *Src);
00116 
00117   /// Return true if the given member pointer can be zero-initialized
00118   /// (in the C++ sense) with an LLVM zeroinitializer.
00119   virtual bool isZeroInitializable(const MemberPointerType *MPT);
00120 
00121   /// Create a null member pointer of the given type.
00122   virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
00123 
00124   /// Create a member pointer for the given method.
00125   virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
00126 
00127   /// Create a member pointer for the given field.
00128   virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
00129                                                 CharUnits offset);
00130 
00131   /// Create a member pointer for the given member pointer constant.
00132   virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
00133 
00134   /// Emit a comparison between two member pointers.  Returns an i1.
00135   virtual llvm::Value *
00136   EmitMemberPointerComparison(CodeGenFunction &CGF,
00137                               llvm::Value *L,
00138                               llvm::Value *R,
00139                               const MemberPointerType *MPT,
00140                               bool Inequality);
00141 
00142   /// Determine if a member pointer is non-null.  Returns an i1.
00143   virtual llvm::Value *
00144   EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
00145                              llvm::Value *MemPtr,
00146                              const MemberPointerType *MPT);
00147 
00148 protected:
00149   /// A utility method for computing the offset required for the given
00150   /// base-to-derived or derived-to-base member-pointer conversion.
00151   /// Does not handle virtual conversions (in case we ever fully
00152   /// support an ABI that allows this).  Returns null if no adjustment
00153   /// is required.
00154   llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
00155 
00156 public:
00157   /// Build the signature of the given constructor variant by adding
00158   /// any required parameters.  For convenience, ResTy has been
00159   /// initialized to 'void', and ArgTys has been initialized with the
00160   /// type of 'this' (although this may be changed by the ABI) and
00161   /// will have the formal parameters added to it afterwards.
00162   ///
00163   /// If there are ever any ABIs where the implicit parameters are
00164   /// intermixed with the formal parameters, we can address those
00165   /// then.
00166   virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
00167                                          CXXCtorType T,
00168                                          CanQualType &ResTy,
00169                                SmallVectorImpl<CanQualType> &ArgTys) = 0;
00170 
00171   /// Build the signature of the given destructor variant by adding
00172   /// any required parameters.  For convenience, ResTy has been
00173   /// initialized to 'void' and ArgTys has been initialized with the
00174   /// type of 'this' (although this may be changed by the ABI).
00175   virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
00176                                         CXXDtorType T,
00177                                         CanQualType &ResTy,
00178                                SmallVectorImpl<CanQualType> &ArgTys) = 0;
00179 
00180   /// Build the ABI-specific portion of the parameter list for a
00181   /// function.  This generally involves a 'this' parameter and
00182   /// possibly some extra data for constructors and destructors.
00183   ///
00184   /// ABIs may also choose to override the return type, which has been
00185   /// initialized with the formal return type of the function.
00186   virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
00187                                            QualType &ResTy,
00188                                            FunctionArgList &Params) = 0;
00189 
00190   /// Emit the ABI-specific prolog for the function.
00191   virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
00192 
00193   virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
00194                                    RValue RV, QualType ResultType);
00195 
00196   /**************************** Array cookies ******************************/
00197 
00198   /// Returns the extra size required in order to store the array
00199   /// cookie for the given type.  May return 0 to indicate that no
00200   /// array cookie is required.
00201   ///
00202   /// Several cases are filtered out before this method is called:
00203   ///   - non-array allocations never need a cookie
00204   ///   - calls to ::operator new(size_t, void*) never need a cookie
00205   ///
00206   /// \param ElementType - the allocated type of the expression,
00207   ///   i.e. the pointee type of the expression result type
00208   virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
00209 
00210   /// Initialize the array cookie for the given allocation.
00211   ///
00212   /// \param NewPtr - a char* which is the presumed-non-null
00213   ///   return value of the allocation function
00214   /// \param NumElements - the computed number of elements,
00215   ///   potentially collapsed from the multidimensional array case;
00216   ///   always a size_t
00217   /// \param ElementType - the base element allocated type,
00218   ///   i.e. the allocated type after stripping all array types
00219   virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
00220                                              llvm::Value *NewPtr,
00221                                              llvm::Value *NumElements,
00222                                              const CXXNewExpr *expr,
00223                                              QualType ElementType);
00224 
00225   /// Reads the array cookie associated with the given pointer,
00226   /// if it has one.
00227   ///
00228   /// \param Ptr - a pointer to the first element in the array
00229   /// \param ElementType - the base element type of elements of the array
00230   /// \param NumElements - an out parameter which will be initialized
00231   ///   with the number of elements allocated, or zero if there is no
00232   ///   cookie
00233   /// \param AllocPtr - an out parameter which will be initialized
00234   ///   with a char* pointing to the address returned by the allocation
00235   ///   function
00236   /// \param CookieSize - an out parameter which will be initialized
00237   ///   with the size of the cookie, or zero if there is no cookie
00238   virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
00239                                const CXXDeleteExpr *expr,
00240                                QualType ElementType, llvm::Value *&NumElements,
00241                                llvm::Value *&AllocPtr, CharUnits &CookieSize);
00242 
00243 protected:
00244   /// Returns the extra size required in order to store the array
00245   /// cookie for the given type.  Assumes that an array cookie is
00246   /// required.
00247   virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
00248 
00249   /// Reads the array cookie for an allocation which is known to have one.
00250   /// This is called by the standard implementation of ReadArrayCookie.
00251   ///
00252   /// \param ptr - a pointer to the allocation made for an array, as a char*
00253   /// \param cookieSize - the computed cookie size of an array
00254   /// Other parameters are as above.
00255   /// \return a size_t
00256   virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF,
00257                                            llvm::Value *ptr,
00258                                            CharUnits cookieSize);
00259 
00260 public:
00261 
00262   /*************************** Static local guards ****************************/
00263 
00264   /// Emits the guarded initializer and destructor setup for the given
00265   /// variable, given that it couldn't be emitted as a constant.
00266   /// If \p PerformInit is false, the initialization has been folded to a
00267   /// constant and should not be performed.
00268   ///
00269   /// The variable may be:
00270   ///   - a static local variable
00271   ///   - a static data member of a class template instantiation
00272   virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
00273                                llvm::GlobalVariable *DeclPtr, bool PerformInit);
00274 
00275   /// Emit code to force the execution of a destructor during global
00276   /// teardown.  The default implementation of this uses atexit.
00277   ///
00278   /// \param dtor - a function taking a single pointer argument
00279   /// \param addr - a pointer to pass to the destructor function.
00280   virtual void registerGlobalDtor(CodeGenFunction &CGF, llvm::Constant *dtor,
00281                                   llvm::Constant *addr);
00282 };
00283 
00284 /// Creates an instance of a C++ ABI class.
00285 CGCXXABI *CreateARMCXXABI(CodeGenModule &CGM);
00286 CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
00287 CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);
00288 
00289 }
00290 }
00291 
00292 #endif