| 1 |
//===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
// This is the code that handles AST -> LLVM type lowering. |
| 10 |
// |
| 11 |
//===----------------------------------------------------------------------===// |
| 12 |
|
| 13 |
#include "CodeGenTypes.h" |
| 14 |
#include "CGCXXABI.h" |
| 15 |
#include "CGCall.h" |
| 16 |
#include "CGOpenCLRuntime.h" |
| 17 |
#include "CGRecordLayout.h" |
| 18 |
#include "TargetInfo.h" |
| 19 |
#include "clang/AST/ASTContext.h" |
| 20 |
#include "clang/AST/DeclCXX.h" |
| 21 |
#include "clang/AST/DeclObjC.h" |
| 22 |
#include "clang/AST/Expr.h" |
| 23 |
#include "clang/AST/RecordLayout.h" |
| 24 |
#include "clang/CodeGen/CGFunctionInfo.h" |
| 25 |
#include "llvm/IR/DataLayout.h" |
| 26 |
#include "llvm/IR/DerivedTypes.h" |
| 27 |
#include "llvm/IR/Module.h" |
| 28 |
|
| 29 |
using namespace clang; |
| 30 |
using namespace CodeGen; |
| 31 |
|
| 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; |
| 37 |
} |
| 38 |
|
| 39 |
CodeGenTypes::~CodeGenTypes() { |
| 40 |
for (llvm::FoldingSet<CGFunctionInfo>::iterator |
| 41 |
I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; ) |
| 42 |
delete &*I++; |
| 43 |
} |
| 44 |
|
| 45 |
const CodeGenOptions &CodeGenTypes::getCodeGenOpts() const { |
| 46 |
return CGM.getCodeGenOpts(); |
| 47 |
} |
| 48 |
|
| 49 |
void CodeGenTypes::addRecordTypeName(const RecordDecl *RD, |
| 50 |
llvm::StructType *Ty, |
| 51 |
StringRef suffix) { |
| 52 |
SmallString<256> TypeName; |
| 53 |
llvm::raw_svector_ostream OS(TypeName); |
| 54 |
OS << RD->getKindName() << '.'; |
| 55 |
|
| 56 |
// FIXME: We probably want to make more tweaks to the printing policy. For |
| 57 |
// example, we should probably enable PrintCanonicalTypes and |
| 58 |
// FullyQualifiedNames. |
| 59 |
PrintingPolicy Policy = RD->getASTContext().getPrintingPolicy(); |
| 60 |
Policy.SuppressInlineNamespace = false; |
| 61 |
|
| 62 |
// Name the codegen type after the typedef name |
| 63 |
// if there is no tag type name available |
| 64 |
if (RD->getIdentifier()) { |
| 65 |
// FIXME: We should not have to check for a null decl context here. |
| 66 |
// Right now we do it because the implicit Obj-C decls don't have one. |
| 67 |
if (RD->getDeclContext()) |
| 68 |
RD->printQualifiedName(OS, Policy); |
| 69 |
else |
| 70 |
RD->printName(OS, Policy); |
| 71 |
} else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) { |
| 72 |
// FIXME: We should not have to check for a null decl context here. |
| 73 |
// Right now we do it because the implicit Obj-C decls don't have one. |
| 74 |
if (TDD->getDeclContext()) |
| 75 |
TDD->printQualifiedName(OS, Policy); |
| 76 |
else |
| 77 |
TDD->printName(OS); |
| 78 |
} else |
| 79 |
OS << "anon"; |
| 80 |
|
| 81 |
if (!suffix.empty()) |
| 82 |
OS << suffix; |
| 83 |
|
| 84 |
Ty->setName(OS.str()); |
| 85 |
} |
| 86 |
|
| 87 |
/// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from |
| 88 |
/// ConvertType in that it is used to convert to the memory representation for |
| 89 |
/// a type. For example, the scalar representation for _Bool is i1, but the |
| 90 |
/// memory representation is usually i8 or i32, depending on the target. |
| 91 |
llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T, bool ForBitField) { |
| 92 |
if (T->isConstantMatrixType()) { |
| 93 |
const Type *Ty = Context.getCanonicalType(T).getTypePtr(); |
| 94 |
const ConstantMatrixType *MT = cast<ConstantMatrixType>(Ty); |
| 95 |
return llvm::ArrayType::get(ConvertType(MT->getElementType()), |
| 96 |
MT->getNumRows() * MT->getNumColumns()); |
| 97 |
} |
| 98 |
|
| 99 |
llvm::Type *R = ConvertType(T); |
| 100 |
|
| 101 |
// Check for the boolean vector case. |
| 102 |
if (T->isExtVectorBoolType()) { |
| 103 |
auto *FixedVT = cast<llvm::FixedVectorType>(R); |
| 104 |
// Pad to at least one byte. |
| 105 |
uint64_t BytePadded = std::max<uint64_t>(FixedVT->getNumElements(), 8); |
| 106 |
return llvm::IntegerType::get(FixedVT->getContext(), BytePadded); |
| 107 |
} |
| 108 |
|
| 109 |
// If this is a bool type, or a bit-precise integer type in a bitfield |
| 110 |
// representation, map this integer to the target-specified size. |
| 111 |
if ((ForBitField && T->isBitIntType()) || |
| 112 |
(!T->isBitIntType() && R->isIntegerTy(1))) |
| 113 |
return llvm::IntegerType::get(getLLVMContext(), |
| 114 |
(unsigned)Context.getTypeSize(T)); |
| 115 |
|
| 116 |
// Else, don't map it. |
| 117 |
return R; |
| 118 |
} |
| 119 |
|
| 120 |
/// isRecordLayoutComplete - Return true if the specified type is already |
| 121 |
/// completely laid out. |
| 122 |
bool CodeGenTypes::isRecordLayoutComplete(const Type *Ty) const { |
| 123 |
llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I = |
| 124 |
RecordDeclTypes.find(Ty); |
| 125 |
return I != RecordDeclTypes.end() && !I->second->isOpaque(); |
| 126 |
} |
| 127 |
|
| 128 |
static bool |
| 129 |
isSafeToConvert(QualType T, CodeGenTypes &CGT, |
| 130 |
llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked); |
| 131 |
|
| 132 |
|
| 133 |
/// isSafeToConvert - Return true if it is safe to convert the specified record |
| 134 |
/// decl to IR and lay it out, false if doing so would cause us to get into a |
| 135 |
/// recursive compilation mess. |
| 136 |
static bool |
| 137 |
isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT, |
| 138 |
llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) { |
| 139 |
// If we have already checked this type (maybe the same type is used by-value |
| 140 |
// multiple times in multiple structure fields, don't check again. |
| 141 |
if (!AlreadyChecked.insert(RD).second) |
| 142 |
return true; |
| 143 |
|
| 144 |
const Type *Key = CGT.getContext().getTagDeclType(RD).getTypePtr(); |
| 145 |
|
| 146 |
// If this type is already laid out, converting it is a noop. |
| 147 |
if (CGT.isRecordLayoutComplete(Key)) return true; |
| 148 |
|
| 149 |
// If this type is currently being laid out, we can't recursively compile it. |
| 150 |
if (CGT.isRecordBeingLaidOut(Key)) |
| 151 |
return false; |
| 152 |
|
| 153 |
// If this type would require laying out bases that are currently being laid |
| 154 |
// out, don't do it. This includes virtual base classes which get laid out |
| 155 |
// when a class is translated, even though they aren't embedded by-value into |
| 156 |
// the class. |
| 157 |
if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 158 |
for (const auto &I : CRD->bases()) |
| 159 |
if (!isSafeToConvert(I.getType()->castAs<RecordType>()->getDecl(), CGT, |
| 160 |
AlreadyChecked)) |
| 161 |
return false; |
| 162 |
} |
| 163 |
|
| 164 |
// If this type would require laying out members that are currently being laid |
| 165 |
// out, don't do it. |
| 166 |
for (const auto *I : RD->fields()) |
| 167 |
if (!isSafeToConvert(I->getType(), CGT, AlreadyChecked)) |
| 168 |
return false; |
| 169 |
|
| 170 |
// If there are no problems, lets do it. |
| 171 |
return true; |
| 172 |
} |
| 173 |
|
| 174 |
/// isSafeToConvert - Return true if it is safe to convert this field type, |
| 175 |
/// which requires the structure elements contained by-value to all be |
| 176 |
/// recursively safe to convert. |
| 177 |
static bool |
| 178 |
isSafeToConvert(QualType T, CodeGenTypes &CGT, |
| 179 |
llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) { |
| 180 |
// Strip off atomic type sugar. |
| 181 |
if (const auto *AT = T->getAs<AtomicType>()) |
| 182 |
T = AT->getValueType(); |
| 183 |
|
| 184 |
// If this is a record, check it. |
| 185 |
if (const auto *RT = T->getAs<RecordType>()) |
| 186 |
return isSafeToConvert(RT->getDecl(), CGT, AlreadyChecked); |
| 187 |
|
| 188 |
// If this is an array, check the elements, which are embedded inline. |
| 189 |
if (const auto *AT = CGT.getContext().getAsArrayType(T)) |
| 190 |
return isSafeToConvert(AT->getElementType(), CGT, AlreadyChecked); |
| 191 |
|
| 192 |
// Otherwise, there is no concern about transforming this. We only care about |
| 193 |
// things that are contained by-value in a structure that can have another |
| 194 |
// structure as a member. |
| 195 |
return true; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
/// isSafeToConvert - Return true if it is safe to convert the specified record |
| 200 |
/// decl to IR and lay it out, false if doing so would cause us to get into a |
| 201 |
/// recursive compilation mess. |
| 202 |
static bool isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT) { |
| 203 |
// If no structs are being laid out, we can certainly do this one. |
| 204 |
if (CGT.noRecordsBeingLaidOut()) return true; |
| 205 |
|
| 206 |
llvm::SmallPtrSet<const RecordDecl*, 16> AlreadyChecked; |
| 207 |
return isSafeToConvert(RD, CGT, AlreadyChecked); |
| 208 |
} |
| 209 |
|
| 210 |
/// isFuncParamTypeConvertible - Return true if the specified type in a |
| 211 |
/// function parameter or result position can be converted to an IR type at this |
| 212 |
/// point. This boils down to being whether it is complete, as well as whether |
| 213 |
/// we've temporarily deferred expanding the type because we're in a recursive |
| 214 |
/// context. |
| 215 |
bool CodeGenTypes::isFuncParamTypeConvertible(QualType Ty) { |
| 216 |
// Some ABIs cannot have their member pointers represented in IR unless |
| 217 |
// certain circumstances have been reached. |
| 218 |
if (const auto *MPT = Ty->getAs<MemberPointerType>()) |
| 219 |
return getCXXABI().isMemberPointerConvertible(MPT); |
| 220 |
|
| 221 |
// If this isn't a tagged type, we can convert it! |
| 222 |
const TagType *TT = Ty->getAs<TagType>(); |
| 223 |
if (!TT) return true; |
| 224 |
|
| 225 |
// Incomplete types cannot be converted. |
| 226 |
if (TT->isIncompleteType()) |
| 227 |
return false; |
| 228 |
|
| 229 |
// If this is an enum, then it is always safe to convert. |
| 230 |
const RecordType *RT = dyn_cast<RecordType>(TT); |
| 231 |
if (!RT) return true; |
| 232 |
|
| 233 |
// Otherwise, we have to be careful. If it is a struct that we're in the |
| 234 |
// process of expanding, then we can't convert the function type. That's ok |
| 235 |
// though because we must be in a pointer context under the struct, so we can |
| 236 |
// just convert it to a dummy type. |
| 237 |
// |
| 238 |
// We decide this by checking whether ConvertRecordDeclType returns us an |
| 239 |
// opaque type for a struct that we know is defined. |
| 240 |
return isSafeToConvert(RT->getDecl(), *this); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
/// Code to verify a given function type is complete, i.e. the return type |
| 245 |
/// and all of the parameter types are complete. Also check to see if we are in |
| 246 |
/// a RS_StructPointer context, and if so whether any struct types have been |
| 247 |
/// pended. If so, we don't want to ask the ABI lowering code to handle a type |
| 248 |
/// that cannot be converted to an IR type. |
| 249 |
bool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) { |
| 250 |
if (!isFuncParamTypeConvertible(FT->getReturnType())) |
| 251 |
return false; |
| 252 |
|
| 253 |
if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) |
| 254 |
for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++) |
| 255 |
if (!isFuncParamTypeConvertible(FPT->getParamType(i))) |
| 256 |
return false; |
| 257 |
|
| 258 |
return true; |
| 259 |
} |
| 260 |
|
| 261 |
/// UpdateCompletedType - When we find the full definition for a TagDecl, |
| 262 |
/// replace the 'opaque' type we previously made for it if applicable. |
| 263 |
void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) { |
| 264 |
// If this is an enum being completed, then we flush all non-struct types from |
| 265 |
// the cache. This allows function types and other things that may be derived |
| 266 |
// from the enum to be recomputed. |
| 267 |
if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) { |
| 268 |
// Only flush the cache if we've actually already converted this type. |
| 269 |
if (TypeCache.count(ED->getTypeForDecl())) { |
| 270 |
// Okay, we formed some types based on this. We speculated that the enum |
| 271 |
// would be lowered to i32, so we only need to flush the cache if this |
| 272 |
// didn't happen. |
| 273 |
if (!ConvertType(ED->getIntegerType())->isIntegerTy(32)) |
| 274 |
TypeCache.clear(); |
| 275 |
} |
| 276 |
// If necessary, provide the full definition of a type only used with a |
| 277 |
// declaration so far. |
| 278 |
if (CGDebugInfo *DI = CGM.getModuleDebugInfo()) |
| 279 |
DI->completeType(ED); |
| 280 |
return; |
| 281 |
} |
| 282 |
|
| 283 |
// If we completed a RecordDecl that we previously used and converted to an |
| 284 |
// anonymous type, then go ahead and complete it now. |
| 285 |
const RecordDecl *RD = cast<RecordDecl>(TD); |
| 286 |
if (RD->isDependentType()) return; |
| 287 |
|
| 288 |
// Only complete it if we converted it already. If we haven't converted it |
| 289 |
// yet, we'll just do it lazily. |
| 290 |
if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr())) |
| 291 |
ConvertRecordDeclType(RD); |
| 292 |
|
| 293 |
// If necessary, provide the full definition of a type only used with a |
| 294 |
// declaration so far. |
| 295 |
if (CGDebugInfo *DI = CGM.getModuleDebugInfo()) |
| 296 |
DI->completeType(RD); |
| 297 |
} |
| 298 |
|
| 299 |
void CodeGenTypes::RefreshTypeCacheForClass(const CXXRecordDecl *RD) { |
| 300 |
QualType T = Context.getRecordType(RD); |
| 301 |
T = Context.getCanonicalType(T); |
| 302 |
|
| 303 |
const Type *Ty = T.getTypePtr(); |
| 304 |
if (RecordsWithOpaqueMemberPointers.count(Ty)) { |
| 305 |
TypeCache.clear(); |
| 306 |
RecordsWithOpaqueMemberPointers.clear(); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
static llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext, |
| 311 |
const llvm::fltSemantics &format, |
| 312 |
bool UseNativeHalf = false) { |
| 313 |
if (&format == &llvm::APFloat::IEEEhalf()) { |
| 314 |
if (UseNativeHalf) |
| 315 |
return llvm::Type::getHalfTy(VMContext); |
| 316 |
else |
| 317 |
return llvm::Type::getInt16Ty(VMContext); |
| 318 |
} |
| 319 |
if (&format == &llvm::APFloat::BFloat()) |
| 320 |
return llvm::Type::getBFloatTy(VMContext); |
| 321 |
if (&format == &llvm::APFloat::IEEEsingle()) |
| 322 |
return llvm::Type::getFloatTy(VMContext); |
| 323 |
if (&format == &llvm::APFloat::IEEEdouble()) |
| 324 |
return llvm::Type::getDoubleTy(VMContext); |
| 325 |
if (&format == &llvm::APFloat::IEEEquad()) |
| 326 |
return llvm::Type::getFP128Ty(VMContext); |
| 327 |
if (&format == &llvm::APFloat::PPCDoubleDouble()) |
| 328 |
return llvm::Type::getPPC_FP128Ty(VMContext); |
| 329 |
if (&format == &llvm::APFloat::x87DoubleExtended()) |
| 330 |
return llvm::Type::getX86_FP80Ty(VMContext); |
| 331 |
llvm_unreachable("Unknown float format!"); |
| 332 |
} |
| 333 |
|
| 334 |
llvm::Type *CodeGenTypes::ConvertFunctionTypeInternal(QualType QFT) { |
| 335 |
assert(QFT.isCanonical()); |
| 336 |
const Type *Ty = QFT.getTypePtr(); |
| 337 |
const FunctionType *FT = cast<FunctionType>(QFT.getTypePtr()); |
| 338 |
// First, check whether we can build the full function type. If the |
| 339 |
// function type depends on an incomplete type (e.g. a struct or enum), we |
| 340 |
// cannot lower the function type. |
| 341 |
if (!isFuncTypeConvertible(FT)) { |
| 342 |
// This function's type depends on an incomplete tag type. |
| 343 |
|
| 344 |
// Force conversion of all the relevant record types, to make sure |
| 345 |
// we re-convert the FunctionType when appropriate. |
| 346 |
if (const RecordType *RT = FT->getReturnType()->getAs<RecordType>()) |
| 347 |
ConvertRecordDeclType(RT->getDecl()); |
| 348 |
if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) |
| 349 |
for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++) |
| 350 |
if (const RecordType *RT = FPT->getParamType(i)->getAs<RecordType>()) |
| 351 |
ConvertRecordDeclType(RT->getDecl()); |
| 352 |
|
| 353 |
SkippedLayout = true; |
| 354 |
|
| 355 |
// Return a placeholder type. |
| 356 |
return llvm::StructType::get(getLLVMContext()); |
| 357 |
} |
| 358 |
|
| 359 |
// While we're converting the parameter types for a function, we don't want |
| 360 |
// to recursively convert any pointed-to structs. Converting directly-used |
| 361 |
// structs is ok though. |
| 362 |
if (!RecordsBeingLaidOut.insert(Ty).second) { |
| 363 |
SkippedLayout = true; |
| 364 |
return llvm::StructType::get(getLLVMContext()); |
| 365 |
} |
| 366 |
|
| 367 |
// The function type can be built; call the appropriate routines to |
| 368 |
// build it. |
| 369 |
const CGFunctionInfo *FI; |
| 370 |
if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) { |
| 371 |
FI = &arrangeFreeFunctionType( |
| 372 |
CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0))); |
| 373 |
} else { |
| 374 |
const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT); |
| 375 |
FI = &arrangeFreeFunctionType( |
| 376 |
CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0))); |
| 377 |
} |
| 378 |
|
| 379 |
llvm::Type *ResultType = nullptr; |
| 380 |
// If there is something higher level prodding our CGFunctionInfo, then |
| 381 |
// don't recurse into it again. |
| 382 |
if (FunctionsBeingProcessed.count(FI)) { |
| 383 |
|
| 384 |
ResultType = llvm::StructType::get(getLLVMContext()); |
| 385 |
SkippedLayout = true; |
| 386 |
} else { |
| 387 |
|
| 388 |
// Otherwise, we're good to go, go ahead and convert it. |
| 389 |
ResultType = GetFunctionType(*FI); |
| 390 |
} |
| 391 |
|
| 392 |
RecordsBeingLaidOut.erase(Ty); |
| 393 |
|
| 394 |
if (RecordsBeingLaidOut.empty()) |
| 395 |
while (!DeferredRecords.empty()) |
| 396 |
ConvertRecordDeclType(DeferredRecords.pop_back_val()); |
| 397 |
return ResultType; |
| 398 |
} |
| 399 |
|
| 400 |
/// ConvertType - Convert the specified type to its LLVM form. |
| 401 |
llvm::Type *CodeGenTypes::ConvertType(QualType T) { |
| 402 |
T = Context.getCanonicalType(T); |
| 403 |
|
| 404 |
const Type *Ty = T.getTypePtr(); |
| 405 |
|
| 406 |
// For the device-side compilation, CUDA device builtin surface/texture types |
| 407 |
// may be represented in different types. |
| 408 |
if (Context.getLangOpts().CUDAIsDevice) { |
| 409 |
if (T->isCUDADeviceBuiltinSurfaceType()) { |
| 410 |
if (auto *Ty = CGM.getTargetCodeGenInfo() |
| 411 |
.getCUDADeviceBuiltinSurfaceDeviceType()) |
| 412 |
return Ty; |
| 413 |
} else if (T->isCUDADeviceBuiltinTextureType()) { |
| 414 |
if (auto *Ty = CGM.getTargetCodeGenInfo() |
| 415 |
.getCUDADeviceBuiltinTextureDeviceType()) |
| 416 |
return Ty; |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
// RecordTypes are cached and processed specially. |
| 421 |
if (const RecordType *RT = dyn_cast<RecordType>(Ty)) |
| 422 |
return ConvertRecordDeclType(RT->getDecl()); |
| 423 |
|
| 424 |
// The LLVM type we return for a given Clang type may not always be the same, |
| 425 |
// most notably when dealing with recursive structs. We mark these potential |
| 426 |
// cases with ShouldUseCache below. Builtin types cannot be recursive. |
| 427 |
// TODO: when clang uses LLVM opaque pointers we won't be able to represent |
| 428 |
// recursive types with LLVM types, making this logic much simpler. |
| 429 |
llvm::Type *CachedType = nullptr; |
| 430 |
bool ShouldUseCache = |
| 431 |
Ty->isBuiltinType() || |
| 432 |
(noRecordsBeingLaidOut() && FunctionsBeingProcessed.empty()); |
| 433 |
if (ShouldUseCache) { |
| 434 |
llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI = |
| 435 |
TypeCache.find(Ty); |
| 436 |
if (TCI != TypeCache.end()) |
| 437 |
CachedType = TCI->second; |
| 438 |
// With expensive checks, check that the type we compute matches the |
| 439 |
// cached type. |
| 440 |
#ifndef EXPENSIVE_CHECKS |
| 441 |
if (CachedType) |
| 442 |
return CachedType; |
| 443 |
#endif |
| 444 |
} |
| 445 |
|
| 446 |
// If we don't have it in the cache, convert it now. |
| 447 |
llvm::Type *ResultType = nullptr; |
| 448 |
switch (Ty->getTypeClass()) { |
| 449 |
case Type::Record: // Handled above. |
| 450 |
#define TYPE(Class, Base) |
| 451 |
#define ABSTRACT_TYPE(Class, Base) |
| 452 |
#define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 453 |
#define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 454 |
#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 455 |
#include "clang/AST/TypeNodes.inc" |
| 456 |
llvm_unreachable("Non-canonical or dependent types aren't possible."); |
| 457 |
|
| 458 |
case Type::Builtin: { |
| 459 |
switch (cast<BuiltinType>(Ty)->getKind()) { |
| 460 |
case BuiltinType::Void: |
| 461 |
case BuiltinType::ObjCId: |
| 462 |
case BuiltinType::ObjCClass: |
| 463 |
case BuiltinType::ObjCSel: |
| 464 |
// LLVM void type can only be used as the result of a function call. Just |
| 465 |
// map to the same as char. |
| 466 |
ResultType = llvm::Type::getInt8Ty(getLLVMContext()); |
| 467 |
break; |
| 468 |
|
| 469 |
case BuiltinType::Bool: |
| 470 |
// Note that we always return bool as i1 for use as a scalar type. |
| 471 |
ResultType = llvm::Type::getInt1Ty(getLLVMContext()); |
| 472 |
break; |
| 473 |
|
| 474 |
case BuiltinType::Char_S: |
| 475 |
case BuiltinType::Char_U: |
| 476 |
case BuiltinType::SChar: |
| 477 |
case BuiltinType::UChar: |
| 478 |
case BuiltinType::Short: |
| 479 |
case BuiltinType::UShort: |
| 480 |
case BuiltinType::Int: |
| 481 |
case BuiltinType::UInt: |
| 482 |
case BuiltinType::Long: |
| 483 |
case BuiltinType::ULong: |
| 484 |
case BuiltinType::LongLong: |
| 485 |
case BuiltinType::ULongLong: |
| 486 |
case BuiltinType::WChar_S: |
| 487 |
case BuiltinType::WChar_U: |
| 488 |
case BuiltinType::Char8: |
| 489 |
case BuiltinType::Char16: |
| 490 |
case BuiltinType::Char32: |
| 491 |
case BuiltinType::ShortAccum: |
| 492 |
case BuiltinType::Accum: |
| 493 |
case BuiltinType::LongAccum: |
| 494 |
case BuiltinType::UShortAccum: |
| 495 |
case BuiltinType::UAccum: |
| 496 |
case BuiltinType::ULongAccum: |
| 497 |
case BuiltinType::ShortFract: |
| 498 |
case BuiltinType::Fract: |
| 499 |
case BuiltinType::LongFract: |
| 500 |
case BuiltinType::UShortFract: |
| 501 |
case BuiltinType::UFract: |
| 502 |
case BuiltinType::ULongFract: |
| 503 |
case BuiltinType::SatShortAccum: |
| 504 |
case BuiltinType::SatAccum: |
| 505 |
case BuiltinType::SatLongAccum: |
| 506 |
case BuiltinType::SatUShortAccum: |
| 507 |
case BuiltinType::SatUAccum: |
| 508 |
case BuiltinType::SatULongAccum: |
| 509 |
case BuiltinType::SatShortFract: |
| 510 |
case BuiltinType::SatFract: |
| 511 |
case BuiltinType::SatLongFract: |
| 512 |
case BuiltinType::SatUShortFract: |
| 513 |
case BuiltinType::SatUFract: |
| 514 |
case BuiltinType::SatULongFract: |
| 515 |
ResultType = llvm::IntegerType::get(getLLVMContext(), |
| 516 |
static_cast<unsigned>(Context.getTypeSize(T))); |
| 517 |
break; |
| 518 |
|
| 519 |
case BuiltinType::Float16: |
| 520 |
ResultType = |
| 521 |
getTypeForFormat(getLLVMContext(), Context.getFloatTypeSemantics(T), |
| 522 |
/* UseNativeHalf = */ true); |
| 523 |
break; |
| 524 |
|
| 525 |
case BuiltinType::Half: |
| 526 |
// Half FP can either be storage-only (lowered to i16) or native. |
| 527 |
ResultType = getTypeForFormat( |
| 528 |
getLLVMContext(), Context.getFloatTypeSemantics(T), |
| 529 |
Context.getLangOpts().NativeHalfType || |
| 530 |
!Context.getTargetInfo().useFP16ConversionIntrinsics()); |
| 531 |
break; |
| 532 |
case BuiltinType::BFloat16: |
| 533 |
case BuiltinType::Float: |
| 534 |
case BuiltinType::Double: |
| 535 |
case BuiltinType::LongDouble: |
| 536 |
case BuiltinType::Float128: |
| 537 |
case BuiltinType::Ibm128: |
| 538 |
ResultType = getTypeForFormat(getLLVMContext(), |
| 539 |
Context.getFloatTypeSemantics(T), |
| 540 |
/* UseNativeHalf = */ false); |
| 541 |
break; |
| 542 |
|
| 543 |
case BuiltinType::NullPtr: |
| 544 |
// Model std::nullptr_t as i8* |
| 545 |
ResultType = llvm::Type::getInt8PtrTy(getLLVMContext()); |
| 546 |
break; |
| 547 |
|
| 548 |
case BuiltinType::UInt128: |
| 549 |
case BuiltinType::Int128: |
| 550 |
ResultType = llvm::IntegerType::get(getLLVMContext(), 128); |
| 551 |
break; |
| 552 |
|
| 553 |
#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 554 |
case BuiltinType::Id: |
| 555 |
#include "clang/Basic/OpenCLImageTypes.def" |
| 556 |
#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
| 557 |
case BuiltinType::Id: |
| 558 |
#include "clang/Basic/OpenCLExtensionTypes.def" |
| 559 |
case BuiltinType::OCLSampler: |
| 560 |
case BuiltinType::OCLEvent: |
| 561 |
case BuiltinType::OCLClkEvent: |
| 562 |
case BuiltinType::OCLQueue: |
| 563 |
case BuiltinType::OCLReserveID: |
| 564 |
ResultType = CGM.getOpenCLRuntime().convertOpenCLSpecificType(Ty); |
| 565 |
break; |
| 566 |
case BuiltinType::SveInt8: |
| 567 |
case BuiltinType::SveUint8: |
| 568 |
case BuiltinType::SveInt8x2: |
| 569 |
case BuiltinType::SveUint8x2: |
| 570 |
case BuiltinType::SveInt8x3: |
| 571 |
case BuiltinType::SveUint8x3: |
| 572 |
case BuiltinType::SveInt8x4: |
| 573 |
case BuiltinType::SveUint8x4: |
| 574 |
case BuiltinType::SveInt16: |
| 575 |
case BuiltinType::SveUint16: |
| 576 |
case BuiltinType::SveInt16x2: |
| 577 |
case BuiltinType::SveUint16x2: |
| 578 |
case BuiltinType::SveInt16x3: |
| 579 |
case BuiltinType::SveUint16x3: |
| 580 |
case BuiltinType::SveInt16x4: |
| 581 |
case BuiltinType::SveUint16x4: |
| 582 |
case BuiltinType::SveInt32: |
| 583 |
case BuiltinType::SveUint32: |
| 584 |
case BuiltinType::SveInt32x2: |
| 585 |
case BuiltinType::SveUint32x2: |
| 586 |
case BuiltinType::SveInt32x3: |
| 587 |
case BuiltinType::SveUint32x3: |
| 588 |
case BuiltinType::SveInt32x4: |
| 589 |
case BuiltinType::SveUint32x4: |
| 590 |
case BuiltinType::SveInt64: |
| 591 |
case BuiltinType::SveUint64: |
| 592 |
case BuiltinType::SveInt64x2: |
| 593 |
case BuiltinType::SveUint64x2: |
| 594 |
case BuiltinType::SveInt64x3: |
| 595 |
case BuiltinType::SveUint64x3: |
| 596 |
case BuiltinType::SveInt64x4: |
| 597 |
case BuiltinType::SveUint64x4: |
| 598 |
case BuiltinType::SveBool: |
| 599 |
case BuiltinType::SveFloat16: |
| 600 |
case BuiltinType::SveFloat16x2: |
| 601 |
case BuiltinType::SveFloat16x3: |
| 602 |
case BuiltinType::SveFloat16x4: |
| 603 |
case BuiltinType::SveFloat32: |
| 604 |
case BuiltinType::SveFloat32x2: |
| 605 |
case BuiltinType::SveFloat32x3: |
| 606 |
case BuiltinType::SveFloat32x4: |
| 607 |
case BuiltinType::SveFloat64: |
| 608 |
case BuiltinType::SveFloat64x2: |
| 609 |
case BuiltinType::SveFloat64x3: |
| 610 |
case BuiltinType::SveFloat64x4: |
| 611 |
case BuiltinType::SveBFloat16: |
| 612 |
case BuiltinType::SveBFloat16x2: |
| 613 |
case BuiltinType::SveBFloat16x3: |
| 614 |
case BuiltinType::SveBFloat16x4: { |
| 615 |
ASTContext::BuiltinVectorTypeInfo Info = |
| 616 |
Context.getBuiltinVectorTypeInfo(cast<BuiltinType>(Ty)); |
| 617 |
return llvm::ScalableVectorType::get(ConvertType(Info.ElementType), |
| 618 |
Info.EC.getKnownMinValue() * |
| 619 |
Info.NumVectors); |
| 620 |
} |
| 621 |
#define PPC_VECTOR_TYPE(Name, Id, Size) \ |
| 622 |
case BuiltinType::Id: \ |
| 623 |
ResultType = \ |
| 624 |
llvm::FixedVectorType::get(ConvertType(Context.BoolTy), Size); \ |
| 625 |
break; |
| 626 |
#include "clang/Basic/PPCTypes.def" |
| 627 |
#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
| 628 |
#include "clang/Basic/RISCVVTypes.def" |
| 629 |
{ |
| 630 |
ASTContext::BuiltinVectorTypeInfo Info = |
| 631 |
Context.getBuiltinVectorTypeInfo(cast<BuiltinType>(Ty)); |
| 632 |
return llvm::ScalableVectorType::get(ConvertType(Info.ElementType), |
| 633 |
Info.EC.getKnownMinValue() * |
| 634 |
Info.NumVectors); |
| 635 |
} |
| 636 |
case BuiltinType::Dependent: |
| 637 |
#define BUILTIN_TYPE(Id, SingletonId) |
| 638 |
#define PLACEHOLDER_TYPE(Id, SingletonId) \ |
| 639 |
case BuiltinType::Id: |
| 640 |
#include "clang/AST/BuiltinTypes.def" |
| 641 |
llvm_unreachable("Unexpected placeholder builtin type!"); |
| 642 |
} |
| 643 |
break; |
| 644 |
} |
| 645 |
case Type::Auto: |
| 646 |
case Type::DeducedTemplateSpecialization: |
| 647 |
llvm_unreachable("Unexpected undeduced type!"); |
| 648 |
case Type::Complex: { |
| 649 |
llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType()); |
| 650 |
ResultType = llvm::StructType::get(EltTy, EltTy); |
| 651 |
break; |
| 652 |
} |
| 653 |
case Type::LValueReference: |
| 654 |
case Type::RValueReference: { |
| 655 |
const ReferenceType *RTy = cast<ReferenceType>(Ty); |
| 656 |
QualType ETy = RTy->getPointeeType(); |
| 657 |
llvm::Type *PointeeType = ConvertTypeForMem(ETy); |
| 658 |
unsigned AS = getTargetAddressSpace(ETy); |
| 659 |
ResultType = llvm::PointerType::get(PointeeType, AS); |
| 660 |
break; |
| 661 |
} |
| 662 |
case Type::Pointer: { |
| 663 |
const PointerType *PTy = cast<PointerType>(Ty); |
| 664 |
QualType ETy = PTy->getPointeeType(); |
| 665 |
llvm::Type *PointeeType = ConvertTypeForMem(ETy); |
| 666 |
if (PointeeType->isVoidTy()) |
| 667 |
PointeeType = llvm::Type::getInt8Ty(getLLVMContext()); |
| 668 |
unsigned AS = getTargetAddressSpace(ETy); |
| 669 |
ResultType = llvm::PointerType::get(PointeeType, AS); |
| 670 |
break; |
| 671 |
} |
| 672 |
|
| 673 |
case Type::VariableArray: { |
| 674 |
const VariableArrayType *A = cast<VariableArrayType>(Ty); |
| 675 |
assert(A->getIndexTypeCVRQualifiers() == 0 && |
| 676 |
"FIXME: We only handle trivial array types so far!"); |
| 677 |
// VLAs resolve to the innermost element type; this matches |
| 678 |
// the return of alloca, and there isn't any obviously better choice. |
| 679 |
ResultType = ConvertTypeForMem(A->getElementType()); |
| 680 |
break; |
| 681 |
} |
| 682 |
case Type::IncompleteArray: { |
| 683 |
const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty); |
| 684 |
assert(A->getIndexTypeCVRQualifiers() == 0 && |
| 685 |
"FIXME: We only handle trivial array types so far!"); |
| 686 |
// int X[] -> [0 x int], unless the element type is not sized. If it is |
| 687 |
// unsized (e.g. an incomplete struct) just use [0 x i8]. |
| 688 |
ResultType = ConvertTypeForMem(A->getElementType()); |
| 689 |
if (!ResultType->isSized()) { |
| 690 |
SkippedLayout = true; |
| 691 |
ResultType = llvm::Type::getInt8Ty(getLLVMContext()); |
| 692 |
} |
| 693 |
ResultType = llvm::ArrayType::get(ResultType, 0); |
| 694 |
break; |
| 695 |
} |
| 696 |
case Type::ConstantArray: { |
| 697 |
const ConstantArrayType *A = cast<ConstantArrayType>(Ty); |
| 698 |
llvm::Type *EltTy = ConvertTypeForMem(A->getElementType()); |
| 699 |
|
| 700 |
// Lower arrays of undefined struct type to arrays of i8 just to have a |
| 701 |
// concrete type. |
| 702 |
if (!EltTy->isSized()) { |
| 703 |
SkippedLayout = true; |
| 704 |
EltTy = llvm::Type::getInt8Ty(getLLVMContext()); |
| 705 |
} |
| 706 |
|
| 707 |
ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue()); |
| 708 |
break; |
| 709 |
} |
| 710 |
case Type::ExtVector: |
| 711 |
case Type::Vector: { |
| 712 |
const auto *VT = cast<VectorType>(Ty); |
| 713 |
// An ext_vector_type of Bool is really a vector of bits. |
| 714 |
llvm::Type *IRElemTy = VT->isExtVectorBoolType() |
| 715 |
? llvm::Type::getInt1Ty(getLLVMContext()) |
| 716 |
: ConvertType(VT->getElementType()); |
| 717 |
ResultType = llvm::FixedVectorType::get(IRElemTy, VT->getNumElements()); |
| 718 |
break; |
| 719 |
} |
| 720 |
case Type::ConstantMatrix: { |
| 721 |
const ConstantMatrixType *MT = cast<ConstantMatrixType>(Ty); |
| 722 |
ResultType = |
| 723 |
llvm::FixedVectorType::get(ConvertType(MT->getElementType()), |
| 724 |
MT->getNumRows() * MT->getNumColumns()); |
| 725 |
break; |
| 726 |
} |
| 727 |
case Type::FunctionNoProto: |
| 728 |
case Type::FunctionProto: |
| 729 |
ResultType = ConvertFunctionTypeInternal(T); |
| 730 |
break; |
| 731 |
case Type::ObjCObject: |
| 732 |
ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType()); |
| 733 |
break; |
| 734 |
|
| 735 |
case Type::ObjCInterface: { |
| 736 |
// Objective-C interfaces are always opaque (outside of the |
| 737 |
// runtime, which can do whatever it likes); we never refine |
| 738 |
// these. |
| 739 |
llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)]; |
| 740 |
if (!T) |
| 741 |
T = llvm::StructType::create(getLLVMContext()); |
| 742 |
ResultType = T; |
| 743 |
break; |
| 744 |
} |
| 745 |
|
| 746 |
case Type::ObjCObjectPointer: { |
| 747 |
// Protocol qualifications do not influence the LLVM type, we just return a |
| 748 |
// pointer to the underlying interface type. We don't need to worry about |
| 749 |
// recursive conversion. |
| 750 |
llvm::Type *T = |
| 751 |
ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType()); |
| 752 |
ResultType = T->getPointerTo(); |
| 753 |
break; |
| 754 |
} |
| 755 |
|
| 756 |
case Type::Enum: { |
| 757 |
const EnumDecl *ED = cast<EnumType>(Ty)->getDecl(); |
| 758 |
if (ED->isCompleteDefinition() || ED->isFixed()) |
| 759 |
return ConvertType(ED->getIntegerType()); |
| 760 |
// Return a placeholder 'i32' type. This can be changed later when the |
| 761 |
// type is defined (see UpdateCompletedType), but is likely to be the |
| 762 |
// "right" answer. |
| 763 |
ResultType = llvm::Type::getInt32Ty(getLLVMContext()); |
| 764 |
break; |
| 765 |
} |
| 766 |
|
| 767 |
case Type::BlockPointer: { |
| 768 |
const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType(); |
| 769 |
llvm::Type *PointeeType = CGM.getLangOpts().OpenCL |
| 770 |
? CGM.getGenericBlockLiteralType() |
| 771 |
: ConvertTypeForMem(FTy); |
| 772 |
// Block pointers lower to function type. For function type, |
| 773 |
// getTargetAddressSpace() returns default address space for |
| 774 |
// function pointer i.e. program address space. Therefore, for block |
| 775 |
// pointers, it is important to pass the pointee AST address space when |
| 776 |
// calling getTargetAddressSpace(), to ensure that we get the LLVM IR |
| 777 |
// address space for data pointers and not function pointers. |
| 778 |
unsigned AS = Context.getTargetAddressSpace(FTy.getAddressSpace()); |
| 779 |
ResultType = llvm::PointerType::get(PointeeType, AS); |
| 780 |
break; |
| 781 |
} |
| 782 |
|
| 783 |
case Type::MemberPointer: { |
| 784 |
auto *MPTy = cast<MemberPointerType>(Ty); |
| 785 |
if (!getCXXABI().isMemberPointerConvertible(MPTy)) { |
| 786 |
auto *C = MPTy->getClass(); |
| 787 |
auto Insertion = RecordsWithOpaqueMemberPointers.insert({C, nullptr}); |
| 788 |
if (Insertion.second) |
| 789 |
Insertion.first->second = llvm::StructType::create(getLLVMContext()); |
| 790 |
ResultType = Insertion.first->second; |
| 791 |
} else { |
| 792 |
ResultType = getCXXABI().ConvertMemberPointerType(MPTy); |
| 793 |
} |
| 794 |
break; |
| 795 |
} |
| 796 |
|
| 797 |
case Type::Atomic: { |
| 798 |
QualType valueType = cast<AtomicType>(Ty)->getValueType(); |
| 799 |
ResultType = ConvertTypeForMem(valueType); |
| 800 |
|
| 801 |
// Pad out to the inflated size if necessary. |
| 802 |
uint64_t valueSize = Context.getTypeSize(valueType); |
| 803 |
uint64_t atomicSize = Context.getTypeSize(Ty); |
| 804 |
if (valueSize != atomicSize) { |
| 805 |
assert(valueSize < atomicSize); |
| 806 |
llvm::Type *elts[] = { |
| 807 |
ResultType, |
| 808 |
llvm::ArrayType::get(CGM.Int8Ty, (atomicSize - valueSize) / 8) |
| 809 |
}; |
| 810 |
ResultType = |
| 811 |
llvm::StructType::get(getLLVMContext(), llvm::ArrayRef(elts)); |
| 812 |
} |
| 813 |
break; |
| 814 |
} |
| 815 |
case Type::Pipe: { |
| 816 |
ResultType = CGM.getOpenCLRuntime().getPipeType(cast<PipeType>(Ty)); |
| 817 |
break; |
| 818 |
} |
| 819 |
case Type::BitInt: { |
| 820 |
const auto &EIT = cast<BitIntType>(Ty); |
| 821 |
ResultType = llvm::Type::getIntNTy(getLLVMContext(), EIT->getNumBits()); |
| 822 |
break; |
| 823 |
} |
| 824 |
} |
| 825 |
|
| 826 |
assert(ResultType && "Didn't convert a type?"); |
| 827 |
assert((!CachedType || CachedType == ResultType) && |
| 828 |
"Cached type doesn't match computed type"); |
| 829 |
|
| 830 |
if (ShouldUseCache) |
| 831 |
TypeCache[Ty] = ResultType; |
| 832 |
return ResultType; |
| 833 |
} |
| 834 |
|
| 835 |
bool CodeGenModule::isPaddedAtomicType(QualType type) { |
| 836 |
return isPaddedAtomicType(type->castAs<AtomicType>()); |
| 837 |
} |
| 838 |
|
| 839 |
bool CodeGenModule::isPaddedAtomicType(const AtomicType *type) { |
| 840 |
return Context.getTypeSize(type) != Context.getTypeSize(type->getValueType()); |
| 841 |
} |
| 842 |
|
| 843 |
/// ConvertRecordDeclType - Lay out a tagged decl type like struct or union. |
| 844 |
llvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) { |
| 845 |
// TagDecl's are not necessarily unique, instead use the (clang) |
| 846 |
// type connected to the decl. |
| 847 |
const Type *Key = Context.getTagDeclType(RD).getTypePtr(); |
| 848 |
|
| 849 |
llvm::StructType *&Entry = RecordDeclTypes[Key]; |
| 850 |
|
| 851 |
// If we don't have a StructType at all yet, create the forward declaration. |
| 852 |
if (!Entry) { |
| 853 |
Entry = llvm::StructType::create(getLLVMContext()); |
| 854 |
addRecordTypeName(RD, Entry, ""); |
| 855 |
} |
| 856 |
llvm::StructType *Ty = Entry; |
| 857 |
|
| 858 |
// If this is still a forward declaration, or the LLVM type is already |
| 859 |
// complete, there's nothing more to do. |
| 860 |
RD = RD->getDefinition(); |
| 861 |
if (!RD || !RD->isCompleteDefinition() || !Ty->isOpaque()) |
| 862 |
return Ty; |
| 863 |
|
| 864 |
// If converting this type would cause us to infinitely loop, don't do it! |
| 865 |
if (!isSafeToConvert(RD, *this)) { |
| 866 |
DeferredRecords.push_back(RD); |
| 867 |
return Ty; |
| 868 |
} |
| 869 |
|
| 870 |
// Okay, this is a definition of a type. Compile the implementation now. |
| 871 |
bool InsertResult = RecordsBeingLaidOut.insert(Key).second; |
| 872 |
(void)InsertResult; |
| 873 |
assert(InsertResult && "Recursively compiling a struct?"); |
| 874 |
|
| 875 |
// Force conversion of non-virtual base classes recursively. |
| 876 |
if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 877 |
for (const auto &I : CRD->bases()) { |
| 878 |
if (I.isVirtual()) continue; |
| 879 |
ConvertRecordDeclType(I.getType()->castAs<RecordType>()->getDecl()); |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
// Layout fields. |
| 884 |
std::unique_ptr<CGRecordLayout> Layout = ComputeRecordLayout(RD, Ty); |
| 885 |
CGRecordLayouts[Key] = std::move(Layout); |
| 886 |
|
| 887 |
// We're done laying out this struct. |
| 888 |
bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult; |
| 889 |
assert(EraseResult && "struct not in RecordsBeingLaidOut set?"); |
| 890 |
|
| 891 |
// If this struct blocked a FunctionType conversion, then recompute whatever |
| 892 |
// was derived from that. |
| 893 |
// FIXME: This is hugely overconservative. |
| 894 |
if (SkippedLayout) |
| 895 |
TypeCache.clear(); |
| 896 |
|
| 897 |
// If we're done converting the outer-most record, then convert any deferred |
| 898 |
// structs as well. |
| 899 |
if (RecordsBeingLaidOut.empty()) |
| 900 |
while (!DeferredRecords.empty()) |
| 901 |
ConvertRecordDeclType(DeferredRecords.pop_back_val()); |
| 902 |
|
| 903 |
return Ty; |
| 904 |
} |
| 905 |
|
| 906 |
/// getCGRecordLayout - Return record layout info for the given record decl. |
| 907 |
const CGRecordLayout & |
| 908 |
CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) { |
| 909 |
const Type *Key = Context.getTagDeclType(RD).getTypePtr(); |
| 910 |
|
| 911 |
auto I = CGRecordLayouts.find(Key); |
| 912 |
if (I != CGRecordLayouts.end()) |
| 913 |
return *I->second; |
| 914 |
// Compute the type information. |
| 915 |
ConvertRecordDeclType(RD); |
| 916 |
|
| 917 |
// Now try again. |
| 918 |
I = CGRecordLayouts.find(Key); |
| 919 |
|
| 920 |
assert(I != CGRecordLayouts.end() && |
| 921 |
"Unable to find record layout information for type"); |
| 922 |
return *I->second; |
| 923 |
} |
| 924 |
|
| 925 |
bool CodeGenTypes::isPointerZeroInitializable(QualType T) { |
| 926 |
assert((T->isAnyPointerType() || T->isBlockPointerType()) && "Invalid type"); |
| 927 |
return isZeroInitializable(T); |
| 928 |
} |
| 929 |
|
| 930 |
bool CodeGenTypes::isZeroInitializable(QualType T) { |
| 931 |
if (T->getAs<PointerType>()) |
| 932 |
return Context.getTargetNullPointerValue(T) == 0; |
| 933 |
|
| 934 |
if (const auto *AT = Context.getAsArrayType(T)) { |
| 935 |
if (isa<IncompleteArrayType>(AT)) |
| 936 |
return true; |
| 937 |
if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 938 |
if (Context.getConstantArrayElementCount(CAT) == 0) |
| 939 |
return true; |
| 940 |
T = Context.getBaseElementType(T); |
| 941 |
} |
| 942 |
|
| 943 |
// Records are non-zero-initializable if they contain any |
| 944 |
// non-zero-initializable subobjects. |
| 945 |
if (const RecordType *RT = T->getAs<RecordType>()) { |
| 946 |
const RecordDecl *RD = RT->getDecl(); |
| 947 |
return isZeroInitializable(RD); |
| 948 |
} |
| 949 |
|
| 950 |
// We have to ask the ABI about member pointers. |
| 951 |
if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) |
| 952 |
return getCXXABI().isZeroInitializable(MPT); |
| 953 |
|
| 954 |
// Everything else is okay. |
| 955 |
return true; |
| 956 |
} |
| 957 |
|
| 958 |
bool CodeGenTypes::isZeroInitializable(const RecordDecl *RD) { |
| 959 |
return getCGRecordLayout(RD).isZeroInitializable(); |
| 960 |
} |
| 961 |
|
| 962 |
unsigned CodeGenTypes::getTargetAddressSpace(QualType T) const { |
| 963 |
// Return the address space for the type. If the type is a |
| 964 |
// function type without an address space qualifier, the |
| 965 |
// program address space is used. Otherwise, the target picks |
| 966 |
// the best address space based on the type information |
| 967 |
return T->isFunctionType() && !T.hasAddressSpace() |
| 968 |
? getDataLayout().getProgramAddressSpace() |
| 969 |
: getContext().getTargetAddressSpace(T.getAddressSpace()); |
| 970 |
} |