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