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