blob: ebe55c70d817efcedee17d2744597d2283437d0b [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===//
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
14#include "CodeGenTypes.h"
John McCallf16aa102010-08-22 21:01:12 +000015#include "CGCXXABI.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "CGCall.h"
Guy Benyeib13621d2012-12-18 14:38:23 +000017#include "CGOpenCLRuntime.h"
Daniel Dunbar2924ade2010-03-30 22:26:10 +000018#include "CGRecordLayout.h"
John McCallde5d3c72012-02-17 03:33:10 +000019#include "TargetInfo.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000020#include "clang/AST/ASTContext.h"
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +000021#include "clang/AST/DeclCXX.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/AST/DeclObjC.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000023#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Mark Lacey8b549992013-10-30 21:53:58 +000025#include "clang/CodeGen/CGFunctionInfo.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000026#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/DerivedTypes.h"
28#include "llvm/IR/Module.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000029using namespace clang;
30using namespace CodeGen;
31
John McCall64aa4b32013-04-16 22:48:15 +000032CodeGenTypes::CodeGenTypes(CodeGenModule &cgm)
33 : CGM(cgm), Context(cgm.getContext()), TheModule(cgm.getModule()),
John McCall64aa4b32013-04-16 22:48:15 +000034 Target(cgm.getTarget()), TheCXXABI(cgm.getCXXABI()),
John McCall64aa4b32013-04-16 22:48:15 +000035 TheABIInfo(cgm.getTargetCodeGenInfo().getABIInfo()) {
Chris Lattner57eb23f2011-07-10 05:39:13 +000036 SkippedLayout = false;
Chris Lattnerd2d2a112007-07-14 01:29:45 +000037}
Reid Spencer5f016e22007-07-11 17:01:13 +000038
Devang Patelb84a06e2007-10-23 02:10:49 +000039CodeGenTypes::~CodeGenTypes() {
Stephen Hines651f13c2014-04-23 16:59:28 -070040 llvm::DeleteContainerSeconds(CGRecordLayouts);
Chris Lattner6f41c172010-01-11 19:58:10 +000041
42 for (llvm::FoldingSet<CGFunctionInfo>::iterator
43 I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
44 delete &*I++;
Devang Patelb84a06e2007-10-23 02:10:49 +000045}
46
Chris Lattner9cbe4f02011-07-09 17:41:47 +000047void CodeGenTypes::addRecordTypeName(const RecordDecl *RD,
48 llvm::StructType *Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +000049 StringRef suffix) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000050 SmallString<256> TypeName;
Anders Carlssone9742b02011-04-17 21:36:59 +000051 llvm::raw_svector_ostream OS(TypeName);
Anders Carlssone0047b12011-04-20 23:51:43 +000052 OS << RD->getKindName() << '.';
Anders Carlssone9742b02011-04-17 21:36:59 +000053
54 // Name the codegen type after the typedef name
55 // if there is no tag type name available
Anders Carlssone0047b12011-04-20 23:51:43 +000056 if (RD->getIdentifier()) {
Anders Carlssone9742b02011-04-17 21:36:59 +000057 // FIXME: We should not have to check for a null decl context here.
58 // Right now we do it because the implicit Obj-C decls don't have one.
Anders Carlssone0047b12011-04-20 23:51:43 +000059 if (RD->getDeclContext())
Benjamin Kramerb063ef02013-02-23 13:53:57 +000060 RD->printQualifiedName(OS);
Anders Carlssone9742b02011-04-17 21:36:59 +000061 else
Anders Carlssone0047b12011-04-20 23:51:43 +000062 RD->printName(OS);
63 } else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) {
Anders Carlssone9742b02011-04-17 21:36:59 +000064 // FIXME: We should not have to check for a null decl context here.
65 // Right now we do it because the implicit Obj-C decls don't have one.
66 if (TDD->getDeclContext())
Benjamin Kramerb063ef02013-02-23 13:53:57 +000067 TDD->printQualifiedName(OS);
Anders Carlssone9742b02011-04-17 21:36:59 +000068 else
69 TDD->printName(OS);
70 } else
71 OS << "anon";
72
73 if (!suffix.empty())
74 OS << suffix;
75
Chris Lattner9cbe4f02011-07-09 17:41:47 +000076 Ty->setName(OS.str());
Devang Patel30ec9972007-10-25 18:32:36 +000077}
78
Chris Lattner4581fff2008-02-06 05:21:55 +000079/// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
80/// ConvertType in that it is used to convert to the memory representation for
81/// a type. For example, the scalar representation for _Bool is i1, but the
82/// memory representation is usually i8 or i32, depending on the target.
Stephen Hines176edba2014-12-01 14:53:08 -080083llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000084 llvm::Type *R = ConvertType(T);
Mike Stump1eb44332009-09-09 15:08:12 +000085
Chris Lattner19009e62008-01-09 18:47:25 +000086 // If this is a non-bool type, don't map it.
Duncan Sandsf177d9d2010-02-15 16:14:01 +000087 if (!R->isIntegerTy(1))
Chris Lattner19009e62008-01-09 18:47:25 +000088 return R;
Mike Stump1eb44332009-09-09 15:08:12 +000089
Chris Lattner19009e62008-01-09 18:47:25 +000090 // Otherwise, return an integer of the target-specified size.
Owen Anderson0032b272009-08-13 21:57:51 +000091 return llvm::IntegerType::get(getLLVMContext(),
92 (unsigned)Context.getTypeSize(T));
Chris Lattner19009e62008-01-09 18:47:25 +000093}
94
Chris Lattner71305cc2011-07-15 05:16:14 +000095
96/// isRecordLayoutComplete - Return true if the specified type is already
97/// completely laid out.
98bool CodeGenTypes::isRecordLayoutComplete(const Type *Ty) const {
99 llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I =
100 RecordDeclTypes.find(Ty);
101 return I != RecordDeclTypes.end() && !I->second->isOpaque();
102}
103
104static bool
105isSafeToConvert(QualType T, CodeGenTypes &CGT,
106 llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked);
107
108
109/// isSafeToConvert - Return true if it is safe to convert the specified record
110/// decl to IR and lay it out, false if doing so would cause us to get into a
111/// recursive compilation mess.
112static bool
113isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT,
114 llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
115 // If we have already checked this type (maybe the same type is used by-value
116 // multiple times in multiple structure fields, don't check again.
Stephen Hines176edba2014-12-01 14:53:08 -0800117 if (!AlreadyChecked.insert(RD).second)
118 return true;
119
Chris Lattner71305cc2011-07-15 05:16:14 +0000120 const Type *Key = CGT.getContext().getTagDeclType(RD).getTypePtr();
121
122 // If this type is already laid out, converting it is a noop.
123 if (CGT.isRecordLayoutComplete(Key)) return true;
124
125 // If this type is currently being laid out, we can't recursively compile it.
126 if (CGT.isRecordBeingLaidOut(Key))
127 return false;
128
129 // If this type would require laying out bases that are currently being laid
130 // out, don't do it. This includes virtual base classes which get laid out
131 // when a class is translated, even though they aren't embedded by-value into
132 // the class.
133 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700134 for (const auto &I : CRD->bases())
135 if (!isSafeToConvert(I.getType()->getAs<RecordType>()->getDecl(),
Chris Lattner71305cc2011-07-15 05:16:14 +0000136 CGT, AlreadyChecked))
137 return false;
138 }
139
140 // If this type would require laying out members that are currently being laid
141 // out, don't do it.
Stephen Hines651f13c2014-04-23 16:59:28 -0700142 for (const auto *I : RD->fields())
Chris Lattner71305cc2011-07-15 05:16:14 +0000143 if (!isSafeToConvert(I->getType(), CGT, AlreadyChecked))
144 return false;
145
146 // If there are no problems, lets do it.
147 return true;
148}
149
150/// isSafeToConvert - Return true if it is safe to convert this field type,
151/// which requires the structure elements contained by-value to all be
152/// recursively safe to convert.
153static bool
154isSafeToConvert(QualType T, CodeGenTypes &CGT,
155 llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800156 // Strip off atomic type sugar.
157 if (const auto *AT = T->getAs<AtomicType>())
158 T = AT->getValueType();
159
Chris Lattner71305cc2011-07-15 05:16:14 +0000160 // If this is a record, check it.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800161 if (const auto *RT = T->getAs<RecordType>())
Chris Lattner71305cc2011-07-15 05:16:14 +0000162 return isSafeToConvert(RT->getDecl(), CGT, AlreadyChecked);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800163
Chris Lattner71305cc2011-07-15 05:16:14 +0000164 // If this is an array, check the elements, which are embedded inline.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800165 if (const auto *AT = CGT.getContext().getAsArrayType(T))
Chris Lattner71305cc2011-07-15 05:16:14 +0000166 return isSafeToConvert(AT->getElementType(), CGT, AlreadyChecked);
167
168 // Otherwise, there is no concern about transforming this. We only care about
169 // things that are contained by-value in a structure that can have another
170 // structure as a member.
171 return true;
172}
173
174
175/// isSafeToConvert - Return true if it is safe to convert the specified record
176/// decl to IR and lay it out, false if doing so would cause us to get into a
177/// recursive compilation mess.
178static bool isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT) {
179 // If no structs are being laid out, we can certainly do this one.
180 if (CGT.noRecordsBeingLaidOut()) return true;
181
182 llvm::SmallPtrSet<const RecordDecl*, 16> AlreadyChecked;
183 return isSafeToConvert(RD, CGT, AlreadyChecked);
184}
185
Stephen Hines651f13c2014-04-23 16:59:28 -0700186/// isFuncParamTypeConvertible - Return true if the specified type in a
187/// function parameter or result position can be converted to an IR type at this
Chris Lattnerf742eb02011-07-10 00:18:59 +0000188/// point. This boils down to being whether it is complete, as well as whether
189/// we've temporarily deferred expanding the type because we're in a recursive
190/// context.
Stephen Hines651f13c2014-04-23 16:59:28 -0700191bool CodeGenTypes::isFuncParamTypeConvertible(QualType Ty) {
Stephen Hines176edba2014-12-01 14:53:08 -0800192 // Some ABIs cannot have their member pointers represented in IR unless
193 // certain circumstances have been reached.
194 if (const auto *MPT = Ty->getAs<MemberPointerType>())
195 return getCXXABI().isMemberPointerConvertible(MPT);
196
Chris Lattnerf742eb02011-07-10 00:18:59 +0000197 // If this isn't a tagged type, we can convert it!
198 const TagType *TT = Ty->getAs<TagType>();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700199 if (!TT) return true;
200
Douglas Gregorbcf38f22012-02-25 00:16:17 +0000201 // Incomplete types cannot be converted.
Douglas Gregor4a59bc22012-02-25 00:06:47 +0000202 if (TT->isIncompleteType())
Chris Lattnerf742eb02011-07-10 00:18:59 +0000203 return false;
Stephen Hines176edba2014-12-01 14:53:08 -0800204
Chris Lattner71305cc2011-07-15 05:16:14 +0000205 // If this is an enum, then it is always safe to convert.
Chris Lattnerf742eb02011-07-10 00:18:59 +0000206 const RecordType *RT = dyn_cast<RecordType>(TT);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700207 if (!RT) return true;
Chris Lattnerf742eb02011-07-10 00:18:59 +0000208
209 // Otherwise, we have to be careful. If it is a struct that we're in the
210 // process of expanding, then we can't convert the function type. That's ok
211 // though because we must be in a pointer context under the struct, so we can
212 // just convert it to a dummy type.
213 //
214 // We decide this by checking whether ConvertRecordDeclType returns us an
215 // opaque type for a struct that we know is defined.
Chris Lattner71305cc2011-07-15 05:16:14 +0000216 return isSafeToConvert(RT->getDecl(), *this);
Chris Lattnerf742eb02011-07-10 00:18:59 +0000217}
218
219
220/// Code to verify a given function type is complete, i.e. the return type
Stephen Hines651f13c2014-04-23 16:59:28 -0700221/// and all of the parameter types are complete. Also check to see if we are in
Chris Lattnerf742eb02011-07-10 00:18:59 +0000222/// a RS_StructPointer context, and if so whether any struct types have been
223/// pended. If so, we don't want to ask the ABI lowering code to handle a type
224/// that cannot be converted to an IR type.
225bool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700226 if (!isFuncParamTypeConvertible(FT->getReturnType()))
Chris Lattnerf742eb02011-07-10 00:18:59 +0000227 return false;
228
229 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
Stephen Hines651f13c2014-04-23 16:59:28 -0700230 for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++)
231 if (!isFuncParamTypeConvertible(FPT->getParamType(i)))
Chris Lattnerf742eb02011-07-10 00:18:59 +0000232 return false;
233
234 return true;
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000235}
236
Chris Lattnerc5b88062008-02-06 05:08:19 +0000237/// UpdateCompletedType - When we find the full definition for a TagDecl,
238/// replace the 'opaque' type we previously made for it if applicable.
239void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000240 // If this is an enum being completed, then we flush all non-struct types from
241 // the cache. This allows function types and other things that may be derived
242 // from the enum to be recomputed.
Chris Lattner8dd5cdf2011-07-09 18:53:56 +0000243 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) {
244 // Only flush the cache if we've actually already converted this type.
Chris Lattner2045b2d2011-07-13 05:31:19 +0000245 if (TypeCache.count(ED->getTypeForDecl())) {
246 // Okay, we formed some types based on this. We speculated that the enum
247 // would be lowered to i32, so we only need to flush the cache if this
248 // didn't happen.
249 if (!ConvertType(ED->getIntegerType())->isIntegerTy(32))
250 TypeCache.clear();
251 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700252 // If necessary, provide the full definition of a type only used with a
253 // declaration so far.
254 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
255 DI->completeType(ED);
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000256 return;
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000257 }
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000258
Chris Lattnerf0a86792011-07-10 03:47:27 +0000259 // If we completed a RecordDecl that we previously used and converted to an
260 // anonymous type, then go ahead and complete it now.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000261 const RecordDecl *RD = cast<RecordDecl>(TD);
Chris Lattnerf0a86792011-07-10 03:47:27 +0000262 if (RD->isDependentType()) return;
263
264 // Only complete it if we converted it already. If we haven't converted it
265 // yet, we'll just do it lazily.
Chris Lattner3ade9752011-07-10 06:03:22 +0000266 if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr()))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000267 ConvertRecordDeclType(RD);
David Blaikieeab6a362013-06-21 00:40:50 +0000268
269 // If necessary, provide the full definition of a type only used with a
270 // declaration so far.
271 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
David Blaikie27804892013-08-15 20:49:17 +0000272 DI->completeType(RD);
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000273}
274
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700275void CodeGenTypes::RefreshTypeCacheForClass(const CXXRecordDecl *RD) {
276 QualType T = Context.getRecordType(RD);
277 T = Context.getCanonicalType(T);
278
279 const Type *Ty = T.getTypePtr();
280 if (RecordsWithOpaqueMemberPointers.count(Ty)) {
281 TypeCache.clear();
282 RecordsWithOpaqueMemberPointers.clear();
283 }
284}
285
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000286static llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext,
Joey Gouly19dbb202013-01-23 11:56:20 +0000287 const llvm::fltSemantics &format,
288 bool UseNativeHalf = false) {
289 if (&format == &llvm::APFloat::IEEEhalf) {
290 if (UseNativeHalf)
291 return llvm::Type::getHalfTy(VMContext);
292 else
293 return llvm::Type::getInt16Ty(VMContext);
294 }
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000295 if (&format == &llvm::APFloat::IEEEsingle)
Owen Anderson0032b272009-08-13 21:57:51 +0000296 return llvm::Type::getFloatTy(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000297 if (&format == &llvm::APFloat::IEEEdouble)
Owen Anderson0032b272009-08-13 21:57:51 +0000298 return llvm::Type::getDoubleTy(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000299 if (&format == &llvm::APFloat::IEEEquad)
Owen Anderson0032b272009-08-13 21:57:51 +0000300 return llvm::Type::getFP128Ty(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000301 if (&format == &llvm::APFloat::PPCDoubleDouble)
Owen Anderson0032b272009-08-13 21:57:51 +0000302 return llvm::Type::getPPC_FP128Ty(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000303 if (&format == &llvm::APFloat::x87DoubleExtended)
Owen Anderson0032b272009-08-13 21:57:51 +0000304 return llvm::Type::getX86_FP80Ty(VMContext);
David Blaikieb219cfc2011-09-23 05:06:16 +0000305 llvm_unreachable("Unknown float format!");
Eli Friedmanf6a943e2008-05-27 04:20:05 +0000306}
307
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800308llvm::Type *CodeGenTypes::ConvertFunctionType(QualType QFT,
309 const FunctionDecl *FD) {
310 assert(QFT.isCanonical());
311 const Type *Ty = QFT.getTypePtr();
312 const FunctionType *FT = cast<FunctionType>(QFT.getTypePtr());
313 // First, check whether we can build the full function type. If the
314 // function type depends on an incomplete type (e.g. a struct or enum), we
315 // cannot lower the function type.
316 if (!isFuncTypeConvertible(FT)) {
317 // This function's type depends on an incomplete tag type.
318
319 // Force conversion of all the relevant record types, to make sure
320 // we re-convert the FunctionType when appropriate.
321 if (const RecordType *RT = FT->getReturnType()->getAs<RecordType>())
322 ConvertRecordDeclType(RT->getDecl());
323 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
324 for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++)
325 if (const RecordType *RT = FPT->getParamType(i)->getAs<RecordType>())
326 ConvertRecordDeclType(RT->getDecl());
327
328 SkippedLayout = true;
329
330 // Return a placeholder type.
331 return llvm::StructType::get(getLLVMContext());
332 }
333
334 // While we're converting the parameter types for a function, we don't want
335 // to recursively convert any pointed-to structs. Converting directly-used
336 // structs is ok though.
337 if (!RecordsBeingLaidOut.insert(Ty).second) {
338 SkippedLayout = true;
339 return llvm::StructType::get(getLLVMContext());
340 }
341
342 // The function type can be built; call the appropriate routines to
343 // build it.
344 const CGFunctionInfo *FI;
345 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
346 FI = &arrangeFreeFunctionType(
347 CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0)), FD);
348 } else {
349 const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT);
350 FI = &arrangeFreeFunctionType(
351 CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0)));
352 }
353
354 llvm::Type *ResultType = nullptr;
355 // If there is something higher level prodding our CGFunctionInfo, then
356 // don't recurse into it again.
357 if (FunctionsBeingProcessed.count(FI)) {
358
359 ResultType = llvm::StructType::get(getLLVMContext());
360 SkippedLayout = true;
361 } else {
362
363 // Otherwise, we're good to go, go ahead and convert it.
364 ResultType = GetFunctionType(*FI);
365 }
366
367 RecordsBeingLaidOut.erase(Ty);
368
369 if (SkippedLayout)
370 TypeCache.clear();
371
372 if (RecordsBeingLaidOut.empty())
373 while (!DeferredRecords.empty())
374 ConvertRecordDeclType(DeferredRecords.pop_back_val());
375 return ResultType;
376}
377
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000378/// ConvertType - Convert the specified type to its LLVM form.
379llvm::Type *CodeGenTypes::ConvertType(QualType T) {
380 T = Context.getCanonicalType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000382 const Type *Ty = T.getTypePtr();
383
384 // RecordTypes are cached and processed specially.
385 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
386 return ConvertRecordDeclType(RT->getDecl());
387
388 // See if type is already cached.
389 llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI = TypeCache.find(Ty);
390 // If type is found in map then use it. Otherwise, convert type T.
391 if (TCI != TypeCache.end())
392 return TCI->second;
393
394 // If we don't have it in the cache, convert it now.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700395 llvm::Type *ResultType = nullptr;
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000396 switch (Ty->getTypeClass()) {
397 case Type::Record: // Handled above.
Douglas Gregor72564e72009-02-26 23:50:07 +0000398#define TYPE(Class, Base)
399#define ABSTRACT_TYPE(Class, Base)
400#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
401#define DEPENDENT_TYPE(Class, Base) case Type::Class:
John McCallad5e7382010-03-01 23:49:17 +0000402#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregor72564e72009-02-26 23:50:07 +0000403#include "clang/AST/TypeNodes.def"
John McCall864c0412011-04-26 20:42:42 +0000404 llvm_unreachable("Non-canonical or dependent types aren't possible.");
Douglas Gregor72564e72009-02-26 23:50:07 +0000405
Reid Spencer5f016e22007-07-11 17:01:13 +0000406 case Type::Builtin: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000407 switch (cast<BuiltinType>(Ty)->getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 case BuiltinType::Void:
Steve Naroffde2e22d2009-07-15 18:40:39 +0000409 case BuiltinType::ObjCId:
410 case BuiltinType::ObjCClass:
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000411 case BuiltinType::ObjCSel:
Reid Spencer5f016e22007-07-11 17:01:13 +0000412 // LLVM void type can only be used as the result of a function call. Just
413 // map to the same as char.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000414 ResultType = llvm::Type::getInt8Ty(getLLVMContext());
415 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000416
417 case BuiltinType::Bool:
Chris Lattner19009e62008-01-09 18:47:25 +0000418 // Note that we always return bool as i1 for use as a scalar type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000419 ResultType = llvm::Type::getInt1Ty(getLLVMContext());
420 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000422 case BuiltinType::Char_S:
423 case BuiltinType::Char_U:
424 case BuiltinType::SChar:
425 case BuiltinType::UChar:
Reid Spencer5f016e22007-07-11 17:01:13 +0000426 case BuiltinType::Short:
427 case BuiltinType::UShort:
Reid Spencer5f016e22007-07-11 17:01:13 +0000428 case BuiltinType::Int:
429 case BuiltinType::UInt:
Reid Spencer5f016e22007-07-11 17:01:13 +0000430 case BuiltinType::Long:
431 case BuiltinType::ULong:
Reid Spencer5f016e22007-07-11 17:01:13 +0000432 case BuiltinType::LongLong:
433 case BuiltinType::ULongLong:
Chris Lattner3f59c972010-12-25 23:25:43 +0000434 case BuiltinType::WChar_S:
435 case BuiltinType::WChar_U:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000436 case BuiltinType::Char16:
437 case BuiltinType::Char32:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000438 ResultType = llvm::IntegerType::get(getLLVMContext(),
439 static_cast<unsigned>(Context.getTypeSize(T)));
440 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000442 case BuiltinType::Half:
Joey Gouly19dbb202013-01-23 11:56:20 +0000443 // Half FP can either be storage-only (lowered to i16) or native.
Stephen Hines176edba2014-12-01 14:53:08 -0800444 ResultType =
445 getTypeForFormat(getLLVMContext(), Context.getFloatTypeSemantics(T),
446 Context.getLangOpts().NativeHalfType ||
447 Context.getLangOpts().HalfArgsAndReturns);
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000448 break;
Eli Friedmanf6a943e2008-05-27 04:20:05 +0000449 case BuiltinType::Float:
Nate Begemanc8b12272008-04-18 05:41:31 +0000450 case BuiltinType::Double:
Reid Spencer5f016e22007-07-11 17:01:13 +0000451 case BuiltinType::LongDouble:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700452 case BuiltinType::Float128:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000453 ResultType = getTypeForFormat(getLLVMContext(),
Joey Gouly19dbb202013-01-23 11:56:20 +0000454 Context.getFloatTypeSemantics(T),
455 /* UseNativeHalf = */ false);
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000456 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000458 case BuiltinType::NullPtr:
Anders Carlssonc1eb14a2009-09-15 04:39:46 +0000459 // Model std::nullptr_t as i8*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000460 ResultType = llvm::Type::getInt8PtrTy(getLLVMContext());
461 break;
Anders Carlssonc1eb14a2009-09-15 04:39:46 +0000462
Chris Lattner2df9ced2009-04-30 02:43:43 +0000463 case BuiltinType::UInt128:
464 case BuiltinType::Int128:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000465 ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
466 break;
Guy Benyeib13621d2012-12-18 14:38:23 +0000467
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700468#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
469 case BuiltinType::Id:
470#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei21f18c42013-02-07 10:55:47 +0000471 case BuiltinType::OCLSampler:
Guy Benyeie6b9d802013-01-20 12:31:11 +0000472 case BuiltinType::OCLEvent:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800473 case BuiltinType::OCLClkEvent:
474 case BuiltinType::OCLQueue:
475 case BuiltinType::OCLNDRange:
476 case BuiltinType::OCLReserveID:
Guy Benyeib13621d2012-12-18 14:38:23 +0000477 ResultType = CGM.getOpenCLRuntime().convertOpenCLSpecificType(Ty);
478 break;
Eli Friedman8c692352009-12-18 23:28:34 +0000479
Eli Friedman8c692352009-12-18 23:28:34 +0000480 case BuiltinType::Dependent:
John McCall2dde35b2011-10-18 22:28:37 +0000481#define BUILTIN_TYPE(Id, SingletonId)
482#define PLACEHOLDER_TYPE(Id, SingletonId) \
483 case BuiltinType::Id:
484#include "clang/AST/BuiltinTypes.def"
John McCall864c0412011-04-26 20:42:42 +0000485 llvm_unreachable("Unexpected placeholder builtin type!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000486 }
487 break;
488 }
Richard Smithdc7a4f52013-04-30 13:56:41 +0000489 case Type::Auto:
490 llvm_unreachable("Unexpected undeduced auto type!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000491 case Type::Complex: {
Jay Foadef6de3d2011-07-11 09:56:20 +0000492 llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType());
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700493 ResultType = llvm::StructType::get(EltTy, EltTy, nullptr);
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000494 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000495 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000496 case Type::LValueReference:
497 case Type::RValueReference: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000498 const ReferenceType *RTy = cast<ReferenceType>(Ty);
499 QualType ETy = RTy->getPointeeType();
500 llvm::Type *PointeeType = ConvertTypeForMem(ETy);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000501 unsigned AS = Context.getTargetAddressSpace(ETy);
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000502 ResultType = llvm::PointerType::get(PointeeType, AS);
503 break;
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +0000504 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000505 case Type::Pointer: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000506 const PointerType *PTy = cast<PointerType>(Ty);
507 QualType ETy = PTy->getPointeeType();
508 llvm::Type *PointeeType = ConvertTypeForMem(ETy);
509 if (PointeeType->isVoidTy())
510 PointeeType = llvm::Type::getInt8Ty(getLLVMContext());
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000511 unsigned AS = Context.getTargetAddressSpace(ETy);
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000512 ResultType = llvm::PointerType::get(PointeeType, AS);
513 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000514 }
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Steve Narofffb22d962007-08-30 01:06:46 +0000516 case Type::VariableArray: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000517 const VariableArrayType *A = cast<VariableArrayType>(Ty);
518 assert(A->getIndexTypeCVRQualifiers() == 0 &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000519 "FIXME: We only handle trivial array types so far!");
Eli Friedmanc5773c42008-02-15 18:16:39 +0000520 // VLAs resolve to the innermost element type; this matches
521 // the return of alloca, and there isn't any obviously better choice.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000522 ResultType = ConvertTypeForMem(A->getElementType());
523 break;
Eli Friedmanc5773c42008-02-15 18:16:39 +0000524 }
525 case Type::IncompleteArray: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000526 const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
527 assert(A->getIndexTypeCVRQualifiers() == 0 &&
Eli Friedmanc5773c42008-02-15 18:16:39 +0000528 "FIXME: We only handle trivial array types so far!");
Chris Lattner3a2b6572011-07-12 06:52:18 +0000529 // int X[] -> [0 x int], unless the element type is not sized. If it is
530 // unsized (e.g. an incomplete struct) just use [0 x i8].
531 ResultType = ConvertTypeForMem(A->getElementType());
532 if (!ResultType->isSized()) {
533 SkippedLayout = true;
534 ResultType = llvm::Type::getInt8Ty(getLLVMContext());
535 }
536 ResultType = llvm::ArrayType::get(ResultType, 0);
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000537 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000538 }
Steve Narofffb22d962007-08-30 01:06:46 +0000539 case Type::ConstantArray: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000540 const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000541 llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
Chris Lattner01c5d1d2011-07-22 06:27:26 +0000542
543 // Lower arrays of undefined struct type to arrays of i8 just to have a
544 // concrete type.
545 if (!EltTy->isSized()) {
546 SkippedLayout = true;
547 EltTy = llvm::Type::getInt8Ty(getLLVMContext());
548 }
549
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000550 ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue());
551 break;
Steve Narofffb22d962007-08-30 01:06:46 +0000552 }
Nate Begeman213541a2008-04-18 23:10:10 +0000553 case Type::ExtVector:
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 case Type::Vector: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000555 const VectorType *VT = cast<VectorType>(Ty);
556 ResultType = llvm::VectorType::get(ConvertType(VT->getElementType()),
557 VT->getNumElements());
558 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 }
560 case Type::FunctionNoProto:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800561 case Type::FunctionProto:
562 ResultType = ConvertFunctionType(T);
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000563 break;
John McCallc12c5bb2010-05-15 11:32:37 +0000564 case Type::ObjCObject:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000565 ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
566 break;
John McCallc12c5bb2010-05-15 11:32:37 +0000567
Chris Lattner391d77a2008-03-30 23:03:07 +0000568 case Type::ObjCInterface: {
Daniel Dunbar412f59b2009-04-22 10:28:39 +0000569 // Objective-C interfaces are always opaque (outside of the
570 // runtime, which can do whatever it likes); we never refine
571 // these.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000572 llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
Daniel Dunbar412f59b2009-04-22 10:28:39 +0000573 if (!T)
Chris Lattnerc1c20112011-08-12 17:43:31 +0000574 T = llvm::StructType::create(getLLVMContext());
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000575 ResultType = T;
576 break;
Chris Lattner391d77a2008-03-30 23:03:07 +0000577 }
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Steve Naroff14108da2009-07-10 23:34:53 +0000579 case Type::ObjCObjectPointer: {
Daniel Dunbar28e47802009-07-11 21:12:14 +0000580 // Protocol qualifications do not influence the LLVM type, we just return a
581 // pointer to the underlying interface type. We don't need to worry about
582 // recursive conversion.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000583 llvm::Type *T =
Chris Lattner181eeee2011-07-20 06:23:59 +0000584 ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000585 ResultType = T->getPointerTo();
586 break;
Steve Naroff14108da2009-07-10 23:34:53 +0000587 }
Daniel Dunbar28e47802009-07-11 21:12:14 +0000588
Chris Lattner2045b2d2011-07-13 05:31:19 +0000589 case Type::Enum: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000590 const EnumDecl *ED = cast<EnumType>(Ty)->getDecl();
John McCall5e1cdac2011-10-07 06:10:15 +0000591 if (ED->isCompleteDefinition() || ED->isFixed())
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000592 return ConvertType(ED->getIntegerType());
Chris Lattner2045b2d2011-07-13 05:31:19 +0000593 // Return a placeholder 'i32' type. This can be changed later when the
594 // type is defined (see UpdateCompletedType), but is likely to be the
595 // "right" answer.
596 ResultType = llvm::Type::getInt32Ty(getLLVMContext());
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000597 break;
Chris Lattnerde0efb32008-02-06 05:48:29 +0000598 }
Daniel Dunbar90488912008-08-28 18:02:04 +0000599
600 case Type::BlockPointer: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000601 const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
602 llvm::Type *PointeeType = ConvertTypeForMem(FTy);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000603 unsigned AS = Context.getTargetAddressSpace(FTy);
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000604 ResultType = llvm::PointerType::get(PointeeType, AS);
605 break;
Daniel Dunbar90488912008-08-28 18:02:04 +0000606 }
Sebastian Redl424c51d2009-01-25 13:35:30 +0000607
Anders Carlsson0e650012009-05-17 17:41:20 +0000608 case Type::MemberPointer: {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700609 auto *MPTy = cast<MemberPointerType>(Ty);
610 if (!getCXXABI().isMemberPointerConvertible(MPTy)) {
611 RecordsWithOpaqueMemberPointers.insert(MPTy->getClass());
612 ResultType = llvm::StructType::create(getLLVMContext());
613 } else {
614 ResultType = getCXXABI().ConvertMemberPointerType(MPTy);
615 }
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000616 break;
Anders Carlsson0e650012009-05-17 17:41:20 +0000617 }
Eli Friedmanb001de72011-10-06 23:00:33 +0000618
619 case Type::Atomic: {
John McCall9eda3ab2013-03-07 21:37:17 +0000620 QualType valueType = cast<AtomicType>(Ty)->getValueType();
621 ResultType = ConvertTypeForMem(valueType);
622
623 // Pad out to the inflated size if necessary.
624 uint64_t valueSize = Context.getTypeSize(valueType);
625 uint64_t atomicSize = Context.getTypeSize(Ty);
626 if (valueSize != atomicSize) {
627 assert(valueSize < atomicSize);
628 llvm::Type *elts[] = {
629 ResultType,
630 llvm::ArrayType::get(CGM.Int8Ty, (atomicSize - valueSize) / 8)
631 };
632 ResultType = llvm::StructType::get(getLLVMContext(),
633 llvm::makeArrayRef(elts));
634 }
Eli Friedmanb001de72011-10-06 23:00:33 +0000635 break;
636 }
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700637 case Type::Pipe: {
638 ResultType = CGM.getOpenCLRuntime().getPipeType();
639 break;
640 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 }
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000642
643 assert(ResultType && "Didn't convert a type?");
644
645 TypeCache[Ty] = ResultType;
646 return ResultType;
Reid Spencer5f016e22007-07-11 17:01:13 +0000647}
648
John McCall9eda3ab2013-03-07 21:37:17 +0000649bool CodeGenModule::isPaddedAtomicType(QualType type) {
650 return isPaddedAtomicType(type->castAs<AtomicType>());
651}
652
653bool CodeGenModule::isPaddedAtomicType(const AtomicType *type) {
654 return Context.getTypeSize(type) != Context.getTypeSize(type->getValueType());
655}
656
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000657/// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
658llvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) {
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000659 // TagDecl's are not necessarily unique, instead use the (clang)
660 // type connected to the decl.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000661 const Type *Key = Context.getTagDeclType(RD).getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000663 llvm::StructType *&Entry = RecordDeclTypes[Key];
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000665 // If we don't have a StructType at all yet, create the forward declaration.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700666 if (!Entry) {
Chris Lattnerc1c20112011-08-12 17:43:31 +0000667 Entry = llvm::StructType::create(getLLVMContext());
Chris Lattnercd87d1e2011-07-12 05:53:08 +0000668 addRecordTypeName(RD, Entry, "");
669 }
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000670 llvm::StructType *Ty = Entry;
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000671
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000672 // If this is still a forward declaration, or the LLVM type is already
673 // complete, there's nothing more to do.
Chris Lattner71305cc2011-07-15 05:16:14 +0000674 RD = RD->getDefinition();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700675 if (!RD || !RD->isCompleteDefinition() || !Ty->isOpaque())
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000676 return Ty;
677
Chris Lattner71305cc2011-07-15 05:16:14 +0000678 // If converting this type would cause us to infinitely loop, don't do it!
679 if (!isSafeToConvert(RD, *this)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000680 DeferredRecords.push_back(RD);
681 return Ty;
Chris Lattner5de00fc2008-02-06 06:03:51 +0000682 }
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Chris Lattner5de00fc2008-02-06 06:03:51 +0000684 // Okay, this is a definition of a type. Compile the implementation now.
Stephen Hines176edba2014-12-01 14:53:08 -0800685 bool InsertResult = RecordsBeingLaidOut.insert(Key).second;
686 (void)InsertResult;
Chris Lattner71305cc2011-07-15 05:16:14 +0000687 assert(InsertResult && "Recursively compiling a struct?");
688
John McCall86ff3082010-02-04 22:26:26 +0000689 // Force conversion of non-virtual base classes recursively.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000690 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700691 for (const auto &I : CRD->bases()) {
692 if (I.isVirtual()) continue;
Chris Lattner71305cc2011-07-15 05:16:14 +0000693
Stephen Hines651f13c2014-04-23 16:59:28 -0700694 ConvertRecordDeclType(I.getType()->getAs<RecordType>()->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000695 }
696 }
697
Anders Carlsson696798f2009-07-27 17:10:54 +0000698 // Layout fields.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000699 CGRecordLayout *Layout = ComputeRecordLayout(RD, Ty);
Anders Carlsson696798f2009-07-27 17:10:54 +0000700 CGRecordLayouts[Key] = Layout;
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Chris Lattner71305cc2011-07-15 05:16:14 +0000702 // We're done laying out this struct.
703 bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult;
704 assert(EraseResult && "struct not in RecordsBeingLaidOut set?");
705
Chris Lattnerf0a86792011-07-10 03:47:27 +0000706 // If this struct blocked a FunctionType conversion, then recompute whatever
707 // was derived from that.
708 // FIXME: This is hugely overconservative.
Chris Lattner57eb23f2011-07-10 05:39:13 +0000709 if (SkippedLayout)
710 TypeCache.clear();
Chris Lattner71305cc2011-07-15 05:16:14 +0000711
712 // If we're done converting the outer-most record, then convert any deferred
713 // structs as well.
714 if (RecordsBeingLaidOut.empty())
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000715 while (!DeferredRecords.empty())
716 ConvertRecordDeclType(DeferredRecords.pop_back_val());
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000718 return Ty;
Mike Stump1eb44332009-09-09 15:08:12 +0000719}
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000720
Anders Carlsson2d987772010-11-24 20:22:04 +0000721/// getCGRecordLayout - Return record layout info for the given record decl.
Anders Carlssonad3e7112009-08-24 17:16:23 +0000722const CGRecordLayout &
Anders Carlsson2d987772010-11-24 20:22:04 +0000723CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
724 const Type *Key = Context.getTagDeclType(RD).getTypePtr();
Anders Carlsson82926962010-11-24 19:52:29 +0000725
Daniel Dunbar270e2032010-03-31 00:11:27 +0000726 const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key);
Anders Carlssonc8f01eb2010-11-24 19:51:04 +0000727 if (!Layout) {
Anders Carlsson2d987772010-11-24 20:22:04 +0000728 // Compute the type information.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000729 ConvertRecordDeclType(RD);
Anders Carlssonc8f01eb2010-11-24 19:51:04 +0000730
731 // Now try again.
732 Layout = CGRecordLayouts.lookup(Key);
733 }
734
Daniel Dunbar270e2032010-03-31 00:11:27 +0000735 assert(Layout && "Unable to find record layout information for type");
736 return *Layout;
Devang Patelb84a06e2007-10-23 02:10:49 +0000737}
Anders Carlsson3e5af902010-05-14 19:41:56 +0000738
John McCallf16aa102010-08-22 21:01:12 +0000739bool CodeGenTypes::isZeroInitializable(QualType T) {
Anders Carlsson3e5af902010-05-14 19:41:56 +0000740 // No need to check for member pointers when not compiling C++.
David Blaikie4e4d0842012-03-11 07:00:24 +0000741 if (!Context.getLangOpts().CPlusPlus)
John McCallf16aa102010-08-22 21:01:12 +0000742 return true;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700743
744 if (const auto *AT = Context.getAsArrayType(T)) {
745 if (isa<IncompleteArrayType>(AT))
746 return true;
747 if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
748 if (Context.getConstantArrayElementCount(CAT) == 0)
749 return true;
750 T = Context.getBaseElementType(T);
751 }
752
John McCallf16aa102010-08-22 21:01:12 +0000753 // Records are non-zero-initializable if they contain any
754 // non-zero-initializable subobjects.
Anders Carlsson3e5af902010-05-14 19:41:56 +0000755 if (const RecordType *RT = T->getAs<RecordType>()) {
756 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCallf16aa102010-08-22 21:01:12 +0000757 return isZeroInitializable(RD);
Anders Carlsson3e5af902010-05-14 19:41:56 +0000758 }
John McCallf16aa102010-08-22 21:01:12 +0000759
760 // We have to ask the ABI about member pointers.
Anders Carlsson3e5af902010-05-14 19:41:56 +0000761 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
John McCallf16aa102010-08-22 21:01:12 +0000762 return getCXXABI().isZeroInitializable(MPT);
Anders Carlsson3e5af902010-05-14 19:41:56 +0000763
John McCallf16aa102010-08-22 21:01:12 +0000764 // Everything else is okay.
765 return true;
Anders Carlsson3e5af902010-05-14 19:41:56 +0000766}
Anders Carlssonc39211d2010-05-18 03:47:15 +0000767
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700768bool CodeGenTypes::isZeroInitializable(const RecordDecl *RD) {
Anders Carlsson3379e9b2010-11-24 19:57:04 +0000769 return getCGRecordLayout(RD).isZeroInitializable();
Anders Carlssonc39211d2010-05-18 03:47:15 +0000770}