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