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