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