blob: 51e0309d61a2896c1af6da3422b37253184e6a05 [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
Stephen Hines176edba2014-12-01 14:53:08 -080014#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H
15#define LLVM_CLANG_LIB_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"
Mark Laceycdd7a962013-10-30 23:40:28 +000019#include "clang/CodeGen/CGFunctionInfo.h"
Anders Carlssonc9e20912007-08-21 00:21:21 +000020#include "llvm/ADT/DenseMap.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000021#include "llvm/IR/Module.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include <vector>
23
24namespace llvm {
Stephen Hines176edba2014-12-01 14:53:08 -080025class FunctionType;
26class Module;
27class DataLayout;
28class Type;
29class LLVMContext;
30class StructType;
Reid Spencer5f016e22007-07-11 17:01:13 +000031}
32
33namespace clang {
Stephen Hines176edba2014-12-01 14:53:08 -080034class ABIInfo;
35class ASTContext;
36template <typename> class CanQual;
37class CXXConstructorDecl;
38class CXXDestructorDecl;
39class CXXMethodDecl;
40class CodeGenOptions;
41class FieldDecl;
42class FunctionProtoType;
43class ObjCInterfaceDecl;
44class ObjCIvarDecl;
45class PointerType;
46class QualType;
47class RecordDecl;
48class TagDecl;
49class TargetInfo;
50class Type;
51typedef CanQual<Type> CanQualType;
Devang Patelb84a06e2007-10-23 02:10:49 +000052
Reid Spencer5f016e22007-07-11 17:01:13 +000053namespace CodeGen {
Stephen Hines176edba2014-12-01 14:53:08 -080054class CGCXXABI;
55class CGRecordLayout;
56class CodeGenModule;
57class RequiredArgs;
58
59enum class StructorType {
60 Complete, // constructor or destructor
61 Base, // constructor or destructor
62 Deleting // destructor only
63};
64
65inline CXXCtorType toCXXCtorType(StructorType T) {
66 switch (T) {
67 case StructorType::Complete:
68 return Ctor_Complete;
69 case StructorType::Base:
70 return Ctor_Base;
71 case StructorType::Deleting:
72 llvm_unreachable("cannot have a deleting ctor");
73 }
74 llvm_unreachable("not a StructorType");
75}
76
77inline StructorType getFromCtorType(CXXCtorType T) {
78 switch (T) {
79 case Ctor_Complete:
80 return StructorType::Complete;
81 case Ctor_Base:
82 return StructorType::Base;
83 case Ctor_Comdat:
84 llvm_unreachable("not expecting a COMDAT");
85 }
86 llvm_unreachable("not a CXXCtorType");
87}
88
89inline CXXDtorType toCXXDtorType(StructorType T) {
90 switch (T) {
91 case StructorType::Complete:
92 return Dtor_Complete;
93 case StructorType::Base:
94 return Dtor_Base;
95 case StructorType::Deleting:
96 return Dtor_Deleting;
97 }
98 llvm_unreachable("not a StructorType");
99}
100
101inline StructorType getFromDtorType(CXXDtorType T) {
102 switch (T) {
103 case Dtor_Deleting:
104 return StructorType::Deleting;
105 case Dtor_Complete:
106 return StructorType::Complete;
107 case Dtor_Base:
108 return StructorType::Base;
109 case Dtor_Comdat:
110 llvm_unreachable("not expecting a COMDAT");
111 }
112 llvm_unreachable("not a CXXDtorType");
113}
Devang Patelb84a06e2007-10-23 02:10:49 +0000114
Reid Spencer5f016e22007-07-11 17:01:13 +0000115/// CodeGenTypes - This class organizes the cross-module state that is used
116/// while lowering AST types to LLVM types.
117class CodeGenTypes {
John McCall64aa4b32013-04-16 22:48:15 +0000118 CodeGenModule &CGM;
Mark Laceyc3f7fd62013-10-10 20:57:00 +0000119 // Some of this stuff should probably be left on the CGM.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000120 ASTContext &Context;
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000121 llvm::Module &TheModule;
Micah Villmow25a6a842012-10-08 16:25:52 +0000122 const llvm::DataLayout &TheDataLayout;
John McCall64aa4b32013-04-16 22:48:15 +0000123 const TargetInfo &Target;
John McCallf16aa102010-08-22 21:01:12 +0000124 CGCXXABI &TheCXXABI;
John McCall64aa4b32013-04-16 22:48:15 +0000125
126 // This should not be moved earlier, since its initialization depends on some
127 // of the previous reference members being already initialized
128 const ABIInfo &TheABIInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Daniel Dunbar412f59b2009-04-22 10:28:39 +0000130 /// The opaque type map for Objective-C interfaces. All direct
131 /// manipulation is done by the runtime interfaces, which are
132 /// responsible for coercing to the appropriate type; these opaque
133 /// types are never refined.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000134 llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
Daniel Dunbar412f59b2009-04-22 10:28:39 +0000135
Mike Stump1eb44332009-09-09 15:08:12 +0000136 /// CGRecordLayouts - This maps llvm struct type with corresponding
137 /// record layout info.
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000138 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
Devang Patelb84a06e2007-10-23 02:10:49 +0000139
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000140 /// RecordDeclTypes - This contains the LLVM IR type for any converted
141 /// RecordDecl.
142 llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
143
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000144 /// FunctionInfos - Hold memoized CGFunctionInfo results.
145 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
146
Chris Lattner71305cc2011-07-15 05:16:14 +0000147 /// RecordsBeingLaidOut - This set keeps track of records that we're currently
148 /// converting to an IR type. For example, when converting:
149 /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
150 /// types will be in this set.
151 llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut;
152
153 llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
154
155 /// SkippedLayout - True if we didn't layout a function due to a being inside
156 /// a recursive struct conversion, set this to true.
Chris Lattner57eb23f2011-07-10 05:39:13 +0000157 bool SkippedLayout;
158
Chris Lattner686775d2011-07-20 06:58:45 +0000159 SmallVector<const RecordDecl *, 8> DeferredRecords;
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000160
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000161private:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000162 /// TypeCache - This map keeps cache of llvm::Types
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700163 /// and maps clang::Type to corresponding llvm::Type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000164 llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
Anders Carlssone9742b02011-04-17 21:36:59 +0000165
Reid Spencer5f016e22007-07-11 17:01:13 +0000166public:
John McCall64aa4b32013-04-16 22:48:15 +0000167 CodeGenTypes(CodeGenModule &cgm);
Devang Patelb84a06e2007-10-23 02:10:49 +0000168 ~CodeGenTypes();
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Micah Villmow25a6a842012-10-08 16:25:52 +0000170 const llvm::DataLayout &getDataLayout() const { return TheDataLayout; }
Devang Patel86522b92007-10-29 20:50:19 +0000171 ASTContext &getContext() const { return Context; }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000172 const ABIInfo &getABIInfo() const { return TheABIInfo; }
John McCall64aa4b32013-04-16 22:48:15 +0000173 const TargetInfo &getTarget() const { return Target; }
John McCallf16aa102010-08-22 21:01:12 +0000174 CGCXXABI &getCXXABI() const { return TheCXXABI; }
Owen Anderson47a434f2009-08-05 23:18:46 +0000175 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
Devang Patel5825ac22007-10-25 21:40:12 +0000176
Mike Stump1eb44332009-09-09 15:08:12 +0000177 /// ConvertType - Convert type T into a llvm::Type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000178 llvm::Type *ConvertType(QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Chris Lattner4581fff2008-02-06 05:21:55 +0000180 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
181 /// ConvertType in that it is used to convert to the memory representation for
182 /// a type. For example, the scalar representation for _Bool is i1, but the
183 /// memory representation is usually i8 or i32, depending on the target.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000184 llvm::Type *ConvertTypeForMem(QualType T);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000185
Daniel Dunbar36b5f5e2009-01-31 03:05:44 +0000186 /// GetFunctionType - Get the LLVM function type for \arg Info.
John McCallde5d3c72012-02-17 03:33:10 +0000187 llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000189 llvm::FunctionType *GetFunctionType(GlobalDecl GD);
John McCallc0bf4622010-02-23 00:48:20 +0000190
Chris Lattnerf742eb02011-07-10 00:18:59 +0000191 /// isFuncTypeConvertible - Utility to check whether a function type can
Eli Friedmanc00129a2010-05-30 06:03:20 +0000192 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
193 /// type).
Chris Lattnerf742eb02011-07-10 00:18:59 +0000194 bool isFuncTypeConvertible(const FunctionType *FT);
Stephen Hines651f13c2014-04-23 16:59:28 -0700195 bool isFuncParamTypeConvertible(QualType Ty);
196
Anders Carlsson046c2942010-04-17 20:15:18 +0000197 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000198 /// given a CXXMethodDecl. If the method to has an incomplete return type,
Anders Carlssonecf282b2009-11-24 05:08:52 +0000199 /// and/or incomplete argument types, this will return the opaque type.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000200 llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000201
Anders Carlssonc8f01eb2010-11-24 19:51:04 +0000202 const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Chris Lattnerc5b88062008-02-06 05:08:19 +0000204 /// UpdateCompletedType - When we find the full definition for a TagDecl,
205 /// replace the 'opaque' type we previously made for it if applicable.
206 void UpdateCompletedType(const TagDecl *TD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000207
John McCalld26bc762011-03-09 04:27:21 +0000208 /// getNullaryFunctionInfo - Get the function info for a void()
209 /// function with standard CC.
John McCallde5d3c72012-02-17 03:33:10 +0000210 const CGFunctionInfo &arrangeNullaryFunction();
John McCalld26bc762011-03-09 04:27:21 +0000211
John McCallde5d3c72012-02-17 03:33:10 +0000212 // The arrangement methods are split into three families:
213 // - those meant to drive the signature and prologue/epilogue
214 // of a function declaration or definition,
215 // - those meant for the computation of the LLVM type for an abstract
216 // appearance of a function, and
217 // - those meant for performing the IR-generation of a call.
218 // They differ mainly in how they deal with optional (i.e. variadic)
219 // arguments, as well as unprototyped functions.
220 //
221 // Key points:
222 // - The CGFunctionInfo for emitting a specific call site must include
223 // entries for the optional arguments.
224 // - The function type used at the call site must reflect the formal
225 // signature of the declaration being called, or else the call will
226 // go awry.
227 // - For the most part, unprototyped functions are called by casting to
228 // a formal signature inferred from the specific argument types used
229 // at the call-site. However, some targets (e.g. x86-64) screw with
230 // this for compatibility reasons.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000231
John McCallde5d3c72012-02-17 03:33:10 +0000232 const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
233 const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
Stephen Hines651f13c2014-04-23 16:59:28 -0700234 const CGFunctionInfo &
235 arrangeFreeFunctionDeclaration(QualType ResTy, const FunctionArgList &Args,
236 const FunctionType::ExtInfo &Info,
237 bool isVariadic);
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000238
John McCallde5d3c72012-02-17 03:33:10 +0000239 const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
240 const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
241 QualType receiverType);
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000242
John McCallde5d3c72012-02-17 03:33:10 +0000243 const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
Stephen Hines176edba2014-12-01 14:53:08 -0800244 const CGFunctionInfo &arrangeCXXStructorDeclaration(const CXXMethodDecl *MD,
245 StructorType Type);
Stephen Hines651f13c2014-04-23 16:59:28 -0700246 const CGFunctionInfo &arrangeCXXConstructorCall(const CallArgList &Args,
247 const CXXConstructorDecl *D,
248 CXXCtorType CtorKind,
249 unsigned ExtraArgs);
John McCall0f3d0972012-07-07 06:41:13 +0000250 const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
251 const FunctionType *Ty);
252 const CGFunctionInfo &arrangeFreeFunctionCall(QualType ResTy,
253 const CallArgList &args,
254 FunctionType::ExtInfo info,
255 RequiredArgs required);
John McCalle56bb362012-12-07 07:03:17 +0000256 const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
257 const FunctionType *type);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000258
John McCall0f3d0972012-07-07 06:41:13 +0000259 const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
260 const FunctionProtoType *type,
261 RequiredArgs required);
Stephen Hines176edba2014-12-01 14:53:08 -0800262 const CGFunctionInfo &arrangeMSMemberPointerThunk(const CXXMethodDecl *MD);
John McCall0f3d0972012-07-07 06:41:13 +0000263
264 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty);
265 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
John McCallde5d3c72012-02-17 03:33:10 +0000266 const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
267 const FunctionProtoType *FTP);
John McCallead608a2010-02-26 00:48:12 +0000268
John McCall0f3d0972012-07-07 06:41:13 +0000269 /// "Arrange" the LLVM information for a call or type with the given
270 /// signature. This is largely an internal method; other clients
271 /// should use one of the above routines, which ultimately defer to
272 /// this.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000273 ///
John McCallde5d3c72012-02-17 03:33:10 +0000274 /// \param argTypes - must all actually be canonical as params
John McCall0f3d0972012-07-07 06:41:13 +0000275 const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
Stephen Hines651f13c2014-04-23 16:59:28 -0700276 bool IsInstanceMethod,
John McCall0f3d0972012-07-07 06:41:13 +0000277 ArrayRef<CanQualType> argTypes,
278 FunctionType::ExtInfo info,
279 RequiredArgs args);
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Daniel Dunbar270e2032010-03-31 00:11:27 +0000281 /// \brief Compute a new LLVM record layout object for the given record.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000282 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
283 llvm::StructType *Ty);
284
285 /// addRecordTypeName - Compute a name from the given record decl with an
286 /// optional suffix and name the given LLVM type using it.
287 void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
Chris Lattner686775d2011-07-20 06:58:45 +0000288 StringRef suffix);
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000289
Daniel Dunbar270e2032010-03-31 00:11:27 +0000290
Chris Lattner19009e62008-01-09 18:47:25 +0000291public: // These are internal details of CGT that shouldn't be used externally.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000292 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
293 llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
Daniel Dunbar56273772008-09-17 00:51:38 +0000294
Stephen Hines176edba2014-12-01 14:53:08 -0800295 /// getExpandedTypes - Expand the type \arg Ty into the LLVM
296 /// argument types it would be passed as. See ABIArgInfo::Expand.
297 void getExpandedTypes(QualType Ty,
298 SmallVectorImpl<llvm::Type *>::iterator &TI);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000299
John McCallf16aa102010-08-22 21:01:12 +0000300 /// IsZeroInitializable - Return whether a type can be
301 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
302 bool isZeroInitializable(QualType T);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000303
John McCallf16aa102010-08-22 21:01:12 +0000304 /// IsZeroInitializable - Return whether a record type can be
305 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
306 bool isZeroInitializable(const CXXRecordDecl *RD);
Chris Lattner71305cc2011-07-15 05:16:14 +0000307
308 bool isRecordLayoutComplete(const Type *Ty) const;
309 bool noRecordsBeingLaidOut() const {
310 return RecordsBeingLaidOut.empty();
311 }
312 bool isRecordBeingLaidOut(const Type *Ty) const {
313 return RecordsBeingLaidOut.count(Ty);
314 }
315
Reid Spencer5f016e22007-07-11 17:01:13 +0000316};
Chris Lattnera7674d82007-07-13 22:13:22 +0000317
Reid Spencer5f016e22007-07-11 17:01:13 +0000318} // end namespace CodeGen
319} // end namespace clang
320
321#endif