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 CLANG_CODEGEN_CODEGENTYPES_H
     15 #define CLANG_CODEGEN_CODEGENTYPES_H
     16 
     17 #include "CGCall.h"
     18 #include "clang/AST/GlobalDecl.h"
     19 #include "llvm/Module.h"
     20 #include "llvm/ADT/DenseMap.h"
     21 #include <vector>
     22 
     23 namespace llvm {
     24   class FunctionType;
     25   class Module;
     26   class TargetData;
     27   class Type;
     28   class LLVMContext;
     29   class StructType;
     30 }
     31 
     32 namespace clang {
     33   class ABIInfo;
     34   class ASTContext;
     35   template <typename> class CanQual;
     36   class CXXConstructorDecl;
     37   class CXXDestructorDecl;
     38   class CXXMethodDecl;
     39   class CodeGenOptions;
     40   class FieldDecl;
     41   class FunctionProtoType;
     42   class ObjCInterfaceDecl;
     43   class ObjCIvarDecl;
     44   class PointerType;
     45   class QualType;
     46   class RecordDecl;
     47   class TagDecl;
     48   class TargetInfo;
     49   class Type;
     50   typedef CanQual<Type> CanQualType;
     51 
     52 namespace CodeGen {
     53   class CGCXXABI;
     54   class CGRecordLayout;
     55 
     56 /// CodeGenTypes - This class organizes the cross-module state that is used
     57 /// while lowering AST types to LLVM types.
     58 class CodeGenTypes {
     59   ASTContext &Context;
     60   const TargetInfo &Target;
     61   llvm::Module &TheModule;
     62   const llvm::TargetData &TheTargetData;
     63   const ABIInfo &TheABIInfo;
     64   CGCXXABI &TheCXXABI;
     65   const CodeGenOptions &CodeGenOpts;
     66 
     67   /// The opaque type map for Objective-C interfaces. All direct
     68   /// manipulation is done by the runtime interfaces, which are
     69   /// responsible for coercing to the appropriate type; these opaque
     70   /// types are never refined.
     71   llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
     72 
     73   /// CGRecordLayouts - This maps llvm struct type with corresponding
     74   /// record layout info.
     75   llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
     76 
     77   /// RecordDeclTypes - This contains the LLVM IR type for any converted
     78   /// RecordDecl.
     79   llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
     80 
     81   /// FunctionInfos - Hold memoized CGFunctionInfo results.
     82   llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
     83 
     84   /// RecordsBeingLaidOut - This set keeps track of records that we're currently
     85   /// converting to an IR type.  For example, when converting:
     86   /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
     87   /// types will be in this set.
     88   llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut;
     89 
     90   llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
     91 
     92   /// SkippedLayout - True if we didn't layout a function due to a being inside
     93   /// a recursive struct conversion, set this to true.
     94   bool SkippedLayout;
     95 
     96   SmallVector<const RecordDecl *, 8> DeferredRecords;
     97 
     98 private:
     99   /// TypeCache - This map keeps cache of llvm::Types
    100   /// and maps llvm::Types to corresponding clang::Type.
    101   llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
    102 
    103 public:
    104   CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD,
    105                const ABIInfo &Info, CGCXXABI &CXXABI,
    106                const CodeGenOptions &Opts);
    107   ~CodeGenTypes();
    108 
    109   const llvm::TargetData &getTargetData() const { return TheTargetData; }
    110   const TargetInfo &getTarget() const { return Target; }
    111   ASTContext &getContext() const { return Context; }
    112   const ABIInfo &getABIInfo() const { return TheABIInfo; }
    113   const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
    114   CGCXXABI &getCXXABI() const { return TheCXXABI; }
    115   llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
    116 
    117   /// ConvertType - Convert type T into a llvm::Type.
    118   llvm::Type *ConvertType(QualType T);
    119 
    120   /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
    121   /// ConvertType in that it is used to convert to the memory representation for
    122   /// a type.  For example, the scalar representation for _Bool is i1, but the
    123   /// memory representation is usually i8 or i32, depending on the target.
    124   llvm::Type *ConvertTypeForMem(QualType T);
    125 
    126   /// GetFunctionType - Get the LLVM function type for \arg Info.
    127   llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
    128                                       bool IsVariadic);
    129 
    130   llvm::FunctionType *GetFunctionType(GlobalDecl GD);
    131 
    132   /// isFuncTypeConvertible - Utility to check whether a function type can
    133   /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
    134   /// type).
    135   bool isFuncTypeConvertible(const FunctionType *FT);
    136   bool isFuncTypeArgumentConvertible(QualType Ty);
    137 
    138   /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
    139   /// given a CXXMethodDecl. If the method to has an incomplete return type,
    140   /// and/or incomplete argument types, this will return the opaque type.
    141   llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
    142 
    143   const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
    144 
    145   /// UpdateCompletedType - When we find the full definition for a TagDecl,
    146   /// replace the 'opaque' type we previously made for it if applicable.
    147   void UpdateCompletedType(const TagDecl *TD);
    148 
    149   /// getNullaryFunctionInfo - Get the function info for a void()
    150   /// function with standard CC.
    151   const CGFunctionInfo &getNullaryFunctionInfo();
    152 
    153   /// getFunctionInfo - Get the function info for the specified function decl.
    154   const CGFunctionInfo &getFunctionInfo(GlobalDecl GD);
    155 
    156   const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
    157   const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
    158   const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
    159   const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D,
    160                                         CXXCtorType Type);
    161   const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D,
    162                                         CXXDtorType Type);
    163 
    164   const CGFunctionInfo &getFunctionInfo(const CallArgList &Args,
    165                                         const FunctionType *Ty) {
    166     return getFunctionInfo(Ty->getResultType(), Args,
    167                            Ty->getExtInfo());
    168   }
    169 
    170   const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty);
    171   const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty);
    172 
    173   /// getFunctionInfo - Get the function info for a member function of
    174   /// the given type.  This is used for calls through member function
    175   /// pointers.
    176   const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
    177                                         const FunctionProtoType *FTP);
    178 
    179   /// getFunctionInfo - Get the function info for a function described by a
    180   /// return type and argument types. If the calling convention is not
    181   /// specified, the "C" calling convention will be used.
    182   const CGFunctionInfo &getFunctionInfo(QualType ResTy,
    183                                         const CallArgList &Args,
    184                                         const FunctionType::ExtInfo &Info);
    185   const CGFunctionInfo &getFunctionInfo(QualType ResTy,
    186                                         const FunctionArgList &Args,
    187                                         const FunctionType::ExtInfo &Info);
    188 
    189   /// Retrieves the ABI information for the given function signature.
    190   ///
    191   /// \param ArgTys - must all actually be canonical as params
    192   const CGFunctionInfo &getFunctionInfo(CanQualType RetTy,
    193                                const SmallVectorImpl<CanQualType> &ArgTys,
    194                                         const FunctionType::ExtInfo &Info);
    195 
    196   /// \brief Compute a new LLVM record layout object for the given record.
    197   CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
    198                                       llvm::StructType *Ty);
    199 
    200   /// addRecordTypeName - Compute a name from the given record decl with an
    201   /// optional suffix and name the given LLVM type using it.
    202   void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
    203                          StringRef suffix);
    204 
    205 
    206 public:  // These are internal details of CGT that shouldn't be used externally.
    207   /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
    208   llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
    209 
    210   /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
    211   /// argument types it would be passed as on the provided vector \arg
    212   /// ArgTys. See ABIArgInfo::Expand.
    213   void GetExpandedTypes(QualType type,
    214                         SmallVectorImpl<llvm::Type*> &expanded);
    215 
    216   /// IsZeroInitializable - Return whether a type can be
    217   /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
    218   bool isZeroInitializable(QualType T);
    219 
    220   /// IsZeroInitializable - Return whether a record type can be
    221   /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
    222   bool isZeroInitializable(const CXXRecordDecl *RD);
    223 
    224   bool isRecordLayoutComplete(const Type *Ty) const;
    225   bool noRecordsBeingLaidOut() const {
    226     return RecordsBeingLaidOut.empty();
    227   }
    228   bool isRecordBeingLaidOut(const Type *Ty) const {
    229     return RecordsBeingLaidOut.count(Ty);
    230   }
    231 
    232 };
    233 
    234 }  // end namespace CodeGen
    235 }  // end namespace clang
    236 
    237 #endif
    238