1 //===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the code that handles AST -> LLVM type lowering.
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenTypes.h"
17 #include "CGOpenCLRuntime.h"
18 #include "CGRecordLayout.h"
19 #include "TargetInfo.h"
20 #include "clang/AST/ASTContext.h"
21 #include "clang/AST/DeclCXX.h"
22 #include "clang/AST/DeclObjC.h"
23 #include "clang/AST/Expr.h"
24 #include "clang/AST/RecordLayout.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/Module.h"
28 using namespace clang;
29 using namespace CodeGen;
31 CodeGenTypes::CodeGenTypes(CodeGenModule &cgm)
32 : CGM(cgm), Context(cgm.getContext()), TheModule(cgm.getModule()),
33 TheDataLayout(cgm.getDataLayout()),
34 Target(cgm.getTarget()), TheCXXABI(cgm.getCXXABI()),
35 CodeGenOpts(cgm.getCodeGenOpts()),
36 TheABIInfo(cgm.getTargetCodeGenInfo().getABIInfo()) {
37 SkippedLayout = false;
40 CodeGenTypes::~CodeGenTypes() {
41 for (llvm::DenseMap<const Type *, CGRecordLayout *>::iterator
42 I = CGRecordLayouts.begin(), E = CGRecordLayouts.end();
46 for (llvm::FoldingSet<CGFunctionInfo>::iterator
47 I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
51 void CodeGenTypes::addRecordTypeName(const RecordDecl *RD,
54 SmallString<256> TypeName;
55 llvm::raw_svector_ostream OS(TypeName);
56 OS << RD->getKindName() << '.';
58 // Name the codegen type after the typedef name
59 // if there is no tag type name available
60 if (RD->getIdentifier()) {
61 // FIXME: We should not have to check for a null decl context here.
62 // Right now we do it because the implicit Obj-C decls don't have one.
63 if (RD->getDeclContext())
64 RD->printQualifiedName(OS);
67 } else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) {
68 // FIXME: We should not have to check for a null decl context here.
69 // Right now we do it because the implicit Obj-C decls don't have one.
70 if (TDD->getDeclContext())
71 TDD->printQualifiedName(OS);
80 Ty->setName(OS.str());
83 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
84 /// ConvertType in that it is used to convert to the memory representation for
85 /// a type. For example, the scalar representation for _Bool is i1, but the
86 /// memory representation is usually i8 or i32, depending on the target.
87 llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T){
88 llvm::Type *R = ConvertType(T);
90 // If this is a non-bool type, don't map it.
91 if (!R->isIntegerTy(1))
94 // Otherwise, return an integer of the target-specified size.
95 return llvm::IntegerType::get(getLLVMContext(),
96 (unsigned)Context.getTypeSize(T));
100 /// isRecordLayoutComplete - Return true if the specified type is already
101 /// completely laid out.
102 bool CodeGenTypes::isRecordLayoutComplete(const Type *Ty) const {
103 llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I =
104 RecordDeclTypes.find(Ty);
105 return I != RecordDeclTypes.end() && !I->second->isOpaque();
109 isSafeToConvert(QualType T, CodeGenTypes &CGT,
110 llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked);
113 /// isSafeToConvert - Return true if it is safe to convert the specified record
114 /// decl to IR and lay it out, false if doing so would cause us to get into a
115 /// recursive compilation mess.
117 isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT,
118 llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
119 // If we have already checked this type (maybe the same type is used by-value
120 // multiple times in multiple structure fields, don't check again.
121 if (!AlreadyChecked.insert(RD)) return true;
123 const Type *Key = CGT.getContext().getTagDeclType(RD).getTypePtr();
125 // If this type is already laid out, converting it is a noop.
126 if (CGT.isRecordLayoutComplete(Key)) return true;
128 // If this type is currently being laid out, we can't recursively compile it.
129 if (CGT.isRecordBeingLaidOut(Key))
132 // If this type would require laying out bases that are currently being laid
133 // out, don't do it. This includes virtual base classes which get laid out
134 // when a class is translated, even though they aren't embedded by-value into
136 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
137 for (CXXRecordDecl::base_class_const_iterator I = CRD->bases_begin(),
138 E = CRD->bases_end(); I != E; ++I)
139 if (!isSafeToConvert(I->getType()->getAs<RecordType>()->getDecl(),
140 CGT, AlreadyChecked))
144 // If this type would require laying out members that are currently being laid
146 for (RecordDecl::field_iterator I = RD->field_begin(),
147 E = RD->field_end(); I != E; ++I)
148 if (!isSafeToConvert(I->getType(), CGT, AlreadyChecked))
151 // If there are no problems, lets do it.
155 /// isSafeToConvert - Return true if it is safe to convert this field type,
156 /// which requires the structure elements contained by-value to all be
157 /// recursively safe to convert.
159 isSafeToConvert(QualType T, CodeGenTypes &CGT,
160 llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
161 T = T.getCanonicalType();
163 // If this is a record, check it.
164 if (const RecordType *RT = dyn_cast<RecordType>(T))
165 return isSafeToConvert(RT->getDecl(), CGT, AlreadyChecked);
167 // If this is an array, check the elements, which are embedded inline.
168 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
169 return isSafeToConvert(AT->getElementType(), CGT, AlreadyChecked);
171 // Otherwise, there is no concern about transforming this. We only care about
172 // things that are contained by-value in a structure that can have another
173 // structure as a member.
178 /// isSafeToConvert - Return true if it is safe to convert the specified record
179 /// decl to IR and lay it out, false if doing so would cause us to get into a
180 /// recursive compilation mess.
181 static bool isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT) {
182 // If no structs are being laid out, we can certainly do this one.
183 if (CGT.noRecordsBeingLaidOut()) return true;
185 llvm::SmallPtrSet<const RecordDecl*, 16> AlreadyChecked;
186 return isSafeToConvert(RD, CGT, AlreadyChecked);
190 /// isFuncTypeArgumentConvertible - Return true if the specified type in a
191 /// function argument or result position can be converted to an IR type at this
192 /// point. This boils down to being whether it is complete, as well as whether
193 /// we've temporarily deferred expanding the type because we're in a recursive
195 bool CodeGenTypes::isFuncTypeArgumentConvertible(QualType Ty) {
196 // If this isn't a tagged type, we can convert it!
197 const TagType *TT = Ty->getAs<TagType>();
198 if (TT == 0) return true;
200 // Incomplete types cannot be converted.
201 if (TT->isIncompleteType())
204 // If this is an enum, then it is always safe to convert.
205 const RecordType *RT = dyn_cast<RecordType>(TT);
206 if (RT == 0) return true;
208 // Otherwise, we have to be careful. If it is a struct that we're in the
209 // process of expanding, then we can't convert the function type. That's ok
210 // though because we must be in a pointer context under the struct, so we can
211 // just convert it to a dummy type.
213 // We decide this by checking whether ConvertRecordDeclType returns us an
214 // opaque type for a struct that we know is defined.
215 return isSafeToConvert(RT->getDecl(), *this);
219 /// Code to verify a given function type is complete, i.e. the return type
220 /// and all of the argument types are complete. Also check to see if we are in
221 /// a RS_StructPointer context, and if so whether any struct types have been
222 /// pended. If so, we don't want to ask the ABI lowering code to handle a type
223 /// that cannot be converted to an IR type.
224 bool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) {
225 if (!isFuncTypeArgumentConvertible(FT->getResultType()))
228 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
229 for (unsigned i = 0, e = FPT->getNumArgs(); i != e; i++)
230 if (!isFuncTypeArgumentConvertible(FPT->getArgType(i)))
236 /// UpdateCompletedType - When we find the full definition for a TagDecl,
237 /// replace the 'opaque' type we previously made for it if applicable.
238 void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
239 // If this is an enum being completed, then we flush all non-struct types from
240 // the cache. This allows function types and other things that may be derived
241 // from the enum to be recomputed.
242 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) {
243 // Only flush the cache if we've actually already converted this type.
244 if (TypeCache.count(ED->getTypeForDecl())) {
245 // Okay, we formed some types based on this. We speculated that the enum
246 // would be lowered to i32, so we only need to flush the cache if this
248 if (!ConvertType(ED->getIntegerType())->isIntegerTy(32))
254 // If we completed a RecordDecl that we previously used and converted to an
255 // anonymous type, then go ahead and complete it now.
256 const RecordDecl *RD = cast<RecordDecl>(TD);
257 if (RD->isDependentType()) return;
259 // Only complete it if we converted it already. If we haven't converted it
260 // yet, we'll just do it lazily.
261 if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr()))
262 ConvertRecordDeclType(RD);
265 static llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext,
266 const llvm::fltSemantics &format,
267 bool UseNativeHalf = false) {
268 if (&format == &llvm::APFloat::IEEEhalf) {
270 return llvm::Type::getHalfTy(VMContext);
272 return llvm::Type::getInt16Ty(VMContext);
274 if (&format == &llvm::APFloat::IEEEsingle)
275 return llvm::Type::getFloatTy(VMContext);
276 if (&format == &llvm::APFloat::IEEEdouble)
277 return llvm::Type::getDoubleTy(VMContext);
278 if (&format == &llvm::APFloat::IEEEquad)
279 return llvm::Type::getFP128Ty(VMContext);
280 if (&format == &llvm::APFloat::PPCDoubleDouble)
281 return llvm::Type::getPPC_FP128Ty(VMContext);
282 if (&format == &llvm::APFloat::x87DoubleExtended)
283 return llvm::Type::getX86_FP80Ty(VMContext);
284 llvm_unreachable("Unknown float format!");
287 /// ConvertType - Convert the specified type to its LLVM form.
288 llvm::Type *CodeGenTypes::ConvertType(QualType T) {
289 T = Context.getCanonicalType(T);
291 const Type *Ty = T.getTypePtr();
293 // RecordTypes are cached and processed specially.
294 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
295 return ConvertRecordDeclType(RT->getDecl());
297 // See if type is already cached.
298 llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI = TypeCache.find(Ty);
299 // If type is found in map then use it. Otherwise, convert type T.
300 if (TCI != TypeCache.end())
303 // If we don't have it in the cache, convert it now.
304 llvm::Type *ResultType = 0;
305 switch (Ty->getTypeClass()) {
306 case Type::Record: // Handled above.
307 #define TYPE(Class, Base)
308 #define ABSTRACT_TYPE(Class, Base)
309 #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
310 #define DEPENDENT_TYPE(Class, Base) case Type::Class:
311 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
312 #include "clang/AST/TypeNodes.def"
313 llvm_unreachable("Non-canonical or dependent types aren't possible.");
315 case Type::Builtin: {
316 switch (cast<BuiltinType>(Ty)->getKind()) {
317 case BuiltinType::Void:
318 case BuiltinType::ObjCId:
319 case BuiltinType::ObjCClass:
320 case BuiltinType::ObjCSel:
321 // LLVM void type can only be used as the result of a function call. Just
322 // map to the same as char.
323 ResultType = llvm::Type::getInt8Ty(getLLVMContext());
326 case BuiltinType::Bool:
327 // Note that we always return bool as i1 for use as a scalar type.
328 ResultType = llvm::Type::getInt1Ty(getLLVMContext());
331 case BuiltinType::Char_S:
332 case BuiltinType::Char_U:
333 case BuiltinType::SChar:
334 case BuiltinType::UChar:
335 case BuiltinType::Short:
336 case BuiltinType::UShort:
337 case BuiltinType::Int:
338 case BuiltinType::UInt:
339 case BuiltinType::Long:
340 case BuiltinType::ULong:
341 case BuiltinType::LongLong:
342 case BuiltinType::ULongLong:
343 case BuiltinType::WChar_S:
344 case BuiltinType::WChar_U:
345 case BuiltinType::Char16:
346 case BuiltinType::Char32:
347 ResultType = llvm::IntegerType::get(getLLVMContext(),
348 static_cast<unsigned>(Context.getTypeSize(T)));
351 case BuiltinType::Half:
352 // Half FP can either be storage-only (lowered to i16) or native.
353 ResultType = getTypeForFormat(getLLVMContext(),
354 Context.getFloatTypeSemantics(T),
355 Context.getLangOpts().NativeHalfType);
357 case BuiltinType::Float:
358 case BuiltinType::Double:
359 case BuiltinType::LongDouble:
360 ResultType = getTypeForFormat(getLLVMContext(),
361 Context.getFloatTypeSemantics(T),
362 /* UseNativeHalf = */ false);
365 case BuiltinType::NullPtr:
366 // Model std::nullptr_t as i8*
367 ResultType = llvm::Type::getInt8PtrTy(getLLVMContext());
370 case BuiltinType::UInt128:
371 case BuiltinType::Int128:
372 ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
375 case BuiltinType::OCLImage1d:
376 case BuiltinType::OCLImage1dArray:
377 case BuiltinType::OCLImage1dBuffer:
378 case BuiltinType::OCLImage2d:
379 case BuiltinType::OCLImage2dArray:
380 case BuiltinType::OCLImage3d:
381 case BuiltinType::OCLSampler:
382 case BuiltinType::OCLEvent:
383 ResultType = CGM.getOpenCLRuntime().convertOpenCLSpecificType(Ty);
386 case BuiltinType::Dependent:
387 #define BUILTIN_TYPE(Id, SingletonId)
388 #define PLACEHOLDER_TYPE(Id, SingletonId) \
389 case BuiltinType::Id:
390 #include "clang/AST/BuiltinTypes.def"
391 llvm_unreachable("Unexpected placeholder builtin type!");
396 llvm_unreachable("Unexpected undeduced auto type!");
397 case Type::Complex: {
398 llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType());
399 ResultType = llvm::StructType::get(EltTy, EltTy, NULL);
402 case Type::LValueReference:
403 case Type::RValueReference: {
404 const ReferenceType *RTy = cast<ReferenceType>(Ty);
405 QualType ETy = RTy->getPointeeType();
406 llvm::Type *PointeeType = ConvertTypeForMem(ETy);
407 unsigned AS = Context.getTargetAddressSpace(ETy);
408 ResultType = llvm::PointerType::get(PointeeType, AS);
411 case Type::Pointer: {
412 const PointerType *PTy = cast<PointerType>(Ty);
413 QualType ETy = PTy->getPointeeType();
414 llvm::Type *PointeeType = ConvertTypeForMem(ETy);
415 if (PointeeType->isVoidTy())
416 PointeeType = llvm::Type::getInt8Ty(getLLVMContext());
417 unsigned AS = Context.getTargetAddressSpace(ETy);
418 ResultType = llvm::PointerType::get(PointeeType, AS);
422 case Type::VariableArray: {
423 const VariableArrayType *A = cast<VariableArrayType>(Ty);
424 assert(A->getIndexTypeCVRQualifiers() == 0 &&
425 "FIXME: We only handle trivial array types so far!");
426 // VLAs resolve to the innermost element type; this matches
427 // the return of alloca, and there isn't any obviously better choice.
428 ResultType = ConvertTypeForMem(A->getElementType());
431 case Type::IncompleteArray: {
432 const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
433 assert(A->getIndexTypeCVRQualifiers() == 0 &&
434 "FIXME: We only handle trivial array types so far!");
435 // int X[] -> [0 x int], unless the element type is not sized. If it is
436 // unsized (e.g. an incomplete struct) just use [0 x i8].
437 ResultType = ConvertTypeForMem(A->getElementType());
438 if (!ResultType->isSized()) {
439 SkippedLayout = true;
440 ResultType = llvm::Type::getInt8Ty(getLLVMContext());
442 ResultType = llvm::ArrayType::get(ResultType, 0);
445 case Type::ConstantArray: {
446 const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
447 llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
449 // Lower arrays of undefined struct type to arrays of i8 just to have a
451 if (!EltTy->isSized()) {
452 SkippedLayout = true;
453 EltTy = llvm::Type::getInt8Ty(getLLVMContext());
456 ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue());
459 case Type::ExtVector:
461 const VectorType *VT = cast<VectorType>(Ty);
462 ResultType = llvm::VectorType::get(ConvertType(VT->getElementType()),
463 VT->getNumElements());
466 case Type::FunctionNoProto:
467 case Type::FunctionProto: {
468 const FunctionType *FT = cast<FunctionType>(Ty);
469 // First, check whether we can build the full function type. If the
470 // function type depends on an incomplete type (e.g. a struct or enum), we
471 // cannot lower the function type.
472 if (!isFuncTypeConvertible(FT)) {
473 // This function's type depends on an incomplete tag type.
475 // Force conversion of all the relevant record types, to make sure
476 // we re-convert the FunctionType when appropriate.
477 if (const RecordType *RT = FT->getResultType()->getAs<RecordType>())
478 ConvertRecordDeclType(RT->getDecl());
479 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
480 for (unsigned i = 0, e = FPT->getNumArgs(); i != e; i++)
481 if (const RecordType *RT = FPT->getArgType(i)->getAs<RecordType>())
482 ConvertRecordDeclType(RT->getDecl());
484 // Return a placeholder type.
485 ResultType = llvm::StructType::get(getLLVMContext());
487 SkippedLayout = true;
491 // While we're converting the argument types for a function, we don't want
492 // to recursively convert any pointed-to structs. Converting directly-used
493 // structs is ok though.
494 if (!RecordsBeingLaidOut.insert(Ty)) {
495 ResultType = llvm::StructType::get(getLLVMContext());
497 SkippedLayout = true;
501 // The function type can be built; call the appropriate routines to
503 const CGFunctionInfo *FI;
504 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
505 FI = &arrangeFreeFunctionType(
506 CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0)));
508 const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT);
509 FI = &arrangeFreeFunctionType(
510 CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0)));
513 // If there is something higher level prodding our CGFunctionInfo, then
514 // don't recurse into it again.
515 if (FunctionsBeingProcessed.count(FI)) {
517 ResultType = llvm::StructType::get(getLLVMContext());
518 SkippedLayout = true;
521 // Otherwise, we're good to go, go ahead and convert it.
522 ResultType = GetFunctionType(*FI);
525 RecordsBeingLaidOut.erase(Ty);
530 if (RecordsBeingLaidOut.empty())
531 while (!DeferredRecords.empty())
532 ConvertRecordDeclType(DeferredRecords.pop_back_val());
536 case Type::ObjCObject:
537 ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
540 case Type::ObjCInterface: {
541 // Objective-C interfaces are always opaque (outside of the
542 // runtime, which can do whatever it likes); we never refine
544 llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
546 T = llvm::StructType::create(getLLVMContext());
551 case Type::ObjCObjectPointer: {
552 // Protocol qualifications do not influence the LLVM type, we just return a
553 // pointer to the underlying interface type. We don't need to worry about
554 // recursive conversion.
556 ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
557 ResultType = T->getPointerTo();
562 const EnumDecl *ED = cast<EnumType>(Ty)->getDecl();
563 if (ED->isCompleteDefinition() || ED->isFixed())
564 return ConvertType(ED->getIntegerType());
565 // Return a placeholder 'i32' type. This can be changed later when the
566 // type is defined (see UpdateCompletedType), but is likely to be the
568 ResultType = llvm::Type::getInt32Ty(getLLVMContext());
572 case Type::BlockPointer: {
573 const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
574 llvm::Type *PointeeType = ConvertTypeForMem(FTy);
575 unsigned AS = Context.getTargetAddressSpace(FTy);
576 ResultType = llvm::PointerType::get(PointeeType, AS);
580 case Type::MemberPointer: {
582 getCXXABI().ConvertMemberPointerType(cast<MemberPointerType>(Ty));
587 QualType valueType = cast<AtomicType>(Ty)->getValueType();
588 ResultType = ConvertTypeForMem(valueType);
590 // Pad out to the inflated size if necessary.
591 uint64_t valueSize = Context.getTypeSize(valueType);
592 uint64_t atomicSize = Context.getTypeSize(Ty);
593 if (valueSize != atomicSize) {
594 assert(valueSize < atomicSize);
595 llvm::Type *elts[] = {
597 llvm::ArrayType::get(CGM.Int8Ty, (atomicSize - valueSize) / 8)
599 ResultType = llvm::StructType::get(getLLVMContext(),
600 llvm::makeArrayRef(elts));
606 assert(ResultType && "Didn't convert a type?");
608 TypeCache[Ty] = ResultType;
612 bool CodeGenModule::isPaddedAtomicType(QualType type) {
613 return isPaddedAtomicType(type->castAs<AtomicType>());
616 bool CodeGenModule::isPaddedAtomicType(const AtomicType *type) {
617 return Context.getTypeSize(type) != Context.getTypeSize(type->getValueType());
620 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
621 llvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) {
622 // TagDecl's are not necessarily unique, instead use the (clang)
623 // type connected to the decl.
624 const Type *Key = Context.getTagDeclType(RD).getTypePtr();
626 llvm::StructType *&Entry = RecordDeclTypes[Key];
628 // If we don't have a StructType at all yet, create the forward declaration.
630 Entry = llvm::StructType::create(getLLVMContext());
631 addRecordTypeName(RD, Entry, "");
633 llvm::StructType *Ty = Entry;
635 // If this is still a forward declaration, or the LLVM type is already
636 // complete, there's nothing more to do.
637 RD = RD->getDefinition();
638 if (RD == 0 || !RD->isCompleteDefinition() || !Ty->isOpaque())
641 // If converting this type would cause us to infinitely loop, don't do it!
642 if (!isSafeToConvert(RD, *this)) {
643 DeferredRecords.push_back(RD);
647 // Okay, this is a definition of a type. Compile the implementation now.
648 bool InsertResult = RecordsBeingLaidOut.insert(Key); (void)InsertResult;
649 assert(InsertResult && "Recursively compiling a struct?");
651 // Force conversion of non-virtual base classes recursively.
652 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
653 for (CXXRecordDecl::base_class_const_iterator i = CRD->bases_begin(),
654 e = CRD->bases_end(); i != e; ++i) {
655 if (i->isVirtual()) continue;
657 ConvertRecordDeclType(i->getType()->getAs<RecordType>()->getDecl());
662 CGRecordLayout *Layout = ComputeRecordLayout(RD, Ty);
663 CGRecordLayouts[Key] = Layout;
665 // We're done laying out this struct.
666 bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult;
667 assert(EraseResult && "struct not in RecordsBeingLaidOut set?");
669 // If this struct blocked a FunctionType conversion, then recompute whatever
670 // was derived from that.
671 // FIXME: This is hugely overconservative.
675 // If we're done converting the outer-most record, then convert any deferred
677 if (RecordsBeingLaidOut.empty())
678 while (!DeferredRecords.empty())
679 ConvertRecordDeclType(DeferredRecords.pop_back_val());
684 /// getCGRecordLayout - Return record layout info for the given record decl.
685 const CGRecordLayout &
686 CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
687 const Type *Key = Context.getTagDeclType(RD).getTypePtr();
689 const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key);
691 // Compute the type information.
692 ConvertRecordDeclType(RD);
695 Layout = CGRecordLayouts.lookup(Key);
698 assert(Layout && "Unable to find record layout information for type");
702 bool CodeGenTypes::isZeroInitializable(QualType T) {
703 // No need to check for member pointers when not compiling C++.
704 if (!Context.getLangOpts().CPlusPlus)
707 T = Context.getBaseElementType(T);
709 // Records are non-zero-initializable if they contain any
710 // non-zero-initializable subobjects.
711 if (const RecordType *RT = T->getAs<RecordType>()) {
712 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
713 return isZeroInitializable(RD);
716 // We have to ask the ABI about member pointers.
717 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
718 return getCXXABI().isZeroInitializable(MPT);
720 // Everything else is okay.
724 bool CodeGenTypes::isZeroInitializable(const CXXRecordDecl *RD) {
725 return getCGRecordLayout(RD).isZeroInitializable();