#include "CodeGenTypes.h"
#include "CGCXXABI.h"
#include "CGCall.h"
#include "CGOpenCLRuntime.h"
#include "CGRecordLayout.h"
#include "TargetInfo.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/Expr.h"
#include "clang/AST/RecordLayout.h"
#include "clang/CodeGen/CGFunctionInfo.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Module.h"
using namespace clang;
using namespace CodeGen;
CodeGenTypes::CodeGenTypes(CodeGenModule &cgm)
: CGM(cgm), Context(cgm.getContext()), TheModule(cgm.getModule()),
Target(cgm.getTarget()), TheCXXABI(cgm.getCXXABI()),
TheABIInfo(cgm.getTargetCodeGenInfo().getABIInfo()) {
SkippedLayout = false;
}
CodeGenTypes::~CodeGenTypes() {
for (llvm::FoldingSet<CGFunctionInfo>::iterator
I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
delete &*I++;
}
const CodeGenOptions &CodeGenTypes::getCodeGenOpts() const {
return CGM.getCodeGenOpts();
}
void CodeGenTypes::addRecordTypeName(const RecordDecl *RD,
llvm::StructType *Ty,
StringRef suffix) {
SmallString<256> TypeName;
llvm::raw_svector_ostream OS(TypeName);
OS << RD->getKindName() << '.';
PrintingPolicy Policy = RD->getASTContext().getPrintingPolicy();
Policy.SuppressInlineNamespace = false;
if (RD->getIdentifier()) {
if (RD->getDeclContext())
RD->printQualifiedName(OS, Policy);
else
RD->printName(OS);
} else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) {
if (TDD->getDeclContext())
TDD->printQualifiedName(OS, Policy);
else
TDD->printName(OS);
} else
OS << "anon";
if (!suffix.empty())
OS << suffix;
Ty->setName(OS.str());
}
llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T, bool ForBitField) {
if (T->isConstantMatrixType()) {
const Type *Ty = Context.getCanonicalType(T).getTypePtr();
const ConstantMatrixType *MT = cast<ConstantMatrixType>(Ty);
return llvm::ArrayType::get(ConvertType(MT->getElementType()),
MT->getNumRows() * MT->getNumColumns());
}
llvm::Type *R = ConvertType(T);
if (T->isExtVectorBoolType()) {
auto *FixedVT = cast<llvm::FixedVectorType>(R);
uint64_t BytePadded = std::max<uint64_t>(FixedVT->getNumElements(), 8);
return llvm::IntegerType::get(FixedVT->getContext(), BytePadded);
}
if ((ForBitField && T->isBitIntType()) ||
(!T->isBitIntType() && R->isIntegerTy(1)))
return llvm::IntegerType::get(getLLVMContext(),
(unsigned)Context.getTypeSize(T));
return R;
}
bool CodeGenTypes::isRecordLayoutComplete(const Type *Ty) const {
llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I =
RecordDeclTypes.find(Ty);
return I != RecordDeclTypes.end() && !I->second->isOpaque();
}
static bool
isSafeToConvert(QualType T, CodeGenTypes &CGT,
llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked);
static bool
isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT,
llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
if (!AlreadyChecked.insert(RD).second)
return true;
const Type *Key = CGT.getContext().getTagDeclType(RD).getTypePtr();
if (CGT.isRecordLayoutComplete(Key)) return true;
if (CGT.isRecordBeingLaidOut(Key))
return false;
if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
for (const auto &I : CRD->bases())
if (!isSafeToConvert(I.getType()->castAs<RecordType>()->getDecl(), CGT,
AlreadyChecked))
return false;
}
for (const auto *I : RD->fields())
if (!isSafeToConvert(I->getType(), CGT, AlreadyChecked))
return false;
return true;
}
static bool
isSafeToConvert(QualType T, CodeGenTypes &CGT,
llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
if (const auto *AT = T->getAs<AtomicType>())
T = AT->getValueType();
if (const auto *RT = T->getAs<RecordType>())
return isSafeToConvert(RT->getDecl(), CGT, AlreadyChecked);
if (const auto *AT = CGT.getContext().getAsArrayType(T))
return isSafeToConvert(AT->getElementType(), CGT, AlreadyChecked);
return true;
}
static bool isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT) {
if (CGT.noRecordsBeingLaidOut()) return true;
llvm::SmallPtrSet<const RecordDecl*, 16> AlreadyChecked;
return isSafeToConvert(RD, CGT, AlreadyChecked);
}
bool CodeGenTypes::isFuncParamTypeConvertible(QualType Ty) {
if (const auto *MPT = Ty->getAs<MemberPointerType>())
return getCXXABI().isMemberPointerConvertible(MPT);
const TagType *TT = Ty->getAs<TagType>();
if (!TT) return true;
if (TT->isIncompleteType())
return false;
const RecordType *RT = dyn_cast<RecordType>(TT);
if (!RT) return true;
return isSafeToConvert(RT->getDecl(), *this);
}
bool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) {
if (!isFuncParamTypeConvertible(FT->getReturnType()))
return false;
if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++)
if (!isFuncParamTypeConvertible(FPT->getParamType(i)))
return false;
return true;
}
void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) {
if (TypeCache.count(ED->getTypeForDecl())) {
if (!ConvertType(ED->getIntegerType())->isIntegerTy(32))
TypeCache.clear();
}
if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
DI->completeType(ED);
return;
}
const RecordDecl *RD = cast<RecordDecl>(TD);
if (RD->isDependentType()) return;
if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr()))
ConvertRecordDeclType(RD);
if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
DI->completeType(RD);
}
void CodeGenTypes::RefreshTypeCacheForClass(const CXXRecordDecl *RD) {
QualType T = Context.getRecordType(RD);
T = Context.getCanonicalType(T);
const Type *Ty = T.getTypePtr();
if (RecordsWithOpaqueMemberPointers.count(Ty)) {
TypeCache.clear();
RecordsWithOpaqueMemberPointers.clear();
}
}
static llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext,
const llvm::fltSemantics &format,
bool UseNativeHalf = false) {
if (&format == &llvm::APFloat::IEEEhalf()) {
if (UseNativeHalf)
return llvm::Type::getHalfTy(VMContext);
else
return llvm::Type::getInt16Ty(VMContext);
}
if (&format == &llvm::APFloat::BFloat())
return llvm::Type::getBFloatTy(VMContext);
if (&format == &llvm::APFloat::IEEEsingle())
return llvm::Type::getFloatTy(VMContext);
if (&format == &llvm::APFloat::IEEEdouble())
return llvm::Type::getDoubleTy(VMContext);
if (&format == &llvm::APFloat::IEEEquad())
return llvm::Type::getFP128Ty(VMContext);
if (&format == &llvm::APFloat::PPCDoubleDouble())
return llvm::Type::getPPC_FP128Ty(VMContext);
if (&format == &llvm::APFloat::x87DoubleExtended())
return llvm::Type::getX86_FP80Ty(VMContext);
llvm_unreachable("Unknown float format!");
}
llvm::Type *CodeGenTypes::ConvertFunctionTypeInternal(QualType QFT) {
assert(QFT.isCanonical());
const Type *Ty = QFT.getTypePtr();
const FunctionType *FT = cast<FunctionType>(QFT.getTypePtr());
if (!isFuncTypeConvertible(FT)) {
if (const RecordType *RT = FT->getReturnType()->getAs<RecordType>())
ConvertRecordDeclType(RT->getDecl());
if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++)
if (const RecordType *RT = FPT->getParamType(i)->getAs<RecordType>())
ConvertRecordDeclType(RT->getDecl());
SkippedLayout = true;
return llvm::StructType::get(getLLVMContext());
}
if (!RecordsBeingLaidOut.insert(Ty).second) {
SkippedLayout = true;
return llvm::StructType::get(getLLVMContext());
}
const CGFunctionInfo *FI;
if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
FI = &arrangeFreeFunctionType(
CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0)));
} else {
const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT);
FI = &arrangeFreeFunctionType(
CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0)));
}
llvm::Type *ResultType = nullptr;
if (FunctionsBeingProcessed.count(FI)) {
ResultType = llvm::StructType::get(getLLVMContext());
SkippedLayout = true;
} else {
ResultType = GetFunctionType(*FI);
}
RecordsBeingLaidOut.erase(Ty);
if (RecordsBeingLaidOut.empty())
while (!DeferredRecords.empty())
ConvertRecordDeclType(DeferredRecords.pop_back_val());
return ResultType;
}
llvm::Type *CodeGenTypes::ConvertType(QualType T) {
T = Context.getCanonicalType(T);
const Type *Ty = T.getTypePtr();
if (Context.getLangOpts().CUDAIsDevice) {
if (T->isCUDADeviceBuiltinSurfaceType()) {
if (auto *Ty = CGM.getTargetCodeGenInfo()
.getCUDADeviceBuiltinSurfaceDeviceType())
return Ty;
} else if (T->isCUDADeviceBuiltinTextureType()) {
if (auto *Ty = CGM.getTargetCodeGenInfo()
.getCUDADeviceBuiltinTextureDeviceType())
return Ty;
}
}
if (const RecordType *RT = dyn_cast<RecordType>(Ty))
return ConvertRecordDeclType(RT->getDecl());
llvm::Type *CachedType = nullptr;
bool ShouldUseCache =
Ty->isBuiltinType() ||
(noRecordsBeingLaidOut() && FunctionsBeingProcessed.empty());
if (ShouldUseCache) {
llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI =
TypeCache.find(Ty);
if (TCI != TypeCache.end())
CachedType = TCI->second;
#ifndef EXPENSIVE_CHECKS
if (CachedType)
return CachedType;
#endif
}
llvm::Type *ResultType = nullptr;
switch (Ty->getTypeClass()) {
case Type::Record: #define TYPE(Class, Base)
#define ABSTRACT_TYPE(Class, Base)
#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
#define DEPENDENT_TYPE(Class, Base) case Type::Class:
#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
#include "clang/AST/TypeNodes.inc"
llvm_unreachable("Non-canonical or dependent types aren't possible.");
case Type::Builtin: {
switch (cast<BuiltinType>(Ty)->getKind()) {
case BuiltinType::Void:
case BuiltinType::ObjCId:
case BuiltinType::ObjCClass:
case BuiltinType::ObjCSel:
ResultType = llvm::Type::getInt8Ty(getLLVMContext());
break;
case BuiltinType::Bool:
ResultType = llvm::Type::getInt1Ty(getLLVMContext());
break;
case BuiltinType::Char_S:
case BuiltinType::Char_U:
case BuiltinType::SChar:
case BuiltinType::UChar:
case BuiltinType::Short:
case BuiltinType::UShort:
case BuiltinType::Int:
case BuiltinType::UInt:
case BuiltinType::Long:
case BuiltinType::ULong:
case BuiltinType::LongLong:
case BuiltinType::ULongLong:
case BuiltinType::WChar_S:
case BuiltinType::WChar_U:
case BuiltinType::Char8:
case BuiltinType::Char16:
case BuiltinType::Char32:
case BuiltinType::ShortAccum:
case BuiltinType::Accum:
case BuiltinType::LongAccum:
case BuiltinType::UShortAccum:
case BuiltinType::UAccum:
case BuiltinType::ULongAccum:
case BuiltinType::ShortFract:
case BuiltinType::Fract:
case BuiltinType::LongFract:
case BuiltinType::UShortFract:
case BuiltinType::UFract:
case BuiltinType::ULongFract:
case BuiltinType::SatShortAccum:
case BuiltinType::SatAccum:
case BuiltinType::SatLongAccum:
case BuiltinType::SatUShortAccum:
case BuiltinType::SatUAccum:
case BuiltinType::SatULongAccum:
case BuiltinType::SatShortFract:
case BuiltinType::SatFract:
case BuiltinType::SatLongFract:
case BuiltinType::SatUShortFract:
case BuiltinType::SatUFract:
case BuiltinType::SatULongFract:
ResultType = llvm::IntegerType::get(getLLVMContext(),
static_cast<unsigned>(Context.getTypeSize(T)));
break;
case BuiltinType::Float16:
ResultType =
getTypeForFormat(getLLVMContext(), Context.getFloatTypeSemantics(T),
true);
break;
case BuiltinType::Half:
ResultType = getTypeForFormat(
getLLVMContext(), Context.getFloatTypeSemantics(T),
Context.getLangOpts().NativeHalfType ||
!Context.getTargetInfo().useFP16ConversionIntrinsics());
break;
case BuiltinType::BFloat16:
case BuiltinType::Float:
case BuiltinType::Double:
case BuiltinType::LongDouble:
case BuiltinType::Float128:
case BuiltinType::Ibm128:
ResultType = getTypeForFormat(getLLVMContext(),
Context.getFloatTypeSemantics(T),
false);
break;
case BuiltinType::NullPtr:
ResultType = llvm::Type::getInt8PtrTy(getLLVMContext());
break;
case BuiltinType::UInt128:
case BuiltinType::Int128:
ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
break;
#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
case BuiltinType::Id:
#include "clang/Basic/OpenCLImageTypes.def"
#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
case BuiltinType::Id:
#include "clang/Basic/OpenCLExtensionTypes.def"
case BuiltinType::OCLSampler:
case BuiltinType::OCLEvent:
case BuiltinType::OCLClkEvent:
case BuiltinType::OCLQueue:
case BuiltinType::OCLReserveID:
ResultType = CGM.getOpenCLRuntime().convertOpenCLSpecificType(Ty);
break;
case BuiltinType::SveInt8:
case BuiltinType::SveUint8:
case BuiltinType::SveInt8x2:
case BuiltinType::SveUint8x2:
case BuiltinType::SveInt8x3:
case BuiltinType::SveUint8x3:
case BuiltinType::SveInt8x4:
case BuiltinType::SveUint8x4:
case BuiltinType::SveInt16:
case BuiltinType::SveUint16:
case BuiltinType::SveInt16x2:
case BuiltinType::SveUint16x2:
case BuiltinType::SveInt16x3:
case BuiltinType::SveUint16x3:
case BuiltinType::SveInt16x4:
case BuiltinType::SveUint16x4:
case BuiltinType::SveInt32:
case BuiltinType::SveUint32:
case BuiltinType::SveInt32x2:
case BuiltinType::SveUint32x2:
case BuiltinType::SveInt32x3:
case BuiltinType::SveUint32x3:
case BuiltinType::SveInt32x4:
case BuiltinType::SveUint32x4:
case BuiltinType::SveInt64:
case BuiltinType::SveUint64:
case BuiltinType::SveInt64x2:
case BuiltinType::SveUint64x2:
case BuiltinType::SveInt64x3:
case BuiltinType::SveUint64x3:
case BuiltinType::SveInt64x4:
case BuiltinType::SveUint64x4:
case BuiltinType::SveBool:
case BuiltinType::SveFloat16:
case BuiltinType::SveFloat16x2:
case BuiltinType::SveFloat16x3:
case BuiltinType::SveFloat16x4:
case BuiltinType::SveFloat32:
case BuiltinType::SveFloat32x2:
case BuiltinType::SveFloat32x3:
case BuiltinType::SveFloat32x4:
case BuiltinType::SveFloat64:
case BuiltinType::SveFloat64x2:
case BuiltinType::SveFloat64x3:
case BuiltinType::SveFloat64x4:
case BuiltinType::SveBFloat16:
case BuiltinType::SveBFloat16x2:
case BuiltinType::SveBFloat16x3:
case BuiltinType::SveBFloat16x4: {
ASTContext::BuiltinVectorTypeInfo Info =
Context.getBuiltinVectorTypeInfo(cast<BuiltinType>(Ty));
return llvm::ScalableVectorType::get(ConvertType(Info.ElementType),
Info.EC.getKnownMinValue() *
Info.NumVectors);
}
#define PPC_VECTOR_TYPE(Name, Id, Size) \
case BuiltinType::Id: \
ResultType = \
llvm::FixedVectorType::get(ConvertType(Context.BoolTy), Size); \
break;
#include "clang/Basic/PPCTypes.def"
#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
#include "clang/Basic/RISCVVTypes.def"
{
ASTContext::BuiltinVectorTypeInfo Info =
Context.getBuiltinVectorTypeInfo(cast<BuiltinType>(Ty));
return llvm::ScalableVectorType::get(ConvertType(Info.ElementType),
Info.EC.getKnownMinValue() *
Info.NumVectors);
}
case BuiltinType::Dependent:
#define BUILTIN_TYPE(Id, SingletonId)
#define PLACEHOLDER_TYPE(Id, SingletonId) \
case BuiltinType::Id:
#include "clang/AST/BuiltinTypes.def"
llvm_unreachable("Unexpected placeholder builtin type!");
}
break;
}
case Type::Auto:
case Type::DeducedTemplateSpecialization:
llvm_unreachable("Unexpected undeduced type!");
case Type::Complex: {
llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType());
ResultType = llvm::StructType::get(EltTy, EltTy);
break;
}
case Type::LValueReference:
case Type::RValueReference: {
const ReferenceType *RTy = cast<ReferenceType>(Ty);
QualType ETy = RTy->getPointeeType();
llvm::Type *PointeeType = ConvertTypeForMem(ETy);
unsigned AS = Context.getTargetAddressSpace(ETy);
ResultType = llvm::PointerType::get(PointeeType, AS);
break;
}
case Type::Pointer: {
const PointerType *PTy = cast<PointerType>(Ty);
QualType ETy = PTy->getPointeeType();
llvm::Type *PointeeType = ConvertTypeForMem(ETy);
if (PointeeType->isVoidTy())
PointeeType = llvm::Type::getInt8Ty(getLLVMContext());
unsigned AS = Context.getTargetAddressSpace(ETy);
ResultType = llvm::PointerType::get(PointeeType, AS);
break;
}
case Type::VariableArray: {
const VariableArrayType *A = cast<VariableArrayType>(Ty);
assert(A->getIndexTypeCVRQualifiers() == 0 &&
"FIXME: We only handle trivial array types so far!");
ResultType = ConvertTypeForMem(A->getElementType());
break;
}
case Type::IncompleteArray: {
const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
assert(A->getIndexTypeCVRQualifiers() == 0 &&
"FIXME: We only handle trivial array types so far!");
ResultType = ConvertTypeForMem(A->getElementType());
if (!ResultType->isSized()) {
SkippedLayout = true;
ResultType = llvm::Type::getInt8Ty(getLLVMContext());
}
ResultType = llvm::ArrayType::get(ResultType, 0);
break;
}
case Type::ConstantArray: {
const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
if (!EltTy->isSized()) {
SkippedLayout = true;
EltTy = llvm::Type::getInt8Ty(getLLVMContext());
}
ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue());
break;
}
case Type::ExtVector:
case Type::Vector: {
const auto *VT = cast<VectorType>(Ty);
llvm::Type *IRElemTy = VT->isExtVectorBoolType()
? llvm::Type::getInt1Ty(getLLVMContext())
: ConvertType(VT->getElementType());
ResultType = llvm::FixedVectorType::get(IRElemTy, VT->getNumElements());
break;
}
case Type::ConstantMatrix: {
const ConstantMatrixType *MT = cast<ConstantMatrixType>(Ty);
ResultType =
llvm::FixedVectorType::get(ConvertType(MT->getElementType()),
MT->getNumRows() * MT->getNumColumns());
break;
}
case Type::FunctionNoProto:
case Type::FunctionProto:
ResultType = ConvertFunctionTypeInternal(T);
break;
case Type::ObjCObject:
ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
break;
case Type::ObjCInterface: {
llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
if (!T)
T = llvm::StructType::create(getLLVMContext());
ResultType = T;
break;
}
case Type::ObjCObjectPointer: {
llvm::Type *T =
ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
ResultType = T->getPointerTo();
break;
}
case Type::Enum: {
const EnumDecl *ED = cast<EnumType>(Ty)->getDecl();
if (ED->isCompleteDefinition() || ED->isFixed())
return ConvertType(ED->getIntegerType());
ResultType = llvm::Type::getInt32Ty(getLLVMContext());
break;
}
case Type::BlockPointer: {
const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
llvm::Type *PointeeType = CGM.getLangOpts().OpenCL
? CGM.getGenericBlockLiteralType()
: ConvertTypeForMem(FTy);
unsigned AS = Context.getTargetAddressSpace(FTy.getQualifiers());
ResultType = llvm::PointerType::get(PointeeType, AS);
break;
}
case Type::MemberPointer: {
auto *MPTy = cast<MemberPointerType>(Ty);
if (!getCXXABI().isMemberPointerConvertible(MPTy)) {
auto *C = MPTy->getClass();
auto Insertion = RecordsWithOpaqueMemberPointers.insert({C, nullptr});
if (Insertion.second)
Insertion.first->second = llvm::StructType::create(getLLVMContext());
ResultType = Insertion.first->second;
} else {
ResultType = getCXXABI().ConvertMemberPointerType(MPTy);
}
break;
}
case Type::Atomic: {
QualType valueType = cast<AtomicType>(Ty)->getValueType();
ResultType = ConvertTypeForMem(valueType);
uint64_t valueSize = Context.getTypeSize(valueType);
uint64_t atomicSize = Context.getTypeSize(Ty);
if (valueSize != atomicSize) {
assert(valueSize < atomicSize);
llvm::Type *elts[] = {
ResultType,
llvm::ArrayType::get(CGM.Int8Ty, (atomicSize - valueSize) / 8)
};
ResultType = llvm::StructType::get(getLLVMContext(),
llvm::makeArrayRef(elts));
}
break;
}
case Type::Pipe: {
ResultType = CGM.getOpenCLRuntime().getPipeType(cast<PipeType>(Ty));
break;
}
case Type::BitInt: {
const auto &EIT = cast<BitIntType>(Ty);
ResultType = llvm::Type::getIntNTy(getLLVMContext(), EIT->getNumBits());
break;
}
}
assert(ResultType && "Didn't convert a type?");
assert((!CachedType || CachedType == ResultType) &&
"Cached type doesn't match computed type");
if (ShouldUseCache)
TypeCache[Ty] = ResultType;
return ResultType;
}
bool CodeGenModule::isPaddedAtomicType(QualType type) {
return isPaddedAtomicType(type->castAs<AtomicType>());
}
bool CodeGenModule::isPaddedAtomicType(const AtomicType *type) {
return Context.getTypeSize(type) != Context.getTypeSize(type->getValueType());
}
llvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) {
const Type *Key = Context.getTagDeclType(RD).getTypePtr();
llvm::StructType *&Entry = RecordDeclTypes[Key];
if (!Entry) {
Entry = llvm::StructType::create(getLLVMContext());
addRecordTypeName(RD, Entry, "");
}
llvm::StructType *Ty = Entry;
RD = RD->getDefinition();
if (!RD || !RD->isCompleteDefinition() || !Ty->isOpaque())
return Ty;
if (!isSafeToConvert(RD, *this)) {
DeferredRecords.push_back(RD);
return Ty;
}
bool InsertResult = RecordsBeingLaidOut.insert(Key).second;
(void)InsertResult;
assert(InsertResult && "Recursively compiling a struct?");
if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
for (const auto &I : CRD->bases()) {
if (I.isVirtual()) continue;
ConvertRecordDeclType(I.getType()->castAs<RecordType>()->getDecl());
}
}
std::unique_ptr<CGRecordLayout> Layout = ComputeRecordLayout(RD, Ty);
CGRecordLayouts[Key] = std::move(Layout);
bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult;
assert(EraseResult && "struct not in RecordsBeingLaidOut set?");
if (SkippedLayout)
TypeCache.clear();
if (RecordsBeingLaidOut.empty())
while (!DeferredRecords.empty())
ConvertRecordDeclType(DeferredRecords.pop_back_val());
return Ty;
}
const CGRecordLayout &
CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
const Type *Key = Context.getTagDeclType(RD).getTypePtr();
auto I = CGRecordLayouts.find(Key);
if (I != CGRecordLayouts.end())
return *I->second;
ConvertRecordDeclType(RD);
I = CGRecordLayouts.find(Key);
assert(I != CGRecordLayouts.end() &&
"Unable to find record layout information for type");
return *I->second;
}
bool CodeGenTypes::isPointerZeroInitializable(QualType T) {
assert((T->isAnyPointerType() || T->isBlockPointerType()) && "Invalid type");
return isZeroInitializable(T);
}
bool CodeGenTypes::isZeroInitializable(QualType T) {
if (T->getAs<PointerType>())
return Context.getTargetNullPointerValue(T) == 0;
if (const auto *AT = Context.getAsArrayType(T)) {
if (isa<IncompleteArrayType>(AT))
return true;
if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
if (Context.getConstantArrayElementCount(CAT) == 0)
return true;
T = Context.getBaseElementType(T);
}
if (const RecordType *RT = T->getAs<RecordType>()) {
const RecordDecl *RD = RT->getDecl();
return isZeroInitializable(RD);
}
if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
return getCXXABI().isZeroInitializable(MPT);
return true;
}
bool CodeGenTypes::isZeroInitializable(const RecordDecl *RD) {
return getCGRecordLayout(RD).isZeroInitializable();
}