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