CodeGenTypes.h revision 206084
1107120Sjulian//===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===//
2107120Sjulian//
3107120Sjulian//                     The LLVM Compiler Infrastructure
4107120Sjulian//
5107120Sjulian// This file is distributed under the University of Illinois Open Source
6107120Sjulian// License. See LICENSE.TXT for details.
7107120Sjulian//
8107120Sjulian//===----------------------------------------------------------------------===//
9107120Sjulian//
10107120Sjulian// This is the code that handles AST -> LLVM type lowering.
11107120Sjulian//
12107120Sjulian//===----------------------------------------------------------------------===//
13107120Sjulian
14107120Sjulian#ifndef CLANG_CODEGEN_CODEGENTYPES_H
15107120Sjulian#define CLANG_CODEGEN_CODEGENTYPES_H
16107120Sjulian
17107120Sjulian#include "llvm/Module.h"
18107120Sjulian#include "llvm/ADT/DenseMap.h"
19107120Sjulian#include <vector>
20107120Sjulian
21107120Sjulian#include "CGCall.h"
22107120Sjulian#include "GlobalDecl.h"
23107120Sjulian
24107120Sjuliannamespace llvm {
25107120Sjulian  class FunctionType;
26107120Sjulian  class Module;
27107120Sjulian  class OpaqueType;
28107120Sjulian  class PATypeHolder;
29107120Sjulian  class TargetData;
30107120Sjulian  class Type;
31107120Sjulian  class LLVMContext;
32107120Sjulian}
33107120Sjulian
34107120Sjuliannamespace clang {
35107120Sjulian  class ABIInfo;
36107120Sjulian  class ASTContext;
37107120Sjulian  template <typename> class CanQual;
38107120Sjulian  class CXXConstructorDecl;
39107120Sjulian  class CXXDestructorDecl;
40107120Sjulian  class CXXMethodDecl;
41107120Sjulian  class FieldDecl;
42107120Sjulian  class FunctionProtoType;
43107120Sjulian  class ObjCInterfaceDecl;
44107120Sjulian  class ObjCIvarDecl;
45107120Sjulian  class PointerType;
46107120Sjulian  class QualType;
47107120Sjulian  class RecordDecl;
48107120Sjulian  class TagDecl;
49107120Sjulian  class TargetInfo;
50107120Sjulian  class Type;
51107120Sjulian  typedef CanQual<Type> CanQualType;
52107120Sjulian
53107120Sjuliannamespace CodeGen {
54107120Sjulian  class CGRecordLayout;
55107120Sjulian  class CodeGenTypes;
56107120Sjulian
57107120Sjulian/// CodeGenTypes - This class organizes the cross-module state that is used
58107120Sjulian/// while lowering AST types to LLVM types.
59107120Sjulianclass CodeGenTypes {
60107120Sjulian  ASTContext &Context;
61107120Sjulian  const TargetInfo &Target;
62107120Sjulian  llvm::Module& TheModule;
63107120Sjulian  const llvm::TargetData& TheTargetData;
64107120Sjulian  const ABIInfo& TheABIInfo;
65107120Sjulian
66107120Sjulian  llvm::SmallVector<std::pair<QualType,
67107120Sjulian                              llvm::OpaqueType *>, 8>  PointersToResolve;
68107120Sjulian
69107120Sjulian  llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes;
70107120Sjulian
71107120Sjulian  llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes;
72107120Sjulian
73107120Sjulian  /// The opaque type map for Objective-C interfaces. All direct
74107120Sjulian  /// manipulation is done by the runtime interfaces, which are
75107120Sjulian  /// responsible for coercing to the appropriate type; these opaque
76107120Sjulian  /// types are never refined.
77107120Sjulian  llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes;
78107120Sjulian
79107120Sjulian  /// CGRecordLayouts - This maps llvm struct type with corresponding
80107120Sjulian  /// record layout info.
81107120Sjulian  llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
82107120Sjulian
83107120Sjulian  /// FunctionInfos - Hold memoized CGFunctionInfo results.
84107120Sjulian  llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
85107120Sjulian
86107120Sjulianprivate:
87107120Sjulian  /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
88107120Sjulian  /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
89107120Sjulian  /// used instead of llvm::Type because it allows us to bypass potential
90107120Sjulian  /// dangling type pointers due to type refinement on llvm side.
91107120Sjulian  llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache;
92107120Sjulian
93107120Sjulian  /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
94107120Sjulian  /// method directly because it does not do any type caching. This method
95107120Sjulian  /// is available only for ConvertType(). CovertType() is preferred
96107120Sjulian  /// interface to convert type T into a llvm::Type.
97107120Sjulian  const llvm::Type *ConvertNewType(QualType T);
98107120Sjulianpublic:
99107120Sjulian  CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD,
100107120Sjulian               const ABIInfo &Info);
101107120Sjulian  ~CodeGenTypes();
102107120Sjulian
103107120Sjulian  const llvm::TargetData &getTargetData() const { return TheTargetData; }
104107120Sjulian  const TargetInfo &getTarget() const { return Target; }
105107120Sjulian  ASTContext &getContext() const { return Context; }
106107120Sjulian  const ABIInfo &getABIInfo() const { return TheABIInfo; }
107107120Sjulian  llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
108107120Sjulian
109107120Sjulian  /// ConvertType - Convert type T into a llvm::Type.
110107120Sjulian  const llvm::Type *ConvertType(QualType T);
111107120Sjulian  const llvm::Type *ConvertTypeRecursive(QualType T);
112107120Sjulian
113107120Sjulian  /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
114107120Sjulian  /// ConvertType in that it is used to convert to the memory representation for
115107120Sjulian  /// a type.  For example, the scalar representation for _Bool is i1, but the
116107120Sjulian  /// memory representation is usually i8 or i32, depending on the target.
117107120Sjulian  const llvm::Type *ConvertTypeForMem(QualType T);
118107120Sjulian  const llvm::Type *ConvertTypeForMemRecursive(QualType T);
119107120Sjulian
120107120Sjulian  /// GetFunctionType - Get the LLVM function type for \arg Info.
121107120Sjulian  const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
122107120Sjulian                                            bool IsVariadic);
123107120Sjulian
124107120Sjulian  const llvm::FunctionType *GetFunctionType(GlobalDecl GD);
125107120Sjulian
126107120Sjulian
127107120Sjulian  /// GetFunctionTypeForVtable - Get the LLVM function type for use in a vtable,
128107120Sjulian  /// given a CXXMethodDecl. If the method to has an incomplete return type,
129107120Sjulian  /// and/or incomplete argument types, this will return the opaque type.
130107120Sjulian  const llvm::Type *GetFunctionTypeForVtable(const CXXMethodDecl *MD);
131107120Sjulian
132107120Sjulian  const CGRecordLayout &getCGRecordLayout(const RecordDecl*) const;
133107120Sjulian
134107120Sjulian  /// UpdateCompletedType - When we find the full definition for a TagDecl,
135107120Sjulian  /// replace the 'opaque' type we previously made for it if applicable.
136107120Sjulian  void UpdateCompletedType(const TagDecl *TD);
137107120Sjulian
138107120Sjulian  /// getFunctionInfo - Get the function info for the specified function decl.
139107120Sjulian  const CGFunctionInfo &getFunctionInfo(GlobalDecl GD);
140107120Sjulian
141107120Sjulian  const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
142107120Sjulian  const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
143107120Sjulian  const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
144107120Sjulian  const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D,
145107120Sjulian                                        CXXCtorType Type);
146107120Sjulian  const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D,
147107120Sjulian                                        CXXDtorType Type);
148107120Sjulian
149107120Sjulian  const CGFunctionInfo &getFunctionInfo(const CallArgList &Args,
150107120Sjulian                                        const FunctionType *Ty) {
151107120Sjulian    return getFunctionInfo(Ty->getResultType(), Args,
152107120Sjulian                           Ty->getExtInfo());
153107120Sjulian  }
154107120Sjulian  const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty);
155107120Sjulian  const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty);
156107120Sjulian
157107120Sjulian  // getFunctionInfo - Get the function info for a member function.
158107120Sjulian  const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
159107120Sjulian                                        const FunctionProtoType *FTP);
160107120Sjulian
161107120Sjulian  /// getFunctionInfo - Get the function info for a function described by a
162107120Sjulian  /// return type and argument types. If the calling convention is not
163107120Sjulian  /// specified, the "C" calling convention will be used.
164107120Sjulian  const CGFunctionInfo &getFunctionInfo(QualType ResTy,
165107120Sjulian                                        const CallArgList &Args,
166107120Sjulian                                        const FunctionType::ExtInfo &Info);
167107120Sjulian  const CGFunctionInfo &getFunctionInfo(QualType ResTy,
168107120Sjulian                                        const FunctionArgList &Args,
169107120Sjulian                                        const FunctionType::ExtInfo &Info);
170107120Sjulian
171107120Sjulian  /// Retrieves the ABI information for the given function signature.
172107120Sjulian  ///
173107120Sjulian  /// \param ArgTys - must all actually be canonical as params
174107120Sjulian  const CGFunctionInfo &getFunctionInfo(CanQualType RetTy,
175107120Sjulian                               const llvm::SmallVectorImpl<CanQualType> &ArgTys,
176107120Sjulian                                        const FunctionType::ExtInfo &Info);
177107120Sjulian
178107120Sjulian  /// \brief Compute a new LLVM record layout object for the given record.
179107120Sjulian  CGRecordLayout *ComputeRecordLayout(const RecordDecl *D);
180107120Sjulian
181107120Sjulianpublic:  // These are internal details of CGT that shouldn't be used externally.
182107120Sjulian  /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
183  /// enum.
184  const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
185
186  /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
187  /// argument types it would be passed as on the provided vector \arg
188  /// ArgTys. See ABIArgInfo::Expand.
189  void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys);
190};
191
192}  // end namespace CodeGen
193}  // end namespace clang
194
195#endif
196