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 "clang/CodeGen/CGFunctionInfo.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/DerivedTypes.h"
28 #include "llvm/IR/Module.h"
29 using namespace clang;
30 using namespace CodeGen;
32 CodeGenTypes::CodeGenTypes(CodeGenModule &cgm)
33 : CGM(cgm), Context(cgm.getContext()), TheModule(cgm.getModule()),
34 Target(cgm.getTarget()), TheCXXABI(cgm.getCXXABI()),
35 TheABIInfo(cgm.getTargetCodeGenInfo().getABIInfo()) {
36 SkippedLayout = false;
39 CodeGenTypes::~CodeGenTypes() {
40 llvm::DeleteContainerSeconds(CGRecordLayouts);
42 for (llvm::FoldingSet<CGFunctionInfo>::iterator
43 I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
47 const CodeGenOptions &CodeGenTypes::getCodeGenOpts() const {
48 return CGM.getCodeGenOpts();
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).second)
124 const Type *Key = CGT.getContext().getTagDeclType(RD).getTypePtr();
126 // If this type is already laid out, converting it is a noop.
127 if (CGT.isRecordLayoutComplete(Key)) return true;
129 // If this type is currently being laid out, we can't recursively compile it.
130 if (CGT.isRecordBeingLaidOut(Key))
133 // If this type would require laying out bases that are currently being laid
134 // out, don't do it. This includes virtual base classes which get laid out
135 // when a class is translated, even though they aren't embedded by-value into
137 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
138 for (const auto &I : CRD->bases())
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 (const auto *I : RD->fields())
147 if (!isSafeToConvert(I->getType(), CGT, AlreadyChecked))
150 // If there are no problems, lets do it.
154 /// isSafeToConvert - Return true if it is safe to convert this field type,
155 /// which requires the structure elements contained by-value to all be
156 /// recursively safe to convert.
158 isSafeToConvert(QualType T, CodeGenTypes &CGT,
159 llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
160 // Strip off atomic type sugar.
161 if (const auto *AT = T->getAs<AtomicType>())
162 T = AT->getValueType();
164 // If this is a record, check it.
165 if (const auto *RT = T->getAs<RecordType>())
166 return isSafeToConvert(RT->getDecl(), CGT, AlreadyChecked);
168 // If this is an array, check the elements, which are embedded inline.
169 if (const auto *AT = CGT.getContext().getAsArrayType(T))
170 return isSafeToConvert(AT->getElementType(), CGT, AlreadyChecked);
172 // Otherwise, there is no concern about transforming this. We only care about
173 // things that are contained by-value in a structure that can have another
174 // structure as a member.
179 /// isSafeToConvert - Return true if it is safe to convert the specified record
180 /// decl to IR and lay it out, false if doing so would cause us to get into a
181 /// recursive compilation mess.
182 static bool isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT) {
183 // If no structs are being laid out, we can certainly do this one.
184 if (CGT.noRecordsBeingLaidOut()) return true;
186 llvm::SmallPtrSet<const RecordDecl*, 16> AlreadyChecked;
187 return isSafeToConvert(RD, CGT, AlreadyChecked);
190 /// isFuncParamTypeConvertible - Return true if the specified type in a
191 /// function parameter 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::isFuncParamTypeConvertible(QualType Ty) {
196 // Some ABIs cannot have their member pointers represented in IR unless
197 // certain circumstances have been reached.
198 if (const auto *MPT = Ty->getAs<MemberPointerType>())
199 return getCXXABI().isMemberPointerConvertible(MPT);
201 // If this isn't a tagged type, we can convert it!
202 const TagType *TT = Ty->getAs<TagType>();
203 if (!TT) return true;
205 // Incomplete types cannot be converted.
206 if (TT->isIncompleteType())
209 // If this is an enum, then it is always safe to convert.
210 const RecordType *RT = dyn_cast<RecordType>(TT);
211 if (!RT) return true;
213 // Otherwise, we have to be careful. If it is a struct that we're in the
214 // process of expanding, then we can't convert the function type. That's ok
215 // though because we must be in a pointer context under the struct, so we can
216 // just convert it to a dummy type.
218 // We decide this by checking whether ConvertRecordDeclType returns us an
219 // opaque type for a struct that we know is defined.
220 return isSafeToConvert(RT->getDecl(), *this);
224 /// Code to verify a given function type is complete, i.e. the return type
225 /// and all of the parameter types are complete. Also check to see if we are in
226 /// a RS_StructPointer context, and if so whether any struct types have been
227 /// pended. If so, we don't want to ask the ABI lowering code to handle a type
228 /// that cannot be converted to an IR type.
229 bool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) {
230 if (!isFuncParamTypeConvertible(FT->getReturnType()))
233 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
234 for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++)
235 if (!isFuncParamTypeConvertible(FPT->getParamType(i)))
241 /// UpdateCompletedType - When we find the full definition for a TagDecl,
242 /// replace the 'opaque' type we previously made for it if applicable.
243 void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
244 // If this is an enum being completed, then we flush all non-struct types from
245 // the cache. This allows function types and other things that may be derived
246 // from the enum to be recomputed.
247 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) {
248 // Only flush the cache if we've actually already converted this type.
249 if (TypeCache.count(ED->getTypeForDecl())) {
250 // Okay, we formed some types based on this. We speculated that the enum
251 // would be lowered to i32, so we only need to flush the cache if this
253 if (!ConvertType(ED->getIntegerType())->isIntegerTy(32))
256 // If necessary, provide the full definition of a type only used with a
257 // declaration so far.
258 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
259 DI->completeType(ED);
263 // If we completed a RecordDecl that we previously used and converted to an
264 // anonymous type, then go ahead and complete it now.
265 const RecordDecl *RD = cast<RecordDecl>(TD);
266 if (RD->isDependentType()) return;
268 // Only complete it if we converted it already. If we haven't converted it
269 // yet, we'll just do it lazily.
270 if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr()))
271 ConvertRecordDeclType(RD);
273 // If necessary, provide the full definition of a type only used with a
274 // declaration so far.
275 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
276 DI->completeType(RD);
279 void CodeGenTypes::RefreshTypeCacheForClass(const CXXRecordDecl *RD) {
280 QualType T = Context.getRecordType(RD);
281 T = Context.getCanonicalType(T);
283 const Type *Ty = T.getTypePtr();
284 if (RecordsWithOpaqueMemberPointers.count(Ty)) {
286 RecordsWithOpaqueMemberPointers.clear();
290 static llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext,
291 const llvm::fltSemantics &format,
292 bool UseNativeHalf = false) {
293 if (&format == &llvm::APFloat::IEEEhalf()) {
295 return llvm::Type::getHalfTy(VMContext);
297 return llvm::Type::getInt16Ty(VMContext);
299 if (&format == &llvm::APFloat::IEEEsingle())
300 return llvm::Type::getFloatTy(VMContext);
301 if (&format == &llvm::APFloat::IEEEdouble())
302 return llvm::Type::getDoubleTy(VMContext);
303 if (&format == &llvm::APFloat::IEEEquad())
304 return llvm::Type::getFP128Ty(VMContext);
305 if (&format == &llvm::APFloat::PPCDoubleDouble())
306 return llvm::Type::getPPC_FP128Ty(VMContext);
307 if (&format == &llvm::APFloat::x87DoubleExtended())
308 return llvm::Type::getX86_FP80Ty(VMContext);
309 llvm_unreachable("Unknown float format!");
312 llvm::Type *CodeGenTypes::ConvertFunctionType(QualType QFT,
313 const FunctionDecl *FD) {
314 assert(QFT.isCanonical());
315 const Type *Ty = QFT.getTypePtr();
316 const FunctionType *FT = cast<FunctionType>(QFT.getTypePtr());
317 // First, check whether we can build the full function type. If the
318 // function type depends on an incomplete type (e.g. a struct or enum), we
319 // cannot lower the function type.
320 if (!isFuncTypeConvertible(FT)) {
321 // This function's type depends on an incomplete tag type.
323 // Force conversion of all the relevant record types, to make sure
324 // we re-convert the FunctionType when appropriate.
325 if (const RecordType *RT = FT->getReturnType()->getAs<RecordType>())
326 ConvertRecordDeclType(RT->getDecl());
327 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
328 for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++)
329 if (const RecordType *RT = FPT->getParamType(i)->getAs<RecordType>())
330 ConvertRecordDeclType(RT->getDecl());
332 SkippedLayout = true;
334 // Return a placeholder type.
335 return llvm::StructType::get(getLLVMContext());
338 // While we're converting the parameter types for a function, we don't want
339 // to recursively convert any pointed-to structs. Converting directly-used
340 // structs is ok though.
341 if (!RecordsBeingLaidOut.insert(Ty).second) {
342 SkippedLayout = true;
343 return llvm::StructType::get(getLLVMContext());
346 // The function type can be built; call the appropriate routines to
348 const CGFunctionInfo *FI;
349 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
350 FI = &arrangeFreeFunctionType(
351 CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0)), FD);
353 const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT);
354 FI = &arrangeFreeFunctionType(
355 CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0)));
358 llvm::Type *ResultType = nullptr;
359 // If there is something higher level prodding our CGFunctionInfo, then
360 // don't recurse into it again.
361 if (FunctionsBeingProcessed.count(FI)) {
363 ResultType = llvm::StructType::get(getLLVMContext());
364 SkippedLayout = true;
367 // Otherwise, we're good to go, go ahead and convert it.
368 ResultType = GetFunctionType(*FI);
371 RecordsBeingLaidOut.erase(Ty);
376 if (RecordsBeingLaidOut.empty())
377 while (!DeferredRecords.empty())
378 ConvertRecordDeclType(DeferredRecords.pop_back_val());
382 /// ConvertType - Convert the specified type to its LLVM form.
383 llvm::Type *CodeGenTypes::ConvertType(QualType T) {
384 T = Context.getCanonicalType(T);
386 const Type *Ty = T.getTypePtr();
388 // RecordTypes are cached and processed specially.
389 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
390 return ConvertRecordDeclType(RT->getDecl());
392 // See if type is already cached.
393 llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI = TypeCache.find(Ty);
394 // If type is found in map then use it. Otherwise, convert type T.
395 if (TCI != TypeCache.end())
398 // If we don't have it in the cache, convert it now.
399 llvm::Type *ResultType = nullptr;
400 switch (Ty->getTypeClass()) {
401 case Type::Record: // Handled above.
402 #define TYPE(Class, Base)
403 #define ABSTRACT_TYPE(Class, Base)
404 #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
405 #define DEPENDENT_TYPE(Class, Base) case Type::Class:
406 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
407 #include "clang/AST/TypeNodes.def"
408 llvm_unreachable("Non-canonical or dependent types aren't possible.");
410 case Type::Builtin: {
411 switch (cast<BuiltinType>(Ty)->getKind()) {
412 case BuiltinType::Void:
413 case BuiltinType::ObjCId:
414 case BuiltinType::ObjCClass:
415 case BuiltinType::ObjCSel:
416 // LLVM void type can only be used as the result of a function call. Just
417 // map to the same as char.
418 ResultType = llvm::Type::getInt8Ty(getLLVMContext());
421 case BuiltinType::Bool:
422 // Note that we always return bool as i1 for use as a scalar type.
423 ResultType = llvm::Type::getInt1Ty(getLLVMContext());
426 case BuiltinType::Char_S:
427 case BuiltinType::Char_U:
428 case BuiltinType::SChar:
429 case BuiltinType::UChar:
430 case BuiltinType::Short:
431 case BuiltinType::UShort:
432 case BuiltinType::Int:
433 case BuiltinType::UInt:
434 case BuiltinType::Long:
435 case BuiltinType::ULong:
436 case BuiltinType::LongLong:
437 case BuiltinType::ULongLong:
438 case BuiltinType::WChar_S:
439 case BuiltinType::WChar_U:
440 case BuiltinType::Char16:
441 case BuiltinType::Char32:
442 ResultType = llvm::IntegerType::get(getLLVMContext(),
443 static_cast<unsigned>(Context.getTypeSize(T)));
446 case BuiltinType::Float16:
448 getTypeForFormat(getLLVMContext(), Context.getFloatTypeSemantics(T),
449 /* UseNativeHalf = */ true);
452 case BuiltinType::Half:
453 // Half FP can either be storage-only (lowered to i16) or native.
454 ResultType = getTypeForFormat(
455 getLLVMContext(), Context.getFloatTypeSemantics(T),
456 Context.getLangOpts().NativeHalfType ||
457 !Context.getTargetInfo().useFP16ConversionIntrinsics());
459 case BuiltinType::Float:
460 case BuiltinType::Double:
461 case BuiltinType::LongDouble:
462 case BuiltinType::Float128:
463 ResultType = getTypeForFormat(getLLVMContext(),
464 Context.getFloatTypeSemantics(T),
465 /* UseNativeHalf = */ false);
468 case BuiltinType::NullPtr:
469 // Model std::nullptr_t as i8*
470 ResultType = llvm::Type::getInt8PtrTy(getLLVMContext());
473 case BuiltinType::UInt128:
474 case BuiltinType::Int128:
475 ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
478 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
479 case BuiltinType::Id:
480 #include "clang/Basic/OpenCLImageTypes.def"
481 case BuiltinType::OCLSampler:
482 case BuiltinType::OCLEvent:
483 case BuiltinType::OCLClkEvent:
484 case BuiltinType::OCLQueue:
485 case BuiltinType::OCLReserveID:
486 ResultType = CGM.getOpenCLRuntime().convertOpenCLSpecificType(Ty);
489 case BuiltinType::Dependent:
490 #define BUILTIN_TYPE(Id, SingletonId)
491 #define PLACEHOLDER_TYPE(Id, SingletonId) \
492 case BuiltinType::Id:
493 #include "clang/AST/BuiltinTypes.def"
494 llvm_unreachable("Unexpected placeholder builtin type!");
499 case Type::DeducedTemplateSpecialization:
500 llvm_unreachable("Unexpected undeduced type!");
501 case Type::Complex: {
502 llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType());
503 ResultType = llvm::StructType::get(EltTy, EltTy);
506 case Type::LValueReference:
507 case Type::RValueReference: {
508 const ReferenceType *RTy = cast<ReferenceType>(Ty);
509 QualType ETy = RTy->getPointeeType();
510 llvm::Type *PointeeType = ConvertTypeForMem(ETy);
511 unsigned AS = Context.getTargetAddressSpace(ETy);
512 ResultType = llvm::PointerType::get(PointeeType, AS);
515 case Type::Pointer: {
516 const PointerType *PTy = cast<PointerType>(Ty);
517 QualType ETy = PTy->getPointeeType();
518 llvm::Type *PointeeType = ConvertTypeForMem(ETy);
519 if (PointeeType->isVoidTy())
520 PointeeType = llvm::Type::getInt8Ty(getLLVMContext());
521 unsigned AS = Context.getTargetAddressSpace(ETy);
522 ResultType = llvm::PointerType::get(PointeeType, AS);
526 case Type::VariableArray: {
527 const VariableArrayType *A = cast<VariableArrayType>(Ty);
528 assert(A->getIndexTypeCVRQualifiers() == 0 &&
529 "FIXME: We only handle trivial array types so far!");
530 // VLAs resolve to the innermost element type; this matches
531 // the return of alloca, and there isn't any obviously better choice.
532 ResultType = ConvertTypeForMem(A->getElementType());
535 case Type::IncompleteArray: {
536 const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
537 assert(A->getIndexTypeCVRQualifiers() == 0 &&
538 "FIXME: We only handle trivial array types so far!");
539 // int X[] -> [0 x int], unless the element type is not sized. If it is
540 // unsized (e.g. an incomplete struct) just use [0 x i8].
541 ResultType = ConvertTypeForMem(A->getElementType());
542 if (!ResultType->isSized()) {
543 SkippedLayout = true;
544 ResultType = llvm::Type::getInt8Ty(getLLVMContext());
546 ResultType = llvm::ArrayType::get(ResultType, 0);
549 case Type::ConstantArray: {
550 const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
551 llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
553 // Lower arrays of undefined struct type to arrays of i8 just to have a
555 if (!EltTy->isSized()) {
556 SkippedLayout = true;
557 EltTy = llvm::Type::getInt8Ty(getLLVMContext());
560 ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue());
563 case Type::ExtVector:
565 const VectorType *VT = cast<VectorType>(Ty);
566 ResultType = llvm::VectorType::get(ConvertType(VT->getElementType()),
567 VT->getNumElements());
570 case Type::FunctionNoProto:
571 case Type::FunctionProto:
572 ResultType = ConvertFunctionType(T);
574 case Type::ObjCObject:
575 ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
578 case Type::ObjCInterface: {
579 // Objective-C interfaces are always opaque (outside of the
580 // runtime, which can do whatever it likes); we never refine
582 llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
584 T = llvm::StructType::create(getLLVMContext());
589 case Type::ObjCObjectPointer: {
590 // Protocol qualifications do not influence the LLVM type, we just return a
591 // pointer to the underlying interface type. We don't need to worry about
592 // recursive conversion.
594 ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
595 ResultType = T->getPointerTo();
600 const EnumDecl *ED = cast<EnumType>(Ty)->getDecl();
601 if (ED->isCompleteDefinition() || ED->isFixed())
602 return ConvertType(ED->getIntegerType());
603 // Return a placeholder 'i32' type. This can be changed later when the
604 // type is defined (see UpdateCompletedType), but is likely to be the
606 ResultType = llvm::Type::getInt32Ty(getLLVMContext());
610 case Type::BlockPointer: {
611 const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
612 llvm::Type *PointeeType = ConvertTypeForMem(FTy);
613 unsigned AS = Context.getTargetAddressSpace(FTy);
614 ResultType = llvm::PointerType::get(PointeeType, AS);
618 case Type::MemberPointer: {
619 auto *MPTy = cast<MemberPointerType>(Ty);
620 if (!getCXXABI().isMemberPointerConvertible(MPTy)) {
621 RecordsWithOpaqueMemberPointers.insert(MPTy->getClass());
622 ResultType = llvm::StructType::create(getLLVMContext());
624 ResultType = getCXXABI().ConvertMemberPointerType(MPTy);
630 QualType valueType = cast<AtomicType>(Ty)->getValueType();
631 ResultType = ConvertTypeForMem(valueType);
633 // Pad out to the inflated size if necessary.
634 uint64_t valueSize = Context.getTypeSize(valueType);
635 uint64_t atomicSize = Context.getTypeSize(Ty);
636 if (valueSize != atomicSize) {
637 assert(valueSize < atomicSize);
638 llvm::Type *elts[] = {
640 llvm::ArrayType::get(CGM.Int8Ty, (atomicSize - valueSize) / 8)
642 ResultType = llvm::StructType::get(getLLVMContext(),
643 llvm::makeArrayRef(elts));
648 ResultType = CGM.getOpenCLRuntime().getPipeType(cast<PipeType>(Ty));
653 assert(ResultType && "Didn't convert a type?");
655 TypeCache[Ty] = ResultType;
659 bool CodeGenModule::isPaddedAtomicType(QualType type) {
660 return isPaddedAtomicType(type->castAs<AtomicType>());
663 bool CodeGenModule::isPaddedAtomicType(const AtomicType *type) {
664 return Context.getTypeSize(type) != Context.getTypeSize(type->getValueType());
667 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
668 llvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) {
669 // TagDecl's are not necessarily unique, instead use the (clang)
670 // type connected to the decl.
671 const Type *Key = Context.getTagDeclType(RD).getTypePtr();
673 llvm::StructType *&Entry = RecordDeclTypes[Key];
675 // If we don't have a StructType at all yet, create the forward declaration.
677 Entry = llvm::StructType::create(getLLVMContext());
678 addRecordTypeName(RD, Entry, "");
680 llvm::StructType *Ty = Entry;
682 // If this is still a forward declaration, or the LLVM type is already
683 // complete, there's nothing more to do.
684 RD = RD->getDefinition();
685 if (!RD || !RD->isCompleteDefinition() || !Ty->isOpaque())
688 // If converting this type would cause us to infinitely loop, don't do it!
689 if (!isSafeToConvert(RD, *this)) {
690 DeferredRecords.push_back(RD);
694 // Okay, this is a definition of a type. Compile the implementation now.
695 bool InsertResult = RecordsBeingLaidOut.insert(Key).second;
697 assert(InsertResult && "Recursively compiling a struct?");
699 // Force conversion of non-virtual base classes recursively.
700 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
701 for (const auto &I : CRD->bases()) {
702 if (I.isVirtual()) continue;
704 ConvertRecordDeclType(I.getType()->getAs<RecordType>()->getDecl());
709 CGRecordLayout *Layout = ComputeRecordLayout(RD, Ty);
710 CGRecordLayouts[Key] = Layout;
712 // We're done laying out this struct.
713 bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult;
714 assert(EraseResult && "struct not in RecordsBeingLaidOut set?");
716 // If this struct blocked a FunctionType conversion, then recompute whatever
717 // was derived from that.
718 // FIXME: This is hugely overconservative.
722 // If we're done converting the outer-most record, then convert any deferred
724 if (RecordsBeingLaidOut.empty())
725 while (!DeferredRecords.empty())
726 ConvertRecordDeclType(DeferredRecords.pop_back_val());
731 /// getCGRecordLayout - Return record layout info for the given record decl.
732 const CGRecordLayout &
733 CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
734 const Type *Key = Context.getTagDeclType(RD).getTypePtr();
736 const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key);
738 // Compute the type information.
739 ConvertRecordDeclType(RD);
742 Layout = CGRecordLayouts.lookup(Key);
745 assert(Layout && "Unable to find record layout information for type");
749 bool CodeGenTypes::isPointerZeroInitializable(QualType T) {
750 assert((T->isAnyPointerType() || T->isBlockPointerType()) && "Invalid type");
751 return isZeroInitializable(T);
754 bool CodeGenTypes::isZeroInitializable(QualType T) {
755 if (T->getAs<PointerType>())
756 return Context.getTargetNullPointerValue(T) == 0;
758 if (const auto *AT = Context.getAsArrayType(T)) {
759 if (isa<IncompleteArrayType>(AT))
761 if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
762 if (Context.getConstantArrayElementCount(CAT) == 0)
764 T = Context.getBaseElementType(T);
767 // Records are non-zero-initializable if they contain any
768 // non-zero-initializable subobjects.
769 if (const RecordType *RT = T->getAs<RecordType>()) {
770 auto RD = cast<RecordDecl>(RT->getDecl());
771 return isZeroInitializable(RD);
774 // We have to ask the ABI about member pointers.
775 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
776 return getCXXABI().isZeroInitializable(MPT);
778 // Everything else is okay.
782 bool CodeGenTypes::isZeroInitializable(const RecordDecl *RD) {
783 return getCGRecordLayout(RD).isZeroInitializable();