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