CodeGenTypes.h revision 6876fe615e16b0e76c7711e129e470305b7e9d41
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 "llvm/Module.h"
18#include "llvm/ADT/DenseMap.h"
19#include <vector>
20
21#include "CGCall.h"
22#include "GlobalDecl.h"
23
24namespace llvm {
25  class FunctionType;
26  class Module;
27  class OpaqueType;
28  class PATypeHolder;
29  class TargetData;
30  class Type;
31  class LLVMContext;
32}
33
34namespace clang {
35  class ABIInfo;
36  class ASTContext;
37  template <typename> class CanQual;
38  class CXXConstructorDecl;
39  class CXXDestructorDecl;
40  class CXXMethodDecl;
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 CGRecordLayout;
55  class CodeGenTypes;
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
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<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);
98public:
99  CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD,
100               const ABIInfo &Info);
101  ~CodeGenTypes();
102
103  const llvm::TargetData &getTargetData() const { return TheTargetData; }
104  const TargetInfo &getTarget() const { return Target; }
105  ASTContext &getContext() const { return Context; }
106  const ABIInfo &getABIInfo() const { return TheABIInfo; }
107  llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
108
109  /// ConvertType - Convert type T into a llvm::Type.
110  const llvm::Type *ConvertType(QualType T);
111  const llvm::Type *ConvertTypeRecursive(QualType T);
112
113  /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
114  /// ConvertType in that it is used to convert to the memory representation for
115  /// a type.  For example, the scalar representation for _Bool is i1, but the
116  /// memory representation is usually i8 or i32, depending on the target.
117  const llvm::Type *ConvertTypeForMem(QualType T);
118  const llvm::Type *ConvertTypeForMemRecursive(QualType T);
119
120  /// GetFunctionType - Get the LLVM function type for \arg Info.
121  const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
122                                            bool IsVariadic);
123
124  const llvm::FunctionType *GetFunctionType(GlobalDecl GD);
125
126
127  /// GetFunctionTypeForVtable - Get the LLVM function type for use in a vtable,
128  /// given a CXXMethodDecl. If the method to has an incomplete return type,
129  /// and/or incomplete argument types, this will return the opaque type.
130  const llvm::Type *GetFunctionTypeForVtable(const CXXMethodDecl *MD);
131
132  const CGRecordLayout &getCGRecordLayout(const RecordDecl*) const;
133
134  /// UpdateCompletedType - When we find the full definition for a TagDecl,
135  /// replace the 'opaque' type we previously made for it if applicable.
136  void UpdateCompletedType(const TagDecl *TD);
137
138  /// getFunctionInfo - Get the function info for the specified function decl.
139  const CGFunctionInfo &getFunctionInfo(GlobalDecl GD);
140
141  const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
142  const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
143  const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
144  const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D,
145                                        CXXCtorType Type);
146  const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D,
147                                        CXXDtorType Type);
148
149  const CGFunctionInfo &getFunctionInfo(const CallArgList &Args,
150                                        const FunctionType *Ty) {
151    return getFunctionInfo(Ty->getResultType(), Args,
152                           Ty->getExtInfo());
153  }
154  const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty);
155  const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty);
156
157  // getFunctionInfo - Get the function info for a member function.
158  const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
159                                        const FunctionProtoType *FTP);
160
161  /// getFunctionInfo - Get the function info for a function described by a
162  /// return type and argument types. If the calling convention is not
163  /// specified, the "C" calling convention will be used.
164  const CGFunctionInfo &getFunctionInfo(QualType ResTy,
165                                        const CallArgList &Args,
166                                        const FunctionType::ExtInfo &Info);
167  const CGFunctionInfo &getFunctionInfo(QualType ResTy,
168                                        const FunctionArgList &Args,
169                                        const FunctionType::ExtInfo &Info);
170
171  /// Retrieves the ABI information for the given function signature.
172  ///
173  /// \param ArgTys - must all actually be canonical as params
174  const CGFunctionInfo &getFunctionInfo(CanQualType RetTy,
175                               const llvm::SmallVectorImpl<CanQualType> &ArgTys,
176                                        const FunctionType::ExtInfo &Info);
177
178  /// \brief Compute a new LLVM record layout object for the given record.
179  CGRecordLayout *ComputeRecordLayout(const RecordDecl *D);
180
181public:  // These are internal details of CGT that shouldn't be used externally.
182  /// 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