106f32e7eSjoerg //===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===//
206f32e7eSjoerg //
306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f32e7eSjoerg //
706f32e7eSjoerg //===----------------------------------------------------------------------===//
806f32e7eSjoerg //
906f32e7eSjoerg // This is the code that handles AST -> LLVM type lowering.
1006f32e7eSjoerg //
1106f32e7eSjoerg //===----------------------------------------------------------------------===//
1206f32e7eSjoerg 
1306f32e7eSjoerg #include "CodeGenTypes.h"
1406f32e7eSjoerg #include "CGCXXABI.h"
1506f32e7eSjoerg #include "CGCall.h"
1606f32e7eSjoerg #include "CGOpenCLRuntime.h"
1706f32e7eSjoerg #include "CGRecordLayout.h"
1806f32e7eSjoerg #include "TargetInfo.h"
1906f32e7eSjoerg #include "clang/AST/ASTContext.h"
2006f32e7eSjoerg #include "clang/AST/DeclCXX.h"
2106f32e7eSjoerg #include "clang/AST/DeclObjC.h"
2206f32e7eSjoerg #include "clang/AST/Expr.h"
2306f32e7eSjoerg #include "clang/AST/RecordLayout.h"
2406f32e7eSjoerg #include "clang/CodeGen/CGFunctionInfo.h"
2506f32e7eSjoerg #include "llvm/IR/DataLayout.h"
2606f32e7eSjoerg #include "llvm/IR/DerivedTypes.h"
2706f32e7eSjoerg #include "llvm/IR/Module.h"
2806f32e7eSjoerg using namespace clang;
2906f32e7eSjoerg using namespace CodeGen;
3006f32e7eSjoerg 
CodeGenTypes(CodeGenModule & cgm)3106f32e7eSjoerg CodeGenTypes::CodeGenTypes(CodeGenModule &cgm)
3206f32e7eSjoerg   : CGM(cgm), Context(cgm.getContext()), TheModule(cgm.getModule()),
3306f32e7eSjoerg     Target(cgm.getTarget()), TheCXXABI(cgm.getCXXABI()),
3406f32e7eSjoerg     TheABIInfo(cgm.getTargetCodeGenInfo().getABIInfo()) {
3506f32e7eSjoerg   SkippedLayout = false;
3606f32e7eSjoerg }
3706f32e7eSjoerg 
~CodeGenTypes()3806f32e7eSjoerg CodeGenTypes::~CodeGenTypes() {
3906f32e7eSjoerg   for (llvm::FoldingSet<CGFunctionInfo>::iterator
4006f32e7eSjoerg        I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
4106f32e7eSjoerg     delete &*I++;
4206f32e7eSjoerg }
4306f32e7eSjoerg 
getCodeGenOpts() const4406f32e7eSjoerg const CodeGenOptions &CodeGenTypes::getCodeGenOpts() const {
4506f32e7eSjoerg   return CGM.getCodeGenOpts();
4606f32e7eSjoerg }
4706f32e7eSjoerg 
addRecordTypeName(const RecordDecl * RD,llvm::StructType * Ty,StringRef suffix)4806f32e7eSjoerg void CodeGenTypes::addRecordTypeName(const RecordDecl *RD,
4906f32e7eSjoerg                                      llvm::StructType *Ty,
5006f32e7eSjoerg                                      StringRef suffix) {
5106f32e7eSjoerg   SmallString<256> TypeName;
5206f32e7eSjoerg   llvm::raw_svector_ostream OS(TypeName);
5306f32e7eSjoerg   OS << RD->getKindName() << '.';
5406f32e7eSjoerg 
55*13fbcb42Sjoerg   // FIXME: We probably want to make more tweaks to the printing policy. For
56*13fbcb42Sjoerg   // example, we should probably enable PrintCanonicalTypes and
57*13fbcb42Sjoerg   // FullyQualifiedNames.
58*13fbcb42Sjoerg   PrintingPolicy Policy = RD->getASTContext().getPrintingPolicy();
59*13fbcb42Sjoerg   Policy.SuppressInlineNamespace = false;
60*13fbcb42Sjoerg 
6106f32e7eSjoerg   // Name the codegen type after the typedef name
6206f32e7eSjoerg   // if there is no tag type name available
6306f32e7eSjoerg   if (RD->getIdentifier()) {
6406f32e7eSjoerg     // FIXME: We should not have to check for a null decl context here.
6506f32e7eSjoerg     // Right now we do it because the implicit Obj-C decls don't have one.
6606f32e7eSjoerg     if (RD->getDeclContext())
67*13fbcb42Sjoerg       RD->printQualifiedName(OS, Policy);
6806f32e7eSjoerg     else
6906f32e7eSjoerg       RD->printName(OS);
7006f32e7eSjoerg   } else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) {
7106f32e7eSjoerg     // FIXME: We should not have to check for a null decl context here.
7206f32e7eSjoerg     // Right now we do it because the implicit Obj-C decls don't have one.
7306f32e7eSjoerg     if (TDD->getDeclContext())
74*13fbcb42Sjoerg       TDD->printQualifiedName(OS, Policy);
7506f32e7eSjoerg     else
7606f32e7eSjoerg       TDD->printName(OS);
7706f32e7eSjoerg   } else
7806f32e7eSjoerg     OS << "anon";
7906f32e7eSjoerg 
8006f32e7eSjoerg   if (!suffix.empty())
8106f32e7eSjoerg     OS << suffix;
8206f32e7eSjoerg 
8306f32e7eSjoerg   Ty->setName(OS.str());
8406f32e7eSjoerg }
8506f32e7eSjoerg 
8606f32e7eSjoerg /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
8706f32e7eSjoerg /// ConvertType in that it is used to convert to the memory representation for
8806f32e7eSjoerg /// a type.  For example, the scalar representation for _Bool is i1, but the
8906f32e7eSjoerg /// memory representation is usually i8 or i32, depending on the target.
ConvertTypeForMem(QualType T,bool ForBitField)90*13fbcb42Sjoerg llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T, bool ForBitField) {
91*13fbcb42Sjoerg   if (T->isConstantMatrixType()) {
92*13fbcb42Sjoerg     const Type *Ty = Context.getCanonicalType(T).getTypePtr();
93*13fbcb42Sjoerg     const ConstantMatrixType *MT = cast<ConstantMatrixType>(Ty);
94*13fbcb42Sjoerg     return llvm::ArrayType::get(ConvertType(MT->getElementType()),
95*13fbcb42Sjoerg                                 MT->getNumRows() * MT->getNumColumns());
9606f32e7eSjoerg   }
9706f32e7eSjoerg 
98*13fbcb42Sjoerg   llvm::Type *R = ConvertType(T);
99*13fbcb42Sjoerg 
100*13fbcb42Sjoerg   // If this is a bool type, or an ExtIntType in a bitfield representation,
101*13fbcb42Sjoerg   // map this integer to the target-specified size.
102*13fbcb42Sjoerg   if ((ForBitField && T->isExtIntType()) ||
103*13fbcb42Sjoerg       (!T->isExtIntType() && R->isIntegerTy(1)))
104*13fbcb42Sjoerg     return llvm::IntegerType::get(getLLVMContext(),
105*13fbcb42Sjoerg                                   (unsigned)Context.getTypeSize(T));
106*13fbcb42Sjoerg 
107*13fbcb42Sjoerg   // Else, don't map it.
108*13fbcb42Sjoerg   return R;
109*13fbcb42Sjoerg }
11006f32e7eSjoerg 
11106f32e7eSjoerg /// isRecordLayoutComplete - Return true if the specified type is already
11206f32e7eSjoerg /// completely laid out.
isRecordLayoutComplete(const Type * Ty) const11306f32e7eSjoerg bool CodeGenTypes::isRecordLayoutComplete(const Type *Ty) const {
11406f32e7eSjoerg   llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I =
11506f32e7eSjoerg   RecordDeclTypes.find(Ty);
11606f32e7eSjoerg   return I != RecordDeclTypes.end() && !I->second->isOpaque();
11706f32e7eSjoerg }
11806f32e7eSjoerg 
11906f32e7eSjoerg static bool
12006f32e7eSjoerg isSafeToConvert(QualType T, CodeGenTypes &CGT,
12106f32e7eSjoerg                 llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked);
12206f32e7eSjoerg 
12306f32e7eSjoerg 
12406f32e7eSjoerg /// isSafeToConvert - Return true if it is safe to convert the specified record
12506f32e7eSjoerg /// decl to IR and lay it out, false if doing so would cause us to get into a
12606f32e7eSjoerg /// recursive compilation mess.
12706f32e7eSjoerg static bool
isSafeToConvert(const RecordDecl * RD,CodeGenTypes & CGT,llvm::SmallPtrSet<const RecordDecl *,16> & AlreadyChecked)12806f32e7eSjoerg isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT,
12906f32e7eSjoerg                 llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
13006f32e7eSjoerg   // If we have already checked this type (maybe the same type is used by-value
13106f32e7eSjoerg   // multiple times in multiple structure fields, don't check again.
13206f32e7eSjoerg   if (!AlreadyChecked.insert(RD).second)
13306f32e7eSjoerg     return true;
13406f32e7eSjoerg 
13506f32e7eSjoerg   const Type *Key = CGT.getContext().getTagDeclType(RD).getTypePtr();
13606f32e7eSjoerg 
13706f32e7eSjoerg   // If this type is already laid out, converting it is a noop.
13806f32e7eSjoerg   if (CGT.isRecordLayoutComplete(Key)) return true;
13906f32e7eSjoerg 
14006f32e7eSjoerg   // If this type is currently being laid out, we can't recursively compile it.
14106f32e7eSjoerg   if (CGT.isRecordBeingLaidOut(Key))
14206f32e7eSjoerg     return false;
14306f32e7eSjoerg 
14406f32e7eSjoerg   // If this type would require laying out bases that are currently being laid
14506f32e7eSjoerg   // out, don't do it.  This includes virtual base classes which get laid out
14606f32e7eSjoerg   // when a class is translated, even though they aren't embedded by-value into
14706f32e7eSjoerg   // the class.
14806f32e7eSjoerg   if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
14906f32e7eSjoerg     for (const auto &I : CRD->bases())
15006f32e7eSjoerg       if (!isSafeToConvert(I.getType()->castAs<RecordType>()->getDecl(), CGT,
15106f32e7eSjoerg                            AlreadyChecked))
15206f32e7eSjoerg         return false;
15306f32e7eSjoerg   }
15406f32e7eSjoerg 
15506f32e7eSjoerg   // If this type would require laying out members that are currently being laid
15606f32e7eSjoerg   // out, don't do it.
15706f32e7eSjoerg   for (const auto *I : RD->fields())
15806f32e7eSjoerg     if (!isSafeToConvert(I->getType(), CGT, AlreadyChecked))
15906f32e7eSjoerg       return false;
16006f32e7eSjoerg 
16106f32e7eSjoerg   // If there are no problems, lets do it.
16206f32e7eSjoerg   return true;
16306f32e7eSjoerg }
16406f32e7eSjoerg 
16506f32e7eSjoerg /// isSafeToConvert - Return true if it is safe to convert this field type,
16606f32e7eSjoerg /// which requires the structure elements contained by-value to all be
16706f32e7eSjoerg /// recursively safe to convert.
16806f32e7eSjoerg static bool
isSafeToConvert(QualType T,CodeGenTypes & CGT,llvm::SmallPtrSet<const RecordDecl *,16> & AlreadyChecked)16906f32e7eSjoerg isSafeToConvert(QualType T, CodeGenTypes &CGT,
17006f32e7eSjoerg                 llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
17106f32e7eSjoerg   // Strip off atomic type sugar.
17206f32e7eSjoerg   if (const auto *AT = T->getAs<AtomicType>())
17306f32e7eSjoerg     T = AT->getValueType();
17406f32e7eSjoerg 
17506f32e7eSjoerg   // If this is a record, check it.
17606f32e7eSjoerg   if (const auto *RT = T->getAs<RecordType>())
17706f32e7eSjoerg     return isSafeToConvert(RT->getDecl(), CGT, AlreadyChecked);
17806f32e7eSjoerg 
17906f32e7eSjoerg   // If this is an array, check the elements, which are embedded inline.
18006f32e7eSjoerg   if (const auto *AT = CGT.getContext().getAsArrayType(T))
18106f32e7eSjoerg     return isSafeToConvert(AT->getElementType(), CGT, AlreadyChecked);
18206f32e7eSjoerg 
18306f32e7eSjoerg   // Otherwise, there is no concern about transforming this.  We only care about
18406f32e7eSjoerg   // things that are contained by-value in a structure that can have another
18506f32e7eSjoerg   // structure as a member.
18606f32e7eSjoerg   return true;
18706f32e7eSjoerg }
18806f32e7eSjoerg 
18906f32e7eSjoerg 
19006f32e7eSjoerg /// isSafeToConvert - Return true if it is safe to convert the specified record
19106f32e7eSjoerg /// decl to IR and lay it out, false if doing so would cause us to get into a
19206f32e7eSjoerg /// recursive compilation mess.
isSafeToConvert(const RecordDecl * RD,CodeGenTypes & CGT)19306f32e7eSjoerg static bool isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT) {
19406f32e7eSjoerg   // If no structs are being laid out, we can certainly do this one.
19506f32e7eSjoerg   if (CGT.noRecordsBeingLaidOut()) return true;
19606f32e7eSjoerg 
19706f32e7eSjoerg   llvm::SmallPtrSet<const RecordDecl*, 16> AlreadyChecked;
19806f32e7eSjoerg   return isSafeToConvert(RD, CGT, AlreadyChecked);
19906f32e7eSjoerg }
20006f32e7eSjoerg 
20106f32e7eSjoerg /// isFuncParamTypeConvertible - Return true if the specified type in a
20206f32e7eSjoerg /// function parameter or result position can be converted to an IR type at this
20306f32e7eSjoerg /// point.  This boils down to being whether it is complete, as well as whether
20406f32e7eSjoerg /// we've temporarily deferred expanding the type because we're in a recursive
20506f32e7eSjoerg /// context.
isFuncParamTypeConvertible(QualType Ty)20606f32e7eSjoerg bool CodeGenTypes::isFuncParamTypeConvertible(QualType Ty) {
20706f32e7eSjoerg   // Some ABIs cannot have their member pointers represented in IR unless
20806f32e7eSjoerg   // certain circumstances have been reached.
20906f32e7eSjoerg   if (const auto *MPT = Ty->getAs<MemberPointerType>())
21006f32e7eSjoerg     return getCXXABI().isMemberPointerConvertible(MPT);
21106f32e7eSjoerg 
21206f32e7eSjoerg   // If this isn't a tagged type, we can convert it!
21306f32e7eSjoerg   const TagType *TT = Ty->getAs<TagType>();
21406f32e7eSjoerg   if (!TT) return true;
21506f32e7eSjoerg 
21606f32e7eSjoerg   // Incomplete types cannot be converted.
21706f32e7eSjoerg   if (TT->isIncompleteType())
21806f32e7eSjoerg     return false;
21906f32e7eSjoerg 
22006f32e7eSjoerg   // If this is an enum, then it is always safe to convert.
22106f32e7eSjoerg   const RecordType *RT = dyn_cast<RecordType>(TT);
22206f32e7eSjoerg   if (!RT) return true;
22306f32e7eSjoerg 
22406f32e7eSjoerg   // Otherwise, we have to be careful.  If it is a struct that we're in the
22506f32e7eSjoerg   // process of expanding, then we can't convert the function type.  That's ok
22606f32e7eSjoerg   // though because we must be in a pointer context under the struct, so we can
22706f32e7eSjoerg   // just convert it to a dummy type.
22806f32e7eSjoerg   //
22906f32e7eSjoerg   // We decide this by checking whether ConvertRecordDeclType returns us an
23006f32e7eSjoerg   // opaque type for a struct that we know is defined.
23106f32e7eSjoerg   return isSafeToConvert(RT->getDecl(), *this);
23206f32e7eSjoerg }
23306f32e7eSjoerg 
23406f32e7eSjoerg 
23506f32e7eSjoerg /// Code to verify a given function type is complete, i.e. the return type
23606f32e7eSjoerg /// and all of the parameter types are complete.  Also check to see if we are in
23706f32e7eSjoerg /// a RS_StructPointer context, and if so whether any struct types have been
23806f32e7eSjoerg /// pended.  If so, we don't want to ask the ABI lowering code to handle a type
23906f32e7eSjoerg /// that cannot be converted to an IR type.
isFuncTypeConvertible(const FunctionType * FT)24006f32e7eSjoerg bool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) {
24106f32e7eSjoerg   if (!isFuncParamTypeConvertible(FT->getReturnType()))
24206f32e7eSjoerg     return false;
24306f32e7eSjoerg 
24406f32e7eSjoerg   if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
24506f32e7eSjoerg     for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++)
24606f32e7eSjoerg       if (!isFuncParamTypeConvertible(FPT->getParamType(i)))
24706f32e7eSjoerg         return false;
24806f32e7eSjoerg 
24906f32e7eSjoerg   return true;
25006f32e7eSjoerg }
25106f32e7eSjoerg 
25206f32e7eSjoerg /// UpdateCompletedType - When we find the full definition for a TagDecl,
25306f32e7eSjoerg /// replace the 'opaque' type we previously made for it if applicable.
UpdateCompletedType(const TagDecl * TD)25406f32e7eSjoerg void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
25506f32e7eSjoerg   // If this is an enum being completed, then we flush all non-struct types from
25606f32e7eSjoerg   // the cache.  This allows function types and other things that may be derived
25706f32e7eSjoerg   // from the enum to be recomputed.
25806f32e7eSjoerg   if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) {
25906f32e7eSjoerg     // Only flush the cache if we've actually already converted this type.
26006f32e7eSjoerg     if (TypeCache.count(ED->getTypeForDecl())) {
26106f32e7eSjoerg       // Okay, we formed some types based on this.  We speculated that the enum
26206f32e7eSjoerg       // would be lowered to i32, so we only need to flush the cache if this
26306f32e7eSjoerg       // didn't happen.
26406f32e7eSjoerg       if (!ConvertType(ED->getIntegerType())->isIntegerTy(32))
26506f32e7eSjoerg         TypeCache.clear();
26606f32e7eSjoerg     }
26706f32e7eSjoerg     // If necessary, provide the full definition of a type only used with a
26806f32e7eSjoerg     // declaration so far.
26906f32e7eSjoerg     if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
27006f32e7eSjoerg       DI->completeType(ED);
27106f32e7eSjoerg     return;
27206f32e7eSjoerg   }
27306f32e7eSjoerg 
27406f32e7eSjoerg   // If we completed a RecordDecl that we previously used and converted to an
27506f32e7eSjoerg   // anonymous type, then go ahead and complete it now.
27606f32e7eSjoerg   const RecordDecl *RD = cast<RecordDecl>(TD);
27706f32e7eSjoerg   if (RD->isDependentType()) return;
27806f32e7eSjoerg 
27906f32e7eSjoerg   // Only complete it if we converted it already.  If we haven't converted it
28006f32e7eSjoerg   // yet, we'll just do it lazily.
28106f32e7eSjoerg   if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr()))
28206f32e7eSjoerg     ConvertRecordDeclType(RD);
28306f32e7eSjoerg 
28406f32e7eSjoerg   // If necessary, provide the full definition of a type only used with a
28506f32e7eSjoerg   // declaration so far.
28606f32e7eSjoerg   if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
28706f32e7eSjoerg     DI->completeType(RD);
28806f32e7eSjoerg }
28906f32e7eSjoerg 
RefreshTypeCacheForClass(const CXXRecordDecl * RD)29006f32e7eSjoerg void CodeGenTypes::RefreshTypeCacheForClass(const CXXRecordDecl *RD) {
29106f32e7eSjoerg   QualType T = Context.getRecordType(RD);
29206f32e7eSjoerg   T = Context.getCanonicalType(T);
29306f32e7eSjoerg 
29406f32e7eSjoerg   const Type *Ty = T.getTypePtr();
29506f32e7eSjoerg   if (RecordsWithOpaqueMemberPointers.count(Ty)) {
29606f32e7eSjoerg     TypeCache.clear();
29706f32e7eSjoerg     RecordsWithOpaqueMemberPointers.clear();
29806f32e7eSjoerg   }
29906f32e7eSjoerg }
30006f32e7eSjoerg 
getTypeForFormat(llvm::LLVMContext & VMContext,const llvm::fltSemantics & format,bool UseNativeHalf=false)30106f32e7eSjoerg static llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext,
30206f32e7eSjoerg                                     const llvm::fltSemantics &format,
30306f32e7eSjoerg                                     bool UseNativeHalf = false) {
30406f32e7eSjoerg   if (&format == &llvm::APFloat::IEEEhalf()) {
30506f32e7eSjoerg     if (UseNativeHalf)
30606f32e7eSjoerg       return llvm::Type::getHalfTy(VMContext);
30706f32e7eSjoerg     else
30806f32e7eSjoerg       return llvm::Type::getInt16Ty(VMContext);
30906f32e7eSjoerg   }
310*13fbcb42Sjoerg   if (&format == &llvm::APFloat::BFloat())
311*13fbcb42Sjoerg     return llvm::Type::getBFloatTy(VMContext);
31206f32e7eSjoerg   if (&format == &llvm::APFloat::IEEEsingle())
31306f32e7eSjoerg     return llvm::Type::getFloatTy(VMContext);
31406f32e7eSjoerg   if (&format == &llvm::APFloat::IEEEdouble())
31506f32e7eSjoerg     return llvm::Type::getDoubleTy(VMContext);
31606f32e7eSjoerg   if (&format == &llvm::APFloat::IEEEquad())
31706f32e7eSjoerg     return llvm::Type::getFP128Ty(VMContext);
31806f32e7eSjoerg   if (&format == &llvm::APFloat::PPCDoubleDouble())
31906f32e7eSjoerg     return llvm::Type::getPPC_FP128Ty(VMContext);
32006f32e7eSjoerg   if (&format == &llvm::APFloat::x87DoubleExtended())
32106f32e7eSjoerg     return llvm::Type::getX86_FP80Ty(VMContext);
32206f32e7eSjoerg   llvm_unreachable("Unknown float format!");
32306f32e7eSjoerg }
32406f32e7eSjoerg 
ConvertFunctionTypeInternal(QualType QFT)32506f32e7eSjoerg llvm::Type *CodeGenTypes::ConvertFunctionTypeInternal(QualType QFT) {
32606f32e7eSjoerg   assert(QFT.isCanonical());
32706f32e7eSjoerg   const Type *Ty = QFT.getTypePtr();
32806f32e7eSjoerg   const FunctionType *FT = cast<FunctionType>(QFT.getTypePtr());
32906f32e7eSjoerg   // First, check whether we can build the full function type.  If the
33006f32e7eSjoerg   // function type depends on an incomplete type (e.g. a struct or enum), we
33106f32e7eSjoerg   // cannot lower the function type.
33206f32e7eSjoerg   if (!isFuncTypeConvertible(FT)) {
33306f32e7eSjoerg     // This function's type depends on an incomplete tag type.
33406f32e7eSjoerg 
33506f32e7eSjoerg     // Force conversion of all the relevant record types, to make sure
33606f32e7eSjoerg     // we re-convert the FunctionType when appropriate.
33706f32e7eSjoerg     if (const RecordType *RT = FT->getReturnType()->getAs<RecordType>())
33806f32e7eSjoerg       ConvertRecordDeclType(RT->getDecl());
33906f32e7eSjoerg     if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
34006f32e7eSjoerg       for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++)
34106f32e7eSjoerg         if (const RecordType *RT = FPT->getParamType(i)->getAs<RecordType>())
34206f32e7eSjoerg           ConvertRecordDeclType(RT->getDecl());
34306f32e7eSjoerg 
34406f32e7eSjoerg     SkippedLayout = true;
34506f32e7eSjoerg 
34606f32e7eSjoerg     // Return a placeholder type.
34706f32e7eSjoerg     return llvm::StructType::get(getLLVMContext());
34806f32e7eSjoerg   }
34906f32e7eSjoerg 
35006f32e7eSjoerg   // While we're converting the parameter types for a function, we don't want
35106f32e7eSjoerg   // to recursively convert any pointed-to structs.  Converting directly-used
35206f32e7eSjoerg   // structs is ok though.
35306f32e7eSjoerg   if (!RecordsBeingLaidOut.insert(Ty).second) {
35406f32e7eSjoerg     SkippedLayout = true;
35506f32e7eSjoerg     return llvm::StructType::get(getLLVMContext());
35606f32e7eSjoerg   }
35706f32e7eSjoerg 
35806f32e7eSjoerg   // The function type can be built; call the appropriate routines to
35906f32e7eSjoerg   // build it.
36006f32e7eSjoerg   const CGFunctionInfo *FI;
36106f32e7eSjoerg   if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
36206f32e7eSjoerg     FI = &arrangeFreeFunctionType(
36306f32e7eSjoerg         CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0)));
36406f32e7eSjoerg   } else {
36506f32e7eSjoerg     const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT);
36606f32e7eSjoerg     FI = &arrangeFreeFunctionType(
36706f32e7eSjoerg         CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0)));
36806f32e7eSjoerg   }
36906f32e7eSjoerg 
37006f32e7eSjoerg   llvm::Type *ResultType = nullptr;
37106f32e7eSjoerg   // If there is something higher level prodding our CGFunctionInfo, then
37206f32e7eSjoerg   // don't recurse into it again.
37306f32e7eSjoerg   if (FunctionsBeingProcessed.count(FI)) {
37406f32e7eSjoerg 
37506f32e7eSjoerg     ResultType = llvm::StructType::get(getLLVMContext());
37606f32e7eSjoerg     SkippedLayout = true;
37706f32e7eSjoerg   } else {
37806f32e7eSjoerg 
37906f32e7eSjoerg     // Otherwise, we're good to go, go ahead and convert it.
38006f32e7eSjoerg     ResultType = GetFunctionType(*FI);
38106f32e7eSjoerg   }
38206f32e7eSjoerg 
38306f32e7eSjoerg   RecordsBeingLaidOut.erase(Ty);
38406f32e7eSjoerg 
38506f32e7eSjoerg   if (SkippedLayout)
38606f32e7eSjoerg     TypeCache.clear();
38706f32e7eSjoerg 
38806f32e7eSjoerg   if (RecordsBeingLaidOut.empty())
38906f32e7eSjoerg     while (!DeferredRecords.empty())
39006f32e7eSjoerg       ConvertRecordDeclType(DeferredRecords.pop_back_val());
39106f32e7eSjoerg   return ResultType;
39206f32e7eSjoerg }
39306f32e7eSjoerg 
39406f32e7eSjoerg /// ConvertType - Convert the specified type to its LLVM form.
ConvertType(QualType T)39506f32e7eSjoerg llvm::Type *CodeGenTypes::ConvertType(QualType T) {
39606f32e7eSjoerg   T = Context.getCanonicalType(T);
39706f32e7eSjoerg 
39806f32e7eSjoerg   const Type *Ty = T.getTypePtr();
39906f32e7eSjoerg 
400*13fbcb42Sjoerg   // For the device-side compilation, CUDA device builtin surface/texture types
401*13fbcb42Sjoerg   // may be represented in different types.
402*13fbcb42Sjoerg   if (Context.getLangOpts().CUDAIsDevice) {
403*13fbcb42Sjoerg     if (T->isCUDADeviceBuiltinSurfaceType()) {
404*13fbcb42Sjoerg       if (auto *Ty = CGM.getTargetCodeGenInfo()
405*13fbcb42Sjoerg                          .getCUDADeviceBuiltinSurfaceDeviceType())
406*13fbcb42Sjoerg         return Ty;
407*13fbcb42Sjoerg     } else if (T->isCUDADeviceBuiltinTextureType()) {
408*13fbcb42Sjoerg       if (auto *Ty = CGM.getTargetCodeGenInfo()
409*13fbcb42Sjoerg                          .getCUDADeviceBuiltinTextureDeviceType())
410*13fbcb42Sjoerg         return Ty;
411*13fbcb42Sjoerg     }
412*13fbcb42Sjoerg   }
413*13fbcb42Sjoerg 
41406f32e7eSjoerg   // RecordTypes are cached and processed specially.
41506f32e7eSjoerg   if (const RecordType *RT = dyn_cast<RecordType>(Ty))
41606f32e7eSjoerg     return ConvertRecordDeclType(RT->getDecl());
41706f32e7eSjoerg 
41806f32e7eSjoerg   // See if type is already cached.
41906f32e7eSjoerg   llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI = TypeCache.find(Ty);
42006f32e7eSjoerg   // If type is found in map then use it. Otherwise, convert type T.
42106f32e7eSjoerg   if (TCI != TypeCache.end())
42206f32e7eSjoerg     return TCI->second;
42306f32e7eSjoerg 
42406f32e7eSjoerg   // If we don't have it in the cache, convert it now.
42506f32e7eSjoerg   llvm::Type *ResultType = nullptr;
42606f32e7eSjoerg   switch (Ty->getTypeClass()) {
42706f32e7eSjoerg   case Type::Record: // Handled above.
42806f32e7eSjoerg #define TYPE(Class, Base)
42906f32e7eSjoerg #define ABSTRACT_TYPE(Class, Base)
43006f32e7eSjoerg #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
43106f32e7eSjoerg #define DEPENDENT_TYPE(Class, Base) case Type::Class:
43206f32e7eSjoerg #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
43306f32e7eSjoerg #include "clang/AST/TypeNodes.inc"
43406f32e7eSjoerg     llvm_unreachable("Non-canonical or dependent types aren't possible.");
43506f32e7eSjoerg 
43606f32e7eSjoerg   case Type::Builtin: {
43706f32e7eSjoerg     switch (cast<BuiltinType>(Ty)->getKind()) {
43806f32e7eSjoerg     case BuiltinType::Void:
43906f32e7eSjoerg     case BuiltinType::ObjCId:
44006f32e7eSjoerg     case BuiltinType::ObjCClass:
44106f32e7eSjoerg     case BuiltinType::ObjCSel:
44206f32e7eSjoerg       // LLVM void type can only be used as the result of a function call.  Just
44306f32e7eSjoerg       // map to the same as char.
44406f32e7eSjoerg       ResultType = llvm::Type::getInt8Ty(getLLVMContext());
44506f32e7eSjoerg       break;
44606f32e7eSjoerg 
44706f32e7eSjoerg     case BuiltinType::Bool:
44806f32e7eSjoerg       // Note that we always return bool as i1 for use as a scalar type.
44906f32e7eSjoerg       ResultType = llvm::Type::getInt1Ty(getLLVMContext());
45006f32e7eSjoerg       break;
45106f32e7eSjoerg 
45206f32e7eSjoerg     case BuiltinType::Char_S:
45306f32e7eSjoerg     case BuiltinType::Char_U:
45406f32e7eSjoerg     case BuiltinType::SChar:
45506f32e7eSjoerg     case BuiltinType::UChar:
45606f32e7eSjoerg     case BuiltinType::Short:
45706f32e7eSjoerg     case BuiltinType::UShort:
45806f32e7eSjoerg     case BuiltinType::Int:
45906f32e7eSjoerg     case BuiltinType::UInt:
46006f32e7eSjoerg     case BuiltinType::Long:
46106f32e7eSjoerg     case BuiltinType::ULong:
46206f32e7eSjoerg     case BuiltinType::LongLong:
46306f32e7eSjoerg     case BuiltinType::ULongLong:
46406f32e7eSjoerg     case BuiltinType::WChar_S:
46506f32e7eSjoerg     case BuiltinType::WChar_U:
46606f32e7eSjoerg     case BuiltinType::Char8:
46706f32e7eSjoerg     case BuiltinType::Char16:
46806f32e7eSjoerg     case BuiltinType::Char32:
46906f32e7eSjoerg     case BuiltinType::ShortAccum:
47006f32e7eSjoerg     case BuiltinType::Accum:
47106f32e7eSjoerg     case BuiltinType::LongAccum:
47206f32e7eSjoerg     case BuiltinType::UShortAccum:
47306f32e7eSjoerg     case BuiltinType::UAccum:
47406f32e7eSjoerg     case BuiltinType::ULongAccum:
47506f32e7eSjoerg     case BuiltinType::ShortFract:
47606f32e7eSjoerg     case BuiltinType::Fract:
47706f32e7eSjoerg     case BuiltinType::LongFract:
47806f32e7eSjoerg     case BuiltinType::UShortFract:
47906f32e7eSjoerg     case BuiltinType::UFract:
48006f32e7eSjoerg     case BuiltinType::ULongFract:
48106f32e7eSjoerg     case BuiltinType::SatShortAccum:
48206f32e7eSjoerg     case BuiltinType::SatAccum:
48306f32e7eSjoerg     case BuiltinType::SatLongAccum:
48406f32e7eSjoerg     case BuiltinType::SatUShortAccum:
48506f32e7eSjoerg     case BuiltinType::SatUAccum:
48606f32e7eSjoerg     case BuiltinType::SatULongAccum:
48706f32e7eSjoerg     case BuiltinType::SatShortFract:
48806f32e7eSjoerg     case BuiltinType::SatFract:
48906f32e7eSjoerg     case BuiltinType::SatLongFract:
49006f32e7eSjoerg     case BuiltinType::SatUShortFract:
49106f32e7eSjoerg     case BuiltinType::SatUFract:
49206f32e7eSjoerg     case BuiltinType::SatULongFract:
49306f32e7eSjoerg       ResultType = llvm::IntegerType::get(getLLVMContext(),
49406f32e7eSjoerg                                  static_cast<unsigned>(Context.getTypeSize(T)));
49506f32e7eSjoerg       break;
49606f32e7eSjoerg 
49706f32e7eSjoerg     case BuiltinType::Float16:
49806f32e7eSjoerg       ResultType =
49906f32e7eSjoerg           getTypeForFormat(getLLVMContext(), Context.getFloatTypeSemantics(T),
50006f32e7eSjoerg                            /* UseNativeHalf = */ true);
50106f32e7eSjoerg       break;
50206f32e7eSjoerg 
50306f32e7eSjoerg     case BuiltinType::Half:
50406f32e7eSjoerg       // Half FP can either be storage-only (lowered to i16) or native.
50506f32e7eSjoerg       ResultType = getTypeForFormat(
50606f32e7eSjoerg           getLLVMContext(), Context.getFloatTypeSemantics(T),
50706f32e7eSjoerg           Context.getLangOpts().NativeHalfType ||
50806f32e7eSjoerg               !Context.getTargetInfo().useFP16ConversionIntrinsics());
50906f32e7eSjoerg       break;
510*13fbcb42Sjoerg     case BuiltinType::BFloat16:
51106f32e7eSjoerg     case BuiltinType::Float:
51206f32e7eSjoerg     case BuiltinType::Double:
51306f32e7eSjoerg     case BuiltinType::LongDouble:
51406f32e7eSjoerg     case BuiltinType::Float128:
51506f32e7eSjoerg       ResultType = getTypeForFormat(getLLVMContext(),
51606f32e7eSjoerg                                     Context.getFloatTypeSemantics(T),
51706f32e7eSjoerg                                     /* UseNativeHalf = */ false);
51806f32e7eSjoerg       break;
51906f32e7eSjoerg 
52006f32e7eSjoerg     case BuiltinType::NullPtr:
52106f32e7eSjoerg       // Model std::nullptr_t as i8*
52206f32e7eSjoerg       ResultType = llvm::Type::getInt8PtrTy(getLLVMContext());
52306f32e7eSjoerg       break;
52406f32e7eSjoerg 
52506f32e7eSjoerg     case BuiltinType::UInt128:
52606f32e7eSjoerg     case BuiltinType::Int128:
52706f32e7eSjoerg       ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
52806f32e7eSjoerg       break;
52906f32e7eSjoerg 
53006f32e7eSjoerg #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
53106f32e7eSjoerg     case BuiltinType::Id:
53206f32e7eSjoerg #include "clang/Basic/OpenCLImageTypes.def"
53306f32e7eSjoerg #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
53406f32e7eSjoerg     case BuiltinType::Id:
53506f32e7eSjoerg #include "clang/Basic/OpenCLExtensionTypes.def"
53606f32e7eSjoerg     case BuiltinType::OCLSampler:
53706f32e7eSjoerg     case BuiltinType::OCLEvent:
53806f32e7eSjoerg     case BuiltinType::OCLClkEvent:
53906f32e7eSjoerg     case BuiltinType::OCLQueue:
54006f32e7eSjoerg     case BuiltinType::OCLReserveID:
54106f32e7eSjoerg       ResultType = CGM.getOpenCLRuntime().convertOpenCLSpecificType(Ty);
54206f32e7eSjoerg       break;
543*13fbcb42Sjoerg     case BuiltinType::SveInt8:
544*13fbcb42Sjoerg     case BuiltinType::SveUint8:
545*13fbcb42Sjoerg     case BuiltinType::SveInt8x2:
546*13fbcb42Sjoerg     case BuiltinType::SveUint8x2:
547*13fbcb42Sjoerg     case BuiltinType::SveInt8x3:
548*13fbcb42Sjoerg     case BuiltinType::SveUint8x3:
549*13fbcb42Sjoerg     case BuiltinType::SveInt8x4:
550*13fbcb42Sjoerg     case BuiltinType::SveUint8x4:
551*13fbcb42Sjoerg     case BuiltinType::SveInt16:
552*13fbcb42Sjoerg     case BuiltinType::SveUint16:
553*13fbcb42Sjoerg     case BuiltinType::SveInt16x2:
554*13fbcb42Sjoerg     case BuiltinType::SveUint16x2:
555*13fbcb42Sjoerg     case BuiltinType::SveInt16x3:
556*13fbcb42Sjoerg     case BuiltinType::SveUint16x3:
557*13fbcb42Sjoerg     case BuiltinType::SveInt16x4:
558*13fbcb42Sjoerg     case BuiltinType::SveUint16x4:
559*13fbcb42Sjoerg     case BuiltinType::SveInt32:
560*13fbcb42Sjoerg     case BuiltinType::SveUint32:
561*13fbcb42Sjoerg     case BuiltinType::SveInt32x2:
562*13fbcb42Sjoerg     case BuiltinType::SveUint32x2:
563*13fbcb42Sjoerg     case BuiltinType::SveInt32x3:
564*13fbcb42Sjoerg     case BuiltinType::SveUint32x3:
565*13fbcb42Sjoerg     case BuiltinType::SveInt32x4:
566*13fbcb42Sjoerg     case BuiltinType::SveUint32x4:
567*13fbcb42Sjoerg     case BuiltinType::SveInt64:
568*13fbcb42Sjoerg     case BuiltinType::SveUint64:
569*13fbcb42Sjoerg     case BuiltinType::SveInt64x2:
570*13fbcb42Sjoerg     case BuiltinType::SveUint64x2:
571*13fbcb42Sjoerg     case BuiltinType::SveInt64x3:
572*13fbcb42Sjoerg     case BuiltinType::SveUint64x3:
573*13fbcb42Sjoerg     case BuiltinType::SveInt64x4:
574*13fbcb42Sjoerg     case BuiltinType::SveUint64x4:
575*13fbcb42Sjoerg     case BuiltinType::SveBool:
576*13fbcb42Sjoerg     case BuiltinType::SveFloat16:
577*13fbcb42Sjoerg     case BuiltinType::SveFloat16x2:
578*13fbcb42Sjoerg     case BuiltinType::SveFloat16x3:
579*13fbcb42Sjoerg     case BuiltinType::SveFloat16x4:
580*13fbcb42Sjoerg     case BuiltinType::SveFloat32:
581*13fbcb42Sjoerg     case BuiltinType::SveFloat32x2:
582*13fbcb42Sjoerg     case BuiltinType::SveFloat32x3:
583*13fbcb42Sjoerg     case BuiltinType::SveFloat32x4:
584*13fbcb42Sjoerg     case BuiltinType::SveFloat64:
585*13fbcb42Sjoerg     case BuiltinType::SveFloat64x2:
586*13fbcb42Sjoerg     case BuiltinType::SveFloat64x3:
587*13fbcb42Sjoerg     case BuiltinType::SveFloat64x4:
588*13fbcb42Sjoerg     case BuiltinType::SveBFloat16:
589*13fbcb42Sjoerg     case BuiltinType::SveBFloat16x2:
590*13fbcb42Sjoerg     case BuiltinType::SveBFloat16x3:
591*13fbcb42Sjoerg     case BuiltinType::SveBFloat16x4: {
592*13fbcb42Sjoerg       ASTContext::BuiltinVectorTypeInfo Info =
593*13fbcb42Sjoerg           Context.getBuiltinVectorTypeInfo(cast<BuiltinType>(Ty));
594*13fbcb42Sjoerg       return llvm::ScalableVectorType::get(ConvertType(Info.ElementType),
595*13fbcb42Sjoerg                                            Info.EC.getKnownMinValue() *
596*13fbcb42Sjoerg                                                Info.NumVectors);
59706f32e7eSjoerg     }
598*13fbcb42Sjoerg #define PPC_VECTOR_TYPE(Name, Id, Size) \
599*13fbcb42Sjoerg     case BuiltinType::Id: \
600*13fbcb42Sjoerg       ResultType = \
601*13fbcb42Sjoerg         llvm::FixedVectorType::get(ConvertType(Context.BoolTy), Size); \
602*13fbcb42Sjoerg       break;
603*13fbcb42Sjoerg #include "clang/Basic/PPCTypes.def"
604*13fbcb42Sjoerg #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
605*13fbcb42Sjoerg #include "clang/Basic/RISCVVTypes.def"
606*13fbcb42Sjoerg     {
607*13fbcb42Sjoerg       ASTContext::BuiltinVectorTypeInfo Info =
608*13fbcb42Sjoerg           Context.getBuiltinVectorTypeInfo(cast<BuiltinType>(Ty));
609*13fbcb42Sjoerg       return llvm::ScalableVectorType::get(ConvertType(Info.ElementType),
610*13fbcb42Sjoerg                                            Info.EC.getKnownMinValue() *
611*13fbcb42Sjoerg                                            Info.NumVectors);
612*13fbcb42Sjoerg     }
61306f32e7eSjoerg    case BuiltinType::Dependent:
61406f32e7eSjoerg #define BUILTIN_TYPE(Id, SingletonId)
61506f32e7eSjoerg #define PLACEHOLDER_TYPE(Id, SingletonId) \
61606f32e7eSjoerg     case BuiltinType::Id:
61706f32e7eSjoerg #include "clang/AST/BuiltinTypes.def"
61806f32e7eSjoerg       llvm_unreachable("Unexpected placeholder builtin type!");
61906f32e7eSjoerg     }
62006f32e7eSjoerg     break;
62106f32e7eSjoerg   }
62206f32e7eSjoerg   case Type::Auto:
62306f32e7eSjoerg   case Type::DeducedTemplateSpecialization:
62406f32e7eSjoerg     llvm_unreachable("Unexpected undeduced type!");
62506f32e7eSjoerg   case Type::Complex: {
62606f32e7eSjoerg     llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType());
62706f32e7eSjoerg     ResultType = llvm::StructType::get(EltTy, EltTy);
62806f32e7eSjoerg     break;
62906f32e7eSjoerg   }
63006f32e7eSjoerg   case Type::LValueReference:
63106f32e7eSjoerg   case Type::RValueReference: {
63206f32e7eSjoerg     const ReferenceType *RTy = cast<ReferenceType>(Ty);
63306f32e7eSjoerg     QualType ETy = RTy->getPointeeType();
63406f32e7eSjoerg     llvm::Type *PointeeType = ConvertTypeForMem(ETy);
63506f32e7eSjoerg     unsigned AS = Context.getTargetAddressSpace(ETy);
63606f32e7eSjoerg     ResultType = llvm::PointerType::get(PointeeType, AS);
63706f32e7eSjoerg     break;
63806f32e7eSjoerg   }
63906f32e7eSjoerg   case Type::Pointer: {
64006f32e7eSjoerg     const PointerType *PTy = cast<PointerType>(Ty);
64106f32e7eSjoerg     QualType ETy = PTy->getPointeeType();
64206f32e7eSjoerg     llvm::Type *PointeeType = ConvertTypeForMem(ETy);
64306f32e7eSjoerg     if (PointeeType->isVoidTy())
64406f32e7eSjoerg       PointeeType = llvm::Type::getInt8Ty(getLLVMContext());
645*13fbcb42Sjoerg 
646*13fbcb42Sjoerg     unsigned AS = PointeeType->isFunctionTy()
647*13fbcb42Sjoerg                       ? getDataLayout().getProgramAddressSpace()
648*13fbcb42Sjoerg                       : Context.getTargetAddressSpace(ETy);
649*13fbcb42Sjoerg 
65006f32e7eSjoerg     ResultType = llvm::PointerType::get(PointeeType, AS);
65106f32e7eSjoerg     break;
65206f32e7eSjoerg   }
65306f32e7eSjoerg 
65406f32e7eSjoerg   case Type::VariableArray: {
65506f32e7eSjoerg     const VariableArrayType *A = cast<VariableArrayType>(Ty);
65606f32e7eSjoerg     assert(A->getIndexTypeCVRQualifiers() == 0 &&
65706f32e7eSjoerg            "FIXME: We only handle trivial array types so far!");
65806f32e7eSjoerg     // VLAs resolve to the innermost element type; this matches
65906f32e7eSjoerg     // the return of alloca, and there isn't any obviously better choice.
66006f32e7eSjoerg     ResultType = ConvertTypeForMem(A->getElementType());
66106f32e7eSjoerg     break;
66206f32e7eSjoerg   }
66306f32e7eSjoerg   case Type::IncompleteArray: {
66406f32e7eSjoerg     const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
66506f32e7eSjoerg     assert(A->getIndexTypeCVRQualifiers() == 0 &&
66606f32e7eSjoerg            "FIXME: We only handle trivial array types so far!");
66706f32e7eSjoerg     // int X[] -> [0 x int], unless the element type is not sized.  If it is
66806f32e7eSjoerg     // unsized (e.g. an incomplete struct) just use [0 x i8].
66906f32e7eSjoerg     ResultType = ConvertTypeForMem(A->getElementType());
67006f32e7eSjoerg     if (!ResultType->isSized()) {
67106f32e7eSjoerg       SkippedLayout = true;
67206f32e7eSjoerg       ResultType = llvm::Type::getInt8Ty(getLLVMContext());
67306f32e7eSjoerg     }
67406f32e7eSjoerg     ResultType = llvm::ArrayType::get(ResultType, 0);
67506f32e7eSjoerg     break;
67606f32e7eSjoerg   }
67706f32e7eSjoerg   case Type::ConstantArray: {
67806f32e7eSjoerg     const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
67906f32e7eSjoerg     llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
68006f32e7eSjoerg 
68106f32e7eSjoerg     // Lower arrays of undefined struct type to arrays of i8 just to have a
68206f32e7eSjoerg     // concrete type.
68306f32e7eSjoerg     if (!EltTy->isSized()) {
68406f32e7eSjoerg       SkippedLayout = true;
68506f32e7eSjoerg       EltTy = llvm::Type::getInt8Ty(getLLVMContext());
68606f32e7eSjoerg     }
68706f32e7eSjoerg 
68806f32e7eSjoerg     ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue());
68906f32e7eSjoerg     break;
69006f32e7eSjoerg   }
69106f32e7eSjoerg   case Type::ExtVector:
69206f32e7eSjoerg   case Type::Vector: {
69306f32e7eSjoerg     const VectorType *VT = cast<VectorType>(Ty);
694*13fbcb42Sjoerg     ResultType = llvm::FixedVectorType::get(ConvertType(VT->getElementType()),
69506f32e7eSjoerg                                             VT->getNumElements());
69606f32e7eSjoerg     break;
69706f32e7eSjoerg   }
698*13fbcb42Sjoerg   case Type::ConstantMatrix: {
699*13fbcb42Sjoerg     const ConstantMatrixType *MT = cast<ConstantMatrixType>(Ty);
700*13fbcb42Sjoerg     ResultType =
701*13fbcb42Sjoerg         llvm::FixedVectorType::get(ConvertType(MT->getElementType()),
702*13fbcb42Sjoerg                                    MT->getNumRows() * MT->getNumColumns());
703*13fbcb42Sjoerg     break;
704*13fbcb42Sjoerg   }
70506f32e7eSjoerg   case Type::FunctionNoProto:
70606f32e7eSjoerg   case Type::FunctionProto:
70706f32e7eSjoerg     ResultType = ConvertFunctionTypeInternal(T);
70806f32e7eSjoerg     break;
70906f32e7eSjoerg   case Type::ObjCObject:
71006f32e7eSjoerg     ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
71106f32e7eSjoerg     break;
71206f32e7eSjoerg 
71306f32e7eSjoerg   case Type::ObjCInterface: {
71406f32e7eSjoerg     // Objective-C interfaces are always opaque (outside of the
71506f32e7eSjoerg     // runtime, which can do whatever it likes); we never refine
71606f32e7eSjoerg     // these.
71706f32e7eSjoerg     llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
71806f32e7eSjoerg     if (!T)
71906f32e7eSjoerg       T = llvm::StructType::create(getLLVMContext());
72006f32e7eSjoerg     ResultType = T;
72106f32e7eSjoerg     break;
72206f32e7eSjoerg   }
72306f32e7eSjoerg 
72406f32e7eSjoerg   case Type::ObjCObjectPointer: {
72506f32e7eSjoerg     // Protocol qualifications do not influence the LLVM type, we just return a
72606f32e7eSjoerg     // pointer to the underlying interface type. We don't need to worry about
72706f32e7eSjoerg     // recursive conversion.
72806f32e7eSjoerg     llvm::Type *T =
72906f32e7eSjoerg       ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
73006f32e7eSjoerg     ResultType = T->getPointerTo();
73106f32e7eSjoerg     break;
73206f32e7eSjoerg   }
73306f32e7eSjoerg 
73406f32e7eSjoerg   case Type::Enum: {
73506f32e7eSjoerg     const EnumDecl *ED = cast<EnumType>(Ty)->getDecl();
73606f32e7eSjoerg     if (ED->isCompleteDefinition() || ED->isFixed())
73706f32e7eSjoerg       return ConvertType(ED->getIntegerType());
73806f32e7eSjoerg     // Return a placeholder 'i32' type.  This can be changed later when the
73906f32e7eSjoerg     // type is defined (see UpdateCompletedType), but is likely to be the
74006f32e7eSjoerg     // "right" answer.
74106f32e7eSjoerg     ResultType = llvm::Type::getInt32Ty(getLLVMContext());
74206f32e7eSjoerg     break;
74306f32e7eSjoerg   }
74406f32e7eSjoerg 
74506f32e7eSjoerg   case Type::BlockPointer: {
74606f32e7eSjoerg     const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
74706f32e7eSjoerg     llvm::Type *PointeeType = CGM.getLangOpts().OpenCL
74806f32e7eSjoerg                                   ? CGM.getGenericBlockLiteralType()
74906f32e7eSjoerg                                   : ConvertTypeForMem(FTy);
75006f32e7eSjoerg     unsigned AS = Context.getTargetAddressSpace(FTy);
75106f32e7eSjoerg     ResultType = llvm::PointerType::get(PointeeType, AS);
75206f32e7eSjoerg     break;
75306f32e7eSjoerg   }
75406f32e7eSjoerg 
75506f32e7eSjoerg   case Type::MemberPointer: {
75606f32e7eSjoerg     auto *MPTy = cast<MemberPointerType>(Ty);
75706f32e7eSjoerg     if (!getCXXABI().isMemberPointerConvertible(MPTy)) {
75806f32e7eSjoerg       RecordsWithOpaqueMemberPointers.insert(MPTy->getClass());
75906f32e7eSjoerg       ResultType = llvm::StructType::create(getLLVMContext());
76006f32e7eSjoerg     } else {
76106f32e7eSjoerg       ResultType = getCXXABI().ConvertMemberPointerType(MPTy);
76206f32e7eSjoerg     }
76306f32e7eSjoerg     break;
76406f32e7eSjoerg   }
76506f32e7eSjoerg 
76606f32e7eSjoerg   case Type::Atomic: {
76706f32e7eSjoerg     QualType valueType = cast<AtomicType>(Ty)->getValueType();
76806f32e7eSjoerg     ResultType = ConvertTypeForMem(valueType);
76906f32e7eSjoerg 
77006f32e7eSjoerg     // Pad out to the inflated size if necessary.
77106f32e7eSjoerg     uint64_t valueSize = Context.getTypeSize(valueType);
77206f32e7eSjoerg     uint64_t atomicSize = Context.getTypeSize(Ty);
77306f32e7eSjoerg     if (valueSize != atomicSize) {
77406f32e7eSjoerg       assert(valueSize < atomicSize);
77506f32e7eSjoerg       llvm::Type *elts[] = {
77606f32e7eSjoerg         ResultType,
77706f32e7eSjoerg         llvm::ArrayType::get(CGM.Int8Ty, (atomicSize - valueSize) / 8)
77806f32e7eSjoerg       };
77906f32e7eSjoerg       ResultType = llvm::StructType::get(getLLVMContext(),
78006f32e7eSjoerg                                          llvm::makeArrayRef(elts));
78106f32e7eSjoerg     }
78206f32e7eSjoerg     break;
78306f32e7eSjoerg   }
78406f32e7eSjoerg   case Type::Pipe: {
78506f32e7eSjoerg     ResultType = CGM.getOpenCLRuntime().getPipeType(cast<PipeType>(Ty));
78606f32e7eSjoerg     break;
78706f32e7eSjoerg   }
788*13fbcb42Sjoerg   case Type::ExtInt: {
789*13fbcb42Sjoerg     const auto &EIT = cast<ExtIntType>(Ty);
790*13fbcb42Sjoerg     ResultType = llvm::Type::getIntNTy(getLLVMContext(), EIT->getNumBits());
791*13fbcb42Sjoerg     break;
792*13fbcb42Sjoerg   }
79306f32e7eSjoerg   }
79406f32e7eSjoerg 
79506f32e7eSjoerg   assert(ResultType && "Didn't convert a type?");
79606f32e7eSjoerg 
79706f32e7eSjoerg   TypeCache[Ty] = ResultType;
79806f32e7eSjoerg   return ResultType;
79906f32e7eSjoerg }
80006f32e7eSjoerg 
isPaddedAtomicType(QualType type)80106f32e7eSjoerg bool CodeGenModule::isPaddedAtomicType(QualType type) {
80206f32e7eSjoerg   return isPaddedAtomicType(type->castAs<AtomicType>());
80306f32e7eSjoerg }
80406f32e7eSjoerg 
isPaddedAtomicType(const AtomicType * type)80506f32e7eSjoerg bool CodeGenModule::isPaddedAtomicType(const AtomicType *type) {
80606f32e7eSjoerg   return Context.getTypeSize(type) != Context.getTypeSize(type->getValueType());
80706f32e7eSjoerg }
80806f32e7eSjoerg 
80906f32e7eSjoerg /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
ConvertRecordDeclType(const RecordDecl * RD)81006f32e7eSjoerg llvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) {
81106f32e7eSjoerg   // TagDecl's are not necessarily unique, instead use the (clang)
81206f32e7eSjoerg   // type connected to the decl.
81306f32e7eSjoerg   const Type *Key = Context.getTagDeclType(RD).getTypePtr();
81406f32e7eSjoerg 
81506f32e7eSjoerg   llvm::StructType *&Entry = RecordDeclTypes[Key];
81606f32e7eSjoerg 
81706f32e7eSjoerg   // If we don't have a StructType at all yet, create the forward declaration.
81806f32e7eSjoerg   if (!Entry) {
81906f32e7eSjoerg     Entry = llvm::StructType::create(getLLVMContext());
82006f32e7eSjoerg     addRecordTypeName(RD, Entry, "");
82106f32e7eSjoerg   }
82206f32e7eSjoerg   llvm::StructType *Ty = Entry;
82306f32e7eSjoerg 
82406f32e7eSjoerg   // If this is still a forward declaration, or the LLVM type is already
82506f32e7eSjoerg   // complete, there's nothing more to do.
82606f32e7eSjoerg   RD = RD->getDefinition();
82706f32e7eSjoerg   if (!RD || !RD->isCompleteDefinition() || !Ty->isOpaque())
82806f32e7eSjoerg     return Ty;
82906f32e7eSjoerg 
83006f32e7eSjoerg   // If converting this type would cause us to infinitely loop, don't do it!
83106f32e7eSjoerg   if (!isSafeToConvert(RD, *this)) {
83206f32e7eSjoerg     DeferredRecords.push_back(RD);
83306f32e7eSjoerg     return Ty;
83406f32e7eSjoerg   }
83506f32e7eSjoerg 
83606f32e7eSjoerg   // Okay, this is a definition of a type.  Compile the implementation now.
83706f32e7eSjoerg   bool InsertResult = RecordsBeingLaidOut.insert(Key).second;
83806f32e7eSjoerg   (void)InsertResult;
83906f32e7eSjoerg   assert(InsertResult && "Recursively compiling a struct?");
84006f32e7eSjoerg 
84106f32e7eSjoerg   // Force conversion of non-virtual base classes recursively.
84206f32e7eSjoerg   if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
84306f32e7eSjoerg     for (const auto &I : CRD->bases()) {
84406f32e7eSjoerg       if (I.isVirtual()) continue;
84506f32e7eSjoerg       ConvertRecordDeclType(I.getType()->castAs<RecordType>()->getDecl());
84606f32e7eSjoerg     }
84706f32e7eSjoerg   }
84806f32e7eSjoerg 
84906f32e7eSjoerg   // Layout fields.
850*13fbcb42Sjoerg   std::unique_ptr<CGRecordLayout> Layout = ComputeRecordLayout(RD, Ty);
851*13fbcb42Sjoerg   CGRecordLayouts[Key] = std::move(Layout);
85206f32e7eSjoerg 
85306f32e7eSjoerg   // We're done laying out this struct.
85406f32e7eSjoerg   bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult;
85506f32e7eSjoerg   assert(EraseResult && "struct not in RecordsBeingLaidOut set?");
85606f32e7eSjoerg 
85706f32e7eSjoerg   // If this struct blocked a FunctionType conversion, then recompute whatever
85806f32e7eSjoerg   // was derived from that.
85906f32e7eSjoerg   // FIXME: This is hugely overconservative.
86006f32e7eSjoerg   if (SkippedLayout)
86106f32e7eSjoerg     TypeCache.clear();
86206f32e7eSjoerg 
86306f32e7eSjoerg   // If we're done converting the outer-most record, then convert any deferred
86406f32e7eSjoerg   // structs as well.
86506f32e7eSjoerg   if (RecordsBeingLaidOut.empty())
86606f32e7eSjoerg     while (!DeferredRecords.empty())
86706f32e7eSjoerg       ConvertRecordDeclType(DeferredRecords.pop_back_val());
86806f32e7eSjoerg 
86906f32e7eSjoerg   return Ty;
87006f32e7eSjoerg }
87106f32e7eSjoerg 
87206f32e7eSjoerg /// getCGRecordLayout - Return record layout info for the given record decl.
87306f32e7eSjoerg const CGRecordLayout &
getCGRecordLayout(const RecordDecl * RD)87406f32e7eSjoerg CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
87506f32e7eSjoerg   const Type *Key = Context.getTagDeclType(RD).getTypePtr();
87606f32e7eSjoerg 
877*13fbcb42Sjoerg   auto I = CGRecordLayouts.find(Key);
878*13fbcb42Sjoerg   if (I != CGRecordLayouts.end())
879*13fbcb42Sjoerg     return *I->second;
88006f32e7eSjoerg   // Compute the type information.
88106f32e7eSjoerg   ConvertRecordDeclType(RD);
88206f32e7eSjoerg 
88306f32e7eSjoerg   // Now try again.
884*13fbcb42Sjoerg   I = CGRecordLayouts.find(Key);
88506f32e7eSjoerg 
886*13fbcb42Sjoerg   assert(I != CGRecordLayouts.end() &&
887*13fbcb42Sjoerg          "Unable to find record layout information for type");
888*13fbcb42Sjoerg   return *I->second;
88906f32e7eSjoerg }
89006f32e7eSjoerg 
isPointerZeroInitializable(QualType T)89106f32e7eSjoerg bool CodeGenTypes::isPointerZeroInitializable(QualType T) {
89206f32e7eSjoerg   assert((T->isAnyPointerType() || T->isBlockPointerType()) && "Invalid type");
89306f32e7eSjoerg   return isZeroInitializable(T);
89406f32e7eSjoerg }
89506f32e7eSjoerg 
isZeroInitializable(QualType T)89606f32e7eSjoerg bool CodeGenTypes::isZeroInitializable(QualType T) {
89706f32e7eSjoerg   if (T->getAs<PointerType>())
89806f32e7eSjoerg     return Context.getTargetNullPointerValue(T) == 0;
89906f32e7eSjoerg 
90006f32e7eSjoerg   if (const auto *AT = Context.getAsArrayType(T)) {
90106f32e7eSjoerg     if (isa<IncompleteArrayType>(AT))
90206f32e7eSjoerg       return true;
90306f32e7eSjoerg     if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
90406f32e7eSjoerg       if (Context.getConstantArrayElementCount(CAT) == 0)
90506f32e7eSjoerg         return true;
90606f32e7eSjoerg     T = Context.getBaseElementType(T);
90706f32e7eSjoerg   }
90806f32e7eSjoerg 
90906f32e7eSjoerg   // Records are non-zero-initializable if they contain any
91006f32e7eSjoerg   // non-zero-initializable subobjects.
91106f32e7eSjoerg   if (const RecordType *RT = T->getAs<RecordType>()) {
91206f32e7eSjoerg     const RecordDecl *RD = RT->getDecl();
91306f32e7eSjoerg     return isZeroInitializable(RD);
91406f32e7eSjoerg   }
91506f32e7eSjoerg 
91606f32e7eSjoerg   // We have to ask the ABI about member pointers.
91706f32e7eSjoerg   if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
91806f32e7eSjoerg     return getCXXABI().isZeroInitializable(MPT);
91906f32e7eSjoerg 
92006f32e7eSjoerg   // Everything else is okay.
92106f32e7eSjoerg   return true;
92206f32e7eSjoerg }
92306f32e7eSjoerg 
isZeroInitializable(const RecordDecl * RD)92406f32e7eSjoerg bool CodeGenTypes::isZeroInitializable(const RecordDecl *RD) {
92506f32e7eSjoerg   return getCGRecordLayout(RD).isZeroInitializable();
92606f32e7eSjoerg }
927