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/ADT/DenseMap.h"
     20 #include "llvm/IR/Module.h"
     21 #include <vector>
     22 
     23 namespace llvm {
     24   class FunctionType;
     25   class Module;
     26   class DataLayout;
     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   class CodeGenModule;
     56   class RequiredArgs;
     57 
     58 /// CodeGenTypes - This class organizes the cross-module state that is used
     59 /// while lowering AST types to LLVM types.
     60 class CodeGenTypes {
     61 public:
     62   // Some of this stuff should probably be left on the CGM.
     63   CodeGenModule &CGM;
     64   ASTContext &Context;
     65   llvm::Module &TheModule;
     66   const llvm::DataLayout &TheDataLayout;
     67   const TargetInfo &Target;
     68   CGCXXABI &TheCXXABI;
     69   const CodeGenOptions &CodeGenOpts;
     70 
     71   // This should not be moved earlier, since its initialization depends on some
     72   // of the previous reference members being already initialized
     73   const ABIInfo &TheABIInfo;
     74 
     75 private:
     76   /// The opaque type map for Objective-C interfaces. All direct
     77   /// manipulation is done by the runtime interfaces, which are
     78   /// responsible for coercing to the appropriate type; these opaque
     79   /// types are never refined.
     80   llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
     81 
     82   /// CGRecordLayouts - This maps llvm struct type with corresponding
     83   /// record layout info.
     84   llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
     85 
     86   /// RecordDeclTypes - This contains the LLVM IR type for any converted
     87   /// RecordDecl.
     88   llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
     89 
     90   /// FunctionInfos - Hold memoized CGFunctionInfo results.
     91   llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
     92 
     93   /// RecordsBeingLaidOut - This set keeps track of records that we're currently
     94   /// converting to an IR type.  For example, when converting:
     95   /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
     96   /// types will be in this set.
     97   llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut;
     98 
     99   llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
    100 
    101   /// SkippedLayout - True if we didn't layout a function due to a being inside
    102   /// a recursive struct conversion, set this to true.
    103   bool SkippedLayout;
    104 
    105   SmallVector<const RecordDecl *, 8> DeferredRecords;
    106 
    107 private:
    108   /// TypeCache - This map keeps cache of llvm::Types
    109   /// and maps llvm::Types to corresponding clang::Type.
    110   llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
    111 
    112 public:
    113   CodeGenTypes(CodeGenModule &cgm);
    114   ~CodeGenTypes();
    115 
    116   const llvm::DataLayout &getDataLayout() const { return TheDataLayout; }
    117   ASTContext &getContext() const { return Context; }
    118   const ABIInfo &getABIInfo() const { return TheABIInfo; }
    119   const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
    120   const TargetInfo &getTarget() const { return Target; }
    121   CGCXXABI &getCXXABI() const { return TheCXXABI; }
    122   llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
    123 
    124   /// ConvertType - Convert type T into a llvm::Type.
    125   llvm::Type *ConvertType(QualType T);
    126 
    127   /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
    128   /// ConvertType in that it is used to convert to the memory representation for
    129   /// a type.  For example, the scalar representation for _Bool is i1, but the
    130   /// memory representation is usually i8 or i32, depending on the target.
    131   llvm::Type *ConvertTypeForMem(QualType T);
    132 
    133   /// GetFunctionType - Get the LLVM function type for \arg Info.
    134   llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
    135 
    136   llvm::FunctionType *GetFunctionType(GlobalDecl GD);
    137 
    138   /// isFuncTypeConvertible - Utility to check whether a function type can
    139   /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
    140   /// type).
    141   bool isFuncTypeConvertible(const FunctionType *FT);
    142   bool isFuncTypeArgumentConvertible(QualType Ty);
    143 
    144   /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
    145   /// given a CXXMethodDecl. If the method to has an incomplete return type,
    146   /// and/or incomplete argument types, this will return the opaque type.
    147   llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
    148 
    149   const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
    150 
    151   /// UpdateCompletedType - When we find the full definition for a TagDecl,
    152   /// replace the 'opaque' type we previously made for it if applicable.
    153   void UpdateCompletedType(const TagDecl *TD);
    154 
    155   /// getNullaryFunctionInfo - Get the function info for a void()
    156   /// function with standard CC.
    157   const CGFunctionInfo &arrangeNullaryFunction();
    158 
    159   // The arrangement methods are split into three families:
    160   //   - those meant to drive the signature and prologue/epilogue
    161   //     of a function declaration or definition,
    162   //   - those meant for the computation of the LLVM type for an abstract
    163   //     appearance of a function, and
    164   //   - those meant for performing the IR-generation of a call.
    165   // They differ mainly in how they deal with optional (i.e. variadic)
    166   // arguments, as well as unprototyped functions.
    167   //
    168   // Key points:
    169   // - The CGFunctionInfo for emitting a specific call site must include
    170   //   entries for the optional arguments.
    171   // - The function type used at the call site must reflect the formal
    172   //   signature of the declaration being called, or else the call will
    173   //   go awry.
    174   // - For the most part, unprototyped functions are called by casting to
    175   //   a formal signature inferred from the specific argument types used
    176   //   at the call-site.  However, some targets (e.g. x86-64) screw with
    177   //   this for compatibility reasons.
    178 
    179   const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
    180   const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
    181   const CGFunctionInfo &arrangeFunctionDeclaration(QualType ResTy,
    182                                                    const FunctionArgList &Args,
    183                                              const FunctionType::ExtInfo &Info,
    184                                                    bool isVariadic);
    185 
    186   const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
    187   const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
    188                                                         QualType receiverType);
    189 
    190   const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
    191   const CGFunctionInfo &arrangeCXXConstructorDeclaration(
    192                                                     const CXXConstructorDecl *D,
    193                                                     CXXCtorType Type);
    194   const CGFunctionInfo &arrangeCXXDestructor(const CXXDestructorDecl *D,
    195                                              CXXDtorType Type);
    196 
    197   const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
    198                                                 const FunctionType *Ty);
    199   const CGFunctionInfo &arrangeFreeFunctionCall(QualType ResTy,
    200                                                 const CallArgList &args,
    201                                                 FunctionType::ExtInfo info,
    202                                                 RequiredArgs required);
    203   const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
    204                                                  const FunctionType *type);
    205 
    206   const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
    207                                              const FunctionProtoType *type,
    208                                              RequiredArgs required);
    209 
    210   const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty);
    211   const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
    212   const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
    213                                              const FunctionProtoType *FTP);
    214 
    215   /// "Arrange" the LLVM information for a call or type with the given
    216   /// signature.  This is largely an internal method; other clients
    217   /// should use one of the above routines, which ultimately defer to
    218   /// this.
    219   ///
    220   /// \param argTypes - must all actually be canonical as params
    221   const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
    222                                                 ArrayRef<CanQualType> argTypes,
    223                                                 FunctionType::ExtInfo info,
    224                                                 RequiredArgs args);
    225 
    226   /// \brief Compute a new LLVM record layout object for the given record.
    227   CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
    228                                       llvm::StructType *Ty);
    229 
    230   /// addRecordTypeName - Compute a name from the given record decl with an
    231   /// optional suffix and name the given LLVM type using it.
    232   void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
    233                          StringRef suffix);
    234 
    235 
    236 public:  // These are internal details of CGT that shouldn't be used externally.
    237   /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
    238   llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
    239 
    240   /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
    241   /// argument types it would be passed as on the provided vector \arg
    242   /// ArgTys. See ABIArgInfo::Expand.
    243   void GetExpandedTypes(QualType type,
    244                         SmallVectorImpl<llvm::Type*> &expanded);
    245 
    246   /// IsZeroInitializable - Return whether a type can be
    247   /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
    248   bool isZeroInitializable(QualType T);
    249 
    250   /// IsZeroInitializable - Return whether a record type can be
    251   /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
    252   bool isZeroInitializable(const CXXRecordDecl *RD);
    253 
    254   bool isRecordLayoutComplete(const Type *Ty) const;
    255   bool noRecordsBeingLaidOut() const {
    256     return RecordsBeingLaidOut.empty();
    257   }
    258   bool isRecordBeingLaidOut(const Type *Ty) const {
    259     return RecordsBeingLaidOut.count(Ty);
    260   }
    261 
    262 };
    263 
    264 }  // end namespace CodeGen
    265 }  // end namespace clang
    266 
    267 #endif
    268