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
24namespace llvm {
25  class FunctionType;
26  class Module;
27  class DataLayout;
28  class Type;
29  class LLVMContext;
30  class StructType;
31}
32
33namespace 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
53namespace 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.
61class 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
105private:
106  /// TypeCache - This map keeps cache of llvm::Types
107  /// and maps clang::Type to corresponding llvm::Type.
108  llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
109
110public:
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 isFuncParamTypeConvertible(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 &
179  arrangeFreeFunctionDeclaration(QualType ResTy, 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 &arrangeCXXConstructorCall(const CallArgList &Args,
192                                                  const CXXConstructorDecl *D,
193                                                  CXXCtorType CtorKind,
194                                                  unsigned ExtraArgs);
195  const CGFunctionInfo &arrangeCXXDestructor(const CXXDestructorDecl *D,
196                                             CXXDtorType Type);
197
198  const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
199                                                const FunctionType *Ty);
200  const CGFunctionInfo &arrangeFreeFunctionCall(QualType ResTy,
201                                                const CallArgList &args,
202                                                FunctionType::ExtInfo info,
203                                                RequiredArgs required);
204  const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
205                                                 const FunctionType *type);
206
207  const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
208                                             const FunctionProtoType *type,
209                                             RequiredArgs required);
210
211  const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty);
212  const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
213  const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
214                                             const FunctionProtoType *FTP);
215
216  /// "Arrange" the LLVM information for a call or type with the given
217  /// signature.  This is largely an internal method; other clients
218  /// should use one of the above routines, which ultimately defer to
219  /// this.
220  ///
221  /// \param argTypes - must all actually be canonical as params
222  const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
223                                                bool IsInstanceMethod,
224                                                ArrayRef<CanQualType> argTypes,
225                                                FunctionType::ExtInfo info,
226                                                RequiredArgs args);
227
228  /// \brief Compute a new LLVM record layout object for the given record.
229  CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
230                                      llvm::StructType *Ty);
231
232  /// addRecordTypeName - Compute a name from the given record decl with an
233  /// optional suffix and name the given LLVM type using it.
234  void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
235                         StringRef suffix);
236
237
238public:  // These are internal details of CGT that shouldn't be used externally.
239  /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
240  llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
241
242  /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
243  /// argument types it would be passed as on the provided vector \arg
244  /// ArgTys. See ABIArgInfo::Expand.
245  void GetExpandedTypes(QualType type,
246                        SmallVectorImpl<llvm::Type*> &expanded);
247
248  /// IsZeroInitializable - Return whether a type can be
249  /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
250  bool isZeroInitializable(QualType T);
251
252  /// IsZeroInitializable - Return whether a record type can be
253  /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
254  bool isZeroInitializable(const CXXRecordDecl *RD);
255
256  bool isRecordLayoutComplete(const Type *Ty) const;
257  bool noRecordsBeingLaidOut() const {
258    return RecordsBeingLaidOut.empty();
259  }
260  bool isRecordBeingLaidOut(const Type *Ty) const {
261    return RecordsBeingLaidOut.count(Ty);
262  }
263
264};
265
266}  // end namespace CodeGen
267}  // end namespace clang
268
269#endif
270