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