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