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