1 //===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This is the code that handles AST -> LLVM type lowering. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "CodeGenTypes.h" 15 #include "CGCXXABI.h" 16 #include "CGCall.h" 17 #include "CGOpenCLRuntime.h" 18 #include "CGRecordLayout.h" 19 #include "TargetInfo.h" 20 #include "clang/AST/ASTContext.h" 21 #include "clang/AST/DeclCXX.h" 22 #include "clang/AST/DeclObjC.h" 23 #include "clang/AST/Expr.h" 24 #include "clang/AST/RecordLayout.h" 25 #include "clang/CodeGen/CGFunctionInfo.h" 26 #include "llvm/IR/DataLayout.h" 27 #include "llvm/IR/DerivedTypes.h" 28 #include "llvm/IR/Module.h" 29 using namespace clang; 30 using namespace CodeGen; 31 32 CodeGenTypes::CodeGenTypes(CodeGenModule &cgm) 33 : CGM(cgm), Context(cgm.getContext()), TheModule(cgm.getModule()), 34 TheDataLayout(cgm.getDataLayout()), 35 Target(cgm.getTarget()), TheCXXABI(cgm.getCXXABI()), 36 TheABIInfo(cgm.getTargetCodeGenInfo().getABIInfo()) { 37 SkippedLayout = false; 38 } 39 40 CodeGenTypes::~CodeGenTypes() { 41 llvm::DeleteContainerSeconds(CGRecordLayouts); 42 43 for (llvm::FoldingSet<CGFunctionInfo>::iterator 44 I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; ) 45 delete &*I++; 46 } 47 48 void CodeGenTypes::addRecordTypeName(const RecordDecl *RD, 49 llvm::StructType *Ty, 50 StringRef suffix) { 51 SmallString<256> TypeName; 52 llvm::raw_svector_ostream OS(TypeName); 53 OS << RD->getKindName() << '.'; 54 55 // Name the codegen type after the typedef name 56 // if there is no tag type name available 57 if (RD->getIdentifier()) { 58 // FIXME: We should not have to check for a null decl context here. 59 // Right now we do it because the implicit Obj-C decls don't have one. 60 if (RD->getDeclContext()) 61 RD->printQualifiedName(OS); 62 else 63 RD->printName(OS); 64 } else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) { 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 (TDD->getDeclContext()) 68 TDD->printQualifiedName(OS); 69 else 70 TDD->printName(OS); 71 } else 72 OS << "anon"; 73 74 if (!suffix.empty()) 75 OS << suffix; 76 77 Ty->setName(OS.str()); 78 } 79 80 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from 81 /// ConvertType in that it is used to convert to the memory representation for 82 /// a type. For example, the scalar representation for _Bool is i1, but the 83 /// memory representation is usually i8 or i32, depending on the target. 84 llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T) { 85 llvm::Type *R = ConvertType(T); 86 87 // If this is a non-bool type, don't map it. 88 if (!R->isIntegerTy(1)) 89 return R; 90 91 // Otherwise, return an integer of the target-specified size. 92 return llvm::IntegerType::get(getLLVMContext(), 93 (unsigned)Context.getTypeSize(T)); 94 } 95 96 97 /// isRecordLayoutComplete - Return true if the specified type is already 98 /// completely laid out. 99 bool CodeGenTypes::isRecordLayoutComplete(const Type *Ty) const { 100 llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I = 101 RecordDeclTypes.find(Ty); 102 return I != RecordDeclTypes.end() && !I->second->isOpaque(); 103 } 104 105 static bool 106 isSafeToConvert(QualType T, CodeGenTypes &CGT, 107 llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked); 108 109 110 /// isSafeToConvert - Return true if it is safe to convert the specified record 111 /// decl to IR and lay it out, false if doing so would cause us to get into a 112 /// recursive compilation mess. 113 static bool 114 isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT, 115 llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) { 116 // If we have already checked this type (maybe the same type is used by-value 117 // multiple times in multiple structure fields, don't check again. 118 if (!AlreadyChecked.insert(RD).second) 119 return true; 120 121 const Type *Key = CGT.getContext().getTagDeclType(RD).getTypePtr(); 122 123 // If this type is already laid out, converting it is a noop. 124 if (CGT.isRecordLayoutComplete(Key)) return true; 125 126 // If this type is currently being laid out, we can't recursively compile it. 127 if (CGT.isRecordBeingLaidOut(Key)) 128 return false; 129 130 // If this type would require laying out bases that are currently being laid 131 // out, don't do it. This includes virtual base classes which get laid out 132 // when a class is translated, even though they aren't embedded by-value into 133 // the class. 134 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { 135 for (const auto &I : CRD->bases()) 136 if (!isSafeToConvert(I.getType()->getAs<RecordType>()->getDecl(), 137 CGT, AlreadyChecked)) 138 return false; 139 } 140 141 // If this type would require laying out members that are currently being laid 142 // out, don't do it. 143 for (const auto *I : RD->fields()) 144 if (!isSafeToConvert(I->getType(), CGT, AlreadyChecked)) 145 return false; 146 147 // If there are no problems, lets do it. 148 return true; 149 } 150 151 /// isSafeToConvert - Return true if it is safe to convert this field type, 152 /// which requires the structure elements contained by-value to all be 153 /// recursively safe to convert. 154 static bool 155 isSafeToConvert(QualType T, CodeGenTypes &CGT, 156 llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) { 157 // Strip off atomic type sugar. 158 if (const auto *AT = T->getAs<AtomicType>()) 159 T = AT->getValueType(); 160 161 // If this is a record, check it. 162 if (const auto *RT = T->getAs<RecordType>()) 163 return isSafeToConvert(RT->getDecl(), CGT, AlreadyChecked); 164 165 // If this is an array, check the elements, which are embedded inline. 166 if (const auto *AT = CGT.getContext().getAsArrayType(T)) 167 return isSafeToConvert(AT->getElementType(), CGT, AlreadyChecked); 168 169 // Otherwise, there is no concern about transforming this. We only care about 170 // things that are contained by-value in a structure that can have another 171 // structure as a member. 172 return true; 173 } 174 175 176 /// isSafeToConvert - Return true if it is safe to convert the specified record 177 /// decl to IR and lay it out, false if doing so would cause us to get into a 178 /// recursive compilation mess. 179 static bool isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT) { 180 // If no structs are being laid out, we can certainly do this one. 181 if (CGT.noRecordsBeingLaidOut()) return true; 182 183 llvm::SmallPtrSet<const RecordDecl*, 16> AlreadyChecked; 184 return isSafeToConvert(RD, CGT, AlreadyChecked); 185 } 186 187 /// isFuncParamTypeConvertible - Return true if the specified type in a 188 /// function parameter or result position can be converted to an IR type at this 189 /// point. This boils down to being whether it is complete, as well as whether 190 /// we've temporarily deferred expanding the type because we're in a recursive 191 /// context. 192 bool CodeGenTypes::isFuncParamTypeConvertible(QualType Ty) { 193 // Some ABIs cannot have their member pointers represented in IR unless 194 // certain circumstances have been reached. 195 if (const auto *MPT = Ty->getAs<MemberPointerType>()) 196 return getCXXABI().isMemberPointerConvertible(MPT); 197 198 // If this isn't a tagged type, we can convert it! 199 const TagType *TT = Ty->getAs<TagType>(); 200 if (!TT) return true; 201 202 // Incomplete types cannot be converted. 203 if (TT->isIncompleteType()) 204 return false; 205 206 // If this is an enum, then it is always safe to convert. 207 const RecordType *RT = dyn_cast<RecordType>(TT); 208 if (!RT) return true; 209 210 // Otherwise, we have to be careful. If it is a struct that we're in the 211 // process of expanding, then we can't convert the function type. That's ok 212 // though because we must be in a pointer context under the struct, so we can 213 // just convert it to a dummy type. 214 // 215 // We decide this by checking whether ConvertRecordDeclType returns us an 216 // opaque type for a struct that we know is defined. 217 return isSafeToConvert(RT->getDecl(), *this); 218 } 219 220 221 /// Code to verify a given function type is complete, i.e. the return type 222 /// and all of the parameter types are complete. Also check to see if we are in 223 /// a RS_StructPointer context, and if so whether any struct types have been 224 /// pended. If so, we don't want to ask the ABI lowering code to handle a type 225 /// that cannot be converted to an IR type. 226 bool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) { 227 if (!isFuncParamTypeConvertible(FT->getReturnType())) 228 return false; 229 230 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) 231 for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++) 232 if (!isFuncParamTypeConvertible(FPT->getParamType(i))) 233 return false; 234 235 return true; 236 } 237 238 /// UpdateCompletedType - When we find the full definition for a TagDecl, 239 /// replace the 'opaque' type we previously made for it if applicable. 240 void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) { 241 // If this is an enum being completed, then we flush all non-struct types from 242 // the cache. This allows function types and other things that may be derived 243 // from the enum to be recomputed. 244 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) { 245 // Only flush the cache if we've actually already converted this type. 246 if (TypeCache.count(ED->getTypeForDecl())) { 247 // Okay, we formed some types based on this. We speculated that the enum 248 // would be lowered to i32, so we only need to flush the cache if this 249 // didn't happen. 250 if (!ConvertType(ED->getIntegerType())->isIntegerTy(32)) 251 TypeCache.clear(); 252 } 253 // If necessary, provide the full definition of a type only used with a 254 // declaration so far. 255 if (CGDebugInfo *DI = CGM.getModuleDebugInfo()) 256 DI->completeType(ED); 257 return; 258 } 259 260 // If we completed a RecordDecl that we previously used and converted to an 261 // anonymous type, then go ahead and complete it now. 262 const RecordDecl *RD = cast<RecordDecl>(TD); 263 if (RD->isDependentType()) return; 264 265 // Only complete it if we converted it already. If we haven't converted it 266 // yet, we'll just do it lazily. 267 if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr())) 268 ConvertRecordDeclType(RD); 269 270 // If necessary, provide the full definition of a type only used with a 271 // declaration so far. 272 if (CGDebugInfo *DI = CGM.getModuleDebugInfo()) 273 DI->completeType(RD); 274 } 275 276 static llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext, 277 const llvm::fltSemantics &format, 278 bool UseNativeHalf = false) { 279 if (&format == &llvm::APFloat::IEEEhalf) { 280 if (UseNativeHalf) 281 return llvm::Type::getHalfTy(VMContext); 282 else 283 return llvm::Type::getInt16Ty(VMContext); 284 } 285 if (&format == &llvm::APFloat::IEEEsingle) 286 return llvm::Type::getFloatTy(VMContext); 287 if (&format == &llvm::APFloat::IEEEdouble) 288 return llvm::Type::getDoubleTy(VMContext); 289 if (&format == &llvm::APFloat::IEEEquad) 290 return llvm::Type::getFP128Ty(VMContext); 291 if (&format == &llvm::APFloat::PPCDoubleDouble) 292 return llvm::Type::getPPC_FP128Ty(VMContext); 293 if (&format == &llvm::APFloat::x87DoubleExtended) 294 return llvm::Type::getX86_FP80Ty(VMContext); 295 llvm_unreachable("Unknown float format!"); 296 } 297 298 /// ConvertType - Convert the specified type to its LLVM form. 299 llvm::Type *CodeGenTypes::ConvertType(QualType T) { 300 T = Context.getCanonicalType(T); 301 302 const Type *Ty = T.getTypePtr(); 303 304 // RecordTypes are cached and processed specially. 305 if (const RecordType *RT = dyn_cast<RecordType>(Ty)) 306 return ConvertRecordDeclType(RT->getDecl()); 307 308 // See if type is already cached. 309 llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI = TypeCache.find(Ty); 310 // If type is found in map then use it. Otherwise, convert type T. 311 if (TCI != TypeCache.end()) 312 return TCI->second; 313 314 // If we don't have it in the cache, convert it now. 315 llvm::Type *ResultType = nullptr; 316 switch (Ty->getTypeClass()) { 317 case Type::Record: // Handled above. 318 #define TYPE(Class, Base) 319 #define ABSTRACT_TYPE(Class, Base) 320 #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 321 #define DEPENDENT_TYPE(Class, Base) case Type::Class: 322 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: 323 #include "clang/AST/TypeNodes.def" 324 llvm_unreachable("Non-canonical or dependent types aren't possible."); 325 326 case Type::Builtin: { 327 switch (cast<BuiltinType>(Ty)->getKind()) { 328 case BuiltinType::Void: 329 case BuiltinType::ObjCId: 330 case BuiltinType::ObjCClass: 331 case BuiltinType::ObjCSel: 332 // LLVM void type can only be used as the result of a function call. Just 333 // map to the same as char. 334 ResultType = llvm::Type::getInt8Ty(getLLVMContext()); 335 break; 336 337 case BuiltinType::Bool: 338 // Note that we always return bool as i1 for use as a scalar type. 339 ResultType = llvm::Type::getInt1Ty(getLLVMContext()); 340 break; 341 342 case BuiltinType::Char_S: 343 case BuiltinType::Char_U: 344 case BuiltinType::SChar: 345 case BuiltinType::UChar: 346 case BuiltinType::Short: 347 case BuiltinType::UShort: 348 case BuiltinType::Int: 349 case BuiltinType::UInt: 350 case BuiltinType::Long: 351 case BuiltinType::ULong: 352 case BuiltinType::LongLong: 353 case BuiltinType::ULongLong: 354 case BuiltinType::WChar_S: 355 case BuiltinType::WChar_U: 356 case BuiltinType::Char16: 357 case BuiltinType::Char32: 358 ResultType = llvm::IntegerType::get(getLLVMContext(), 359 static_cast<unsigned>(Context.getTypeSize(T))); 360 break; 361 362 case BuiltinType::Half: 363 // Half FP can either be storage-only (lowered to i16) or native. 364 ResultType = 365 getTypeForFormat(getLLVMContext(), Context.getFloatTypeSemantics(T), 366 Context.getLangOpts().NativeHalfType || 367 Context.getLangOpts().HalfArgsAndReturns); 368 break; 369 case BuiltinType::Float: 370 case BuiltinType::Double: 371 case BuiltinType::LongDouble: 372 ResultType = getTypeForFormat(getLLVMContext(), 373 Context.getFloatTypeSemantics(T), 374 /* UseNativeHalf = */ false); 375 break; 376 377 case BuiltinType::NullPtr: 378 // Model std::nullptr_t as i8* 379 ResultType = llvm::Type::getInt8PtrTy(getLLVMContext()); 380 break; 381 382 case BuiltinType::UInt128: 383 case BuiltinType::Int128: 384 ResultType = llvm::IntegerType::get(getLLVMContext(), 128); 385 break; 386 387 case BuiltinType::OCLImage1d: 388 case BuiltinType::OCLImage1dArray: 389 case BuiltinType::OCLImage1dBuffer: 390 case BuiltinType::OCLImage2d: 391 case BuiltinType::OCLImage2dArray: 392 case BuiltinType::OCLImage3d: 393 case BuiltinType::OCLSampler: 394 case BuiltinType::OCLEvent: 395 ResultType = CGM.getOpenCLRuntime().convertOpenCLSpecificType(Ty); 396 break; 397 398 case BuiltinType::Dependent: 399 #define BUILTIN_TYPE(Id, SingletonId) 400 #define PLACEHOLDER_TYPE(Id, SingletonId) \ 401 case BuiltinType::Id: 402 #include "clang/AST/BuiltinTypes.def" 403 llvm_unreachable("Unexpected placeholder builtin type!"); 404 } 405 break; 406 } 407 case Type::Auto: 408 llvm_unreachable("Unexpected undeduced auto type!"); 409 case Type::Complex: { 410 llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType()); 411 ResultType = llvm::StructType::get(EltTy, EltTy, nullptr); 412 break; 413 } 414 case Type::LValueReference: 415 case Type::RValueReference: { 416 const ReferenceType *RTy = cast<ReferenceType>(Ty); 417 QualType ETy = RTy->getPointeeType(); 418 llvm::Type *PointeeType = ConvertTypeForMem(ETy); 419 unsigned AS = Context.getTargetAddressSpace(ETy); 420 ResultType = llvm::PointerType::get(PointeeType, AS); 421 break; 422 } 423 case Type::Pointer: { 424 const PointerType *PTy = cast<PointerType>(Ty); 425 QualType ETy = PTy->getPointeeType(); 426 llvm::Type *PointeeType = ConvertTypeForMem(ETy); 427 if (PointeeType->isVoidTy()) 428 PointeeType = llvm::Type::getInt8Ty(getLLVMContext()); 429 unsigned AS = Context.getTargetAddressSpace(ETy); 430 ResultType = llvm::PointerType::get(PointeeType, AS); 431 break; 432 } 433 434 case Type::VariableArray: { 435 const VariableArrayType *A = cast<VariableArrayType>(Ty); 436 assert(A->getIndexTypeCVRQualifiers() == 0 && 437 "FIXME: We only handle trivial array types so far!"); 438 // VLAs resolve to the innermost element type; this matches 439 // the return of alloca, and there isn't any obviously better choice. 440 ResultType = ConvertTypeForMem(A->getElementType()); 441 break; 442 } 443 case Type::IncompleteArray: { 444 const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty); 445 assert(A->getIndexTypeCVRQualifiers() == 0 && 446 "FIXME: We only handle trivial array types so far!"); 447 // int X[] -> [0 x int], unless the element type is not sized. If it is 448 // unsized (e.g. an incomplete struct) just use [0 x i8]. 449 ResultType = ConvertTypeForMem(A->getElementType()); 450 if (!ResultType->isSized()) { 451 SkippedLayout = true; 452 ResultType = llvm::Type::getInt8Ty(getLLVMContext()); 453 } 454 ResultType = llvm::ArrayType::get(ResultType, 0); 455 break; 456 } 457 case Type::ConstantArray: { 458 const ConstantArrayType *A = cast<ConstantArrayType>(Ty); 459 llvm::Type *EltTy = ConvertTypeForMem(A->getElementType()); 460 461 // Lower arrays of undefined struct type to arrays of i8 just to have a 462 // concrete type. 463 if (!EltTy->isSized()) { 464 SkippedLayout = true; 465 EltTy = llvm::Type::getInt8Ty(getLLVMContext()); 466 } 467 468 ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue()); 469 break; 470 } 471 case Type::ExtVector: 472 case Type::Vector: { 473 const VectorType *VT = cast<VectorType>(Ty); 474 ResultType = llvm::VectorType::get(ConvertType(VT->getElementType()), 475 VT->getNumElements()); 476 break; 477 } 478 case Type::FunctionNoProto: 479 case Type::FunctionProto: { 480 const FunctionType *FT = cast<FunctionType>(Ty); 481 // First, check whether we can build the full function type. If the 482 // function type depends on an incomplete type (e.g. a struct or enum), we 483 // cannot lower the function type. 484 if (!isFuncTypeConvertible(FT)) { 485 // This function's type depends on an incomplete tag type. 486 487 // Force conversion of all the relevant record types, to make sure 488 // we re-convert the FunctionType when appropriate. 489 if (const RecordType *RT = FT->getReturnType()->getAs<RecordType>()) 490 ConvertRecordDeclType(RT->getDecl()); 491 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) 492 for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++) 493 if (const RecordType *RT = FPT->getParamType(i)->getAs<RecordType>()) 494 ConvertRecordDeclType(RT->getDecl()); 495 496 // Return a placeholder type. 497 ResultType = llvm::StructType::get(getLLVMContext()); 498 499 SkippedLayout = true; 500 break; 501 } 502 503 // While we're converting the parameter types for a function, we don't want 504 // to recursively convert any pointed-to structs. Converting directly-used 505 // structs is ok though. 506 if (!RecordsBeingLaidOut.insert(Ty).second) { 507 ResultType = llvm::StructType::get(getLLVMContext()); 508 509 SkippedLayout = true; 510 break; 511 } 512 513 // The function type can be built; call the appropriate routines to 514 // build it. 515 const CGFunctionInfo *FI; 516 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) { 517 FI = &arrangeFreeFunctionType( 518 CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0))); 519 } else { 520 const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT); 521 FI = &arrangeFreeFunctionType( 522 CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0))); 523 } 524 525 // If there is something higher level prodding our CGFunctionInfo, then 526 // don't recurse into it again. 527 if (FunctionsBeingProcessed.count(FI)) { 528 529 ResultType = llvm::StructType::get(getLLVMContext()); 530 SkippedLayout = true; 531 } else { 532 533 // Otherwise, we're good to go, go ahead and convert it. 534 ResultType = GetFunctionType(*FI); 535 } 536 537 RecordsBeingLaidOut.erase(Ty); 538 539 if (SkippedLayout) 540 TypeCache.clear(); 541 542 if (RecordsBeingLaidOut.empty()) 543 while (!DeferredRecords.empty()) 544 ConvertRecordDeclType(DeferredRecords.pop_back_val()); 545 break; 546 } 547 548 case Type::ObjCObject: 549 ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType()); 550 break; 551 552 case Type::ObjCInterface: { 553 // Objective-C interfaces are always opaque (outside of the 554 // runtime, which can do whatever it likes); we never refine 555 // these. 556 llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)]; 557 if (!T) 558 T = llvm::StructType::create(getLLVMContext()); 559 ResultType = T; 560 break; 561 } 562 563 case Type::ObjCObjectPointer: { 564 // Protocol qualifications do not influence the LLVM type, we just return a 565 // pointer to the underlying interface type. We don't need to worry about 566 // recursive conversion. 567 llvm::Type *T = 568 ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType()); 569 ResultType = T->getPointerTo(); 570 break; 571 } 572 573 case Type::Enum: { 574 const EnumDecl *ED = cast<EnumType>(Ty)->getDecl(); 575 if (ED->isCompleteDefinition() || ED->isFixed()) 576 return ConvertType(ED->getIntegerType()); 577 // Return a placeholder 'i32' type. This can be changed later when the 578 // type is defined (see UpdateCompletedType), but is likely to be the 579 // "right" answer. 580 ResultType = llvm::Type::getInt32Ty(getLLVMContext()); 581 break; 582 } 583 584 case Type::BlockPointer: { 585 const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType(); 586 llvm::Type *PointeeType = ConvertTypeForMem(FTy); 587 unsigned AS = Context.getTargetAddressSpace(FTy); 588 ResultType = llvm::PointerType::get(PointeeType, AS); 589 break; 590 } 591 592 case Type::MemberPointer: { 593 if (!getCXXABI().isMemberPointerConvertible(cast<MemberPointerType>(Ty))) 594 return llvm::StructType::create(getLLVMContext()); 595 ResultType = 596 getCXXABI().ConvertMemberPointerType(cast<MemberPointerType>(Ty)); 597 break; 598 } 599 600 case Type::Atomic: { 601 QualType valueType = cast<AtomicType>(Ty)->getValueType(); 602 ResultType = ConvertTypeForMem(valueType); 603 604 // Pad out to the inflated size if necessary. 605 uint64_t valueSize = Context.getTypeSize(valueType); 606 uint64_t atomicSize = Context.getTypeSize(Ty); 607 if (valueSize != atomicSize) { 608 assert(valueSize < atomicSize); 609 llvm::Type *elts[] = { 610 ResultType, 611 llvm::ArrayType::get(CGM.Int8Ty, (atomicSize - valueSize) / 8) 612 }; 613 ResultType = llvm::StructType::get(getLLVMContext(), 614 llvm::makeArrayRef(elts)); 615 } 616 break; 617 } 618 } 619 620 assert(ResultType && "Didn't convert a type?"); 621 622 TypeCache[Ty] = ResultType; 623 return ResultType; 624 } 625 626 bool CodeGenModule::isPaddedAtomicType(QualType type) { 627 return isPaddedAtomicType(type->castAs<AtomicType>()); 628 } 629 630 bool CodeGenModule::isPaddedAtomicType(const AtomicType *type) { 631 return Context.getTypeSize(type) != Context.getTypeSize(type->getValueType()); 632 } 633 634 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union. 635 llvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) { 636 // TagDecl's are not necessarily unique, instead use the (clang) 637 // type connected to the decl. 638 const Type *Key = Context.getTagDeclType(RD).getTypePtr(); 639 640 llvm::StructType *&Entry = RecordDeclTypes[Key]; 641 642 // If we don't have a StructType at all yet, create the forward declaration. 643 if (!Entry) { 644 Entry = llvm::StructType::create(getLLVMContext()); 645 addRecordTypeName(RD, Entry, ""); 646 } 647 llvm::StructType *Ty = Entry; 648 649 // If this is still a forward declaration, or the LLVM type is already 650 // complete, there's nothing more to do. 651 RD = RD->getDefinition(); 652 if (!RD || !RD->isCompleteDefinition() || !Ty->isOpaque()) 653 return Ty; 654 655 // If converting this type would cause us to infinitely loop, don't do it! 656 if (!isSafeToConvert(RD, *this)) { 657 DeferredRecords.push_back(RD); 658 return Ty; 659 } 660 661 // Okay, this is a definition of a type. Compile the implementation now. 662 bool InsertResult = RecordsBeingLaidOut.insert(Key).second; 663 (void)InsertResult; 664 assert(InsertResult && "Recursively compiling a struct?"); 665 666 // Force conversion of non-virtual base classes recursively. 667 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { 668 for (const auto &I : CRD->bases()) { 669 if (I.isVirtual()) continue; 670 671 ConvertRecordDeclType(I.getType()->getAs<RecordType>()->getDecl()); 672 } 673 } 674 675 // Layout fields. 676 CGRecordLayout *Layout = ComputeRecordLayout(RD, Ty); 677 CGRecordLayouts[Key] = Layout; 678 679 // We're done laying out this struct. 680 bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult; 681 assert(EraseResult && "struct not in RecordsBeingLaidOut set?"); 682 683 // If this struct blocked a FunctionType conversion, then recompute whatever 684 // was derived from that. 685 // FIXME: This is hugely overconservative. 686 if (SkippedLayout) 687 TypeCache.clear(); 688 689 // If we're done converting the outer-most record, then convert any deferred 690 // structs as well. 691 if (RecordsBeingLaidOut.empty()) 692 while (!DeferredRecords.empty()) 693 ConvertRecordDeclType(DeferredRecords.pop_back_val()); 694 695 return Ty; 696 } 697 698 /// getCGRecordLayout - Return record layout info for the given record decl. 699 const CGRecordLayout & 700 CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) { 701 const Type *Key = Context.getTagDeclType(RD).getTypePtr(); 702 703 const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key); 704 if (!Layout) { 705 // Compute the type information. 706 ConvertRecordDeclType(RD); 707 708 // Now try again. 709 Layout = CGRecordLayouts.lookup(Key); 710 } 711 712 assert(Layout && "Unable to find record layout information for type"); 713 return *Layout; 714 } 715 716 bool CodeGenTypes::isZeroInitializable(QualType T) { 717 // No need to check for member pointers when not compiling C++. 718 if (!Context.getLangOpts().CPlusPlus) 719 return true; 720 721 if (const auto *AT = Context.getAsArrayType(T)) { 722 if (isa<IncompleteArrayType>(AT)) 723 return true; 724 if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) 725 if (Context.getConstantArrayElementCount(CAT) == 0) 726 return true; 727 T = Context.getBaseElementType(T); 728 } 729 730 // Records are non-zero-initializable if they contain any 731 // non-zero-initializable subobjects. 732 if (const RecordType *RT = T->getAs<RecordType>()) { 733 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 734 return isZeroInitializable(RD); 735 } 736 737 // We have to ask the ABI about member pointers. 738 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) 739 return getCXXABI().isZeroInitializable(MPT); 740 741 // Everything else is okay. 742 return true; 743 } 744 745 bool CodeGenTypes::isZeroInitializable(const RecordDecl *RD) { 746 return getCGRecordLayout(RD).isZeroInitializable(); 747 } 748