CodeGenTypes.h revision 08d4792bb5d9e0e7f122c6273636807029c06aaa
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 "CGCall.h"
18#include "clang/AST/GlobalDecl.h"
19#include "llvm/Module.h"
20#include "llvm/ADT/DenseMap.h"
21#include <vector>
22
23namespace llvm {
24  class FunctionType;
25  class Module;
26  class OpaqueType;
27  class PATypeHolder;
28  class TargetData;
29  class Type;
30  class LLVMContext;
31}
32
33namespace clang {
34  class ABIInfo;
35  class ASTContext;
36  template <typename> class CanQual;
37  class CXXConstructorDecl;
38  class CXXDestructorDecl;
39  class CXXMethodDecl;
40  class CodeGenOptions;
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  typedef CanQual<Type> CanQualType;
52
53namespace CodeGen {
54  class CGCXXABI;
55  class CGRecordLayout;
56
57/// CodeGenTypes - This class organizes the cross-module state that is used
58/// while lowering AST types to LLVM types.
59class CodeGenTypes {
60  ASTContext &Context;
61  const TargetInfo &Target;
62  llvm::Module& TheModule;
63  const llvm::TargetData& TheTargetData;
64  const ABIInfo& TheABIInfo;
65  CGCXXABI &TheCXXABI;
66  const CodeGenOptions &CodeGenOpts;
67
68  llvm::SmallVector<std::pair<QualType,
69                              llvm::OpaqueType *>, 8>  PointersToResolve;
70
71  llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes;
72
73  llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes;
74
75  /// The opaque type map for Objective-C interfaces. All direct
76  /// manipulation is done by the runtime interfaces, which are
77  /// responsible for coercing to the appropriate type; these opaque
78  /// types are never refined.
79  llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes;
80
81  /// CGRecordLayouts - This maps llvm struct type with corresponding
82  /// record layout info.
83  llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
84
85  /// FunctionInfos - Hold memoized CGFunctionInfo results.
86  llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
87
88private:
89  /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
90  /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
91  /// used instead of llvm::Type because it allows us to bypass potential
92  /// dangling type pointers due to type refinement on llvm side.
93  llvm::DenseMap<const Type *, llvm::PATypeHolder> TypeCache;
94
95  /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
96  /// method directly because it does not do any type caching. This method
97  /// is available only for ConvertType(). CovertType() is preferred
98  /// interface to convert type T into a llvm::Type.
99  const llvm::Type *ConvertNewType(QualType T);
100
101  /// HandleLateResolvedPointers - For top-level ConvertType calls, this handles
102  /// pointers that are referenced but have not been converted yet.  This is
103  /// used to handle cyclic structures properly.
104  void HandleLateResolvedPointers();
105
106  /// addRecordTypeName - Compute a name from the given record decl with an
107  /// optional suffix and name the given LLVM type using it.
108  void addRecordTypeName(const RecordDecl *RD, const llvm::Type *Ty,
109                         llvm::StringRef suffix);
110
111public:
112  CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD,
113               const ABIInfo &Info, CGCXXABI &CXXABI,
114               const CodeGenOptions &Opts);
115  ~CodeGenTypes();
116
117  const llvm::TargetData &getTargetData() const { return TheTargetData; }
118  const TargetInfo &getTarget() const { return Target; }
119  ASTContext &getContext() const { return Context; }
120  const ABIInfo &getABIInfo() const { return TheABIInfo; }
121  const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
122  CGCXXABI &getCXXABI() const { return TheCXXABI; }
123  llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
124
125  /// ConvertType - Convert type T into a llvm::Type.
126  const llvm::Type *ConvertType(QualType T, bool IsRecursive = false);
127  const llvm::Type *ConvertTypeRecursive(QualType T);
128
129  /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
130  /// ConvertType in that it is used to convert to the memory representation for
131  /// a type.  For example, the scalar representation for _Bool is i1, but the
132  /// memory representation is usually i8 or i32, depending on the target.
133  const llvm::Type *ConvertTypeForMem(QualType T, bool IsRecursive = false);
134  const llvm::Type *ConvertTypeForMemRecursive(QualType T) {
135    return ConvertTypeForMem(T, true);
136  }
137
138  /// GetFunctionType - Get the LLVM function type for \arg Info.
139  const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
140                                            bool IsVariadic,
141                                            bool IsRecursive = false);
142
143  const llvm::FunctionType *GetFunctionType(GlobalDecl GD);
144
145  /// VerifyFuncTypeComplete - Utility to check whether a function type can
146  /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
147  /// type).
148  static const TagType *VerifyFuncTypeComplete(const Type* T);
149
150  /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
151  /// given a CXXMethodDecl. If the method to has an incomplete return type,
152  /// and/or incomplete argument types, this will return the opaque type.
153  const llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
154
155  const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
156
157  /// addBaseSubobjectTypeName - Add a type name for the base subobject of the
158  /// given record layout.
159  void addBaseSubobjectTypeName(const CXXRecordDecl *RD,
160                                const CGRecordLayout &layout);
161
162  /// UpdateCompletedType - When we find the full definition for a TagDecl,
163  /// replace the 'opaque' type we previously made for it if applicable.
164  void UpdateCompletedType(const TagDecl *TD);
165
166  /// getNullaryFunctionInfo - Get the function info for a void()
167  /// function with standard CC.
168  const CGFunctionInfo &getNullaryFunctionInfo();
169
170  /// getFunctionInfo - Get the function info for the specified function decl.
171  const CGFunctionInfo &getFunctionInfo(GlobalDecl GD);
172
173  const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
174  const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
175  const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
176  const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D,
177                                        CXXCtorType Type);
178  const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D,
179                                        CXXDtorType Type);
180
181  const CGFunctionInfo &getFunctionInfo(const CallArgList &Args,
182                                        const FunctionType *Ty) {
183    return getFunctionInfo(Ty->getResultType(), Args,
184                           Ty->getExtInfo());
185  }
186
187  const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty,
188                                        bool IsRecursive = false);
189  const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty,
190                                        bool IsRecursive = false);
191
192  /// getFunctionInfo - Get the function info for a member function of
193  /// the given type.  This is used for calls through member function
194  /// pointers.
195  const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
196                                        const FunctionProtoType *FTP);
197
198  /// getFunctionInfo - Get the function info for a function described by a
199  /// return type and argument types. If the calling convention is not
200  /// specified, the "C" calling convention will be used.
201  const CGFunctionInfo &getFunctionInfo(QualType ResTy,
202                                        const CallArgList &Args,
203                                        const FunctionType::ExtInfo &Info);
204  const CGFunctionInfo &getFunctionInfo(QualType ResTy,
205                                        const FunctionArgList &Args,
206                                        const FunctionType::ExtInfo &Info);
207
208  /// Retrieves the ABI information for the given function signature.
209  ///
210  /// \param ArgTys - must all actually be canonical as params
211  const CGFunctionInfo &getFunctionInfo(CanQualType RetTy,
212                               const llvm::SmallVectorImpl<CanQualType> &ArgTys,
213                                        const FunctionType::ExtInfo &Info,
214                                        bool IsRecursive = false);
215
216  /// \brief Compute a new LLVM record layout object for the given record.
217  CGRecordLayout *ComputeRecordLayout(const RecordDecl *D);
218
219public:  // These are internal details of CGT that shouldn't be used externally.
220  /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
221  /// enum.
222  const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
223
224  /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
225  /// argument types it would be passed as on the provided vector \arg
226  /// ArgTys. See ABIArgInfo::Expand.
227  void GetExpandedTypes(QualType type,
228                        llvm::SmallVectorImpl<const llvm::Type*> &expanded,
229                        bool isRecursive);
230
231  /// IsZeroInitializable - Return whether a type can be
232  /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
233  bool isZeroInitializable(QualType T);
234
235  /// IsZeroInitializable - Return whether a record type can be
236  /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
237  bool isZeroInitializable(const CXXRecordDecl *RD);
238};
239
240}  // end namespace CodeGen
241}  // end namespace clang
242
243#endif
244