1 //===--- Type.h - C Language Family Type Representation ---------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the Type interface and subclasses. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CLANG_AST_TYPE_H 15 #define LLVM_CLANG_AST_TYPE_H 16 17 #include "clang/Basic/Diagnostic.h" 18 #include "clang/Basic/ExceptionSpecificationType.h" 19 #include "clang/Basic/IdentifierTable.h" 20 #include "clang/Basic/Linkage.h" 21 #include "clang/Basic/PartialDiagnostic.h" 22 #include "clang/Basic/Visibility.h" 23 #include "clang/AST/NestedNameSpecifier.h" 24 #include "clang/AST/TemplateName.h" 25 #include "llvm/Support/type_traits.h" 26 #include "llvm/Support/ErrorHandling.h" 27 #include "llvm/ADT/APSInt.h" 28 #include "llvm/ADT/FoldingSet.h" 29 #include "llvm/ADT/Optional.h" 30 #include "llvm/ADT/PointerIntPair.h" 31 #include "llvm/ADT/PointerUnion.h" 32 #include "clang/Basic/LLVM.h" 33 34 namespace clang { 35 enum { 36 TypeAlignmentInBits = 4, 37 TypeAlignment = 1 << TypeAlignmentInBits 38 }; 39 class Type; 40 class ExtQuals; 41 class QualType; 42 } 43 44 namespace llvm { 45 template <typename T> 46 class PointerLikeTypeTraits; 47 template<> 48 class PointerLikeTypeTraits< ::clang::Type*> { 49 public: 50 static inline void *getAsVoidPointer(::clang::Type *P) { return P; } 51 static inline ::clang::Type *getFromVoidPointer(void *P) { 52 return static_cast< ::clang::Type*>(P); 53 } 54 enum { NumLowBitsAvailable = clang::TypeAlignmentInBits }; 55 }; 56 template<> 57 class PointerLikeTypeTraits< ::clang::ExtQuals*> { 58 public: 59 static inline void *getAsVoidPointer(::clang::ExtQuals *P) { return P; } 60 static inline ::clang::ExtQuals *getFromVoidPointer(void *P) { 61 return static_cast< ::clang::ExtQuals*>(P); 62 } 63 enum { NumLowBitsAvailable = clang::TypeAlignmentInBits }; 64 }; 65 66 template <> 67 struct isPodLike<clang::QualType> { static const bool value = true; }; 68 } 69 70 namespace clang { 71 class ASTContext; 72 class TypedefNameDecl; 73 class TemplateDecl; 74 class TemplateTypeParmDecl; 75 class NonTypeTemplateParmDecl; 76 class TemplateTemplateParmDecl; 77 class TagDecl; 78 class RecordDecl; 79 class CXXRecordDecl; 80 class EnumDecl; 81 class FieldDecl; 82 class FunctionDecl; 83 class ObjCInterfaceDecl; 84 class ObjCProtocolDecl; 85 class ObjCMethodDecl; 86 class UnresolvedUsingTypenameDecl; 87 class Expr; 88 class Stmt; 89 class SourceLocation; 90 class StmtIteratorBase; 91 class TemplateArgument; 92 class TemplateArgumentLoc; 93 class TemplateArgumentListInfo; 94 class ElaboratedType; 95 class ExtQuals; 96 class ExtQualsTypeCommonBase; 97 struct PrintingPolicy; 98 99 template <typename> class CanQual; 100 typedef CanQual<Type> CanQualType; 101 102 // Provide forward declarations for all of the *Type classes 103 #define TYPE(Class, Base) class Class##Type; 104 #include "clang/AST/TypeNodes.def" 105 106 /// Qualifiers - The collection of all-type qualifiers we support. 107 /// Clang supports five independent qualifiers: 108 /// * C99: const, volatile, and restrict 109 /// * Embedded C (TR18037): address spaces 110 /// * Objective C: the GC attributes (none, weak, or strong) 111 class Qualifiers { 112 public: 113 enum TQ { // NOTE: These flags must be kept in sync with DeclSpec::TQ. 114 Const = 0x1, 115 Restrict = 0x2, 116 Volatile = 0x4, 117 CVRMask = Const | Volatile | Restrict 118 }; 119 120 enum GC { 121 GCNone = 0, 122 Weak, 123 Strong 124 }; 125 126 enum ObjCLifetime { 127 /// There is no lifetime qualification on this type. 128 OCL_None, 129 130 /// This object can be modified without requiring retains or 131 /// releases. 132 OCL_ExplicitNone, 133 134 /// Assigning into this object requires the old value to be 135 /// released and the new value to be retained. The timing of the 136 /// release of the old value is inexact: it may be moved to 137 /// immediately after the last known point where the value is 138 /// live. 139 OCL_Strong, 140 141 /// Reading or writing from this object requires a barrier call. 142 OCL_Weak, 143 144 /// Assigning into this object requires a lifetime extension. 145 OCL_Autoreleasing 146 }; 147 148 enum { 149 /// The maximum supported address space number. 150 /// 24 bits should be enough for anyone. 151 MaxAddressSpace = 0xffffffu, 152 153 /// The width of the "fast" qualifier mask. 154 FastWidth = 3, 155 156 /// The fast qualifier mask. 157 FastMask = (1 << FastWidth) - 1 158 }; 159 160 Qualifiers() : Mask(0) {} 161 162 static Qualifiers fromFastMask(unsigned Mask) { 163 Qualifiers Qs; 164 Qs.addFastQualifiers(Mask); 165 return Qs; 166 } 167 168 static Qualifiers fromCVRMask(unsigned CVR) { 169 Qualifiers Qs; 170 Qs.addCVRQualifiers(CVR); 171 return Qs; 172 } 173 174 // Deserialize qualifiers from an opaque representation. 175 static Qualifiers fromOpaqueValue(unsigned opaque) { 176 Qualifiers Qs; 177 Qs.Mask = opaque; 178 return Qs; 179 } 180 181 // Serialize these qualifiers into an opaque representation. 182 unsigned getAsOpaqueValue() const { 183 return Mask; 184 } 185 186 bool hasConst() const { return Mask & Const; } 187 void setConst(bool flag) { 188 Mask = (Mask & ~Const) | (flag ? Const : 0); 189 } 190 void removeConst() { Mask &= ~Const; } 191 void addConst() { Mask |= Const; } 192 193 bool hasVolatile() const { return Mask & Volatile; } 194 void setVolatile(bool flag) { 195 Mask = (Mask & ~Volatile) | (flag ? Volatile : 0); 196 } 197 void removeVolatile() { Mask &= ~Volatile; } 198 void addVolatile() { Mask |= Volatile; } 199 200 bool hasRestrict() const { return Mask & Restrict; } 201 void setRestrict(bool flag) { 202 Mask = (Mask & ~Restrict) | (flag ? Restrict : 0); 203 } 204 void removeRestrict() { Mask &= ~Restrict; } 205 void addRestrict() { Mask |= Restrict; } 206 207 bool hasCVRQualifiers() const { return getCVRQualifiers(); } 208 unsigned getCVRQualifiers() const { return Mask & CVRMask; } 209 void setCVRQualifiers(unsigned mask) { 210 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); 211 Mask = (Mask & ~CVRMask) | mask; 212 } 213 void removeCVRQualifiers(unsigned mask) { 214 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); 215 Mask &= ~mask; 216 } 217 void removeCVRQualifiers() { 218 removeCVRQualifiers(CVRMask); 219 } 220 void addCVRQualifiers(unsigned mask) { 221 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); 222 Mask |= mask; 223 } 224 225 bool hasObjCGCAttr() const { return Mask & GCAttrMask; } 226 GC getObjCGCAttr() const { return GC((Mask & GCAttrMask) >> GCAttrShift); } 227 void setObjCGCAttr(GC type) { 228 Mask = (Mask & ~GCAttrMask) | (type << GCAttrShift); 229 } 230 void removeObjCGCAttr() { setObjCGCAttr(GCNone); } 231 void addObjCGCAttr(GC type) { 232 assert(type); 233 setObjCGCAttr(type); 234 } 235 Qualifiers withoutObjCGCAttr() const { 236 Qualifiers qs = *this; 237 qs.removeObjCGCAttr(); 238 return qs; 239 } 240 Qualifiers withoutObjCLifetime() const { 241 Qualifiers qs = *this; 242 qs.removeObjCLifetime(); 243 return qs; 244 } 245 246 bool hasObjCLifetime() const { return Mask & LifetimeMask; } 247 ObjCLifetime getObjCLifetime() const { 248 return ObjCLifetime((Mask & LifetimeMask) >> LifetimeShift); 249 } 250 void setObjCLifetime(ObjCLifetime type) { 251 Mask = (Mask & ~LifetimeMask) | (type << LifetimeShift); 252 } 253 void removeObjCLifetime() { setObjCLifetime(OCL_None); } 254 void addObjCLifetime(ObjCLifetime type) { 255 assert(type); 256 assert(!hasObjCLifetime()); 257 Mask |= (type << LifetimeShift); 258 } 259 260 /// True if the lifetime is neither None or ExplicitNone. 261 bool hasNonTrivialObjCLifetime() const { 262 ObjCLifetime lifetime = getObjCLifetime(); 263 return (lifetime > OCL_ExplicitNone); 264 } 265 266 /// True if the lifetime is either strong or weak. 267 bool hasStrongOrWeakObjCLifetime() const { 268 ObjCLifetime lifetime = getObjCLifetime(); 269 return (lifetime == OCL_Strong || lifetime == OCL_Weak); 270 } 271 272 bool hasAddressSpace() const { return Mask & AddressSpaceMask; } 273 unsigned getAddressSpace() const { return Mask >> AddressSpaceShift; } 274 void setAddressSpace(unsigned space) { 275 assert(space <= MaxAddressSpace); 276 Mask = (Mask & ~AddressSpaceMask) 277 | (((uint32_t) space) << AddressSpaceShift); 278 } 279 void removeAddressSpace() { setAddressSpace(0); } 280 void addAddressSpace(unsigned space) { 281 assert(space); 282 setAddressSpace(space); 283 } 284 285 // Fast qualifiers are those that can be allocated directly 286 // on a QualType object. 287 bool hasFastQualifiers() const { return getFastQualifiers(); } 288 unsigned getFastQualifiers() const { return Mask & FastMask; } 289 void setFastQualifiers(unsigned mask) { 290 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); 291 Mask = (Mask & ~FastMask) | mask; 292 } 293 void removeFastQualifiers(unsigned mask) { 294 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); 295 Mask &= ~mask; 296 } 297 void removeFastQualifiers() { 298 removeFastQualifiers(FastMask); 299 } 300 void addFastQualifiers(unsigned mask) { 301 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); 302 Mask |= mask; 303 } 304 305 /// hasNonFastQualifiers - Return true if the set contains any 306 /// qualifiers which require an ExtQuals node to be allocated. 307 bool hasNonFastQualifiers() const { return Mask & ~FastMask; } 308 Qualifiers getNonFastQualifiers() const { 309 Qualifiers Quals = *this; 310 Quals.setFastQualifiers(0); 311 return Quals; 312 } 313 314 /// hasQualifiers - Return true if the set contains any qualifiers. 315 bool hasQualifiers() const { return Mask; } 316 bool empty() const { return !Mask; } 317 318 /// \brief Add the qualifiers from the given set to this set. 319 void addQualifiers(Qualifiers Q) { 320 // If the other set doesn't have any non-boolean qualifiers, just 321 // bit-or it in. 322 if (!(Q.Mask & ~CVRMask)) 323 Mask |= Q.Mask; 324 else { 325 Mask |= (Q.Mask & CVRMask); 326 if (Q.hasAddressSpace()) 327 addAddressSpace(Q.getAddressSpace()); 328 if (Q.hasObjCGCAttr()) 329 addObjCGCAttr(Q.getObjCGCAttr()); 330 if (Q.hasObjCLifetime()) 331 addObjCLifetime(Q.getObjCLifetime()); 332 } 333 } 334 335 /// \brief Add the qualifiers from the given set to this set, given that 336 /// they don't conflict. 337 void addConsistentQualifiers(Qualifiers qs) { 338 assert(getAddressSpace() == qs.getAddressSpace() || 339 !hasAddressSpace() || !qs.hasAddressSpace()); 340 assert(getObjCGCAttr() == qs.getObjCGCAttr() || 341 !hasObjCGCAttr() || !qs.hasObjCGCAttr()); 342 assert(getObjCLifetime() == qs.getObjCLifetime() || 343 !hasObjCLifetime() || !qs.hasObjCLifetime()); 344 Mask |= qs.Mask; 345 } 346 347 /// \brief Determines if these qualifiers compatibly include another set. 348 /// Generally this answers the question of whether an object with the other 349 /// qualifiers can be safely used as an object with these qualifiers. 350 bool compatiblyIncludes(Qualifiers other) const { 351 return 352 // Address spaces must match exactly. 353 getAddressSpace() == other.getAddressSpace() && 354 // ObjC GC qualifiers can match, be added, or be removed, but can't be 355 // changed. 356 (getObjCGCAttr() == other.getObjCGCAttr() || 357 !hasObjCGCAttr() || !other.hasObjCGCAttr()) && 358 // ObjC lifetime qualifiers must match exactly. 359 getObjCLifetime() == other.getObjCLifetime() && 360 // CVR qualifiers may subset. 361 (((Mask & CVRMask) | (other.Mask & CVRMask)) == (Mask & CVRMask)); 362 } 363 364 /// \brief Determines if these qualifiers compatibly include another set of 365 /// qualifiers from the narrow perspective of Objective-C ARC lifetime. 366 /// 367 /// One set of Objective-C lifetime qualifiers compatibly includes the other 368 /// if the lifetime qualifiers match, or if both are non-__weak and the 369 /// including set also contains the 'const' qualifier. 370 bool compatiblyIncludesObjCLifetime(Qualifiers other) const { 371 if (getObjCLifetime() == other.getObjCLifetime()) 372 return true; 373 374 if (getObjCLifetime() == OCL_Weak || other.getObjCLifetime() == OCL_Weak) 375 return false; 376 377 return hasConst(); 378 } 379 380 bool isSupersetOf(Qualifiers Other) const; 381 382 /// \brief Determine whether this set of qualifiers is a strict superset of 383 /// another set of qualifiers, not considering qualifier compatibility. 384 bool isStrictSupersetOf(Qualifiers Other) const; 385 386 bool operator==(Qualifiers Other) const { return Mask == Other.Mask; } 387 bool operator!=(Qualifiers Other) const { return Mask != Other.Mask; } 388 389 operator bool() const { return hasQualifiers(); } 390 391 Qualifiers &operator+=(Qualifiers R) { 392 addQualifiers(R); 393 return *this; 394 } 395 396 // Union two qualifier sets. If an enumerated qualifier appears 397 // in both sets, use the one from the right. 398 friend Qualifiers operator+(Qualifiers L, Qualifiers R) { 399 L += R; 400 return L; 401 } 402 403 Qualifiers &operator-=(Qualifiers R) { 404 Mask = Mask & ~(R.Mask); 405 return *this; 406 } 407 408 /// \brief Compute the difference between two qualifier sets. 409 friend Qualifiers operator-(Qualifiers L, Qualifiers R) { 410 L -= R; 411 return L; 412 } 413 414 std::string getAsString() const; 415 std::string getAsString(const PrintingPolicy &Policy) const { 416 std::string Buffer; 417 getAsStringInternal(Buffer, Policy); 418 return Buffer; 419 } 420 void getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const; 421 422 void Profile(llvm::FoldingSetNodeID &ID) const { 423 ID.AddInteger(Mask); 424 } 425 426 private: 427 428 // bits: |0 1 2|3 .. 4|5 .. 7|8 ... 31| 429 // |C R V|GCAttr|Lifetime|AddressSpace| 430 uint32_t Mask; 431 432 static const uint32_t GCAttrMask = 0x18; 433 static const uint32_t GCAttrShift = 3; 434 static const uint32_t LifetimeMask = 0xE0; 435 static const uint32_t LifetimeShift = 5; 436 static const uint32_t AddressSpaceMask = ~(CVRMask|GCAttrMask|LifetimeMask); 437 static const uint32_t AddressSpaceShift = 8; 438 }; 439 440 /// CallingConv - Specifies the calling convention that a function uses. 441 enum CallingConv { 442 CC_Default, 443 CC_C, // __attribute__((cdecl)) 444 CC_X86StdCall, // __attribute__((stdcall)) 445 CC_X86FastCall, // __attribute__((fastcall)) 446 CC_X86ThisCall, // __attribute__((thiscall)) 447 CC_X86Pascal, // __attribute__((pascal)) 448 CC_AAPCS, // __attribute__((pcs("aapcs"))) 449 CC_AAPCS_VFP // __attribute__((pcs("aapcs-vfp"))) 450 }; 451 452 /// A std::pair-like structure for storing a qualified type split 453 /// into its local qualifiers and its locally-unqualified type. 454 struct SplitQualType { 455 /// The locally-unqualified type. 456 const Type *Ty; 457 458 /// The local qualifiers. 459 Qualifiers Quals; 460 461 SplitQualType() : Ty(0), Quals() {} 462 SplitQualType(const Type *ty, Qualifiers qs) : Ty(ty), Quals(qs) {} 463 464 SplitQualType getSingleStepDesugaredType() const; // end of this file 465 466 // Make llvm::tie work. 467 operator std::pair<const Type *,Qualifiers>() const { 468 return std::pair<const Type *,Qualifiers>(Ty, Quals); 469 } 470 471 friend bool operator==(SplitQualType a, SplitQualType b) { 472 return a.Ty == b.Ty && a.Quals == b.Quals; 473 } 474 friend bool operator!=(SplitQualType a, SplitQualType b) { 475 return a.Ty != b.Ty || a.Quals != b.Quals; 476 } 477 }; 478 479 /// QualType - For efficiency, we don't store CV-qualified types as nodes on 480 /// their own: instead each reference to a type stores the qualifiers. This 481 /// greatly reduces the number of nodes we need to allocate for types (for 482 /// example we only need one for 'int', 'const int', 'volatile int', 483 /// 'const volatile int', etc). 484 /// 485 /// As an added efficiency bonus, instead of making this a pair, we 486 /// just store the two bits we care about in the low bits of the 487 /// pointer. To handle the packing/unpacking, we make QualType be a 488 /// simple wrapper class that acts like a smart pointer. A third bit 489 /// indicates whether there are extended qualifiers present, in which 490 /// case the pointer points to a special structure. 491 class QualType { 492 // Thankfully, these are efficiently composable. 493 llvm::PointerIntPair<llvm::PointerUnion<const Type*,const ExtQuals*>, 494 Qualifiers::FastWidth> Value; 495 496 const ExtQuals *getExtQualsUnsafe() const { 497 return Value.getPointer().get<const ExtQuals*>(); 498 } 499 500 const Type *getTypePtrUnsafe() const { 501 return Value.getPointer().get<const Type*>(); 502 } 503 504 const ExtQualsTypeCommonBase *getCommonPtr() const { 505 assert(!isNull() && "Cannot retrieve a NULL type pointer"); 506 uintptr_t CommonPtrVal 507 = reinterpret_cast<uintptr_t>(Value.getOpaqueValue()); 508 CommonPtrVal &= ~(uintptr_t)((1 << TypeAlignmentInBits) - 1); 509 return reinterpret_cast<ExtQualsTypeCommonBase*>(CommonPtrVal); 510 } 511 512 friend class QualifierCollector; 513 public: 514 QualType() {} 515 516 QualType(const Type *Ptr, unsigned Quals) 517 : Value(Ptr, Quals) {} 518 QualType(const ExtQuals *Ptr, unsigned Quals) 519 : Value(Ptr, Quals) {} 520 521 unsigned getLocalFastQualifiers() const { return Value.getInt(); } 522 void setLocalFastQualifiers(unsigned Quals) { Value.setInt(Quals); } 523 524 /// Retrieves a pointer to the underlying (unqualified) type. 525 /// This should really return a const Type, but it's not worth 526 /// changing all the users right now. 527 /// 528 /// This function requires that the type not be NULL. If the type might be 529 /// NULL, use the (slightly less efficient) \c getTypePtrOrNull(). 530 const Type *getTypePtr() const; 531 532 const Type *getTypePtrOrNull() const; 533 534 /// Retrieves a pointer to the name of the base type. 535 const IdentifierInfo *getBaseTypeIdentifier() const; 536 537 /// Divides a QualType into its unqualified type and a set of local 538 /// qualifiers. 539 SplitQualType split() const; 540 541 void *getAsOpaquePtr() const { return Value.getOpaqueValue(); } 542 static QualType getFromOpaquePtr(const void *Ptr) { 543 QualType T; 544 T.Value.setFromOpaqueValue(const_cast<void*>(Ptr)); 545 return T; 546 } 547 548 const Type &operator*() const { 549 return *getTypePtr(); 550 } 551 552 const Type *operator->() const { 553 return getTypePtr(); 554 } 555 556 bool isCanonical() const; 557 bool isCanonicalAsParam() const; 558 559 /// isNull - Return true if this QualType doesn't point to a type yet. 560 bool isNull() const { 561 return Value.getPointer().isNull(); 562 } 563 564 /// \brief Determine whether this particular QualType instance has the 565 /// "const" qualifier set, without looking through typedefs that may have 566 /// added "const" at a different level. 567 bool isLocalConstQualified() const { 568 return (getLocalFastQualifiers() & Qualifiers::Const); 569 } 570 571 /// \brief Determine whether this type is const-qualified. 572 bool isConstQualified() const; 573 574 /// \brief Determine whether this particular QualType instance has the 575 /// "restrict" qualifier set, without looking through typedefs that may have 576 /// added "restrict" at a different level. 577 bool isLocalRestrictQualified() const { 578 return (getLocalFastQualifiers() & Qualifiers::Restrict); 579 } 580 581 /// \brief Determine whether this type is restrict-qualified. 582 bool isRestrictQualified() const; 583 584 /// \brief Determine whether this particular QualType instance has the 585 /// "volatile" qualifier set, without looking through typedefs that may have 586 /// added "volatile" at a different level. 587 bool isLocalVolatileQualified() const { 588 return (getLocalFastQualifiers() & Qualifiers::Volatile); 589 } 590 591 /// \brief Determine whether this type is volatile-qualified. 592 bool isVolatileQualified() const; 593 594 /// \brief Determine whether this particular QualType instance has any 595 /// qualifiers, without looking through any typedefs that might add 596 /// qualifiers at a different level. 597 bool hasLocalQualifiers() const { 598 return getLocalFastQualifiers() || hasLocalNonFastQualifiers(); 599 } 600 601 /// \brief Determine whether this type has any qualifiers. 602 bool hasQualifiers() const; 603 604 /// \brief Determine whether this particular QualType instance has any 605 /// "non-fast" qualifiers, e.g., those that are stored in an ExtQualType 606 /// instance. 607 bool hasLocalNonFastQualifiers() const { 608 return Value.getPointer().is<const ExtQuals*>(); 609 } 610 611 /// \brief Retrieve the set of qualifiers local to this particular QualType 612 /// instance, not including any qualifiers acquired through typedefs or 613 /// other sugar. 614 Qualifiers getLocalQualifiers() const; 615 616 /// \brief Retrieve the set of qualifiers applied to this type. 617 Qualifiers getQualifiers() const; 618 619 /// \brief Retrieve the set of CVR (const-volatile-restrict) qualifiers 620 /// local to this particular QualType instance, not including any qualifiers 621 /// acquired through typedefs or other sugar. 622 unsigned getLocalCVRQualifiers() const { 623 return getLocalFastQualifiers(); 624 } 625 626 /// \brief Retrieve the set of CVR (const-volatile-restrict) qualifiers 627 /// applied to this type. 628 unsigned getCVRQualifiers() const; 629 630 bool isConstant(ASTContext& Ctx) const { 631 return QualType::isConstant(*this, Ctx); 632 } 633 634 /// \brief Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10). 635 bool isPODType(ASTContext &Context) const; 636 637 /// isCXX11PODType() - Return true if this is a POD type according to the 638 /// more relaxed rules of the C++11 standard, regardless of the current 639 /// compilation's language. 640 /// (C++0x [basic.types]p9) 641 bool isCXX11PODType(ASTContext &Context) const; 642 643 /// isTrivialType - Return true if this is a trivial type 644 /// (C++0x [basic.types]p9) 645 bool isTrivialType(ASTContext &Context) const; 646 647 /// isTriviallyCopyableType - Return true if this is a trivially 648 /// copyable type (C++0x [basic.types]p9) 649 bool isTriviallyCopyableType(ASTContext &Context) const; 650 651 // Don't promise in the API that anything besides 'const' can be 652 // easily added. 653 654 /// addConst - add the specified type qualifier to this QualType. 655 void addConst() { 656 addFastQualifiers(Qualifiers::Const); 657 } 658 QualType withConst() const { 659 return withFastQualifiers(Qualifiers::Const); 660 } 661 662 /// addVolatile - add the specified type qualifier to this QualType. 663 void addVolatile() { 664 addFastQualifiers(Qualifiers::Volatile); 665 } 666 QualType withVolatile() const { 667 return withFastQualifiers(Qualifiers::Volatile); 668 } 669 670 /// Add the restrict qualifier to this QualType. 671 void addRestrict() { 672 addFastQualifiers(Qualifiers::Restrict); 673 } 674 QualType withRestrict() const { 675 return withFastQualifiers(Qualifiers::Restrict); 676 } 677 678 QualType withCVRQualifiers(unsigned CVR) const { 679 return withFastQualifiers(CVR); 680 } 681 682 void addFastQualifiers(unsigned TQs) { 683 assert(!(TQs & ~Qualifiers::FastMask) 684 && "non-fast qualifier bits set in mask!"); 685 Value.setInt(Value.getInt() | TQs); 686 } 687 688 void removeLocalConst(); 689 void removeLocalVolatile(); 690 void removeLocalRestrict(); 691 void removeLocalCVRQualifiers(unsigned Mask); 692 693 void removeLocalFastQualifiers() { Value.setInt(0); } 694 void removeLocalFastQualifiers(unsigned Mask) { 695 assert(!(Mask & ~Qualifiers::FastMask) && "mask has non-fast qualifiers"); 696 Value.setInt(Value.getInt() & ~Mask); 697 } 698 699 // Creates a type with the given qualifiers in addition to any 700 // qualifiers already on this type. 701 QualType withFastQualifiers(unsigned TQs) const { 702 QualType T = *this; 703 T.addFastQualifiers(TQs); 704 return T; 705 } 706 707 // Creates a type with exactly the given fast qualifiers, removing 708 // any existing fast qualifiers. 709 QualType withExactLocalFastQualifiers(unsigned TQs) const { 710 return withoutLocalFastQualifiers().withFastQualifiers(TQs); 711 } 712 713 // Removes fast qualifiers, but leaves any extended qualifiers in place. 714 QualType withoutLocalFastQualifiers() const { 715 QualType T = *this; 716 T.removeLocalFastQualifiers(); 717 return T; 718 } 719 720 QualType getCanonicalType() const; 721 722 /// \brief Return this type with all of the instance-specific qualifiers 723 /// removed, but without removing any qualifiers that may have been applied 724 /// through typedefs. 725 QualType getLocalUnqualifiedType() const { return QualType(getTypePtr(), 0); } 726 727 /// \brief Retrieve the unqualified variant of the given type, 728 /// removing as little sugar as possible. 729 /// 730 /// This routine looks through various kinds of sugar to find the 731 /// least-desugared type that is unqualified. For example, given: 732 /// 733 /// \code 734 /// typedef int Integer; 735 /// typedef const Integer CInteger; 736 /// typedef CInteger DifferenceType; 737 /// \endcode 738 /// 739 /// Executing \c getUnqualifiedType() on the type \c DifferenceType will 740 /// desugar until we hit the type \c Integer, which has no qualifiers on it. 741 /// 742 /// The resulting type might still be qualified if it's an array 743 /// type. To strip qualifiers even from within an array type, use 744 /// ASTContext::getUnqualifiedArrayType. 745 inline QualType getUnqualifiedType() const; 746 747 /// getSplitUnqualifiedType - Retrieve the unqualified variant of the 748 /// given type, removing as little sugar as possible. 749 /// 750 /// Like getUnqualifiedType(), but also returns the set of 751 /// qualifiers that were built up. 752 /// 753 /// The resulting type might still be qualified if it's an array 754 /// type. To strip qualifiers even from within an array type, use 755 /// ASTContext::getUnqualifiedArrayType. 756 inline SplitQualType getSplitUnqualifiedType() const; 757 758 /// \brief Determine whether this type is more qualified than the other 759 /// given type, requiring exact equality for non-CVR qualifiers. 760 bool isMoreQualifiedThan(QualType Other) const; 761 762 /// \brief Determine whether this type is at least as qualified as the other 763 /// given type, requiring exact equality for non-CVR qualifiers. 764 bool isAtLeastAsQualifiedAs(QualType Other) const; 765 766 QualType getNonReferenceType() const; 767 768 /// \brief Determine the type of a (typically non-lvalue) expression with the 769 /// specified result type. 770 /// 771 /// This routine should be used for expressions for which the return type is 772 /// explicitly specified (e.g., in a cast or call) and isn't necessarily 773 /// an lvalue. It removes a top-level reference (since there are no 774 /// expressions of reference type) and deletes top-level cvr-qualifiers 775 /// from non-class types (in C++) or all types (in C). 776 QualType getNonLValueExprType(ASTContext &Context) const; 777 778 /// getDesugaredType - Return the specified type with any "sugar" removed from 779 /// the type. This takes off typedefs, typeof's etc. If the outer level of 780 /// the type is already concrete, it returns it unmodified. This is similar 781 /// to getting the canonical type, but it doesn't remove *all* typedefs. For 782 /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is 783 /// concrete. 784 /// 785 /// Qualifiers are left in place. 786 QualType getDesugaredType(const ASTContext &Context) const { 787 return getDesugaredType(*this, Context); 788 } 789 790 SplitQualType getSplitDesugaredType() const { 791 return getSplitDesugaredType(*this); 792 } 793 794 /// \brief Return the specified type with one level of "sugar" removed from 795 /// the type. 796 /// 797 /// This routine takes off the first typedef, typeof, etc. If the outer level 798 /// of the type is already concrete, it returns it unmodified. 799 QualType getSingleStepDesugaredType(const ASTContext &Context) const { 800 return getSingleStepDesugaredTypeImpl(*this, Context); 801 } 802 803 /// IgnoreParens - Returns the specified type after dropping any 804 /// outer-level parentheses. 805 QualType IgnoreParens() const { 806 if (isa<ParenType>(*this)) 807 return QualType::IgnoreParens(*this); 808 return *this; 809 } 810 811 /// operator==/!= - Indicate whether the specified types and qualifiers are 812 /// identical. 813 friend bool operator==(const QualType &LHS, const QualType &RHS) { 814 return LHS.Value == RHS.Value; 815 } 816 friend bool operator!=(const QualType &LHS, const QualType &RHS) { 817 return LHS.Value != RHS.Value; 818 } 819 std::string getAsString() const { 820 return getAsString(split()); 821 } 822 static std::string getAsString(SplitQualType split) { 823 return getAsString(split.Ty, split.Quals); 824 } 825 static std::string getAsString(const Type *ty, Qualifiers qs); 826 827 std::string getAsString(const PrintingPolicy &Policy) const { 828 std::string S; 829 getAsStringInternal(S, Policy); 830 return S; 831 } 832 void getAsStringInternal(std::string &Str, 833 const PrintingPolicy &Policy) const { 834 return getAsStringInternal(split(), Str, Policy); 835 } 836 static void getAsStringInternal(SplitQualType split, std::string &out, 837 const PrintingPolicy &policy) { 838 return getAsStringInternal(split.Ty, split.Quals, out, policy); 839 } 840 static void getAsStringInternal(const Type *ty, Qualifiers qs, 841 std::string &out, 842 const PrintingPolicy &policy); 843 844 void dump(const char *s) const; 845 void dump() const; 846 847 void Profile(llvm::FoldingSetNodeID &ID) const { 848 ID.AddPointer(getAsOpaquePtr()); 849 } 850 851 /// getAddressSpace - Return the address space of this type. 852 inline unsigned getAddressSpace() const; 853 854 /// getObjCGCAttr - Returns gc attribute of this type. 855 inline Qualifiers::GC getObjCGCAttr() const; 856 857 /// isObjCGCWeak true when Type is objc's weak. 858 bool isObjCGCWeak() const { 859 return getObjCGCAttr() == Qualifiers::Weak; 860 } 861 862 /// isObjCGCStrong true when Type is objc's strong. 863 bool isObjCGCStrong() const { 864 return getObjCGCAttr() == Qualifiers::Strong; 865 } 866 867 /// getObjCLifetime - Returns lifetime attribute of this type. 868 Qualifiers::ObjCLifetime getObjCLifetime() const { 869 return getQualifiers().getObjCLifetime(); 870 } 871 872 bool hasNonTrivialObjCLifetime() const { 873 return getQualifiers().hasNonTrivialObjCLifetime(); 874 } 875 876 bool hasStrongOrWeakObjCLifetime() const { 877 return getQualifiers().hasStrongOrWeakObjCLifetime(); 878 } 879 880 enum DestructionKind { 881 DK_none, 882 DK_cxx_destructor, 883 DK_objc_strong_lifetime, 884 DK_objc_weak_lifetime 885 }; 886 887 /// isDestructedType - nonzero if objects of this type require 888 /// non-trivial work to clean up after. Non-zero because it's 889 /// conceivable that qualifiers (objc_gc(weak)?) could make 890 /// something require destruction. 891 DestructionKind isDestructedType() const { 892 return isDestructedTypeImpl(*this); 893 } 894 895 /// \brief Determine whether expressions of the given type are forbidden 896 /// from being lvalues in C. 897 /// 898 /// The expression types that are forbidden to be lvalues are: 899 /// - 'void', but not qualified void 900 /// - function types 901 /// 902 /// The exact rule here is C99 6.3.2.1: 903 /// An lvalue is an expression with an object type or an incomplete 904 /// type other than void. 905 bool isCForbiddenLValueType() const; 906 907 /// \brief Determine whether this type has trivial copy/move-assignment 908 /// semantics. 909 bool hasTrivialAssignment(ASTContext &Context, bool Copying) const; 910 911 private: 912 // These methods are implemented in a separate translation unit; 913 // "static"-ize them to avoid creating temporary QualTypes in the 914 // caller. 915 static bool isConstant(QualType T, ASTContext& Ctx); 916 static QualType getDesugaredType(QualType T, const ASTContext &Context); 917 static SplitQualType getSplitDesugaredType(QualType T); 918 static SplitQualType getSplitUnqualifiedTypeImpl(QualType type); 919 static QualType getSingleStepDesugaredTypeImpl(QualType type, 920 const ASTContext &C); 921 static QualType IgnoreParens(QualType T); 922 static DestructionKind isDestructedTypeImpl(QualType type); 923 }; 924 925 } // end clang. 926 927 namespace llvm { 928 /// Implement simplify_type for QualType, so that we can dyn_cast from QualType 929 /// to a specific Type class. 930 template<> struct simplify_type<const ::clang::QualType> { 931 typedef const ::clang::Type *SimpleType; 932 static SimpleType getSimplifiedValue(const ::clang::QualType &Val) { 933 return Val.getTypePtr(); 934 } 935 }; 936 template<> struct simplify_type< ::clang::QualType> 937 : public simplify_type<const ::clang::QualType> {}; 938 939 // Teach SmallPtrSet that QualType is "basically a pointer". 940 template<> 941 class PointerLikeTypeTraits<clang::QualType> { 942 public: 943 static inline void *getAsVoidPointer(clang::QualType P) { 944 return P.getAsOpaquePtr(); 945 } 946 static inline clang::QualType getFromVoidPointer(void *P) { 947 return clang::QualType::getFromOpaquePtr(P); 948 } 949 // Various qualifiers go in low bits. 950 enum { NumLowBitsAvailable = 0 }; 951 }; 952 953 } // end namespace llvm 954 955 namespace clang { 956 957 /// \brief Base class that is common to both the \c ExtQuals and \c Type 958 /// classes, which allows \c QualType to access the common fields between the 959 /// two. 960 /// 961 class ExtQualsTypeCommonBase { 962 ExtQualsTypeCommonBase(const Type *baseType, QualType canon) 963 : BaseType(baseType), CanonicalType(canon) {} 964 965 /// \brief The "base" type of an extended qualifiers type (\c ExtQuals) or 966 /// a self-referential pointer (for \c Type). 967 /// 968 /// This pointer allows an efficient mapping from a QualType to its 969 /// underlying type pointer. 970 const Type *const BaseType; 971 972 /// \brief The canonical type of this type. A QualType. 973 QualType CanonicalType; 974 975 friend class QualType; 976 friend class Type; 977 friend class ExtQuals; 978 }; 979 980 /// ExtQuals - We can encode up to four bits in the low bits of a 981 /// type pointer, but there are many more type qualifiers that we want 982 /// to be able to apply to an arbitrary type. Therefore we have this 983 /// struct, intended to be heap-allocated and used by QualType to 984 /// store qualifiers. 985 /// 986 /// The current design tags the 'const', 'restrict', and 'volatile' qualifiers 987 /// in three low bits on the QualType pointer; a fourth bit records whether 988 /// the pointer is an ExtQuals node. The extended qualifiers (address spaces, 989 /// Objective-C GC attributes) are much more rare. 990 class ExtQuals : public ExtQualsTypeCommonBase, public llvm::FoldingSetNode { 991 // NOTE: changing the fast qualifiers should be straightforward as 992 // long as you don't make 'const' non-fast. 993 // 1. Qualifiers: 994 // a) Modify the bitmasks (Qualifiers::TQ and DeclSpec::TQ). 995 // Fast qualifiers must occupy the low-order bits. 996 // b) Update Qualifiers::FastWidth and FastMask. 997 // 2. QualType: 998 // a) Update is{Volatile,Restrict}Qualified(), defined inline. 999 // b) Update remove{Volatile,Restrict}, defined near the end of 1000 // this header. 1001 // 3. ASTContext: 1002 // a) Update get{Volatile,Restrict}Type. 1003 1004 /// Quals - the immutable set of qualifiers applied by this 1005 /// node; always contains extended qualifiers. 1006 Qualifiers Quals; 1007 1008 ExtQuals *this_() { return this; } 1009 1010 public: 1011 ExtQuals(const Type *baseType, QualType canon, Qualifiers quals) 1012 : ExtQualsTypeCommonBase(baseType, 1013 canon.isNull() ? QualType(this_(), 0) : canon), 1014 Quals(quals) 1015 { 1016 assert(Quals.hasNonFastQualifiers() 1017 && "ExtQuals created with no fast qualifiers"); 1018 assert(!Quals.hasFastQualifiers() 1019 && "ExtQuals created with fast qualifiers"); 1020 } 1021 1022 Qualifiers getQualifiers() const { return Quals; } 1023 1024 bool hasObjCGCAttr() const { return Quals.hasObjCGCAttr(); } 1025 Qualifiers::GC getObjCGCAttr() const { return Quals.getObjCGCAttr(); } 1026 1027 bool hasObjCLifetime() const { return Quals.hasObjCLifetime(); } 1028 Qualifiers::ObjCLifetime getObjCLifetime() const { 1029 return Quals.getObjCLifetime(); 1030 } 1031 1032 bool hasAddressSpace() const { return Quals.hasAddressSpace(); } 1033 unsigned getAddressSpace() const { return Quals.getAddressSpace(); } 1034 1035 const Type *getBaseType() const { return BaseType; } 1036 1037 public: 1038 void Profile(llvm::FoldingSetNodeID &ID) const { 1039 Profile(ID, getBaseType(), Quals); 1040 } 1041 static void Profile(llvm::FoldingSetNodeID &ID, 1042 const Type *BaseType, 1043 Qualifiers Quals) { 1044 assert(!Quals.hasFastQualifiers() && "fast qualifiers in ExtQuals hash!"); 1045 ID.AddPointer(BaseType); 1046 Quals.Profile(ID); 1047 } 1048 }; 1049 1050 /// \brief The kind of C++0x ref-qualifier associated with a function type, 1051 /// which determines whether a member function's "this" object can be an 1052 /// lvalue, rvalue, or neither. 1053 enum RefQualifierKind { 1054 /// \brief No ref-qualifier was provided. 1055 RQ_None = 0, 1056 /// \brief An lvalue ref-qualifier was provided (\c &). 1057 RQ_LValue, 1058 /// \brief An rvalue ref-qualifier was provided (\c &&). 1059 RQ_RValue 1060 }; 1061 1062 /// Type - This is the base class of the type hierarchy. A central concept 1063 /// with types is that each type always has a canonical type. A canonical type 1064 /// is the type with any typedef names stripped out of it or the types it 1065 /// references. For example, consider: 1066 /// 1067 /// typedef int foo; 1068 /// typedef foo* bar; 1069 /// 'int *' 'foo *' 'bar' 1070 /// 1071 /// There will be a Type object created for 'int'. Since int is canonical, its 1072 /// canonicaltype pointer points to itself. There is also a Type for 'foo' (a 1073 /// TypedefType). Its CanonicalType pointer points to the 'int' Type. Next 1074 /// there is a PointerType that represents 'int*', which, like 'int', is 1075 /// canonical. Finally, there is a PointerType type for 'foo*' whose canonical 1076 /// type is 'int*', and there is a TypedefType for 'bar', whose canonical type 1077 /// is also 'int*'. 1078 /// 1079 /// Non-canonical types are useful for emitting diagnostics, without losing 1080 /// information about typedefs being used. Canonical types are useful for type 1081 /// comparisons (they allow by-pointer equality tests) and useful for reasoning 1082 /// about whether something has a particular form (e.g. is a function type), 1083 /// because they implicitly, recursively, strip all typedefs out of a type. 1084 /// 1085 /// Types, once created, are immutable. 1086 /// 1087 class Type : public ExtQualsTypeCommonBase { 1088 public: 1089 enum TypeClass { 1090 #define TYPE(Class, Base) Class, 1091 #define LAST_TYPE(Class) TypeLast = Class, 1092 #define ABSTRACT_TYPE(Class, Base) 1093 #include "clang/AST/TypeNodes.def" 1094 TagFirst = Record, TagLast = Enum 1095 }; 1096 1097 private: 1098 Type(const Type&); // DO NOT IMPLEMENT. 1099 void operator=(const Type&); // DO NOT IMPLEMENT. 1100 1101 /// Bitfields required by the Type class. 1102 class TypeBitfields { 1103 friend class Type; 1104 template <class T> friend class TypePropertyCache; 1105 1106 /// TypeClass bitfield - Enum that specifies what subclass this belongs to. 1107 unsigned TC : 8; 1108 1109 /// Dependent - Whether this type is a dependent type (C++ [temp.dep.type]). 1110 /// Note that this should stay at the end of the ivars for Type so that 1111 /// subclasses can pack their bitfields into the same word. 1112 unsigned Dependent : 1; 1113 1114 /// \brief Whether this type somehow involves a template parameter, even 1115 /// if the resolution of the type does not depend on a template parameter. 1116 unsigned InstantiationDependent : 1; 1117 1118 /// \brief Whether this type is a variably-modified type (C99 6.7.5). 1119 unsigned VariablyModified : 1; 1120 1121 /// \brief Whether this type contains an unexpanded parameter pack 1122 /// (for C++0x variadic templates). 1123 unsigned ContainsUnexpandedParameterPack : 1; 1124 1125 /// \brief Nonzero if the cache (i.e. the bitfields here starting 1126 /// with 'Cache') is valid. If so, then this is a 1127 /// LangOptions::VisibilityMode+1. 1128 mutable unsigned CacheValidAndVisibility : 2; 1129 1130 /// \brief True if the visibility was set explicitly in the source code. 1131 mutable unsigned CachedExplicitVisibility : 1; 1132 1133 /// \brief Linkage of this type. 1134 mutable unsigned CachedLinkage : 2; 1135 1136 /// \brief Whether this type involves and local or unnamed types. 1137 mutable unsigned CachedLocalOrUnnamed : 1; 1138 1139 /// \brief FromAST - Whether this type comes from an AST file. 1140 mutable unsigned FromAST : 1; 1141 1142 bool isCacheValid() const { 1143 return (CacheValidAndVisibility != 0); 1144 } 1145 Visibility getVisibility() const { 1146 assert(isCacheValid() && "getting linkage from invalid cache"); 1147 return static_cast<Visibility>(CacheValidAndVisibility-1); 1148 } 1149 bool isVisibilityExplicit() const { 1150 assert(isCacheValid() && "getting linkage from invalid cache"); 1151 return CachedExplicitVisibility; 1152 } 1153 Linkage getLinkage() const { 1154 assert(isCacheValid() && "getting linkage from invalid cache"); 1155 return static_cast<Linkage>(CachedLinkage); 1156 } 1157 bool hasLocalOrUnnamedType() const { 1158 assert(isCacheValid() && "getting linkage from invalid cache"); 1159 return CachedLocalOrUnnamed; 1160 } 1161 }; 1162 enum { NumTypeBits = 19 }; 1163 1164 protected: 1165 // These classes allow subclasses to somewhat cleanly pack bitfields 1166 // into Type. 1167 1168 class ArrayTypeBitfields { 1169 friend class ArrayType; 1170 1171 unsigned : NumTypeBits; 1172 1173 /// IndexTypeQuals - CVR qualifiers from declarations like 1174 /// 'int X[static restrict 4]'. For function parameters only. 1175 unsigned IndexTypeQuals : 3; 1176 1177 /// SizeModifier - storage class qualifiers from declarations like 1178 /// 'int X[static restrict 4]'. For function parameters only. 1179 /// Actually an ArrayType::ArraySizeModifier. 1180 unsigned SizeModifier : 3; 1181 }; 1182 1183 class BuiltinTypeBitfields { 1184 friend class BuiltinType; 1185 1186 unsigned : NumTypeBits; 1187 1188 /// The kind (BuiltinType::Kind) of builtin type this is. 1189 unsigned Kind : 8; 1190 }; 1191 1192 class FunctionTypeBitfields { 1193 friend class FunctionType; 1194 1195 unsigned : NumTypeBits; 1196 1197 /// Extra information which affects how the function is called, like 1198 /// regparm and the calling convention. 1199 unsigned ExtInfo : 8; 1200 1201 /// TypeQuals - Used only by FunctionProtoType, put here to pack with the 1202 /// other bitfields. 1203 /// The qualifiers are part of FunctionProtoType because... 1204 /// 1205 /// C++ 8.3.5p4: The return type, the parameter type list and the 1206 /// cv-qualifier-seq, [...], are part of the function type. 1207 unsigned TypeQuals : 3; 1208 1209 /// \brief The ref-qualifier associated with a \c FunctionProtoType. 1210 /// 1211 /// This is a value of type \c RefQualifierKind. 1212 unsigned RefQualifier : 2; 1213 }; 1214 1215 class ObjCObjectTypeBitfields { 1216 friend class ObjCObjectType; 1217 1218 unsigned : NumTypeBits; 1219 1220 /// NumProtocols - The number of protocols stored directly on this 1221 /// object type. 1222 unsigned NumProtocols : 32 - NumTypeBits; 1223 }; 1224 1225 class ReferenceTypeBitfields { 1226 friend class ReferenceType; 1227 1228 unsigned : NumTypeBits; 1229 1230 /// True if the type was originally spelled with an lvalue sigil. 1231 /// This is never true of rvalue references but can also be false 1232 /// on lvalue references because of C++0x [dcl.typedef]p9, 1233 /// as follows: 1234 /// 1235 /// typedef int &ref; // lvalue, spelled lvalue 1236 /// typedef int &&rvref; // rvalue 1237 /// ref &a; // lvalue, inner ref, spelled lvalue 1238 /// ref &&a; // lvalue, inner ref 1239 /// rvref &a; // lvalue, inner ref, spelled lvalue 1240 /// rvref &&a; // rvalue, inner ref 1241 unsigned SpelledAsLValue : 1; 1242 1243 /// True if the inner type is a reference type. This only happens 1244 /// in non-canonical forms. 1245 unsigned InnerRef : 1; 1246 }; 1247 1248 class TypeWithKeywordBitfields { 1249 friend class TypeWithKeyword; 1250 1251 unsigned : NumTypeBits; 1252 1253 /// An ElaboratedTypeKeyword. 8 bits for efficient access. 1254 unsigned Keyword : 8; 1255 }; 1256 1257 class VectorTypeBitfields { 1258 friend class VectorType; 1259 1260 unsigned : NumTypeBits; 1261 1262 /// VecKind - The kind of vector, either a generic vector type or some 1263 /// target-specific vector type such as for AltiVec or Neon. 1264 unsigned VecKind : 3; 1265 1266 /// NumElements - The number of elements in the vector. 1267 unsigned NumElements : 29 - NumTypeBits; 1268 }; 1269 1270 class AttributedTypeBitfields { 1271 friend class AttributedType; 1272 1273 unsigned : NumTypeBits; 1274 1275 /// AttrKind - an AttributedType::Kind 1276 unsigned AttrKind : 32 - NumTypeBits; 1277 }; 1278 1279 union { 1280 TypeBitfields TypeBits; 1281 ArrayTypeBitfields ArrayTypeBits; 1282 AttributedTypeBitfields AttributedTypeBits; 1283 BuiltinTypeBitfields BuiltinTypeBits; 1284 FunctionTypeBitfields FunctionTypeBits; 1285 ObjCObjectTypeBitfields ObjCObjectTypeBits; 1286 ReferenceTypeBitfields ReferenceTypeBits; 1287 TypeWithKeywordBitfields TypeWithKeywordBits; 1288 VectorTypeBitfields VectorTypeBits; 1289 }; 1290 1291 private: 1292 /// \brief Set whether this type comes from an AST file. 1293 void setFromAST(bool V = true) const { 1294 TypeBits.FromAST = V; 1295 } 1296 1297 template <class T> friend class TypePropertyCache; 1298 1299 protected: 1300 // silence VC++ warning C4355: 'this' : used in base member initializer list 1301 Type *this_() { return this; } 1302 Type(TypeClass tc, QualType canon, bool Dependent, 1303 bool InstantiationDependent, bool VariablyModified, 1304 bool ContainsUnexpandedParameterPack) 1305 : ExtQualsTypeCommonBase(this, 1306 canon.isNull() ? QualType(this_(), 0) : canon) { 1307 TypeBits.TC = tc; 1308 TypeBits.Dependent = Dependent; 1309 TypeBits.InstantiationDependent = Dependent || InstantiationDependent; 1310 TypeBits.VariablyModified = VariablyModified; 1311 TypeBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack; 1312 TypeBits.CacheValidAndVisibility = 0; 1313 TypeBits.CachedExplicitVisibility = false; 1314 TypeBits.CachedLocalOrUnnamed = false; 1315 TypeBits.CachedLinkage = NoLinkage; 1316 TypeBits.FromAST = false; 1317 } 1318 friend class ASTContext; 1319 1320 void setDependent(bool D = true) { 1321 TypeBits.Dependent = D; 1322 if (D) 1323 TypeBits.InstantiationDependent = true; 1324 } 1325 void setInstantiationDependent(bool D = true) { 1326 TypeBits.InstantiationDependent = D; } 1327 void setVariablyModified(bool VM = true) { TypeBits.VariablyModified = VM; 1328 } 1329 void setContainsUnexpandedParameterPack(bool PP = true) { 1330 TypeBits.ContainsUnexpandedParameterPack = PP; 1331 } 1332 1333 public: 1334 TypeClass getTypeClass() const { return static_cast<TypeClass>(TypeBits.TC); } 1335 1336 /// \brief Whether this type comes from an AST file. 1337 bool isFromAST() const { return TypeBits.FromAST; } 1338 1339 /// \brief Whether this type is or contains an unexpanded parameter 1340 /// pack, used to support C++0x variadic templates. 1341 /// 1342 /// A type that contains a parameter pack shall be expanded by the 1343 /// ellipsis operator at some point. For example, the typedef in the 1344 /// following example contains an unexpanded parameter pack 'T': 1345 /// 1346 /// \code 1347 /// template<typename ...T> 1348 /// struct X { 1349 /// typedef T* pointer_types; // ill-formed; T is a parameter pack. 1350 /// }; 1351 /// \endcode 1352 /// 1353 /// Note that this routine does not specify which 1354 bool containsUnexpandedParameterPack() const { 1355 return TypeBits.ContainsUnexpandedParameterPack; 1356 } 1357 1358 /// Determines if this type would be canonical if it had no further 1359 /// qualification. 1360 bool isCanonicalUnqualified() const { 1361 return CanonicalType == QualType(this, 0); 1362 } 1363 1364 /// Pull a single level of sugar off of this locally-unqualified type. 1365 /// Users should generally prefer SplitQualType::getSingleStepDesugaredType() 1366 /// or QualType::getSingleStepDesugaredType(const ASTContext&). 1367 QualType getLocallyUnqualifiedSingleStepDesugaredType() const; 1368 1369 /// Types are partitioned into 3 broad categories (C99 6.2.5p1): 1370 /// object types, function types, and incomplete types. 1371 1372 /// isIncompleteType - Return true if this is an incomplete type. 1373 /// A type that can describe objects, but which lacks information needed to 1374 /// determine its size (e.g. void, or a fwd declared struct). Clients of this 1375 /// routine will need to determine if the size is actually required. 1376 /// 1377 /// \brief Def If non-NULL, and the type refers to some kind of declaration 1378 /// that can be completed (such as a C struct, C++ class, or Objective-C 1379 /// class), will be set to the declaration. 1380 bool isIncompleteType(NamedDecl **Def = 0) const; 1381 1382 /// isIncompleteOrObjectType - Return true if this is an incomplete or object 1383 /// type, in other words, not a function type. 1384 bool isIncompleteOrObjectType() const { 1385 return !isFunctionType(); 1386 } 1387 1388 /// \brief Determine whether this type is an object type. 1389 bool isObjectType() const { 1390 // C++ [basic.types]p8: 1391 // An object type is a (possibly cv-qualified) type that is not a 1392 // function type, not a reference type, and not a void type. 1393 return !isReferenceType() && !isFunctionType() && !isVoidType(); 1394 } 1395 1396 /// isLiteralType - Return true if this is a literal type 1397 /// (C++0x [basic.types]p10) 1398 bool isLiteralType() const; 1399 1400 /// \brief Test if this type is a standard-layout type. 1401 /// (C++0x [basic.type]p9) 1402 bool isStandardLayoutType() const; 1403 1404 /// Helper methods to distinguish type categories. All type predicates 1405 /// operate on the canonical type, ignoring typedefs and qualifiers. 1406 1407 /// isBuiltinType - returns true if the type is a builtin type. 1408 bool isBuiltinType() const; 1409 1410 /// isSpecificBuiltinType - Test for a particular builtin type. 1411 bool isSpecificBuiltinType(unsigned K) const; 1412 1413 /// isPlaceholderType - Test for a type which does not represent an 1414 /// actual type-system type but is instead used as a placeholder for 1415 /// various convenient purposes within Clang. All such types are 1416 /// BuiltinTypes. 1417 bool isPlaceholderType() const; 1418 const BuiltinType *getAsPlaceholderType() const; 1419 1420 /// isSpecificPlaceholderType - Test for a specific placeholder type. 1421 bool isSpecificPlaceholderType(unsigned K) const; 1422 1423 /// isNonOverloadPlaceholderType - Test for a placeholder type 1424 /// other than Overload; see BuiltinType::isNonOverloadPlaceholderType. 1425 bool isNonOverloadPlaceholderType() const; 1426 1427 /// isIntegerType() does *not* include complex integers (a GCC extension). 1428 /// isComplexIntegerType() can be used to test for complex integers. 1429 bool isIntegerType() const; // C99 6.2.5p17 (int, char, bool, enum) 1430 bool isEnumeralType() const; 1431 bool isBooleanType() const; 1432 bool isCharType() const; 1433 bool isWideCharType() const; 1434 bool isChar16Type() const; 1435 bool isChar32Type() const; 1436 bool isAnyCharacterType() const; 1437 bool isIntegralType(ASTContext &Ctx) const; 1438 1439 /// \brief Determine whether this type is an integral or enumeration type. 1440 bool isIntegralOrEnumerationType() const; 1441 /// \brief Determine whether this type is an integral or unscoped enumeration 1442 /// type. 1443 bool isIntegralOrUnscopedEnumerationType() const; 1444 1445 /// Floating point categories. 1446 bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double) 1447 /// isComplexType() does *not* include complex integers (a GCC extension). 1448 /// isComplexIntegerType() can be used to test for complex integers. 1449 bool isComplexType() const; // C99 6.2.5p11 (complex) 1450 bool isAnyComplexType() const; // C99 6.2.5p11 (complex) + Complex Int. 1451 bool isFloatingType() const; // C99 6.2.5p11 (real floating + complex) 1452 bool isHalfType() const; // OpenCL 6.1.1.1, NEON (IEEE 754-2008 half) 1453 bool isRealType() const; // C99 6.2.5p17 (real floating + integer) 1454 bool isArithmeticType() const; // C99 6.2.5p18 (integer + floating) 1455 bool isVoidType() const; // C99 6.2.5p19 1456 bool isDerivedType() const; // C99 6.2.5p20 1457 bool isScalarType() const; // C99 6.2.5p21 (arithmetic + pointers) 1458 bool isAggregateType() const; 1459 bool isFundamentalType() const; 1460 bool isCompoundType() const; 1461 1462 // Type Predicates: Check to see if this type is structurally the specified 1463 // type, ignoring typedefs and qualifiers. 1464 bool isFunctionType() const; 1465 bool isFunctionNoProtoType() const { return getAs<FunctionNoProtoType>(); } 1466 bool isFunctionProtoType() const { return getAs<FunctionProtoType>(); } 1467 bool isPointerType() const; 1468 bool isAnyPointerType() const; // Any C pointer or ObjC object pointer 1469 bool isBlockPointerType() const; 1470 bool isVoidPointerType() const; 1471 bool isReferenceType() const; 1472 bool isLValueReferenceType() const; 1473 bool isRValueReferenceType() const; 1474 bool isFunctionPointerType() const; 1475 bool isMemberPointerType() const; 1476 bool isMemberFunctionPointerType() const; 1477 bool isMemberDataPointerType() const; 1478 bool isArrayType() const; 1479 bool isConstantArrayType() const; 1480 bool isIncompleteArrayType() const; 1481 bool isVariableArrayType() const; 1482 bool isDependentSizedArrayType() const; 1483 bool isRecordType() const; 1484 bool isClassType() const; 1485 bool isStructureType() const; 1486 bool isStructureOrClassType() const; 1487 bool isUnionType() const; 1488 bool isComplexIntegerType() const; // GCC _Complex integer type. 1489 bool isVectorType() const; // GCC vector type. 1490 bool isExtVectorType() const; // Extended vector type. 1491 bool isObjCObjectPointerType() const; // pointer to ObjC object 1492 bool isObjCRetainableType() const; // ObjC object or block pointer 1493 bool isObjCLifetimeType() const; // (array of)* retainable type 1494 bool isObjCIndirectLifetimeType() const; // (pointer to)* lifetime type 1495 bool isObjCNSObjectType() const; // __attribute__((NSObject)) 1496 // FIXME: change this to 'raw' interface type, so we can used 'interface' type 1497 // for the common case. 1498 bool isObjCObjectType() const; // NSString or typeof(*(id)0) 1499 bool isObjCQualifiedInterfaceType() const; // NSString<foo> 1500 bool isObjCQualifiedIdType() const; // id<foo> 1501 bool isObjCQualifiedClassType() const; // Class<foo> 1502 bool isObjCObjectOrInterfaceType() const; 1503 bool isObjCIdType() const; // id 1504 bool isObjCClassType() const; // Class 1505 bool isObjCSelType() const; // Class 1506 bool isObjCBuiltinType() const; // 'id' or 'Class' 1507 bool isObjCARCBridgableType() const; 1508 bool isCARCBridgableType() const; 1509 bool isTemplateTypeParmType() const; // C++ template type parameter 1510 bool isNullPtrType() const; // C++0x nullptr_t 1511 bool isAtomicType() const; // C11 _Atomic() 1512 1513 /// Determines if this type, which must satisfy 1514 /// isObjCLifetimeType(), is implicitly __unsafe_unretained rather 1515 /// than implicitly __strong. 1516 bool isObjCARCImplicitlyUnretainedType() const; 1517 1518 /// Return the implicit lifetime for this type, which must not be dependent. 1519 Qualifiers::ObjCLifetime getObjCARCImplicitLifetime() const; 1520 1521 enum ScalarTypeKind { 1522 STK_CPointer, 1523 STK_BlockPointer, 1524 STK_ObjCObjectPointer, 1525 STK_MemberPointer, 1526 STK_Bool, 1527 STK_Integral, 1528 STK_Floating, 1529 STK_IntegralComplex, 1530 STK_FloatingComplex 1531 }; 1532 /// getScalarTypeKind - Given that this is a scalar type, classify it. 1533 ScalarTypeKind getScalarTypeKind() const; 1534 1535 /// isDependentType - Whether this type is a dependent type, meaning 1536 /// that its definition somehow depends on a template parameter 1537 /// (C++ [temp.dep.type]). 1538 bool isDependentType() const { return TypeBits.Dependent; } 1539 1540 /// \brief Determine whether this type is an instantiation-dependent type, 1541 /// meaning that the type involves a template parameter (even if the 1542 /// definition does not actually depend on the type substituted for that 1543 /// template parameter). 1544 bool isInstantiationDependentType() const { 1545 return TypeBits.InstantiationDependent; 1546 } 1547 1548 /// \brief Whether this type is a variably-modified type (C99 6.7.5). 1549 bool isVariablyModifiedType() const { return TypeBits.VariablyModified; } 1550 1551 /// \brief Whether this type involves a variable-length array type 1552 /// with a definite size. 1553 bool hasSizedVLAType() const; 1554 1555 /// \brief Whether this type is or contains a local or unnamed type. 1556 bool hasUnnamedOrLocalType() const; 1557 1558 bool isOverloadableType() const; 1559 1560 /// \brief Determine wither this type is a C++ elaborated-type-specifier. 1561 bool isElaboratedTypeSpecifier() const; 1562 1563 bool canDecayToPointerType() const; 1564 1565 /// hasPointerRepresentation - Whether this type is represented 1566 /// natively as a pointer; this includes pointers, references, block 1567 /// pointers, and Objective-C interface, qualified id, and qualified 1568 /// interface types, as well as nullptr_t. 1569 bool hasPointerRepresentation() const; 1570 1571 /// hasObjCPointerRepresentation - Whether this type can represent 1572 /// an objective pointer type for the purpose of GC'ability 1573 bool hasObjCPointerRepresentation() const; 1574 1575 /// \brief Determine whether this type has an integer representation 1576 /// of some sort, e.g., it is an integer type or a vector. 1577 bool hasIntegerRepresentation() const; 1578 1579 /// \brief Determine whether this type has an signed integer representation 1580 /// of some sort, e.g., it is an signed integer type or a vector. 1581 bool hasSignedIntegerRepresentation() const; 1582 1583 /// \brief Determine whether this type has an unsigned integer representation 1584 /// of some sort, e.g., it is an unsigned integer type or a vector. 1585 bool hasUnsignedIntegerRepresentation() const; 1586 1587 /// \brief Determine whether this type has a floating-point representation 1588 /// of some sort, e.g., it is a floating-point type or a vector thereof. 1589 bool hasFloatingRepresentation() const; 1590 1591 // Type Checking Functions: Check to see if this type is structurally the 1592 // specified type, ignoring typedefs and qualifiers, and return a pointer to 1593 // the best type we can. 1594 const RecordType *getAsStructureType() const; 1595 /// NOTE: getAs*ArrayType are methods on ASTContext. 1596 const RecordType *getAsUnionType() const; 1597 const ComplexType *getAsComplexIntegerType() const; // GCC complex int type. 1598 // The following is a convenience method that returns an ObjCObjectPointerType 1599 // for object declared using an interface. 1600 const ObjCObjectPointerType *getAsObjCInterfacePointerType() const; 1601 const ObjCObjectPointerType *getAsObjCQualifiedIdType() const; 1602 const ObjCObjectPointerType *getAsObjCQualifiedClassType() const; 1603 const ObjCObjectType *getAsObjCQualifiedInterfaceType() const; 1604 const CXXRecordDecl *getCXXRecordDeclForPointerType() const; 1605 1606 /// \brief Retrieves the CXXRecordDecl that this type refers to, either 1607 /// because the type is a RecordType or because it is the injected-class-name 1608 /// type of a class template or class template partial specialization. 1609 CXXRecordDecl *getAsCXXRecordDecl() const; 1610 1611 /// \brief Get the AutoType whose type will be deduced for a variable with 1612 /// an initializer of this type. This looks through declarators like pointer 1613 /// types, but not through decltype or typedefs. 1614 AutoType *getContainedAutoType() const; 1615 1616 /// Member-template getAs<specific type>'. Look through sugar for 1617 /// an instance of <specific type>. This scheme will eventually 1618 /// replace the specific getAsXXXX methods above. 1619 /// 1620 /// There are some specializations of this member template listed 1621 /// immediately following this class. 1622 template <typename T> const T *getAs() const; 1623 1624 /// A variant of getAs<> for array types which silently discards 1625 /// qualifiers from the outermost type. 1626 const ArrayType *getAsArrayTypeUnsafe() const; 1627 1628 /// Member-template castAs<specific type>. Look through sugar for 1629 /// the underlying instance of <specific type>. 1630 /// 1631 /// This method has the same relationship to getAs<T> as cast<T> has 1632 /// to dyn_cast<T>; which is to say, the underlying type *must* 1633 /// have the intended type, and this method will never return null. 1634 template <typename T> const T *castAs() const; 1635 1636 /// A variant of castAs<> for array type which silently discards 1637 /// qualifiers from the outermost type. 1638 const ArrayType *castAsArrayTypeUnsafe() const; 1639 1640 /// getBaseElementTypeUnsafe - Get the base element type of this 1641 /// type, potentially discarding type qualifiers. This method 1642 /// should never be used when type qualifiers are meaningful. 1643 const Type *getBaseElementTypeUnsafe() const; 1644 1645 /// getArrayElementTypeNoTypeQual - If this is an array type, return the 1646 /// element type of the array, potentially with type qualifiers missing. 1647 /// This method should never be used when type qualifiers are meaningful. 1648 const Type *getArrayElementTypeNoTypeQual() const; 1649 1650 /// getPointeeType - If this is a pointer, ObjC object pointer, or block 1651 /// pointer, this returns the respective pointee. 1652 QualType getPointeeType() const; 1653 1654 /// getUnqualifiedDesugaredType() - Return the specified type with 1655 /// any "sugar" removed from the type, removing any typedefs, 1656 /// typeofs, etc., as well as any qualifiers. 1657 const Type *getUnqualifiedDesugaredType() const; 1658 1659 /// More type predicates useful for type checking/promotion 1660 bool isPromotableIntegerType() const; // C99 6.3.1.1p2 1661 1662 /// isSignedIntegerType - Return true if this is an integer type that is 1663 /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], 1664 /// or an enum decl which has a signed representation. 1665 bool isSignedIntegerType() const; 1666 1667 /// isUnsignedIntegerType - Return true if this is an integer type that is 1668 /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], 1669 /// or an enum decl which has an unsigned representation. 1670 bool isUnsignedIntegerType() const; 1671 1672 /// Determines whether this is an integer type that is signed or an 1673 /// enumeration types whose underlying type is a signed integer type. 1674 bool isSignedIntegerOrEnumerationType() const; 1675 1676 /// Determines whether this is an integer type that is unsigned or an 1677 /// enumeration types whose underlying type is a unsigned integer type. 1678 bool isUnsignedIntegerOrEnumerationType() const; 1679 1680 /// isConstantSizeType - Return true if this is not a variable sized type, 1681 /// according to the rules of C99 6.7.5p3. It is not legal to call this on 1682 /// incomplete types. 1683 bool isConstantSizeType() const; 1684 1685 /// isSpecifierType - Returns true if this type can be represented by some 1686 /// set of type specifiers. 1687 bool isSpecifierType() const; 1688 1689 /// \brief Determine the linkage of this type. 1690 Linkage getLinkage() const; 1691 1692 /// \brief Determine the visibility of this type. 1693 Visibility getVisibility() const; 1694 1695 /// \brief Return true if the visibility was explicitly set is the code. 1696 bool isVisibilityExplicit() const; 1697 1698 /// \brief Determine the linkage and visibility of this type. 1699 std::pair<Linkage,Visibility> getLinkageAndVisibility() const; 1700 1701 /// \brief Note that the linkage is no longer known. 1702 void ClearLinkageCache(); 1703 1704 const char *getTypeClassName() const; 1705 1706 QualType getCanonicalTypeInternal() const { 1707 return CanonicalType; 1708 } 1709 CanQualType getCanonicalTypeUnqualified() const; // in CanonicalType.h 1710 LLVM_ATTRIBUTE_USED void dump() const; 1711 1712 static bool classof(const Type *) { return true; } 1713 1714 friend class ASTReader; 1715 friend class ASTWriter; 1716 }; 1717 1718 template <> inline const TypedefType *Type::getAs() const { 1719 return dyn_cast<TypedefType>(this); 1720 } 1721 1722 // We can do canonical leaf types faster, because we don't have to 1723 // worry about preserving child type decoration. 1724 #define TYPE(Class, Base) 1725 #define LEAF_TYPE(Class) \ 1726 template <> inline const Class##Type *Type::getAs() const { \ 1727 return dyn_cast<Class##Type>(CanonicalType); \ 1728 } \ 1729 template <> inline const Class##Type *Type::castAs() const { \ 1730 return cast<Class##Type>(CanonicalType); \ 1731 } 1732 #include "clang/AST/TypeNodes.def" 1733 1734 1735 /// BuiltinType - This class is used for builtin types like 'int'. Builtin 1736 /// types are always canonical and have a literal name field. 1737 class BuiltinType : public Type { 1738 public: 1739 enum Kind { 1740 #define BUILTIN_TYPE(Id, SingletonId) Id, 1741 #define LAST_BUILTIN_TYPE(Id) LastKind = Id 1742 #include "clang/AST/BuiltinTypes.def" 1743 }; 1744 1745 public: 1746 BuiltinType(Kind K) 1747 : Type(Builtin, QualType(), /*Dependent=*/(K == Dependent), 1748 /*InstantiationDependent=*/(K == Dependent), 1749 /*VariablyModified=*/false, 1750 /*Unexpanded paramter pack=*/false) { 1751 BuiltinTypeBits.Kind = K; 1752 } 1753 1754 Kind getKind() const { return static_cast<Kind>(BuiltinTypeBits.Kind); } 1755 const char *getName(const PrintingPolicy &Policy) const; 1756 1757 bool isSugared() const { return false; } 1758 QualType desugar() const { return QualType(this, 0); } 1759 1760 bool isInteger() const { 1761 return getKind() >= Bool && getKind() <= Int128; 1762 } 1763 1764 bool isSignedInteger() const { 1765 return getKind() >= Char_S && getKind() <= Int128; 1766 } 1767 1768 bool isUnsignedInteger() const { 1769 return getKind() >= Bool && getKind() <= UInt128; 1770 } 1771 1772 bool isFloatingPoint() const { 1773 return getKind() >= Half && getKind() <= LongDouble; 1774 } 1775 1776 /// Determines whether the given kind corresponds to a placeholder type. 1777 static bool isPlaceholderTypeKind(Kind K) { 1778 return K >= Overload; 1779 } 1780 1781 /// Determines whether this type is a placeholder type, i.e. a type 1782 /// which cannot appear in arbitrary positions in a fully-formed 1783 /// expression. 1784 bool isPlaceholderType() const { 1785 return isPlaceholderTypeKind(getKind()); 1786 } 1787 1788 /// Determines whether this type is a placeholder type other than 1789 /// Overload. Most placeholder types require only syntactic 1790 /// information about their context in order to be resolved (e.g. 1791 /// whether it is a call expression), which means they can (and 1792 /// should) be resolved in an earlier "phase" of analysis. 1793 /// Overload expressions sometimes pick up further information 1794 /// from their context, like whether the context expects a 1795 /// specific function-pointer type, and so frequently need 1796 /// special treatment. 1797 bool isNonOverloadPlaceholderType() const { 1798 return getKind() > Overload; 1799 } 1800 1801 static bool classof(const Type *T) { return T->getTypeClass() == Builtin; } 1802 static bool classof(const BuiltinType *) { return true; } 1803 }; 1804 1805 /// ComplexType - C99 6.2.5p11 - Complex values. This supports the C99 complex 1806 /// types (_Complex float etc) as well as the GCC integer complex extensions. 1807 /// 1808 class ComplexType : public Type, public llvm::FoldingSetNode { 1809 QualType ElementType; 1810 ComplexType(QualType Element, QualType CanonicalPtr) : 1811 Type(Complex, CanonicalPtr, Element->isDependentType(), 1812 Element->isInstantiationDependentType(), 1813 Element->isVariablyModifiedType(), 1814 Element->containsUnexpandedParameterPack()), 1815 ElementType(Element) { 1816 } 1817 friend class ASTContext; // ASTContext creates these. 1818 1819 public: 1820 QualType getElementType() const { return ElementType; } 1821 1822 bool isSugared() const { return false; } 1823 QualType desugar() const { return QualType(this, 0); } 1824 1825 void Profile(llvm::FoldingSetNodeID &ID) { 1826 Profile(ID, getElementType()); 1827 } 1828 static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) { 1829 ID.AddPointer(Element.getAsOpaquePtr()); 1830 } 1831 1832 static bool classof(const Type *T) { return T->getTypeClass() == Complex; } 1833 static bool classof(const ComplexType *) { return true; } 1834 }; 1835 1836 /// ParenType - Sugar for parentheses used when specifying types. 1837 /// 1838 class ParenType : public Type, public llvm::FoldingSetNode { 1839 QualType Inner; 1840 1841 ParenType(QualType InnerType, QualType CanonType) : 1842 Type(Paren, CanonType, InnerType->isDependentType(), 1843 InnerType->isInstantiationDependentType(), 1844 InnerType->isVariablyModifiedType(), 1845 InnerType->containsUnexpandedParameterPack()), 1846 Inner(InnerType) { 1847 } 1848 friend class ASTContext; // ASTContext creates these. 1849 1850 public: 1851 1852 QualType getInnerType() const { return Inner; } 1853 1854 bool isSugared() const { return true; } 1855 QualType desugar() const { return getInnerType(); } 1856 1857 void Profile(llvm::FoldingSetNodeID &ID) { 1858 Profile(ID, getInnerType()); 1859 } 1860 static void Profile(llvm::FoldingSetNodeID &ID, QualType Inner) { 1861 Inner.Profile(ID); 1862 } 1863 1864 static bool classof(const Type *T) { return T->getTypeClass() == Paren; } 1865 static bool classof(const ParenType *) { return true; } 1866 }; 1867 1868 /// PointerType - C99 6.7.5.1 - Pointer Declarators. 1869 /// 1870 class PointerType : public Type, public llvm::FoldingSetNode { 1871 QualType PointeeType; 1872 1873 PointerType(QualType Pointee, QualType CanonicalPtr) : 1874 Type(Pointer, CanonicalPtr, Pointee->isDependentType(), 1875 Pointee->isInstantiationDependentType(), 1876 Pointee->isVariablyModifiedType(), 1877 Pointee->containsUnexpandedParameterPack()), 1878 PointeeType(Pointee) { 1879 } 1880 friend class ASTContext; // ASTContext creates these. 1881 1882 public: 1883 1884 QualType getPointeeType() const { return PointeeType; } 1885 1886 bool isSugared() const { return false; } 1887 QualType desugar() const { return QualType(this, 0); } 1888 1889 void Profile(llvm::FoldingSetNodeID &ID) { 1890 Profile(ID, getPointeeType()); 1891 } 1892 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { 1893 ID.AddPointer(Pointee.getAsOpaquePtr()); 1894 } 1895 1896 static bool classof(const Type *T) { return T->getTypeClass() == Pointer; } 1897 static bool classof(const PointerType *) { return true; } 1898 }; 1899 1900 /// BlockPointerType - pointer to a block type. 1901 /// This type is to represent types syntactically represented as 1902 /// "void (^)(int)", etc. Pointee is required to always be a function type. 1903 /// 1904 class BlockPointerType : public Type, public llvm::FoldingSetNode { 1905 QualType PointeeType; // Block is some kind of pointer type 1906 BlockPointerType(QualType Pointee, QualType CanonicalCls) : 1907 Type(BlockPointer, CanonicalCls, Pointee->isDependentType(), 1908 Pointee->isInstantiationDependentType(), 1909 Pointee->isVariablyModifiedType(), 1910 Pointee->containsUnexpandedParameterPack()), 1911 PointeeType(Pointee) { 1912 } 1913 friend class ASTContext; // ASTContext creates these. 1914 1915 public: 1916 1917 // Get the pointee type. Pointee is required to always be a function type. 1918 QualType getPointeeType() const { return PointeeType; } 1919 1920 bool isSugared() const { return false; } 1921 QualType desugar() const { return QualType(this, 0); } 1922 1923 void Profile(llvm::FoldingSetNodeID &ID) { 1924 Profile(ID, getPointeeType()); 1925 } 1926 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { 1927 ID.AddPointer(Pointee.getAsOpaquePtr()); 1928 } 1929 1930 static bool classof(const Type *T) { 1931 return T->getTypeClass() == BlockPointer; 1932 } 1933 static bool classof(const BlockPointerType *) { return true; } 1934 }; 1935 1936 /// ReferenceType - Base for LValueReferenceType and RValueReferenceType 1937 /// 1938 class ReferenceType : public Type, public llvm::FoldingSetNode { 1939 QualType PointeeType; 1940 1941 protected: 1942 ReferenceType(TypeClass tc, QualType Referencee, QualType CanonicalRef, 1943 bool SpelledAsLValue) : 1944 Type(tc, CanonicalRef, Referencee->isDependentType(), 1945 Referencee->isInstantiationDependentType(), 1946 Referencee->isVariablyModifiedType(), 1947 Referencee->containsUnexpandedParameterPack()), 1948 PointeeType(Referencee) 1949 { 1950 ReferenceTypeBits.SpelledAsLValue = SpelledAsLValue; 1951 ReferenceTypeBits.InnerRef = Referencee->isReferenceType(); 1952 } 1953 1954 public: 1955 bool isSpelledAsLValue() const { return ReferenceTypeBits.SpelledAsLValue; } 1956 bool isInnerRef() const { return ReferenceTypeBits.InnerRef; } 1957 1958 QualType getPointeeTypeAsWritten() const { return PointeeType; } 1959 QualType getPointeeType() const { 1960 // FIXME: this might strip inner qualifiers; okay? 1961 const ReferenceType *T = this; 1962 while (T->isInnerRef()) 1963 T = T->PointeeType->castAs<ReferenceType>(); 1964 return T->PointeeType; 1965 } 1966 1967 void Profile(llvm::FoldingSetNodeID &ID) { 1968 Profile(ID, PointeeType, isSpelledAsLValue()); 1969 } 1970 static void Profile(llvm::FoldingSetNodeID &ID, 1971 QualType Referencee, 1972 bool SpelledAsLValue) { 1973 ID.AddPointer(Referencee.getAsOpaquePtr()); 1974 ID.AddBoolean(SpelledAsLValue); 1975 } 1976 1977 static bool classof(const Type *T) { 1978 return T->getTypeClass() == LValueReference || 1979 T->getTypeClass() == RValueReference; 1980 } 1981 static bool classof(const ReferenceType *) { return true; } 1982 }; 1983 1984 /// LValueReferenceType - C++ [dcl.ref] - Lvalue reference 1985 /// 1986 class LValueReferenceType : public ReferenceType { 1987 LValueReferenceType(QualType Referencee, QualType CanonicalRef, 1988 bool SpelledAsLValue) : 1989 ReferenceType(LValueReference, Referencee, CanonicalRef, SpelledAsLValue) 1990 {} 1991 friend class ASTContext; // ASTContext creates these 1992 public: 1993 bool isSugared() const { return false; } 1994 QualType desugar() const { return QualType(this, 0); } 1995 1996 static bool classof(const Type *T) { 1997 return T->getTypeClass() == LValueReference; 1998 } 1999 static bool classof(const LValueReferenceType *) { return true; } 2000 }; 2001 2002 /// RValueReferenceType - C++0x [dcl.ref] - Rvalue reference 2003 /// 2004 class RValueReferenceType : public ReferenceType { 2005 RValueReferenceType(QualType Referencee, QualType CanonicalRef) : 2006 ReferenceType(RValueReference, Referencee, CanonicalRef, false) { 2007 } 2008 friend class ASTContext; // ASTContext creates these 2009 public: 2010 bool isSugared() const { return false; } 2011 QualType desugar() const { return QualType(this, 0); } 2012 2013 static bool classof(const Type *T) { 2014 return T->getTypeClass() == RValueReference; 2015 } 2016 static bool classof(const RValueReferenceType *) { return true; } 2017 }; 2018 2019 /// MemberPointerType - C++ 8.3.3 - Pointers to members 2020 /// 2021 class MemberPointerType : public Type, public llvm::FoldingSetNode { 2022 QualType PointeeType; 2023 /// The class of which the pointee is a member. Must ultimately be a 2024 /// RecordType, but could be a typedef or a template parameter too. 2025 const Type *Class; 2026 2027 MemberPointerType(QualType Pointee, const Type *Cls, QualType CanonicalPtr) : 2028 Type(MemberPointer, CanonicalPtr, 2029 Cls->isDependentType() || Pointee->isDependentType(), 2030 (Cls->isInstantiationDependentType() || 2031 Pointee->isInstantiationDependentType()), 2032 Pointee->isVariablyModifiedType(), 2033 (Cls->containsUnexpandedParameterPack() || 2034 Pointee->containsUnexpandedParameterPack())), 2035 PointeeType(Pointee), Class(Cls) { 2036 } 2037 friend class ASTContext; // ASTContext creates these. 2038 2039 public: 2040 QualType getPointeeType() const { return PointeeType; } 2041 2042 /// Returns true if the member type (i.e. the pointee type) is a 2043 /// function type rather than a data-member type. 2044 bool isMemberFunctionPointer() const { 2045 return PointeeType->isFunctionProtoType(); 2046 } 2047 2048 /// Returns true if the member type (i.e. the pointee type) is a 2049 /// data type rather than a function type. 2050 bool isMemberDataPointer() const { 2051 return !PointeeType->isFunctionProtoType(); 2052 } 2053 2054 const Type *getClass() const { return Class; } 2055 2056 bool isSugared() const { return false; } 2057 QualType desugar() const { return QualType(this, 0); } 2058 2059 void Profile(llvm::FoldingSetNodeID &ID) { 2060 Profile(ID, getPointeeType(), getClass()); 2061 } 2062 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee, 2063 const Type *Class) { 2064 ID.AddPointer(Pointee.getAsOpaquePtr()); 2065 ID.AddPointer(Class); 2066 } 2067 2068 static bool classof(const Type *T) { 2069 return T->getTypeClass() == MemberPointer; 2070 } 2071 static bool classof(const MemberPointerType *) { return true; } 2072 }; 2073 2074 /// ArrayType - C99 6.7.5.2 - Array Declarators. 2075 /// 2076 class ArrayType : public Type, public llvm::FoldingSetNode { 2077 public: 2078 /// ArraySizeModifier - Capture whether this is a normal array (e.g. int X[4]) 2079 /// an array with a static size (e.g. int X[static 4]), or an array 2080 /// with a star size (e.g. int X[*]). 2081 /// 'static' is only allowed on function parameters. 2082 enum ArraySizeModifier { 2083 Normal, Static, Star 2084 }; 2085 private: 2086 /// ElementType - The element type of the array. 2087 QualType ElementType; 2088 2089 protected: 2090 // C++ [temp.dep.type]p1: 2091 // A type is dependent if it is... 2092 // - an array type constructed from any dependent type or whose 2093 // size is specified by a constant expression that is 2094 // value-dependent, 2095 ArrayType(TypeClass tc, QualType et, QualType can, 2096 ArraySizeModifier sm, unsigned tq, 2097 bool ContainsUnexpandedParameterPack) 2098 : Type(tc, can, et->isDependentType() || tc == DependentSizedArray, 2099 et->isInstantiationDependentType() || tc == DependentSizedArray, 2100 (tc == VariableArray || et->isVariablyModifiedType()), 2101 ContainsUnexpandedParameterPack), 2102 ElementType(et) { 2103 ArrayTypeBits.IndexTypeQuals = tq; 2104 ArrayTypeBits.SizeModifier = sm; 2105 } 2106 2107 friend class ASTContext; // ASTContext creates these. 2108 2109 public: 2110 QualType getElementType() const { return ElementType; } 2111 ArraySizeModifier getSizeModifier() const { 2112 return ArraySizeModifier(ArrayTypeBits.SizeModifier); 2113 } 2114 Qualifiers getIndexTypeQualifiers() const { 2115 return Qualifiers::fromCVRMask(getIndexTypeCVRQualifiers()); 2116 } 2117 unsigned getIndexTypeCVRQualifiers() const { 2118 return ArrayTypeBits.IndexTypeQuals; 2119 } 2120 2121 static bool classof(const Type *T) { 2122 return T->getTypeClass() == ConstantArray || 2123 T->getTypeClass() == VariableArray || 2124 T->getTypeClass() == IncompleteArray || 2125 T->getTypeClass() == DependentSizedArray; 2126 } 2127 static bool classof(const ArrayType *) { return true; } 2128 }; 2129 2130 /// ConstantArrayType - This class represents the canonical version of 2131 /// C arrays with a specified constant size. For example, the canonical 2132 /// type for 'int A[4 + 4*100]' is a ConstantArrayType where the element 2133 /// type is 'int' and the size is 404. 2134 class ConstantArrayType : public ArrayType { 2135 llvm::APInt Size; // Allows us to unique the type. 2136 2137 ConstantArrayType(QualType et, QualType can, const llvm::APInt &size, 2138 ArraySizeModifier sm, unsigned tq) 2139 : ArrayType(ConstantArray, et, can, sm, tq, 2140 et->containsUnexpandedParameterPack()), 2141 Size(size) {} 2142 protected: 2143 ConstantArrayType(TypeClass tc, QualType et, QualType can, 2144 const llvm::APInt &size, ArraySizeModifier sm, unsigned tq) 2145 : ArrayType(tc, et, can, sm, tq, et->containsUnexpandedParameterPack()), 2146 Size(size) {} 2147 friend class ASTContext; // ASTContext creates these. 2148 public: 2149 const llvm::APInt &getSize() const { return Size; } 2150 bool isSugared() const { return false; } 2151 QualType desugar() const { return QualType(this, 0); } 2152 2153 2154 /// \brief Determine the number of bits required to address a member of 2155 // an array with the given element type and number of elements. 2156 static unsigned getNumAddressingBits(ASTContext &Context, 2157 QualType ElementType, 2158 const llvm::APInt &NumElements); 2159 2160 /// \brief Determine the maximum number of active bits that an array's size 2161 /// can require, which limits the maximum size of the array. 2162 static unsigned getMaxSizeBits(ASTContext &Context); 2163 2164 void Profile(llvm::FoldingSetNodeID &ID) { 2165 Profile(ID, getElementType(), getSize(), 2166 getSizeModifier(), getIndexTypeCVRQualifiers()); 2167 } 2168 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, 2169 const llvm::APInt &ArraySize, ArraySizeModifier SizeMod, 2170 unsigned TypeQuals) { 2171 ID.AddPointer(ET.getAsOpaquePtr()); 2172 ID.AddInteger(ArraySize.getZExtValue()); 2173 ID.AddInteger(SizeMod); 2174 ID.AddInteger(TypeQuals); 2175 } 2176 static bool classof(const Type *T) { 2177 return T->getTypeClass() == ConstantArray; 2178 } 2179 static bool classof(const ConstantArrayType *) { return true; } 2180 }; 2181 2182 /// IncompleteArrayType - This class represents C arrays with an unspecified 2183 /// size. For example 'int A[]' has an IncompleteArrayType where the element 2184 /// type is 'int' and the size is unspecified. 2185 class IncompleteArrayType : public ArrayType { 2186 2187 IncompleteArrayType(QualType et, QualType can, 2188 ArraySizeModifier sm, unsigned tq) 2189 : ArrayType(IncompleteArray, et, can, sm, tq, 2190 et->containsUnexpandedParameterPack()) {} 2191 friend class ASTContext; // ASTContext creates these. 2192 public: 2193 bool isSugared() const { return false; } 2194 QualType desugar() const { return QualType(this, 0); } 2195 2196 static bool classof(const Type *T) { 2197 return T->getTypeClass() == IncompleteArray; 2198 } 2199 static bool classof(const IncompleteArrayType *) { return true; } 2200 2201 friend class StmtIteratorBase; 2202 2203 void Profile(llvm::FoldingSetNodeID &ID) { 2204 Profile(ID, getElementType(), getSizeModifier(), 2205 getIndexTypeCVRQualifiers()); 2206 } 2207 2208 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, 2209 ArraySizeModifier SizeMod, unsigned TypeQuals) { 2210 ID.AddPointer(ET.getAsOpaquePtr()); 2211 ID.AddInteger(SizeMod); 2212 ID.AddInteger(TypeQuals); 2213 } 2214 }; 2215 2216 /// VariableArrayType - This class represents C arrays with a specified size 2217 /// which is not an integer-constant-expression. For example, 'int s[x+foo()]'. 2218 /// Since the size expression is an arbitrary expression, we store it as such. 2219 /// 2220 /// Note: VariableArrayType's aren't uniqued (since the expressions aren't) and 2221 /// should not be: two lexically equivalent variable array types could mean 2222 /// different things, for example, these variables do not have the same type 2223 /// dynamically: 2224 /// 2225 /// void foo(int x) { 2226 /// int Y[x]; 2227 /// ++x; 2228 /// int Z[x]; 2229 /// } 2230 /// 2231 class VariableArrayType : public ArrayType { 2232 /// SizeExpr - An assignment expression. VLA's are only permitted within 2233 /// a function block. 2234 Stmt *SizeExpr; 2235 /// Brackets - The left and right array brackets. 2236 SourceRange Brackets; 2237 2238 VariableArrayType(QualType et, QualType can, Expr *e, 2239 ArraySizeModifier sm, unsigned tq, 2240 SourceRange brackets) 2241 : ArrayType(VariableArray, et, can, sm, tq, 2242 et->containsUnexpandedParameterPack()), 2243 SizeExpr((Stmt*) e), Brackets(brackets) {} 2244 friend class ASTContext; // ASTContext creates these. 2245 2246 public: 2247 Expr *getSizeExpr() const { 2248 // We use C-style casts instead of cast<> here because we do not wish 2249 // to have a dependency of Type.h on Stmt.h/Expr.h. 2250 return (Expr*) SizeExpr; 2251 } 2252 SourceRange getBracketsRange() const { return Brackets; } 2253 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } 2254 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } 2255 2256 bool isSugared() const { return false; } 2257 QualType desugar() const { return QualType(this, 0); } 2258 2259 static bool classof(const Type *T) { 2260 return T->getTypeClass() == VariableArray; 2261 } 2262 static bool classof(const VariableArrayType *) { return true; } 2263 2264 friend class StmtIteratorBase; 2265 2266 void Profile(llvm::FoldingSetNodeID &ID) { 2267 llvm_unreachable("Cannot unique VariableArrayTypes."); 2268 } 2269 }; 2270 2271 /// DependentSizedArrayType - This type represents an array type in 2272 /// C++ whose size is a value-dependent expression. For example: 2273 /// 2274 /// \code 2275 /// template<typename T, int Size> 2276 /// class array { 2277 /// T data[Size]; 2278 /// }; 2279 /// \endcode 2280 /// 2281 /// For these types, we won't actually know what the array bound is 2282 /// until template instantiation occurs, at which point this will 2283 /// become either a ConstantArrayType or a VariableArrayType. 2284 class DependentSizedArrayType : public ArrayType { 2285 const ASTContext &Context; 2286 2287 /// \brief An assignment expression that will instantiate to the 2288 /// size of the array. 2289 /// 2290 /// The expression itself might be NULL, in which case the array 2291 /// type will have its size deduced from an initializer. 2292 Stmt *SizeExpr; 2293 2294 /// Brackets - The left and right array brackets. 2295 SourceRange Brackets; 2296 2297 DependentSizedArrayType(const ASTContext &Context, QualType et, QualType can, 2298 Expr *e, ArraySizeModifier sm, unsigned tq, 2299 SourceRange brackets); 2300 2301 friend class ASTContext; // ASTContext creates these. 2302 2303 public: 2304 Expr *getSizeExpr() const { 2305 // We use C-style casts instead of cast<> here because we do not wish 2306 // to have a dependency of Type.h on Stmt.h/Expr.h. 2307 return (Expr*) SizeExpr; 2308 } 2309 SourceRange getBracketsRange() const { return Brackets; } 2310 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } 2311 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } 2312 2313 bool isSugared() const { return false; } 2314 QualType desugar() const { return QualType(this, 0); } 2315 2316 static bool classof(const Type *T) { 2317 return T->getTypeClass() == DependentSizedArray; 2318 } 2319 static bool classof(const DependentSizedArrayType *) { return true; } 2320 2321 friend class StmtIteratorBase; 2322 2323 2324 void Profile(llvm::FoldingSetNodeID &ID) { 2325 Profile(ID, Context, getElementType(), 2326 getSizeModifier(), getIndexTypeCVRQualifiers(), getSizeExpr()); 2327 } 2328 2329 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 2330 QualType ET, ArraySizeModifier SizeMod, 2331 unsigned TypeQuals, Expr *E); 2332 }; 2333 2334 /// DependentSizedExtVectorType - This type represent an extended vector type 2335 /// where either the type or size is dependent. For example: 2336 /// @code 2337 /// template<typename T, int Size> 2338 /// class vector { 2339 /// typedef T __attribute__((ext_vector_type(Size))) type; 2340 /// } 2341 /// @endcode 2342 class DependentSizedExtVectorType : public Type, public llvm::FoldingSetNode { 2343 const ASTContext &Context; 2344 Expr *SizeExpr; 2345 /// ElementType - The element type of the array. 2346 QualType ElementType; 2347 SourceLocation loc; 2348 2349 DependentSizedExtVectorType(const ASTContext &Context, QualType ElementType, 2350 QualType can, Expr *SizeExpr, SourceLocation loc); 2351 2352 friend class ASTContext; 2353 2354 public: 2355 Expr *getSizeExpr() const { return SizeExpr; } 2356 QualType getElementType() const { return ElementType; } 2357 SourceLocation getAttributeLoc() const { return loc; } 2358 2359 bool isSugared() const { return false; } 2360 QualType desugar() const { return QualType(this, 0); } 2361 2362 static bool classof(const Type *T) { 2363 return T->getTypeClass() == DependentSizedExtVector; 2364 } 2365 static bool classof(const DependentSizedExtVectorType *) { return true; } 2366 2367 void Profile(llvm::FoldingSetNodeID &ID) { 2368 Profile(ID, Context, getElementType(), getSizeExpr()); 2369 } 2370 2371 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 2372 QualType ElementType, Expr *SizeExpr); 2373 }; 2374 2375 2376 /// VectorType - GCC generic vector type. This type is created using 2377 /// __attribute__((vector_size(n)), where "n" specifies the vector size in 2378 /// bytes; or from an Altivec __vector or vector declaration. 2379 /// Since the constructor takes the number of vector elements, the 2380 /// client is responsible for converting the size into the number of elements. 2381 class VectorType : public Type, public llvm::FoldingSetNode { 2382 public: 2383 enum VectorKind { 2384 GenericVector, // not a target-specific vector type 2385 AltiVecVector, // is AltiVec vector 2386 AltiVecPixel, // is AltiVec 'vector Pixel' 2387 AltiVecBool, // is AltiVec 'vector bool ...' 2388 NeonVector, // is ARM Neon vector 2389 NeonPolyVector // is ARM Neon polynomial vector 2390 }; 2391 protected: 2392 /// ElementType - The element type of the vector. 2393 QualType ElementType; 2394 2395 VectorType(QualType vecType, unsigned nElements, QualType canonType, 2396 VectorKind vecKind); 2397 2398 VectorType(TypeClass tc, QualType vecType, unsigned nElements, 2399 QualType canonType, VectorKind vecKind); 2400 2401 friend class ASTContext; // ASTContext creates these. 2402 2403 public: 2404 2405 QualType getElementType() const { return ElementType; } 2406 unsigned getNumElements() const { return VectorTypeBits.NumElements; } 2407 2408 bool isSugared() const { return false; } 2409 QualType desugar() const { return QualType(this, 0); } 2410 2411 VectorKind getVectorKind() const { 2412 return VectorKind(VectorTypeBits.VecKind); 2413 } 2414 2415 void Profile(llvm::FoldingSetNodeID &ID) { 2416 Profile(ID, getElementType(), getNumElements(), 2417 getTypeClass(), getVectorKind()); 2418 } 2419 static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, 2420 unsigned NumElements, TypeClass TypeClass, 2421 VectorKind VecKind) { 2422 ID.AddPointer(ElementType.getAsOpaquePtr()); 2423 ID.AddInteger(NumElements); 2424 ID.AddInteger(TypeClass); 2425 ID.AddInteger(VecKind); 2426 } 2427 2428 static bool classof(const Type *T) { 2429 return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector; 2430 } 2431 static bool classof(const VectorType *) { return true; } 2432 }; 2433 2434 /// ExtVectorType - Extended vector type. This type is created using 2435 /// __attribute__((ext_vector_type(n)), where "n" is the number of elements. 2436 /// Unlike vector_size, ext_vector_type is only allowed on typedef's. This 2437 /// class enables syntactic extensions, like Vector Components for accessing 2438 /// points, colors, and textures (modeled after OpenGL Shading Language). 2439 class ExtVectorType : public VectorType { 2440 ExtVectorType(QualType vecType, unsigned nElements, QualType canonType) : 2441 VectorType(ExtVector, vecType, nElements, canonType, GenericVector) {} 2442 friend class ASTContext; // ASTContext creates these. 2443 public: 2444 static int getPointAccessorIdx(char c) { 2445 switch (c) { 2446 default: return -1; 2447 case 'x': case 'r': return 0; 2448 case 'y': case 'g': return 1; 2449 case 'z': case 'b': return 2; 2450 case 'w': case 'a': return 3; 2451 } 2452 } 2453 static int getNumericAccessorIdx(char c) { 2454 switch (c) { 2455 default: return -1; 2456 case '0': return 0; 2457 case '1': return 1; 2458 case '2': return 2; 2459 case '3': return 3; 2460 case '4': return 4; 2461 case '5': return 5; 2462 case '6': return 6; 2463 case '7': return 7; 2464 case '8': return 8; 2465 case '9': return 9; 2466 case 'A': 2467 case 'a': return 10; 2468 case 'B': 2469 case 'b': return 11; 2470 case 'C': 2471 case 'c': return 12; 2472 case 'D': 2473 case 'd': return 13; 2474 case 'E': 2475 case 'e': return 14; 2476 case 'F': 2477 case 'f': return 15; 2478 } 2479 } 2480 2481 static int getAccessorIdx(char c) { 2482 if (int idx = getPointAccessorIdx(c)+1) return idx-1; 2483 return getNumericAccessorIdx(c); 2484 } 2485 2486 bool isAccessorWithinNumElements(char c) const { 2487 if (int idx = getAccessorIdx(c)+1) 2488 return unsigned(idx-1) < getNumElements(); 2489 return false; 2490 } 2491 bool isSugared() const { return false; } 2492 QualType desugar() const { return QualType(this, 0); } 2493 2494 static bool classof(const Type *T) { 2495 return T->getTypeClass() == ExtVector; 2496 } 2497 static bool classof(const ExtVectorType *) { return true; } 2498 }; 2499 2500 /// FunctionType - C99 6.7.5.3 - Function Declarators. This is the common base 2501 /// class of FunctionNoProtoType and FunctionProtoType. 2502 /// 2503 class FunctionType : public Type { 2504 // The type returned by the function. 2505 QualType ResultType; 2506 2507 public: 2508 /// ExtInfo - A class which abstracts out some details necessary for 2509 /// making a call. 2510 /// 2511 /// It is not actually used directly for storing this information in 2512 /// a FunctionType, although FunctionType does currently use the 2513 /// same bit-pattern. 2514 /// 2515 // If you add a field (say Foo), other than the obvious places (both, 2516 // constructors, compile failures), what you need to update is 2517 // * Operator== 2518 // * getFoo 2519 // * withFoo 2520 // * functionType. Add Foo, getFoo. 2521 // * ASTContext::getFooType 2522 // * ASTContext::mergeFunctionTypes 2523 // * FunctionNoProtoType::Profile 2524 // * FunctionProtoType::Profile 2525 // * TypePrinter::PrintFunctionProto 2526 // * AST read and write 2527 // * Codegen 2528 class ExtInfo { 2529 // Feel free to rearrange or add bits, but if you go over 8, 2530 // you'll need to adjust both the Bits field below and 2531 // Type::FunctionTypeBitfields. 2532 2533 // | CC |noreturn|produces|regparm| 2534 // |0 .. 2| 3 | 4 | 5 .. 7| 2535 // 2536 // regparm is either 0 (no regparm attribute) or the regparm value+1. 2537 enum { CallConvMask = 0x7 }; 2538 enum { NoReturnMask = 0x8 }; 2539 enum { ProducesResultMask = 0x10 }; 2540 enum { RegParmMask = ~(CallConvMask | NoReturnMask | ProducesResultMask), 2541 RegParmOffset = 5 }; // Assumed to be the last field 2542 2543 uint16_t Bits; 2544 2545 ExtInfo(unsigned Bits) : Bits(static_cast<uint16_t>(Bits)) {} 2546 2547 friend class FunctionType; 2548 2549 public: 2550 // Constructor with no defaults. Use this when you know that you 2551 // have all the elements (when reading an AST file for example). 2552 ExtInfo(bool noReturn, bool hasRegParm, unsigned regParm, CallingConv cc, 2553 bool producesResult) { 2554 assert((!hasRegParm || regParm < 7) && "Invalid regparm value"); 2555 Bits = ((unsigned) cc) | 2556 (noReturn ? NoReturnMask : 0) | 2557 (producesResult ? ProducesResultMask : 0) | 2558 (hasRegParm ? ((regParm + 1) << RegParmOffset) : 0); 2559 } 2560 2561 // Constructor with all defaults. Use when for example creating a 2562 // function know to use defaults. 2563 ExtInfo() : Bits(0) {} 2564 2565 bool getNoReturn() const { return Bits & NoReturnMask; } 2566 bool getProducesResult() const { return Bits & ProducesResultMask; } 2567 bool getHasRegParm() const { return (Bits >> RegParmOffset) != 0; } 2568 unsigned getRegParm() const { 2569 unsigned RegParm = Bits >> RegParmOffset; 2570 if (RegParm > 0) 2571 --RegParm; 2572 return RegParm; 2573 } 2574 CallingConv getCC() const { return CallingConv(Bits & CallConvMask); } 2575 2576 bool operator==(ExtInfo Other) const { 2577 return Bits == Other.Bits; 2578 } 2579 bool operator!=(ExtInfo Other) const { 2580 return Bits != Other.Bits; 2581 } 2582 2583 // Note that we don't have setters. That is by design, use 2584 // the following with methods instead of mutating these objects. 2585 2586 ExtInfo withNoReturn(bool noReturn) const { 2587 if (noReturn) 2588 return ExtInfo(Bits | NoReturnMask); 2589 else 2590 return ExtInfo(Bits & ~NoReturnMask); 2591 } 2592 2593 ExtInfo withProducesResult(bool producesResult) const { 2594 if (producesResult) 2595 return ExtInfo(Bits | ProducesResultMask); 2596 else 2597 return ExtInfo(Bits & ~ProducesResultMask); 2598 } 2599 2600 ExtInfo withRegParm(unsigned RegParm) const { 2601 assert(RegParm < 7 && "Invalid regparm value"); 2602 return ExtInfo((Bits & ~RegParmMask) | 2603 ((RegParm + 1) << RegParmOffset)); 2604 } 2605 2606 ExtInfo withCallingConv(CallingConv cc) const { 2607 return ExtInfo((Bits & ~CallConvMask) | (unsigned) cc); 2608 } 2609 2610 void Profile(llvm::FoldingSetNodeID &ID) const { 2611 ID.AddInteger(Bits); 2612 } 2613 }; 2614 2615 protected: 2616 FunctionType(TypeClass tc, QualType res, 2617 unsigned typeQuals, RefQualifierKind RefQualifier, 2618 QualType Canonical, bool Dependent, 2619 bool InstantiationDependent, 2620 bool VariablyModified, bool ContainsUnexpandedParameterPack, 2621 ExtInfo Info) 2622 : Type(tc, Canonical, Dependent, InstantiationDependent, VariablyModified, 2623 ContainsUnexpandedParameterPack), 2624 ResultType(res) { 2625 FunctionTypeBits.ExtInfo = Info.Bits; 2626 FunctionTypeBits.TypeQuals = typeQuals; 2627 FunctionTypeBits.RefQualifier = static_cast<unsigned>(RefQualifier); 2628 } 2629 unsigned getTypeQuals() const { return FunctionTypeBits.TypeQuals; } 2630 2631 RefQualifierKind getRefQualifier() const { 2632 return static_cast<RefQualifierKind>(FunctionTypeBits.RefQualifier); 2633 } 2634 2635 public: 2636 2637 QualType getResultType() const { return ResultType; } 2638 2639 bool getHasRegParm() const { return getExtInfo().getHasRegParm(); } 2640 unsigned getRegParmType() const { return getExtInfo().getRegParm(); } 2641 bool getNoReturnAttr() const { return getExtInfo().getNoReturn(); } 2642 CallingConv getCallConv() const { return getExtInfo().getCC(); } 2643 ExtInfo getExtInfo() const { return ExtInfo(FunctionTypeBits.ExtInfo); } 2644 2645 /// \brief Determine the type of an expression that calls a function of 2646 /// this type. 2647 QualType getCallResultType(ASTContext &Context) const { 2648 return getResultType().getNonLValueExprType(Context); 2649 } 2650 2651 static StringRef getNameForCallConv(CallingConv CC); 2652 2653 static bool classof(const Type *T) { 2654 return T->getTypeClass() == FunctionNoProto || 2655 T->getTypeClass() == FunctionProto; 2656 } 2657 static bool classof(const FunctionType *) { return true; } 2658 }; 2659 2660 /// FunctionNoProtoType - Represents a K&R-style 'int foo()' function, which has 2661 /// no information available about its arguments. 2662 class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode { 2663 FunctionNoProtoType(QualType Result, QualType Canonical, ExtInfo Info) 2664 : FunctionType(FunctionNoProto, Result, 0, RQ_None, Canonical, 2665 /*Dependent=*/false, /*InstantiationDependent=*/false, 2666 Result->isVariablyModifiedType(), 2667 /*ContainsUnexpandedParameterPack=*/false, Info) {} 2668 2669 friend class ASTContext; // ASTContext creates these. 2670 2671 public: 2672 // No additional state past what FunctionType provides. 2673 2674 bool isSugared() const { return false; } 2675 QualType desugar() const { return QualType(this, 0); } 2676 2677 void Profile(llvm::FoldingSetNodeID &ID) { 2678 Profile(ID, getResultType(), getExtInfo()); 2679 } 2680 static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType, 2681 ExtInfo Info) { 2682 Info.Profile(ID); 2683 ID.AddPointer(ResultType.getAsOpaquePtr()); 2684 } 2685 2686 static bool classof(const Type *T) { 2687 return T->getTypeClass() == FunctionNoProto; 2688 } 2689 static bool classof(const FunctionNoProtoType *) { return true; } 2690 }; 2691 2692 /// FunctionProtoType - Represents a prototype with argument type info, e.g. 2693 /// 'int foo(int)' or 'int foo(void)'. 'void' is represented as having no 2694 /// arguments, not as having a single void argument. Such a type can have an 2695 /// exception specification, but this specification is not part of the canonical 2696 /// type. 2697 class FunctionProtoType : public FunctionType, public llvm::FoldingSetNode { 2698 public: 2699 /// ExtProtoInfo - Extra information about a function prototype. 2700 struct ExtProtoInfo { 2701 ExtProtoInfo() : 2702 Variadic(false), HasTrailingReturn(false), TypeQuals(0), 2703 ExceptionSpecType(EST_None), RefQualifier(RQ_None), 2704 NumExceptions(0), Exceptions(0), NoexceptExpr(0), 2705 ExceptionSpecDecl(0), ExceptionSpecTemplate(0), 2706 ConsumedArguments(0) {} 2707 2708 FunctionType::ExtInfo ExtInfo; 2709 bool Variadic : 1; 2710 bool HasTrailingReturn : 1; 2711 unsigned char TypeQuals; 2712 ExceptionSpecificationType ExceptionSpecType; 2713 RefQualifierKind RefQualifier; 2714 unsigned NumExceptions; 2715 const QualType *Exceptions; 2716 Expr *NoexceptExpr; 2717 FunctionDecl *ExceptionSpecDecl; 2718 FunctionDecl *ExceptionSpecTemplate; 2719 const bool *ConsumedArguments; 2720 }; 2721 2722 private: 2723 /// \brief Determine whether there are any argument types that 2724 /// contain an unexpanded parameter pack. 2725 static bool containsAnyUnexpandedParameterPack(const QualType *ArgArray, 2726 unsigned numArgs) { 2727 for (unsigned Idx = 0; Idx < numArgs; ++Idx) 2728 if (ArgArray[Idx]->containsUnexpandedParameterPack()) 2729 return true; 2730 2731 return false; 2732 } 2733 2734 FunctionProtoType(QualType result, const QualType *args, unsigned numArgs, 2735 QualType canonical, const ExtProtoInfo &epi); 2736 2737 /// NumArgs - The number of arguments this function has, not counting '...'. 2738 unsigned NumArgs : 17; 2739 2740 /// NumExceptions - The number of types in the exception spec, if any. 2741 unsigned NumExceptions : 9; 2742 2743 /// ExceptionSpecType - The type of exception specification this function has. 2744 unsigned ExceptionSpecType : 3; 2745 2746 /// HasAnyConsumedArgs - Whether this function has any consumed arguments. 2747 unsigned HasAnyConsumedArgs : 1; 2748 2749 /// Variadic - Whether the function is variadic. 2750 unsigned Variadic : 1; 2751 2752 /// HasTrailingReturn - Whether this function has a trailing return type. 2753 unsigned HasTrailingReturn : 1; 2754 2755 // ArgInfo - There is an variable size array after the class in memory that 2756 // holds the argument types. 2757 2758 // Exceptions - There is another variable size array after ArgInfo that 2759 // holds the exception types. 2760 2761 // NoexceptExpr - Instead of Exceptions, there may be a single Expr* pointing 2762 // to the expression in the noexcept() specifier. 2763 2764 // ExceptionSpecDecl, ExceptionSpecTemplate - Instead of Exceptions, there may 2765 // be a pair of FunctionDecl* pointing to the function which should be used to 2766 // instantiate this function type's exception specification, and the function 2767 // from which it should be instantiated. 2768 2769 // ConsumedArgs - A variable size array, following Exceptions 2770 // and of length NumArgs, holding flags indicating which arguments 2771 // are consumed. This only appears if HasAnyConsumedArgs is true. 2772 2773 friend class ASTContext; // ASTContext creates these. 2774 2775 const bool *getConsumedArgsBuffer() const { 2776 assert(hasAnyConsumedArgs()); 2777 2778 // Find the end of the exceptions. 2779 Expr * const *eh_end = reinterpret_cast<Expr * const *>(arg_type_end()); 2780 if (getExceptionSpecType() != EST_ComputedNoexcept) 2781 eh_end += NumExceptions; 2782 else 2783 eh_end += 1; // NoexceptExpr 2784 2785 return reinterpret_cast<const bool*>(eh_end); 2786 } 2787 2788 public: 2789 unsigned getNumArgs() const { return NumArgs; } 2790 QualType getArgType(unsigned i) const { 2791 assert(i < NumArgs && "Invalid argument number!"); 2792 return arg_type_begin()[i]; 2793 } 2794 2795 ExtProtoInfo getExtProtoInfo() const { 2796 ExtProtoInfo EPI; 2797 EPI.ExtInfo = getExtInfo(); 2798 EPI.Variadic = isVariadic(); 2799 EPI.HasTrailingReturn = hasTrailingReturn(); 2800 EPI.ExceptionSpecType = getExceptionSpecType(); 2801 EPI.TypeQuals = static_cast<unsigned char>(getTypeQuals()); 2802 EPI.RefQualifier = getRefQualifier(); 2803 if (EPI.ExceptionSpecType == EST_Dynamic) { 2804 EPI.NumExceptions = NumExceptions; 2805 EPI.Exceptions = exception_begin(); 2806 } else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) { 2807 EPI.NoexceptExpr = getNoexceptExpr(); 2808 } else if (EPI.ExceptionSpecType == EST_Uninstantiated) { 2809 EPI.ExceptionSpecDecl = getExceptionSpecDecl(); 2810 EPI.ExceptionSpecTemplate = getExceptionSpecTemplate(); 2811 } 2812 if (hasAnyConsumedArgs()) 2813 EPI.ConsumedArguments = getConsumedArgsBuffer(); 2814 return EPI; 2815 } 2816 2817 /// \brief Get the kind of exception specification on this function. 2818 ExceptionSpecificationType getExceptionSpecType() const { 2819 return static_cast<ExceptionSpecificationType>(ExceptionSpecType); 2820 } 2821 /// \brief Return whether this function has any kind of exception spec. 2822 bool hasExceptionSpec() const { 2823 return getExceptionSpecType() != EST_None; 2824 } 2825 /// \brief Return whether this function has a dynamic (throw) exception spec. 2826 bool hasDynamicExceptionSpec() const { 2827 return isDynamicExceptionSpec(getExceptionSpecType()); 2828 } 2829 /// \brief Return whether this function has a noexcept exception spec. 2830 bool hasNoexceptExceptionSpec() const { 2831 return isNoexceptExceptionSpec(getExceptionSpecType()); 2832 } 2833 /// \brief Result type of getNoexceptSpec(). 2834 enum NoexceptResult { 2835 NR_NoNoexcept, ///< There is no noexcept specifier. 2836 NR_BadNoexcept, ///< The noexcept specifier has a bad expression. 2837 NR_Dependent, ///< The noexcept specifier is dependent. 2838 NR_Throw, ///< The noexcept specifier evaluates to false. 2839 NR_Nothrow ///< The noexcept specifier evaluates to true. 2840 }; 2841 /// \brief Get the meaning of the noexcept spec on this function, if any. 2842 NoexceptResult getNoexceptSpec(ASTContext &Ctx) const; 2843 unsigned getNumExceptions() const { return NumExceptions; } 2844 QualType getExceptionType(unsigned i) const { 2845 assert(i < NumExceptions && "Invalid exception number!"); 2846 return exception_begin()[i]; 2847 } 2848 Expr *getNoexceptExpr() const { 2849 if (getExceptionSpecType() != EST_ComputedNoexcept) 2850 return 0; 2851 // NoexceptExpr sits where the arguments end. 2852 return *reinterpret_cast<Expr *const *>(arg_type_end()); 2853 } 2854 /// \brief If this function type has an uninstantiated exception 2855 /// specification, this is the function whose exception specification 2856 /// is represented by this type. 2857 FunctionDecl *getExceptionSpecDecl() const { 2858 if (getExceptionSpecType() != EST_Uninstantiated) 2859 return 0; 2860 return reinterpret_cast<FunctionDecl * const *>(arg_type_end())[0]; 2861 } 2862 /// \brief If this function type has an uninstantiated exception 2863 /// specification, this is the function whose exception specification 2864 /// should be instantiated to find the exception specification for 2865 /// this type. 2866 FunctionDecl *getExceptionSpecTemplate() const { 2867 if (getExceptionSpecType() != EST_Uninstantiated) 2868 return 0; 2869 return reinterpret_cast<FunctionDecl * const *>(arg_type_end())[1]; 2870 } 2871 bool isNothrow(ASTContext &Ctx) const { 2872 ExceptionSpecificationType EST = getExceptionSpecType(); 2873 assert(EST != EST_Delayed && EST != EST_Uninstantiated); 2874 if (EST == EST_DynamicNone || EST == EST_BasicNoexcept) 2875 return true; 2876 if (EST != EST_ComputedNoexcept) 2877 return false; 2878 return getNoexceptSpec(Ctx) == NR_Nothrow; 2879 } 2880 2881 bool isVariadic() const { return Variadic; } 2882 2883 /// \brief Determines whether this function prototype contains a 2884 /// parameter pack at the end. 2885 /// 2886 /// A function template whose last parameter is a parameter pack can be 2887 /// called with an arbitrary number of arguments, much like a variadic 2888 /// function. 2889 bool isTemplateVariadic() const; 2890 2891 bool hasTrailingReturn() const { return HasTrailingReturn; } 2892 2893 unsigned getTypeQuals() const { return FunctionType::getTypeQuals(); } 2894 2895 2896 /// \brief Retrieve the ref-qualifier associated with this function type. 2897 RefQualifierKind getRefQualifier() const { 2898 return FunctionType::getRefQualifier(); 2899 } 2900 2901 typedef const QualType *arg_type_iterator; 2902 arg_type_iterator arg_type_begin() const { 2903 return reinterpret_cast<const QualType *>(this+1); 2904 } 2905 arg_type_iterator arg_type_end() const { return arg_type_begin()+NumArgs; } 2906 2907 typedef const QualType *exception_iterator; 2908 exception_iterator exception_begin() const { 2909 // exceptions begin where arguments end 2910 return arg_type_end(); 2911 } 2912 exception_iterator exception_end() const { 2913 if (getExceptionSpecType() != EST_Dynamic) 2914 return exception_begin(); 2915 return exception_begin() + NumExceptions; 2916 } 2917 2918 bool hasAnyConsumedArgs() const { 2919 return HasAnyConsumedArgs; 2920 } 2921 bool isArgConsumed(unsigned I) const { 2922 assert(I < getNumArgs() && "argument index out of range!"); 2923 if (hasAnyConsumedArgs()) 2924 return getConsumedArgsBuffer()[I]; 2925 return false; 2926 } 2927 2928 bool isSugared() const { return false; } 2929 QualType desugar() const { return QualType(this, 0); } 2930 2931 void printExceptionSpecification(std::string &S, 2932 PrintingPolicy Policy) const; 2933 2934 static bool classof(const Type *T) { 2935 return T->getTypeClass() == FunctionProto; 2936 } 2937 static bool classof(const FunctionProtoType *) { return true; } 2938 2939 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx); 2940 static void Profile(llvm::FoldingSetNodeID &ID, QualType Result, 2941 arg_type_iterator ArgTys, unsigned NumArgs, 2942 const ExtProtoInfo &EPI, const ASTContext &Context); 2943 }; 2944 2945 2946 /// \brief Represents the dependent type named by a dependently-scoped 2947 /// typename using declaration, e.g. 2948 /// using typename Base<T>::foo; 2949 /// Template instantiation turns these into the underlying type. 2950 class UnresolvedUsingType : public Type { 2951 UnresolvedUsingTypenameDecl *Decl; 2952 2953 UnresolvedUsingType(const UnresolvedUsingTypenameDecl *D) 2954 : Type(UnresolvedUsing, QualType(), true, true, false, 2955 /*ContainsUnexpandedParameterPack=*/false), 2956 Decl(const_cast<UnresolvedUsingTypenameDecl*>(D)) {} 2957 friend class ASTContext; // ASTContext creates these. 2958 public: 2959 2960 UnresolvedUsingTypenameDecl *getDecl() const { return Decl; } 2961 2962 bool isSugared() const { return false; } 2963 QualType desugar() const { return QualType(this, 0); } 2964 2965 static bool classof(const Type *T) { 2966 return T->getTypeClass() == UnresolvedUsing; 2967 } 2968 static bool classof(const UnresolvedUsingType *) { return true; } 2969 2970 void Profile(llvm::FoldingSetNodeID &ID) { 2971 return Profile(ID, Decl); 2972 } 2973 static void Profile(llvm::FoldingSetNodeID &ID, 2974 UnresolvedUsingTypenameDecl *D) { 2975 ID.AddPointer(D); 2976 } 2977 }; 2978 2979 2980 class TypedefType : public Type { 2981 TypedefNameDecl *Decl; 2982 protected: 2983 TypedefType(TypeClass tc, const TypedefNameDecl *D, QualType can) 2984 : Type(tc, can, can->isDependentType(), 2985 can->isInstantiationDependentType(), 2986 can->isVariablyModifiedType(), 2987 /*ContainsUnexpandedParameterPack=*/false), 2988 Decl(const_cast<TypedefNameDecl*>(D)) { 2989 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 2990 } 2991 friend class ASTContext; // ASTContext creates these. 2992 public: 2993 2994 TypedefNameDecl *getDecl() const { return Decl; } 2995 2996 bool isSugared() const { return true; } 2997 QualType desugar() const; 2998 2999 static bool classof(const Type *T) { return T->getTypeClass() == Typedef; } 3000 static bool classof(const TypedefType *) { return true; } 3001 }; 3002 3003 /// TypeOfExprType (GCC extension). 3004 class TypeOfExprType : public Type { 3005 Expr *TOExpr; 3006 3007 protected: 3008 TypeOfExprType(Expr *E, QualType can = QualType()); 3009 friend class ASTContext; // ASTContext creates these. 3010 public: 3011 Expr *getUnderlyingExpr() const { return TOExpr; } 3012 3013 /// \brief Remove a single level of sugar. 3014 QualType desugar() const; 3015 3016 /// \brief Returns whether this type directly provides sugar. 3017 bool isSugared() const; 3018 3019 static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; } 3020 static bool classof(const TypeOfExprType *) { return true; } 3021 }; 3022 3023 /// \brief Internal representation of canonical, dependent 3024 /// typeof(expr) types. 3025 /// 3026 /// This class is used internally by the ASTContext to manage 3027 /// canonical, dependent types, only. Clients will only see instances 3028 /// of this class via TypeOfExprType nodes. 3029 class DependentTypeOfExprType 3030 : public TypeOfExprType, public llvm::FoldingSetNode { 3031 const ASTContext &Context; 3032 3033 public: 3034 DependentTypeOfExprType(const ASTContext &Context, Expr *E) 3035 : TypeOfExprType(E), Context(Context) { } 3036 3037 void Profile(llvm::FoldingSetNodeID &ID) { 3038 Profile(ID, Context, getUnderlyingExpr()); 3039 } 3040 3041 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 3042 Expr *E); 3043 }; 3044 3045 /// TypeOfType (GCC extension). 3046 class TypeOfType : public Type { 3047 QualType TOType; 3048 TypeOfType(QualType T, QualType can) 3049 : Type(TypeOf, can, T->isDependentType(), 3050 T->isInstantiationDependentType(), 3051 T->isVariablyModifiedType(), 3052 T->containsUnexpandedParameterPack()), 3053 TOType(T) { 3054 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 3055 } 3056 friend class ASTContext; // ASTContext creates these. 3057 public: 3058 QualType getUnderlyingType() const { return TOType; } 3059 3060 /// \brief Remove a single level of sugar. 3061 QualType desugar() const { return getUnderlyingType(); } 3062 3063 /// \brief Returns whether this type directly provides sugar. 3064 bool isSugared() const { return true; } 3065 3066 static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; } 3067 static bool classof(const TypeOfType *) { return true; } 3068 }; 3069 3070 /// DecltypeType (C++0x) 3071 class DecltypeType : public Type { 3072 Expr *E; 3073 QualType UnderlyingType; 3074 3075 protected: 3076 DecltypeType(Expr *E, QualType underlyingType, QualType can = QualType()); 3077 friend class ASTContext; // ASTContext creates these. 3078 public: 3079 Expr *getUnderlyingExpr() const { return E; } 3080 QualType getUnderlyingType() const { return UnderlyingType; } 3081 3082 /// \brief Remove a single level of sugar. 3083 QualType desugar() const; 3084 3085 /// \brief Returns whether this type directly provides sugar. 3086 bool isSugared() const; 3087 3088 static bool classof(const Type *T) { return T->getTypeClass() == Decltype; } 3089 static bool classof(const DecltypeType *) { return true; } 3090 }; 3091 3092 /// \brief Internal representation of canonical, dependent 3093 /// decltype(expr) types. 3094 /// 3095 /// This class is used internally by the ASTContext to manage 3096 /// canonical, dependent types, only. Clients will only see instances 3097 /// of this class via DecltypeType nodes. 3098 class DependentDecltypeType : public DecltypeType, public llvm::FoldingSetNode { 3099 const ASTContext &Context; 3100 3101 public: 3102 DependentDecltypeType(const ASTContext &Context, Expr *E); 3103 3104 void Profile(llvm::FoldingSetNodeID &ID) { 3105 Profile(ID, Context, getUnderlyingExpr()); 3106 } 3107 3108 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 3109 Expr *E); 3110 }; 3111 3112 /// \brief A unary type transform, which is a type constructed from another 3113 class UnaryTransformType : public Type { 3114 public: 3115 enum UTTKind { 3116 EnumUnderlyingType 3117 }; 3118 3119 private: 3120 /// The untransformed type. 3121 QualType BaseType; 3122 /// The transformed type if not dependent, otherwise the same as BaseType. 3123 QualType UnderlyingType; 3124 3125 UTTKind UKind; 3126 protected: 3127 UnaryTransformType(QualType BaseTy, QualType UnderlyingTy, UTTKind UKind, 3128 QualType CanonicalTy); 3129 friend class ASTContext; 3130 public: 3131 bool isSugared() const { return !isDependentType(); } 3132 QualType desugar() const { return UnderlyingType; } 3133 3134 QualType getUnderlyingType() const { return UnderlyingType; } 3135 QualType getBaseType() const { return BaseType; } 3136 3137 UTTKind getUTTKind() const { return UKind; } 3138 3139 static bool classof(const Type *T) { 3140 return T->getTypeClass() == UnaryTransform; 3141 } 3142 static bool classof(const UnaryTransformType *) { return true; } 3143 }; 3144 3145 class TagType : public Type { 3146 /// Stores the TagDecl associated with this type. The decl may point to any 3147 /// TagDecl that declares the entity. 3148 TagDecl * decl; 3149 3150 friend class ASTReader; 3151 3152 protected: 3153 TagType(TypeClass TC, const TagDecl *D, QualType can); 3154 3155 public: 3156 TagDecl *getDecl() const; 3157 3158 /// @brief Determines whether this type is in the process of being 3159 /// defined. 3160 bool isBeingDefined() const; 3161 3162 static bool classof(const Type *T) { 3163 return T->getTypeClass() >= TagFirst && T->getTypeClass() <= TagLast; 3164 } 3165 static bool classof(const TagType *) { return true; } 3166 }; 3167 3168 /// RecordType - This is a helper class that allows the use of isa/cast/dyncast 3169 /// to detect TagType objects of structs/unions/classes. 3170 class RecordType : public TagType { 3171 protected: 3172 explicit RecordType(const RecordDecl *D) 3173 : TagType(Record, reinterpret_cast<const TagDecl*>(D), QualType()) { } 3174 explicit RecordType(TypeClass TC, RecordDecl *D) 3175 : TagType(TC, reinterpret_cast<const TagDecl*>(D), QualType()) { } 3176 friend class ASTContext; // ASTContext creates these. 3177 public: 3178 3179 RecordDecl *getDecl() const { 3180 return reinterpret_cast<RecordDecl*>(TagType::getDecl()); 3181 } 3182 3183 // FIXME: This predicate is a helper to QualType/Type. It needs to 3184 // recursively check all fields for const-ness. If any field is declared 3185 // const, it needs to return false. 3186 bool hasConstFields() const { return false; } 3187 3188 bool isSugared() const { return false; } 3189 QualType desugar() const { return QualType(this, 0); } 3190 3191 static bool classof(const Type *T) { return T->getTypeClass() == Record; } 3192 static bool classof(const RecordType *) { return true; } 3193 }; 3194 3195 /// EnumType - This is a helper class that allows the use of isa/cast/dyncast 3196 /// to detect TagType objects of enums. 3197 class EnumType : public TagType { 3198 explicit EnumType(const EnumDecl *D) 3199 : TagType(Enum, reinterpret_cast<const TagDecl*>(D), QualType()) { } 3200 friend class ASTContext; // ASTContext creates these. 3201 public: 3202 3203 EnumDecl *getDecl() const { 3204 return reinterpret_cast<EnumDecl*>(TagType::getDecl()); 3205 } 3206 3207 bool isSugared() const { return false; } 3208 QualType desugar() const { return QualType(this, 0); } 3209 3210 static bool classof(const Type *T) { return T->getTypeClass() == Enum; } 3211 static bool classof(const EnumType *) { return true; } 3212 }; 3213 3214 /// AttributedType - An attributed type is a type to which a type 3215 /// attribute has been applied. The "modified type" is the 3216 /// fully-sugared type to which the attributed type was applied; 3217 /// generally it is not canonically equivalent to the attributed type. 3218 /// The "equivalent type" is the minimally-desugared type which the 3219 /// type is canonically equivalent to. 3220 /// 3221 /// For example, in the following attributed type: 3222 /// int32_t __attribute__((vector_size(16))) 3223 /// - the modified type is the TypedefType for int32_t 3224 /// - the equivalent type is VectorType(16, int32_t) 3225 /// - the canonical type is VectorType(16, int) 3226 class AttributedType : public Type, public llvm::FoldingSetNode { 3227 public: 3228 // It is really silly to have yet another attribute-kind enum, but 3229 // clang::attr::Kind doesn't currently cover the pure type attrs. 3230 enum Kind { 3231 // Expression operand. 3232 attr_address_space, 3233 attr_regparm, 3234 attr_vector_size, 3235 attr_neon_vector_type, 3236 attr_neon_polyvector_type, 3237 3238 FirstExprOperandKind = attr_address_space, 3239 LastExprOperandKind = attr_neon_polyvector_type, 3240 3241 // Enumerated operand (string or keyword). 3242 attr_objc_gc, 3243 attr_objc_ownership, 3244 attr_pcs, 3245 3246 FirstEnumOperandKind = attr_objc_gc, 3247 LastEnumOperandKind = attr_pcs, 3248 3249 // No operand. 3250 attr_noreturn, 3251 attr_cdecl, 3252 attr_fastcall, 3253 attr_stdcall, 3254 attr_thiscall, 3255 attr_pascal 3256 }; 3257 3258 private: 3259 QualType ModifiedType; 3260 QualType EquivalentType; 3261 3262 friend class ASTContext; // creates these 3263 3264 AttributedType(QualType canon, Kind attrKind, 3265 QualType modified, QualType equivalent) 3266 : Type(Attributed, canon, canon->isDependentType(), 3267 canon->isInstantiationDependentType(), 3268 canon->isVariablyModifiedType(), 3269 canon->containsUnexpandedParameterPack()), 3270 ModifiedType(modified), EquivalentType(equivalent) { 3271 AttributedTypeBits.AttrKind = attrKind; 3272 } 3273 3274 public: 3275 Kind getAttrKind() const { 3276 return static_cast<Kind>(AttributedTypeBits.AttrKind); 3277 } 3278 3279 QualType getModifiedType() const { return ModifiedType; } 3280 QualType getEquivalentType() const { return EquivalentType; } 3281 3282 bool isSugared() const { return true; } 3283 QualType desugar() const { return getEquivalentType(); } 3284 3285 void Profile(llvm::FoldingSetNodeID &ID) { 3286 Profile(ID, getAttrKind(), ModifiedType, EquivalentType); 3287 } 3288 3289 static void Profile(llvm::FoldingSetNodeID &ID, Kind attrKind, 3290 QualType modified, QualType equivalent) { 3291 ID.AddInteger(attrKind); 3292 ID.AddPointer(modified.getAsOpaquePtr()); 3293 ID.AddPointer(equivalent.getAsOpaquePtr()); 3294 } 3295 3296 static bool classof(const Type *T) { 3297 return T->getTypeClass() == Attributed; 3298 } 3299 static bool classof(const AttributedType *T) { return true; } 3300 }; 3301 3302 class TemplateTypeParmType : public Type, public llvm::FoldingSetNode { 3303 // Helper data collector for canonical types. 3304 struct CanonicalTTPTInfo { 3305 unsigned Depth : 15; 3306 unsigned ParameterPack : 1; 3307 unsigned Index : 16; 3308 }; 3309 3310 union { 3311 // Info for the canonical type. 3312 CanonicalTTPTInfo CanTTPTInfo; 3313 // Info for the non-canonical type. 3314 TemplateTypeParmDecl *TTPDecl; 3315 }; 3316 3317 /// Build a non-canonical type. 3318 TemplateTypeParmType(TemplateTypeParmDecl *TTPDecl, QualType Canon) 3319 : Type(TemplateTypeParm, Canon, /*Dependent=*/true, 3320 /*InstantiationDependent=*/true, 3321 /*VariablyModified=*/false, 3322 Canon->containsUnexpandedParameterPack()), 3323 TTPDecl(TTPDecl) { } 3324 3325 /// Build the canonical type. 3326 TemplateTypeParmType(unsigned D, unsigned I, bool PP) 3327 : Type(TemplateTypeParm, QualType(this, 0), 3328 /*Dependent=*/true, 3329 /*InstantiationDependent=*/true, 3330 /*VariablyModified=*/false, PP) { 3331 CanTTPTInfo.Depth = D; 3332 CanTTPTInfo.Index = I; 3333 CanTTPTInfo.ParameterPack = PP; 3334 } 3335 3336 friend class ASTContext; // ASTContext creates these 3337 3338 const CanonicalTTPTInfo& getCanTTPTInfo() const { 3339 QualType Can = getCanonicalTypeInternal(); 3340 return Can->castAs<TemplateTypeParmType>()->CanTTPTInfo; 3341 } 3342 3343 public: 3344 unsigned getDepth() const { return getCanTTPTInfo().Depth; } 3345 unsigned getIndex() const { return getCanTTPTInfo().Index; } 3346 bool isParameterPack() const { return getCanTTPTInfo().ParameterPack; } 3347 3348 TemplateTypeParmDecl *getDecl() const { 3349 return isCanonicalUnqualified() ? 0 : TTPDecl; 3350 } 3351 3352 IdentifierInfo *getIdentifier() const; 3353 3354 bool isSugared() const { return false; } 3355 QualType desugar() const { return QualType(this, 0); } 3356 3357 void Profile(llvm::FoldingSetNodeID &ID) { 3358 Profile(ID, getDepth(), getIndex(), isParameterPack(), getDecl()); 3359 } 3360 3361 static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth, 3362 unsigned Index, bool ParameterPack, 3363 TemplateTypeParmDecl *TTPDecl) { 3364 ID.AddInteger(Depth); 3365 ID.AddInteger(Index); 3366 ID.AddBoolean(ParameterPack); 3367 ID.AddPointer(TTPDecl); 3368 } 3369 3370 static bool classof(const Type *T) { 3371 return T->getTypeClass() == TemplateTypeParm; 3372 } 3373 static bool classof(const TemplateTypeParmType *T) { return true; } 3374 }; 3375 3376 /// \brief Represents the result of substituting a type for a template 3377 /// type parameter. 3378 /// 3379 /// Within an instantiated template, all template type parameters have 3380 /// been replaced with these. They are used solely to record that a 3381 /// type was originally written as a template type parameter; 3382 /// therefore they are never canonical. 3383 class SubstTemplateTypeParmType : public Type, public llvm::FoldingSetNode { 3384 // The original type parameter. 3385 const TemplateTypeParmType *Replaced; 3386 3387 SubstTemplateTypeParmType(const TemplateTypeParmType *Param, QualType Canon) 3388 : Type(SubstTemplateTypeParm, Canon, Canon->isDependentType(), 3389 Canon->isInstantiationDependentType(), 3390 Canon->isVariablyModifiedType(), 3391 Canon->containsUnexpandedParameterPack()), 3392 Replaced(Param) { } 3393 3394 friend class ASTContext; 3395 3396 public: 3397 /// Gets the template parameter that was substituted for. 3398 const TemplateTypeParmType *getReplacedParameter() const { 3399 return Replaced; 3400 } 3401 3402 /// Gets the type that was substituted for the template 3403 /// parameter. 3404 QualType getReplacementType() const { 3405 return getCanonicalTypeInternal(); 3406 } 3407 3408 bool isSugared() const { return true; } 3409 QualType desugar() const { return getReplacementType(); } 3410 3411 void Profile(llvm::FoldingSetNodeID &ID) { 3412 Profile(ID, getReplacedParameter(), getReplacementType()); 3413 } 3414 static void Profile(llvm::FoldingSetNodeID &ID, 3415 const TemplateTypeParmType *Replaced, 3416 QualType Replacement) { 3417 ID.AddPointer(Replaced); 3418 ID.AddPointer(Replacement.getAsOpaquePtr()); 3419 } 3420 3421 static bool classof(const Type *T) { 3422 return T->getTypeClass() == SubstTemplateTypeParm; 3423 } 3424 static bool classof(const SubstTemplateTypeParmType *T) { return true; } 3425 }; 3426 3427 /// \brief Represents the result of substituting a set of types for a template 3428 /// type parameter pack. 3429 /// 3430 /// When a pack expansion in the source code contains multiple parameter packs 3431 /// and those parameter packs correspond to different levels of template 3432 /// parameter lists, this type node is used to represent a template type 3433 /// parameter pack from an outer level, which has already had its argument pack 3434 /// substituted but that still lives within a pack expansion that itself 3435 /// could not be instantiated. When actually performing a substitution into 3436 /// that pack expansion (e.g., when all template parameters have corresponding 3437 /// arguments), this type will be replaced with the \c SubstTemplateTypeParmType 3438 /// at the current pack substitution index. 3439 class SubstTemplateTypeParmPackType : public Type, public llvm::FoldingSetNode { 3440 /// \brief The original type parameter. 3441 const TemplateTypeParmType *Replaced; 3442 3443 /// \brief A pointer to the set of template arguments that this 3444 /// parameter pack is instantiated with. 3445 const TemplateArgument *Arguments; 3446 3447 /// \brief The number of template arguments in \c Arguments. 3448 unsigned NumArguments; 3449 3450 SubstTemplateTypeParmPackType(const TemplateTypeParmType *Param, 3451 QualType Canon, 3452 const TemplateArgument &ArgPack); 3453 3454 friend class ASTContext; 3455 3456 public: 3457 IdentifierInfo *getIdentifier() const { return Replaced->getIdentifier(); } 3458 3459 /// Gets the template parameter that was substituted for. 3460 const TemplateTypeParmType *getReplacedParameter() const { 3461 return Replaced; 3462 } 3463 3464 bool isSugared() const { return false; } 3465 QualType desugar() const { return QualType(this, 0); } 3466 3467 TemplateArgument getArgumentPack() const; 3468 3469 void Profile(llvm::FoldingSetNodeID &ID); 3470 static void Profile(llvm::FoldingSetNodeID &ID, 3471 const TemplateTypeParmType *Replaced, 3472 const TemplateArgument &ArgPack); 3473 3474 static bool classof(const Type *T) { 3475 return T->getTypeClass() == SubstTemplateTypeParmPack; 3476 } 3477 static bool classof(const SubstTemplateTypeParmPackType *T) { return true; } 3478 }; 3479 3480 /// \brief Represents a C++0x auto type. 3481 /// 3482 /// These types are usually a placeholder for a deduced type. However, within 3483 /// templates and before the initializer is attached, there is no deduced type 3484 /// and an auto type is type-dependent and canonical. 3485 class AutoType : public Type, public llvm::FoldingSetNode { 3486 AutoType(QualType DeducedType) 3487 : Type(Auto, DeducedType.isNull() ? QualType(this, 0) : DeducedType, 3488 /*Dependent=*/DeducedType.isNull(), 3489 /*InstantiationDependent=*/DeducedType.isNull(), 3490 /*VariablyModified=*/false, /*ContainsParameterPack=*/false) { 3491 assert((DeducedType.isNull() || !DeducedType->isDependentType()) && 3492 "deduced a dependent type for auto"); 3493 } 3494 3495 friend class ASTContext; // ASTContext creates these 3496 3497 public: 3498 bool isSugared() const { return isDeduced(); } 3499 QualType desugar() const { return getCanonicalTypeInternal(); } 3500 3501 QualType getDeducedType() const { 3502 return isDeduced() ? getCanonicalTypeInternal() : QualType(); 3503 } 3504 bool isDeduced() const { 3505 return !isDependentType(); 3506 } 3507 3508 void Profile(llvm::FoldingSetNodeID &ID) { 3509 Profile(ID, getDeducedType()); 3510 } 3511 3512 static void Profile(llvm::FoldingSetNodeID &ID, 3513 QualType Deduced) { 3514 ID.AddPointer(Deduced.getAsOpaquePtr()); 3515 } 3516 3517 static bool classof(const Type *T) { 3518 return T->getTypeClass() == Auto; 3519 } 3520 static bool classof(const AutoType *T) { return true; } 3521 }; 3522 3523 /// \brief Represents a type template specialization; the template 3524 /// must be a class template, a type alias template, or a template 3525 /// template parameter. A template which cannot be resolved to one of 3526 /// these, e.g. because it is written with a dependent scope 3527 /// specifier, is instead represented as a 3528 /// @c DependentTemplateSpecializationType. 3529 /// 3530 /// A non-dependent template specialization type is always "sugar", 3531 /// typically for a @c RecordType. For example, a class template 3532 /// specialization type of @c vector<int> will refer to a tag type for 3533 /// the instantiation @c std::vector<int, std::allocator<int>> 3534 /// 3535 /// Template specializations are dependent if either the template or 3536 /// any of the template arguments are dependent, in which case the 3537 /// type may also be canonical. 3538 /// 3539 /// Instances of this type are allocated with a trailing array of 3540 /// TemplateArguments, followed by a QualType representing the 3541 /// non-canonical aliased type when the template is a type alias 3542 /// template. 3543 class TemplateSpecializationType 3544 : public Type, public llvm::FoldingSetNode { 3545 /// \brief The name of the template being specialized. This is 3546 /// either a TemplateName::Template (in which case it is a 3547 /// ClassTemplateDecl*, a TemplateTemplateParmDecl*, or a 3548 /// TypeAliasTemplateDecl*), a 3549 /// TemplateName::SubstTemplateTemplateParmPack, or a 3550 /// TemplateName::SubstTemplateTemplateParm (in which case the 3551 /// replacement must, recursively, be one of these). 3552 TemplateName Template; 3553 3554 /// \brief - The number of template arguments named in this class 3555 /// template specialization. 3556 unsigned NumArgs : 31; 3557 3558 /// \brief Whether this template specialization type is a substituted 3559 /// type alias. 3560 bool TypeAlias : 1; 3561 3562 TemplateSpecializationType(TemplateName T, 3563 const TemplateArgument *Args, 3564 unsigned NumArgs, QualType Canon, 3565 QualType Aliased); 3566 3567 friend class ASTContext; // ASTContext creates these 3568 3569 public: 3570 /// \brief Determine whether any of the given template arguments are 3571 /// dependent. 3572 static bool anyDependentTemplateArguments(const TemplateArgument *Args, 3573 unsigned NumArgs, 3574 bool &InstantiationDependent); 3575 3576 static bool anyDependentTemplateArguments(const TemplateArgumentLoc *Args, 3577 unsigned NumArgs, 3578 bool &InstantiationDependent); 3579 3580 static bool anyDependentTemplateArguments(const TemplateArgumentListInfo &, 3581 bool &InstantiationDependent); 3582 3583 /// \brief Print a template argument list, including the '<' and '>' 3584 /// enclosing the template arguments. 3585 static std::string PrintTemplateArgumentList(const TemplateArgument *Args, 3586 unsigned NumArgs, 3587 const PrintingPolicy &Policy, 3588 bool SkipBrackets = false); 3589 3590 static std::string PrintTemplateArgumentList(const TemplateArgumentLoc *Args, 3591 unsigned NumArgs, 3592 const PrintingPolicy &Policy); 3593 3594 static std::string PrintTemplateArgumentList(const TemplateArgumentListInfo &, 3595 const PrintingPolicy &Policy); 3596 3597 /// True if this template specialization type matches a current 3598 /// instantiation in the context in which it is found. 3599 bool isCurrentInstantiation() const { 3600 return isa<InjectedClassNameType>(getCanonicalTypeInternal()); 3601 } 3602 3603 /// \brief Determine if this template specialization type is for a type alias 3604 /// template that has been substituted. 3605 /// 3606 /// Nearly every template specialization type whose template is an alias 3607 /// template will be substituted. However, this is not the case when 3608 /// the specialization contains a pack expansion but the template alias 3609 /// does not have a corresponding parameter pack, e.g., 3610 /// 3611 /// \code 3612 /// template<typename T, typename U, typename V> struct S; 3613 /// template<typename T, typename U> using A = S<T, int, U>; 3614 /// template<typename... Ts> struct X { 3615 /// typedef A<Ts...> type; // not a type alias 3616 /// }; 3617 /// \endcode 3618 bool isTypeAlias() const { return TypeAlias; } 3619 3620 /// Get the aliased type, if this is a specialization of a type alias 3621 /// template. 3622 QualType getAliasedType() const { 3623 assert(isTypeAlias() && "not a type alias template specialization"); 3624 return *reinterpret_cast<const QualType*>(end()); 3625 } 3626 3627 typedef const TemplateArgument * iterator; 3628 3629 iterator begin() const { return getArgs(); } 3630 iterator end() const; // defined inline in TemplateBase.h 3631 3632 /// \brief Retrieve the name of the template that we are specializing. 3633 TemplateName getTemplateName() const { return Template; } 3634 3635 /// \brief Retrieve the template arguments. 3636 const TemplateArgument *getArgs() const { 3637 return reinterpret_cast<const TemplateArgument *>(this + 1); 3638 } 3639 3640 /// \brief Retrieve the number of template arguments. 3641 unsigned getNumArgs() const { return NumArgs; } 3642 3643 /// \brief Retrieve a specific template argument as a type. 3644 /// \precondition @c isArgType(Arg) 3645 const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h 3646 3647 bool isSugared() const { 3648 return !isDependentType() || isCurrentInstantiation() || isTypeAlias(); 3649 } 3650 QualType desugar() const { return getCanonicalTypeInternal(); } 3651 3652 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) { 3653 Profile(ID, Template, getArgs(), NumArgs, Ctx); 3654 if (isTypeAlias()) 3655 getAliasedType().Profile(ID); 3656 } 3657 3658 static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T, 3659 const TemplateArgument *Args, 3660 unsigned NumArgs, 3661 const ASTContext &Context); 3662 3663 static bool classof(const Type *T) { 3664 return T->getTypeClass() == TemplateSpecialization; 3665 } 3666 static bool classof(const TemplateSpecializationType *T) { return true; } 3667 }; 3668 3669 /// \brief The injected class name of a C++ class template or class 3670 /// template partial specialization. Used to record that a type was 3671 /// spelled with a bare identifier rather than as a template-id; the 3672 /// equivalent for non-templated classes is just RecordType. 3673 /// 3674 /// Injected class name types are always dependent. Template 3675 /// instantiation turns these into RecordTypes. 3676 /// 3677 /// Injected class name types are always canonical. This works 3678 /// because it is impossible to compare an injected class name type 3679 /// with the corresponding non-injected template type, for the same 3680 /// reason that it is impossible to directly compare template 3681 /// parameters from different dependent contexts: injected class name 3682 /// types can only occur within the scope of a particular templated 3683 /// declaration, and within that scope every template specialization 3684 /// will canonicalize to the injected class name (when appropriate 3685 /// according to the rules of the language). 3686 class InjectedClassNameType : public Type { 3687 CXXRecordDecl *Decl; 3688 3689 /// The template specialization which this type represents. 3690 /// For example, in 3691 /// template <class T> class A { ... }; 3692 /// this is A<T>, whereas in 3693 /// template <class X, class Y> class A<B<X,Y> > { ... }; 3694 /// this is A<B<X,Y> >. 3695 /// 3696 /// It is always unqualified, always a template specialization type, 3697 /// and always dependent. 3698 QualType InjectedType; 3699 3700 friend class ASTContext; // ASTContext creates these. 3701 friend class ASTReader; // FIXME: ASTContext::getInjectedClassNameType is not 3702 // currently suitable for AST reading, too much 3703 // interdependencies. 3704 InjectedClassNameType(CXXRecordDecl *D, QualType TST) 3705 : Type(InjectedClassName, QualType(), /*Dependent=*/true, 3706 /*InstantiationDependent=*/true, 3707 /*VariablyModified=*/false, 3708 /*ContainsUnexpandedParameterPack=*/false), 3709 Decl(D), InjectedType(TST) { 3710 assert(isa<TemplateSpecializationType>(TST)); 3711 assert(!TST.hasQualifiers()); 3712 assert(TST->isDependentType()); 3713 } 3714 3715 public: 3716 QualType getInjectedSpecializationType() const { return InjectedType; } 3717 const TemplateSpecializationType *getInjectedTST() const { 3718 return cast<TemplateSpecializationType>(InjectedType.getTypePtr()); 3719 } 3720 3721 CXXRecordDecl *getDecl() const; 3722 3723 bool isSugared() const { return false; } 3724 QualType desugar() const { return QualType(this, 0); } 3725 3726 static bool classof(const Type *T) { 3727 return T->getTypeClass() == InjectedClassName; 3728 } 3729 static bool classof(const InjectedClassNameType *T) { return true; } 3730 }; 3731 3732 /// \brief The kind of a tag type. 3733 enum TagTypeKind { 3734 /// \brief The "struct" keyword. 3735 TTK_Struct, 3736 /// \brief The "union" keyword. 3737 TTK_Union, 3738 /// \brief The "class" keyword. 3739 TTK_Class, 3740 /// \brief The "enum" keyword. 3741 TTK_Enum 3742 }; 3743 3744 /// \brief The elaboration keyword that precedes a qualified type name or 3745 /// introduces an elaborated-type-specifier. 3746 enum ElaboratedTypeKeyword { 3747 /// \brief The "struct" keyword introduces the elaborated-type-specifier. 3748 ETK_Struct, 3749 /// \brief The "union" keyword introduces the elaborated-type-specifier. 3750 ETK_Union, 3751 /// \brief The "class" keyword introduces the elaborated-type-specifier. 3752 ETK_Class, 3753 /// \brief The "enum" keyword introduces the elaborated-type-specifier. 3754 ETK_Enum, 3755 /// \brief The "typename" keyword precedes the qualified type name, e.g., 3756 /// \c typename T::type. 3757 ETK_Typename, 3758 /// \brief No keyword precedes the qualified type name. 3759 ETK_None 3760 }; 3761 3762 /// A helper class for Type nodes having an ElaboratedTypeKeyword. 3763 /// The keyword in stored in the free bits of the base class. 3764 /// Also provides a few static helpers for converting and printing 3765 /// elaborated type keyword and tag type kind enumerations. 3766 class TypeWithKeyword : public Type { 3767 protected: 3768 TypeWithKeyword(ElaboratedTypeKeyword Keyword, TypeClass tc, 3769 QualType Canonical, bool Dependent, 3770 bool InstantiationDependent, bool VariablyModified, 3771 bool ContainsUnexpandedParameterPack) 3772 : Type(tc, Canonical, Dependent, InstantiationDependent, VariablyModified, 3773 ContainsUnexpandedParameterPack) { 3774 TypeWithKeywordBits.Keyword = Keyword; 3775 } 3776 3777 public: 3778 ElaboratedTypeKeyword getKeyword() const { 3779 return static_cast<ElaboratedTypeKeyword>(TypeWithKeywordBits.Keyword); 3780 } 3781 3782 /// getKeywordForTypeSpec - Converts a type specifier (DeclSpec::TST) 3783 /// into an elaborated type keyword. 3784 static ElaboratedTypeKeyword getKeywordForTypeSpec(unsigned TypeSpec); 3785 3786 /// getTagTypeKindForTypeSpec - Converts a type specifier (DeclSpec::TST) 3787 /// into a tag type kind. It is an error to provide a type specifier 3788 /// which *isn't* a tag kind here. 3789 static TagTypeKind getTagTypeKindForTypeSpec(unsigned TypeSpec); 3790 3791 /// getKeywordForTagDeclKind - Converts a TagTypeKind into an 3792 /// elaborated type keyword. 3793 static ElaboratedTypeKeyword getKeywordForTagTypeKind(TagTypeKind Tag); 3794 3795 /// getTagTypeKindForKeyword - Converts an elaborated type keyword into 3796 // a TagTypeKind. It is an error to provide an elaborated type keyword 3797 /// which *isn't* a tag kind here. 3798 static TagTypeKind getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword); 3799 3800 static bool KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword); 3801 3802 static const char *getKeywordName(ElaboratedTypeKeyword Keyword); 3803 3804 static const char *getTagTypeKindName(TagTypeKind Kind) { 3805 return getKeywordName(getKeywordForTagTypeKind(Kind)); 3806 } 3807 3808 class CannotCastToThisType {}; 3809 static CannotCastToThisType classof(const Type *); 3810 }; 3811 3812 /// \brief Represents a type that was referred to using an elaborated type 3813 /// keyword, e.g., struct S, or via a qualified name, e.g., N::M::type, 3814 /// or both. 3815 /// 3816 /// This type is used to keep track of a type name as written in the 3817 /// source code, including tag keywords and any nested-name-specifiers. 3818 /// The type itself is always "sugar", used to express what was written 3819 /// in the source code but containing no additional semantic information. 3820 class ElaboratedType : public TypeWithKeyword, public llvm::FoldingSetNode { 3821 3822 /// \brief The nested name specifier containing the qualifier. 3823 NestedNameSpecifier *NNS; 3824 3825 /// \brief The type that this qualified name refers to. 3826 QualType NamedType; 3827 3828 ElaboratedType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, 3829 QualType NamedType, QualType CanonType) 3830 : TypeWithKeyword(Keyword, Elaborated, CanonType, 3831 NamedType->isDependentType(), 3832 NamedType->isInstantiationDependentType(), 3833 NamedType->isVariablyModifiedType(), 3834 NamedType->containsUnexpandedParameterPack()), 3835 NNS(NNS), NamedType(NamedType) { 3836 assert(!(Keyword == ETK_None && NNS == 0) && 3837 "ElaboratedType cannot have elaborated type keyword " 3838 "and name qualifier both null."); 3839 } 3840 3841 friend class ASTContext; // ASTContext creates these 3842 3843 public: 3844 ~ElaboratedType(); 3845 3846 /// \brief Retrieve the qualification on this type. 3847 NestedNameSpecifier *getQualifier() const { return NNS; } 3848 3849 /// \brief Retrieve the type named by the qualified-id. 3850 QualType getNamedType() const { return NamedType; } 3851 3852 /// \brief Remove a single level of sugar. 3853 QualType desugar() const { return getNamedType(); } 3854 3855 /// \brief Returns whether this type directly provides sugar. 3856 bool isSugared() const { return true; } 3857 3858 void Profile(llvm::FoldingSetNodeID &ID) { 3859 Profile(ID, getKeyword(), NNS, NamedType); 3860 } 3861 3862 static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, 3863 NestedNameSpecifier *NNS, QualType NamedType) { 3864 ID.AddInteger(Keyword); 3865 ID.AddPointer(NNS); 3866 NamedType.Profile(ID); 3867 } 3868 3869 static bool classof(const Type *T) { 3870 return T->getTypeClass() == Elaborated; 3871 } 3872 static bool classof(const ElaboratedType *T) { return true; } 3873 }; 3874 3875 /// \brief Represents a qualified type name for which the type name is 3876 /// dependent. 3877 /// 3878 /// DependentNameType represents a class of dependent types that involve a 3879 /// dependent nested-name-specifier (e.g., "T::") followed by a (dependent) 3880 /// name of a type. The DependentNameType may start with a "typename" (for a 3881 /// typename-specifier), "class", "struct", "union", or "enum" (for a 3882 /// dependent elaborated-type-specifier), or nothing (in contexts where we 3883 /// know that we must be referring to a type, e.g., in a base class specifier). 3884 class DependentNameType : public TypeWithKeyword, public llvm::FoldingSetNode { 3885 3886 /// \brief The nested name specifier containing the qualifier. 3887 NestedNameSpecifier *NNS; 3888 3889 /// \brief The type that this typename specifier refers to. 3890 const IdentifierInfo *Name; 3891 3892 DependentNameType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, 3893 const IdentifierInfo *Name, QualType CanonType) 3894 : TypeWithKeyword(Keyword, DependentName, CanonType, /*Dependent=*/true, 3895 /*InstantiationDependent=*/true, 3896 /*VariablyModified=*/false, 3897 NNS->containsUnexpandedParameterPack()), 3898 NNS(NNS), Name(Name) { 3899 assert(NNS->isDependent() && 3900 "DependentNameType requires a dependent nested-name-specifier"); 3901 } 3902 3903 friend class ASTContext; // ASTContext creates these 3904 3905 public: 3906 /// \brief Retrieve the qualification on this type. 3907 NestedNameSpecifier *getQualifier() const { return NNS; } 3908 3909 /// \brief Retrieve the type named by the typename specifier as an 3910 /// identifier. 3911 /// 3912 /// This routine will return a non-NULL identifier pointer when the 3913 /// form of the original typename was terminated by an identifier, 3914 /// e.g., "typename T::type". 3915 const IdentifierInfo *getIdentifier() const { 3916 return Name; 3917 } 3918 3919 bool isSugared() const { return false; } 3920 QualType desugar() const { return QualType(this, 0); } 3921 3922 void Profile(llvm::FoldingSetNodeID &ID) { 3923 Profile(ID, getKeyword(), NNS, Name); 3924 } 3925 3926 static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, 3927 NestedNameSpecifier *NNS, const IdentifierInfo *Name) { 3928 ID.AddInteger(Keyword); 3929 ID.AddPointer(NNS); 3930 ID.AddPointer(Name); 3931 } 3932 3933 static bool classof(const Type *T) { 3934 return T->getTypeClass() == DependentName; 3935 } 3936 static bool classof(const DependentNameType *T) { return true; } 3937 }; 3938 3939 /// DependentTemplateSpecializationType - Represents a template 3940 /// specialization type whose template cannot be resolved, e.g. 3941 /// A<T>::template B<T> 3942 class DependentTemplateSpecializationType : 3943 public TypeWithKeyword, public llvm::FoldingSetNode { 3944 3945 /// \brief The nested name specifier containing the qualifier. 3946 NestedNameSpecifier *NNS; 3947 3948 /// \brief The identifier of the template. 3949 const IdentifierInfo *Name; 3950 3951 /// \brief - The number of template arguments named in this class 3952 /// template specialization. 3953 unsigned NumArgs; 3954 3955 const TemplateArgument *getArgBuffer() const { 3956 return reinterpret_cast<const TemplateArgument*>(this+1); 3957 } 3958 TemplateArgument *getArgBuffer() { 3959 return reinterpret_cast<TemplateArgument*>(this+1); 3960 } 3961 3962 DependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, 3963 NestedNameSpecifier *NNS, 3964 const IdentifierInfo *Name, 3965 unsigned NumArgs, 3966 const TemplateArgument *Args, 3967 QualType Canon); 3968 3969 friend class ASTContext; // ASTContext creates these 3970 3971 public: 3972 NestedNameSpecifier *getQualifier() const { return NNS; } 3973 const IdentifierInfo *getIdentifier() const { return Name; } 3974 3975 /// \brief Retrieve the template arguments. 3976 const TemplateArgument *getArgs() const { 3977 return getArgBuffer(); 3978 } 3979 3980 /// \brief Retrieve the number of template arguments. 3981 unsigned getNumArgs() const { return NumArgs; } 3982 3983 const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h 3984 3985 typedef const TemplateArgument * iterator; 3986 iterator begin() const { return getArgs(); } 3987 iterator end() const; // inline in TemplateBase.h 3988 3989 bool isSugared() const { return false; } 3990 QualType desugar() const { return QualType(this, 0); } 3991 3992 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { 3993 Profile(ID, Context, getKeyword(), NNS, Name, NumArgs, getArgs()); 3994 } 3995 3996 static void Profile(llvm::FoldingSetNodeID &ID, 3997 const ASTContext &Context, 3998 ElaboratedTypeKeyword Keyword, 3999 NestedNameSpecifier *Qualifier, 4000 const IdentifierInfo *Name, 4001 unsigned NumArgs, 4002 const TemplateArgument *Args); 4003 4004 static bool classof(const Type *T) { 4005 return T->getTypeClass() == DependentTemplateSpecialization; 4006 } 4007 static bool classof(const DependentTemplateSpecializationType *T) { 4008 return true; 4009 } 4010 }; 4011 4012 /// \brief Represents a pack expansion of types. 4013 /// 4014 /// Pack expansions are part of C++0x variadic templates. A pack 4015 /// expansion contains a pattern, which itself contains one or more 4016 /// "unexpanded" parameter packs. When instantiated, a pack expansion 4017 /// produces a series of types, each instantiated from the pattern of 4018 /// the expansion, where the Ith instantiation of the pattern uses the 4019 /// Ith arguments bound to each of the unexpanded parameter packs. The 4020 /// pack expansion is considered to "expand" these unexpanded 4021 /// parameter packs. 4022 /// 4023 /// \code 4024 /// template<typename ...Types> struct tuple; 4025 /// 4026 /// template<typename ...Types> 4027 /// struct tuple_of_references { 4028 /// typedef tuple<Types&...> type; 4029 /// }; 4030 /// \endcode 4031 /// 4032 /// Here, the pack expansion \c Types&... is represented via a 4033 /// PackExpansionType whose pattern is Types&. 4034 class PackExpansionType : public Type, public llvm::FoldingSetNode { 4035 /// \brief The pattern of the pack expansion. 4036 QualType Pattern; 4037 4038 /// \brief The number of expansions that this pack expansion will 4039 /// generate when substituted (+1), or indicates that 4040 /// 4041 /// This field will only have a non-zero value when some of the parameter 4042 /// packs that occur within the pattern have been substituted but others have 4043 /// not. 4044 unsigned NumExpansions; 4045 4046 PackExpansionType(QualType Pattern, QualType Canon, 4047 llvm::Optional<unsigned> NumExpansions) 4048 : Type(PackExpansion, Canon, /*Dependent=*/true, 4049 /*InstantiationDependent=*/true, 4050 /*VariableModified=*/Pattern->isVariablyModifiedType(), 4051 /*ContainsUnexpandedParameterPack=*/false), 4052 Pattern(Pattern), 4053 NumExpansions(NumExpansions? *NumExpansions + 1: 0) { } 4054 4055 friend class ASTContext; // ASTContext creates these 4056 4057 public: 4058 /// \brief Retrieve the pattern of this pack expansion, which is the 4059 /// type that will be repeatedly instantiated when instantiating the 4060 /// pack expansion itself. 4061 QualType getPattern() const { return Pattern; } 4062 4063 /// \brief Retrieve the number of expansions that this pack expansion will 4064 /// generate, if known. 4065 llvm::Optional<unsigned> getNumExpansions() const { 4066 if (NumExpansions) 4067 return NumExpansions - 1; 4068 4069 return llvm::Optional<unsigned>(); 4070 } 4071 4072 bool isSugared() const { return false; } 4073 QualType desugar() const { return QualType(this, 0); } 4074 4075 void Profile(llvm::FoldingSetNodeID &ID) { 4076 Profile(ID, getPattern(), getNumExpansions()); 4077 } 4078 4079 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pattern, 4080 llvm::Optional<unsigned> NumExpansions) { 4081 ID.AddPointer(Pattern.getAsOpaquePtr()); 4082 ID.AddBoolean(NumExpansions); 4083 if (NumExpansions) 4084 ID.AddInteger(*NumExpansions); 4085 } 4086 4087 static bool classof(const Type *T) { 4088 return T->getTypeClass() == PackExpansion; 4089 } 4090 static bool classof(const PackExpansionType *T) { 4091 return true; 4092 } 4093 }; 4094 4095 /// ObjCObjectType - Represents a class type in Objective C. 4096 /// Every Objective C type is a combination of a base type and a 4097 /// list of protocols. 4098 /// 4099 /// Given the following declarations: 4100 /// @class C; 4101 /// @protocol P; 4102 /// 4103 /// 'C' is an ObjCInterfaceType C. It is sugar for an ObjCObjectType 4104 /// with base C and no protocols. 4105 /// 4106 /// 'C<P>' is an ObjCObjectType with base C and protocol list [P]. 4107 /// 4108 /// 'id' is a TypedefType which is sugar for an ObjCPointerType whose 4109 /// pointee is an ObjCObjectType with base BuiltinType::ObjCIdType 4110 /// and no protocols. 4111 /// 4112 /// 'id<P>' is an ObjCPointerType whose pointee is an ObjCObjecType 4113 /// with base BuiltinType::ObjCIdType and protocol list [P]. Eventually 4114 /// this should get its own sugar class to better represent the source. 4115 class ObjCObjectType : public Type { 4116 // ObjCObjectType.NumProtocols - the number of protocols stored 4117 // after the ObjCObjectPointerType node. 4118 // 4119 // These protocols are those written directly on the type. If 4120 // protocol qualifiers ever become additive, the iterators will need 4121 // to get kindof complicated. 4122 // 4123 // In the canonical object type, these are sorted alphabetically 4124 // and uniqued. 4125 4126 /// Either a BuiltinType or an InterfaceType or sugar for either. 4127 QualType BaseType; 4128 4129 ObjCProtocolDecl * const *getProtocolStorage() const { 4130 return const_cast<ObjCObjectType*>(this)->getProtocolStorage(); 4131 } 4132 4133 ObjCProtocolDecl **getProtocolStorage(); 4134 4135 protected: 4136 ObjCObjectType(QualType Canonical, QualType Base, 4137 ObjCProtocolDecl * const *Protocols, unsigned NumProtocols); 4138 4139 enum Nonce_ObjCInterface { Nonce_ObjCInterface }; 4140 ObjCObjectType(enum Nonce_ObjCInterface) 4141 : Type(ObjCInterface, QualType(), false, false, false, false), 4142 BaseType(QualType(this_(), 0)) { 4143 ObjCObjectTypeBits.NumProtocols = 0; 4144 } 4145 4146 public: 4147 /// getBaseType - Gets the base type of this object type. This is 4148 /// always (possibly sugar for) one of: 4149 /// - the 'id' builtin type (as opposed to the 'id' type visible to the 4150 /// user, which is a typedef for an ObjCPointerType) 4151 /// - the 'Class' builtin type (same caveat) 4152 /// - an ObjCObjectType (currently always an ObjCInterfaceType) 4153 QualType getBaseType() const { return BaseType; } 4154 4155 bool isObjCId() const { 4156 return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCId); 4157 } 4158 bool isObjCClass() const { 4159 return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCClass); 4160 } 4161 bool isObjCUnqualifiedId() const { return qual_empty() && isObjCId(); } 4162 bool isObjCUnqualifiedClass() const { return qual_empty() && isObjCClass(); } 4163 bool isObjCUnqualifiedIdOrClass() const { 4164 if (!qual_empty()) return false; 4165 if (const BuiltinType *T = getBaseType()->getAs<BuiltinType>()) 4166 return T->getKind() == BuiltinType::ObjCId || 4167 T->getKind() == BuiltinType::ObjCClass; 4168 return false; 4169 } 4170 bool isObjCQualifiedId() const { return !qual_empty() && isObjCId(); } 4171 bool isObjCQualifiedClass() const { return !qual_empty() && isObjCClass(); } 4172 4173 /// Gets the interface declaration for this object type, if the base type 4174 /// really is an interface. 4175 ObjCInterfaceDecl *getInterface() const; 4176 4177 typedef ObjCProtocolDecl * const *qual_iterator; 4178 4179 qual_iterator qual_begin() const { return getProtocolStorage(); } 4180 qual_iterator qual_end() const { return qual_begin() + getNumProtocols(); } 4181 4182 bool qual_empty() const { return getNumProtocols() == 0; } 4183 4184 /// getNumProtocols - Return the number of qualifying protocols in this 4185 /// interface type, or 0 if there are none. 4186 unsigned getNumProtocols() const { return ObjCObjectTypeBits.NumProtocols; } 4187 4188 /// \brief Fetch a protocol by index. 4189 ObjCProtocolDecl *getProtocol(unsigned I) const { 4190 assert(I < getNumProtocols() && "Out-of-range protocol access"); 4191 return qual_begin()[I]; 4192 } 4193 4194 bool isSugared() const { return false; } 4195 QualType desugar() const { return QualType(this, 0); } 4196 4197 static bool classof(const Type *T) { 4198 return T->getTypeClass() == ObjCObject || 4199 T->getTypeClass() == ObjCInterface; 4200 } 4201 static bool classof(const ObjCObjectType *) { return true; } 4202 }; 4203 4204 /// ObjCObjectTypeImpl - A class providing a concrete implementation 4205 /// of ObjCObjectType, so as to not increase the footprint of 4206 /// ObjCInterfaceType. Code outside of ASTContext and the core type 4207 /// system should not reference this type. 4208 class ObjCObjectTypeImpl : public ObjCObjectType, public llvm::FoldingSetNode { 4209 friend class ASTContext; 4210 4211 // If anyone adds fields here, ObjCObjectType::getProtocolStorage() 4212 // will need to be modified. 4213 4214 ObjCObjectTypeImpl(QualType Canonical, QualType Base, 4215 ObjCProtocolDecl * const *Protocols, 4216 unsigned NumProtocols) 4217 : ObjCObjectType(Canonical, Base, Protocols, NumProtocols) {} 4218 4219 public: 4220 void Profile(llvm::FoldingSetNodeID &ID); 4221 static void Profile(llvm::FoldingSetNodeID &ID, 4222 QualType Base, 4223 ObjCProtocolDecl *const *protocols, 4224 unsigned NumProtocols); 4225 }; 4226 4227 inline ObjCProtocolDecl **ObjCObjectType::getProtocolStorage() { 4228 return reinterpret_cast<ObjCProtocolDecl**>( 4229 static_cast<ObjCObjectTypeImpl*>(this) + 1); 4230 } 4231 4232 /// ObjCInterfaceType - Interfaces are the core concept in Objective-C for 4233 /// object oriented design. They basically correspond to C++ classes. There 4234 /// are two kinds of interface types, normal interfaces like "NSString" and 4235 /// qualified interfaces, which are qualified with a protocol list like 4236 /// "NSString<NSCopyable, NSAmazing>". 4237 /// 4238 /// ObjCInterfaceType guarantees the following properties when considered 4239 /// as a subtype of its superclass, ObjCObjectType: 4240 /// - There are no protocol qualifiers. To reinforce this, code which 4241 /// tries to invoke the protocol methods via an ObjCInterfaceType will 4242 /// fail to compile. 4243 /// - It is its own base type. That is, if T is an ObjCInterfaceType*, 4244 /// T->getBaseType() == QualType(T, 0). 4245 class ObjCInterfaceType : public ObjCObjectType { 4246 mutable ObjCInterfaceDecl *Decl; 4247 4248 ObjCInterfaceType(const ObjCInterfaceDecl *D) 4249 : ObjCObjectType(Nonce_ObjCInterface), 4250 Decl(const_cast<ObjCInterfaceDecl*>(D)) {} 4251 friend class ASTContext; // ASTContext creates these. 4252 friend class ASTReader; 4253 friend class ObjCInterfaceDecl; 4254 4255 public: 4256 /// getDecl - Get the declaration of this interface. 4257 ObjCInterfaceDecl *getDecl() const { return Decl; } 4258 4259 bool isSugared() const { return false; } 4260 QualType desugar() const { return QualType(this, 0); } 4261 4262 static bool classof(const Type *T) { 4263 return T->getTypeClass() == ObjCInterface; 4264 } 4265 static bool classof(const ObjCInterfaceType *) { return true; } 4266 4267 // Nonsense to "hide" certain members of ObjCObjectType within this 4268 // class. People asking for protocols on an ObjCInterfaceType are 4269 // not going to get what they want: ObjCInterfaceTypes are 4270 // guaranteed to have no protocols. 4271 enum { 4272 qual_iterator, 4273 qual_begin, 4274 qual_end, 4275 getNumProtocols, 4276 getProtocol 4277 }; 4278 }; 4279 4280 inline ObjCInterfaceDecl *ObjCObjectType::getInterface() const { 4281 if (const ObjCInterfaceType *T = 4282 getBaseType()->getAs<ObjCInterfaceType>()) 4283 return T->getDecl(); 4284 return 0; 4285 } 4286 4287 /// ObjCObjectPointerType - Used to represent a pointer to an 4288 /// Objective C object. These are constructed from pointer 4289 /// declarators when the pointee type is an ObjCObjectType (or sugar 4290 /// for one). In addition, the 'id' and 'Class' types are typedefs 4291 /// for these, and the protocol-qualified types 'id<P>' and 'Class<P>' 4292 /// are translated into these. 4293 /// 4294 /// Pointers to pointers to Objective C objects are still PointerTypes; 4295 /// only the first level of pointer gets it own type implementation. 4296 class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode { 4297 QualType PointeeType; 4298 4299 ObjCObjectPointerType(QualType Canonical, QualType Pointee) 4300 : Type(ObjCObjectPointer, Canonical, false, false, false, false), 4301 PointeeType(Pointee) {} 4302 friend class ASTContext; // ASTContext creates these. 4303 4304 public: 4305 /// getPointeeType - Gets the type pointed to by this ObjC pointer. 4306 /// The result will always be an ObjCObjectType or sugar thereof. 4307 QualType getPointeeType() const { return PointeeType; } 4308 4309 /// getObjCObjectType - Gets the type pointed to by this ObjC 4310 /// pointer. This method always returns non-null. 4311 /// 4312 /// This method is equivalent to getPointeeType() except that 4313 /// it discards any typedefs (or other sugar) between this 4314 /// type and the "outermost" object type. So for: 4315 /// @class A; @protocol P; @protocol Q; 4316 /// typedef A<P> AP; 4317 /// typedef A A1; 4318 /// typedef A1<P> A1P; 4319 /// typedef A1P<Q> A1PQ; 4320 /// For 'A*', getObjectType() will return 'A'. 4321 /// For 'A<P>*', getObjectType() will return 'A<P>'. 4322 /// For 'AP*', getObjectType() will return 'A<P>'. 4323 /// For 'A1*', getObjectType() will return 'A'. 4324 /// For 'A1<P>*', getObjectType() will return 'A1<P>'. 4325 /// For 'A1P*', getObjectType() will return 'A1<P>'. 4326 /// For 'A1PQ*', getObjectType() will return 'A1<Q>', because 4327 /// adding protocols to a protocol-qualified base discards the 4328 /// old qualifiers (for now). But if it didn't, getObjectType() 4329 /// would return 'A1P<Q>' (and we'd have to make iterating over 4330 /// qualifiers more complicated). 4331 const ObjCObjectType *getObjectType() const { 4332 return PointeeType->castAs<ObjCObjectType>(); 4333 } 4334 4335 /// getInterfaceType - If this pointer points to an Objective C 4336 /// @interface type, gets the type for that interface. Any protocol 4337 /// qualifiers on the interface are ignored. 4338 /// 4339 /// \return null if the base type for this pointer is 'id' or 'Class' 4340 const ObjCInterfaceType *getInterfaceType() const { 4341 return getObjectType()->getBaseType()->getAs<ObjCInterfaceType>(); 4342 } 4343 4344 /// getInterfaceDecl - If this pointer points to an Objective @interface 4345 /// type, gets the declaration for that interface. 4346 /// 4347 /// \return null if the base type for this pointer is 'id' or 'Class' 4348 ObjCInterfaceDecl *getInterfaceDecl() const { 4349 return getObjectType()->getInterface(); 4350 } 4351 4352 /// isObjCIdType - True if this is equivalent to the 'id' type, i.e. if 4353 /// its object type is the primitive 'id' type with no protocols. 4354 bool isObjCIdType() const { 4355 return getObjectType()->isObjCUnqualifiedId(); 4356 } 4357 4358 /// isObjCClassType - True if this is equivalent to the 'Class' type, 4359 /// i.e. if its object tive is the primitive 'Class' type with no protocols. 4360 bool isObjCClassType() const { 4361 return getObjectType()->isObjCUnqualifiedClass(); 4362 } 4363 4364 /// isObjCQualifiedIdType - True if this is equivalent to 'id<P>' for some 4365 /// non-empty set of protocols. 4366 bool isObjCQualifiedIdType() const { 4367 return getObjectType()->isObjCQualifiedId(); 4368 } 4369 4370 /// isObjCQualifiedClassType - True if this is equivalent to 'Class<P>' for 4371 /// some non-empty set of protocols. 4372 bool isObjCQualifiedClassType() const { 4373 return getObjectType()->isObjCQualifiedClass(); 4374 } 4375 4376 /// An iterator over the qualifiers on the object type. Provided 4377 /// for convenience. This will always iterate over the full set of 4378 /// protocols on a type, not just those provided directly. 4379 typedef ObjCObjectType::qual_iterator qual_iterator; 4380 4381 qual_iterator qual_begin() const { 4382 return getObjectType()->qual_begin(); 4383 } 4384 qual_iterator qual_end() const { 4385 return getObjectType()->qual_end(); 4386 } 4387 bool qual_empty() const { return getObjectType()->qual_empty(); } 4388 4389 /// getNumProtocols - Return the number of qualifying protocols on 4390 /// the object type. 4391 unsigned getNumProtocols() const { 4392 return getObjectType()->getNumProtocols(); 4393 } 4394 4395 /// \brief Retrieve a qualifying protocol by index on the object 4396 /// type. 4397 ObjCProtocolDecl *getProtocol(unsigned I) const { 4398 return getObjectType()->getProtocol(I); 4399 } 4400 4401 bool isSugared() const { return false; } 4402 QualType desugar() const { return QualType(this, 0); } 4403 4404 void Profile(llvm::FoldingSetNodeID &ID) { 4405 Profile(ID, getPointeeType()); 4406 } 4407 static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { 4408 ID.AddPointer(T.getAsOpaquePtr()); 4409 } 4410 static bool classof(const Type *T) { 4411 return T->getTypeClass() == ObjCObjectPointer; 4412 } 4413 static bool classof(const ObjCObjectPointerType *) { return true; } 4414 }; 4415 4416 class AtomicType : public Type, public llvm::FoldingSetNode { 4417 QualType ValueType; 4418 4419 AtomicType(QualType ValTy, QualType Canonical) 4420 : Type(Atomic, Canonical, ValTy->isDependentType(), 4421 ValTy->isInstantiationDependentType(), 4422 ValTy->isVariablyModifiedType(), 4423 ValTy->containsUnexpandedParameterPack()), 4424 ValueType(ValTy) {} 4425 friend class ASTContext; // ASTContext creates these. 4426 4427 public: 4428 /// getValueType - Gets the type contained by this atomic type, i.e. 4429 /// the type returned by performing an atomic load of this atomic type. 4430 QualType getValueType() const { return ValueType; } 4431 4432 bool isSugared() const { return false; } 4433 QualType desugar() const { return QualType(this, 0); } 4434 4435 void Profile(llvm::FoldingSetNodeID &ID) { 4436 Profile(ID, getValueType()); 4437 } 4438 static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { 4439 ID.AddPointer(T.getAsOpaquePtr()); 4440 } 4441 static bool classof(const Type *T) { 4442 return T->getTypeClass() == Atomic; 4443 } 4444 static bool classof(const AtomicType *) { return true; } 4445 }; 4446 4447 /// A qualifier set is used to build a set of qualifiers. 4448 class QualifierCollector : public Qualifiers { 4449 public: 4450 QualifierCollector(Qualifiers Qs = Qualifiers()) : Qualifiers(Qs) {} 4451 4452 /// Collect any qualifiers on the given type and return an 4453 /// unqualified type. The qualifiers are assumed to be consistent 4454 /// with those already in the type. 4455 const Type *strip(QualType type) { 4456 addFastQualifiers(type.getLocalFastQualifiers()); 4457 if (!type.hasLocalNonFastQualifiers()) 4458 return type.getTypePtrUnsafe(); 4459 4460 const ExtQuals *extQuals = type.getExtQualsUnsafe(); 4461 addConsistentQualifiers(extQuals->getQualifiers()); 4462 return extQuals->getBaseType(); 4463 } 4464 4465 /// Apply the collected qualifiers to the given type. 4466 QualType apply(const ASTContext &Context, QualType QT) const; 4467 4468 /// Apply the collected qualifiers to the given type. 4469 QualType apply(const ASTContext &Context, const Type* T) const; 4470 }; 4471 4472 4473 // Inline function definitions. 4474 4475 inline SplitQualType SplitQualType::getSingleStepDesugaredType() const { 4476 SplitQualType desugar = 4477 Ty->getLocallyUnqualifiedSingleStepDesugaredType().split(); 4478 desugar.Quals.addConsistentQualifiers(Quals); 4479 return desugar; 4480 } 4481 4482 inline const Type *QualType::getTypePtr() const { 4483 return getCommonPtr()->BaseType; 4484 } 4485 4486 inline const Type *QualType::getTypePtrOrNull() const { 4487 return (isNull() ? 0 : getCommonPtr()->BaseType); 4488 } 4489 4490 inline SplitQualType QualType::split() const { 4491 if (!hasLocalNonFastQualifiers()) 4492 return SplitQualType(getTypePtrUnsafe(), 4493 Qualifiers::fromFastMask(getLocalFastQualifiers())); 4494 4495 const ExtQuals *eq = getExtQualsUnsafe(); 4496 Qualifiers qs = eq->getQualifiers(); 4497 qs.addFastQualifiers(getLocalFastQualifiers()); 4498 return SplitQualType(eq->getBaseType(), qs); 4499 } 4500 4501 inline Qualifiers QualType::getLocalQualifiers() const { 4502 Qualifiers Quals; 4503 if (hasLocalNonFastQualifiers()) 4504 Quals = getExtQualsUnsafe()->getQualifiers(); 4505 Quals.addFastQualifiers(getLocalFastQualifiers()); 4506 return Quals; 4507 } 4508 4509 inline Qualifiers QualType::getQualifiers() const { 4510 Qualifiers quals = getCommonPtr()->CanonicalType.getLocalQualifiers(); 4511 quals.addFastQualifiers(getLocalFastQualifiers()); 4512 return quals; 4513 } 4514 4515 inline unsigned QualType::getCVRQualifiers() const { 4516 unsigned cvr = getCommonPtr()->CanonicalType.getLocalCVRQualifiers(); 4517 cvr |= getLocalCVRQualifiers(); 4518 return cvr; 4519 } 4520 4521 inline QualType QualType::getCanonicalType() const { 4522 QualType canon = getCommonPtr()->CanonicalType; 4523 return canon.withFastQualifiers(getLocalFastQualifiers()); 4524 } 4525 4526 inline bool QualType::isCanonical() const { 4527 return getTypePtr()->isCanonicalUnqualified(); 4528 } 4529 4530 inline bool QualType::isCanonicalAsParam() const { 4531 if (!isCanonical()) return false; 4532 if (hasLocalQualifiers()) return false; 4533 4534 const Type *T = getTypePtr(); 4535 if (T->isVariablyModifiedType() && T->hasSizedVLAType()) 4536 return false; 4537 4538 return !isa<FunctionType>(T) && !isa<ArrayType>(T); 4539 } 4540 4541 inline bool QualType::isConstQualified() const { 4542 return isLocalConstQualified() || 4543 getCommonPtr()->CanonicalType.isLocalConstQualified(); 4544 } 4545 4546 inline bool QualType::isRestrictQualified() const { 4547 return isLocalRestrictQualified() || 4548 getCommonPtr()->CanonicalType.isLocalRestrictQualified(); 4549 } 4550 4551 4552 inline bool QualType::isVolatileQualified() const { 4553 return isLocalVolatileQualified() || 4554 getCommonPtr()->CanonicalType.isLocalVolatileQualified(); 4555 } 4556 4557 inline bool QualType::hasQualifiers() const { 4558 return hasLocalQualifiers() || 4559 getCommonPtr()->CanonicalType.hasLocalQualifiers(); 4560 } 4561 4562 inline QualType QualType::getUnqualifiedType() const { 4563 if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) 4564 return QualType(getTypePtr(), 0); 4565 4566 return QualType(getSplitUnqualifiedTypeImpl(*this).Ty, 0); 4567 } 4568 4569 inline SplitQualType QualType::getSplitUnqualifiedType() const { 4570 if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) 4571 return split(); 4572 4573 return getSplitUnqualifiedTypeImpl(*this); 4574 } 4575 4576 inline void QualType::removeLocalConst() { 4577 removeLocalFastQualifiers(Qualifiers::Const); 4578 } 4579 4580 inline void QualType::removeLocalRestrict() { 4581 removeLocalFastQualifiers(Qualifiers::Restrict); 4582 } 4583 4584 inline void QualType::removeLocalVolatile() { 4585 removeLocalFastQualifiers(Qualifiers::Volatile); 4586 } 4587 4588 inline void QualType::removeLocalCVRQualifiers(unsigned Mask) { 4589 assert(!(Mask & ~Qualifiers::CVRMask) && "mask has non-CVR bits"); 4590 assert((int)Qualifiers::CVRMask == (int)Qualifiers::FastMask); 4591 4592 // Fast path: we don't need to touch the slow qualifiers. 4593 removeLocalFastQualifiers(Mask); 4594 } 4595 4596 /// getAddressSpace - Return the address space of this type. 4597 inline unsigned QualType::getAddressSpace() const { 4598 return getQualifiers().getAddressSpace(); 4599 } 4600 4601 /// getObjCGCAttr - Return the gc attribute of this type. 4602 inline Qualifiers::GC QualType::getObjCGCAttr() const { 4603 return getQualifiers().getObjCGCAttr(); 4604 } 4605 4606 inline FunctionType::ExtInfo getFunctionExtInfo(const Type &t) { 4607 if (const PointerType *PT = t.getAs<PointerType>()) { 4608 if (const FunctionType *FT = PT->getPointeeType()->getAs<FunctionType>()) 4609 return FT->getExtInfo(); 4610 } else if (const FunctionType *FT = t.getAs<FunctionType>()) 4611 return FT->getExtInfo(); 4612 4613 return FunctionType::ExtInfo(); 4614 } 4615 4616 inline FunctionType::ExtInfo getFunctionExtInfo(QualType t) { 4617 return getFunctionExtInfo(*t); 4618 } 4619 4620 /// isMoreQualifiedThan - Determine whether this type is more 4621 /// qualified than the Other type. For example, "const volatile int" 4622 /// is more qualified than "const int", "volatile int", and 4623 /// "int". However, it is not more qualified than "const volatile 4624 /// int". 4625 inline bool QualType::isMoreQualifiedThan(QualType other) const { 4626 Qualifiers myQuals = getQualifiers(); 4627 Qualifiers otherQuals = other.getQualifiers(); 4628 return (myQuals != otherQuals && myQuals.compatiblyIncludes(otherQuals)); 4629 } 4630 4631 /// isAtLeastAsQualifiedAs - Determine whether this type is at last 4632 /// as qualified as the Other type. For example, "const volatile 4633 /// int" is at least as qualified as "const int", "volatile int", 4634 /// "int", and "const volatile int". 4635 inline bool QualType::isAtLeastAsQualifiedAs(QualType other) const { 4636 return getQualifiers().compatiblyIncludes(other.getQualifiers()); 4637 } 4638 4639 /// getNonReferenceType - If Type is a reference type (e.g., const 4640 /// int&), returns the type that the reference refers to ("const 4641 /// int"). Otherwise, returns the type itself. This routine is used 4642 /// throughout Sema to implement C++ 5p6: 4643 /// 4644 /// If an expression initially has the type "reference to T" (8.3.2, 4645 /// 8.5.3), the type is adjusted to "T" prior to any further 4646 /// analysis, the expression designates the object or function 4647 /// denoted by the reference, and the expression is an lvalue. 4648 inline QualType QualType::getNonReferenceType() const { 4649 if (const ReferenceType *RefType = (*this)->getAs<ReferenceType>()) 4650 return RefType->getPointeeType(); 4651 else 4652 return *this; 4653 } 4654 4655 inline bool QualType::isCForbiddenLValueType() const { 4656 return ((getTypePtr()->isVoidType() && !hasQualifiers()) || 4657 getTypePtr()->isFunctionType()); 4658 } 4659 4660 /// \brief Tests whether the type is categorized as a fundamental type. 4661 /// 4662 /// \returns True for types specified in C++0x [basic.fundamental]. 4663 inline bool Type::isFundamentalType() const { 4664 return isVoidType() || 4665 // FIXME: It's really annoying that we don't have an 4666 // 'isArithmeticType()' which agrees with the standard definition. 4667 (isArithmeticType() && !isEnumeralType()); 4668 } 4669 4670 /// \brief Tests whether the type is categorized as a compound type. 4671 /// 4672 /// \returns True for types specified in C++0x [basic.compound]. 4673 inline bool Type::isCompoundType() const { 4674 // C++0x [basic.compound]p1: 4675 // Compound types can be constructed in the following ways: 4676 // -- arrays of objects of a given type [...]; 4677 return isArrayType() || 4678 // -- functions, which have parameters of given types [...]; 4679 isFunctionType() || 4680 // -- pointers to void or objects or functions [...]; 4681 isPointerType() || 4682 // -- references to objects or functions of a given type. [...] 4683 isReferenceType() || 4684 // -- classes containing a sequence of objects of various types, [...]; 4685 isRecordType() || 4686 // -- unions, which are classes capable of containing objects of different 4687 // types at different times; 4688 isUnionType() || 4689 // -- enumerations, which comprise a set of named constant values. [...]; 4690 isEnumeralType() || 4691 // -- pointers to non-static class members, [...]. 4692 isMemberPointerType(); 4693 } 4694 4695 inline bool Type::isFunctionType() const { 4696 return isa<FunctionType>(CanonicalType); 4697 } 4698 inline bool Type::isPointerType() const { 4699 return isa<PointerType>(CanonicalType); 4700 } 4701 inline bool Type::isAnyPointerType() const { 4702 return isPointerType() || isObjCObjectPointerType(); 4703 } 4704 inline bool Type::isBlockPointerType() const { 4705 return isa<BlockPointerType>(CanonicalType); 4706 } 4707 inline bool Type::isReferenceType() const { 4708 return isa<ReferenceType>(CanonicalType); 4709 } 4710 inline bool Type::isLValueReferenceType() const { 4711 return isa<LValueReferenceType>(CanonicalType); 4712 } 4713 inline bool Type::isRValueReferenceType() const { 4714 return isa<RValueReferenceType>(CanonicalType); 4715 } 4716 inline bool Type::isFunctionPointerType() const { 4717 if (const PointerType *T = getAs<PointerType>()) 4718 return T->getPointeeType()->isFunctionType(); 4719 else 4720 return false; 4721 } 4722 inline bool Type::isMemberPointerType() const { 4723 return isa<MemberPointerType>(CanonicalType); 4724 } 4725 inline bool Type::isMemberFunctionPointerType() const { 4726 if (const MemberPointerType* T = getAs<MemberPointerType>()) 4727 return T->isMemberFunctionPointer(); 4728 else 4729 return false; 4730 } 4731 inline bool Type::isMemberDataPointerType() const { 4732 if (const MemberPointerType* T = getAs<MemberPointerType>()) 4733 return T->isMemberDataPointer(); 4734 else 4735 return false; 4736 } 4737 inline bool Type::isArrayType() const { 4738 return isa<ArrayType>(CanonicalType); 4739 } 4740 inline bool Type::isConstantArrayType() const { 4741 return isa<ConstantArrayType>(CanonicalType); 4742 } 4743 inline bool Type::isIncompleteArrayType() const { 4744 return isa<IncompleteArrayType>(CanonicalType); 4745 } 4746 inline bool Type::isVariableArrayType() const { 4747 return isa<VariableArrayType>(CanonicalType); 4748 } 4749 inline bool Type::isDependentSizedArrayType() const { 4750 return isa<DependentSizedArrayType>(CanonicalType); 4751 } 4752 inline bool Type::isBuiltinType() const { 4753 return isa<BuiltinType>(CanonicalType); 4754 } 4755 inline bool Type::isRecordType() const { 4756 return isa<RecordType>(CanonicalType); 4757 } 4758 inline bool Type::isEnumeralType() const { 4759 return isa<EnumType>(CanonicalType); 4760 } 4761 inline bool Type::isAnyComplexType() const { 4762 return isa<ComplexType>(CanonicalType); 4763 } 4764 inline bool Type::isVectorType() const { 4765 return isa<VectorType>(CanonicalType); 4766 } 4767 inline bool Type::isExtVectorType() const { 4768 return isa<ExtVectorType>(CanonicalType); 4769 } 4770 inline bool Type::isObjCObjectPointerType() const { 4771 return isa<ObjCObjectPointerType>(CanonicalType); 4772 } 4773 inline bool Type::isObjCObjectType() const { 4774 return isa<ObjCObjectType>(CanonicalType); 4775 } 4776 inline bool Type::isObjCObjectOrInterfaceType() const { 4777 return isa<ObjCInterfaceType>(CanonicalType) || 4778 isa<ObjCObjectType>(CanonicalType); 4779 } 4780 inline bool Type::isAtomicType() const { 4781 return isa<AtomicType>(CanonicalType); 4782 } 4783 4784 inline bool Type::isObjCQualifiedIdType() const { 4785 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 4786 return OPT->isObjCQualifiedIdType(); 4787 return false; 4788 } 4789 inline bool Type::isObjCQualifiedClassType() const { 4790 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 4791 return OPT->isObjCQualifiedClassType(); 4792 return false; 4793 } 4794 inline bool Type::isObjCIdType() const { 4795 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 4796 return OPT->isObjCIdType(); 4797 return false; 4798 } 4799 inline bool Type::isObjCClassType() const { 4800 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 4801 return OPT->isObjCClassType(); 4802 return false; 4803 } 4804 inline bool Type::isObjCSelType() const { 4805 if (const PointerType *OPT = getAs<PointerType>()) 4806 return OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCSel); 4807 return false; 4808 } 4809 inline bool Type::isObjCBuiltinType() const { 4810 return isObjCIdType() || isObjCClassType() || isObjCSelType(); 4811 } 4812 inline bool Type::isTemplateTypeParmType() const { 4813 return isa<TemplateTypeParmType>(CanonicalType); 4814 } 4815 4816 inline bool Type::isSpecificBuiltinType(unsigned K) const { 4817 if (const BuiltinType *BT = getAs<BuiltinType>()) 4818 if (BT->getKind() == (BuiltinType::Kind) K) 4819 return true; 4820 return false; 4821 } 4822 4823 inline bool Type::isPlaceholderType() const { 4824 if (const BuiltinType *BT = dyn_cast<BuiltinType>(this)) 4825 return BT->isPlaceholderType(); 4826 return false; 4827 } 4828 4829 inline const BuiltinType *Type::getAsPlaceholderType() const { 4830 if (const BuiltinType *BT = dyn_cast<BuiltinType>(this)) 4831 if (BT->isPlaceholderType()) 4832 return BT; 4833 return 0; 4834 } 4835 4836 inline bool Type::isSpecificPlaceholderType(unsigned K) const { 4837 assert(BuiltinType::isPlaceholderTypeKind((BuiltinType::Kind) K)); 4838 if (const BuiltinType *BT = dyn_cast<BuiltinType>(this)) 4839 return (BT->getKind() == (BuiltinType::Kind) K); 4840 return false; 4841 } 4842 4843 inline bool Type::isNonOverloadPlaceholderType() const { 4844 if (const BuiltinType *BT = dyn_cast<BuiltinType>(this)) 4845 return BT->isNonOverloadPlaceholderType(); 4846 return false; 4847 } 4848 4849 inline bool Type::isVoidType() const { 4850 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 4851 return BT->getKind() == BuiltinType::Void; 4852 return false; 4853 } 4854 4855 inline bool Type::isHalfType() const { 4856 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 4857 return BT->getKind() == BuiltinType::Half; 4858 // FIXME: Should we allow complex __fp16? Probably not. 4859 return false; 4860 } 4861 4862 inline bool Type::isNullPtrType() const { 4863 if (const BuiltinType *BT = getAs<BuiltinType>()) 4864 return BT->getKind() == BuiltinType::NullPtr; 4865 return false; 4866 } 4867 4868 extern bool IsEnumDeclComplete(EnumDecl *); 4869 extern bool IsEnumDeclScoped(EnumDecl *); 4870 4871 inline bool Type::isIntegerType() const { 4872 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 4873 return BT->getKind() >= BuiltinType::Bool && 4874 BT->getKind() <= BuiltinType::Int128; 4875 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) { 4876 // Incomplete enum types are not treated as integer types. 4877 // FIXME: In C++, enum types are never integer types. 4878 return IsEnumDeclComplete(ET->getDecl()) && 4879 !IsEnumDeclScoped(ET->getDecl()); 4880 } 4881 return false; 4882 } 4883 4884 inline bool Type::isScalarType() const { 4885 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 4886 return BT->getKind() > BuiltinType::Void && 4887 BT->getKind() <= BuiltinType::NullPtr; 4888 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 4889 // Enums are scalar types, but only if they are defined. Incomplete enums 4890 // are not treated as scalar types. 4891 return IsEnumDeclComplete(ET->getDecl()); 4892 return isa<PointerType>(CanonicalType) || 4893 isa<BlockPointerType>(CanonicalType) || 4894 isa<MemberPointerType>(CanonicalType) || 4895 isa<ComplexType>(CanonicalType) || 4896 isa<ObjCObjectPointerType>(CanonicalType); 4897 } 4898 4899 inline bool Type::isIntegralOrEnumerationType() const { 4900 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 4901 return BT->getKind() >= BuiltinType::Bool && 4902 BT->getKind() <= BuiltinType::Int128; 4903 4904 // Check for a complete enum type; incomplete enum types are not properly an 4905 // enumeration type in the sense required here. 4906 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 4907 return IsEnumDeclComplete(ET->getDecl()); 4908 4909 return false; 4910 } 4911 4912 inline bool Type::isBooleanType() const { 4913 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 4914 return BT->getKind() == BuiltinType::Bool; 4915 return false; 4916 } 4917 4918 /// \brief Determines whether this is a type for which one can define 4919 /// an overloaded operator. 4920 inline bool Type::isOverloadableType() const { 4921 return isDependentType() || isRecordType() || isEnumeralType(); 4922 } 4923 4924 /// \brief Determines whether this type can decay to a pointer type. 4925 inline bool Type::canDecayToPointerType() const { 4926 return isFunctionType() || isArrayType(); 4927 } 4928 4929 inline bool Type::hasPointerRepresentation() const { 4930 return (isPointerType() || isReferenceType() || isBlockPointerType() || 4931 isObjCObjectPointerType() || isNullPtrType()); 4932 } 4933 4934 inline bool Type::hasObjCPointerRepresentation() const { 4935 return isObjCObjectPointerType(); 4936 } 4937 4938 inline const Type *Type::getBaseElementTypeUnsafe() const { 4939 const Type *type = this; 4940 while (const ArrayType *arrayType = type->getAsArrayTypeUnsafe()) 4941 type = arrayType->getElementType().getTypePtr(); 4942 return type; 4943 } 4944 4945 /// Insertion operator for diagnostics. This allows sending QualType's into a 4946 /// diagnostic with <<. 4947 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 4948 QualType T) { 4949 DB.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), 4950 DiagnosticsEngine::ak_qualtype); 4951 return DB; 4952 } 4953 4954 /// Insertion operator for partial diagnostics. This allows sending QualType's 4955 /// into a diagnostic with <<. 4956 inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD, 4957 QualType T) { 4958 PD.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), 4959 DiagnosticsEngine::ak_qualtype); 4960 return PD; 4961 } 4962 4963 // Helper class template that is used by Type::getAs to ensure that one does 4964 // not try to look through a qualified type to get to an array type. 4965 template<typename T, 4966 bool isArrayType = (llvm::is_same<T, ArrayType>::value || 4967 llvm::is_base_of<ArrayType, T>::value)> 4968 struct ArrayType_cannot_be_used_with_getAs { }; 4969 4970 template<typename T> 4971 struct ArrayType_cannot_be_used_with_getAs<T, true>; 4972 4973 /// Member-template getAs<specific type>'. 4974 template <typename T> const T *Type::getAs() const { 4975 ArrayType_cannot_be_used_with_getAs<T> at; 4976 (void)at; 4977 4978 // If this is directly a T type, return it. 4979 if (const T *Ty = dyn_cast<T>(this)) 4980 return Ty; 4981 4982 // If the canonical form of this type isn't the right kind, reject it. 4983 if (!isa<T>(CanonicalType)) 4984 return 0; 4985 4986 // If this is a typedef for the type, strip the typedef off without 4987 // losing all typedef information. 4988 return cast<T>(getUnqualifiedDesugaredType()); 4989 } 4990 4991 inline const ArrayType *Type::getAsArrayTypeUnsafe() const { 4992 // If this is directly an array type, return it. 4993 if (const ArrayType *arr = dyn_cast<ArrayType>(this)) 4994 return arr; 4995 4996 // If the canonical form of this type isn't the right kind, reject it. 4997 if (!isa<ArrayType>(CanonicalType)) 4998 return 0; 4999 5000 // If this is a typedef for the type, strip the typedef off without 5001 // losing all typedef information. 5002 return cast<ArrayType>(getUnqualifiedDesugaredType()); 5003 } 5004 5005 template <typename T> const T *Type::castAs() const { 5006 ArrayType_cannot_be_used_with_getAs<T> at; 5007 (void) at; 5008 5009 assert(isa<T>(CanonicalType)); 5010 if (const T *ty = dyn_cast<T>(this)) return ty; 5011 return cast<T>(getUnqualifiedDesugaredType()); 5012 } 5013 5014 inline const ArrayType *Type::castAsArrayTypeUnsafe() const { 5015 assert(isa<ArrayType>(CanonicalType)); 5016 if (const ArrayType *arr = dyn_cast<ArrayType>(this)) return arr; 5017 return cast<ArrayType>(getUnqualifiedDesugaredType()); 5018 } 5019 5020 } // end namespace clang 5021 5022 #endif 5023