CodeGenTypes.h revision 223017
1155093Smarius//===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===//
2155093Smarius//
3155093Smarius//                     The LLVM Compiler Infrastructure
4155093Smarius//
5155093Smarius// This file is distributed under the University of Illinois Open Source
6155093Smarius// License. See LICENSE.TXT for details.
7155093Smarius//
8155093Smarius//===----------------------------------------------------------------------===//
9155093Smarius//
10155093Smarius// This is the code that handles AST -> LLVM type lowering.
11155093Smarius//
12155093Smarius//===----------------------------------------------------------------------===//
13155093Smarius
14155093Smarius#ifndef CLANG_CODEGEN_CODEGENTYPES_H
15155093Smarius#define CLANG_CODEGEN_CODEGENTYPES_H
16155093Smarius
17155093Smarius#include "CGCall.h"
18155093Smarius#include "GlobalDecl.h"
19155093Smarius#include "llvm/Module.h"
20155093Smarius#include "llvm/ADT/DenseMap.h"
21155093Smarius#include <vector>
22155093Smarius
23155093Smariusnamespace llvm {
24155093Smarius  class FunctionType;
25155093Smarius  class Module;
26155093Smarius  class OpaqueType;
27155093Smarius  class PATypeHolder;
28155093Smarius  class TargetData;
29155093Smarius  class Type;
30155093Smarius  class LLVMContext;
31155093Smarius}
32155093Smarius
33155093Smariusnamespace clang {
34155093Smarius  class ABIInfo;
35155093Smarius  class ASTContext;
36155093Smarius  template <typename> class CanQual;
37155093Smarius  class CXXConstructorDecl;
38155093Smarius  class CXXDestructorDecl;
39155093Smarius  class CXXMethodDecl;
40155093Smarius  class FieldDecl;
41155093Smarius  class FunctionProtoType;
42155093Smarius  class ObjCInterfaceDecl;
43155093Smarius  class ObjCIvarDecl;
44164933Smarius  class PointerType;
45164933Smarius  class QualType;
46155093Smarius  class RecordDecl;
47155093Smarius  class TagDecl;
48155093Smarius  class TargetInfo;
49155093Smarius  class Type;
50155093Smarius  typedef CanQual<Type> CanQualType;
51155093Smarius
52155093Smariusnamespace CodeGen {
53155093Smarius  class CGCXXABI;
54155093Smarius  class CGRecordLayout;
55155093Smarius
56155093Smarius/// CodeGenTypes - This class organizes the cross-module state that is used
57155093Smarius/// while lowering AST types to LLVM types.
58155093Smariusclass CodeGenTypes {
59155093Smarius  ASTContext &Context;
60155093Smarius  const TargetInfo &Target;
61155093Smarius  llvm::Module& TheModule;
62155093Smarius  const llvm::TargetData& TheTargetData;
63155093Smarius  const ABIInfo& TheABIInfo;
64155093Smarius  CGCXXABI &TheCXXABI;
65155093Smarius
66155093Smarius  llvm::SmallVector<std::pair<QualType,
67155093Smarius                              llvm::OpaqueType *>, 8>  PointersToResolve;
68155093Smarius
69155093Smarius  llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes;
70155093Smarius
71155093Smarius  llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes;
72155093Smarius
73155093Smarius  /// The opaque type map for Objective-C interfaces. All direct
74155093Smarius  /// manipulation is done by the runtime interfaces, which are
75155093Smarius  /// responsible for coercing to the appropriate type; these opaque
76155093Smarius  /// types are never refined.
77155093Smarius  llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes;
78155093Smarius
79155093Smarius  /// CGRecordLayouts - This maps llvm struct type with corresponding
80155093Smarius  /// record layout info.
81155093Smarius  llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
82155093Smarius
83155093Smarius  /// FunctionInfos - Hold memoized CGFunctionInfo results.
84155093Smarius  llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
85155093Smarius
86155093Smariusprivate:
87155093Smarius  /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
88158663Smarius  /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
89158663Smarius  /// used instead of llvm::Type because it allows us to bypass potential
90155093Smarius  /// dangling type pointers due to type refinement on llvm side.
91158663Smarius  llvm::DenseMap<const Type *, llvm::PATypeHolder> TypeCache;
92155093Smarius
93155093Smarius  /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
94155093Smarius  /// method directly because it does not do any type caching. This method
95155093Smarius  /// is available only for ConvertType(). CovertType() is preferred
96155093Smarius  /// interface to convert type T into a llvm::Type.
97155093Smarius  const llvm::Type *ConvertNewType(QualType T);
98155093Smarius
99155093Smarius  /// HandleLateResolvedPointers - For top-level ConvertType calls, this handles
100155093Smarius  /// pointers that are referenced but have not been converted yet.  This is
101155093Smarius  /// used to handle cyclic structures properly.
102155093Smarius  void HandleLateResolvedPointers();
103155093Smarius
104155093Smarius  /// addRecordTypeName - Compute a name from the given record decl with an
105155093Smarius  /// optional suffix and name the given LLVM type using it.
106155093Smarius  void addRecordTypeName(const RecordDecl *RD, const llvm::Type *Ty,
107155093Smarius                         llvm::StringRef suffix);
108155093Smarius
109155093Smariuspublic:
110155093Smarius  CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD,
111155093Smarius               const ABIInfo &Info, CGCXXABI &CXXABI);
112155093Smarius  ~CodeGenTypes();
113155093Smarius
114155093Smarius  const llvm::TargetData &getTargetData() const { return TheTargetData; }
115155093Smarius  const TargetInfo &getTarget() const { return Target; }
116155093Smarius  ASTContext &getContext() const { return Context; }
117155093Smarius  const ABIInfo &getABIInfo() const { return TheABIInfo; }
118155093Smarius  CGCXXABI &getCXXABI() const { return TheCXXABI; }
119155093Smarius  llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
120155093Smarius
121155093Smarius  /// ConvertType - Convert type T into a llvm::Type.
122155093Smarius  const llvm::Type *ConvertType(QualType T, bool IsRecursive = false);
123155093Smarius  const llvm::Type *ConvertTypeRecursive(QualType T);
124155093Smarius
125155093Smarius  /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
126155093Smarius  /// ConvertType in that it is used to convert to the memory representation for
127158663Smarius  /// a type.  For example, the scalar representation for _Bool is i1, but the
128158663Smarius  /// memory representation is usually i8 or i32, depending on the target.
129158663Smarius  const llvm::Type *ConvertTypeForMem(QualType T, bool IsRecursive = false);
130158663Smarius  const llvm::Type *ConvertTypeForMemRecursive(QualType T) {
131158663Smarius    return ConvertTypeForMem(T, true);
132155093Smarius  }
133155093Smarius
134155093Smarius  /// GetFunctionType - Get the LLVM function type for \arg Info.
135155093Smarius  const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
136155093Smarius                                            bool IsVariadic,
137155093Smarius                                            bool IsRecursive = false);
138155093Smarius
139155093Smarius  const llvm::FunctionType *GetFunctionType(GlobalDecl GD);
140155093Smarius
141155093Smarius  /// VerifyFuncTypeComplete - Utility to check whether a function type can
142155093Smarius  /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
143155093Smarius  /// type).
144158663Smarius  static const TagType *VerifyFuncTypeComplete(const Type* T);
145155093Smarius
146155093Smarius  /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
147155093Smarius  /// given a CXXMethodDecl. If the method to has an incomplete return type,
148155093Smarius  /// and/or incomplete argument types, this will return the opaque type.
149155093Smarius  const llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
150155093Smarius
151155093Smarius  const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
152155093Smarius
153155093Smarius  /// addBaseSubobjectTypeName - Add a type name for the base subobject of the
154155093Smarius  /// given record layout.
155155093Smarius  void addBaseSubobjectTypeName(const CXXRecordDecl *RD,
156155093Smarius                                const CGRecordLayout &layout);
157155093Smarius
158155093Smarius  /// UpdateCompletedType - When we find the full definition for a TagDecl,
159155093Smarius  /// replace the 'opaque' type we previously made for it if applicable.
160155093Smarius  void UpdateCompletedType(const TagDecl *TD);
161155093Smarius
162155093Smarius  /// getNullaryFunctionInfo - Get the function info for a void()
163155093Smarius  /// function with standard CC.
164155093Smarius  const CGFunctionInfo &getNullaryFunctionInfo();
165155093Smarius
166155093Smarius  /// getFunctionInfo - Get the function info for the specified function decl.
167158663Smarius  const CGFunctionInfo &getFunctionInfo(GlobalDecl GD);
168158663Smarius
169158663Smarius  const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
170158663Smarius  const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
171158663Smarius  const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
172158663Smarius  const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D,
173158663Smarius                                        CXXCtorType Type);
174158663Smarius  const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D,
175158663Smarius                                        CXXDtorType Type);
176158663Smarius
177158663Smarius  const CGFunctionInfo &getFunctionInfo(const CallArgList &Args,
178158663Smarius                                        const FunctionType *Ty) {
179158663Smarius    return getFunctionInfo(Ty->getResultType(), Args,
180158663Smarius                           Ty->getExtInfo());
181158663Smarius  }
182158663Smarius
183158663Smarius  const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty,
184158663Smarius                                        bool IsRecursive = false);
185158663Smarius  const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty,
186158663Smarius                                        bool IsRecursive = false);
187158663Smarius
188158663Smarius  /// getFunctionInfo - Get the function info for a member function of
189158663Smarius  /// the given type.  This is used for calls through member function
190158663Smarius  /// pointers.
191158663Smarius  const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
192158663Smarius                                        const FunctionProtoType *FTP);
193158663Smarius
194158663Smarius  /// getFunctionInfo - Get the function info for a function described by a
195158663Smarius  /// return type and argument types. If the calling convention is not
196158663Smarius  /// specified, the "C" calling convention will be used.
197158663Smarius  const CGFunctionInfo &getFunctionInfo(QualType ResTy,
198158663Smarius                                        const CallArgList &Args,
199158663Smarius                                        const FunctionType::ExtInfo &Info);
200158663Smarius  const CGFunctionInfo &getFunctionInfo(QualType ResTy,
201158663Smarius                                        const FunctionArgList &Args,
202158663Smarius                                        const FunctionType::ExtInfo &Info);
203158663Smarius
204158663Smarius  /// Retrieves the ABI information for the given function signature.
205158663Smarius  ///
206158663Smarius  /// \param ArgTys - must all actually be canonical as params
207158663Smarius  const CGFunctionInfo &getFunctionInfo(CanQualType RetTy,
208158663Smarius                               const llvm::SmallVectorImpl<CanQualType> &ArgTys,
209155093Smarius                                        const FunctionType::ExtInfo &Info,
210                                        bool IsRecursive = false);
211
212  /// \brief Compute a new LLVM record layout object for the given record.
213  CGRecordLayout *ComputeRecordLayout(const RecordDecl *D);
214
215public:  // These are internal details of CGT that shouldn't be used externally.
216  /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
217  /// enum.
218  const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
219
220  /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
221  /// argument types it would be passed as on the provided vector \arg
222  /// ArgTys. See ABIArgInfo::Expand.
223  void GetExpandedTypes(QualType type,
224                        llvm::SmallVectorImpl<const llvm::Type*> &expanded,
225                        bool isRecursive);
226
227  /// IsZeroInitializable - Return whether a type can be
228  /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
229  bool isZeroInitializable(QualType T);
230
231  /// IsZeroInitializable - Return whether a record type can be
232  /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
233  bool isZeroInitializable(const CXXRecordDecl *RD);
234};
235
236}  // end namespace CodeGen
237}  // end namespace clang
238
239#endif
240