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