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