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