Type.h revision 3f59c975aa5d047f7edd1b900b5e885c38af0ef7
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 ElaboratedType; 96 struct PrintingPolicy; 97 98 template <typename> class CanQual; 99 typedef CanQual<Type> CanQualType; 100 101 // Provide forward declarations for all of the *Type classes 102#define TYPE(Class, Base) class Class##Type; 103#include "clang/AST/TypeNodes.def" 104 105/// Qualifiers - The collection of all-type qualifiers we support. 106/// Clang supports five independent qualifiers: 107/// * C99: const, volatile, and restrict 108/// * Embedded C (TR18037): address spaces 109/// * Objective C: the GC attributes (none, weak, or strong) 110class Qualifiers { 111public: 112 enum TQ { // NOTE: These flags must be kept in sync with DeclSpec::TQ. 113 Const = 0x1, 114 Restrict = 0x2, 115 Volatile = 0x4, 116 CVRMask = Const | Volatile | Restrict 117 }; 118 119 enum GC { 120 GCNone = 0, 121 Weak, 122 Strong 123 }; 124 125 enum { 126 /// The maximum supported address space number. 127 /// 24 bits should be enough for anyone. 128 MaxAddressSpace = 0xffffffu, 129 130 /// The width of the "fast" qualifier mask. 131 FastWidth = 3, 132 133 /// The fast qualifier mask. 134 FastMask = (1 << FastWidth) - 1 135 }; 136 137 Qualifiers() : Mask(0) {} 138 139 static Qualifiers fromFastMask(unsigned Mask) { 140 Qualifiers Qs; 141 Qs.addFastQualifiers(Mask); 142 return Qs; 143 } 144 145 static Qualifiers fromCVRMask(unsigned CVR) { 146 Qualifiers Qs; 147 Qs.addCVRQualifiers(CVR); 148 return Qs; 149 } 150 151 // Deserialize qualifiers from an opaque representation. 152 static Qualifiers fromOpaqueValue(unsigned opaque) { 153 Qualifiers Qs; 154 Qs.Mask = opaque; 155 return Qs; 156 } 157 158 // Serialize these qualifiers into an opaque representation. 159 unsigned getAsOpaqueValue() const { 160 return Mask; 161 } 162 163 bool hasConst() const { return Mask & Const; } 164 void setConst(bool flag) { 165 Mask = (Mask & ~Const) | (flag ? Const : 0); 166 } 167 void removeConst() { Mask &= ~Const; } 168 void addConst() { Mask |= Const; } 169 170 bool hasVolatile() const { return Mask & Volatile; } 171 void setVolatile(bool flag) { 172 Mask = (Mask & ~Volatile) | (flag ? Volatile : 0); 173 } 174 void removeVolatile() { Mask &= ~Volatile; } 175 void addVolatile() { Mask |= Volatile; } 176 177 bool hasRestrict() const { return Mask & Restrict; } 178 void setRestrict(bool flag) { 179 Mask = (Mask & ~Restrict) | (flag ? Restrict : 0); 180 } 181 void removeRestrict() { Mask &= ~Restrict; } 182 void addRestrict() { Mask |= Restrict; } 183 184 bool hasCVRQualifiers() const { return getCVRQualifiers(); } 185 unsigned getCVRQualifiers() const { return Mask & CVRMask; } 186 void setCVRQualifiers(unsigned mask) { 187 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); 188 Mask = (Mask & ~CVRMask) | mask; 189 } 190 void removeCVRQualifiers(unsigned mask) { 191 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); 192 Mask &= ~mask; 193 } 194 void removeCVRQualifiers() { 195 removeCVRQualifiers(CVRMask); 196 } 197 void addCVRQualifiers(unsigned mask) { 198 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); 199 Mask |= mask; 200 } 201 202 bool hasObjCGCAttr() const { return Mask & GCAttrMask; } 203 GC getObjCGCAttr() const { return GC((Mask & GCAttrMask) >> GCAttrShift); } 204 void setObjCGCAttr(GC type) { 205 Mask = (Mask & ~GCAttrMask) | (type << GCAttrShift); 206 } 207 void removeObjCGCAttr() { setObjCGCAttr(GCNone); } 208 void addObjCGCAttr(GC type) { 209 assert(type); 210 setObjCGCAttr(type); 211 } 212 213 bool hasAddressSpace() const { return Mask & AddressSpaceMask; } 214 unsigned getAddressSpace() const { return Mask >> AddressSpaceShift; } 215 void setAddressSpace(unsigned space) { 216 assert(space <= MaxAddressSpace); 217 Mask = (Mask & ~AddressSpaceMask) 218 | (((uint32_t) space) << AddressSpaceShift); 219 } 220 void removeAddressSpace() { setAddressSpace(0); } 221 void addAddressSpace(unsigned space) { 222 assert(space); 223 setAddressSpace(space); 224 } 225 226 // Fast qualifiers are those that can be allocated directly 227 // on a QualType object. 228 bool hasFastQualifiers() const { return getFastQualifiers(); } 229 unsigned getFastQualifiers() const { return Mask & FastMask; } 230 void setFastQualifiers(unsigned mask) { 231 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); 232 Mask = (Mask & ~FastMask) | mask; 233 } 234 void removeFastQualifiers(unsigned mask) { 235 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); 236 Mask &= ~mask; 237 } 238 void removeFastQualifiers() { 239 removeFastQualifiers(FastMask); 240 } 241 void addFastQualifiers(unsigned mask) { 242 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); 243 Mask |= mask; 244 } 245 246 /// hasNonFastQualifiers - Return true if the set contains any 247 /// qualifiers which require an ExtQuals node to be allocated. 248 bool hasNonFastQualifiers() const { return Mask & ~FastMask; } 249 Qualifiers getNonFastQualifiers() const { 250 Qualifiers Quals = *this; 251 Quals.setFastQualifiers(0); 252 return Quals; 253 } 254 255 /// hasQualifiers - Return true if the set contains any qualifiers. 256 bool hasQualifiers() const { return Mask; } 257 bool empty() const { return !Mask; } 258 259 /// \brief Add the qualifiers from the given set to this set. 260 void addQualifiers(Qualifiers Q) { 261 // If the other set doesn't have any non-boolean qualifiers, just 262 // bit-or it in. 263 if (!(Q.Mask & ~CVRMask)) 264 Mask |= Q.Mask; 265 else { 266 Mask |= (Q.Mask & CVRMask); 267 if (Q.hasAddressSpace()) 268 addAddressSpace(Q.getAddressSpace()); 269 if (Q.hasObjCGCAttr()) 270 addObjCGCAttr(Q.getObjCGCAttr()); 271 } 272 } 273 274 bool isSupersetOf(Qualifiers Other) const; 275 276 bool operator==(Qualifiers Other) const { return Mask == Other.Mask; } 277 bool operator!=(Qualifiers Other) const { return Mask != Other.Mask; } 278 279 operator bool() const { return hasQualifiers(); } 280 281 Qualifiers &operator+=(Qualifiers R) { 282 addQualifiers(R); 283 return *this; 284 } 285 286 // Union two qualifier sets. If an enumerated qualifier appears 287 // in both sets, use the one from the right. 288 friend Qualifiers operator+(Qualifiers L, Qualifiers R) { 289 L += R; 290 return L; 291 } 292 293 Qualifiers &operator-=(Qualifiers R) { 294 Mask = Mask & ~(R.Mask); 295 return *this; 296 } 297 298 /// \brief Compute the difference between two qualifier sets. 299 friend Qualifiers operator-(Qualifiers L, Qualifiers R) { 300 L -= R; 301 return L; 302 } 303 304 std::string getAsString() const; 305 std::string getAsString(const PrintingPolicy &Policy) const { 306 std::string Buffer; 307 getAsStringInternal(Buffer, Policy); 308 return Buffer; 309 } 310 void getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const; 311 312 void Profile(llvm::FoldingSetNodeID &ID) const { 313 ID.AddInteger(Mask); 314 } 315 316private: 317 318 // bits: |0 1 2|3 .. 4|5 .. 31| 319 // |C R V|GCAttr|AddrSpace| 320 uint32_t Mask; 321 322 static const uint32_t GCAttrMask = 0x18; 323 static const uint32_t GCAttrShift = 3; 324 static const uint32_t AddressSpaceMask = ~(CVRMask | GCAttrMask); 325 static const uint32_t AddressSpaceShift = 5; 326}; 327 328/// \brief Base class that is common to both the \c ExtQuals and \c Type 329/// classes, which allows \c QualType to access the common fields between the 330/// two. 331/// 332class ExtQualsTypeCommonBase { 333protected: 334 ExtQualsTypeCommonBase(const Type *BaseType) : BaseType(BaseType) { } 335 336 /// \brief The "base" type of an extended qualifiers type (\c ExtQuals) or 337 /// a self-referential pointer (for \c Type). 338 /// 339 /// This pointer allows an efficient mapping from a QualType to its 340 /// underlying type pointer. 341 const Type *BaseType; 342 343 friend class QualType; 344}; 345 346/// ExtQuals - We can encode up to four bits in the low bits of a 347/// type pointer, but there are many more type qualifiers that we want 348/// to be able to apply to an arbitrary type. Therefore we have this 349/// struct, intended to be heap-allocated and used by QualType to 350/// store qualifiers. 351/// 352/// The current design tags the 'const', 'restrict', and 'volatile' qualifiers 353/// in three low bits on the QualType pointer; a fourth bit records whether 354/// the pointer is an ExtQuals node. The extended qualifiers (address spaces, 355/// Objective-C GC attributes) are much more rare. 356class ExtQuals : public ExtQualsTypeCommonBase, public llvm::FoldingSetNode { 357 // NOTE: changing the fast qualifiers should be straightforward as 358 // long as you don't make 'const' non-fast. 359 // 1. Qualifiers: 360 // a) Modify the bitmasks (Qualifiers::TQ and DeclSpec::TQ). 361 // Fast qualifiers must occupy the low-order bits. 362 // b) Update Qualifiers::FastWidth and FastMask. 363 // 2. QualType: 364 // a) Update is{Volatile,Restrict}Qualified(), defined inline. 365 // b) Update remove{Volatile,Restrict}, defined near the end of 366 // this header. 367 // 3. ASTContext: 368 // a) Update get{Volatile,Restrict}Type. 369 370 /// Quals - the immutable set of qualifiers applied by this 371 /// node; always contains extended qualifiers. 372 Qualifiers Quals; 373 374public: 375 ExtQuals(const Type *Base, Qualifiers Quals) 376 : ExtQualsTypeCommonBase(Base), Quals(Quals) 377 { 378 assert(Quals.hasNonFastQualifiers() 379 && "ExtQuals created with no fast qualifiers"); 380 assert(!Quals.hasFastQualifiers() 381 && "ExtQuals created with fast qualifiers"); 382 } 383 384 Qualifiers getQualifiers() const { return Quals; } 385 386 bool hasObjCGCAttr() const { return Quals.hasObjCGCAttr(); } 387 Qualifiers::GC getObjCGCAttr() const { return Quals.getObjCGCAttr(); } 388 389 bool hasAddressSpace() const { return Quals.hasAddressSpace(); } 390 unsigned getAddressSpace() const { return Quals.getAddressSpace(); } 391 392 const Type *getBaseType() const { return BaseType; } 393 394public: 395 void Profile(llvm::FoldingSetNodeID &ID) const { 396 Profile(ID, getBaseType(), Quals); 397 } 398 static void Profile(llvm::FoldingSetNodeID &ID, 399 const Type *BaseType, 400 Qualifiers Quals) { 401 assert(!Quals.hasFastQualifiers() && "fast qualifiers in ExtQuals hash!"); 402 ID.AddPointer(BaseType); 403 Quals.Profile(ID); 404 } 405}; 406 407/// CallingConv - Specifies the calling convention that a function uses. 408enum CallingConv { 409 CC_Default, 410 CC_C, // __attribute__((cdecl)) 411 CC_X86StdCall, // __attribute__((stdcall)) 412 CC_X86FastCall, // __attribute__((fastcall)) 413 CC_X86ThisCall, // __attribute__((thiscall)) 414 CC_X86Pascal // __attribute__((pascal)) 415}; 416 417typedef std::pair<const Type*, Qualifiers> SplitQualType; 418 419/// QualType - For efficiency, we don't store CV-qualified types as nodes on 420/// their own: instead each reference to a type stores the qualifiers. This 421/// greatly reduces the number of nodes we need to allocate for types (for 422/// example we only need one for 'int', 'const int', 'volatile int', 423/// 'const volatile int', etc). 424/// 425/// As an added efficiency bonus, instead of making this a pair, we 426/// just store the two bits we care about in the low bits of the 427/// pointer. To handle the packing/unpacking, we make QualType be a 428/// simple wrapper class that acts like a smart pointer. A third bit 429/// indicates whether there are extended qualifiers present, in which 430/// case the pointer points to a special structure. 431class QualType { 432 // Thankfully, these are efficiently composable. 433 llvm::PointerIntPair<llvm::PointerUnion<const Type*,const ExtQuals*>, 434 Qualifiers::FastWidth> Value; 435 436 const ExtQuals *getExtQualsUnsafe() const { 437 return Value.getPointer().get<const ExtQuals*>(); 438 } 439 440 const Type *getTypePtrUnsafe() const { 441 return Value.getPointer().get<const Type*>(); 442 } 443 444 QualType getUnqualifiedTypeSlow() const; 445 446 friend class QualifierCollector; 447public: 448 QualType() {} 449 450 QualType(const Type *Ptr, unsigned Quals) 451 : Value(Ptr, Quals) {} 452 QualType(const ExtQuals *Ptr, unsigned Quals) 453 : Value(Ptr, Quals) {} 454 455 unsigned getLocalFastQualifiers() const { return Value.getInt(); } 456 void setLocalFastQualifiers(unsigned Quals) { Value.setInt(Quals); } 457 458 /// Retrieves a pointer to the underlying (unqualified) type. 459 /// This should really return a const Type, but it's not worth 460 /// changing all the users right now. 461 /// 462 /// This function requires that the type not be NULL. If the type might be 463 /// NULL, use the (slightly less efficient) \c getTypePtrOrNull(). 464 Type *getTypePtr() const { 465 assert(!isNull() && "Cannot retrieve a NULL type pointer"); 466 uintptr_t CommonPtrVal 467 = reinterpret_cast<uintptr_t>(Value.getOpaqueValue()); 468 CommonPtrVal &= ~(uintptr_t)((1 << TypeAlignmentInBits) - 1); 469 ExtQualsTypeCommonBase *CommonPtr 470 = reinterpret_cast<ExtQualsTypeCommonBase*>(CommonPtrVal); 471 return const_cast<Type *>(CommonPtr->BaseType); 472 } 473 474 Type *getTypePtrOrNull() const { 475 uintptr_t TypePtrPtrVal 476 = reinterpret_cast<uintptr_t>(Value.getOpaqueValue()); 477 TypePtrPtrVal &= ~(uintptr_t)((1 << TypeAlignmentInBits) - 1); 478 Type **TypePtrPtr = reinterpret_cast<Type**>(TypePtrPtrVal); 479 return TypePtrPtr? *TypePtrPtr : 0; 480 } 481 482 /// Divides a QualType into its unqualified type and a set of local 483 /// qualifiers. 484 SplitQualType split() const { 485 if (!hasLocalNonFastQualifiers()) 486 return SplitQualType(getTypePtrUnsafe(), 487 Qualifiers::fromFastMask(getLocalFastQualifiers())); 488 489 const ExtQuals *eq = getExtQualsUnsafe(); 490 Qualifiers qs = eq->getQualifiers(); 491 qs.addFastQualifiers(getLocalFastQualifiers()); 492 return SplitQualType(eq->getBaseType(), qs); 493 } 494 495 void *getAsOpaquePtr() const { return Value.getOpaqueValue(); } 496 static QualType getFromOpaquePtr(void *Ptr) { 497 QualType T; 498 T.Value.setFromOpaqueValue(Ptr); 499 return T; 500 } 501 502 Type &operator*() const { 503 return *getTypePtr(); 504 } 505 506 Type *operator->() const { 507 return getTypePtr(); 508 } 509 510 bool isCanonical() const; 511 bool isCanonicalAsParam() const; 512 513 /// isNull - Return true if this QualType doesn't point to a type yet. 514 bool isNull() const { 515 return Value.getPointer().isNull(); 516 } 517 518 /// \brief Determine whether this particular QualType instance has the 519 /// "const" qualifier set, without looking through typedefs that may have 520 /// added "const" at a different level. 521 bool isLocalConstQualified() const { 522 return (getLocalFastQualifiers() & Qualifiers::Const); 523 } 524 525 /// \brief Determine whether this type is const-qualified. 526 bool isConstQualified() const; 527 528 /// \brief Determine whether this particular QualType instance has the 529 /// "restrict" qualifier set, without looking through typedefs that may have 530 /// added "restrict" at a different level. 531 bool isLocalRestrictQualified() const { 532 return (getLocalFastQualifiers() & Qualifiers::Restrict); 533 } 534 535 /// \brief Determine whether this type is restrict-qualified. 536 bool isRestrictQualified() const; 537 538 /// \brief Determine whether this particular QualType instance has the 539 /// "volatile" qualifier set, without looking through typedefs that may have 540 /// added "volatile" at a different level. 541 bool isLocalVolatileQualified() const { 542 return (getLocalFastQualifiers() & Qualifiers::Volatile); 543 } 544 545 /// \brief Determine whether this type is volatile-qualified. 546 bool isVolatileQualified() const; 547 548 /// \brief Determine whether this particular QualType instance has any 549 /// qualifiers, without looking through any typedefs that might add 550 /// qualifiers at a different level. 551 bool hasLocalQualifiers() const { 552 return getLocalFastQualifiers() || hasLocalNonFastQualifiers(); 553 } 554 555 /// \brief Determine whether this type has any qualifiers. 556 bool hasQualifiers() const; 557 558 /// \brief Determine whether this particular QualType instance has any 559 /// "non-fast" qualifiers, e.g., those that are stored in an ExtQualType 560 /// instance. 561 bool hasLocalNonFastQualifiers() const { 562 return Value.getPointer().is<const ExtQuals*>(); 563 } 564 565 /// \brief Retrieve the set of qualifiers local to this particular QualType 566 /// instance, not including any qualifiers acquired through typedefs or 567 /// other sugar. 568 Qualifiers getLocalQualifiers() const { 569 Qualifiers Quals; 570 if (hasLocalNonFastQualifiers()) 571 Quals = getExtQualsUnsafe()->getQualifiers(); 572 Quals.addFastQualifiers(getLocalFastQualifiers()); 573 return Quals; 574 } 575 576 /// \brief Retrieve the set of qualifiers applied to this type. 577 Qualifiers getQualifiers() const; 578 579 /// \brief Retrieve the set of CVR (const-volatile-restrict) qualifiers 580 /// local to this particular QualType instance, not including any qualifiers 581 /// acquired through typedefs or other sugar. 582 unsigned getLocalCVRQualifiers() const { 583 return getLocalFastQualifiers(); 584 } 585 586 /// \brief Retrieve the set of CVR (const-volatile-restrict) qualifiers 587 /// applied to this type. 588 unsigned getCVRQualifiers() const; 589 590 /// \brief Retrieve the set of CVR (const-volatile-restrict) qualifiers 591 /// applied to this type, looking through any number of unqualified array 592 /// types to their element types' qualifiers. 593 unsigned getCVRQualifiersThroughArrayTypes() const; 594 595 bool isConstant(ASTContext& Ctx) const { 596 return QualType::isConstant(*this, Ctx); 597 } 598 599 // Don't promise in the API that anything besides 'const' can be 600 // easily added. 601 602 /// addConst - add the specified type qualifier to this QualType. 603 void addConst() { 604 addFastQualifiers(Qualifiers::Const); 605 } 606 QualType withConst() const { 607 return withFastQualifiers(Qualifiers::Const); 608 } 609 610 void addFastQualifiers(unsigned TQs) { 611 assert(!(TQs & ~Qualifiers::FastMask) 612 && "non-fast qualifier bits set in mask!"); 613 Value.setInt(Value.getInt() | TQs); 614 } 615 616 void removeLocalConst(); 617 void removeLocalVolatile(); 618 void removeLocalRestrict(); 619 void removeLocalCVRQualifiers(unsigned Mask); 620 621 void removeLocalFastQualifiers() { Value.setInt(0); } 622 void removeLocalFastQualifiers(unsigned Mask) { 623 assert(!(Mask & ~Qualifiers::FastMask) && "mask has non-fast qualifiers"); 624 Value.setInt(Value.getInt() & ~Mask); 625 } 626 627 // Creates a type with the given qualifiers in addition to any 628 // qualifiers already on this type. 629 QualType withFastQualifiers(unsigned TQs) const { 630 QualType T = *this; 631 T.addFastQualifiers(TQs); 632 return T; 633 } 634 635 // Creates a type with exactly the given fast qualifiers, removing 636 // any existing fast qualifiers. 637 QualType withExactLocalFastQualifiers(unsigned TQs) const { 638 return withoutLocalFastQualifiers().withFastQualifiers(TQs); 639 } 640 641 // Removes fast qualifiers, but leaves any extended qualifiers in place. 642 QualType withoutLocalFastQualifiers() const { 643 QualType T = *this; 644 T.removeLocalFastQualifiers(); 645 return T; 646 } 647 648 /// \brief Return this type with all of the instance-specific qualifiers 649 /// removed, but without removing any qualifiers that may have been applied 650 /// through typedefs. 651 QualType getLocalUnqualifiedType() const { return QualType(getTypePtr(), 0); } 652 653 /// \brief Return the unqualified form of the given type, which might be 654 /// desugared to eliminate qualifiers introduced via typedefs. 655 QualType getUnqualifiedType() const { 656 QualType T = getLocalUnqualifiedType(); 657 if (!T.hasQualifiers()) 658 return T; 659 660 return getUnqualifiedTypeSlow(); 661 } 662 663 bool isMoreQualifiedThan(QualType Other) const; 664 bool isAtLeastAsQualifiedAs(QualType Other) const; 665 QualType getNonReferenceType() const; 666 667 /// \brief Determine the type of a (typically non-lvalue) expression with the 668 /// specified result type. 669 /// 670 /// This routine should be used for expressions for which the return type is 671 /// explicitly specified (e.g., in a cast or call) and isn't necessarily 672 /// an lvalue. It removes a top-level reference (since there are no 673 /// expressions of reference type) and deletes top-level cvr-qualifiers 674 /// from non-class types (in C++) or all types (in C). 675 QualType getNonLValueExprType(ASTContext &Context) const; 676 677 /// getDesugaredType - Return the specified type with any "sugar" removed from 678 /// the type. This takes off typedefs, typeof's etc. If the outer level of 679 /// the type is already concrete, it returns it unmodified. This is similar 680 /// to getting the canonical type, but it doesn't remove *all* typedefs. For 681 /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is 682 /// concrete. 683 /// 684 /// Qualifiers are left in place. 685 QualType getDesugaredType(ASTContext &Context) const { 686 return getDesugaredType(*this, Context); 687 } 688 689 SplitQualType getSplitDesugaredType() const { 690 return getSplitDesugaredType(*this); 691 } 692 693 /// IgnoreParens - Returns the specified type after dropping any 694 /// outer-level parentheses. 695 QualType IgnoreParens() const { 696 if (isa<ParenType>(*this)) 697 return QualType::IgnoreParens(*this); 698 return *this; 699 } 700 701 /// operator==/!= - Indicate whether the specified types and qualifiers are 702 /// identical. 703 friend bool operator==(const QualType &LHS, const QualType &RHS) { 704 return LHS.Value == RHS.Value; 705 } 706 friend bool operator!=(const QualType &LHS, const QualType &RHS) { 707 return LHS.Value != RHS.Value; 708 } 709 std::string getAsString() const { 710 return getAsString(split()); 711 } 712 static std::string getAsString(SplitQualType split) { 713 return getAsString(split.first, split.second); 714 } 715 static std::string getAsString(const Type *ty, Qualifiers qs); 716 717 std::string getAsString(const PrintingPolicy &Policy) const { 718 std::string S; 719 getAsStringInternal(S, Policy); 720 return S; 721 } 722 void getAsStringInternal(std::string &Str, 723 const PrintingPolicy &Policy) const { 724 return getAsStringInternal(split(), Str, Policy); 725 } 726 static void getAsStringInternal(SplitQualType split, std::string &out, 727 const PrintingPolicy &policy) { 728 return getAsStringInternal(split.first, split.second, out, policy); 729 } 730 static void getAsStringInternal(const Type *ty, Qualifiers qs, 731 std::string &out, 732 const PrintingPolicy &policy); 733 734 void dump(const char *s) const; 735 void dump() const; 736 737 void Profile(llvm::FoldingSetNodeID &ID) const { 738 ID.AddPointer(getAsOpaquePtr()); 739 } 740 741 /// getAddressSpace - Return the address space of this type. 742 inline unsigned getAddressSpace() const; 743 744 /// GCAttrTypesAttr - Returns gc attribute of this type. 745 inline Qualifiers::GC getObjCGCAttr() const; 746 747 /// isObjCGCWeak true when Type is objc's weak. 748 bool isObjCGCWeak() const { 749 return getObjCGCAttr() == Qualifiers::Weak; 750 } 751 752 /// isObjCGCStrong true when Type is objc's strong. 753 bool isObjCGCStrong() const { 754 return getObjCGCAttr() == Qualifiers::Strong; 755 } 756 757private: 758 // These methods are implemented in a separate translation unit; 759 // "static"-ize them to avoid creating temporary QualTypes in the 760 // caller. 761 static bool isConstant(QualType T, ASTContext& Ctx); 762 static QualType getDesugaredType(QualType T, ASTContext &Context); 763 static SplitQualType getSplitDesugaredType(QualType T); 764 static QualType IgnoreParens(QualType T); 765}; 766 767} // end clang. 768 769namespace llvm { 770/// Implement simplify_type for QualType, so that we can dyn_cast from QualType 771/// to a specific Type class. 772template<> struct simplify_type<const ::clang::QualType> { 773 typedef ::clang::Type* SimpleType; 774 static SimpleType getSimplifiedValue(const ::clang::QualType &Val) { 775 return Val.getTypePtr(); 776 } 777}; 778template<> struct simplify_type< ::clang::QualType> 779 : public simplify_type<const ::clang::QualType> {}; 780 781// Teach SmallPtrSet that QualType is "basically a pointer". 782template<> 783class PointerLikeTypeTraits<clang::QualType> { 784public: 785 static inline void *getAsVoidPointer(clang::QualType P) { 786 return P.getAsOpaquePtr(); 787 } 788 static inline clang::QualType getFromVoidPointer(void *P) { 789 return clang::QualType::getFromOpaquePtr(P); 790 } 791 // Various qualifiers go in low bits. 792 enum { NumLowBitsAvailable = 0 }; 793}; 794 795} // end namespace llvm 796 797namespace clang { 798 799/// Type - This is the base class of the type hierarchy. A central concept 800/// with types is that each type always has a canonical type. A canonical type 801/// is the type with any typedef names stripped out of it or the types it 802/// references. For example, consider: 803/// 804/// typedef int foo; 805/// typedef foo* bar; 806/// 'int *' 'foo *' 'bar' 807/// 808/// There will be a Type object created for 'int'. Since int is canonical, its 809/// canonicaltype pointer points to itself. There is also a Type for 'foo' (a 810/// TypedefType). Its CanonicalType pointer points to the 'int' Type. Next 811/// there is a PointerType that represents 'int*', which, like 'int', is 812/// canonical. Finally, there is a PointerType type for 'foo*' whose canonical 813/// type is 'int*', and there is a TypedefType for 'bar', whose canonical type 814/// is also 'int*'. 815/// 816/// Non-canonical types are useful for emitting diagnostics, without losing 817/// information about typedefs being used. Canonical types are useful for type 818/// comparisons (they allow by-pointer equality tests) and useful for reasoning 819/// about whether something has a particular form (e.g. is a function type), 820/// because they implicitly, recursively, strip all typedefs out of a type. 821/// 822/// Types, once created, are immutable. 823/// 824class Type : public ExtQualsTypeCommonBase { 825public: 826 enum TypeClass { 827#define TYPE(Class, Base) Class, 828#define LAST_TYPE(Class) TypeLast = Class, 829#define ABSTRACT_TYPE(Class, Base) 830#include "clang/AST/TypeNodes.def" 831 TagFirst = Record, TagLast = Enum 832 }; 833 834private: 835 Type(const Type&); // DO NOT IMPLEMENT. 836 void operator=(const Type&); // DO NOT IMPLEMENT. 837 838 QualType CanonicalType; 839 840 /// Bitfields required by the Type class. 841 class TypeBitfields { 842 friend class Type; 843 template <class T> friend class TypePropertyCache; 844 845 /// TypeClass bitfield - Enum that specifies what subclass this belongs to. 846 unsigned TC : 8; 847 848 /// Dependent - Whether this type is a dependent type (C++ [temp.dep.type]). 849 /// Note that this should stay at the end of the ivars for Type so that 850 /// subclasses can pack their bitfields into the same word. 851 unsigned Dependent : 1; 852 853 /// \brief Whether this type is a variably-modified type (C99 6.7.5). 854 unsigned VariablyModified : 1; 855 856 /// \brief Whether this type contains an unexpanded parameter pack 857 /// (for C++0x variadic templates). 858 unsigned ContainsUnexpandedParameterPack : 1; 859 860 /// \brief Nonzero if the cache (i.e. the bitfields here starting 861 /// with 'Cache') is valid. If so, then this is a 862 /// LangOptions::VisibilityMode+1. 863 mutable unsigned CacheValidAndVisibility : 2; 864 865 /// \brief Linkage of this type. 866 mutable unsigned CachedLinkage : 2; 867 868 /// \brief Whether this type involves and local or unnamed types. 869 mutable unsigned CachedLocalOrUnnamed : 1; 870 871 /// \brief FromAST - Whether this type comes from an AST file. 872 mutable unsigned FromAST : 1; 873 874 bool isCacheValid() const { 875 return (CacheValidAndVisibility != 0); 876 } 877 Visibility getVisibility() const { 878 assert(isCacheValid() && "getting linkage from invalid cache"); 879 return static_cast<Visibility>(CacheValidAndVisibility-1); 880 } 881 Linkage getLinkage() const { 882 assert(isCacheValid() && "getting linkage from invalid cache"); 883 return static_cast<Linkage>(CachedLinkage); 884 } 885 bool hasLocalOrUnnamedType() const { 886 assert(isCacheValid() && "getting linkage from invalid cache"); 887 return CachedLocalOrUnnamed; 888 } 889 }; 890 enum { NumTypeBits = 17 }; 891 892protected: 893 // These classes allow subclasses to somewhat cleanly pack bitfields 894 // into Type. 895 896 class ArrayTypeBitfields { 897 friend class ArrayType; 898 899 unsigned : NumTypeBits; 900 901 /// IndexTypeQuals - CVR qualifiers from declarations like 902 /// 'int X[static restrict 4]'. For function parameters only. 903 unsigned IndexTypeQuals : 3; 904 905 /// SizeModifier - storage class qualifiers from declarations like 906 /// 'int X[static restrict 4]'. For function parameters only. 907 /// Actually an ArrayType::ArraySizeModifier. 908 unsigned SizeModifier : 3; 909 }; 910 911 class BuiltinTypeBitfields { 912 friend class BuiltinType; 913 914 unsigned : NumTypeBits; 915 916 /// The kind (BuiltinType::Kind) of builtin type this is. 917 unsigned Kind : 8; 918 }; 919 920 class FunctionTypeBitfields { 921 friend class FunctionType; 922 923 unsigned : NumTypeBits; 924 925 /// Extra information which affects how the function is called, like 926 /// regparm and the calling convention. 927 unsigned ExtInfo : 8; 928 929 /// Whether the function is variadic. Only used by FunctionProtoType. 930 unsigned Variadic : 1; 931 932 /// TypeQuals - Used only by FunctionProtoType, put here to pack with the 933 /// other bitfields. 934 /// The qualifiers are part of FunctionProtoType because... 935 /// 936 /// C++ 8.3.5p4: The return type, the parameter type list and the 937 /// cv-qualifier-seq, [...], are part of the function type. 938 unsigned TypeQuals : 3; 939 }; 940 941 class ObjCObjectTypeBitfields { 942 friend class ObjCObjectType; 943 944 unsigned : NumTypeBits; 945 946 /// NumProtocols - The number of protocols stored directly on this 947 /// object type. 948 unsigned NumProtocols : 32 - NumTypeBits; 949 }; 950 951 class ReferenceTypeBitfields { 952 friend class ReferenceType; 953 954 unsigned : NumTypeBits; 955 956 /// True if the type was originally spelled with an lvalue sigil. 957 /// This is never true of rvalue references but can also be false 958 /// on lvalue references because of C++0x [dcl.typedef]p9, 959 /// as follows: 960 /// 961 /// typedef int &ref; // lvalue, spelled lvalue 962 /// typedef int &&rvref; // rvalue 963 /// ref &a; // lvalue, inner ref, spelled lvalue 964 /// ref &&a; // lvalue, inner ref 965 /// rvref &a; // lvalue, inner ref, spelled lvalue 966 /// rvref &&a; // rvalue, inner ref 967 unsigned SpelledAsLValue : 1; 968 969 /// True if the inner type is a reference type. This only happens 970 /// in non-canonical forms. 971 unsigned InnerRef : 1; 972 }; 973 974 class TypeWithKeywordBitfields { 975 friend class TypeWithKeyword; 976 977 unsigned : NumTypeBits; 978 979 /// An ElaboratedTypeKeyword. 8 bits for efficient access. 980 unsigned Keyword : 8; 981 }; 982 983 class VectorTypeBitfields { 984 friend class VectorType; 985 986 unsigned : NumTypeBits; 987 988 /// VecKind - The kind of vector, either a generic vector type or some 989 /// target-specific vector type such as for AltiVec or Neon. 990 unsigned VecKind : 3; 991 992 /// NumElements - The number of elements in the vector. 993 unsigned NumElements : 29 - NumTypeBits; 994 }; 995 996 union { 997 TypeBitfields TypeBits; 998 ArrayTypeBitfields ArrayTypeBits; 999 BuiltinTypeBitfields BuiltinTypeBits; 1000 FunctionTypeBitfields FunctionTypeBits; 1001 ObjCObjectTypeBitfields ObjCObjectTypeBits; 1002 ReferenceTypeBitfields ReferenceTypeBits; 1003 TypeWithKeywordBitfields TypeWithKeywordBits; 1004 VectorTypeBitfields VectorTypeBits; 1005 }; 1006 1007private: 1008 /// \brief Set whether this type comes from an AST file. 1009 void setFromAST(bool V = true) const { 1010 TypeBits.FromAST = V; 1011 } 1012 1013 template <class T> friend class TypePropertyCache; 1014 1015protected: 1016 // silence VC++ warning C4355: 'this' : used in base member initializer list 1017 Type *this_() { return this; } 1018 Type(TypeClass tc, QualType Canonical, bool Dependent, bool VariablyModified, 1019 bool ContainsUnexpandedParameterPack) 1020 : ExtQualsTypeCommonBase(this), 1021 CanonicalType(Canonical.isNull() ? QualType(this_(), 0) : Canonical) { 1022 TypeBits.TC = tc; 1023 TypeBits.Dependent = Dependent; 1024 TypeBits.VariablyModified = VariablyModified; 1025 TypeBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack; 1026 TypeBits.CacheValidAndVisibility = 0; 1027 TypeBits.CachedLocalOrUnnamed = false; 1028 TypeBits.CachedLinkage = NoLinkage; 1029 TypeBits.FromAST = false; 1030 } 1031 friend class ASTContext; 1032 1033 void setDependent(bool D = true) { TypeBits.Dependent = D; } 1034 void setVariablyModified(bool VM = true) { TypeBits.VariablyModified = VM; } 1035 void setContainsUnexpandedParameterPack(bool PP = true) { 1036 TypeBits.ContainsUnexpandedParameterPack = PP; 1037 } 1038 1039public: 1040 TypeClass getTypeClass() const { return static_cast<TypeClass>(TypeBits.TC); } 1041 1042 /// \brief Whether this type comes from an AST file. 1043 bool isFromAST() const { return TypeBits.FromAST; } 1044 1045 /// \brief Whether this type is or contains an unexpanded parameter 1046 /// pack, used to support C++0x variadic templates. 1047 /// 1048 /// A type that contains a parameter pack shall be expanded by the 1049 /// ellipsis operator at some point. For example, the typedef in the 1050 /// following example contains an unexpanded parameter pack 'T': 1051 /// 1052 /// \code 1053 /// template<typename ...T> 1054 /// struct X { 1055 /// typedef T* pointer_types; // ill-formed; T is a parameter pack. 1056 /// }; 1057 /// \endcode 1058 /// 1059 /// Note that this routine does not specify which 1060 bool containsUnexpandedParameterPack() const { 1061 return TypeBits.ContainsUnexpandedParameterPack; 1062 } 1063 1064 bool isCanonicalUnqualified() const { 1065 return CanonicalType.getTypePtr() == this; 1066 } 1067 1068 /// Types are partitioned into 3 broad categories (C99 6.2.5p1): 1069 /// object types, function types, and incomplete types. 1070 1071 /// isIncompleteType - Return true if this is an incomplete type. 1072 /// A type that can describe objects, but which lacks information needed to 1073 /// determine its size (e.g. void, or a fwd declared struct). Clients of this 1074 /// routine will need to determine if the size is actually required. 1075 bool isIncompleteType() const; 1076 1077 /// isIncompleteOrObjectType - Return true if this is an incomplete or object 1078 /// type, in other words, not a function type. 1079 bool isIncompleteOrObjectType() const { 1080 return !isFunctionType(); 1081 } 1082 1083 /// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10). 1084 bool isPODType() const; 1085 1086 /// isLiteralType - Return true if this is a literal type 1087 /// (C++0x [basic.types]p10) 1088 bool isLiteralType() const; 1089 1090 /// Helper methods to distinguish type categories. All type predicates 1091 /// operate on the canonical type, ignoring typedefs and qualifiers. 1092 1093 /// isBuiltinType - returns true if the type is a builtin type. 1094 bool isBuiltinType() const; 1095 1096 /// isSpecificBuiltinType - Test for a particular builtin type. 1097 bool isSpecificBuiltinType(unsigned K) const; 1098 1099 /// isPlaceholderType - Test for a type which does not represent an 1100 /// actual type-system type but is instead used as a placeholder for 1101 /// various convenient purposes within Clang. All such types are 1102 /// BuiltinTypes. 1103 bool isPlaceholderType() const; 1104 1105 /// isIntegerType() does *not* include complex integers (a GCC extension). 1106 /// isComplexIntegerType() can be used to test for complex integers. 1107 bool isIntegerType() const; // C99 6.2.5p17 (int, char, bool, enum) 1108 bool isEnumeralType() const; 1109 bool isBooleanType() const; 1110 bool isCharType() const; 1111 bool isWideCharType() const; 1112 bool isAnyCharacterType() const; 1113 bool isIntegralType(ASTContext &Ctx) const; 1114 1115 /// \brief Determine whether this type is an integral or enumeration type. 1116 bool isIntegralOrEnumerationType() const; 1117 /// \brief Determine whether this type is an integral or unscoped enumeration 1118 /// type. 1119 bool isIntegralOrUnscopedEnumerationType() const; 1120 1121 /// Floating point categories. 1122 bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double) 1123 /// isComplexType() does *not* include complex integers (a GCC extension). 1124 /// isComplexIntegerType() can be used to test for complex integers. 1125 bool isComplexType() const; // C99 6.2.5p11 (complex) 1126 bool isAnyComplexType() const; // C99 6.2.5p11 (complex) + Complex Int. 1127 bool isFloatingType() const; // C99 6.2.5p11 (real floating + complex) 1128 bool isRealType() const; // C99 6.2.5p17 (real floating + integer) 1129 bool isArithmeticType() const; // C99 6.2.5p18 (integer + floating) 1130 bool isVoidType() const; // C99 6.2.5p19 1131 bool isDerivedType() const; // C99 6.2.5p20 1132 bool isScalarType() const; // C99 6.2.5p21 (arithmetic + pointers) 1133 bool isAggregateType() const; 1134 1135 // Type Predicates: Check to see if this type is structurally the specified 1136 // type, ignoring typedefs and qualifiers. 1137 bool isFunctionType() const; 1138 bool isFunctionNoProtoType() const { return getAs<FunctionNoProtoType>(); } 1139 bool isFunctionProtoType() const { return getAs<FunctionProtoType>(); } 1140 bool isPointerType() const; 1141 bool isAnyPointerType() const; // Any C pointer or ObjC object pointer 1142 bool isBlockPointerType() const; 1143 bool isVoidPointerType() const; 1144 bool isReferenceType() const; 1145 bool isLValueReferenceType() const; 1146 bool isRValueReferenceType() const; 1147 bool isFunctionPointerType() const; 1148 bool isMemberPointerType() const; 1149 bool isMemberFunctionPointerType() const; 1150 bool isMemberDataPointerType() const; 1151 bool isArrayType() const; 1152 bool isConstantArrayType() const; 1153 bool isIncompleteArrayType() const; 1154 bool isVariableArrayType() const; 1155 bool isDependentSizedArrayType() const; 1156 bool isRecordType() const; 1157 bool isClassType() const; 1158 bool isStructureType() const; 1159 bool isStructureOrClassType() const; 1160 bool isUnionType() const; 1161 bool isComplexIntegerType() const; // GCC _Complex integer type. 1162 bool isVectorType() const; // GCC vector type. 1163 bool isExtVectorType() const; // Extended vector type. 1164 bool isObjCObjectPointerType() const; // Pointer to *any* ObjC object. 1165 // FIXME: change this to 'raw' interface type, so we can used 'interface' type 1166 // for the common case. 1167 bool isObjCObjectType() const; // NSString or typeof(*(id)0) 1168 bool isObjCQualifiedInterfaceType() const; // NSString<foo> 1169 bool isObjCQualifiedIdType() const; // id<foo> 1170 bool isObjCQualifiedClassType() const; // Class<foo> 1171 bool isObjCObjectOrInterfaceType() const; 1172 bool isObjCIdType() const; // id 1173 bool isObjCClassType() const; // Class 1174 bool isObjCSelType() const; // Class 1175 bool isObjCBuiltinType() const; // 'id' or 'Class' 1176 bool isTemplateTypeParmType() const; // C++ template type parameter 1177 bool isNullPtrType() const; // C++0x nullptr_t 1178 1179 enum ScalarTypeKind { 1180 STK_Pointer, 1181 STK_MemberPointer, 1182 STK_Bool, 1183 STK_Integral, 1184 STK_Floating, 1185 STK_IntegralComplex, 1186 STK_FloatingComplex 1187 }; 1188 /// getScalarTypeKind - Given that this is a scalar type, classify it. 1189 ScalarTypeKind getScalarTypeKind() const; 1190 1191 /// isDependentType - Whether this type is a dependent type, meaning 1192 /// that its definition somehow depends on a template parameter 1193 /// (C++ [temp.dep.type]). 1194 bool isDependentType() const { return TypeBits.Dependent; } 1195 1196 /// \brief Whether this type is a variably-modified type (C99 6.7.5). 1197 bool isVariablyModifiedType() const { return TypeBits.VariablyModified; } 1198 1199 /// \brief Whether this type is or contains a local or unnamed type. 1200 bool hasUnnamedOrLocalType() const; 1201 1202 bool isOverloadableType() const; 1203 1204 /// \brief Determine wither this type is a C++ elaborated-type-specifier. 1205 bool isElaboratedTypeSpecifier() const; 1206 1207 /// hasPointerRepresentation - Whether this type is represented 1208 /// natively as a pointer; this includes pointers, references, block 1209 /// pointers, and Objective-C interface, qualified id, and qualified 1210 /// interface types, as well as nullptr_t. 1211 bool hasPointerRepresentation() const; 1212 1213 /// hasObjCPointerRepresentation - Whether this type can represent 1214 /// an objective pointer type for the purpose of GC'ability 1215 bool hasObjCPointerRepresentation() const; 1216 1217 /// \brief Determine whether this type has an integer representation 1218 /// of some sort, e.g., it is an integer type or a vector. 1219 bool hasIntegerRepresentation() const; 1220 1221 /// \brief Determine whether this type has an signed integer representation 1222 /// of some sort, e.g., it is an signed integer type or a vector. 1223 bool hasSignedIntegerRepresentation() const; 1224 1225 /// \brief Determine whether this type has an unsigned integer representation 1226 /// of some sort, e.g., it is an unsigned integer type or a vector. 1227 bool hasUnsignedIntegerRepresentation() const; 1228 1229 /// \brief Determine whether this type has a floating-point representation 1230 /// of some sort, e.g., it is a floating-point type or a vector thereof. 1231 bool hasFloatingRepresentation() const; 1232 1233 // Type Checking Functions: Check to see if this type is structurally the 1234 // specified type, ignoring typedefs and qualifiers, and return a pointer to 1235 // the best type we can. 1236 const RecordType *getAsStructureType() const; 1237 /// NOTE: getAs*ArrayType are methods on ASTContext. 1238 const RecordType *getAsUnionType() const; 1239 const ComplexType *getAsComplexIntegerType() const; // GCC complex int type. 1240 // The following is a convenience method that returns an ObjCObjectPointerType 1241 // for object declared using an interface. 1242 const ObjCObjectPointerType *getAsObjCInterfacePointerType() const; 1243 const ObjCObjectPointerType *getAsObjCQualifiedIdType() const; 1244 const ObjCObjectType *getAsObjCQualifiedInterfaceType() const; 1245 const CXXRecordDecl *getCXXRecordDeclForPointerType() const; 1246 1247 /// \brief Retrieves the CXXRecordDecl that this type refers to, either 1248 /// because the type is a RecordType or because it is the injected-class-name 1249 /// type of a class template or class template partial specialization. 1250 CXXRecordDecl *getAsCXXRecordDecl() const; 1251 1252 // Member-template getAs<specific type>'. Look through sugar for 1253 // an instance of <specific type>. This scheme will eventually 1254 // replace the specific getAsXXXX methods above. 1255 // 1256 // There are some specializations of this member template listed 1257 // immediately following this class. 1258 template <typename T> const T *getAs() const; 1259 1260 /// getArrayElementTypeNoTypeQual - If this is an array type, return the 1261 /// element type of the array, potentially with type qualifiers missing. 1262 /// This method should never be used when type qualifiers are meaningful. 1263 const Type *getArrayElementTypeNoTypeQual() const; 1264 1265 /// getPointeeType - If this is a pointer, ObjC object pointer, or block 1266 /// pointer, this returns the respective pointee. 1267 QualType getPointeeType() const; 1268 1269 /// getUnqualifiedDesugaredType() - Return the specified type with 1270 /// any "sugar" removed from the type, removing any typedefs, 1271 /// typeofs, etc., as well as any qualifiers. 1272 const Type *getUnqualifiedDesugaredType() const; 1273 1274 /// More type predicates useful for type checking/promotion 1275 bool isPromotableIntegerType() const; // C99 6.3.1.1p2 1276 1277 /// isSignedIntegerType - Return true if this is an integer type that is 1278 /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], 1279 /// an enum decl which has a signed representation, or a vector of signed 1280 /// integer element type. 1281 bool isSignedIntegerType() const; 1282 1283 /// isUnsignedIntegerType - Return true if this is an integer type that is 1284 /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum 1285 /// decl which has an unsigned representation, or a vector of unsigned integer 1286 /// element type. 1287 bool isUnsignedIntegerType() const; 1288 1289 /// isConstantSizeType - Return true if this is not a variable sized type, 1290 /// according to the rules of C99 6.7.5p3. It is not legal to call this on 1291 /// incomplete types. 1292 bool isConstantSizeType() const; 1293 1294 /// isSpecifierType - Returns true if this type can be represented by some 1295 /// set of type specifiers. 1296 bool isSpecifierType() const; 1297 1298 /// \brief Determine the linkage of this type. 1299 Linkage getLinkage() const; 1300 1301 /// \brief Determine the visibility of this type. 1302 Visibility getVisibility() const; 1303 1304 /// \brief Determine the linkage and visibility of this type. 1305 std::pair<Linkage,Visibility> getLinkageAndVisibility() const; 1306 1307 /// \brief Note that the linkage is no longer known. 1308 void ClearLinkageCache(); 1309 1310 const char *getTypeClassName() const; 1311 1312 QualType getCanonicalTypeInternal() const { 1313 return CanonicalType; 1314 } 1315 CanQualType getCanonicalTypeUnqualified() const; // in CanonicalType.h 1316 void dump() const; 1317 static bool classof(const Type *) { return true; } 1318 1319 friend class ASTReader; 1320 friend class ASTWriter; 1321}; 1322 1323template <> inline const TypedefType *Type::getAs() const { 1324 return dyn_cast<TypedefType>(this); 1325} 1326 1327// We can do canonical leaf types faster, because we don't have to 1328// worry about preserving child type decoration. 1329#define TYPE(Class, Base) 1330#define LEAF_TYPE(Class) \ 1331template <> inline const Class##Type *Type::getAs() const { \ 1332 return dyn_cast<Class##Type>(CanonicalType); \ 1333} 1334#include "clang/AST/TypeNodes.def" 1335 1336 1337/// BuiltinType - This class is used for builtin types like 'int'. Builtin 1338/// types are always canonical and have a literal name field. 1339class BuiltinType : public Type { 1340public: 1341 enum Kind { 1342 Void, 1343 1344 Bool, // This is bool and/or _Bool. 1345 Char_U, // This is 'char' for targets where char is unsigned. 1346 UChar, // This is explicitly qualified unsigned char. 1347 WChar_U, // This is 'wchar_t' for C++, when unsigned. 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_S, // This is 'wchar_t' for C++, when signed. 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 bool isSugared() const { return false; } 2621 QualType desugar() const { return QualType(this, 0); } 2622 2623 static bool classof(const TagType *T); 2624 static bool classof(const Type *T) { 2625 return isa<TagType>(T) && classof(cast<TagType>(T)); 2626 } 2627 static bool classof(const RecordType *) { return true; } 2628}; 2629 2630/// EnumType - This is a helper class that allows the use of isa/cast/dyncast 2631/// to detect TagType objects of enums. 2632class EnumType : public TagType { 2633 explicit EnumType(const EnumDecl *D) 2634 : TagType(Enum, reinterpret_cast<const TagDecl*>(D), QualType()) { } 2635 friend class ASTContext; // ASTContext creates these. 2636public: 2637 2638 EnumDecl *getDecl() const { 2639 return reinterpret_cast<EnumDecl*>(TagType::getDecl()); 2640 } 2641 2642 bool isSugared() const { return false; } 2643 QualType desugar() const { return QualType(this, 0); } 2644 2645 static bool classof(const TagType *T); 2646 static bool classof(const Type *T) { 2647 return isa<TagType>(T) && classof(cast<TagType>(T)); 2648 } 2649 static bool classof(const EnumType *) { return true; } 2650}; 2651 2652class TemplateTypeParmType : public Type, public llvm::FoldingSetNode { 2653 unsigned Depth : 15; 2654 unsigned ParameterPack : 1; 2655 unsigned Index : 16; 2656 IdentifierInfo *Name; 2657 2658 TemplateTypeParmType(unsigned D, unsigned I, bool PP, IdentifierInfo *N, 2659 QualType Canon) 2660 : Type(TemplateTypeParm, Canon, /*Dependent=*/true, 2661 /*VariablyModified=*/false, PP), 2662 Depth(D), ParameterPack(PP), Index(I), Name(N) { } 2663 2664 TemplateTypeParmType(unsigned D, unsigned I, bool PP) 2665 : Type(TemplateTypeParm, QualType(this, 0), /*Dependent=*/true, 2666 /*VariablyModified=*/false, PP), 2667 Depth(D), ParameterPack(PP), Index(I), Name(0) { } 2668 2669 friend class ASTContext; // ASTContext creates these 2670 2671public: 2672 unsigned getDepth() const { return Depth; } 2673 unsigned getIndex() const { return Index; } 2674 bool isParameterPack() const { return ParameterPack; } 2675 IdentifierInfo *getName() const { return Name; } 2676 2677 bool isSugared() const { return false; } 2678 QualType desugar() const { return QualType(this, 0); } 2679 2680 void Profile(llvm::FoldingSetNodeID &ID) { 2681 Profile(ID, Depth, Index, ParameterPack, Name); 2682 } 2683 2684 static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth, 2685 unsigned Index, bool ParameterPack, 2686 IdentifierInfo *Name) { 2687 ID.AddInteger(Depth); 2688 ID.AddInteger(Index); 2689 ID.AddBoolean(ParameterPack); 2690 ID.AddPointer(Name); 2691 } 2692 2693 static bool classof(const Type *T) { 2694 return T->getTypeClass() == TemplateTypeParm; 2695 } 2696 static bool classof(const TemplateTypeParmType *T) { return true; } 2697}; 2698 2699/// \brief Represents the result of substituting a type for a template 2700/// type parameter. 2701/// 2702/// Within an instantiated template, all template type parameters have 2703/// been replaced with these. They are used solely to record that a 2704/// type was originally written as a template type parameter; 2705/// therefore they are never canonical. 2706class SubstTemplateTypeParmType : public Type, public llvm::FoldingSetNode { 2707 // The original type parameter. 2708 const TemplateTypeParmType *Replaced; 2709 2710 SubstTemplateTypeParmType(const TemplateTypeParmType *Param, QualType Canon) 2711 : Type(SubstTemplateTypeParm, Canon, Canon->isDependentType(), 2712 Canon->isVariablyModifiedType(), 2713 Canon->containsUnexpandedParameterPack()), 2714 Replaced(Param) { } 2715 2716 friend class ASTContext; 2717 2718public: 2719 IdentifierInfo *getName() const { return Replaced->getName(); } 2720 2721 /// Gets the template parameter that was substituted for. 2722 const TemplateTypeParmType *getReplacedParameter() const { 2723 return Replaced; 2724 } 2725 2726 /// Gets the type that was substituted for the template 2727 /// parameter. 2728 QualType getReplacementType() const { 2729 return getCanonicalTypeInternal(); 2730 } 2731 2732 bool isSugared() const { return true; } 2733 QualType desugar() const { return getReplacementType(); } 2734 2735 void Profile(llvm::FoldingSetNodeID &ID) { 2736 Profile(ID, getReplacedParameter(), getReplacementType()); 2737 } 2738 static void Profile(llvm::FoldingSetNodeID &ID, 2739 const TemplateTypeParmType *Replaced, 2740 QualType Replacement) { 2741 ID.AddPointer(Replaced); 2742 ID.AddPointer(Replacement.getAsOpaquePtr()); 2743 } 2744 2745 static bool classof(const Type *T) { 2746 return T->getTypeClass() == SubstTemplateTypeParm; 2747 } 2748 static bool classof(const SubstTemplateTypeParmType *T) { return true; } 2749}; 2750 2751/// \brief Represents the type of a template specialization as written 2752/// in the source code. 2753/// 2754/// Template specialization types represent the syntactic form of a 2755/// template-id that refers to a type, e.g., @c vector<int>. Some 2756/// template specialization types are syntactic sugar, whose canonical 2757/// type will point to some other type node that represents the 2758/// instantiation or class template specialization. For example, a 2759/// class template specialization type of @c vector<int> will refer to 2760/// a tag type for the instantiation 2761/// @c std::vector<int, std::allocator<int>>. 2762/// 2763/// Other template specialization types, for which the template name 2764/// is dependent, may be canonical types. These types are always 2765/// dependent. 2766class TemplateSpecializationType 2767 : public Type, public llvm::FoldingSetNode { 2768 /// \brief The name of the template being specialized. 2769 TemplateName Template; 2770 2771 /// \brief - The number of template arguments named in this class 2772 /// template specialization. 2773 unsigned NumArgs; 2774 2775 TemplateSpecializationType(TemplateName T, 2776 const TemplateArgument *Args, 2777 unsigned NumArgs, QualType Canon); 2778 2779 friend class ASTContext; // ASTContext creates these 2780 2781public: 2782 /// \brief Determine whether any of the given template arguments are 2783 /// dependent. 2784 static bool anyDependentTemplateArguments(const TemplateArgument *Args, 2785 unsigned NumArgs); 2786 2787 static bool anyDependentTemplateArguments(const TemplateArgumentLoc *Args, 2788 unsigned NumArgs); 2789 2790 static bool anyDependentTemplateArguments(const TemplateArgumentListInfo &); 2791 2792 /// \brief Print a template argument list, including the '<' and '>' 2793 /// enclosing the template arguments. 2794 static std::string PrintTemplateArgumentList(const TemplateArgument *Args, 2795 unsigned NumArgs, 2796 const PrintingPolicy &Policy, 2797 bool SkipBrackets = false); 2798 2799 static std::string PrintTemplateArgumentList(const TemplateArgumentLoc *Args, 2800 unsigned NumArgs, 2801 const PrintingPolicy &Policy); 2802 2803 static std::string PrintTemplateArgumentList(const TemplateArgumentListInfo &, 2804 const PrintingPolicy &Policy); 2805 2806 /// True if this template specialization type matches a current 2807 /// instantiation in the context in which it is found. 2808 bool isCurrentInstantiation() const { 2809 return isa<InjectedClassNameType>(getCanonicalTypeInternal()); 2810 } 2811 2812 typedef const TemplateArgument * iterator; 2813 2814 iterator begin() const { return getArgs(); } 2815 iterator end() const; // defined inline in TemplateBase.h 2816 2817 /// \brief Retrieve the name of the template that we are specializing. 2818 TemplateName getTemplateName() const { return Template; } 2819 2820 /// \brief Retrieve the template arguments. 2821 const TemplateArgument *getArgs() const { 2822 return reinterpret_cast<const TemplateArgument *>(this + 1); 2823 } 2824 2825 /// \brief Retrieve the number of template arguments. 2826 unsigned getNumArgs() const { return NumArgs; } 2827 2828 /// \brief Retrieve a specific template argument as a type. 2829 /// \precondition @c isArgType(Arg) 2830 const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h 2831 2832 bool isSugared() const { 2833 return !isDependentType() || isCurrentInstantiation(); 2834 } 2835 QualType desugar() const { return getCanonicalTypeInternal(); } 2836 2837 void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Ctx) { 2838 Profile(ID, Template, getArgs(), NumArgs, Ctx); 2839 } 2840 2841 static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T, 2842 const TemplateArgument *Args, 2843 unsigned NumArgs, 2844 ASTContext &Context); 2845 2846 static bool classof(const Type *T) { 2847 return T->getTypeClass() == TemplateSpecialization; 2848 } 2849 static bool classof(const TemplateSpecializationType *T) { return true; } 2850}; 2851 2852/// \brief The injected class name of a C++ class template or class 2853/// template partial specialization. Used to record that a type was 2854/// spelled with a bare identifier rather than as a template-id; the 2855/// equivalent for non-templated classes is just RecordType. 2856/// 2857/// Injected class name types are always dependent. Template 2858/// instantiation turns these into RecordTypes. 2859/// 2860/// Injected class name types are always canonical. This works 2861/// because it is impossible to compare an injected class name type 2862/// with the corresponding non-injected template type, for the same 2863/// reason that it is impossible to directly compare template 2864/// parameters from different dependent contexts: injected class name 2865/// types can only occur within the scope of a particular templated 2866/// declaration, and within that scope every template specialization 2867/// will canonicalize to the injected class name (when appropriate 2868/// according to the rules of the language). 2869class InjectedClassNameType : public Type { 2870 CXXRecordDecl *Decl; 2871 2872 /// The template specialization which this type represents. 2873 /// For example, in 2874 /// template <class T> class A { ... }; 2875 /// this is A<T>, whereas in 2876 /// template <class X, class Y> class A<B<X,Y> > { ... }; 2877 /// this is A<B<X,Y> >. 2878 /// 2879 /// It is always unqualified, always a template specialization type, 2880 /// and always dependent. 2881 QualType InjectedType; 2882 2883 friend class ASTContext; // ASTContext creates these. 2884 friend class ASTReader; // FIXME: ASTContext::getInjectedClassNameType is not 2885 // currently suitable for AST reading, too much 2886 // interdependencies. 2887 InjectedClassNameType(CXXRecordDecl *D, QualType TST) 2888 : Type(InjectedClassName, QualType(), /*Dependent=*/true, 2889 /*VariablyModified=*/false, 2890 /*ContainsUnexpandedParameterPack=*/false), 2891 Decl(D), InjectedType(TST) { 2892 assert(isa<TemplateSpecializationType>(TST)); 2893 assert(!TST.hasQualifiers()); 2894 assert(TST->isDependentType()); 2895 } 2896 2897public: 2898 QualType getInjectedSpecializationType() const { return InjectedType; } 2899 const TemplateSpecializationType *getInjectedTST() const { 2900 return cast<TemplateSpecializationType>(InjectedType.getTypePtr()); 2901 } 2902 2903 CXXRecordDecl *getDecl() const; 2904 2905 bool isSugared() const { return false; } 2906 QualType desugar() const { return QualType(this, 0); } 2907 2908 static bool classof(const Type *T) { 2909 return T->getTypeClass() == InjectedClassName; 2910 } 2911 static bool classof(const InjectedClassNameType *T) { return true; } 2912}; 2913 2914/// \brief The kind of a tag type. 2915enum TagTypeKind { 2916 /// \brief The "struct" keyword. 2917 TTK_Struct, 2918 /// \brief The "union" keyword. 2919 TTK_Union, 2920 /// \brief The "class" keyword. 2921 TTK_Class, 2922 /// \brief The "enum" keyword. 2923 TTK_Enum 2924}; 2925 2926/// \brief The elaboration keyword that precedes a qualified type name or 2927/// introduces an elaborated-type-specifier. 2928enum ElaboratedTypeKeyword { 2929 /// \brief The "struct" keyword introduces the elaborated-type-specifier. 2930 ETK_Struct, 2931 /// \brief The "union" keyword introduces the elaborated-type-specifier. 2932 ETK_Union, 2933 /// \brief The "class" keyword introduces the elaborated-type-specifier. 2934 ETK_Class, 2935 /// \brief The "enum" keyword introduces the elaborated-type-specifier. 2936 ETK_Enum, 2937 /// \brief The "typename" keyword precedes the qualified type name, e.g., 2938 /// \c typename T::type. 2939 ETK_Typename, 2940 /// \brief No keyword precedes the qualified type name. 2941 ETK_None 2942}; 2943 2944/// A helper class for Type nodes having an ElaboratedTypeKeyword. 2945/// The keyword in stored in the free bits of the base class. 2946/// Also provides a few static helpers for converting and printing 2947/// elaborated type keyword and tag type kind enumerations. 2948class TypeWithKeyword : public Type { 2949protected: 2950 TypeWithKeyword(ElaboratedTypeKeyword Keyword, TypeClass tc, 2951 QualType Canonical, bool Dependent, bool VariablyModified, 2952 bool ContainsUnexpandedParameterPack) 2953 : Type(tc, Canonical, Dependent, VariablyModified, 2954 ContainsUnexpandedParameterPack) { 2955 TypeWithKeywordBits.Keyword = Keyword; 2956 } 2957 2958public: 2959 ElaboratedTypeKeyword getKeyword() const { 2960 return static_cast<ElaboratedTypeKeyword>(TypeWithKeywordBits.Keyword); 2961 } 2962 2963 /// getKeywordForTypeSpec - Converts a type specifier (DeclSpec::TST) 2964 /// into an elaborated type keyword. 2965 static ElaboratedTypeKeyword getKeywordForTypeSpec(unsigned TypeSpec); 2966 2967 /// getTagTypeKindForTypeSpec - Converts a type specifier (DeclSpec::TST) 2968 /// into a tag type kind. It is an error to provide a type specifier 2969 /// which *isn't* a tag kind here. 2970 static TagTypeKind getTagTypeKindForTypeSpec(unsigned TypeSpec); 2971 2972 /// getKeywordForTagDeclKind - Converts a TagTypeKind into an 2973 /// elaborated type keyword. 2974 static ElaboratedTypeKeyword getKeywordForTagTypeKind(TagTypeKind Tag); 2975 2976 /// getTagTypeKindForKeyword - Converts an elaborated type keyword into 2977 // a TagTypeKind. It is an error to provide an elaborated type keyword 2978 /// which *isn't* a tag kind here. 2979 static TagTypeKind getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword); 2980 2981 static bool KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword); 2982 2983 static const char *getKeywordName(ElaboratedTypeKeyword Keyword); 2984 2985 static const char *getTagTypeKindName(TagTypeKind Kind) { 2986 return getKeywordName(getKeywordForTagTypeKind(Kind)); 2987 } 2988 2989 class CannotCastToThisType {}; 2990 static CannotCastToThisType classof(const Type *); 2991}; 2992 2993/// \brief Represents a type that was referred to using an elaborated type 2994/// keyword, e.g., struct S, or via a qualified name, e.g., N::M::type, 2995/// or both. 2996/// 2997/// This type is used to keep track of a type name as written in the 2998/// source code, including tag keywords and any nested-name-specifiers. 2999/// The type itself is always "sugar", used to express what was written 3000/// in the source code but containing no additional semantic information. 3001class ElaboratedType : public TypeWithKeyword, public llvm::FoldingSetNode { 3002 3003 /// \brief The nested name specifier containing the qualifier. 3004 NestedNameSpecifier *NNS; 3005 3006 /// \brief The type that this qualified name refers to. 3007 QualType NamedType; 3008 3009 ElaboratedType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, 3010 QualType NamedType, QualType CanonType) 3011 : TypeWithKeyword(Keyword, Elaborated, CanonType, 3012 NamedType->isDependentType(), 3013 NamedType->isVariablyModifiedType(), 3014 NamedType->containsUnexpandedParameterPack()), 3015 NNS(NNS), NamedType(NamedType) { 3016 assert(!(Keyword == ETK_None && NNS == 0) && 3017 "ElaboratedType cannot have elaborated type keyword " 3018 "and name qualifier both null."); 3019 } 3020 3021 friend class ASTContext; // ASTContext creates these 3022 3023public: 3024 ~ElaboratedType(); 3025 3026 /// \brief Retrieve the qualification on this type. 3027 NestedNameSpecifier *getQualifier() const { return NNS; } 3028 3029 /// \brief Retrieve the type named by the qualified-id. 3030 QualType getNamedType() const { return NamedType; } 3031 3032 /// \brief Remove a single level of sugar. 3033 QualType desugar() const { return getNamedType(); } 3034 3035 /// \brief Returns whether this type directly provides sugar. 3036 bool isSugared() const { return true; } 3037 3038 void Profile(llvm::FoldingSetNodeID &ID) { 3039 Profile(ID, getKeyword(), NNS, NamedType); 3040 } 3041 3042 static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, 3043 NestedNameSpecifier *NNS, QualType NamedType) { 3044 ID.AddInteger(Keyword); 3045 ID.AddPointer(NNS); 3046 NamedType.Profile(ID); 3047 } 3048 3049 static bool classof(const Type *T) { 3050 return T->getTypeClass() == Elaborated; 3051 } 3052 static bool classof(const ElaboratedType *T) { return true; } 3053}; 3054 3055/// \brief Represents a qualified type name for which the type name is 3056/// dependent. 3057/// 3058/// DependentNameType represents a class of dependent types that involve a 3059/// dependent nested-name-specifier (e.g., "T::") followed by a (dependent) 3060/// name of a type. The DependentNameType may start with a "typename" (for a 3061/// typename-specifier), "class", "struct", "union", or "enum" (for a 3062/// dependent elaborated-type-specifier), or nothing (in contexts where we 3063/// know that we must be referring to a type, e.g., in a base class specifier). 3064class DependentNameType : public TypeWithKeyword, public llvm::FoldingSetNode { 3065 3066 /// \brief The nested name specifier containing the qualifier. 3067 NestedNameSpecifier *NNS; 3068 3069 /// \brief The type that this typename specifier refers to. 3070 const IdentifierInfo *Name; 3071 3072 DependentNameType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, 3073 const IdentifierInfo *Name, QualType CanonType) 3074 : TypeWithKeyword(Keyword, DependentName, CanonType, /*Dependent=*/true, 3075 /*VariablyModified=*/false, 3076 NNS->containsUnexpandedParameterPack()), 3077 NNS(NNS), Name(Name) { 3078 assert(NNS->isDependent() && 3079 "DependentNameType requires a dependent nested-name-specifier"); 3080 } 3081 3082 friend class ASTContext; // ASTContext creates these 3083 3084public: 3085 /// \brief Retrieve the qualification on this type. 3086 NestedNameSpecifier *getQualifier() const { return NNS; } 3087 3088 /// \brief Retrieve the type named by the typename specifier as an 3089 /// identifier. 3090 /// 3091 /// This routine will return a non-NULL identifier pointer when the 3092 /// form of the original typename was terminated by an identifier, 3093 /// e.g., "typename T::type". 3094 const IdentifierInfo *getIdentifier() const { 3095 return Name; 3096 } 3097 3098 bool isSugared() const { return false; } 3099 QualType desugar() const { return QualType(this, 0); } 3100 3101 void Profile(llvm::FoldingSetNodeID &ID) { 3102 Profile(ID, getKeyword(), NNS, Name); 3103 } 3104 3105 static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, 3106 NestedNameSpecifier *NNS, const IdentifierInfo *Name) { 3107 ID.AddInteger(Keyword); 3108 ID.AddPointer(NNS); 3109 ID.AddPointer(Name); 3110 } 3111 3112 static bool classof(const Type *T) { 3113 return T->getTypeClass() == DependentName; 3114 } 3115 static bool classof(const DependentNameType *T) { return true; } 3116}; 3117 3118/// DependentTemplateSpecializationType - Represents a template 3119/// specialization type whose template cannot be resolved, e.g. 3120/// A<T>::template B<T> 3121class DependentTemplateSpecializationType : 3122 public TypeWithKeyword, public llvm::FoldingSetNode { 3123 3124 /// \brief The nested name specifier containing the qualifier. 3125 NestedNameSpecifier *NNS; 3126 3127 /// \brief The identifier of the template. 3128 const IdentifierInfo *Name; 3129 3130 /// \brief - The number of template arguments named in this class 3131 /// template specialization. 3132 unsigned NumArgs; 3133 3134 const TemplateArgument *getArgBuffer() const { 3135 return reinterpret_cast<const TemplateArgument*>(this+1); 3136 } 3137 TemplateArgument *getArgBuffer() { 3138 return reinterpret_cast<TemplateArgument*>(this+1); 3139 } 3140 3141 DependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, 3142 NestedNameSpecifier *NNS, 3143 const IdentifierInfo *Name, 3144 unsigned NumArgs, 3145 const TemplateArgument *Args, 3146 QualType Canon); 3147 3148 friend class ASTContext; // ASTContext creates these 3149 3150public: 3151 NestedNameSpecifier *getQualifier() const { return NNS; } 3152 const IdentifierInfo *getIdentifier() const { return Name; } 3153 3154 /// \brief Retrieve the template arguments. 3155 const TemplateArgument *getArgs() const { 3156 return getArgBuffer(); 3157 } 3158 3159 /// \brief Retrieve the number of template arguments. 3160 unsigned getNumArgs() const { return NumArgs; } 3161 3162 const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h 3163 3164 typedef const TemplateArgument * iterator; 3165 iterator begin() const { return getArgs(); } 3166 iterator end() const; // inline in TemplateBase.h 3167 3168 bool isSugared() const { return false; } 3169 QualType desugar() const { return QualType(this, 0); } 3170 3171 void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context) { 3172 Profile(ID, Context, getKeyword(), NNS, Name, NumArgs, getArgs()); 3173 } 3174 3175 static void Profile(llvm::FoldingSetNodeID &ID, 3176 ASTContext &Context, 3177 ElaboratedTypeKeyword Keyword, 3178 NestedNameSpecifier *Qualifier, 3179 const IdentifierInfo *Name, 3180 unsigned NumArgs, 3181 const TemplateArgument *Args); 3182 3183 static bool classof(const Type *T) { 3184 return T->getTypeClass() == DependentTemplateSpecialization; 3185 } 3186 static bool classof(const DependentTemplateSpecializationType *T) { 3187 return true; 3188 } 3189}; 3190 3191/// \brief Represents a pack expansion of types. 3192/// 3193/// Pack expansions are part of C++0x variadic templates. A pack 3194/// expansion contains a pattern, which itself contains one or more 3195/// "unexpanded" parameter packs. When instantiated, a pack expansion 3196/// produces a series of types, each instantiated from the pattern of 3197/// the expansion, where the Ith instantiation of the pattern uses the 3198/// Ith arguments bound to each of the unexpanded parameter packs. The 3199/// pack expansion is considered to "expand" these unexpanded 3200/// parameter packs. 3201/// 3202/// \code 3203/// template<typename ...Types> struct tuple; 3204/// 3205/// template<typename ...Types> 3206/// struct tuple_of_references { 3207/// typedef tuple<Types&...> type; 3208/// }; 3209/// \endcode 3210/// 3211/// Here, the pack expansion \c Types&... is represented via a 3212/// PackExpansionType whose pattern is Types&. 3213class PackExpansionType : public Type, public llvm::FoldingSetNode { 3214 /// \brief The pattern of the pack expansion. 3215 QualType Pattern; 3216 3217 PackExpansionType(QualType Pattern, QualType Canon) 3218 : Type(PackExpansion, Canon, /*Dependent=*/true, 3219 /*VariableModified=*/Pattern->isVariablyModifiedType(), 3220 /*ContainsUnexpandedParameterPack=*/false), 3221 Pattern(Pattern) { } 3222 3223 friend class ASTContext; // ASTContext creates these 3224 3225public: 3226 /// \brief Retrieve the pattern of this pack expansion, which is the 3227 /// type that will be repeatedly instantiated when instantiating the 3228 /// pack expansion itself. 3229 QualType getPattern() const { return Pattern; } 3230 3231 bool isSugared() const { return false; } 3232 QualType desugar() const { return QualType(this, 0); } 3233 3234 void Profile(llvm::FoldingSetNodeID &ID) { 3235 Profile(ID, getPattern()); 3236 } 3237 3238 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pattern) { 3239 ID.AddPointer(Pattern.getAsOpaquePtr()); 3240 } 3241 3242 static bool classof(const Type *T) { 3243 return T->getTypeClass() == PackExpansion; 3244 } 3245 static bool classof(const PackExpansionType *T) { 3246 return true; 3247 } 3248}; 3249 3250/// ObjCObjectType - Represents a class type in Objective C. 3251/// Every Objective C type is a combination of a base type and a 3252/// list of protocols. 3253/// 3254/// Given the following declarations: 3255/// @class C; 3256/// @protocol P; 3257/// 3258/// 'C' is an ObjCInterfaceType C. It is sugar for an ObjCObjectType 3259/// with base C and no protocols. 3260/// 3261/// 'C<P>' is an ObjCObjectType with base C and protocol list [P]. 3262/// 3263/// 'id' is a TypedefType which is sugar for an ObjCPointerType whose 3264/// pointee is an ObjCObjectType with base BuiltinType::ObjCIdType 3265/// and no protocols. 3266/// 3267/// 'id<P>' is an ObjCPointerType whose pointee is an ObjCObjecType 3268/// with base BuiltinType::ObjCIdType and protocol list [P]. Eventually 3269/// this should get its own sugar class to better represent the source. 3270class ObjCObjectType : public Type { 3271 // ObjCObjectType.NumProtocols - the number of protocols stored 3272 // after the ObjCObjectPointerType node. 3273 // 3274 // These protocols are those written directly on the type. If 3275 // protocol qualifiers ever become additive, the iterators will need 3276 // to get kindof complicated. 3277 // 3278 // In the canonical object type, these are sorted alphabetically 3279 // and uniqued. 3280 3281 /// Either a BuiltinType or an InterfaceType or sugar for either. 3282 QualType BaseType; 3283 3284 ObjCProtocolDecl * const *getProtocolStorage() const { 3285 return const_cast<ObjCObjectType*>(this)->getProtocolStorage(); 3286 } 3287 3288 ObjCProtocolDecl **getProtocolStorage(); 3289 3290protected: 3291 ObjCObjectType(QualType Canonical, QualType Base, 3292 ObjCProtocolDecl * const *Protocols, unsigned NumProtocols); 3293 3294 enum Nonce_ObjCInterface { Nonce_ObjCInterface }; 3295 ObjCObjectType(enum Nonce_ObjCInterface) 3296 : Type(ObjCInterface, QualType(), false, false, false), 3297 BaseType(QualType(this_(), 0)) { 3298 ObjCObjectTypeBits.NumProtocols = 0; 3299 } 3300 3301public: 3302 /// getBaseType - Gets the base type of this object type. This is 3303 /// always (possibly sugar for) one of: 3304 /// - the 'id' builtin type (as opposed to the 'id' type visible to the 3305 /// user, which is a typedef for an ObjCPointerType) 3306 /// - the 'Class' builtin type (same caveat) 3307 /// - an ObjCObjectType (currently always an ObjCInterfaceType) 3308 QualType getBaseType() const { return BaseType; } 3309 3310 bool isObjCId() const { 3311 return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCId); 3312 } 3313 bool isObjCClass() const { 3314 return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCClass); 3315 } 3316 bool isObjCUnqualifiedId() const { return qual_empty() && isObjCId(); } 3317 bool isObjCUnqualifiedClass() const { return qual_empty() && isObjCClass(); } 3318 bool isObjCUnqualifiedIdOrClass() const { 3319 if (!qual_empty()) return false; 3320 if (const BuiltinType *T = getBaseType()->getAs<BuiltinType>()) 3321 return T->getKind() == BuiltinType::ObjCId || 3322 T->getKind() == BuiltinType::ObjCClass; 3323 return false; 3324 } 3325 bool isObjCQualifiedId() const { return !qual_empty() && isObjCId(); } 3326 bool isObjCQualifiedClass() const { return !qual_empty() && isObjCClass(); } 3327 3328 /// Gets the interface declaration for this object type, if the base type 3329 /// really is an interface. 3330 ObjCInterfaceDecl *getInterface() const; 3331 3332 typedef ObjCProtocolDecl * const *qual_iterator; 3333 3334 qual_iterator qual_begin() const { return getProtocolStorage(); } 3335 qual_iterator qual_end() const { return qual_begin() + getNumProtocols(); } 3336 3337 bool qual_empty() const { return getNumProtocols() == 0; } 3338 3339 /// getNumProtocols - Return the number of qualifying protocols in this 3340 /// interface type, or 0 if there are none. 3341 unsigned getNumProtocols() const { return ObjCObjectTypeBits.NumProtocols; } 3342 3343 /// \brief Fetch a protocol by index. 3344 ObjCProtocolDecl *getProtocol(unsigned I) const { 3345 assert(I < getNumProtocols() && "Out-of-range protocol access"); 3346 return qual_begin()[I]; 3347 } 3348 3349 bool isSugared() const { return false; } 3350 QualType desugar() const { return QualType(this, 0); } 3351 3352 static bool classof(const Type *T) { 3353 return T->getTypeClass() == ObjCObject || 3354 T->getTypeClass() == ObjCInterface; 3355 } 3356 static bool classof(const ObjCObjectType *) { return true; } 3357}; 3358 3359/// ObjCObjectTypeImpl - A class providing a concrete implementation 3360/// of ObjCObjectType, so as to not increase the footprint of 3361/// ObjCInterfaceType. Code outside of ASTContext and the core type 3362/// system should not reference this type. 3363class ObjCObjectTypeImpl : public ObjCObjectType, public llvm::FoldingSetNode { 3364 friend class ASTContext; 3365 3366 // If anyone adds fields here, ObjCObjectType::getProtocolStorage() 3367 // will need to be modified. 3368 3369 ObjCObjectTypeImpl(QualType Canonical, QualType Base, 3370 ObjCProtocolDecl * const *Protocols, 3371 unsigned NumProtocols) 3372 : ObjCObjectType(Canonical, Base, Protocols, NumProtocols) {} 3373 3374public: 3375 void Profile(llvm::FoldingSetNodeID &ID); 3376 static void Profile(llvm::FoldingSetNodeID &ID, 3377 QualType Base, 3378 ObjCProtocolDecl *const *protocols, 3379 unsigned NumProtocols); 3380}; 3381 3382inline ObjCProtocolDecl **ObjCObjectType::getProtocolStorage() { 3383 return reinterpret_cast<ObjCProtocolDecl**>( 3384 static_cast<ObjCObjectTypeImpl*>(this) + 1); 3385} 3386 3387/// ObjCInterfaceType - Interfaces are the core concept in Objective-C for 3388/// object oriented design. They basically correspond to C++ classes. There 3389/// are two kinds of interface types, normal interfaces like "NSString" and 3390/// qualified interfaces, which are qualified with a protocol list like 3391/// "NSString<NSCopyable, NSAmazing>". 3392/// 3393/// ObjCInterfaceType guarantees the following properties when considered 3394/// as a subtype of its superclass, ObjCObjectType: 3395/// - There are no protocol qualifiers. To reinforce this, code which 3396/// tries to invoke the protocol methods via an ObjCInterfaceType will 3397/// fail to compile. 3398/// - It is its own base type. That is, if T is an ObjCInterfaceType*, 3399/// T->getBaseType() == QualType(T, 0). 3400class ObjCInterfaceType : public ObjCObjectType { 3401 ObjCInterfaceDecl *Decl; 3402 3403 ObjCInterfaceType(const ObjCInterfaceDecl *D) 3404 : ObjCObjectType(Nonce_ObjCInterface), 3405 Decl(const_cast<ObjCInterfaceDecl*>(D)) {} 3406 friend class ASTContext; // ASTContext creates these. 3407 3408public: 3409 /// getDecl - Get the declaration of this interface. 3410 ObjCInterfaceDecl *getDecl() const { return Decl; } 3411 3412 bool isSugared() const { return false; } 3413 QualType desugar() const { return QualType(this, 0); } 3414 3415 static bool classof(const Type *T) { 3416 return T->getTypeClass() == ObjCInterface; 3417 } 3418 static bool classof(const ObjCInterfaceType *) { return true; } 3419 3420 // Nonsense to "hide" certain members of ObjCObjectType within this 3421 // class. People asking for protocols on an ObjCInterfaceType are 3422 // not going to get what they want: ObjCInterfaceTypes are 3423 // guaranteed to have no protocols. 3424 enum { 3425 qual_iterator, 3426 qual_begin, 3427 qual_end, 3428 getNumProtocols, 3429 getProtocol 3430 }; 3431}; 3432 3433inline ObjCInterfaceDecl *ObjCObjectType::getInterface() const { 3434 if (const ObjCInterfaceType *T = 3435 getBaseType()->getAs<ObjCInterfaceType>()) 3436 return T->getDecl(); 3437 return 0; 3438} 3439 3440/// ObjCObjectPointerType - Used to represent a pointer to an 3441/// Objective C object. These are constructed from pointer 3442/// declarators when the pointee type is an ObjCObjectType (or sugar 3443/// for one). In addition, the 'id' and 'Class' types are typedefs 3444/// for these, and the protocol-qualified types 'id<P>' and 'Class<P>' 3445/// are translated into these. 3446/// 3447/// Pointers to pointers to Objective C objects are still PointerTypes; 3448/// only the first level of pointer gets it own type implementation. 3449class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode { 3450 QualType PointeeType; 3451 3452 ObjCObjectPointerType(QualType Canonical, QualType Pointee) 3453 : Type(ObjCObjectPointer, Canonical, false, false, false), 3454 PointeeType(Pointee) {} 3455 friend class ASTContext; // ASTContext creates these. 3456 3457public: 3458 /// getPointeeType - Gets the type pointed to by this ObjC pointer. 3459 /// The result will always be an ObjCObjectType or sugar thereof. 3460 QualType getPointeeType() const { return PointeeType; } 3461 3462 /// getObjCObjectType - Gets the type pointed to by this ObjC 3463 /// pointer. This method always returns non-null. 3464 /// 3465 /// This method is equivalent to getPointeeType() except that 3466 /// it discards any typedefs (or other sugar) between this 3467 /// type and the "outermost" object type. So for: 3468 /// @class A; @protocol P; @protocol Q; 3469 /// typedef A<P> AP; 3470 /// typedef A A1; 3471 /// typedef A1<P> A1P; 3472 /// typedef A1P<Q> A1PQ; 3473 /// For 'A*', getObjectType() will return 'A'. 3474 /// For 'A<P>*', getObjectType() will return 'A<P>'. 3475 /// For 'AP*', getObjectType() will return 'A<P>'. 3476 /// For 'A1*', getObjectType() will return 'A'. 3477 /// For 'A1<P>*', getObjectType() will return 'A1<P>'. 3478 /// For 'A1P*', getObjectType() will return 'A1<P>'. 3479 /// For 'A1PQ*', getObjectType() will return 'A1<Q>', because 3480 /// adding protocols to a protocol-qualified base discards the 3481 /// old qualifiers (for now). But if it didn't, getObjectType() 3482 /// would return 'A1P<Q>' (and we'd have to make iterating over 3483 /// qualifiers more complicated). 3484 const ObjCObjectType *getObjectType() const { 3485 return PointeeType->getAs<ObjCObjectType>(); 3486 } 3487 3488 /// getInterfaceType - If this pointer points to an Objective C 3489 /// @interface type, gets the type for that interface. Any protocol 3490 /// qualifiers on the interface are ignored. 3491 /// 3492 /// \return null if the base type for this pointer is 'id' or 'Class' 3493 const ObjCInterfaceType *getInterfaceType() const { 3494 return getObjectType()->getBaseType()->getAs<ObjCInterfaceType>(); 3495 } 3496 3497 /// getInterfaceDecl - If this pointer points to an Objective @interface 3498 /// type, gets the declaration for that interface. 3499 /// 3500 /// \return null if the base type for this pointer is 'id' or 'Class' 3501 ObjCInterfaceDecl *getInterfaceDecl() const { 3502 return getObjectType()->getInterface(); 3503 } 3504 3505 /// isObjCIdType - True if this is equivalent to the 'id' type, i.e. if 3506 /// its object type is the primitive 'id' type with no protocols. 3507 bool isObjCIdType() const { 3508 return getObjectType()->isObjCUnqualifiedId(); 3509 } 3510 3511 /// isObjCClassType - True if this is equivalent to the 'Class' type, 3512 /// i.e. if its object tive is the primitive 'Class' type with no protocols. 3513 bool isObjCClassType() const { 3514 return getObjectType()->isObjCUnqualifiedClass(); 3515 } 3516 3517 /// isObjCQualifiedIdType - True if this is equivalent to 'id<P>' for some 3518 /// non-empty set of protocols. 3519 bool isObjCQualifiedIdType() const { 3520 return getObjectType()->isObjCQualifiedId(); 3521 } 3522 3523 /// isObjCQualifiedClassType - True if this is equivalent to 'Class<P>' for 3524 /// some non-empty set of protocols. 3525 bool isObjCQualifiedClassType() const { 3526 return getObjectType()->isObjCQualifiedClass(); 3527 } 3528 3529 /// An iterator over the qualifiers on the object type. Provided 3530 /// for convenience. This will always iterate over the full set of 3531 /// protocols on a type, not just those provided directly. 3532 typedef ObjCObjectType::qual_iterator qual_iterator; 3533 3534 qual_iterator qual_begin() const { 3535 return getObjectType()->qual_begin(); 3536 } 3537 qual_iterator qual_end() const { 3538 return getObjectType()->qual_end(); 3539 } 3540 bool qual_empty() const { return getObjectType()->qual_empty(); } 3541 3542 /// getNumProtocols - Return the number of qualifying protocols on 3543 /// the object type. 3544 unsigned getNumProtocols() const { 3545 return getObjectType()->getNumProtocols(); 3546 } 3547 3548 /// \brief Retrieve a qualifying protocol by index on the object 3549 /// type. 3550 ObjCProtocolDecl *getProtocol(unsigned I) const { 3551 return getObjectType()->getProtocol(I); 3552 } 3553 3554 bool isSugared() const { return false; } 3555 QualType desugar() const { return QualType(this, 0); } 3556 3557 void Profile(llvm::FoldingSetNodeID &ID) { 3558 Profile(ID, getPointeeType()); 3559 } 3560 static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { 3561 ID.AddPointer(T.getAsOpaquePtr()); 3562 } 3563 static bool classof(const Type *T) { 3564 return T->getTypeClass() == ObjCObjectPointer; 3565 } 3566 static bool classof(const ObjCObjectPointerType *) { return true; } 3567}; 3568 3569/// A qualifier set is used to build a set of qualifiers. 3570class QualifierCollector : public Qualifiers { 3571public: 3572 QualifierCollector(Qualifiers Qs = Qualifiers()) : Qualifiers(Qs) {} 3573 3574 /// Collect any qualifiers on the given type and return an 3575 /// unqualified type. 3576 const Type *strip(QualType QT) { 3577 addFastQualifiers(QT.getLocalFastQualifiers()); 3578 if (QT.hasLocalNonFastQualifiers()) { 3579 const ExtQuals *EQ = QT.getExtQualsUnsafe(); 3580 addQualifiers(EQ->getQualifiers()); 3581 return EQ->getBaseType(); 3582 } 3583 return QT.getTypePtrUnsafe(); 3584 } 3585 3586 /// Apply the collected qualifiers to the given type. 3587 QualType apply(ASTContext &Context, QualType QT) const; 3588 3589 /// Apply the collected qualifiers to the given type. 3590 QualType apply(ASTContext &Context, const Type* T) const; 3591}; 3592 3593 3594// Inline function definitions. 3595 3596inline bool QualType::isCanonical() const { 3597 const Type *T = getTypePtr(); 3598 if (hasLocalQualifiers()) 3599 return T->isCanonicalUnqualified() && !isa<ArrayType>(T); 3600 return T->isCanonicalUnqualified(); 3601} 3602 3603inline bool QualType::isCanonicalAsParam() const { 3604 if (hasLocalQualifiers()) return false; 3605 3606 const Type *T = getTypePtr(); 3607 if ((*this)->isPointerType()) { 3608 QualType BaseType = (*this)->getAs<PointerType>()->getPointeeType(); 3609 if (isa<VariableArrayType>(BaseType)) { 3610 ArrayType *AT = dyn_cast<ArrayType>(BaseType); 3611 VariableArrayType *VAT = cast<VariableArrayType>(AT); 3612 if (VAT->getSizeExpr()) 3613 T = BaseType.getTypePtr(); 3614 } 3615 } 3616 return T->isCanonicalUnqualified() && 3617 !isa<FunctionType>(T) && !isa<ArrayType>(T); 3618} 3619 3620inline bool QualType::isConstQualified() const { 3621 return isLocalConstQualified() || 3622 getTypePtr()->getCanonicalTypeInternal().isLocalConstQualified(); 3623} 3624 3625inline bool QualType::isRestrictQualified() const { 3626 return isLocalRestrictQualified() || 3627 getTypePtr()->getCanonicalTypeInternal().isLocalRestrictQualified(); 3628} 3629 3630 3631inline bool QualType::isVolatileQualified() const { 3632 return isLocalVolatileQualified() || 3633 getTypePtr()->getCanonicalTypeInternal().isLocalVolatileQualified(); 3634} 3635 3636inline bool QualType::hasQualifiers() const { 3637 return hasLocalQualifiers() || 3638 getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers(); 3639} 3640 3641inline Qualifiers QualType::getQualifiers() const { 3642 Qualifiers Quals = getLocalQualifiers(); 3643 Quals.addQualifiers( 3644 getTypePtr()->getCanonicalTypeInternal().getLocalQualifiers()); 3645 return Quals; 3646} 3647 3648inline unsigned QualType::getCVRQualifiers() const { 3649 return getLocalCVRQualifiers() | 3650 getTypePtr()->getCanonicalTypeInternal().getLocalCVRQualifiers(); 3651} 3652 3653/// getCVRQualifiersThroughArrayTypes - If there are CVR qualifiers for this 3654/// type, returns them. Otherwise, if this is an array type, recurses 3655/// on the element type until some qualifiers have been found or a non-array 3656/// type reached. 3657inline unsigned QualType::getCVRQualifiersThroughArrayTypes() const { 3658 if (unsigned Quals = getCVRQualifiers()) 3659 return Quals; 3660 QualType CT = getTypePtr()->getCanonicalTypeInternal(); 3661 if (const ArrayType *AT = dyn_cast<ArrayType>(CT)) 3662 return AT->getElementType().getCVRQualifiersThroughArrayTypes(); 3663 return 0; 3664} 3665 3666inline void QualType::removeLocalConst() { 3667 removeLocalFastQualifiers(Qualifiers::Const); 3668} 3669 3670inline void QualType::removeLocalRestrict() { 3671 removeLocalFastQualifiers(Qualifiers::Restrict); 3672} 3673 3674inline void QualType::removeLocalVolatile() { 3675 removeLocalFastQualifiers(Qualifiers::Volatile); 3676} 3677 3678inline void QualType::removeLocalCVRQualifiers(unsigned Mask) { 3679 assert(!(Mask & ~Qualifiers::CVRMask) && "mask has non-CVR bits"); 3680 assert((int)Qualifiers::CVRMask == (int)Qualifiers::FastMask); 3681 3682 // Fast path: we don't need to touch the slow qualifiers. 3683 removeLocalFastQualifiers(Mask); 3684} 3685 3686/// getAddressSpace - Return the address space of this type. 3687inline unsigned QualType::getAddressSpace() const { 3688 if (hasLocalNonFastQualifiers()) { 3689 const ExtQuals *EQ = getExtQualsUnsafe(); 3690 if (EQ->hasAddressSpace()) 3691 return EQ->getAddressSpace(); 3692 } 3693 3694 QualType CT = getTypePtr()->getCanonicalTypeInternal(); 3695 if (CT.hasLocalNonFastQualifiers()) { 3696 const ExtQuals *EQ = CT.getExtQualsUnsafe(); 3697 if (EQ->hasAddressSpace()) 3698 return EQ->getAddressSpace(); 3699 } 3700 3701 if (const ArrayType *AT = dyn_cast<ArrayType>(CT)) 3702 return AT->getElementType().getAddressSpace(); 3703 return 0; 3704} 3705 3706/// getObjCGCAttr - Return the gc attribute of this type. 3707inline Qualifiers::GC QualType::getObjCGCAttr() const { 3708 if (hasLocalNonFastQualifiers()) { 3709 const ExtQuals *EQ = getExtQualsUnsafe(); 3710 if (EQ->hasObjCGCAttr()) 3711 return EQ->getObjCGCAttr(); 3712 } 3713 3714 QualType CT = getTypePtr()->getCanonicalTypeInternal(); 3715 if (CT.hasLocalNonFastQualifiers()) { 3716 const ExtQuals *EQ = CT.getExtQualsUnsafe(); 3717 if (EQ->hasObjCGCAttr()) 3718 return EQ->getObjCGCAttr(); 3719 } 3720 3721 if (const ArrayType *AT = dyn_cast<ArrayType>(CT)) 3722 return AT->getElementType().getObjCGCAttr(); 3723 if (const ObjCObjectPointerType *PT = CT->getAs<ObjCObjectPointerType>()) 3724 return PT->getPointeeType().getObjCGCAttr(); 3725 // We most look at all pointer types, not just pointer to interface types. 3726 if (const PointerType *PT = CT->getAs<PointerType>()) 3727 return PT->getPointeeType().getObjCGCAttr(); 3728 return Qualifiers::GCNone; 3729} 3730 3731inline FunctionType::ExtInfo getFunctionExtInfo(const Type &t) { 3732 if (const PointerType *PT = t.getAs<PointerType>()) { 3733 if (const FunctionType *FT = PT->getPointeeType()->getAs<FunctionType>()) 3734 return FT->getExtInfo(); 3735 } else if (const FunctionType *FT = t.getAs<FunctionType>()) 3736 return FT->getExtInfo(); 3737 3738 return FunctionType::ExtInfo(); 3739} 3740 3741inline FunctionType::ExtInfo getFunctionExtInfo(QualType t) { 3742 return getFunctionExtInfo(*t); 3743} 3744 3745/// \brief Determine whether this set of qualifiers is a superset of the given 3746/// set of qualifiers. 3747inline bool Qualifiers::isSupersetOf(Qualifiers Other) const { 3748 return Mask != Other.Mask && (Mask | Other.Mask) == Mask; 3749} 3750 3751/// isMoreQualifiedThan - Determine whether this type is more 3752/// qualified than the Other type. For example, "const volatile int" 3753/// is more qualified than "const int", "volatile int", and 3754/// "int". However, it is not more qualified than "const volatile 3755/// int". 3756inline bool QualType::isMoreQualifiedThan(QualType Other) const { 3757 // FIXME: work on arbitrary qualifiers 3758 unsigned MyQuals = this->getCVRQualifiersThroughArrayTypes(); 3759 unsigned OtherQuals = Other.getCVRQualifiersThroughArrayTypes(); 3760 if (getAddressSpace() != Other.getAddressSpace()) 3761 return false; 3762 return MyQuals != OtherQuals && (MyQuals | OtherQuals) == MyQuals; 3763} 3764 3765/// isAtLeastAsQualifiedAs - Determine whether this type is at last 3766/// as qualified as the Other type. For example, "const volatile 3767/// int" is at least as qualified as "const int", "volatile int", 3768/// "int", and "const volatile int". 3769inline bool QualType::isAtLeastAsQualifiedAs(QualType Other) const { 3770 // FIXME: work on arbitrary qualifiers 3771 unsigned MyQuals = this->getCVRQualifiersThroughArrayTypes(); 3772 unsigned OtherQuals = Other.getCVRQualifiersThroughArrayTypes(); 3773 if (getAddressSpace() != Other.getAddressSpace()) 3774 return false; 3775 return (MyQuals | OtherQuals) == MyQuals; 3776} 3777 3778/// getNonReferenceType - If Type is a reference type (e.g., const 3779/// int&), returns the type that the reference refers to ("const 3780/// int"). Otherwise, returns the type itself. This routine is used 3781/// throughout Sema to implement C++ 5p6: 3782/// 3783/// If an expression initially has the type "reference to T" (8.3.2, 3784/// 8.5.3), the type is adjusted to "T" prior to any further 3785/// analysis, the expression designates the object or function 3786/// denoted by the reference, and the expression is an lvalue. 3787inline QualType QualType::getNonReferenceType() const { 3788 if (const ReferenceType *RefType = (*this)->getAs<ReferenceType>()) 3789 return RefType->getPointeeType(); 3790 else 3791 return *this; 3792} 3793 3794inline bool Type::isFunctionType() const { 3795 return isa<FunctionType>(CanonicalType); 3796} 3797inline bool Type::isPointerType() const { 3798 return isa<PointerType>(CanonicalType); 3799} 3800inline bool Type::isAnyPointerType() const { 3801 return isPointerType() || isObjCObjectPointerType(); 3802} 3803inline bool Type::isBlockPointerType() const { 3804 return isa<BlockPointerType>(CanonicalType); 3805} 3806inline bool Type::isReferenceType() const { 3807 return isa<ReferenceType>(CanonicalType); 3808} 3809inline bool Type::isLValueReferenceType() const { 3810 return isa<LValueReferenceType>(CanonicalType); 3811} 3812inline bool Type::isRValueReferenceType() const { 3813 return isa<RValueReferenceType>(CanonicalType); 3814} 3815inline bool Type::isFunctionPointerType() const { 3816 if (const PointerType* T = getAs<PointerType>()) 3817 return T->getPointeeType()->isFunctionType(); 3818 else 3819 return false; 3820} 3821inline bool Type::isMemberPointerType() const { 3822 return isa<MemberPointerType>(CanonicalType); 3823} 3824inline bool Type::isMemberFunctionPointerType() const { 3825 if (const MemberPointerType* T = getAs<MemberPointerType>()) 3826 return T->isMemberFunctionPointer(); 3827 else 3828 return false; 3829} 3830inline bool Type::isMemberDataPointerType() const { 3831 if (const MemberPointerType* T = getAs<MemberPointerType>()) 3832 return T->isMemberDataPointer(); 3833 else 3834 return false; 3835} 3836inline bool Type::isArrayType() const { 3837 return isa<ArrayType>(CanonicalType); 3838} 3839inline bool Type::isConstantArrayType() const { 3840 return isa<ConstantArrayType>(CanonicalType); 3841} 3842inline bool Type::isIncompleteArrayType() const { 3843 return isa<IncompleteArrayType>(CanonicalType); 3844} 3845inline bool Type::isVariableArrayType() const { 3846 return isa<VariableArrayType>(CanonicalType); 3847} 3848inline bool Type::isDependentSizedArrayType() const { 3849 return isa<DependentSizedArrayType>(CanonicalType); 3850} 3851inline bool Type::isBuiltinType() const { 3852 return isa<BuiltinType>(CanonicalType); 3853} 3854inline bool Type::isRecordType() const { 3855 return isa<RecordType>(CanonicalType); 3856} 3857inline bool Type::isEnumeralType() const { 3858 return isa<EnumType>(CanonicalType); 3859} 3860inline bool Type::isAnyComplexType() const { 3861 return isa<ComplexType>(CanonicalType); 3862} 3863inline bool Type::isVectorType() const { 3864 return isa<VectorType>(CanonicalType); 3865} 3866inline bool Type::isExtVectorType() const { 3867 return isa<ExtVectorType>(CanonicalType); 3868} 3869inline bool Type::isObjCObjectPointerType() const { 3870 return isa<ObjCObjectPointerType>(CanonicalType); 3871} 3872inline bool Type::isObjCObjectType() const { 3873 return isa<ObjCObjectType>(CanonicalType); 3874} 3875inline bool Type::isObjCObjectOrInterfaceType() const { 3876 return isa<ObjCInterfaceType>(CanonicalType) || 3877 isa<ObjCObjectType>(CanonicalType); 3878} 3879 3880inline bool Type::isObjCQualifiedIdType() const { 3881 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 3882 return OPT->isObjCQualifiedIdType(); 3883 return false; 3884} 3885inline bool Type::isObjCQualifiedClassType() const { 3886 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 3887 return OPT->isObjCQualifiedClassType(); 3888 return false; 3889} 3890inline bool Type::isObjCIdType() const { 3891 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 3892 return OPT->isObjCIdType(); 3893 return false; 3894} 3895inline bool Type::isObjCClassType() const { 3896 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 3897 return OPT->isObjCClassType(); 3898 return false; 3899} 3900inline bool Type::isObjCSelType() const { 3901 if (const PointerType *OPT = getAs<PointerType>()) 3902 return OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCSel); 3903 return false; 3904} 3905inline bool Type::isObjCBuiltinType() const { 3906 return isObjCIdType() || isObjCClassType() || isObjCSelType(); 3907} 3908inline bool Type::isTemplateTypeParmType() const { 3909 return isa<TemplateTypeParmType>(CanonicalType); 3910} 3911 3912inline bool Type::isSpecificBuiltinType(unsigned K) const { 3913 if (const BuiltinType *BT = getAs<BuiltinType>()) 3914 if (BT->getKind() == (BuiltinType::Kind) K) 3915 return true; 3916 return false; 3917} 3918 3919inline bool Type::isPlaceholderType() const { 3920 if (const BuiltinType *BT = getAs<BuiltinType>()) 3921 return BT->isPlaceholderType(); 3922 return false; 3923} 3924 3925/// \brief Determines whether this is a type for which one can define 3926/// an overloaded operator. 3927inline bool Type::isOverloadableType() const { 3928 return isDependentType() || isRecordType() || isEnumeralType(); 3929} 3930 3931inline bool Type::hasPointerRepresentation() const { 3932 return (isPointerType() || isReferenceType() || isBlockPointerType() || 3933 isObjCObjectPointerType() || isNullPtrType()); 3934} 3935 3936inline bool Type::hasObjCPointerRepresentation() const { 3937 return isObjCObjectPointerType(); 3938} 3939 3940/// Insertion operator for diagnostics. This allows sending QualType's into a 3941/// diagnostic with <<. 3942inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 3943 QualType T) { 3944 DB.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), 3945 Diagnostic::ak_qualtype); 3946 return DB; 3947} 3948 3949/// Insertion operator for partial diagnostics. This allows sending QualType's 3950/// into a diagnostic with <<. 3951inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD, 3952 QualType T) { 3953 PD.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), 3954 Diagnostic::ak_qualtype); 3955 return PD; 3956} 3957 3958// Helper class template that is used by Type::getAs to ensure that one does 3959// not try to look through a qualified type to get to an array type. 3960template<typename T, 3961 bool isArrayType = (llvm::is_same<T, ArrayType>::value || 3962 llvm::is_base_of<ArrayType, T>::value)> 3963struct ArrayType_cannot_be_used_with_getAs { }; 3964 3965template<typename T> 3966struct ArrayType_cannot_be_used_with_getAs<T, true>; 3967 3968/// Member-template getAs<specific type>'. 3969template <typename T> const T *Type::getAs() const { 3970 ArrayType_cannot_be_used_with_getAs<T> at; 3971 (void)at; 3972 3973 // If this is directly a T type, return it. 3974 if (const T *Ty = dyn_cast<T>(this)) 3975 return Ty; 3976 3977 // If the canonical form of this type isn't the right kind, reject it. 3978 if (!isa<T>(CanonicalType)) 3979 return 0; 3980 3981 // If this is a typedef for the type, strip the typedef off without 3982 // losing all typedef information. 3983 return cast<T>(getUnqualifiedDesugaredType()); 3984} 3985 3986} // end namespace clang 3987 3988#endif 3989