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