clang API Documentation

CodeGenTypes.h
Go to the documentation of this file.
00001 //===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- 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 is the code that handles AST -> LLVM type lowering.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H
00015 #define LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H
00016 
00017 #include "CGCall.h"
00018 #include "clang/AST/GlobalDecl.h"
00019 #include "clang/CodeGen/CGFunctionInfo.h"
00020 #include "llvm/ADT/DenseMap.h"
00021 #include "llvm/IR/Module.h"
00022 #include <vector>
00023 
00024 namespace llvm {
00025 class FunctionType;
00026 class Module;
00027 class DataLayout;
00028 class Type;
00029 class LLVMContext;
00030 class StructType;
00031 }
00032 
00033 namespace clang {
00034 class ABIInfo;
00035 class ASTContext;
00036 template <typename> class CanQual;
00037 class CXXConstructorDecl;
00038 class CXXDestructorDecl;
00039 class CXXMethodDecl;
00040 class CodeGenOptions;
00041 class FieldDecl;
00042 class FunctionProtoType;
00043 class ObjCInterfaceDecl;
00044 class ObjCIvarDecl;
00045 class PointerType;
00046 class QualType;
00047 class RecordDecl;
00048 class TagDecl;
00049 class TargetInfo;
00050 class Type;
00051 typedef CanQual<Type> CanQualType;
00052 
00053 namespace CodeGen {
00054 class CGCXXABI;
00055 class CGRecordLayout;
00056 class CodeGenModule;
00057 class RequiredArgs;
00058 
00059 enum class StructorType {
00060   Complete, // constructor or destructor
00061   Base,     // constructor or destructor
00062   Deleting  // destructor only
00063 };
00064 
00065 inline CXXCtorType toCXXCtorType(StructorType T) {
00066   switch (T) {
00067   case StructorType::Complete:
00068     return Ctor_Complete;
00069   case StructorType::Base:
00070     return Ctor_Base;
00071   case StructorType::Deleting:
00072     llvm_unreachable("cannot have a deleting ctor");
00073   }
00074   llvm_unreachable("not a StructorType");
00075 }
00076 
00077 inline StructorType getFromCtorType(CXXCtorType T) {
00078   switch (T) {
00079   case Ctor_Complete:
00080     return StructorType::Complete;
00081   case Ctor_Base:
00082     return StructorType::Base;
00083   case Ctor_Comdat:
00084     llvm_unreachable("not expecting a COMDAT");
00085   }
00086   llvm_unreachable("not a CXXCtorType");
00087 }
00088 
00089 inline CXXDtorType toCXXDtorType(StructorType T) {
00090   switch (T) {
00091   case StructorType::Complete:
00092     return Dtor_Complete;
00093   case StructorType::Base:
00094     return Dtor_Base;
00095   case StructorType::Deleting:
00096     return Dtor_Deleting;
00097   }
00098   llvm_unreachable("not a StructorType");
00099 }
00100 
00101 inline StructorType getFromDtorType(CXXDtorType T) {
00102   switch (T) {
00103   case Dtor_Deleting:
00104     return StructorType::Deleting;
00105   case Dtor_Complete:
00106     return StructorType::Complete;
00107   case Dtor_Base:
00108     return StructorType::Base;
00109   case Dtor_Comdat:
00110     llvm_unreachable("not expecting a COMDAT");
00111   }
00112   llvm_unreachable("not a CXXDtorType");
00113 }
00114 
00115 /// CodeGenTypes - This class organizes the cross-module state that is used
00116 /// while lowering AST types to LLVM types.
00117 class CodeGenTypes {
00118   CodeGenModule &CGM;
00119   // Some of this stuff should probably be left on the CGM.
00120   ASTContext &Context;
00121   llvm::Module &TheModule;
00122   const llvm::DataLayout &TheDataLayout;
00123   const TargetInfo &Target;
00124   CGCXXABI &TheCXXABI;
00125 
00126   // This should not be moved earlier, since its initialization depends on some
00127   // of the previous reference members being already initialized
00128   const ABIInfo &TheABIInfo;
00129 
00130   /// The opaque type map for Objective-C interfaces. All direct
00131   /// manipulation is done by the runtime interfaces, which are
00132   /// responsible for coercing to the appropriate type; these opaque
00133   /// types are never refined.
00134   llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
00135 
00136   /// CGRecordLayouts - This maps llvm struct type with corresponding
00137   /// record layout info.
00138   llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
00139 
00140   /// RecordDeclTypes - This contains the LLVM IR type for any converted
00141   /// RecordDecl.
00142   llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
00143   
00144   /// FunctionInfos - Hold memoized CGFunctionInfo results.
00145   llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
00146 
00147   /// RecordsBeingLaidOut - This set keeps track of records that we're currently
00148   /// converting to an IR type.  For example, when converting:
00149   /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
00150   /// types will be in this set.
00151   llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut;
00152   
00153   llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
00154   
00155   /// SkippedLayout - True if we didn't layout a function due to a being inside
00156   /// a recursive struct conversion, set this to true.
00157   bool SkippedLayout;
00158 
00159   SmallVector<const RecordDecl *, 8> DeferredRecords;
00160   
00161 private:
00162   /// TypeCache - This map keeps cache of llvm::Types
00163   /// and maps clang::Type to corresponding llvm::Type.
00164   llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
00165 
00166 public:
00167   CodeGenTypes(CodeGenModule &cgm);
00168   ~CodeGenTypes();
00169 
00170   const llvm::DataLayout &getDataLayout() const { return TheDataLayout; }
00171   ASTContext &getContext() const { return Context; }
00172   const ABIInfo &getABIInfo() const { return TheABIInfo; }
00173   const TargetInfo &getTarget() const { return Target; }
00174   CGCXXABI &getCXXABI() const { return TheCXXABI; }
00175   llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
00176 
00177   /// ConvertType - Convert type T into a llvm::Type.
00178   llvm::Type *ConvertType(QualType T);
00179 
00180   /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
00181   /// ConvertType in that it is used to convert to the memory representation for
00182   /// a type.  For example, the scalar representation for _Bool is i1, but the
00183   /// memory representation is usually i8 or i32, depending on the target.
00184   llvm::Type *ConvertTypeForMem(QualType T);
00185 
00186   /// GetFunctionType - Get the LLVM function type for \arg Info.
00187   llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
00188 
00189   llvm::FunctionType *GetFunctionType(GlobalDecl GD);
00190 
00191   /// isFuncTypeConvertible - Utility to check whether a function type can
00192   /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
00193   /// type).
00194   bool isFuncTypeConvertible(const FunctionType *FT);
00195   bool isFuncParamTypeConvertible(QualType Ty);
00196 
00197   /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
00198   /// given a CXXMethodDecl. If the method to has an incomplete return type,
00199   /// and/or incomplete argument types, this will return the opaque type.
00200   llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
00201 
00202   const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
00203 
00204   /// UpdateCompletedType - When we find the full definition for a TagDecl,
00205   /// replace the 'opaque' type we previously made for it if applicable.
00206   void UpdateCompletedType(const TagDecl *TD);
00207 
00208   /// getNullaryFunctionInfo - Get the function info for a void()
00209   /// function with standard CC.
00210   const CGFunctionInfo &arrangeNullaryFunction();
00211 
00212   // The arrangement methods are split into three families:
00213   //   - those meant to drive the signature and prologue/epilogue
00214   //     of a function declaration or definition,
00215   //   - those meant for the computation of the LLVM type for an abstract
00216   //     appearance of a function, and
00217   //   - those meant for performing the IR-generation of a call.
00218   // They differ mainly in how they deal with optional (i.e. variadic)
00219   // arguments, as well as unprototyped functions.
00220   //
00221   // Key points:
00222   // - The CGFunctionInfo for emitting a specific call site must include
00223   //   entries for the optional arguments.
00224   // - The function type used at the call site must reflect the formal
00225   //   signature of the declaration being called, or else the call will
00226   //   go awry.
00227   // - For the most part, unprototyped functions are called by casting to
00228   //   a formal signature inferred from the specific argument types used
00229   //   at the call-site.  However, some targets (e.g. x86-64) screw with
00230   //   this for compatibility reasons.
00231 
00232   const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
00233   const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
00234   const CGFunctionInfo &
00235   arrangeFreeFunctionDeclaration(QualType ResTy, const FunctionArgList &Args,
00236                                  const FunctionType::ExtInfo &Info,
00237                                  bool isVariadic);
00238 
00239   const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
00240   const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
00241                                                         QualType receiverType);
00242 
00243   const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
00244   const CGFunctionInfo &arrangeCXXStructorDeclaration(const CXXMethodDecl *MD,
00245                                                       StructorType Type);
00246   const CGFunctionInfo &arrangeCXXConstructorCall(const CallArgList &Args,
00247                                                   const CXXConstructorDecl *D,
00248                                                   CXXCtorType CtorKind,
00249                                                   unsigned ExtraArgs);
00250   const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
00251                                                 const FunctionType *Ty);
00252   const CGFunctionInfo &arrangeFreeFunctionCall(QualType ResTy,
00253                                                 const CallArgList &args,
00254                                                 FunctionType::ExtInfo info,
00255                                                 RequiredArgs required);
00256   const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
00257                                                  const FunctionType *type);
00258 
00259   const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
00260                                              const FunctionProtoType *type,
00261                                              RequiredArgs required);
00262   const CGFunctionInfo &arrangeMSMemberPointerThunk(const CXXMethodDecl *MD);
00263 
00264   const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty);
00265   const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
00266   const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
00267                                              const FunctionProtoType *FTP);
00268 
00269   /// "Arrange" the LLVM information for a call or type with the given
00270   /// signature.  This is largely an internal method; other clients
00271   /// should use one of the above routines, which ultimately defer to
00272   /// this.
00273   ///
00274   /// \param argTypes - must all actually be canonical as params
00275   const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
00276                                                 bool IsInstanceMethod,
00277                                                 ArrayRef<CanQualType> argTypes,
00278                                                 FunctionType::ExtInfo info,
00279                                                 RequiredArgs args);
00280 
00281   /// \brief Compute a new LLVM record layout object for the given record.
00282   CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
00283                                       llvm::StructType *Ty);
00284 
00285   /// addRecordTypeName - Compute a name from the given record decl with an
00286   /// optional suffix and name the given LLVM type using it.
00287   void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
00288                          StringRef suffix);
00289   
00290 
00291 public:  // These are internal details of CGT that shouldn't be used externally.
00292   /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
00293   llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
00294 
00295   /// getExpandedTypes - Expand the type \arg Ty into the LLVM
00296   /// argument types it would be passed as. See ABIArgInfo::Expand.
00297   void getExpandedTypes(QualType Ty,
00298                         SmallVectorImpl<llvm::Type *>::iterator &TI);
00299 
00300   /// IsZeroInitializable - Return whether a type can be
00301   /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
00302   bool isZeroInitializable(QualType T);
00303 
00304   /// IsZeroInitializable - Return whether a record type can be
00305   /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
00306   bool isZeroInitializable(const CXXRecordDecl *RD);
00307   
00308   bool isRecordLayoutComplete(const Type *Ty) const;
00309   bool noRecordsBeingLaidOut() const {
00310     return RecordsBeingLaidOut.empty();
00311   }
00312   bool isRecordBeingLaidOut(const Type *Ty) const {
00313     return RecordsBeingLaidOut.count(Ty);
00314   }
00315                             
00316 };
00317 
00318 }  // end namespace CodeGen
00319 }  // end namespace clang
00320 
00321 #endif