CodeGenTypes.h revision 8330ceeebb3bfac31116b387b90ff2ce3cef85e4
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
21#include "CGCall.h"
22
23namespace llvm {
24  class FunctionType;
25  class Module;
26  class OpaqueType;
27  class PATypeHolder;
28  class TargetData;
29  class Type;
30}
31
32namespace clang {
33  class ABIInfo;
34  class ASTContext;
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
47namespace CodeGen {
48  class CodeGenTypes;
49
50  /// CGRecordLayout - This class handles struct and union layout info while
51  /// lowering AST types to LLVM types.
52  class CGRecordLayout {
53    CGRecordLayout(); // DO NOT IMPLEMENT
54  public:
55    CGRecordLayout(const llvm::Type *T,
56                   const llvm::SmallSet<unsigned, 8> &PF)
57      : STy(T), PaddingFields(PF) {
58      // FIXME : Collect info about fields that requires adjustments
59      // (i.e. fields that do not directly map to llvm struct fields.)
60    }
61
62    /// getLLVMType - Return llvm type associated with this record.
63    const llvm::Type *getLLVMType() const {
64      return STy;
65    }
66
67    bool isPaddingField(unsigned No) const {
68      return PaddingFields.count(No) != 0;
69    }
70
71    unsigned getNumPaddingFields() {
72      return PaddingFields.size();
73    }
74
75  private:
76    const llvm::Type *STy;
77    llvm::SmallSet<unsigned, 8> PaddingFields;
78  };
79
80/// CodeGenTypes - This class organizes the cross-module state that is used
81/// while lowering AST types to LLVM types.
82class CodeGenTypes {
83  ASTContext &Context;
84  TargetInfo &Target;
85  llvm::Module& TheModule;
86  const llvm::TargetData& TheTargetData;
87  mutable const ABIInfo* TheABIInfo;
88
89  llvm::SmallVector<std::pair<QualType,
90                              llvm::OpaqueType *>, 8>  PointersToResolve;
91
92  llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes;
93
94  llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes;
95
96  /// The opaque type map for Objective-C interfaces. All direct
97  /// manipulation is done by the runtime interfaces, which are
98  /// responsible for coercing to the appropriate type; these opaque
99  /// types are never refined.
100  llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes;
101
102  /// CGRecordLayouts - This maps llvm struct type with corresponding
103  /// record layout info.
104  /// FIXME : If CGRecordLayout is less than 16 bytes then use
105  /// inline it in the map.
106  llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
107
108  /// FieldInfo - This maps struct field with corresponding llvm struct type
109  /// field no. This info is populated by record organizer.
110  llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
111
112  /// FunctionInfos - Hold memoized CGFunctionInfo results.
113  llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
114
115public:
116  struct BitFieldInfo {
117    BitFieldInfo(unsigned FieldNo,
118                 unsigned Start,
119                 unsigned Size)
120      : FieldNo(FieldNo), Start(Start), Size(Size) {}
121
122    unsigned FieldNo;
123    unsigned Start;
124    unsigned Size;
125  };
126
127private:
128  llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields;
129
130  /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
131  /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
132  /// used instead of llvm::Type because it allows us to bypass potential
133  /// dangling type pointers due to type refinement on llvm side.
134  llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache;
135
136  /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
137  /// method directly because it does not do any type caching. This method
138  /// is available only for ConvertType(). CovertType() is preferred
139  /// interface to convert type T into a llvm::Type.
140  const llvm::Type *ConvertNewType(QualType T);
141public:
142  CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD);
143  ~CodeGenTypes();
144
145  const llvm::TargetData &getTargetData() const { return TheTargetData; }
146  TargetInfo &getTarget() const { return Target; }
147  ASTContext &getContext() const { return Context; }
148  const ABIInfo &getABIInfo() const;
149
150  /// ConvertType - Convert type T into a llvm::Type.
151  const llvm::Type *ConvertType(QualType T);
152  const llvm::Type *ConvertTypeRecursive(QualType T);
153
154  /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
155  /// ConvertType in that it is used to convert to the memory representation for
156  /// a type.  For example, the scalar representation for _Bool is i1, but the
157  /// memory representation is usually i8 or i32, depending on the target.
158  const llvm::Type *ConvertTypeForMem(QualType T);
159  const llvm::Type *ConvertTypeForMemRecursive(QualType T);
160
161  /// GetFunctionType - Get the LLVM function type for \arg Info.
162  const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
163                                            bool IsVariadic);
164
165  const CGRecordLayout *getCGRecordLayout(const TagDecl*) const;
166
167  /// getLLVMFieldNo - Return llvm::StructType element number
168  /// that corresponds to the field FD.
169  unsigned getLLVMFieldNo(const FieldDecl *FD);
170
171  /// UpdateCompletedType - When we find the full definition for a TagDecl,
172  /// replace the 'opaque' type we previously made for it if applicable.
173  void UpdateCompletedType(const TagDecl *TD);
174
175  /// getFunctionInfo - Get the CGFunctionInfo for this function signature.
176  const CGFunctionInfo &getFunctionInfo(QualType RetTy,
177                                        const llvm::SmallVector<QualType,16>
178                                        &ArgTys);
179
180  const CGFunctionInfo &getFunctionInfo(const FunctionNoProtoType *FTNP);
181  const CGFunctionInfo &getFunctionInfo(const FunctionProtoType *FTP);
182  const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
183  const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
184  const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
185  const CGFunctionInfo &getFunctionInfo(QualType ResTy,
186                                        const CallArgList &Args);
187public:
188  const CGFunctionInfo &getFunctionInfo(QualType ResTy,
189                                        const FunctionArgList &Args);
190
191public:  // These are internal details of CGT that shouldn't be used externally.
192  /// addFieldInfo - Assign field number to field FD.
193  void addFieldInfo(const FieldDecl *FD, unsigned FieldNo);
194
195  /// addBitFieldInfo - Assign a start bit and a size to field FD.
196  void addBitFieldInfo(const FieldDecl *FD, unsigned FieldNo,
197                       unsigned Start, unsigned Size);
198
199  /// getBitFieldInfo - Return the BitFieldInfo  that corresponds to the field
200  /// FD.
201  BitFieldInfo getBitFieldInfo(const FieldDecl *FD);
202
203  /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
204  /// enum.
205  const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
206
207  /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
208  /// argument types it would be passed as on the provided vector \arg
209  /// ArgTys. See ABIArgInfo::Expand.
210  void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys);
211};
212
213}  // end namespace CodeGen
214}  // end namespace clang
215
216#endif
217