CodeGenTypes.h revision 223017
1115420Shmp//===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===//
2115420Shmp//
3115420Shmp//                     The LLVM Compiler Infrastructure
4115420Shmp//
5115420Shmp// This file is distributed under the University of Illinois Open Source
6115420Shmp// License. See LICENSE.TXT for details.
7115420Shmp//
8115420Shmp//===----------------------------------------------------------------------===//
9115420Shmp//
10115420Shmp// This is the code that handles AST -> LLVM type lowering.
11115420Shmp//
12115420Shmp//===----------------------------------------------------------------------===//
13115420Shmp
14115420Shmp#ifndef CLANG_CODEGEN_CODEGENTYPES_H
15115420Shmp#define CLANG_CODEGEN_CODEGENTYPES_H
16115420Shmp
17115420Shmp#include "CGCall.h"
18115420Shmp#include "GlobalDecl.h"
19115420Shmp#include "llvm/Module.h"
20115420Shmp#include "llvm/ADT/DenseMap.h"
21115420Shmp#include <vector>
22115420Shmp
23115420Shmpnamespace llvm {
24115420Shmp  class FunctionType;
25115420Shmp  class Module;
26115420Shmp  class OpaqueType;
27115420Shmp  class PATypeHolder;
28115420Shmp  class TargetData;
29115420Shmp  class Type;
30115420Shmp  class LLVMContext;
31115420Shmp}
32115420Shmp
33115420Shmpnamespace clang {
34115420Shmp  class ABIInfo;
35115420Shmp  class ASTContext;
36115420Shmp  template <typename> class CanQual;
37115420Shmp  class CXXConstructorDecl;
38115420Shmp  class CXXDestructorDecl;
39115420Shmp  class CXXMethodDecl;
40115420Shmp  class FieldDecl;
41115420Shmp  class FunctionProtoType;
42115420Shmp  class ObjCInterfaceDecl;
43115420Shmp  class ObjCIvarDecl;
44115420Shmp  class PointerType;
45115420Shmp  class QualType;
46115420Shmp  class RecordDecl;
47115420Shmp  class TagDecl;
48115420Shmp  class TargetInfo;
49115420Shmp  class Type;
50115420Shmp  typedef CanQual<Type> CanQualType;
51115420Shmp
52115420Shmpnamespace CodeGen {
53115420Shmp  class CGCXXABI;
54115420Shmp  class CGRecordLayout;
55115420Shmp
56253434Sjimharris/// CodeGenTypes - This class organizes the cross-module state that is used
57115420Shmp/// while lowering AST types to LLVM types.
58115420Shmpclass CodeGenTypes {
59115420Shmp  ASTContext &Context;
60115420Shmp  const TargetInfo &Target;
61115420Shmp  llvm::Module& TheModule;
62115420Shmp  const llvm::TargetData& TheTargetData;
63115420Shmp  const ABIInfo& TheABIInfo;
64115420Shmp  CGCXXABI &TheCXXABI;
65115420Shmp
66253434Sjimharris  llvm::SmallVector<std::pair<QualType,
67253434Sjimharris                              llvm::OpaqueType *>, 8>  PointersToResolve;
68115420Shmp
69140317Sscottl  llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes;
70115420Shmp
71115420Shmp  llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes;
72115420Shmp
73115420Shmp  /// The opaque type map for Objective-C interfaces. All direct
74129231Shmp  /// manipulation is done by the runtime interfaces, which are
75115420Shmp  /// responsible for coercing to the appropriate type; these opaque
76115420Shmp  /// types are never refined.
77115420Shmp  llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes;
78115420Shmp
79115420Shmp  /// CGRecordLayouts - This maps llvm struct type with corresponding
80232359Sjhb  /// record layout info.
81115420Shmp  llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
82117190Sscottl
83117190Sscottl  /// FunctionInfos - Hold memoized CGFunctionInfo results.
84115420Shmp  llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
85115420Shmp
86115420Shmpprivate:
87115420Shmp  /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
88115420Shmp  /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
89115420Shmp  /// used instead of llvm::Type because it allows us to bypass potential
90115420Shmp  /// dangling type pointers due to type refinement on llvm side.
91115420Shmp  llvm::DenseMap<const Type *, llvm::PATypeHolder> TypeCache;
92115420Shmp
93115420Shmp  /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
94115420Shmp  /// method directly because it does not do any type caching. This method
95253434Sjimharris  /// is available only for ConvertType(). CovertType() is preferred
96253434Sjimharris  /// interface to convert type T into a llvm::Type.
97253434Sjimharris  const llvm::Type *ConvertNewType(QualType T);
98253434Sjimharris
99253434Sjimharris  /// HandleLateResolvedPointers - For top-level ConvertType calls, this handles
100253434Sjimharris  /// pointers that are referenced but have not been converted yet.  This is
101253434Sjimharris  /// used to handle cyclic structures properly.
102253434Sjimharris  void HandleLateResolvedPointers();
103115420Shmp
104115420Shmp  /// addRecordTypeName - Compute a name from the given record decl with an
105115420Shmp  /// optional suffix and name the given LLVM type using it.
106115420Shmp  void addRecordTypeName(const RecordDecl *RD, const llvm::Type *Ty,
107140317Sscottl                         llvm::StringRef suffix);
108140317Sscottl
109140317Sscottlpublic:
110115420Shmp  CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD,
111115420Shmp               const ABIInfo &Info, CGCXXABI &CXXABI);
112115420Shmp  ~CodeGenTypes();
113115420Shmp
114115420Shmp  const llvm::TargetData &getTargetData() const { return TheTargetData; }
115115420Shmp  const TargetInfo &getTarget() const { return Target; }
116115420Shmp  ASTContext &getContext() const { return Context; }
117115420Shmp  const ABIInfo &getABIInfo() const { return TheABIInfo; }
118134229Simp  CGCXXABI &getCXXABI() const { return TheCXXABI; }
119134229Simp  llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
120134229Simp
121115420Shmp  /// ConvertType - Convert type T into a llvm::Type.
122115420Shmp  const llvm::Type *ConvertType(QualType T, bool IsRecursive = false);
123115420Shmp  const llvm::Type *ConvertTypeRecursive(QualType T);
124115420Shmp
125115420Shmp  /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
126115420Shmp  /// ConvertType in that it is used to convert to the memory representation for
127115420Shmp  /// a type.  For example, the scalar representation for _Bool is i1, but the
128115420Shmp  /// memory representation is usually i8 or i32, depending on the target.
129115420Shmp  const llvm::Type *ConvertTypeForMem(QualType T, bool IsRecursive = false);
130115420Shmp  const llvm::Type *ConvertTypeForMemRecursive(QualType T) {
131115420Shmp    return ConvertTypeForMem(T, true);
132115420Shmp  }
133115420Shmp
134115420Shmp  /// GetFunctionType - Get the LLVM function type for \arg Info.
135115420Shmp  const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
136115420Shmp                                            bool IsVariadic,
137115420Shmp                                            bool IsRecursive = false);
138115420Shmp
139162404Sru  const llvm::FunctionType *GetFunctionType(GlobalDecl GD);
140115420Shmp
141115420Shmp  /// VerifyFuncTypeComplete - Utility to check whether a function type can
142115420Shmp  /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
143115420Shmp  /// type).
144115420Shmp  static const TagType *VerifyFuncTypeComplete(const Type* T);
145115420Shmp
146115420Shmp  /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
147115420Shmp  /// given a CXXMethodDecl. If the method to has an incomplete return type,
148115420Shmp  /// and/or incomplete argument types, this will return the opaque type.
149162404Sru  const llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
150115420Shmp
151115420Shmp  const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
152115420Shmp
153162404Sru  /// addBaseSubobjectTypeName - Add a type name for the base subobject of the
154115420Shmp  /// given record layout.
155141849Sru  void addBaseSubobjectTypeName(const CXXRecordDecl *RD,
156115420Shmp                                const CGRecordLayout &layout);
157115420Shmp
158115420Shmp  /// UpdateCompletedType - When we find the full definition for a TagDecl,
159167274Sjhb  /// replace the 'opaque' type we previously made for it if applicable.
160115420Shmp  void UpdateCompletedType(const TagDecl *TD);
161115420Shmp
162115420Shmp  /// getNullaryFunctionInfo - Get the function info for a void()
163115420Shmp  /// function with standard CC.
164115420Shmp  const CGFunctionInfo &getNullaryFunctionInfo();
165115420Shmp
166115420Shmp  /// getFunctionInfo - Get the function info for the specified function decl.
167115420Shmp  const CGFunctionInfo &getFunctionInfo(GlobalDecl GD);
168115420Shmp
169167274Sjhb  const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
170167274Sjhb  const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
171115420Shmp  const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
172115420Shmp  const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D,
173115420Shmp                                        CXXCtorType Type);
174167274Sjhb  const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D,
175115420Shmp                                        CXXDtorType Type);
176115420Shmp
177115420Shmp  const CGFunctionInfo &getFunctionInfo(const CallArgList &Args,
178115420Shmp                                        const FunctionType *Ty) {
179162404Sru    return getFunctionInfo(Ty->getResultType(), Args,
180115420Shmp                           Ty->getExtInfo());
181115420Shmp  }
182115420Shmp
183115420Shmp  const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty,
184115420Shmp                                        bool IsRecursive = false);
185115420Shmp  const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty,
186115420Shmp                                        bool IsRecursive = false);
187131530Sru
188115420Shmp  /// getFunctionInfo - Get the function info for a member function of
189115420Shmp  /// the given type.  This is used for calls through member function
190115420Shmp  /// pointers.
191153147Sscottl  const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
192153147Sscottl                                        const FunctionProtoType *FTP);
193153147Sscottl
194162404Sru  /// getFunctionInfo - Get the function info for a function described by a
195162404Sru  /// return type and argument types. If the calling convention is not
196162404Sru  /// specified, the "C" calling convention will be used.
197162404Sru  const CGFunctionInfo &getFunctionInfo(QualType ResTy,
198162404Sru                                        const CallArgList &Args,
199162404Sru                                        const FunctionType::ExtInfo &Info);
200115420Shmp  const CGFunctionInfo &getFunctionInfo(QualType ResTy,
201115420Shmp                                        const FunctionArgList &Args,
202115420Shmp                                        const FunctionType::ExtInfo &Info);
203115420Shmp
204115420Shmp  /// Retrieves the ABI information for the given function signature.
205253434Sjimharris  ///
206253434Sjimharris  /// \param ArgTys - must all actually be canonical as params
207253434Sjimharris  const CGFunctionInfo &getFunctionInfo(CanQualType RetTy,
208253434Sjimharris                               const llvm::SmallVectorImpl<CanQualType> &ArgTys,
209115420Shmp                                        const FunctionType::ExtInfo &Info,
210162404Sru                                        bool IsRecursive = false);
211115420Shmp
212115420Shmp  /// \brief Compute a new LLVM record layout object for the given record.
213115420Shmp  CGRecordLayout *ComputeRecordLayout(const RecordDecl *D);
214115420Shmp
215162404Srupublic:  // These are internal details of CGT that shouldn't be used externally.
216115420Shmp  /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
217115420Shmp  /// enum.
218115420Shmp  const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
219131530Sru
220115420Shmp  /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
221115420Shmp  /// argument types it would be passed as on the provided vector \arg
222115420Shmp  /// ArgTys. See ABIArgInfo::Expand.
223167274Sjhb  void GetExpandedTypes(QualType type,
224115420Shmp                        llvm::SmallVectorImpl<const llvm::Type*> &expanded,
225115420Shmp                        bool isRecursive);
226115420Shmp
227115420Shmp  /// IsZeroInitializable - Return whether a type can be
228115420Shmp  /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
229167274Sjhb  bool isZeroInitializable(QualType T);
230115420Shmp
231115420Shmp  /// IsZeroInitializable - Return whether a record type can be
232115420Shmp  /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
233115420Shmp  bool isZeroInitializable(const CXXRecordDecl *RD);
234115420Shmp};
235115420Shmp
236115420Shmp}  // end namespace CodeGen
237115420Shmp}  // end namespace clang
238131530Sru
239115420Shmp#endif
240162404Sru