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