blob: 5796ab8fe5aa6b4cb82b382f6ed2d8c6d1538742 [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 ASTContext;
35template <typename> class CanQual;
36class CXXConstructorDecl;
37class CXXDestructorDecl;
38class CXXMethodDecl;
39class CodeGenOptions;
40class FieldDecl;
41class FunctionProtoType;
42class ObjCInterfaceDecl;
43class ObjCIvarDecl;
44class PointerType;
45class QualType;
46class RecordDecl;
47class TagDecl;
48class TargetInfo;
49class Type;
50typedef CanQual<Type> CanQualType;
Devang Patelb84a06e2007-10-23 02:10:49 +000051
Reid Spencer5f016e22007-07-11 17:01:13 +000052namespace CodeGen {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070053class ABIInfo;
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");
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070085 case Ctor_CopyingClosure:
86 case Ctor_DefaultClosure:
87 llvm_unreachable("not expecting a closure");
Stephen Hines176edba2014-12-01 14:53:08 -080088 }
89 llvm_unreachable("not a CXXCtorType");
90}
91
92inline CXXDtorType toCXXDtorType(StructorType T) {
93 switch (T) {
94 case StructorType::Complete:
95 return Dtor_Complete;
96 case StructorType::Base:
97 return Dtor_Base;
98 case StructorType::Deleting:
99 return Dtor_Deleting;
100 }
101 llvm_unreachable("not a StructorType");
102}
103
104inline StructorType getFromDtorType(CXXDtorType T) {
105 switch (T) {
106 case Dtor_Deleting:
107 return StructorType::Deleting;
108 case Dtor_Complete:
109 return StructorType::Complete;
110 case Dtor_Base:
111 return StructorType::Base;
112 case Dtor_Comdat:
113 llvm_unreachable("not expecting a COMDAT");
114 }
115 llvm_unreachable("not a CXXDtorType");
116}
Devang Patelb84a06e2007-10-23 02:10:49 +0000117
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700118/// This class organizes the cross-module state that is used while lowering
119/// AST types to LLVM types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000120class CodeGenTypes {
John McCall64aa4b32013-04-16 22:48:15 +0000121 CodeGenModule &CGM;
Mark Laceyc3f7fd62013-10-10 20:57:00 +0000122 // Some of this stuff should probably be left on the CGM.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000123 ASTContext &Context;
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000124 llvm::Module &TheModule;
John McCall64aa4b32013-04-16 22:48:15 +0000125 const TargetInfo &Target;
John McCallf16aa102010-08-22 21:01:12 +0000126 CGCXXABI &TheCXXABI;
John McCall64aa4b32013-04-16 22:48:15 +0000127
128 // This should not be moved earlier, since its initialization depends on some
129 // of the previous reference members being already initialized
130 const ABIInfo &TheABIInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Daniel Dunbar412f59b2009-04-22 10:28:39 +0000132 /// The opaque type map for Objective-C interfaces. All direct
133 /// manipulation is done by the runtime interfaces, which are
134 /// responsible for coercing to the appropriate type; these opaque
135 /// types are never refined.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000136 llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
Daniel Dunbar412f59b2009-04-22 10:28:39 +0000137
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700138 /// Maps clang struct type with corresponding record layout info.
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000139 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
Devang Patelb84a06e2007-10-23 02:10:49 +0000140
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700141 /// Contains the LLVM IR type for any converted RecordDecl.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000142 llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
143
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700144 /// Hold memoized CGFunctionInfo results.
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000145 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
146
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700147 /// This set keeps track of records that we're currently converting
148 /// to an IR type. For example, when converting:
Chris Lattner71305cc2011-07-15 05:16:14 +0000149 /// 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
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700155 /// True if we didn't layout a function due to a being inside
Chris Lattner71305cc2011-07-15 05:16:14 +0000156 /// 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
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700161 /// This map keeps cache of llvm::Types and maps clang::Type to
162 /// corresponding llvm::Type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000163 llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
Anders Carlssone9742b02011-04-17 21:36:59 +0000164
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700165 llvm::SmallSet<const Type *, 8> RecordsWithOpaqueMemberPointers;
166
167 unsigned ClangCallConvToLLVMCallConv(CallingConv CC);
168
Reid Spencer5f016e22007-07-11 17:01:13 +0000169public:
John McCall64aa4b32013-04-16 22:48:15 +0000170 CodeGenTypes(CodeGenModule &cgm);
Devang Patelb84a06e2007-10-23 02:10:49 +0000171 ~CodeGenTypes();
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800173 const llvm::DataLayout &getDataLayout() const {
174 return TheModule.getDataLayout();
175 }
Devang Patel86522b92007-10-29 20:50:19 +0000176 ASTContext &getContext() const { return Context; }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000177 const ABIInfo &getABIInfo() const { return TheABIInfo; }
John McCall64aa4b32013-04-16 22:48:15 +0000178 const TargetInfo &getTarget() const { return Target; }
John McCallf16aa102010-08-22 21:01:12 +0000179 CGCXXABI &getCXXABI() const { return TheCXXABI; }
Owen Anderson47a434f2009-08-05 23:18:46 +0000180 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
Devang Patel5825ac22007-10-25 21:40:12 +0000181
Mike Stump1eb44332009-09-09 15:08:12 +0000182 /// ConvertType - Convert type T into a llvm::Type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000183 llvm::Type *ConvertType(QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800185 /// \brief Converts the GlobalDecl into an llvm::Type. This should be used
186 /// when we know the target of the function we want to convert. This is
187 /// because some functions (explicitly, those with pass_object_size
188 /// parameters) may not have the same signature as their type portrays, and
189 /// can only be called directly.
190 llvm::Type *ConvertFunctionType(QualType FT,
191 const FunctionDecl *FD = nullptr);
192
Chris Lattner4581fff2008-02-06 05:21:55 +0000193 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
194 /// ConvertType in that it is used to convert to the memory representation for
195 /// a type. For example, the scalar representation for _Bool is i1, but the
196 /// memory representation is usually i8 or i32, depending on the target.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000197 llvm::Type *ConvertTypeForMem(QualType T);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000198
Daniel Dunbar36b5f5e2009-01-31 03:05:44 +0000199 /// GetFunctionType - Get the LLVM function type for \arg Info.
John McCallde5d3c72012-02-17 03:33:10 +0000200 llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000202 llvm::FunctionType *GetFunctionType(GlobalDecl GD);
John McCallc0bf4622010-02-23 00:48:20 +0000203
Chris Lattnerf742eb02011-07-10 00:18:59 +0000204 /// isFuncTypeConvertible - Utility to check whether a function type can
Eli Friedmanc00129a2010-05-30 06:03:20 +0000205 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
206 /// type).
Chris Lattnerf742eb02011-07-10 00:18:59 +0000207 bool isFuncTypeConvertible(const FunctionType *FT);
Stephen Hines651f13c2014-04-23 16:59:28 -0700208 bool isFuncParamTypeConvertible(QualType Ty);
209
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700210 /// Determine if a C++ inheriting constructor should have parameters matching
211 /// those of its inherited constructor.
212 bool inheritingCtorHasParams(const InheritedConstructor &Inherited,
213 CXXCtorType Type);
214
Anders Carlsson046c2942010-04-17 20:15:18 +0000215 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000216 /// given a CXXMethodDecl. If the method to has an incomplete return type,
Anders Carlssonecf282b2009-11-24 05:08:52 +0000217 /// and/or incomplete argument types, this will return the opaque type.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000218 llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000219
Anders Carlssonc8f01eb2010-11-24 19:51:04 +0000220 const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Chris Lattnerc5b88062008-02-06 05:08:19 +0000222 /// UpdateCompletedType - When we find the full definition for a TagDecl,
223 /// replace the 'opaque' type we previously made for it if applicable.
224 void UpdateCompletedType(const TagDecl *TD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000225
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700226 /// \brief Remove stale types from the type cache when an inheritance model
227 /// gets assigned to a class.
228 void RefreshTypeCacheForClass(const CXXRecordDecl *RD);
John McCalld26bc762011-03-09 04:27:21 +0000229
John McCallde5d3c72012-02-17 03:33:10 +0000230 // The arrangement methods are split into three families:
231 // - those meant to drive the signature and prologue/epilogue
232 // of a function declaration or definition,
233 // - those meant for the computation of the LLVM type for an abstract
234 // appearance of a function, and
235 // - those meant for performing the IR-generation of a call.
236 // They differ mainly in how they deal with optional (i.e. variadic)
237 // arguments, as well as unprototyped functions.
238 //
239 // Key points:
240 // - The CGFunctionInfo for emitting a specific call site must include
241 // entries for the optional arguments.
242 // - The function type used at the call site must reflect the formal
243 // signature of the declaration being called, or else the call will
244 // go awry.
245 // - For the most part, unprototyped functions are called by casting to
246 // a formal signature inferred from the specific argument types used
247 // at the call-site. However, some targets (e.g. x86-64) screw with
248 // this for compatibility reasons.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000249
John McCallde5d3c72012-02-17 03:33:10 +0000250 const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000251
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700252 /// Given a function info for a declaration, return the function info
253 /// for a call with the given arguments.
254 ///
255 /// Often this will be able to simply return the declaration info.
256 const CGFunctionInfo &arrangeCall(const CGFunctionInfo &declFI,
257 const CallArgList &args);
258
259 /// Free functions are functions that are compatible with an ordinary
260 /// C function pointer type.
261 const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
262 const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
263 const FunctionType *Ty,
264 bool ChainCall);
265 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty,
266 const FunctionDecl *FD);
267 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
268
269 /// A nullary function is a freestanding function of type 'void ()'.
270 /// This method works for both calls and declarations.
271 const CGFunctionInfo &arrangeNullaryFunction();
272
273 /// A builtin function is a freestanding function using the default
274 /// C conventions.
275 const CGFunctionInfo &
276 arrangeBuiltinFunctionDeclaration(QualType resultType,
277 const FunctionArgList &args);
278 const CGFunctionInfo &
279 arrangeBuiltinFunctionDeclaration(CanQualType resultType,
280 ArrayRef<CanQualType> argTypes);
281 const CGFunctionInfo &arrangeBuiltinFunctionCall(QualType resultType,
282 const CallArgList &args);
283
284 /// Objective-C methods are C functions with some implicit parameters.
John McCallde5d3c72012-02-17 03:33:10 +0000285 const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
286 const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
287 QualType receiverType);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700288 const CGFunctionInfo &arrangeUnprototypedObjCMessageSend(
289 QualType returnType,
290 const CallArgList &args);
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000291
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700292 /// Block invocation functions are C functions with an implicit parameter.
293 const CGFunctionInfo &arrangeBlockFunctionDeclaration(
294 const FunctionProtoType *type,
295 const FunctionArgList &args);
296 const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
297 const FunctionType *type);
298
299 /// C++ methods have some special rules and also have implicit parameters.
John McCallde5d3c72012-02-17 03:33:10 +0000300 const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
Stephen Hines176edba2014-12-01 14:53:08 -0800301 const CGFunctionInfo &arrangeCXXStructorDeclaration(const CXXMethodDecl *MD,
302 StructorType Type);
Stephen Hines651f13c2014-04-23 16:59:28 -0700303 const CGFunctionInfo &arrangeCXXConstructorCall(const CallArgList &Args,
304 const CXXConstructorDecl *D,
305 CXXCtorType CtorKind,
306 unsigned ExtraArgs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000307
John McCall0f3d0972012-07-07 06:41:13 +0000308 const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
309 const FunctionProtoType *type,
310 RequiredArgs required);
Stephen Hines176edba2014-12-01 14:53:08 -0800311 const CGFunctionInfo &arrangeMSMemberPointerThunk(const CXXMethodDecl *MD);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700312 const CGFunctionInfo &arrangeMSCtorClosure(const CXXConstructorDecl *CD,
313 CXXCtorType CT);
John McCallde5d3c72012-02-17 03:33:10 +0000314 const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800315 const FunctionProtoType *FTP,
316 const CXXMethodDecl *MD);
John McCallead608a2010-02-26 00:48:12 +0000317
John McCall0f3d0972012-07-07 06:41:13 +0000318 /// "Arrange" the LLVM information for a call or type with the given
319 /// signature. This is largely an internal method; other clients
320 /// should use one of the above routines, which ultimately defer to
321 /// this.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000322 ///
John McCallde5d3c72012-02-17 03:33:10 +0000323 /// \param argTypes - must all actually be canonical as params
John McCall0f3d0972012-07-07 06:41:13 +0000324 const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700325 bool instanceMethod,
326 bool chainCall,
John McCall0f3d0972012-07-07 06:41:13 +0000327 ArrayRef<CanQualType> argTypes,
328 FunctionType::ExtInfo info,
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700329 ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos,
John McCall0f3d0972012-07-07 06:41:13 +0000330 RequiredArgs args);
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Daniel Dunbar270e2032010-03-31 00:11:27 +0000332 /// \brief Compute a new LLVM record layout object for the given record.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000333 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
334 llvm::StructType *Ty);
335
336 /// addRecordTypeName - Compute a name from the given record decl with an
337 /// optional suffix and name the given LLVM type using it.
338 void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
Chris Lattner686775d2011-07-20 06:58:45 +0000339 StringRef suffix);
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000340
Daniel Dunbar270e2032010-03-31 00:11:27 +0000341
Chris Lattner19009e62008-01-09 18:47:25 +0000342public: // These are internal details of CGT that shouldn't be used externally.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000343 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
344 llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
Daniel Dunbar56273772008-09-17 00:51:38 +0000345
Stephen Hines176edba2014-12-01 14:53:08 -0800346 /// getExpandedTypes - Expand the type \arg Ty into the LLVM
347 /// argument types it would be passed as. See ABIArgInfo::Expand.
348 void getExpandedTypes(QualType Ty,
349 SmallVectorImpl<llvm::Type *>::iterator &TI);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000350
John McCallf16aa102010-08-22 21:01:12 +0000351 /// IsZeroInitializable - Return whether a type can be
352 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
353 bool isZeroInitializable(QualType T);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000354
John McCallf16aa102010-08-22 21:01:12 +0000355 /// IsZeroInitializable - Return whether a record type can be
356 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700357 bool isZeroInitializable(const RecordDecl *RD);
Chris Lattner71305cc2011-07-15 05:16:14 +0000358
359 bool isRecordLayoutComplete(const Type *Ty) const;
360 bool noRecordsBeingLaidOut() const {
361 return RecordsBeingLaidOut.empty();
362 }
363 bool isRecordBeingLaidOut(const Type *Ty) const {
364 return RecordsBeingLaidOut.count(Ty);
365 }
366
Reid Spencer5f016e22007-07-11 17:01:13 +0000367};
Chris Lattnera7674d82007-07-13 22:13:22 +0000368
Reid Spencer5f016e22007-07-11 17:01:13 +0000369} // end namespace CodeGen
370} // end namespace clang
371
372#endif