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