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