1 //===- Type.h - C Language Family Type Representation -----------*- C++ -*-===// 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 /// \file 10 /// C Language Family Type Representation 11 /// 12 /// This file defines the clang::Type interface and subclasses, used to 13 /// represent types for languages in the C family. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #ifndef LLVM_CLANG_AST_TYPE_H 18 #define LLVM_CLANG_AST_TYPE_H 19 20 #include "clang/AST/DependenceFlags.h" 21 #include "clang/AST/NestedNameSpecifier.h" 22 #include "clang/AST/TemplateName.h" 23 #include "clang/Basic/AddressSpaces.h" 24 #include "clang/Basic/AttrKinds.h" 25 #include "clang/Basic/Diagnostic.h" 26 #include "clang/Basic/ExceptionSpecificationType.h" 27 #include "clang/Basic/LLVM.h" 28 #include "clang/Basic/LangOptions.h" 29 #include "clang/Basic/Linkage.h" 30 #include "clang/Basic/PartialDiagnostic.h" 31 #include "clang/Basic/PointerAuthOptions.h" 32 #include "clang/Basic/SourceLocation.h" 33 #include "clang/Basic/Specifiers.h" 34 #include "clang/Basic/Visibility.h" 35 #include "llvm/ADT/APInt.h" 36 #include "llvm/ADT/APSInt.h" 37 #include "llvm/ADT/ArrayRef.h" 38 #include "llvm/ADT/FoldingSet.h" 39 #include "llvm/ADT/PointerIntPair.h" 40 #include "llvm/ADT/PointerUnion.h" 41 #include "llvm/ADT/STLForwardCompat.h" 42 #include "llvm/ADT/StringRef.h" 43 #include "llvm/ADT/Twine.h" 44 #include "llvm/ADT/iterator_range.h" 45 #include "llvm/Support/Casting.h" 46 #include "llvm/Support/Compiler.h" 47 #include "llvm/Support/DXILABI.h" 48 #include "llvm/Support/ErrorHandling.h" 49 #include "llvm/Support/PointerLikeTypeTraits.h" 50 #include "llvm/Support/TrailingObjects.h" 51 #include "llvm/Support/type_traits.h" 52 #include <bitset> 53 #include <cassert> 54 #include <cstddef> 55 #include <cstdint> 56 #include <cstring> 57 #include <optional> 58 #include <string> 59 #include <type_traits> 60 #include <utility> 61 62 namespace clang { 63 64 class BTFTypeTagAttr; 65 class ExtQuals; 66 class QualType; 67 class ConceptDecl; 68 class ValueDecl; 69 class TagDecl; 70 class TemplateParameterList; 71 class Type; 72 class Attr; 73 74 enum { 75 TypeAlignmentInBits = 4, 76 TypeAlignment = 1 << TypeAlignmentInBits 77 }; 78 79 namespace serialization { 80 template <class T> class AbstractTypeReader; 81 template <class T> class AbstractTypeWriter; 82 } 83 84 } // namespace clang 85 86 namespace llvm { 87 88 template <typename T> 89 struct PointerLikeTypeTraits; 90 template<> 91 struct PointerLikeTypeTraits< ::clang::Type*> { 92 static inline void *getAsVoidPointer(::clang::Type *P) { return P; } 93 94 static inline ::clang::Type *getFromVoidPointer(void *P) { 95 return static_cast< ::clang::Type*>(P); 96 } 97 98 static constexpr int NumLowBitsAvailable = clang::TypeAlignmentInBits; 99 }; 100 101 template<> 102 struct PointerLikeTypeTraits< ::clang::ExtQuals*> { 103 static inline void *getAsVoidPointer(::clang::ExtQuals *P) { return P; } 104 105 static inline ::clang::ExtQuals *getFromVoidPointer(void *P) { 106 return static_cast< ::clang::ExtQuals*>(P); 107 } 108 109 static constexpr int NumLowBitsAvailable = clang::TypeAlignmentInBits; 110 }; 111 112 } // namespace llvm 113 114 namespace clang { 115 116 class ASTContext; 117 template <typename> class CanQual; 118 class CXXRecordDecl; 119 class DeclContext; 120 class EnumDecl; 121 class Expr; 122 class ExtQualsTypeCommonBase; 123 class FunctionDecl; 124 class FunctionEffectsRef; 125 class FunctionEffectKindSet; 126 class FunctionEffectSet; 127 class IdentifierInfo; 128 class NamedDecl; 129 class ObjCInterfaceDecl; 130 class ObjCProtocolDecl; 131 class ObjCTypeParamDecl; 132 struct PrintingPolicy; 133 class RecordDecl; 134 class Stmt; 135 class TagDecl; 136 class TemplateArgument; 137 class TemplateArgumentListInfo; 138 class TemplateArgumentLoc; 139 class TemplateTypeParmDecl; 140 class TypedefNameDecl; 141 class UnresolvedUsingTypenameDecl; 142 class UsingShadowDecl; 143 144 using CanQualType = CanQual<Type>; 145 146 // Provide forward declarations for all of the *Type classes. 147 #define TYPE(Class, Base) class Class##Type; 148 #include "clang/AST/TypeNodes.inc" 149 150 /// Pointer-authentication qualifiers. 151 class PointerAuthQualifier { 152 enum : uint32_t { 153 EnabledShift = 0, 154 EnabledBits = 1, 155 EnabledMask = 1 << EnabledShift, 156 AddressDiscriminatedShift = EnabledShift + EnabledBits, 157 AddressDiscriminatedBits = 1, 158 AddressDiscriminatedMask = 1 << AddressDiscriminatedShift, 159 AuthenticationModeShift = 160 AddressDiscriminatedShift + AddressDiscriminatedBits, 161 AuthenticationModeBits = 2, 162 AuthenticationModeMask = ((1 << AuthenticationModeBits) - 1) 163 << AuthenticationModeShift, 164 IsaPointerShift = AuthenticationModeShift + AuthenticationModeBits, 165 IsaPointerBits = 1, 166 IsaPointerMask = ((1 << IsaPointerBits) - 1) << IsaPointerShift, 167 AuthenticatesNullValuesShift = IsaPointerShift + IsaPointerBits, 168 AuthenticatesNullValuesBits = 1, 169 AuthenticatesNullValuesMask = ((1 << AuthenticatesNullValuesBits) - 1) 170 << AuthenticatesNullValuesShift, 171 KeyShift = AuthenticatesNullValuesShift + AuthenticatesNullValuesBits, 172 KeyBits = 10, 173 KeyMask = ((1 << KeyBits) - 1) << KeyShift, 174 DiscriminatorShift = KeyShift + KeyBits, 175 DiscriminatorBits = 16, 176 DiscriminatorMask = ((1u << DiscriminatorBits) - 1) << DiscriminatorShift, 177 }; 178 179 // bits: |0 |1 |2..3 |4 | 180 // |Enabled|Address|AuthenticationMode|ISA pointer| 181 // bits: |5 |6..15| 16...31 | 182 // |AuthenticatesNull|Key |Discriminator| 183 uint32_t Data = 0; 184 185 // The following static assertions check that each of the 32 bits is present 186 // exactly in one of the constants. 187 static_assert((EnabledBits + AddressDiscriminatedBits + 188 AuthenticationModeBits + IsaPointerBits + 189 AuthenticatesNullValuesBits + KeyBits + DiscriminatorBits) == 190 32, 191 "PointerAuthQualifier should be exactly 32 bits"); 192 static_assert((EnabledMask + AddressDiscriminatedMask + 193 AuthenticationModeMask + IsaPointerMask + 194 AuthenticatesNullValuesMask + KeyMask + DiscriminatorMask) == 195 0xFFFFFFFF, 196 "All masks should cover the entire bits"); 197 static_assert((EnabledMask ^ AddressDiscriminatedMask ^ 198 AuthenticationModeMask ^ IsaPointerMask ^ 199 AuthenticatesNullValuesMask ^ KeyMask ^ DiscriminatorMask) == 200 0xFFFFFFFF, 201 "All masks should cover the entire bits"); 202 203 PointerAuthQualifier(unsigned Key, bool IsAddressDiscriminated, 204 unsigned ExtraDiscriminator, 205 PointerAuthenticationMode AuthenticationMode, 206 bool IsIsaPointer, bool AuthenticatesNullValues) 207 : Data(EnabledMask | 208 (IsAddressDiscriminated 209 ? llvm::to_underlying(AddressDiscriminatedMask) 210 : 0) | 211 (Key << KeyShift) | 212 (llvm::to_underlying(AuthenticationMode) 213 << AuthenticationModeShift) | 214 (ExtraDiscriminator << DiscriminatorShift) | 215 (IsIsaPointer << IsaPointerShift) | 216 (AuthenticatesNullValues << AuthenticatesNullValuesShift)) { 217 assert(Key <= KeyNoneInternal); 218 assert(ExtraDiscriminator <= MaxDiscriminator); 219 assert((Data == 0) == 220 (getAuthenticationMode() == PointerAuthenticationMode::None)); 221 } 222 223 public: 224 enum { 225 KeyNoneInternal = (1u << KeyBits) - 1, 226 227 /// The maximum supported pointer-authentication key. 228 MaxKey = KeyNoneInternal - 1, 229 230 /// The maximum supported pointer-authentication discriminator. 231 MaxDiscriminator = (1u << DiscriminatorBits) - 1 232 }; 233 234 public: 235 PointerAuthQualifier() = default; 236 237 static PointerAuthQualifier 238 Create(unsigned Key, bool IsAddressDiscriminated, unsigned ExtraDiscriminator, 239 PointerAuthenticationMode AuthenticationMode, bool IsIsaPointer, 240 bool AuthenticatesNullValues) { 241 if (Key == PointerAuthKeyNone) 242 Key = KeyNoneInternal; 243 assert(Key <= KeyNoneInternal && "out-of-range key value"); 244 return PointerAuthQualifier(Key, IsAddressDiscriminated, ExtraDiscriminator, 245 AuthenticationMode, IsIsaPointer, 246 AuthenticatesNullValues); 247 } 248 249 bool isPresent() const { 250 assert((Data == 0) == 251 (getAuthenticationMode() == PointerAuthenticationMode::None)); 252 return Data != 0; 253 } 254 255 explicit operator bool() const { return isPresent(); } 256 257 unsigned getKey() const { 258 assert(isPresent()); 259 return (Data & KeyMask) >> KeyShift; 260 } 261 262 bool hasKeyNone() const { return isPresent() && getKey() == KeyNoneInternal; } 263 264 bool isAddressDiscriminated() const { 265 assert(isPresent()); 266 return (Data & AddressDiscriminatedMask) >> AddressDiscriminatedShift; 267 } 268 269 unsigned getExtraDiscriminator() const { 270 assert(isPresent()); 271 return (Data >> DiscriminatorShift); 272 } 273 274 PointerAuthenticationMode getAuthenticationMode() const { 275 return PointerAuthenticationMode((Data & AuthenticationModeMask) >> 276 AuthenticationModeShift); 277 } 278 279 bool isIsaPointer() const { 280 assert(isPresent()); 281 return (Data & IsaPointerMask) >> IsaPointerShift; 282 } 283 284 bool authenticatesNullValues() const { 285 assert(isPresent()); 286 return (Data & AuthenticatesNullValuesMask) >> AuthenticatesNullValuesShift; 287 } 288 289 PointerAuthQualifier withoutKeyNone() const { 290 return hasKeyNone() ? PointerAuthQualifier() : *this; 291 } 292 293 friend bool operator==(PointerAuthQualifier Lhs, PointerAuthQualifier Rhs) { 294 return Lhs.Data == Rhs.Data; 295 } 296 friend bool operator!=(PointerAuthQualifier Lhs, PointerAuthQualifier Rhs) { 297 return Lhs.Data != Rhs.Data; 298 } 299 300 bool isEquivalent(PointerAuthQualifier Other) const { 301 return withoutKeyNone() == Other.withoutKeyNone(); 302 } 303 304 uint32_t getAsOpaqueValue() const { return Data; } 305 306 // Deserialize pointer-auth qualifiers from an opaque representation. 307 static PointerAuthQualifier fromOpaqueValue(uint32_t Opaque) { 308 PointerAuthQualifier Result; 309 Result.Data = Opaque; 310 assert((Result.Data == 0) == 311 (Result.getAuthenticationMode() == PointerAuthenticationMode::None)); 312 return Result; 313 } 314 315 void Profile(llvm::FoldingSetNodeID &ID) const { ID.AddInteger(Data); } 316 }; 317 318 /// The collection of all-type qualifiers we support. 319 /// Clang supports five independent qualifiers: 320 /// * C99: const, volatile, and restrict 321 /// * MS: __unaligned 322 /// * Embedded C (TR18037): address spaces 323 /// * Objective C: the GC attributes (none, weak, or strong) 324 class Qualifiers { 325 public: 326 Qualifiers() = default; 327 enum TQ : uint64_t { 328 // NOTE: These flags must be kept in sync with DeclSpec::TQ. 329 Const = 0x1, 330 Restrict = 0x2, 331 Volatile = 0x4, 332 CVRMask = Const | Volatile | Restrict 333 }; 334 335 enum GC { 336 GCNone = 0, 337 Weak, 338 Strong 339 }; 340 341 enum ObjCLifetime { 342 /// There is no lifetime qualification on this type. 343 OCL_None, 344 345 /// This object can be modified without requiring retains or 346 /// releases. 347 OCL_ExplicitNone, 348 349 /// Assigning into this object requires the old value to be 350 /// released and the new value to be retained. The timing of the 351 /// release of the old value is inexact: it may be moved to 352 /// immediately after the last known point where the value is 353 /// live. 354 OCL_Strong, 355 356 /// Reading or writing from this object requires a barrier call. 357 OCL_Weak, 358 359 /// Assigning into this object requires a lifetime extension. 360 OCL_Autoreleasing 361 }; 362 363 enum : uint64_t { 364 /// The maximum supported address space number. 365 /// 23 bits should be enough for anyone. 366 MaxAddressSpace = 0x7fffffu, 367 368 /// The width of the "fast" qualifier mask. 369 FastWidth = 3, 370 371 /// The fast qualifier mask. 372 FastMask = (1 << FastWidth) - 1 373 }; 374 375 /// Returns the common set of qualifiers while removing them from 376 /// the given sets. 377 static Qualifiers removeCommonQualifiers(Qualifiers &L, Qualifiers &R) { 378 Qualifiers Q; 379 PointerAuthQualifier LPtrAuth = L.getPointerAuth(); 380 if (LPtrAuth.isPresent() && 381 LPtrAuth.getKey() != PointerAuthQualifier::KeyNoneInternal && 382 LPtrAuth == R.getPointerAuth()) { 383 Q.setPointerAuth(LPtrAuth); 384 PointerAuthQualifier Empty; 385 L.setPointerAuth(Empty); 386 R.setPointerAuth(Empty); 387 } 388 389 // If both are only CVR-qualified, bit operations are sufficient. 390 if (!(L.Mask & ~CVRMask) && !(R.Mask & ~CVRMask)) { 391 Q.Mask = L.Mask & R.Mask; 392 L.Mask &= ~Q.Mask; 393 R.Mask &= ~Q.Mask; 394 return Q; 395 } 396 397 unsigned CommonCRV = L.getCVRQualifiers() & R.getCVRQualifiers(); 398 Q.addCVRQualifiers(CommonCRV); 399 L.removeCVRQualifiers(CommonCRV); 400 R.removeCVRQualifiers(CommonCRV); 401 402 if (L.getObjCGCAttr() == R.getObjCGCAttr()) { 403 Q.setObjCGCAttr(L.getObjCGCAttr()); 404 L.removeObjCGCAttr(); 405 R.removeObjCGCAttr(); 406 } 407 408 if (L.getObjCLifetime() == R.getObjCLifetime()) { 409 Q.setObjCLifetime(L.getObjCLifetime()); 410 L.removeObjCLifetime(); 411 R.removeObjCLifetime(); 412 } 413 414 if (L.getAddressSpace() == R.getAddressSpace()) { 415 Q.setAddressSpace(L.getAddressSpace()); 416 L.removeAddressSpace(); 417 R.removeAddressSpace(); 418 } 419 return Q; 420 } 421 422 static Qualifiers fromFastMask(unsigned Mask) { 423 Qualifiers Qs; 424 Qs.addFastQualifiers(Mask); 425 return Qs; 426 } 427 428 static Qualifiers fromCVRMask(unsigned CVR) { 429 Qualifiers Qs; 430 Qs.addCVRQualifiers(CVR); 431 return Qs; 432 } 433 434 static Qualifiers fromCVRUMask(unsigned CVRU) { 435 Qualifiers Qs; 436 Qs.addCVRUQualifiers(CVRU); 437 return Qs; 438 } 439 440 // Deserialize qualifiers from an opaque representation. 441 static Qualifiers fromOpaqueValue(uint64_t opaque) { 442 Qualifiers Qs; 443 Qs.Mask = opaque; 444 return Qs; 445 } 446 447 // Serialize these qualifiers into an opaque representation. 448 uint64_t getAsOpaqueValue() const { return Mask; } 449 450 bool hasConst() const { return Mask & Const; } 451 bool hasOnlyConst() const { return Mask == Const; } 452 void removeConst() { Mask &= ~Const; } 453 void addConst() { Mask |= Const; } 454 Qualifiers withConst() const { 455 Qualifiers Qs = *this; 456 Qs.addConst(); 457 return Qs; 458 } 459 460 bool hasVolatile() const { return Mask & Volatile; } 461 bool hasOnlyVolatile() const { return Mask == Volatile; } 462 void removeVolatile() { Mask &= ~Volatile; } 463 void addVolatile() { Mask |= Volatile; } 464 Qualifiers withVolatile() const { 465 Qualifiers Qs = *this; 466 Qs.addVolatile(); 467 return Qs; 468 } 469 470 bool hasRestrict() const { return Mask & Restrict; } 471 bool hasOnlyRestrict() const { return Mask == Restrict; } 472 void removeRestrict() { Mask &= ~Restrict; } 473 void addRestrict() { Mask |= Restrict; } 474 Qualifiers withRestrict() const { 475 Qualifiers Qs = *this; 476 Qs.addRestrict(); 477 return Qs; 478 } 479 480 bool hasCVRQualifiers() const { return getCVRQualifiers(); } 481 unsigned getCVRQualifiers() const { return Mask & CVRMask; } 482 unsigned getCVRUQualifiers() const { return Mask & (CVRMask | UMask); } 483 484 void setCVRQualifiers(unsigned mask) { 485 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); 486 Mask = (Mask & ~CVRMask) | mask; 487 } 488 void removeCVRQualifiers(unsigned mask) { 489 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); 490 Mask &= ~static_cast<uint64_t>(mask); 491 } 492 void removeCVRQualifiers() { 493 removeCVRQualifiers(CVRMask); 494 } 495 void addCVRQualifiers(unsigned mask) { 496 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); 497 Mask |= mask; 498 } 499 void addCVRUQualifiers(unsigned mask) { 500 assert(!(mask & ~CVRMask & ~UMask) && "bitmask contains non-CVRU bits"); 501 Mask |= mask; 502 } 503 504 bool hasUnaligned() const { return Mask & UMask; } 505 void setUnaligned(bool flag) { 506 Mask = (Mask & ~UMask) | (flag ? UMask : 0); 507 } 508 void removeUnaligned() { Mask &= ~UMask; } 509 void addUnaligned() { Mask |= UMask; } 510 511 bool hasObjCGCAttr() const { return Mask & GCAttrMask; } 512 GC getObjCGCAttr() const { return GC((Mask & GCAttrMask) >> GCAttrShift); } 513 void setObjCGCAttr(GC type) { 514 Mask = (Mask & ~GCAttrMask) | (type << GCAttrShift); 515 } 516 void removeObjCGCAttr() { setObjCGCAttr(GCNone); } 517 void addObjCGCAttr(GC type) { 518 assert(type); 519 setObjCGCAttr(type); 520 } 521 Qualifiers withoutObjCGCAttr() const { 522 Qualifiers qs = *this; 523 qs.removeObjCGCAttr(); 524 return qs; 525 } 526 Qualifiers withoutObjCLifetime() const { 527 Qualifiers qs = *this; 528 qs.removeObjCLifetime(); 529 return qs; 530 } 531 Qualifiers withoutAddressSpace() const { 532 Qualifiers qs = *this; 533 qs.removeAddressSpace(); 534 return qs; 535 } 536 537 bool hasObjCLifetime() const { return Mask & LifetimeMask; } 538 ObjCLifetime getObjCLifetime() const { 539 return ObjCLifetime((Mask & LifetimeMask) >> LifetimeShift); 540 } 541 void setObjCLifetime(ObjCLifetime type) { 542 Mask = (Mask & ~LifetimeMask) | (type << LifetimeShift); 543 } 544 void removeObjCLifetime() { setObjCLifetime(OCL_None); } 545 void addObjCLifetime(ObjCLifetime type) { 546 assert(type); 547 assert(!hasObjCLifetime()); 548 Mask |= (type << LifetimeShift); 549 } 550 551 /// True if the lifetime is neither None or ExplicitNone. 552 bool hasNonTrivialObjCLifetime() const { 553 ObjCLifetime lifetime = getObjCLifetime(); 554 return (lifetime > OCL_ExplicitNone); 555 } 556 557 /// True if the lifetime is either strong or weak. 558 bool hasStrongOrWeakObjCLifetime() const { 559 ObjCLifetime lifetime = getObjCLifetime(); 560 return (lifetime == OCL_Strong || lifetime == OCL_Weak); 561 } 562 563 bool hasAddressSpace() const { return Mask & AddressSpaceMask; } 564 LangAS getAddressSpace() const { 565 return static_cast<LangAS>(Mask >> AddressSpaceShift); 566 } 567 bool hasTargetSpecificAddressSpace() const { 568 return isTargetAddressSpace(getAddressSpace()); 569 } 570 /// Get the address space attribute value to be printed by diagnostics. 571 unsigned getAddressSpaceAttributePrintValue() const { 572 auto Addr = getAddressSpace(); 573 // This function is not supposed to be used with language specific 574 // address spaces. If that happens, the diagnostic message should consider 575 // printing the QualType instead of the address space value. 576 assert(Addr == LangAS::Default || hasTargetSpecificAddressSpace()); 577 if (Addr != LangAS::Default) 578 return toTargetAddressSpace(Addr); 579 // TODO: The diagnostic messages where Addr may be 0 should be fixed 580 // since it cannot differentiate the situation where 0 denotes the default 581 // address space or user specified __attribute__((address_space(0))). 582 return 0; 583 } 584 void setAddressSpace(LangAS space) { 585 assert((unsigned)space <= MaxAddressSpace); 586 Mask = (Mask & ~AddressSpaceMask) 587 | (((uint32_t) space) << AddressSpaceShift); 588 } 589 void removeAddressSpace() { setAddressSpace(LangAS::Default); } 590 void addAddressSpace(LangAS space) { 591 assert(space != LangAS::Default); 592 setAddressSpace(space); 593 } 594 595 bool hasPointerAuth() const { return Mask & PtrAuthMask; } 596 PointerAuthQualifier getPointerAuth() const { 597 return PointerAuthQualifier::fromOpaqueValue(Mask >> PtrAuthShift); 598 } 599 void setPointerAuth(PointerAuthQualifier Q) { 600 Mask = (Mask & ~PtrAuthMask) | 601 (uint64_t(Q.getAsOpaqueValue()) << PtrAuthShift); 602 } 603 void removePointerAuth() { Mask &= ~PtrAuthMask; } 604 void addPointerAuth(PointerAuthQualifier Q) { 605 assert(Q.isPresent()); 606 setPointerAuth(Q); 607 } 608 609 // Fast qualifiers are those that can be allocated directly 610 // on a QualType object. 611 bool hasFastQualifiers() const { return getFastQualifiers(); } 612 unsigned getFastQualifiers() const { return Mask & FastMask; } 613 void setFastQualifiers(unsigned mask) { 614 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); 615 Mask = (Mask & ~FastMask) | mask; 616 } 617 void removeFastQualifiers(unsigned mask) { 618 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); 619 Mask &= ~static_cast<uint64_t>(mask); 620 } 621 void removeFastQualifiers() { 622 removeFastQualifiers(FastMask); 623 } 624 void addFastQualifiers(unsigned mask) { 625 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); 626 Mask |= mask; 627 } 628 629 /// Return true if the set contains any qualifiers which require an ExtQuals 630 /// node to be allocated. 631 bool hasNonFastQualifiers() const { return Mask & ~FastMask; } 632 Qualifiers getNonFastQualifiers() const { 633 Qualifiers Quals = *this; 634 Quals.setFastQualifiers(0); 635 return Quals; 636 } 637 638 /// Return true if the set contains any qualifiers. 639 bool hasQualifiers() const { return Mask; } 640 bool empty() const { return !Mask; } 641 642 /// Add the qualifiers from the given set to this set. 643 void addQualifiers(Qualifiers Q) { 644 // If the other set doesn't have any non-boolean qualifiers, just 645 // bit-or it in. 646 if (!(Q.Mask & ~CVRMask)) 647 Mask |= Q.Mask; 648 else { 649 Mask |= (Q.Mask & CVRMask); 650 if (Q.hasAddressSpace()) 651 addAddressSpace(Q.getAddressSpace()); 652 if (Q.hasObjCGCAttr()) 653 addObjCGCAttr(Q.getObjCGCAttr()); 654 if (Q.hasObjCLifetime()) 655 addObjCLifetime(Q.getObjCLifetime()); 656 if (Q.hasPointerAuth()) 657 addPointerAuth(Q.getPointerAuth()); 658 } 659 } 660 661 /// Remove the qualifiers from the given set from this set. 662 void removeQualifiers(Qualifiers Q) { 663 // If the other set doesn't have any non-boolean qualifiers, just 664 // bit-and the inverse in. 665 if (!(Q.Mask & ~CVRMask)) 666 Mask &= ~Q.Mask; 667 else { 668 Mask &= ~(Q.Mask & CVRMask); 669 if (getObjCGCAttr() == Q.getObjCGCAttr()) 670 removeObjCGCAttr(); 671 if (getObjCLifetime() == Q.getObjCLifetime()) 672 removeObjCLifetime(); 673 if (getAddressSpace() == Q.getAddressSpace()) 674 removeAddressSpace(); 675 if (getPointerAuth() == Q.getPointerAuth()) 676 removePointerAuth(); 677 } 678 } 679 680 /// Add the qualifiers from the given set to this set, given that 681 /// they don't conflict. 682 void addConsistentQualifiers(Qualifiers qs) { 683 assert(getAddressSpace() == qs.getAddressSpace() || 684 !hasAddressSpace() || !qs.hasAddressSpace()); 685 assert(getObjCGCAttr() == qs.getObjCGCAttr() || 686 !hasObjCGCAttr() || !qs.hasObjCGCAttr()); 687 assert(getObjCLifetime() == qs.getObjCLifetime() || 688 !hasObjCLifetime() || !qs.hasObjCLifetime()); 689 assert(!hasPointerAuth() || !qs.hasPointerAuth() || 690 getPointerAuth() == qs.getPointerAuth()); 691 Mask |= qs.Mask; 692 } 693 694 /// Returns true if address space A is equal to or a superset of B. 695 /// OpenCL v2.0 defines conversion rules (OpenCLC v2.0 s6.5.5) and notion of 696 /// overlapping address spaces. 697 /// CL1.1 or CL1.2: 698 /// every address space is a superset of itself. 699 /// CL2.0 adds: 700 /// __generic is a superset of any address space except for __constant. 701 static bool isAddressSpaceSupersetOf(LangAS A, LangAS B, 702 const ASTContext &Ctx) { 703 // Address spaces must match exactly. 704 return A == B || isTargetAddressSpaceSupersetOf(A, B, Ctx); 705 } 706 707 static bool isTargetAddressSpaceSupersetOf(LangAS A, LangAS B, 708 const ASTContext &Ctx); 709 710 /// Returns true if the address space in these qualifiers is equal to or 711 /// a superset of the address space in the argument qualifiers. 712 bool isAddressSpaceSupersetOf(Qualifiers other, const ASTContext &Ctx) const { 713 return isAddressSpaceSupersetOf(getAddressSpace(), other.getAddressSpace(), 714 Ctx); 715 } 716 717 /// Determines if these qualifiers compatibly include another set. 718 /// Generally this answers the question of whether an object with the other 719 /// qualifiers can be safely used as an object with these qualifiers. 720 bool compatiblyIncludes(Qualifiers other, const ASTContext &Ctx) const { 721 return isAddressSpaceSupersetOf(other, Ctx) && 722 // ObjC GC qualifiers can match, be added, or be removed, but can't 723 // be changed. 724 (getObjCGCAttr() == other.getObjCGCAttr() || !hasObjCGCAttr() || 725 !other.hasObjCGCAttr()) && 726 // Pointer-auth qualifiers must match exactly. 727 getPointerAuth() == other.getPointerAuth() && 728 // ObjC lifetime qualifiers must match exactly. 729 getObjCLifetime() == other.getObjCLifetime() && 730 // CVR qualifiers may subset. 731 (((Mask & CVRMask) | (other.Mask & CVRMask)) == (Mask & CVRMask)) && 732 // U qualifier may superset. 733 (!other.hasUnaligned() || hasUnaligned()); 734 } 735 736 /// Determines if these qualifiers compatibly include another set of 737 /// qualifiers from the narrow perspective of Objective-C ARC lifetime. 738 /// 739 /// One set of Objective-C lifetime qualifiers compatibly includes the other 740 /// if the lifetime qualifiers match, or if both are non-__weak and the 741 /// including set also contains the 'const' qualifier, or both are non-__weak 742 /// and one is None (which can only happen in non-ARC modes). 743 bool compatiblyIncludesObjCLifetime(Qualifiers other) const { 744 if (getObjCLifetime() == other.getObjCLifetime()) 745 return true; 746 747 if (getObjCLifetime() == OCL_Weak || other.getObjCLifetime() == OCL_Weak) 748 return false; 749 750 if (getObjCLifetime() == OCL_None || other.getObjCLifetime() == OCL_None) 751 return true; 752 753 return hasConst(); 754 } 755 756 /// Determine whether this set of qualifiers is a strict superset of 757 /// another set of qualifiers, not considering qualifier compatibility. 758 bool isStrictSupersetOf(Qualifiers Other) const; 759 760 bool operator==(Qualifiers Other) const { return Mask == Other.Mask; } 761 bool operator!=(Qualifiers Other) const { return Mask != Other.Mask; } 762 763 explicit operator bool() const { return hasQualifiers(); } 764 765 Qualifiers &operator+=(Qualifiers R) { 766 addQualifiers(R); 767 return *this; 768 } 769 770 // Union two qualifier sets. If an enumerated qualifier appears 771 // in both sets, use the one from the right. 772 friend Qualifiers operator+(Qualifiers L, Qualifiers R) { 773 L += R; 774 return L; 775 } 776 777 Qualifiers &operator-=(Qualifiers R) { 778 removeQualifiers(R); 779 return *this; 780 } 781 782 /// Compute the difference between two qualifier sets. 783 friend Qualifiers operator-(Qualifiers L, Qualifiers R) { 784 L -= R; 785 return L; 786 } 787 788 std::string getAsString() const; 789 std::string getAsString(const PrintingPolicy &Policy) const; 790 791 static std::string getAddrSpaceAsString(LangAS AS); 792 793 bool isEmptyWhenPrinted(const PrintingPolicy &Policy) const; 794 void print(raw_ostream &OS, const PrintingPolicy &Policy, 795 bool appendSpaceIfNonEmpty = false) const; 796 797 void Profile(llvm::FoldingSetNodeID &ID) const { ID.AddInteger(Mask); } 798 799 private: 800 // bits: |0 1 2|3|4 .. 5|6 .. 8|9 ... 31|32 ... 63| 801 // |C R V|U|GCAttr|Lifetime|AddressSpace| PtrAuth | 802 uint64_t Mask = 0; 803 static_assert(sizeof(PointerAuthQualifier) == sizeof(uint32_t), 804 "PointerAuthQualifier must be 32 bits"); 805 806 static constexpr uint64_t UMask = 0x8; 807 static constexpr uint64_t UShift = 3; 808 static constexpr uint64_t GCAttrMask = 0x30; 809 static constexpr uint64_t GCAttrShift = 4; 810 static constexpr uint64_t LifetimeMask = 0x1C0; 811 static constexpr uint64_t LifetimeShift = 6; 812 static constexpr uint64_t AddressSpaceMask = 813 ~(CVRMask | UMask | GCAttrMask | LifetimeMask); 814 static constexpr uint64_t AddressSpaceShift = 9; 815 static constexpr uint64_t PtrAuthShift = 32; 816 static constexpr uint64_t PtrAuthMask = uint64_t(0xffffffff) << PtrAuthShift; 817 }; 818 819 class QualifiersAndAtomic { 820 Qualifiers Quals; 821 bool HasAtomic; 822 823 public: 824 QualifiersAndAtomic() : HasAtomic(false) {} 825 QualifiersAndAtomic(Qualifiers Quals, bool HasAtomic) 826 : Quals(Quals), HasAtomic(HasAtomic) {} 827 828 operator Qualifiers() const { return Quals; } 829 830 bool hasVolatile() const { return Quals.hasVolatile(); } 831 bool hasConst() const { return Quals.hasConst(); } 832 bool hasRestrict() const { return Quals.hasRestrict(); } 833 bool hasAtomic() const { return HasAtomic; } 834 835 void addVolatile() { Quals.addVolatile(); } 836 void addConst() { Quals.addConst(); } 837 void addRestrict() { Quals.addRestrict(); } 838 void addAtomic() { HasAtomic = true; } 839 840 void removeVolatile() { Quals.removeVolatile(); } 841 void removeConst() { Quals.removeConst(); } 842 void removeRestrict() { Quals.removeRestrict(); } 843 void removeAtomic() { HasAtomic = false; } 844 845 QualifiersAndAtomic withVolatile() { 846 return {Quals.withVolatile(), HasAtomic}; 847 } 848 QualifiersAndAtomic withConst() { return {Quals.withConst(), HasAtomic}; } 849 QualifiersAndAtomic withRestrict() { 850 return {Quals.withRestrict(), HasAtomic}; 851 } 852 QualifiersAndAtomic withAtomic() { return {Quals, true}; } 853 854 QualifiersAndAtomic &operator+=(Qualifiers RHS) { 855 Quals += RHS; 856 return *this; 857 } 858 }; 859 860 /// A std::pair-like structure for storing a qualified type split 861 /// into its local qualifiers and its locally-unqualified type. 862 struct SplitQualType { 863 /// The locally-unqualified type. 864 const Type *Ty = nullptr; 865 866 /// The local qualifiers. 867 Qualifiers Quals; 868 869 SplitQualType() = default; 870 SplitQualType(const Type *ty, Qualifiers qs) : Ty(ty), Quals(qs) {} 871 872 SplitQualType getSingleStepDesugaredType() const; // end of this file 873 874 // Make std::tie work. 875 std::pair<const Type *,Qualifiers> asPair() const { 876 return std::pair<const Type *, Qualifiers>(Ty, Quals); 877 } 878 879 friend bool operator==(SplitQualType a, SplitQualType b) { 880 return a.Ty == b.Ty && a.Quals == b.Quals; 881 } 882 friend bool operator!=(SplitQualType a, SplitQualType b) { 883 return a.Ty != b.Ty || a.Quals != b.Quals; 884 } 885 }; 886 887 /// The kind of type we are substituting Objective-C type arguments into. 888 /// 889 /// The kind of substitution affects the replacement of type parameters when 890 /// no concrete type information is provided, e.g., when dealing with an 891 /// unspecialized type. 892 enum class ObjCSubstitutionContext { 893 /// An ordinary type. 894 Ordinary, 895 896 /// The result type of a method or function. 897 Result, 898 899 /// The parameter type of a method or function. 900 Parameter, 901 902 /// The type of a property. 903 Property, 904 905 /// The superclass of a type. 906 Superclass, 907 }; 908 909 /// The kind of 'typeof' expression we're after. 910 enum class TypeOfKind : uint8_t { 911 Qualified, 912 Unqualified, 913 }; 914 915 /// A (possibly-)qualified type. 916 /// 917 /// For efficiency, we don't store CV-qualified types as nodes on their 918 /// own: instead each reference to a type stores the qualifiers. This 919 /// greatly reduces the number of nodes we need to allocate for types (for 920 /// example we only need one for 'int', 'const int', 'volatile int', 921 /// 'const volatile int', etc). 922 /// 923 /// As an added efficiency bonus, instead of making this a pair, we 924 /// just store the two bits we care about in the low bits of the 925 /// pointer. To handle the packing/unpacking, we make QualType be a 926 /// simple wrapper class that acts like a smart pointer. A third bit 927 /// indicates whether there are extended qualifiers present, in which 928 /// case the pointer points to a special structure. 929 class QualType { 930 friend class QualifierCollector; 931 932 // Thankfully, these are efficiently composable. 933 llvm::PointerIntPair<llvm::PointerUnion<const Type *, const ExtQuals *>, 934 Qualifiers::FastWidth> Value; 935 936 const ExtQuals *getExtQualsUnsafe() const { 937 return cast<const ExtQuals *>(Value.getPointer()); 938 } 939 940 const Type *getTypePtrUnsafe() const { 941 return cast<const Type *>(Value.getPointer()); 942 } 943 944 const ExtQualsTypeCommonBase *getCommonPtr() const { 945 assert(!isNull() && "Cannot retrieve a NULL type pointer"); 946 auto CommonPtrVal = reinterpret_cast<uintptr_t>(Value.getOpaqueValue()); 947 CommonPtrVal &= ~(uintptr_t)((1 << TypeAlignmentInBits) - 1); 948 return reinterpret_cast<ExtQualsTypeCommonBase*>(CommonPtrVal); 949 } 950 951 public: 952 QualType() = default; 953 QualType(const Type *Ptr, unsigned Quals) : Value(Ptr, Quals) {} 954 QualType(const ExtQuals *Ptr, unsigned Quals) : Value(Ptr, Quals) {} 955 956 unsigned getLocalFastQualifiers() const { return Value.getInt(); } 957 void setLocalFastQualifiers(unsigned Quals) { Value.setInt(Quals); } 958 959 bool UseExcessPrecision(const ASTContext &Ctx); 960 961 /// Retrieves a pointer to the underlying (unqualified) type. 962 /// 963 /// This function requires that the type not be NULL. If the type might be 964 /// NULL, use the (slightly less efficient) \c getTypePtrOrNull(). 965 const Type *getTypePtr() const; 966 967 const Type *getTypePtrOrNull() const; 968 969 /// Retrieves a pointer to the name of the base type. 970 const IdentifierInfo *getBaseTypeIdentifier() const; 971 972 /// Divides a QualType into its unqualified type and a set of local 973 /// qualifiers. 974 SplitQualType split() const; 975 976 void *getAsOpaquePtr() const { return Value.getOpaqueValue(); } 977 978 static QualType getFromOpaquePtr(const void *Ptr) { 979 QualType T; 980 T.Value.setFromOpaqueValue(const_cast<void*>(Ptr)); 981 return T; 982 } 983 984 const Type &operator*() const { 985 return *getTypePtr(); 986 } 987 988 const Type *operator->() const { 989 return getTypePtr(); 990 } 991 992 bool isCanonical() const; 993 bool isCanonicalAsParam() const; 994 995 /// Return true if this QualType doesn't point to a type yet. 996 bool isNull() const { 997 return Value.getPointer().isNull(); 998 } 999 1000 // Determines if a type can form `T&`. 1001 bool isReferenceable() const; 1002 1003 /// Determine whether this particular QualType instance has the 1004 /// "const" qualifier set, without looking through typedefs that may have 1005 /// added "const" at a different level. 1006 bool isLocalConstQualified() const { 1007 return (getLocalFastQualifiers() & Qualifiers::Const); 1008 } 1009 1010 /// Determine whether this type is const-qualified. 1011 bool isConstQualified() const; 1012 1013 enum class NonConstantStorageReason { 1014 MutableField, 1015 NonConstNonReferenceType, 1016 NonTrivialCtor, 1017 NonTrivialDtor, 1018 }; 1019 /// Determine whether instances of this type can be placed in immutable 1020 /// storage. 1021 /// If ExcludeCtor is true, the duration when the object's constructor runs 1022 /// will not be considered. The caller will need to verify that the object is 1023 /// not written to during its construction. ExcludeDtor works similarly. 1024 std::optional<NonConstantStorageReason> 1025 isNonConstantStorage(const ASTContext &Ctx, bool ExcludeCtor, 1026 bool ExcludeDtor); 1027 1028 bool isConstantStorage(const ASTContext &Ctx, bool ExcludeCtor, 1029 bool ExcludeDtor) { 1030 return !isNonConstantStorage(Ctx, ExcludeCtor, ExcludeDtor); 1031 } 1032 1033 /// Determine whether this particular QualType instance has the 1034 /// "restrict" qualifier set, without looking through typedefs that may have 1035 /// added "restrict" at a different level. 1036 bool isLocalRestrictQualified() const { 1037 return (getLocalFastQualifiers() & Qualifiers::Restrict); 1038 } 1039 1040 /// Determine whether this type is restrict-qualified. 1041 bool isRestrictQualified() const; 1042 1043 /// Determine whether this particular QualType instance has the 1044 /// "volatile" qualifier set, without looking through typedefs that may have 1045 /// added "volatile" at a different level. 1046 bool isLocalVolatileQualified() const { 1047 return (getLocalFastQualifiers() & Qualifiers::Volatile); 1048 } 1049 1050 /// Determine whether this type is volatile-qualified. 1051 bool isVolatileQualified() const; 1052 1053 /// Determine whether this particular QualType instance has any 1054 /// qualifiers, without looking through any typedefs that might add 1055 /// qualifiers at a different level. 1056 bool hasLocalQualifiers() const { 1057 return getLocalFastQualifiers() || hasLocalNonFastQualifiers(); 1058 } 1059 1060 /// Determine whether this type has any qualifiers. 1061 bool hasQualifiers() const; 1062 1063 /// Determine whether this particular QualType instance has any 1064 /// "non-fast" qualifiers, e.g., those that are stored in an ExtQualType 1065 /// instance. 1066 bool hasLocalNonFastQualifiers() const { 1067 return isa<const ExtQuals *>(Value.getPointer()); 1068 } 1069 1070 /// Retrieve the set of qualifiers local to this particular QualType 1071 /// instance, not including any qualifiers acquired through typedefs or 1072 /// other sugar. 1073 Qualifiers getLocalQualifiers() const; 1074 1075 /// Retrieve the set of qualifiers applied to this type. 1076 Qualifiers getQualifiers() const; 1077 1078 /// Retrieve the set of CVR (const-volatile-restrict) qualifiers 1079 /// local to this particular QualType instance, not including any qualifiers 1080 /// acquired through typedefs or other sugar. 1081 unsigned getLocalCVRQualifiers() const { 1082 return getLocalFastQualifiers(); 1083 } 1084 1085 /// Retrieve the set of CVR (const-volatile-restrict) qualifiers 1086 /// applied to this type. 1087 unsigned getCVRQualifiers() const; 1088 1089 bool isConstant(const ASTContext& Ctx) const { 1090 return QualType::isConstant(*this, Ctx); 1091 } 1092 1093 /// Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10). 1094 bool isPODType(const ASTContext &Context) const; 1095 1096 /// Return true if this is a POD type according to the rules of the C++98 1097 /// standard, regardless of the current compilation's language. 1098 bool isCXX98PODType(const ASTContext &Context) const; 1099 1100 /// Return true if this is a POD type according to the more relaxed rules 1101 /// of the C++11 standard, regardless of the current compilation's language. 1102 /// (C++0x [basic.types]p9). Note that, unlike 1103 /// CXXRecordDecl::isCXX11StandardLayout, this takes DRs into account. 1104 bool isCXX11PODType(const ASTContext &Context) const; 1105 1106 /// Return true if this is a trivial type per (C++0x [basic.types]p9) 1107 bool isTrivialType(const ASTContext &Context) const; 1108 1109 /// Return true if this is a trivially copyable type (C++0x [basic.types]p9) 1110 bool isTriviallyCopyableType(const ASTContext &Context) const; 1111 1112 /// Return true if the type is safe to bitwise copy using memcpy/memmove. 1113 /// 1114 /// This is an extension in clang: bitwise cloneable types act as trivially 1115 /// copyable types, meaning their underlying bytes can be safely copied by 1116 /// memcpy or memmove. After the copy, the destination object has the same 1117 /// object representation. 1118 /// 1119 /// However, there are cases where it is not safe to copy: 1120 /// - When sanitizers, such as AddressSanitizer, add padding with poison, 1121 /// which can cause issues if those poisoned padding bits are accessed. 1122 /// - Types with Objective-C lifetimes, where specific runtime 1123 /// semantics may not be preserved during a bitwise copy. 1124 bool isBitwiseCloneableType(const ASTContext &Context) const; 1125 1126 /// Return true if this is a trivially copyable type 1127 bool isTriviallyCopyConstructibleType(const ASTContext &Context) const; 1128 1129 /// Return true if this is a trivially relocatable type. 1130 bool isTriviallyRelocatableType(const ASTContext &Context) const; 1131 1132 /// Returns true if it is a class and it might be dynamic. 1133 bool mayBeDynamicClass() const; 1134 1135 /// Returns true if it is not a class or if the class might not be dynamic. 1136 bool mayBeNotDynamicClass() const; 1137 1138 /// Returns true if it is a WebAssembly Reference Type. 1139 bool isWebAssemblyReferenceType() const; 1140 1141 /// Returns true if it is a WebAssembly Externref Type. 1142 bool isWebAssemblyExternrefType() const; 1143 1144 /// Returns true if it is a WebAssembly Funcref Type. 1145 bool isWebAssemblyFuncrefType() const; 1146 1147 // Don't promise in the API that anything besides 'const' can be 1148 // easily added. 1149 1150 /// Add the `const` type qualifier to this QualType. 1151 void addConst() { 1152 addFastQualifiers(Qualifiers::Const); 1153 } 1154 QualType withConst() const { 1155 return withFastQualifiers(Qualifiers::Const); 1156 } 1157 1158 /// Add the `volatile` type qualifier to this QualType. 1159 void addVolatile() { 1160 addFastQualifiers(Qualifiers::Volatile); 1161 } 1162 QualType withVolatile() const { 1163 return withFastQualifiers(Qualifiers::Volatile); 1164 } 1165 1166 /// Add the `restrict` qualifier to this QualType. 1167 void addRestrict() { 1168 addFastQualifiers(Qualifiers::Restrict); 1169 } 1170 QualType withRestrict() const { 1171 return withFastQualifiers(Qualifiers::Restrict); 1172 } 1173 1174 QualType withCVRQualifiers(unsigned CVR) const { 1175 return withFastQualifiers(CVR); 1176 } 1177 1178 void addFastQualifiers(unsigned TQs) { 1179 assert(!(TQs & ~Qualifiers::FastMask) 1180 && "non-fast qualifier bits set in mask!"); 1181 Value.setInt(Value.getInt() | TQs); 1182 } 1183 1184 void removeLocalConst(); 1185 void removeLocalVolatile(); 1186 void removeLocalRestrict(); 1187 1188 void removeLocalFastQualifiers() { Value.setInt(0); } 1189 void removeLocalFastQualifiers(unsigned Mask) { 1190 assert(!(Mask & ~Qualifiers::FastMask) && "mask has non-fast qualifiers"); 1191 Value.setInt(Value.getInt() & ~Mask); 1192 } 1193 1194 // Creates a type with the given qualifiers in addition to any 1195 // qualifiers already on this type. 1196 QualType withFastQualifiers(unsigned TQs) const { 1197 QualType T = *this; 1198 T.addFastQualifiers(TQs); 1199 return T; 1200 } 1201 1202 // Creates a type with exactly the given fast qualifiers, removing 1203 // any existing fast qualifiers. 1204 QualType withExactLocalFastQualifiers(unsigned TQs) const { 1205 return withoutLocalFastQualifiers().withFastQualifiers(TQs); 1206 } 1207 1208 // Removes fast qualifiers, but leaves any extended qualifiers in place. 1209 QualType withoutLocalFastQualifiers() const { 1210 QualType T = *this; 1211 T.removeLocalFastQualifiers(); 1212 return T; 1213 } 1214 1215 QualType getCanonicalType() const; 1216 1217 /// Return this type with all of the instance-specific qualifiers 1218 /// removed, but without removing any qualifiers that may have been applied 1219 /// through typedefs. 1220 QualType getLocalUnqualifiedType() const { return QualType(getTypePtr(), 0); } 1221 1222 /// Retrieve the unqualified variant of the given type, 1223 /// removing as little sugar as possible. 1224 /// 1225 /// This routine looks through various kinds of sugar to find the 1226 /// least-desugared type that is unqualified. For example, given: 1227 /// 1228 /// \code 1229 /// typedef int Integer; 1230 /// typedef const Integer CInteger; 1231 /// typedef CInteger DifferenceType; 1232 /// \endcode 1233 /// 1234 /// Executing \c getUnqualifiedType() on the type \c DifferenceType will 1235 /// desugar until we hit the type \c Integer, which has no qualifiers on it. 1236 /// 1237 /// The resulting type might still be qualified if it's sugar for an array 1238 /// type. To strip qualifiers even from within a sugared array type, use 1239 /// ASTContext::getUnqualifiedArrayType. 1240 /// 1241 /// Note: In C, the _Atomic qualifier is special (see C23 6.2.5p32 for 1242 /// details), and it is not stripped by this function. Use 1243 /// getAtomicUnqualifiedType() to strip qualifiers including _Atomic. 1244 inline QualType getUnqualifiedType() const; 1245 1246 /// Retrieve the unqualified variant of the given type, removing as little 1247 /// sugar as possible. 1248 /// 1249 /// Like getUnqualifiedType(), but also returns the set of 1250 /// qualifiers that were built up. 1251 /// 1252 /// The resulting type might still be qualified if it's sugar for an array 1253 /// type. To strip qualifiers even from within a sugared array type, use 1254 /// ASTContext::getUnqualifiedArrayType. 1255 inline SplitQualType getSplitUnqualifiedType() const; 1256 1257 /// Determine whether this type is more qualified than the other 1258 /// given type, requiring exact equality for non-CVR qualifiers. 1259 bool isMoreQualifiedThan(QualType Other, const ASTContext &Ctx) const; 1260 1261 /// Determine whether this type is at least as qualified as the other 1262 /// given type, requiring exact equality for non-CVR qualifiers. 1263 bool isAtLeastAsQualifiedAs(QualType Other, const ASTContext &Ctx) const; 1264 1265 QualType getNonReferenceType() const; 1266 1267 /// Determine the type of a (typically non-lvalue) expression with the 1268 /// specified result type. 1269 /// 1270 /// This routine should be used for expressions for which the return type is 1271 /// explicitly specified (e.g., in a cast or call) and isn't necessarily 1272 /// an lvalue. It removes a top-level reference (since there are no 1273 /// expressions of reference type) and deletes top-level cvr-qualifiers 1274 /// from non-class types (in C++) or all types (in C). 1275 QualType getNonLValueExprType(const ASTContext &Context) const; 1276 1277 /// Remove an outer pack expansion type (if any) from this type. Used as part 1278 /// of converting the type of a declaration to the type of an expression that 1279 /// references that expression. It's meaningless for an expression to have a 1280 /// pack expansion type. 1281 QualType getNonPackExpansionType() const; 1282 1283 /// Return the specified type with any "sugar" removed from 1284 /// the type. This takes off typedefs, typeof's etc. If the outer level of 1285 /// the type is already concrete, it returns it unmodified. This is similar 1286 /// to getting the canonical type, but it doesn't remove *all* typedefs. For 1287 /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is 1288 /// concrete. 1289 /// 1290 /// Qualifiers are left in place. 1291 QualType getDesugaredType(const ASTContext &Context) const { 1292 return getDesugaredType(*this, Context); 1293 } 1294 1295 SplitQualType getSplitDesugaredType() const { 1296 return getSplitDesugaredType(*this); 1297 } 1298 1299 /// Return the specified type with one level of "sugar" removed from 1300 /// the type. 1301 /// 1302 /// This routine takes off the first typedef, typeof, etc. If the outer level 1303 /// of the type is already concrete, it returns it unmodified. 1304 QualType getSingleStepDesugaredType(const ASTContext &Context) const { 1305 return getSingleStepDesugaredTypeImpl(*this, Context); 1306 } 1307 1308 /// Returns the specified type after dropping any 1309 /// outer-level parentheses. 1310 QualType IgnoreParens() const { 1311 if (isa<ParenType>(*this)) 1312 return QualType::IgnoreParens(*this); 1313 return *this; 1314 } 1315 1316 /// Indicate whether the specified types and qualifiers are identical. 1317 friend bool operator==(const QualType &LHS, const QualType &RHS) { 1318 return LHS.Value == RHS.Value; 1319 } 1320 friend bool operator!=(const QualType &LHS, const QualType &RHS) { 1321 return LHS.Value != RHS.Value; 1322 } 1323 friend bool operator<(const QualType &LHS, const QualType &RHS) { 1324 return LHS.Value < RHS.Value; 1325 } 1326 1327 static std::string getAsString(SplitQualType split, 1328 const PrintingPolicy &Policy) { 1329 return getAsString(split.Ty, split.Quals, Policy); 1330 } 1331 static std::string getAsString(const Type *ty, Qualifiers qs, 1332 const PrintingPolicy &Policy); 1333 1334 std::string getAsString() const; 1335 std::string getAsString(const PrintingPolicy &Policy) const; 1336 1337 void print(raw_ostream &OS, const PrintingPolicy &Policy, 1338 const Twine &PlaceHolder = Twine(), 1339 unsigned Indentation = 0) const; 1340 1341 static void print(SplitQualType split, raw_ostream &OS, 1342 const PrintingPolicy &policy, const Twine &PlaceHolder, 1343 unsigned Indentation = 0) { 1344 return print(split.Ty, split.Quals, OS, policy, PlaceHolder, Indentation); 1345 } 1346 1347 static void print(const Type *ty, Qualifiers qs, 1348 raw_ostream &OS, const PrintingPolicy &policy, 1349 const Twine &PlaceHolder, 1350 unsigned Indentation = 0); 1351 1352 void getAsStringInternal(std::string &Str, 1353 const PrintingPolicy &Policy) const; 1354 1355 static void getAsStringInternal(SplitQualType split, std::string &out, 1356 const PrintingPolicy &policy) { 1357 return getAsStringInternal(split.Ty, split.Quals, out, policy); 1358 } 1359 1360 static void getAsStringInternal(const Type *ty, Qualifiers qs, 1361 std::string &out, 1362 const PrintingPolicy &policy); 1363 1364 class StreamedQualTypeHelper { 1365 const QualType &T; 1366 const PrintingPolicy &Policy; 1367 const Twine &PlaceHolder; 1368 unsigned Indentation; 1369 1370 public: 1371 StreamedQualTypeHelper(const QualType &T, const PrintingPolicy &Policy, 1372 const Twine &PlaceHolder, unsigned Indentation) 1373 : T(T), Policy(Policy), PlaceHolder(PlaceHolder), 1374 Indentation(Indentation) {} 1375 1376 friend raw_ostream &operator<<(raw_ostream &OS, 1377 const StreamedQualTypeHelper &SQT) { 1378 SQT.T.print(OS, SQT.Policy, SQT.PlaceHolder, SQT.Indentation); 1379 return OS; 1380 } 1381 }; 1382 1383 StreamedQualTypeHelper stream(const PrintingPolicy &Policy, 1384 const Twine &PlaceHolder = Twine(), 1385 unsigned Indentation = 0) const { 1386 return StreamedQualTypeHelper(*this, Policy, PlaceHolder, Indentation); 1387 } 1388 1389 void dump(const char *s) const; 1390 void dump() const; 1391 void dump(llvm::raw_ostream &OS, const ASTContext &Context) const; 1392 1393 void Profile(llvm::FoldingSetNodeID &ID) const { 1394 ID.AddPointer(getAsOpaquePtr()); 1395 } 1396 1397 /// Check if this type has any address space qualifier. 1398 inline bool hasAddressSpace() const; 1399 1400 /// Return the address space of this type. 1401 inline LangAS getAddressSpace() const; 1402 1403 /// Returns true if address space qualifiers overlap with T address space 1404 /// qualifiers. 1405 /// OpenCL C defines conversion rules for pointers to different address spaces 1406 /// and notion of overlapping address spaces. 1407 /// CL1.1 or CL1.2: 1408 /// address spaces overlap iff they are they same. 1409 /// OpenCL C v2.0 s6.5.5 adds: 1410 /// __generic overlaps with any address space except for __constant. 1411 bool isAddressSpaceOverlapping(QualType T, const ASTContext &Ctx) const { 1412 Qualifiers Q = getQualifiers(); 1413 Qualifiers TQ = T.getQualifiers(); 1414 // Address spaces overlap if at least one of them is a superset of another 1415 return Q.isAddressSpaceSupersetOf(TQ, Ctx) || 1416 TQ.isAddressSpaceSupersetOf(Q, Ctx); 1417 } 1418 1419 /// Returns gc attribute of this type. 1420 inline Qualifiers::GC getObjCGCAttr() const; 1421 1422 /// true when Type is objc's weak. 1423 bool isObjCGCWeak() const { 1424 return getObjCGCAttr() == Qualifiers::Weak; 1425 } 1426 1427 /// true when Type is objc's strong. 1428 bool isObjCGCStrong() const { 1429 return getObjCGCAttr() == Qualifiers::Strong; 1430 } 1431 1432 /// Returns lifetime attribute of this type. 1433 Qualifiers::ObjCLifetime getObjCLifetime() const { 1434 return getQualifiers().getObjCLifetime(); 1435 } 1436 1437 bool hasNonTrivialObjCLifetime() const { 1438 return getQualifiers().hasNonTrivialObjCLifetime(); 1439 } 1440 1441 bool hasStrongOrWeakObjCLifetime() const { 1442 return getQualifiers().hasStrongOrWeakObjCLifetime(); 1443 } 1444 1445 // true when Type is objc's weak and weak is enabled but ARC isn't. 1446 bool isNonWeakInMRRWithObjCWeak(const ASTContext &Context) const; 1447 1448 PointerAuthQualifier getPointerAuth() const { 1449 return getQualifiers().getPointerAuth(); 1450 } 1451 1452 enum PrimitiveDefaultInitializeKind { 1453 /// The type does not fall into any of the following categories. Note that 1454 /// this case is zero-valued so that values of this enum can be used as a 1455 /// boolean condition for non-triviality. 1456 PDIK_Trivial, 1457 1458 /// The type is an Objective-C retainable pointer type that is qualified 1459 /// with the ARC __strong qualifier. 1460 PDIK_ARCStrong, 1461 1462 /// The type is an Objective-C retainable pointer type that is qualified 1463 /// with the ARC __weak qualifier. 1464 PDIK_ARCWeak, 1465 1466 /// The type is a struct containing a field whose type is not PCK_Trivial. 1467 PDIK_Struct 1468 }; 1469 1470 /// Functions to query basic properties of non-trivial C struct types. 1471 1472 /// Check if this is a non-trivial type that would cause a C struct 1473 /// transitively containing this type to be non-trivial to default initialize 1474 /// and return the kind. 1475 PrimitiveDefaultInitializeKind 1476 isNonTrivialToPrimitiveDefaultInitialize() const; 1477 1478 enum PrimitiveCopyKind { 1479 /// The type does not fall into any of the following categories. Note that 1480 /// this case is zero-valued so that values of this enum can be used as a 1481 /// boolean condition for non-triviality. 1482 PCK_Trivial, 1483 1484 /// The type would be trivial except that it is volatile-qualified. Types 1485 /// that fall into one of the other non-trivial cases may additionally be 1486 /// volatile-qualified. 1487 PCK_VolatileTrivial, 1488 1489 /// The type is an Objective-C retainable pointer type that is qualified 1490 /// with the ARC __strong qualifier. 1491 PCK_ARCStrong, 1492 1493 /// The type is an Objective-C retainable pointer type that is qualified 1494 /// with the ARC __weak qualifier. 1495 PCK_ARCWeak, 1496 1497 /// The type is a struct containing a field whose type is neither 1498 /// PCK_Trivial nor PCK_VolatileTrivial. 1499 /// Note that a C++ struct type does not necessarily match this; C++ copying 1500 /// semantics are too complex to express here, in part because they depend 1501 /// on the exact constructor or assignment operator that is chosen by 1502 /// overload resolution to do the copy. 1503 PCK_Struct 1504 }; 1505 1506 /// Check if this is a non-trivial type that would cause a C struct 1507 /// transitively containing this type to be non-trivial to copy and return the 1508 /// kind. 1509 PrimitiveCopyKind isNonTrivialToPrimitiveCopy() const; 1510 1511 /// Check if this is a non-trivial type that would cause a C struct 1512 /// transitively containing this type to be non-trivial to destructively 1513 /// move and return the kind. Destructive move in this context is a C++-style 1514 /// move in which the source object is placed in a valid but unspecified state 1515 /// after it is moved, as opposed to a truly destructive move in which the 1516 /// source object is placed in an uninitialized state. 1517 PrimitiveCopyKind isNonTrivialToPrimitiveDestructiveMove() const; 1518 1519 enum DestructionKind { 1520 DK_none, 1521 DK_cxx_destructor, 1522 DK_objc_strong_lifetime, 1523 DK_objc_weak_lifetime, 1524 DK_nontrivial_c_struct 1525 }; 1526 1527 /// Returns a nonzero value if objects of this type require 1528 /// non-trivial work to clean up after. Non-zero because it's 1529 /// conceivable that qualifiers (objc_gc(weak)?) could make 1530 /// something require destruction. 1531 DestructionKind isDestructedType() const { 1532 return isDestructedTypeImpl(*this); 1533 } 1534 1535 /// Check if this is or contains a C union that is non-trivial to 1536 /// default-initialize, which is a union that has a member that is non-trivial 1537 /// to default-initialize. If this returns true, 1538 /// isNonTrivialToPrimitiveDefaultInitialize returns PDIK_Struct. 1539 bool hasNonTrivialToPrimitiveDefaultInitializeCUnion() const; 1540 1541 /// Check if this is or contains a C union that is non-trivial to destruct, 1542 /// which is a union that has a member that is non-trivial to destruct. If 1543 /// this returns true, isDestructedType returns DK_nontrivial_c_struct. 1544 bool hasNonTrivialToPrimitiveDestructCUnion() const; 1545 1546 /// Check if this is or contains a C union that is non-trivial to copy, which 1547 /// is a union that has a member that is non-trivial to copy. If this returns 1548 /// true, isNonTrivialToPrimitiveCopy returns PCK_Struct. 1549 bool hasNonTrivialToPrimitiveCopyCUnion() const; 1550 1551 /// Determine whether expressions of the given type are forbidden 1552 /// from being lvalues in C. 1553 /// 1554 /// The expression types that are forbidden to be lvalues are: 1555 /// - 'void', but not qualified void 1556 /// - function types 1557 /// 1558 /// The exact rule here is C99 6.3.2.1: 1559 /// An lvalue is an expression with an object type or an incomplete 1560 /// type other than void. 1561 bool isCForbiddenLValueType() const; 1562 1563 /// Substitute type arguments for the Objective-C type parameters used in the 1564 /// subject type. 1565 /// 1566 /// \param ctx ASTContext in which the type exists. 1567 /// 1568 /// \param typeArgs The type arguments that will be substituted for the 1569 /// Objective-C type parameters in the subject type, which are generally 1570 /// computed via \c Type::getObjCSubstitutions. If empty, the type 1571 /// parameters will be replaced with their bounds or id/Class, as appropriate 1572 /// for the context. 1573 /// 1574 /// \param context The context in which the subject type was written. 1575 /// 1576 /// \returns the resulting type. 1577 QualType substObjCTypeArgs(ASTContext &ctx, 1578 ArrayRef<QualType> typeArgs, 1579 ObjCSubstitutionContext context) const; 1580 1581 /// Substitute type arguments from an object type for the Objective-C type 1582 /// parameters used in the subject type. 1583 /// 1584 /// This operation combines the computation of type arguments for 1585 /// substitution (\c Type::getObjCSubstitutions) with the actual process of 1586 /// substitution (\c QualType::substObjCTypeArgs) for the convenience of 1587 /// callers that need to perform a single substitution in isolation. 1588 /// 1589 /// \param objectType The type of the object whose member type we're 1590 /// substituting into. For example, this might be the receiver of a message 1591 /// or the base of a property access. 1592 /// 1593 /// \param dc The declaration context from which the subject type was 1594 /// retrieved, which indicates (for example) which type parameters should 1595 /// be substituted. 1596 /// 1597 /// \param context The context in which the subject type was written. 1598 /// 1599 /// \returns the subject type after replacing all of the Objective-C type 1600 /// parameters with their corresponding arguments. 1601 QualType substObjCMemberType(QualType objectType, 1602 const DeclContext *dc, 1603 ObjCSubstitutionContext context) const; 1604 1605 /// Strip Objective-C "__kindof" types from the given type. 1606 QualType stripObjCKindOfType(const ASTContext &ctx) const; 1607 1608 /// Remove all qualifiers including _Atomic. 1609 /// 1610 /// Like getUnqualifiedType(), the type may still be qualified if it is a 1611 /// sugared array type. To strip qualifiers even from within a sugared array 1612 /// type, use in conjunction with ASTContext::getUnqualifiedArrayType. 1613 QualType getAtomicUnqualifiedType() const; 1614 1615 private: 1616 // These methods are implemented in a separate translation unit; 1617 // "static"-ize them to avoid creating temporary QualTypes in the 1618 // caller. 1619 static bool isConstant(QualType T, const ASTContext& Ctx); 1620 static QualType getDesugaredType(QualType T, const ASTContext &Context); 1621 static SplitQualType getSplitDesugaredType(QualType T); 1622 static SplitQualType getSplitUnqualifiedTypeImpl(QualType type); 1623 static QualType getSingleStepDesugaredTypeImpl(QualType type, 1624 const ASTContext &C); 1625 static QualType IgnoreParens(QualType T); 1626 static DestructionKind isDestructedTypeImpl(QualType type); 1627 1628 /// Check if \param RD is or contains a non-trivial C union. 1629 static bool hasNonTrivialToPrimitiveDefaultInitializeCUnion(const RecordDecl *RD); 1630 static bool hasNonTrivialToPrimitiveDestructCUnion(const RecordDecl *RD); 1631 static bool hasNonTrivialToPrimitiveCopyCUnion(const RecordDecl *RD); 1632 }; 1633 1634 raw_ostream &operator<<(raw_ostream &OS, QualType QT); 1635 1636 } // namespace clang 1637 1638 namespace llvm { 1639 1640 /// Implement simplify_type for QualType, so that we can dyn_cast from QualType 1641 /// to a specific Type class. 1642 template<> struct simplify_type< ::clang::QualType> { 1643 using SimpleType = const ::clang::Type *; 1644 1645 static SimpleType getSimplifiedValue(::clang::QualType Val) { 1646 return Val.getTypePtr(); 1647 } 1648 }; 1649 1650 // Teach SmallPtrSet that QualType is "basically a pointer". 1651 template<> 1652 struct PointerLikeTypeTraits<clang::QualType> { 1653 static inline void *getAsVoidPointer(clang::QualType P) { 1654 return P.getAsOpaquePtr(); 1655 } 1656 1657 static inline clang::QualType getFromVoidPointer(void *P) { 1658 return clang::QualType::getFromOpaquePtr(P); 1659 } 1660 1661 // Various qualifiers go in low bits. 1662 static constexpr int NumLowBitsAvailable = 0; 1663 }; 1664 1665 } // namespace llvm 1666 1667 namespace clang { 1668 1669 /// Base class that is common to both the \c ExtQuals and \c Type 1670 /// classes, which allows \c QualType to access the common fields between the 1671 /// two. 1672 class ExtQualsTypeCommonBase { 1673 friend class ExtQuals; 1674 friend class QualType; 1675 friend class Type; 1676 friend class ASTReader; 1677 1678 /// The "base" type of an extended qualifiers type (\c ExtQuals) or 1679 /// a self-referential pointer (for \c Type). 1680 /// 1681 /// This pointer allows an efficient mapping from a QualType to its 1682 /// underlying type pointer. 1683 const Type *const BaseType; 1684 1685 /// The canonical type of this type. A QualType. 1686 QualType CanonicalType; 1687 1688 ExtQualsTypeCommonBase(const Type *baseType, QualType canon) 1689 : BaseType(baseType), CanonicalType(canon) {} 1690 }; 1691 1692 /// We can encode up to four bits in the low bits of a 1693 /// type pointer, but there are many more type qualifiers that we want 1694 /// to be able to apply to an arbitrary type. Therefore we have this 1695 /// struct, intended to be heap-allocated and used by QualType to 1696 /// store qualifiers. 1697 /// 1698 /// The current design tags the 'const', 'restrict', and 'volatile' qualifiers 1699 /// in three low bits on the QualType pointer; a fourth bit records whether 1700 /// the pointer is an ExtQuals node. The extended qualifiers (address spaces, 1701 /// Objective-C GC attributes) are much more rare. 1702 class alignas(TypeAlignment) ExtQuals : public ExtQualsTypeCommonBase, 1703 public llvm::FoldingSetNode { 1704 // NOTE: changing the fast qualifiers should be straightforward as 1705 // long as you don't make 'const' non-fast. 1706 // 1. Qualifiers: 1707 // a) Modify the bitmasks (Qualifiers::TQ and DeclSpec::TQ). 1708 // Fast qualifiers must occupy the low-order bits. 1709 // b) Update Qualifiers::FastWidth and FastMask. 1710 // 2. QualType: 1711 // a) Update is{Volatile,Restrict}Qualified(), defined inline. 1712 // b) Update remove{Volatile,Restrict}, defined near the end of 1713 // this header. 1714 // 3. ASTContext: 1715 // a) Update get{Volatile,Restrict}Type. 1716 1717 /// The immutable set of qualifiers applied by this node. Always contains 1718 /// extended qualifiers. 1719 Qualifiers Quals; 1720 1721 ExtQuals *this_() { return this; } 1722 1723 public: 1724 ExtQuals(const Type *baseType, QualType canon, Qualifiers quals) 1725 : ExtQualsTypeCommonBase(baseType, 1726 canon.isNull() ? QualType(this_(), 0) : canon), 1727 Quals(quals) { 1728 assert(Quals.hasNonFastQualifiers() 1729 && "ExtQuals created with no fast qualifiers"); 1730 assert(!Quals.hasFastQualifiers() 1731 && "ExtQuals created with fast qualifiers"); 1732 } 1733 1734 Qualifiers getQualifiers() const { return Quals; } 1735 1736 bool hasObjCGCAttr() const { return Quals.hasObjCGCAttr(); } 1737 Qualifiers::GC getObjCGCAttr() const { return Quals.getObjCGCAttr(); } 1738 1739 bool hasObjCLifetime() const { return Quals.hasObjCLifetime(); } 1740 Qualifiers::ObjCLifetime getObjCLifetime() const { 1741 return Quals.getObjCLifetime(); 1742 } 1743 1744 bool hasAddressSpace() const { return Quals.hasAddressSpace(); } 1745 LangAS getAddressSpace() const { return Quals.getAddressSpace(); } 1746 1747 const Type *getBaseType() const { return BaseType; } 1748 1749 public: 1750 void Profile(llvm::FoldingSetNodeID &ID) const { 1751 Profile(ID, getBaseType(), Quals); 1752 } 1753 1754 static void Profile(llvm::FoldingSetNodeID &ID, 1755 const Type *BaseType, 1756 Qualifiers Quals) { 1757 assert(!Quals.hasFastQualifiers() && "fast qualifiers in ExtQuals hash!"); 1758 ID.AddPointer(BaseType); 1759 Quals.Profile(ID); 1760 } 1761 }; 1762 1763 /// The kind of C++11 ref-qualifier associated with a function type. 1764 /// This determines whether a member function's "this" object can be an 1765 /// lvalue, rvalue, or neither. 1766 enum RefQualifierKind { 1767 /// No ref-qualifier was provided. 1768 RQ_None = 0, 1769 1770 /// An lvalue ref-qualifier was provided (\c &). 1771 RQ_LValue, 1772 1773 /// An rvalue ref-qualifier was provided (\c &&). 1774 RQ_RValue 1775 }; 1776 1777 /// Which keyword(s) were used to create an AutoType. 1778 enum class AutoTypeKeyword { 1779 /// auto 1780 Auto, 1781 1782 /// decltype(auto) 1783 DecltypeAuto, 1784 1785 /// __auto_type (GNU extension) 1786 GNUAutoType 1787 }; 1788 1789 enum class SubstTemplateTypeParmTypeFlag { 1790 None, 1791 1792 /// Whether to expand the pack using the stored PackIndex in place. This is 1793 /// useful for e.g. substituting into an atomic constraint expression, where 1794 /// that expression is part of an unexpanded pack. 1795 ExpandPacksInPlace, 1796 }; 1797 1798 enum class ArraySizeModifier; 1799 enum class ElaboratedTypeKeyword; 1800 enum class VectorKind; 1801 1802 /// The base class of the type hierarchy. 1803 /// 1804 /// A central concept with types is that each type always has a canonical 1805 /// type. A canonical type is the type with any typedef names stripped out 1806 /// of it or the types it references. For example, consider: 1807 /// 1808 /// typedef int foo; 1809 /// typedef foo* bar; 1810 /// 'int *' 'foo *' 'bar' 1811 /// 1812 /// There will be a Type object created for 'int'. Since int is canonical, its 1813 /// CanonicalType pointer points to itself. There is also a Type for 'foo' (a 1814 /// TypedefType). Its CanonicalType pointer points to the 'int' Type. Next 1815 /// there is a PointerType that represents 'int*', which, like 'int', is 1816 /// canonical. Finally, there is a PointerType type for 'foo*' whose canonical 1817 /// type is 'int*', and there is a TypedefType for 'bar', whose canonical type 1818 /// is also 'int*'. 1819 /// 1820 /// Non-canonical types are useful for emitting diagnostics, without losing 1821 /// information about typedefs being used. Canonical types are useful for type 1822 /// comparisons (they allow by-pointer equality tests) and useful for reasoning 1823 /// about whether something has a particular form (e.g. is a function type), 1824 /// because they implicitly, recursively, strip all typedefs out of a type. 1825 /// 1826 /// Types, once created, are immutable. 1827 /// 1828 class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase { 1829 public: 1830 enum TypeClass { 1831 #define TYPE(Class, Base) Class, 1832 #define LAST_TYPE(Class) TypeLast = Class 1833 #define ABSTRACT_TYPE(Class, Base) 1834 #include "clang/AST/TypeNodes.inc" 1835 }; 1836 1837 private: 1838 /// Bitfields required by the Type class. 1839 class TypeBitfields { 1840 friend class Type; 1841 template <class T> friend class TypePropertyCache; 1842 1843 /// TypeClass bitfield - Enum that specifies what subclass this belongs to. 1844 LLVM_PREFERRED_TYPE(TypeClass) 1845 unsigned TC : 8; 1846 1847 /// Store information on the type dependency. 1848 LLVM_PREFERRED_TYPE(TypeDependence) 1849 unsigned Dependence : llvm::BitWidth<TypeDependence>; 1850 1851 /// True if the cache (i.e. the bitfields here starting with 1852 /// 'Cache') is valid. 1853 LLVM_PREFERRED_TYPE(bool) 1854 mutable unsigned CacheValid : 1; 1855 1856 /// Linkage of this type. 1857 LLVM_PREFERRED_TYPE(Linkage) 1858 mutable unsigned CachedLinkage : 3; 1859 1860 /// Whether this type involves and local or unnamed types. 1861 LLVM_PREFERRED_TYPE(bool) 1862 mutable unsigned CachedLocalOrUnnamed : 1; 1863 1864 /// Whether this type comes from an AST file. 1865 LLVM_PREFERRED_TYPE(bool) 1866 mutable unsigned FromAST : 1; 1867 1868 bool isCacheValid() const { 1869 return CacheValid; 1870 } 1871 1872 Linkage getLinkage() const { 1873 assert(isCacheValid() && "getting linkage from invalid cache"); 1874 return static_cast<Linkage>(CachedLinkage); 1875 } 1876 1877 bool hasLocalOrUnnamedType() const { 1878 assert(isCacheValid() && "getting linkage from invalid cache"); 1879 return CachedLocalOrUnnamed; 1880 } 1881 }; 1882 enum { NumTypeBits = 8 + llvm::BitWidth<TypeDependence> + 6 }; 1883 1884 protected: 1885 // These classes allow subclasses to somewhat cleanly pack bitfields 1886 // into Type. 1887 1888 class ArrayTypeBitfields { 1889 friend class ArrayType; 1890 1891 LLVM_PREFERRED_TYPE(TypeBitfields) 1892 unsigned : NumTypeBits; 1893 1894 /// CVR qualifiers from declarations like 1895 /// 'int X[static restrict 4]'. For function parameters only. 1896 LLVM_PREFERRED_TYPE(Qualifiers) 1897 unsigned IndexTypeQuals : 3; 1898 1899 /// Storage class qualifiers from declarations like 1900 /// 'int X[static restrict 4]'. For function parameters only. 1901 LLVM_PREFERRED_TYPE(ArraySizeModifier) 1902 unsigned SizeModifier : 3; 1903 }; 1904 enum { NumArrayTypeBits = NumTypeBits + 6 }; 1905 1906 class ConstantArrayTypeBitfields { 1907 friend class ConstantArrayType; 1908 1909 LLVM_PREFERRED_TYPE(ArrayTypeBitfields) 1910 unsigned : NumArrayTypeBits; 1911 1912 /// Whether we have a stored size expression. 1913 LLVM_PREFERRED_TYPE(bool) 1914 unsigned HasExternalSize : 1; 1915 1916 LLVM_PREFERRED_TYPE(unsigned) 1917 unsigned SizeWidth : 5; 1918 }; 1919 1920 class BuiltinTypeBitfields { 1921 friend class BuiltinType; 1922 1923 LLVM_PREFERRED_TYPE(TypeBitfields) 1924 unsigned : NumTypeBits; 1925 1926 /// The kind (BuiltinType::Kind) of builtin type this is. 1927 static constexpr unsigned NumOfBuiltinTypeBits = 9; 1928 unsigned Kind : NumOfBuiltinTypeBits; 1929 }; 1930 1931 public: 1932 static constexpr int FunctionTypeNumParamsWidth = 16; 1933 static constexpr int FunctionTypeNumParamsLimit = (1 << 16) - 1; 1934 1935 protected: 1936 /// FunctionTypeBitfields store various bits belonging to FunctionProtoType. 1937 /// Only common bits are stored here. Additional uncommon bits are stored 1938 /// in a trailing object after FunctionProtoType. 1939 class FunctionTypeBitfields { 1940 friend class FunctionProtoType; 1941 friend class FunctionType; 1942 1943 LLVM_PREFERRED_TYPE(TypeBitfields) 1944 unsigned : NumTypeBits; 1945 1946 /// Extra information which affects how the function is called, like 1947 /// regparm and the calling convention. 1948 LLVM_PREFERRED_TYPE(CallingConv) 1949 unsigned ExtInfo : 13; 1950 1951 /// The ref-qualifier associated with a \c FunctionProtoType. 1952 /// 1953 /// This is a value of type \c RefQualifierKind. 1954 LLVM_PREFERRED_TYPE(RefQualifierKind) 1955 unsigned RefQualifier : 2; 1956 1957 /// Used only by FunctionProtoType, put here to pack with the 1958 /// other bitfields. 1959 /// The qualifiers are part of FunctionProtoType because... 1960 /// 1961 /// C++ 8.3.5p4: The return type, the parameter type list and the 1962 /// cv-qualifier-seq, [...], are part of the function type. 1963 LLVM_PREFERRED_TYPE(Qualifiers) 1964 unsigned FastTypeQuals : Qualifiers::FastWidth; 1965 /// Whether this function has extended Qualifiers. 1966 LLVM_PREFERRED_TYPE(bool) 1967 unsigned HasExtQuals : 1; 1968 1969 /// The number of parameters this function has, not counting '...'. 1970 /// According to [implimits] 8 bits should be enough here but this is 1971 /// somewhat easy to exceed with metaprogramming and so we would like to 1972 /// keep NumParams as wide as reasonably possible. 1973 unsigned NumParams : FunctionTypeNumParamsWidth; 1974 1975 /// The type of exception specification this function has. 1976 LLVM_PREFERRED_TYPE(ExceptionSpecificationType) 1977 unsigned ExceptionSpecType : 4; 1978 1979 /// Whether this function has extended parameter information. 1980 LLVM_PREFERRED_TYPE(bool) 1981 unsigned HasExtParameterInfos : 1; 1982 1983 /// Whether this function has extra bitfields for the prototype. 1984 LLVM_PREFERRED_TYPE(bool) 1985 unsigned HasExtraBitfields : 1; 1986 1987 /// Whether the function is variadic. 1988 LLVM_PREFERRED_TYPE(bool) 1989 unsigned Variadic : 1; 1990 1991 /// Whether this function has a trailing return type. 1992 LLVM_PREFERRED_TYPE(bool) 1993 unsigned HasTrailingReturn : 1; 1994 }; 1995 1996 class ObjCObjectTypeBitfields { 1997 friend class ObjCObjectType; 1998 1999 LLVM_PREFERRED_TYPE(TypeBitfields) 2000 unsigned : NumTypeBits; 2001 2002 /// The number of type arguments stored directly on this object type. 2003 unsigned NumTypeArgs : 7; 2004 2005 /// The number of protocols stored directly on this object type. 2006 unsigned NumProtocols : 6; 2007 2008 /// Whether this is a "kindof" type. 2009 LLVM_PREFERRED_TYPE(bool) 2010 unsigned IsKindOf : 1; 2011 }; 2012 2013 class ReferenceTypeBitfields { 2014 friend class ReferenceType; 2015 2016 LLVM_PREFERRED_TYPE(TypeBitfields) 2017 unsigned : NumTypeBits; 2018 2019 /// True if the type was originally spelled with an lvalue sigil. 2020 /// This is never true of rvalue references but can also be false 2021 /// on lvalue references because of C++0x [dcl.typedef]p9, 2022 /// as follows: 2023 /// 2024 /// typedef int &ref; // lvalue, spelled lvalue 2025 /// typedef int &&rvref; // rvalue 2026 /// ref &a; // lvalue, inner ref, spelled lvalue 2027 /// ref &&a; // lvalue, inner ref 2028 /// rvref &a; // lvalue, inner ref, spelled lvalue 2029 /// rvref &&a; // rvalue, inner ref 2030 LLVM_PREFERRED_TYPE(bool) 2031 unsigned SpelledAsLValue : 1; 2032 2033 /// True if the inner type is a reference type. This only happens 2034 /// in non-canonical forms. 2035 LLVM_PREFERRED_TYPE(bool) 2036 unsigned InnerRef : 1; 2037 }; 2038 2039 class TypeWithKeywordBitfields { 2040 friend class TypeWithKeyword; 2041 2042 LLVM_PREFERRED_TYPE(TypeBitfields) 2043 unsigned : NumTypeBits; 2044 2045 /// An ElaboratedTypeKeyword. 8 bits for efficient access. 2046 LLVM_PREFERRED_TYPE(ElaboratedTypeKeyword) 2047 unsigned Keyword : 8; 2048 }; 2049 2050 enum { NumTypeWithKeywordBits = NumTypeBits + 8 }; 2051 2052 class ElaboratedTypeBitfields { 2053 friend class ElaboratedType; 2054 2055 LLVM_PREFERRED_TYPE(TypeWithKeywordBitfields) 2056 unsigned : NumTypeWithKeywordBits; 2057 2058 /// Whether the ElaboratedType has a trailing OwnedTagDecl. 2059 LLVM_PREFERRED_TYPE(bool) 2060 unsigned HasOwnedTagDecl : 1; 2061 }; 2062 2063 class VectorTypeBitfields { 2064 friend class VectorType; 2065 friend class DependentVectorType; 2066 2067 LLVM_PREFERRED_TYPE(TypeBitfields) 2068 unsigned : NumTypeBits; 2069 2070 /// The kind of vector, either a generic vector type or some 2071 /// target-specific vector type such as for AltiVec or Neon. 2072 LLVM_PREFERRED_TYPE(VectorKind) 2073 unsigned VecKind : 4; 2074 /// The number of elements in the vector. 2075 uint32_t NumElements; 2076 }; 2077 2078 class AttributedTypeBitfields { 2079 friend class AttributedType; 2080 2081 LLVM_PREFERRED_TYPE(TypeBitfields) 2082 unsigned : NumTypeBits; 2083 2084 LLVM_PREFERRED_TYPE(attr::Kind) 2085 unsigned AttrKind : 32 - NumTypeBits; 2086 }; 2087 2088 class AutoTypeBitfields { 2089 friend class AutoType; 2090 2091 LLVM_PREFERRED_TYPE(TypeBitfields) 2092 unsigned : NumTypeBits; 2093 2094 /// Was this placeholder type spelled as 'auto', 'decltype(auto)', 2095 /// or '__auto_type'? AutoTypeKeyword value. 2096 LLVM_PREFERRED_TYPE(AutoTypeKeyword) 2097 unsigned Keyword : 2; 2098 2099 /// The number of template arguments in the type-constraints, which is 2100 /// expected to be able to hold at least 1024 according to [implimits]. 2101 /// However as this limit is somewhat easy to hit with template 2102 /// metaprogramming we'd prefer to keep it as large as possible. 2103 /// At the moment it has been left as a non-bitfield since this type 2104 /// safely fits in 64 bits as an unsigned, so there is no reason to 2105 /// introduce the performance impact of a bitfield. 2106 unsigned NumArgs; 2107 }; 2108 2109 class TypeOfBitfields { 2110 friend class TypeOfType; 2111 friend class TypeOfExprType; 2112 2113 LLVM_PREFERRED_TYPE(TypeBitfields) 2114 unsigned : NumTypeBits; 2115 LLVM_PREFERRED_TYPE(TypeOfKind) 2116 unsigned Kind : 1; 2117 }; 2118 2119 class UsingBitfields { 2120 friend class UsingType; 2121 2122 LLVM_PREFERRED_TYPE(TypeBitfields) 2123 unsigned : NumTypeBits; 2124 2125 /// True if the underlying type is different from the declared one. 2126 LLVM_PREFERRED_TYPE(bool) 2127 unsigned hasTypeDifferentFromDecl : 1; 2128 }; 2129 2130 class TypedefBitfields { 2131 friend class TypedefType; 2132 2133 LLVM_PREFERRED_TYPE(TypeBitfields) 2134 unsigned : NumTypeBits; 2135 2136 /// True if the underlying type is different from the declared one. 2137 LLVM_PREFERRED_TYPE(bool) 2138 unsigned hasTypeDifferentFromDecl : 1; 2139 }; 2140 2141 class TemplateTypeParmTypeBitfields { 2142 friend class TemplateTypeParmType; 2143 2144 LLVM_PREFERRED_TYPE(TypeBitfields) 2145 unsigned : NumTypeBits; 2146 2147 /// The depth of the template parameter. 2148 unsigned Depth : 15; 2149 2150 /// Whether this is a template parameter pack. 2151 LLVM_PREFERRED_TYPE(bool) 2152 unsigned ParameterPack : 1; 2153 2154 /// The index of the template parameter. 2155 unsigned Index : 16; 2156 }; 2157 2158 class SubstTemplateTypeParmTypeBitfields { 2159 friend class SubstTemplateTypeParmType; 2160 2161 LLVM_PREFERRED_TYPE(TypeBitfields) 2162 unsigned : NumTypeBits; 2163 2164 LLVM_PREFERRED_TYPE(bool) 2165 unsigned HasNonCanonicalUnderlyingType : 1; 2166 2167 LLVM_PREFERRED_TYPE(SubstTemplateTypeParmTypeFlag) 2168 unsigned SubstitutionFlag : 1; 2169 2170 // The index of the template parameter this substitution represents. 2171 unsigned Index : 15; 2172 2173 /// Represents the index within a pack if this represents a substitution 2174 /// from a pack expansion. This index starts at the end of the pack and 2175 /// increments towards the beginning. 2176 /// Positive non-zero number represents the index + 1. 2177 /// Zero means this is not substituted from an expansion. 2178 unsigned PackIndex : 16; 2179 }; 2180 2181 class SubstTemplateTypeParmPackTypeBitfields { 2182 friend class SubstTemplateTypeParmPackType; 2183 2184 LLVM_PREFERRED_TYPE(TypeBitfields) 2185 unsigned : NumTypeBits; 2186 2187 // The index of the template parameter this substitution represents. 2188 unsigned Index : 16; 2189 2190 /// The number of template arguments in \c Arguments, which is 2191 /// expected to be able to hold at least 1024 according to [implimits]. 2192 /// However as this limit is somewhat easy to hit with template 2193 /// metaprogramming we'd prefer to keep it as large as possible. 2194 unsigned NumArgs : 16; 2195 }; 2196 2197 class TemplateSpecializationTypeBitfields { 2198 friend class TemplateSpecializationType; 2199 2200 LLVM_PREFERRED_TYPE(TypeBitfields) 2201 unsigned : NumTypeBits; 2202 2203 /// Whether this template specialization type is a substituted type alias. 2204 LLVM_PREFERRED_TYPE(bool) 2205 unsigned TypeAlias : 1; 2206 2207 /// The number of template arguments named in this class template 2208 /// specialization, which is expected to be able to hold at least 1024 2209 /// according to [implimits]. However, as this limit is somewhat easy to 2210 /// hit with template metaprogramming we'd prefer to keep it as large 2211 /// as possible. At the moment it has been left as a non-bitfield since 2212 /// this type safely fits in 64 bits as an unsigned, so there is no reason 2213 /// to introduce the performance impact of a bitfield. 2214 unsigned NumArgs; 2215 }; 2216 2217 class DependentTemplateSpecializationTypeBitfields { 2218 friend class DependentTemplateSpecializationType; 2219 2220 LLVM_PREFERRED_TYPE(TypeWithKeywordBitfields) 2221 unsigned : NumTypeWithKeywordBits; 2222 2223 /// The number of template arguments named in this class template 2224 /// specialization, which is expected to be able to hold at least 1024 2225 /// according to [implimits]. However, as this limit is somewhat easy to 2226 /// hit with template metaprogramming we'd prefer to keep it as large 2227 /// as possible. At the moment it has been left as a non-bitfield since 2228 /// this type safely fits in 64 bits as an unsigned, so there is no reason 2229 /// to introduce the performance impact of a bitfield. 2230 unsigned NumArgs; 2231 }; 2232 2233 class PackExpansionTypeBitfields { 2234 friend class PackExpansionType; 2235 2236 LLVM_PREFERRED_TYPE(TypeBitfields) 2237 unsigned : NumTypeBits; 2238 2239 /// The number of expansions that this pack expansion will 2240 /// generate when substituted (+1), which is expected to be able to 2241 /// hold at least 1024 according to [implimits]. However, as this limit 2242 /// is somewhat easy to hit with template metaprogramming we'd prefer to 2243 /// keep it as large as possible. At the moment it has been left as a 2244 /// non-bitfield since this type safely fits in 64 bits as an unsigned, so 2245 /// there is no reason to introduce the performance impact of a bitfield. 2246 /// 2247 /// This field will only have a non-zero value when some of the parameter 2248 /// packs that occur within the pattern have been substituted but others 2249 /// have not. 2250 unsigned NumExpansions; 2251 }; 2252 2253 class CountAttributedTypeBitfields { 2254 friend class CountAttributedType; 2255 2256 LLVM_PREFERRED_TYPE(TypeBitfields) 2257 unsigned : NumTypeBits; 2258 2259 static constexpr unsigned NumCoupledDeclsBits = 4; 2260 unsigned NumCoupledDecls : NumCoupledDeclsBits; 2261 LLVM_PREFERRED_TYPE(bool) 2262 unsigned CountInBytes : 1; 2263 LLVM_PREFERRED_TYPE(bool) 2264 unsigned OrNull : 1; 2265 }; 2266 static_assert(sizeof(CountAttributedTypeBitfields) <= sizeof(unsigned)); 2267 2268 union { 2269 TypeBitfields TypeBits; 2270 ArrayTypeBitfields ArrayTypeBits; 2271 ConstantArrayTypeBitfields ConstantArrayTypeBits; 2272 AttributedTypeBitfields AttributedTypeBits; 2273 AutoTypeBitfields AutoTypeBits; 2274 TypeOfBitfields TypeOfBits; 2275 TypedefBitfields TypedefBits; 2276 UsingBitfields UsingBits; 2277 BuiltinTypeBitfields BuiltinTypeBits; 2278 FunctionTypeBitfields FunctionTypeBits; 2279 ObjCObjectTypeBitfields ObjCObjectTypeBits; 2280 ReferenceTypeBitfields ReferenceTypeBits; 2281 TypeWithKeywordBitfields TypeWithKeywordBits; 2282 ElaboratedTypeBitfields ElaboratedTypeBits; 2283 VectorTypeBitfields VectorTypeBits; 2284 TemplateTypeParmTypeBitfields TemplateTypeParmTypeBits; 2285 SubstTemplateTypeParmTypeBitfields SubstTemplateTypeParmTypeBits; 2286 SubstTemplateTypeParmPackTypeBitfields SubstTemplateTypeParmPackTypeBits; 2287 TemplateSpecializationTypeBitfields TemplateSpecializationTypeBits; 2288 DependentTemplateSpecializationTypeBitfields 2289 DependentTemplateSpecializationTypeBits; 2290 PackExpansionTypeBitfields PackExpansionTypeBits; 2291 CountAttributedTypeBitfields CountAttributedTypeBits; 2292 }; 2293 2294 private: 2295 template <class T> friend class TypePropertyCache; 2296 2297 /// Set whether this type comes from an AST file. 2298 void setFromAST(bool V = true) const { 2299 TypeBits.FromAST = V; 2300 } 2301 2302 protected: 2303 friend class ASTContext; 2304 2305 Type(TypeClass tc, QualType canon, TypeDependence Dependence) 2306 : ExtQualsTypeCommonBase(this, 2307 canon.isNull() ? QualType(this_(), 0) : canon) { 2308 static_assert(sizeof(*this) <= 2309 alignof(decltype(*this)) + sizeof(ExtQualsTypeCommonBase), 2310 "changing bitfields changed sizeof(Type)!"); 2311 static_assert(alignof(decltype(*this)) % TypeAlignment == 0, 2312 "Insufficient alignment!"); 2313 TypeBits.TC = tc; 2314 TypeBits.Dependence = static_cast<unsigned>(Dependence); 2315 TypeBits.CacheValid = false; 2316 TypeBits.CachedLocalOrUnnamed = false; 2317 TypeBits.CachedLinkage = llvm::to_underlying(Linkage::Invalid); 2318 TypeBits.FromAST = false; 2319 } 2320 2321 // silence VC++ warning C4355: 'this' : used in base member initializer list 2322 Type *this_() { return this; } 2323 2324 void setDependence(TypeDependence D) { 2325 TypeBits.Dependence = static_cast<unsigned>(D); 2326 } 2327 2328 void addDependence(TypeDependence D) { setDependence(getDependence() | D); } 2329 2330 public: 2331 friend class ASTReader; 2332 friend class ASTWriter; 2333 template <class T> friend class serialization::AbstractTypeReader; 2334 template <class T> friend class serialization::AbstractTypeWriter; 2335 2336 Type(const Type &) = delete; 2337 Type(Type &&) = delete; 2338 Type &operator=(const Type &) = delete; 2339 Type &operator=(Type &&) = delete; 2340 2341 TypeClass getTypeClass() const { return static_cast<TypeClass>(TypeBits.TC); } 2342 2343 /// Whether this type comes from an AST file. 2344 bool isFromAST() const { return TypeBits.FromAST; } 2345 2346 /// Whether this type is or contains an unexpanded parameter 2347 /// pack, used to support C++0x variadic templates. 2348 /// 2349 /// A type that contains a parameter pack shall be expanded by the 2350 /// ellipsis operator at some point. For example, the typedef in the 2351 /// following example contains an unexpanded parameter pack 'T': 2352 /// 2353 /// \code 2354 /// template<typename ...T> 2355 /// struct X { 2356 /// typedef T* pointer_types; // ill-formed; T is a parameter pack. 2357 /// }; 2358 /// \endcode 2359 /// 2360 /// Note that this routine does not specify which 2361 bool containsUnexpandedParameterPack() const { 2362 return getDependence() & TypeDependence::UnexpandedPack; 2363 } 2364 2365 /// Determines if this type would be canonical if it had no further 2366 /// qualification. 2367 bool isCanonicalUnqualified() const { 2368 return CanonicalType == QualType(this, 0); 2369 } 2370 2371 /// Pull a single level of sugar off of this locally-unqualified type. 2372 /// Users should generally prefer SplitQualType::getSingleStepDesugaredType() 2373 /// or QualType::getSingleStepDesugaredType(const ASTContext&). 2374 QualType getLocallyUnqualifiedSingleStepDesugaredType() const; 2375 2376 /// As an extension, we classify types as one of "sized" or "sizeless"; 2377 /// every type is one or the other. Standard types are all sized; 2378 /// sizeless types are purely an extension. 2379 /// 2380 /// Sizeless types contain data with no specified size, alignment, 2381 /// or layout. 2382 bool isSizelessType() const; 2383 bool isSizelessBuiltinType() const; 2384 2385 /// Returns true for all scalable vector types. 2386 bool isSizelessVectorType() const; 2387 2388 /// Returns true for SVE scalable vector types. 2389 bool isSVESizelessBuiltinType() const; 2390 2391 /// Returns true for RVV scalable vector types. 2392 bool isRVVSizelessBuiltinType() const; 2393 2394 /// Check if this is a WebAssembly Externref Type. 2395 bool isWebAssemblyExternrefType() const; 2396 2397 /// Returns true if this is a WebAssembly table type: either an array of 2398 /// reference types, or a pointer to a reference type (which can only be 2399 /// created by array to pointer decay). 2400 bool isWebAssemblyTableType() const; 2401 2402 /// Determines if this is a sizeless type supported by the 2403 /// 'arm_sve_vector_bits' type attribute, which can be applied to a single 2404 /// SVE vector or predicate, excluding tuple types such as svint32x4_t. 2405 bool isSveVLSBuiltinType() const; 2406 2407 /// Returns the representative type for the element of an SVE builtin type. 2408 /// This is used to represent fixed-length SVE vectors created with the 2409 /// 'arm_sve_vector_bits' type attribute as VectorType. 2410 QualType getSveEltType(const ASTContext &Ctx) const; 2411 2412 /// Determines if this is a sizeless type supported by the 2413 /// 'riscv_rvv_vector_bits' type attribute, which can be applied to a single 2414 /// RVV vector or mask. 2415 bool isRVVVLSBuiltinType() const; 2416 2417 /// Returns the representative type for the element of an RVV builtin type. 2418 /// This is used to represent fixed-length RVV vectors created with the 2419 /// 'riscv_rvv_vector_bits' type attribute as VectorType. 2420 QualType getRVVEltType(const ASTContext &Ctx) const; 2421 2422 /// Returns the representative type for the element of a sizeless vector 2423 /// builtin type. 2424 QualType getSizelessVectorEltType(const ASTContext &Ctx) const; 2425 2426 /// Types are partitioned into 3 broad categories (C99 6.2.5p1): 2427 /// object types, function types, and incomplete types. 2428 2429 /// Return true if this is an incomplete type. 2430 /// A type that can describe objects, but which lacks information needed to 2431 /// determine its size (e.g. void, or a fwd declared struct). Clients of this 2432 /// routine will need to determine if the size is actually required. 2433 /// 2434 /// Def If non-null, and the type refers to some kind of declaration 2435 /// that can be completed (such as a C struct, C++ class, or Objective-C 2436 /// class), will be set to the declaration. 2437 bool isIncompleteType(NamedDecl **Def = nullptr) const; 2438 2439 /// Return true if this is an incomplete or object 2440 /// type, in other words, not a function type. 2441 bool isIncompleteOrObjectType() const { 2442 return !isFunctionType(); 2443 } 2444 2445 /// Determine whether this type is an object type. 2446 bool isObjectType() const { 2447 // C++ [basic.types]p8: 2448 // An object type is a (possibly cv-qualified) type that is not a 2449 // function type, not a reference type, and not a void type. 2450 return !isReferenceType() && !isFunctionType() && !isVoidType(); 2451 } 2452 2453 /// Return true if this is a literal type 2454 /// (C++11 [basic.types]p10) 2455 bool isLiteralType(const ASTContext &Ctx) const; 2456 2457 /// Determine if this type is a structural type, per C++20 [temp.param]p7. 2458 bool isStructuralType() const; 2459 2460 /// Test if this type is a standard-layout type. 2461 /// (C++0x [basic.type]p9) 2462 bool isStandardLayoutType() const; 2463 2464 /// Helper methods to distinguish type categories. All type predicates 2465 /// operate on the canonical type, ignoring typedefs and qualifiers. 2466 2467 /// Returns true if the type is a builtin type. 2468 bool isBuiltinType() const; 2469 2470 /// Test for a particular builtin type. 2471 bool isSpecificBuiltinType(unsigned K) const; 2472 2473 /// Test for a type which does not represent an actual type-system type but 2474 /// is instead used as a placeholder for various convenient purposes within 2475 /// Clang. All such types are BuiltinTypes. 2476 bool isPlaceholderType() const; 2477 const BuiltinType *getAsPlaceholderType() const; 2478 2479 /// Test for a specific placeholder type. 2480 bool isSpecificPlaceholderType(unsigned K) const; 2481 2482 /// Test for a placeholder type other than Overload; see 2483 /// BuiltinType::isNonOverloadPlaceholderType. 2484 bool isNonOverloadPlaceholderType() const; 2485 2486 /// isIntegerType() does *not* include complex integers (a GCC extension). 2487 /// isComplexIntegerType() can be used to test for complex integers. 2488 bool isIntegerType() const; // C99 6.2.5p17 (int, char, bool, enum) 2489 bool isEnumeralType() const; 2490 2491 /// Determine whether this type is a scoped enumeration type. 2492 bool isScopedEnumeralType() const; 2493 bool isBooleanType() const; 2494 bool isCharType() const; 2495 bool isWideCharType() const; 2496 bool isChar8Type() const; 2497 bool isChar16Type() const; 2498 bool isChar32Type() const; 2499 bool isAnyCharacterType() const; 2500 bool isIntegralType(const ASTContext &Ctx) const; 2501 2502 /// Determine whether this type is an integral or enumeration type. 2503 bool isIntegralOrEnumerationType() const; 2504 2505 /// Determine whether this type is an integral or unscoped enumeration type. 2506 bool isIntegralOrUnscopedEnumerationType() const; 2507 bool isUnscopedEnumerationType() const; 2508 2509 /// Floating point categories. 2510 bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double) 2511 /// isComplexType() does *not* include complex integers (a GCC extension). 2512 /// isComplexIntegerType() can be used to test for complex integers. 2513 bool isComplexType() const; // C99 6.2.5p11 (complex) 2514 bool isAnyComplexType() const; // C99 6.2.5p11 (complex) + Complex Int. 2515 bool isFloatingType() const; // C99 6.2.5p11 (real floating + complex) 2516 bool isHalfType() const; // OpenCL 6.1.1.1, NEON (IEEE 754-2008 half) 2517 bool isFloat16Type() const; // C11 extension ISO/IEC TS 18661 2518 bool isFloat32Type() const; 2519 bool isDoubleType() const; 2520 bool isBFloat16Type() const; 2521 bool isMFloat8Type() const; 2522 bool isFloat128Type() const; 2523 bool isIbm128Type() const; 2524 bool isRealType() const; // C99 6.2.5p17 (real floating + integer) 2525 bool isArithmeticType() const; // C99 6.2.5p18 (integer + floating) 2526 bool isVoidType() const; // C99 6.2.5p19 2527 bool isScalarType() const; // C99 6.2.5p21 (arithmetic + pointers) 2528 bool isAggregateType() const; 2529 bool isFundamentalType() const; 2530 bool isCompoundType() const; 2531 2532 // Type Predicates: Check to see if this type is structurally the specified 2533 // type, ignoring typedefs and qualifiers. 2534 bool isFunctionType() const; 2535 bool isFunctionNoProtoType() const { return getAs<FunctionNoProtoType>(); } 2536 bool isFunctionProtoType() const { return getAs<FunctionProtoType>(); } 2537 bool isPointerType() const; 2538 bool isPointerOrReferenceType() const; 2539 bool isSignableType() const; 2540 bool isAnyPointerType() const; // Any C pointer or ObjC object pointer 2541 bool isCountAttributedType() const; 2542 bool isBlockPointerType() const; 2543 bool isVoidPointerType() const; 2544 bool isReferenceType() const; 2545 bool isLValueReferenceType() const; 2546 bool isRValueReferenceType() const; 2547 bool isObjectPointerType() const; 2548 bool isFunctionPointerType() const; 2549 bool isFunctionReferenceType() const; 2550 bool isMemberPointerType() const; 2551 bool isMemberFunctionPointerType() const; 2552 bool isMemberDataPointerType() const; 2553 bool isArrayType() const; 2554 bool isConstantArrayType() const; 2555 bool isIncompleteArrayType() const; 2556 bool isVariableArrayType() const; 2557 bool isArrayParameterType() const; 2558 bool isDependentSizedArrayType() const; 2559 bool isRecordType() const; 2560 bool isClassType() const; 2561 bool isStructureType() const; 2562 bool isStructureTypeWithFlexibleArrayMember() const; 2563 bool isObjCBoxableRecordType() const; 2564 bool isInterfaceType() const; 2565 bool isStructureOrClassType() const; 2566 bool isUnionType() const; 2567 bool isComplexIntegerType() const; // GCC _Complex integer type. 2568 bool isVectorType() const; // GCC vector type. 2569 bool isExtVectorType() const; // Extended vector type. 2570 bool isExtVectorBoolType() const; // Extended vector type with bool element. 2571 bool isSubscriptableVectorType() const; 2572 bool isMatrixType() const; // Matrix type. 2573 bool isConstantMatrixType() const; // Constant matrix type. 2574 bool isDependentAddressSpaceType() const; // value-dependent address space qualifier 2575 bool isObjCObjectPointerType() const; // pointer to ObjC object 2576 bool isObjCRetainableType() const; // ObjC object or block pointer 2577 bool isObjCLifetimeType() const; // (array of)* retainable type 2578 bool isObjCIndirectLifetimeType() const; // (pointer to)* lifetime type 2579 bool isObjCNSObjectType() const; // __attribute__((NSObject)) 2580 bool isObjCIndependentClassType() const; // __attribute__((objc_independent_class)) 2581 // FIXME: change this to 'raw' interface type, so we can used 'interface' type 2582 // for the common case. 2583 bool isObjCObjectType() const; // NSString or typeof(*(id)0) 2584 bool isObjCQualifiedInterfaceType() const; // NSString<foo> 2585 bool isObjCQualifiedIdType() const; // id<foo> 2586 bool isObjCQualifiedClassType() const; // Class<foo> 2587 bool isObjCObjectOrInterfaceType() const; 2588 bool isObjCIdType() const; // id 2589 bool isDecltypeType() const; 2590 /// Was this type written with the special inert-in-ARC __unsafe_unretained 2591 /// qualifier? 2592 /// 2593 /// This approximates the answer to the following question: if this 2594 /// translation unit were compiled in ARC, would this type be qualified 2595 /// with __unsafe_unretained? 2596 bool isObjCInertUnsafeUnretainedType() const { 2597 return hasAttr(attr::ObjCInertUnsafeUnretained); 2598 } 2599 2600 /// Whether the type is Objective-C 'id' or a __kindof type of an 2601 /// object type, e.g., __kindof NSView * or __kindof id 2602 /// <NSCopying>. 2603 /// 2604 /// \param bound Will be set to the bound on non-id subtype types, 2605 /// which will be (possibly specialized) Objective-C class type, or 2606 /// null for 'id. 2607 bool isObjCIdOrObjectKindOfType(const ASTContext &ctx, 2608 const ObjCObjectType *&bound) const; 2609 2610 bool isObjCClassType() const; // Class 2611 2612 /// Whether the type is Objective-C 'Class' or a __kindof type of an 2613 /// Class type, e.g., __kindof Class <NSCopying>. 2614 /// 2615 /// Unlike \c isObjCIdOrObjectKindOfType, there is no relevant bound 2616 /// here because Objective-C's type system cannot express "a class 2617 /// object for a subclass of NSFoo". 2618 bool isObjCClassOrClassKindOfType() const; 2619 2620 bool isBlockCompatibleObjCPointerType(ASTContext &ctx) const; 2621 bool isObjCSelType() const; // Class 2622 bool isObjCBuiltinType() const; // 'id' or 'Class' 2623 bool isObjCARCBridgableType() const; 2624 bool isCARCBridgableType() const; 2625 bool isTemplateTypeParmType() const; // C++ template type parameter 2626 bool isNullPtrType() const; // C++11 std::nullptr_t or 2627 // C23 nullptr_t 2628 bool isNothrowT() const; // C++ std::nothrow_t 2629 bool isAlignValT() const; // C++17 std::align_val_t 2630 bool isStdByteType() const; // C++17 std::byte 2631 bool isAtomicType() const; // C11 _Atomic() 2632 bool isUndeducedAutoType() const; // C++11 auto or 2633 // C++14 decltype(auto) 2634 bool isTypedefNameType() const; // typedef or alias template 2635 2636 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 2637 bool is##Id##Type() const; 2638 #include "clang/Basic/OpenCLImageTypes.def" 2639 2640 bool isImageType() const; // Any OpenCL image type 2641 2642 bool isSamplerT() const; // OpenCL sampler_t 2643 bool isEventT() const; // OpenCL event_t 2644 bool isClkEventT() const; // OpenCL clk_event_t 2645 bool isQueueT() const; // OpenCL queue_t 2646 bool isReserveIDT() const; // OpenCL reserve_id_t 2647 2648 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 2649 bool is##Id##Type() const; 2650 #include "clang/Basic/OpenCLExtensionTypes.def" 2651 // Type defined in cl_intel_device_side_avc_motion_estimation OpenCL extension 2652 bool isOCLIntelSubgroupAVCType() const; 2653 bool isOCLExtOpaqueType() const; // Any OpenCL extension type 2654 2655 bool isPipeType() const; // OpenCL pipe type 2656 bool isBitIntType() const; // Bit-precise integer type 2657 bool isOpenCLSpecificType() const; // Any OpenCL specific type 2658 2659 #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) bool is##Id##Type() const; 2660 #include "clang/Basic/HLSLIntangibleTypes.def" 2661 bool isHLSLSpecificType() const; // Any HLSL specific type 2662 bool isHLSLBuiltinIntangibleType() const; // Any HLSL builtin intangible type 2663 bool isHLSLAttributedResourceType() const; 2664 bool isHLSLIntangibleType() 2665 const; // Any HLSL intangible type (builtin, array, class) 2666 2667 /// Determines if this type, which must satisfy 2668 /// isObjCLifetimeType(), is implicitly __unsafe_unretained rather 2669 /// than implicitly __strong. 2670 bool isObjCARCImplicitlyUnretainedType() const; 2671 2672 /// Check if the type is the CUDA device builtin surface type. 2673 bool isCUDADeviceBuiltinSurfaceType() const; 2674 /// Check if the type is the CUDA device builtin texture type. 2675 bool isCUDADeviceBuiltinTextureType() const; 2676 2677 /// Return the implicit lifetime for this type, which must not be dependent. 2678 Qualifiers::ObjCLifetime getObjCARCImplicitLifetime() const; 2679 2680 enum ScalarTypeKind { 2681 STK_CPointer, 2682 STK_BlockPointer, 2683 STK_ObjCObjectPointer, 2684 STK_MemberPointer, 2685 STK_Bool, 2686 STK_Integral, 2687 STK_Floating, 2688 STK_IntegralComplex, 2689 STK_FloatingComplex, 2690 STK_FixedPoint 2691 }; 2692 2693 /// Given that this is a scalar type, classify it. 2694 ScalarTypeKind getScalarTypeKind() const; 2695 2696 TypeDependence getDependence() const { 2697 return static_cast<TypeDependence>(TypeBits.Dependence); 2698 } 2699 2700 /// Whether this type is an error type. 2701 bool containsErrors() const { 2702 return getDependence() & TypeDependence::Error; 2703 } 2704 2705 /// Whether this type is a dependent type, meaning that its definition 2706 /// somehow depends on a template parameter (C++ [temp.dep.type]). 2707 bool isDependentType() const { 2708 return getDependence() & TypeDependence::Dependent; 2709 } 2710 2711 /// Determine whether this type is an instantiation-dependent type, 2712 /// meaning that the type involves a template parameter (even if the 2713 /// definition does not actually depend on the type substituted for that 2714 /// template parameter). 2715 bool isInstantiationDependentType() const { 2716 return getDependence() & TypeDependence::Instantiation; 2717 } 2718 2719 /// Determine whether this type is an undeduced type, meaning that 2720 /// it somehow involves a C++11 'auto' type or similar which has not yet been 2721 /// deduced. 2722 bool isUndeducedType() const; 2723 2724 /// Whether this type is a variably-modified type (C99 6.7.5). 2725 bool isVariablyModifiedType() const { 2726 return getDependence() & TypeDependence::VariablyModified; 2727 } 2728 2729 /// Whether this type involves a variable-length array type 2730 /// with a definite size. 2731 bool hasSizedVLAType() const; 2732 2733 /// Whether this type is or contains a local or unnamed type. 2734 bool hasUnnamedOrLocalType() const; 2735 2736 bool isOverloadableType() const; 2737 2738 /// Determine wither this type is a C++ elaborated-type-specifier. 2739 bool isElaboratedTypeSpecifier() const; 2740 2741 bool canDecayToPointerType() const; 2742 2743 /// Whether this type is represented natively as a pointer. This includes 2744 /// pointers, references, block pointers, and Objective-C interface, 2745 /// qualified id, and qualified interface types, as well as nullptr_t. 2746 bool hasPointerRepresentation() const; 2747 2748 /// Whether this type can represent an objective pointer type for the 2749 /// purpose of GC'ability 2750 bool hasObjCPointerRepresentation() const; 2751 2752 /// Determine whether this type has an integer representation 2753 /// of some sort, e.g., it is an integer type or a vector. 2754 bool hasIntegerRepresentation() const; 2755 2756 /// Determine whether this type has an signed integer representation 2757 /// of some sort, e.g., it is an signed integer type or a vector. 2758 bool hasSignedIntegerRepresentation() const; 2759 2760 /// Determine whether this type has an unsigned integer representation 2761 /// of some sort, e.g., it is an unsigned integer type or a vector. 2762 bool hasUnsignedIntegerRepresentation() const; 2763 2764 /// Determine whether this type has a floating-point representation 2765 /// of some sort, e.g., it is a floating-point type or a vector thereof. 2766 bool hasFloatingRepresentation() const; 2767 2768 // Type Checking Functions: Check to see if this type is structurally the 2769 // specified type, ignoring typedefs and qualifiers, and return a pointer to 2770 // the best type we can. 2771 const RecordType *getAsStructureType() const; 2772 /// NOTE: getAs*ArrayType are methods on ASTContext. 2773 const RecordType *getAsUnionType() const; 2774 const ComplexType *getAsComplexIntegerType() const; // GCC complex int type. 2775 const ObjCObjectType *getAsObjCInterfaceType() const; 2776 2777 // The following is a convenience method that returns an ObjCObjectPointerType 2778 // for object declared using an interface. 2779 const ObjCObjectPointerType *getAsObjCInterfacePointerType() const; 2780 const ObjCObjectPointerType *getAsObjCQualifiedIdType() const; 2781 const ObjCObjectPointerType *getAsObjCQualifiedClassType() const; 2782 const ObjCObjectType *getAsObjCQualifiedInterfaceType() const; 2783 2784 /// Retrieves the CXXRecordDecl that this type refers to, either 2785 /// because the type is a RecordType or because it is the injected-class-name 2786 /// type of a class template or class template partial specialization. 2787 CXXRecordDecl *getAsCXXRecordDecl() const; 2788 2789 /// Retrieves the RecordDecl this type refers to. 2790 RecordDecl *getAsRecordDecl() const; 2791 2792 /// Retrieves the TagDecl that this type refers to, either 2793 /// because the type is a TagType or because it is the injected-class-name 2794 /// type of a class template or class template partial specialization. 2795 TagDecl *getAsTagDecl() const; 2796 2797 /// If this is a pointer or reference to a RecordType, return the 2798 /// CXXRecordDecl that the type refers to. 2799 /// 2800 /// If this is not a pointer or reference, or the type being pointed to does 2801 /// not refer to a CXXRecordDecl, returns NULL. 2802 const CXXRecordDecl *getPointeeCXXRecordDecl() const; 2803 2804 /// Get the DeducedType whose type will be deduced for a variable with 2805 /// an initializer of this type. This looks through declarators like pointer 2806 /// types, but not through decltype or typedefs. 2807 DeducedType *getContainedDeducedType() const; 2808 2809 /// Get the AutoType whose type will be deduced for a variable with 2810 /// an initializer of this type. This looks through declarators like pointer 2811 /// types, but not through decltype or typedefs. 2812 AutoType *getContainedAutoType() const { 2813 return dyn_cast_or_null<AutoType>(getContainedDeducedType()); 2814 } 2815 2816 /// Determine whether this type was written with a leading 'auto' 2817 /// corresponding to a trailing return type (possibly for a nested 2818 /// function type within a pointer to function type or similar). 2819 bool hasAutoForTrailingReturnType() const; 2820 2821 /// Member-template getAs<specific type>'. Look through sugar for 2822 /// an instance of \<specific type>. This scheme will eventually 2823 /// replace the specific getAsXXXX methods above. 2824 /// 2825 /// There are some specializations of this member template listed 2826 /// immediately following this class. 2827 template <typename T> const T *getAs() const; 2828 2829 /// Member-template getAsAdjusted<specific type>. Look through specific kinds 2830 /// of sugar (parens, attributes, etc) for an instance of \<specific type>. 2831 /// This is used when you need to walk over sugar nodes that represent some 2832 /// kind of type adjustment from a type that was written as a \<specific type> 2833 /// to another type that is still canonically a \<specific type>. 2834 template <typename T> const T *getAsAdjusted() const; 2835 2836 /// A variant of getAs<> for array types which silently discards 2837 /// qualifiers from the outermost type. 2838 const ArrayType *getAsArrayTypeUnsafe() const; 2839 2840 /// Member-template castAs<specific type>. Look through sugar for 2841 /// the underlying instance of \<specific type>. 2842 /// 2843 /// This method has the same relationship to getAs<T> as cast<T> has 2844 /// to dyn_cast<T>; which is to say, the underlying type *must* 2845 /// have the intended type, and this method will never return null. 2846 template <typename T> const T *castAs() const; 2847 2848 /// A variant of castAs<> for array type which silently discards 2849 /// qualifiers from the outermost type. 2850 const ArrayType *castAsArrayTypeUnsafe() const; 2851 2852 /// Determine whether this type had the specified attribute applied to it 2853 /// (looking through top-level type sugar). 2854 bool hasAttr(attr::Kind AK) const; 2855 2856 /// Get the base element type of this type, potentially discarding type 2857 /// qualifiers. This should never be used when type qualifiers 2858 /// are meaningful. 2859 const Type *getBaseElementTypeUnsafe() const; 2860 2861 /// If this is an array type, return the element type of the array, 2862 /// potentially with type qualifiers missing. 2863 /// This should never be used when type qualifiers are meaningful. 2864 const Type *getArrayElementTypeNoTypeQual() const; 2865 2866 /// If this is a pointer type, return the pointee type. 2867 /// If this is an array type, return the array element type. 2868 /// This should never be used when type qualifiers are meaningful. 2869 const Type *getPointeeOrArrayElementType() const; 2870 2871 /// If this is a pointer, ObjC object pointer, or block 2872 /// pointer, this returns the respective pointee. 2873 QualType getPointeeType() const; 2874 2875 /// Return the specified type with any "sugar" removed from the type, 2876 /// removing any typedefs, typeofs, etc., as well as any qualifiers. 2877 const Type *getUnqualifiedDesugaredType() const; 2878 2879 /// Return true if this is an integer type that is 2880 /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], 2881 /// or an enum decl which has a signed representation. 2882 bool isSignedIntegerType() const; 2883 2884 /// Return true if this is an integer type that is 2885 /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], 2886 /// or an enum decl which has an unsigned representation. 2887 bool isUnsignedIntegerType() const; 2888 2889 /// Determines whether this is an integer type that is signed or an 2890 /// enumeration types whose underlying type is a signed integer type. 2891 bool isSignedIntegerOrEnumerationType() const; 2892 2893 /// Determines whether this is an integer type that is unsigned or an 2894 /// enumeration types whose underlying type is a unsigned integer type. 2895 bool isUnsignedIntegerOrEnumerationType() const; 2896 2897 /// Return true if this is a fixed point type according to 2898 /// ISO/IEC JTC1 SC22 WG14 N1169. 2899 bool isFixedPointType() const; 2900 2901 /// Return true if this is a fixed point or integer type. 2902 bool isFixedPointOrIntegerType() const; 2903 2904 /// Return true if this can be converted to (or from) a fixed point type. 2905 bool isConvertibleToFixedPointType() const; 2906 2907 /// Return true if this is a saturated fixed point type according to 2908 /// ISO/IEC JTC1 SC22 WG14 N1169. This type can be signed or unsigned. 2909 bool isSaturatedFixedPointType() const; 2910 2911 /// Return true if this is a saturated fixed point type according to 2912 /// ISO/IEC JTC1 SC22 WG14 N1169. This type can be signed or unsigned. 2913 bool isUnsaturatedFixedPointType() const; 2914 2915 /// Return true if this is a fixed point type that is signed according 2916 /// to ISO/IEC JTC1 SC22 WG14 N1169. This type can also be saturated. 2917 bool isSignedFixedPointType() const; 2918 2919 /// Return true if this is a fixed point type that is unsigned according 2920 /// to ISO/IEC JTC1 SC22 WG14 N1169. This type can also be saturated. 2921 bool isUnsignedFixedPointType() const; 2922 2923 /// Return true if this is not a variable sized type, 2924 /// according to the rules of C99 6.7.5p3. It is not legal to call this on 2925 /// incomplete types. 2926 bool isConstantSizeType() const; 2927 2928 /// Returns true if this type can be represented by some 2929 /// set of type specifiers. 2930 bool isSpecifierType() const; 2931 2932 /// Determine the linkage of this type. 2933 Linkage getLinkage() const; 2934 2935 /// Determine the visibility of this type. 2936 Visibility getVisibility() const { 2937 return getLinkageAndVisibility().getVisibility(); 2938 } 2939 2940 /// Return true if the visibility was explicitly set is the code. 2941 bool isVisibilityExplicit() const { 2942 return getLinkageAndVisibility().isVisibilityExplicit(); 2943 } 2944 2945 /// Determine the linkage and visibility of this type. 2946 LinkageInfo getLinkageAndVisibility() const; 2947 2948 /// True if the computed linkage is valid. Used for consistency 2949 /// checking. Should always return true. 2950 bool isLinkageValid() const; 2951 2952 /// Determine the nullability of the given type. 2953 /// 2954 /// Note that nullability is only captured as sugar within the type 2955 /// system, not as part of the canonical type, so nullability will 2956 /// be lost by canonicalization and desugaring. 2957 std::optional<NullabilityKind> getNullability() const; 2958 2959 /// Determine whether the given type can have a nullability 2960 /// specifier applied to it, i.e., if it is any kind of pointer type. 2961 /// 2962 /// \param ResultIfUnknown The value to return if we don't yet know whether 2963 /// this type can have nullability because it is dependent. 2964 bool canHaveNullability(bool ResultIfUnknown = true) const; 2965 2966 /// Retrieve the set of substitutions required when accessing a member 2967 /// of the Objective-C receiver type that is declared in the given context. 2968 /// 2969 /// \c *this is the type of the object we're operating on, e.g., the 2970 /// receiver for a message send or the base of a property access, and is 2971 /// expected to be of some object or object pointer type. 2972 /// 2973 /// \param dc The declaration context for which we are building up a 2974 /// substitution mapping, which should be an Objective-C class, extension, 2975 /// category, or method within. 2976 /// 2977 /// \returns an array of type arguments that can be substituted for 2978 /// the type parameters of the given declaration context in any type described 2979 /// within that context, or an empty optional to indicate that no 2980 /// substitution is required. 2981 std::optional<ArrayRef<QualType>> 2982 getObjCSubstitutions(const DeclContext *dc) const; 2983 2984 /// Determines if this is an ObjC interface type that may accept type 2985 /// parameters. 2986 bool acceptsObjCTypeParams() const; 2987 2988 const char *getTypeClassName() const; 2989 2990 QualType getCanonicalTypeInternal() const { 2991 return CanonicalType; 2992 } 2993 2994 CanQualType getCanonicalTypeUnqualified() const; // in CanonicalType.h 2995 void dump() const; 2996 void dump(llvm::raw_ostream &OS, const ASTContext &Context) const; 2997 }; 2998 2999 /// This will check for a TypedefType by removing any existing sugar 3000 /// until it reaches a TypedefType or a non-sugared type. 3001 template <> const TypedefType *Type::getAs() const; 3002 template <> const UsingType *Type::getAs() const; 3003 3004 /// This will check for a TemplateSpecializationType by removing any 3005 /// existing sugar until it reaches a TemplateSpecializationType or a 3006 /// non-sugared type. 3007 template <> const TemplateSpecializationType *Type::getAs() const; 3008 3009 /// This will check for an AttributedType by removing any existing sugar 3010 /// until it reaches an AttributedType or a non-sugared type. 3011 template <> const AttributedType *Type::getAs() const; 3012 3013 /// This will check for a BoundsAttributedType by removing any existing 3014 /// sugar until it reaches an BoundsAttributedType or a non-sugared type. 3015 template <> const BoundsAttributedType *Type::getAs() const; 3016 3017 /// This will check for a CountAttributedType by removing any existing 3018 /// sugar until it reaches an CountAttributedType or a non-sugared type. 3019 template <> const CountAttributedType *Type::getAs() const; 3020 3021 // We can do canonical leaf types faster, because we don't have to 3022 // worry about preserving child type decoration. 3023 #define TYPE(Class, Base) 3024 #define LEAF_TYPE(Class) \ 3025 template <> inline const Class##Type *Type::getAs() const { \ 3026 return dyn_cast<Class##Type>(CanonicalType); \ 3027 } \ 3028 template <> inline const Class##Type *Type::castAs() const { \ 3029 return cast<Class##Type>(CanonicalType); \ 3030 } 3031 #include "clang/AST/TypeNodes.inc" 3032 3033 /// This class is used for builtin types like 'int'. Builtin 3034 /// types are always canonical and have a literal name field. 3035 class BuiltinType : public Type { 3036 public: 3037 enum Kind { 3038 // OpenCL image types 3039 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) Id, 3040 #include "clang/Basic/OpenCLImageTypes.def" 3041 // OpenCL extension types 3042 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) Id, 3043 #include "clang/Basic/OpenCLExtensionTypes.def" 3044 // SVE Types 3045 #define SVE_TYPE(Name, Id, SingletonId) Id, 3046 #include "clang/Basic/AArch64SVEACLETypes.def" 3047 // PPC MMA Types 3048 #define PPC_VECTOR_TYPE(Name, Id, Size) Id, 3049 #include "clang/Basic/PPCTypes.def" 3050 // RVV Types 3051 #define RVV_TYPE(Name, Id, SingletonId) Id, 3052 #include "clang/Basic/RISCVVTypes.def" 3053 // WebAssembly reference types 3054 #define WASM_TYPE(Name, Id, SingletonId) Id, 3055 #include "clang/Basic/WebAssemblyReferenceTypes.def" 3056 // AMDGPU types 3057 #define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) Id, 3058 #include "clang/Basic/AMDGPUTypes.def" 3059 // HLSL intangible Types 3060 #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) Id, 3061 #include "clang/Basic/HLSLIntangibleTypes.def" 3062 // All other builtin types 3063 #define BUILTIN_TYPE(Id, SingletonId) Id, 3064 #define LAST_BUILTIN_TYPE(Id) LastKind = Id 3065 #include "clang/AST/BuiltinTypes.def" 3066 }; 3067 3068 private: 3069 friend class ASTContext; // ASTContext creates these. 3070 3071 BuiltinType(Kind K) 3072 : Type(Builtin, QualType(), 3073 K == Dependent ? TypeDependence::DependentInstantiation 3074 : TypeDependence::None) { 3075 static_assert(Kind::LastKind < 3076 (1 << BuiltinTypeBitfields::NumOfBuiltinTypeBits) && 3077 "Defined builtin type exceeds the allocated space for serial " 3078 "numbering"); 3079 BuiltinTypeBits.Kind = K; 3080 } 3081 3082 public: 3083 Kind getKind() const { return static_cast<Kind>(BuiltinTypeBits.Kind); } 3084 StringRef getName(const PrintingPolicy &Policy) const; 3085 3086 const char *getNameAsCString(const PrintingPolicy &Policy) const { 3087 // The StringRef is null-terminated. 3088 StringRef str = getName(Policy); 3089 assert(!str.empty() && str.data()[str.size()] == '\0'); 3090 return str.data(); 3091 } 3092 3093 bool isSugared() const { return false; } 3094 QualType desugar() const { return QualType(this, 0); } 3095 3096 bool isInteger() const { 3097 return getKind() >= Bool && getKind() <= Int128; 3098 } 3099 3100 bool isSignedInteger() const { 3101 return getKind() >= Char_S && getKind() <= Int128; 3102 } 3103 3104 bool isUnsignedInteger() const { 3105 return getKind() >= Bool && getKind() <= UInt128; 3106 } 3107 3108 bool isFloatingPoint() const { 3109 return getKind() >= Half && getKind() <= Ibm128; 3110 } 3111 3112 bool isSVEBool() const { return getKind() == Kind::SveBool; } 3113 3114 bool isSVECount() const { return getKind() == Kind::SveCount; } 3115 3116 /// Determines whether the given kind corresponds to a placeholder type. 3117 static bool isPlaceholderTypeKind(Kind K) { 3118 return K >= Overload; 3119 } 3120 3121 /// Determines whether this type is a placeholder type, i.e. a type 3122 /// which cannot appear in arbitrary positions in a fully-formed 3123 /// expression. 3124 bool isPlaceholderType() const { 3125 return isPlaceholderTypeKind(getKind()); 3126 } 3127 3128 /// Determines whether this type is a placeholder type other than 3129 /// Overload. Most placeholder types require only syntactic 3130 /// information about their context in order to be resolved (e.g. 3131 /// whether it is a call expression), which means they can (and 3132 /// should) be resolved in an earlier "phase" of analysis. 3133 /// Overload expressions sometimes pick up further information 3134 /// from their context, like whether the context expects a 3135 /// specific function-pointer type, and so frequently need 3136 /// special treatment. 3137 bool isNonOverloadPlaceholderType() const { 3138 return getKind() > Overload; 3139 } 3140 3141 static bool classof(const Type *T) { return T->getTypeClass() == Builtin; } 3142 }; 3143 3144 /// Complex values, per C99 6.2.5p11. This supports the C99 complex 3145 /// types (_Complex float etc) as well as the GCC integer complex extensions. 3146 class ComplexType : public Type, public llvm::FoldingSetNode { 3147 friend class ASTContext; // ASTContext creates these. 3148 3149 QualType ElementType; 3150 3151 ComplexType(QualType Element, QualType CanonicalPtr) 3152 : Type(Complex, CanonicalPtr, Element->getDependence()), 3153 ElementType(Element) {} 3154 3155 public: 3156 QualType getElementType() const { return ElementType; } 3157 3158 bool isSugared() const { return false; } 3159 QualType desugar() const { return QualType(this, 0); } 3160 3161 void Profile(llvm::FoldingSetNodeID &ID) { 3162 Profile(ID, getElementType()); 3163 } 3164 3165 static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) { 3166 ID.AddPointer(Element.getAsOpaquePtr()); 3167 } 3168 3169 static bool classof(const Type *T) { return T->getTypeClass() == Complex; } 3170 }; 3171 3172 /// Sugar for parentheses used when specifying types. 3173 class ParenType : public Type, public llvm::FoldingSetNode { 3174 friend class ASTContext; // ASTContext creates these. 3175 3176 QualType Inner; 3177 3178 ParenType(QualType InnerType, QualType CanonType) 3179 : Type(Paren, CanonType, InnerType->getDependence()), Inner(InnerType) {} 3180 3181 public: 3182 QualType getInnerType() const { return Inner; } 3183 3184 bool isSugared() const { return true; } 3185 QualType desugar() const { return getInnerType(); } 3186 3187 void Profile(llvm::FoldingSetNodeID &ID) { 3188 Profile(ID, getInnerType()); 3189 } 3190 3191 static void Profile(llvm::FoldingSetNodeID &ID, QualType Inner) { 3192 Inner.Profile(ID); 3193 } 3194 3195 static bool classof(const Type *T) { return T->getTypeClass() == Paren; } 3196 }; 3197 3198 /// PointerType - C99 6.7.5.1 - Pointer Declarators. 3199 class PointerType : public Type, public llvm::FoldingSetNode { 3200 friend class ASTContext; // ASTContext creates these. 3201 3202 QualType PointeeType; 3203 3204 PointerType(QualType Pointee, QualType CanonicalPtr) 3205 : Type(Pointer, CanonicalPtr, Pointee->getDependence()), 3206 PointeeType(Pointee) {} 3207 3208 public: 3209 QualType getPointeeType() const { return PointeeType; } 3210 3211 bool isSugared() const { return false; } 3212 QualType desugar() const { return QualType(this, 0); } 3213 3214 void Profile(llvm::FoldingSetNodeID &ID) { 3215 Profile(ID, getPointeeType()); 3216 } 3217 3218 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { 3219 ID.AddPointer(Pointee.getAsOpaquePtr()); 3220 } 3221 3222 static bool classof(const Type *T) { return T->getTypeClass() == Pointer; } 3223 }; 3224 3225 /// [BoundsSafety] Represents information of declarations referenced by the 3226 /// arguments of the `counted_by` attribute and the likes. 3227 class TypeCoupledDeclRefInfo { 3228 public: 3229 using BaseTy = llvm::PointerIntPair<ValueDecl *, 1, unsigned>; 3230 3231 private: 3232 enum { 3233 DerefShift = 0, 3234 DerefMask = 1, 3235 }; 3236 BaseTy Data; 3237 3238 public: 3239 /// \p D is to a declaration referenced by the argument of attribute. \p Deref 3240 /// indicates whether \p D is referenced as a dereferenced form, e.g., \p 3241 /// Deref is true for `*n` in `int *__counted_by(*n)`. 3242 TypeCoupledDeclRefInfo(ValueDecl *D = nullptr, bool Deref = false); 3243 3244 bool isDeref() const; 3245 ValueDecl *getDecl() const; 3246 unsigned getInt() const; 3247 void *getOpaqueValue() const; 3248 bool operator==(const TypeCoupledDeclRefInfo &Other) const; 3249 void setFromOpaqueValue(void *V); 3250 }; 3251 3252 /// [BoundsSafety] Represents a parent type class for CountAttributedType and 3253 /// similar sugar types that will be introduced to represent a type with a 3254 /// bounds attribute. 3255 /// 3256 /// Provides a common interface to navigate declarations referred to by the 3257 /// bounds expression. 3258 3259 class BoundsAttributedType : public Type, public llvm::FoldingSetNode { 3260 QualType WrappedTy; 3261 3262 protected: 3263 ArrayRef<TypeCoupledDeclRefInfo> Decls; // stored in trailing objects 3264 3265 BoundsAttributedType(TypeClass TC, QualType Wrapped, QualType Canon); 3266 3267 public: 3268 bool isSugared() const { return true; } 3269 QualType desugar() const { return WrappedTy; } 3270 3271 using decl_iterator = const TypeCoupledDeclRefInfo *; 3272 using decl_range = llvm::iterator_range<decl_iterator>; 3273 3274 decl_iterator dependent_decl_begin() const { return Decls.begin(); } 3275 decl_iterator dependent_decl_end() const { return Decls.end(); } 3276 3277 unsigned getNumCoupledDecls() const { return Decls.size(); } 3278 3279 decl_range dependent_decls() const { 3280 return decl_range(dependent_decl_begin(), dependent_decl_end()); 3281 } 3282 3283 ArrayRef<TypeCoupledDeclRefInfo> getCoupledDecls() const { 3284 return {dependent_decl_begin(), dependent_decl_end()}; 3285 } 3286 3287 bool referencesFieldDecls() const; 3288 3289 static bool classof(const Type *T) { 3290 // Currently, only `class CountAttributedType` inherits 3291 // `BoundsAttributedType` but the subclass will grow as we add more bounds 3292 // annotations. 3293 switch (T->getTypeClass()) { 3294 case CountAttributed: 3295 return true; 3296 default: 3297 return false; 3298 } 3299 } 3300 }; 3301 3302 /// Represents a sugar type with `__counted_by` or `__sized_by` annotations, 3303 /// including their `_or_null` variants. 3304 class CountAttributedType final 3305 : public BoundsAttributedType, 3306 public llvm::TrailingObjects<CountAttributedType, 3307 TypeCoupledDeclRefInfo> { 3308 friend class ASTContext; 3309 3310 Expr *CountExpr; 3311 /// \p CountExpr represents the argument of __counted_by or the likes. \p 3312 /// CountInBytes indicates that \p CountExpr is a byte count (i.e., 3313 /// __sized_by(_or_null)) \p OrNull means it's an or_null variant (i.e., 3314 /// __counted_by_or_null or __sized_by_or_null) \p CoupledDecls contains the 3315 /// list of declarations referenced by \p CountExpr, which the type depends on 3316 /// for the bounds information. 3317 CountAttributedType(QualType Wrapped, QualType Canon, Expr *CountExpr, 3318 bool CountInBytes, bool OrNull, 3319 ArrayRef<TypeCoupledDeclRefInfo> CoupledDecls); 3320 3321 unsigned numTrailingObjects(OverloadToken<TypeCoupledDeclRefInfo>) const { 3322 return CountAttributedTypeBits.NumCoupledDecls; 3323 } 3324 3325 public: 3326 enum DynamicCountPointerKind { 3327 CountedBy = 0, 3328 SizedBy, 3329 CountedByOrNull, 3330 SizedByOrNull, 3331 }; 3332 3333 Expr *getCountExpr() const { return CountExpr; } 3334 bool isCountInBytes() const { return CountAttributedTypeBits.CountInBytes; } 3335 bool isOrNull() const { return CountAttributedTypeBits.OrNull; } 3336 3337 DynamicCountPointerKind getKind() const { 3338 if (isOrNull()) 3339 return isCountInBytes() ? SizedByOrNull : CountedByOrNull; 3340 return isCountInBytes() ? SizedBy : CountedBy; 3341 } 3342 3343 void Profile(llvm::FoldingSetNodeID &ID) { 3344 Profile(ID, desugar(), CountExpr, isCountInBytes(), isOrNull()); 3345 } 3346 3347 static void Profile(llvm::FoldingSetNodeID &ID, QualType WrappedTy, 3348 Expr *CountExpr, bool CountInBytes, bool Nullable); 3349 3350 static bool classof(const Type *T) { 3351 return T->getTypeClass() == CountAttributed; 3352 } 3353 }; 3354 3355 /// Represents a type which was implicitly adjusted by the semantic 3356 /// engine for arbitrary reasons. For example, array and function types can 3357 /// decay, and function types can have their calling conventions adjusted. 3358 class AdjustedType : public Type, public llvm::FoldingSetNode { 3359 QualType OriginalTy; 3360 QualType AdjustedTy; 3361 3362 protected: 3363 friend class ASTContext; // ASTContext creates these. 3364 3365 AdjustedType(TypeClass TC, QualType OriginalTy, QualType AdjustedTy, 3366 QualType CanonicalPtr) 3367 : Type(TC, CanonicalPtr, OriginalTy->getDependence()), 3368 OriginalTy(OriginalTy), AdjustedTy(AdjustedTy) {} 3369 3370 public: 3371 QualType getOriginalType() const { return OriginalTy; } 3372 QualType getAdjustedType() const { return AdjustedTy; } 3373 3374 bool isSugared() const { return true; } 3375 QualType desugar() const { return AdjustedTy; } 3376 3377 void Profile(llvm::FoldingSetNodeID &ID) { 3378 Profile(ID, OriginalTy, AdjustedTy); 3379 } 3380 3381 static void Profile(llvm::FoldingSetNodeID &ID, QualType Orig, QualType New) { 3382 ID.AddPointer(Orig.getAsOpaquePtr()); 3383 ID.AddPointer(New.getAsOpaquePtr()); 3384 } 3385 3386 static bool classof(const Type *T) { 3387 return T->getTypeClass() == Adjusted || T->getTypeClass() == Decayed; 3388 } 3389 }; 3390 3391 /// Represents a pointer type decayed from an array or function type. 3392 class DecayedType : public AdjustedType { 3393 friend class ASTContext; // ASTContext creates these. 3394 3395 inline 3396 DecayedType(QualType OriginalType, QualType Decayed, QualType Canonical); 3397 3398 public: 3399 QualType getDecayedType() const { return getAdjustedType(); } 3400 3401 inline QualType getPointeeType() const; 3402 3403 static bool classof(const Type *T) { return T->getTypeClass() == Decayed; } 3404 }; 3405 3406 /// Pointer to a block type. 3407 /// This type is to represent types syntactically represented as 3408 /// "void (^)(int)", etc. Pointee is required to always be a function type. 3409 class BlockPointerType : public Type, public llvm::FoldingSetNode { 3410 friend class ASTContext; // ASTContext creates these. 3411 3412 // Block is some kind of pointer type 3413 QualType PointeeType; 3414 3415 BlockPointerType(QualType Pointee, QualType CanonicalCls) 3416 : Type(BlockPointer, CanonicalCls, Pointee->getDependence()), 3417 PointeeType(Pointee) {} 3418 3419 public: 3420 // Get the pointee type. Pointee is required to always be a function type. 3421 QualType getPointeeType() const { return PointeeType; } 3422 3423 bool isSugared() const { return false; } 3424 QualType desugar() const { return QualType(this, 0); } 3425 3426 void Profile(llvm::FoldingSetNodeID &ID) { 3427 Profile(ID, getPointeeType()); 3428 } 3429 3430 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { 3431 ID.AddPointer(Pointee.getAsOpaquePtr()); 3432 } 3433 3434 static bool classof(const Type *T) { 3435 return T->getTypeClass() == BlockPointer; 3436 } 3437 }; 3438 3439 /// Base for LValueReferenceType and RValueReferenceType 3440 class ReferenceType : public Type, public llvm::FoldingSetNode { 3441 QualType PointeeType; 3442 3443 protected: 3444 ReferenceType(TypeClass tc, QualType Referencee, QualType CanonicalRef, 3445 bool SpelledAsLValue) 3446 : Type(tc, CanonicalRef, Referencee->getDependence()), 3447 PointeeType(Referencee) { 3448 ReferenceTypeBits.SpelledAsLValue = SpelledAsLValue; 3449 ReferenceTypeBits.InnerRef = Referencee->isReferenceType(); 3450 } 3451 3452 public: 3453 bool isSpelledAsLValue() const { return ReferenceTypeBits.SpelledAsLValue; } 3454 bool isInnerRef() const { return ReferenceTypeBits.InnerRef; } 3455 3456 QualType getPointeeTypeAsWritten() const { return PointeeType; } 3457 3458 QualType getPointeeType() const { 3459 // FIXME: this might strip inner qualifiers; okay? 3460 const ReferenceType *T = this; 3461 while (T->isInnerRef()) 3462 T = T->PointeeType->castAs<ReferenceType>(); 3463 return T->PointeeType; 3464 } 3465 3466 void Profile(llvm::FoldingSetNodeID &ID) { 3467 Profile(ID, PointeeType, isSpelledAsLValue()); 3468 } 3469 3470 static void Profile(llvm::FoldingSetNodeID &ID, 3471 QualType Referencee, 3472 bool SpelledAsLValue) { 3473 ID.AddPointer(Referencee.getAsOpaquePtr()); 3474 ID.AddBoolean(SpelledAsLValue); 3475 } 3476 3477 static bool classof(const Type *T) { 3478 return T->getTypeClass() == LValueReference || 3479 T->getTypeClass() == RValueReference; 3480 } 3481 }; 3482 3483 /// An lvalue reference type, per C++11 [dcl.ref]. 3484 class LValueReferenceType : public ReferenceType { 3485 friend class ASTContext; // ASTContext creates these 3486 3487 LValueReferenceType(QualType Referencee, QualType CanonicalRef, 3488 bool SpelledAsLValue) 3489 : ReferenceType(LValueReference, Referencee, CanonicalRef, 3490 SpelledAsLValue) {} 3491 3492 public: 3493 bool isSugared() const { return false; } 3494 QualType desugar() const { return QualType(this, 0); } 3495 3496 static bool classof(const Type *T) { 3497 return T->getTypeClass() == LValueReference; 3498 } 3499 }; 3500 3501 /// An rvalue reference type, per C++11 [dcl.ref]. 3502 class RValueReferenceType : public ReferenceType { 3503 friend class ASTContext; // ASTContext creates these 3504 3505 RValueReferenceType(QualType Referencee, QualType CanonicalRef) 3506 : ReferenceType(RValueReference, Referencee, CanonicalRef, false) {} 3507 3508 public: 3509 bool isSugared() const { return false; } 3510 QualType desugar() const { return QualType(this, 0); } 3511 3512 static bool classof(const Type *T) { 3513 return T->getTypeClass() == RValueReference; 3514 } 3515 }; 3516 3517 /// A pointer to member type per C++ 8.3.3 - Pointers to members. 3518 /// 3519 /// This includes both pointers to data members and pointer to member functions. 3520 class MemberPointerType : public Type, public llvm::FoldingSetNode { 3521 friend class ASTContext; // ASTContext creates these. 3522 3523 QualType PointeeType; 3524 3525 /// The class of which the pointee is a member. Must ultimately be a 3526 /// RecordType, but could be a typedef or a template parameter too. 3527 const Type *Class; 3528 3529 MemberPointerType(QualType Pointee, const Type *Cls, QualType CanonicalPtr) 3530 : Type(MemberPointer, CanonicalPtr, 3531 (Cls->getDependence() & ~TypeDependence::VariablyModified) | 3532 Pointee->getDependence()), 3533 PointeeType(Pointee), Class(Cls) {} 3534 3535 public: 3536 QualType getPointeeType() const { return PointeeType; } 3537 3538 /// Returns true if the member type (i.e. the pointee type) is a 3539 /// function type rather than a data-member type. 3540 bool isMemberFunctionPointer() const { 3541 return PointeeType->isFunctionProtoType(); 3542 } 3543 3544 /// Returns true if the member type (i.e. the pointee type) is a 3545 /// data type rather than a function type. 3546 bool isMemberDataPointer() const { 3547 return !PointeeType->isFunctionProtoType(); 3548 } 3549 3550 const Type *getClass() const { return Class; } 3551 CXXRecordDecl *getMostRecentCXXRecordDecl() const; 3552 3553 bool isSugared() const { return false; } 3554 QualType desugar() const { return QualType(this, 0); } 3555 3556 void Profile(llvm::FoldingSetNodeID &ID) { 3557 Profile(ID, getPointeeType(), getClass()); 3558 } 3559 3560 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee, 3561 const Type *Class) { 3562 ID.AddPointer(Pointee.getAsOpaquePtr()); 3563 ID.AddPointer(Class); 3564 } 3565 3566 static bool classof(const Type *T) { 3567 return T->getTypeClass() == MemberPointer; 3568 } 3569 }; 3570 3571 /// Capture whether this is a normal array (e.g. int X[4]) 3572 /// an array with a static size (e.g. int X[static 4]), or an array 3573 /// with a star size (e.g. int X[*]). 3574 /// 'static' is only allowed on function parameters. 3575 enum class ArraySizeModifier { Normal, Static, Star }; 3576 3577 /// Represents an array type, per C99 6.7.5.2 - Array Declarators. 3578 class ArrayType : public Type, public llvm::FoldingSetNode { 3579 private: 3580 /// The element type of the array. 3581 QualType ElementType; 3582 3583 protected: 3584 friend class ASTContext; // ASTContext creates these. 3585 3586 ArrayType(TypeClass tc, QualType et, QualType can, ArraySizeModifier sm, 3587 unsigned tq, const Expr *sz = nullptr); 3588 3589 public: 3590 QualType getElementType() const { return ElementType; } 3591 3592 ArraySizeModifier getSizeModifier() const { 3593 return ArraySizeModifier(ArrayTypeBits.SizeModifier); 3594 } 3595 3596 Qualifiers getIndexTypeQualifiers() const { 3597 return Qualifiers::fromCVRMask(getIndexTypeCVRQualifiers()); 3598 } 3599 3600 unsigned getIndexTypeCVRQualifiers() const { 3601 return ArrayTypeBits.IndexTypeQuals; 3602 } 3603 3604 static bool classof(const Type *T) { 3605 return T->getTypeClass() == ConstantArray || 3606 T->getTypeClass() == VariableArray || 3607 T->getTypeClass() == IncompleteArray || 3608 T->getTypeClass() == DependentSizedArray || 3609 T->getTypeClass() == ArrayParameter; 3610 } 3611 }; 3612 3613 /// Represents the canonical version of C arrays with a specified constant size. 3614 /// For example, the canonical type for 'int A[4 + 4*100]' is a 3615 /// ConstantArrayType where the element type is 'int' and the size is 404. 3616 class ConstantArrayType : public ArrayType { 3617 friend class ASTContext; // ASTContext creates these. 3618 3619 struct ExternalSize { 3620 ExternalSize(const llvm::APInt &Sz, const Expr *SE) 3621 : Size(Sz), SizeExpr(SE) {} 3622 llvm::APInt Size; // Allows us to unique the type. 3623 const Expr *SizeExpr; 3624 }; 3625 3626 union { 3627 uint64_t Size; 3628 ExternalSize *SizePtr; 3629 }; 3630 3631 ConstantArrayType(QualType Et, QualType Can, uint64_t Width, uint64_t Sz, 3632 ArraySizeModifier SM, unsigned TQ) 3633 : ArrayType(ConstantArray, Et, Can, SM, TQ, nullptr), Size(Sz) { 3634 ConstantArrayTypeBits.HasExternalSize = false; 3635 ConstantArrayTypeBits.SizeWidth = Width / 8; 3636 // The in-structure size stores the size in bytes rather than bits so we 3637 // drop the three least significant bits since they're always zero anyways. 3638 assert(Width < 0xFF && "Type width in bits must be less than 8 bits"); 3639 } 3640 3641 ConstantArrayType(QualType Et, QualType Can, ExternalSize *SzPtr, 3642 ArraySizeModifier SM, unsigned TQ) 3643 : ArrayType(ConstantArray, Et, Can, SM, TQ, SzPtr->SizeExpr), 3644 SizePtr(SzPtr) { 3645 ConstantArrayTypeBits.HasExternalSize = true; 3646 ConstantArrayTypeBits.SizeWidth = 0; 3647 3648 assert((SzPtr->SizeExpr == nullptr || !Can.isNull()) && 3649 "canonical constant array should not have size expression"); 3650 } 3651 3652 static ConstantArrayType *Create(const ASTContext &Ctx, QualType ET, 3653 QualType Can, const llvm::APInt &Sz, 3654 const Expr *SzExpr, ArraySizeModifier SzMod, 3655 unsigned Qual); 3656 3657 protected: 3658 ConstantArrayType(TypeClass Tc, const ConstantArrayType *ATy, QualType Can) 3659 : ArrayType(Tc, ATy->getElementType(), Can, ATy->getSizeModifier(), 3660 ATy->getIndexTypeQualifiers().getAsOpaqueValue(), nullptr) { 3661 ConstantArrayTypeBits.HasExternalSize = 3662 ATy->ConstantArrayTypeBits.HasExternalSize; 3663 if (!ConstantArrayTypeBits.HasExternalSize) { 3664 ConstantArrayTypeBits.SizeWidth = ATy->ConstantArrayTypeBits.SizeWidth; 3665 Size = ATy->Size; 3666 } else 3667 SizePtr = ATy->SizePtr; 3668 } 3669 3670 public: 3671 /// Return the constant array size as an APInt. 3672 llvm::APInt getSize() const { 3673 return ConstantArrayTypeBits.HasExternalSize 3674 ? SizePtr->Size 3675 : llvm::APInt(ConstantArrayTypeBits.SizeWidth * 8, Size); 3676 } 3677 3678 /// Return the bit width of the size type. 3679 unsigned getSizeBitWidth() const { 3680 return ConstantArrayTypeBits.HasExternalSize 3681 ? SizePtr->Size.getBitWidth() 3682 : static_cast<unsigned>(ConstantArrayTypeBits.SizeWidth * 8); 3683 } 3684 3685 /// Return true if the size is zero. 3686 bool isZeroSize() const { 3687 return ConstantArrayTypeBits.HasExternalSize ? SizePtr->Size.isZero() 3688 : 0 == Size; 3689 } 3690 3691 /// Return the size zero-extended as a uint64_t. 3692 uint64_t getZExtSize() const { 3693 return ConstantArrayTypeBits.HasExternalSize ? SizePtr->Size.getZExtValue() 3694 : Size; 3695 } 3696 3697 /// Return the size sign-extended as a uint64_t. 3698 int64_t getSExtSize() const { 3699 return ConstantArrayTypeBits.HasExternalSize ? SizePtr->Size.getSExtValue() 3700 : static_cast<int64_t>(Size); 3701 } 3702 3703 /// Return the size zero-extended to uint64_t or UINT64_MAX if the value is 3704 /// larger than UINT64_MAX. 3705 uint64_t getLimitedSize() const { 3706 return ConstantArrayTypeBits.HasExternalSize 3707 ? SizePtr->Size.getLimitedValue() 3708 : Size; 3709 } 3710 3711 /// Return a pointer to the size expression. 3712 const Expr *getSizeExpr() const { 3713 return ConstantArrayTypeBits.HasExternalSize ? SizePtr->SizeExpr : nullptr; 3714 } 3715 3716 bool isSugared() const { return false; } 3717 QualType desugar() const { return QualType(this, 0); } 3718 3719 /// Determine the number of bits required to address a member of 3720 // an array with the given element type and number of elements. 3721 static unsigned getNumAddressingBits(const ASTContext &Context, 3722 QualType ElementType, 3723 const llvm::APInt &NumElements); 3724 3725 unsigned getNumAddressingBits(const ASTContext &Context) const; 3726 3727 /// Determine the maximum number of active bits that an array's size 3728 /// can require, which limits the maximum size of the array. 3729 static unsigned getMaxSizeBits(const ASTContext &Context); 3730 3731 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) { 3732 Profile(ID, Ctx, getElementType(), getZExtSize(), getSizeExpr(), 3733 getSizeModifier(), getIndexTypeCVRQualifiers()); 3734 } 3735 3736 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx, 3737 QualType ET, uint64_t ArraySize, const Expr *SizeExpr, 3738 ArraySizeModifier SizeMod, unsigned TypeQuals); 3739 3740 static bool classof(const Type *T) { 3741 return T->getTypeClass() == ConstantArray || 3742 T->getTypeClass() == ArrayParameter; 3743 } 3744 }; 3745 3746 /// Represents a constant array type that does not decay to a pointer when used 3747 /// as a function parameter. 3748 class ArrayParameterType : public ConstantArrayType { 3749 friend class ASTContext; // ASTContext creates these. 3750 3751 ArrayParameterType(const ConstantArrayType *ATy, QualType CanTy) 3752 : ConstantArrayType(ArrayParameter, ATy, CanTy) {} 3753 3754 public: 3755 static bool classof(const Type *T) { 3756 return T->getTypeClass() == ArrayParameter; 3757 } 3758 3759 QualType getConstantArrayType(const ASTContext &Ctx) const; 3760 }; 3761 3762 /// Represents a C array with an unspecified size. For example 'int A[]' has 3763 /// an IncompleteArrayType where the element type is 'int' and the size is 3764 /// unspecified. 3765 class IncompleteArrayType : public ArrayType { 3766 friend class ASTContext; // ASTContext creates these. 3767 3768 IncompleteArrayType(QualType et, QualType can, 3769 ArraySizeModifier sm, unsigned tq) 3770 : ArrayType(IncompleteArray, et, can, sm, tq) {} 3771 3772 public: 3773 friend class StmtIteratorBase; 3774 3775 bool isSugared() const { return false; } 3776 QualType desugar() const { return QualType(this, 0); } 3777 3778 static bool classof(const Type *T) { 3779 return T->getTypeClass() == IncompleteArray; 3780 } 3781 3782 void Profile(llvm::FoldingSetNodeID &ID) { 3783 Profile(ID, getElementType(), getSizeModifier(), 3784 getIndexTypeCVRQualifiers()); 3785 } 3786 3787 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, 3788 ArraySizeModifier SizeMod, unsigned TypeQuals) { 3789 ID.AddPointer(ET.getAsOpaquePtr()); 3790 ID.AddInteger(llvm::to_underlying(SizeMod)); 3791 ID.AddInteger(TypeQuals); 3792 } 3793 }; 3794 3795 /// Represents a C array with a specified size that is not an 3796 /// integer-constant-expression. For example, 'int s[x+foo()]'. 3797 /// Since the size expression is an arbitrary expression, we store it as such. 3798 /// 3799 /// Note: VariableArrayType's aren't uniqued (since the expressions aren't) and 3800 /// should not be: two lexically equivalent variable array types could mean 3801 /// different things, for example, these variables do not have the same type 3802 /// dynamically: 3803 /// 3804 /// void foo(int x) { 3805 /// int Y[x]; 3806 /// ++x; 3807 /// int Z[x]; 3808 /// } 3809 class VariableArrayType : public ArrayType { 3810 friend class ASTContext; // ASTContext creates these. 3811 3812 /// An assignment-expression. VLA's are only permitted within 3813 /// a function block. 3814 Stmt *SizeExpr; 3815 3816 /// The range spanned by the left and right array brackets. 3817 SourceRange Brackets; 3818 3819 VariableArrayType(QualType et, QualType can, Expr *e, 3820 ArraySizeModifier sm, unsigned tq, 3821 SourceRange brackets) 3822 : ArrayType(VariableArray, et, can, sm, tq, e), 3823 SizeExpr((Stmt*) e), Brackets(brackets) {} 3824 3825 public: 3826 friend class StmtIteratorBase; 3827 3828 Expr *getSizeExpr() const { 3829 // We use C-style casts instead of cast<> here because we do not wish 3830 // to have a dependency of Type.h on Stmt.h/Expr.h. 3831 return (Expr*) SizeExpr; 3832 } 3833 3834 SourceRange getBracketsRange() const { return Brackets; } 3835 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } 3836 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } 3837 3838 bool isSugared() const { return false; } 3839 QualType desugar() const { return QualType(this, 0); } 3840 3841 static bool classof(const Type *T) { 3842 return T->getTypeClass() == VariableArray; 3843 } 3844 3845 void Profile(llvm::FoldingSetNodeID &ID) { 3846 llvm_unreachable("Cannot unique VariableArrayTypes."); 3847 } 3848 }; 3849 3850 /// Represents an array type in C++ whose size is a value-dependent expression. 3851 /// 3852 /// For example: 3853 /// \code 3854 /// template<typename T, int Size> 3855 /// class array { 3856 /// T data[Size]; 3857 /// }; 3858 /// \endcode 3859 /// 3860 /// For these types, we won't actually know what the array bound is 3861 /// until template instantiation occurs, at which point this will 3862 /// become either a ConstantArrayType or a VariableArrayType. 3863 class DependentSizedArrayType : public ArrayType { 3864 friend class ASTContext; // ASTContext creates these. 3865 3866 /// An assignment expression that will instantiate to the 3867 /// size of the array. 3868 /// 3869 /// The expression itself might be null, in which case the array 3870 /// type will have its size deduced from an initializer. 3871 Stmt *SizeExpr; 3872 3873 /// The range spanned by the left and right array brackets. 3874 SourceRange Brackets; 3875 3876 DependentSizedArrayType(QualType et, QualType can, Expr *e, 3877 ArraySizeModifier sm, unsigned tq, 3878 SourceRange brackets); 3879 3880 public: 3881 friend class StmtIteratorBase; 3882 3883 Expr *getSizeExpr() const { 3884 // We use C-style casts instead of cast<> here because we do not wish 3885 // to have a dependency of Type.h on Stmt.h/Expr.h. 3886 return (Expr*) SizeExpr; 3887 } 3888 3889 SourceRange getBracketsRange() const { return Brackets; } 3890 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } 3891 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } 3892 3893 bool isSugared() const { return false; } 3894 QualType desugar() const { return QualType(this, 0); } 3895 3896 static bool classof(const Type *T) { 3897 return T->getTypeClass() == DependentSizedArray; 3898 } 3899 3900 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { 3901 Profile(ID, Context, getElementType(), 3902 getSizeModifier(), getIndexTypeCVRQualifiers(), getSizeExpr()); 3903 } 3904 3905 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 3906 QualType ET, ArraySizeModifier SizeMod, 3907 unsigned TypeQuals, Expr *E); 3908 }; 3909 3910 /// Represents an extended address space qualifier where the input address space 3911 /// value is dependent. Non-dependent address spaces are not represented with a 3912 /// special Type subclass; they are stored on an ExtQuals node as part of a QualType. 3913 /// 3914 /// For example: 3915 /// \code 3916 /// template<typename T, int AddrSpace> 3917 /// class AddressSpace { 3918 /// typedef T __attribute__((address_space(AddrSpace))) type; 3919 /// } 3920 /// \endcode 3921 class DependentAddressSpaceType : public Type, public llvm::FoldingSetNode { 3922 friend class ASTContext; 3923 3924 Expr *AddrSpaceExpr; 3925 QualType PointeeType; 3926 SourceLocation loc; 3927 3928 DependentAddressSpaceType(QualType PointeeType, QualType can, 3929 Expr *AddrSpaceExpr, SourceLocation loc); 3930 3931 public: 3932 Expr *getAddrSpaceExpr() const { return AddrSpaceExpr; } 3933 QualType getPointeeType() const { return PointeeType; } 3934 SourceLocation getAttributeLoc() const { return loc; } 3935 3936 bool isSugared() const { return false; } 3937 QualType desugar() const { return QualType(this, 0); } 3938 3939 static bool classof(const Type *T) { 3940 return T->getTypeClass() == DependentAddressSpace; 3941 } 3942 3943 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { 3944 Profile(ID, Context, getPointeeType(), getAddrSpaceExpr()); 3945 } 3946 3947 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 3948 QualType PointeeType, Expr *AddrSpaceExpr); 3949 }; 3950 3951 /// Represents an extended vector type where either the type or size is 3952 /// dependent. 3953 /// 3954 /// For example: 3955 /// \code 3956 /// template<typename T, int Size> 3957 /// class vector { 3958 /// typedef T __attribute__((ext_vector_type(Size))) type; 3959 /// } 3960 /// \endcode 3961 class DependentSizedExtVectorType : public Type, public llvm::FoldingSetNode { 3962 friend class ASTContext; 3963 3964 Expr *SizeExpr; 3965 3966 /// The element type of the array. 3967 QualType ElementType; 3968 3969 SourceLocation loc; 3970 3971 DependentSizedExtVectorType(QualType ElementType, QualType can, 3972 Expr *SizeExpr, SourceLocation loc); 3973 3974 public: 3975 Expr *getSizeExpr() const { return SizeExpr; } 3976 QualType getElementType() const { return ElementType; } 3977 SourceLocation getAttributeLoc() const { return loc; } 3978 3979 bool isSugared() const { return false; } 3980 QualType desugar() const { return QualType(this, 0); } 3981 3982 static bool classof(const Type *T) { 3983 return T->getTypeClass() == DependentSizedExtVector; 3984 } 3985 3986 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { 3987 Profile(ID, Context, getElementType(), getSizeExpr()); 3988 } 3989 3990 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 3991 QualType ElementType, Expr *SizeExpr); 3992 }; 3993 3994 enum class VectorKind { 3995 /// not a target-specific vector type 3996 Generic, 3997 3998 /// is AltiVec vector 3999 AltiVecVector, 4000 4001 /// is AltiVec 'vector Pixel' 4002 AltiVecPixel, 4003 4004 /// is AltiVec 'vector bool ...' 4005 AltiVecBool, 4006 4007 /// is ARM Neon vector 4008 Neon, 4009 4010 /// is ARM Neon polynomial vector 4011 NeonPoly, 4012 4013 /// is AArch64 SVE fixed-length data vector 4014 SveFixedLengthData, 4015 4016 /// is AArch64 SVE fixed-length predicate vector 4017 SveFixedLengthPredicate, 4018 4019 /// is RISC-V RVV fixed-length data vector 4020 RVVFixedLengthData, 4021 4022 /// is RISC-V RVV fixed-length mask vector 4023 RVVFixedLengthMask, 4024 4025 RVVFixedLengthMask_1, 4026 RVVFixedLengthMask_2, 4027 RVVFixedLengthMask_4 4028 }; 4029 4030 /// Represents a GCC generic vector type. This type is created using 4031 /// __attribute__((vector_size(n)), where "n" specifies the vector size in 4032 /// bytes; or from an Altivec __vector or vector declaration. 4033 /// Since the constructor takes the number of vector elements, the 4034 /// client is responsible for converting the size into the number of elements. 4035 class VectorType : public Type, public llvm::FoldingSetNode { 4036 protected: 4037 friend class ASTContext; // ASTContext creates these. 4038 4039 /// The element type of the vector. 4040 QualType ElementType; 4041 4042 VectorType(QualType vecType, unsigned nElements, QualType canonType, 4043 VectorKind vecKind); 4044 4045 VectorType(TypeClass tc, QualType vecType, unsigned nElements, 4046 QualType canonType, VectorKind vecKind); 4047 4048 public: 4049 QualType getElementType() const { return ElementType; } 4050 unsigned getNumElements() const { return VectorTypeBits.NumElements; } 4051 4052 bool isSugared() const { return false; } 4053 QualType desugar() const { return QualType(this, 0); } 4054 4055 VectorKind getVectorKind() const { 4056 return VectorKind(VectorTypeBits.VecKind); 4057 } 4058 4059 void Profile(llvm::FoldingSetNodeID &ID) { 4060 Profile(ID, getElementType(), getNumElements(), 4061 getTypeClass(), getVectorKind()); 4062 } 4063 4064 static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, 4065 unsigned NumElements, TypeClass TypeClass, 4066 VectorKind VecKind) { 4067 ID.AddPointer(ElementType.getAsOpaquePtr()); 4068 ID.AddInteger(NumElements); 4069 ID.AddInteger(TypeClass); 4070 ID.AddInteger(llvm::to_underlying(VecKind)); 4071 } 4072 4073 static bool classof(const Type *T) { 4074 return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector; 4075 } 4076 }; 4077 4078 /// Represents a vector type where either the type or size is dependent. 4079 //// 4080 /// For example: 4081 /// \code 4082 /// template<typename T, int Size> 4083 /// class vector { 4084 /// typedef T __attribute__((vector_size(Size))) type; 4085 /// } 4086 /// \endcode 4087 class DependentVectorType : public Type, public llvm::FoldingSetNode { 4088 friend class ASTContext; 4089 4090 QualType ElementType; 4091 Expr *SizeExpr; 4092 SourceLocation Loc; 4093 4094 DependentVectorType(QualType ElementType, QualType CanonType, Expr *SizeExpr, 4095 SourceLocation Loc, VectorKind vecKind); 4096 4097 public: 4098 Expr *getSizeExpr() const { return SizeExpr; } 4099 QualType getElementType() const { return ElementType; } 4100 SourceLocation getAttributeLoc() const { return Loc; } 4101 VectorKind getVectorKind() const { 4102 return VectorKind(VectorTypeBits.VecKind); 4103 } 4104 4105 bool isSugared() const { return false; } 4106 QualType desugar() const { return QualType(this, 0); } 4107 4108 static bool classof(const Type *T) { 4109 return T->getTypeClass() == DependentVector; 4110 } 4111 4112 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { 4113 Profile(ID, Context, getElementType(), getSizeExpr(), getVectorKind()); 4114 } 4115 4116 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 4117 QualType ElementType, const Expr *SizeExpr, 4118 VectorKind VecKind); 4119 }; 4120 4121 /// ExtVectorType - Extended vector type. This type is created using 4122 /// __attribute__((ext_vector_type(n)), where "n" is the number of elements. 4123 /// Unlike vector_size, ext_vector_type is only allowed on typedef's. This 4124 /// class enables syntactic extensions, like Vector Components for accessing 4125 /// points (as .xyzw), colors (as .rgba), and textures (modeled after OpenGL 4126 /// Shading Language). 4127 class ExtVectorType : public VectorType { 4128 friend class ASTContext; // ASTContext creates these. 4129 4130 ExtVectorType(QualType vecType, unsigned nElements, QualType canonType) 4131 : VectorType(ExtVector, vecType, nElements, canonType, 4132 VectorKind::Generic) {} 4133 4134 public: 4135 static int getPointAccessorIdx(char c) { 4136 switch (c) { 4137 default: return -1; 4138 case 'x': case 'r': return 0; 4139 case 'y': case 'g': return 1; 4140 case 'z': case 'b': return 2; 4141 case 'w': case 'a': return 3; 4142 } 4143 } 4144 4145 static int getNumericAccessorIdx(char c) { 4146 switch (c) { 4147 default: return -1; 4148 case '0': return 0; 4149 case '1': return 1; 4150 case '2': return 2; 4151 case '3': return 3; 4152 case '4': return 4; 4153 case '5': return 5; 4154 case '6': return 6; 4155 case '7': return 7; 4156 case '8': return 8; 4157 case '9': return 9; 4158 case 'A': 4159 case 'a': return 10; 4160 case 'B': 4161 case 'b': return 11; 4162 case 'C': 4163 case 'c': return 12; 4164 case 'D': 4165 case 'd': return 13; 4166 case 'E': 4167 case 'e': return 14; 4168 case 'F': 4169 case 'f': return 15; 4170 } 4171 } 4172 4173 static int getAccessorIdx(char c, bool isNumericAccessor) { 4174 if (isNumericAccessor) 4175 return getNumericAccessorIdx(c); 4176 else 4177 return getPointAccessorIdx(c); 4178 } 4179 4180 bool isAccessorWithinNumElements(char c, bool isNumericAccessor) const { 4181 if (int idx = getAccessorIdx(c, isNumericAccessor)+1) 4182 return unsigned(idx-1) < getNumElements(); 4183 return false; 4184 } 4185 4186 bool isSugared() const { return false; } 4187 QualType desugar() const { return QualType(this, 0); } 4188 4189 static bool classof(const Type *T) { 4190 return T->getTypeClass() == ExtVector; 4191 } 4192 }; 4193 4194 /// Represents a matrix type, as defined in the Matrix Types clang extensions. 4195 /// __attribute__((matrix_type(rows, columns))), where "rows" specifies 4196 /// number of rows and "columns" specifies the number of columns. 4197 class MatrixType : public Type, public llvm::FoldingSetNode { 4198 protected: 4199 friend class ASTContext; 4200 4201 /// The element type of the matrix. 4202 QualType ElementType; 4203 4204 MatrixType(QualType ElementTy, QualType CanonElementTy); 4205 4206 MatrixType(TypeClass TypeClass, QualType ElementTy, QualType CanonElementTy, 4207 const Expr *RowExpr = nullptr, const Expr *ColumnExpr = nullptr); 4208 4209 public: 4210 /// Returns type of the elements being stored in the matrix 4211 QualType getElementType() const { return ElementType; } 4212 4213 /// Valid elements types are the following: 4214 /// * an integer type (as in C23 6.2.5p22), but excluding enumerated types 4215 /// and _Bool 4216 /// * the standard floating types float or double 4217 /// * a half-precision floating point type, if one is supported on the target 4218 static bool isValidElementType(QualType T) { 4219 return T->isDependentType() || 4220 (T->isRealType() && !T->isBooleanType() && !T->isEnumeralType()); 4221 } 4222 4223 bool isSugared() const { return false; } 4224 QualType desugar() const { return QualType(this, 0); } 4225 4226 static bool classof(const Type *T) { 4227 return T->getTypeClass() == ConstantMatrix || 4228 T->getTypeClass() == DependentSizedMatrix; 4229 } 4230 }; 4231 4232 /// Represents a concrete matrix type with constant number of rows and columns 4233 class ConstantMatrixType final : public MatrixType { 4234 protected: 4235 friend class ASTContext; 4236 4237 /// Number of rows and columns. 4238 unsigned NumRows; 4239 unsigned NumColumns; 4240 4241 static constexpr unsigned MaxElementsPerDimension = (1 << 20) - 1; 4242 4243 ConstantMatrixType(QualType MatrixElementType, unsigned NRows, 4244 unsigned NColumns, QualType CanonElementType); 4245 4246 ConstantMatrixType(TypeClass typeClass, QualType MatrixType, unsigned NRows, 4247 unsigned NColumns, QualType CanonElementType); 4248 4249 public: 4250 /// Returns the number of rows in the matrix. 4251 unsigned getNumRows() const { return NumRows; } 4252 4253 /// Returns the number of columns in the matrix. 4254 unsigned getNumColumns() const { return NumColumns; } 4255 4256 /// Returns the number of elements required to embed the matrix into a vector. 4257 unsigned getNumElementsFlattened() const { 4258 return getNumRows() * getNumColumns(); 4259 } 4260 4261 /// Returns true if \p NumElements is a valid matrix dimension. 4262 static constexpr bool isDimensionValid(size_t NumElements) { 4263 return NumElements > 0 && NumElements <= MaxElementsPerDimension; 4264 } 4265 4266 /// Returns the maximum number of elements per dimension. 4267 static constexpr unsigned getMaxElementsPerDimension() { 4268 return MaxElementsPerDimension; 4269 } 4270 4271 void Profile(llvm::FoldingSetNodeID &ID) { 4272 Profile(ID, getElementType(), getNumRows(), getNumColumns(), 4273 getTypeClass()); 4274 } 4275 4276 static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, 4277 unsigned NumRows, unsigned NumColumns, 4278 TypeClass TypeClass) { 4279 ID.AddPointer(ElementType.getAsOpaquePtr()); 4280 ID.AddInteger(NumRows); 4281 ID.AddInteger(NumColumns); 4282 ID.AddInteger(TypeClass); 4283 } 4284 4285 static bool classof(const Type *T) { 4286 return T->getTypeClass() == ConstantMatrix; 4287 } 4288 }; 4289 4290 /// Represents a matrix type where the type and the number of rows and columns 4291 /// is dependent on a template. 4292 class DependentSizedMatrixType final : public MatrixType { 4293 friend class ASTContext; 4294 4295 Expr *RowExpr; 4296 Expr *ColumnExpr; 4297 4298 SourceLocation loc; 4299 4300 DependentSizedMatrixType(QualType ElementType, QualType CanonicalType, 4301 Expr *RowExpr, Expr *ColumnExpr, SourceLocation loc); 4302 4303 public: 4304 Expr *getRowExpr() const { return RowExpr; } 4305 Expr *getColumnExpr() const { return ColumnExpr; } 4306 SourceLocation getAttributeLoc() const { return loc; } 4307 4308 static bool classof(const Type *T) { 4309 return T->getTypeClass() == DependentSizedMatrix; 4310 } 4311 4312 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { 4313 Profile(ID, Context, getElementType(), getRowExpr(), getColumnExpr()); 4314 } 4315 4316 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 4317 QualType ElementType, Expr *RowExpr, Expr *ColumnExpr); 4318 }; 4319 4320 /// FunctionType - C99 6.7.5.3 - Function Declarators. This is the common base 4321 /// class of FunctionNoProtoType and FunctionProtoType. 4322 class FunctionType : public Type { 4323 // The type returned by the function. 4324 QualType ResultType; 4325 4326 public: 4327 /// Interesting information about a specific parameter that can't simply 4328 /// be reflected in parameter's type. This is only used by FunctionProtoType 4329 /// but is in FunctionType to make this class available during the 4330 /// specification of the bases of FunctionProtoType. 4331 /// 4332 /// It makes sense to model language features this way when there's some 4333 /// sort of parameter-specific override (such as an attribute) that 4334 /// affects how the function is called. For example, the ARC ns_consumed 4335 /// attribute changes whether a parameter is passed at +0 (the default) 4336 /// or +1 (ns_consumed). This must be reflected in the function type, 4337 /// but isn't really a change to the parameter type. 4338 /// 4339 /// One serious disadvantage of modelling language features this way is 4340 /// that they generally do not work with language features that attempt 4341 /// to destructure types. For example, template argument deduction will 4342 /// not be able to match a parameter declared as 4343 /// T (*)(U) 4344 /// against an argument of type 4345 /// void (*)(__attribute__((ns_consumed)) id) 4346 /// because the substitution of T=void, U=id into the former will 4347 /// not produce the latter. 4348 class ExtParameterInfo { 4349 enum { 4350 ABIMask = 0x0F, 4351 IsConsumed = 0x10, 4352 HasPassObjSize = 0x20, 4353 IsNoEscape = 0x40, 4354 }; 4355 unsigned char Data = 0; 4356 4357 public: 4358 ExtParameterInfo() = default; 4359 4360 /// Return the ABI treatment of this parameter. 4361 ParameterABI getABI() const { return ParameterABI(Data & ABIMask); } 4362 ExtParameterInfo withABI(ParameterABI kind) const { 4363 ExtParameterInfo copy = *this; 4364 copy.Data = (copy.Data & ~ABIMask) | unsigned(kind); 4365 return copy; 4366 } 4367 4368 /// Is this parameter considered "consumed" by Objective-C ARC? 4369 /// Consumed parameters must have retainable object type. 4370 bool isConsumed() const { return (Data & IsConsumed); } 4371 ExtParameterInfo withIsConsumed(bool consumed) const { 4372 ExtParameterInfo copy = *this; 4373 if (consumed) 4374 copy.Data |= IsConsumed; 4375 else 4376 copy.Data &= ~IsConsumed; 4377 return copy; 4378 } 4379 4380 bool hasPassObjectSize() const { return Data & HasPassObjSize; } 4381 ExtParameterInfo withHasPassObjectSize() const { 4382 ExtParameterInfo Copy = *this; 4383 Copy.Data |= HasPassObjSize; 4384 return Copy; 4385 } 4386 4387 bool isNoEscape() const { return Data & IsNoEscape; } 4388 ExtParameterInfo withIsNoEscape(bool NoEscape) const { 4389 ExtParameterInfo Copy = *this; 4390 if (NoEscape) 4391 Copy.Data |= IsNoEscape; 4392 else 4393 Copy.Data &= ~IsNoEscape; 4394 return Copy; 4395 } 4396 4397 unsigned char getOpaqueValue() const { return Data; } 4398 static ExtParameterInfo getFromOpaqueValue(unsigned char data) { 4399 ExtParameterInfo result; 4400 result.Data = data; 4401 return result; 4402 } 4403 4404 friend bool operator==(ExtParameterInfo lhs, ExtParameterInfo rhs) { 4405 return lhs.Data == rhs.Data; 4406 } 4407 4408 friend bool operator!=(ExtParameterInfo lhs, ExtParameterInfo rhs) { 4409 return lhs.Data != rhs.Data; 4410 } 4411 }; 4412 4413 /// A class which abstracts out some details necessary for 4414 /// making a call. 4415 /// 4416 /// It is not actually used directly for storing this information in 4417 /// a FunctionType, although FunctionType does currently use the 4418 /// same bit-pattern. 4419 /// 4420 // If you add a field (say Foo), other than the obvious places (both, 4421 // constructors, compile failures), what you need to update is 4422 // * Operator== 4423 // * getFoo 4424 // * withFoo 4425 // * functionType. Add Foo, getFoo. 4426 // * ASTContext::getFooType 4427 // * ASTContext::mergeFunctionTypes 4428 // * FunctionNoProtoType::Profile 4429 // * FunctionProtoType::Profile 4430 // * TypePrinter::PrintFunctionProto 4431 // * AST read and write 4432 // * Codegen 4433 class ExtInfo { 4434 friend class FunctionType; 4435 4436 // Feel free to rearrange or add bits, but if you go over 16, you'll need to 4437 // adjust the Bits field below, and if you add bits, you'll need to adjust 4438 // Type::FunctionTypeBitfields::ExtInfo as well. 4439 4440 // | CC |noreturn|produces|nocallersavedregs|regparm|nocfcheck|cmsenscall| 4441 // |0 .. 4| 5 | 6 | 7 |8 .. 10| 11 | 12 | 4442 // 4443 // regparm is either 0 (no regparm attribute) or the regparm value+1. 4444 enum { CallConvMask = 0x1F }; 4445 enum { NoReturnMask = 0x20 }; 4446 enum { ProducesResultMask = 0x40 }; 4447 enum { NoCallerSavedRegsMask = 0x80 }; 4448 enum { 4449 RegParmMask = 0x700, 4450 RegParmOffset = 8 4451 }; 4452 enum { NoCfCheckMask = 0x800 }; 4453 enum { CmseNSCallMask = 0x1000 }; 4454 uint16_t Bits = CC_C; 4455 4456 ExtInfo(unsigned Bits) : Bits(static_cast<uint16_t>(Bits)) {} 4457 4458 public: 4459 // Constructor with no defaults. Use this when you know that you 4460 // have all the elements (when reading an AST file for example). 4461 ExtInfo(bool noReturn, bool hasRegParm, unsigned regParm, CallingConv cc, 4462 bool producesResult, bool noCallerSavedRegs, bool NoCfCheck, 4463 bool cmseNSCall) { 4464 assert((!hasRegParm || regParm < 7) && "Invalid regparm value"); 4465 Bits = ((unsigned)cc) | (noReturn ? NoReturnMask : 0) | 4466 (producesResult ? ProducesResultMask : 0) | 4467 (noCallerSavedRegs ? NoCallerSavedRegsMask : 0) | 4468 (hasRegParm ? ((regParm + 1) << RegParmOffset) : 0) | 4469 (NoCfCheck ? NoCfCheckMask : 0) | 4470 (cmseNSCall ? CmseNSCallMask : 0); 4471 } 4472 4473 // Constructor with all defaults. Use when for example creating a 4474 // function known to use defaults. 4475 ExtInfo() = default; 4476 4477 // Constructor with just the calling convention, which is an important part 4478 // of the canonical type. 4479 ExtInfo(CallingConv CC) : Bits(CC) {} 4480 4481 bool getNoReturn() const { return Bits & NoReturnMask; } 4482 bool getProducesResult() const { return Bits & ProducesResultMask; } 4483 bool getCmseNSCall() const { return Bits & CmseNSCallMask; } 4484 bool getNoCallerSavedRegs() const { return Bits & NoCallerSavedRegsMask; } 4485 bool getNoCfCheck() const { return Bits & NoCfCheckMask; } 4486 bool getHasRegParm() const { return ((Bits & RegParmMask) >> RegParmOffset) != 0; } 4487 4488 unsigned getRegParm() const { 4489 unsigned RegParm = (Bits & RegParmMask) >> RegParmOffset; 4490 if (RegParm > 0) 4491 --RegParm; 4492 return RegParm; 4493 } 4494 4495 CallingConv getCC() const { return CallingConv(Bits & CallConvMask); } 4496 4497 bool operator==(ExtInfo Other) const { 4498 return Bits == Other.Bits; 4499 } 4500 bool operator!=(ExtInfo Other) const { 4501 return Bits != Other.Bits; 4502 } 4503 4504 // Note that we don't have setters. That is by design, use 4505 // the following with methods instead of mutating these objects. 4506 4507 ExtInfo withNoReturn(bool noReturn) const { 4508 if (noReturn) 4509 return ExtInfo(Bits | NoReturnMask); 4510 else 4511 return ExtInfo(Bits & ~NoReturnMask); 4512 } 4513 4514 ExtInfo withProducesResult(bool producesResult) const { 4515 if (producesResult) 4516 return ExtInfo(Bits | ProducesResultMask); 4517 else 4518 return ExtInfo(Bits & ~ProducesResultMask); 4519 } 4520 4521 ExtInfo withCmseNSCall(bool cmseNSCall) const { 4522 if (cmseNSCall) 4523 return ExtInfo(Bits | CmseNSCallMask); 4524 else 4525 return ExtInfo(Bits & ~CmseNSCallMask); 4526 } 4527 4528 ExtInfo withNoCallerSavedRegs(bool noCallerSavedRegs) const { 4529 if (noCallerSavedRegs) 4530 return ExtInfo(Bits | NoCallerSavedRegsMask); 4531 else 4532 return ExtInfo(Bits & ~NoCallerSavedRegsMask); 4533 } 4534 4535 ExtInfo withNoCfCheck(bool noCfCheck) const { 4536 if (noCfCheck) 4537 return ExtInfo(Bits | NoCfCheckMask); 4538 else 4539 return ExtInfo(Bits & ~NoCfCheckMask); 4540 } 4541 4542 ExtInfo withRegParm(unsigned RegParm) const { 4543 assert(RegParm < 7 && "Invalid regparm value"); 4544 return ExtInfo((Bits & ~RegParmMask) | 4545 ((RegParm + 1) << RegParmOffset)); 4546 } 4547 4548 ExtInfo withCallingConv(CallingConv cc) const { 4549 return ExtInfo((Bits & ~CallConvMask) | (unsigned) cc); 4550 } 4551 4552 void Profile(llvm::FoldingSetNodeID &ID) const { 4553 ID.AddInteger(Bits); 4554 } 4555 }; 4556 4557 /// A simple holder for a QualType representing a type in an 4558 /// exception specification. Unfortunately needed by FunctionProtoType 4559 /// because TrailingObjects cannot handle repeated types. 4560 struct ExceptionType { QualType Type; }; 4561 4562 /// A simple holder for various uncommon bits which do not fit in 4563 /// FunctionTypeBitfields. Aligned to alignof(void *) to maintain the 4564 /// alignment of subsequent objects in TrailingObjects. 4565 struct alignas(void *) FunctionTypeExtraBitfields { 4566 /// The number of types in the exception specification. 4567 /// A whole unsigned is not needed here and according to 4568 /// [implimits] 8 bits would be enough here. 4569 unsigned NumExceptionType : 10; 4570 4571 LLVM_PREFERRED_TYPE(bool) 4572 unsigned HasArmTypeAttributes : 1; 4573 4574 LLVM_PREFERRED_TYPE(bool) 4575 unsigned EffectsHaveConditions : 1; 4576 unsigned NumFunctionEffects : 4; 4577 4578 FunctionTypeExtraBitfields() 4579 : NumExceptionType(0), HasArmTypeAttributes(false), 4580 EffectsHaveConditions(false), NumFunctionEffects(0) {} 4581 }; 4582 4583 /// The AArch64 SME ACLE (Arm C/C++ Language Extensions) define a number 4584 /// of function type attributes that can be set on function types, including 4585 /// function pointers. 4586 enum AArch64SMETypeAttributes : unsigned { 4587 SME_NormalFunction = 0, 4588 SME_PStateSMEnabledMask = 1 << 0, 4589 SME_PStateSMCompatibleMask = 1 << 1, 4590 4591 // Describes the value of the state using ArmStateValue. 4592 SME_ZAShift = 2, 4593 SME_ZAMask = 0b111 << SME_ZAShift, 4594 SME_ZT0Shift = 5, 4595 SME_ZT0Mask = 0b111 << SME_ZT0Shift, 4596 4597 // A bit to tell whether a function is agnostic about sme ZA state. 4598 SME_AgnosticZAStateShift = 8, 4599 SME_AgnosticZAStateMask = 1 << SME_AgnosticZAStateShift, 4600 4601 SME_AttributeMask = 4602 0b1'111'111'11 // We can't support more than 9 bits because of 4603 // the bitmask in FunctionTypeArmAttributes 4604 // and ExtProtoInfo. 4605 }; 4606 4607 enum ArmStateValue : unsigned { 4608 ARM_None = 0, 4609 ARM_Preserves = 1, 4610 ARM_In = 2, 4611 ARM_Out = 3, 4612 ARM_InOut = 4, 4613 }; 4614 4615 static ArmStateValue getArmZAState(unsigned AttrBits) { 4616 return (ArmStateValue)((AttrBits & SME_ZAMask) >> SME_ZAShift); 4617 } 4618 4619 static ArmStateValue getArmZT0State(unsigned AttrBits) { 4620 return (ArmStateValue)((AttrBits & SME_ZT0Mask) >> SME_ZT0Shift); 4621 } 4622 4623 /// A holder for Arm type attributes as described in the Arm C/C++ 4624 /// Language extensions which are not particularly common to all 4625 /// types and therefore accounted separately from FunctionTypeBitfields. 4626 struct alignas(void *) FunctionTypeArmAttributes { 4627 /// Any AArch64 SME ACLE type attributes that need to be propagated 4628 /// on declarations and function pointers. 4629 unsigned AArch64SMEAttributes : 9; 4630 4631 FunctionTypeArmAttributes() : AArch64SMEAttributes(SME_NormalFunction) {} 4632 }; 4633 4634 protected: 4635 FunctionType(TypeClass tc, QualType res, QualType Canonical, 4636 TypeDependence Dependence, ExtInfo Info) 4637 : Type(tc, Canonical, Dependence), ResultType(res) { 4638 FunctionTypeBits.ExtInfo = Info.Bits; 4639 } 4640 4641 Qualifiers getFastTypeQuals() const { 4642 if (isFunctionProtoType()) 4643 return Qualifiers::fromFastMask(FunctionTypeBits.FastTypeQuals); 4644 4645 return Qualifiers(); 4646 } 4647 4648 public: 4649 QualType getReturnType() const { return ResultType; } 4650 4651 bool getHasRegParm() const { return getExtInfo().getHasRegParm(); } 4652 unsigned getRegParmType() const { return getExtInfo().getRegParm(); } 4653 4654 /// Determine whether this function type includes the GNU noreturn 4655 /// attribute. The C++11 [[noreturn]] attribute does not affect the function 4656 /// type. 4657 bool getNoReturnAttr() const { return getExtInfo().getNoReturn(); } 4658 4659 bool getCmseNSCallAttr() const { return getExtInfo().getCmseNSCall(); } 4660 CallingConv getCallConv() const { return getExtInfo().getCC(); } 4661 ExtInfo getExtInfo() const { return ExtInfo(FunctionTypeBits.ExtInfo); } 4662 4663 static_assert((~Qualifiers::FastMask & Qualifiers::CVRMask) == 0, 4664 "Const, volatile and restrict are assumed to be a subset of " 4665 "the fast qualifiers."); 4666 4667 bool isConst() const { return getFastTypeQuals().hasConst(); } 4668 bool isVolatile() const { return getFastTypeQuals().hasVolatile(); } 4669 bool isRestrict() const { return getFastTypeQuals().hasRestrict(); } 4670 4671 /// Determine the type of an expression that calls a function of 4672 /// this type. 4673 QualType getCallResultType(const ASTContext &Context) const { 4674 return getReturnType().getNonLValueExprType(Context); 4675 } 4676 4677 static StringRef getNameForCallConv(CallingConv CC); 4678 4679 static bool classof(const Type *T) { 4680 return T->getTypeClass() == FunctionNoProto || 4681 T->getTypeClass() == FunctionProto; 4682 } 4683 }; 4684 4685 /// Represents a K&R-style 'int foo()' function, which has 4686 /// no information available about its arguments. 4687 class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode { 4688 friend class ASTContext; // ASTContext creates these. 4689 4690 FunctionNoProtoType(QualType Result, QualType Canonical, ExtInfo Info) 4691 : FunctionType(FunctionNoProto, Result, Canonical, 4692 Result->getDependence() & 4693 ~(TypeDependence::DependentInstantiation | 4694 TypeDependence::UnexpandedPack), 4695 Info) {} 4696 4697 public: 4698 // No additional state past what FunctionType provides. 4699 4700 bool isSugared() const { return false; } 4701 QualType desugar() const { return QualType(this, 0); } 4702 4703 void Profile(llvm::FoldingSetNodeID &ID) { 4704 Profile(ID, getReturnType(), getExtInfo()); 4705 } 4706 4707 static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType, 4708 ExtInfo Info) { 4709 Info.Profile(ID); 4710 ID.AddPointer(ResultType.getAsOpaquePtr()); 4711 } 4712 4713 static bool classof(const Type *T) { 4714 return T->getTypeClass() == FunctionNoProto; 4715 } 4716 }; 4717 4718 // ------------------------------------------------------------------------------ 4719 4720 /// Represents an abstract function effect, using just an enumeration describing 4721 /// its kind. 4722 class FunctionEffect { 4723 public: 4724 /// Identifies the particular effect. 4725 enum class Kind : uint8_t { 4726 NonBlocking, 4727 NonAllocating, 4728 Blocking, 4729 Allocating, 4730 Last = Allocating 4731 }; 4732 constexpr static size_t KindCount = static_cast<size_t>(Kind::Last) + 1; 4733 4734 /// Flags describing some behaviors of the effect. 4735 using Flags = unsigned; 4736 enum FlagBit : Flags { 4737 // Can verification inspect callees' implementations? (e.g. nonblocking: 4738 // yes, tcb+types: no). This also implies the need for 2nd-pass 4739 // verification. 4740 FE_InferrableOnCallees = 0x1, 4741 4742 // Language constructs which effects can diagnose as disallowed. 4743 FE_ExcludeThrow = 0x2, 4744 FE_ExcludeCatch = 0x4, 4745 FE_ExcludeObjCMessageSend = 0x8, 4746 FE_ExcludeStaticLocalVars = 0x10, 4747 FE_ExcludeThreadLocalVars = 0x20 4748 }; 4749 4750 private: 4751 Kind FKind; 4752 4753 // Expansion: for hypothetical TCB+types, there could be one Kind for TCB, 4754 // then ~16(?) bits "SubKind" to map to a specific named TCB. SubKind would 4755 // be considered for uniqueness. 4756 4757 public: 4758 explicit FunctionEffect(Kind K) : FKind(K) {} 4759 4760 /// The kind of the effect. 4761 Kind kind() const { return FKind; } 4762 4763 /// Return the opposite kind, for effects which have opposites. 4764 Kind oppositeKind() const; 4765 4766 /// For serialization. 4767 uint32_t toOpaqueInt32() const { return uint32_t(FKind); } 4768 static FunctionEffect fromOpaqueInt32(uint32_t Value) { 4769 return FunctionEffect(Kind(Value)); 4770 } 4771 4772 /// Flags describing some behaviors of the effect. 4773 Flags flags() const { 4774 switch (kind()) { 4775 case Kind::NonBlocking: 4776 return FE_InferrableOnCallees | FE_ExcludeThrow | FE_ExcludeCatch | 4777 FE_ExcludeObjCMessageSend | FE_ExcludeStaticLocalVars | 4778 FE_ExcludeThreadLocalVars; 4779 case Kind::NonAllocating: 4780 // Same as NonBlocking, except without FE_ExcludeStaticLocalVars. 4781 return FE_InferrableOnCallees | FE_ExcludeThrow | FE_ExcludeCatch | 4782 FE_ExcludeObjCMessageSend | FE_ExcludeThreadLocalVars; 4783 case Kind::Blocking: 4784 case Kind::Allocating: 4785 return 0; 4786 } 4787 llvm_unreachable("unknown effect kind"); 4788 } 4789 4790 /// The description printed in diagnostics, e.g. 'nonblocking'. 4791 StringRef name() const; 4792 4793 friend raw_ostream &operator<<(raw_ostream &OS, 4794 const FunctionEffect &Effect) { 4795 OS << Effect.name(); 4796 return OS; 4797 } 4798 4799 /// Determine whether the effect is allowed to be inferred on the callee, 4800 /// which is either a FunctionDecl or BlockDecl. If the returned optional 4801 /// is empty, inference is permitted; otherwise it holds the effect which 4802 /// blocked inference. 4803 /// Example: This allows nonblocking(false) to prevent inference for the 4804 /// function. 4805 std::optional<FunctionEffect> 4806 effectProhibitingInference(const Decl &Callee, 4807 FunctionEffectKindSet CalleeFX) const; 4808 4809 // Return false for success. When true is returned for a direct call, then the 4810 // FE_InferrableOnCallees flag may trigger inference rather than an immediate 4811 // diagnostic. Caller should be assumed to have the effect (it may not have it 4812 // explicitly when inferring). 4813 bool shouldDiagnoseFunctionCall(bool Direct, 4814 FunctionEffectKindSet CalleeFX) const; 4815 4816 friend bool operator==(FunctionEffect LHS, FunctionEffect RHS) { 4817 return LHS.FKind == RHS.FKind; 4818 } 4819 friend bool operator!=(FunctionEffect LHS, FunctionEffect RHS) { 4820 return !(LHS == RHS); 4821 } 4822 friend bool operator<(FunctionEffect LHS, FunctionEffect RHS) { 4823 return LHS.FKind < RHS.FKind; 4824 } 4825 }; 4826 4827 /// Wrap a function effect's condition expression in another struct so 4828 /// that FunctionProtoType's TrailingObjects can treat it separately. 4829 class EffectConditionExpr { 4830 Expr *Cond = nullptr; // if null, unconditional. 4831 4832 public: 4833 EffectConditionExpr() = default; 4834 EffectConditionExpr(Expr *E) : Cond(E) {} 4835 4836 Expr *getCondition() const { return Cond; } 4837 4838 bool operator==(const EffectConditionExpr &RHS) const { 4839 return Cond == RHS.Cond; 4840 } 4841 }; 4842 4843 /// A FunctionEffect plus a potential boolean expression determining whether 4844 /// the effect is declared (e.g. nonblocking(expr)). Generally the condition 4845 /// expression when present, is dependent. 4846 struct FunctionEffectWithCondition { 4847 FunctionEffect Effect; 4848 EffectConditionExpr Cond; 4849 4850 FunctionEffectWithCondition(FunctionEffect E, const EffectConditionExpr &C) 4851 : Effect(E), Cond(C) {} 4852 4853 /// Return a textual description of the effect, and its condition, if any. 4854 std::string description() const; 4855 4856 friend raw_ostream &operator<<(raw_ostream &OS, 4857 const FunctionEffectWithCondition &CFE); 4858 }; 4859 4860 /// Support iteration in parallel through a pair of FunctionEffect and 4861 /// EffectConditionExpr containers. 4862 template <typename Container> class FunctionEffectIterator { 4863 friend Container; 4864 4865 const Container *Outer = nullptr; 4866 size_t Idx = 0; 4867 4868 public: 4869 FunctionEffectIterator(); 4870 FunctionEffectIterator(const Container &O, size_t I) : Outer(&O), Idx(I) {} 4871 bool operator==(const FunctionEffectIterator &Other) const { 4872 return Idx == Other.Idx; 4873 } 4874 bool operator!=(const FunctionEffectIterator &Other) const { 4875 return Idx != Other.Idx; 4876 } 4877 4878 FunctionEffectIterator operator++() { 4879 ++Idx; 4880 return *this; 4881 } 4882 4883 FunctionEffectWithCondition operator*() const { 4884 assert(Outer != nullptr && "invalid FunctionEffectIterator"); 4885 bool HasConds = !Outer->Conditions.empty(); 4886 return FunctionEffectWithCondition{Outer->Effects[Idx], 4887 HasConds ? Outer->Conditions[Idx] 4888 : EffectConditionExpr()}; 4889 } 4890 }; 4891 4892 /// An immutable set of FunctionEffects and possibly conditions attached to 4893 /// them. The effects and conditions reside in memory not managed by this object 4894 /// (typically, trailing objects in FunctionProtoType, or borrowed references 4895 /// from a FunctionEffectSet). 4896 /// 4897 /// Invariants: 4898 /// - there is never more than one instance of any given effect. 4899 /// - the array of conditions is either empty or has the same size as the 4900 /// array of effects. 4901 /// - some conditions may be null expressions; each condition pertains to 4902 /// the effect at the same array index. 4903 /// 4904 /// Also, if there are any conditions, at least one of those expressions will be 4905 /// dependent, but this is only asserted in the constructor of 4906 /// FunctionProtoType. 4907 /// 4908 /// See also FunctionEffectSet, in Sema, which provides a mutable set. 4909 class FunctionEffectsRef { 4910 // Restrict classes which can call the private constructor -- these friends 4911 // all maintain the required invariants. FunctionEffectSet is generally the 4912 // only way in which the arrays are created; FunctionProtoType will not 4913 // reorder them. 4914 friend FunctionProtoType; 4915 friend FunctionEffectSet; 4916 4917 ArrayRef<FunctionEffect> Effects; 4918 ArrayRef<EffectConditionExpr> Conditions; 4919 4920 // The arrays are expected to have been sorted by the caller, with the 4921 // effects in order. The conditions array must be empty or the same size 4922 // as the effects array, since the conditions are associated with the effects 4923 // at the same array indices. 4924 FunctionEffectsRef(ArrayRef<FunctionEffect> FX, 4925 ArrayRef<EffectConditionExpr> Conds) 4926 : Effects(FX), Conditions(Conds) {} 4927 4928 public: 4929 /// Extract the effects from a Type if it is a function, block, or member 4930 /// function pointer, or a reference or pointer to one. 4931 static FunctionEffectsRef get(QualType QT); 4932 4933 /// Asserts invariants. 4934 static FunctionEffectsRef create(ArrayRef<FunctionEffect> FX, 4935 ArrayRef<EffectConditionExpr> Conds); 4936 4937 FunctionEffectsRef() = default; 4938 4939 bool empty() const { return Effects.empty(); } 4940 size_t size() const { return Effects.size(); } 4941 4942 ArrayRef<FunctionEffect> effects() const { return Effects; } 4943 ArrayRef<EffectConditionExpr> conditions() const { return Conditions; } 4944 4945 using iterator = FunctionEffectIterator<FunctionEffectsRef>; 4946 friend iterator; 4947 iterator begin() const { return iterator(*this, 0); } 4948 iterator end() const { return iterator(*this, size()); } 4949 4950 friend bool operator==(const FunctionEffectsRef &LHS, 4951 const FunctionEffectsRef &RHS) { 4952 return LHS.Effects == RHS.Effects && LHS.Conditions == RHS.Conditions; 4953 } 4954 friend bool operator!=(const FunctionEffectsRef &LHS, 4955 const FunctionEffectsRef &RHS) { 4956 return !(LHS == RHS); 4957 } 4958 4959 void dump(llvm::raw_ostream &OS) const; 4960 }; 4961 4962 /// A mutable set of FunctionEffect::Kind. 4963 class FunctionEffectKindSet { 4964 // For now this only needs to be a bitmap. 4965 constexpr static size_t EndBitPos = FunctionEffect::KindCount; 4966 using KindBitsT = std::bitset<EndBitPos>; 4967 4968 KindBitsT KindBits{}; 4969 4970 explicit FunctionEffectKindSet(KindBitsT KB) : KindBits(KB) {} 4971 4972 // Functions to translate between an effect kind, starting at 1, and a 4973 // position in the bitset. 4974 4975 constexpr static size_t kindToPos(FunctionEffect::Kind K) { 4976 return static_cast<size_t>(K); 4977 } 4978 4979 constexpr static FunctionEffect::Kind posToKind(size_t Pos) { 4980 return static_cast<FunctionEffect::Kind>(Pos); 4981 } 4982 4983 // Iterates through the bits which are set. 4984 class iterator { 4985 const FunctionEffectKindSet *Outer = nullptr; 4986 size_t Idx = 0; 4987 4988 // If Idx does not reference a set bit, advance it until it does, 4989 // or until it reaches EndBitPos. 4990 void advanceToNextSetBit() { 4991 while (Idx < EndBitPos && !Outer->KindBits.test(Idx)) 4992 ++Idx; 4993 } 4994 4995 public: 4996 iterator(); 4997 iterator(const FunctionEffectKindSet &O, size_t I) : Outer(&O), Idx(I) { 4998 advanceToNextSetBit(); 4999 } 5000 bool operator==(const iterator &Other) const { return Idx == Other.Idx; } 5001 bool operator!=(const iterator &Other) const { return Idx != Other.Idx; } 5002 5003 iterator operator++() { 5004 ++Idx; 5005 advanceToNextSetBit(); 5006 return *this; 5007 } 5008 5009 FunctionEffect operator*() const { 5010 assert(Idx < EndBitPos && "Dereference of end iterator"); 5011 return FunctionEffect(posToKind(Idx)); 5012 } 5013 }; 5014 5015 public: 5016 FunctionEffectKindSet() = default; 5017 explicit FunctionEffectKindSet(FunctionEffectsRef FX) { insert(FX); } 5018 5019 iterator begin() const { return iterator(*this, 0); } 5020 iterator end() const { return iterator(*this, EndBitPos); } 5021 5022 void insert(FunctionEffect Effect) { KindBits.set(kindToPos(Effect.kind())); } 5023 void insert(FunctionEffectsRef FX) { 5024 for (FunctionEffect Item : FX.effects()) 5025 insert(Item); 5026 } 5027 void insert(FunctionEffectKindSet Set) { KindBits |= Set.KindBits; } 5028 5029 bool empty() const { return KindBits.none(); } 5030 bool contains(const FunctionEffect::Kind EK) const { 5031 return KindBits.test(kindToPos(EK)); 5032 } 5033 void dump(llvm::raw_ostream &OS) const; 5034 5035 static FunctionEffectKindSet difference(FunctionEffectKindSet LHS, 5036 FunctionEffectKindSet RHS) { 5037 return FunctionEffectKindSet(LHS.KindBits & ~RHS.KindBits); 5038 } 5039 }; 5040 5041 /// A mutable set of FunctionEffects and possibly conditions attached to them. 5042 /// Used to compare and merge effects on declarations. 5043 /// 5044 /// Has the same invariants as FunctionEffectsRef. 5045 class FunctionEffectSet { 5046 SmallVector<FunctionEffect> Effects; 5047 SmallVector<EffectConditionExpr> Conditions; 5048 5049 public: 5050 FunctionEffectSet() = default; 5051 5052 explicit FunctionEffectSet(const FunctionEffectsRef &FX) 5053 : Effects(FX.effects()), Conditions(FX.conditions()) {} 5054 5055 bool empty() const { return Effects.empty(); } 5056 size_t size() const { return Effects.size(); } 5057 5058 using iterator = FunctionEffectIterator<FunctionEffectSet>; 5059 friend iterator; 5060 iterator begin() const { return iterator(*this, 0); } 5061 iterator end() const { return iterator(*this, size()); } 5062 5063 operator FunctionEffectsRef() const { return {Effects, Conditions}; } 5064 5065 void dump(llvm::raw_ostream &OS) const; 5066 5067 // Mutators 5068 5069 // On insertion, a conflict occurs when attempting to insert an 5070 // effect which is opposite an effect already in the set, or attempting 5071 // to insert an effect which is already in the set but with a condition 5072 // which is not identical. 5073 struct Conflict { 5074 FunctionEffectWithCondition Kept; 5075 FunctionEffectWithCondition Rejected; 5076 }; 5077 using Conflicts = SmallVector<Conflict>; 5078 5079 // Returns true for success (obviating a check of Errs.empty()). 5080 bool insert(const FunctionEffectWithCondition &NewEC, Conflicts &Errs); 5081 5082 // Returns true for success (obviating a check of Errs.empty()). 5083 bool insert(const FunctionEffectsRef &Set, Conflicts &Errs); 5084 5085 // Set operations 5086 5087 static FunctionEffectSet getUnion(FunctionEffectsRef LHS, 5088 FunctionEffectsRef RHS, Conflicts &Errs); 5089 static FunctionEffectSet getIntersection(FunctionEffectsRef LHS, 5090 FunctionEffectsRef RHS); 5091 }; 5092 5093 /// Represents a prototype with parameter type info, e.g. 5094 /// 'int foo(int)' or 'int foo(void)'. 'void' is represented as having no 5095 /// parameters, not as having a single void parameter. Such a type can have 5096 /// an exception specification, but this specification is not part of the 5097 /// canonical type. FunctionProtoType has several trailing objects, some of 5098 /// which optional. For more information about the trailing objects see 5099 /// the first comment inside FunctionProtoType. 5100 class FunctionProtoType final 5101 : public FunctionType, 5102 public llvm::FoldingSetNode, 5103 private llvm::TrailingObjects< 5104 FunctionProtoType, QualType, SourceLocation, 5105 FunctionType::FunctionTypeExtraBitfields, 5106 FunctionType::FunctionTypeArmAttributes, FunctionType::ExceptionType, 5107 Expr *, FunctionDecl *, FunctionType::ExtParameterInfo, Qualifiers, 5108 FunctionEffect, EffectConditionExpr> { 5109 friend class ASTContext; // ASTContext creates these. 5110 friend TrailingObjects; 5111 5112 // FunctionProtoType is followed by several trailing objects, some of 5113 // which optional. They are in order: 5114 // 5115 // * An array of getNumParams() QualType holding the parameter types. 5116 // Always present. Note that for the vast majority of FunctionProtoType, 5117 // these will be the only trailing objects. 5118 // 5119 // * Optionally if the function is variadic, the SourceLocation of the 5120 // ellipsis. 5121 // 5122 // * Optionally if some extra data is stored in FunctionTypeExtraBitfields 5123 // (see FunctionTypeExtraBitfields and FunctionTypeBitfields): 5124 // a single FunctionTypeExtraBitfields. Present if and only if 5125 // hasExtraBitfields() is true. 5126 // 5127 // * Optionally exactly one of: 5128 // * an array of getNumExceptions() ExceptionType, 5129 // * a single Expr *, 5130 // * a pair of FunctionDecl *, 5131 // * a single FunctionDecl * 5132 // used to store information about the various types of exception 5133 // specification. See getExceptionSpecSize for the details. 5134 // 5135 // * Optionally an array of getNumParams() ExtParameterInfo holding 5136 // an ExtParameterInfo for each of the parameters. Present if and 5137 // only if hasExtParameterInfos() is true. 5138 // 5139 // * Optionally a Qualifiers object to represent extra qualifiers that can't 5140 // be represented by FunctionTypeBitfields.FastTypeQuals. Present if and 5141 // only if hasExtQualifiers() is true. 5142 // 5143 // * Optionally, an array of getNumFunctionEffects() FunctionEffect. 5144 // Present only when getNumFunctionEffects() > 0 5145 // 5146 // * Optionally, an array of getNumFunctionEffects() EffectConditionExpr. 5147 // Present only when getNumFunctionEffectConditions() > 0. 5148 // 5149 // The optional FunctionTypeExtraBitfields has to be before the data 5150 // related to the exception specification since it contains the number 5151 // of exception types. 5152 // 5153 // We put the ExtParameterInfos later. If all were equal, it would make 5154 // more sense to put these before the exception specification, because 5155 // it's much easier to skip past them compared to the elaborate switch 5156 // required to skip the exception specification. However, all is not 5157 // equal; ExtParameterInfos are used to model very uncommon features, 5158 // and it's better not to burden the more common paths. 5159 5160 public: 5161 /// Holds information about the various types of exception specification. 5162 /// ExceptionSpecInfo is not stored as such in FunctionProtoType but is 5163 /// used to group together the various bits of information about the 5164 /// exception specification. 5165 struct ExceptionSpecInfo { 5166 /// The kind of exception specification this is. 5167 ExceptionSpecificationType Type = EST_None; 5168 5169 /// Explicitly-specified list of exception types. 5170 ArrayRef<QualType> Exceptions; 5171 5172 /// Noexcept expression, if this is a computed noexcept specification. 5173 Expr *NoexceptExpr = nullptr; 5174 5175 /// The function whose exception specification this is, for 5176 /// EST_Unevaluated and EST_Uninstantiated. 5177 FunctionDecl *SourceDecl = nullptr; 5178 5179 /// The function template whose exception specification this is instantiated 5180 /// from, for EST_Uninstantiated. 5181 FunctionDecl *SourceTemplate = nullptr; 5182 5183 ExceptionSpecInfo() = default; 5184 5185 ExceptionSpecInfo(ExceptionSpecificationType EST) : Type(EST) {} 5186 5187 void instantiate(); 5188 }; 5189 5190 /// Extra information about a function prototype. ExtProtoInfo is not 5191 /// stored as such in FunctionProtoType but is used to group together 5192 /// the various bits of extra information about a function prototype. 5193 struct ExtProtoInfo { 5194 FunctionType::ExtInfo ExtInfo; 5195 unsigned Variadic : 1; 5196 unsigned HasTrailingReturn : 1; 5197 unsigned AArch64SMEAttributes : 9; 5198 Qualifiers TypeQuals; 5199 RefQualifierKind RefQualifier = RQ_None; 5200 ExceptionSpecInfo ExceptionSpec; 5201 const ExtParameterInfo *ExtParameterInfos = nullptr; 5202 SourceLocation EllipsisLoc; 5203 FunctionEffectsRef FunctionEffects; 5204 5205 ExtProtoInfo() 5206 : Variadic(false), HasTrailingReturn(false), 5207 AArch64SMEAttributes(SME_NormalFunction) {} 5208 5209 ExtProtoInfo(CallingConv CC) 5210 : ExtInfo(CC), Variadic(false), HasTrailingReturn(false), 5211 AArch64SMEAttributes(SME_NormalFunction) {} 5212 5213 ExtProtoInfo withExceptionSpec(const ExceptionSpecInfo &ESI) { 5214 ExtProtoInfo Result(*this); 5215 Result.ExceptionSpec = ESI; 5216 return Result; 5217 } 5218 5219 bool requiresFunctionProtoTypeExtraBitfields() const { 5220 return ExceptionSpec.Type == EST_Dynamic || 5221 requiresFunctionProtoTypeArmAttributes() || 5222 !FunctionEffects.empty(); 5223 } 5224 5225 bool requiresFunctionProtoTypeArmAttributes() const { 5226 return AArch64SMEAttributes != SME_NormalFunction; 5227 } 5228 5229 void setArmSMEAttribute(AArch64SMETypeAttributes Kind, bool Enable = true) { 5230 if (Enable) 5231 AArch64SMEAttributes |= Kind; 5232 else 5233 AArch64SMEAttributes &= ~Kind; 5234 } 5235 }; 5236 5237 private: 5238 unsigned numTrailingObjects(OverloadToken<QualType>) const { 5239 return getNumParams(); 5240 } 5241 5242 unsigned numTrailingObjects(OverloadToken<SourceLocation>) const { 5243 return isVariadic(); 5244 } 5245 5246 unsigned numTrailingObjects(OverloadToken<FunctionTypeArmAttributes>) const { 5247 return hasArmTypeAttributes(); 5248 } 5249 5250 unsigned numTrailingObjects(OverloadToken<FunctionTypeExtraBitfields>) const { 5251 return hasExtraBitfields(); 5252 } 5253 5254 unsigned numTrailingObjects(OverloadToken<ExceptionType>) const { 5255 return getExceptionSpecSize().NumExceptionType; 5256 } 5257 5258 unsigned numTrailingObjects(OverloadToken<Expr *>) const { 5259 return getExceptionSpecSize().NumExprPtr; 5260 } 5261 5262 unsigned numTrailingObjects(OverloadToken<FunctionDecl *>) const { 5263 return getExceptionSpecSize().NumFunctionDeclPtr; 5264 } 5265 5266 unsigned numTrailingObjects(OverloadToken<ExtParameterInfo>) const { 5267 return hasExtParameterInfos() ? getNumParams() : 0; 5268 } 5269 5270 unsigned numTrailingObjects(OverloadToken<Qualifiers>) const { 5271 return hasExtQualifiers() ? 1 : 0; 5272 } 5273 5274 unsigned numTrailingObjects(OverloadToken<FunctionEffect>) const { 5275 return getNumFunctionEffects(); 5276 } 5277 5278 unsigned numTrailingObjects(OverloadToken<EffectConditionExpr>) const { 5279 return getNumFunctionEffectConditions(); 5280 } 5281 5282 /// Determine whether there are any argument types that 5283 /// contain an unexpanded parameter pack. 5284 static bool containsAnyUnexpandedParameterPack(const QualType *ArgArray, 5285 unsigned numArgs) { 5286 for (unsigned Idx = 0; Idx < numArgs; ++Idx) 5287 if (ArgArray[Idx]->containsUnexpandedParameterPack()) 5288 return true; 5289 5290 return false; 5291 } 5292 5293 FunctionProtoType(QualType result, ArrayRef<QualType> params, 5294 QualType canonical, const ExtProtoInfo &epi); 5295 5296 /// This struct is returned by getExceptionSpecSize and is used to 5297 /// translate an ExceptionSpecificationType to the number and kind 5298 /// of trailing objects related to the exception specification. 5299 struct ExceptionSpecSizeHolder { 5300 unsigned NumExceptionType; 5301 unsigned NumExprPtr; 5302 unsigned NumFunctionDeclPtr; 5303 }; 5304 5305 /// Return the number and kind of trailing objects 5306 /// related to the exception specification. 5307 static ExceptionSpecSizeHolder 5308 getExceptionSpecSize(ExceptionSpecificationType EST, unsigned NumExceptions) { 5309 switch (EST) { 5310 case EST_None: 5311 case EST_DynamicNone: 5312 case EST_MSAny: 5313 case EST_BasicNoexcept: 5314 case EST_Unparsed: 5315 case EST_NoThrow: 5316 return {0, 0, 0}; 5317 5318 case EST_Dynamic: 5319 return {NumExceptions, 0, 0}; 5320 5321 case EST_DependentNoexcept: 5322 case EST_NoexceptFalse: 5323 case EST_NoexceptTrue: 5324 return {0, 1, 0}; 5325 5326 case EST_Uninstantiated: 5327 return {0, 0, 2}; 5328 5329 case EST_Unevaluated: 5330 return {0, 0, 1}; 5331 } 5332 llvm_unreachable("bad exception specification kind"); 5333 } 5334 5335 /// Return the number and kind of trailing objects 5336 /// related to the exception specification. 5337 ExceptionSpecSizeHolder getExceptionSpecSize() const { 5338 return getExceptionSpecSize(getExceptionSpecType(), getNumExceptions()); 5339 } 5340 5341 /// Whether the trailing FunctionTypeExtraBitfields is present. 5342 bool hasExtraBitfields() const { 5343 assert((getExceptionSpecType() != EST_Dynamic || 5344 FunctionTypeBits.HasExtraBitfields) && 5345 "ExtraBitfields are required for given ExceptionSpecType"); 5346 return FunctionTypeBits.HasExtraBitfields; 5347 5348 } 5349 5350 bool hasArmTypeAttributes() const { 5351 return FunctionTypeBits.HasExtraBitfields && 5352 getTrailingObjects<FunctionTypeExtraBitfields>() 5353 ->HasArmTypeAttributes; 5354 } 5355 5356 bool hasExtQualifiers() const { 5357 return FunctionTypeBits.HasExtQuals; 5358 } 5359 5360 public: 5361 unsigned getNumParams() const { return FunctionTypeBits.NumParams; } 5362 5363 QualType getParamType(unsigned i) const { 5364 assert(i < getNumParams() && "invalid parameter index"); 5365 return param_type_begin()[i]; 5366 } 5367 5368 ArrayRef<QualType> getParamTypes() const { 5369 return llvm::ArrayRef(param_type_begin(), param_type_end()); 5370 } 5371 5372 ExtProtoInfo getExtProtoInfo() const { 5373 ExtProtoInfo EPI; 5374 EPI.ExtInfo = getExtInfo(); 5375 EPI.Variadic = isVariadic(); 5376 EPI.EllipsisLoc = getEllipsisLoc(); 5377 EPI.HasTrailingReturn = hasTrailingReturn(); 5378 EPI.ExceptionSpec = getExceptionSpecInfo(); 5379 EPI.TypeQuals = getMethodQuals(); 5380 EPI.RefQualifier = getRefQualifier(); 5381 EPI.ExtParameterInfos = getExtParameterInfosOrNull(); 5382 EPI.AArch64SMEAttributes = getAArch64SMEAttributes(); 5383 EPI.FunctionEffects = getFunctionEffects(); 5384 return EPI; 5385 } 5386 5387 /// Get the kind of exception specification on this function. 5388 ExceptionSpecificationType getExceptionSpecType() const { 5389 return static_cast<ExceptionSpecificationType>( 5390 FunctionTypeBits.ExceptionSpecType); 5391 } 5392 5393 /// Return whether this function has any kind of exception spec. 5394 bool hasExceptionSpec() const { return getExceptionSpecType() != EST_None; } 5395 5396 /// Return whether this function has a dynamic (throw) exception spec. 5397 bool hasDynamicExceptionSpec() const { 5398 return isDynamicExceptionSpec(getExceptionSpecType()); 5399 } 5400 5401 /// Return whether this function has a noexcept exception spec. 5402 bool hasNoexceptExceptionSpec() const { 5403 return isNoexceptExceptionSpec(getExceptionSpecType()); 5404 } 5405 5406 /// Return whether this function has a dependent exception spec. 5407 bool hasDependentExceptionSpec() const; 5408 5409 /// Return whether this function has an instantiation-dependent exception 5410 /// spec. 5411 bool hasInstantiationDependentExceptionSpec() const; 5412 5413 /// Return all the available information about this type's exception spec. 5414 ExceptionSpecInfo getExceptionSpecInfo() const { 5415 ExceptionSpecInfo Result; 5416 Result.Type = getExceptionSpecType(); 5417 if (Result.Type == EST_Dynamic) { 5418 Result.Exceptions = exceptions(); 5419 } else if (isComputedNoexcept(Result.Type)) { 5420 Result.NoexceptExpr = getNoexceptExpr(); 5421 } else if (Result.Type == EST_Uninstantiated) { 5422 Result.SourceDecl = getExceptionSpecDecl(); 5423 Result.SourceTemplate = getExceptionSpecTemplate(); 5424 } else if (Result.Type == EST_Unevaluated) { 5425 Result.SourceDecl = getExceptionSpecDecl(); 5426 } 5427 return Result; 5428 } 5429 5430 /// Return the number of types in the exception specification. 5431 unsigned getNumExceptions() const { 5432 return getExceptionSpecType() == EST_Dynamic 5433 ? getTrailingObjects<FunctionTypeExtraBitfields>() 5434 ->NumExceptionType 5435 : 0; 5436 } 5437 5438 /// Return the ith exception type, where 0 <= i < getNumExceptions(). 5439 QualType getExceptionType(unsigned i) const { 5440 assert(i < getNumExceptions() && "Invalid exception number!"); 5441 return exception_begin()[i]; 5442 } 5443 5444 /// Return the expression inside noexcept(expression), or a null pointer 5445 /// if there is none (because the exception spec is not of this form). 5446 Expr *getNoexceptExpr() const { 5447 if (!isComputedNoexcept(getExceptionSpecType())) 5448 return nullptr; 5449 return *getTrailingObjects<Expr *>(); 5450 } 5451 5452 /// If this function type has an exception specification which hasn't 5453 /// been determined yet (either because it has not been evaluated or because 5454 /// it has not been instantiated), this is the function whose exception 5455 /// specification is represented by this type. 5456 FunctionDecl *getExceptionSpecDecl() const { 5457 if (getExceptionSpecType() != EST_Uninstantiated && 5458 getExceptionSpecType() != EST_Unevaluated) 5459 return nullptr; 5460 return getTrailingObjects<FunctionDecl *>()[0]; 5461 } 5462 5463 /// If this function type has an uninstantiated exception 5464 /// specification, this is the function whose exception specification 5465 /// should be instantiated to find the exception specification for 5466 /// this type. 5467 FunctionDecl *getExceptionSpecTemplate() const { 5468 if (getExceptionSpecType() != EST_Uninstantiated) 5469 return nullptr; 5470 return getTrailingObjects<FunctionDecl *>()[1]; 5471 } 5472 5473 /// Determine whether this function type has a non-throwing exception 5474 /// specification. 5475 CanThrowResult canThrow() const; 5476 5477 /// Determine whether this function type has a non-throwing exception 5478 /// specification. If this depends on template arguments, returns 5479 /// \c ResultIfDependent. 5480 bool isNothrow(bool ResultIfDependent = false) const { 5481 return ResultIfDependent ? canThrow() != CT_Can : canThrow() == CT_Cannot; 5482 } 5483 5484 /// Whether this function prototype is variadic. 5485 bool isVariadic() const { return FunctionTypeBits.Variadic; } 5486 5487 SourceLocation getEllipsisLoc() const { 5488 return isVariadic() ? *getTrailingObjects<SourceLocation>() 5489 : SourceLocation(); 5490 } 5491 5492 /// Determines whether this function prototype contains a 5493 /// parameter pack at the end. 5494 /// 5495 /// A function template whose last parameter is a parameter pack can be 5496 /// called with an arbitrary number of arguments, much like a variadic 5497 /// function. 5498 bool isTemplateVariadic() const; 5499 5500 /// Whether this function prototype has a trailing return type. 5501 bool hasTrailingReturn() const { return FunctionTypeBits.HasTrailingReturn; } 5502 5503 Qualifiers getMethodQuals() const { 5504 if (hasExtQualifiers()) 5505 return *getTrailingObjects<Qualifiers>(); 5506 else 5507 return getFastTypeQuals(); 5508 } 5509 5510 /// Retrieve the ref-qualifier associated with this function type. 5511 RefQualifierKind getRefQualifier() const { 5512 return static_cast<RefQualifierKind>(FunctionTypeBits.RefQualifier); 5513 } 5514 5515 using param_type_iterator = const QualType *; 5516 5517 ArrayRef<QualType> param_types() const { 5518 return llvm::ArrayRef(param_type_begin(), param_type_end()); 5519 } 5520 5521 param_type_iterator param_type_begin() const { 5522 return getTrailingObjects<QualType>(); 5523 } 5524 5525 param_type_iterator param_type_end() const { 5526 return param_type_begin() + getNumParams(); 5527 } 5528 5529 using exception_iterator = const QualType *; 5530 5531 ArrayRef<QualType> exceptions() const { 5532 return llvm::ArrayRef(exception_begin(), exception_end()); 5533 } 5534 5535 exception_iterator exception_begin() const { 5536 return reinterpret_cast<exception_iterator>( 5537 getTrailingObjects<ExceptionType>()); 5538 } 5539 5540 exception_iterator exception_end() const { 5541 return exception_begin() + getNumExceptions(); 5542 } 5543 5544 /// Is there any interesting extra information for any of the parameters 5545 /// of this function type? 5546 bool hasExtParameterInfos() const { 5547 return FunctionTypeBits.HasExtParameterInfos; 5548 } 5549 5550 ArrayRef<ExtParameterInfo> getExtParameterInfos() const { 5551 assert(hasExtParameterInfos()); 5552 return ArrayRef<ExtParameterInfo>(getTrailingObjects<ExtParameterInfo>(), 5553 getNumParams()); 5554 } 5555 5556 /// Return a pointer to the beginning of the array of extra parameter 5557 /// information, if present, or else null if none of the parameters 5558 /// carry it. This is equivalent to getExtProtoInfo().ExtParameterInfos. 5559 const ExtParameterInfo *getExtParameterInfosOrNull() const { 5560 if (!hasExtParameterInfos()) 5561 return nullptr; 5562 return getTrailingObjects<ExtParameterInfo>(); 5563 } 5564 5565 /// Return a bitmask describing the SME attributes on the function type, see 5566 /// AArch64SMETypeAttributes for their values. 5567 unsigned getAArch64SMEAttributes() const { 5568 if (!hasArmTypeAttributes()) 5569 return SME_NormalFunction; 5570 return getTrailingObjects<FunctionTypeArmAttributes>() 5571 ->AArch64SMEAttributes; 5572 } 5573 5574 ExtParameterInfo getExtParameterInfo(unsigned I) const { 5575 assert(I < getNumParams() && "parameter index out of range"); 5576 if (hasExtParameterInfos()) 5577 return getTrailingObjects<ExtParameterInfo>()[I]; 5578 return ExtParameterInfo(); 5579 } 5580 5581 ParameterABI getParameterABI(unsigned I) const { 5582 assert(I < getNumParams() && "parameter index out of range"); 5583 if (hasExtParameterInfos()) 5584 return getTrailingObjects<ExtParameterInfo>()[I].getABI(); 5585 return ParameterABI::Ordinary; 5586 } 5587 5588 bool isParamConsumed(unsigned I) const { 5589 assert(I < getNumParams() && "parameter index out of range"); 5590 if (hasExtParameterInfos()) 5591 return getTrailingObjects<ExtParameterInfo>()[I].isConsumed(); 5592 return false; 5593 } 5594 5595 unsigned getNumFunctionEffects() const { 5596 return hasExtraBitfields() 5597 ? getTrailingObjects<FunctionTypeExtraBitfields>() 5598 ->NumFunctionEffects 5599 : 0; 5600 } 5601 5602 // For serialization. 5603 ArrayRef<FunctionEffect> getFunctionEffectsWithoutConditions() const { 5604 if (hasExtraBitfields()) { 5605 const auto *Bitfields = getTrailingObjects<FunctionTypeExtraBitfields>(); 5606 if (Bitfields->NumFunctionEffects > 0) 5607 return {getTrailingObjects<FunctionEffect>(), 5608 Bitfields->NumFunctionEffects}; 5609 } 5610 return {}; 5611 } 5612 5613 unsigned getNumFunctionEffectConditions() const { 5614 if (hasExtraBitfields()) { 5615 const auto *Bitfields = getTrailingObjects<FunctionTypeExtraBitfields>(); 5616 if (Bitfields->EffectsHaveConditions) 5617 return Bitfields->NumFunctionEffects; 5618 } 5619 return 0; 5620 } 5621 5622 // For serialization. 5623 ArrayRef<EffectConditionExpr> getFunctionEffectConditions() const { 5624 if (hasExtraBitfields()) { 5625 const auto *Bitfields = getTrailingObjects<FunctionTypeExtraBitfields>(); 5626 if (Bitfields->EffectsHaveConditions) 5627 return {getTrailingObjects<EffectConditionExpr>(), 5628 Bitfields->NumFunctionEffects}; 5629 } 5630 return {}; 5631 } 5632 5633 // Combines effects with their conditions. 5634 FunctionEffectsRef getFunctionEffects() const { 5635 if (hasExtraBitfields()) { 5636 const auto *Bitfields = getTrailingObjects<FunctionTypeExtraBitfields>(); 5637 if (Bitfields->NumFunctionEffects > 0) { 5638 const size_t NumConds = Bitfields->EffectsHaveConditions 5639 ? Bitfields->NumFunctionEffects 5640 : 0; 5641 return FunctionEffectsRef( 5642 {getTrailingObjects<FunctionEffect>(), 5643 Bitfields->NumFunctionEffects}, 5644 {NumConds ? getTrailingObjects<EffectConditionExpr>() : nullptr, 5645 NumConds}); 5646 } 5647 } 5648 return {}; 5649 } 5650 5651 bool isSugared() const { return false; } 5652 QualType desugar() const { return QualType(this, 0); } 5653 5654 void printExceptionSpecification(raw_ostream &OS, 5655 const PrintingPolicy &Policy) const; 5656 5657 static bool classof(const Type *T) { 5658 return T->getTypeClass() == FunctionProto; 5659 } 5660 5661 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx); 5662 static void Profile(llvm::FoldingSetNodeID &ID, QualType Result, 5663 param_type_iterator ArgTys, unsigned NumArgs, 5664 const ExtProtoInfo &EPI, const ASTContext &Context, 5665 bool Canonical); 5666 }; 5667 5668 /// Represents the dependent type named by a dependently-scoped 5669 /// typename using declaration, e.g. 5670 /// using typename Base<T>::foo; 5671 /// 5672 /// Template instantiation turns these into the underlying type. 5673 class UnresolvedUsingType : public Type { 5674 friend class ASTContext; // ASTContext creates these. 5675 5676 UnresolvedUsingTypenameDecl *Decl; 5677 5678 UnresolvedUsingType(const UnresolvedUsingTypenameDecl *D) 5679 : Type(UnresolvedUsing, QualType(), 5680 TypeDependence::DependentInstantiation), 5681 Decl(const_cast<UnresolvedUsingTypenameDecl *>(D)) {} 5682 5683 public: 5684 UnresolvedUsingTypenameDecl *getDecl() const { return Decl; } 5685 5686 bool isSugared() const { return false; } 5687 QualType desugar() const { return QualType(this, 0); } 5688 5689 static bool classof(const Type *T) { 5690 return T->getTypeClass() == UnresolvedUsing; 5691 } 5692 5693 void Profile(llvm::FoldingSetNodeID &ID) { 5694 return Profile(ID, Decl); 5695 } 5696 5697 static void Profile(llvm::FoldingSetNodeID &ID, 5698 UnresolvedUsingTypenameDecl *D) { 5699 ID.AddPointer(D); 5700 } 5701 }; 5702 5703 class UsingType final : public Type, 5704 public llvm::FoldingSetNode, 5705 private llvm::TrailingObjects<UsingType, QualType> { 5706 UsingShadowDecl *Found; 5707 friend class ASTContext; // ASTContext creates these. 5708 friend TrailingObjects; 5709 5710 UsingType(const UsingShadowDecl *Found, QualType Underlying, QualType Canon); 5711 5712 public: 5713 UsingShadowDecl *getFoundDecl() const { return Found; } 5714 QualType getUnderlyingType() const; 5715 5716 bool isSugared() const { return true; } 5717 5718 // This always has the 'same' type as declared, but not necessarily identical. 5719 QualType desugar() const { return getUnderlyingType(); } 5720 5721 // Internal helper, for debugging purposes. 5722 bool typeMatchesDecl() const { return !UsingBits.hasTypeDifferentFromDecl; } 5723 5724 void Profile(llvm::FoldingSetNodeID &ID) { 5725 Profile(ID, Found, getUnderlyingType()); 5726 } 5727 static void Profile(llvm::FoldingSetNodeID &ID, const UsingShadowDecl *Found, 5728 QualType Underlying) { 5729 ID.AddPointer(Found); 5730 Underlying.Profile(ID); 5731 } 5732 static bool classof(const Type *T) { return T->getTypeClass() == Using; } 5733 }; 5734 5735 class TypedefType final : public Type, 5736 public llvm::FoldingSetNode, 5737 private llvm::TrailingObjects<TypedefType, QualType> { 5738 TypedefNameDecl *Decl; 5739 friend class ASTContext; // ASTContext creates these. 5740 friend TrailingObjects; 5741 5742 TypedefType(TypeClass tc, const TypedefNameDecl *D, QualType underlying, 5743 QualType can); 5744 5745 public: 5746 TypedefNameDecl *getDecl() const { return Decl; } 5747 5748 bool isSugared() const { return true; } 5749 5750 // This always has the 'same' type as declared, but not necessarily identical. 5751 QualType desugar() const; 5752 5753 // Internal helper, for debugging purposes. 5754 bool typeMatchesDecl() const { return !TypedefBits.hasTypeDifferentFromDecl; } 5755 5756 void Profile(llvm::FoldingSetNodeID &ID) { 5757 Profile(ID, Decl, typeMatchesDecl() ? QualType() : desugar()); 5758 } 5759 static void Profile(llvm::FoldingSetNodeID &ID, const TypedefNameDecl *Decl, 5760 QualType Underlying) { 5761 ID.AddPointer(Decl); 5762 if (!Underlying.isNull()) 5763 Underlying.Profile(ID); 5764 } 5765 5766 static bool classof(const Type *T) { return T->getTypeClass() == Typedef; } 5767 }; 5768 5769 /// Sugar type that represents a type that was qualified by a qualifier written 5770 /// as a macro invocation. 5771 class MacroQualifiedType : public Type { 5772 friend class ASTContext; // ASTContext creates these. 5773 5774 QualType UnderlyingTy; 5775 const IdentifierInfo *MacroII; 5776 5777 MacroQualifiedType(QualType UnderlyingTy, QualType CanonTy, 5778 const IdentifierInfo *MacroII) 5779 : Type(MacroQualified, CanonTy, UnderlyingTy->getDependence()), 5780 UnderlyingTy(UnderlyingTy), MacroII(MacroII) { 5781 assert(isa<AttributedType>(UnderlyingTy) && 5782 "Expected a macro qualified type to only wrap attributed types."); 5783 } 5784 5785 public: 5786 const IdentifierInfo *getMacroIdentifier() const { return MacroII; } 5787 QualType getUnderlyingType() const { return UnderlyingTy; } 5788 5789 /// Return this attributed type's modified type with no qualifiers attached to 5790 /// it. 5791 QualType getModifiedType() const; 5792 5793 bool isSugared() const { return true; } 5794 QualType desugar() const; 5795 5796 static bool classof(const Type *T) { 5797 return T->getTypeClass() == MacroQualified; 5798 } 5799 }; 5800 5801 /// Represents a `typeof` (or __typeof__) expression (a C23 feature and GCC 5802 /// extension) or a `typeof_unqual` expression (a C23 feature). 5803 class TypeOfExprType : public Type { 5804 Expr *TOExpr; 5805 const ASTContext &Context; 5806 5807 protected: 5808 friend class ASTContext; // ASTContext creates these. 5809 5810 TypeOfExprType(const ASTContext &Context, Expr *E, TypeOfKind Kind, 5811 QualType Can = QualType()); 5812 5813 public: 5814 Expr *getUnderlyingExpr() const { return TOExpr; } 5815 5816 /// Returns the kind of 'typeof' type this is. 5817 TypeOfKind getKind() const { 5818 return static_cast<TypeOfKind>(TypeOfBits.Kind); 5819 } 5820 5821 /// Remove a single level of sugar. 5822 QualType desugar() const; 5823 5824 /// Returns whether this type directly provides sugar. 5825 bool isSugared() const; 5826 5827 static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; } 5828 }; 5829 5830 /// Internal representation of canonical, dependent 5831 /// `typeof(expr)` types. 5832 /// 5833 /// This class is used internally by the ASTContext to manage 5834 /// canonical, dependent types, only. Clients will only see instances 5835 /// of this class via TypeOfExprType nodes. 5836 class DependentTypeOfExprType : public TypeOfExprType, 5837 public llvm::FoldingSetNode { 5838 public: 5839 DependentTypeOfExprType(const ASTContext &Context, Expr *E, TypeOfKind Kind) 5840 : TypeOfExprType(Context, E, Kind) {} 5841 5842 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { 5843 Profile(ID, Context, getUnderlyingExpr(), 5844 getKind() == TypeOfKind::Unqualified); 5845 } 5846 5847 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 5848 Expr *E, bool IsUnqual); 5849 }; 5850 5851 /// Represents `typeof(type)`, a C23 feature and GCC extension, or 5852 /// `typeof_unqual(type), a C23 feature. 5853 class TypeOfType : public Type { 5854 friend class ASTContext; // ASTContext creates these. 5855 5856 QualType TOType; 5857 const ASTContext &Context; 5858 5859 TypeOfType(const ASTContext &Context, QualType T, QualType Can, 5860 TypeOfKind Kind); 5861 5862 public: 5863 QualType getUnmodifiedType() const { return TOType; } 5864 5865 /// Remove a single level of sugar. 5866 QualType desugar() const; 5867 5868 /// Returns whether this type directly provides sugar. 5869 bool isSugared() const { return true; } 5870 5871 /// Returns the kind of 'typeof' type this is. 5872 TypeOfKind getKind() const { 5873 return static_cast<TypeOfKind>(TypeOfBits.Kind); 5874 } 5875 5876 static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; } 5877 }; 5878 5879 /// Represents the type `decltype(expr)` (C++11). 5880 class DecltypeType : public Type { 5881 Expr *E; 5882 QualType UnderlyingType; 5883 5884 protected: 5885 friend class ASTContext; // ASTContext creates these. 5886 5887 DecltypeType(Expr *E, QualType underlyingType, QualType can = QualType()); 5888 5889 public: 5890 Expr *getUnderlyingExpr() const { return E; } 5891 QualType getUnderlyingType() const { return UnderlyingType; } 5892 5893 /// Remove a single level of sugar. 5894 QualType desugar() const; 5895 5896 /// Returns whether this type directly provides sugar. 5897 bool isSugared() const; 5898 5899 static bool classof(const Type *T) { return T->getTypeClass() == Decltype; } 5900 }; 5901 5902 /// Internal representation of canonical, dependent 5903 /// decltype(expr) types. 5904 /// 5905 /// This class is used internally by the ASTContext to manage 5906 /// canonical, dependent types, only. Clients will only see instances 5907 /// of this class via DecltypeType nodes. 5908 class DependentDecltypeType : public DecltypeType, public llvm::FoldingSetNode { 5909 public: 5910 DependentDecltypeType(Expr *E, QualType UnderlyingTpe); 5911 5912 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { 5913 Profile(ID, Context, getUnderlyingExpr()); 5914 } 5915 5916 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 5917 Expr *E); 5918 }; 5919 5920 class PackIndexingType final 5921 : public Type, 5922 public llvm::FoldingSetNode, 5923 private llvm::TrailingObjects<PackIndexingType, QualType> { 5924 friend TrailingObjects; 5925 5926 const ASTContext &Context; 5927 QualType Pattern; 5928 Expr *IndexExpr; 5929 5930 unsigned Size : 31; 5931 5932 LLVM_PREFERRED_TYPE(bool) 5933 unsigned FullySubstituted : 1; 5934 5935 protected: 5936 friend class ASTContext; // ASTContext creates these. 5937 PackIndexingType(const ASTContext &Context, QualType Canonical, 5938 QualType Pattern, Expr *IndexExpr, bool FullySubstituted, 5939 ArrayRef<QualType> Expansions = {}); 5940 5941 public: 5942 Expr *getIndexExpr() const { return IndexExpr; } 5943 QualType getPattern() const { return Pattern; } 5944 5945 bool isSugared() const { return hasSelectedType(); } 5946 5947 QualType desugar() const { 5948 if (hasSelectedType()) 5949 return getSelectedType(); 5950 return QualType(this, 0); 5951 } 5952 5953 QualType getSelectedType() const { 5954 assert(hasSelectedType() && "Type is dependant"); 5955 return *(getExpansionsPtr() + *getSelectedIndex()); 5956 } 5957 5958 std::optional<unsigned> getSelectedIndex() const; 5959 5960 bool hasSelectedType() const { return getSelectedIndex() != std::nullopt; } 5961 5962 bool isFullySubstituted() const { return FullySubstituted; } 5963 5964 bool expandsToEmptyPack() const { return isFullySubstituted() && Size == 0; } 5965 5966 ArrayRef<QualType> getExpansions() const { 5967 return {getExpansionsPtr(), Size}; 5968 } 5969 5970 static bool classof(const Type *T) { 5971 return T->getTypeClass() == PackIndexing; 5972 } 5973 5974 void Profile(llvm::FoldingSetNodeID &ID) { 5975 if (hasSelectedType()) 5976 getSelectedType().Profile(ID); 5977 else 5978 Profile(ID, Context, getPattern(), getIndexExpr(), isFullySubstituted()); 5979 } 5980 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 5981 QualType Pattern, Expr *E, bool FullySubstituted); 5982 5983 private: 5984 const QualType *getExpansionsPtr() const { 5985 return getTrailingObjects<QualType>(); 5986 } 5987 5988 static TypeDependence computeDependence(QualType Pattern, Expr *IndexExpr, 5989 ArrayRef<QualType> Expansions = {}); 5990 5991 unsigned numTrailingObjects(OverloadToken<QualType>) const { return Size; } 5992 }; 5993 5994 /// A unary type transform, which is a type constructed from another. 5995 class UnaryTransformType : public Type { 5996 public: 5997 enum UTTKind { 5998 #define TRANSFORM_TYPE_TRAIT_DEF(Enum, _) Enum, 5999 #include "clang/Basic/TransformTypeTraits.def" 6000 }; 6001 6002 private: 6003 /// The untransformed type. 6004 QualType BaseType; 6005 6006 /// The transformed type if not dependent, otherwise the same as BaseType. 6007 QualType UnderlyingType; 6008 6009 UTTKind UKind; 6010 6011 protected: 6012 friend class ASTContext; 6013 6014 UnaryTransformType(QualType BaseTy, QualType UnderlyingTy, UTTKind UKind, 6015 QualType CanonicalTy); 6016 6017 public: 6018 bool isSugared() const { return !isDependentType(); } 6019 QualType desugar() const { return UnderlyingType; } 6020 6021 QualType getUnderlyingType() const { return UnderlyingType; } 6022 QualType getBaseType() const { return BaseType; } 6023 6024 UTTKind getUTTKind() const { return UKind; } 6025 6026 static bool classof(const Type *T) { 6027 return T->getTypeClass() == UnaryTransform; 6028 } 6029 }; 6030 6031 /// Internal representation of canonical, dependent 6032 /// __underlying_type(type) types. 6033 /// 6034 /// This class is used internally by the ASTContext to manage 6035 /// canonical, dependent types, only. Clients will only see instances 6036 /// of this class via UnaryTransformType nodes. 6037 class DependentUnaryTransformType : public UnaryTransformType, 6038 public llvm::FoldingSetNode { 6039 public: 6040 DependentUnaryTransformType(const ASTContext &C, QualType BaseType, 6041 UTTKind UKind); 6042 6043 void Profile(llvm::FoldingSetNodeID &ID) { 6044 Profile(ID, getBaseType(), getUTTKind()); 6045 } 6046 6047 static void Profile(llvm::FoldingSetNodeID &ID, QualType BaseType, 6048 UTTKind UKind) { 6049 ID.AddPointer(BaseType.getAsOpaquePtr()); 6050 ID.AddInteger((unsigned)UKind); 6051 } 6052 }; 6053 6054 class TagType : public Type { 6055 friend class ASTReader; 6056 template <class T> friend class serialization::AbstractTypeReader; 6057 6058 /// Stores the TagDecl associated with this type. The decl may point to any 6059 /// TagDecl that declares the entity. 6060 TagDecl *decl; 6061 6062 protected: 6063 TagType(TypeClass TC, const TagDecl *D, QualType can); 6064 6065 public: 6066 TagDecl *getDecl() const; 6067 6068 /// Determines whether this type is in the process of being defined. 6069 bool isBeingDefined() const; 6070 6071 static bool classof(const Type *T) { 6072 return T->getTypeClass() == Enum || T->getTypeClass() == Record; 6073 } 6074 }; 6075 6076 /// A helper class that allows the use of isa/cast/dyncast 6077 /// to detect TagType objects of structs/unions/classes. 6078 class RecordType : public TagType { 6079 protected: 6080 friend class ASTContext; // ASTContext creates these. 6081 6082 explicit RecordType(const RecordDecl *D) 6083 : TagType(Record, reinterpret_cast<const TagDecl*>(D), QualType()) {} 6084 explicit RecordType(TypeClass TC, RecordDecl *D) 6085 : TagType(TC, reinterpret_cast<const TagDecl*>(D), QualType()) {} 6086 6087 public: 6088 RecordDecl *getDecl() const { 6089 return reinterpret_cast<RecordDecl*>(TagType::getDecl()); 6090 } 6091 6092 /// Recursively check all fields in the record for const-ness. If any field 6093 /// is declared const, return true. Otherwise, return false. 6094 bool hasConstFields() const; 6095 6096 bool isSugared() const { return false; } 6097 QualType desugar() const { return QualType(this, 0); } 6098 6099 static bool classof(const Type *T) { return T->getTypeClass() == Record; } 6100 }; 6101 6102 /// A helper class that allows the use of isa/cast/dyncast 6103 /// to detect TagType objects of enums. 6104 class EnumType : public TagType { 6105 friend class ASTContext; // ASTContext creates these. 6106 6107 explicit EnumType(const EnumDecl *D) 6108 : TagType(Enum, reinterpret_cast<const TagDecl*>(D), QualType()) {} 6109 6110 public: 6111 EnumDecl *getDecl() const { 6112 return reinterpret_cast<EnumDecl*>(TagType::getDecl()); 6113 } 6114 6115 bool isSugared() const { return false; } 6116 QualType desugar() const { return QualType(this, 0); } 6117 6118 static bool classof(const Type *T) { return T->getTypeClass() == Enum; } 6119 }; 6120 6121 /// An attributed type is a type to which a type attribute has been applied. 6122 /// 6123 /// The "modified type" is the fully-sugared type to which the attributed 6124 /// type was applied; generally it is not canonically equivalent to the 6125 /// attributed type. The "equivalent type" is the minimally-desugared type 6126 /// which the type is canonically equivalent to. 6127 /// 6128 /// For example, in the following attributed type: 6129 /// int32_t __attribute__((vector_size(16))) 6130 /// - the modified type is the TypedefType for int32_t 6131 /// - the equivalent type is VectorType(16, int32_t) 6132 /// - the canonical type is VectorType(16, int) 6133 class AttributedType : public Type, public llvm::FoldingSetNode { 6134 public: 6135 using Kind = attr::Kind; 6136 6137 private: 6138 friend class ASTContext; // ASTContext creates these 6139 6140 const Attr *Attribute; 6141 6142 QualType ModifiedType; 6143 QualType EquivalentType; 6144 6145 AttributedType(QualType canon, attr::Kind attrKind, QualType modified, 6146 QualType equivalent) 6147 : AttributedType(canon, attrKind, nullptr, modified, equivalent) {} 6148 6149 AttributedType(QualType canon, const Attr *attr, QualType modified, 6150 QualType equivalent); 6151 6152 private: 6153 AttributedType(QualType canon, attr::Kind attrKind, const Attr *attr, 6154 QualType modified, QualType equivalent); 6155 6156 public: 6157 Kind getAttrKind() const { 6158 return static_cast<Kind>(AttributedTypeBits.AttrKind); 6159 } 6160 6161 const Attr *getAttr() const { return Attribute; } 6162 6163 QualType getModifiedType() const { return ModifiedType; } 6164 QualType getEquivalentType() const { return EquivalentType; } 6165 6166 bool isSugared() const { return true; } 6167 QualType desugar() const { return getEquivalentType(); } 6168 6169 /// Does this attribute behave like a type qualifier? 6170 /// 6171 /// A type qualifier adjusts a type to provide specialized rules for 6172 /// a specific object, like the standard const and volatile qualifiers. 6173 /// This includes attributes controlling things like nullability, 6174 /// address spaces, and ARC ownership. The value of the object is still 6175 /// largely described by the modified type. 6176 /// 6177 /// In contrast, many type attributes "rewrite" their modified type to 6178 /// produce a fundamentally different type, not necessarily related in any 6179 /// formalizable way to the original type. For example, calling convention 6180 /// and vector attributes are not simple type qualifiers. 6181 /// 6182 /// Type qualifiers are often, but not always, reflected in the canonical 6183 /// type. 6184 bool isQualifier() const; 6185 6186 bool isMSTypeSpec() const; 6187 6188 bool isWebAssemblyFuncrefSpec() const; 6189 6190 bool isCallingConv() const; 6191 6192 std::optional<NullabilityKind> getImmediateNullability() const; 6193 6194 /// Strip off the top-level nullability annotation on the given 6195 /// type, if it's there. 6196 /// 6197 /// \param T The type to strip. If the type is exactly an 6198 /// AttributedType specifying nullability (without looking through 6199 /// type sugar), the nullability is returned and this type changed 6200 /// to the underlying modified type. 6201 /// 6202 /// \returns the top-level nullability, if present. 6203 static std::optional<NullabilityKind> stripOuterNullability(QualType &T); 6204 6205 void Profile(llvm::FoldingSetNodeID &ID) { 6206 Profile(ID, getAttrKind(), ModifiedType, EquivalentType, Attribute); 6207 } 6208 6209 static void Profile(llvm::FoldingSetNodeID &ID, Kind attrKind, 6210 QualType modified, QualType equivalent, 6211 const Attr *attr) { 6212 ID.AddInteger(attrKind); 6213 ID.AddPointer(modified.getAsOpaquePtr()); 6214 ID.AddPointer(equivalent.getAsOpaquePtr()); 6215 ID.AddPointer(attr); 6216 } 6217 6218 static bool classof(const Type *T) { 6219 return T->getTypeClass() == Attributed; 6220 } 6221 }; 6222 6223 class BTFTagAttributedType : public Type, public llvm::FoldingSetNode { 6224 private: 6225 friend class ASTContext; // ASTContext creates these 6226 6227 QualType WrappedType; 6228 const BTFTypeTagAttr *BTFAttr; 6229 6230 BTFTagAttributedType(QualType Canon, QualType Wrapped, 6231 const BTFTypeTagAttr *BTFAttr) 6232 : Type(BTFTagAttributed, Canon, Wrapped->getDependence()), 6233 WrappedType(Wrapped), BTFAttr(BTFAttr) {} 6234 6235 public: 6236 QualType getWrappedType() const { return WrappedType; } 6237 const BTFTypeTagAttr *getAttr() const { return BTFAttr; } 6238 6239 bool isSugared() const { return true; } 6240 QualType desugar() const { return getWrappedType(); } 6241 6242 void Profile(llvm::FoldingSetNodeID &ID) { 6243 Profile(ID, WrappedType, BTFAttr); 6244 } 6245 6246 static void Profile(llvm::FoldingSetNodeID &ID, QualType Wrapped, 6247 const BTFTypeTagAttr *BTFAttr) { 6248 ID.AddPointer(Wrapped.getAsOpaquePtr()); 6249 ID.AddPointer(BTFAttr); 6250 } 6251 6252 static bool classof(const Type *T) { 6253 return T->getTypeClass() == BTFTagAttributed; 6254 } 6255 }; 6256 6257 class HLSLAttributedResourceType : public Type, public llvm::FoldingSetNode { 6258 public: 6259 struct Attributes { 6260 // Data gathered from HLSL resource attributes 6261 llvm::dxil::ResourceClass ResourceClass; 6262 6263 LLVM_PREFERRED_TYPE(bool) 6264 uint8_t IsROV : 1; 6265 6266 LLVM_PREFERRED_TYPE(bool) 6267 uint8_t RawBuffer : 1; 6268 6269 Attributes(llvm::dxil::ResourceClass ResourceClass, bool IsROV, 6270 bool RawBuffer) 6271 : ResourceClass(ResourceClass), IsROV(IsROV), RawBuffer(RawBuffer) {} 6272 6273 Attributes() : Attributes(llvm::dxil::ResourceClass::UAV, false, false) {} 6274 6275 friend bool operator==(const Attributes &LHS, const Attributes &RHS) { 6276 return std::tie(LHS.ResourceClass, LHS.IsROV, LHS.RawBuffer) == 6277 std::tie(RHS.ResourceClass, RHS.IsROV, RHS.RawBuffer); 6278 } 6279 friend bool operator!=(const Attributes &LHS, const Attributes &RHS) { 6280 return !(LHS == RHS); 6281 } 6282 }; 6283 6284 private: 6285 friend class ASTContext; // ASTContext creates these 6286 6287 QualType WrappedType; 6288 QualType ContainedType; 6289 const Attributes Attrs; 6290 6291 HLSLAttributedResourceType(QualType Wrapped, QualType Contained, 6292 const Attributes &Attrs) 6293 : Type(HLSLAttributedResource, QualType(), 6294 Contained.isNull() ? TypeDependence::None 6295 : Contained->getDependence()), 6296 WrappedType(Wrapped), ContainedType(Contained), Attrs(Attrs) {} 6297 6298 public: 6299 QualType getWrappedType() const { return WrappedType; } 6300 QualType getContainedType() const { return ContainedType; } 6301 bool hasContainedType() const { return !ContainedType.isNull(); } 6302 const Attributes &getAttrs() const { return Attrs; } 6303 6304 bool isSugared() const { return false; } 6305 QualType desugar() const { return QualType(this, 0); } 6306 6307 void Profile(llvm::FoldingSetNodeID &ID) { 6308 Profile(ID, WrappedType, ContainedType, Attrs); 6309 } 6310 6311 static void Profile(llvm::FoldingSetNodeID &ID, QualType Wrapped, 6312 QualType Contained, const Attributes &Attrs) { 6313 ID.AddPointer(Wrapped.getAsOpaquePtr()); 6314 ID.AddPointer(Contained.getAsOpaquePtr()); 6315 ID.AddInteger(static_cast<uint32_t>(Attrs.ResourceClass)); 6316 ID.AddBoolean(Attrs.IsROV); 6317 ID.AddBoolean(Attrs.RawBuffer); 6318 } 6319 6320 static bool classof(const Type *T) { 6321 return T->getTypeClass() == HLSLAttributedResource; 6322 } 6323 6324 // Returns handle type from HLSL resource, if the type is a resource 6325 static const HLSLAttributedResourceType * 6326 findHandleTypeOnResource(const Type *RT); 6327 }; 6328 6329 class TemplateTypeParmType : public Type, public llvm::FoldingSetNode { 6330 friend class ASTContext; // ASTContext creates these 6331 6332 // The associated TemplateTypeParmDecl for the non-canonical type. 6333 TemplateTypeParmDecl *TTPDecl; 6334 6335 TemplateTypeParmType(unsigned D, unsigned I, bool PP, 6336 TemplateTypeParmDecl *TTPDecl, QualType Canon) 6337 : Type(TemplateTypeParm, Canon, 6338 TypeDependence::DependentInstantiation | 6339 (PP ? TypeDependence::UnexpandedPack : TypeDependence::None)), 6340 TTPDecl(TTPDecl) { 6341 assert(!TTPDecl == Canon.isNull()); 6342 TemplateTypeParmTypeBits.Depth = D; 6343 TemplateTypeParmTypeBits.Index = I; 6344 TemplateTypeParmTypeBits.ParameterPack = PP; 6345 } 6346 6347 public: 6348 unsigned getDepth() const { return TemplateTypeParmTypeBits.Depth; } 6349 unsigned getIndex() const { return TemplateTypeParmTypeBits.Index; } 6350 bool isParameterPack() const { 6351 return TemplateTypeParmTypeBits.ParameterPack; 6352 } 6353 6354 TemplateTypeParmDecl *getDecl() const { return TTPDecl; } 6355 6356 IdentifierInfo *getIdentifier() const; 6357 6358 bool isSugared() const { return false; } 6359 QualType desugar() const { return QualType(this, 0); } 6360 6361 void Profile(llvm::FoldingSetNodeID &ID) { 6362 Profile(ID, getDepth(), getIndex(), isParameterPack(), getDecl()); 6363 } 6364 6365 static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth, 6366 unsigned Index, bool ParameterPack, 6367 TemplateTypeParmDecl *TTPDecl) { 6368 ID.AddInteger(Depth); 6369 ID.AddInteger(Index); 6370 ID.AddBoolean(ParameterPack); 6371 ID.AddPointer(TTPDecl); 6372 } 6373 6374 static bool classof(const Type *T) { 6375 return T->getTypeClass() == TemplateTypeParm; 6376 } 6377 }; 6378 6379 /// Represents the result of substituting a type for a template 6380 /// type parameter. 6381 /// 6382 /// Within an instantiated template, all template type parameters have 6383 /// been replaced with these. They are used solely to record that a 6384 /// type was originally written as a template type parameter; 6385 /// therefore they are never canonical. 6386 class SubstTemplateTypeParmType final 6387 : public Type, 6388 public llvm::FoldingSetNode, 6389 private llvm::TrailingObjects<SubstTemplateTypeParmType, QualType> { 6390 friend class ASTContext; 6391 friend class llvm::TrailingObjects<SubstTemplateTypeParmType, QualType>; 6392 6393 Decl *AssociatedDecl; 6394 6395 SubstTemplateTypeParmType(QualType Replacement, Decl *AssociatedDecl, 6396 unsigned Index, std::optional<unsigned> PackIndex, 6397 SubstTemplateTypeParmTypeFlag Flag); 6398 6399 public: 6400 /// Gets the type that was substituted for the template 6401 /// parameter. 6402 QualType getReplacementType() const { 6403 return SubstTemplateTypeParmTypeBits.HasNonCanonicalUnderlyingType 6404 ? *getTrailingObjects<QualType>() 6405 : getCanonicalTypeInternal(); 6406 } 6407 6408 /// A template-like entity which owns the whole pattern being substituted. 6409 /// This will usually own a set of template parameters, or in some 6410 /// cases might even be a template parameter itself. 6411 Decl *getAssociatedDecl() const { return AssociatedDecl; } 6412 6413 /// Gets the template parameter declaration that was substituted for. 6414 const TemplateTypeParmDecl *getReplacedParameter() const; 6415 6416 /// Returns the index of the replaced parameter in the associated declaration. 6417 /// This should match the result of `getReplacedParameter()->getIndex()`. 6418 unsigned getIndex() const { return SubstTemplateTypeParmTypeBits.Index; } 6419 6420 std::optional<unsigned> getPackIndex() const { 6421 if (SubstTemplateTypeParmTypeBits.PackIndex == 0) 6422 return std::nullopt; 6423 return SubstTemplateTypeParmTypeBits.PackIndex - 1; 6424 } 6425 6426 SubstTemplateTypeParmTypeFlag getSubstitutionFlag() const { 6427 return static_cast<SubstTemplateTypeParmTypeFlag>( 6428 SubstTemplateTypeParmTypeBits.SubstitutionFlag); 6429 } 6430 6431 bool isSugared() const { return true; } 6432 QualType desugar() const { return getReplacementType(); } 6433 6434 void Profile(llvm::FoldingSetNodeID &ID) { 6435 Profile(ID, getReplacementType(), getAssociatedDecl(), getIndex(), 6436 getPackIndex(), getSubstitutionFlag()); 6437 } 6438 6439 static void Profile(llvm::FoldingSetNodeID &ID, QualType Replacement, 6440 const Decl *AssociatedDecl, unsigned Index, 6441 std::optional<unsigned> PackIndex, 6442 SubstTemplateTypeParmTypeFlag Flag) { 6443 Replacement.Profile(ID); 6444 ID.AddPointer(AssociatedDecl); 6445 ID.AddInteger(Index); 6446 ID.AddInteger(PackIndex ? *PackIndex - 1 : 0); 6447 ID.AddInteger(llvm::to_underlying(Flag)); 6448 assert((Flag != SubstTemplateTypeParmTypeFlag::ExpandPacksInPlace || 6449 PackIndex) && 6450 "ExpandPacksInPlace needs a valid PackIndex"); 6451 } 6452 6453 static bool classof(const Type *T) { 6454 return T->getTypeClass() == SubstTemplateTypeParm; 6455 } 6456 }; 6457 6458 /// Represents the result of substituting a set of types for a template 6459 /// type parameter pack. 6460 /// 6461 /// When a pack expansion in the source code contains multiple parameter packs 6462 /// and those parameter packs correspond to different levels of template 6463 /// parameter lists, this type node is used to represent a template type 6464 /// parameter pack from an outer level, which has already had its argument pack 6465 /// substituted but that still lives within a pack expansion that itself 6466 /// could not be instantiated. When actually performing a substitution into 6467 /// that pack expansion (e.g., when all template parameters have corresponding 6468 /// arguments), this type will be replaced with the \c SubstTemplateTypeParmType 6469 /// at the current pack substitution index. 6470 class SubstTemplateTypeParmPackType : public Type, public llvm::FoldingSetNode { 6471 friend class ASTContext; 6472 6473 /// A pointer to the set of template arguments that this 6474 /// parameter pack is instantiated with. 6475 const TemplateArgument *Arguments; 6476 6477 llvm::PointerIntPair<Decl *, 1, bool> AssociatedDeclAndFinal; 6478 6479 SubstTemplateTypeParmPackType(QualType Canon, Decl *AssociatedDecl, 6480 unsigned Index, bool Final, 6481 const TemplateArgument &ArgPack); 6482 6483 public: 6484 IdentifierInfo *getIdentifier() const; 6485 6486 /// A template-like entity which owns the whole pattern being substituted. 6487 /// This will usually own a set of template parameters, or in some 6488 /// cases might even be a template parameter itself. 6489 Decl *getAssociatedDecl() const; 6490 6491 /// Gets the template parameter declaration that was substituted for. 6492 const TemplateTypeParmDecl *getReplacedParameter() const; 6493 6494 /// Returns the index of the replaced parameter in the associated declaration. 6495 /// This should match the result of `getReplacedParameter()->getIndex()`. 6496 unsigned getIndex() const { return SubstTemplateTypeParmPackTypeBits.Index; } 6497 6498 // When true the substitution will be 'Final' (subst node won't be placed). 6499 bool getFinal() const; 6500 6501 unsigned getNumArgs() const { 6502 return SubstTemplateTypeParmPackTypeBits.NumArgs; 6503 } 6504 6505 bool isSugared() const { return false; } 6506 QualType desugar() const { return QualType(this, 0); } 6507 6508 TemplateArgument getArgumentPack() const; 6509 6510 void Profile(llvm::FoldingSetNodeID &ID); 6511 static void Profile(llvm::FoldingSetNodeID &ID, const Decl *AssociatedDecl, 6512 unsigned Index, bool Final, 6513 const TemplateArgument &ArgPack); 6514 6515 static bool classof(const Type *T) { 6516 return T->getTypeClass() == SubstTemplateTypeParmPack; 6517 } 6518 }; 6519 6520 /// Common base class for placeholders for types that get replaced by 6521 /// placeholder type deduction: C++11 auto, C++14 decltype(auto), C++17 deduced 6522 /// class template types, and constrained type names. 6523 /// 6524 /// These types are usually a placeholder for a deduced type. However, before 6525 /// the initializer is attached, or (usually) if the initializer is 6526 /// type-dependent, there is no deduced type and the type is canonical. In 6527 /// the latter case, it is also a dependent type. 6528 class DeducedType : public Type { 6529 QualType DeducedAsType; 6530 6531 protected: 6532 DeducedType(TypeClass TC, QualType DeducedAsType, 6533 TypeDependence ExtraDependence, QualType Canon) 6534 : Type(TC, Canon, 6535 ExtraDependence | (DeducedAsType.isNull() 6536 ? TypeDependence::None 6537 : DeducedAsType->getDependence() & 6538 ~TypeDependence::VariablyModified)), 6539 DeducedAsType(DeducedAsType) {} 6540 6541 public: 6542 bool isSugared() const { return !DeducedAsType.isNull(); } 6543 QualType desugar() const { 6544 return isSugared() ? DeducedAsType : QualType(this, 0); 6545 } 6546 6547 /// Get the type deduced for this placeholder type, or null if it 6548 /// has not been deduced. 6549 QualType getDeducedType() const { return DeducedAsType; } 6550 bool isDeduced() const { 6551 return !DeducedAsType.isNull() || isDependentType(); 6552 } 6553 6554 static bool classof(const Type *T) { 6555 return T->getTypeClass() == Auto || 6556 T->getTypeClass() == DeducedTemplateSpecialization; 6557 } 6558 }; 6559 6560 /// Represents a C++11 auto or C++14 decltype(auto) type, possibly constrained 6561 /// by a type-constraint. 6562 class AutoType : public DeducedType { 6563 friend class ASTContext; // ASTContext creates these 6564 6565 ConceptDecl *TypeConstraintConcept; 6566 6567 AutoType(QualType DeducedAsType, AutoTypeKeyword Keyword, 6568 TypeDependence ExtraDependence, QualType Canon, ConceptDecl *CD, 6569 ArrayRef<TemplateArgument> TypeConstraintArgs); 6570 6571 public: 6572 ArrayRef<TemplateArgument> getTypeConstraintArguments() const { 6573 return {reinterpret_cast<const TemplateArgument *>(this + 1), 6574 AutoTypeBits.NumArgs}; 6575 } 6576 6577 ConceptDecl *getTypeConstraintConcept() const { 6578 return TypeConstraintConcept; 6579 } 6580 6581 bool isConstrained() const { 6582 return TypeConstraintConcept != nullptr; 6583 } 6584 6585 bool isDecltypeAuto() const { 6586 return getKeyword() == AutoTypeKeyword::DecltypeAuto; 6587 } 6588 6589 bool isGNUAutoType() const { 6590 return getKeyword() == AutoTypeKeyword::GNUAutoType; 6591 } 6592 6593 AutoTypeKeyword getKeyword() const { 6594 return (AutoTypeKeyword)AutoTypeBits.Keyword; 6595 } 6596 6597 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context); 6598 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 6599 QualType Deduced, AutoTypeKeyword Keyword, 6600 bool IsDependent, ConceptDecl *CD, 6601 ArrayRef<TemplateArgument> Arguments); 6602 6603 static bool classof(const Type *T) { 6604 return T->getTypeClass() == Auto; 6605 } 6606 }; 6607 6608 /// Represents a C++17 deduced template specialization type. 6609 class DeducedTemplateSpecializationType : public DeducedType, 6610 public llvm::FoldingSetNode { 6611 friend class ASTContext; // ASTContext creates these 6612 6613 /// The name of the template whose arguments will be deduced. 6614 TemplateName Template; 6615 6616 DeducedTemplateSpecializationType(TemplateName Template, 6617 QualType DeducedAsType, 6618 bool IsDeducedAsDependent, QualType Canon) 6619 : DeducedType(DeducedTemplateSpecialization, DeducedAsType, 6620 toTypeDependence(Template.getDependence()) | 6621 (IsDeducedAsDependent 6622 ? TypeDependence::DependentInstantiation 6623 : TypeDependence::None), 6624 Canon), 6625 Template(Template) {} 6626 6627 public: 6628 /// Retrieve the name of the template that we are deducing. 6629 TemplateName getTemplateName() const { return Template;} 6630 6631 void Profile(llvm::FoldingSetNodeID &ID) const { 6632 Profile(ID, getTemplateName(), getDeducedType(), isDependentType()); 6633 } 6634 6635 static void Profile(llvm::FoldingSetNodeID &ID, TemplateName Template, 6636 QualType Deduced, bool IsDependent) { 6637 Template.Profile(ID); 6638 Deduced.Profile(ID); 6639 ID.AddBoolean(IsDependent || Template.isDependent()); 6640 } 6641 6642 static bool classof(const Type *T) { 6643 return T->getTypeClass() == DeducedTemplateSpecialization; 6644 } 6645 }; 6646 6647 /// Represents a type template specialization; the template 6648 /// must be a class template, a type alias template, or a template 6649 /// template parameter. A template which cannot be resolved to one of 6650 /// these, e.g. because it is written with a dependent scope 6651 /// specifier, is instead represented as a 6652 /// @c DependentTemplateSpecializationType. 6653 /// 6654 /// A non-dependent template specialization type is always "sugar", 6655 /// typically for a \c RecordType. For example, a class template 6656 /// specialization type of \c vector<int> will refer to a tag type for 6657 /// the instantiation \c std::vector<int, std::allocator<int>> 6658 /// 6659 /// Template specializations are dependent if either the template or 6660 /// any of the template arguments are dependent, in which case the 6661 /// type may also be canonical. 6662 /// 6663 /// Instances of this type are allocated with a trailing array of 6664 /// TemplateArguments, followed by a QualType representing the 6665 /// non-canonical aliased type when the template is a type alias 6666 /// template. 6667 class TemplateSpecializationType : public Type, public llvm::FoldingSetNode { 6668 friend class ASTContext; // ASTContext creates these 6669 6670 /// The name of the template being specialized. This is 6671 /// either a TemplateName::Template (in which case it is a 6672 /// ClassTemplateDecl*, a TemplateTemplateParmDecl*, or a 6673 /// TypeAliasTemplateDecl*), a 6674 /// TemplateName::SubstTemplateTemplateParmPack, or a 6675 /// TemplateName::SubstTemplateTemplateParm (in which case the 6676 /// replacement must, recursively, be one of these). 6677 TemplateName Template; 6678 6679 TemplateSpecializationType(TemplateName T, 6680 ArrayRef<TemplateArgument> Args, 6681 QualType Canon, 6682 QualType Aliased); 6683 6684 public: 6685 /// Determine whether any of the given template arguments are dependent. 6686 /// 6687 /// The converted arguments should be supplied when known; whether an 6688 /// argument is dependent can depend on the conversions performed on it 6689 /// (for example, a 'const int' passed as a template argument might be 6690 /// dependent if the parameter is a reference but non-dependent if the 6691 /// parameter is an int). 6692 /// 6693 /// Note that the \p Args parameter is unused: this is intentional, to remind 6694 /// the caller that they need to pass in the converted arguments, not the 6695 /// specified arguments. 6696 static bool 6697 anyDependentTemplateArguments(ArrayRef<TemplateArgumentLoc> Args, 6698 ArrayRef<TemplateArgument> Converted); 6699 static bool 6700 anyDependentTemplateArguments(const TemplateArgumentListInfo &, 6701 ArrayRef<TemplateArgument> Converted); 6702 static bool anyInstantiationDependentTemplateArguments( 6703 ArrayRef<TemplateArgumentLoc> Args); 6704 6705 /// True if this template specialization type matches a current 6706 /// instantiation in the context in which it is found. 6707 bool isCurrentInstantiation() const { 6708 return isa<InjectedClassNameType>(getCanonicalTypeInternal()); 6709 } 6710 6711 /// Determine if this template specialization type is for a type alias 6712 /// template that has been substituted. 6713 /// 6714 /// Nearly every template specialization type whose template is an alias 6715 /// template will be substituted. However, this is not the case when 6716 /// the specialization contains a pack expansion but the template alias 6717 /// does not have a corresponding parameter pack, e.g., 6718 /// 6719 /// \code 6720 /// template<typename T, typename U, typename V> struct S; 6721 /// template<typename T, typename U> using A = S<T, int, U>; 6722 /// template<typename... Ts> struct X { 6723 /// typedef A<Ts...> type; // not a type alias 6724 /// }; 6725 /// \endcode 6726 bool isTypeAlias() const { return TemplateSpecializationTypeBits.TypeAlias; } 6727 6728 /// Get the aliased type, if this is a specialization of a type alias 6729 /// template. 6730 QualType getAliasedType() const; 6731 6732 /// Retrieve the name of the template that we are specializing. 6733 TemplateName getTemplateName() const { return Template; } 6734 6735 ArrayRef<TemplateArgument> template_arguments() const { 6736 return {reinterpret_cast<const TemplateArgument *>(this + 1), 6737 TemplateSpecializationTypeBits.NumArgs}; 6738 } 6739 6740 bool isSugared() const { 6741 return !isDependentType() || isCurrentInstantiation() || isTypeAlias(); 6742 } 6743 6744 QualType desugar() const { 6745 return isTypeAlias() ? getAliasedType() : getCanonicalTypeInternal(); 6746 } 6747 6748 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx); 6749 static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T, 6750 ArrayRef<TemplateArgument> Args, 6751 const ASTContext &Context); 6752 6753 static bool classof(const Type *T) { 6754 return T->getTypeClass() == TemplateSpecialization; 6755 } 6756 }; 6757 6758 /// Print a template argument list, including the '<' and '>' 6759 /// enclosing the template arguments. 6760 void printTemplateArgumentList(raw_ostream &OS, 6761 ArrayRef<TemplateArgument> Args, 6762 const PrintingPolicy &Policy, 6763 const TemplateParameterList *TPL = nullptr); 6764 6765 void printTemplateArgumentList(raw_ostream &OS, 6766 ArrayRef<TemplateArgumentLoc> Args, 6767 const PrintingPolicy &Policy, 6768 const TemplateParameterList *TPL = nullptr); 6769 6770 void printTemplateArgumentList(raw_ostream &OS, 6771 const TemplateArgumentListInfo &Args, 6772 const PrintingPolicy &Policy, 6773 const TemplateParameterList *TPL = nullptr); 6774 6775 /// Make a best-effort determination of whether the type T can be produced by 6776 /// substituting Args into the default argument of Param. 6777 bool isSubstitutedDefaultArgument(ASTContext &Ctx, TemplateArgument Arg, 6778 const NamedDecl *Param, 6779 ArrayRef<TemplateArgument> Args, 6780 unsigned Depth); 6781 6782 /// The injected class name of a C++ class template or class 6783 /// template partial specialization. Used to record that a type was 6784 /// spelled with a bare identifier rather than as a template-id; the 6785 /// equivalent for non-templated classes is just RecordType. 6786 /// 6787 /// Injected class name types are always dependent. Template 6788 /// instantiation turns these into RecordTypes. 6789 /// 6790 /// Injected class name types are always canonical. This works 6791 /// because it is impossible to compare an injected class name type 6792 /// with the corresponding non-injected template type, for the same 6793 /// reason that it is impossible to directly compare template 6794 /// parameters from different dependent contexts: injected class name 6795 /// types can only occur within the scope of a particular templated 6796 /// declaration, and within that scope every template specialization 6797 /// will canonicalize to the injected class name (when appropriate 6798 /// according to the rules of the language). 6799 class InjectedClassNameType : public Type { 6800 friend class ASTContext; // ASTContext creates these. 6801 friend class ASTNodeImporter; 6802 friend class ASTReader; // FIXME: ASTContext::getInjectedClassNameType is not 6803 // currently suitable for AST reading, too much 6804 // interdependencies. 6805 template <class T> friend class serialization::AbstractTypeReader; 6806 6807 CXXRecordDecl *Decl; 6808 6809 /// The template specialization which this type represents. 6810 /// For example, in 6811 /// template <class T> class A { ... }; 6812 /// this is A<T>, whereas in 6813 /// template <class X, class Y> class A<B<X,Y> > { ... }; 6814 /// this is A<B<X,Y> >. 6815 /// 6816 /// It is always unqualified, always a template specialization type, 6817 /// and always dependent. 6818 QualType InjectedType; 6819 6820 InjectedClassNameType(CXXRecordDecl *D, QualType TST) 6821 : Type(InjectedClassName, QualType(), 6822 TypeDependence::DependentInstantiation), 6823 Decl(D), InjectedType(TST) { 6824 assert(isa<TemplateSpecializationType>(TST)); 6825 assert(!TST.hasQualifiers()); 6826 assert(TST->isDependentType()); 6827 } 6828 6829 public: 6830 QualType getInjectedSpecializationType() const { return InjectedType; } 6831 6832 const TemplateSpecializationType *getInjectedTST() const { 6833 return cast<TemplateSpecializationType>(InjectedType.getTypePtr()); 6834 } 6835 6836 TemplateName getTemplateName() const { 6837 return getInjectedTST()->getTemplateName(); 6838 } 6839 6840 CXXRecordDecl *getDecl() const; 6841 6842 bool isSugared() const { return false; } 6843 QualType desugar() const { return QualType(this, 0); } 6844 6845 static bool classof(const Type *T) { 6846 return T->getTypeClass() == InjectedClassName; 6847 } 6848 }; 6849 6850 /// The elaboration keyword that precedes a qualified type name or 6851 /// introduces an elaborated-type-specifier. 6852 enum class ElaboratedTypeKeyword { 6853 /// The "struct" keyword introduces the elaborated-type-specifier. 6854 Struct, 6855 6856 /// The "__interface" keyword introduces the elaborated-type-specifier. 6857 Interface, 6858 6859 /// The "union" keyword introduces the elaborated-type-specifier. 6860 Union, 6861 6862 /// The "class" keyword introduces the elaborated-type-specifier. 6863 Class, 6864 6865 /// The "enum" keyword introduces the elaborated-type-specifier. 6866 Enum, 6867 6868 /// The "typename" keyword precedes the qualified type name, e.g., 6869 /// \c typename T::type. 6870 Typename, 6871 6872 /// No keyword precedes the qualified type name. 6873 None 6874 }; 6875 6876 /// The kind of a tag type. 6877 enum class TagTypeKind { 6878 /// The "struct" keyword. 6879 Struct, 6880 6881 /// The "__interface" keyword. 6882 Interface, 6883 6884 /// The "union" keyword. 6885 Union, 6886 6887 /// The "class" keyword. 6888 Class, 6889 6890 /// The "enum" keyword. 6891 Enum 6892 }; 6893 6894 /// A helper class for Type nodes having an ElaboratedTypeKeyword. 6895 /// The keyword in stored in the free bits of the base class. 6896 /// Also provides a few static helpers for converting and printing 6897 /// elaborated type keyword and tag type kind enumerations. 6898 class TypeWithKeyword : public Type { 6899 protected: 6900 TypeWithKeyword(ElaboratedTypeKeyword Keyword, TypeClass tc, 6901 QualType Canonical, TypeDependence Dependence) 6902 : Type(tc, Canonical, Dependence) { 6903 TypeWithKeywordBits.Keyword = llvm::to_underlying(Keyword); 6904 } 6905 6906 public: 6907 ElaboratedTypeKeyword getKeyword() const { 6908 return static_cast<ElaboratedTypeKeyword>(TypeWithKeywordBits.Keyword); 6909 } 6910 6911 /// Converts a type specifier (DeclSpec::TST) into an elaborated type keyword. 6912 static ElaboratedTypeKeyword getKeywordForTypeSpec(unsigned TypeSpec); 6913 6914 /// Converts a type specifier (DeclSpec::TST) into a tag type kind. 6915 /// It is an error to provide a type specifier which *isn't* a tag kind here. 6916 static TagTypeKind getTagTypeKindForTypeSpec(unsigned TypeSpec); 6917 6918 /// Converts a TagTypeKind into an elaborated type keyword. 6919 static ElaboratedTypeKeyword getKeywordForTagTypeKind(TagTypeKind Tag); 6920 6921 /// Converts an elaborated type keyword into a TagTypeKind. 6922 /// It is an error to provide an elaborated type keyword 6923 /// which *isn't* a tag kind here. 6924 static TagTypeKind getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword); 6925 6926 static bool KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword); 6927 6928 static StringRef getKeywordName(ElaboratedTypeKeyword Keyword); 6929 6930 static StringRef getTagTypeKindName(TagTypeKind Kind) { 6931 return getKeywordName(getKeywordForTagTypeKind(Kind)); 6932 } 6933 6934 class CannotCastToThisType {}; 6935 static CannotCastToThisType classof(const Type *); 6936 }; 6937 6938 /// Represents a type that was referred to using an elaborated type 6939 /// keyword, e.g., struct S, or via a qualified name, e.g., N::M::type, 6940 /// or both. 6941 /// 6942 /// This type is used to keep track of a type name as written in the 6943 /// source code, including tag keywords and any nested-name-specifiers. 6944 /// The type itself is always "sugar", used to express what was written 6945 /// in the source code but containing no additional semantic information. 6946 class ElaboratedType final 6947 : public TypeWithKeyword, 6948 public llvm::FoldingSetNode, 6949 private llvm::TrailingObjects<ElaboratedType, TagDecl *> { 6950 friend class ASTContext; // ASTContext creates these 6951 friend TrailingObjects; 6952 6953 /// The nested name specifier containing the qualifier. 6954 NestedNameSpecifier *NNS; 6955 6956 /// The type that this qualified name refers to. 6957 QualType NamedType; 6958 6959 /// The (re)declaration of this tag type owned by this occurrence is stored 6960 /// as a trailing object if there is one. Use getOwnedTagDecl to obtain 6961 /// it, or obtain a null pointer if there is none. 6962 6963 ElaboratedType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, 6964 QualType NamedType, QualType CanonType, TagDecl *OwnedTagDecl) 6965 : TypeWithKeyword(Keyword, Elaborated, CanonType, 6966 // Any semantic dependence on the qualifier will have 6967 // been incorporated into NamedType. We still need to 6968 // track syntactic (instantiation / error / pack) 6969 // dependence on the qualifier. 6970 NamedType->getDependence() | 6971 (NNS ? toSyntacticDependence( 6972 toTypeDependence(NNS->getDependence())) 6973 : TypeDependence::None)), 6974 NNS(NNS), NamedType(NamedType) { 6975 ElaboratedTypeBits.HasOwnedTagDecl = false; 6976 if (OwnedTagDecl) { 6977 ElaboratedTypeBits.HasOwnedTagDecl = true; 6978 *getTrailingObjects<TagDecl *>() = OwnedTagDecl; 6979 } 6980 } 6981 6982 public: 6983 /// Retrieve the qualification on this type. 6984 NestedNameSpecifier *getQualifier() const { return NNS; } 6985 6986 /// Retrieve the type named by the qualified-id. 6987 QualType getNamedType() const { return NamedType; } 6988 6989 /// Remove a single level of sugar. 6990 QualType desugar() const { return getNamedType(); } 6991 6992 /// Returns whether this type directly provides sugar. 6993 bool isSugared() const { return true; } 6994 6995 /// Return the (re)declaration of this type owned by this occurrence of this 6996 /// type, or nullptr if there is none. 6997 TagDecl *getOwnedTagDecl() const { 6998 return ElaboratedTypeBits.HasOwnedTagDecl ? *getTrailingObjects<TagDecl *>() 6999 : nullptr; 7000 } 7001 7002 void Profile(llvm::FoldingSetNodeID &ID) { 7003 Profile(ID, getKeyword(), NNS, NamedType, getOwnedTagDecl()); 7004 } 7005 7006 static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, 7007 NestedNameSpecifier *NNS, QualType NamedType, 7008 TagDecl *OwnedTagDecl) { 7009 ID.AddInteger(llvm::to_underlying(Keyword)); 7010 ID.AddPointer(NNS); 7011 NamedType.Profile(ID); 7012 ID.AddPointer(OwnedTagDecl); 7013 } 7014 7015 static bool classof(const Type *T) { return T->getTypeClass() == Elaborated; } 7016 }; 7017 7018 /// Represents a qualified type name for which the type name is 7019 /// dependent. 7020 /// 7021 /// DependentNameType represents a class of dependent types that involve a 7022 /// possibly dependent nested-name-specifier (e.g., "T::") followed by a 7023 /// name of a type. The DependentNameType may start with a "typename" (for a 7024 /// typename-specifier), "class", "struct", "union", or "enum" (for a 7025 /// dependent elaborated-type-specifier), or nothing (in contexts where we 7026 /// know that we must be referring to a type, e.g., in a base class specifier). 7027 /// Typically the nested-name-specifier is dependent, but in MSVC compatibility 7028 /// mode, this type is used with non-dependent names to delay name lookup until 7029 /// instantiation. 7030 class DependentNameType : public TypeWithKeyword, public llvm::FoldingSetNode { 7031 friend class ASTContext; // ASTContext creates these 7032 7033 /// The nested name specifier containing the qualifier. 7034 NestedNameSpecifier *NNS; 7035 7036 /// The type that this typename specifier refers to. 7037 const IdentifierInfo *Name; 7038 7039 DependentNameType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, 7040 const IdentifierInfo *Name, QualType CanonType) 7041 : TypeWithKeyword(Keyword, DependentName, CanonType, 7042 TypeDependence::DependentInstantiation | 7043 toTypeDependence(NNS->getDependence())), 7044 NNS(NNS), Name(Name) { 7045 assert(NNS); 7046 assert(Name); 7047 } 7048 7049 public: 7050 /// Retrieve the qualification on this type. 7051 NestedNameSpecifier *getQualifier() const { return NNS; } 7052 7053 /// Retrieve the identifier that terminates this type name. 7054 /// For example, "type" in "typename T::type". 7055 const IdentifierInfo *getIdentifier() const { 7056 return Name; 7057 } 7058 7059 bool isSugared() const { return false; } 7060 QualType desugar() const { return QualType(this, 0); } 7061 7062 void Profile(llvm::FoldingSetNodeID &ID) { 7063 Profile(ID, getKeyword(), NNS, Name); 7064 } 7065 7066 static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, 7067 NestedNameSpecifier *NNS, const IdentifierInfo *Name) { 7068 ID.AddInteger(llvm::to_underlying(Keyword)); 7069 ID.AddPointer(NNS); 7070 ID.AddPointer(Name); 7071 } 7072 7073 static bool classof(const Type *T) { 7074 return T->getTypeClass() == DependentName; 7075 } 7076 }; 7077 7078 /// Represents a template specialization type whose template cannot be 7079 /// resolved, e.g. 7080 /// A<T>::template B<T> 7081 class DependentTemplateSpecializationType : public TypeWithKeyword, 7082 public llvm::FoldingSetNode { 7083 friend class ASTContext; // ASTContext creates these 7084 7085 /// The nested name specifier containing the qualifier. 7086 NestedNameSpecifier *NNS; 7087 7088 /// The identifier of the template. 7089 const IdentifierInfo *Name; 7090 7091 DependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, 7092 NestedNameSpecifier *NNS, 7093 const IdentifierInfo *Name, 7094 ArrayRef<TemplateArgument> Args, 7095 QualType Canon); 7096 7097 public: 7098 NestedNameSpecifier *getQualifier() const { return NNS; } 7099 const IdentifierInfo *getIdentifier() const { return Name; } 7100 7101 ArrayRef<TemplateArgument> template_arguments() const { 7102 return {reinterpret_cast<const TemplateArgument *>(this + 1), 7103 DependentTemplateSpecializationTypeBits.NumArgs}; 7104 } 7105 7106 bool isSugared() const { return false; } 7107 QualType desugar() const { return QualType(this, 0); } 7108 7109 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { 7110 Profile(ID, Context, getKeyword(), NNS, Name, template_arguments()); 7111 } 7112 7113 static void Profile(llvm::FoldingSetNodeID &ID, 7114 const ASTContext &Context, 7115 ElaboratedTypeKeyword Keyword, 7116 NestedNameSpecifier *Qualifier, 7117 const IdentifierInfo *Name, 7118 ArrayRef<TemplateArgument> Args); 7119 7120 static bool classof(const Type *T) { 7121 return T->getTypeClass() == DependentTemplateSpecialization; 7122 } 7123 }; 7124 7125 /// Represents a pack expansion of types. 7126 /// 7127 /// Pack expansions are part of C++11 variadic templates. A pack 7128 /// expansion contains a pattern, which itself contains one or more 7129 /// "unexpanded" parameter packs. When instantiated, a pack expansion 7130 /// produces a series of types, each instantiated from the pattern of 7131 /// the expansion, where the Ith instantiation of the pattern uses the 7132 /// Ith arguments bound to each of the unexpanded parameter packs. The 7133 /// pack expansion is considered to "expand" these unexpanded 7134 /// parameter packs. 7135 /// 7136 /// \code 7137 /// template<typename ...Types> struct tuple; 7138 /// 7139 /// template<typename ...Types> 7140 /// struct tuple_of_references { 7141 /// typedef tuple<Types&...> type; 7142 /// }; 7143 /// \endcode 7144 /// 7145 /// Here, the pack expansion \c Types&... is represented via a 7146 /// PackExpansionType whose pattern is Types&. 7147 class PackExpansionType : public Type, public llvm::FoldingSetNode { 7148 friend class ASTContext; // ASTContext creates these 7149 7150 /// The pattern of the pack expansion. 7151 QualType Pattern; 7152 7153 PackExpansionType(QualType Pattern, QualType Canon, 7154 std::optional<unsigned> NumExpansions) 7155 : Type(PackExpansion, Canon, 7156 (Pattern->getDependence() | TypeDependence::Dependent | 7157 TypeDependence::Instantiation) & 7158 ~TypeDependence::UnexpandedPack), 7159 Pattern(Pattern) { 7160 PackExpansionTypeBits.NumExpansions = 7161 NumExpansions ? *NumExpansions + 1 : 0; 7162 } 7163 7164 public: 7165 /// Retrieve the pattern of this pack expansion, which is the 7166 /// type that will be repeatedly instantiated when instantiating the 7167 /// pack expansion itself. 7168 QualType getPattern() const { return Pattern; } 7169 7170 /// Retrieve the number of expansions that this pack expansion will 7171 /// generate, if known. 7172 std::optional<unsigned> getNumExpansions() const { 7173 if (PackExpansionTypeBits.NumExpansions) 7174 return PackExpansionTypeBits.NumExpansions - 1; 7175 return std::nullopt; 7176 } 7177 7178 bool isSugared() const { return false; } 7179 QualType desugar() const { return QualType(this, 0); } 7180 7181 void Profile(llvm::FoldingSetNodeID &ID) { 7182 Profile(ID, getPattern(), getNumExpansions()); 7183 } 7184 7185 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pattern, 7186 std::optional<unsigned> NumExpansions) { 7187 ID.AddPointer(Pattern.getAsOpaquePtr()); 7188 ID.AddBoolean(NumExpansions.has_value()); 7189 if (NumExpansions) 7190 ID.AddInteger(*NumExpansions); 7191 } 7192 7193 static bool classof(const Type *T) { 7194 return T->getTypeClass() == PackExpansion; 7195 } 7196 }; 7197 7198 /// This class wraps the list of protocol qualifiers. For types that can 7199 /// take ObjC protocol qualifers, they can subclass this class. 7200 template <class T> 7201 class ObjCProtocolQualifiers { 7202 protected: 7203 ObjCProtocolQualifiers() = default; 7204 7205 ObjCProtocolDecl * const *getProtocolStorage() const { 7206 return const_cast<ObjCProtocolQualifiers*>(this)->getProtocolStorage(); 7207 } 7208 7209 ObjCProtocolDecl **getProtocolStorage() { 7210 return static_cast<T*>(this)->getProtocolStorageImpl(); 7211 } 7212 7213 void setNumProtocols(unsigned N) { 7214 static_cast<T*>(this)->setNumProtocolsImpl(N); 7215 } 7216 7217 void initialize(ArrayRef<ObjCProtocolDecl *> protocols) { 7218 setNumProtocols(protocols.size()); 7219 assert(getNumProtocols() == protocols.size() && 7220 "bitfield overflow in protocol count"); 7221 if (!protocols.empty()) 7222 memcpy(getProtocolStorage(), protocols.data(), 7223 protocols.size() * sizeof(ObjCProtocolDecl*)); 7224 } 7225 7226 public: 7227 using qual_iterator = ObjCProtocolDecl * const *; 7228 using qual_range = llvm::iterator_range<qual_iterator>; 7229 7230 qual_range quals() const { return qual_range(qual_begin(), qual_end()); } 7231 qual_iterator qual_begin() const { return getProtocolStorage(); } 7232 qual_iterator qual_end() const { return qual_begin() + getNumProtocols(); } 7233 7234 bool qual_empty() const { return getNumProtocols() == 0; } 7235 7236 /// Return the number of qualifying protocols in this type, or 0 if 7237 /// there are none. 7238 unsigned getNumProtocols() const { 7239 return static_cast<const T*>(this)->getNumProtocolsImpl(); 7240 } 7241 7242 /// Fetch a protocol by index. 7243 ObjCProtocolDecl *getProtocol(unsigned I) const { 7244 assert(I < getNumProtocols() && "Out-of-range protocol access"); 7245 return qual_begin()[I]; 7246 } 7247 7248 /// Retrieve all of the protocol qualifiers. 7249 ArrayRef<ObjCProtocolDecl *> getProtocols() const { 7250 return ArrayRef<ObjCProtocolDecl *>(qual_begin(), getNumProtocols()); 7251 } 7252 }; 7253 7254 /// Represents a type parameter type in Objective C. It can take 7255 /// a list of protocols. 7256 class ObjCTypeParamType : public Type, 7257 public ObjCProtocolQualifiers<ObjCTypeParamType>, 7258 public llvm::FoldingSetNode { 7259 friend class ASTContext; 7260 friend class ObjCProtocolQualifiers<ObjCTypeParamType>; 7261 7262 /// The number of protocols stored on this type. 7263 unsigned NumProtocols : 6; 7264 7265 ObjCTypeParamDecl *OTPDecl; 7266 7267 /// The protocols are stored after the ObjCTypeParamType node. In the 7268 /// canonical type, the list of protocols are sorted alphabetically 7269 /// and uniqued. 7270 ObjCProtocolDecl **getProtocolStorageImpl(); 7271 7272 /// Return the number of qualifying protocols in this interface type, 7273 /// or 0 if there are none. 7274 unsigned getNumProtocolsImpl() const { 7275 return NumProtocols; 7276 } 7277 7278 void setNumProtocolsImpl(unsigned N) { 7279 NumProtocols = N; 7280 } 7281 7282 ObjCTypeParamType(const ObjCTypeParamDecl *D, 7283 QualType can, 7284 ArrayRef<ObjCProtocolDecl *> protocols); 7285 7286 public: 7287 bool isSugared() const { return true; } 7288 QualType desugar() const { return getCanonicalTypeInternal(); } 7289 7290 static bool classof(const Type *T) { 7291 return T->getTypeClass() == ObjCTypeParam; 7292 } 7293 7294 void Profile(llvm::FoldingSetNodeID &ID); 7295 static void Profile(llvm::FoldingSetNodeID &ID, 7296 const ObjCTypeParamDecl *OTPDecl, 7297 QualType CanonicalType, 7298 ArrayRef<ObjCProtocolDecl *> protocols); 7299 7300 ObjCTypeParamDecl *getDecl() const { return OTPDecl; } 7301 }; 7302 7303 /// Represents a class type in Objective C. 7304 /// 7305 /// Every Objective C type is a combination of a base type, a set of 7306 /// type arguments (optional, for parameterized classes) and a list of 7307 /// protocols. 7308 /// 7309 /// Given the following declarations: 7310 /// \code 7311 /// \@class C<T>; 7312 /// \@protocol P; 7313 /// \endcode 7314 /// 7315 /// 'C' is an ObjCInterfaceType C. It is sugar for an ObjCObjectType 7316 /// with base C and no protocols. 7317 /// 7318 /// 'C<P>' is an unspecialized ObjCObjectType with base C and protocol list [P]. 7319 /// 'C<C*>' is a specialized ObjCObjectType with type arguments 'C*' and no 7320 /// protocol list. 7321 /// 'C<C*><P>' is a specialized ObjCObjectType with base C, type arguments 'C*', 7322 /// and protocol list [P]. 7323 /// 7324 /// 'id' is a TypedefType which is sugar for an ObjCObjectPointerType whose 7325 /// pointee is an ObjCObjectType with base BuiltinType::ObjCIdType 7326 /// and no protocols. 7327 /// 7328 /// 'id<P>' is an ObjCObjectPointerType whose pointee is an ObjCObjectType 7329 /// with base BuiltinType::ObjCIdType and protocol list [P]. Eventually 7330 /// this should get its own sugar class to better represent the source. 7331 class ObjCObjectType : public Type, 7332 public ObjCProtocolQualifiers<ObjCObjectType> { 7333 friend class ObjCProtocolQualifiers<ObjCObjectType>; 7334 7335 // ObjCObjectType.NumTypeArgs - the number of type arguments stored 7336 // after the ObjCObjectPointerType node. 7337 // ObjCObjectType.NumProtocols - the number of protocols stored 7338 // after the type arguments of ObjCObjectPointerType node. 7339 // 7340 // These protocols are those written directly on the type. If 7341 // protocol qualifiers ever become additive, the iterators will need 7342 // to get kindof complicated. 7343 // 7344 // In the canonical object type, these are sorted alphabetically 7345 // and uniqued. 7346 7347 /// Either a BuiltinType or an InterfaceType or sugar for either. 7348 QualType BaseType; 7349 7350 /// Cached superclass type. 7351 mutable llvm::PointerIntPair<const ObjCObjectType *, 1, bool> 7352 CachedSuperClassType; 7353 7354 QualType *getTypeArgStorage(); 7355 const QualType *getTypeArgStorage() const { 7356 return const_cast<ObjCObjectType *>(this)->getTypeArgStorage(); 7357 } 7358 7359 ObjCProtocolDecl **getProtocolStorageImpl(); 7360 /// Return the number of qualifying protocols in this interface type, 7361 /// or 0 if there are none. 7362 unsigned getNumProtocolsImpl() const { 7363 return ObjCObjectTypeBits.NumProtocols; 7364 } 7365 void setNumProtocolsImpl(unsigned N) { 7366 ObjCObjectTypeBits.NumProtocols = N; 7367 } 7368 7369 protected: 7370 enum Nonce_ObjCInterface { Nonce_ObjCInterface }; 7371 7372 ObjCObjectType(QualType Canonical, QualType Base, 7373 ArrayRef<QualType> typeArgs, 7374 ArrayRef<ObjCProtocolDecl *> protocols, 7375 bool isKindOf); 7376 7377 ObjCObjectType(enum Nonce_ObjCInterface) 7378 : Type(ObjCInterface, QualType(), TypeDependence::None), 7379 BaseType(QualType(this_(), 0)) { 7380 ObjCObjectTypeBits.NumProtocols = 0; 7381 ObjCObjectTypeBits.NumTypeArgs = 0; 7382 ObjCObjectTypeBits.IsKindOf = 0; 7383 } 7384 7385 void computeSuperClassTypeSlow() const; 7386 7387 public: 7388 /// Gets the base type of this object type. This is always (possibly 7389 /// sugar for) one of: 7390 /// - the 'id' builtin type (as opposed to the 'id' type visible to the 7391 /// user, which is a typedef for an ObjCObjectPointerType) 7392 /// - the 'Class' builtin type (same caveat) 7393 /// - an ObjCObjectType (currently always an ObjCInterfaceType) 7394 QualType getBaseType() const { return BaseType; } 7395 7396 bool isObjCId() const { 7397 return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCId); 7398 } 7399 7400 bool isObjCClass() const { 7401 return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCClass); 7402 } 7403 7404 bool isObjCUnqualifiedId() const { return qual_empty() && isObjCId(); } 7405 bool isObjCUnqualifiedClass() const { return qual_empty() && isObjCClass(); } 7406 bool isObjCUnqualifiedIdOrClass() const { 7407 if (!qual_empty()) return false; 7408 if (const BuiltinType *T = getBaseType()->getAs<BuiltinType>()) 7409 return T->getKind() == BuiltinType::ObjCId || 7410 T->getKind() == BuiltinType::ObjCClass; 7411 return false; 7412 } 7413 bool isObjCQualifiedId() const { return !qual_empty() && isObjCId(); } 7414 bool isObjCQualifiedClass() const { return !qual_empty() && isObjCClass(); } 7415 7416 /// Gets the interface declaration for this object type, if the base type 7417 /// really is an interface. 7418 ObjCInterfaceDecl *getInterface() const; 7419 7420 /// Determine whether this object type is "specialized", meaning 7421 /// that it has type arguments. 7422 bool isSpecialized() const; 7423 7424 /// Determine whether this object type was written with type arguments. 7425 bool isSpecializedAsWritten() const { 7426 return ObjCObjectTypeBits.NumTypeArgs > 0; 7427 } 7428 7429 /// Determine whether this object type is "unspecialized", meaning 7430 /// that it has no type arguments. 7431 bool isUnspecialized() const { return !isSpecialized(); } 7432 7433 /// Determine whether this object type is "unspecialized" as 7434 /// written, meaning that it has no type arguments. 7435 bool isUnspecializedAsWritten() const { return !isSpecializedAsWritten(); } 7436 7437 /// Retrieve the type arguments of this object type (semantically). 7438 ArrayRef<QualType> getTypeArgs() const; 7439 7440 /// Retrieve the type arguments of this object type as they were 7441 /// written. 7442 ArrayRef<QualType> getTypeArgsAsWritten() const { 7443 return llvm::ArrayRef(getTypeArgStorage(), ObjCObjectTypeBits.NumTypeArgs); 7444 } 7445 7446 /// Whether this is a "__kindof" type as written. 7447 bool isKindOfTypeAsWritten() const { return ObjCObjectTypeBits.IsKindOf; } 7448 7449 /// Whether this ia a "__kindof" type (semantically). 7450 bool isKindOfType() const; 7451 7452 /// Retrieve the type of the superclass of this object type. 7453 /// 7454 /// This operation substitutes any type arguments into the 7455 /// superclass of the current class type, potentially producing a 7456 /// specialization of the superclass type. Produces a null type if 7457 /// there is no superclass. 7458 QualType getSuperClassType() const { 7459 if (!CachedSuperClassType.getInt()) 7460 computeSuperClassTypeSlow(); 7461 7462 assert(CachedSuperClassType.getInt() && "Superclass not set?"); 7463 return QualType(CachedSuperClassType.getPointer(), 0); 7464 } 7465 7466 /// Strip off the Objective-C "kindof" type and (with it) any 7467 /// protocol qualifiers. 7468 QualType stripObjCKindOfTypeAndQuals(const ASTContext &ctx) const; 7469 7470 bool isSugared() const { return false; } 7471 QualType desugar() const { return QualType(this, 0); } 7472 7473 static bool classof(const Type *T) { 7474 return T->getTypeClass() == ObjCObject || 7475 T->getTypeClass() == ObjCInterface; 7476 } 7477 }; 7478 7479 /// A class providing a concrete implementation 7480 /// of ObjCObjectType, so as to not increase the footprint of 7481 /// ObjCInterfaceType. Code outside of ASTContext and the core type 7482 /// system should not reference this type. 7483 class ObjCObjectTypeImpl : public ObjCObjectType, public llvm::FoldingSetNode { 7484 friend class ASTContext; 7485 7486 // If anyone adds fields here, ObjCObjectType::getProtocolStorage() 7487 // will need to be modified. 7488 7489 ObjCObjectTypeImpl(QualType Canonical, QualType Base, 7490 ArrayRef<QualType> typeArgs, 7491 ArrayRef<ObjCProtocolDecl *> protocols, 7492 bool isKindOf) 7493 : ObjCObjectType(Canonical, Base, typeArgs, protocols, isKindOf) {} 7494 7495 public: 7496 void Profile(llvm::FoldingSetNodeID &ID); 7497 static void Profile(llvm::FoldingSetNodeID &ID, 7498 QualType Base, 7499 ArrayRef<QualType> typeArgs, 7500 ArrayRef<ObjCProtocolDecl *> protocols, 7501 bool isKindOf); 7502 }; 7503 7504 inline QualType *ObjCObjectType::getTypeArgStorage() { 7505 return reinterpret_cast<QualType *>(static_cast<ObjCObjectTypeImpl*>(this)+1); 7506 } 7507 7508 inline ObjCProtocolDecl **ObjCObjectType::getProtocolStorageImpl() { 7509 return reinterpret_cast<ObjCProtocolDecl**>( 7510 getTypeArgStorage() + ObjCObjectTypeBits.NumTypeArgs); 7511 } 7512 7513 inline ObjCProtocolDecl **ObjCTypeParamType::getProtocolStorageImpl() { 7514 return reinterpret_cast<ObjCProtocolDecl**>( 7515 static_cast<ObjCTypeParamType*>(this)+1); 7516 } 7517 7518 /// Interfaces are the core concept in Objective-C for object oriented design. 7519 /// They basically correspond to C++ classes. There are two kinds of interface 7520 /// types: normal interfaces like `NSString`, and qualified interfaces, which 7521 /// are qualified with a protocol list like `NSString<NSCopyable, NSAmazing>`. 7522 /// 7523 /// ObjCInterfaceType guarantees the following properties when considered 7524 /// as a subtype of its superclass, ObjCObjectType: 7525 /// - There are no protocol qualifiers. To reinforce this, code which 7526 /// tries to invoke the protocol methods via an ObjCInterfaceType will 7527 /// fail to compile. 7528 /// - It is its own base type. That is, if T is an ObjCInterfaceType*, 7529 /// T->getBaseType() == QualType(T, 0). 7530 class ObjCInterfaceType : public ObjCObjectType { 7531 friend class ASTContext; // ASTContext creates these. 7532 friend class ASTReader; 7533 template <class T> friend class serialization::AbstractTypeReader; 7534 7535 ObjCInterfaceDecl *Decl; 7536 7537 ObjCInterfaceType(const ObjCInterfaceDecl *D) 7538 : ObjCObjectType(Nonce_ObjCInterface), 7539 Decl(const_cast<ObjCInterfaceDecl*>(D)) {} 7540 7541 public: 7542 /// Get the declaration of this interface. 7543 ObjCInterfaceDecl *getDecl() const; 7544 7545 bool isSugared() const { return false; } 7546 QualType desugar() const { return QualType(this, 0); } 7547 7548 static bool classof(const Type *T) { 7549 return T->getTypeClass() == ObjCInterface; 7550 } 7551 7552 // Nonsense to "hide" certain members of ObjCObjectType within this 7553 // class. People asking for protocols on an ObjCInterfaceType are 7554 // not going to get what they want: ObjCInterfaceTypes are 7555 // guaranteed to have no protocols. 7556 enum { 7557 qual_iterator, 7558 qual_begin, 7559 qual_end, 7560 getNumProtocols, 7561 getProtocol 7562 }; 7563 }; 7564 7565 inline ObjCInterfaceDecl *ObjCObjectType::getInterface() const { 7566 QualType baseType = getBaseType(); 7567 while (const auto *ObjT = baseType->getAs<ObjCObjectType>()) { 7568 if (const auto *T = dyn_cast<ObjCInterfaceType>(ObjT)) 7569 return T->getDecl(); 7570 7571 baseType = ObjT->getBaseType(); 7572 } 7573 7574 return nullptr; 7575 } 7576 7577 /// Represents a pointer to an Objective C object. 7578 /// 7579 /// These are constructed from pointer declarators when the pointee type is 7580 /// an ObjCObjectType (or sugar for one). In addition, the 'id' and 'Class' 7581 /// types are typedefs for these, and the protocol-qualified types 'id<P>' 7582 /// and 'Class<P>' are translated into these. 7583 /// 7584 /// Pointers to pointers to Objective C objects are still PointerTypes; 7585 /// only the first level of pointer gets it own type implementation. 7586 class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode { 7587 friend class ASTContext; // ASTContext creates these. 7588 7589 QualType PointeeType; 7590 7591 ObjCObjectPointerType(QualType Canonical, QualType Pointee) 7592 : Type(ObjCObjectPointer, Canonical, Pointee->getDependence()), 7593 PointeeType(Pointee) {} 7594 7595 public: 7596 /// Gets the type pointed to by this ObjC pointer. 7597 /// The result will always be an ObjCObjectType or sugar thereof. 7598 QualType getPointeeType() const { return PointeeType; } 7599 7600 /// Gets the type pointed to by this ObjC pointer. Always returns non-null. 7601 /// 7602 /// This method is equivalent to getPointeeType() except that 7603 /// it discards any typedefs (or other sugar) between this 7604 /// type and the "outermost" object type. So for: 7605 /// \code 7606 /// \@class A; \@protocol P; \@protocol Q; 7607 /// typedef A<P> AP; 7608 /// typedef A A1; 7609 /// typedef A1<P> A1P; 7610 /// typedef A1P<Q> A1PQ; 7611 /// \endcode 7612 /// For 'A*', getObjectType() will return 'A'. 7613 /// For 'A<P>*', getObjectType() will return 'A<P>'. 7614 /// For 'AP*', getObjectType() will return 'A<P>'. 7615 /// For 'A1*', getObjectType() will return 'A'. 7616 /// For 'A1<P>*', getObjectType() will return 'A1<P>'. 7617 /// For 'A1P*', getObjectType() will return 'A1<P>'. 7618 /// For 'A1PQ*', getObjectType() will return 'A1<Q>', because 7619 /// adding protocols to a protocol-qualified base discards the 7620 /// old qualifiers (for now). But if it didn't, getObjectType() 7621 /// would return 'A1P<Q>' (and we'd have to make iterating over 7622 /// qualifiers more complicated). 7623 const ObjCObjectType *getObjectType() const { 7624 return PointeeType->castAs<ObjCObjectType>(); 7625 } 7626 7627 /// If this pointer points to an Objective C 7628 /// \@interface type, gets the type for that interface. Any protocol 7629 /// qualifiers on the interface are ignored. 7630 /// 7631 /// \return null if the base type for this pointer is 'id' or 'Class' 7632 const ObjCInterfaceType *getInterfaceType() const; 7633 7634 /// If this pointer points to an Objective \@interface 7635 /// type, gets the declaration for that interface. 7636 /// 7637 /// \return null if the base type for this pointer is 'id' or 'Class' 7638 ObjCInterfaceDecl *getInterfaceDecl() const { 7639 return getObjectType()->getInterface(); 7640 } 7641 7642 /// True if this is equivalent to the 'id' type, i.e. if 7643 /// its object type is the primitive 'id' type with no protocols. 7644 bool isObjCIdType() const { 7645 return getObjectType()->isObjCUnqualifiedId(); 7646 } 7647 7648 /// True if this is equivalent to the 'Class' type, 7649 /// i.e. if its object tive is the primitive 'Class' type with no protocols. 7650 bool isObjCClassType() const { 7651 return getObjectType()->isObjCUnqualifiedClass(); 7652 } 7653 7654 /// True if this is equivalent to the 'id' or 'Class' type, 7655 bool isObjCIdOrClassType() const { 7656 return getObjectType()->isObjCUnqualifiedIdOrClass(); 7657 } 7658 7659 /// True if this is equivalent to 'id<P>' for some non-empty set of 7660 /// protocols. 7661 bool isObjCQualifiedIdType() const { 7662 return getObjectType()->isObjCQualifiedId(); 7663 } 7664 7665 /// True if this is equivalent to 'Class<P>' for some non-empty set of 7666 /// protocols. 7667 bool isObjCQualifiedClassType() const { 7668 return getObjectType()->isObjCQualifiedClass(); 7669 } 7670 7671 /// Whether this is a "__kindof" type. 7672 bool isKindOfType() const { return getObjectType()->isKindOfType(); } 7673 7674 /// Whether this type is specialized, meaning that it has type arguments. 7675 bool isSpecialized() const { return getObjectType()->isSpecialized(); } 7676 7677 /// Whether this type is specialized, meaning that it has type arguments. 7678 bool isSpecializedAsWritten() const { 7679 return getObjectType()->isSpecializedAsWritten(); 7680 } 7681 7682 /// Whether this type is unspecialized, meaning that is has no type arguments. 7683 bool isUnspecialized() const { return getObjectType()->isUnspecialized(); } 7684 7685 /// Determine whether this object type is "unspecialized" as 7686 /// written, meaning that it has no type arguments. 7687 bool isUnspecializedAsWritten() const { return !isSpecializedAsWritten(); } 7688 7689 /// Retrieve the type arguments for this type. 7690 ArrayRef<QualType> getTypeArgs() const { 7691 return getObjectType()->getTypeArgs(); 7692 } 7693 7694 /// Retrieve the type arguments for this type. 7695 ArrayRef<QualType> getTypeArgsAsWritten() const { 7696 return getObjectType()->getTypeArgsAsWritten(); 7697 } 7698 7699 /// An iterator over the qualifiers on the object type. Provided 7700 /// for convenience. This will always iterate over the full set of 7701 /// protocols on a type, not just those provided directly. 7702 using qual_iterator = ObjCObjectType::qual_iterator; 7703 using qual_range = llvm::iterator_range<qual_iterator>; 7704 7705 qual_range quals() const { return qual_range(qual_begin(), qual_end()); } 7706 7707 qual_iterator qual_begin() const { 7708 return getObjectType()->qual_begin(); 7709 } 7710 7711 qual_iterator qual_end() const { 7712 return getObjectType()->qual_end(); 7713 } 7714 7715 bool qual_empty() const { return getObjectType()->qual_empty(); } 7716 7717 /// Return the number of qualifying protocols on the object type. 7718 unsigned getNumProtocols() const { 7719 return getObjectType()->getNumProtocols(); 7720 } 7721 7722 /// Retrieve a qualifying protocol by index on the object type. 7723 ObjCProtocolDecl *getProtocol(unsigned I) const { 7724 return getObjectType()->getProtocol(I); 7725 } 7726 7727 bool isSugared() const { return false; } 7728 QualType desugar() const { return QualType(this, 0); } 7729 7730 /// Retrieve the type of the superclass of this object pointer type. 7731 /// 7732 /// This operation substitutes any type arguments into the 7733 /// superclass of the current class type, potentially producing a 7734 /// pointer to a specialization of the superclass type. Produces a 7735 /// null type if there is no superclass. 7736 QualType getSuperClassType() const; 7737 7738 /// Strip off the Objective-C "kindof" type and (with it) any 7739 /// protocol qualifiers. 7740 const ObjCObjectPointerType *stripObjCKindOfTypeAndQuals( 7741 const ASTContext &ctx) const; 7742 7743 void Profile(llvm::FoldingSetNodeID &ID) { 7744 Profile(ID, getPointeeType()); 7745 } 7746 7747 static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { 7748 ID.AddPointer(T.getAsOpaquePtr()); 7749 } 7750 7751 static bool classof(const Type *T) { 7752 return T->getTypeClass() == ObjCObjectPointer; 7753 } 7754 }; 7755 7756 class AtomicType : public Type, public llvm::FoldingSetNode { 7757 friend class ASTContext; // ASTContext creates these. 7758 7759 QualType ValueType; 7760 7761 AtomicType(QualType ValTy, QualType Canonical) 7762 : Type(Atomic, Canonical, ValTy->getDependence()), ValueType(ValTy) {} 7763 7764 public: 7765 /// Gets the type contained by this atomic type, i.e. 7766 /// the type returned by performing an atomic load of this atomic type. 7767 QualType getValueType() const { return ValueType; } 7768 7769 bool isSugared() const { return false; } 7770 QualType desugar() const { return QualType(this, 0); } 7771 7772 void Profile(llvm::FoldingSetNodeID &ID) { 7773 Profile(ID, getValueType()); 7774 } 7775 7776 static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { 7777 ID.AddPointer(T.getAsOpaquePtr()); 7778 } 7779 7780 static bool classof(const Type *T) { 7781 return T->getTypeClass() == Atomic; 7782 } 7783 }; 7784 7785 /// PipeType - OpenCL20. 7786 class PipeType : public Type, public llvm::FoldingSetNode { 7787 friend class ASTContext; // ASTContext creates these. 7788 7789 QualType ElementType; 7790 bool isRead; 7791 7792 PipeType(QualType elemType, QualType CanonicalPtr, bool isRead) 7793 : Type(Pipe, CanonicalPtr, elemType->getDependence()), 7794 ElementType(elemType), isRead(isRead) {} 7795 7796 public: 7797 QualType getElementType() const { return ElementType; } 7798 7799 bool isSugared() const { return false; } 7800 7801 QualType desugar() const { return QualType(this, 0); } 7802 7803 void Profile(llvm::FoldingSetNodeID &ID) { 7804 Profile(ID, getElementType(), isReadOnly()); 7805 } 7806 7807 static void Profile(llvm::FoldingSetNodeID &ID, QualType T, bool isRead) { 7808 ID.AddPointer(T.getAsOpaquePtr()); 7809 ID.AddBoolean(isRead); 7810 } 7811 7812 static bool classof(const Type *T) { 7813 return T->getTypeClass() == Pipe; 7814 } 7815 7816 bool isReadOnly() const { return isRead; } 7817 }; 7818 7819 /// A fixed int type of a specified bitwidth. 7820 class BitIntType final : public Type, public llvm::FoldingSetNode { 7821 friend class ASTContext; 7822 LLVM_PREFERRED_TYPE(bool) 7823 unsigned IsUnsigned : 1; 7824 unsigned NumBits : 24; 7825 7826 protected: 7827 BitIntType(bool isUnsigned, unsigned NumBits); 7828 7829 public: 7830 bool isUnsigned() const { return IsUnsigned; } 7831 bool isSigned() const { return !IsUnsigned; } 7832 unsigned getNumBits() const { return NumBits; } 7833 7834 bool isSugared() const { return false; } 7835 QualType desugar() const { return QualType(this, 0); } 7836 7837 void Profile(llvm::FoldingSetNodeID &ID) const { 7838 Profile(ID, isUnsigned(), getNumBits()); 7839 } 7840 7841 static void Profile(llvm::FoldingSetNodeID &ID, bool IsUnsigned, 7842 unsigned NumBits) { 7843 ID.AddBoolean(IsUnsigned); 7844 ID.AddInteger(NumBits); 7845 } 7846 7847 static bool classof(const Type *T) { return T->getTypeClass() == BitInt; } 7848 }; 7849 7850 class DependentBitIntType final : public Type, public llvm::FoldingSetNode { 7851 friend class ASTContext; 7852 llvm::PointerIntPair<Expr*, 1, bool> ExprAndUnsigned; 7853 7854 protected: 7855 DependentBitIntType(bool IsUnsigned, Expr *NumBits); 7856 7857 public: 7858 bool isUnsigned() const; 7859 bool isSigned() const { return !isUnsigned(); } 7860 Expr *getNumBitsExpr() const; 7861 7862 bool isSugared() const { return false; } 7863 QualType desugar() const { return QualType(this, 0); } 7864 7865 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { 7866 Profile(ID, Context, isUnsigned(), getNumBitsExpr()); 7867 } 7868 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 7869 bool IsUnsigned, Expr *NumBitsExpr); 7870 7871 static bool classof(const Type *T) { 7872 return T->getTypeClass() == DependentBitInt; 7873 } 7874 }; 7875 7876 /// A qualifier set is used to build a set of qualifiers. 7877 class QualifierCollector : public Qualifiers { 7878 public: 7879 QualifierCollector(Qualifiers Qs = Qualifiers()) : Qualifiers(Qs) {} 7880 7881 /// Collect any qualifiers on the given type and return an 7882 /// unqualified type. The qualifiers are assumed to be consistent 7883 /// with those already in the type. 7884 const Type *strip(QualType type) { 7885 addFastQualifiers(type.getLocalFastQualifiers()); 7886 if (!type.hasLocalNonFastQualifiers()) 7887 return type.getTypePtrUnsafe(); 7888 7889 const ExtQuals *extQuals = type.getExtQualsUnsafe(); 7890 addConsistentQualifiers(extQuals->getQualifiers()); 7891 return extQuals->getBaseType(); 7892 } 7893 7894 /// Apply the collected qualifiers to the given type. 7895 QualType apply(const ASTContext &Context, QualType QT) const; 7896 7897 /// Apply the collected qualifiers to the given type. 7898 QualType apply(const ASTContext &Context, const Type* T) const; 7899 }; 7900 7901 /// A container of type source information. 7902 /// 7903 /// A client can read the relevant info using TypeLoc wrappers, e.g: 7904 /// @code 7905 /// TypeLoc TL = TypeSourceInfo->getTypeLoc(); 7906 /// TL.getBeginLoc().print(OS, SrcMgr); 7907 /// @endcode 7908 class alignas(8) TypeSourceInfo { 7909 // Contains a memory block after the class, used for type source information, 7910 // allocated by ASTContext. 7911 friend class ASTContext; 7912 7913 QualType Ty; 7914 7915 TypeSourceInfo(QualType ty, size_t DataSize); // implemented in TypeLoc.h 7916 7917 public: 7918 /// Return the type wrapped by this type source info. 7919 QualType getType() const { return Ty; } 7920 7921 /// Return the TypeLoc wrapper for the type source info. 7922 TypeLoc getTypeLoc() const; // implemented in TypeLoc.h 7923 7924 /// Override the type stored in this TypeSourceInfo. Use with caution! 7925 void overrideType(QualType T) { Ty = T; } 7926 }; 7927 7928 // Inline function definitions. 7929 7930 inline SplitQualType SplitQualType::getSingleStepDesugaredType() const { 7931 SplitQualType desugar = 7932 Ty->getLocallyUnqualifiedSingleStepDesugaredType().split(); 7933 desugar.Quals.addConsistentQualifiers(Quals); 7934 return desugar; 7935 } 7936 7937 inline const Type *QualType::getTypePtr() const { 7938 return getCommonPtr()->BaseType; 7939 } 7940 7941 inline const Type *QualType::getTypePtrOrNull() const { 7942 return (isNull() ? nullptr : getCommonPtr()->BaseType); 7943 } 7944 7945 inline bool QualType::isReferenceable() const { 7946 // C++ [defns.referenceable] 7947 // type that is either an object type, a function type that does not have 7948 // cv-qualifiers or a ref-qualifier, or a reference type. 7949 const Type &Self = **this; 7950 if (Self.isObjectType() || Self.isReferenceType()) 7951 return true; 7952 if (const auto *F = Self.getAs<FunctionProtoType>()) 7953 return F->getMethodQuals().empty() && F->getRefQualifier() == RQ_None; 7954 7955 return false; 7956 } 7957 7958 inline SplitQualType QualType::split() const { 7959 if (!hasLocalNonFastQualifiers()) 7960 return SplitQualType(getTypePtrUnsafe(), 7961 Qualifiers::fromFastMask(getLocalFastQualifiers())); 7962 7963 const ExtQuals *eq = getExtQualsUnsafe(); 7964 Qualifiers qs = eq->getQualifiers(); 7965 qs.addFastQualifiers(getLocalFastQualifiers()); 7966 return SplitQualType(eq->getBaseType(), qs); 7967 } 7968 7969 inline Qualifiers QualType::getLocalQualifiers() const { 7970 Qualifiers Quals; 7971 if (hasLocalNonFastQualifiers()) 7972 Quals = getExtQualsUnsafe()->getQualifiers(); 7973 Quals.addFastQualifiers(getLocalFastQualifiers()); 7974 return Quals; 7975 } 7976 7977 inline Qualifiers QualType::getQualifiers() const { 7978 Qualifiers quals = getCommonPtr()->CanonicalType.getLocalQualifiers(); 7979 quals.addFastQualifiers(getLocalFastQualifiers()); 7980 return quals; 7981 } 7982 7983 inline unsigned QualType::getCVRQualifiers() const { 7984 unsigned cvr = getCommonPtr()->CanonicalType.getLocalCVRQualifiers(); 7985 cvr |= getLocalCVRQualifiers(); 7986 return cvr; 7987 } 7988 7989 inline QualType QualType::getCanonicalType() const { 7990 QualType canon = getCommonPtr()->CanonicalType; 7991 return canon.withFastQualifiers(getLocalFastQualifiers()); 7992 } 7993 7994 inline bool QualType::isCanonical() const { 7995 return getTypePtr()->isCanonicalUnqualified(); 7996 } 7997 7998 inline bool QualType::isCanonicalAsParam() const { 7999 if (!isCanonical()) return false; 8000 if (hasLocalQualifiers()) return false; 8001 8002 const Type *T = getTypePtr(); 8003 if (T->isVariablyModifiedType() && T->hasSizedVLAType()) 8004 return false; 8005 8006 return !isa<FunctionType>(T) && 8007 (!isa<ArrayType>(T) || isa<ArrayParameterType>(T)); 8008 } 8009 8010 inline bool QualType::isConstQualified() const { 8011 return isLocalConstQualified() || 8012 getCommonPtr()->CanonicalType.isLocalConstQualified(); 8013 } 8014 8015 inline bool QualType::isRestrictQualified() const { 8016 return isLocalRestrictQualified() || 8017 getCommonPtr()->CanonicalType.isLocalRestrictQualified(); 8018 } 8019 8020 8021 inline bool QualType::isVolatileQualified() const { 8022 return isLocalVolatileQualified() || 8023 getCommonPtr()->CanonicalType.isLocalVolatileQualified(); 8024 } 8025 8026 inline bool QualType::hasQualifiers() const { 8027 return hasLocalQualifiers() || 8028 getCommonPtr()->CanonicalType.hasLocalQualifiers(); 8029 } 8030 8031 inline QualType QualType::getUnqualifiedType() const { 8032 if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) 8033 return QualType(getTypePtr(), 0); 8034 8035 return QualType(getSplitUnqualifiedTypeImpl(*this).Ty, 0); 8036 } 8037 8038 inline SplitQualType QualType::getSplitUnqualifiedType() const { 8039 if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) 8040 return split(); 8041 8042 return getSplitUnqualifiedTypeImpl(*this); 8043 } 8044 8045 inline void QualType::removeLocalConst() { 8046 removeLocalFastQualifiers(Qualifiers::Const); 8047 } 8048 8049 inline void QualType::removeLocalRestrict() { 8050 removeLocalFastQualifiers(Qualifiers::Restrict); 8051 } 8052 8053 inline void QualType::removeLocalVolatile() { 8054 removeLocalFastQualifiers(Qualifiers::Volatile); 8055 } 8056 8057 /// Check if this type has any address space qualifier. 8058 inline bool QualType::hasAddressSpace() const { 8059 return getQualifiers().hasAddressSpace(); 8060 } 8061 8062 /// Return the address space of this type. 8063 inline LangAS QualType::getAddressSpace() const { 8064 return getQualifiers().getAddressSpace(); 8065 } 8066 8067 /// Return the gc attribute of this type. 8068 inline Qualifiers::GC QualType::getObjCGCAttr() const { 8069 return getQualifiers().getObjCGCAttr(); 8070 } 8071 8072 inline bool QualType::hasNonTrivialToPrimitiveDefaultInitializeCUnion() const { 8073 if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) 8074 return hasNonTrivialToPrimitiveDefaultInitializeCUnion(RD); 8075 return false; 8076 } 8077 8078 inline bool QualType::hasNonTrivialToPrimitiveDestructCUnion() const { 8079 if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) 8080 return hasNonTrivialToPrimitiveDestructCUnion(RD); 8081 return false; 8082 } 8083 8084 inline bool QualType::hasNonTrivialToPrimitiveCopyCUnion() const { 8085 if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) 8086 return hasNonTrivialToPrimitiveCopyCUnion(RD); 8087 return false; 8088 } 8089 8090 inline FunctionType::ExtInfo getFunctionExtInfo(const Type &t) { 8091 if (const auto *PT = t.getAs<PointerType>()) { 8092 if (const auto *FT = PT->getPointeeType()->getAs<FunctionType>()) 8093 return FT->getExtInfo(); 8094 } else if (const auto *FT = t.getAs<FunctionType>()) 8095 return FT->getExtInfo(); 8096 8097 return FunctionType::ExtInfo(); 8098 } 8099 8100 inline FunctionType::ExtInfo getFunctionExtInfo(QualType t) { 8101 return getFunctionExtInfo(*t); 8102 } 8103 8104 /// Determine whether this type is more 8105 /// qualified than the Other type. For example, "const volatile int" 8106 /// is more qualified than "const int", "volatile int", and 8107 /// "int". However, it is not more qualified than "const volatile 8108 /// int". 8109 inline bool QualType::isMoreQualifiedThan(QualType other, 8110 const ASTContext &Ctx) const { 8111 Qualifiers MyQuals = getQualifiers(); 8112 Qualifiers OtherQuals = other.getQualifiers(); 8113 return (MyQuals != OtherQuals && MyQuals.compatiblyIncludes(OtherQuals, Ctx)); 8114 } 8115 8116 /// Determine whether this type is at last 8117 /// as qualified as the Other type. For example, "const volatile 8118 /// int" is at least as qualified as "const int", "volatile int", 8119 /// "int", and "const volatile int". 8120 inline bool QualType::isAtLeastAsQualifiedAs(QualType other, 8121 const ASTContext &Ctx) const { 8122 Qualifiers OtherQuals = other.getQualifiers(); 8123 8124 // Ignore __unaligned qualifier if this type is a void. 8125 if (getUnqualifiedType()->isVoidType()) 8126 OtherQuals.removeUnaligned(); 8127 8128 return getQualifiers().compatiblyIncludes(OtherQuals, Ctx); 8129 } 8130 8131 /// If Type is a reference type (e.g., const 8132 /// int&), returns the type that the reference refers to ("const 8133 /// int"). Otherwise, returns the type itself. This routine is used 8134 /// throughout Sema to implement C++ 5p6: 8135 /// 8136 /// If an expression initially has the type "reference to T" (8.3.2, 8137 /// 8.5.3), the type is adjusted to "T" prior to any further 8138 /// analysis, the expression designates the object or function 8139 /// denoted by the reference, and the expression is an lvalue. 8140 inline QualType QualType::getNonReferenceType() const { 8141 if (const auto *RefType = (*this)->getAs<ReferenceType>()) 8142 return RefType->getPointeeType(); 8143 else 8144 return *this; 8145 } 8146 8147 inline bool QualType::isCForbiddenLValueType() const { 8148 return ((getTypePtr()->isVoidType() && !hasQualifiers()) || 8149 getTypePtr()->isFunctionType()); 8150 } 8151 8152 /// Tests whether the type is categorized as a fundamental type. 8153 /// 8154 /// \returns True for types specified in C++0x [basic.fundamental]. 8155 inline bool Type::isFundamentalType() const { 8156 return isVoidType() || 8157 isNullPtrType() || 8158 // FIXME: It's really annoying that we don't have an 8159 // 'isArithmeticType()' which agrees with the standard definition. 8160 (isArithmeticType() && !isEnumeralType()); 8161 } 8162 8163 /// Tests whether the type is categorized as a compound type. 8164 /// 8165 /// \returns True for types specified in C++0x [basic.compound]. 8166 inline bool Type::isCompoundType() const { 8167 // C++0x [basic.compound]p1: 8168 // Compound types can be constructed in the following ways: 8169 // -- arrays of objects of a given type [...]; 8170 return isArrayType() || 8171 // -- functions, which have parameters of given types [...]; 8172 isFunctionType() || 8173 // -- pointers to void or objects or functions [...]; 8174 isPointerType() || 8175 // -- references to objects or functions of a given type. [...] 8176 isReferenceType() || 8177 // -- classes containing a sequence of objects of various types, [...]; 8178 isRecordType() || 8179 // -- unions, which are classes capable of containing objects of different 8180 // types at different times; 8181 isUnionType() || 8182 // -- enumerations, which comprise a set of named constant values. [...]; 8183 isEnumeralType() || 8184 // -- pointers to non-static class members, [...]. 8185 isMemberPointerType(); 8186 } 8187 8188 inline bool Type::isFunctionType() const { 8189 return isa<FunctionType>(CanonicalType); 8190 } 8191 8192 inline bool Type::isPointerType() const { 8193 return isa<PointerType>(CanonicalType); 8194 } 8195 8196 inline bool Type::isPointerOrReferenceType() const { 8197 return isPointerType() || isReferenceType(); 8198 } 8199 8200 inline bool Type::isAnyPointerType() const { 8201 return isPointerType() || isObjCObjectPointerType(); 8202 } 8203 8204 inline bool Type::isSignableType() const { return isPointerType(); } 8205 8206 inline bool Type::isBlockPointerType() const { 8207 return isa<BlockPointerType>(CanonicalType); 8208 } 8209 8210 inline bool Type::isReferenceType() const { 8211 return isa<ReferenceType>(CanonicalType); 8212 } 8213 8214 inline bool Type::isLValueReferenceType() const { 8215 return isa<LValueReferenceType>(CanonicalType); 8216 } 8217 8218 inline bool Type::isRValueReferenceType() const { 8219 return isa<RValueReferenceType>(CanonicalType); 8220 } 8221 8222 inline bool Type::isObjectPointerType() const { 8223 // Note: an "object pointer type" is not the same thing as a pointer to an 8224 // object type; rather, it is a pointer to an object type or a pointer to cv 8225 // void. 8226 if (const auto *T = getAs<PointerType>()) 8227 return !T->getPointeeType()->isFunctionType(); 8228 else 8229 return false; 8230 } 8231 8232 inline bool Type::isFunctionPointerType() const { 8233 if (const auto *T = getAs<PointerType>()) 8234 return T->getPointeeType()->isFunctionType(); 8235 else 8236 return false; 8237 } 8238 8239 inline bool Type::isFunctionReferenceType() const { 8240 if (const auto *T = getAs<ReferenceType>()) 8241 return T->getPointeeType()->isFunctionType(); 8242 else 8243 return false; 8244 } 8245 8246 inline bool Type::isMemberPointerType() const { 8247 return isa<MemberPointerType>(CanonicalType); 8248 } 8249 8250 inline bool Type::isMemberFunctionPointerType() const { 8251 if (const auto *T = getAs<MemberPointerType>()) 8252 return T->isMemberFunctionPointer(); 8253 else 8254 return false; 8255 } 8256 8257 inline bool Type::isMemberDataPointerType() const { 8258 if (const auto *T = getAs<MemberPointerType>()) 8259 return T->isMemberDataPointer(); 8260 else 8261 return false; 8262 } 8263 8264 inline bool Type::isArrayType() const { 8265 return isa<ArrayType>(CanonicalType); 8266 } 8267 8268 inline bool Type::isConstantArrayType() const { 8269 return isa<ConstantArrayType>(CanonicalType); 8270 } 8271 8272 inline bool Type::isIncompleteArrayType() const { 8273 return isa<IncompleteArrayType>(CanonicalType); 8274 } 8275 8276 inline bool Type::isVariableArrayType() const { 8277 return isa<VariableArrayType>(CanonicalType); 8278 } 8279 8280 inline bool Type::isArrayParameterType() const { 8281 return isa<ArrayParameterType>(CanonicalType); 8282 } 8283 8284 inline bool Type::isDependentSizedArrayType() const { 8285 return isa<DependentSizedArrayType>(CanonicalType); 8286 } 8287 8288 inline bool Type::isBuiltinType() const { 8289 return isa<BuiltinType>(CanonicalType); 8290 } 8291 8292 inline bool Type::isRecordType() const { 8293 return isa<RecordType>(CanonicalType); 8294 } 8295 8296 inline bool Type::isEnumeralType() const { 8297 return isa<EnumType>(CanonicalType); 8298 } 8299 8300 inline bool Type::isAnyComplexType() const { 8301 return isa<ComplexType>(CanonicalType); 8302 } 8303 8304 inline bool Type::isVectorType() const { 8305 return isa<VectorType>(CanonicalType); 8306 } 8307 8308 inline bool Type::isExtVectorType() const { 8309 return isa<ExtVectorType>(CanonicalType); 8310 } 8311 8312 inline bool Type::isExtVectorBoolType() const { 8313 if (!isExtVectorType()) 8314 return false; 8315 return cast<ExtVectorType>(CanonicalType)->getElementType()->isBooleanType(); 8316 } 8317 8318 inline bool Type::isSubscriptableVectorType() const { 8319 return isVectorType() || isSveVLSBuiltinType(); 8320 } 8321 8322 inline bool Type::isMatrixType() const { 8323 return isa<MatrixType>(CanonicalType); 8324 } 8325 8326 inline bool Type::isConstantMatrixType() const { 8327 return isa<ConstantMatrixType>(CanonicalType); 8328 } 8329 8330 inline bool Type::isDependentAddressSpaceType() const { 8331 return isa<DependentAddressSpaceType>(CanonicalType); 8332 } 8333 8334 inline bool Type::isObjCObjectPointerType() const { 8335 return isa<ObjCObjectPointerType>(CanonicalType); 8336 } 8337 8338 inline bool Type::isObjCObjectType() const { 8339 return isa<ObjCObjectType>(CanonicalType); 8340 } 8341 8342 inline bool Type::isObjCObjectOrInterfaceType() const { 8343 return isa<ObjCInterfaceType>(CanonicalType) || 8344 isa<ObjCObjectType>(CanonicalType); 8345 } 8346 8347 inline bool Type::isAtomicType() const { 8348 return isa<AtomicType>(CanonicalType); 8349 } 8350 8351 inline bool Type::isUndeducedAutoType() const { 8352 return isa<AutoType>(CanonicalType); 8353 } 8354 8355 inline bool Type::isObjCQualifiedIdType() const { 8356 if (const auto *OPT = getAs<ObjCObjectPointerType>()) 8357 return OPT->isObjCQualifiedIdType(); 8358 return false; 8359 } 8360 8361 inline bool Type::isObjCQualifiedClassType() const { 8362 if (const auto *OPT = getAs<ObjCObjectPointerType>()) 8363 return OPT->isObjCQualifiedClassType(); 8364 return false; 8365 } 8366 8367 inline bool Type::isObjCIdType() const { 8368 if (const auto *OPT = getAs<ObjCObjectPointerType>()) 8369 return OPT->isObjCIdType(); 8370 return false; 8371 } 8372 8373 inline bool Type::isObjCClassType() const { 8374 if (const auto *OPT = getAs<ObjCObjectPointerType>()) 8375 return OPT->isObjCClassType(); 8376 return false; 8377 } 8378 8379 inline bool Type::isObjCSelType() const { 8380 if (const auto *OPT = getAs<PointerType>()) 8381 return OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCSel); 8382 return false; 8383 } 8384 8385 inline bool Type::isObjCBuiltinType() const { 8386 return isObjCIdType() || isObjCClassType() || isObjCSelType(); 8387 } 8388 8389 inline bool Type::isDecltypeType() const { 8390 return isa<DecltypeType>(this); 8391 } 8392 8393 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 8394 inline bool Type::is##Id##Type() const { \ 8395 return isSpecificBuiltinType(BuiltinType::Id); \ 8396 } 8397 #include "clang/Basic/OpenCLImageTypes.def" 8398 8399 inline bool Type::isSamplerT() const { 8400 return isSpecificBuiltinType(BuiltinType::OCLSampler); 8401 } 8402 8403 inline bool Type::isEventT() const { 8404 return isSpecificBuiltinType(BuiltinType::OCLEvent); 8405 } 8406 8407 inline bool Type::isClkEventT() const { 8408 return isSpecificBuiltinType(BuiltinType::OCLClkEvent); 8409 } 8410 8411 inline bool Type::isQueueT() const { 8412 return isSpecificBuiltinType(BuiltinType::OCLQueue); 8413 } 8414 8415 inline bool Type::isReserveIDT() const { 8416 return isSpecificBuiltinType(BuiltinType::OCLReserveID); 8417 } 8418 8419 inline bool Type::isImageType() const { 8420 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) is##Id##Type() || 8421 return 8422 #include "clang/Basic/OpenCLImageTypes.def" 8423 false; // end boolean or operation 8424 } 8425 8426 inline bool Type::isPipeType() const { 8427 return isa<PipeType>(CanonicalType); 8428 } 8429 8430 inline bool Type::isBitIntType() const { 8431 return isa<BitIntType>(CanonicalType); 8432 } 8433 8434 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 8435 inline bool Type::is##Id##Type() const { \ 8436 return isSpecificBuiltinType(BuiltinType::Id); \ 8437 } 8438 #include "clang/Basic/OpenCLExtensionTypes.def" 8439 8440 inline bool Type::isOCLIntelSubgroupAVCType() const { 8441 #define INTEL_SUBGROUP_AVC_TYPE(ExtType, Id) \ 8442 isOCLIntelSubgroupAVC##Id##Type() || 8443 return 8444 #include "clang/Basic/OpenCLExtensionTypes.def" 8445 false; // end of boolean or operation 8446 } 8447 8448 inline bool Type::isOCLExtOpaqueType() const { 8449 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) is##Id##Type() || 8450 return 8451 #include "clang/Basic/OpenCLExtensionTypes.def" 8452 false; // end of boolean or operation 8453 } 8454 8455 inline bool Type::isOpenCLSpecificType() const { 8456 return isSamplerT() || isEventT() || isImageType() || isClkEventT() || 8457 isQueueT() || isReserveIDT() || isPipeType() || isOCLExtOpaqueType(); 8458 } 8459 8460 #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \ 8461 inline bool Type::is##Id##Type() const { \ 8462 return isSpecificBuiltinType(BuiltinType::Id); \ 8463 } 8464 #include "clang/Basic/HLSLIntangibleTypes.def" 8465 8466 inline bool Type::isHLSLBuiltinIntangibleType() const { 8467 #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) is##Id##Type() || 8468 return 8469 #include "clang/Basic/HLSLIntangibleTypes.def" 8470 false; 8471 } 8472 8473 inline bool Type::isHLSLSpecificType() const { 8474 return isHLSLBuiltinIntangibleType() || isHLSLAttributedResourceType(); 8475 } 8476 8477 inline bool Type::isHLSLAttributedResourceType() const { 8478 return isa<HLSLAttributedResourceType>(this); 8479 } 8480 8481 inline bool Type::isTemplateTypeParmType() const { 8482 return isa<TemplateTypeParmType>(CanonicalType); 8483 } 8484 8485 inline bool Type::isSpecificBuiltinType(unsigned K) const { 8486 if (const BuiltinType *BT = getAs<BuiltinType>()) { 8487 return BT->getKind() == static_cast<BuiltinType::Kind>(K); 8488 } 8489 return false; 8490 } 8491 8492 inline bool Type::isPlaceholderType() const { 8493 if (const auto *BT = dyn_cast<BuiltinType>(this)) 8494 return BT->isPlaceholderType(); 8495 return false; 8496 } 8497 8498 inline const BuiltinType *Type::getAsPlaceholderType() const { 8499 if (const auto *BT = dyn_cast<BuiltinType>(this)) 8500 if (BT->isPlaceholderType()) 8501 return BT; 8502 return nullptr; 8503 } 8504 8505 inline bool Type::isSpecificPlaceholderType(unsigned K) const { 8506 assert(BuiltinType::isPlaceholderTypeKind((BuiltinType::Kind) K)); 8507 return isSpecificBuiltinType(K); 8508 } 8509 8510 inline bool Type::isNonOverloadPlaceholderType() const { 8511 if (const auto *BT = dyn_cast<BuiltinType>(this)) 8512 return BT->isNonOverloadPlaceholderType(); 8513 return false; 8514 } 8515 8516 inline bool Type::isVoidType() const { 8517 return isSpecificBuiltinType(BuiltinType::Void); 8518 } 8519 8520 inline bool Type::isHalfType() const { 8521 // FIXME: Should we allow complex __fp16? Probably not. 8522 return isSpecificBuiltinType(BuiltinType::Half); 8523 } 8524 8525 inline bool Type::isFloat16Type() const { 8526 return isSpecificBuiltinType(BuiltinType::Float16); 8527 } 8528 8529 inline bool Type::isFloat32Type() const { 8530 return isSpecificBuiltinType(BuiltinType::Float); 8531 } 8532 8533 inline bool Type::isDoubleType() const { 8534 return isSpecificBuiltinType(BuiltinType::Double); 8535 } 8536 8537 inline bool Type::isBFloat16Type() const { 8538 return isSpecificBuiltinType(BuiltinType::BFloat16); 8539 } 8540 8541 inline bool Type::isMFloat8Type() const { 8542 return isSpecificBuiltinType(BuiltinType::MFloat8); 8543 } 8544 8545 inline bool Type::isFloat128Type() const { 8546 return isSpecificBuiltinType(BuiltinType::Float128); 8547 } 8548 8549 inline bool Type::isIbm128Type() const { 8550 return isSpecificBuiltinType(BuiltinType::Ibm128); 8551 } 8552 8553 inline bool Type::isNullPtrType() const { 8554 return isSpecificBuiltinType(BuiltinType::NullPtr); 8555 } 8556 8557 bool IsEnumDeclComplete(EnumDecl *); 8558 bool IsEnumDeclScoped(EnumDecl *); 8559 8560 inline bool Type::isIntegerType() const { 8561 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) 8562 return BT->getKind() >= BuiltinType::Bool && 8563 BT->getKind() <= BuiltinType::Int128; 8564 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) { 8565 // Incomplete enum types are not treated as integer types. 8566 // FIXME: In C++, enum types are never integer types. 8567 return IsEnumDeclComplete(ET->getDecl()) && 8568 !IsEnumDeclScoped(ET->getDecl()); 8569 } 8570 return isBitIntType(); 8571 } 8572 8573 inline bool Type::isFixedPointType() const { 8574 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { 8575 return BT->getKind() >= BuiltinType::ShortAccum && 8576 BT->getKind() <= BuiltinType::SatULongFract; 8577 } 8578 return false; 8579 } 8580 8581 inline bool Type::isFixedPointOrIntegerType() const { 8582 return isFixedPointType() || isIntegerType(); 8583 } 8584 8585 inline bool Type::isConvertibleToFixedPointType() const { 8586 return isRealFloatingType() || isFixedPointOrIntegerType(); 8587 } 8588 8589 inline bool Type::isSaturatedFixedPointType() const { 8590 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { 8591 return BT->getKind() >= BuiltinType::SatShortAccum && 8592 BT->getKind() <= BuiltinType::SatULongFract; 8593 } 8594 return false; 8595 } 8596 8597 inline bool Type::isUnsaturatedFixedPointType() const { 8598 return isFixedPointType() && !isSaturatedFixedPointType(); 8599 } 8600 8601 inline bool Type::isSignedFixedPointType() const { 8602 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { 8603 return ((BT->getKind() >= BuiltinType::ShortAccum && 8604 BT->getKind() <= BuiltinType::LongAccum) || 8605 (BT->getKind() >= BuiltinType::ShortFract && 8606 BT->getKind() <= BuiltinType::LongFract) || 8607 (BT->getKind() >= BuiltinType::SatShortAccum && 8608 BT->getKind() <= BuiltinType::SatLongAccum) || 8609 (BT->getKind() >= BuiltinType::SatShortFract && 8610 BT->getKind() <= BuiltinType::SatLongFract)); 8611 } 8612 return false; 8613 } 8614 8615 inline bool Type::isUnsignedFixedPointType() const { 8616 return isFixedPointType() && !isSignedFixedPointType(); 8617 } 8618 8619 inline bool Type::isScalarType() const { 8620 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) 8621 return BT->getKind() > BuiltinType::Void && 8622 BT->getKind() <= BuiltinType::NullPtr; 8623 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 8624 // Enums are scalar types, but only if they are defined. Incomplete enums 8625 // are not treated as scalar types. 8626 return IsEnumDeclComplete(ET->getDecl()); 8627 return isa<PointerType>(CanonicalType) || 8628 isa<BlockPointerType>(CanonicalType) || 8629 isa<MemberPointerType>(CanonicalType) || 8630 isa<ComplexType>(CanonicalType) || 8631 isa<ObjCObjectPointerType>(CanonicalType) || 8632 isBitIntType(); 8633 } 8634 8635 inline bool Type::isIntegralOrEnumerationType() const { 8636 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) 8637 return BT->getKind() >= BuiltinType::Bool && 8638 BT->getKind() <= BuiltinType::Int128; 8639 8640 // Check for a complete enum type; incomplete enum types are not properly an 8641 // enumeration type in the sense required here. 8642 if (const auto *ET = dyn_cast<EnumType>(CanonicalType)) 8643 return IsEnumDeclComplete(ET->getDecl()); 8644 8645 return isBitIntType(); 8646 } 8647 8648 inline bool Type::isBooleanType() const { 8649 if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) 8650 return BT->getKind() == BuiltinType::Bool; 8651 return false; 8652 } 8653 8654 inline bool Type::isUndeducedType() const { 8655 auto *DT = getContainedDeducedType(); 8656 return DT && !DT->isDeduced(); 8657 } 8658 8659 /// Determines whether this is a type for which one can define 8660 /// an overloaded operator. 8661 inline bool Type::isOverloadableType() const { 8662 if (!isDependentType()) 8663 return isRecordType() || isEnumeralType(); 8664 return !isArrayType() && !isFunctionType() && !isAnyPointerType() && 8665 !isMemberPointerType(); 8666 } 8667 8668 /// Determines whether this type is written as a typedef-name. 8669 inline bool Type::isTypedefNameType() const { 8670 if (getAs<TypedefType>()) 8671 return true; 8672 if (auto *TST = getAs<TemplateSpecializationType>()) 8673 return TST->isTypeAlias(); 8674 return false; 8675 } 8676 8677 /// Determines whether this type can decay to a pointer type. 8678 inline bool Type::canDecayToPointerType() const { 8679 return isFunctionType() || (isArrayType() && !isArrayParameterType()); 8680 } 8681 8682 inline bool Type::hasPointerRepresentation() const { 8683 return (isPointerType() || isReferenceType() || isBlockPointerType() || 8684 isObjCObjectPointerType() || isNullPtrType()); 8685 } 8686 8687 inline bool Type::hasObjCPointerRepresentation() const { 8688 return isObjCObjectPointerType(); 8689 } 8690 8691 inline const Type *Type::getBaseElementTypeUnsafe() const { 8692 const Type *type = this; 8693 while (const ArrayType *arrayType = type->getAsArrayTypeUnsafe()) 8694 type = arrayType->getElementType().getTypePtr(); 8695 return type; 8696 } 8697 8698 inline const Type *Type::getPointeeOrArrayElementType() const { 8699 const Type *type = this; 8700 if (type->isAnyPointerType()) 8701 return type->getPointeeType().getTypePtr(); 8702 else if (type->isArrayType()) 8703 return type->getBaseElementTypeUnsafe(); 8704 return type; 8705 } 8706 /// Insertion operator for partial diagnostics. This allows sending adress 8707 /// spaces into a diagnostic with <<. 8708 inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, 8709 LangAS AS) { 8710 PD.AddTaggedVal(llvm::to_underlying(AS), 8711 DiagnosticsEngine::ArgumentKind::ak_addrspace); 8712 return PD; 8713 } 8714 8715 /// Insertion operator for partial diagnostics. This allows sending Qualifiers 8716 /// into a diagnostic with <<. 8717 inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, 8718 Qualifiers Q) { 8719 PD.AddTaggedVal(Q.getAsOpaqueValue(), 8720 DiagnosticsEngine::ArgumentKind::ak_qual); 8721 return PD; 8722 } 8723 8724 /// Insertion operator for partial diagnostics. This allows sending QualType's 8725 /// into a diagnostic with <<. 8726 inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, 8727 QualType T) { 8728 PD.AddTaggedVal(reinterpret_cast<uint64_t>(T.getAsOpaquePtr()), 8729 DiagnosticsEngine::ak_qualtype); 8730 return PD; 8731 } 8732 8733 // Helper class template that is used by Type::getAs to ensure that one does 8734 // not try to look through a qualified type to get to an array type. 8735 template <typename T> 8736 using TypeIsArrayType = 8737 std::integral_constant<bool, std::is_same<T, ArrayType>::value || 8738 std::is_base_of<ArrayType, T>::value>; 8739 8740 // Member-template getAs<specific type>'. 8741 template <typename T> const T *Type::getAs() const { 8742 static_assert(!TypeIsArrayType<T>::value, 8743 "ArrayType cannot be used with getAs!"); 8744 8745 // If this is directly a T type, return it. 8746 if (const auto *Ty = dyn_cast<T>(this)) 8747 return Ty; 8748 8749 // If the canonical form of this type isn't the right kind, reject it. 8750 if (!isa<T>(CanonicalType)) 8751 return nullptr; 8752 8753 // If this is a typedef for the type, strip the typedef off without 8754 // losing all typedef information. 8755 return cast<T>(getUnqualifiedDesugaredType()); 8756 } 8757 8758 template <typename T> const T *Type::getAsAdjusted() const { 8759 static_assert(!TypeIsArrayType<T>::value, "ArrayType cannot be used with getAsAdjusted!"); 8760 8761 // If this is directly a T type, return it. 8762 if (const auto *Ty = dyn_cast<T>(this)) 8763 return Ty; 8764 8765 // If the canonical form of this type isn't the right kind, reject it. 8766 if (!isa<T>(CanonicalType)) 8767 return nullptr; 8768 8769 // Strip off type adjustments that do not modify the underlying nature of the 8770 // type. 8771 const Type *Ty = this; 8772 while (Ty) { 8773 if (const auto *A = dyn_cast<AttributedType>(Ty)) 8774 Ty = A->getModifiedType().getTypePtr(); 8775 else if (const auto *A = dyn_cast<BTFTagAttributedType>(Ty)) 8776 Ty = A->getWrappedType().getTypePtr(); 8777 else if (const auto *A = dyn_cast<HLSLAttributedResourceType>(Ty)) 8778 Ty = A->getWrappedType().getTypePtr(); 8779 else if (const auto *E = dyn_cast<ElaboratedType>(Ty)) 8780 Ty = E->desugar().getTypePtr(); 8781 else if (const auto *P = dyn_cast<ParenType>(Ty)) 8782 Ty = P->desugar().getTypePtr(); 8783 else if (const auto *A = dyn_cast<AdjustedType>(Ty)) 8784 Ty = A->desugar().getTypePtr(); 8785 else if (const auto *M = dyn_cast<MacroQualifiedType>(Ty)) 8786 Ty = M->desugar().getTypePtr(); 8787 else 8788 break; 8789 } 8790 8791 // Just because the canonical type is correct does not mean we can use cast<>, 8792 // since we may not have stripped off all the sugar down to the base type. 8793 return dyn_cast<T>(Ty); 8794 } 8795 8796 inline const ArrayType *Type::getAsArrayTypeUnsafe() const { 8797 // If this is directly an array type, return it. 8798 if (const auto *arr = dyn_cast<ArrayType>(this)) 8799 return arr; 8800 8801 // If the canonical form of this type isn't the right kind, reject it. 8802 if (!isa<ArrayType>(CanonicalType)) 8803 return nullptr; 8804 8805 // If this is a typedef for the type, strip the typedef off without 8806 // losing all typedef information. 8807 return cast<ArrayType>(getUnqualifiedDesugaredType()); 8808 } 8809 8810 template <typename T> const T *Type::castAs() const { 8811 static_assert(!TypeIsArrayType<T>::value, 8812 "ArrayType cannot be used with castAs!"); 8813 8814 if (const auto *ty = dyn_cast<T>(this)) return ty; 8815 assert(isa<T>(CanonicalType)); 8816 return cast<T>(getUnqualifiedDesugaredType()); 8817 } 8818 8819 inline const ArrayType *Type::castAsArrayTypeUnsafe() const { 8820 assert(isa<ArrayType>(CanonicalType)); 8821 if (const auto *arr = dyn_cast<ArrayType>(this)) return arr; 8822 return cast<ArrayType>(getUnqualifiedDesugaredType()); 8823 } 8824 8825 DecayedType::DecayedType(QualType OriginalType, QualType DecayedPtr, 8826 QualType CanonicalPtr) 8827 : AdjustedType(Decayed, OriginalType, DecayedPtr, CanonicalPtr) { 8828 #ifndef NDEBUG 8829 QualType Adjusted = getAdjustedType(); 8830 (void)AttributedType::stripOuterNullability(Adjusted); 8831 assert(isa<PointerType>(Adjusted)); 8832 #endif 8833 } 8834 8835 QualType DecayedType::getPointeeType() const { 8836 QualType Decayed = getDecayedType(); 8837 (void)AttributedType::stripOuterNullability(Decayed); 8838 return cast<PointerType>(Decayed)->getPointeeType(); 8839 } 8840 8841 // Get the decimal string representation of a fixed point type, represented 8842 // as a scaled integer. 8843 // TODO: At some point, we should change the arguments to instead just accept an 8844 // APFixedPoint instead of APSInt and scale. 8845 void FixedPointValueToString(SmallVectorImpl<char> &Str, llvm::APSInt Val, 8846 unsigned Scale); 8847 8848 inline FunctionEffectsRef FunctionEffectsRef::get(QualType QT) { 8849 const Type *TypePtr = QT.getTypePtr(); 8850 while (true) { 8851 if (QualType Pointee = TypePtr->getPointeeType(); !Pointee.isNull()) 8852 TypePtr = Pointee.getTypePtr(); 8853 else if (TypePtr->isArrayType()) 8854 TypePtr = TypePtr->getBaseElementTypeUnsafe(); 8855 else 8856 break; 8857 } 8858 if (const auto *FPT = TypePtr->getAs<FunctionProtoType>()) 8859 return FPT->getFunctionEffects(); 8860 return {}; 8861 } 8862 8863 } // namespace clang 8864 8865 #endif // LLVM_CLANG_AST_TYPE_H 8866