]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/CodeGen/CodeGenTypes.h
Update clang to r108243.
[FreeBSD/FreeBSD.git] / lib / CodeGen / CodeGenTypes.h
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
24 namespace llvm {
25   class FunctionType;
26   class Module;
27   class OpaqueType;
28   class PATypeHolder;
29   class TargetData;
30   class Type;
31   class LLVMContext;
32 }
33
34 namespace 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
53 namespace 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.
58 class 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
85 private:
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);
97   
98   /// HandleLateResolvedPointers - For top-level ConvertType calls, this handles
99   /// pointers that are referenced but have not been converted yet.  This is
100   /// used to handle cyclic structures properly.
101   void HandleLateResolvedPointers();
102
103 public:
104   CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD,
105                const ABIInfo &Info);
106   ~CodeGenTypes();
107
108   const llvm::TargetData &getTargetData() const { return TheTargetData; }
109   const TargetInfo &getTarget() const { return Target; }
110   ASTContext &getContext() const { return Context; }
111   const ABIInfo &getABIInfo() const { return TheABIInfo; }
112   llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
113
114   /// ConvertType - Convert type T into a llvm::Type.
115   const llvm::Type *ConvertType(QualType T, bool IsRecursive = false);
116   const llvm::Type *ConvertTypeRecursive(QualType T);
117
118   /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
119   /// ConvertType in that it is used to convert to the memory representation for
120   /// a type.  For example, the scalar representation for _Bool is i1, but the
121   /// memory representation is usually i8 or i32, depending on the target.
122   const llvm::Type *ConvertTypeForMem(QualType T, bool IsRecursive = false);
123   const llvm::Type *ConvertTypeForMemRecursive(QualType T) {
124     return ConvertTypeForMem(T, true);
125   }
126
127   /// GetFunctionType - Get the LLVM function type for \arg Info.
128   const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
129                                             bool IsVariadic,
130                                             bool IsRecursive = false);
131
132   const llvm::FunctionType *GetFunctionType(GlobalDecl GD);
133
134   /// VerifyFuncTypeComplete - Utility to check whether a function type can
135   /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
136   /// type).
137   static const TagType *VerifyFuncTypeComplete(const Type* T);
138
139   /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
140   /// given a CXXMethodDecl. If the method to has an incomplete return type, 
141   /// and/or incomplete argument types, this will return the opaque type.
142   const llvm::Type *GetFunctionTypeForVTable(const CXXMethodDecl *MD);
143                                                      
144   const CGRecordLayout &getCGRecordLayout(const RecordDecl*) const;
145
146   /// UpdateCompletedType - When we find the full definition for a TagDecl,
147   /// replace the 'opaque' type we previously made for it if applicable.
148   void UpdateCompletedType(const TagDecl *TD);
149
150   /// getFunctionInfo - Get the function info for the specified function decl.
151   const CGFunctionInfo &getFunctionInfo(GlobalDecl GD);
152   
153   const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
154   const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
155   const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
156   const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D,
157                                         CXXCtorType Type);
158   const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D,
159                                         CXXDtorType Type);
160
161   const CGFunctionInfo &getFunctionInfo(const CallArgList &Args,
162                                         const FunctionType *Ty) {
163     return getFunctionInfo(Ty->getResultType(), Args,
164                            Ty->getExtInfo());
165   }
166
167   const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty,
168                                         bool IsRecursive = false);
169   const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty,
170                                         bool IsRecursive = false);
171
172   // getFunctionInfo - Get the function info for a member function.
173   const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
174                                         const FunctionProtoType *FTP);
175   
176   /// getFunctionInfo - Get the function info for a function described by a
177   /// return type and argument types. If the calling convention is not
178   /// specified, the "C" calling convention will be used.
179   const CGFunctionInfo &getFunctionInfo(QualType ResTy,
180                                         const CallArgList &Args,
181                                         const FunctionType::ExtInfo &Info);
182   const CGFunctionInfo &getFunctionInfo(QualType ResTy,
183                                         const FunctionArgList &Args,
184                                         const FunctionType::ExtInfo &Info);
185
186   /// Retrieves the ABI information for the given function signature.
187   /// 
188   /// \param ArgTys - must all actually be canonical as params
189   const CGFunctionInfo &getFunctionInfo(CanQualType RetTy,
190                                const llvm::SmallVectorImpl<CanQualType> &ArgTys,
191                                         const FunctionType::ExtInfo &Info,
192                                         bool IsRecursive = false);
193
194   /// \brief Compute a new LLVM record layout object for the given record.
195   CGRecordLayout *ComputeRecordLayout(const RecordDecl *D);
196
197 public:  // These are internal details of CGT that shouldn't be used externally.
198   /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
199   /// enum.
200   const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
201
202   /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
203   /// argument types it would be passed as on the provided vector \arg
204   /// ArgTys. See ABIArgInfo::Expand.
205   void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys,
206                         bool IsRecursive);
207   
208   /// ContainsPointerToDataMember - Return whether the given type contains a
209   /// pointer to a data member.
210   bool ContainsPointerToDataMember(QualType T);
211   
212   /// ContainsPointerToDataMember - Return whether the record decl contains a
213   /// pointer to a data member.
214   bool ContainsPointerToDataMember(const CXXRecordDecl *RD);
215 };
216
217 }  // end namespace CodeGen
218 }  // end namespace clang
219
220 #endif