CodeGenTypes.h revision 203955
1181430Sstas//===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===//
2181430Sstas//
3181430Sstas//                     The LLVM Compiler Infrastructure
4181430Sstas//
5181430Sstas// This file is distributed under the University of Illinois Open Source
6181430Sstas// License. See LICENSE.TXT for details.
7181430Sstas//
8181430Sstas//===----------------------------------------------------------------------===//
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/Module.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/SmallSet.h"
20#include <vector>
21
22#include "CGCall.h"
23#include "GlobalDecl.h"
24
25namespace llvm {
26  class FunctionType;
27  class Module;
28  class OpaqueType;
29  class PATypeHolder;
30  class TargetData;
31  class Type;
32  class LLVMContext;
33}
34
35namespace clang {
36  class ABIInfo;
37  class ASTContext;
38  class CXXConstructorDecl;
39  class CXXDestructorDecl;
40  class CXXMethodDecl;
41  class FieldDecl;
42  class FunctionProtoType;
43  class ObjCInterfaceDecl;
44  class ObjCIvarDecl;
45  class PointerType;
46  class QualType;
47  class RecordDecl;
48  class TagDecl;
49  class TargetInfo;
50  class Type;
51
52namespace CodeGen {
53  class CodeGenTypes;
54
55  /// CGRecordLayout - This class handles struct and union layout info while
56  /// lowering AST types to LLVM types.
57  class CGRecordLayout {
58    CGRecordLayout(); // DO NOT IMPLEMENT
59
60    /// LLVMType - The LLVMType corresponding to this record layout.
61    const llvm::Type *LLVMType;
62
63    /// ContainsPointerToDataMember - Whether one of the fields in this record
64    /// layout is a pointer to data member, or a struct that contains pointer to
65    /// data member.
66    bool ContainsPointerToDataMember;
67
68  public:
69    CGRecordLayout(const llvm::Type *T, bool ContainsPointerToDataMember)
70      : LLVMType(T), ContainsPointerToDataMember(ContainsPointerToDataMember) { }
71
72    /// getLLVMType - Return llvm type associated with this record.
73    const llvm::Type *getLLVMType() const {
74      return LLVMType;
75    }
76
77    /// containsPointerToDataMember - Whether this struct contains pointers to
78    /// data members.
79    bool containsPointerToDataMember() const {
80      return ContainsPointerToDataMember;
81    }
82  };
83
84/// CodeGenTypes - This class organizes the cross-module state that is used
85/// while lowering AST types to LLVM types.
86class CodeGenTypes {
87  ASTContext &Context;
88  const TargetInfo &Target;
89  llvm::Module& TheModule;
90  const llvm::TargetData& TheTargetData;
91  const ABIInfo& TheABIInfo;
92
93  llvm::SmallVector<std::pair<QualType,
94                              llvm::OpaqueType *>, 8>  PointersToResolve;
95
96  llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes;
97
98  llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes;
99
100  /// The opaque type map for Objective-C interfaces. All direct
101  /// manipulation is done by the runtime interfaces, which are
102  /// responsible for coercing to the appropriate type; these opaque
103  /// types are never refined.
104  llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes;
105
106  /// CGRecordLayouts - This maps llvm struct type with corresponding
107  /// record layout info.
108  /// FIXME : If CGRecordLayout is less than 16 bytes then use
109  /// inline it in the map.
110  llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
111
112  /// FieldInfo - This maps struct field with corresponding llvm struct type
113  /// field no. This info is populated by record organizer.
114  llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
115
116  /// FunctionInfos - Hold memoized CGFunctionInfo results.
117  llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
118
119public:
120  struct BitFieldInfo {
121    BitFieldInfo(unsigned FieldNo,
122                 unsigned Start,
123                 unsigned Size)
124      : FieldNo(FieldNo), Start(Start), Size(Size) {}
125
126    unsigned FieldNo;
127    unsigned Start;
128    unsigned Size;
129  };
130
131private:
132  llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields;
133
134  /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
135  /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
136  /// used instead of llvm::Type because it allows us to bypass potential
137  /// dangling type pointers due to type refinement on llvm side.
138  llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache;
139
140  /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
141  /// method directly because it does not do any type caching. This method
142  /// is available only for ConvertType(). CovertType() is preferred
143  /// interface to convert type T into a llvm::Type.
144  const llvm::Type *ConvertNewType(QualType T);
145public:
146  CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD,
147               const ABIInfo &Info);
148  ~CodeGenTypes();
149
150  const llvm::TargetData &getTargetData() const { return TheTargetData; }
151  const TargetInfo &getTarget() const { return Target; }
152  ASTContext &getContext() const { return Context; }
153  const ABIInfo &getABIInfo() const { return TheABIInfo; }
154  llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
155
156  /// ConvertType - Convert type T into a llvm::Type.
157  const llvm::Type *ConvertType(QualType T);
158  const llvm::Type *ConvertTypeRecursive(QualType T);
159
160  /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
161  /// ConvertType in that it is used to convert to the memory representation for
162  /// a type.  For example, the scalar representation for _Bool is i1, but the
163  /// memory representation is usually i8 or i32, depending on the target.
164  const llvm::Type *ConvertTypeForMem(QualType T);
165  const llvm::Type *ConvertTypeForMemRecursive(QualType T);
166
167  /// GetFunctionType - Get the LLVM function type for \arg Info.
168  const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
169                                            bool IsVariadic);
170
171
172  /// GetFunctionTypeForVtable - Get the LLVM function type for use in a vtable,
173  /// given a CXXMethodDecl. If the method to has an incomplete return type,
174  /// and/or incomplete argument types, this will return the opaque type.
175  const llvm::Type *GetFunctionTypeForVtable(const CXXMethodDecl *MD);
176
177  const CGRecordLayout &getCGRecordLayout(const TagDecl*) const;
178
179  /// getLLVMFieldNo - Return llvm::StructType element number
180  /// that corresponds to the field FD.
181  unsigned getLLVMFieldNo(const FieldDecl *FD);
182
183  /// UpdateCompletedType - When we find the full definition for a TagDecl,
184  /// replace the 'opaque' type we previously made for it if applicable.
185  void UpdateCompletedType(const TagDecl *TD);
186
187private:
188  const CGFunctionInfo &getFunctionInfo(const FunctionNoProtoType *FTNP);
189  const CGFunctionInfo &getFunctionInfo(const FunctionProtoType *FTP);
190
191public:
192  /// getFunctionInfo - Get the function info for the specified function decl.
193  const CGFunctionInfo &getFunctionInfo(GlobalDecl GD);
194
195  const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
196  const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
197  const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
198  const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D,
199                                        CXXCtorType Type);
200  const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D,
201                                        CXXDtorType Type);
202
203  const CGFunctionInfo &getFunctionInfo(const CallArgList &Args,
204                                        const FunctionType *Ty) {
205    return getFunctionInfo(Ty->getResultType(), Args,
206                           Ty->getCallConv(), Ty->getNoReturnAttr());
207  }
208
209  // getFunctionInfo - Get the function info for a member function.
210  const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
211                                        const FunctionProtoType *FTP);
212
213  /// getFunctionInfo - Get the function info for a function described by a
214  /// return type and argument types. If the calling convention is not
215  /// specified, the "C" calling convention will be used.
216  const CGFunctionInfo &getFunctionInfo(QualType ResTy,
217                                        const CallArgList &Args,
218                                        CallingConv CC,
219                                        bool NoReturn);
220  const CGFunctionInfo &getFunctionInfo(QualType ResTy,
221                                        const FunctionArgList &Args,
222                                        CallingConv CC,
223                                        bool NoReturn);
224  const CGFunctionInfo &getFunctionInfo(QualType RetTy,
225                                  const llvm::SmallVector<QualType, 16> &ArgTys,
226                                        CallingConv CC,
227                                        bool NoReturn);
228
229public:  // These are internal details of CGT that shouldn't be used externally.
230  /// addFieldInfo - Assign field number to field FD.
231  void addFieldInfo(const FieldDecl *FD, unsigned FieldNo);
232
233  /// addBitFieldInfo - Assign a start bit and a size to field FD.
234  void addBitFieldInfo(const FieldDecl *FD, unsigned FieldNo,
235                       unsigned Start, unsigned Size);
236
237  /// getBitFieldInfo - Return the BitFieldInfo  that corresponds to the field
238  /// FD.
239  BitFieldInfo getBitFieldInfo(const FieldDecl *FD);
240
241  /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
242  /// enum.
243  const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
244
245  /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
246  /// argument types it would be passed as on the provided vector \arg
247  /// ArgTys. See ABIArgInfo::Expand.
248  void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys);
249};
250
251}  // end namespace CodeGen
252}  // end namespace clang
253
254#endif
255