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