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