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