1 //===--- Type.cpp - Type representation and manipulation ------------------===// 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 file implements type-related functionality. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/AST/ASTContext.h" 15 #include "clang/AST/Attr.h" 16 #include "clang/AST/CharUnits.h" 17 #include "clang/AST/DeclCXX.h" 18 #include "clang/AST/DeclObjC.h" 19 #include "clang/AST/DeclTemplate.h" 20 #include "clang/AST/Expr.h" 21 #include "clang/AST/PrettyPrinter.h" 22 #include "clang/AST/Type.h" 23 #include "clang/AST/TypeVisitor.h" 24 #include "clang/Basic/Specifiers.h" 25 #include "clang/Basic/TargetInfo.h" 26 #include "llvm/ADT/APSInt.h" 27 #include "llvm/ADT/StringExtras.h" 28 #include "llvm/Support/raw_ostream.h" 29 #include <algorithm> 30 using namespace clang; 31 32 bool Qualifiers::isStrictSupersetOf(Qualifiers Other) const { 33 return (*this != Other) && 34 // CVR qualifiers superset 35 (((Mask & CVRMask) | (Other.Mask & CVRMask)) == (Mask & CVRMask)) && 36 // ObjC GC qualifiers superset 37 ((getObjCGCAttr() == Other.getObjCGCAttr()) || 38 (hasObjCGCAttr() && !Other.hasObjCGCAttr())) && 39 // Address space superset. 40 ((getAddressSpace() == Other.getAddressSpace()) || 41 (hasAddressSpace()&& !Other.hasAddressSpace())) && 42 // Lifetime qualifier superset. 43 ((getObjCLifetime() == Other.getObjCLifetime()) || 44 (hasObjCLifetime() && !Other.hasObjCLifetime())); 45 } 46 47 const IdentifierInfo* QualType::getBaseTypeIdentifier() const { 48 const Type* ty = getTypePtr(); 49 NamedDecl *ND = nullptr; 50 if (ty->isPointerType() || ty->isReferenceType()) 51 return ty->getPointeeType().getBaseTypeIdentifier(); 52 else if (ty->isRecordType()) 53 ND = ty->getAs<RecordType>()->getDecl(); 54 else if (ty->isEnumeralType()) 55 ND = ty->getAs<EnumType>()->getDecl(); 56 else if (ty->getTypeClass() == Type::Typedef) 57 ND = ty->getAs<TypedefType>()->getDecl(); 58 else if (ty->isArrayType()) 59 return ty->castAsArrayTypeUnsafe()-> 60 getElementType().getBaseTypeIdentifier(); 61 62 if (ND) 63 return ND->getIdentifier(); 64 return nullptr; 65 } 66 67 bool QualType::isConstant(QualType T, ASTContext &Ctx) { 68 if (T.isConstQualified()) 69 return true; 70 71 if (const ArrayType *AT = Ctx.getAsArrayType(T)) 72 return AT->getElementType().isConstant(Ctx); 73 74 return T.getAddressSpace() == LangAS::opencl_constant; 75 } 76 77 unsigned ConstantArrayType::getNumAddressingBits(ASTContext &Context, 78 QualType ElementType, 79 const llvm::APInt &NumElements) { 80 uint64_t ElementSize = Context.getTypeSizeInChars(ElementType).getQuantity(); 81 82 // Fast path the common cases so we can avoid the conservative computation 83 // below, which in common cases allocates "large" APSInt values, which are 84 // slow. 85 86 // If the element size is a power of 2, we can directly compute the additional 87 // number of addressing bits beyond those required for the element count. 88 if (llvm::isPowerOf2_64(ElementSize)) { 89 return NumElements.getActiveBits() + llvm::Log2_64(ElementSize); 90 } 91 92 // If both the element count and element size fit in 32-bits, we can do the 93 // computation directly in 64-bits. 94 if ((ElementSize >> 32) == 0 && NumElements.getBitWidth() <= 64 && 95 (NumElements.getZExtValue() >> 32) == 0) { 96 uint64_t TotalSize = NumElements.getZExtValue() * ElementSize; 97 return 64 - llvm::countLeadingZeros(TotalSize); 98 } 99 100 // Otherwise, use APSInt to handle arbitrary sized values. 101 llvm::APSInt SizeExtended(NumElements, true); 102 unsigned SizeTypeBits = Context.getTypeSize(Context.getSizeType()); 103 SizeExtended = SizeExtended.extend(std::max(SizeTypeBits, 104 SizeExtended.getBitWidth()) * 2); 105 106 llvm::APSInt TotalSize(llvm::APInt(SizeExtended.getBitWidth(), ElementSize)); 107 TotalSize *= SizeExtended; 108 109 return TotalSize.getActiveBits(); 110 } 111 112 unsigned ConstantArrayType::getMaxSizeBits(ASTContext &Context) { 113 unsigned Bits = Context.getTypeSize(Context.getSizeType()); 114 115 // Limit the number of bits in size_t so that maximal bit size fits 64 bit 116 // integer (see PR8256). We can do this as currently there is no hardware 117 // that supports full 64-bit virtual space. 118 if (Bits > 61) 119 Bits = 61; 120 121 return Bits; 122 } 123 124 DependentSizedArrayType::DependentSizedArrayType(const ASTContext &Context, 125 QualType et, QualType can, 126 Expr *e, ArraySizeModifier sm, 127 unsigned tq, 128 SourceRange brackets) 129 : ArrayType(DependentSizedArray, et, can, sm, tq, 130 (et->containsUnexpandedParameterPack() || 131 (e && e->containsUnexpandedParameterPack()))), 132 Context(Context), SizeExpr((Stmt*) e), Brackets(brackets) 133 { 134 } 135 136 void DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID, 137 const ASTContext &Context, 138 QualType ET, 139 ArraySizeModifier SizeMod, 140 unsigned TypeQuals, 141 Expr *E) { 142 ID.AddPointer(ET.getAsOpaquePtr()); 143 ID.AddInteger(SizeMod); 144 ID.AddInteger(TypeQuals); 145 E->Profile(ID, Context, true); 146 } 147 148 DependentSizedExtVectorType::DependentSizedExtVectorType(const 149 ASTContext &Context, 150 QualType ElementType, 151 QualType can, 152 Expr *SizeExpr, 153 SourceLocation loc) 154 : Type(DependentSizedExtVector, can, /*Dependent=*/true, 155 /*InstantiationDependent=*/true, 156 ElementType->isVariablyModifiedType(), 157 (ElementType->containsUnexpandedParameterPack() || 158 (SizeExpr && SizeExpr->containsUnexpandedParameterPack()))), 159 Context(Context), SizeExpr(SizeExpr), ElementType(ElementType), 160 loc(loc) 161 { 162 } 163 164 void 165 DependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID, 166 const ASTContext &Context, 167 QualType ElementType, Expr *SizeExpr) { 168 ID.AddPointer(ElementType.getAsOpaquePtr()); 169 SizeExpr->Profile(ID, Context, true); 170 } 171 172 VectorType::VectorType(QualType vecType, unsigned nElements, QualType canonType, 173 VectorKind vecKind) 174 : VectorType(Vector, vecType, nElements, canonType, vecKind) {} 175 176 VectorType::VectorType(TypeClass tc, QualType vecType, unsigned nElements, 177 QualType canonType, VectorKind vecKind) 178 : Type(tc, canonType, vecType->isDependentType(), 179 vecType->isInstantiationDependentType(), 180 vecType->isVariablyModifiedType(), 181 vecType->containsUnexpandedParameterPack()), 182 ElementType(vecType) 183 { 184 VectorTypeBits.VecKind = vecKind; 185 VectorTypeBits.NumElements = nElements; 186 } 187 188 /// getArrayElementTypeNoTypeQual - If this is an array type, return the 189 /// element type of the array, potentially with type qualifiers missing. 190 /// This method should never be used when type qualifiers are meaningful. 191 const Type *Type::getArrayElementTypeNoTypeQual() const { 192 // If this is directly an array type, return it. 193 if (const ArrayType *ATy = dyn_cast<ArrayType>(this)) 194 return ATy->getElementType().getTypePtr(); 195 196 // If the canonical form of this type isn't the right kind, reject it. 197 if (!isa<ArrayType>(CanonicalType)) 198 return nullptr; 199 200 // If this is a typedef for an array type, strip the typedef off without 201 // losing all typedef information. 202 return cast<ArrayType>(getUnqualifiedDesugaredType()) 203 ->getElementType().getTypePtr(); 204 } 205 206 /// getDesugaredType - Return the specified type with any "sugar" removed from 207 /// the type. This takes off typedefs, typeof's etc. If the outer level of 208 /// the type is already concrete, it returns it unmodified. This is similar 209 /// to getting the canonical type, but it doesn't remove *all* typedefs. For 210 /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is 211 /// concrete. 212 QualType QualType::getDesugaredType(QualType T, const ASTContext &Context) { 213 SplitQualType split = getSplitDesugaredType(T); 214 return Context.getQualifiedType(split.Ty, split.Quals); 215 } 216 217 QualType QualType::getSingleStepDesugaredTypeImpl(QualType type, 218 const ASTContext &Context) { 219 SplitQualType split = type.split(); 220 QualType desugar = split.Ty->getLocallyUnqualifiedSingleStepDesugaredType(); 221 return Context.getQualifiedType(desugar, split.Quals); 222 } 223 224 QualType Type::getLocallyUnqualifiedSingleStepDesugaredType() const { 225 switch (getTypeClass()) { 226 #define ABSTRACT_TYPE(Class, Parent) 227 #define TYPE(Class, Parent) \ 228 case Type::Class: { \ 229 const Class##Type *ty = cast<Class##Type>(this); \ 230 if (!ty->isSugared()) return QualType(ty, 0); \ 231 return ty->desugar(); \ 232 } 233 #include "clang/AST/TypeNodes.def" 234 } 235 llvm_unreachable("bad type kind!"); 236 } 237 238 SplitQualType QualType::getSplitDesugaredType(QualType T) { 239 QualifierCollector Qs; 240 241 QualType Cur = T; 242 while (true) { 243 const Type *CurTy = Qs.strip(Cur); 244 switch (CurTy->getTypeClass()) { 245 #define ABSTRACT_TYPE(Class, Parent) 246 #define TYPE(Class, Parent) \ 247 case Type::Class: { \ 248 const Class##Type *Ty = cast<Class##Type>(CurTy); \ 249 if (!Ty->isSugared()) \ 250 return SplitQualType(Ty, Qs); \ 251 Cur = Ty->desugar(); \ 252 break; \ 253 } 254 #include "clang/AST/TypeNodes.def" 255 } 256 } 257 } 258 259 SplitQualType QualType::getSplitUnqualifiedTypeImpl(QualType type) { 260 SplitQualType split = type.split(); 261 262 // All the qualifiers we've seen so far. 263 Qualifiers quals = split.Quals; 264 265 // The last type node we saw with any nodes inside it. 266 const Type *lastTypeWithQuals = split.Ty; 267 268 while (true) { 269 QualType next; 270 271 // Do a single-step desugar, aborting the loop if the type isn't 272 // sugared. 273 switch (split.Ty->getTypeClass()) { 274 #define ABSTRACT_TYPE(Class, Parent) 275 #define TYPE(Class, Parent) \ 276 case Type::Class: { \ 277 const Class##Type *ty = cast<Class##Type>(split.Ty); \ 278 if (!ty->isSugared()) goto done; \ 279 next = ty->desugar(); \ 280 break; \ 281 } 282 #include "clang/AST/TypeNodes.def" 283 } 284 285 // Otherwise, split the underlying type. If that yields qualifiers, 286 // update the information. 287 split = next.split(); 288 if (!split.Quals.empty()) { 289 lastTypeWithQuals = split.Ty; 290 quals.addConsistentQualifiers(split.Quals); 291 } 292 } 293 294 done: 295 return SplitQualType(lastTypeWithQuals, quals); 296 } 297 298 QualType QualType::IgnoreParens(QualType T) { 299 // FIXME: this seems inherently un-qualifiers-safe. 300 while (const ParenType *PT = T->getAs<ParenType>()) 301 T = PT->getInnerType(); 302 return T; 303 } 304 305 /// \brief This will check for a T (which should be a Type which can act as 306 /// sugar, such as a TypedefType) by removing any existing sugar until it 307 /// reaches a T or a non-sugared type. 308 template<typename T> static const T *getAsSugar(const Type *Cur) { 309 while (true) { 310 if (const T *Sugar = dyn_cast<T>(Cur)) 311 return Sugar; 312 switch (Cur->getTypeClass()) { 313 #define ABSTRACT_TYPE(Class, Parent) 314 #define TYPE(Class, Parent) \ 315 case Type::Class: { \ 316 const Class##Type *Ty = cast<Class##Type>(Cur); \ 317 if (!Ty->isSugared()) return 0; \ 318 Cur = Ty->desugar().getTypePtr(); \ 319 break; \ 320 } 321 #include "clang/AST/TypeNodes.def" 322 } 323 } 324 } 325 326 template <> const TypedefType *Type::getAs() const { 327 return getAsSugar<TypedefType>(this); 328 } 329 330 template <> const TemplateSpecializationType *Type::getAs() const { 331 return getAsSugar<TemplateSpecializationType>(this); 332 } 333 334 template <> const AttributedType *Type::getAs() const { 335 return getAsSugar<AttributedType>(this); 336 } 337 338 /// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic 339 /// sugar off the given type. This should produce an object of the 340 /// same dynamic type as the canonical type. 341 const Type *Type::getUnqualifiedDesugaredType() const { 342 const Type *Cur = this; 343 344 while (true) { 345 switch (Cur->getTypeClass()) { 346 #define ABSTRACT_TYPE(Class, Parent) 347 #define TYPE(Class, Parent) \ 348 case Class: { \ 349 const Class##Type *Ty = cast<Class##Type>(Cur); \ 350 if (!Ty->isSugared()) return Cur; \ 351 Cur = Ty->desugar().getTypePtr(); \ 352 break; \ 353 } 354 #include "clang/AST/TypeNodes.def" 355 } 356 } 357 } 358 bool Type::isClassType() const { 359 if (const RecordType *RT = getAs<RecordType>()) 360 return RT->getDecl()->isClass(); 361 return false; 362 } 363 bool Type::isStructureType() const { 364 if (const RecordType *RT = getAs<RecordType>()) 365 return RT->getDecl()->isStruct(); 366 return false; 367 } 368 bool Type::isObjCBoxableRecordType() const { 369 if (const RecordType *RT = getAs<RecordType>()) 370 return RT->getDecl()->hasAttr<ObjCBoxableAttr>(); 371 return false; 372 } 373 bool Type::isInterfaceType() const { 374 if (const RecordType *RT = getAs<RecordType>()) 375 return RT->getDecl()->isInterface(); 376 return false; 377 } 378 bool Type::isStructureOrClassType() const { 379 if (const RecordType *RT = getAs<RecordType>()) { 380 RecordDecl *RD = RT->getDecl(); 381 return RD->isStruct() || RD->isClass() || RD->isInterface(); 382 } 383 return false; 384 } 385 bool Type::isVoidPointerType() const { 386 if (const PointerType *PT = getAs<PointerType>()) 387 return PT->getPointeeType()->isVoidType(); 388 return false; 389 } 390 391 bool Type::isUnionType() const { 392 if (const RecordType *RT = getAs<RecordType>()) 393 return RT->getDecl()->isUnion(); 394 return false; 395 } 396 397 bool Type::isComplexType() const { 398 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) 399 return CT->getElementType()->isFloatingType(); 400 return false; 401 } 402 403 bool Type::isComplexIntegerType() const { 404 // Check for GCC complex integer extension. 405 return getAsComplexIntegerType(); 406 } 407 408 const ComplexType *Type::getAsComplexIntegerType() const { 409 if (const ComplexType *Complex = getAs<ComplexType>()) 410 if (Complex->getElementType()->isIntegerType()) 411 return Complex; 412 return nullptr; 413 } 414 415 QualType Type::getPointeeType() const { 416 if (const PointerType *PT = getAs<PointerType>()) 417 return PT->getPointeeType(); 418 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 419 return OPT->getPointeeType(); 420 if (const BlockPointerType *BPT = getAs<BlockPointerType>()) 421 return BPT->getPointeeType(); 422 if (const ReferenceType *RT = getAs<ReferenceType>()) 423 return RT->getPointeeType(); 424 if (const MemberPointerType *MPT = getAs<MemberPointerType>()) 425 return MPT->getPointeeType(); 426 if (const DecayedType *DT = getAs<DecayedType>()) 427 return DT->getPointeeType(); 428 return QualType(); 429 } 430 431 const RecordType *Type::getAsStructureType() const { 432 // If this is directly a structure type, return it. 433 if (const RecordType *RT = dyn_cast<RecordType>(this)) { 434 if (RT->getDecl()->isStruct()) 435 return RT; 436 } 437 438 // If the canonical form of this type isn't the right kind, reject it. 439 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { 440 if (!RT->getDecl()->isStruct()) 441 return nullptr; 442 443 // If this is a typedef for a structure type, strip the typedef off without 444 // losing all typedef information. 445 return cast<RecordType>(getUnqualifiedDesugaredType()); 446 } 447 return nullptr; 448 } 449 450 const RecordType *Type::getAsUnionType() const { 451 // If this is directly a union type, return it. 452 if (const RecordType *RT = dyn_cast<RecordType>(this)) { 453 if (RT->getDecl()->isUnion()) 454 return RT; 455 } 456 457 // If the canonical form of this type isn't the right kind, reject it. 458 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { 459 if (!RT->getDecl()->isUnion()) 460 return nullptr; 461 462 // If this is a typedef for a union type, strip the typedef off without 463 // losing all typedef information. 464 return cast<RecordType>(getUnqualifiedDesugaredType()); 465 } 466 467 return nullptr; 468 } 469 470 bool Type::isObjCIdOrObjectKindOfType(const ASTContext &ctx, 471 const ObjCObjectType *&bound) const { 472 bound = nullptr; 473 474 const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>(); 475 if (!OPT) 476 return false; 477 478 // Easy case: id. 479 if (OPT->isObjCIdType()) 480 return true; 481 482 // If it's not a __kindof type, reject it now. 483 if (!OPT->isKindOfType()) 484 return false; 485 486 // If it's Class or qualified Class, it's not an object type. 487 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) 488 return false; 489 490 // Figure out the type bound for the __kindof type. 491 bound = OPT->getObjectType()->stripObjCKindOfTypeAndQuals(ctx) 492 ->getAs<ObjCObjectType>(); 493 return true; 494 } 495 496 bool Type::isObjCClassOrClassKindOfType() const { 497 const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>(); 498 if (!OPT) 499 return false; 500 501 // Easy case: Class. 502 if (OPT->isObjCClassType()) 503 return true; 504 505 // If it's not a __kindof type, reject it now. 506 if (!OPT->isKindOfType()) 507 return false; 508 509 // If it's Class or qualified Class, it's a class __kindof type. 510 return OPT->isObjCClassType() || OPT->isObjCQualifiedClassType(); 511 } 512 513 ObjCObjectType::ObjCObjectType(QualType Canonical, QualType Base, 514 ArrayRef<QualType> typeArgs, 515 ArrayRef<ObjCProtocolDecl *> protocols, 516 bool isKindOf) 517 : Type(ObjCObject, Canonical, Base->isDependentType(), 518 Base->isInstantiationDependentType(), 519 Base->isVariablyModifiedType(), 520 Base->containsUnexpandedParameterPack()), 521 BaseType(Base) 522 { 523 ObjCObjectTypeBits.IsKindOf = isKindOf; 524 525 ObjCObjectTypeBits.NumTypeArgs = typeArgs.size(); 526 assert(getTypeArgsAsWritten().size() == typeArgs.size() && 527 "bitfield overflow in type argument count"); 528 ObjCObjectTypeBits.NumProtocols = protocols.size(); 529 assert(getNumProtocols() == protocols.size() && 530 "bitfield overflow in protocol count"); 531 if (!typeArgs.empty()) 532 memcpy(getTypeArgStorage(), typeArgs.data(), 533 typeArgs.size() * sizeof(QualType)); 534 if (!protocols.empty()) 535 memcpy(getProtocolStorage(), protocols.data(), 536 protocols.size() * sizeof(ObjCProtocolDecl*)); 537 538 for (auto typeArg : typeArgs) { 539 if (typeArg->isDependentType()) 540 setDependent(); 541 else if (typeArg->isInstantiationDependentType()) 542 setInstantiationDependent(); 543 544 if (typeArg->containsUnexpandedParameterPack()) 545 setContainsUnexpandedParameterPack(); 546 } 547 } 548 549 bool ObjCObjectType::isSpecialized() const { 550 // If we have type arguments written here, the type is specialized. 551 if (ObjCObjectTypeBits.NumTypeArgs > 0) 552 return true; 553 554 // Otherwise, check whether the base type is specialized. 555 if (auto objcObject = getBaseType()->getAs<ObjCObjectType>()) { 556 // Terminate when we reach an interface type. 557 if (isa<ObjCInterfaceType>(objcObject)) 558 return false; 559 560 return objcObject->isSpecialized(); 561 } 562 563 // Not specialized. 564 return false; 565 } 566 567 ArrayRef<QualType> ObjCObjectType::getTypeArgs() const { 568 // We have type arguments written on this type. 569 if (isSpecializedAsWritten()) 570 return getTypeArgsAsWritten(); 571 572 // Look at the base type, which might have type arguments. 573 if (auto objcObject = getBaseType()->getAs<ObjCObjectType>()) { 574 // Terminate when we reach an interface type. 575 if (isa<ObjCInterfaceType>(objcObject)) 576 return { }; 577 578 return objcObject->getTypeArgs(); 579 } 580 581 // No type arguments. 582 return { }; 583 } 584 585 bool ObjCObjectType::isKindOfType() const { 586 if (isKindOfTypeAsWritten()) 587 return true; 588 589 // Look at the base type, which might have type arguments. 590 if (auto objcObject = getBaseType()->getAs<ObjCObjectType>()) { 591 // Terminate when we reach an interface type. 592 if (isa<ObjCInterfaceType>(objcObject)) 593 return false; 594 595 return objcObject->isKindOfType(); 596 } 597 598 // Not a "__kindof" type. 599 return false; 600 } 601 602 QualType ObjCObjectType::stripObjCKindOfTypeAndQuals( 603 const ASTContext &ctx) const { 604 if (!isKindOfType() && qual_empty()) 605 return QualType(this, 0); 606 607 // Recursively strip __kindof. 608 SplitQualType splitBaseType = getBaseType().split(); 609 QualType baseType(splitBaseType.Ty, 0); 610 if (const ObjCObjectType *baseObj 611 = splitBaseType.Ty->getAs<ObjCObjectType>()) { 612 baseType = baseObj->stripObjCKindOfTypeAndQuals(ctx); 613 } 614 615 return ctx.getObjCObjectType(ctx.getQualifiedType(baseType, 616 splitBaseType.Quals), 617 getTypeArgsAsWritten(), 618 /*protocols=*/{ }, 619 /*isKindOf=*/false); 620 } 621 622 const ObjCObjectPointerType *ObjCObjectPointerType::stripObjCKindOfTypeAndQuals( 623 const ASTContext &ctx) const { 624 if (!isKindOfType() && qual_empty()) 625 return this; 626 627 QualType obj = getObjectType()->stripObjCKindOfTypeAndQuals(ctx); 628 return ctx.getObjCObjectPointerType(obj)->castAs<ObjCObjectPointerType>(); 629 } 630 631 namespace { 632 633 template<typename F> 634 QualType simpleTransform(ASTContext &ctx, QualType type, F &&f); 635 636 /// Visitor used by simpleTransform() to perform the transformation. 637 template<typename F> 638 struct SimpleTransformVisitor 639 : public TypeVisitor<SimpleTransformVisitor<F>, QualType> { 640 ASTContext &Ctx; 641 F &&TheFunc; 642 643 QualType recurse(QualType type) { 644 return simpleTransform(Ctx, type, std::move(TheFunc)); 645 } 646 647 public: 648 SimpleTransformVisitor(ASTContext &ctx, F &&f) : Ctx(ctx), TheFunc(std::move(f)) { } 649 650 // None of the clients of this transformation can occur where 651 // there are dependent types, so skip dependent types. 652 #define TYPE(Class, Base) 653 #define DEPENDENT_TYPE(Class, Base) \ 654 QualType Visit##Class##Type(const Class##Type *T) { return QualType(T, 0); } 655 #include "clang/AST/TypeNodes.def" 656 657 #define TRIVIAL_TYPE_CLASS(Class) \ 658 QualType Visit##Class##Type(const Class##Type *T) { return QualType(T, 0); } 659 660 TRIVIAL_TYPE_CLASS(Builtin) 661 662 QualType VisitComplexType(const ComplexType *T) { 663 QualType elementType = recurse(T->getElementType()); 664 if (elementType.isNull()) 665 return QualType(); 666 667 if (elementType.getAsOpaquePtr() == T->getElementType().getAsOpaquePtr()) 668 return QualType(T, 0); 669 670 return Ctx.getComplexType(elementType); 671 } 672 673 QualType VisitPointerType(const PointerType *T) { 674 QualType pointeeType = recurse(T->getPointeeType()); 675 if (pointeeType.isNull()) 676 return QualType(); 677 678 if (pointeeType.getAsOpaquePtr() == T->getPointeeType().getAsOpaquePtr()) 679 return QualType(T, 0); 680 681 return Ctx.getPointerType(pointeeType); 682 } 683 684 QualType VisitBlockPointerType(const BlockPointerType *T) { 685 QualType pointeeType = recurse(T->getPointeeType()); 686 if (pointeeType.isNull()) 687 return QualType(); 688 689 if (pointeeType.getAsOpaquePtr() == T->getPointeeType().getAsOpaquePtr()) 690 return QualType(T, 0); 691 692 return Ctx.getBlockPointerType(pointeeType); 693 } 694 695 QualType VisitLValueReferenceType(const LValueReferenceType *T) { 696 QualType pointeeType = recurse(T->getPointeeTypeAsWritten()); 697 if (pointeeType.isNull()) 698 return QualType(); 699 700 if (pointeeType.getAsOpaquePtr() 701 == T->getPointeeTypeAsWritten().getAsOpaquePtr()) 702 return QualType(T, 0); 703 704 return Ctx.getLValueReferenceType(pointeeType, T->isSpelledAsLValue()); 705 } 706 707 QualType VisitRValueReferenceType(const RValueReferenceType *T) { 708 QualType pointeeType = recurse(T->getPointeeTypeAsWritten()); 709 if (pointeeType.isNull()) 710 return QualType(); 711 712 if (pointeeType.getAsOpaquePtr() 713 == T->getPointeeTypeAsWritten().getAsOpaquePtr()) 714 return QualType(T, 0); 715 716 return Ctx.getRValueReferenceType(pointeeType); 717 } 718 719 QualType VisitMemberPointerType(const MemberPointerType *T) { 720 QualType pointeeType = recurse(T->getPointeeType()); 721 if (pointeeType.isNull()) 722 return QualType(); 723 724 if (pointeeType.getAsOpaquePtr() == T->getPointeeType().getAsOpaquePtr()) 725 return QualType(T, 0); 726 727 return Ctx.getMemberPointerType(pointeeType, T->getClass()); 728 } 729 730 QualType VisitConstantArrayType(const ConstantArrayType *T) { 731 QualType elementType = recurse(T->getElementType()); 732 if (elementType.isNull()) 733 return QualType(); 734 735 if (elementType.getAsOpaquePtr() == T->getElementType().getAsOpaquePtr()) 736 return QualType(T, 0); 737 738 return Ctx.getConstantArrayType(elementType, T->getSize(), 739 T->getSizeModifier(), 740 T->getIndexTypeCVRQualifiers()); 741 } 742 743 QualType VisitVariableArrayType(const VariableArrayType *T) { 744 QualType elementType = recurse(T->getElementType()); 745 if (elementType.isNull()) 746 return QualType(); 747 748 if (elementType.getAsOpaquePtr() == T->getElementType().getAsOpaquePtr()) 749 return QualType(T, 0); 750 751 return Ctx.getVariableArrayType(elementType, T->getSizeExpr(), 752 T->getSizeModifier(), 753 T->getIndexTypeCVRQualifiers(), 754 T->getBracketsRange()); 755 } 756 757 QualType VisitIncompleteArrayType(const IncompleteArrayType *T) { 758 QualType elementType = recurse(T->getElementType()); 759 if (elementType.isNull()) 760 return QualType(); 761 762 if (elementType.getAsOpaquePtr() == T->getElementType().getAsOpaquePtr()) 763 return QualType(T, 0); 764 765 return Ctx.getIncompleteArrayType(elementType, T->getSizeModifier(), 766 T->getIndexTypeCVRQualifiers()); 767 } 768 769 QualType VisitVectorType(const VectorType *T) { 770 QualType elementType = recurse(T->getElementType()); 771 if (elementType.isNull()) 772 return QualType(); 773 774 if (elementType.getAsOpaquePtr() == T->getElementType().getAsOpaquePtr()) 775 return QualType(T, 0); 776 777 return Ctx.getVectorType(elementType, T->getNumElements(), 778 T->getVectorKind()); 779 } 780 781 QualType VisitExtVectorType(const ExtVectorType *T) { 782 QualType elementType = recurse(T->getElementType()); 783 if (elementType.isNull()) 784 return QualType(); 785 786 if (elementType.getAsOpaquePtr() == T->getElementType().getAsOpaquePtr()) 787 return QualType(T, 0); 788 789 return Ctx.getExtVectorType(elementType, T->getNumElements()); 790 } 791 792 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T) { 793 QualType returnType = recurse(T->getReturnType()); 794 if (returnType.isNull()) 795 return QualType(); 796 797 if (returnType.getAsOpaquePtr() == T->getReturnType().getAsOpaquePtr()) 798 return QualType(T, 0); 799 800 return Ctx.getFunctionNoProtoType(returnType, T->getExtInfo()); 801 } 802 803 QualType VisitFunctionProtoType(const FunctionProtoType *T) { 804 QualType returnType = recurse(T->getReturnType()); 805 if (returnType.isNull()) 806 return QualType(); 807 808 // Transform parameter types. 809 SmallVector<QualType, 4> paramTypes; 810 bool paramChanged = false; 811 for (auto paramType : T->getParamTypes()) { 812 QualType newParamType = recurse(paramType); 813 if (newParamType.isNull()) 814 return QualType(); 815 816 if (newParamType.getAsOpaquePtr() != paramType.getAsOpaquePtr()) 817 paramChanged = true; 818 819 paramTypes.push_back(newParamType); 820 } 821 822 // Transform extended info. 823 FunctionProtoType::ExtProtoInfo info = T->getExtProtoInfo(); 824 bool exceptionChanged = false; 825 if (info.ExceptionSpec.Type == EST_Dynamic) { 826 SmallVector<QualType, 4> exceptionTypes; 827 for (auto exceptionType : info.ExceptionSpec.Exceptions) { 828 QualType newExceptionType = recurse(exceptionType); 829 if (newExceptionType.isNull()) 830 return QualType(); 831 832 if (newExceptionType.getAsOpaquePtr() 833 != exceptionType.getAsOpaquePtr()) 834 exceptionChanged = true; 835 836 exceptionTypes.push_back(newExceptionType); 837 } 838 839 if (exceptionChanged) { 840 info.ExceptionSpec.Exceptions = 841 llvm::makeArrayRef(exceptionTypes).copy(Ctx); 842 } 843 } 844 845 if (returnType.getAsOpaquePtr() == T->getReturnType().getAsOpaquePtr() && 846 !paramChanged && !exceptionChanged) 847 return QualType(T, 0); 848 849 return Ctx.getFunctionType(returnType, paramTypes, info); 850 } 851 852 QualType VisitParenType(const ParenType *T) { 853 QualType innerType = recurse(T->getInnerType()); 854 if (innerType.isNull()) 855 return QualType(); 856 857 if (innerType.getAsOpaquePtr() == T->getInnerType().getAsOpaquePtr()) 858 return QualType(T, 0); 859 860 return Ctx.getParenType(innerType); 861 } 862 863 TRIVIAL_TYPE_CLASS(Typedef) 864 865 QualType VisitAdjustedType(const AdjustedType *T) { 866 QualType originalType = recurse(T->getOriginalType()); 867 if (originalType.isNull()) 868 return QualType(); 869 870 QualType adjustedType = recurse(T->getAdjustedType()); 871 if (adjustedType.isNull()) 872 return QualType(); 873 874 if (originalType.getAsOpaquePtr() 875 == T->getOriginalType().getAsOpaquePtr() && 876 adjustedType.getAsOpaquePtr() == T->getAdjustedType().getAsOpaquePtr()) 877 return QualType(T, 0); 878 879 return Ctx.getAdjustedType(originalType, adjustedType); 880 } 881 882 QualType VisitDecayedType(const DecayedType *T) { 883 QualType originalType = recurse(T->getOriginalType()); 884 if (originalType.isNull()) 885 return QualType(); 886 887 if (originalType.getAsOpaquePtr() 888 == T->getOriginalType().getAsOpaquePtr()) 889 return QualType(T, 0); 890 891 return Ctx.getDecayedType(originalType); 892 } 893 894 TRIVIAL_TYPE_CLASS(TypeOfExpr) 895 TRIVIAL_TYPE_CLASS(TypeOf) 896 TRIVIAL_TYPE_CLASS(Decltype) 897 TRIVIAL_TYPE_CLASS(UnaryTransform) 898 TRIVIAL_TYPE_CLASS(Record) 899 TRIVIAL_TYPE_CLASS(Enum) 900 901 // FIXME: Non-trivial to implement, but important for C++ 902 TRIVIAL_TYPE_CLASS(Elaborated) 903 904 QualType VisitAttributedType(const AttributedType *T) { 905 QualType modifiedType = recurse(T->getModifiedType()); 906 if (modifiedType.isNull()) 907 return QualType(); 908 909 QualType equivalentType = recurse(T->getEquivalentType()); 910 if (equivalentType.isNull()) 911 return QualType(); 912 913 if (modifiedType.getAsOpaquePtr() 914 == T->getModifiedType().getAsOpaquePtr() && 915 equivalentType.getAsOpaquePtr() 916 == T->getEquivalentType().getAsOpaquePtr()) 917 return QualType(T, 0); 918 919 return Ctx.getAttributedType(T->getAttrKind(), modifiedType, 920 equivalentType); 921 } 922 923 QualType VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { 924 QualType replacementType = recurse(T->getReplacementType()); 925 if (replacementType.isNull()) 926 return QualType(); 927 928 if (replacementType.getAsOpaquePtr() 929 == T->getReplacementType().getAsOpaquePtr()) 930 return QualType(T, 0); 931 932 return Ctx.getSubstTemplateTypeParmType(T->getReplacedParameter(), 933 replacementType); 934 } 935 936 // FIXME: Non-trivial to implement, but important for C++ 937 TRIVIAL_TYPE_CLASS(TemplateSpecialization) 938 939 QualType VisitAutoType(const AutoType *T) { 940 if (!T->isDeduced()) 941 return QualType(T, 0); 942 943 QualType deducedType = recurse(T->getDeducedType()); 944 if (deducedType.isNull()) 945 return QualType(); 946 947 if (deducedType.getAsOpaquePtr() 948 == T->getDeducedType().getAsOpaquePtr()) 949 return QualType(T, 0); 950 951 return Ctx.getAutoType(deducedType, T->isDecltypeAuto(), 952 T->isDependentType()); 953 } 954 955 // FIXME: Non-trivial to implement, but important for C++ 956 TRIVIAL_TYPE_CLASS(PackExpansion) 957 958 QualType VisitObjCObjectType(const ObjCObjectType *T) { 959 QualType baseType = recurse(T->getBaseType()); 960 if (baseType.isNull()) 961 return QualType(); 962 963 // Transform type arguments. 964 bool typeArgChanged = false; 965 SmallVector<QualType, 4> typeArgs; 966 for (auto typeArg : T->getTypeArgsAsWritten()) { 967 QualType newTypeArg = recurse(typeArg); 968 if (newTypeArg.isNull()) 969 return QualType(); 970 971 if (newTypeArg.getAsOpaquePtr() != typeArg.getAsOpaquePtr()) 972 typeArgChanged = true; 973 974 typeArgs.push_back(newTypeArg); 975 } 976 977 if (baseType.getAsOpaquePtr() == T->getBaseType().getAsOpaquePtr() && 978 !typeArgChanged) 979 return QualType(T, 0); 980 981 return Ctx.getObjCObjectType(baseType, typeArgs, 982 llvm::makeArrayRef(T->qual_begin(), 983 T->getNumProtocols()), 984 T->isKindOfTypeAsWritten()); 985 } 986 987 TRIVIAL_TYPE_CLASS(ObjCInterface) 988 989 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { 990 QualType pointeeType = recurse(T->getPointeeType()); 991 if (pointeeType.isNull()) 992 return QualType(); 993 994 if (pointeeType.getAsOpaquePtr() 995 == T->getPointeeType().getAsOpaquePtr()) 996 return QualType(T, 0); 997 998 return Ctx.getObjCObjectPointerType(pointeeType); 999 } 1000 1001 QualType VisitAtomicType(const AtomicType *T) { 1002 QualType valueType = recurse(T->getValueType()); 1003 if (valueType.isNull()) 1004 return QualType(); 1005 1006 if (valueType.getAsOpaquePtr() 1007 == T->getValueType().getAsOpaquePtr()) 1008 return QualType(T, 0); 1009 1010 return Ctx.getAtomicType(valueType); 1011 } 1012 1013 #undef TRIVIAL_TYPE_CLASS 1014 }; 1015 1016 /// Perform a simple type transformation that does not change the 1017 /// semantics of the type. 1018 template<typename F> 1019 QualType simpleTransform(ASTContext &ctx, QualType type, F &&f) { 1020 // Transform the type. If it changed, return the transformed result. 1021 QualType transformed = f(type); 1022 if (transformed.getAsOpaquePtr() != type.getAsOpaquePtr()) 1023 return transformed; 1024 1025 // Split out the qualifiers from the type. 1026 SplitQualType splitType = type.split(); 1027 1028 // Visit the type itself. 1029 SimpleTransformVisitor<F> visitor(ctx, std::move(f)); 1030 QualType result = visitor.Visit(splitType.Ty); 1031 if (result.isNull()) 1032 return result; 1033 1034 // Reconstruct the transformed type by applying the local qualifiers 1035 // from the split type. 1036 return ctx.getQualifiedType(result, splitType.Quals); 1037 } 1038 1039 } // end anonymous namespace 1040 1041 /// Substitute the given type arguments for Objective-C type 1042 /// parameters within the given type, recursively. 1043 QualType QualType::substObjCTypeArgs( 1044 ASTContext &ctx, 1045 ArrayRef<QualType> typeArgs, 1046 ObjCSubstitutionContext context) const { 1047 return simpleTransform(ctx, *this, 1048 [&](QualType type) -> QualType { 1049 SplitQualType splitType = type.split(); 1050 1051 // Replace an Objective-C type parameter reference with the corresponding 1052 // type argument. 1053 if (const auto *typedefTy = dyn_cast<TypedefType>(splitType.Ty)) { 1054 if (auto *typeParam = dyn_cast<ObjCTypeParamDecl>(typedefTy->getDecl())) { 1055 // If we have type arguments, use them. 1056 if (!typeArgs.empty()) { 1057 // FIXME: Introduce SubstObjCTypeParamType ? 1058 QualType argType = typeArgs[typeParam->getIndex()]; 1059 return ctx.getQualifiedType(argType, splitType.Quals); 1060 } 1061 1062 switch (context) { 1063 case ObjCSubstitutionContext::Ordinary: 1064 case ObjCSubstitutionContext::Parameter: 1065 case ObjCSubstitutionContext::Superclass: 1066 // Substitute the bound. 1067 return ctx.getQualifiedType(typeParam->getUnderlyingType(), 1068 splitType.Quals); 1069 1070 case ObjCSubstitutionContext::Result: 1071 case ObjCSubstitutionContext::Property: { 1072 // Substitute the __kindof form of the underlying type. 1073 const auto *objPtr = typeParam->getUnderlyingType() 1074 ->castAs<ObjCObjectPointerType>(); 1075 1076 // __kindof types, id, and Class don't need an additional 1077 // __kindof. 1078 if (objPtr->isKindOfType() || objPtr->isObjCIdOrClassType()) 1079 return ctx.getQualifiedType(typeParam->getUnderlyingType(), 1080 splitType.Quals); 1081 1082 // Add __kindof. 1083 const auto *obj = objPtr->getObjectType(); 1084 QualType resultTy = ctx.getObjCObjectType(obj->getBaseType(), 1085 obj->getTypeArgsAsWritten(), 1086 obj->getProtocols(), 1087 /*isKindOf=*/true); 1088 1089 // Rebuild object pointer type. 1090 resultTy = ctx.getObjCObjectPointerType(resultTy); 1091 return ctx.getQualifiedType(resultTy, splitType.Quals); 1092 } 1093 } 1094 } 1095 } 1096 1097 // If we have a function type, update the context appropriately. 1098 if (const auto *funcType = dyn_cast<FunctionType>(splitType.Ty)) { 1099 // Substitute result type. 1100 QualType returnType = funcType->getReturnType().substObjCTypeArgs( 1101 ctx, 1102 typeArgs, 1103 ObjCSubstitutionContext::Result); 1104 if (returnType.isNull()) 1105 return QualType(); 1106 1107 // Handle non-prototyped functions, which only substitute into the result 1108 // type. 1109 if (isa<FunctionNoProtoType>(funcType)) { 1110 // If the return type was unchanged, do nothing. 1111 if (returnType.getAsOpaquePtr() 1112 == funcType->getReturnType().getAsOpaquePtr()) 1113 return type; 1114 1115 // Otherwise, build a new type. 1116 return ctx.getFunctionNoProtoType(returnType, funcType->getExtInfo()); 1117 } 1118 1119 const auto *funcProtoType = cast<FunctionProtoType>(funcType); 1120 1121 // Transform parameter types. 1122 SmallVector<QualType, 4> paramTypes; 1123 bool paramChanged = false; 1124 for (auto paramType : funcProtoType->getParamTypes()) { 1125 QualType newParamType = paramType.substObjCTypeArgs( 1126 ctx, 1127 typeArgs, 1128 ObjCSubstitutionContext::Parameter); 1129 if (newParamType.isNull()) 1130 return QualType(); 1131 1132 if (newParamType.getAsOpaquePtr() != paramType.getAsOpaquePtr()) 1133 paramChanged = true; 1134 1135 paramTypes.push_back(newParamType); 1136 } 1137 1138 // Transform extended info. 1139 FunctionProtoType::ExtProtoInfo info = funcProtoType->getExtProtoInfo(); 1140 bool exceptionChanged = false; 1141 if (info.ExceptionSpec.Type == EST_Dynamic) { 1142 SmallVector<QualType, 4> exceptionTypes; 1143 for (auto exceptionType : info.ExceptionSpec.Exceptions) { 1144 QualType newExceptionType = exceptionType.substObjCTypeArgs( 1145 ctx, 1146 typeArgs, 1147 ObjCSubstitutionContext::Ordinary); 1148 if (newExceptionType.isNull()) 1149 return QualType(); 1150 1151 if (newExceptionType.getAsOpaquePtr() 1152 != exceptionType.getAsOpaquePtr()) 1153 exceptionChanged = true; 1154 1155 exceptionTypes.push_back(newExceptionType); 1156 } 1157 1158 if (exceptionChanged) { 1159 info.ExceptionSpec.Exceptions = 1160 llvm::makeArrayRef(exceptionTypes).copy(ctx); 1161 } 1162 } 1163 1164 if (returnType.getAsOpaquePtr() 1165 == funcProtoType->getReturnType().getAsOpaquePtr() && 1166 !paramChanged && !exceptionChanged) 1167 return type; 1168 1169 return ctx.getFunctionType(returnType, paramTypes, info); 1170 } 1171 1172 // Substitute into the type arguments of a specialized Objective-C object 1173 // type. 1174 if (const auto *objcObjectType = dyn_cast<ObjCObjectType>(splitType.Ty)) { 1175 if (objcObjectType->isSpecializedAsWritten()) { 1176 SmallVector<QualType, 4> newTypeArgs; 1177 bool anyChanged = false; 1178 for (auto typeArg : objcObjectType->getTypeArgsAsWritten()) { 1179 QualType newTypeArg = typeArg.substObjCTypeArgs( 1180 ctx, typeArgs, 1181 ObjCSubstitutionContext::Ordinary); 1182 if (newTypeArg.isNull()) 1183 return QualType(); 1184 1185 if (newTypeArg.getAsOpaquePtr() != typeArg.getAsOpaquePtr()) { 1186 // If we're substituting based on an unspecialized context type, 1187 // produce an unspecialized type. 1188 ArrayRef<ObjCProtocolDecl *> protocols( 1189 objcObjectType->qual_begin(), 1190 objcObjectType->getNumProtocols()); 1191 if (typeArgs.empty() && 1192 context != ObjCSubstitutionContext::Superclass) { 1193 return ctx.getObjCObjectType( 1194 objcObjectType->getBaseType(), { }, 1195 protocols, 1196 objcObjectType->isKindOfTypeAsWritten()); 1197 } 1198 1199 anyChanged = true; 1200 } 1201 1202 newTypeArgs.push_back(newTypeArg); 1203 } 1204 1205 if (anyChanged) { 1206 ArrayRef<ObjCProtocolDecl *> protocols( 1207 objcObjectType->qual_begin(), 1208 objcObjectType->getNumProtocols()); 1209 return ctx.getObjCObjectType(objcObjectType->getBaseType(), 1210 newTypeArgs, protocols, 1211 objcObjectType->isKindOfTypeAsWritten()); 1212 } 1213 } 1214 1215 return type; 1216 } 1217 1218 return type; 1219 }); 1220 } 1221 1222 QualType QualType::substObjCMemberType(QualType objectType, 1223 const DeclContext *dc, 1224 ObjCSubstitutionContext context) const { 1225 if (auto subs = objectType->getObjCSubstitutions(dc)) 1226 return substObjCTypeArgs(dc->getParentASTContext(), *subs, context); 1227 1228 return *this; 1229 } 1230 1231 QualType QualType::stripObjCKindOfType(const ASTContext &constCtx) const { 1232 // FIXME: Because ASTContext::getAttributedType() is non-const. 1233 auto &ctx = const_cast<ASTContext &>(constCtx); 1234 return simpleTransform(ctx, *this, 1235 [&](QualType type) -> QualType { 1236 SplitQualType splitType = type.split(); 1237 if (auto *objType = splitType.Ty->getAs<ObjCObjectType>()) { 1238 if (!objType->isKindOfType()) 1239 return type; 1240 1241 QualType baseType 1242 = objType->getBaseType().stripObjCKindOfType(ctx); 1243 return ctx.getQualifiedType( 1244 ctx.getObjCObjectType(baseType, 1245 objType->getTypeArgsAsWritten(), 1246 objType->getProtocols(), 1247 /*isKindOf=*/false), 1248 splitType.Quals); 1249 } 1250 1251 return type; 1252 }); 1253 } 1254 1255 Optional<ArrayRef<QualType>> Type::getObjCSubstitutions( 1256 const DeclContext *dc) const { 1257 // Look through method scopes. 1258 if (auto method = dyn_cast<ObjCMethodDecl>(dc)) 1259 dc = method->getDeclContext(); 1260 1261 // Find the class or category in which the type we're substituting 1262 // was declared. 1263 const ObjCInterfaceDecl *dcClassDecl = dyn_cast<ObjCInterfaceDecl>(dc); 1264 const ObjCCategoryDecl *dcCategoryDecl = nullptr; 1265 ObjCTypeParamList *dcTypeParams = nullptr; 1266 if (dcClassDecl) { 1267 // If the class does not have any type parameters, there's no 1268 // substitution to do. 1269 dcTypeParams = dcClassDecl->getTypeParamList(); 1270 if (!dcTypeParams) 1271 return None; 1272 } else { 1273 // If we are in neither a class nor a category, there's no 1274 // substitution to perform. 1275 dcCategoryDecl = dyn_cast<ObjCCategoryDecl>(dc); 1276 if (!dcCategoryDecl) 1277 return None; 1278 1279 // If the category does not have any type parameters, there's no 1280 // substitution to do. 1281 dcTypeParams = dcCategoryDecl->getTypeParamList(); 1282 if (!dcTypeParams) 1283 return None; 1284 1285 dcClassDecl = dcCategoryDecl->getClassInterface(); 1286 if (!dcClassDecl) 1287 return None; 1288 } 1289 assert(dcTypeParams && "No substitutions to perform"); 1290 assert(dcClassDecl && "No class context"); 1291 1292 // Find the underlying object type. 1293 const ObjCObjectType *objectType; 1294 if (const auto *objectPointerType = getAs<ObjCObjectPointerType>()) { 1295 objectType = objectPointerType->getObjectType(); 1296 } else if (getAs<BlockPointerType>()) { 1297 ASTContext &ctx = dc->getParentASTContext(); 1298 objectType = ctx.getObjCObjectType(ctx.ObjCBuiltinIdTy, { }, { }) 1299 ->castAs<ObjCObjectType>();; 1300 } else { 1301 objectType = getAs<ObjCObjectType>(); 1302 } 1303 1304 /// Extract the class from the receiver object type. 1305 ObjCInterfaceDecl *curClassDecl = objectType ? objectType->getInterface() 1306 : nullptr; 1307 if (!curClassDecl) { 1308 // If we don't have a context type (e.g., this is "id" or some 1309 // variant thereof), substitute the bounds. 1310 return llvm::ArrayRef<QualType>(); 1311 } 1312 1313 // Follow the superclass chain until we've mapped the receiver type 1314 // to the same class as the context. 1315 while (curClassDecl != dcClassDecl) { 1316 // Map to the superclass type. 1317 QualType superType = objectType->getSuperClassType(); 1318 if (superType.isNull()) { 1319 objectType = nullptr; 1320 break; 1321 } 1322 1323 objectType = superType->castAs<ObjCObjectType>(); 1324 curClassDecl = objectType->getInterface(); 1325 } 1326 1327 // If we don't have a receiver type, or the receiver type does not 1328 // have type arguments, substitute in the defaults. 1329 if (!objectType || objectType->isUnspecialized()) { 1330 return llvm::ArrayRef<QualType>(); 1331 } 1332 1333 // The receiver type has the type arguments we want. 1334 return objectType->getTypeArgs(); 1335 } 1336 1337 bool Type::acceptsObjCTypeParams() const { 1338 if (auto *IfaceT = getAsObjCInterfaceType()) { 1339 if (auto *ID = IfaceT->getInterface()) { 1340 if (ID->getTypeParamList()) 1341 return true; 1342 } 1343 } 1344 1345 return false; 1346 } 1347 1348 void ObjCObjectType::computeSuperClassTypeSlow() const { 1349 // Retrieve the class declaration for this type. If there isn't one 1350 // (e.g., this is some variant of "id" or "Class"), then there is no 1351 // superclass type. 1352 ObjCInterfaceDecl *classDecl = getInterface(); 1353 if (!classDecl) { 1354 CachedSuperClassType.setInt(true); 1355 return; 1356 } 1357 1358 // Extract the superclass type. 1359 const ObjCObjectType *superClassObjTy = classDecl->getSuperClassType(); 1360 if (!superClassObjTy) { 1361 CachedSuperClassType.setInt(true); 1362 return; 1363 } 1364 1365 ObjCInterfaceDecl *superClassDecl = superClassObjTy->getInterface(); 1366 if (!superClassDecl) { 1367 CachedSuperClassType.setInt(true); 1368 return; 1369 } 1370 1371 // If the superclass doesn't have type parameters, then there is no 1372 // substitution to perform. 1373 QualType superClassType(superClassObjTy, 0); 1374 ObjCTypeParamList *superClassTypeParams = superClassDecl->getTypeParamList(); 1375 if (!superClassTypeParams) { 1376 CachedSuperClassType.setPointerAndInt( 1377 superClassType->castAs<ObjCObjectType>(), true); 1378 return; 1379 } 1380 1381 // If the superclass reference is unspecialized, return it. 1382 if (superClassObjTy->isUnspecialized()) { 1383 CachedSuperClassType.setPointerAndInt(superClassObjTy, true); 1384 return; 1385 } 1386 1387 // If the subclass is not parameterized, there aren't any type 1388 // parameters in the superclass reference to substitute. 1389 ObjCTypeParamList *typeParams = classDecl->getTypeParamList(); 1390 if (!typeParams) { 1391 CachedSuperClassType.setPointerAndInt( 1392 superClassType->castAs<ObjCObjectType>(), true); 1393 return; 1394 } 1395 1396 // If the subclass type isn't specialized, return the unspecialized 1397 // superclass. 1398 if (isUnspecialized()) { 1399 QualType unspecializedSuper 1400 = classDecl->getASTContext().getObjCInterfaceType( 1401 superClassObjTy->getInterface()); 1402 CachedSuperClassType.setPointerAndInt( 1403 unspecializedSuper->castAs<ObjCObjectType>(), 1404 true); 1405 return; 1406 } 1407 1408 // Substitute the provided type arguments into the superclass type. 1409 ArrayRef<QualType> typeArgs = getTypeArgs(); 1410 assert(typeArgs.size() == typeParams->size()); 1411 CachedSuperClassType.setPointerAndInt( 1412 superClassType.substObjCTypeArgs(classDecl->getASTContext(), typeArgs, 1413 ObjCSubstitutionContext::Superclass) 1414 ->castAs<ObjCObjectType>(), 1415 true); 1416 } 1417 1418 const ObjCInterfaceType *ObjCObjectPointerType::getInterfaceType() const { 1419 if (auto interfaceDecl = getObjectType()->getInterface()) { 1420 return interfaceDecl->getASTContext().getObjCInterfaceType(interfaceDecl) 1421 ->castAs<ObjCInterfaceType>(); 1422 } 1423 1424 return nullptr; 1425 } 1426 1427 QualType ObjCObjectPointerType::getSuperClassType() const { 1428 QualType superObjectType = getObjectType()->getSuperClassType(); 1429 if (superObjectType.isNull()) 1430 return superObjectType; 1431 1432 ASTContext &ctx = getInterfaceDecl()->getASTContext(); 1433 return ctx.getObjCObjectPointerType(superObjectType); 1434 } 1435 1436 const ObjCObjectType *Type::getAsObjCQualifiedInterfaceType() const { 1437 // There is no sugar for ObjCObjectType's, just return the canonical 1438 // type pointer if it is the right class. There is no typedef information to 1439 // return and these cannot be Address-space qualified. 1440 if (const ObjCObjectType *T = getAs<ObjCObjectType>()) 1441 if (T->getNumProtocols() && T->getInterface()) 1442 return T; 1443 return nullptr; 1444 } 1445 1446 bool Type::isObjCQualifiedInterfaceType() const { 1447 return getAsObjCQualifiedInterfaceType() != nullptr; 1448 } 1449 1450 const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const { 1451 // There is no sugar for ObjCQualifiedIdType's, just return the canonical 1452 // type pointer if it is the right class. 1453 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) { 1454 if (OPT->isObjCQualifiedIdType()) 1455 return OPT; 1456 } 1457 return nullptr; 1458 } 1459 1460 const ObjCObjectPointerType *Type::getAsObjCQualifiedClassType() const { 1461 // There is no sugar for ObjCQualifiedClassType's, just return the canonical 1462 // type pointer if it is the right class. 1463 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) { 1464 if (OPT->isObjCQualifiedClassType()) 1465 return OPT; 1466 } 1467 return nullptr; 1468 } 1469 1470 const ObjCObjectType *Type::getAsObjCInterfaceType() const { 1471 if (const ObjCObjectType *OT = getAs<ObjCObjectType>()) { 1472 if (OT->getInterface()) 1473 return OT; 1474 } 1475 return nullptr; 1476 } 1477 const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const { 1478 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) { 1479 if (OPT->getInterfaceType()) 1480 return OPT; 1481 } 1482 return nullptr; 1483 } 1484 1485 const CXXRecordDecl *Type::getPointeeCXXRecordDecl() const { 1486 QualType PointeeType; 1487 if (const PointerType *PT = getAs<PointerType>()) 1488 PointeeType = PT->getPointeeType(); 1489 else if (const ReferenceType *RT = getAs<ReferenceType>()) 1490 PointeeType = RT->getPointeeType(); 1491 else 1492 return nullptr; 1493 1494 if (const RecordType *RT = PointeeType->getAs<RecordType>()) 1495 return dyn_cast<CXXRecordDecl>(RT->getDecl()); 1496 1497 return nullptr; 1498 } 1499 1500 CXXRecordDecl *Type::getAsCXXRecordDecl() const { 1501 return dyn_cast_or_null<CXXRecordDecl>(getAsTagDecl()); 1502 } 1503 1504 TagDecl *Type::getAsTagDecl() const { 1505 if (const auto *TT = getAs<TagType>()) 1506 return cast<TagDecl>(TT->getDecl()); 1507 if (const auto *Injected = getAs<InjectedClassNameType>()) 1508 return Injected->getDecl(); 1509 1510 return nullptr; 1511 } 1512 1513 namespace { 1514 class GetContainedAutoVisitor : 1515 public TypeVisitor<GetContainedAutoVisitor, AutoType*> { 1516 public: 1517 using TypeVisitor<GetContainedAutoVisitor, AutoType*>::Visit; 1518 AutoType *Visit(QualType T) { 1519 if (T.isNull()) 1520 return nullptr; 1521 return Visit(T.getTypePtr()); 1522 } 1523 1524 // The 'auto' type itself. 1525 AutoType *VisitAutoType(const AutoType *AT) { 1526 return const_cast<AutoType*>(AT); 1527 } 1528 1529 // Only these types can contain the desired 'auto' type. 1530 AutoType *VisitPointerType(const PointerType *T) { 1531 return Visit(T->getPointeeType()); 1532 } 1533 AutoType *VisitBlockPointerType(const BlockPointerType *T) { 1534 return Visit(T->getPointeeType()); 1535 } 1536 AutoType *VisitReferenceType(const ReferenceType *T) { 1537 return Visit(T->getPointeeTypeAsWritten()); 1538 } 1539 AutoType *VisitMemberPointerType(const MemberPointerType *T) { 1540 return Visit(T->getPointeeType()); 1541 } 1542 AutoType *VisitArrayType(const ArrayType *T) { 1543 return Visit(T->getElementType()); 1544 } 1545 AutoType *VisitDependentSizedExtVectorType( 1546 const DependentSizedExtVectorType *T) { 1547 return Visit(T->getElementType()); 1548 } 1549 AutoType *VisitVectorType(const VectorType *T) { 1550 return Visit(T->getElementType()); 1551 } 1552 AutoType *VisitFunctionType(const FunctionType *T) { 1553 return Visit(T->getReturnType()); 1554 } 1555 AutoType *VisitParenType(const ParenType *T) { 1556 return Visit(T->getInnerType()); 1557 } 1558 AutoType *VisitAttributedType(const AttributedType *T) { 1559 return Visit(T->getModifiedType()); 1560 } 1561 AutoType *VisitAdjustedType(const AdjustedType *T) { 1562 return Visit(T->getOriginalType()); 1563 } 1564 }; 1565 } 1566 1567 AutoType *Type::getContainedAutoType() const { 1568 return GetContainedAutoVisitor().Visit(this); 1569 } 1570 1571 bool Type::hasIntegerRepresentation() const { 1572 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 1573 return VT->getElementType()->isIntegerType(); 1574 else 1575 return isIntegerType(); 1576 } 1577 1578 /// \brief Determine whether this type is an integral type. 1579 /// 1580 /// This routine determines whether the given type is an integral type per 1581 /// C++ [basic.fundamental]p7. Although the C standard does not define the 1582 /// term "integral type", it has a similar term "integer type", and in C++ 1583 /// the two terms are equivalent. However, C's "integer type" includes 1584 /// enumeration types, while C++'s "integer type" does not. The \c ASTContext 1585 /// parameter is used to determine whether we should be following the C or 1586 /// C++ rules when determining whether this type is an integral/integer type. 1587 /// 1588 /// For cases where C permits "an integer type" and C++ permits "an integral 1589 /// type", use this routine. 1590 /// 1591 /// For cases where C permits "an integer type" and C++ permits "an integral 1592 /// or enumeration type", use \c isIntegralOrEnumerationType() instead. 1593 /// 1594 /// \param Ctx The context in which this type occurs. 1595 /// 1596 /// \returns true if the type is considered an integral type, false otherwise. 1597 bool Type::isIntegralType(ASTContext &Ctx) const { 1598 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 1599 return BT->getKind() >= BuiltinType::Bool && 1600 BT->getKind() <= BuiltinType::Int128; 1601 1602 // Complete enum types are integral in C. 1603 if (!Ctx.getLangOpts().CPlusPlus) 1604 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 1605 return ET->getDecl()->isComplete(); 1606 1607 return false; 1608 } 1609 1610 1611 bool Type::isIntegralOrUnscopedEnumerationType() const { 1612 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 1613 return BT->getKind() >= BuiltinType::Bool && 1614 BT->getKind() <= BuiltinType::Int128; 1615 1616 // Check for a complete enum type; incomplete enum types are not properly an 1617 // enumeration type in the sense required here. 1618 // C++0x: However, if the underlying type of the enum is fixed, it is 1619 // considered complete. 1620 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 1621 return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped(); 1622 1623 return false; 1624 } 1625 1626 1627 1628 bool Type::isCharType() const { 1629 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 1630 return BT->getKind() == BuiltinType::Char_U || 1631 BT->getKind() == BuiltinType::UChar || 1632 BT->getKind() == BuiltinType::Char_S || 1633 BT->getKind() == BuiltinType::SChar; 1634 return false; 1635 } 1636 1637 bool Type::isWideCharType() const { 1638 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 1639 return BT->getKind() == BuiltinType::WChar_S || 1640 BT->getKind() == BuiltinType::WChar_U; 1641 return false; 1642 } 1643 1644 bool Type::isChar16Type() const { 1645 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 1646 return BT->getKind() == BuiltinType::Char16; 1647 return false; 1648 } 1649 1650 bool Type::isChar32Type() const { 1651 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 1652 return BT->getKind() == BuiltinType::Char32; 1653 return false; 1654 } 1655 1656 /// \brief Determine whether this type is any of the built-in character 1657 /// types. 1658 bool Type::isAnyCharacterType() const { 1659 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType); 1660 if (!BT) return false; 1661 switch (BT->getKind()) { 1662 default: return false; 1663 case BuiltinType::Char_U: 1664 case BuiltinType::UChar: 1665 case BuiltinType::WChar_U: 1666 case BuiltinType::Char16: 1667 case BuiltinType::Char32: 1668 case BuiltinType::Char_S: 1669 case BuiltinType::SChar: 1670 case BuiltinType::WChar_S: 1671 return true; 1672 } 1673 } 1674 1675 /// isSignedIntegerType - Return true if this is an integer type that is 1676 /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], 1677 /// an enum decl which has a signed representation 1678 bool Type::isSignedIntegerType() const { 1679 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { 1680 return BT->getKind() >= BuiltinType::Char_S && 1681 BT->getKind() <= BuiltinType::Int128; 1682 } 1683 1684 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) { 1685 // Incomplete enum types are not treated as integer types. 1686 // FIXME: In C++, enum types are never integer types. 1687 if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped()) 1688 return ET->getDecl()->getIntegerType()->isSignedIntegerType(); 1689 } 1690 1691 return false; 1692 } 1693 1694 bool Type::isSignedIntegerOrEnumerationType() const { 1695 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { 1696 return BT->getKind() >= BuiltinType::Char_S && 1697 BT->getKind() <= BuiltinType::Int128; 1698 } 1699 1700 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) { 1701 if (ET->getDecl()->isComplete()) 1702 return ET->getDecl()->getIntegerType()->isSignedIntegerType(); 1703 } 1704 1705 return false; 1706 } 1707 1708 bool Type::hasSignedIntegerRepresentation() const { 1709 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 1710 return VT->getElementType()->isSignedIntegerOrEnumerationType(); 1711 else 1712 return isSignedIntegerOrEnumerationType(); 1713 } 1714 1715 /// isUnsignedIntegerType - Return true if this is an integer type that is 1716 /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum 1717 /// decl which has an unsigned representation 1718 bool Type::isUnsignedIntegerType() const { 1719 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { 1720 return BT->getKind() >= BuiltinType::Bool && 1721 BT->getKind() <= BuiltinType::UInt128; 1722 } 1723 1724 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) { 1725 // Incomplete enum types are not treated as integer types. 1726 // FIXME: In C++, enum types are never integer types. 1727 if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped()) 1728 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType(); 1729 } 1730 1731 return false; 1732 } 1733 1734 bool Type::isUnsignedIntegerOrEnumerationType() const { 1735 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { 1736 return BT->getKind() >= BuiltinType::Bool && 1737 BT->getKind() <= BuiltinType::UInt128; 1738 } 1739 1740 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) { 1741 if (ET->getDecl()->isComplete()) 1742 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType(); 1743 } 1744 1745 return false; 1746 } 1747 1748 bool Type::hasUnsignedIntegerRepresentation() const { 1749 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 1750 return VT->getElementType()->isUnsignedIntegerOrEnumerationType(); 1751 else 1752 return isUnsignedIntegerOrEnumerationType(); 1753 } 1754 1755 bool Type::isFloatingType() const { 1756 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 1757 return BT->getKind() >= BuiltinType::Half && 1758 BT->getKind() <= BuiltinType::LongDouble; 1759 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) 1760 return CT->getElementType()->isFloatingType(); 1761 return false; 1762 } 1763 1764 bool Type::hasFloatingRepresentation() const { 1765 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) 1766 return VT->getElementType()->isFloatingType(); 1767 else 1768 return isFloatingType(); 1769 } 1770 1771 bool Type::isRealFloatingType() const { 1772 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 1773 return BT->isFloatingPoint(); 1774 return false; 1775 } 1776 1777 bool Type::isRealType() const { 1778 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 1779 return BT->getKind() >= BuiltinType::Bool && 1780 BT->getKind() <= BuiltinType::LongDouble; 1781 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 1782 return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped(); 1783 return false; 1784 } 1785 1786 bool Type::isArithmeticType() const { 1787 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 1788 return BT->getKind() >= BuiltinType::Bool && 1789 BT->getKind() <= BuiltinType::LongDouble; 1790 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 1791 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2). 1792 // If a body isn't seen by the time we get here, return false. 1793 // 1794 // C++0x: Enumerations are not arithmetic types. For now, just return 1795 // false for scoped enumerations since that will disable any 1796 // unwanted implicit conversions. 1797 return !ET->getDecl()->isScoped() && ET->getDecl()->isComplete(); 1798 return isa<ComplexType>(CanonicalType); 1799 } 1800 1801 Type::ScalarTypeKind Type::getScalarTypeKind() const { 1802 assert(isScalarType()); 1803 1804 const Type *T = CanonicalType.getTypePtr(); 1805 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) { 1806 if (BT->getKind() == BuiltinType::Bool) return STK_Bool; 1807 if (BT->getKind() == BuiltinType::NullPtr) return STK_CPointer; 1808 if (BT->isInteger()) return STK_Integral; 1809 if (BT->isFloatingPoint()) return STK_Floating; 1810 llvm_unreachable("unknown scalar builtin type"); 1811 } else if (isa<PointerType>(T)) { 1812 return STK_CPointer; 1813 } else if (isa<BlockPointerType>(T)) { 1814 return STK_BlockPointer; 1815 } else if (isa<ObjCObjectPointerType>(T)) { 1816 return STK_ObjCObjectPointer; 1817 } else if (isa<MemberPointerType>(T)) { 1818 return STK_MemberPointer; 1819 } else if (isa<EnumType>(T)) { 1820 assert(cast<EnumType>(T)->getDecl()->isComplete()); 1821 return STK_Integral; 1822 } else if (const ComplexType *CT = dyn_cast<ComplexType>(T)) { 1823 if (CT->getElementType()->isRealFloatingType()) 1824 return STK_FloatingComplex; 1825 return STK_IntegralComplex; 1826 } 1827 1828 llvm_unreachable("unknown scalar type"); 1829 } 1830 1831 /// \brief Determines whether the type is a C++ aggregate type or C 1832 /// aggregate or union type. 1833 /// 1834 /// An aggregate type is an array or a class type (struct, union, or 1835 /// class) that has no user-declared constructors, no private or 1836 /// protected non-static data members, no base classes, and no virtual 1837 /// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type 1838 /// subsumes the notion of C aggregates (C99 6.2.5p21) because it also 1839 /// includes union types. 1840 bool Type::isAggregateType() const { 1841 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) { 1842 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl())) 1843 return ClassDecl->isAggregate(); 1844 1845 return true; 1846 } 1847 1848 return isa<ArrayType>(CanonicalType); 1849 } 1850 1851 /// isConstantSizeType - Return true if this is not a variable sized type, 1852 /// according to the rules of C99 6.7.5p3. It is not legal to call this on 1853 /// incomplete types or dependent types. 1854 bool Type::isConstantSizeType() const { 1855 assert(!isIncompleteType() && "This doesn't make sense for incomplete types"); 1856 assert(!isDependentType() && "This doesn't make sense for dependent types"); 1857 // The VAT must have a size, as it is known to be complete. 1858 return !isa<VariableArrayType>(CanonicalType); 1859 } 1860 1861 /// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1) 1862 /// - a type that can describe objects, but which lacks information needed to 1863 /// determine its size. 1864 bool Type::isIncompleteType(NamedDecl **Def) const { 1865 if (Def) 1866 *Def = nullptr; 1867 1868 switch (CanonicalType->getTypeClass()) { 1869 default: return false; 1870 case Builtin: 1871 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never 1872 // be completed. 1873 return isVoidType(); 1874 case Enum: { 1875 EnumDecl *EnumD = cast<EnumType>(CanonicalType)->getDecl(); 1876 if (Def) 1877 *Def = EnumD; 1878 1879 // An enumeration with fixed underlying type is complete (C++0x 7.2p3). 1880 if (EnumD->isFixed()) 1881 return false; 1882 1883 return !EnumD->isCompleteDefinition(); 1884 } 1885 case Record: { 1886 // A tagged type (struct/union/enum/class) is incomplete if the decl is a 1887 // forward declaration, but not a full definition (C99 6.2.5p22). 1888 RecordDecl *Rec = cast<RecordType>(CanonicalType)->getDecl(); 1889 if (Def) 1890 *Def = Rec; 1891 return !Rec->isCompleteDefinition(); 1892 } 1893 case ConstantArray: 1894 // An array is incomplete if its element type is incomplete 1895 // (C++ [dcl.array]p1). 1896 // We don't handle variable arrays (they're not allowed in C++) or 1897 // dependent-sized arrays (dependent types are never treated as incomplete). 1898 return cast<ArrayType>(CanonicalType)->getElementType() 1899 ->isIncompleteType(Def); 1900 case IncompleteArray: 1901 // An array of unknown size is an incomplete type (C99 6.2.5p22). 1902 return true; 1903 case MemberPointer: { 1904 // Member pointers in the MS ABI have special behavior in 1905 // RequireCompleteType: they attach a MSInheritanceAttr to the CXXRecordDecl 1906 // to indicate which inheritance model to use. 1907 auto *MPTy = cast<MemberPointerType>(CanonicalType); 1908 const Type *ClassTy = MPTy->getClass(); 1909 // Member pointers with dependent class types don't get special treatment. 1910 if (ClassTy->isDependentType()) 1911 return false; 1912 const CXXRecordDecl *RD = ClassTy->getAsCXXRecordDecl(); 1913 ASTContext &Context = RD->getASTContext(); 1914 // Member pointers not in the MS ABI don't get special treatment. 1915 if (!Context.getTargetInfo().getCXXABI().isMicrosoft()) 1916 return false; 1917 // The inheritance attribute might only be present on the most recent 1918 // CXXRecordDecl, use that one. 1919 RD = RD->getMostRecentDecl(); 1920 // Nothing interesting to do if the inheritance attribute is already set. 1921 if (RD->hasAttr<MSInheritanceAttr>()) 1922 return false; 1923 return true; 1924 } 1925 case ObjCObject: 1926 return cast<ObjCObjectType>(CanonicalType)->getBaseType() 1927 ->isIncompleteType(Def); 1928 case ObjCInterface: { 1929 // ObjC interfaces are incomplete if they are @class, not @interface. 1930 ObjCInterfaceDecl *Interface 1931 = cast<ObjCInterfaceType>(CanonicalType)->getDecl(); 1932 if (Def) 1933 *Def = Interface; 1934 return !Interface->hasDefinition(); 1935 } 1936 } 1937 } 1938 1939 bool QualType::isPODType(ASTContext &Context) const { 1940 // C++11 has a more relaxed definition of POD. 1941 if (Context.getLangOpts().CPlusPlus11) 1942 return isCXX11PODType(Context); 1943 1944 return isCXX98PODType(Context); 1945 } 1946 1947 bool QualType::isCXX98PODType(ASTContext &Context) const { 1948 // The compiler shouldn't query this for incomplete types, but the user might. 1949 // We return false for that case. Except for incomplete arrays of PODs, which 1950 // are PODs according to the standard. 1951 if (isNull()) 1952 return 0; 1953 1954 if ((*this)->isIncompleteArrayType()) 1955 return Context.getBaseElementType(*this).isCXX98PODType(Context); 1956 1957 if ((*this)->isIncompleteType()) 1958 return false; 1959 1960 if (Context.getLangOpts().ObjCAutoRefCount) { 1961 switch (getObjCLifetime()) { 1962 case Qualifiers::OCL_ExplicitNone: 1963 return true; 1964 1965 case Qualifiers::OCL_Strong: 1966 case Qualifiers::OCL_Weak: 1967 case Qualifiers::OCL_Autoreleasing: 1968 return false; 1969 1970 case Qualifiers::OCL_None: 1971 break; 1972 } 1973 } 1974 1975 QualType CanonicalType = getTypePtr()->CanonicalType; 1976 switch (CanonicalType->getTypeClass()) { 1977 // Everything not explicitly mentioned is not POD. 1978 default: return false; 1979 case Type::VariableArray: 1980 case Type::ConstantArray: 1981 // IncompleteArray is handled above. 1982 return Context.getBaseElementType(*this).isCXX98PODType(Context); 1983 1984 case Type::ObjCObjectPointer: 1985 case Type::BlockPointer: 1986 case Type::Builtin: 1987 case Type::Complex: 1988 case Type::Pointer: 1989 case Type::MemberPointer: 1990 case Type::Vector: 1991 case Type::ExtVector: 1992 return true; 1993 1994 case Type::Enum: 1995 return true; 1996 1997 case Type::Record: 1998 if (CXXRecordDecl *ClassDecl 1999 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl())) 2000 return ClassDecl->isPOD(); 2001 2002 // C struct/union is POD. 2003 return true; 2004 } 2005 } 2006 2007 bool QualType::isTrivialType(ASTContext &Context) const { 2008 // The compiler shouldn't query this for incomplete types, but the user might. 2009 // We return false for that case. Except for incomplete arrays of PODs, which 2010 // are PODs according to the standard. 2011 if (isNull()) 2012 return 0; 2013 2014 if ((*this)->isArrayType()) 2015 return Context.getBaseElementType(*this).isTrivialType(Context); 2016 2017 // Return false for incomplete types after skipping any incomplete array 2018 // types which are expressly allowed by the standard and thus our API. 2019 if ((*this)->isIncompleteType()) 2020 return false; 2021 2022 if (Context.getLangOpts().ObjCAutoRefCount) { 2023 switch (getObjCLifetime()) { 2024 case Qualifiers::OCL_ExplicitNone: 2025 return true; 2026 2027 case Qualifiers::OCL_Strong: 2028 case Qualifiers::OCL_Weak: 2029 case Qualifiers::OCL_Autoreleasing: 2030 return false; 2031 2032 case Qualifiers::OCL_None: 2033 if ((*this)->isObjCLifetimeType()) 2034 return false; 2035 break; 2036 } 2037 } 2038 2039 QualType CanonicalType = getTypePtr()->CanonicalType; 2040 if (CanonicalType->isDependentType()) 2041 return false; 2042 2043 // C++0x [basic.types]p9: 2044 // Scalar types, trivial class types, arrays of such types, and 2045 // cv-qualified versions of these types are collectively called trivial 2046 // types. 2047 2048 // As an extension, Clang treats vector types as Scalar types. 2049 if (CanonicalType->isScalarType() || CanonicalType->isVectorType()) 2050 return true; 2051 if (const RecordType *RT = CanonicalType->getAs<RecordType>()) { 2052 if (const CXXRecordDecl *ClassDecl = 2053 dyn_cast<CXXRecordDecl>(RT->getDecl())) { 2054 // C++11 [class]p6: 2055 // A trivial class is a class that has a default constructor, 2056 // has no non-trivial default constructors, and is trivially 2057 // copyable. 2058 return ClassDecl->hasDefaultConstructor() && 2059 !ClassDecl->hasNonTrivialDefaultConstructor() && 2060 ClassDecl->isTriviallyCopyable(); 2061 } 2062 2063 return true; 2064 } 2065 2066 // No other types can match. 2067 return false; 2068 } 2069 2070 bool QualType::isTriviallyCopyableType(ASTContext &Context) const { 2071 if ((*this)->isArrayType()) 2072 return Context.getBaseElementType(*this).isTriviallyCopyableType(Context); 2073 2074 if (Context.getLangOpts().ObjCAutoRefCount) { 2075 switch (getObjCLifetime()) { 2076 case Qualifiers::OCL_ExplicitNone: 2077 return true; 2078 2079 case Qualifiers::OCL_Strong: 2080 case Qualifiers::OCL_Weak: 2081 case Qualifiers::OCL_Autoreleasing: 2082 return false; 2083 2084 case Qualifiers::OCL_None: 2085 if ((*this)->isObjCLifetimeType()) 2086 return false; 2087 break; 2088 } 2089 } 2090 2091 // C++11 [basic.types]p9 2092 // Scalar types, trivially copyable class types, arrays of such types, and 2093 // non-volatile const-qualified versions of these types are collectively 2094 // called trivially copyable types. 2095 2096 QualType CanonicalType = getCanonicalType(); 2097 if (CanonicalType->isDependentType()) 2098 return false; 2099 2100 if (CanonicalType.isVolatileQualified()) 2101 return false; 2102 2103 // Return false for incomplete types after skipping any incomplete array types 2104 // which are expressly allowed by the standard and thus our API. 2105 if (CanonicalType->isIncompleteType()) 2106 return false; 2107 2108 // As an extension, Clang treats vector types as Scalar types. 2109 if (CanonicalType->isScalarType() || CanonicalType->isVectorType()) 2110 return true; 2111 2112 if (const RecordType *RT = CanonicalType->getAs<RecordType>()) { 2113 if (const CXXRecordDecl *ClassDecl = 2114 dyn_cast<CXXRecordDecl>(RT->getDecl())) { 2115 if (!ClassDecl->isTriviallyCopyable()) return false; 2116 } 2117 2118 return true; 2119 } 2120 2121 // No other types can match. 2122 return false; 2123 } 2124 2125 2126 2127 bool Type::isLiteralType(const ASTContext &Ctx) const { 2128 if (isDependentType()) 2129 return false; 2130 2131 // C++1y [basic.types]p10: 2132 // A type is a literal type if it is: 2133 // -- cv void; or 2134 if (Ctx.getLangOpts().CPlusPlus14 && isVoidType()) 2135 return true; 2136 2137 // C++11 [basic.types]p10: 2138 // A type is a literal type if it is: 2139 // [...] 2140 // -- an array of literal type other than an array of runtime bound; or 2141 if (isVariableArrayType()) 2142 return false; 2143 const Type *BaseTy = getBaseElementTypeUnsafe(); 2144 assert(BaseTy && "NULL element type"); 2145 2146 // Return false for incomplete types after skipping any incomplete array 2147 // types; those are expressly allowed by the standard and thus our API. 2148 if (BaseTy->isIncompleteType()) 2149 return false; 2150 2151 // C++11 [basic.types]p10: 2152 // A type is a literal type if it is: 2153 // -- a scalar type; or 2154 // As an extension, Clang treats vector types and complex types as 2155 // literal types. 2156 if (BaseTy->isScalarType() || BaseTy->isVectorType() || 2157 BaseTy->isAnyComplexType()) 2158 return true; 2159 // -- a reference type; or 2160 if (BaseTy->isReferenceType()) 2161 return true; 2162 // -- a class type that has all of the following properties: 2163 if (const RecordType *RT = BaseTy->getAs<RecordType>()) { 2164 // -- a trivial destructor, 2165 // -- every constructor call and full-expression in the 2166 // brace-or-equal-initializers for non-static data members (if any) 2167 // is a constant expression, 2168 // -- it is an aggregate type or has at least one constexpr 2169 // constructor or constructor template that is not a copy or move 2170 // constructor, and 2171 // -- all non-static data members and base classes of literal types 2172 // 2173 // We resolve DR1361 by ignoring the second bullet. 2174 if (const CXXRecordDecl *ClassDecl = 2175 dyn_cast<CXXRecordDecl>(RT->getDecl())) 2176 return ClassDecl->isLiteral(); 2177 2178 return true; 2179 } 2180 2181 // We treat _Atomic T as a literal type if T is a literal type. 2182 if (const AtomicType *AT = BaseTy->getAs<AtomicType>()) 2183 return AT->getValueType()->isLiteralType(Ctx); 2184 2185 // If this type hasn't been deduced yet, then conservatively assume that 2186 // it'll work out to be a literal type. 2187 if (isa<AutoType>(BaseTy->getCanonicalTypeInternal())) 2188 return true; 2189 2190 return false; 2191 } 2192 2193 bool Type::isStandardLayoutType() const { 2194 if (isDependentType()) 2195 return false; 2196 2197 // C++0x [basic.types]p9: 2198 // Scalar types, standard-layout class types, arrays of such types, and 2199 // cv-qualified versions of these types are collectively called 2200 // standard-layout types. 2201 const Type *BaseTy = getBaseElementTypeUnsafe(); 2202 assert(BaseTy && "NULL element type"); 2203 2204 // Return false for incomplete types after skipping any incomplete array 2205 // types which are expressly allowed by the standard and thus our API. 2206 if (BaseTy->isIncompleteType()) 2207 return false; 2208 2209 // As an extension, Clang treats vector types as Scalar types. 2210 if (BaseTy->isScalarType() || BaseTy->isVectorType()) return true; 2211 if (const RecordType *RT = BaseTy->getAs<RecordType>()) { 2212 if (const CXXRecordDecl *ClassDecl = 2213 dyn_cast<CXXRecordDecl>(RT->getDecl())) 2214 if (!ClassDecl->isStandardLayout()) 2215 return false; 2216 2217 // Default to 'true' for non-C++ class types. 2218 // FIXME: This is a bit dubious, but plain C structs should trivially meet 2219 // all the requirements of standard layout classes. 2220 return true; 2221 } 2222 2223 // No other types can match. 2224 return false; 2225 } 2226 2227 // This is effectively the intersection of isTrivialType and 2228 // isStandardLayoutType. We implement it directly to avoid redundant 2229 // conversions from a type to a CXXRecordDecl. 2230 bool QualType::isCXX11PODType(ASTContext &Context) const { 2231 const Type *ty = getTypePtr(); 2232 if (ty->isDependentType()) 2233 return false; 2234 2235 if (Context.getLangOpts().ObjCAutoRefCount) { 2236 switch (getObjCLifetime()) { 2237 case Qualifiers::OCL_ExplicitNone: 2238 return true; 2239 2240 case Qualifiers::OCL_Strong: 2241 case Qualifiers::OCL_Weak: 2242 case Qualifiers::OCL_Autoreleasing: 2243 return false; 2244 2245 case Qualifiers::OCL_None: 2246 break; 2247 } 2248 } 2249 2250 // C++11 [basic.types]p9: 2251 // Scalar types, POD classes, arrays of such types, and cv-qualified 2252 // versions of these types are collectively called trivial types. 2253 const Type *BaseTy = ty->getBaseElementTypeUnsafe(); 2254 assert(BaseTy && "NULL element type"); 2255 2256 // Return false for incomplete types after skipping any incomplete array 2257 // types which are expressly allowed by the standard and thus our API. 2258 if (BaseTy->isIncompleteType()) 2259 return false; 2260 2261 // As an extension, Clang treats vector types as Scalar types. 2262 if (BaseTy->isScalarType() || BaseTy->isVectorType()) return true; 2263 if (const RecordType *RT = BaseTy->getAs<RecordType>()) { 2264 if (const CXXRecordDecl *ClassDecl = 2265 dyn_cast<CXXRecordDecl>(RT->getDecl())) { 2266 // C++11 [class]p10: 2267 // A POD struct is a non-union class that is both a trivial class [...] 2268 if (!ClassDecl->isTrivial()) return false; 2269 2270 // C++11 [class]p10: 2271 // A POD struct is a non-union class that is both a trivial class and 2272 // a standard-layout class [...] 2273 if (!ClassDecl->isStandardLayout()) return false; 2274 2275 // C++11 [class]p10: 2276 // A POD struct is a non-union class that is both a trivial class and 2277 // a standard-layout class, and has no non-static data members of type 2278 // non-POD struct, non-POD union (or array of such types). [...] 2279 // 2280 // We don't directly query the recursive aspect as the requirements for 2281 // both standard-layout classes and trivial classes apply recursively 2282 // already. 2283 } 2284 2285 return true; 2286 } 2287 2288 // No other types can match. 2289 return false; 2290 } 2291 2292 bool Type::isPromotableIntegerType() const { 2293 if (const BuiltinType *BT = getAs<BuiltinType>()) 2294 switch (BT->getKind()) { 2295 case BuiltinType::Bool: 2296 case BuiltinType::Char_S: 2297 case BuiltinType::Char_U: 2298 case BuiltinType::SChar: 2299 case BuiltinType::UChar: 2300 case BuiltinType::Short: 2301 case BuiltinType::UShort: 2302 case BuiltinType::WChar_S: 2303 case BuiltinType::WChar_U: 2304 case BuiltinType::Char16: 2305 case BuiltinType::Char32: 2306 return true; 2307 default: 2308 return false; 2309 } 2310 2311 // Enumerated types are promotable to their compatible integer types 2312 // (C99 6.3.1.1) a.k.a. its underlying type (C++ [conv.prom]p2). 2313 if (const EnumType *ET = getAs<EnumType>()){ 2314 if (this->isDependentType() || ET->getDecl()->getPromotionType().isNull() 2315 || ET->getDecl()->isScoped()) 2316 return false; 2317 2318 return true; 2319 } 2320 2321 return false; 2322 } 2323 2324 bool Type::isSpecifierType() const { 2325 // Note that this intentionally does not use the canonical type. 2326 switch (getTypeClass()) { 2327 case Builtin: 2328 case Record: 2329 case Enum: 2330 case Typedef: 2331 case Complex: 2332 case TypeOfExpr: 2333 case TypeOf: 2334 case TemplateTypeParm: 2335 case SubstTemplateTypeParm: 2336 case TemplateSpecialization: 2337 case Elaborated: 2338 case DependentName: 2339 case DependentTemplateSpecialization: 2340 case ObjCInterface: 2341 case ObjCObject: 2342 case ObjCObjectPointer: // FIXME: object pointers aren't really specifiers 2343 return true; 2344 default: 2345 return false; 2346 } 2347 } 2348 2349 ElaboratedTypeKeyword 2350 TypeWithKeyword::getKeywordForTypeSpec(unsigned TypeSpec) { 2351 switch (TypeSpec) { 2352 default: return ETK_None; 2353 case TST_typename: return ETK_Typename; 2354 case TST_class: return ETK_Class; 2355 case TST_struct: return ETK_Struct; 2356 case TST_interface: return ETK_Interface; 2357 case TST_union: return ETK_Union; 2358 case TST_enum: return ETK_Enum; 2359 } 2360 } 2361 2362 TagTypeKind 2363 TypeWithKeyword::getTagTypeKindForTypeSpec(unsigned TypeSpec) { 2364 switch(TypeSpec) { 2365 case TST_class: return TTK_Class; 2366 case TST_struct: return TTK_Struct; 2367 case TST_interface: return TTK_Interface; 2368 case TST_union: return TTK_Union; 2369 case TST_enum: return TTK_Enum; 2370 } 2371 2372 llvm_unreachable("Type specifier is not a tag type kind."); 2373 } 2374 2375 ElaboratedTypeKeyword 2376 TypeWithKeyword::getKeywordForTagTypeKind(TagTypeKind Kind) { 2377 switch (Kind) { 2378 case TTK_Class: return ETK_Class; 2379 case TTK_Struct: return ETK_Struct; 2380 case TTK_Interface: return ETK_Interface; 2381 case TTK_Union: return ETK_Union; 2382 case TTK_Enum: return ETK_Enum; 2383 } 2384 llvm_unreachable("Unknown tag type kind."); 2385 } 2386 2387 TagTypeKind 2388 TypeWithKeyword::getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword) { 2389 switch (Keyword) { 2390 case ETK_Class: return TTK_Class; 2391 case ETK_Struct: return TTK_Struct; 2392 case ETK_Interface: return TTK_Interface; 2393 case ETK_Union: return TTK_Union; 2394 case ETK_Enum: return TTK_Enum; 2395 case ETK_None: // Fall through. 2396 case ETK_Typename: 2397 llvm_unreachable("Elaborated type keyword is not a tag type kind."); 2398 } 2399 llvm_unreachable("Unknown elaborated type keyword."); 2400 } 2401 2402 bool 2403 TypeWithKeyword::KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword) { 2404 switch (Keyword) { 2405 case ETK_None: 2406 case ETK_Typename: 2407 return false; 2408 case ETK_Class: 2409 case ETK_Struct: 2410 case ETK_Interface: 2411 case ETK_Union: 2412 case ETK_Enum: 2413 return true; 2414 } 2415 llvm_unreachable("Unknown elaborated type keyword."); 2416 } 2417 2418 StringRef TypeWithKeyword::getKeywordName(ElaboratedTypeKeyword Keyword) { 2419 switch (Keyword) { 2420 case ETK_None: return ""; 2421 case ETK_Typename: return "typename"; 2422 case ETK_Class: return "class"; 2423 case ETK_Struct: return "struct"; 2424 case ETK_Interface: return "__interface"; 2425 case ETK_Union: return "union"; 2426 case ETK_Enum: return "enum"; 2427 } 2428 2429 llvm_unreachable("Unknown elaborated type keyword."); 2430 } 2431 2432 DependentTemplateSpecializationType::DependentTemplateSpecializationType( 2433 ElaboratedTypeKeyword Keyword, 2434 NestedNameSpecifier *NNS, const IdentifierInfo *Name, 2435 unsigned NumArgs, const TemplateArgument *Args, 2436 QualType Canon) 2437 : TypeWithKeyword(Keyword, DependentTemplateSpecialization, Canon, true, true, 2438 /*VariablyModified=*/false, 2439 NNS && NNS->containsUnexpandedParameterPack()), 2440 NNS(NNS), Name(Name), NumArgs(NumArgs) { 2441 assert((!NNS || NNS->isDependent()) && 2442 "DependentTemplateSpecializatonType requires dependent qualifier"); 2443 for (unsigned I = 0; I != NumArgs; ++I) { 2444 if (Args[I].containsUnexpandedParameterPack()) 2445 setContainsUnexpandedParameterPack(); 2446 2447 new (&getArgBuffer()[I]) TemplateArgument(Args[I]); 2448 } 2449 } 2450 2451 void 2452 DependentTemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID, 2453 const ASTContext &Context, 2454 ElaboratedTypeKeyword Keyword, 2455 NestedNameSpecifier *Qualifier, 2456 const IdentifierInfo *Name, 2457 unsigned NumArgs, 2458 const TemplateArgument *Args) { 2459 ID.AddInteger(Keyword); 2460 ID.AddPointer(Qualifier); 2461 ID.AddPointer(Name); 2462 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) 2463 Args[Idx].Profile(ID, Context); 2464 } 2465 2466 bool Type::isElaboratedTypeSpecifier() const { 2467 ElaboratedTypeKeyword Keyword; 2468 if (const ElaboratedType *Elab = dyn_cast<ElaboratedType>(this)) 2469 Keyword = Elab->getKeyword(); 2470 else if (const DependentNameType *DepName = dyn_cast<DependentNameType>(this)) 2471 Keyword = DepName->getKeyword(); 2472 else if (const DependentTemplateSpecializationType *DepTST = 2473 dyn_cast<DependentTemplateSpecializationType>(this)) 2474 Keyword = DepTST->getKeyword(); 2475 else 2476 return false; 2477 2478 return TypeWithKeyword::KeywordIsTagTypeKind(Keyword); 2479 } 2480 2481 const char *Type::getTypeClassName() const { 2482 switch (TypeBits.TC) { 2483 #define ABSTRACT_TYPE(Derived, Base) 2484 #define TYPE(Derived, Base) case Derived: return #Derived; 2485 #include "clang/AST/TypeNodes.def" 2486 } 2487 2488 llvm_unreachable("Invalid type class."); 2489 } 2490 2491 StringRef BuiltinType::getName(const PrintingPolicy &Policy) const { 2492 switch (getKind()) { 2493 case Void: 2494 return "void"; 2495 case Bool: 2496 return Policy.Bool ? "bool" : "_Bool"; 2497 case Char_S: 2498 return "char"; 2499 case Char_U: 2500 return "char"; 2501 case SChar: 2502 return "signed char"; 2503 case Short: 2504 return "short"; 2505 case Int: 2506 return "int"; 2507 case Long: 2508 return "long"; 2509 case LongLong: 2510 return "long long"; 2511 case Int128: 2512 return "__int128"; 2513 case UChar: 2514 return "unsigned char"; 2515 case UShort: 2516 return "unsigned short"; 2517 case UInt: 2518 return "unsigned int"; 2519 case ULong: 2520 return "unsigned long"; 2521 case ULongLong: 2522 return "unsigned long long"; 2523 case UInt128: 2524 return "unsigned __int128"; 2525 case Half: 2526 return Policy.Half ? "half" : "__fp16"; 2527 case Float: 2528 return "float"; 2529 case Double: 2530 return "double"; 2531 case LongDouble: 2532 return "long double"; 2533 case WChar_S: 2534 case WChar_U: 2535 return Policy.MSWChar ? "__wchar_t" : "wchar_t"; 2536 case Char16: 2537 return "char16_t"; 2538 case Char32: 2539 return "char32_t"; 2540 case NullPtr: 2541 return "nullptr_t"; 2542 case Overload: 2543 return "<overloaded function type>"; 2544 case BoundMember: 2545 return "<bound member function type>"; 2546 case PseudoObject: 2547 return "<pseudo-object type>"; 2548 case Dependent: 2549 return "<dependent type>"; 2550 case UnknownAny: 2551 return "<unknown type>"; 2552 case ARCUnbridgedCast: 2553 return "<ARC unbridged cast type>"; 2554 case BuiltinFn: 2555 return "<builtin fn type>"; 2556 case ObjCId: 2557 return "id"; 2558 case ObjCClass: 2559 return "Class"; 2560 case ObjCSel: 2561 return "SEL"; 2562 case OCLImage1d: 2563 return "image1d_t"; 2564 case OCLImage1dArray: 2565 return "image1d_array_t"; 2566 case OCLImage1dBuffer: 2567 return "image1d_buffer_t"; 2568 case OCLImage2d: 2569 return "image2d_t"; 2570 case OCLImage2dArray: 2571 return "image2d_array_t"; 2572 case OCLImage2dDepth: 2573 return "image2d_depth_t"; 2574 case OCLImage2dArrayDepth: 2575 return "image2d_array_depth_t"; 2576 case OCLImage2dMSAA: 2577 return "image2d_msaa_t"; 2578 case OCLImage2dArrayMSAA: 2579 return "image2d_array_msaa_t"; 2580 case OCLImage2dMSAADepth: 2581 return "image2d_msaa_depth_t"; 2582 case OCLImage2dArrayMSAADepth: 2583 return "image2d_array_msaa_depth_t"; 2584 case OCLImage3d: 2585 return "image3d_t"; 2586 case OCLSampler: 2587 return "sampler_t"; 2588 case OCLEvent: 2589 return "event_t"; 2590 case OCLClkEvent: 2591 return "clk_event_t"; 2592 case OCLQueue: 2593 return "queue_t"; 2594 case OCLNDRange: 2595 return "event_t"; 2596 case OCLReserveID: 2597 return "reserve_id_t"; 2598 case OMPArraySection: 2599 return "<OpenMP array section type>"; 2600 } 2601 2602 llvm_unreachable("Invalid builtin type."); 2603 } 2604 2605 QualType QualType::getNonLValueExprType(const ASTContext &Context) const { 2606 if (const ReferenceType *RefType = getTypePtr()->getAs<ReferenceType>()) 2607 return RefType->getPointeeType(); 2608 2609 // C++0x [basic.lval]: 2610 // Class prvalues can have cv-qualified types; non-class prvalues always 2611 // have cv-unqualified types. 2612 // 2613 // See also C99 6.3.2.1p2. 2614 if (!Context.getLangOpts().CPlusPlus || 2615 (!getTypePtr()->isDependentType() && !getTypePtr()->isRecordType())) 2616 return getUnqualifiedType(); 2617 2618 return *this; 2619 } 2620 2621 StringRef FunctionType::getNameForCallConv(CallingConv CC) { 2622 switch (CC) { 2623 case CC_C: return "cdecl"; 2624 case CC_X86StdCall: return "stdcall"; 2625 case CC_X86FastCall: return "fastcall"; 2626 case CC_X86ThisCall: return "thiscall"; 2627 case CC_X86Pascal: return "pascal"; 2628 case CC_X86VectorCall: return "vectorcall"; 2629 case CC_X86_64Win64: return "ms_abi"; 2630 case CC_X86_64SysV: return "sysv_abi"; 2631 case CC_AAPCS: return "aapcs"; 2632 case CC_AAPCS_VFP: return "aapcs-vfp"; 2633 case CC_IntelOclBicc: return "intel_ocl_bicc"; 2634 case CC_SpirFunction: return "spir_function"; 2635 case CC_SpirKernel: return "spir_kernel"; 2636 } 2637 2638 llvm_unreachable("Invalid calling convention."); 2639 } 2640 2641 FunctionProtoType::FunctionProtoType(QualType result, ArrayRef<QualType> params, 2642 QualType canonical, 2643 const ExtProtoInfo &epi) 2644 : FunctionType(FunctionProto, result, canonical, 2645 result->isDependentType(), 2646 result->isInstantiationDependentType(), 2647 result->isVariablyModifiedType(), 2648 result->containsUnexpandedParameterPack(), epi.ExtInfo), 2649 NumParams(params.size()), 2650 NumExceptions(epi.ExceptionSpec.Exceptions.size()), 2651 ExceptionSpecType(epi.ExceptionSpec.Type), 2652 HasAnyConsumedParams(epi.ConsumedParameters != nullptr), 2653 Variadic(epi.Variadic), HasTrailingReturn(epi.HasTrailingReturn) { 2654 assert(NumParams == params.size() && "function has too many parameters"); 2655 2656 FunctionTypeBits.TypeQuals = epi.TypeQuals; 2657 FunctionTypeBits.RefQualifier = epi.RefQualifier; 2658 2659 // Fill in the trailing argument array. 2660 QualType *argSlot = reinterpret_cast<QualType*>(this+1); 2661 for (unsigned i = 0; i != NumParams; ++i) { 2662 if (params[i]->isDependentType()) 2663 setDependent(); 2664 else if (params[i]->isInstantiationDependentType()) 2665 setInstantiationDependent(); 2666 2667 if (params[i]->containsUnexpandedParameterPack()) 2668 setContainsUnexpandedParameterPack(); 2669 2670 argSlot[i] = params[i]; 2671 } 2672 2673 if (getExceptionSpecType() == EST_Dynamic) { 2674 // Fill in the exception array. 2675 QualType *exnSlot = argSlot + NumParams; 2676 unsigned I = 0; 2677 for (QualType ExceptionType : epi.ExceptionSpec.Exceptions) { 2678 // Note that a dependent exception specification does *not* make 2679 // a type dependent; it's not even part of the C++ type system. 2680 if (ExceptionType->isInstantiationDependentType()) 2681 setInstantiationDependent(); 2682 2683 if (ExceptionType->containsUnexpandedParameterPack()) 2684 setContainsUnexpandedParameterPack(); 2685 2686 exnSlot[I++] = ExceptionType; 2687 } 2688 } else if (getExceptionSpecType() == EST_ComputedNoexcept) { 2689 // Store the noexcept expression and context. 2690 Expr **noexSlot = reinterpret_cast<Expr **>(argSlot + NumParams); 2691 *noexSlot = epi.ExceptionSpec.NoexceptExpr; 2692 2693 if (epi.ExceptionSpec.NoexceptExpr) { 2694 if (epi.ExceptionSpec.NoexceptExpr->isValueDependent() || 2695 epi.ExceptionSpec.NoexceptExpr->isInstantiationDependent()) 2696 setInstantiationDependent(); 2697 2698 if (epi.ExceptionSpec.NoexceptExpr->containsUnexpandedParameterPack()) 2699 setContainsUnexpandedParameterPack(); 2700 } 2701 } else if (getExceptionSpecType() == EST_Uninstantiated) { 2702 // Store the function decl from which we will resolve our 2703 // exception specification. 2704 FunctionDecl **slot = 2705 reinterpret_cast<FunctionDecl **>(argSlot + NumParams); 2706 slot[0] = epi.ExceptionSpec.SourceDecl; 2707 slot[1] = epi.ExceptionSpec.SourceTemplate; 2708 // This exception specification doesn't make the type dependent, because 2709 // it's not instantiated as part of instantiating the type. 2710 } else if (getExceptionSpecType() == EST_Unevaluated) { 2711 // Store the function decl from which we will resolve our 2712 // exception specification. 2713 FunctionDecl **slot = 2714 reinterpret_cast<FunctionDecl **>(argSlot + NumParams); 2715 slot[0] = epi.ExceptionSpec.SourceDecl; 2716 } 2717 2718 if (epi.ConsumedParameters) { 2719 bool *consumedParams = const_cast<bool *>(getConsumedParamsBuffer()); 2720 for (unsigned i = 0; i != NumParams; ++i) 2721 consumedParams[i] = epi.ConsumedParameters[i]; 2722 } 2723 } 2724 2725 bool FunctionProtoType::hasDependentExceptionSpec() const { 2726 if (Expr *NE = getNoexceptExpr()) 2727 return NE->isValueDependent(); 2728 for (QualType ET : exceptions()) 2729 // A pack expansion with a non-dependent pattern is still dependent, 2730 // because we don't know whether the pattern is in the exception spec 2731 // or not (that depends on whether the pack has 0 expansions). 2732 if (ET->isDependentType() || ET->getAs<PackExpansionType>()) 2733 return true; 2734 return false; 2735 } 2736 2737 FunctionProtoType::NoexceptResult 2738 FunctionProtoType::getNoexceptSpec(const ASTContext &ctx) const { 2739 ExceptionSpecificationType est = getExceptionSpecType(); 2740 if (est == EST_BasicNoexcept) 2741 return NR_Nothrow; 2742 2743 if (est != EST_ComputedNoexcept) 2744 return NR_NoNoexcept; 2745 2746 Expr *noexceptExpr = getNoexceptExpr(); 2747 if (!noexceptExpr) 2748 return NR_BadNoexcept; 2749 if (noexceptExpr->isValueDependent()) 2750 return NR_Dependent; 2751 2752 llvm::APSInt value; 2753 bool isICE = noexceptExpr->isIntegerConstantExpr(value, ctx, nullptr, 2754 /*evaluated*/false); 2755 (void)isICE; 2756 assert(isICE && "AST should not contain bad noexcept expressions."); 2757 2758 return value.getBoolValue() ? NR_Nothrow : NR_Throw; 2759 } 2760 2761 bool FunctionProtoType::isNothrow(const ASTContext &Ctx, 2762 bool ResultIfDependent) const { 2763 ExceptionSpecificationType EST = getExceptionSpecType(); 2764 assert(EST != EST_Unevaluated && EST != EST_Uninstantiated); 2765 if (EST == EST_DynamicNone || EST == EST_BasicNoexcept) 2766 return true; 2767 2768 if (EST == EST_Dynamic && ResultIfDependent) { 2769 // A dynamic exception specification is throwing unless every exception 2770 // type is an (unexpanded) pack expansion type. 2771 for (unsigned I = 0, N = NumExceptions; I != N; ++I) 2772 if (!getExceptionType(I)->getAs<PackExpansionType>()) 2773 return false; 2774 return ResultIfDependent; 2775 } 2776 2777 if (EST != EST_ComputedNoexcept) 2778 return false; 2779 2780 NoexceptResult NR = getNoexceptSpec(Ctx); 2781 if (NR == NR_Dependent) 2782 return ResultIfDependent; 2783 return NR == NR_Nothrow; 2784 } 2785 2786 bool FunctionProtoType::isTemplateVariadic() const { 2787 for (unsigned ArgIdx = getNumParams(); ArgIdx; --ArgIdx) 2788 if (isa<PackExpansionType>(getParamType(ArgIdx - 1))) 2789 return true; 2790 2791 return false; 2792 } 2793 2794 void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result, 2795 const QualType *ArgTys, unsigned NumParams, 2796 const ExtProtoInfo &epi, 2797 const ASTContext &Context) { 2798 2799 // We have to be careful not to get ambiguous profile encodings. 2800 // Note that valid type pointers are never ambiguous with anything else. 2801 // 2802 // The encoding grammar begins: 2803 // type type* bool int bool 2804 // If that final bool is true, then there is a section for the EH spec: 2805 // bool type* 2806 // This is followed by an optional "consumed argument" section of the 2807 // same length as the first type sequence: 2808 // bool* 2809 // Finally, we have the ext info and trailing return type flag: 2810 // int bool 2811 // 2812 // There is no ambiguity between the consumed arguments and an empty EH 2813 // spec because of the leading 'bool' which unambiguously indicates 2814 // whether the following bool is the EH spec or part of the arguments. 2815 2816 ID.AddPointer(Result.getAsOpaquePtr()); 2817 for (unsigned i = 0; i != NumParams; ++i) 2818 ID.AddPointer(ArgTys[i].getAsOpaquePtr()); 2819 // This method is relatively performance sensitive, so as a performance 2820 // shortcut, use one AddInteger call instead of four for the next four 2821 // fields. 2822 assert(!(unsigned(epi.Variadic) & ~1) && 2823 !(unsigned(epi.TypeQuals) & ~255) && 2824 !(unsigned(epi.RefQualifier) & ~3) && 2825 !(unsigned(epi.ExceptionSpec.Type) & ~15) && 2826 "Values larger than expected."); 2827 ID.AddInteger(unsigned(epi.Variadic) + 2828 (epi.TypeQuals << 1) + 2829 (epi.RefQualifier << 9) + 2830 (epi.ExceptionSpec.Type << 11)); 2831 if (epi.ExceptionSpec.Type == EST_Dynamic) { 2832 for (QualType Ex : epi.ExceptionSpec.Exceptions) 2833 ID.AddPointer(Ex.getAsOpaquePtr()); 2834 } else if (epi.ExceptionSpec.Type == EST_ComputedNoexcept && 2835 epi.ExceptionSpec.NoexceptExpr) { 2836 epi.ExceptionSpec.NoexceptExpr->Profile(ID, Context, false); 2837 } else if (epi.ExceptionSpec.Type == EST_Uninstantiated || 2838 epi.ExceptionSpec.Type == EST_Unevaluated) { 2839 ID.AddPointer(epi.ExceptionSpec.SourceDecl->getCanonicalDecl()); 2840 } 2841 if (epi.ConsumedParameters) { 2842 for (unsigned i = 0; i != NumParams; ++i) 2843 ID.AddBoolean(epi.ConsumedParameters[i]); 2844 } 2845 epi.ExtInfo.Profile(ID); 2846 ID.AddBoolean(epi.HasTrailingReturn); 2847 } 2848 2849 void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, 2850 const ASTContext &Ctx) { 2851 Profile(ID, getReturnType(), param_type_begin(), NumParams, getExtProtoInfo(), 2852 Ctx); 2853 } 2854 2855 QualType TypedefType::desugar() const { 2856 return getDecl()->getUnderlyingType(); 2857 } 2858 2859 TypeOfExprType::TypeOfExprType(Expr *E, QualType can) 2860 : Type(TypeOfExpr, can, E->isTypeDependent(), 2861 E->isInstantiationDependent(), 2862 E->getType()->isVariablyModifiedType(), 2863 E->containsUnexpandedParameterPack()), 2864 TOExpr(E) { 2865 } 2866 2867 bool TypeOfExprType::isSugared() const { 2868 return !TOExpr->isTypeDependent(); 2869 } 2870 2871 QualType TypeOfExprType::desugar() const { 2872 if (isSugared()) 2873 return getUnderlyingExpr()->getType(); 2874 2875 return QualType(this, 0); 2876 } 2877 2878 void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID, 2879 const ASTContext &Context, Expr *E) { 2880 E->Profile(ID, Context, true); 2881 } 2882 2883 DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can) 2884 // C++11 [temp.type]p2: "If an expression e involves a template parameter, 2885 // decltype(e) denotes a unique dependent type." Hence a decltype type is 2886 // type-dependent even if its expression is only instantiation-dependent. 2887 : Type(Decltype, can, E->isInstantiationDependent(), 2888 E->isInstantiationDependent(), 2889 E->getType()->isVariablyModifiedType(), 2890 E->containsUnexpandedParameterPack()), 2891 E(E), 2892 UnderlyingType(underlyingType) { 2893 } 2894 2895 bool DecltypeType::isSugared() const { return !E->isInstantiationDependent(); } 2896 2897 QualType DecltypeType::desugar() const { 2898 if (isSugared()) 2899 return getUnderlyingType(); 2900 2901 return QualType(this, 0); 2902 } 2903 2904 DependentDecltypeType::DependentDecltypeType(const ASTContext &Context, Expr *E) 2905 : DecltypeType(E, Context.DependentTy), Context(Context) { } 2906 2907 void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID, 2908 const ASTContext &Context, Expr *E) { 2909 E->Profile(ID, Context, true); 2910 } 2911 2912 TagType::TagType(TypeClass TC, const TagDecl *D, QualType can) 2913 : Type(TC, can, D->isDependentType(), 2914 /*InstantiationDependent=*/D->isDependentType(), 2915 /*VariablyModified=*/false, 2916 /*ContainsUnexpandedParameterPack=*/false), 2917 decl(const_cast<TagDecl*>(D)) {} 2918 2919 static TagDecl *getInterestingTagDecl(TagDecl *decl) { 2920 for (auto I : decl->redecls()) { 2921 if (I->isCompleteDefinition() || I->isBeingDefined()) 2922 return I; 2923 } 2924 // If there's no definition (not even in progress), return what we have. 2925 return decl; 2926 } 2927 2928 UnaryTransformType::UnaryTransformType(QualType BaseType, 2929 QualType UnderlyingType, 2930 UTTKind UKind, 2931 QualType CanonicalType) 2932 : Type(UnaryTransform, CanonicalType, UnderlyingType->isDependentType(), 2933 UnderlyingType->isInstantiationDependentType(), 2934 UnderlyingType->isVariablyModifiedType(), 2935 BaseType->containsUnexpandedParameterPack()) 2936 , BaseType(BaseType), UnderlyingType(UnderlyingType), UKind(UKind) 2937 {} 2938 2939 TagDecl *TagType::getDecl() const { 2940 return getInterestingTagDecl(decl); 2941 } 2942 2943 bool TagType::isBeingDefined() const { 2944 return getDecl()->isBeingDefined(); 2945 } 2946 2947 bool AttributedType::isQualifier() const { 2948 switch (getAttrKind()) { 2949 // These are type qualifiers in the traditional C sense: they annotate 2950 // something about a specific value/variable of a type. (They aren't 2951 // always part of the canonical type, though.) 2952 case AttributedType::attr_address_space: 2953 case AttributedType::attr_objc_gc: 2954 case AttributedType::attr_objc_ownership: 2955 case AttributedType::attr_nonnull: 2956 case AttributedType::attr_nullable: 2957 case AttributedType::attr_null_unspecified: 2958 return true; 2959 2960 // These aren't qualifiers; they rewrite the modified type to be a 2961 // semantically different type. 2962 case AttributedType::attr_regparm: 2963 case AttributedType::attr_vector_size: 2964 case AttributedType::attr_neon_vector_type: 2965 case AttributedType::attr_neon_polyvector_type: 2966 case AttributedType::attr_pcs: 2967 case AttributedType::attr_pcs_vfp: 2968 case AttributedType::attr_noreturn: 2969 case AttributedType::attr_cdecl: 2970 case AttributedType::attr_fastcall: 2971 case AttributedType::attr_stdcall: 2972 case AttributedType::attr_thiscall: 2973 case AttributedType::attr_pascal: 2974 case AttributedType::attr_vectorcall: 2975 case AttributedType::attr_inteloclbicc: 2976 case AttributedType::attr_ms_abi: 2977 case AttributedType::attr_sysv_abi: 2978 case AttributedType::attr_ptr32: 2979 case AttributedType::attr_ptr64: 2980 case AttributedType::attr_sptr: 2981 case AttributedType::attr_uptr: 2982 case AttributedType::attr_objc_kindof: 2983 return false; 2984 } 2985 llvm_unreachable("bad attributed type kind"); 2986 } 2987 2988 bool AttributedType::isMSTypeSpec() const { 2989 switch (getAttrKind()) { 2990 default: return false; 2991 case attr_ptr32: 2992 case attr_ptr64: 2993 case attr_sptr: 2994 case attr_uptr: 2995 return true; 2996 } 2997 llvm_unreachable("invalid attr kind"); 2998 } 2999 3000 bool AttributedType::isCallingConv() const { 3001 switch (getAttrKind()) { 3002 case attr_ptr32: 3003 case attr_ptr64: 3004 case attr_sptr: 3005 case attr_uptr: 3006 case attr_address_space: 3007 case attr_regparm: 3008 case attr_vector_size: 3009 case attr_neon_vector_type: 3010 case attr_neon_polyvector_type: 3011 case attr_objc_gc: 3012 case attr_objc_ownership: 3013 case attr_noreturn: 3014 case attr_nonnull: 3015 case attr_nullable: 3016 case attr_null_unspecified: 3017 case attr_objc_kindof: 3018 return false; 3019 3020 case attr_pcs: 3021 case attr_pcs_vfp: 3022 case attr_cdecl: 3023 case attr_fastcall: 3024 case attr_stdcall: 3025 case attr_thiscall: 3026 case attr_vectorcall: 3027 case attr_pascal: 3028 case attr_ms_abi: 3029 case attr_sysv_abi: 3030 case attr_inteloclbicc: 3031 return true; 3032 } 3033 llvm_unreachable("invalid attr kind"); 3034 } 3035 3036 CXXRecordDecl *InjectedClassNameType::getDecl() const { 3037 return cast<CXXRecordDecl>(getInterestingTagDecl(Decl)); 3038 } 3039 3040 IdentifierInfo *TemplateTypeParmType::getIdentifier() const { 3041 return isCanonicalUnqualified() ? nullptr : getDecl()->getIdentifier(); 3042 } 3043 3044 SubstTemplateTypeParmPackType:: 3045 SubstTemplateTypeParmPackType(const TemplateTypeParmType *Param, 3046 QualType Canon, 3047 const TemplateArgument &ArgPack) 3048 : Type(SubstTemplateTypeParmPack, Canon, true, true, false, true), 3049 Replaced(Param), 3050 Arguments(ArgPack.pack_begin()), NumArguments(ArgPack.pack_size()) 3051 { 3052 } 3053 3054 TemplateArgument SubstTemplateTypeParmPackType::getArgumentPack() const { 3055 return TemplateArgument(llvm::makeArrayRef(Arguments, NumArguments)); 3056 } 3057 3058 void SubstTemplateTypeParmPackType::Profile(llvm::FoldingSetNodeID &ID) { 3059 Profile(ID, getReplacedParameter(), getArgumentPack()); 3060 } 3061 3062 void SubstTemplateTypeParmPackType::Profile(llvm::FoldingSetNodeID &ID, 3063 const TemplateTypeParmType *Replaced, 3064 const TemplateArgument &ArgPack) { 3065 ID.AddPointer(Replaced); 3066 ID.AddInteger(ArgPack.pack_size()); 3067 for (const auto &P : ArgPack.pack_elements()) 3068 ID.AddPointer(P.getAsType().getAsOpaquePtr()); 3069 } 3070 3071 bool TemplateSpecializationType:: 3072 anyDependentTemplateArguments(const TemplateArgumentListInfo &Args, 3073 bool &InstantiationDependent) { 3074 return anyDependentTemplateArguments(Args.getArgumentArray(), Args.size(), 3075 InstantiationDependent); 3076 } 3077 3078 bool TemplateSpecializationType:: 3079 anyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N, 3080 bool &InstantiationDependent) { 3081 for (unsigned i = 0; i != N; ++i) { 3082 if (Args[i].getArgument().isDependent()) { 3083 InstantiationDependent = true; 3084 return true; 3085 } 3086 3087 if (Args[i].getArgument().isInstantiationDependent()) 3088 InstantiationDependent = true; 3089 } 3090 return false; 3091 } 3092 3093 TemplateSpecializationType:: 3094 TemplateSpecializationType(TemplateName T, 3095 const TemplateArgument *Args, unsigned NumArgs, 3096 QualType Canon, QualType AliasedType) 3097 : Type(TemplateSpecialization, 3098 Canon.isNull()? QualType(this, 0) : Canon, 3099 Canon.isNull()? true : Canon->isDependentType(), 3100 Canon.isNull()? true : Canon->isInstantiationDependentType(), 3101 false, 3102 T.containsUnexpandedParameterPack()), 3103 Template(T), NumArgs(NumArgs), TypeAlias(!AliasedType.isNull()) { 3104 assert(!T.getAsDependentTemplateName() && 3105 "Use DependentTemplateSpecializationType for dependent template-name"); 3106 assert((T.getKind() == TemplateName::Template || 3107 T.getKind() == TemplateName::SubstTemplateTemplateParm || 3108 T.getKind() == TemplateName::SubstTemplateTemplateParmPack) && 3109 "Unexpected template name for TemplateSpecializationType"); 3110 3111 TemplateArgument *TemplateArgs 3112 = reinterpret_cast<TemplateArgument *>(this + 1); 3113 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 3114 // Update instantiation-dependent and variably-modified bits. 3115 // If the canonical type exists and is non-dependent, the template 3116 // specialization type can be non-dependent even if one of the type 3117 // arguments is. Given: 3118 // template<typename T> using U = int; 3119 // U<T> is always non-dependent, irrespective of the type T. 3120 // However, U<Ts> contains an unexpanded parameter pack, even though 3121 // its expansion (and thus its desugared type) doesn't. 3122 if (Args[Arg].isInstantiationDependent()) 3123 setInstantiationDependent(); 3124 if (Args[Arg].getKind() == TemplateArgument::Type && 3125 Args[Arg].getAsType()->isVariablyModifiedType()) 3126 setVariablyModified(); 3127 if (Args[Arg].containsUnexpandedParameterPack()) 3128 setContainsUnexpandedParameterPack(); 3129 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]); 3130 } 3131 3132 // Store the aliased type if this is a type alias template specialization. 3133 if (TypeAlias) { 3134 TemplateArgument *Begin = reinterpret_cast<TemplateArgument *>(this + 1); 3135 *reinterpret_cast<QualType*>(Begin + getNumArgs()) = AliasedType; 3136 } 3137 } 3138 3139 void 3140 TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID, 3141 TemplateName T, 3142 const TemplateArgument *Args, 3143 unsigned NumArgs, 3144 const ASTContext &Context) { 3145 T.Profile(ID); 3146 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) 3147 Args[Idx].Profile(ID, Context); 3148 } 3149 3150 QualType 3151 QualifierCollector::apply(const ASTContext &Context, QualType QT) const { 3152 if (!hasNonFastQualifiers()) 3153 return QT.withFastQualifiers(getFastQualifiers()); 3154 3155 return Context.getQualifiedType(QT, *this); 3156 } 3157 3158 QualType 3159 QualifierCollector::apply(const ASTContext &Context, const Type *T) const { 3160 if (!hasNonFastQualifiers()) 3161 return QualType(T, getFastQualifiers()); 3162 3163 return Context.getQualifiedType(T, *this); 3164 } 3165 3166 void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID, 3167 QualType BaseType, 3168 ArrayRef<QualType> typeArgs, 3169 ArrayRef<ObjCProtocolDecl *> protocols, 3170 bool isKindOf) { 3171 ID.AddPointer(BaseType.getAsOpaquePtr()); 3172 ID.AddInteger(typeArgs.size()); 3173 for (auto typeArg : typeArgs) 3174 ID.AddPointer(typeArg.getAsOpaquePtr()); 3175 ID.AddInteger(protocols.size()); 3176 for (auto proto : protocols) 3177 ID.AddPointer(proto); 3178 ID.AddBoolean(isKindOf); 3179 } 3180 3181 void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID) { 3182 Profile(ID, getBaseType(), getTypeArgsAsWritten(), 3183 llvm::makeArrayRef(qual_begin(), getNumProtocols()), 3184 isKindOfTypeAsWritten()); 3185 } 3186 3187 namespace { 3188 3189 /// \brief The cached properties of a type. 3190 class CachedProperties { 3191 Linkage L; 3192 bool local; 3193 3194 public: 3195 CachedProperties(Linkage L, bool local) : L(L), local(local) {} 3196 3197 Linkage getLinkage() const { return L; } 3198 bool hasLocalOrUnnamedType() const { return local; } 3199 3200 friend CachedProperties merge(CachedProperties L, CachedProperties R) { 3201 Linkage MergedLinkage = minLinkage(L.L, R.L); 3202 return CachedProperties(MergedLinkage, 3203 L.hasLocalOrUnnamedType() | R.hasLocalOrUnnamedType()); 3204 } 3205 }; 3206 } 3207 3208 static CachedProperties computeCachedProperties(const Type *T); 3209 3210 namespace clang { 3211 /// The type-property cache. This is templated so as to be 3212 /// instantiated at an internal type to prevent unnecessary symbol 3213 /// leakage. 3214 template <class Private> class TypePropertyCache { 3215 public: 3216 static CachedProperties get(QualType T) { 3217 return get(T.getTypePtr()); 3218 } 3219 3220 static CachedProperties get(const Type *T) { 3221 ensure(T); 3222 return CachedProperties(T->TypeBits.getLinkage(), 3223 T->TypeBits.hasLocalOrUnnamedType()); 3224 } 3225 3226 static void ensure(const Type *T) { 3227 // If the cache is valid, we're okay. 3228 if (T->TypeBits.isCacheValid()) return; 3229 3230 // If this type is non-canonical, ask its canonical type for the 3231 // relevant information. 3232 if (!T->isCanonicalUnqualified()) { 3233 const Type *CT = T->getCanonicalTypeInternal().getTypePtr(); 3234 ensure(CT); 3235 T->TypeBits.CacheValid = true; 3236 T->TypeBits.CachedLinkage = CT->TypeBits.CachedLinkage; 3237 T->TypeBits.CachedLocalOrUnnamed = CT->TypeBits.CachedLocalOrUnnamed; 3238 return; 3239 } 3240 3241 // Compute the cached properties and then set the cache. 3242 CachedProperties Result = computeCachedProperties(T); 3243 T->TypeBits.CacheValid = true; 3244 T->TypeBits.CachedLinkage = Result.getLinkage(); 3245 T->TypeBits.CachedLocalOrUnnamed = Result.hasLocalOrUnnamedType(); 3246 } 3247 }; 3248 } 3249 3250 // Instantiate the friend template at a private class. In a 3251 // reasonable implementation, these symbols will be internal. 3252 // It is terrible that this is the best way to accomplish this. 3253 namespace { class Private {}; } 3254 typedef TypePropertyCache<Private> Cache; 3255 3256 static CachedProperties computeCachedProperties(const Type *T) { 3257 switch (T->getTypeClass()) { 3258 #define TYPE(Class,Base) 3259 #define NON_CANONICAL_TYPE(Class,Base) case Type::Class: 3260 #include "clang/AST/TypeNodes.def" 3261 llvm_unreachable("didn't expect a non-canonical type here"); 3262 3263 #define TYPE(Class,Base) 3264 #define DEPENDENT_TYPE(Class,Base) case Type::Class: 3265 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class,Base) case Type::Class: 3266 #include "clang/AST/TypeNodes.def" 3267 // Treat instantiation-dependent types as external. 3268 assert(T->isInstantiationDependentType()); 3269 return CachedProperties(ExternalLinkage, false); 3270 3271 case Type::Auto: 3272 // Give non-deduced 'auto' types external linkage. We should only see them 3273 // here in error recovery. 3274 return CachedProperties(ExternalLinkage, false); 3275 3276 case Type::Builtin: 3277 // C++ [basic.link]p8: 3278 // A type is said to have linkage if and only if: 3279 // - it is a fundamental type (3.9.1); or 3280 return CachedProperties(ExternalLinkage, false); 3281 3282 case Type::Record: 3283 case Type::Enum: { 3284 const TagDecl *Tag = cast<TagType>(T)->getDecl(); 3285 3286 // C++ [basic.link]p8: 3287 // - it is a class or enumeration type that is named (or has a name 3288 // for linkage purposes (7.1.3)) and the name has linkage; or 3289 // - it is a specialization of a class template (14); or 3290 Linkage L = Tag->getLinkageInternal(); 3291 bool IsLocalOrUnnamed = 3292 Tag->getDeclContext()->isFunctionOrMethod() || 3293 !Tag->hasNameForLinkage(); 3294 return CachedProperties(L, IsLocalOrUnnamed); 3295 } 3296 3297 // C++ [basic.link]p8: 3298 // - it is a compound type (3.9.2) other than a class or enumeration, 3299 // compounded exclusively from types that have linkage; or 3300 case Type::Complex: 3301 return Cache::get(cast<ComplexType>(T)->getElementType()); 3302 case Type::Pointer: 3303 return Cache::get(cast<PointerType>(T)->getPointeeType()); 3304 case Type::BlockPointer: 3305 return Cache::get(cast<BlockPointerType>(T)->getPointeeType()); 3306 case Type::LValueReference: 3307 case Type::RValueReference: 3308 return Cache::get(cast<ReferenceType>(T)->getPointeeType()); 3309 case Type::MemberPointer: { 3310 const MemberPointerType *MPT = cast<MemberPointerType>(T); 3311 return merge(Cache::get(MPT->getClass()), 3312 Cache::get(MPT->getPointeeType())); 3313 } 3314 case Type::ConstantArray: 3315 case Type::IncompleteArray: 3316 case Type::VariableArray: 3317 return Cache::get(cast<ArrayType>(T)->getElementType()); 3318 case Type::Vector: 3319 case Type::ExtVector: 3320 return Cache::get(cast<VectorType>(T)->getElementType()); 3321 case Type::FunctionNoProto: 3322 return Cache::get(cast<FunctionType>(T)->getReturnType()); 3323 case Type::FunctionProto: { 3324 const FunctionProtoType *FPT = cast<FunctionProtoType>(T); 3325 CachedProperties result = Cache::get(FPT->getReturnType()); 3326 for (const auto &ai : FPT->param_types()) 3327 result = merge(result, Cache::get(ai)); 3328 return result; 3329 } 3330 case Type::ObjCInterface: { 3331 Linkage L = cast<ObjCInterfaceType>(T)->getDecl()->getLinkageInternal(); 3332 return CachedProperties(L, false); 3333 } 3334 case Type::ObjCObject: 3335 return Cache::get(cast<ObjCObjectType>(T)->getBaseType()); 3336 case Type::ObjCObjectPointer: 3337 return Cache::get(cast<ObjCObjectPointerType>(T)->getPointeeType()); 3338 case Type::Atomic: 3339 return Cache::get(cast<AtomicType>(T)->getValueType()); 3340 } 3341 3342 llvm_unreachable("unhandled type class"); 3343 } 3344 3345 /// \brief Determine the linkage of this type. 3346 Linkage Type::getLinkage() const { 3347 Cache::ensure(this); 3348 return TypeBits.getLinkage(); 3349 } 3350 3351 bool Type::hasUnnamedOrLocalType() const { 3352 Cache::ensure(this); 3353 return TypeBits.hasLocalOrUnnamedType(); 3354 } 3355 3356 static LinkageInfo computeLinkageInfo(QualType T); 3357 3358 static LinkageInfo computeLinkageInfo(const Type *T) { 3359 switch (T->getTypeClass()) { 3360 #define TYPE(Class,Base) 3361 #define NON_CANONICAL_TYPE(Class,Base) case Type::Class: 3362 #include "clang/AST/TypeNodes.def" 3363 llvm_unreachable("didn't expect a non-canonical type here"); 3364 3365 #define TYPE(Class,Base) 3366 #define DEPENDENT_TYPE(Class,Base) case Type::Class: 3367 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class,Base) case Type::Class: 3368 #include "clang/AST/TypeNodes.def" 3369 // Treat instantiation-dependent types as external. 3370 assert(T->isInstantiationDependentType()); 3371 return LinkageInfo::external(); 3372 3373 case Type::Builtin: 3374 return LinkageInfo::external(); 3375 3376 case Type::Auto: 3377 return LinkageInfo::external(); 3378 3379 case Type::Record: 3380 case Type::Enum: 3381 return cast<TagType>(T)->getDecl()->getLinkageAndVisibility(); 3382 3383 case Type::Complex: 3384 return computeLinkageInfo(cast<ComplexType>(T)->getElementType()); 3385 case Type::Pointer: 3386 return computeLinkageInfo(cast<PointerType>(T)->getPointeeType()); 3387 case Type::BlockPointer: 3388 return computeLinkageInfo(cast<BlockPointerType>(T)->getPointeeType()); 3389 case Type::LValueReference: 3390 case Type::RValueReference: 3391 return computeLinkageInfo(cast<ReferenceType>(T)->getPointeeType()); 3392 case Type::MemberPointer: { 3393 const MemberPointerType *MPT = cast<MemberPointerType>(T); 3394 LinkageInfo LV = computeLinkageInfo(MPT->getClass()); 3395 LV.merge(computeLinkageInfo(MPT->getPointeeType())); 3396 return LV; 3397 } 3398 case Type::ConstantArray: 3399 case Type::IncompleteArray: 3400 case Type::VariableArray: 3401 return computeLinkageInfo(cast<ArrayType>(T)->getElementType()); 3402 case Type::Vector: 3403 case Type::ExtVector: 3404 return computeLinkageInfo(cast<VectorType>(T)->getElementType()); 3405 case Type::FunctionNoProto: 3406 return computeLinkageInfo(cast<FunctionType>(T)->getReturnType()); 3407 case Type::FunctionProto: { 3408 const FunctionProtoType *FPT = cast<FunctionProtoType>(T); 3409 LinkageInfo LV = computeLinkageInfo(FPT->getReturnType()); 3410 for (const auto &ai : FPT->param_types()) 3411 LV.merge(computeLinkageInfo(ai)); 3412 return LV; 3413 } 3414 case Type::ObjCInterface: 3415 return cast<ObjCInterfaceType>(T)->getDecl()->getLinkageAndVisibility(); 3416 case Type::ObjCObject: 3417 return computeLinkageInfo(cast<ObjCObjectType>(T)->getBaseType()); 3418 case Type::ObjCObjectPointer: 3419 return computeLinkageInfo(cast<ObjCObjectPointerType>(T)->getPointeeType()); 3420 case Type::Atomic: 3421 return computeLinkageInfo(cast<AtomicType>(T)->getValueType()); 3422 } 3423 3424 llvm_unreachable("unhandled type class"); 3425 } 3426 3427 static LinkageInfo computeLinkageInfo(QualType T) { 3428 return computeLinkageInfo(T.getTypePtr()); 3429 } 3430 3431 bool Type::isLinkageValid() const { 3432 if (!TypeBits.isCacheValid()) 3433 return true; 3434 3435 return computeLinkageInfo(getCanonicalTypeInternal()).getLinkage() == 3436 TypeBits.getLinkage(); 3437 } 3438 3439 LinkageInfo Type::getLinkageAndVisibility() const { 3440 if (!isCanonicalUnqualified()) 3441 return computeLinkageInfo(getCanonicalTypeInternal()); 3442 3443 LinkageInfo LV = computeLinkageInfo(this); 3444 assert(LV.getLinkage() == getLinkage()); 3445 return LV; 3446 } 3447 3448 Optional<NullabilityKind> Type::getNullability(const ASTContext &context) const { 3449 QualType type(this, 0); 3450 do { 3451 // Check whether this is an attributed type with nullability 3452 // information. 3453 if (auto attributed = dyn_cast<AttributedType>(type.getTypePtr())) { 3454 if (auto nullability = attributed->getImmediateNullability()) 3455 return nullability; 3456 } 3457 3458 // Desugar the type. If desugaring does nothing, we're done. 3459 QualType desugared = type.getSingleStepDesugaredType(context); 3460 if (desugared.getTypePtr() == type.getTypePtr()) 3461 return None; 3462 3463 type = desugared; 3464 } while (true); 3465 } 3466 3467 bool Type::canHaveNullability() const { 3468 QualType type = getCanonicalTypeInternal(); 3469 3470 switch (type->getTypeClass()) { 3471 // We'll only see canonical types here. 3472 #define NON_CANONICAL_TYPE(Class, Parent) \ 3473 case Type::Class: \ 3474 llvm_unreachable("non-canonical type"); 3475 #define TYPE(Class, Parent) 3476 #include "clang/AST/TypeNodes.def" 3477 3478 // Pointer types. 3479 case Type::Pointer: 3480 case Type::BlockPointer: 3481 case Type::MemberPointer: 3482 case Type::ObjCObjectPointer: 3483 return true; 3484 3485 // Dependent types that could instantiate to pointer types. 3486 case Type::UnresolvedUsing: 3487 case Type::TypeOfExpr: 3488 case Type::TypeOf: 3489 case Type::Decltype: 3490 case Type::UnaryTransform: 3491 case Type::TemplateTypeParm: 3492 case Type::SubstTemplateTypeParmPack: 3493 case Type::DependentName: 3494 case Type::DependentTemplateSpecialization: 3495 return true; 3496 3497 // Dependent template specializations can instantiate to pointer 3498 // types unless they're known to be specializations of a class 3499 // template. 3500 case Type::TemplateSpecialization: 3501 if (TemplateDecl *templateDecl 3502 = cast<TemplateSpecializationType>(type.getTypePtr()) 3503 ->getTemplateName().getAsTemplateDecl()) { 3504 if (isa<ClassTemplateDecl>(templateDecl)) 3505 return false; 3506 } 3507 return true; 3508 3509 // auto is considered dependent when it isn't deduced. 3510 case Type::Auto: 3511 return !cast<AutoType>(type.getTypePtr())->isDeduced(); 3512 3513 case Type::Builtin: 3514 switch (cast<BuiltinType>(type.getTypePtr())->getKind()) { 3515 // Signed, unsigned, and floating-point types cannot have nullability. 3516 #define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: 3517 #define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: 3518 #define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id: 3519 #define BUILTIN_TYPE(Id, SingletonId) 3520 #include "clang/AST/BuiltinTypes.def" 3521 return false; 3522 3523 // Dependent types that could instantiate to a pointer type. 3524 case BuiltinType::Dependent: 3525 case BuiltinType::Overload: 3526 case BuiltinType::BoundMember: 3527 case BuiltinType::PseudoObject: 3528 case BuiltinType::UnknownAny: 3529 case BuiltinType::ARCUnbridgedCast: 3530 return true; 3531 3532 case BuiltinType::Void: 3533 case BuiltinType::ObjCId: 3534 case BuiltinType::ObjCClass: 3535 case BuiltinType::ObjCSel: 3536 case BuiltinType::OCLImage1d: 3537 case BuiltinType::OCLImage1dArray: 3538 case BuiltinType::OCLImage1dBuffer: 3539 case BuiltinType::OCLImage2d: 3540 case BuiltinType::OCLImage2dArray: 3541 case BuiltinType::OCLImage2dDepth: 3542 case BuiltinType::OCLImage2dArrayDepth: 3543 case BuiltinType::OCLImage2dMSAA: 3544 case BuiltinType::OCLImage2dArrayMSAA: 3545 case BuiltinType::OCLImage2dMSAADepth: 3546 case BuiltinType::OCLImage2dArrayMSAADepth: 3547 case BuiltinType::OCLImage3d: 3548 case BuiltinType::OCLSampler: 3549 case BuiltinType::OCLEvent: 3550 case BuiltinType::OCLClkEvent: 3551 case BuiltinType::OCLQueue: 3552 case BuiltinType::OCLNDRange: 3553 case BuiltinType::OCLReserveID: 3554 case BuiltinType::BuiltinFn: 3555 case BuiltinType::NullPtr: 3556 case BuiltinType::OMPArraySection: 3557 return false; 3558 } 3559 3560 // Non-pointer types. 3561 case Type::Complex: 3562 case Type::LValueReference: 3563 case Type::RValueReference: 3564 case Type::ConstantArray: 3565 case Type::IncompleteArray: 3566 case Type::VariableArray: 3567 case Type::DependentSizedArray: 3568 case Type::DependentSizedExtVector: 3569 case Type::Vector: 3570 case Type::ExtVector: 3571 case Type::FunctionProto: 3572 case Type::FunctionNoProto: 3573 case Type::Record: 3574 case Type::Enum: 3575 case Type::InjectedClassName: 3576 case Type::PackExpansion: 3577 case Type::ObjCObject: 3578 case Type::ObjCInterface: 3579 case Type::Atomic: 3580 return false; 3581 } 3582 llvm_unreachable("bad type kind!"); 3583 } 3584 3585 llvm::Optional<NullabilityKind> AttributedType::getImmediateNullability() const { 3586 if (getAttrKind() == AttributedType::attr_nonnull) 3587 return NullabilityKind::NonNull; 3588 if (getAttrKind() == AttributedType::attr_nullable) 3589 return NullabilityKind::Nullable; 3590 if (getAttrKind() == AttributedType::attr_null_unspecified) 3591 return NullabilityKind::Unspecified; 3592 return None; 3593 } 3594 3595 Optional<NullabilityKind> AttributedType::stripOuterNullability(QualType &T) { 3596 if (auto attributed = dyn_cast<AttributedType>(T.getTypePtr())) { 3597 if (auto nullability = attributed->getImmediateNullability()) { 3598 T = attributed->getModifiedType(); 3599 return nullability; 3600 } 3601 } 3602 3603 return None; 3604 } 3605 3606 bool Type::isBlockCompatibleObjCPointerType(ASTContext &ctx) const { 3607 const ObjCObjectPointerType *objcPtr = getAs<ObjCObjectPointerType>(); 3608 if (!objcPtr) 3609 return false; 3610 3611 if (objcPtr->isObjCIdType()) { 3612 // id is always okay. 3613 return true; 3614 } 3615 3616 // Blocks are NSObjects. 3617 if (ObjCInterfaceDecl *iface = objcPtr->getInterfaceDecl()) { 3618 if (iface->getIdentifier() != ctx.getNSObjectName()) 3619 return false; 3620 3621 // Continue to check qualifiers, below. 3622 } else if (objcPtr->isObjCQualifiedIdType()) { 3623 // Continue to check qualifiers, below. 3624 } else { 3625 return false; 3626 } 3627 3628 // Check protocol qualifiers. 3629 for (ObjCProtocolDecl *proto : objcPtr->quals()) { 3630 // Blocks conform to NSObject and NSCopying. 3631 if (proto->getIdentifier() != ctx.getNSObjectName() && 3632 proto->getIdentifier() != ctx.getNSCopyingName()) 3633 return false; 3634 } 3635 3636 return true; 3637 } 3638 3639 Qualifiers::ObjCLifetime Type::getObjCARCImplicitLifetime() const { 3640 if (isObjCARCImplicitlyUnretainedType()) 3641 return Qualifiers::OCL_ExplicitNone; 3642 return Qualifiers::OCL_Strong; 3643 } 3644 3645 bool Type::isObjCARCImplicitlyUnretainedType() const { 3646 assert(isObjCLifetimeType() && 3647 "cannot query implicit lifetime for non-inferrable type"); 3648 3649 const Type *canon = getCanonicalTypeInternal().getTypePtr(); 3650 3651 // Walk down to the base type. We don't care about qualifiers for this. 3652 while (const ArrayType *array = dyn_cast<ArrayType>(canon)) 3653 canon = array->getElementType().getTypePtr(); 3654 3655 if (const ObjCObjectPointerType *opt 3656 = dyn_cast<ObjCObjectPointerType>(canon)) { 3657 // Class and Class<Protocol> don't require retention. 3658 if (opt->getObjectType()->isObjCClass()) 3659 return true; 3660 } 3661 3662 return false; 3663 } 3664 3665 bool Type::isObjCNSObjectType() const { 3666 if (const TypedefType *typedefType = dyn_cast<TypedefType>(this)) 3667 return typedefType->getDecl()->hasAttr<ObjCNSObjectAttr>(); 3668 return false; 3669 } 3670 bool Type::isObjCIndependentClassType() const { 3671 if (const TypedefType *typedefType = dyn_cast<TypedefType>(this)) 3672 return typedefType->getDecl()->hasAttr<ObjCIndependentClassAttr>(); 3673 return false; 3674 } 3675 bool Type::isObjCRetainableType() const { 3676 return isObjCObjectPointerType() || 3677 isBlockPointerType() || 3678 isObjCNSObjectType(); 3679 } 3680 bool Type::isObjCIndirectLifetimeType() const { 3681 if (isObjCLifetimeType()) 3682 return true; 3683 if (const PointerType *OPT = getAs<PointerType>()) 3684 return OPT->getPointeeType()->isObjCIndirectLifetimeType(); 3685 if (const ReferenceType *Ref = getAs<ReferenceType>()) 3686 return Ref->getPointeeType()->isObjCIndirectLifetimeType(); 3687 if (const MemberPointerType *MemPtr = getAs<MemberPointerType>()) 3688 return MemPtr->getPointeeType()->isObjCIndirectLifetimeType(); 3689 return false; 3690 } 3691 3692 /// Returns true if objects of this type have lifetime semantics under 3693 /// ARC. 3694 bool Type::isObjCLifetimeType() const { 3695 const Type *type = this; 3696 while (const ArrayType *array = type->getAsArrayTypeUnsafe()) 3697 type = array->getElementType().getTypePtr(); 3698 return type->isObjCRetainableType(); 3699 } 3700 3701 /// \brief Determine whether the given type T is a "bridgable" Objective-C type, 3702 /// which is either an Objective-C object pointer type or an 3703 bool Type::isObjCARCBridgableType() const { 3704 return isObjCObjectPointerType() || isBlockPointerType(); 3705 } 3706 3707 /// \brief Determine whether the given type T is a "bridgeable" C type. 3708 bool Type::isCARCBridgableType() const { 3709 const PointerType *Pointer = getAs<PointerType>(); 3710 if (!Pointer) 3711 return false; 3712 3713 QualType Pointee = Pointer->getPointeeType(); 3714 return Pointee->isVoidType() || Pointee->isRecordType(); 3715 } 3716 3717 bool Type::hasSizedVLAType() const { 3718 if (!isVariablyModifiedType()) return false; 3719 3720 if (const PointerType *ptr = getAs<PointerType>()) 3721 return ptr->getPointeeType()->hasSizedVLAType(); 3722 if (const ReferenceType *ref = getAs<ReferenceType>()) 3723 return ref->getPointeeType()->hasSizedVLAType(); 3724 if (const ArrayType *arr = getAsArrayTypeUnsafe()) { 3725 if (isa<VariableArrayType>(arr) && 3726 cast<VariableArrayType>(arr)->getSizeExpr()) 3727 return true; 3728 3729 return arr->getElementType()->hasSizedVLAType(); 3730 } 3731 3732 return false; 3733 } 3734 3735 QualType::DestructionKind QualType::isDestructedTypeImpl(QualType type) { 3736 switch (type.getObjCLifetime()) { 3737 case Qualifiers::OCL_None: 3738 case Qualifiers::OCL_ExplicitNone: 3739 case Qualifiers::OCL_Autoreleasing: 3740 break; 3741 3742 case Qualifiers::OCL_Strong: 3743 return DK_objc_strong_lifetime; 3744 case Qualifiers::OCL_Weak: 3745 return DK_objc_weak_lifetime; 3746 } 3747 3748 /// Currently, the only destruction kind we recognize is C++ objects 3749 /// with non-trivial destructors. 3750 const CXXRecordDecl *record = 3751 type->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); 3752 if (record && record->hasDefinition() && !record->hasTrivialDestructor()) 3753 return DK_cxx_destructor; 3754 3755 return DK_none; 3756 } 3757 3758 CXXRecordDecl *MemberPointerType::getMostRecentCXXRecordDecl() const { 3759 return getClass()->getAsCXXRecordDecl()->getMostRecentDecl(); 3760 } 3761