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