1 //===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the code that handles AST -> LLVM type lowering.
12 //===----------------------------------------------------------------------===//
14 #ifndef CODEGEN_CODEGENTYPES_H
15 #define CODEGEN_CODEGENTYPES_H
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/SmallSet.h"
34 class FunctionTypeProto;
41 /// CGRecordLayout - This class handles struct and union layout info while
42 /// lowering AST types to LLVM types.
43 class CGRecordLayout {
44 CGRecordLayout(); // DO NOT IMPLEMENT
46 CGRecordLayout(llvm::Type *T, llvm::SmallSet<unsigned, 8> &PF)
47 : STy(T), PaddingFields(PF) {
48 // FIXME : Collect info about fields that requires adjustments
49 // (i.e. fields that do not directly map to llvm struct fields.)
52 /// getLLVMType - Return llvm type associated with this record.
53 llvm::Type *getLLVMType() const {
57 bool isPaddingField(unsigned No) const {
58 return PaddingFields.count(No) != 0;
61 unsigned getNumPaddingFields() {
62 return PaddingFields.size();
67 llvm::SmallSet<unsigned, 8> PaddingFields;
70 /// CodeGenTypes - This class organizes the cross-module state that is used
71 /// while lowering AST types to LLVM types.
75 llvm::Module& TheModule;
76 const llvm::TargetData& TheTargetData;
78 llvm::DenseMap<const TagDecl*, llvm::PATypeHolder> TagDeclTypes;
80 /// CGRecordLayouts - This maps llvm struct type with corresponding
81 /// record layout info.
82 /// FIXME : If CGRecordLayout is less than 16 bytes then use
83 /// inline it in the map.
84 llvm::DenseMap<const TagDecl*, CGRecordLayout *> CGRecordLayouts;
86 /// FieldInfo - This maps struct field with corresponding llvm struct type
87 /// field no. This info is populated by record organizer.
88 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
93 explicit BitFieldInfo(unsigned short B, unsigned short S)
94 : Begin(B), Size(S) {}
101 llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields;
103 /// TypeHolderMap - This map keeps cache of llvm::Types (through PATypeHolder)
104 /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
105 /// used instead of llvm::Type because it allows us to bypass potential
106 /// dangling type pointers due to type refinement on llvm side.
107 llvm::DenseMap<Type *, llvm::PATypeHolder> TypeHolderMap;
109 /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
110 /// method directly because it does not do any type caching. This method
111 /// is available only for ConvertType(). CovertType() is preferred
112 /// interface to convert type T into a llvm::Type.
113 const llvm::Type *ConvertNewType(QualType T);
115 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD);
118 const llvm::TargetData &getTargetData() const { return TheTargetData; }
119 TargetInfo &getTarget() const { return Target; }
120 ASTContext &getContext() const { return Context; }
122 /// ConvertType - Convert type T into a llvm::Type. Maintain and use
123 /// type cache through TypeHolderMap.
124 const llvm::Type *ConvertType(QualType T);
126 /// ConvertTypeForMem - Convert type T into a llvm::Type. Maintain and use
127 /// type cache through TypeHolderMap. This differs from ConvertType in that
128 /// it is used to convert to the memory representation for a type. For
129 /// example, the scalar representation for _Bool is i1, but the memory
130 /// representation is usually i8 or i32, depending on the target.
131 const llvm::Type *ConvertTypeForMem(QualType T);
134 const CGRecordLayout *getCGRecordLayout(const TagDecl*) const;
136 /// getLLVMFieldNo - Return llvm::StructType element number
137 /// that corresponds to the field FD.
138 unsigned getLLVMFieldNo(const FieldDecl *FD);
141 /// UpdateCompletedType - When we find the full definition for a TagDecl,
142 /// replace the 'opaque' type we previously made for it if applicable.
143 void UpdateCompletedType(const TagDecl *TD);
145 public: // These are internal details of CGT that shouldn't be used externally.
146 void DecodeArgumentTypes(const FunctionTypeProto &FTP,
147 std::vector<const llvm::Type*> &ArgTys);
149 /// addFieldInfo - Assign field number to field FD.
150 void addFieldInfo(const FieldDecl *FD, unsigned No);
152 /// addBitFieldInfo - Assign a start bit and a size to field FD.
153 void addBitFieldInfo(const FieldDecl *FD, unsigned Begin, unsigned Size);
155 /// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field
157 BitFieldInfo getBitFieldInfo(const FieldDecl *FD);
159 /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
161 const llvm::Type *ConvertTagDeclType(QualType T, const TagDecl *TD);
164 } // end namespace CodeGen
165 } // end namespace clang