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