]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/llvm/tools/clang/lib/CodeGen/CodeGenTypes.h
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / llvm / tools / clang / 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 "CGCall.h"
18 #include "clang/AST/GlobalDecl.h"
19 #include "clang/CodeGen/CGFunctionInfo.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/IR/Module.h"
22 #include <vector>
23
24 namespace llvm {
25   class FunctionType;
26   class Module;
27   class DataLayout;
28   class Type;
29   class LLVMContext;
30   class StructType;
31 }
32
33 namespace clang {
34   class ABIInfo;
35   class ASTContext;
36   template <typename> class CanQual;
37   class CXXConstructorDecl;
38   class CXXDestructorDecl;
39   class CXXMethodDecl;
40   class CodeGenOptions;
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 CGCXXABI;
55   class CGRecordLayout;
56   class CodeGenModule;
57   class RequiredArgs;
58
59 /// CodeGenTypes - This class organizes the cross-module state that is used
60 /// while lowering AST types to LLVM types.
61 class CodeGenTypes {
62   CodeGenModule &CGM;
63   // Some of this stuff should probably be left on the CGM.
64   ASTContext &Context;
65   llvm::Module &TheModule;
66   const llvm::DataLayout &TheDataLayout;
67   const TargetInfo &Target;
68   CGCXXABI &TheCXXABI;
69
70   // This should not be moved earlier, since its initialization depends on some
71   // of the previous reference members being already initialized
72   const ABIInfo &TheABIInfo;
73
74   /// The opaque type map for Objective-C interfaces. All direct
75   /// manipulation is done by the runtime interfaces, which are
76   /// responsible for coercing to the appropriate type; these opaque
77   /// types are never refined.
78   llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
79
80   /// CGRecordLayouts - This maps llvm struct type with corresponding
81   /// record layout info.
82   llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
83
84   /// RecordDeclTypes - This contains the LLVM IR type for any converted
85   /// RecordDecl.
86   llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
87   
88   /// FunctionInfos - Hold memoized CGFunctionInfo results.
89   llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
90
91   /// RecordsBeingLaidOut - This set keeps track of records that we're currently
92   /// converting to an IR type.  For example, when converting:
93   /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
94   /// types will be in this set.
95   llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut;
96   
97   llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
98   
99   /// SkippedLayout - True if we didn't layout a function due to a being inside
100   /// a recursive struct conversion, set this to true.
101   bool SkippedLayout;
102
103   SmallVector<const RecordDecl *, 8> DeferredRecords;
104   
105 private:
106   /// TypeCache - This map keeps cache of llvm::Types
107   /// and maps llvm::Types to corresponding clang::Type.
108   llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
109
110 public:
111   CodeGenTypes(CodeGenModule &cgm);
112   ~CodeGenTypes();
113
114   const llvm::DataLayout &getDataLayout() const { return TheDataLayout; }
115   ASTContext &getContext() const { return Context; }
116   const ABIInfo &getABIInfo() const { return TheABIInfo; }
117   const TargetInfo &getTarget() const { return Target; }
118   CGCXXABI &getCXXABI() const { return TheCXXABI; }
119   llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
120
121   /// ConvertType - Convert type T into a llvm::Type.
122   llvm::Type *ConvertType(QualType T);
123
124   /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
125   /// ConvertType in that it is used to convert to the memory representation for
126   /// a type.  For example, the scalar representation for _Bool is i1, but the
127   /// memory representation is usually i8 or i32, depending on the target.
128   llvm::Type *ConvertTypeForMem(QualType T);
129
130   /// GetFunctionType - Get the LLVM function type for \arg Info.
131   llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
132
133   llvm::FunctionType *GetFunctionType(GlobalDecl GD);
134
135   /// isFuncTypeConvertible - Utility to check whether a function type can
136   /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
137   /// type).
138   bool isFuncTypeConvertible(const FunctionType *FT);
139   bool isFuncTypeArgumentConvertible(QualType Ty);
140   
141   /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
142   /// given a CXXMethodDecl. If the method to has an incomplete return type,
143   /// and/or incomplete argument types, this will return the opaque type.
144   llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
145
146   const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
147
148   /// UpdateCompletedType - When we find the full definition for a TagDecl,
149   /// replace the 'opaque' type we previously made for it if applicable.
150   void UpdateCompletedType(const TagDecl *TD);
151
152   /// getNullaryFunctionInfo - Get the function info for a void()
153   /// function with standard CC.
154   const CGFunctionInfo &arrangeNullaryFunction();
155
156   // The arrangement methods are split into three families:
157   //   - those meant to drive the signature and prologue/epilogue
158   //     of a function declaration or definition,
159   //   - those meant for the computation of the LLVM type for an abstract
160   //     appearance of a function, and
161   //   - those meant for performing the IR-generation of a call.
162   // They differ mainly in how they deal with optional (i.e. variadic)
163   // arguments, as well as unprototyped functions.
164   //
165   // Key points:
166   // - The CGFunctionInfo for emitting a specific call site must include
167   //   entries for the optional arguments.
168   // - The function type used at the call site must reflect the formal
169   //   signature of the declaration being called, or else the call will
170   //   go awry.
171   // - For the most part, unprototyped functions are called by casting to
172   //   a formal signature inferred from the specific argument types used
173   //   at the call-site.  However, some targets (e.g. x86-64) screw with
174   //   this for compatibility reasons.
175
176   const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
177   const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
178   const CGFunctionInfo &arrangeFunctionDeclaration(QualType ResTy,
179                                                    const FunctionArgList &Args,
180                                              const FunctionType::ExtInfo &Info,
181                                                    bool isVariadic);
182
183   const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
184   const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
185                                                         QualType receiverType);
186
187   const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
188   const CGFunctionInfo &arrangeCXXConstructorDeclaration(
189                                                     const CXXConstructorDecl *D,
190                                                     CXXCtorType Type);
191   const CGFunctionInfo &arrangeCXXDestructor(const CXXDestructorDecl *D,
192                                              CXXDtorType Type);
193
194   const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
195                                                 const FunctionType *Ty);
196   const CGFunctionInfo &arrangeFreeFunctionCall(QualType ResTy,
197                                                 const CallArgList &args,
198                                                 FunctionType::ExtInfo info,
199                                                 RequiredArgs required);
200   const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
201                                                  const FunctionType *type);
202
203   const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
204                                              const FunctionProtoType *type,
205                                              RequiredArgs required);
206
207   const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty);
208   const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
209   const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
210                                              const FunctionProtoType *FTP);
211
212   /// "Arrange" the LLVM information for a call or type with the given
213   /// signature.  This is largely an internal method; other clients
214   /// should use one of the above routines, which ultimately defer to
215   /// this.
216   ///
217   /// \param argTypes - must all actually be canonical as params
218   const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
219                                                 ArrayRef<CanQualType> argTypes,
220                                                 FunctionType::ExtInfo info,
221                                                 RequiredArgs args);
222
223   /// \brief Compute a new LLVM record layout object for the given record.
224   CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
225                                       llvm::StructType *Ty);
226
227   /// addRecordTypeName - Compute a name from the given record decl with an
228   /// optional suffix and name the given LLVM type using it.
229   void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
230                          StringRef suffix);
231   
232
233 public:  // These are internal details of CGT that shouldn't be used externally.
234   /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
235   llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
236
237   /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
238   /// argument types it would be passed as on the provided vector \arg
239   /// ArgTys. See ABIArgInfo::Expand.
240   void GetExpandedTypes(QualType type,
241                         SmallVectorImpl<llvm::Type*> &expanded);
242
243   /// IsZeroInitializable - Return whether a type can be
244   /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
245   bool isZeroInitializable(QualType T);
246
247   /// IsZeroInitializable - Return whether a record type can be
248   /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
249   bool isZeroInitializable(const CXXRecordDecl *RD);
250   
251   bool isRecordLayoutComplete(const Type *Ty) const;
252   bool noRecordsBeingLaidOut() const {
253     return RecordsBeingLaidOut.empty();
254   }
255   bool isRecordBeingLaidOut(const Type *Ty) const {
256     return RecordsBeingLaidOut.count(Ty);
257   }
258                             
259 };
260
261 }  // end namespace CodeGen
262 }  // end namespace clang
263
264 #endif