1193326Sed//===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10198092Srdivacky// This is the code that handles AST -> LLVM type lowering.
11193326Sed//
12193326Sed//===----------------------------------------------------------------------===//
13193326Sed
14193326Sed#ifndef CLANG_CODEGEN_CODEGENTYPES_H
15193326Sed#define CLANG_CODEGEN_CODEGENTYPES_H
16193326Sed
17212904Sdim#include "CGCall.h"
18224145Sdim#include "clang/AST/GlobalDecl.h"
19263508Sdim#include "clang/CodeGen/CGFunctionInfo.h"
20193326Sed#include "llvm/ADT/DenseMap.h"
21249423Sdim#include "llvm/IR/Module.h"
22193326Sed#include <vector>
23193326Sed
24193326Sednamespace llvm {
25193326Sed  class FunctionType;
26193326Sed  class Module;
27243830Sdim  class DataLayout;
28193326Sed  class Type;
29198092Srdivacky  class LLVMContext;
30224145Sdim  class StructType;
31193326Sed}
32193326Sed
33193326Sednamespace clang {
34193326Sed  class ABIInfo;
35193326Sed  class ASTContext;
36204643Srdivacky  template <typename> class CanQual;
37199990Srdivacky  class CXXConstructorDecl;
38199990Srdivacky  class CXXDestructorDecl;
39193326Sed  class CXXMethodDecl;
40224145Sdim  class CodeGenOptions;
41193326Sed  class FieldDecl;
42193326Sed  class FunctionProtoType;
43193326Sed  class ObjCInterfaceDecl;
44193326Sed  class ObjCIvarDecl;
45193326Sed  class PointerType;
46193326Sed  class QualType;
47193326Sed  class RecordDecl;
48193326Sed  class TagDecl;
49193326Sed  class TargetInfo;
50193326Sed  class Type;
51204643Srdivacky  typedef CanQual<Type> CanQualType;
52193326Sed
53193326Sednamespace CodeGen {
54212904Sdim  class CGCXXABI;
55206084Srdivacky  class CGRecordLayout;
56234353Sdim  class CodeGenModule;
57234353Sdim  class RequiredArgs;
58193326Sed
59193326Sed/// CodeGenTypes - This class organizes the cross-module state that is used
60193326Sed/// while lowering AST types to LLVM types.
61193326Sedclass CodeGenTypes {
62263508Sdim  CodeGenModule &CGM;
63234353Sdim  // Some of this stuff should probably be left on the CGM.
64193326Sed  ASTContext &Context;
65224145Sdim  llvm::Module &TheModule;
66243830Sdim  const llvm::DataLayout &TheDataLayout;
67251662Sdim  const TargetInfo &Target;
68212904Sdim  CGCXXABI &TheCXXABI;
69198092Srdivacky
70251662Sdim  // This should not be moved earlier, since its initialization depends on some
71251662Sdim  // of the previous reference members being already initialized
72251662Sdim  const ABIInfo &TheABIInfo;
73251662Sdim
74193326Sed  /// The opaque type map for Objective-C interfaces. All direct
75193326Sed  /// manipulation is done by the runtime interfaces, which are
76193326Sed  /// responsible for coercing to the appropriate type; these opaque
77193326Sed  /// types are never refined.
78224145Sdim  llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
79193326Sed
80198092Srdivacky  /// CGRecordLayouts - This maps llvm struct type with corresponding
81198092Srdivacky  /// record layout info.
82193326Sed  llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
83193326Sed
84224145Sdim  /// RecordDeclTypes - This contains the LLVM IR type for any converted
85224145Sdim  /// RecordDecl.
86224145Sdim  llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
87224145Sdim
88193326Sed  /// FunctionInfos - Hold memoized CGFunctionInfo results.
89193326Sed  llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
90193326Sed
91224145Sdim  /// RecordsBeingLaidOut - This set keeps track of records that we're currently
92224145Sdim  /// converting to an IR type.  For example, when converting:
93224145Sdim  /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
94224145Sdim  /// types will be in this set.
95224145Sdim  llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut;
96224145Sdim
97224145Sdim  llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
98224145Sdim
99224145Sdim  /// SkippedLayout - True if we didn't layout a function due to a being inside
100224145Sdim  /// a recursive struct conversion, set this to true.
101224145Sdim  bool SkippedLayout;
102224145Sdim
103226633Sdim  SmallVector<const RecordDecl *, 8> DeferredRecords;
104224145Sdim
105193326Sedprivate:
106224145Sdim  /// TypeCache - This map keeps cache of llvm::Types
107224145Sdim  /// and maps llvm::Types to corresponding clang::Type.
108224145Sdim  llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
109193326Sed
110193326Sedpublic:
111251662Sdim  CodeGenTypes(CodeGenModule &cgm);
112193326Sed  ~CodeGenTypes();
113198092Srdivacky
114243830Sdim  const llvm::DataLayout &getDataLayout() const { return TheDataLayout; }
115193326Sed  ASTContext &getContext() const { return Context; }
116202379Srdivacky  const ABIInfo &getABIInfo() const { return TheABIInfo; }
117251662Sdim  const TargetInfo &getTarget() const { return Target; }
118212904Sdim  CGCXXABI &getCXXABI() const { return TheCXXABI; }
119198092Srdivacky  llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
120193326Sed
121198092Srdivacky  /// ConvertType - Convert type T into a llvm::Type.
122224145Sdim  llvm::Type *ConvertType(QualType T);
123198092Srdivacky
124193326Sed  /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
125193326Sed  /// ConvertType in that it is used to convert to the memory representation for
126193326Sed  /// a type.  For example, the scalar representation for _Bool is i1, but the
127193326Sed  /// memory representation is usually i8 or i32, depending on the target.
128224145Sdim  llvm::Type *ConvertTypeForMem(QualType T);
129193326Sed
130193326Sed  /// GetFunctionType - Get the LLVM function type for \arg Info.
131234353Sdim  llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
132198092Srdivacky
133224145Sdim  llvm::FunctionType *GetFunctionType(GlobalDecl GD);
134199990Srdivacky
135224145Sdim  /// isFuncTypeConvertible - Utility to check whether a function type can
136210299Sed  /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
137210299Sed  /// type).
138224145Sdim  bool isFuncTypeConvertible(const FunctionType *FT);
139224145Sdim  bool isFuncTypeArgumentConvertible(QualType Ty);
140224145Sdim
141207619Srdivacky  /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
142218893Sdim  /// given a CXXMethodDecl. If the method to has an incomplete return type,
143199990Srdivacky  /// and/or incomplete argument types, this will return the opaque type.
144226633Sdim  llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
145198092Srdivacky
146218893Sdim  const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
147218893Sdim
148193326Sed  /// UpdateCompletedType - When we find the full definition for a TagDecl,
149193326Sed  /// replace the 'opaque' type we previously made for it if applicable.
150193326Sed  void UpdateCompletedType(const TagDecl *TD);
151193326Sed
152221345Sdim  /// getNullaryFunctionInfo - Get the function info for a void()
153221345Sdim  /// function with standard CC.
154234353Sdim  const CGFunctionInfo &arrangeNullaryFunction();
155221345Sdim
156234353Sdim  // The arrangement methods are split into three families:
157234353Sdim  //   - those meant to drive the signature and prologue/epilogue
158234353Sdim  //     of a function declaration or definition,
159234353Sdim  //   - those meant for the computation of the LLVM type for an abstract
160234353Sdim  //     appearance of a function, and
161234353Sdim  //   - those meant for performing the IR-generation of a call.
162234353Sdim  // They differ mainly in how they deal with optional (i.e. variadic)
163234353Sdim  // arguments, as well as unprototyped functions.
164234353Sdim  //
165234353Sdim  // Key points:
166234353Sdim  // - The CGFunctionInfo for emitting a specific call site must include
167234353Sdim  //   entries for the optional arguments.
168234353Sdim  // - The function type used at the call site must reflect the formal
169234353Sdim  //   signature of the declaration being called, or else the call will
170234353Sdim  //   go awry.
171234353Sdim  // - For the most part, unprototyped functions are called by casting to
172234353Sdim  //   a formal signature inferred from the specific argument types used
173234353Sdim  //   at the call-site.  However, some targets (e.g. x86-64) screw with
174234353Sdim  //   this for compatibility reasons.
175218893Sdim
176234353Sdim  const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
177234353Sdim  const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
178234353Sdim  const CGFunctionInfo &arrangeFunctionDeclaration(QualType ResTy,
179234353Sdim                                                   const FunctionArgList &Args,
180234353Sdim                                             const FunctionType::ExtInfo &Info,
181234353Sdim                                                   bool isVariadic);
182199990Srdivacky
183234353Sdim  const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
184234353Sdim  const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
185234353Sdim                                                        QualType receiverType);
186203955Srdivacky
187234353Sdim  const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
188234353Sdim  const CGFunctionInfo &arrangeCXXConstructorDeclaration(
189234353Sdim                                                    const CXXConstructorDecl *D,
190234353Sdim                                                    CXXCtorType Type);
191234353Sdim  const CGFunctionInfo &arrangeCXXDestructor(const CXXDestructorDecl *D,
192234353Sdim                                             CXXDtorType Type);
193210299Sed
194239462Sdim  const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
195239462Sdim                                                const FunctionType *Ty);
196239462Sdim  const CGFunctionInfo &arrangeFreeFunctionCall(QualType ResTy,
197239462Sdim                                                const CallArgList &args,
198239462Sdim                                                FunctionType::ExtInfo info,
199239462Sdim                                                RequiredArgs required);
200249423Sdim  const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
201249423Sdim                                                 const FunctionType *type);
202218893Sdim
203239462Sdim  const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
204239462Sdim                                             const FunctionProtoType *type,
205239462Sdim                                             RequiredArgs required);
206239462Sdim
207239462Sdim  const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty);
208239462Sdim  const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
209234353Sdim  const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
210234353Sdim                                             const FunctionProtoType *FTP);
211204643Srdivacky
212239462Sdim  /// "Arrange" the LLVM information for a call or type with the given
213239462Sdim  /// signature.  This is largely an internal method; other clients
214239462Sdim  /// should use one of the above routines, which ultimately defer to
215239462Sdim  /// this.
216218893Sdim  ///
217234353Sdim  /// \param argTypes - must all actually be canonical as params
218239462Sdim  const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
219239462Sdim                                                ArrayRef<CanQualType> argTypes,
220239462Sdim                                                FunctionType::ExtInfo info,
221239462Sdim                                                RequiredArgs args);
222198092Srdivacky
223206084Srdivacky  /// \brief Compute a new LLVM record layout object for the given record.
224224145Sdim  CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
225224145Sdim                                      llvm::StructType *Ty);
226206084Srdivacky
227224145Sdim  /// addRecordTypeName - Compute a name from the given record decl with an
228224145Sdim  /// optional suffix and name the given LLVM type using it.
229224145Sdim  void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
230226633Sdim                         StringRef suffix);
231224145Sdim
232224145Sdim
233193326Sedpublic:  // These are internal details of CGT that shouldn't be used externally.
234224145Sdim  /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
235224145Sdim  llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
236193326Sed
237193326Sed  /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
238193326Sed  /// argument types it would be passed as on the provided vector \arg
239193326Sed  /// ArgTys. See ABIArgInfo::Expand.
240223017Sdim  void GetExpandedTypes(QualType type,
241226633Sdim                        SmallVectorImpl<llvm::Type*> &expanded);
242218893Sdim
243212904Sdim  /// IsZeroInitializable - Return whether a type can be
244212904Sdim  /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
245212904Sdim  bool isZeroInitializable(QualType T);
246218893Sdim
247212904Sdim  /// IsZeroInitializable - Return whether a record type can be
248212904Sdim  /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
249212904Sdim  bool isZeroInitializable(const CXXRecordDecl *RD);
250224145Sdim
251224145Sdim  bool isRecordLayoutComplete(const Type *Ty) const;
252224145Sdim  bool noRecordsBeingLaidOut() const {
253224145Sdim    return RecordsBeingLaidOut.empty();
254224145Sdim  }
255224145Sdim  bool isRecordBeingLaidOut(const Type *Ty) const {
256224145Sdim    return RecordsBeingLaidOut.count(Ty);
257224145Sdim  }
258224145Sdim
259193326Sed};
260193326Sed
261193326Sed}  // end namespace CodeGen
262193326Sed}  // end namespace clang
263193326Sed
264193326Sed#endif
265