blob: 11fd76fb19a9488eb530627ebb4a84d4d8dd46e0 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Mike Stump1eb44332009-09-09 15:08:12 +000010// This is the code that handles AST -> LLVM type lowering.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattneref52a2f2008-02-29 17:10:38 +000014#ifndef CLANG_CODEGEN_CODEGENTYPES_H
15#define CLANG_CODEGEN_CODEGENTYPES_H
Reid Spencer5f016e22007-07-11 17:01:13 +000016
Chris Lattner800588f2010-07-29 06:26:06 +000017#include "CGCall.h"
Peter Collingbournefd05ca02011-06-14 04:02:39 +000018#include "clang/AST/GlobalDecl.h"
Anders Carlssonc9e20912007-08-21 00:21:21 +000019#include "llvm/ADT/DenseMap.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000020#include "llvm/IR/Module.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include <vector>
22
23namespace llvm {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000024 class FunctionType;
Anders Carlsson4e533282007-08-17 22:00:32 +000025 class Module;
Micah Villmow25a6a842012-10-08 16:25:52 +000026 class DataLayout;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000027 class Type;
Benjamin Kramerf21efe92009-08-11 17:46:57 +000028 class LLVMContext;
Chris Lattner9cbe4f02011-07-09 17:41:47 +000029 class StructType;
Reid Spencer5f016e22007-07-11 17:01:13 +000030}
31
32namespace clang {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000033 class ABIInfo;
Chris Lattnerd2d2a112007-07-14 01:29:45 +000034 class ASTContext;
John McCallead608a2010-02-26 00:48:12 +000035 template <typename> class CanQual;
Anders Carlssonf6c56e22009-11-25 03:15:49 +000036 class CXXConstructorDecl;
37 class CXXDestructorDecl;
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000038 class CXXMethodDecl;
Daniel Dunbar08d47922011-06-21 18:54:39 +000039 class CodeGenOptions;
Devang Patelb1e39892007-10-23 23:26:46 +000040 class FieldDecl;
Douglas Gregor72564e72009-02-26 23:50:07 +000041 class FunctionProtoType;
Chris Lattner391d77a2008-03-30 23:03:07 +000042 class ObjCInterfaceDecl;
43 class ObjCIvarDecl;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000044 class PointerType;
45 class QualType;
46 class RecordDecl;
47 class TagDecl;
48 class TargetInfo;
49 class Type;
John McCallead608a2010-02-26 00:48:12 +000050 typedef CanQual<Type> CanQualType;
Devang Patelb84a06e2007-10-23 02:10:49 +000051
Reid Spencer5f016e22007-07-11 17:01:13 +000052namespace CodeGen {
John McCallf16aa102010-08-22 21:01:12 +000053 class CGCXXABI;
Daniel Dunbar2924ade2010-03-30 22:26:10 +000054 class CGRecordLayout;
John McCallde5d3c72012-02-17 03:33:10 +000055 class CodeGenModule;
56 class RequiredArgs;
Devang Patelb84a06e2007-10-23 02:10:49 +000057
Reid Spencer5f016e22007-07-11 17:01:13 +000058/// CodeGenTypes - This class organizes the cross-module state that is used
59/// while lowering AST types to LLVM types.
60class CodeGenTypes {
John McCalle56bb362012-12-07 07:03:17 +000061public:
John McCallde5d3c72012-02-17 03:33:10 +000062 // Some of this stuff should probably be left on the CGM.
Chris Lattnerd2d2a112007-07-14 01:29:45 +000063 ASTContext &Context;
Daniel Dunbar444be732009-11-13 05:51:54 +000064 const TargetInfo &Target;
Chris Lattner9cbe4f02011-07-09 17:41:47 +000065 llvm::Module &TheModule;
Micah Villmow25a6a842012-10-08 16:25:52 +000066 const llvm::DataLayout &TheDataLayout;
Chris Lattner9cbe4f02011-07-09 17:41:47 +000067 const ABIInfo &TheABIInfo;
John McCallf16aa102010-08-22 21:01:12 +000068 CGCXXABI &TheCXXABI;
Daniel Dunbar08d47922011-06-21 18:54:39 +000069 const CodeGenOptions &CodeGenOpts;
John McCallde5d3c72012-02-17 03:33:10 +000070 CodeGenModule &CGM;
Mike Stump1eb44332009-09-09 15:08:12 +000071
John McCalle56bb362012-12-07 07:03:17 +000072private:
Daniel Dunbar412f59b2009-04-22 10:28:39 +000073 /// 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.
Chris Lattner9cbe4f02011-07-09 17:41:47 +000077 llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
Daniel Dunbar412f59b2009-04-22 10:28:39 +000078
Mike Stump1eb44332009-09-09 15:08:12 +000079 /// CGRecordLayouts - This maps llvm struct type with corresponding
80 /// record layout info.
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +000081 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
Devang Patelb84a06e2007-10-23 02:10:49 +000082
Chris Lattner9cbe4f02011-07-09 17:41:47 +000083 /// RecordDeclTypes - This contains the LLVM IR type for any converted
84 /// RecordDecl.
85 llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
86
Daniel Dunbar40a6be62009-02-03 00:07:12 +000087 /// FunctionInfos - Hold memoized CGFunctionInfo results.
88 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
89
Chris Lattner71305cc2011-07-15 05:16:14 +000090 /// 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.
Chris Lattner57eb23f2011-07-10 05:39:13 +0000100 bool SkippedLayout;
101
Chris Lattner686775d2011-07-20 06:58:45 +0000102 SmallVector<const RecordDecl *, 8> DeferredRecords;
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000103
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000104private:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000105 /// 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;
Anders Carlssone9742b02011-04-17 21:36:59 +0000108
Reid Spencer5f016e22007-07-11 17:01:13 +0000109public:
John McCallde5d3c72012-02-17 03:33:10 +0000110 CodeGenTypes(CodeGenModule &CGM);
Devang Patelb84a06e2007-10-23 02:10:49 +0000111 ~CodeGenTypes();
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Micah Villmow25a6a842012-10-08 16:25:52 +0000113 const llvm::DataLayout &getDataLayout() const { return TheDataLayout; }
Daniel Dunbar444be732009-11-13 05:51:54 +0000114 const TargetInfo &getTarget() const { return Target; }
Devang Patel86522b92007-10-29 20:50:19 +0000115 ASTContext &getContext() const { return Context; }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000116 const ABIInfo &getABIInfo() const { return TheABIInfo; }
Daniel Dunbar08d47922011-06-21 18:54:39 +0000117 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
John McCallf16aa102010-08-22 21:01:12 +0000118 CGCXXABI &getCXXABI() const { return TheCXXABI; }
Owen Anderson47a434f2009-08-05 23:18:46 +0000119 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
Devang Patel5825ac22007-10-25 21:40:12 +0000120
Mike Stump1eb44332009-09-09 15:08:12 +0000121 /// ConvertType - Convert type T into a llvm::Type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000122 llvm::Type *ConvertType(QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Chris Lattner4581fff2008-02-06 05:21:55 +0000124 /// 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.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000128 llvm::Type *ConvertTypeForMem(QualType T);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000129
Daniel Dunbar36b5f5e2009-01-31 03:05:44 +0000130 /// GetFunctionType - Get the LLVM function type for \arg Info.
John McCallde5d3c72012-02-17 03:33:10 +0000131 llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000133 llvm::FunctionType *GetFunctionType(GlobalDecl GD);
John McCallc0bf4622010-02-23 00:48:20 +0000134
Chris Lattnerf742eb02011-07-10 00:18:59 +0000135 /// isFuncTypeConvertible - Utility to check whether a function type can
Eli Friedmanc00129a2010-05-30 06:03:20 +0000136 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
137 /// type).
Chris Lattnerf742eb02011-07-10 00:18:59 +0000138 bool isFuncTypeConvertible(const FunctionType *FT);
139 bool isFuncTypeArgumentConvertible(QualType Ty);
140
Anders Carlsson046c2942010-04-17 20:15:18 +0000141 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000142 /// given a CXXMethodDecl. If the method to has an incomplete return type,
Anders Carlssonecf282b2009-11-24 05:08:52 +0000143 /// and/or incomplete argument types, this will return the opaque type.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000144 llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000145
Anders Carlssonc8f01eb2010-11-24 19:51:04 +0000146 const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Chris Lattnerc5b88062008-02-06 05:08:19 +0000148 /// 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);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000151
John McCalld26bc762011-03-09 04:27:21 +0000152 /// getNullaryFunctionInfo - Get the function info for a void()
153 /// function with standard CC.
John McCallde5d3c72012-02-17 03:33:10 +0000154 const CGFunctionInfo &arrangeNullaryFunction();
John McCalld26bc762011-03-09 04:27:21 +0000155
John McCallde5d3c72012-02-17 03:33:10 +0000156 // 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.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000175
John McCallde5d3c72012-02-17 03:33:10 +0000176 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);
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000182
John McCallde5d3c72012-02-17 03:33:10 +0000183 const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
184 const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
185 QualType receiverType);
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000186
John McCallde5d3c72012-02-17 03:33:10 +0000187 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);
John McCall04a67a62010-02-05 21:31:56 +0000193
John McCall0f3d0972012-07-07 06:41:13 +0000194 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);
John McCalle56bb362012-12-07 07:03:17 +0000200 const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
201 const FunctionType *type);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000202
John McCall0f3d0972012-07-07 06:41:13 +0000203 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);
John McCallde5d3c72012-02-17 03:33:10 +0000209 const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
210 const FunctionProtoType *FTP);
John McCallead608a2010-02-26 00:48:12 +0000211
John McCall0f3d0972012-07-07 06:41:13 +0000212 /// "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.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000216 ///
John McCallde5d3c72012-02-17 03:33:10 +0000217 /// \param argTypes - must all actually be canonical as params
John McCall0f3d0972012-07-07 06:41:13 +0000218 const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
219 ArrayRef<CanQualType> argTypes,
220 FunctionType::ExtInfo info,
221 RequiredArgs args);
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Daniel Dunbar270e2032010-03-31 00:11:27 +0000223 /// \brief Compute a new LLVM record layout object for the given record.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000224 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,
Chris Lattner686775d2011-07-20 06:58:45 +0000230 StringRef suffix);
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000231
Daniel Dunbar270e2032010-03-31 00:11:27 +0000232
Chris Lattner19009e62008-01-09 18:47:25 +0000233public: // These are internal details of CGT that shouldn't be used externally.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000234 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
235 llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
Daniel Dunbar56273772008-09-17 00:51:38 +0000236
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.
John McCall42e06112011-05-15 02:19:42 +0000240 void GetExpandedTypes(QualType type,
Chris Lattner686775d2011-07-20 06:58:45 +0000241 SmallVectorImpl<llvm::Type*> &expanded);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000242
John McCallf16aa102010-08-22 21:01:12 +0000243 /// IsZeroInitializable - Return whether a type can be
244 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
245 bool isZeroInitializable(QualType T);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000246
John McCallf16aa102010-08-22 21:01:12 +0000247 /// 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);
Chris Lattner71305cc2011-07-15 05:16:14 +0000250
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
Reid Spencer5f016e22007-07-11 17:01:13 +0000259};
Chris Lattnera7674d82007-07-13 22:13:22 +0000260
Reid Spencer5f016e22007-07-11 17:01:13 +0000261} // end namespace CodeGen
262} // end namespace clang
263
264#endif