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