CodeGenTypes.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #ifndef CLANG_CODEGEN_CODEGENTYPES_H
  14. #define CLANG_CODEGEN_CODEGENTYPES_H
  15. #include "llvm/Module.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include <vector>
  18. #include "CGCall.h"
  19. #include "GlobalDecl.h"
  20. namespace llvm {
  21. class FunctionType;
  22. class Module;
  23. class OpaqueType;
  24. class PATypeHolder;
  25. class TargetData;
  26. class Type;
  27. class LLVMContext;
  28. }
  29. namespace clang {
  30. class ABIInfo;
  31. class ASTContext;
  32. template <typename> class CanQual;
  33. class CXXConstructorDecl;
  34. class CXXDestructorDecl;
  35. class CXXMethodDecl;
  36. class FieldDecl;
  37. class FunctionProtoType;
  38. class ObjCInterfaceDecl;
  39. class ObjCIvarDecl;
  40. class PointerType;
  41. class QualType;
  42. class RecordDecl;
  43. class TagDecl;
  44. class TargetInfo;
  45. class Type;
  46. typedef CanQual<Type> CanQualType;
  47. namespace CodeGen {
  48. class CGRecordLayout;
  49. /// CodeGenTypes - This class organizes the cross-module state that is used
  50. /// while lowering AST types to LLVM types.
  51. class CodeGenTypes {
  52. ASTContext &Context;
  53. const TargetInfo &Target;
  54. llvm::Module& TheModule;
  55. const llvm::TargetData& TheTargetData;
  56. const ABIInfo& TheABIInfo;
  57. llvm::SmallVector<std::pair<QualType,
  58. llvm::OpaqueType *>, 8> PointersToResolve;
  59. llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes;
  60. llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes;
  61. /// The opaque type map for Objective-C interfaces. All direct
  62. /// manipulation is done by the runtime interfaces, which are
  63. /// responsible for coercing to the appropriate type; these opaque
  64. /// types are never refined.
  65. llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes;
  66. /// CGRecordLayouts - This maps llvm struct type with corresponding
  67. /// record layout info.
  68. llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
  69. /// FunctionInfos - Hold memoized CGFunctionInfo results.
  70. llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
  71. private:
  72. /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
  73. /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
  74. /// used instead of llvm::Type because it allows us to bypass potential
  75. /// dangling type pointers due to type refinement on llvm side.
  76. llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache;
  77. /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
  78. /// method directly because it does not do any type caching. This method
  79. /// is available only for ConvertType(). CovertType() is preferred
  80. /// interface to convert type T into a llvm::Type.
  81. const llvm::Type *ConvertNewType(QualType T);
  82. public:
  83. CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD,
  84. const ABIInfo &Info);
  85. ~CodeGenTypes();
  86. const llvm::TargetData &getTargetData() const { return TheTargetData; }
  87. const TargetInfo &getTarget() const { return Target; }
  88. ASTContext &getContext() const { return Context; }
  89. const ABIInfo &getABIInfo() const { return TheABIInfo; }
  90. llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
  91. /// ConvertType - Convert type T into a llvm::Type.
  92. const llvm::Type *ConvertType(QualType T);
  93. const llvm::Type *ConvertTypeRecursive(QualType T);
  94. /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
  95. /// ConvertType in that it is used to convert to the memory representation for
  96. /// a type. For example, the scalar representation for _Bool is i1, but the
  97. /// memory representation is usually i8 or i32, depending on the target.
  98. const llvm::Type *ConvertTypeForMem(QualType T);
  99. const llvm::Type *ConvertTypeForMemRecursive(QualType T);
  100. /// GetFunctionType - Get the LLVM function type for \arg Info.
  101. const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
  102. bool IsVariadic);
  103. const llvm::FunctionType *GetFunctionType(GlobalDecl GD);
  104. /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
  105. /// given a CXXMethodDecl. If the method to has an incomplete return type,
  106. /// and/or incomplete argument types, this will return the opaque type.
  107. const llvm::Type *GetFunctionTypeForVTable(const CXXMethodDecl *MD);
  108. const CGRecordLayout &getCGRecordLayout(const RecordDecl*) const;
  109. /// UpdateCompletedType - When we find the full definition for a TagDecl,
  110. /// replace the 'opaque' type we previously made for it if applicable.
  111. void UpdateCompletedType(const TagDecl *TD);
  112. /// getFunctionInfo - Get the function info for the specified function decl.
  113. const CGFunctionInfo &getFunctionInfo(GlobalDecl GD);
  114. const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
  115. const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
  116. const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
  117. const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D,
  118. CXXCtorType Type);
  119. const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D,
  120. CXXDtorType Type);
  121. const CGFunctionInfo &getFunctionInfo(const CallArgList &Args,
  122. const FunctionType *Ty) {
  123. return getFunctionInfo(Ty->getResultType(), Args,
  124. Ty->getExtInfo());
  125. }
  126. const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty);
  127. const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty);
  128. // getFunctionInfo - Get the function info for a member function.
  129. const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
  130. const FunctionProtoType *FTP);
  131. /// getFunctionInfo - Get the function info for a function described by a
  132. /// return type and argument types. If the calling convention is not
  133. /// specified, the "C" calling convention will be used.
  134. const CGFunctionInfo &getFunctionInfo(QualType ResTy,
  135. const CallArgList &Args,
  136. const FunctionType::ExtInfo &Info);
  137. const CGFunctionInfo &getFunctionInfo(QualType ResTy,
  138. const FunctionArgList &Args,
  139. const FunctionType::ExtInfo &Info);
  140. /// Retrieves the ABI information for the given function signature.
  141. ///
  142. /// \param ArgTys - must all actually be canonical as params
  143. const CGFunctionInfo &getFunctionInfo(CanQualType RetTy,
  144. const llvm::SmallVectorImpl<CanQualType> &ArgTys,
  145. const FunctionType::ExtInfo &Info);
  146. /// \brief Compute a new LLVM record layout object for the given record.
  147. CGRecordLayout *ComputeRecordLayout(const RecordDecl *D);
  148. public: // These are internal details of CGT that shouldn't be used externally.
  149. /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
  150. /// enum.
  151. const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
  152. /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
  153. /// argument types it would be passed as on the provided vector \arg
  154. /// ArgTys. See ABIArgInfo::Expand.
  155. void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys);
  156. /// ContainsPointerToDataMember - Return whether the given type contains a
  157. /// pointer to a data member.
  158. bool ContainsPointerToDataMember(QualType T);
  159. /// ContainsPointerToDataMember - Return whether the record decl contains a
  160. /// pointer to a data member.
  161. bool ContainsPointerToDataMember(const CXXRecordDecl *RD);
  162. };
  163. } // end namespace CodeGen
  164. } // end namespace clang
  165. #endif