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