CodeGenTypes.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/ADT/DenseMap.h"
  16. #include "llvm/ADT/SmallSet.h"
  17. #include <vector>
  18. namespace llvm {
  19. class Module;
  20. class Type;
  21. class PATypeHolder;
  22. class TargetData;
  23. }
  24. namespace clang {
  25. class ASTContext;
  26. class TagDecl;
  27. class TargetInfo;
  28. class QualType;
  29. class Type;
  30. class FunctionTypeProto;
  31. class FieldDecl;
  32. class RecordDecl;
  33. class ObjCInterfaceDecl;
  34. class ObjCIvarDecl;
  35. namespace CodeGen {
  36. class CodeGenTypes;
  37. /// CGRecordLayout - This class handles struct and union layout info while
  38. /// lowering AST types to LLVM types.
  39. class CGRecordLayout {
  40. CGRecordLayout(); // DO NOT IMPLEMENT
  41. public:
  42. CGRecordLayout(llvm::Type *T, llvm::SmallSet<unsigned, 8> &PF)
  43. : STy(T), PaddingFields(PF) {
  44. // FIXME : Collect info about fields that requires adjustments
  45. // (i.e. fields that do not directly map to llvm struct fields.)
  46. }
  47. /// getLLVMType - Return llvm type associated with this record.
  48. llvm::Type *getLLVMType() const {
  49. return STy;
  50. }
  51. bool isPaddingField(unsigned No) const {
  52. return PaddingFields.count(No) != 0;
  53. }
  54. unsigned getNumPaddingFields() {
  55. return PaddingFields.size();
  56. }
  57. private:
  58. llvm::Type *STy;
  59. llvm::SmallSet<unsigned, 8> PaddingFields;
  60. };
  61. /// CodeGenTypes - This class organizes the cross-module state that is used
  62. /// while lowering AST types to LLVM types.
  63. class CodeGenTypes {
  64. ASTContext &Context;
  65. TargetInfo &Target;
  66. llvm::Module& TheModule;
  67. const llvm::TargetData& TheTargetData;
  68. llvm::DenseMap<const TagDecl*, llvm::PATypeHolder> TagDeclTypes;
  69. /// CGRecordLayouts - This maps llvm struct type with corresponding
  70. /// record layout info.
  71. /// FIXME : If CGRecordLayout is less than 16 bytes then use
  72. /// inline it in the map.
  73. llvm::DenseMap<const TagDecl*, CGRecordLayout *> CGRecordLayouts;
  74. /// FieldInfo - This maps struct field with corresponding llvm struct type
  75. /// field no. This info is populated by record organizer.
  76. llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
  77. llvm::DenseMap<const ObjCIvarDecl *, unsigned> ObjCIvarInfo;
  78. public:
  79. class BitFieldInfo {
  80. public:
  81. explicit BitFieldInfo(unsigned short B, unsigned short S)
  82. : Begin(B), Size(S) {}
  83. unsigned short Begin;
  84. unsigned short Size;
  85. };
  86. private:
  87. llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields;
  88. /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
  89. /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
  90. /// used instead of llvm::Type because it allows us to bypass potential
  91. /// dangling type pointers due to type refinement on llvm side.
  92. llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache;
  93. /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
  94. /// method directly because it does not do any type caching. This method
  95. /// is available only for ConvertType(). CovertType() is preferred
  96. /// interface to convert type T into a llvm::Type.
  97. const llvm::Type *ConvertNewType(QualType T);
  98. public:
  99. CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD);
  100. ~CodeGenTypes();
  101. const llvm::TargetData &getTargetData() const { return TheTargetData; }
  102. TargetInfo &getTarget() const { return Target; }
  103. ASTContext &getContext() const { return Context; }
  104. /// ConvertType - Convert type T into a llvm::Type.
  105. const llvm::Type *ConvertType(QualType T);
  106. /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
  107. /// ConvertType in that it is used to convert to the memory representation for
  108. /// a type. For example, the scalar representation for _Bool is i1, but the
  109. /// memory representation is usually i8 or i32, depending on the target.
  110. const llvm::Type *ConvertTypeForMem(QualType T);
  111. void CollectObjCIvarTypes(ObjCInterfaceDecl *ObjCClass,
  112. std::vector<const llvm::Type*> &IvarTypes);
  113. const CGRecordLayout *getCGRecordLayout(const TagDecl*) const;
  114. /// getLLVMFieldNo - Return llvm::StructType element number
  115. /// that corresponds to the field FD.
  116. unsigned getLLVMFieldNo(const FieldDecl *FD);
  117. unsigned getLLVMFieldNo(const ObjCIvarDecl *OID);
  118. /// UpdateCompletedType - When we find the full definition for a TagDecl,
  119. /// replace the 'opaque' type we previously made for it if applicable.
  120. void UpdateCompletedType(const TagDecl *TD);
  121. public: // These are internal details of CGT that shouldn't be used externally.
  122. void DecodeArgumentTypes(const FunctionTypeProto &FTP,
  123. std::vector<const llvm::Type*> &ArgTys);
  124. /// addFieldInfo - Assign field number to field FD.
  125. void addFieldInfo(const FieldDecl *FD, unsigned No);
  126. /// addBitFieldInfo - Assign a start bit and a size to field FD.
  127. void addBitFieldInfo(const FieldDecl *FD, unsigned Begin, unsigned Size);
  128. /// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field
  129. /// FD.
  130. BitFieldInfo getBitFieldInfo(const FieldDecl *FD);
  131. /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
  132. /// enum.
  133. const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
  134. };
  135. } // end namespace CodeGen
  136. } // end namespace clang
  137. #endif