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::isMSTypeSpec() const { 2948 switch (getAttrKind()) { 2949 default: return false; 2950 case attr_ptr32: 2951 case attr_ptr64: 2952 case attr_sptr: 2953 case attr_uptr: 2954 return true; 2955 } 2956 llvm_unreachable("invalid attr kind"); 2957 } 2958 2959 bool AttributedType::isCallingConv() const { 2960 switch (getAttrKind()) { 2961 case attr_ptr32: 2962 case attr_ptr64: 2963 case attr_sptr: 2964 case attr_uptr: 2965 case attr_address_space: 2966 case attr_regparm: 2967 case attr_vector_size: 2968 case attr_neon_vector_type: 2969 case attr_neon_polyvector_type: 2970 case attr_objc_gc: 2971 case attr_objc_ownership: 2972 case attr_noreturn: 2973 case attr_nonnull: 2974 case attr_nullable: 2975 case attr_null_unspecified: 2976 case attr_objc_kindof: 2977 return false; 2978 2979 case attr_pcs: 2980 case attr_pcs_vfp: 2981 case attr_cdecl: 2982 case attr_fastcall: 2983 case attr_stdcall: 2984 case attr_thiscall: 2985 case attr_vectorcall: 2986 case attr_pascal: 2987 case attr_ms_abi: 2988 case attr_sysv_abi: 2989 case attr_inteloclbicc: 2990 return true; 2991 } 2992 llvm_unreachable("invalid attr kind"); 2993 } 2994 2995 CXXRecordDecl *InjectedClassNameType::getDecl() const { 2996 return cast<CXXRecordDecl>(getInterestingTagDecl(Decl)); 2997 } 2998 2999 IdentifierInfo *TemplateTypeParmType::getIdentifier() const { 3000 return isCanonicalUnqualified() ? nullptr : getDecl()->getIdentifier(); 3001 } 3002 3003 SubstTemplateTypeParmPackType:: 3004 SubstTemplateTypeParmPackType(const TemplateTypeParmType *Param, 3005 QualType Canon, 3006 const TemplateArgument &ArgPack) 3007 : Type(SubstTemplateTypeParmPack, Canon, true, true, false, true), 3008 Replaced(Param), 3009 Arguments(ArgPack.pack_begin()), NumArguments(ArgPack.pack_size()) 3010 { 3011 } 3012 3013 TemplateArgument SubstTemplateTypeParmPackType::getArgumentPack() const { 3014 return TemplateArgument(llvm::makeArrayRef(Arguments, NumArguments)); 3015 } 3016 3017 void SubstTemplateTypeParmPackType::Profile(llvm::FoldingSetNodeID &ID) { 3018 Profile(ID, getReplacedParameter(), getArgumentPack()); 3019 } 3020 3021 void SubstTemplateTypeParmPackType::Profile(llvm::FoldingSetNodeID &ID, 3022 const TemplateTypeParmType *Replaced, 3023 const TemplateArgument &ArgPack) { 3024 ID.AddPointer(Replaced); 3025 ID.AddInteger(ArgPack.pack_size()); 3026 for (const auto &P : ArgPack.pack_elements()) 3027 ID.AddPointer(P.getAsType().getAsOpaquePtr()); 3028 } 3029 3030 bool TemplateSpecializationType:: 3031 anyDependentTemplateArguments(const TemplateArgumentListInfo &Args, 3032 bool &InstantiationDependent) { 3033 return anyDependentTemplateArguments(Args.getArgumentArray(), Args.size(), 3034 InstantiationDependent); 3035 } 3036 3037 bool TemplateSpecializationType:: 3038 anyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N, 3039 bool &InstantiationDependent) { 3040 for (unsigned i = 0; i != N; ++i) { 3041 if (Args[i].getArgument().isDependent()) { 3042 InstantiationDependent = true; 3043 return true; 3044 } 3045 3046 if (Args[i].getArgument().isInstantiationDependent()) 3047 InstantiationDependent = true; 3048 } 3049 return false; 3050 } 3051 3052 TemplateSpecializationType:: 3053 TemplateSpecializationType(TemplateName T, 3054 const TemplateArgument *Args, unsigned NumArgs, 3055 QualType Canon, QualType AliasedType) 3056 : Type(TemplateSpecialization, 3057 Canon.isNull()? QualType(this, 0) : Canon, 3058 Canon.isNull()? true : Canon->isDependentType(), 3059 Canon.isNull()? true : Canon->isInstantiationDependentType(), 3060 false, 3061 T.containsUnexpandedParameterPack()), 3062 Template(T), NumArgs(NumArgs), TypeAlias(!AliasedType.isNull()) { 3063 assert(!T.getAsDependentTemplateName() && 3064 "Use DependentTemplateSpecializationType for dependent template-name"); 3065 assert((T.getKind() == TemplateName::Template || 3066 T.getKind() == TemplateName::SubstTemplateTemplateParm || 3067 T.getKind() == TemplateName::SubstTemplateTemplateParmPack) && 3068 "Unexpected template name for TemplateSpecializationType"); 3069 3070 TemplateArgument *TemplateArgs 3071 = reinterpret_cast<TemplateArgument *>(this + 1); 3072 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 3073 // Update instantiation-dependent and variably-modified bits. 3074 // If the canonical type exists and is non-dependent, the template 3075 // specialization type can be non-dependent even if one of the type 3076 // arguments is. Given: 3077 // template<typename T> using U = int; 3078 // U<T> is always non-dependent, irrespective of the type T. 3079 // However, U<Ts> contains an unexpanded parameter pack, even though 3080 // its expansion (and thus its desugared type) doesn't. 3081 if (Args[Arg].isInstantiationDependent()) 3082 setInstantiationDependent(); 3083 if (Args[Arg].getKind() == TemplateArgument::Type && 3084 Args[Arg].getAsType()->isVariablyModifiedType()) 3085 setVariablyModified(); 3086 if (Args[Arg].containsUnexpandedParameterPack()) 3087 setContainsUnexpandedParameterPack(); 3088 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]); 3089 } 3090 3091 // Store the aliased type if this is a type alias template specialization. 3092 if (TypeAlias) { 3093 TemplateArgument *Begin = reinterpret_cast<TemplateArgument *>(this + 1); 3094 *reinterpret_cast<QualType*>(Begin + getNumArgs()) = AliasedType; 3095 } 3096 } 3097 3098 void 3099 TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID, 3100 TemplateName T, 3101 const TemplateArgument *Args, 3102 unsigned NumArgs, 3103 const ASTContext &Context) { 3104 T.Profile(ID); 3105 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) 3106 Args[Idx].Profile(ID, Context); 3107 } 3108 3109 QualType 3110 QualifierCollector::apply(const ASTContext &Context, QualType QT) const { 3111 if (!hasNonFastQualifiers()) 3112 return QT.withFastQualifiers(getFastQualifiers()); 3113 3114 return Context.getQualifiedType(QT, *this); 3115 } 3116 3117 QualType 3118 QualifierCollector::apply(const ASTContext &Context, const Type *T) const { 3119 if (!hasNonFastQualifiers()) 3120 return QualType(T, getFastQualifiers()); 3121 3122 return Context.getQualifiedType(T, *this); 3123 } 3124 3125 void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID, 3126 QualType BaseType, 3127 ArrayRef<QualType> typeArgs, 3128 ArrayRef<ObjCProtocolDecl *> protocols, 3129 bool isKindOf) { 3130 ID.AddPointer(BaseType.getAsOpaquePtr()); 3131 ID.AddInteger(typeArgs.size()); 3132 for (auto typeArg : typeArgs) 3133 ID.AddPointer(typeArg.getAsOpaquePtr()); 3134 ID.AddInteger(protocols.size()); 3135 for (auto proto : protocols) 3136 ID.AddPointer(proto); 3137 ID.AddBoolean(isKindOf); 3138 } 3139 3140 void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID) { 3141 Profile(ID, getBaseType(), getTypeArgsAsWritten(), 3142 llvm::makeArrayRef(qual_begin(), getNumProtocols()), 3143 isKindOfTypeAsWritten()); 3144 } 3145 3146 namespace { 3147 3148 /// \brief The cached properties of a type. 3149 class CachedProperties { 3150 Linkage L; 3151 bool local; 3152 3153 public: 3154 CachedProperties(Linkage L, bool local) : L(L), local(local) {} 3155 3156 Linkage getLinkage() const { return L; } 3157 bool hasLocalOrUnnamedType() const { return local; } 3158 3159 friend CachedProperties merge(CachedProperties L, CachedProperties R) { 3160 Linkage MergedLinkage = minLinkage(L.L, R.L); 3161 return CachedProperties(MergedLinkage, 3162 L.hasLocalOrUnnamedType() | R.hasLocalOrUnnamedType()); 3163 } 3164 }; 3165 } 3166 3167 static CachedProperties computeCachedProperties(const Type *T); 3168 3169 namespace clang { 3170 /// The type-property cache. This is templated so as to be 3171 /// instantiated at an internal type to prevent unnecessary symbol 3172 /// leakage. 3173 template <class Private> class TypePropertyCache { 3174 public: 3175 static CachedProperties get(QualType T) { 3176 return get(T.getTypePtr()); 3177 } 3178 3179 static CachedProperties get(const Type *T) { 3180 ensure(T); 3181 return CachedProperties(T->TypeBits.getLinkage(), 3182 T->TypeBits.hasLocalOrUnnamedType()); 3183 } 3184 3185 static void ensure(const Type *T) { 3186 // If the cache is valid, we're okay. 3187 if (T->TypeBits.isCacheValid()) return; 3188 3189 // If this type is non-canonical, ask its canonical type for the 3190 // relevant information. 3191 if (!T->isCanonicalUnqualified()) { 3192 const Type *CT = T->getCanonicalTypeInternal().getTypePtr(); 3193 ensure(CT); 3194 T->TypeBits.CacheValid = true; 3195 T->TypeBits.CachedLinkage = CT->TypeBits.CachedLinkage; 3196 T->TypeBits.CachedLocalOrUnnamed = CT->TypeBits.CachedLocalOrUnnamed; 3197 return; 3198 } 3199 3200 // Compute the cached properties and then set the cache. 3201 CachedProperties Result = computeCachedProperties(T); 3202 T->TypeBits.CacheValid = true; 3203 T->TypeBits.CachedLinkage = Result.getLinkage(); 3204 T->TypeBits.CachedLocalOrUnnamed = Result.hasLocalOrUnnamedType(); 3205 } 3206 }; 3207 } 3208 3209 // Instantiate the friend template at a private class. In a 3210 // reasonable implementation, these symbols will be internal. 3211 // It is terrible that this is the best way to accomplish this. 3212 namespace { class Private {}; } 3213 typedef TypePropertyCache<Private> Cache; 3214 3215 static CachedProperties computeCachedProperties(const Type *T) { 3216 switch (T->getTypeClass()) { 3217 #define TYPE(Class,Base) 3218 #define NON_CANONICAL_TYPE(Class,Base) case Type::Class: 3219 #include "clang/AST/TypeNodes.def" 3220 llvm_unreachable("didn't expect a non-canonical type here"); 3221 3222 #define TYPE(Class,Base) 3223 #define DEPENDENT_TYPE(Class,Base) case Type::Class: 3224 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class,Base) case Type::Class: 3225 #include "clang/AST/TypeNodes.def" 3226 // Treat instantiation-dependent types as external. 3227 assert(T->isInstantiationDependentType()); 3228 return CachedProperties(ExternalLinkage, false); 3229 3230 case Type::Auto: 3231 // Give non-deduced 'auto' types external linkage. We should only see them 3232 // here in error recovery. 3233 return CachedProperties(ExternalLinkage, false); 3234 3235 case Type::Builtin: 3236 // C++ [basic.link]p8: 3237 // A type is said to have linkage if and only if: 3238 // - it is a fundamental type (3.9.1); or 3239 return CachedProperties(ExternalLinkage, false); 3240 3241 case Type::Record: 3242 case Type::Enum: { 3243 const TagDecl *Tag = cast<TagType>(T)->getDecl(); 3244 3245 // C++ [basic.link]p8: 3246 // - it is a class or enumeration type that is named (or has a name 3247 // for linkage purposes (7.1.3)) and the name has linkage; or 3248 // - it is a specialization of a class template (14); or 3249 Linkage L = Tag->getLinkageInternal(); 3250 bool IsLocalOrUnnamed = 3251 Tag->getDeclContext()->isFunctionOrMethod() || 3252 !Tag->hasNameForLinkage(); 3253 return CachedProperties(L, IsLocalOrUnnamed); 3254 } 3255 3256 // C++ [basic.link]p8: 3257 // - it is a compound type (3.9.2) other than a class or enumeration, 3258 // compounded exclusively from types that have linkage; or 3259 case Type::Complex: 3260 return Cache::get(cast<ComplexType>(T)->getElementType()); 3261 case Type::Pointer: 3262 return Cache::get(cast<PointerType>(T)->getPointeeType()); 3263 case Type::BlockPointer: 3264 return Cache::get(cast<BlockPointerType>(T)->getPointeeType()); 3265 case Type::LValueReference: 3266 case Type::RValueReference: 3267 return Cache::get(cast<ReferenceType>(T)->getPointeeType()); 3268 case Type::MemberPointer: { 3269 const MemberPointerType *MPT = cast<MemberPointerType>(T); 3270 return merge(Cache::get(MPT->getClass()), 3271 Cache::get(MPT->getPointeeType())); 3272 } 3273 case Type::ConstantArray: 3274 case Type::IncompleteArray: 3275 case Type::VariableArray: 3276 return Cache::get(cast<ArrayType>(T)->getElementType()); 3277 case Type::Vector: 3278 case Type::ExtVector: 3279 return Cache::get(cast<VectorType>(T)->getElementType()); 3280 case Type::FunctionNoProto: 3281 return Cache::get(cast<FunctionType>(T)->getReturnType()); 3282 case Type::FunctionProto: { 3283 const FunctionProtoType *FPT = cast<FunctionProtoType>(T); 3284 CachedProperties result = Cache::get(FPT->getReturnType()); 3285 for (const auto &ai : FPT->param_types()) 3286 result = merge(result, Cache::get(ai)); 3287 return result; 3288 } 3289 case Type::ObjCInterface: { 3290 Linkage L = cast<ObjCInterfaceType>(T)->getDecl()->getLinkageInternal(); 3291 return CachedProperties(L, false); 3292 } 3293 case Type::ObjCObject: 3294 return Cache::get(cast<ObjCObjectType>(T)->getBaseType()); 3295 case Type::ObjCObjectPointer: 3296 return Cache::get(cast<ObjCObjectPointerType>(T)->getPointeeType()); 3297 case Type::Atomic: 3298 return Cache::get(cast<AtomicType>(T)->getValueType()); 3299 } 3300 3301 llvm_unreachable("unhandled type class"); 3302 } 3303 3304 /// \brief Determine the linkage of this type. 3305 Linkage Type::getLinkage() const { 3306 Cache::ensure(this); 3307 return TypeBits.getLinkage(); 3308 } 3309 3310 bool Type::hasUnnamedOrLocalType() const { 3311 Cache::ensure(this); 3312 return TypeBits.hasLocalOrUnnamedType(); 3313 } 3314 3315 static LinkageInfo computeLinkageInfo(QualType T); 3316 3317 static LinkageInfo computeLinkageInfo(const Type *T) { 3318 switch (T->getTypeClass()) { 3319 #define TYPE(Class,Base) 3320 #define NON_CANONICAL_TYPE(Class,Base) case Type::Class: 3321 #include "clang/AST/TypeNodes.def" 3322 llvm_unreachable("didn't expect a non-canonical type here"); 3323 3324 #define TYPE(Class,Base) 3325 #define DEPENDENT_TYPE(Class,Base) case Type::Class: 3326 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class,Base) case Type::Class: 3327 #include "clang/AST/TypeNodes.def" 3328 // Treat instantiation-dependent types as external. 3329 assert(T->isInstantiationDependentType()); 3330 return LinkageInfo::external(); 3331 3332 case Type::Builtin: 3333 return LinkageInfo::external(); 3334 3335 case Type::Auto: 3336 return LinkageInfo::external(); 3337 3338 case Type::Record: 3339 case Type::Enum: 3340 return cast<TagType>(T)->getDecl()->getLinkageAndVisibility(); 3341 3342 case Type::Complex: 3343 return computeLinkageInfo(cast<ComplexType>(T)->getElementType()); 3344 case Type::Pointer: 3345 return computeLinkageInfo(cast<PointerType>(T)->getPointeeType()); 3346 case Type::BlockPointer: 3347 return computeLinkageInfo(cast<BlockPointerType>(T)->getPointeeType()); 3348 case Type::LValueReference: 3349 case Type::RValueReference: 3350 return computeLinkageInfo(cast<ReferenceType>(T)->getPointeeType()); 3351 case Type::MemberPointer: { 3352 const MemberPointerType *MPT = cast<MemberPointerType>(T); 3353 LinkageInfo LV = computeLinkageInfo(MPT->getClass()); 3354 LV.merge(computeLinkageInfo(MPT->getPointeeType())); 3355 return LV; 3356 } 3357 case Type::ConstantArray: 3358 case Type::IncompleteArray: 3359 case Type::VariableArray: 3360 return computeLinkageInfo(cast<ArrayType>(T)->getElementType()); 3361 case Type::Vector: 3362 case Type::ExtVector: 3363 return computeLinkageInfo(cast<VectorType>(T)->getElementType()); 3364 case Type::FunctionNoProto: 3365 return computeLinkageInfo(cast<FunctionType>(T)->getReturnType()); 3366 case Type::FunctionProto: { 3367 const FunctionProtoType *FPT = cast<FunctionProtoType>(T); 3368 LinkageInfo LV = computeLinkageInfo(FPT->getReturnType()); 3369 for (const auto &ai : FPT->param_types()) 3370 LV.merge(computeLinkageInfo(ai)); 3371 return LV; 3372 } 3373 case Type::ObjCInterface: 3374 return cast<ObjCInterfaceType>(T)->getDecl()->getLinkageAndVisibility(); 3375 case Type::ObjCObject: 3376 return computeLinkageInfo(cast<ObjCObjectType>(T)->getBaseType()); 3377 case Type::ObjCObjectPointer: 3378 return computeLinkageInfo(cast<ObjCObjectPointerType>(T)->getPointeeType()); 3379 case Type::Atomic: 3380 return computeLinkageInfo(cast<AtomicType>(T)->getValueType()); 3381 } 3382 3383 llvm_unreachable("unhandled type class"); 3384 } 3385 3386 static LinkageInfo computeLinkageInfo(QualType T) { 3387 return computeLinkageInfo(T.getTypePtr()); 3388 } 3389 3390 bool Type::isLinkageValid() const { 3391 if (!TypeBits.isCacheValid()) 3392 return true; 3393 3394 return computeLinkageInfo(getCanonicalTypeInternal()).getLinkage() == 3395 TypeBits.getLinkage(); 3396 } 3397 3398 LinkageInfo Type::getLinkageAndVisibility() const { 3399 if (!isCanonicalUnqualified()) 3400 return computeLinkageInfo(getCanonicalTypeInternal()); 3401 3402 LinkageInfo LV = computeLinkageInfo(this); 3403 assert(LV.getLinkage() == getLinkage()); 3404 return LV; 3405 } 3406 3407 Optional<NullabilityKind> Type::getNullability(const ASTContext &context) const { 3408 QualType type(this, 0); 3409 do { 3410 // Check whether this is an attributed type with nullability 3411 // information. 3412 if (auto attributed = dyn_cast<AttributedType>(type.getTypePtr())) { 3413 if (auto nullability = attributed->getImmediateNullability()) 3414 return nullability; 3415 } 3416 3417 // Desugar the type. If desugaring does nothing, we're done. 3418 QualType desugared = type.getSingleStepDesugaredType(context); 3419 if (desugared.getTypePtr() == type.getTypePtr()) 3420 return None; 3421 3422 type = desugared; 3423 } while (true); 3424 } 3425 3426 bool Type::canHaveNullability() const { 3427 QualType type = getCanonicalTypeInternal(); 3428 3429 switch (type->getTypeClass()) { 3430 // We'll only see canonical types here. 3431 #define NON_CANONICAL_TYPE(Class, Parent) \ 3432 case Type::Class: \ 3433 llvm_unreachable("non-canonical type"); 3434 #define TYPE(Class, Parent) 3435 #include "clang/AST/TypeNodes.def" 3436 3437 // Pointer types. 3438 case Type::Pointer: 3439 case Type::BlockPointer: 3440 case Type::MemberPointer: 3441 case Type::ObjCObjectPointer: 3442 return true; 3443 3444 // Dependent types that could instantiate to pointer types. 3445 case Type::UnresolvedUsing: 3446 case Type::TypeOfExpr: 3447 case Type::TypeOf: 3448 case Type::Decltype: 3449 case Type::UnaryTransform: 3450 case Type::TemplateTypeParm: 3451 case Type::SubstTemplateTypeParmPack: 3452 case Type::DependentName: 3453 case Type::DependentTemplateSpecialization: 3454 return true; 3455 3456 // Dependent template specializations can instantiate to pointer 3457 // types unless they're known to be specializations of a class 3458 // template. 3459 case Type::TemplateSpecialization: 3460 if (TemplateDecl *templateDecl 3461 = cast<TemplateSpecializationType>(type.getTypePtr()) 3462 ->getTemplateName().getAsTemplateDecl()) { 3463 if (isa<ClassTemplateDecl>(templateDecl)) 3464 return false; 3465 } 3466 return true; 3467 3468 // auto is considered dependent when it isn't deduced. 3469 case Type::Auto: 3470 return !cast<AutoType>(type.getTypePtr())->isDeduced(); 3471 3472 case Type::Builtin: 3473 switch (cast<BuiltinType>(type.getTypePtr())->getKind()) { 3474 // Signed, unsigned, and floating-point types cannot have nullability. 3475 #define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: 3476 #define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: 3477 #define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id: 3478 #define BUILTIN_TYPE(Id, SingletonId) 3479 #include "clang/AST/BuiltinTypes.def" 3480 return false; 3481 3482 // Dependent types that could instantiate to a pointer type. 3483 case BuiltinType::Dependent: 3484 case BuiltinType::Overload: 3485 case BuiltinType::BoundMember: 3486 case BuiltinType::PseudoObject: 3487 case BuiltinType::UnknownAny: 3488 case BuiltinType::ARCUnbridgedCast: 3489 return true; 3490 3491 case BuiltinType::Void: 3492 case BuiltinType::ObjCId: 3493 case BuiltinType::ObjCClass: 3494 case BuiltinType::ObjCSel: 3495 case BuiltinType::OCLImage1d: 3496 case BuiltinType::OCLImage1dArray: 3497 case BuiltinType::OCLImage1dBuffer: 3498 case BuiltinType::OCLImage2d: 3499 case BuiltinType::OCLImage2dArray: 3500 case BuiltinType::OCLImage2dDepth: 3501 case BuiltinType::OCLImage2dArrayDepth: 3502 case BuiltinType::OCLImage2dMSAA: 3503 case BuiltinType::OCLImage2dArrayMSAA: 3504 case BuiltinType::OCLImage2dMSAADepth: 3505 case BuiltinType::OCLImage2dArrayMSAADepth: 3506 case BuiltinType::OCLImage3d: 3507 case BuiltinType::OCLSampler: 3508 case BuiltinType::OCLEvent: 3509 case BuiltinType::OCLClkEvent: 3510 case BuiltinType::OCLQueue: 3511 case BuiltinType::OCLNDRange: 3512 case BuiltinType::OCLReserveID: 3513 case BuiltinType::BuiltinFn: 3514 case BuiltinType::NullPtr: 3515 case BuiltinType::OMPArraySection: 3516 return false; 3517 } 3518 3519 // Non-pointer types. 3520 case Type::Complex: 3521 case Type::LValueReference: 3522 case Type::RValueReference: 3523 case Type::ConstantArray: 3524 case Type::IncompleteArray: 3525 case Type::VariableArray: 3526 case Type::DependentSizedArray: 3527 case Type::DependentSizedExtVector: 3528 case Type::Vector: 3529 case Type::ExtVector: 3530 case Type::FunctionProto: 3531 case Type::FunctionNoProto: 3532 case Type::Record: 3533 case Type::Enum: 3534 case Type::InjectedClassName: 3535 case Type::PackExpansion: 3536 case Type::ObjCObject: 3537 case Type::ObjCInterface: 3538 case Type::Atomic: 3539 return false; 3540 } 3541 llvm_unreachable("bad type kind!"); 3542 } 3543 3544 llvm::Optional<NullabilityKind> AttributedType::getImmediateNullability() const { 3545 if (getAttrKind() == AttributedType::attr_nonnull) 3546 return NullabilityKind::NonNull; 3547 if (getAttrKind() == AttributedType::attr_nullable) 3548 return NullabilityKind::Nullable; 3549 if (getAttrKind() == AttributedType::attr_null_unspecified) 3550 return NullabilityKind::Unspecified; 3551 return None; 3552 } 3553 3554 Optional<NullabilityKind> AttributedType::stripOuterNullability(QualType &T) { 3555 if (auto attributed = dyn_cast<AttributedType>(T.getTypePtr())) { 3556 if (auto nullability = attributed->getImmediateNullability()) { 3557 T = attributed->getModifiedType(); 3558 return nullability; 3559 } 3560 } 3561 3562 return None; 3563 } 3564 3565 bool Type::isBlockCompatibleObjCPointerType(ASTContext &ctx) const { 3566 const ObjCObjectPointerType *objcPtr = getAs<ObjCObjectPointerType>(); 3567 if (!objcPtr) 3568 return false; 3569 3570 if (objcPtr->isObjCIdType()) { 3571 // id is always okay. 3572 return true; 3573 } 3574 3575 // Blocks are NSObjects. 3576 if (ObjCInterfaceDecl *iface = objcPtr->getInterfaceDecl()) { 3577 if (iface->getIdentifier() != ctx.getNSObjectName()) 3578 return false; 3579 3580 // Continue to check qualifiers, below. 3581 } else if (objcPtr->isObjCQualifiedIdType()) { 3582 // Continue to check qualifiers, below. 3583 } else { 3584 return false; 3585 } 3586 3587 // Check protocol qualifiers. 3588 for (ObjCProtocolDecl *proto : objcPtr->quals()) { 3589 // Blocks conform to NSObject and NSCopying. 3590 if (proto->getIdentifier() != ctx.getNSObjectName() && 3591 proto->getIdentifier() != ctx.getNSCopyingName()) 3592 return false; 3593 } 3594 3595 return true; 3596 } 3597 3598 Qualifiers::ObjCLifetime Type::getObjCARCImplicitLifetime() const { 3599 if (isObjCARCImplicitlyUnretainedType()) 3600 return Qualifiers::OCL_ExplicitNone; 3601 return Qualifiers::OCL_Strong; 3602 } 3603 3604 bool Type::isObjCARCImplicitlyUnretainedType() const { 3605 assert(isObjCLifetimeType() && 3606 "cannot query implicit lifetime for non-inferrable type"); 3607 3608 const Type *canon = getCanonicalTypeInternal().getTypePtr(); 3609 3610 // Walk down to the base type. We don't care about qualifiers for this. 3611 while (const ArrayType *array = dyn_cast<ArrayType>(canon)) 3612 canon = array->getElementType().getTypePtr(); 3613 3614 if (const ObjCObjectPointerType *opt 3615 = dyn_cast<ObjCObjectPointerType>(canon)) { 3616 // Class and Class<Protocol> don't require retention. 3617 if (opt->getObjectType()->isObjCClass()) 3618 return true; 3619 } 3620 3621 return false; 3622 } 3623 3624 bool Type::isObjCNSObjectType() const { 3625 if (const TypedefType *typedefType = dyn_cast<TypedefType>(this)) 3626 return typedefType->getDecl()->hasAttr<ObjCNSObjectAttr>(); 3627 return false; 3628 } 3629 bool Type::isObjCIndependentClassType() const { 3630 if (const TypedefType *typedefType = dyn_cast<TypedefType>(this)) 3631 return typedefType->getDecl()->hasAttr<ObjCIndependentClassAttr>(); 3632 return false; 3633 } 3634 bool Type::isObjCRetainableType() const { 3635 return isObjCObjectPointerType() || 3636 isBlockPointerType() || 3637 isObjCNSObjectType(); 3638 } 3639 bool Type::isObjCIndirectLifetimeType() const { 3640 if (isObjCLifetimeType()) 3641 return true; 3642 if (const PointerType *OPT = getAs<PointerType>()) 3643 return OPT->getPointeeType()->isObjCIndirectLifetimeType(); 3644 if (const ReferenceType *Ref = getAs<ReferenceType>()) 3645 return Ref->getPointeeType()->isObjCIndirectLifetimeType(); 3646 if (const MemberPointerType *MemPtr = getAs<MemberPointerType>()) 3647 return MemPtr->getPointeeType()->isObjCIndirectLifetimeType(); 3648 return false; 3649 } 3650 3651 /// Returns true if objects of this type have lifetime semantics under 3652 /// ARC. 3653 bool Type::isObjCLifetimeType() const { 3654 const Type *type = this; 3655 while (const ArrayType *array = type->getAsArrayTypeUnsafe()) 3656 type = array->getElementType().getTypePtr(); 3657 return type->isObjCRetainableType(); 3658 } 3659 3660 /// \brief Determine whether the given type T is a "bridgable" Objective-C type, 3661 /// which is either an Objective-C object pointer type or an 3662 bool Type::isObjCARCBridgableType() const { 3663 return isObjCObjectPointerType() || isBlockPointerType(); 3664 } 3665 3666 /// \brief Determine whether the given type T is a "bridgeable" C type. 3667 bool Type::isCARCBridgableType() const { 3668 const PointerType *Pointer = getAs<PointerType>(); 3669 if (!Pointer) 3670 return false; 3671 3672 QualType Pointee = Pointer->getPointeeType(); 3673 return Pointee->isVoidType() || Pointee->isRecordType(); 3674 } 3675 3676 bool Type::hasSizedVLAType() const { 3677 if (!isVariablyModifiedType()) return false; 3678 3679 if (const PointerType *ptr = getAs<PointerType>()) 3680 return ptr->getPointeeType()->hasSizedVLAType(); 3681 if (const ReferenceType *ref = getAs<ReferenceType>()) 3682 return ref->getPointeeType()->hasSizedVLAType(); 3683 if (const ArrayType *arr = getAsArrayTypeUnsafe()) { 3684 if (isa<VariableArrayType>(arr) && 3685 cast<VariableArrayType>(arr)->getSizeExpr()) 3686 return true; 3687 3688 return arr->getElementType()->hasSizedVLAType(); 3689 } 3690 3691 return false; 3692 } 3693 3694 QualType::DestructionKind QualType::isDestructedTypeImpl(QualType type) { 3695 switch (type.getObjCLifetime()) { 3696 case Qualifiers::OCL_None: 3697 case Qualifiers::OCL_ExplicitNone: 3698 case Qualifiers::OCL_Autoreleasing: 3699 break; 3700 3701 case Qualifiers::OCL_Strong: 3702 return DK_objc_strong_lifetime; 3703 case Qualifiers::OCL_Weak: 3704 return DK_objc_weak_lifetime; 3705 } 3706 3707 /// Currently, the only destruction kind we recognize is C++ objects 3708 /// with non-trivial destructors. 3709 const CXXRecordDecl *record = 3710 type->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); 3711 if (record && record->hasDefinition() && !record->hasTrivialDestructor()) 3712 return DK_cxx_destructor; 3713 3714 return DK_none; 3715 } 3716 3717 CXXRecordDecl *MemberPointerType::getMostRecentCXXRecordDecl() const { 3718 return getClass()->getAsCXXRecordDecl()->getMostRecentDecl(); 3719 } 3720