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