Home | History | Annotate | Download | only in CodeGen
      1 //===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- 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 is the code that handles AST -> LLVM type lowering.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H
     15 #define LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H
     16 
     17 #include "CGCall.h"
     18 #include "clang/AST/GlobalDecl.h"
     19 #include "clang/CodeGen/CGFunctionInfo.h"
     20 #include "llvm/ADT/DenseMap.h"
     21 #include "llvm/IR/Module.h"
     22 #include <vector>
     23 
     24 namespace llvm {
     25 class FunctionType;
     26 class Module;
     27 class DataLayout;
     28 class Type;
     29 class LLVMContext;
     30 class StructType;
     31 }
     32 
     33 namespace clang {
     34 class ABIInfo;
     35 class ASTContext;
     36 template <typename> class CanQual;
     37 class CXXConstructorDecl;
     38 class CXXDestructorDecl;
     39 class CXXMethodDecl;
     40 class CodeGenOptions;
     41 class FieldDecl;
     42 class FunctionProtoType;
     43 class ObjCInterfaceDecl;
     44 class ObjCIvarDecl;
     45 class PointerType;
     46 class QualType;
     47 class RecordDecl;
     48 class TagDecl;
     49 class TargetInfo;
     50 class Type;
     51 typedef CanQual<Type> CanQualType;
     52 
     53 namespace CodeGen {
     54 class CGCXXABI;
     55 class CGRecordLayout;
     56 class CodeGenModule;
     57 class RequiredArgs;
     58 
     59 enum class StructorType {
     60   Complete, // constructor or destructor
     61   Base,     // constructor or destructor
     62   Deleting  // destructor only
     63 };
     64 
     65 inline CXXCtorType toCXXCtorType(StructorType T) {
     66   switch (T) {
     67   case StructorType::Complete:
     68     return Ctor_Complete;
     69   case StructorType::Base:
     70     return Ctor_Base;
     71   case StructorType::Deleting:
     72     llvm_unreachable("cannot have a deleting ctor");
     73   }
     74   llvm_unreachable("not a StructorType");
     75 }
     76 
     77 inline StructorType getFromCtorType(CXXCtorType T) {
     78   switch (T) {
     79   case Ctor_Complete:
     80     return StructorType::Complete;
     81   case Ctor_Base:
     82     return StructorType::Base;
     83   case Ctor_Comdat:
     84     llvm_unreachable("not expecting a COMDAT");
     85   case Ctor_CopyingClosure:
     86   case Ctor_DefaultClosure:
     87     llvm_unreachable("not expecting a closure");
     88   }
     89   llvm_unreachable("not a CXXCtorType");
     90 }
     91 
     92 inline CXXDtorType toCXXDtorType(StructorType T) {
     93   switch (T) {
     94   case StructorType::Complete:
     95     return Dtor_Complete;
     96   case StructorType::Base:
     97     return Dtor_Base;
     98   case StructorType::Deleting:
     99     return Dtor_Deleting;
    100   }
    101   llvm_unreachable("not a StructorType");
    102 }
    103 
    104 inline StructorType getFromDtorType(CXXDtorType T) {
    105   switch (T) {
    106   case Dtor_Deleting:
    107     return StructorType::Deleting;
    108   case Dtor_Complete:
    109     return StructorType::Complete;
    110   case Dtor_Base:
    111     return StructorType::Base;
    112   case Dtor_Comdat:
    113     llvm_unreachable("not expecting a COMDAT");
    114   }
    115   llvm_unreachable("not a CXXDtorType");
    116 }
    117 
    118 /// CodeGenTypes - This class organizes the cross-module state that is used
    119 /// while lowering AST types to LLVM types.
    120 class CodeGenTypes {
    121   CodeGenModule &CGM;
    122   // Some of this stuff should probably be left on the CGM.
    123   ASTContext &Context;
    124   llvm::Module &TheModule;
    125   const llvm::DataLayout &TheDataLayout;
    126   const TargetInfo &Target;
    127   CGCXXABI &TheCXXABI;
    128 
    129   // This should not be moved earlier, since its initialization depends on some
    130   // of the previous reference members being already initialized
    131   const ABIInfo &TheABIInfo;
    132 
    133   /// The opaque type map for Objective-C interfaces. All direct
    134   /// manipulation is done by the runtime interfaces, which are
    135   /// responsible for coercing to the appropriate type; these opaque
    136   /// types are never refined.
    137   llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
    138 
    139   /// CGRecordLayouts - This maps llvm struct type with corresponding
    140   /// record layout info.
    141   llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
    142 
    143   /// RecordDeclTypes - This contains the LLVM IR type for any converted
    144   /// RecordDecl.
    145   llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
    146 
    147   /// FunctionInfos - Hold memoized CGFunctionInfo results.
    148   llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
    149 
    150   /// RecordsBeingLaidOut - This set keeps track of records that we're currently
    151   /// converting to an IR type.  For example, when converting:
    152   /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
    153   /// types will be in this set.
    154   llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut;
    155 
    156   llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
    157 
    158   /// SkippedLayout - True if we didn't layout a function due to a being inside
    159   /// a recursive struct conversion, set this to true.
    160   bool SkippedLayout;
    161 
    162   SmallVector<const RecordDecl *, 8> DeferredRecords;
    163 
    164 private:
    165   /// TypeCache - This map keeps cache of llvm::Types
    166   /// and maps clang::Type to corresponding llvm::Type.
    167   llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
    168 
    169 public:
    170   CodeGenTypes(CodeGenModule &cgm);
    171   ~CodeGenTypes();
    172 
    173   const llvm::DataLayout &getDataLayout() const { return TheDataLayout; }
    174   ASTContext &getContext() const { return Context; }
    175   const ABIInfo &getABIInfo() const { return TheABIInfo; }
    176   const TargetInfo &getTarget() const { return Target; }
    177   CGCXXABI &getCXXABI() const { return TheCXXABI; }
    178   llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
    179 
    180   /// ConvertType - Convert type T into a llvm::Type.
    181   llvm::Type *ConvertType(QualType T);
    182 
    183   /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
    184   /// ConvertType in that it is used to convert to the memory representation for
    185   /// a type.  For example, the scalar representation for _Bool is i1, but the
    186   /// memory representation is usually i8 or i32, depending on the target.
    187   llvm::Type *ConvertTypeForMem(QualType T);
    188 
    189   /// GetFunctionType - Get the LLVM function type for \arg Info.
    190   llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
    191 
    192   llvm::FunctionType *GetFunctionType(GlobalDecl GD);
    193 
    194   /// isFuncTypeConvertible - Utility to check whether a function type can
    195   /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
    196   /// type).
    197   bool isFuncTypeConvertible(const FunctionType *FT);
    198   bool isFuncParamTypeConvertible(QualType Ty);
    199 
    200   /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
    201   /// given a CXXMethodDecl. If the method to has an incomplete return type,
    202   /// and/or incomplete argument types, this will return the opaque type.
    203   llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
    204 
    205   const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
    206 
    207   /// UpdateCompletedType - When we find the full definition for a TagDecl,
    208   /// replace the 'opaque' type we previously made for it if applicable.
    209   void UpdateCompletedType(const TagDecl *TD);
    210 
    211   /// getNullaryFunctionInfo - Get the function info for a void()
    212   /// function with standard CC.
    213   const CGFunctionInfo &arrangeNullaryFunction();
    214 
    215   // The arrangement methods are split into three families:
    216   //   - those meant to drive the signature and prologue/epilogue
    217   //     of a function declaration or definition,
    218   //   - those meant for the computation of the LLVM type for an abstract
    219   //     appearance of a function, and
    220   //   - those meant for performing the IR-generation of a call.
    221   // They differ mainly in how they deal with optional (i.e. variadic)
    222   // arguments, as well as unprototyped functions.
    223   //
    224   // Key points:
    225   // - The CGFunctionInfo for emitting a specific call site must include
    226   //   entries for the optional arguments.
    227   // - The function type used at the call site must reflect the formal
    228   //   signature of the declaration being called, or else the call will
    229   //   go awry.
    230   // - For the most part, unprototyped functions are called by casting to
    231   //   a formal signature inferred from the specific argument types used
    232   //   at the call-site.  However, some targets (e.g. x86-64) screw with
    233   //   this for compatibility reasons.
    234 
    235   const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
    236   const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
    237   const CGFunctionInfo &
    238   arrangeFreeFunctionDeclaration(QualType ResTy, const FunctionArgList &Args,
    239                                  const FunctionType::ExtInfo &Info,
    240                                  bool isVariadic);
    241 
    242   const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
    243   const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
    244                                                         QualType receiverType);
    245 
    246   const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
    247   const CGFunctionInfo &arrangeCXXStructorDeclaration(const CXXMethodDecl *MD,
    248                                                       StructorType Type);
    249   const CGFunctionInfo &arrangeCXXConstructorCall(const CallArgList &Args,
    250                                                   const CXXConstructorDecl *D,
    251                                                   CXXCtorType CtorKind,
    252                                                   unsigned ExtraArgs);
    253   const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
    254                                                 const FunctionType *Ty,
    255                                                 bool ChainCall);
    256   const CGFunctionInfo &arrangeFreeFunctionCall(QualType ResTy,
    257                                                 const CallArgList &args,
    258                                                 FunctionType::ExtInfo info,
    259                                                 RequiredArgs required);
    260   const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
    261                                                  const FunctionType *type);
    262 
    263   const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
    264                                              const FunctionProtoType *type,
    265                                              RequiredArgs required);
    266   const CGFunctionInfo &arrangeMSMemberPointerThunk(const CXXMethodDecl *MD);
    267   const CGFunctionInfo &arrangeMSCtorClosure(const CXXConstructorDecl *CD,
    268                                                  CXXCtorType CT);
    269 
    270   const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty);
    271   const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
    272   const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
    273                                              const FunctionProtoType *FTP);
    274 
    275   /// "Arrange" the LLVM information for a call or type with the given
    276   /// signature.  This is largely an internal method; other clients
    277   /// should use one of the above routines, which ultimately defer to
    278   /// this.
    279   ///
    280   /// \param argTypes - must all actually be canonical as params
    281   const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
    282                                                 bool instanceMethod,
    283                                                 bool chainCall,
    284                                                 ArrayRef<CanQualType> argTypes,
    285                                                 FunctionType::ExtInfo info,
    286                                                 RequiredArgs args);
    287 
    288   /// \brief Compute a new LLVM record layout object for the given record.
    289   CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
    290                                       llvm::StructType *Ty);
    291 
    292   /// addRecordTypeName - Compute a name from the given record decl with an
    293   /// optional suffix and name the given LLVM type using it.
    294   void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
    295                          StringRef suffix);
    296 
    297 
    298 public:  // These are internal details of CGT that shouldn't be used externally.
    299   /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
    300   llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
    301 
    302   /// getExpandedTypes - Expand the type \arg Ty into the LLVM
    303   /// argument types it would be passed as. See ABIArgInfo::Expand.
    304   void getExpandedTypes(QualType Ty,
    305                         SmallVectorImpl<llvm::Type *>::iterator &TI);
    306 
    307   /// IsZeroInitializable - Return whether a type can be
    308   /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
    309   bool isZeroInitializable(QualType T);
    310 
    311   /// IsZeroInitializable - Return whether a record type can be
    312   /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
    313   bool isZeroInitializable(const CXXRecordDecl *RD);
    314 
    315   bool isRecordLayoutComplete(const Type *Ty) const;
    316   bool noRecordsBeingLaidOut() const {
    317     return RecordsBeingLaidOut.empty();
    318   }
    319   bool isRecordBeingLaidOut(const Type *Ty) const {
    320     return RecordsBeingLaidOut.count(Ty);
    321   }
    322 
    323 };
    324 
    325 }  // end namespace CodeGen
    326 }  // end namespace clang
    327 
    328 #endif
    329