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