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