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