Type.h revision fc8f0e14ad142ed811e90fbd9a30e419e301c717
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 TypedefDecl; 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 /// Helper methods to distinguish type categories. All type predicates 1175 /// operate on the canonical type, ignoring typedefs and qualifiers. 1176 1177 /// isBuiltinType - returns true if the type is a builtin type. 1178 bool isBuiltinType() const; 1179 1180 /// isSpecificBuiltinType - Test for a particular builtin type. 1181 bool isSpecificBuiltinType(unsigned K) const; 1182 1183 /// isPlaceholderType - Test for a type which does not represent an 1184 /// actual type-system type but is instead used as a placeholder for 1185 /// various convenient purposes within Clang. All such types are 1186 /// BuiltinTypes. 1187 bool isPlaceholderType() const; 1188 1189 /// isIntegerType() does *not* include complex integers (a GCC extension). 1190 /// isComplexIntegerType() can be used to test for complex integers. 1191 bool isIntegerType() const; // C99 6.2.5p17 (int, char, bool, enum) 1192 bool isEnumeralType() const; 1193 bool isBooleanType() const; 1194 bool isCharType() const; 1195 bool isWideCharType() const; 1196 bool isAnyCharacterType() const; 1197 bool isIntegralType(ASTContext &Ctx) const; 1198 1199 /// \brief Determine whether this type is an integral or enumeration type. 1200 bool isIntegralOrEnumerationType() const; 1201 /// \brief Determine whether this type is an integral or unscoped enumeration 1202 /// type. 1203 bool isIntegralOrUnscopedEnumerationType() const; 1204 1205 /// Floating point categories. 1206 bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double) 1207 /// isComplexType() does *not* include complex integers (a GCC extension). 1208 /// isComplexIntegerType() can be used to test for complex integers. 1209 bool isComplexType() const; // C99 6.2.5p11 (complex) 1210 bool isAnyComplexType() const; // C99 6.2.5p11 (complex) + Complex Int. 1211 bool isFloatingType() const; // C99 6.2.5p11 (real floating + complex) 1212 bool isRealType() const; // C99 6.2.5p17 (real floating + integer) 1213 bool isArithmeticType() const; // C99 6.2.5p18 (integer + floating) 1214 bool isVoidType() const; // C99 6.2.5p19 1215 bool isDerivedType() const; // C99 6.2.5p20 1216 bool isScalarType() const; // C99 6.2.5p21 (arithmetic + pointers) 1217 bool isAggregateType() const; 1218 1219 // Type Predicates: Check to see if this type is structurally the specified 1220 // type, ignoring typedefs and qualifiers. 1221 bool isFunctionType() const; 1222 bool isFunctionNoProtoType() const { return getAs<FunctionNoProtoType>(); } 1223 bool isFunctionProtoType() const { return getAs<FunctionProtoType>(); } 1224 bool isPointerType() const; 1225 bool isAnyPointerType() const; // Any C pointer or ObjC object pointer 1226 bool isBlockPointerType() const; 1227 bool isVoidPointerType() const; 1228 bool isReferenceType() const; 1229 bool isLValueReferenceType() const; 1230 bool isRValueReferenceType() const; 1231 bool isFunctionPointerType() const; 1232 bool isMemberPointerType() const; 1233 bool isMemberFunctionPointerType() const; 1234 bool isMemberDataPointerType() const; 1235 bool isArrayType() const; 1236 bool isConstantArrayType() const; 1237 bool isIncompleteArrayType() const; 1238 bool isVariableArrayType() const; 1239 bool isDependentSizedArrayType() const; 1240 bool isRecordType() const; 1241 bool isClassType() const; 1242 bool isStructureType() const; 1243 bool isStructureOrClassType() const; 1244 bool isUnionType() const; 1245 bool isComplexIntegerType() const; // GCC _Complex integer type. 1246 bool isVectorType() const; // GCC vector type. 1247 bool isExtVectorType() const; // Extended vector type. 1248 bool isObjCObjectPointerType() const; // Pointer to *any* ObjC object. 1249 // FIXME: change this to 'raw' interface type, so we can used 'interface' type 1250 // for the common case. 1251 bool isObjCObjectType() const; // NSString or typeof(*(id)0) 1252 bool isObjCQualifiedInterfaceType() const; // NSString<foo> 1253 bool isObjCQualifiedIdType() const; // id<foo> 1254 bool isObjCQualifiedClassType() const; // Class<foo> 1255 bool isObjCObjectOrInterfaceType() const; 1256 bool isObjCIdType() const; // id 1257 bool isObjCClassType() const; // Class 1258 bool isObjCSelType() const; // Class 1259 bool isObjCBuiltinType() const; // 'id' or 'Class' 1260 bool isTemplateTypeParmType() const; // C++ template type parameter 1261 bool isNullPtrType() const; // C++0x nullptr_t 1262 1263 enum ScalarTypeKind { 1264 STK_Pointer, 1265 STK_MemberPointer, 1266 STK_Bool, 1267 STK_Integral, 1268 STK_Floating, 1269 STK_IntegralComplex, 1270 STK_FloatingComplex 1271 }; 1272 /// getScalarTypeKind - Given that this is a scalar type, classify it. 1273 ScalarTypeKind getScalarTypeKind() const; 1274 1275 /// isDependentType - Whether this type is a dependent type, meaning 1276 /// that its definition somehow depends on a template parameter 1277 /// (C++ [temp.dep.type]). 1278 bool isDependentType() const { return TypeBits.Dependent; } 1279 1280 /// \brief Whether this type is a variably-modified type (C99 6.7.5). 1281 bool isVariablyModifiedType() const { return TypeBits.VariablyModified; } 1282 1283 /// \brief Whether this type involves a variable-length array type 1284 /// with a definite size. 1285 bool hasSizedVLAType() const; 1286 1287 /// \brief Whether this type is or contains a local or unnamed type. 1288 bool hasUnnamedOrLocalType() const; 1289 1290 bool isOverloadableType() const; 1291 1292 /// \brief Determine wither this type is a C++ elaborated-type-specifier. 1293 bool isElaboratedTypeSpecifier() const; 1294 1295 /// hasPointerRepresentation - Whether this type is represented 1296 /// natively as a pointer; this includes pointers, references, block 1297 /// pointers, and Objective-C interface, qualified id, and qualified 1298 /// interface types, as well as nullptr_t. 1299 bool hasPointerRepresentation() const; 1300 1301 /// hasObjCPointerRepresentation - Whether this type can represent 1302 /// an objective pointer type for the purpose of GC'ability 1303 bool hasObjCPointerRepresentation() const; 1304 1305 /// \brief Determine whether this type has an integer representation 1306 /// of some sort, e.g., it is an integer type or a vector. 1307 bool hasIntegerRepresentation() const; 1308 1309 /// \brief Determine whether this type has an signed integer representation 1310 /// of some sort, e.g., it is an signed integer type or a vector. 1311 bool hasSignedIntegerRepresentation() const; 1312 1313 /// \brief Determine whether this type has an unsigned integer representation 1314 /// of some sort, e.g., it is an unsigned integer type or a vector. 1315 bool hasUnsignedIntegerRepresentation() const; 1316 1317 /// \brief Determine whether this type has a floating-point representation 1318 /// of some sort, e.g., it is a floating-point type or a vector thereof. 1319 bool hasFloatingRepresentation() const; 1320 1321 // Type Checking Functions: Check to see if this type is structurally the 1322 // specified type, ignoring typedefs and qualifiers, and return a pointer to 1323 // the best type we can. 1324 const RecordType *getAsStructureType() const; 1325 /// NOTE: getAs*ArrayType are methods on ASTContext. 1326 const RecordType *getAsUnionType() const; 1327 const ComplexType *getAsComplexIntegerType() const; // GCC complex int type. 1328 // The following is a convenience method that returns an ObjCObjectPointerType 1329 // for object declared using an interface. 1330 const ObjCObjectPointerType *getAsObjCInterfacePointerType() const; 1331 const ObjCObjectPointerType *getAsObjCQualifiedIdType() const; 1332 const ObjCObjectPointerType *getAsObjCQualifiedClassType() const; 1333 const ObjCObjectType *getAsObjCQualifiedInterfaceType() const; 1334 const CXXRecordDecl *getCXXRecordDeclForPointerType() const; 1335 1336 /// \brief Retrieves the CXXRecordDecl that this type refers to, either 1337 /// because the type is a RecordType or because it is the injected-class-name 1338 /// type of a class template or class template partial specialization. 1339 CXXRecordDecl *getAsCXXRecordDecl() const; 1340 1341 /// \brief Get the AutoType whose type will be deduced for a variable with 1342 /// an initializer of this type. This looks through declarators like pointer 1343 /// types, but not through decltype or typedefs. 1344 AutoType *getContainedAutoType() const; 1345 1346 /// Member-template getAs<specific type>'. Look through sugar for 1347 /// an instance of <specific type>. This scheme will eventually 1348 /// replace the specific getAsXXXX methods above. 1349 /// 1350 /// There are some specializations of this member template listed 1351 /// immediately following this class. 1352 template <typename T> const T *getAs() const; 1353 1354 /// A variant of getAs<> for array types which silently discards 1355 /// qualifiers from the outermost type. 1356 const ArrayType *getAsArrayTypeUnsafe() const; 1357 1358 /// Member-template castAs<specific type>. Look through sugar for 1359 /// the underlying instance of <specific type>. 1360 /// 1361 /// This method has the same relationship to getAs<T> as cast<T> has 1362 /// to dyn_cast<T>; which is to say, the underlying type *must* 1363 /// have the intended type, and this method will never return null. 1364 template <typename T> const T *castAs() const; 1365 1366 /// A variant of castAs<> for array type which silently discards 1367 /// qualifiers from the outermost type. 1368 const ArrayType *castAsArrayTypeUnsafe() const; 1369 1370 /// getBaseElementTypeUnsafe - Get the base element type of this 1371 /// type, potentially discarding type qualifiers. This method 1372 /// should never be used when type qualifiers are meaningful. 1373 const Type *getBaseElementTypeUnsafe() const; 1374 1375 /// getArrayElementTypeNoTypeQual - If this is an array type, return the 1376 /// element type of the array, potentially with type qualifiers missing. 1377 /// This method should never be used when type qualifiers are meaningful. 1378 const Type *getArrayElementTypeNoTypeQual() const; 1379 1380 /// getPointeeType - If this is a pointer, ObjC object pointer, or block 1381 /// pointer, this returns the respective pointee. 1382 QualType getPointeeType() const; 1383 1384 /// getUnqualifiedDesugaredType() - Return the specified type with 1385 /// any "sugar" removed from the type, removing any typedefs, 1386 /// typeofs, etc., as well as any qualifiers. 1387 const Type *getUnqualifiedDesugaredType() const; 1388 1389 /// More type predicates useful for type checking/promotion 1390 bool isPromotableIntegerType() const; // C99 6.3.1.1p2 1391 1392 /// isSignedIntegerType - Return true if this is an integer type that is 1393 /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], 1394 /// an enum decl which has a signed representation, or a vector of signed 1395 /// integer element type. 1396 bool isSignedIntegerType() const; 1397 1398 /// isUnsignedIntegerType - Return true if this is an integer type that is 1399 /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum 1400 /// decl which has an unsigned representation, or a vector of unsigned integer 1401 /// element type. 1402 bool isUnsignedIntegerType() const; 1403 1404 /// isConstantSizeType - Return true if this is not a variable sized type, 1405 /// according to the rules of C99 6.7.5p3. It is not legal to call this on 1406 /// incomplete types. 1407 bool isConstantSizeType() const; 1408 1409 /// isSpecifierType - Returns true if this type can be represented by some 1410 /// set of type specifiers. 1411 bool isSpecifierType() const; 1412 1413 /// \brief Determine the linkage of this type. 1414 Linkage getLinkage() const; 1415 1416 /// \brief Determine the visibility of this type. 1417 Visibility getVisibility() const; 1418 1419 /// \brief Determine the linkage and visibility of this type. 1420 std::pair<Linkage,Visibility> getLinkageAndVisibility() const; 1421 1422 /// \brief Note that the linkage is no longer known. 1423 void ClearLinkageCache(); 1424 1425 const char *getTypeClassName() const; 1426 1427 QualType getCanonicalTypeInternal() const { 1428 return CanonicalType; 1429 } 1430 CanQualType getCanonicalTypeUnqualified() const; // in CanonicalType.h 1431 void dump() const; 1432 static bool classof(const Type *) { return true; } 1433 1434 friend class ASTReader; 1435 friend class ASTWriter; 1436}; 1437 1438template <> inline const TypedefType *Type::getAs() const { 1439 return dyn_cast<TypedefType>(this); 1440} 1441 1442// We can do canonical leaf types faster, because we don't have to 1443// worry about preserving child type decoration. 1444#define TYPE(Class, Base) 1445#define LEAF_TYPE(Class) \ 1446template <> inline const Class##Type *Type::getAs() const { \ 1447 return dyn_cast<Class##Type>(CanonicalType); \ 1448} \ 1449template <> inline const Class##Type *Type::castAs() const { \ 1450 return cast<Class##Type>(CanonicalType); \ 1451} 1452#include "clang/AST/TypeNodes.def" 1453 1454 1455/// BuiltinType - This class is used for builtin types like 'int'. Builtin 1456/// types are always canonical and have a literal name field. 1457class BuiltinType : public Type { 1458public: 1459 enum Kind { 1460 Void, 1461 1462 Bool, // This is bool and/or _Bool. 1463 Char_U, // This is 'char' for targets where char is unsigned. 1464 UChar, // This is explicitly qualified unsigned char. 1465 WChar_U, // This is 'wchar_t' for C++, when unsigned. 1466 Char16, // This is 'char16_t' for C++. 1467 Char32, // This is 'char32_t' for C++. 1468 UShort, 1469 UInt, 1470 ULong, 1471 ULongLong, 1472 UInt128, // __uint128_t 1473 1474 Char_S, // This is 'char' for targets where char is signed. 1475 SChar, // This is explicitly qualified signed char. 1476 WChar_S, // This is 'wchar_t' for C++, when signed. 1477 Short, 1478 Int, 1479 Long, 1480 LongLong, 1481 Int128, // __int128_t 1482 1483 Float, Double, LongDouble, 1484 1485 NullPtr, // This is the type of C++0x 'nullptr'. 1486 1487 /// This represents the type of an expression whose type is 1488 /// totally unknown, e.g. 'T::foo'. It is permitted for this to 1489 /// appear in situations where the structure of the type is 1490 /// theoretically deducible. 1491 Dependent, 1492 1493 /// The type of an unresolved overload set. 1494 Overload, 1495 1496 /// __builtin_any_type. Useful for clients like debuggers 1497 /// that don't know what type to give something. Only a small 1498 /// number of operations are valid on expressions of unknown type; 1499 /// notable among them, calls and explicit casts. 1500 UnknownAny, 1501 1502 /// The primitive Objective C 'id' type. The type pointed to by the 1503 /// user-visible 'id' type. Only ever shows up in an AST as the base 1504 /// type of an ObjCObjectType. 1505 ObjCId, 1506 1507 /// The primitive Objective C 'Class' type. The type pointed to by the 1508 /// user-visible 'Class' type. Only ever shows up in an AST as the 1509 /// base type of an ObjCObjectType. 1510 ObjCClass, 1511 1512 ObjCSel // This represents the ObjC 'SEL' type. 1513 }; 1514 1515public: 1516 BuiltinType(Kind K) 1517 : Type(Builtin, QualType(), /*Dependent=*/(K == Dependent), 1518 /*VariablyModified=*/false, 1519 /*Unexpanded paramter pack=*/false) { 1520 BuiltinTypeBits.Kind = K; 1521 } 1522 1523 Kind getKind() const { return static_cast<Kind>(BuiltinTypeBits.Kind); } 1524 const char *getName(const LangOptions &LO) const; 1525 1526 bool isSugared() const { return false; } 1527 QualType desugar() const { return QualType(this, 0); } 1528 1529 bool isInteger() const { 1530 return getKind() >= Bool && getKind() <= Int128; 1531 } 1532 1533 bool isSignedInteger() const { 1534 return getKind() >= Char_S && getKind() <= Int128; 1535 } 1536 1537 bool isUnsignedInteger() const { 1538 return getKind() >= Bool && getKind() <= UInt128; 1539 } 1540 1541 bool isFloatingPoint() const { 1542 return getKind() >= Float && getKind() <= LongDouble; 1543 } 1544 1545 /// Determines whether this type is a "forbidden" placeholder type, 1546 /// i.e. a type which cannot appear in arbitrary positions in a 1547 /// fully-formed expression. 1548 bool isPlaceholderType() const { 1549 return getKind() == Overload || getKind() == UnknownAny; 1550 } 1551 1552 static bool classof(const Type *T) { return T->getTypeClass() == Builtin; } 1553 static bool classof(const BuiltinType *) { return true; } 1554}; 1555 1556/// ComplexType - C99 6.2.5p11 - Complex values. This supports the C99 complex 1557/// types (_Complex float etc) as well as the GCC integer complex extensions. 1558/// 1559class ComplexType : public Type, public llvm::FoldingSetNode { 1560 QualType ElementType; 1561 ComplexType(QualType Element, QualType CanonicalPtr) : 1562 Type(Complex, CanonicalPtr, Element->isDependentType(), 1563 Element->isVariablyModifiedType(), 1564 Element->containsUnexpandedParameterPack()), 1565 ElementType(Element) { 1566 } 1567 friend class ASTContext; // ASTContext creates these. 1568 1569public: 1570 QualType getElementType() const { return ElementType; } 1571 1572 bool isSugared() const { return false; } 1573 QualType desugar() const { return QualType(this, 0); } 1574 1575 void Profile(llvm::FoldingSetNodeID &ID) { 1576 Profile(ID, getElementType()); 1577 } 1578 static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) { 1579 ID.AddPointer(Element.getAsOpaquePtr()); 1580 } 1581 1582 static bool classof(const Type *T) { return T->getTypeClass() == Complex; } 1583 static bool classof(const ComplexType *) { return true; } 1584}; 1585 1586/// ParenType - Sugar for parentheses used when specifying types. 1587/// 1588class ParenType : public Type, public llvm::FoldingSetNode { 1589 QualType Inner; 1590 1591 ParenType(QualType InnerType, QualType CanonType) : 1592 Type(Paren, CanonType, InnerType->isDependentType(), 1593 InnerType->isVariablyModifiedType(), 1594 InnerType->containsUnexpandedParameterPack()), 1595 Inner(InnerType) { 1596 } 1597 friend class ASTContext; // ASTContext creates these. 1598 1599public: 1600 1601 QualType getInnerType() const { return Inner; } 1602 1603 bool isSugared() const { return true; } 1604 QualType desugar() const { return getInnerType(); } 1605 1606 void Profile(llvm::FoldingSetNodeID &ID) { 1607 Profile(ID, getInnerType()); 1608 } 1609 static void Profile(llvm::FoldingSetNodeID &ID, QualType Inner) { 1610 Inner.Profile(ID); 1611 } 1612 1613 static bool classof(const Type *T) { return T->getTypeClass() == Paren; } 1614 static bool classof(const ParenType *) { return true; } 1615}; 1616 1617/// PointerType - C99 6.7.5.1 - Pointer Declarators. 1618/// 1619class PointerType : public Type, public llvm::FoldingSetNode { 1620 QualType PointeeType; 1621 1622 PointerType(QualType Pointee, QualType CanonicalPtr) : 1623 Type(Pointer, CanonicalPtr, Pointee->isDependentType(), 1624 Pointee->isVariablyModifiedType(), 1625 Pointee->containsUnexpandedParameterPack()), 1626 PointeeType(Pointee) { 1627 } 1628 friend class ASTContext; // ASTContext creates these. 1629 1630public: 1631 1632 QualType getPointeeType() const { return PointeeType; } 1633 1634 bool isSugared() const { return false; } 1635 QualType desugar() const { return QualType(this, 0); } 1636 1637 void Profile(llvm::FoldingSetNodeID &ID) { 1638 Profile(ID, getPointeeType()); 1639 } 1640 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { 1641 ID.AddPointer(Pointee.getAsOpaquePtr()); 1642 } 1643 1644 static bool classof(const Type *T) { return T->getTypeClass() == Pointer; } 1645 static bool classof(const PointerType *) { return true; } 1646}; 1647 1648/// BlockPointerType - pointer to a block type. 1649/// This type is to represent types syntactically represented as 1650/// "void (^)(int)", etc. Pointee is required to always be a function type. 1651/// 1652class BlockPointerType : public Type, public llvm::FoldingSetNode { 1653 QualType PointeeType; // Block is some kind of pointer type 1654 BlockPointerType(QualType Pointee, QualType CanonicalCls) : 1655 Type(BlockPointer, CanonicalCls, Pointee->isDependentType(), 1656 Pointee->isVariablyModifiedType(), 1657 Pointee->containsUnexpandedParameterPack()), 1658 PointeeType(Pointee) { 1659 } 1660 friend class ASTContext; // ASTContext creates these. 1661 1662public: 1663 1664 // Get the pointee type. Pointee is required to always be a function type. 1665 QualType getPointeeType() const { return PointeeType; } 1666 1667 bool isSugared() const { return false; } 1668 QualType desugar() const { return QualType(this, 0); } 1669 1670 void Profile(llvm::FoldingSetNodeID &ID) { 1671 Profile(ID, getPointeeType()); 1672 } 1673 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { 1674 ID.AddPointer(Pointee.getAsOpaquePtr()); 1675 } 1676 1677 static bool classof(const Type *T) { 1678 return T->getTypeClass() == BlockPointer; 1679 } 1680 static bool classof(const BlockPointerType *) { return true; } 1681}; 1682 1683/// ReferenceType - Base for LValueReferenceType and RValueReferenceType 1684/// 1685class ReferenceType : public Type, public llvm::FoldingSetNode { 1686 QualType PointeeType; 1687 1688protected: 1689 ReferenceType(TypeClass tc, QualType Referencee, QualType CanonicalRef, 1690 bool SpelledAsLValue) : 1691 Type(tc, CanonicalRef, Referencee->isDependentType(), 1692 Referencee->isVariablyModifiedType(), 1693 Referencee->containsUnexpandedParameterPack()), 1694 PointeeType(Referencee) 1695 { 1696 ReferenceTypeBits.SpelledAsLValue = SpelledAsLValue; 1697 ReferenceTypeBits.InnerRef = Referencee->isReferenceType(); 1698 } 1699 1700public: 1701 bool isSpelledAsLValue() const { return ReferenceTypeBits.SpelledAsLValue; } 1702 bool isInnerRef() const { return ReferenceTypeBits.InnerRef; } 1703 1704 QualType getPointeeTypeAsWritten() const { return PointeeType; } 1705 QualType getPointeeType() const { 1706 // FIXME: this might strip inner qualifiers; okay? 1707 const ReferenceType *T = this; 1708 while (T->isInnerRef()) 1709 T = T->PointeeType->castAs<ReferenceType>(); 1710 return T->PointeeType; 1711 } 1712 1713 void Profile(llvm::FoldingSetNodeID &ID) { 1714 Profile(ID, PointeeType, isSpelledAsLValue()); 1715 } 1716 static void Profile(llvm::FoldingSetNodeID &ID, 1717 QualType Referencee, 1718 bool SpelledAsLValue) { 1719 ID.AddPointer(Referencee.getAsOpaquePtr()); 1720 ID.AddBoolean(SpelledAsLValue); 1721 } 1722 1723 static bool classof(const Type *T) { 1724 return T->getTypeClass() == LValueReference || 1725 T->getTypeClass() == RValueReference; 1726 } 1727 static bool classof(const ReferenceType *) { return true; } 1728}; 1729 1730/// LValueReferenceType - C++ [dcl.ref] - Lvalue reference 1731/// 1732class LValueReferenceType : public ReferenceType { 1733 LValueReferenceType(QualType Referencee, QualType CanonicalRef, 1734 bool SpelledAsLValue) : 1735 ReferenceType(LValueReference, Referencee, CanonicalRef, SpelledAsLValue) 1736 {} 1737 friend class ASTContext; // ASTContext creates these 1738public: 1739 bool isSugared() const { return false; } 1740 QualType desugar() const { return QualType(this, 0); } 1741 1742 static bool classof(const Type *T) { 1743 return T->getTypeClass() == LValueReference; 1744 } 1745 static bool classof(const LValueReferenceType *) { return true; } 1746}; 1747 1748/// RValueReferenceType - C++0x [dcl.ref] - Rvalue reference 1749/// 1750class RValueReferenceType : public ReferenceType { 1751 RValueReferenceType(QualType Referencee, QualType CanonicalRef) : 1752 ReferenceType(RValueReference, Referencee, CanonicalRef, false) { 1753 } 1754 friend class ASTContext; // ASTContext creates these 1755public: 1756 bool isSugared() const { return false; } 1757 QualType desugar() const { return QualType(this, 0); } 1758 1759 static bool classof(const Type *T) { 1760 return T->getTypeClass() == RValueReference; 1761 } 1762 static bool classof(const RValueReferenceType *) { return true; } 1763}; 1764 1765/// MemberPointerType - C++ 8.3.3 - Pointers to members 1766/// 1767class MemberPointerType : public Type, public llvm::FoldingSetNode { 1768 QualType PointeeType; 1769 /// The class of which the pointee is a member. Must ultimately be a 1770 /// RecordType, but could be a typedef or a template parameter too. 1771 const Type *Class; 1772 1773 MemberPointerType(QualType Pointee, const Type *Cls, QualType CanonicalPtr) : 1774 Type(MemberPointer, CanonicalPtr, 1775 Cls->isDependentType() || Pointee->isDependentType(), 1776 Pointee->isVariablyModifiedType(), 1777 (Cls->containsUnexpandedParameterPack() || 1778 Pointee->containsUnexpandedParameterPack())), 1779 PointeeType(Pointee), Class(Cls) { 1780 } 1781 friend class ASTContext; // ASTContext creates these. 1782 1783public: 1784 QualType getPointeeType() const { return PointeeType; } 1785 1786 /// Returns true if the member type (i.e. the pointee type) is a 1787 /// function type rather than a data-member type. 1788 bool isMemberFunctionPointer() const { 1789 return PointeeType->isFunctionProtoType(); 1790 } 1791 1792 /// Returns true if the member type (i.e. the pointee type) is a 1793 /// data type rather than a function type. 1794 bool isMemberDataPointer() const { 1795 return !PointeeType->isFunctionProtoType(); 1796 } 1797 1798 const Type *getClass() const { return Class; } 1799 1800 bool isSugared() const { return false; } 1801 QualType desugar() const { return QualType(this, 0); } 1802 1803 void Profile(llvm::FoldingSetNodeID &ID) { 1804 Profile(ID, getPointeeType(), getClass()); 1805 } 1806 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee, 1807 const Type *Class) { 1808 ID.AddPointer(Pointee.getAsOpaquePtr()); 1809 ID.AddPointer(Class); 1810 } 1811 1812 static bool classof(const Type *T) { 1813 return T->getTypeClass() == MemberPointer; 1814 } 1815 static bool classof(const MemberPointerType *) { return true; } 1816}; 1817 1818/// ArrayType - C99 6.7.5.2 - Array Declarators. 1819/// 1820class ArrayType : public Type, public llvm::FoldingSetNode { 1821public: 1822 /// ArraySizeModifier - Capture whether this is a normal array (e.g. int X[4]) 1823 /// an array with a static size (e.g. int X[static 4]), or an array 1824 /// with a star size (e.g. int X[*]). 1825 /// 'static' is only allowed on function parameters. 1826 enum ArraySizeModifier { 1827 Normal, Static, Star 1828 }; 1829private: 1830 /// ElementType - The element type of the array. 1831 QualType ElementType; 1832 1833protected: 1834 // C++ [temp.dep.type]p1: 1835 // A type is dependent if it is... 1836 // - an array type constructed from any dependent type or whose 1837 // size is specified by a constant expression that is 1838 // value-dependent, 1839 ArrayType(TypeClass tc, QualType et, QualType can, 1840 ArraySizeModifier sm, unsigned tq, 1841 bool ContainsUnexpandedParameterPack) 1842 : Type(tc, can, et->isDependentType() || tc == DependentSizedArray, 1843 (tc == VariableArray || et->isVariablyModifiedType()), 1844 ContainsUnexpandedParameterPack), 1845 ElementType(et) { 1846 ArrayTypeBits.IndexTypeQuals = tq; 1847 ArrayTypeBits.SizeModifier = sm; 1848 } 1849 1850 friend class ASTContext; // ASTContext creates these. 1851 1852public: 1853 QualType getElementType() const { return ElementType; } 1854 ArraySizeModifier getSizeModifier() const { 1855 return ArraySizeModifier(ArrayTypeBits.SizeModifier); 1856 } 1857 Qualifiers getIndexTypeQualifiers() const { 1858 return Qualifiers::fromCVRMask(getIndexTypeCVRQualifiers()); 1859 } 1860 unsigned getIndexTypeCVRQualifiers() const { 1861 return ArrayTypeBits.IndexTypeQuals; 1862 } 1863 1864 static bool classof(const Type *T) { 1865 return T->getTypeClass() == ConstantArray || 1866 T->getTypeClass() == VariableArray || 1867 T->getTypeClass() == IncompleteArray || 1868 T->getTypeClass() == DependentSizedArray; 1869 } 1870 static bool classof(const ArrayType *) { return true; } 1871}; 1872 1873/// ConstantArrayType - This class represents the canonical version of 1874/// C arrays with a specified constant size. For example, the canonical 1875/// type for 'int A[4 + 4*100]' is a ConstantArrayType where the element 1876/// type is 'int' and the size is 404. 1877class ConstantArrayType : public ArrayType { 1878 llvm::APInt Size; // Allows us to unique the type. 1879 1880 ConstantArrayType(QualType et, QualType can, const llvm::APInt &size, 1881 ArraySizeModifier sm, unsigned tq) 1882 : ArrayType(ConstantArray, et, can, sm, tq, 1883 et->containsUnexpandedParameterPack()), 1884 Size(size) {} 1885protected: 1886 ConstantArrayType(TypeClass tc, QualType et, QualType can, 1887 const llvm::APInt &size, ArraySizeModifier sm, unsigned tq) 1888 : ArrayType(tc, et, can, sm, tq, et->containsUnexpandedParameterPack()), 1889 Size(size) {} 1890 friend class ASTContext; // ASTContext creates these. 1891public: 1892 const llvm::APInt &getSize() const { return Size; } 1893 bool isSugared() const { return false; } 1894 QualType desugar() const { return QualType(this, 0); } 1895 1896 1897 /// \brief Determine the number of bits required to address a member of 1898 // an array with the given element type and number of elements. 1899 static unsigned getNumAddressingBits(ASTContext &Context, 1900 QualType ElementType, 1901 const llvm::APInt &NumElements); 1902 1903 /// \brief Determine the maximum number of active bits that an array's size 1904 /// can require, which limits the maximum size of the array. 1905 static unsigned getMaxSizeBits(ASTContext &Context); 1906 1907 void Profile(llvm::FoldingSetNodeID &ID) { 1908 Profile(ID, getElementType(), getSize(), 1909 getSizeModifier(), getIndexTypeCVRQualifiers()); 1910 } 1911 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, 1912 const llvm::APInt &ArraySize, ArraySizeModifier SizeMod, 1913 unsigned TypeQuals) { 1914 ID.AddPointer(ET.getAsOpaquePtr()); 1915 ID.AddInteger(ArraySize.getZExtValue()); 1916 ID.AddInteger(SizeMod); 1917 ID.AddInteger(TypeQuals); 1918 } 1919 static bool classof(const Type *T) { 1920 return T->getTypeClass() == ConstantArray; 1921 } 1922 static bool classof(const ConstantArrayType *) { return true; } 1923}; 1924 1925/// IncompleteArrayType - This class represents C arrays with an unspecified 1926/// size. For example 'int A[]' has an IncompleteArrayType where the element 1927/// type is 'int' and the size is unspecified. 1928class IncompleteArrayType : public ArrayType { 1929 1930 IncompleteArrayType(QualType et, QualType can, 1931 ArraySizeModifier sm, unsigned tq) 1932 : ArrayType(IncompleteArray, et, can, sm, tq, 1933 et->containsUnexpandedParameterPack()) {} 1934 friend class ASTContext; // ASTContext creates these. 1935public: 1936 bool isSugared() const { return false; } 1937 QualType desugar() const { return QualType(this, 0); } 1938 1939 static bool classof(const Type *T) { 1940 return T->getTypeClass() == IncompleteArray; 1941 } 1942 static bool classof(const IncompleteArrayType *) { return true; } 1943 1944 friend class StmtIteratorBase; 1945 1946 void Profile(llvm::FoldingSetNodeID &ID) { 1947 Profile(ID, getElementType(), getSizeModifier(), 1948 getIndexTypeCVRQualifiers()); 1949 } 1950 1951 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, 1952 ArraySizeModifier SizeMod, unsigned TypeQuals) { 1953 ID.AddPointer(ET.getAsOpaquePtr()); 1954 ID.AddInteger(SizeMod); 1955 ID.AddInteger(TypeQuals); 1956 } 1957}; 1958 1959/// VariableArrayType - This class represents C arrays with a specified size 1960/// which is not an integer-constant-expression. For example, 'int s[x+foo()]'. 1961/// Since the size expression is an arbitrary expression, we store it as such. 1962/// 1963/// Note: VariableArrayType's aren't uniqued (since the expressions aren't) and 1964/// should not be: two lexically equivalent variable array types could mean 1965/// different things, for example, these variables do not have the same type 1966/// dynamically: 1967/// 1968/// void foo(int x) { 1969/// int Y[x]; 1970/// ++x; 1971/// int Z[x]; 1972/// } 1973/// 1974class VariableArrayType : public ArrayType { 1975 /// SizeExpr - An assignment expression. VLA's are only permitted within 1976 /// a function block. 1977 Stmt *SizeExpr; 1978 /// Brackets - The left and right array brackets. 1979 SourceRange Brackets; 1980 1981 VariableArrayType(QualType et, QualType can, Expr *e, 1982 ArraySizeModifier sm, unsigned tq, 1983 SourceRange brackets) 1984 : ArrayType(VariableArray, et, can, sm, tq, 1985 et->containsUnexpandedParameterPack()), 1986 SizeExpr((Stmt*) e), Brackets(brackets) {} 1987 friend class ASTContext; // ASTContext creates these. 1988 1989public: 1990 Expr *getSizeExpr() const { 1991 // We use C-style casts instead of cast<> here because we do not wish 1992 // to have a dependency of Type.h on Stmt.h/Expr.h. 1993 return (Expr*) SizeExpr; 1994 } 1995 SourceRange getBracketsRange() const { return Brackets; } 1996 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } 1997 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } 1998 1999 bool isSugared() const { return false; } 2000 QualType desugar() const { return QualType(this, 0); } 2001 2002 static bool classof(const Type *T) { 2003 return T->getTypeClass() == VariableArray; 2004 } 2005 static bool classof(const VariableArrayType *) { return true; } 2006 2007 friend class StmtIteratorBase; 2008 2009 void Profile(llvm::FoldingSetNodeID &ID) { 2010 assert(0 && "Cannot unique VariableArrayTypes."); 2011 } 2012}; 2013 2014/// DependentSizedArrayType - This type represents an array type in 2015/// C++ whose size is a value-dependent expression. For example: 2016/// 2017/// \code 2018/// template<typename T, int Size> 2019/// class array { 2020/// T data[Size]; 2021/// }; 2022/// \endcode 2023/// 2024/// For these types, we won't actually know what the array bound is 2025/// until template instantiation occurs, at which point this will 2026/// become either a ConstantArrayType or a VariableArrayType. 2027class DependentSizedArrayType : public ArrayType { 2028 const ASTContext &Context; 2029 2030 /// \brief An assignment expression that will instantiate to the 2031 /// size of the array. 2032 /// 2033 /// The expression itself might be NULL, in which case the array 2034 /// type will have its size deduced from an initializer. 2035 Stmt *SizeExpr; 2036 2037 /// Brackets - The left and right array brackets. 2038 SourceRange Brackets; 2039 2040 DependentSizedArrayType(const ASTContext &Context, QualType et, QualType can, 2041 Expr *e, ArraySizeModifier sm, unsigned tq, 2042 SourceRange brackets); 2043 2044 friend class ASTContext; // ASTContext creates these. 2045 2046public: 2047 Expr *getSizeExpr() const { 2048 // We use C-style casts instead of cast<> here because we do not wish 2049 // to have a dependency of Type.h on Stmt.h/Expr.h. 2050 return (Expr*) SizeExpr; 2051 } 2052 SourceRange getBracketsRange() const { return Brackets; } 2053 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } 2054 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } 2055 2056 bool isSugared() const { return false; } 2057 QualType desugar() const { return QualType(this, 0); } 2058 2059 static bool classof(const Type *T) { 2060 return T->getTypeClass() == DependentSizedArray; 2061 } 2062 static bool classof(const DependentSizedArrayType *) { return true; } 2063 2064 friend class StmtIteratorBase; 2065 2066 2067 void Profile(llvm::FoldingSetNodeID &ID) { 2068 Profile(ID, Context, getElementType(), 2069 getSizeModifier(), getIndexTypeCVRQualifiers(), getSizeExpr()); 2070 } 2071 2072 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 2073 QualType ET, ArraySizeModifier SizeMod, 2074 unsigned TypeQuals, Expr *E); 2075}; 2076 2077/// DependentSizedExtVectorType - This type represent an extended vector type 2078/// where either the type or size is dependent. For example: 2079/// @code 2080/// template<typename T, int Size> 2081/// class vector { 2082/// typedef T __attribute__((ext_vector_type(Size))) type; 2083/// } 2084/// @endcode 2085class DependentSizedExtVectorType : public Type, public llvm::FoldingSetNode { 2086 const ASTContext &Context; 2087 Expr *SizeExpr; 2088 /// ElementType - The element type of the array. 2089 QualType ElementType; 2090 SourceLocation loc; 2091 2092 DependentSizedExtVectorType(const ASTContext &Context, QualType ElementType, 2093 QualType can, Expr *SizeExpr, SourceLocation loc); 2094 2095 friend class ASTContext; 2096 2097public: 2098 Expr *getSizeExpr() const { return SizeExpr; } 2099 QualType getElementType() const { return ElementType; } 2100 SourceLocation getAttributeLoc() const { return loc; } 2101 2102 bool isSugared() const { return false; } 2103 QualType desugar() const { return QualType(this, 0); } 2104 2105 static bool classof(const Type *T) { 2106 return T->getTypeClass() == DependentSizedExtVector; 2107 } 2108 static bool classof(const DependentSizedExtVectorType *) { return true; } 2109 2110 void Profile(llvm::FoldingSetNodeID &ID) { 2111 Profile(ID, Context, getElementType(), getSizeExpr()); 2112 } 2113 2114 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 2115 QualType ElementType, Expr *SizeExpr); 2116}; 2117 2118 2119/// VectorType - GCC generic vector type. This type is created using 2120/// __attribute__((vector_size(n)), where "n" specifies the vector size in 2121/// bytes; or from an Altivec __vector or vector declaration. 2122/// Since the constructor takes the number of vector elements, the 2123/// client is responsible for converting the size into the number of elements. 2124class VectorType : public Type, public llvm::FoldingSetNode { 2125public: 2126 enum VectorKind { 2127 GenericVector, // not a target-specific vector type 2128 AltiVecVector, // is AltiVec vector 2129 AltiVecPixel, // is AltiVec 'vector Pixel' 2130 AltiVecBool, // is AltiVec 'vector bool ...' 2131 NeonVector, // is ARM Neon vector 2132 NeonPolyVector // is ARM Neon polynomial vector 2133 }; 2134protected: 2135 /// ElementType - The element type of the vector. 2136 QualType ElementType; 2137 2138 VectorType(QualType vecType, unsigned nElements, QualType canonType, 2139 VectorKind vecKind); 2140 2141 VectorType(TypeClass tc, QualType vecType, unsigned nElements, 2142 QualType canonType, VectorKind vecKind); 2143 2144 friend class ASTContext; // ASTContext creates these. 2145 2146public: 2147 2148 QualType getElementType() const { return ElementType; } 2149 unsigned getNumElements() const { return VectorTypeBits.NumElements; } 2150 2151 bool isSugared() const { return false; } 2152 QualType desugar() const { return QualType(this, 0); } 2153 2154 VectorKind getVectorKind() const { 2155 return VectorKind(VectorTypeBits.VecKind); 2156 } 2157 2158 void Profile(llvm::FoldingSetNodeID &ID) { 2159 Profile(ID, getElementType(), getNumElements(), 2160 getTypeClass(), getVectorKind()); 2161 } 2162 static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, 2163 unsigned NumElements, TypeClass TypeClass, 2164 VectorKind VecKind) { 2165 ID.AddPointer(ElementType.getAsOpaquePtr()); 2166 ID.AddInteger(NumElements); 2167 ID.AddInteger(TypeClass); 2168 ID.AddInteger(VecKind); 2169 } 2170 2171 static bool classof(const Type *T) { 2172 return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector; 2173 } 2174 static bool classof(const VectorType *) { return true; } 2175}; 2176 2177/// ExtVectorType - Extended vector type. This type is created using 2178/// __attribute__((ext_vector_type(n)), where "n" is the number of elements. 2179/// Unlike vector_size, ext_vector_type is only allowed on typedef's. This 2180/// class enables syntactic extensions, like Vector Components for accessing 2181/// points, colors, and textures (modeled after OpenGL Shading Language). 2182class ExtVectorType : public VectorType { 2183 ExtVectorType(QualType vecType, unsigned nElements, QualType canonType) : 2184 VectorType(ExtVector, vecType, nElements, canonType, GenericVector) {} 2185 friend class ASTContext; // ASTContext creates these. 2186public: 2187 static int getPointAccessorIdx(char c) { 2188 switch (c) { 2189 default: return -1; 2190 case 'x': return 0; 2191 case 'y': return 1; 2192 case 'z': return 2; 2193 case 'w': return 3; 2194 } 2195 } 2196 static int getNumericAccessorIdx(char c) { 2197 switch (c) { 2198 default: return -1; 2199 case '0': return 0; 2200 case '1': return 1; 2201 case '2': return 2; 2202 case '3': return 3; 2203 case '4': return 4; 2204 case '5': return 5; 2205 case '6': return 6; 2206 case '7': return 7; 2207 case '8': return 8; 2208 case '9': return 9; 2209 case 'A': 2210 case 'a': return 10; 2211 case 'B': 2212 case 'b': return 11; 2213 case 'C': 2214 case 'c': return 12; 2215 case 'D': 2216 case 'd': return 13; 2217 case 'E': 2218 case 'e': return 14; 2219 case 'F': 2220 case 'f': return 15; 2221 } 2222 } 2223 2224 static int getAccessorIdx(char c) { 2225 if (int idx = getPointAccessorIdx(c)+1) return idx-1; 2226 return getNumericAccessorIdx(c); 2227 } 2228 2229 bool isAccessorWithinNumElements(char c) const { 2230 if (int idx = getAccessorIdx(c)+1) 2231 return unsigned(idx-1) < getNumElements(); 2232 return false; 2233 } 2234 bool isSugared() const { return false; } 2235 QualType desugar() const { return QualType(this, 0); } 2236 2237 static bool classof(const Type *T) { 2238 return T->getTypeClass() == ExtVector; 2239 } 2240 static bool classof(const ExtVectorType *) { return true; } 2241}; 2242 2243/// FunctionType - C99 6.7.5.3 - Function Declarators. This is the common base 2244/// class of FunctionNoProtoType and FunctionProtoType. 2245/// 2246class FunctionType : public Type { 2247 // The type returned by the function. 2248 QualType ResultType; 2249 2250 public: 2251 /// ExtInfo - A class which abstracts out some details necessary for 2252 /// making a call. 2253 /// 2254 /// It is not actually used directly for storing this information in 2255 /// a FunctionType, although FunctionType does currently use the 2256 /// same bit-pattern. 2257 /// 2258 // If you add a field (say Foo), other than the obvious places (both, 2259 // constructors, compile failures), what you need to update is 2260 // * Operator== 2261 // * getFoo 2262 // * withFoo 2263 // * functionType. Add Foo, getFoo. 2264 // * ASTContext::getFooType 2265 // * ASTContext::mergeFunctionTypes 2266 // * FunctionNoProtoType::Profile 2267 // * FunctionProtoType::Profile 2268 // * TypePrinter::PrintFunctionProto 2269 // * AST read and write 2270 // * Codegen 2271 class ExtInfo { 2272 // Feel free to rearrange or add bits, but if you go over 8, 2273 // you'll need to adjust both the Bits field below and 2274 // Type::FunctionTypeBitfields. 2275 2276 // | CC |noreturn|hasregparm|regparm 2277 // |0 .. 2| 3 | 4 |5 .. 7 2278 enum { CallConvMask = 0x7 }; 2279 enum { NoReturnMask = 0x8 }; 2280 enum { HasRegParmMask = 0x10 }; 2281 enum { RegParmMask = ~(CallConvMask | NoReturnMask), 2282 RegParmOffset = 5 }; 2283 2284 unsigned char Bits; 2285 2286 ExtInfo(unsigned Bits) : Bits(static_cast<unsigned char>(Bits)) {} 2287 2288 friend class FunctionType; 2289 2290 public: 2291 // Constructor with no defaults. Use this when you know that you 2292 // have all the elements (when reading an AST file for example). 2293 ExtInfo(bool noReturn, bool hasRegParm, unsigned regParm, CallingConv cc) { 2294 Bits = ((unsigned) cc) | 2295 (noReturn ? NoReturnMask : 0) | 2296 (hasRegParm ? HasRegParmMask : 0) | 2297 (regParm << RegParmOffset); 2298 } 2299 2300 // Constructor with all defaults. Use when for example creating a 2301 // function know to use defaults. 2302 ExtInfo() : Bits(0) {} 2303 2304 bool getNoReturn() const { return Bits & NoReturnMask; } 2305 bool getHasRegParm() const { return Bits & HasRegParmMask; } 2306 unsigned getRegParm() const { return Bits >> RegParmOffset; } 2307 CallingConv getCC() const { return CallingConv(Bits & CallConvMask); } 2308 2309 bool operator==(ExtInfo Other) const { 2310 return Bits == Other.Bits; 2311 } 2312 bool operator!=(ExtInfo Other) const { 2313 return Bits != Other.Bits; 2314 } 2315 2316 // Note that we don't have setters. That is by design, use 2317 // the following with methods instead of mutating these objects. 2318 2319 ExtInfo withNoReturn(bool noReturn) const { 2320 if (noReturn) 2321 return ExtInfo(Bits | NoReturnMask); 2322 else 2323 return ExtInfo(Bits & ~NoReturnMask); 2324 } 2325 2326 ExtInfo withRegParm(unsigned RegParm) const { 2327 return ExtInfo(HasRegParmMask | (Bits & ~RegParmMask) | (RegParm << RegParmOffset)); 2328 } 2329 2330 ExtInfo withCallingConv(CallingConv cc) const { 2331 return ExtInfo((Bits & ~CallConvMask) | (unsigned) cc); 2332 } 2333 2334 void Profile(llvm::FoldingSetNodeID &ID) const { 2335 ID.AddInteger(Bits); 2336 } 2337 }; 2338 2339protected: 2340 FunctionType(TypeClass tc, QualType res, bool variadic, 2341 unsigned typeQuals, RefQualifierKind RefQualifier, 2342 QualType Canonical, bool Dependent, 2343 bool VariablyModified, bool ContainsUnexpandedParameterPack, 2344 ExtInfo Info) 2345 : Type(tc, Canonical, Dependent, VariablyModified, 2346 ContainsUnexpandedParameterPack), 2347 ResultType(res) { 2348 FunctionTypeBits.ExtInfo = Info.Bits; 2349 FunctionTypeBits.Variadic = variadic; 2350 FunctionTypeBits.TypeQuals = typeQuals; 2351 FunctionTypeBits.RefQualifier = static_cast<unsigned>(RefQualifier); 2352 } 2353 bool isVariadic() const { return FunctionTypeBits.Variadic; } 2354 unsigned getTypeQuals() const { return FunctionTypeBits.TypeQuals; } 2355 2356 RefQualifierKind getRefQualifier() const { 2357 return static_cast<RefQualifierKind>(FunctionTypeBits.RefQualifier); 2358 } 2359 2360public: 2361 2362 QualType getResultType() const { return ResultType; } 2363 2364 bool getHasRegParm() const { return getExtInfo().getHasRegParm(); } 2365 unsigned getRegParmType() const { return getExtInfo().getRegParm(); } 2366 bool getNoReturnAttr() const { return getExtInfo().getNoReturn(); } 2367 CallingConv getCallConv() const { return getExtInfo().getCC(); } 2368 ExtInfo getExtInfo() const { return ExtInfo(FunctionTypeBits.ExtInfo); } 2369 2370 /// \brief Determine the type of an expression that calls a function of 2371 /// this type. 2372 QualType getCallResultType(ASTContext &Context) const { 2373 return getResultType().getNonLValueExprType(Context); 2374 } 2375 2376 static llvm::StringRef getNameForCallConv(CallingConv CC); 2377 2378 static bool classof(const Type *T) { 2379 return T->getTypeClass() == FunctionNoProto || 2380 T->getTypeClass() == FunctionProto; 2381 } 2382 static bool classof(const FunctionType *) { return true; } 2383}; 2384 2385/// FunctionNoProtoType - Represents a K&R-style 'int foo()' function, which has 2386/// no information available about its arguments. 2387class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode { 2388 FunctionNoProtoType(QualType Result, QualType Canonical, ExtInfo Info) 2389 : FunctionType(FunctionNoProto, Result, false, 0, RQ_None, Canonical, 2390 /*Dependent=*/false, Result->isVariablyModifiedType(), 2391 /*ContainsUnexpandedParameterPack=*/false, Info) {} 2392 2393 friend class ASTContext; // ASTContext creates these. 2394 2395public: 2396 // No additional state past what FunctionType provides. 2397 2398 bool isSugared() const { return false; } 2399 QualType desugar() const { return QualType(this, 0); } 2400 2401 void Profile(llvm::FoldingSetNodeID &ID) { 2402 Profile(ID, getResultType(), getExtInfo()); 2403 } 2404 static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType, 2405 ExtInfo Info) { 2406 Info.Profile(ID); 2407 ID.AddPointer(ResultType.getAsOpaquePtr()); 2408 } 2409 2410 static bool classof(const Type *T) { 2411 return T->getTypeClass() == FunctionNoProto; 2412 } 2413 static bool classof(const FunctionNoProtoType *) { return true; } 2414}; 2415 2416/// FunctionProtoType - Represents a prototype with argument type info, e.g. 2417/// 'int foo(int)' or 'int foo(void)'. 'void' is represented as having no 2418/// arguments, not as having a single void argument. Such a type can have an 2419/// exception specification, but this specification is not part of the canonical 2420/// type. 2421class FunctionProtoType : public FunctionType, public llvm::FoldingSetNode { 2422public: 2423 /// ExtProtoInfo - Extra information about a function prototype. 2424 struct ExtProtoInfo { 2425 ExtProtoInfo() : 2426 Variadic(false), ExceptionSpecType(EST_None), TypeQuals(0), 2427 RefQualifier(RQ_None), NumExceptions(0), Exceptions(0), NoexceptExpr(0) {} 2428 2429 FunctionType::ExtInfo ExtInfo; 2430 bool Variadic; 2431 ExceptionSpecificationType ExceptionSpecType; 2432 unsigned char TypeQuals; 2433 RefQualifierKind RefQualifier; 2434 unsigned NumExceptions; 2435 const QualType *Exceptions; 2436 Expr *NoexceptExpr; 2437 }; 2438 2439private: 2440 /// \brief Determine whether there are any argument types that 2441 /// contain an unexpanded parameter pack. 2442 static bool containsAnyUnexpandedParameterPack(const QualType *ArgArray, 2443 unsigned numArgs) { 2444 for (unsigned Idx = 0; Idx < numArgs; ++Idx) 2445 if (ArgArray[Idx]->containsUnexpandedParameterPack()) 2446 return true; 2447 2448 return false; 2449 } 2450 2451 FunctionProtoType(QualType result, const QualType *args, unsigned numArgs, 2452 QualType canonical, const ExtProtoInfo &epi); 2453 2454 /// NumArgs - The number of arguments this function has, not counting '...'. 2455 unsigned NumArgs : 20; 2456 2457 /// NumExceptions - The number of types in the exception spec, if any. 2458 unsigned NumExceptions : 9; 2459 2460 /// ExceptionSpecType - The type of exception specification this function has. 2461 unsigned ExceptionSpecType : 3; 2462 2463 /// ArgInfo - There is an variable size array after the class in memory that 2464 /// holds the argument types. 2465 2466 /// Exceptions - There is another variable size array after ArgInfo that 2467 /// holds the exception types. 2468 2469 /// NoexceptExpr - Instead of Exceptions, there may be a single Expr* pointing 2470 /// to the expression in the noexcept() specifier. 2471 2472 friend class ASTContext; // ASTContext creates these. 2473 2474public: 2475 unsigned getNumArgs() const { return NumArgs; } 2476 QualType getArgType(unsigned i) const { 2477 assert(i < NumArgs && "Invalid argument number!"); 2478 return arg_type_begin()[i]; 2479 } 2480 2481 ExtProtoInfo getExtProtoInfo() const { 2482 ExtProtoInfo EPI; 2483 EPI.ExtInfo = getExtInfo(); 2484 EPI.Variadic = isVariadic(); 2485 EPI.ExceptionSpecType = getExceptionSpecType(); 2486 EPI.TypeQuals = static_cast<unsigned char>(getTypeQuals()); 2487 EPI.RefQualifier = getRefQualifier(); 2488 if (EPI.ExceptionSpecType == EST_Dynamic) { 2489 EPI.NumExceptions = NumExceptions; 2490 EPI.Exceptions = exception_begin(); 2491 } else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) { 2492 EPI.NoexceptExpr = getNoexceptExpr(); 2493 } 2494 return EPI; 2495 } 2496 2497 /// \brief Get the kind of exception specification on this function. 2498 ExceptionSpecificationType getExceptionSpecType() const { 2499 return static_cast<ExceptionSpecificationType>(ExceptionSpecType); 2500 } 2501 /// \brief Return whether this function has any kind of exception spec. 2502 bool hasExceptionSpec() const { 2503 return getExceptionSpecType() != EST_None; 2504 } 2505 /// \brief Return whether this function has a dynamic (throw) exception spec. 2506 bool hasDynamicExceptionSpec() const { 2507 return isDynamicExceptionSpec(getExceptionSpecType()); 2508 } 2509 /// \brief Return whether this function has a noexcept exception spec. 2510 bool hasNoexceptExceptionSpec() const { 2511 return isNoexceptExceptionSpec(getExceptionSpecType()); 2512 } 2513 /// \brief Result type of getNoexceptSpec(). 2514 enum NoexceptResult { 2515 NR_NoNoexcept, ///< There is no noexcept specifier. 2516 NR_BadNoexcept, ///< The noexcept specifier has a bad expression. 2517 NR_Dependent, ///< The noexcept specifier is dependent. 2518 NR_Throw, ///< The noexcept specifier evaluates to false. 2519 NR_Nothrow ///< The noexcept specifier evaluates to true. 2520 }; 2521 /// \brief Get the meaning of the noexcept spec on this function, if any. 2522 NoexceptResult getNoexceptSpec(ASTContext &Ctx) const; 2523 unsigned getNumExceptions() const { return NumExceptions; } 2524 QualType getExceptionType(unsigned i) const { 2525 assert(i < NumExceptions && "Invalid exception number!"); 2526 return exception_begin()[i]; 2527 } 2528 Expr *getNoexceptExpr() const { 2529 if (getExceptionSpecType() != EST_ComputedNoexcept) 2530 return 0; 2531 // NoexceptExpr sits where the arguments end. 2532 return *reinterpret_cast<Expr *const *>(arg_type_end()); 2533 } 2534 bool isNothrow(ASTContext &Ctx) const { 2535 ExceptionSpecificationType EST = getExceptionSpecType(); 2536 if (EST == EST_DynamicNone || EST == EST_BasicNoexcept) 2537 return true; 2538 if (EST != EST_ComputedNoexcept) 2539 return false; 2540 return getNoexceptSpec(Ctx) == NR_Nothrow; 2541 } 2542 2543 using FunctionType::isVariadic; 2544 2545 /// \brief Determines whether this function prototype contains a 2546 /// parameter pack at the end. 2547 /// 2548 /// A function template whose last parameter is a parameter pack can be 2549 /// called with an arbitrary number of arguments, much like a variadic 2550 /// function. However, 2551 bool isTemplateVariadic() const; 2552 2553 unsigned getTypeQuals() const { return FunctionType::getTypeQuals(); } 2554 2555 2556 /// \brief Retrieve the ref-qualifier associated with this function type. 2557 RefQualifierKind getRefQualifier() const { 2558 return FunctionType::getRefQualifier(); 2559 } 2560 2561 typedef const QualType *arg_type_iterator; 2562 arg_type_iterator arg_type_begin() const { 2563 return reinterpret_cast<const QualType *>(this+1); 2564 } 2565 arg_type_iterator arg_type_end() const { return arg_type_begin()+NumArgs; } 2566 2567 typedef const QualType *exception_iterator; 2568 exception_iterator exception_begin() const { 2569 // exceptions begin where arguments end 2570 return arg_type_end(); 2571 } 2572 exception_iterator exception_end() const { 2573 if (getExceptionSpecType() != EST_Dynamic) 2574 return exception_begin(); 2575 return exception_begin() + NumExceptions; 2576 } 2577 2578 bool isSugared() const { return false; } 2579 QualType desugar() const { return QualType(this, 0); } 2580 2581 static bool classof(const Type *T) { 2582 return T->getTypeClass() == FunctionProto; 2583 } 2584 static bool classof(const FunctionProtoType *) { return true; } 2585 2586 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx); 2587 static void Profile(llvm::FoldingSetNodeID &ID, QualType Result, 2588 arg_type_iterator ArgTys, unsigned NumArgs, 2589 const ExtProtoInfo &EPI, const ASTContext &Context); 2590}; 2591 2592 2593/// \brief Represents the dependent type named by a dependently-scoped 2594/// typename using declaration, e.g. 2595/// using typename Base<T>::foo; 2596/// Template instantiation turns these into the underlying type. 2597class UnresolvedUsingType : public Type { 2598 UnresolvedUsingTypenameDecl *Decl; 2599 2600 UnresolvedUsingType(const UnresolvedUsingTypenameDecl *D) 2601 : Type(UnresolvedUsing, QualType(), true, false, 2602 /*ContainsUnexpandedParameterPack=*/false), 2603 Decl(const_cast<UnresolvedUsingTypenameDecl*>(D)) {} 2604 friend class ASTContext; // ASTContext creates these. 2605public: 2606 2607 UnresolvedUsingTypenameDecl *getDecl() const { return Decl; } 2608 2609 bool isSugared() const { return false; } 2610 QualType desugar() const { return QualType(this, 0); } 2611 2612 static bool classof(const Type *T) { 2613 return T->getTypeClass() == UnresolvedUsing; 2614 } 2615 static bool classof(const UnresolvedUsingType *) { return true; } 2616 2617 void Profile(llvm::FoldingSetNodeID &ID) { 2618 return Profile(ID, Decl); 2619 } 2620 static void Profile(llvm::FoldingSetNodeID &ID, 2621 UnresolvedUsingTypenameDecl *D) { 2622 ID.AddPointer(D); 2623 } 2624}; 2625 2626 2627class TypedefType : public Type { 2628 TypedefDecl *Decl; 2629protected: 2630 TypedefType(TypeClass tc, const TypedefDecl *D, QualType can) 2631 : Type(tc, can, can->isDependentType(), can->isVariablyModifiedType(), 2632 /*ContainsUnexpandedParameterPack=*/false), 2633 Decl(const_cast<TypedefDecl*>(D)) { 2634 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 2635 } 2636 friend class ASTContext; // ASTContext creates these. 2637public: 2638 2639 TypedefDecl *getDecl() const { return Decl; } 2640 2641 bool isSugared() const { return true; } 2642 QualType desugar() const; 2643 2644 static bool classof(const Type *T) { return T->getTypeClass() == Typedef; } 2645 static bool classof(const TypedefType *) { return true; } 2646}; 2647 2648/// TypeOfExprType (GCC extension). 2649class TypeOfExprType : public Type { 2650 Expr *TOExpr; 2651 2652protected: 2653 TypeOfExprType(Expr *E, QualType can = QualType()); 2654 friend class ASTContext; // ASTContext creates these. 2655public: 2656 Expr *getUnderlyingExpr() const { return TOExpr; } 2657 2658 /// \brief Remove a single level of sugar. 2659 QualType desugar() const; 2660 2661 /// \brief Returns whether this type directly provides sugar. 2662 bool isSugared() const { return true; } 2663 2664 static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; } 2665 static bool classof(const TypeOfExprType *) { return true; } 2666}; 2667 2668/// \brief Internal representation of canonical, dependent 2669/// typeof(expr) types. 2670/// 2671/// This class is used internally by the ASTContext to manage 2672/// canonical, dependent types, only. Clients will only see instances 2673/// of this class via TypeOfExprType nodes. 2674class DependentTypeOfExprType 2675 : public TypeOfExprType, public llvm::FoldingSetNode { 2676 const ASTContext &Context; 2677 2678public: 2679 DependentTypeOfExprType(const ASTContext &Context, Expr *E) 2680 : TypeOfExprType(E), Context(Context) { } 2681 2682 bool isSugared() const { return false; } 2683 QualType desugar() const { return QualType(this, 0); } 2684 2685 void Profile(llvm::FoldingSetNodeID &ID) { 2686 Profile(ID, Context, getUnderlyingExpr()); 2687 } 2688 2689 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 2690 Expr *E); 2691}; 2692 2693/// TypeOfType (GCC extension). 2694class TypeOfType : public Type { 2695 QualType TOType; 2696 TypeOfType(QualType T, QualType can) 2697 : Type(TypeOf, can, T->isDependentType(), T->isVariablyModifiedType(), 2698 T->containsUnexpandedParameterPack()), 2699 TOType(T) { 2700 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 2701 } 2702 friend class ASTContext; // ASTContext creates these. 2703public: 2704 QualType getUnderlyingType() const { return TOType; } 2705 2706 /// \brief Remove a single level of sugar. 2707 QualType desugar() const { return getUnderlyingType(); } 2708 2709 /// \brief Returns whether this type directly provides sugar. 2710 bool isSugared() const { return true; } 2711 2712 static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; } 2713 static bool classof(const TypeOfType *) { return true; } 2714}; 2715 2716/// DecltypeType (C++0x) 2717class DecltypeType : public Type { 2718 Expr *E; 2719 2720 // FIXME: We could get rid of UnderlyingType if we wanted to: We would have to 2721 // Move getDesugaredType to ASTContext so that it can call getDecltypeForExpr 2722 // from it. 2723 QualType UnderlyingType; 2724 2725protected: 2726 DecltypeType(Expr *E, QualType underlyingType, QualType can = QualType()); 2727 friend class ASTContext; // ASTContext creates these. 2728public: 2729 Expr *getUnderlyingExpr() const { return E; } 2730 QualType getUnderlyingType() const { return UnderlyingType; } 2731 2732 /// \brief Remove a single level of sugar. 2733 QualType desugar() const { return getUnderlyingType(); } 2734 2735 /// \brief Returns whether this type directly provides sugar. 2736 bool isSugared() const { return !isDependentType(); } 2737 2738 static bool classof(const Type *T) { return T->getTypeClass() == Decltype; } 2739 static bool classof(const DecltypeType *) { return true; } 2740}; 2741 2742/// \brief Internal representation of canonical, dependent 2743/// decltype(expr) types. 2744/// 2745/// This class is used internally by the ASTContext to manage 2746/// canonical, dependent types, only. Clients will only see instances 2747/// of this class via DecltypeType nodes. 2748class DependentDecltypeType : public DecltypeType, public llvm::FoldingSetNode { 2749 const ASTContext &Context; 2750 2751public: 2752 DependentDecltypeType(const ASTContext &Context, Expr *E); 2753 2754 bool isSugared() const { return false; } 2755 QualType desugar() const { return QualType(this, 0); } 2756 2757 void Profile(llvm::FoldingSetNodeID &ID) { 2758 Profile(ID, Context, getUnderlyingExpr()); 2759 } 2760 2761 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 2762 Expr *E); 2763}; 2764 2765class TagType : public Type { 2766 /// Stores the TagDecl associated with this type. The decl may point to any 2767 /// TagDecl that declares the entity. 2768 TagDecl * decl; 2769 2770protected: 2771 TagType(TypeClass TC, const TagDecl *D, QualType can); 2772 2773public: 2774 TagDecl *getDecl() const; 2775 2776 /// @brief Determines whether this type is in the process of being 2777 /// defined. 2778 bool isBeingDefined() const; 2779 2780 static bool classof(const Type *T) { 2781 return T->getTypeClass() >= TagFirst && T->getTypeClass() <= TagLast; 2782 } 2783 static bool classof(const TagType *) { return true; } 2784 static bool classof(const RecordType *) { return true; } 2785 static bool classof(const EnumType *) { return true; } 2786}; 2787 2788/// RecordType - This is a helper class that allows the use of isa/cast/dyncast 2789/// to detect TagType objects of structs/unions/classes. 2790class RecordType : public TagType { 2791protected: 2792 explicit RecordType(const RecordDecl *D) 2793 : TagType(Record, reinterpret_cast<const TagDecl*>(D), QualType()) { } 2794 explicit RecordType(TypeClass TC, RecordDecl *D) 2795 : TagType(TC, reinterpret_cast<const TagDecl*>(D), QualType()) { } 2796 friend class ASTContext; // ASTContext creates these. 2797public: 2798 2799 RecordDecl *getDecl() const { 2800 return reinterpret_cast<RecordDecl*>(TagType::getDecl()); 2801 } 2802 2803 // FIXME: This predicate is a helper to QualType/Type. It needs to 2804 // recursively check all fields for const-ness. If any field is declared 2805 // const, it needs to return false. 2806 bool hasConstFields() const { return false; } 2807 2808 bool isSugared() const { return false; } 2809 QualType desugar() const { return QualType(this, 0); } 2810 2811 static bool classof(const TagType *T); 2812 static bool classof(const Type *T) { 2813 return isa<TagType>(T) && classof(cast<TagType>(T)); 2814 } 2815 static bool classof(const RecordType *) { return true; } 2816}; 2817 2818/// EnumType - This is a helper class that allows the use of isa/cast/dyncast 2819/// to detect TagType objects of enums. 2820class EnumType : public TagType { 2821 explicit EnumType(const EnumDecl *D) 2822 : TagType(Enum, reinterpret_cast<const TagDecl*>(D), QualType()) { } 2823 friend class ASTContext; // ASTContext creates these. 2824public: 2825 2826 EnumDecl *getDecl() const { 2827 return reinterpret_cast<EnumDecl*>(TagType::getDecl()); 2828 } 2829 2830 bool isSugared() const { return false; } 2831 QualType desugar() const { return QualType(this, 0); } 2832 2833 static bool classof(const TagType *T); 2834 static bool classof(const Type *T) { 2835 return isa<TagType>(T) && classof(cast<TagType>(T)); 2836 } 2837 static bool classof(const EnumType *) { return true; } 2838}; 2839 2840/// AttributedType - An attributed type is a type to which a type 2841/// attribute has been applied. The "modified type" is the 2842/// fully-sugared type to which the attributed type was applied; 2843/// generally it is not canonically equivalent to the attributed type. 2844/// The "equivalent type" is the minimally-desugared type which the 2845/// type is canonically equivalent to. 2846/// 2847/// For example, in the following attributed type: 2848/// int32_t __attribute__((vector_size(16))) 2849/// - the modified type is the TypedefType for int32_t 2850/// - the equivalent type is VectorType(16, int32_t) 2851/// - the canonical type is VectorType(16, int) 2852class AttributedType : public Type, public llvm::FoldingSetNode { 2853public: 2854 // It is really silly to have yet another attribute-kind enum, but 2855 // clang::attr::Kind doesn't currently cover the pure type attrs. 2856 enum Kind { 2857 // Expression operand. 2858 attr_address_space, 2859 attr_regparm, 2860 attr_vector_size, 2861 attr_neon_vector_type, 2862 attr_neon_polyvector_type, 2863 2864 FirstExprOperandKind = attr_address_space, 2865 LastExprOperandKind = attr_neon_polyvector_type, 2866 2867 // Enumerated operand (string or keyword). 2868 attr_objc_gc, 2869 attr_pcs, 2870 2871 FirstEnumOperandKind = attr_objc_gc, 2872 LastEnumOperandKind = attr_pcs, 2873 2874 // No operand. 2875 attr_noreturn, 2876 attr_cdecl, 2877 attr_fastcall, 2878 attr_stdcall, 2879 attr_thiscall, 2880 attr_pascal 2881 }; 2882 2883private: 2884 QualType ModifiedType; 2885 QualType EquivalentType; 2886 2887 friend class ASTContext; // creates these 2888 2889 AttributedType(QualType canon, Kind attrKind, 2890 QualType modified, QualType equivalent) 2891 : Type(Attributed, canon, canon->isDependentType(), 2892 canon->isVariablyModifiedType(), 2893 canon->containsUnexpandedParameterPack()), 2894 ModifiedType(modified), EquivalentType(equivalent) { 2895 AttributedTypeBits.AttrKind = attrKind; 2896 } 2897 2898public: 2899 Kind getAttrKind() const { 2900 return static_cast<Kind>(AttributedTypeBits.AttrKind); 2901 } 2902 2903 QualType getModifiedType() const { return ModifiedType; } 2904 QualType getEquivalentType() const { return EquivalentType; } 2905 2906 bool isSugared() const { return true; } 2907 QualType desugar() const { return getEquivalentType(); } 2908 2909 void Profile(llvm::FoldingSetNodeID &ID) { 2910 Profile(ID, getAttrKind(), ModifiedType, EquivalentType); 2911 } 2912 2913 static void Profile(llvm::FoldingSetNodeID &ID, Kind attrKind, 2914 QualType modified, QualType equivalent) { 2915 ID.AddInteger(attrKind); 2916 ID.AddPointer(modified.getAsOpaquePtr()); 2917 ID.AddPointer(equivalent.getAsOpaquePtr()); 2918 } 2919 2920 static bool classof(const Type *T) { 2921 return T->getTypeClass() == Attributed; 2922 } 2923 static bool classof(const AttributedType *T) { return true; } 2924}; 2925 2926class TemplateTypeParmType : public Type, public llvm::FoldingSetNode { 2927 unsigned Depth : 15; 2928 unsigned ParameterPack : 1; 2929 unsigned Index : 16; 2930 IdentifierInfo *Name; 2931 2932 TemplateTypeParmType(unsigned D, unsigned I, bool PP, IdentifierInfo *N, 2933 QualType Canon) 2934 : Type(TemplateTypeParm, Canon, /*Dependent=*/true, 2935 /*VariablyModified=*/false, PP), 2936 Depth(D), ParameterPack(PP), Index(I), Name(N) { } 2937 2938 TemplateTypeParmType(unsigned D, unsigned I, bool PP) 2939 : Type(TemplateTypeParm, QualType(this, 0), /*Dependent=*/true, 2940 /*VariablyModified=*/false, PP), 2941 Depth(D), ParameterPack(PP), Index(I), Name(0) { } 2942 2943 friend class ASTContext; // ASTContext creates these 2944 2945public: 2946 unsigned getDepth() const { return Depth; } 2947 unsigned getIndex() const { return Index; } 2948 bool isParameterPack() const { return ParameterPack; } 2949 IdentifierInfo *getName() const { return Name; } 2950 2951 bool isSugared() const { return false; } 2952 QualType desugar() const { return QualType(this, 0); } 2953 2954 void Profile(llvm::FoldingSetNodeID &ID) { 2955 Profile(ID, Depth, Index, ParameterPack, Name); 2956 } 2957 2958 static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth, 2959 unsigned Index, bool ParameterPack, 2960 IdentifierInfo *Name) { 2961 ID.AddInteger(Depth); 2962 ID.AddInteger(Index); 2963 ID.AddBoolean(ParameterPack); 2964 ID.AddPointer(Name); 2965 } 2966 2967 static bool classof(const Type *T) { 2968 return T->getTypeClass() == TemplateTypeParm; 2969 } 2970 static bool classof(const TemplateTypeParmType *T) { return true; } 2971}; 2972 2973/// \brief Represents the result of substituting a type for a template 2974/// type parameter. 2975/// 2976/// Within an instantiated template, all template type parameters have 2977/// been replaced with these. They are used solely to record that a 2978/// type was originally written as a template type parameter; 2979/// therefore they are never canonical. 2980class SubstTemplateTypeParmType : public Type, public llvm::FoldingSetNode { 2981 // The original type parameter. 2982 const TemplateTypeParmType *Replaced; 2983 2984 SubstTemplateTypeParmType(const TemplateTypeParmType *Param, QualType Canon) 2985 : Type(SubstTemplateTypeParm, Canon, Canon->isDependentType(), 2986 Canon->isVariablyModifiedType(), 2987 Canon->containsUnexpandedParameterPack()), 2988 Replaced(Param) { } 2989 2990 friend class ASTContext; 2991 2992public: 2993 IdentifierInfo *getName() const { return Replaced->getName(); } 2994 2995 /// Gets the template parameter that was substituted for. 2996 const TemplateTypeParmType *getReplacedParameter() const { 2997 return Replaced; 2998 } 2999 3000 /// Gets the type that was substituted for the template 3001 /// parameter. 3002 QualType getReplacementType() const { 3003 return getCanonicalTypeInternal(); 3004 } 3005 3006 bool isSugared() const { return true; } 3007 QualType desugar() const { return getReplacementType(); } 3008 3009 void Profile(llvm::FoldingSetNodeID &ID) { 3010 Profile(ID, getReplacedParameter(), getReplacementType()); 3011 } 3012 static void Profile(llvm::FoldingSetNodeID &ID, 3013 const TemplateTypeParmType *Replaced, 3014 QualType Replacement) { 3015 ID.AddPointer(Replaced); 3016 ID.AddPointer(Replacement.getAsOpaquePtr()); 3017 } 3018 3019 static bool classof(const Type *T) { 3020 return T->getTypeClass() == SubstTemplateTypeParm; 3021 } 3022 static bool classof(const SubstTemplateTypeParmType *T) { return true; } 3023}; 3024 3025/// \brief Represents the result of substituting a set of types for a template 3026/// type parameter pack. 3027/// 3028/// When a pack expansion in the source code contains multiple parameter packs 3029/// and those parameter packs correspond to different levels of template 3030/// parameter lists, this type node is used to represent a template type 3031/// parameter pack from an outer level, which has already had its argument pack 3032/// substituted but that still lives within a pack expansion that itself 3033/// could not be instantiated. When actually performing a substitution into 3034/// that pack expansion (e.g., when all template parameters have corresponding 3035/// arguments), this type will be replaced with the \c SubstTemplateTypeParmType 3036/// at the current pack substitution index. 3037class SubstTemplateTypeParmPackType : public Type, public llvm::FoldingSetNode { 3038 /// \brief The original type parameter. 3039 const TemplateTypeParmType *Replaced; 3040 3041 /// \brief A pointer to the set of template arguments that this 3042 /// parameter pack is instantiated with. 3043 const TemplateArgument *Arguments; 3044 3045 /// \brief The number of template arguments in \c Arguments. 3046 unsigned NumArguments; 3047 3048 SubstTemplateTypeParmPackType(const TemplateTypeParmType *Param, 3049 QualType Canon, 3050 const TemplateArgument &ArgPack); 3051 3052 friend class ASTContext; 3053 3054public: 3055 IdentifierInfo *getName() const { return Replaced->getName(); } 3056 3057 /// Gets the template parameter that was substituted for. 3058 const TemplateTypeParmType *getReplacedParameter() const { 3059 return Replaced; 3060 } 3061 3062 bool isSugared() const { return false; } 3063 QualType desugar() const { return QualType(this, 0); } 3064 3065 TemplateArgument getArgumentPack() const; 3066 3067 void Profile(llvm::FoldingSetNodeID &ID); 3068 static void Profile(llvm::FoldingSetNodeID &ID, 3069 const TemplateTypeParmType *Replaced, 3070 const TemplateArgument &ArgPack); 3071 3072 static bool classof(const Type *T) { 3073 return T->getTypeClass() == SubstTemplateTypeParmPack; 3074 } 3075 static bool classof(const SubstTemplateTypeParmPackType *T) { return true; } 3076}; 3077 3078/// \brief Represents a C++0x auto type. 3079/// 3080/// These types are usually a placeholder for a deduced type. However, within 3081/// templates and before the initializer is attached, there is no deduced type 3082/// and an auto type is type-dependent and canonical. 3083class AutoType : public Type, public llvm::FoldingSetNode { 3084 AutoType(QualType DeducedType) 3085 : Type(Auto, DeducedType.isNull() ? QualType(this, 0) : DeducedType, 3086 /*Dependent=*/DeducedType.isNull(), 3087 /*VariablyModified=*/false, /*ContainsParameterPack=*/false) { 3088 assert((DeducedType.isNull() || !DeducedType->isDependentType()) && 3089 "deduced a dependent type for auto"); 3090 } 3091 3092 friend class ASTContext; // ASTContext creates these 3093 3094public: 3095 bool isSugared() const { return isDeduced(); } 3096 QualType desugar() const { return getCanonicalTypeInternal(); } 3097 3098 QualType getDeducedType() const { 3099 return isDeduced() ? getCanonicalTypeInternal() : QualType(); 3100 } 3101 bool isDeduced() const { 3102 return !isDependentType(); 3103 } 3104 3105 void Profile(llvm::FoldingSetNodeID &ID) { 3106 Profile(ID, getDeducedType()); 3107 } 3108 3109 static void Profile(llvm::FoldingSetNodeID &ID, 3110 QualType Deduced) { 3111 ID.AddPointer(Deduced.getAsOpaquePtr()); 3112 } 3113 3114 static bool classof(const Type *T) { 3115 return T->getTypeClass() == Auto; 3116 } 3117 static bool classof(const AutoType *T) { return true; } 3118}; 3119 3120/// \brief Represents the type of a template specialization as written 3121/// in the source code. 3122/// 3123/// Template specialization types represent the syntactic form of a 3124/// template-id that refers to a type, e.g., @c vector<int>. Some 3125/// template specialization types are syntactic sugar, whose canonical 3126/// type will point to some other type node that represents the 3127/// instantiation or class template specialization. For example, a 3128/// class template specialization type of @c vector<int> will refer to 3129/// a tag type for the instantiation 3130/// @c std::vector<int, std::allocator<int>>. 3131/// 3132/// Other template specialization types, for which the template name 3133/// is dependent, may be canonical types. These types are always 3134/// dependent. 3135class TemplateSpecializationType 3136 : public Type, public llvm::FoldingSetNode { 3137 /// \brief The name of the template being specialized. 3138 TemplateName Template; 3139 3140 /// \brief - The number of template arguments named in this class 3141 /// template specialization. 3142 unsigned NumArgs; 3143 3144 TemplateSpecializationType(TemplateName T, 3145 const TemplateArgument *Args, 3146 unsigned NumArgs, QualType Canon); 3147 3148 friend class ASTContext; // ASTContext creates these 3149 3150public: 3151 /// \brief Determine whether any of the given template arguments are 3152 /// dependent. 3153 static bool anyDependentTemplateArguments(const TemplateArgument *Args, 3154 unsigned NumArgs); 3155 3156 static bool anyDependentTemplateArguments(const TemplateArgumentLoc *Args, 3157 unsigned NumArgs); 3158 3159 static bool anyDependentTemplateArguments(const TemplateArgumentListInfo &); 3160 3161 /// \brief Print a template argument list, including the '<' and '>' 3162 /// enclosing the template arguments. 3163 static std::string PrintTemplateArgumentList(const TemplateArgument *Args, 3164 unsigned NumArgs, 3165 const PrintingPolicy &Policy, 3166 bool SkipBrackets = false); 3167 3168 static std::string PrintTemplateArgumentList(const TemplateArgumentLoc *Args, 3169 unsigned NumArgs, 3170 const PrintingPolicy &Policy); 3171 3172 static std::string PrintTemplateArgumentList(const TemplateArgumentListInfo &, 3173 const PrintingPolicy &Policy); 3174 3175 /// True if this template specialization type matches a current 3176 /// instantiation in the context in which it is found. 3177 bool isCurrentInstantiation() const { 3178 return isa<InjectedClassNameType>(getCanonicalTypeInternal()); 3179 } 3180 3181 typedef const TemplateArgument * iterator; 3182 3183 iterator begin() const { return getArgs(); } 3184 iterator end() const; // defined inline in TemplateBase.h 3185 3186 /// \brief Retrieve the name of the template that we are specializing. 3187 TemplateName getTemplateName() const { return Template; } 3188 3189 /// \brief Retrieve the template arguments. 3190 const TemplateArgument *getArgs() const { 3191 return reinterpret_cast<const TemplateArgument *>(this + 1); 3192 } 3193 3194 /// \brief Retrieve the number of template arguments. 3195 unsigned getNumArgs() const { return NumArgs; } 3196 3197 /// \brief Retrieve a specific template argument as a type. 3198 /// \precondition @c isArgType(Arg) 3199 const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h 3200 3201 bool isSugared() const { 3202 return !isDependentType() || isCurrentInstantiation(); 3203 } 3204 QualType desugar() const { return getCanonicalTypeInternal(); } 3205 3206 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) { 3207 Profile(ID, Template, getArgs(), NumArgs, Ctx); 3208 } 3209 3210 static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T, 3211 const TemplateArgument *Args, 3212 unsigned NumArgs, 3213 const ASTContext &Context); 3214 3215 static bool classof(const Type *T) { 3216 return T->getTypeClass() == TemplateSpecialization; 3217 } 3218 static bool classof(const TemplateSpecializationType *T) { return true; } 3219}; 3220 3221/// \brief The injected class name of a C++ class template or class 3222/// template partial specialization. Used to record that a type was 3223/// spelled with a bare identifier rather than as a template-id; the 3224/// equivalent for non-templated classes is just RecordType. 3225/// 3226/// Injected class name types are always dependent. Template 3227/// instantiation turns these into RecordTypes. 3228/// 3229/// Injected class name types are always canonical. This works 3230/// because it is impossible to compare an injected class name type 3231/// with the corresponding non-injected template type, for the same 3232/// reason that it is impossible to directly compare template 3233/// parameters from different dependent contexts: injected class name 3234/// types can only occur within the scope of a particular templated 3235/// declaration, and within that scope every template specialization 3236/// will canonicalize to the injected class name (when appropriate 3237/// according to the rules of the language). 3238class InjectedClassNameType : public Type { 3239 CXXRecordDecl *Decl; 3240 3241 /// The template specialization which this type represents. 3242 /// For example, in 3243 /// template <class T> class A { ... }; 3244 /// this is A<T>, whereas in 3245 /// template <class X, class Y> class A<B<X,Y> > { ... }; 3246 /// this is A<B<X,Y> >. 3247 /// 3248 /// It is always unqualified, always a template specialization type, 3249 /// and always dependent. 3250 QualType InjectedType; 3251 3252 friend class ASTContext; // ASTContext creates these. 3253 friend class ASTReader; // FIXME: ASTContext::getInjectedClassNameType is not 3254 // currently suitable for AST reading, too much 3255 // interdependencies. 3256 InjectedClassNameType(CXXRecordDecl *D, QualType TST) 3257 : Type(InjectedClassName, QualType(), /*Dependent=*/true, 3258 /*VariablyModified=*/false, 3259 /*ContainsUnexpandedParameterPack=*/false), 3260 Decl(D), InjectedType(TST) { 3261 assert(isa<TemplateSpecializationType>(TST)); 3262 assert(!TST.hasQualifiers()); 3263 assert(TST->isDependentType()); 3264 } 3265 3266public: 3267 QualType getInjectedSpecializationType() const { return InjectedType; } 3268 const TemplateSpecializationType *getInjectedTST() const { 3269 return cast<TemplateSpecializationType>(InjectedType.getTypePtr()); 3270 } 3271 3272 CXXRecordDecl *getDecl() const; 3273 3274 bool isSugared() const { return false; } 3275 QualType desugar() const { return QualType(this, 0); } 3276 3277 static bool classof(const Type *T) { 3278 return T->getTypeClass() == InjectedClassName; 3279 } 3280 static bool classof(const InjectedClassNameType *T) { return true; } 3281}; 3282 3283/// \brief The kind of a tag type. 3284enum TagTypeKind { 3285 /// \brief The "struct" keyword. 3286 TTK_Struct, 3287 /// \brief The "union" keyword. 3288 TTK_Union, 3289 /// \brief The "class" keyword. 3290 TTK_Class, 3291 /// \brief The "enum" keyword. 3292 TTK_Enum 3293}; 3294 3295/// \brief The elaboration keyword that precedes a qualified type name or 3296/// introduces an elaborated-type-specifier. 3297enum ElaboratedTypeKeyword { 3298 /// \brief The "struct" keyword introduces the elaborated-type-specifier. 3299 ETK_Struct, 3300 /// \brief The "union" keyword introduces the elaborated-type-specifier. 3301 ETK_Union, 3302 /// \brief The "class" keyword introduces the elaborated-type-specifier. 3303 ETK_Class, 3304 /// \brief The "enum" keyword introduces the elaborated-type-specifier. 3305 ETK_Enum, 3306 /// \brief The "typename" keyword precedes the qualified type name, e.g., 3307 /// \c typename T::type. 3308 ETK_Typename, 3309 /// \brief No keyword precedes the qualified type name. 3310 ETK_None 3311}; 3312 3313/// A helper class for Type nodes having an ElaboratedTypeKeyword. 3314/// The keyword in stored in the free bits of the base class. 3315/// Also provides a few static helpers for converting and printing 3316/// elaborated type keyword and tag type kind enumerations. 3317class TypeWithKeyword : public Type { 3318protected: 3319 TypeWithKeyword(ElaboratedTypeKeyword Keyword, TypeClass tc, 3320 QualType Canonical, bool Dependent, bool VariablyModified, 3321 bool ContainsUnexpandedParameterPack) 3322 : Type(tc, Canonical, Dependent, VariablyModified, 3323 ContainsUnexpandedParameterPack) { 3324 TypeWithKeywordBits.Keyword = Keyword; 3325 } 3326 3327public: 3328 ElaboratedTypeKeyword getKeyword() const { 3329 return static_cast<ElaboratedTypeKeyword>(TypeWithKeywordBits.Keyword); 3330 } 3331 3332 /// getKeywordForTypeSpec - Converts a type specifier (DeclSpec::TST) 3333 /// into an elaborated type keyword. 3334 static ElaboratedTypeKeyword getKeywordForTypeSpec(unsigned TypeSpec); 3335 3336 /// getTagTypeKindForTypeSpec - Converts a type specifier (DeclSpec::TST) 3337 /// into a tag type kind. It is an error to provide a type specifier 3338 /// which *isn't* a tag kind here. 3339 static TagTypeKind getTagTypeKindForTypeSpec(unsigned TypeSpec); 3340 3341 /// getKeywordForTagDeclKind - Converts a TagTypeKind into an 3342 /// elaborated type keyword. 3343 static ElaboratedTypeKeyword getKeywordForTagTypeKind(TagTypeKind Tag); 3344 3345 /// getTagTypeKindForKeyword - Converts an elaborated type keyword into 3346 // a TagTypeKind. It is an error to provide an elaborated type keyword 3347 /// which *isn't* a tag kind here. 3348 static TagTypeKind getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword); 3349 3350 static bool KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword); 3351 3352 static const char *getKeywordName(ElaboratedTypeKeyword Keyword); 3353 3354 static const char *getTagTypeKindName(TagTypeKind Kind) { 3355 return getKeywordName(getKeywordForTagTypeKind(Kind)); 3356 } 3357 3358 class CannotCastToThisType {}; 3359 static CannotCastToThisType classof(const Type *); 3360}; 3361 3362/// \brief Represents a type that was referred to using an elaborated type 3363/// keyword, e.g., struct S, or via a qualified name, e.g., N::M::type, 3364/// or both. 3365/// 3366/// This type is used to keep track of a type name as written in the 3367/// source code, including tag keywords and any nested-name-specifiers. 3368/// The type itself is always "sugar", used to express what was written 3369/// in the source code but containing no additional semantic information. 3370class ElaboratedType : public TypeWithKeyword, public llvm::FoldingSetNode { 3371 3372 /// \brief The nested name specifier containing the qualifier. 3373 NestedNameSpecifier *NNS; 3374 3375 /// \brief The type that this qualified name refers to. 3376 QualType NamedType; 3377 3378 ElaboratedType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, 3379 QualType NamedType, QualType CanonType) 3380 : TypeWithKeyword(Keyword, Elaborated, CanonType, 3381 NamedType->isDependentType(), 3382 NamedType->isVariablyModifiedType(), 3383 NamedType->containsUnexpandedParameterPack()), 3384 NNS(NNS), NamedType(NamedType) { 3385 assert(!(Keyword == ETK_None && NNS == 0) && 3386 "ElaboratedType cannot have elaborated type keyword " 3387 "and name qualifier both null."); 3388 } 3389 3390 friend class ASTContext; // ASTContext creates these 3391 3392public: 3393 ~ElaboratedType(); 3394 3395 /// \brief Retrieve the qualification on this type. 3396 NestedNameSpecifier *getQualifier() const { return NNS; } 3397 3398 /// \brief Retrieve the type named by the qualified-id. 3399 QualType getNamedType() const { return NamedType; } 3400 3401 /// \brief Remove a single level of sugar. 3402 QualType desugar() const { return getNamedType(); } 3403 3404 /// \brief Returns whether this type directly provides sugar. 3405 bool isSugared() const { return true; } 3406 3407 void Profile(llvm::FoldingSetNodeID &ID) { 3408 Profile(ID, getKeyword(), NNS, NamedType); 3409 } 3410 3411 static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, 3412 NestedNameSpecifier *NNS, QualType NamedType) { 3413 ID.AddInteger(Keyword); 3414 ID.AddPointer(NNS); 3415 NamedType.Profile(ID); 3416 } 3417 3418 static bool classof(const Type *T) { 3419 return T->getTypeClass() == Elaborated; 3420 } 3421 static bool classof(const ElaboratedType *T) { return true; } 3422}; 3423 3424/// \brief Represents a qualified type name for which the type name is 3425/// dependent. 3426/// 3427/// DependentNameType represents a class of dependent types that involve a 3428/// dependent nested-name-specifier (e.g., "T::") followed by a (dependent) 3429/// name of a type. The DependentNameType may start with a "typename" (for a 3430/// typename-specifier), "class", "struct", "union", or "enum" (for a 3431/// dependent elaborated-type-specifier), or nothing (in contexts where we 3432/// know that we must be referring to a type, e.g., in a base class specifier). 3433class DependentNameType : public TypeWithKeyword, public llvm::FoldingSetNode { 3434 3435 /// \brief The nested name specifier containing the qualifier. 3436 NestedNameSpecifier *NNS; 3437 3438 /// \brief The type that this typename specifier refers to. 3439 const IdentifierInfo *Name; 3440 3441 DependentNameType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, 3442 const IdentifierInfo *Name, QualType CanonType) 3443 : TypeWithKeyword(Keyword, DependentName, CanonType, /*Dependent=*/true, 3444 /*VariablyModified=*/false, 3445 NNS->containsUnexpandedParameterPack()), 3446 NNS(NNS), Name(Name) { 3447 assert(NNS->isDependent() && 3448 "DependentNameType requires a dependent nested-name-specifier"); 3449 } 3450 3451 friend class ASTContext; // ASTContext creates these 3452 3453public: 3454 /// \brief Retrieve the qualification on this type. 3455 NestedNameSpecifier *getQualifier() const { return NNS; } 3456 3457 /// \brief Retrieve the type named by the typename specifier as an 3458 /// identifier. 3459 /// 3460 /// This routine will return a non-NULL identifier pointer when the 3461 /// form of the original typename was terminated by an identifier, 3462 /// e.g., "typename T::type". 3463 const IdentifierInfo *getIdentifier() const { 3464 return Name; 3465 } 3466 3467 bool isSugared() const { return false; } 3468 QualType desugar() const { return QualType(this, 0); } 3469 3470 void Profile(llvm::FoldingSetNodeID &ID) { 3471 Profile(ID, getKeyword(), NNS, Name); 3472 } 3473 3474 static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, 3475 NestedNameSpecifier *NNS, const IdentifierInfo *Name) { 3476 ID.AddInteger(Keyword); 3477 ID.AddPointer(NNS); 3478 ID.AddPointer(Name); 3479 } 3480 3481 static bool classof(const Type *T) { 3482 return T->getTypeClass() == DependentName; 3483 } 3484 static bool classof(const DependentNameType *T) { return true; } 3485}; 3486 3487/// DependentTemplateSpecializationType - Represents a template 3488/// specialization type whose template cannot be resolved, e.g. 3489/// A<T>::template B<T> 3490class DependentTemplateSpecializationType : 3491 public TypeWithKeyword, public llvm::FoldingSetNode { 3492 3493 /// \brief The nested name specifier containing the qualifier. 3494 NestedNameSpecifier *NNS; 3495 3496 /// \brief The identifier of the template. 3497 const IdentifierInfo *Name; 3498 3499 /// \brief - The number of template arguments named in this class 3500 /// template specialization. 3501 unsigned NumArgs; 3502 3503 const TemplateArgument *getArgBuffer() const { 3504 return reinterpret_cast<const TemplateArgument*>(this+1); 3505 } 3506 TemplateArgument *getArgBuffer() { 3507 return reinterpret_cast<TemplateArgument*>(this+1); 3508 } 3509 3510 DependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, 3511 NestedNameSpecifier *NNS, 3512 const IdentifierInfo *Name, 3513 unsigned NumArgs, 3514 const TemplateArgument *Args, 3515 QualType Canon); 3516 3517 friend class ASTContext; // ASTContext creates these 3518 3519public: 3520 NestedNameSpecifier *getQualifier() const { return NNS; } 3521 const IdentifierInfo *getIdentifier() const { return Name; } 3522 3523 /// \brief Retrieve the template arguments. 3524 const TemplateArgument *getArgs() const { 3525 return getArgBuffer(); 3526 } 3527 3528 /// \brief Retrieve the number of template arguments. 3529 unsigned getNumArgs() const { return NumArgs; } 3530 3531 const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h 3532 3533 typedef const TemplateArgument * iterator; 3534 iterator begin() const { return getArgs(); } 3535 iterator end() const; // inline in TemplateBase.h 3536 3537 bool isSugared() const { return false; } 3538 QualType desugar() const { return QualType(this, 0); } 3539 3540 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { 3541 Profile(ID, Context, getKeyword(), NNS, Name, NumArgs, getArgs()); 3542 } 3543 3544 static void Profile(llvm::FoldingSetNodeID &ID, 3545 const ASTContext &Context, 3546 ElaboratedTypeKeyword Keyword, 3547 NestedNameSpecifier *Qualifier, 3548 const IdentifierInfo *Name, 3549 unsigned NumArgs, 3550 const TemplateArgument *Args); 3551 3552 static bool classof(const Type *T) { 3553 return T->getTypeClass() == DependentTemplateSpecialization; 3554 } 3555 static bool classof(const DependentTemplateSpecializationType *T) { 3556 return true; 3557 } 3558}; 3559 3560/// \brief Represents a pack expansion of types. 3561/// 3562/// Pack expansions are part of C++0x variadic templates. A pack 3563/// expansion contains a pattern, which itself contains one or more 3564/// "unexpanded" parameter packs. When instantiated, a pack expansion 3565/// produces a series of types, each instantiated from the pattern of 3566/// the expansion, where the Ith instantiation of the pattern uses the 3567/// Ith arguments bound to each of the unexpanded parameter packs. The 3568/// pack expansion is considered to "expand" these unexpanded 3569/// parameter packs. 3570/// 3571/// \code 3572/// template<typename ...Types> struct tuple; 3573/// 3574/// template<typename ...Types> 3575/// struct tuple_of_references { 3576/// typedef tuple<Types&...> type; 3577/// }; 3578/// \endcode 3579/// 3580/// Here, the pack expansion \c Types&... is represented via a 3581/// PackExpansionType whose pattern is Types&. 3582class PackExpansionType : public Type, public llvm::FoldingSetNode { 3583 /// \brief The pattern of the pack expansion. 3584 QualType Pattern; 3585 3586 /// \brief The number of expansions that this pack expansion will 3587 /// generate when substituted (+1), or indicates that 3588 /// 3589 /// This field will only have a non-zero value when some of the parameter 3590 /// packs that occur within the pattern have been substituted but others have 3591 /// not. 3592 unsigned NumExpansions; 3593 3594 PackExpansionType(QualType Pattern, QualType Canon, 3595 llvm::Optional<unsigned> NumExpansions) 3596 : Type(PackExpansion, Canon, /*Dependent=*/true, 3597 /*VariableModified=*/Pattern->isVariablyModifiedType(), 3598 /*ContainsUnexpandedParameterPack=*/false), 3599 Pattern(Pattern), 3600 NumExpansions(NumExpansions? *NumExpansions + 1: 0) { } 3601 3602 friend class ASTContext; // ASTContext creates these 3603 3604public: 3605 /// \brief Retrieve the pattern of this pack expansion, which is the 3606 /// type that will be repeatedly instantiated when instantiating the 3607 /// pack expansion itself. 3608 QualType getPattern() const { return Pattern; } 3609 3610 /// \brief Retrieve the number of expansions that this pack expansion will 3611 /// generate, if known. 3612 llvm::Optional<unsigned> getNumExpansions() const { 3613 if (NumExpansions) 3614 return NumExpansions - 1; 3615 3616 return llvm::Optional<unsigned>(); 3617 } 3618 3619 bool isSugared() const { return false; } 3620 QualType desugar() const { return QualType(this, 0); } 3621 3622 void Profile(llvm::FoldingSetNodeID &ID) { 3623 Profile(ID, getPattern(), getNumExpansions()); 3624 } 3625 3626 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pattern, 3627 llvm::Optional<unsigned> NumExpansions) { 3628 ID.AddPointer(Pattern.getAsOpaquePtr()); 3629 ID.AddBoolean(NumExpansions); 3630 if (NumExpansions) 3631 ID.AddInteger(*NumExpansions); 3632 } 3633 3634 static bool classof(const Type *T) { 3635 return T->getTypeClass() == PackExpansion; 3636 } 3637 static bool classof(const PackExpansionType *T) { 3638 return true; 3639 } 3640}; 3641 3642/// ObjCObjectType - Represents a class type in Objective C. 3643/// Every Objective C type is a combination of a base type and a 3644/// list of protocols. 3645/// 3646/// Given the following declarations: 3647/// @class C; 3648/// @protocol P; 3649/// 3650/// 'C' is an ObjCInterfaceType C. It is sugar for an ObjCObjectType 3651/// with base C and no protocols. 3652/// 3653/// 'C<P>' is an ObjCObjectType with base C and protocol list [P]. 3654/// 3655/// 'id' is a TypedefType which is sugar for an ObjCPointerType whose 3656/// pointee is an ObjCObjectType with base BuiltinType::ObjCIdType 3657/// and no protocols. 3658/// 3659/// 'id<P>' is an ObjCPointerType whose pointee is an ObjCObjecType 3660/// with base BuiltinType::ObjCIdType and protocol list [P]. Eventually 3661/// this should get its own sugar class to better represent the source. 3662class ObjCObjectType : public Type { 3663 // ObjCObjectType.NumProtocols - the number of protocols stored 3664 // after the ObjCObjectPointerType node. 3665 // 3666 // These protocols are those written directly on the type. If 3667 // protocol qualifiers ever become additive, the iterators will need 3668 // to get kindof complicated. 3669 // 3670 // In the canonical object type, these are sorted alphabetically 3671 // and uniqued. 3672 3673 /// Either a BuiltinType or an InterfaceType or sugar for either. 3674 QualType BaseType; 3675 3676 ObjCProtocolDecl * const *getProtocolStorage() const { 3677 return const_cast<ObjCObjectType*>(this)->getProtocolStorage(); 3678 } 3679 3680 ObjCProtocolDecl **getProtocolStorage(); 3681 3682protected: 3683 ObjCObjectType(QualType Canonical, QualType Base, 3684 ObjCProtocolDecl * const *Protocols, unsigned NumProtocols); 3685 3686 enum Nonce_ObjCInterface { Nonce_ObjCInterface }; 3687 ObjCObjectType(enum Nonce_ObjCInterface) 3688 : Type(ObjCInterface, QualType(), false, false, false), 3689 BaseType(QualType(this_(), 0)) { 3690 ObjCObjectTypeBits.NumProtocols = 0; 3691 } 3692 3693public: 3694 /// getBaseType - Gets the base type of this object type. This is 3695 /// always (possibly sugar for) one of: 3696 /// - the 'id' builtin type (as opposed to the 'id' type visible to the 3697 /// user, which is a typedef for an ObjCPointerType) 3698 /// - the 'Class' builtin type (same caveat) 3699 /// - an ObjCObjectType (currently always an ObjCInterfaceType) 3700 QualType getBaseType() const { return BaseType; } 3701 3702 bool isObjCId() const { 3703 return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCId); 3704 } 3705 bool isObjCClass() const { 3706 return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCClass); 3707 } 3708 bool isObjCUnqualifiedId() const { return qual_empty() && isObjCId(); } 3709 bool isObjCUnqualifiedClass() const { return qual_empty() && isObjCClass(); } 3710 bool isObjCUnqualifiedIdOrClass() const { 3711 if (!qual_empty()) return false; 3712 if (const BuiltinType *T = getBaseType()->getAs<BuiltinType>()) 3713 return T->getKind() == BuiltinType::ObjCId || 3714 T->getKind() == BuiltinType::ObjCClass; 3715 return false; 3716 } 3717 bool isObjCQualifiedId() const { return !qual_empty() && isObjCId(); } 3718 bool isObjCQualifiedClass() const { return !qual_empty() && isObjCClass(); } 3719 3720 /// Gets the interface declaration for this object type, if the base type 3721 /// really is an interface. 3722 ObjCInterfaceDecl *getInterface() const; 3723 3724 typedef ObjCProtocolDecl * const *qual_iterator; 3725 3726 qual_iterator qual_begin() const { return getProtocolStorage(); } 3727 qual_iterator qual_end() const { return qual_begin() + getNumProtocols(); } 3728 3729 bool qual_empty() const { return getNumProtocols() == 0; } 3730 3731 /// getNumProtocols - Return the number of qualifying protocols in this 3732 /// interface type, or 0 if there are none. 3733 unsigned getNumProtocols() const { return ObjCObjectTypeBits.NumProtocols; } 3734 3735 /// \brief Fetch a protocol by index. 3736 ObjCProtocolDecl *getProtocol(unsigned I) const { 3737 assert(I < getNumProtocols() && "Out-of-range protocol access"); 3738 return qual_begin()[I]; 3739 } 3740 3741 bool isSugared() const { return false; } 3742 QualType desugar() const { return QualType(this, 0); } 3743 3744 static bool classof(const Type *T) { 3745 return T->getTypeClass() == ObjCObject || 3746 T->getTypeClass() == ObjCInterface; 3747 } 3748 static bool classof(const ObjCObjectType *) { return true; } 3749}; 3750 3751/// ObjCObjectTypeImpl - A class providing a concrete implementation 3752/// of ObjCObjectType, so as to not increase the footprint of 3753/// ObjCInterfaceType. Code outside of ASTContext and the core type 3754/// system should not reference this type. 3755class ObjCObjectTypeImpl : public ObjCObjectType, public llvm::FoldingSetNode { 3756 friend class ASTContext; 3757 3758 // If anyone adds fields here, ObjCObjectType::getProtocolStorage() 3759 // will need to be modified. 3760 3761 ObjCObjectTypeImpl(QualType Canonical, QualType Base, 3762 ObjCProtocolDecl * const *Protocols, 3763 unsigned NumProtocols) 3764 : ObjCObjectType(Canonical, Base, Protocols, NumProtocols) {} 3765 3766public: 3767 void Profile(llvm::FoldingSetNodeID &ID); 3768 static void Profile(llvm::FoldingSetNodeID &ID, 3769 QualType Base, 3770 ObjCProtocolDecl *const *protocols, 3771 unsigned NumProtocols); 3772}; 3773 3774inline ObjCProtocolDecl **ObjCObjectType::getProtocolStorage() { 3775 return reinterpret_cast<ObjCProtocolDecl**>( 3776 static_cast<ObjCObjectTypeImpl*>(this) + 1); 3777} 3778 3779/// ObjCInterfaceType - Interfaces are the core concept in Objective-C for 3780/// object oriented design. They basically correspond to C++ classes. There 3781/// are two kinds of interface types, normal interfaces like "NSString" and 3782/// qualified interfaces, which are qualified with a protocol list like 3783/// "NSString<NSCopyable, NSAmazing>". 3784/// 3785/// ObjCInterfaceType guarantees the following properties when considered 3786/// as a subtype of its superclass, ObjCObjectType: 3787/// - There are no protocol qualifiers. To reinforce this, code which 3788/// tries to invoke the protocol methods via an ObjCInterfaceType will 3789/// fail to compile. 3790/// - It is its own base type. That is, if T is an ObjCInterfaceType*, 3791/// T->getBaseType() == QualType(T, 0). 3792class ObjCInterfaceType : public ObjCObjectType { 3793 ObjCInterfaceDecl *Decl; 3794 3795 ObjCInterfaceType(const ObjCInterfaceDecl *D) 3796 : ObjCObjectType(Nonce_ObjCInterface), 3797 Decl(const_cast<ObjCInterfaceDecl*>(D)) {} 3798 friend class ASTContext; // ASTContext creates these. 3799 3800public: 3801 /// getDecl - Get the declaration of this interface. 3802 ObjCInterfaceDecl *getDecl() const { return Decl; } 3803 3804 bool isSugared() const { return false; } 3805 QualType desugar() const { return QualType(this, 0); } 3806 3807 static bool classof(const Type *T) { 3808 return T->getTypeClass() == ObjCInterface; 3809 } 3810 static bool classof(const ObjCInterfaceType *) { return true; } 3811 3812 // Nonsense to "hide" certain members of ObjCObjectType within this 3813 // class. People asking for protocols on an ObjCInterfaceType are 3814 // not going to get what they want: ObjCInterfaceTypes are 3815 // guaranteed to have no protocols. 3816 enum { 3817 qual_iterator, 3818 qual_begin, 3819 qual_end, 3820 getNumProtocols, 3821 getProtocol 3822 }; 3823}; 3824 3825inline ObjCInterfaceDecl *ObjCObjectType::getInterface() const { 3826 if (const ObjCInterfaceType *T = 3827 getBaseType()->getAs<ObjCInterfaceType>()) 3828 return T->getDecl(); 3829 return 0; 3830} 3831 3832/// ObjCObjectPointerType - Used to represent a pointer to an 3833/// Objective C object. These are constructed from pointer 3834/// declarators when the pointee type is an ObjCObjectType (or sugar 3835/// for one). In addition, the 'id' and 'Class' types are typedefs 3836/// for these, and the protocol-qualified types 'id<P>' and 'Class<P>' 3837/// are translated into these. 3838/// 3839/// Pointers to pointers to Objective C objects are still PointerTypes; 3840/// only the first level of pointer gets it own type implementation. 3841class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode { 3842 QualType PointeeType; 3843 3844 ObjCObjectPointerType(QualType Canonical, QualType Pointee) 3845 : Type(ObjCObjectPointer, Canonical, false, false, false), 3846 PointeeType(Pointee) {} 3847 friend class ASTContext; // ASTContext creates these. 3848 3849public: 3850 /// getPointeeType - Gets the type pointed to by this ObjC pointer. 3851 /// The result will always be an ObjCObjectType or sugar thereof. 3852 QualType getPointeeType() const { return PointeeType; } 3853 3854 /// getObjCObjectType - Gets the type pointed to by this ObjC 3855 /// pointer. This method always returns non-null. 3856 /// 3857 /// This method is equivalent to getPointeeType() except that 3858 /// it discards any typedefs (or other sugar) between this 3859 /// type and the "outermost" object type. So for: 3860 /// @class A; @protocol P; @protocol Q; 3861 /// typedef A<P> AP; 3862 /// typedef A A1; 3863 /// typedef A1<P> A1P; 3864 /// typedef A1P<Q> A1PQ; 3865 /// For 'A*', getObjectType() will return 'A'. 3866 /// For 'A<P>*', getObjectType() will return 'A<P>'. 3867 /// For 'AP*', getObjectType() will return 'A<P>'. 3868 /// For 'A1*', getObjectType() will return 'A'. 3869 /// For 'A1<P>*', getObjectType() will return 'A1<P>'. 3870 /// For 'A1P*', getObjectType() will return 'A1<P>'. 3871 /// For 'A1PQ*', getObjectType() will return 'A1<Q>', because 3872 /// adding protocols to a protocol-qualified base discards the 3873 /// old qualifiers (for now). But if it didn't, getObjectType() 3874 /// would return 'A1P<Q>' (and we'd have to make iterating over 3875 /// qualifiers more complicated). 3876 const ObjCObjectType *getObjectType() const { 3877 return PointeeType->castAs<ObjCObjectType>(); 3878 } 3879 3880 /// getInterfaceType - If this pointer points to an Objective C 3881 /// @interface type, gets the type for that interface. Any protocol 3882 /// qualifiers on the interface are ignored. 3883 /// 3884 /// \return null if the base type for this pointer is 'id' or 'Class' 3885 const ObjCInterfaceType *getInterfaceType() const { 3886 return getObjectType()->getBaseType()->getAs<ObjCInterfaceType>(); 3887 } 3888 3889 /// getInterfaceDecl - If this pointer points to an Objective @interface 3890 /// type, gets the declaration for that interface. 3891 /// 3892 /// \return null if the base type for this pointer is 'id' or 'Class' 3893 ObjCInterfaceDecl *getInterfaceDecl() const { 3894 return getObjectType()->getInterface(); 3895 } 3896 3897 /// isObjCIdType - True if this is equivalent to the 'id' type, i.e. if 3898 /// its object type is the primitive 'id' type with no protocols. 3899 bool isObjCIdType() const { 3900 return getObjectType()->isObjCUnqualifiedId(); 3901 } 3902 3903 /// isObjCClassType - True if this is equivalent to the 'Class' type, 3904 /// i.e. if its object tive is the primitive 'Class' type with no protocols. 3905 bool isObjCClassType() const { 3906 return getObjectType()->isObjCUnqualifiedClass(); 3907 } 3908 3909 /// isObjCQualifiedIdType - True if this is equivalent to 'id<P>' for some 3910 /// non-empty set of protocols. 3911 bool isObjCQualifiedIdType() const { 3912 return getObjectType()->isObjCQualifiedId(); 3913 } 3914 3915 /// isObjCQualifiedClassType - True if this is equivalent to 'Class<P>' for 3916 /// some non-empty set of protocols. 3917 bool isObjCQualifiedClassType() const { 3918 return getObjectType()->isObjCQualifiedClass(); 3919 } 3920 3921 /// An iterator over the qualifiers on the object type. Provided 3922 /// for convenience. This will always iterate over the full set of 3923 /// protocols on a type, not just those provided directly. 3924 typedef ObjCObjectType::qual_iterator qual_iterator; 3925 3926 qual_iterator qual_begin() const { 3927 return getObjectType()->qual_begin(); 3928 } 3929 qual_iterator qual_end() const { 3930 return getObjectType()->qual_end(); 3931 } 3932 bool qual_empty() const { return getObjectType()->qual_empty(); } 3933 3934 /// getNumProtocols - Return the number of qualifying protocols on 3935 /// the object type. 3936 unsigned getNumProtocols() const { 3937 return getObjectType()->getNumProtocols(); 3938 } 3939 3940 /// \brief Retrieve a qualifying protocol by index on the object 3941 /// type. 3942 ObjCProtocolDecl *getProtocol(unsigned I) const { 3943 return getObjectType()->getProtocol(I); 3944 } 3945 3946 bool isSugared() const { return false; } 3947 QualType desugar() const { return QualType(this, 0); } 3948 3949 void Profile(llvm::FoldingSetNodeID &ID) { 3950 Profile(ID, getPointeeType()); 3951 } 3952 static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { 3953 ID.AddPointer(T.getAsOpaquePtr()); 3954 } 3955 static bool classof(const Type *T) { 3956 return T->getTypeClass() == ObjCObjectPointer; 3957 } 3958 static bool classof(const ObjCObjectPointerType *) { return true; } 3959}; 3960 3961/// A qualifier set is used to build a set of qualifiers. 3962class QualifierCollector : public Qualifiers { 3963public: 3964 QualifierCollector(Qualifiers Qs = Qualifiers()) : Qualifiers(Qs) {} 3965 3966 /// Collect any qualifiers on the given type and return an 3967 /// unqualified type. The qualifiers are assumed to be consistent 3968 /// with those already in the type. 3969 const Type *strip(QualType type) { 3970 addFastQualifiers(type.getLocalFastQualifiers()); 3971 if (!type.hasLocalNonFastQualifiers()) 3972 return type.getTypePtrUnsafe(); 3973 3974 const ExtQuals *extQuals = type.getExtQualsUnsafe(); 3975 addConsistentQualifiers(extQuals->getQualifiers()); 3976 return extQuals->getBaseType(); 3977 } 3978 3979 /// Apply the collected qualifiers to the given type. 3980 QualType apply(const ASTContext &Context, QualType QT) const; 3981 3982 /// Apply the collected qualifiers to the given type. 3983 QualType apply(const ASTContext &Context, const Type* T) const; 3984}; 3985 3986 3987// Inline function definitions. 3988 3989inline const Type *QualType::getTypePtr() const { 3990 return getCommonPtr()->BaseType; 3991} 3992 3993inline const Type *QualType::getTypePtrOrNull() const { 3994 return (isNull() ? 0 : getCommonPtr()->BaseType); 3995} 3996 3997inline SplitQualType QualType::split() const { 3998 if (!hasLocalNonFastQualifiers()) 3999 return SplitQualType(getTypePtrUnsafe(), 4000 Qualifiers::fromFastMask(getLocalFastQualifiers())); 4001 4002 const ExtQuals *eq = getExtQualsUnsafe(); 4003 Qualifiers qs = eq->getQualifiers(); 4004 qs.addFastQualifiers(getLocalFastQualifiers()); 4005 return SplitQualType(eq->getBaseType(), qs); 4006} 4007 4008inline Qualifiers QualType::getLocalQualifiers() const { 4009 Qualifiers Quals; 4010 if (hasLocalNonFastQualifiers()) 4011 Quals = getExtQualsUnsafe()->getQualifiers(); 4012 Quals.addFastQualifiers(getLocalFastQualifiers()); 4013 return Quals; 4014} 4015 4016inline Qualifiers QualType::getQualifiers() const { 4017 Qualifiers quals = getCommonPtr()->CanonicalType.getLocalQualifiers(); 4018 quals.addFastQualifiers(getLocalFastQualifiers()); 4019 return quals; 4020} 4021 4022inline unsigned QualType::getCVRQualifiers() const { 4023 unsigned cvr = getCommonPtr()->CanonicalType.getLocalCVRQualifiers(); 4024 cvr |= getLocalCVRQualifiers(); 4025 return cvr; 4026} 4027 4028inline QualType QualType::getCanonicalType() const { 4029 QualType canon = getCommonPtr()->CanonicalType; 4030 return canon.withFastQualifiers(getLocalFastQualifiers()); 4031} 4032 4033inline bool QualType::isCanonical() const { 4034 return getTypePtr()->isCanonicalUnqualified(); 4035} 4036 4037inline bool QualType::isCanonicalAsParam() const { 4038 if (!isCanonical()) return false; 4039 if (hasLocalQualifiers()) return false; 4040 4041 const Type *T = getTypePtr(); 4042 if (T->isVariablyModifiedType() && T->hasSizedVLAType()) 4043 return false; 4044 4045 return !isa<FunctionType>(T) && !isa<ArrayType>(T); 4046} 4047 4048inline bool QualType::isConstQualified() const { 4049 return isLocalConstQualified() || 4050 getCommonPtr()->CanonicalType.isLocalConstQualified(); 4051} 4052 4053inline bool QualType::isRestrictQualified() const { 4054 return isLocalRestrictQualified() || 4055 getCommonPtr()->CanonicalType.isLocalRestrictQualified(); 4056} 4057 4058 4059inline bool QualType::isVolatileQualified() const { 4060 return isLocalVolatileQualified() || 4061 getCommonPtr()->CanonicalType.isLocalVolatileQualified(); 4062} 4063 4064inline bool QualType::hasQualifiers() const { 4065 return hasLocalQualifiers() || 4066 getCommonPtr()->CanonicalType.hasLocalQualifiers(); 4067} 4068 4069inline QualType QualType::getUnqualifiedType() const { 4070 if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) 4071 return QualType(getTypePtr(), 0); 4072 4073 return QualType(getSplitUnqualifiedTypeImpl(*this).first, 0); 4074} 4075 4076inline SplitQualType QualType::getSplitUnqualifiedType() const { 4077 if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) 4078 return split(); 4079 4080 return getSplitUnqualifiedTypeImpl(*this); 4081} 4082 4083inline void QualType::removeLocalConst() { 4084 removeLocalFastQualifiers(Qualifiers::Const); 4085} 4086 4087inline void QualType::removeLocalRestrict() { 4088 removeLocalFastQualifiers(Qualifiers::Restrict); 4089} 4090 4091inline void QualType::removeLocalVolatile() { 4092 removeLocalFastQualifiers(Qualifiers::Volatile); 4093} 4094 4095inline void QualType::removeLocalCVRQualifiers(unsigned Mask) { 4096 assert(!(Mask & ~Qualifiers::CVRMask) && "mask has non-CVR bits"); 4097 assert((int)Qualifiers::CVRMask == (int)Qualifiers::FastMask); 4098 4099 // Fast path: we don't need to touch the slow qualifiers. 4100 removeLocalFastQualifiers(Mask); 4101} 4102 4103/// getAddressSpace - Return the address space of this type. 4104inline unsigned QualType::getAddressSpace() const { 4105 return getQualifiers().getAddressSpace(); 4106} 4107 4108/// getObjCGCAttr - Return the gc attribute of this type. 4109inline Qualifiers::GC QualType::getObjCGCAttr() const { 4110 return getQualifiers().getObjCGCAttr(); 4111} 4112 4113inline FunctionType::ExtInfo getFunctionExtInfo(const Type &t) { 4114 if (const PointerType *PT = t.getAs<PointerType>()) { 4115 if (const FunctionType *FT = PT->getPointeeType()->getAs<FunctionType>()) 4116 return FT->getExtInfo(); 4117 } else if (const FunctionType *FT = t.getAs<FunctionType>()) 4118 return FT->getExtInfo(); 4119 4120 return FunctionType::ExtInfo(); 4121} 4122 4123inline FunctionType::ExtInfo getFunctionExtInfo(QualType t) { 4124 return getFunctionExtInfo(*t); 4125} 4126 4127/// \brief Determine whether this set of qualifiers is a superset of the given 4128/// set of qualifiers. 4129inline bool Qualifiers::isSupersetOf(Qualifiers Other) const { 4130 return Mask != Other.Mask && (Mask | Other.Mask) == Mask; 4131} 4132 4133/// isMoreQualifiedThan - Determine whether this type is more 4134/// qualified than the Other type. For example, "const volatile int" 4135/// is more qualified than "const int", "volatile int", and 4136/// "int". However, it is not more qualified than "const volatile 4137/// int". 4138inline bool QualType::isMoreQualifiedThan(QualType other) const { 4139 Qualifiers myQuals = getQualifiers(); 4140 Qualifiers otherQuals = other.getQualifiers(); 4141 return (myQuals != otherQuals && myQuals.compatiblyIncludes(otherQuals)); 4142} 4143 4144/// isAtLeastAsQualifiedAs - Determine whether this type is at last 4145/// as qualified as the Other type. For example, "const volatile 4146/// int" is at least as qualified as "const int", "volatile int", 4147/// "int", and "const volatile int". 4148inline bool QualType::isAtLeastAsQualifiedAs(QualType other) const { 4149 return getQualifiers().compatiblyIncludes(other.getQualifiers()); 4150} 4151 4152/// getNonReferenceType - If Type is a reference type (e.g., const 4153/// int&), returns the type that the reference refers to ("const 4154/// int"). Otherwise, returns the type itself. This routine is used 4155/// throughout Sema to implement C++ 5p6: 4156/// 4157/// If an expression initially has the type "reference to T" (8.3.2, 4158/// 8.5.3), the type is adjusted to "T" prior to any further 4159/// analysis, the expression designates the object or function 4160/// denoted by the reference, and the expression is an lvalue. 4161inline QualType QualType::getNonReferenceType() const { 4162 if (const ReferenceType *RefType = (*this)->getAs<ReferenceType>()) 4163 return RefType->getPointeeType(); 4164 else 4165 return *this; 4166} 4167 4168inline bool Type::isFunctionType() const { 4169 return isa<FunctionType>(CanonicalType); 4170} 4171inline bool Type::isPointerType() const { 4172 return isa<PointerType>(CanonicalType); 4173} 4174inline bool Type::isAnyPointerType() const { 4175 return isPointerType() || isObjCObjectPointerType(); 4176} 4177inline bool Type::isBlockPointerType() const { 4178 return isa<BlockPointerType>(CanonicalType); 4179} 4180inline bool Type::isReferenceType() const { 4181 return isa<ReferenceType>(CanonicalType); 4182} 4183inline bool Type::isLValueReferenceType() const { 4184 return isa<LValueReferenceType>(CanonicalType); 4185} 4186inline bool Type::isRValueReferenceType() const { 4187 return isa<RValueReferenceType>(CanonicalType); 4188} 4189inline bool Type::isFunctionPointerType() const { 4190 if (const PointerType *T = getAs<PointerType>()) 4191 return T->getPointeeType()->isFunctionType(); 4192 else 4193 return false; 4194} 4195inline bool Type::isMemberPointerType() const { 4196 return isa<MemberPointerType>(CanonicalType); 4197} 4198inline bool Type::isMemberFunctionPointerType() const { 4199 if (const MemberPointerType* T = getAs<MemberPointerType>()) 4200 return T->isMemberFunctionPointer(); 4201 else 4202 return false; 4203} 4204inline bool Type::isMemberDataPointerType() const { 4205 if (const MemberPointerType* T = getAs<MemberPointerType>()) 4206 return T->isMemberDataPointer(); 4207 else 4208 return false; 4209} 4210inline bool Type::isArrayType() const { 4211 return isa<ArrayType>(CanonicalType); 4212} 4213inline bool Type::isConstantArrayType() const { 4214 return isa<ConstantArrayType>(CanonicalType); 4215} 4216inline bool Type::isIncompleteArrayType() const { 4217 return isa<IncompleteArrayType>(CanonicalType); 4218} 4219inline bool Type::isVariableArrayType() const { 4220 return isa<VariableArrayType>(CanonicalType); 4221} 4222inline bool Type::isDependentSizedArrayType() const { 4223 return isa<DependentSizedArrayType>(CanonicalType); 4224} 4225inline bool Type::isBuiltinType() const { 4226 return isa<BuiltinType>(CanonicalType); 4227} 4228inline bool Type::isRecordType() const { 4229 return isa<RecordType>(CanonicalType); 4230} 4231inline bool Type::isEnumeralType() const { 4232 return isa<EnumType>(CanonicalType); 4233} 4234inline bool Type::isAnyComplexType() const { 4235 return isa<ComplexType>(CanonicalType); 4236} 4237inline bool Type::isVectorType() const { 4238 return isa<VectorType>(CanonicalType); 4239} 4240inline bool Type::isExtVectorType() const { 4241 return isa<ExtVectorType>(CanonicalType); 4242} 4243inline bool Type::isObjCObjectPointerType() const { 4244 return isa<ObjCObjectPointerType>(CanonicalType); 4245} 4246inline bool Type::isObjCObjectType() const { 4247 return isa<ObjCObjectType>(CanonicalType); 4248} 4249inline bool Type::isObjCObjectOrInterfaceType() const { 4250 return isa<ObjCInterfaceType>(CanonicalType) || 4251 isa<ObjCObjectType>(CanonicalType); 4252} 4253 4254inline bool Type::isObjCQualifiedIdType() const { 4255 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 4256 return OPT->isObjCQualifiedIdType(); 4257 return false; 4258} 4259inline bool Type::isObjCQualifiedClassType() const { 4260 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 4261 return OPT->isObjCQualifiedClassType(); 4262 return false; 4263} 4264inline bool Type::isObjCIdType() const { 4265 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 4266 return OPT->isObjCIdType(); 4267 return false; 4268} 4269inline bool Type::isObjCClassType() const { 4270 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 4271 return OPT->isObjCClassType(); 4272 return false; 4273} 4274inline bool Type::isObjCSelType() const { 4275 if (const PointerType *OPT = getAs<PointerType>()) 4276 return OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCSel); 4277 return false; 4278} 4279inline bool Type::isObjCBuiltinType() const { 4280 return isObjCIdType() || isObjCClassType() || isObjCSelType(); 4281} 4282inline bool Type::isTemplateTypeParmType() const { 4283 return isa<TemplateTypeParmType>(CanonicalType); 4284} 4285 4286inline bool Type::isSpecificBuiltinType(unsigned K) const { 4287 if (const BuiltinType *BT = getAs<BuiltinType>()) 4288 if (BT->getKind() == (BuiltinType::Kind) K) 4289 return true; 4290 return false; 4291} 4292 4293inline bool Type::isPlaceholderType() const { 4294 if (const BuiltinType *BT = getAs<BuiltinType>()) 4295 return BT->isPlaceholderType(); 4296 return false; 4297} 4298 4299/// \brief Determines whether this is a type for which one can define 4300/// an overloaded operator. 4301inline bool Type::isOverloadableType() const { 4302 return isDependentType() || isRecordType() || isEnumeralType(); 4303} 4304 4305inline bool Type::hasPointerRepresentation() const { 4306 return (isPointerType() || isReferenceType() || isBlockPointerType() || 4307 isObjCObjectPointerType() || isNullPtrType()); 4308} 4309 4310inline bool Type::hasObjCPointerRepresentation() const { 4311 return isObjCObjectPointerType(); 4312} 4313 4314inline const Type *Type::getBaseElementTypeUnsafe() const { 4315 const Type *type = this; 4316 while (const ArrayType *arrayType = type->getAsArrayTypeUnsafe()) 4317 type = arrayType->getElementType().getTypePtr(); 4318 return type; 4319} 4320 4321/// Insertion operator for diagnostics. This allows sending QualType's into a 4322/// diagnostic with <<. 4323inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 4324 QualType T) { 4325 DB.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), 4326 Diagnostic::ak_qualtype); 4327 return DB; 4328} 4329 4330/// Insertion operator for partial diagnostics. This allows sending QualType's 4331/// into a diagnostic with <<. 4332inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD, 4333 QualType T) { 4334 PD.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), 4335 Diagnostic::ak_qualtype); 4336 return PD; 4337} 4338 4339// Helper class template that is used by Type::getAs to ensure that one does 4340// not try to look through a qualified type to get to an array type. 4341template<typename T, 4342 bool isArrayType = (llvm::is_same<T, ArrayType>::value || 4343 llvm::is_base_of<ArrayType, T>::value)> 4344struct ArrayType_cannot_be_used_with_getAs { }; 4345 4346template<typename T> 4347struct ArrayType_cannot_be_used_with_getAs<T, true>; 4348 4349/// Member-template getAs<specific type>'. 4350template <typename T> const T *Type::getAs() const { 4351 ArrayType_cannot_be_used_with_getAs<T> at; 4352 (void)at; 4353 4354 // If this is directly a T type, return it. 4355 if (const T *Ty = dyn_cast<T>(this)) 4356 return Ty; 4357 4358 // If the canonical form of this type isn't the right kind, reject it. 4359 if (!isa<T>(CanonicalType)) 4360 return 0; 4361 4362 // If this is a typedef for the type, strip the typedef off without 4363 // losing all typedef information. 4364 return cast<T>(getUnqualifiedDesugaredType()); 4365} 4366 4367inline const ArrayType *Type::getAsArrayTypeUnsafe() const { 4368 // If this is directly an array type, return it. 4369 if (const ArrayType *arr = dyn_cast<ArrayType>(this)) 4370 return arr; 4371 4372 // If the canonical form of this type isn't the right kind, reject it. 4373 if (!isa<ArrayType>(CanonicalType)) 4374 return 0; 4375 4376 // If this is a typedef for the type, strip the typedef off without 4377 // losing all typedef information. 4378 return cast<ArrayType>(getUnqualifiedDesugaredType()); 4379} 4380 4381template <typename T> const T *Type::castAs() const { 4382 ArrayType_cannot_be_used_with_getAs<T> at; 4383 (void) at; 4384 4385 assert(isa<T>(CanonicalType)); 4386 if (const T *ty = dyn_cast<T>(this)) return ty; 4387 return cast<T>(getUnqualifiedDesugaredType()); 4388} 4389 4390inline const ArrayType *Type::castAsArrayTypeUnsafe() const { 4391 assert(isa<ArrayType>(CanonicalType)); 4392 if (const ArrayType *arr = dyn_cast<ArrayType>(this)) return arr; 4393 return cast<ArrayType>(getUnqualifiedDesugaredType()); 4394} 4395 4396} // end namespace clang 4397 4398#endif 4399