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