CodeGenTypes.h revision ce2b41dab3378750a79db518ab3ba8a5c8f39457
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
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
65  llvm::SmallVector<std::pair<QualType,
66                              llvm::OpaqueType *>, 8>  PointersToResolve;
67
68  llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes;
69
70  llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes;
71
72  /// The opaque type map for Objective-C interfaces. All direct
73  /// manipulation is done by the runtime interfaces, which are
74  /// responsible for coercing to the appropriate type; these opaque
75  /// types are never refined.
76  llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes;
77
78  /// CGRecordLayouts - This maps llvm struct type with corresponding
79  /// record layout info.
80  llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
81
82  /// FunctionInfos - Hold memoized CGFunctionInfo results.
83  llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
84
85private:
86  /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
87  /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
88  /// used instead of llvm::Type because it allows us to bypass potential
89  /// dangling type pointers due to type refinement on llvm side.
90  llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache;
91
92  /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
93  /// method directly because it does not do any type caching. This method
94  /// is available only for ConvertType(). CovertType() is preferred
95  /// interface to convert type T into a llvm::Type.
96  const llvm::Type *ConvertNewType(QualType T);
97public:
98  CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD,
99               const ABIInfo &Info);
100  ~CodeGenTypes();
101
102  const llvm::TargetData &getTargetData() const { return TheTargetData; }
103  const TargetInfo &getTarget() const { return Target; }
104  ASTContext &getContext() const { return Context; }
105  const ABIInfo &getABIInfo() const { return TheABIInfo; }
106  llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
107
108  /// ConvertType - Convert type T into a llvm::Type.
109  const llvm::Type *ConvertType(QualType T);
110  const llvm::Type *ConvertTypeRecursive(QualType T);
111
112  /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
113  /// ConvertType in that it is used to convert to the memory representation for
114  /// a type.  For example, the scalar representation for _Bool is i1, but the
115  /// memory representation is usually i8 or i32, depending on the target.
116  const llvm::Type *ConvertTypeForMem(QualType T);
117  const llvm::Type *ConvertTypeForMemRecursive(QualType T);
118
119  /// GetFunctionType - Get the LLVM function type for \arg Info.
120  const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
121                                            bool IsVariadic);
122
123  const llvm::FunctionType *GetFunctionType(GlobalDecl GD);
124
125
126  /// GetFunctionTypeForVtable - Get the LLVM function type for use in a vtable,
127  /// given a CXXMethodDecl. If the method to has an incomplete return type,
128  /// and/or incomplete argument types, this will return the opaque type.
129  const llvm::Type *GetFunctionTypeForVtable(const CXXMethodDecl *MD);
130
131  const CGRecordLayout &getCGRecordLayout(const RecordDecl*) const;
132
133  /// UpdateCompletedType - When we find the full definition for a TagDecl,
134  /// replace the 'opaque' type we previously made for it if applicable.
135  void UpdateCompletedType(const TagDecl *TD);
136
137  /// getFunctionInfo - Get the function info for the specified function decl.
138  const CGFunctionInfo &getFunctionInfo(GlobalDecl GD);
139
140  const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
141  const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
142  const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
143  const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D,
144                                        CXXCtorType Type);
145  const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D,
146                                        CXXDtorType Type);
147
148  const CGFunctionInfo &getFunctionInfo(const CallArgList &Args,
149                                        const FunctionType *Ty) {
150    return getFunctionInfo(Ty->getResultType(), Args,
151                           Ty->getExtInfo());
152  }
153  const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty);
154  const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty);
155
156  // getFunctionInfo - Get the function info for a member function.
157  const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
158                                        const FunctionProtoType *FTP);
159
160  /// getFunctionInfo - Get the function info for a function described by a
161  /// return type and argument types. If the calling convention is not
162  /// specified, the "C" calling convention will be used.
163  const CGFunctionInfo &getFunctionInfo(QualType ResTy,
164                                        const CallArgList &Args,
165                                        const FunctionType::ExtInfo &Info);
166  const CGFunctionInfo &getFunctionInfo(QualType ResTy,
167                                        const FunctionArgList &Args,
168                                        const FunctionType::ExtInfo &Info);
169
170  /// Retrieves the ABI information for the given function signature.
171  ///
172  /// \param ArgTys - must all actually be canonical as params
173  const CGFunctionInfo &getFunctionInfo(CanQualType RetTy,
174                               const llvm::SmallVectorImpl<CanQualType> &ArgTys,
175                                        const FunctionType::ExtInfo &Info);
176
177  /// \brief Compute a new LLVM record layout object for the given record.
178  CGRecordLayout *ComputeRecordLayout(const RecordDecl *D);
179
180public:  // These are internal details of CGT that shouldn't be used externally.
181  /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
182  /// enum.
183  const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
184
185  /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
186  /// argument types it would be passed as on the provided vector \arg
187  /// ArgTys. See ABIArgInfo::Expand.
188  void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys);
189};
190
191}  // end namespace CodeGen
192}  // end namespace clang
193
194#endif
195