Type.h revision 2df1a5819fd98708ff3b4772f3477f6c1a8da59a
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 /// This should really return a const Type, but it's not worth 526 /// changing all the users right now. 527 /// 528 /// This function requires that the type not be NULL. If the type might be 529 /// NULL, use the (slightly less efficient) \c getTypePtrOrNull(). 530 const Type *getTypePtr() const; 531 532 const Type *getTypePtrOrNull() const; 533 534 /// Retrieves a pointer to the name of the base type. 535 const IdentifierInfo *getBaseTypeIdentifier() const; 536 537 /// Divides a QualType into its unqualified type and a set of local 538 /// qualifiers. 539 SplitQualType split() const; 540 541 void *getAsOpaquePtr() const { return Value.getOpaqueValue(); } 542 static QualType getFromOpaquePtr(const void *Ptr) { 543 QualType T; 544 T.Value.setFromOpaqueValue(const_cast<void*>(Ptr)); 545 return T; 546 } 547 548 const Type &operator*() const { 549 return *getTypePtr(); 550 } 551 552 const Type *operator->() const { 553 return getTypePtr(); 554 } 555 556 bool isCanonical() const; 557 bool isCanonicalAsParam() const; 558 559 /// isNull - Return true if this QualType doesn't point to a type yet. 560 bool isNull() const { 561 return Value.getPointer().isNull(); 562 } 563 564 /// \brief Determine whether this particular QualType instance has the 565 /// "const" qualifier set, without looking through typedefs that may have 566 /// added "const" at a different level. 567 bool isLocalConstQualified() const { 568 return (getLocalFastQualifiers() & Qualifiers::Const); 569 } 570 571 /// \brief Determine whether this type is const-qualified. 572 bool isConstQualified() const; 573 574 /// \brief Determine whether this particular QualType instance has the 575 /// "restrict" qualifier set, without looking through typedefs that may have 576 /// added "restrict" at a different level. 577 bool isLocalRestrictQualified() const { 578 return (getLocalFastQualifiers() & Qualifiers::Restrict); 579 } 580 581 /// \brief Determine whether this type is restrict-qualified. 582 bool isRestrictQualified() const; 583 584 /// \brief Determine whether this particular QualType instance has the 585 /// "volatile" qualifier set, without looking through typedefs that may have 586 /// added "volatile" at a different level. 587 bool isLocalVolatileQualified() const { 588 return (getLocalFastQualifiers() & Qualifiers::Volatile); 589 } 590 591 /// \brief Determine whether this type is volatile-qualified. 592 bool isVolatileQualified() const; 593 594 /// \brief Determine whether this particular QualType instance has any 595 /// qualifiers, without looking through any typedefs that might add 596 /// qualifiers at a different level. 597 bool hasLocalQualifiers() const { 598 return getLocalFastQualifiers() || hasLocalNonFastQualifiers(); 599 } 600 601 /// \brief Determine whether this type has any qualifiers. 602 bool hasQualifiers() const; 603 604 /// \brief Determine whether this particular QualType instance has any 605 /// "non-fast" qualifiers, e.g., those that are stored in an ExtQualType 606 /// instance. 607 bool hasLocalNonFastQualifiers() const { 608 return Value.getPointer().is<const ExtQuals*>(); 609 } 610 611 /// \brief Retrieve the set of qualifiers local to this particular QualType 612 /// instance, not including any qualifiers acquired through typedefs or 613 /// other sugar. 614 Qualifiers getLocalQualifiers() const; 615 616 /// \brief Retrieve the set of qualifiers applied to this type. 617 Qualifiers getQualifiers() const; 618 619 /// \brief Retrieve the set of CVR (const-volatile-restrict) qualifiers 620 /// local to this particular QualType instance, not including any qualifiers 621 /// acquired through typedefs or other sugar. 622 unsigned getLocalCVRQualifiers() const { 623 return getLocalFastQualifiers(); 624 } 625 626 /// \brief Retrieve the set of CVR (const-volatile-restrict) qualifiers 627 /// applied to this type. 628 unsigned getCVRQualifiers() const; 629 630 bool isConstant(ASTContext& Ctx) const { 631 return QualType::isConstant(*this, Ctx); 632 } 633 634 /// \brief Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10). 635 bool isPODType(ASTContext &Context) const; 636 637 /// isCXX98PODType() - Return true if this is a POD type according to the 638 /// rules of the C++98 standard, regardless of the current compilation's 639 /// language. 640 bool isCXX98PODType(ASTContext &Context) const; 641 642 /// isCXX11PODType() - Return true if this is a POD type according to the 643 /// more relaxed rules of the C++11 standard, regardless of the current 644 /// compilation's language. 645 /// (C++0x [basic.types]p9) 646 bool isCXX11PODType(ASTContext &Context) const; 647 648 /// isTrivialType - Return true if this is a trivial type 649 /// (C++0x [basic.types]p9) 650 bool isTrivialType(ASTContext &Context) const; 651 652 /// isTriviallyCopyableType - Return true if this is a trivially 653 /// copyable type (C++0x [basic.types]p9) 654 bool isTriviallyCopyableType(ASTContext &Context) const; 655 656 // Don't promise in the API that anything besides 'const' can be 657 // easily added. 658 659 /// addConst - add the specified type qualifier to this QualType. 660 void addConst() { 661 addFastQualifiers(Qualifiers::Const); 662 } 663 QualType withConst() const { 664 return withFastQualifiers(Qualifiers::Const); 665 } 666 667 /// addVolatile - add the specified type qualifier to this QualType. 668 void addVolatile() { 669 addFastQualifiers(Qualifiers::Volatile); 670 } 671 QualType withVolatile() const { 672 return withFastQualifiers(Qualifiers::Volatile); 673 } 674 675 /// Add the restrict qualifier to this QualType. 676 void addRestrict() { 677 addFastQualifiers(Qualifiers::Restrict); 678 } 679 QualType withRestrict() const { 680 return withFastQualifiers(Qualifiers::Restrict); 681 } 682 683 QualType withCVRQualifiers(unsigned CVR) const { 684 return withFastQualifiers(CVR); 685 } 686 687 void addFastQualifiers(unsigned TQs) { 688 assert(!(TQs & ~Qualifiers::FastMask) 689 && "non-fast qualifier bits set in mask!"); 690 Value.setInt(Value.getInt() | TQs); 691 } 692 693 void removeLocalConst(); 694 void removeLocalVolatile(); 695 void removeLocalRestrict(); 696 void removeLocalCVRQualifiers(unsigned Mask); 697 698 void removeLocalFastQualifiers() { Value.setInt(0); } 699 void removeLocalFastQualifiers(unsigned Mask) { 700 assert(!(Mask & ~Qualifiers::FastMask) && "mask has non-fast qualifiers"); 701 Value.setInt(Value.getInt() & ~Mask); 702 } 703 704 // Creates a type with the given qualifiers in addition to any 705 // qualifiers already on this type. 706 QualType withFastQualifiers(unsigned TQs) const { 707 QualType T = *this; 708 T.addFastQualifiers(TQs); 709 return T; 710 } 711 712 // Creates a type with exactly the given fast qualifiers, removing 713 // any existing fast qualifiers. 714 QualType withExactLocalFastQualifiers(unsigned TQs) const { 715 return withoutLocalFastQualifiers().withFastQualifiers(TQs); 716 } 717 718 // Removes fast qualifiers, but leaves any extended qualifiers in place. 719 QualType withoutLocalFastQualifiers() const { 720 QualType T = *this; 721 T.removeLocalFastQualifiers(); 722 return T; 723 } 724 725 QualType getCanonicalType() const; 726 727 /// \brief Return this type with all of the instance-specific qualifiers 728 /// removed, but without removing any qualifiers that may have been applied 729 /// through typedefs. 730 QualType getLocalUnqualifiedType() const { return QualType(getTypePtr(), 0); } 731 732 /// \brief Retrieve the unqualified variant of the given type, 733 /// removing as little sugar as possible. 734 /// 735 /// This routine looks through various kinds of sugar to find the 736 /// least-desugared type that is unqualified. For example, given: 737 /// 738 /// \code 739 /// typedef int Integer; 740 /// typedef const Integer CInteger; 741 /// typedef CInteger DifferenceType; 742 /// \endcode 743 /// 744 /// Executing \c getUnqualifiedType() on the type \c DifferenceType will 745 /// desugar until we hit the type \c Integer, which has no qualifiers on it. 746 /// 747 /// The resulting type might still be qualified if it's an array 748 /// type. To strip qualifiers even from within an array type, use 749 /// ASTContext::getUnqualifiedArrayType. 750 inline QualType getUnqualifiedType() const; 751 752 /// getSplitUnqualifiedType - Retrieve the unqualified variant of the 753 /// given type, removing as little sugar as possible. 754 /// 755 /// Like getUnqualifiedType(), but also returns the set of 756 /// qualifiers that were built up. 757 /// 758 /// The resulting type might still be qualified if it's an array 759 /// type. To strip qualifiers even from within an array type, use 760 /// ASTContext::getUnqualifiedArrayType. 761 inline SplitQualType getSplitUnqualifiedType() const; 762 763 /// \brief Determine whether this type is more qualified than the other 764 /// given type, requiring exact equality for non-CVR qualifiers. 765 bool isMoreQualifiedThan(QualType Other) const; 766 767 /// \brief Determine whether this type is at least as qualified as the other 768 /// given type, requiring exact equality for non-CVR qualifiers. 769 bool isAtLeastAsQualifiedAs(QualType Other) const; 770 771 QualType getNonReferenceType() const; 772 773 /// \brief Determine the type of a (typically non-lvalue) expression with the 774 /// specified result type. 775 /// 776 /// This routine should be used for expressions for which the return type is 777 /// explicitly specified (e.g., in a cast or call) and isn't necessarily 778 /// an lvalue. It removes a top-level reference (since there are no 779 /// expressions of reference type) and deletes top-level cvr-qualifiers 780 /// from non-class types (in C++) or all types (in C). 781 QualType getNonLValueExprType(ASTContext &Context) const; 782 783 /// getDesugaredType - Return the specified type with any "sugar" removed from 784 /// the type. This takes off typedefs, typeof's etc. If the outer level of 785 /// the type is already concrete, it returns it unmodified. This is similar 786 /// to getting the canonical type, but it doesn't remove *all* typedefs. For 787 /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is 788 /// concrete. 789 /// 790 /// Qualifiers are left in place. 791 QualType getDesugaredType(const ASTContext &Context) const { 792 return getDesugaredType(*this, Context); 793 } 794 795 SplitQualType getSplitDesugaredType() const { 796 return getSplitDesugaredType(*this); 797 } 798 799 /// \brief Return the specified type with one level of "sugar" removed from 800 /// the type. 801 /// 802 /// This routine takes off the first typedef, typeof, etc. If the outer level 803 /// of the type is already concrete, it returns it unmodified. 804 QualType getSingleStepDesugaredType(const ASTContext &Context) const { 805 return getSingleStepDesugaredTypeImpl(*this, Context); 806 } 807 808 /// IgnoreParens - Returns the specified type after dropping any 809 /// outer-level parentheses. 810 QualType IgnoreParens() const { 811 if (isa<ParenType>(*this)) 812 return QualType::IgnoreParens(*this); 813 return *this; 814 } 815 816 /// operator==/!= - Indicate whether the specified types and qualifiers are 817 /// identical. 818 friend bool operator==(const QualType &LHS, const QualType &RHS) { 819 return LHS.Value == RHS.Value; 820 } 821 friend bool operator!=(const QualType &LHS, const QualType &RHS) { 822 return LHS.Value != RHS.Value; 823 } 824 std::string getAsString() const { 825 return getAsString(split()); 826 } 827 static std::string getAsString(SplitQualType split) { 828 return getAsString(split.Ty, split.Quals); 829 } 830 static std::string getAsString(const Type *ty, Qualifiers qs); 831 832 std::string getAsString(const PrintingPolicy &Policy) const; 833 834 void print(raw_ostream &OS, const PrintingPolicy &Policy, 835 const Twine &PlaceHolder = Twine()) const { 836 print(split(), OS, Policy, PlaceHolder); 837 } 838 static void print(SplitQualType split, raw_ostream &OS, 839 const PrintingPolicy &policy, const Twine &PlaceHolder) { 840 return print(split.Ty, split.Quals, OS, policy, PlaceHolder); 841 } 842 static void print(const Type *ty, Qualifiers qs, 843 raw_ostream &OS, const PrintingPolicy &policy, 844 const Twine &PlaceHolder); 845 846 void getAsStringInternal(std::string &Str, 847 const PrintingPolicy &Policy) const { 848 return getAsStringInternal(split(), Str, Policy); 849 } 850 static void getAsStringInternal(SplitQualType split, std::string &out, 851 const PrintingPolicy &policy) { 852 return getAsStringInternal(split.Ty, split.Quals, out, policy); 853 } 854 static void getAsStringInternal(const Type *ty, Qualifiers qs, 855 std::string &out, 856 const PrintingPolicy &policy); 857 858 class StreamedQualTypeHelper { 859 const QualType &T; 860 const PrintingPolicy &Policy; 861 const Twine &PlaceHolder; 862 public: 863 StreamedQualTypeHelper(const QualType &T, const PrintingPolicy &Policy, 864 const Twine &PlaceHolder) 865 : T(T), Policy(Policy), PlaceHolder(PlaceHolder) { } 866 867 friend raw_ostream &operator<<(raw_ostream &OS, 868 const StreamedQualTypeHelper &SQT) { 869 SQT.T.print(OS, SQT.Policy, SQT.PlaceHolder); 870 return OS; 871 } 872 }; 873 874 StreamedQualTypeHelper stream(const PrintingPolicy &Policy, 875 const Twine &PlaceHolder = Twine()) const { 876 return StreamedQualTypeHelper(*this, Policy, PlaceHolder); 877 } 878 879 void dump(const char *s) const; 880 void dump() const; 881 882 void Profile(llvm::FoldingSetNodeID &ID) const { 883 ID.AddPointer(getAsOpaquePtr()); 884 } 885 886 /// getAddressSpace - Return the address space of this type. 887 inline unsigned getAddressSpace() const; 888 889 /// getObjCGCAttr - Returns gc attribute of this type. 890 inline Qualifiers::GC getObjCGCAttr() const; 891 892 /// isObjCGCWeak true when Type is objc's weak. 893 bool isObjCGCWeak() const { 894 return getObjCGCAttr() == Qualifiers::Weak; 895 } 896 897 /// isObjCGCStrong true when Type is objc's strong. 898 bool isObjCGCStrong() const { 899 return getObjCGCAttr() == Qualifiers::Strong; 900 } 901 902 /// getObjCLifetime - Returns lifetime attribute of this type. 903 Qualifiers::ObjCLifetime getObjCLifetime() const { 904 return getQualifiers().getObjCLifetime(); 905 } 906 907 bool hasNonTrivialObjCLifetime() const { 908 return getQualifiers().hasNonTrivialObjCLifetime(); 909 } 910 911 bool hasStrongOrWeakObjCLifetime() const { 912 return getQualifiers().hasStrongOrWeakObjCLifetime(); 913 } 914 915 enum DestructionKind { 916 DK_none, 917 DK_cxx_destructor, 918 DK_objc_strong_lifetime, 919 DK_objc_weak_lifetime 920 }; 921 922 /// isDestructedType - nonzero if objects of this type require 923 /// non-trivial work to clean up after. Non-zero because it's 924 /// conceivable that qualifiers (objc_gc(weak)?) could make 925 /// something require destruction. 926 DestructionKind isDestructedType() const { 927 return isDestructedTypeImpl(*this); 928 } 929 930 /// \brief Determine whether expressions of the given type are forbidden 931 /// from being lvalues in C. 932 /// 933 /// The expression types that are forbidden to be lvalues are: 934 /// - 'void', but not qualified void 935 /// - function types 936 /// 937 /// The exact rule here is C99 6.3.2.1: 938 /// An lvalue is an expression with an object type or an incomplete 939 /// type other than void. 940 bool isCForbiddenLValueType() const; 941 942 /// \brief Determine whether this type has trivial copy/move-assignment 943 /// semantics. 944 bool hasTrivialAssignment(ASTContext &Context, bool Copying) const; 945 946private: 947 // These methods are implemented in a separate translation unit; 948 // "static"-ize them to avoid creating temporary QualTypes in the 949 // caller. 950 static bool isConstant(QualType T, ASTContext& Ctx); 951 static QualType getDesugaredType(QualType T, const ASTContext &Context); 952 static SplitQualType getSplitDesugaredType(QualType T); 953 static SplitQualType getSplitUnqualifiedTypeImpl(QualType type); 954 static QualType getSingleStepDesugaredTypeImpl(QualType type, 955 const ASTContext &C); 956 static QualType IgnoreParens(QualType T); 957 static DestructionKind isDestructedTypeImpl(QualType type); 958}; 959 960} // end clang. 961 962namespace llvm { 963/// Implement simplify_type for QualType, so that we can dyn_cast from QualType 964/// to a specific Type class. 965template<> struct simplify_type<const ::clang::QualType> { 966 typedef const ::clang::Type *SimpleType; 967 static SimpleType getSimplifiedValue(const ::clang::QualType &Val) { 968 return Val.getTypePtr(); 969 } 970}; 971template<> struct simplify_type< ::clang::QualType> 972 : public simplify_type<const ::clang::QualType> {}; 973 974// Teach SmallPtrSet that QualType is "basically a pointer". 975template<> 976class PointerLikeTypeTraits<clang::QualType> { 977public: 978 static inline void *getAsVoidPointer(clang::QualType P) { 979 return P.getAsOpaquePtr(); 980 } 981 static inline clang::QualType getFromVoidPointer(void *P) { 982 return clang::QualType::getFromOpaquePtr(P); 983 } 984 // Various qualifiers go in low bits. 985 enum { NumLowBitsAvailable = 0 }; 986}; 987 988} // end namespace llvm 989 990namespace clang { 991 992/// \brief Base class that is common to both the \c ExtQuals and \c Type 993/// classes, which allows \c QualType to access the common fields between the 994/// two. 995/// 996class ExtQualsTypeCommonBase { 997 ExtQualsTypeCommonBase(const Type *baseType, QualType canon) 998 : BaseType(baseType), CanonicalType(canon) {} 999 1000 /// \brief The "base" type of an extended qualifiers type (\c ExtQuals) or 1001 /// a self-referential pointer (for \c Type). 1002 /// 1003 /// This pointer allows an efficient mapping from a QualType to its 1004 /// underlying type pointer. 1005 const Type *const BaseType; 1006 1007 /// \brief The canonical type of this type. A QualType. 1008 QualType CanonicalType; 1009 1010 friend class QualType; 1011 friend class Type; 1012 friend class ExtQuals; 1013}; 1014 1015/// ExtQuals - We can encode up to four bits in the low bits of a 1016/// type pointer, but there are many more type qualifiers that we want 1017/// to be able to apply to an arbitrary type. Therefore we have this 1018/// struct, intended to be heap-allocated and used by QualType to 1019/// store qualifiers. 1020/// 1021/// The current design tags the 'const', 'restrict', and 'volatile' qualifiers 1022/// in three low bits on the QualType pointer; a fourth bit records whether 1023/// the pointer is an ExtQuals node. The extended qualifiers (address spaces, 1024/// Objective-C GC attributes) are much more rare. 1025class ExtQuals : public ExtQualsTypeCommonBase, public llvm::FoldingSetNode { 1026 // NOTE: changing the fast qualifiers should be straightforward as 1027 // long as you don't make 'const' non-fast. 1028 // 1. Qualifiers: 1029 // a) Modify the bitmasks (Qualifiers::TQ and DeclSpec::TQ). 1030 // Fast qualifiers must occupy the low-order bits. 1031 // b) Update Qualifiers::FastWidth and FastMask. 1032 // 2. QualType: 1033 // a) Update is{Volatile,Restrict}Qualified(), defined inline. 1034 // b) Update remove{Volatile,Restrict}, defined near the end of 1035 // this header. 1036 // 3. ASTContext: 1037 // a) Update get{Volatile,Restrict}Type. 1038 1039 /// Quals - the immutable set of qualifiers applied by this 1040 /// node; always contains extended qualifiers. 1041 Qualifiers Quals; 1042 1043 ExtQuals *this_() { return this; } 1044 1045public: 1046 ExtQuals(const Type *baseType, QualType canon, Qualifiers quals) 1047 : ExtQualsTypeCommonBase(baseType, 1048 canon.isNull() ? QualType(this_(), 0) : canon), 1049 Quals(quals) 1050 { 1051 assert(Quals.hasNonFastQualifiers() 1052 && "ExtQuals created with no fast qualifiers"); 1053 assert(!Quals.hasFastQualifiers() 1054 && "ExtQuals created with fast qualifiers"); 1055 } 1056 1057 Qualifiers getQualifiers() const { return Quals; } 1058 1059 bool hasObjCGCAttr() const { return Quals.hasObjCGCAttr(); } 1060 Qualifiers::GC getObjCGCAttr() const { return Quals.getObjCGCAttr(); } 1061 1062 bool hasObjCLifetime() const { return Quals.hasObjCLifetime(); } 1063 Qualifiers::ObjCLifetime getObjCLifetime() const { 1064 return Quals.getObjCLifetime(); 1065 } 1066 1067 bool hasAddressSpace() const { return Quals.hasAddressSpace(); } 1068 unsigned getAddressSpace() const { return Quals.getAddressSpace(); } 1069 1070 const Type *getBaseType() const { return BaseType; } 1071 1072public: 1073 void Profile(llvm::FoldingSetNodeID &ID) const { 1074 Profile(ID, getBaseType(), Quals); 1075 } 1076 static void Profile(llvm::FoldingSetNodeID &ID, 1077 const Type *BaseType, 1078 Qualifiers Quals) { 1079 assert(!Quals.hasFastQualifiers() && "fast qualifiers in ExtQuals hash!"); 1080 ID.AddPointer(BaseType); 1081 Quals.Profile(ID); 1082 } 1083}; 1084 1085/// \brief The kind of C++0x ref-qualifier associated with a function type, 1086/// which determines whether a member function's "this" object can be an 1087/// lvalue, rvalue, or neither. 1088enum RefQualifierKind { 1089 /// \brief No ref-qualifier was provided. 1090 RQ_None = 0, 1091 /// \brief An lvalue ref-qualifier was provided (\c &). 1092 RQ_LValue, 1093 /// \brief An rvalue ref-qualifier was provided (\c &&). 1094 RQ_RValue 1095}; 1096 1097/// Type - This is the base class of the type hierarchy. A central concept 1098/// with types is that each type always has a canonical type. A canonical type 1099/// is the type with any typedef names stripped out of it or the types it 1100/// references. For example, consider: 1101/// 1102/// typedef int foo; 1103/// typedef foo* bar; 1104/// 'int *' 'foo *' 'bar' 1105/// 1106/// There will be a Type object created for 'int'. Since int is canonical, its 1107/// canonicaltype pointer points to itself. There is also a Type for 'foo' (a 1108/// TypedefType). Its CanonicalType pointer points to the 'int' Type. Next 1109/// there is a PointerType that represents 'int*', which, like 'int', is 1110/// canonical. Finally, there is a PointerType type for 'foo*' whose canonical 1111/// type is 'int*', and there is a TypedefType for 'bar', whose canonical type 1112/// is also 'int*'. 1113/// 1114/// Non-canonical types are useful for emitting diagnostics, without losing 1115/// information about typedefs being used. Canonical types are useful for type 1116/// comparisons (they allow by-pointer equality tests) and useful for reasoning 1117/// about whether something has a particular form (e.g. is a function type), 1118/// because they implicitly, recursively, strip all typedefs out of a type. 1119/// 1120/// Types, once created, are immutable. 1121/// 1122class Type : public ExtQualsTypeCommonBase { 1123public: 1124 enum TypeClass { 1125#define TYPE(Class, Base) Class, 1126#define LAST_TYPE(Class) TypeLast = Class, 1127#define ABSTRACT_TYPE(Class, Base) 1128#include "clang/AST/TypeNodes.def" 1129 TagFirst = Record, TagLast = Enum 1130 }; 1131 1132private: 1133 Type(const Type&); // DO NOT IMPLEMENT. 1134 void operator=(const Type&); // DO NOT IMPLEMENT. 1135 1136 /// Bitfields required by the Type class. 1137 class TypeBitfields { 1138 friend class Type; 1139 template <class T> friend class TypePropertyCache; 1140 1141 /// TypeClass bitfield - Enum that specifies what subclass this belongs to. 1142 unsigned TC : 8; 1143 1144 /// Dependent - Whether this type is a dependent type (C++ [temp.dep.type]). 1145 /// Note that this should stay at the end of the ivars for Type so that 1146 /// subclasses can pack their bitfields into the same word. 1147 unsigned Dependent : 1; 1148 1149 /// \brief Whether this type somehow involves a template parameter, even 1150 /// if the resolution of the type does not depend on a template parameter. 1151 unsigned InstantiationDependent : 1; 1152 1153 /// \brief Whether this type is a variably-modified type (C99 6.7.5). 1154 unsigned VariablyModified : 1; 1155 1156 /// \brief Whether this type contains an unexpanded parameter pack 1157 /// (for C++0x variadic templates). 1158 unsigned ContainsUnexpandedParameterPack : 1; 1159 1160 /// \brief Nonzero if the cache (i.e. the bitfields here starting 1161 /// with 'Cache') is valid. If so, then this is a 1162 /// LangOptions::VisibilityMode+1. 1163 mutable unsigned CacheValidAndVisibility : 2; 1164 1165 /// \brief True if the visibility was set explicitly in the source code. 1166 mutable unsigned CachedExplicitVisibility : 1; 1167 1168 /// \brief Linkage of this type. 1169 mutable unsigned CachedLinkage : 2; 1170 1171 /// \brief Whether this type involves and local or unnamed types. 1172 mutable unsigned CachedLocalOrUnnamed : 1; 1173 1174 /// \brief FromAST - Whether this type comes from an AST file. 1175 mutable unsigned FromAST : 1; 1176 1177 bool isCacheValid() const { 1178 return (CacheValidAndVisibility != 0); 1179 } 1180 Visibility getVisibility() const { 1181 assert(isCacheValid() && "getting linkage from invalid cache"); 1182 return static_cast<Visibility>(CacheValidAndVisibility-1); 1183 } 1184 bool isVisibilityExplicit() const { 1185 assert(isCacheValid() && "getting linkage from invalid cache"); 1186 return CachedExplicitVisibility; 1187 } 1188 Linkage getLinkage() const { 1189 assert(isCacheValid() && "getting linkage from invalid cache"); 1190 return static_cast<Linkage>(CachedLinkage); 1191 } 1192 bool hasLocalOrUnnamedType() const { 1193 assert(isCacheValid() && "getting linkage from invalid cache"); 1194 return CachedLocalOrUnnamed; 1195 } 1196 }; 1197 enum { NumTypeBits = 19 }; 1198 1199protected: 1200 // These classes allow subclasses to somewhat cleanly pack bitfields 1201 // into Type. 1202 1203 class ArrayTypeBitfields { 1204 friend class ArrayType; 1205 1206 unsigned : NumTypeBits; 1207 1208 /// IndexTypeQuals - CVR qualifiers from declarations like 1209 /// 'int X[static restrict 4]'. For function parameters only. 1210 unsigned IndexTypeQuals : 3; 1211 1212 /// SizeModifier - storage class qualifiers from declarations like 1213 /// 'int X[static restrict 4]'. For function parameters only. 1214 /// Actually an ArrayType::ArraySizeModifier. 1215 unsigned SizeModifier : 3; 1216 }; 1217 1218 class BuiltinTypeBitfields { 1219 friend class BuiltinType; 1220 1221 unsigned : NumTypeBits; 1222 1223 /// The kind (BuiltinType::Kind) of builtin type this is. 1224 unsigned Kind : 8; 1225 }; 1226 1227 class FunctionTypeBitfields { 1228 friend class FunctionType; 1229 1230 unsigned : NumTypeBits; 1231 1232 /// Extra information which affects how the function is called, like 1233 /// regparm and the calling convention. 1234 unsigned ExtInfo : 8; 1235 1236 /// TypeQuals - Used only by FunctionProtoType, put here to pack with the 1237 /// other bitfields. 1238 /// The qualifiers are part of FunctionProtoType because... 1239 /// 1240 /// C++ 8.3.5p4: The return type, the parameter type list and the 1241 /// cv-qualifier-seq, [...], are part of the function type. 1242 unsigned TypeQuals : 3; 1243 1244 /// \brief The ref-qualifier associated with a \c FunctionProtoType. 1245 /// 1246 /// This is a value of type \c RefQualifierKind. 1247 unsigned RefQualifier : 2; 1248 }; 1249 1250 class ObjCObjectTypeBitfields { 1251 friend class ObjCObjectType; 1252 1253 unsigned : NumTypeBits; 1254 1255 /// NumProtocols - The number of protocols stored directly on this 1256 /// object type. 1257 unsigned NumProtocols : 32 - NumTypeBits; 1258 }; 1259 1260 class ReferenceTypeBitfields { 1261 friend class ReferenceType; 1262 1263 unsigned : NumTypeBits; 1264 1265 /// True if the type was originally spelled with an lvalue sigil. 1266 /// This is never true of rvalue references but can also be false 1267 /// on lvalue references because of C++0x [dcl.typedef]p9, 1268 /// as follows: 1269 /// 1270 /// typedef int &ref; // lvalue, spelled lvalue 1271 /// typedef int &&rvref; // rvalue 1272 /// ref &a; // lvalue, inner ref, spelled lvalue 1273 /// ref &&a; // lvalue, inner ref 1274 /// rvref &a; // lvalue, inner ref, spelled lvalue 1275 /// rvref &&a; // rvalue, inner ref 1276 unsigned SpelledAsLValue : 1; 1277 1278 /// True if the inner type is a reference type. This only happens 1279 /// in non-canonical forms. 1280 unsigned InnerRef : 1; 1281 }; 1282 1283 class TypeWithKeywordBitfields { 1284 friend class TypeWithKeyword; 1285 1286 unsigned : NumTypeBits; 1287 1288 /// An ElaboratedTypeKeyword. 8 bits for efficient access. 1289 unsigned Keyword : 8; 1290 }; 1291 1292 class VectorTypeBitfields { 1293 friend class VectorType; 1294 1295 unsigned : NumTypeBits; 1296 1297 /// VecKind - The kind of vector, either a generic vector type or some 1298 /// target-specific vector type such as for AltiVec or Neon. 1299 unsigned VecKind : 3; 1300 1301 /// NumElements - The number of elements in the vector. 1302 unsigned NumElements : 29 - NumTypeBits; 1303 }; 1304 1305 class AttributedTypeBitfields { 1306 friend class AttributedType; 1307 1308 unsigned : NumTypeBits; 1309 1310 /// AttrKind - an AttributedType::Kind 1311 unsigned AttrKind : 32 - NumTypeBits; 1312 }; 1313 1314 union { 1315 TypeBitfields TypeBits; 1316 ArrayTypeBitfields ArrayTypeBits; 1317 AttributedTypeBitfields AttributedTypeBits; 1318 BuiltinTypeBitfields BuiltinTypeBits; 1319 FunctionTypeBitfields FunctionTypeBits; 1320 ObjCObjectTypeBitfields ObjCObjectTypeBits; 1321 ReferenceTypeBitfields ReferenceTypeBits; 1322 TypeWithKeywordBitfields TypeWithKeywordBits; 1323 VectorTypeBitfields VectorTypeBits; 1324 }; 1325 1326private: 1327 /// \brief Set whether this type comes from an AST file. 1328 void setFromAST(bool V = true) const { 1329 TypeBits.FromAST = V; 1330 } 1331 1332 template <class T> friend class TypePropertyCache; 1333 1334protected: 1335 // silence VC++ warning C4355: 'this' : used in base member initializer list 1336 Type *this_() { return this; } 1337 Type(TypeClass tc, QualType canon, bool Dependent, 1338 bool InstantiationDependent, bool VariablyModified, 1339 bool ContainsUnexpandedParameterPack) 1340 : ExtQualsTypeCommonBase(this, 1341 canon.isNull() ? QualType(this_(), 0) : canon) { 1342 TypeBits.TC = tc; 1343 TypeBits.Dependent = Dependent; 1344 TypeBits.InstantiationDependent = Dependent || InstantiationDependent; 1345 TypeBits.VariablyModified = VariablyModified; 1346 TypeBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack; 1347 TypeBits.CacheValidAndVisibility = 0; 1348 TypeBits.CachedExplicitVisibility = false; 1349 TypeBits.CachedLocalOrUnnamed = false; 1350 TypeBits.CachedLinkage = NoLinkage; 1351 TypeBits.FromAST = false; 1352 } 1353 friend class ASTContext; 1354 1355 void setDependent(bool D = true) { 1356 TypeBits.Dependent = D; 1357 if (D) 1358 TypeBits.InstantiationDependent = true; 1359 } 1360 void setInstantiationDependent(bool D = true) { 1361 TypeBits.InstantiationDependent = D; } 1362 void setVariablyModified(bool VM = true) { TypeBits.VariablyModified = VM; 1363 } 1364 void setContainsUnexpandedParameterPack(bool PP = true) { 1365 TypeBits.ContainsUnexpandedParameterPack = PP; 1366 } 1367 1368public: 1369 TypeClass getTypeClass() const { return static_cast<TypeClass>(TypeBits.TC); } 1370 1371 /// \brief Whether this type comes from an AST file. 1372 bool isFromAST() const { return TypeBits.FromAST; } 1373 1374 /// \brief Whether this type is or contains an unexpanded parameter 1375 /// pack, used to support C++0x variadic templates. 1376 /// 1377 /// A type that contains a parameter pack shall be expanded by the 1378 /// ellipsis operator at some point. For example, the typedef in the 1379 /// following example contains an unexpanded parameter pack 'T': 1380 /// 1381 /// \code 1382 /// template<typename ...T> 1383 /// struct X { 1384 /// typedef T* pointer_types; // ill-formed; T is a parameter pack. 1385 /// }; 1386 /// \endcode 1387 /// 1388 /// Note that this routine does not specify which 1389 bool containsUnexpandedParameterPack() const { 1390 return TypeBits.ContainsUnexpandedParameterPack; 1391 } 1392 1393 /// Determines if this type would be canonical if it had no further 1394 /// qualification. 1395 bool isCanonicalUnqualified() const { 1396 return CanonicalType == QualType(this, 0); 1397 } 1398 1399 /// Pull a single level of sugar off of this locally-unqualified type. 1400 /// Users should generally prefer SplitQualType::getSingleStepDesugaredType() 1401 /// or QualType::getSingleStepDesugaredType(const ASTContext&). 1402 QualType getLocallyUnqualifiedSingleStepDesugaredType() const; 1403 1404 /// Types are partitioned into 3 broad categories (C99 6.2.5p1): 1405 /// object types, function types, and incomplete types. 1406 1407 /// isIncompleteType - Return true if this is an incomplete type. 1408 /// A type that can describe objects, but which lacks information needed to 1409 /// determine its size (e.g. void, or a fwd declared struct). Clients of this 1410 /// routine will need to determine if the size is actually required. 1411 /// 1412 /// \brief Def If non-NULL, and the type refers to some kind of declaration 1413 /// that can be completed (such as a C struct, C++ class, or Objective-C 1414 /// class), will be set to the declaration. 1415 bool isIncompleteType(NamedDecl **Def = 0) const; 1416 1417 /// isIncompleteOrObjectType - Return true if this is an incomplete or object 1418 /// type, in other words, not a function type. 1419 bool isIncompleteOrObjectType() const { 1420 return !isFunctionType(); 1421 } 1422 1423 /// \brief Determine whether this type is an object type. 1424 bool isObjectType() const { 1425 // C++ [basic.types]p8: 1426 // An object type is a (possibly cv-qualified) type that is not a 1427 // function type, not a reference type, and not a void type. 1428 return !isReferenceType() && !isFunctionType() && !isVoidType(); 1429 } 1430 1431 /// isLiteralType - Return true if this is a literal type 1432 /// (C++0x [basic.types]p10) 1433 bool isLiteralType() const; 1434 1435 /// \brief Test if this type is a standard-layout type. 1436 /// (C++0x [basic.type]p9) 1437 bool isStandardLayoutType() const; 1438 1439 /// Helper methods to distinguish type categories. All type predicates 1440 /// operate on the canonical type, ignoring typedefs and qualifiers. 1441 1442 /// isBuiltinType - returns true if the type is a builtin type. 1443 bool isBuiltinType() const; 1444 1445 /// isSpecificBuiltinType - Test for a particular builtin type. 1446 bool isSpecificBuiltinType(unsigned K) const; 1447 1448 /// isPlaceholderType - Test for a type which does not represent an 1449 /// actual type-system type but is instead used as a placeholder for 1450 /// various convenient purposes within Clang. All such types are 1451 /// BuiltinTypes. 1452 bool isPlaceholderType() const; 1453 const BuiltinType *getAsPlaceholderType() const; 1454 1455 /// isSpecificPlaceholderType - Test for a specific placeholder type. 1456 bool isSpecificPlaceholderType(unsigned K) const; 1457 1458 /// isNonOverloadPlaceholderType - Test for a placeholder type 1459 /// other than Overload; see BuiltinType::isNonOverloadPlaceholderType. 1460 bool isNonOverloadPlaceholderType() const; 1461 1462 /// isIntegerType() does *not* include complex integers (a GCC extension). 1463 /// isComplexIntegerType() can be used to test for complex integers. 1464 bool isIntegerType() const; // C99 6.2.5p17 (int, char, bool, enum) 1465 bool isEnumeralType() const; 1466 bool isBooleanType() const; 1467 bool isCharType() const; 1468 bool isWideCharType() const; 1469 bool isChar16Type() const; 1470 bool isChar32Type() const; 1471 bool isAnyCharacterType() const; 1472 bool isIntegralType(ASTContext &Ctx) const; 1473 1474 /// \brief Determine whether this type is an integral or enumeration type. 1475 bool isIntegralOrEnumerationType() const; 1476 /// \brief Determine whether this type is an integral or unscoped enumeration 1477 /// type. 1478 bool isIntegralOrUnscopedEnumerationType() const; 1479 1480 /// Floating point categories. 1481 bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double) 1482 /// isComplexType() does *not* include complex integers (a GCC extension). 1483 /// isComplexIntegerType() can be used to test for complex integers. 1484 bool isComplexType() const; // C99 6.2.5p11 (complex) 1485 bool isAnyComplexType() const; // C99 6.2.5p11 (complex) + Complex Int. 1486 bool isFloatingType() const; // C99 6.2.5p11 (real floating + complex) 1487 bool isHalfType() const; // OpenCL 6.1.1.1, NEON (IEEE 754-2008 half) 1488 bool isRealType() const; // C99 6.2.5p17 (real floating + integer) 1489 bool isArithmeticType() const; // C99 6.2.5p18 (integer + floating) 1490 bool isVoidType() const; // C99 6.2.5p19 1491 bool isDerivedType() const; // C99 6.2.5p20 1492 bool isScalarType() const; // C99 6.2.5p21 (arithmetic + pointers) 1493 bool isAggregateType() const; 1494 bool isFundamentalType() const; 1495 bool isCompoundType() const; 1496 1497 // Type Predicates: Check to see if this type is structurally the specified 1498 // type, ignoring typedefs and qualifiers. 1499 bool isFunctionType() const; 1500 bool isFunctionNoProtoType() const { return getAs<FunctionNoProtoType>(); } 1501 bool isFunctionProtoType() const { return getAs<FunctionProtoType>(); } 1502 bool isPointerType() const; 1503 bool isAnyPointerType() const; // Any C pointer or ObjC object pointer 1504 bool isBlockPointerType() const; 1505 bool isVoidPointerType() const; 1506 bool isReferenceType() const; 1507 bool isLValueReferenceType() const; 1508 bool isRValueReferenceType() const; 1509 bool isFunctionPointerType() const; 1510 bool isMemberPointerType() const; 1511 bool isMemberFunctionPointerType() const; 1512 bool isMemberDataPointerType() const; 1513 bool isArrayType() const; 1514 bool isConstantArrayType() const; 1515 bool isIncompleteArrayType() const; 1516 bool isVariableArrayType() const; 1517 bool isDependentSizedArrayType() const; 1518 bool isRecordType() const; 1519 bool isClassType() const; 1520 bool isStructureType() const; 1521 bool isStructureOrClassType() const; 1522 bool isUnionType() const; 1523 bool isComplexIntegerType() const; // GCC _Complex integer type. 1524 bool isVectorType() const; // GCC vector type. 1525 bool isExtVectorType() const; // Extended vector type. 1526 bool isObjCObjectPointerType() const; // pointer to ObjC object 1527 bool isObjCRetainableType() const; // ObjC object or block pointer 1528 bool isObjCLifetimeType() const; // (array of)* retainable type 1529 bool isObjCIndirectLifetimeType() const; // (pointer to)* lifetime type 1530 bool isObjCNSObjectType() const; // __attribute__((NSObject)) 1531 // FIXME: change this to 'raw' interface type, so we can used 'interface' type 1532 // for the common case. 1533 bool isObjCObjectType() const; // NSString or typeof(*(id)0) 1534 bool isObjCQualifiedInterfaceType() const; // NSString<foo> 1535 bool isObjCQualifiedIdType() const; // id<foo> 1536 bool isObjCQualifiedClassType() const; // Class<foo> 1537 bool isObjCObjectOrInterfaceType() const; 1538 bool isObjCIdType() const; // id 1539 bool isObjCClassType() const; // Class 1540 bool isObjCSelType() const; // Class 1541 bool isObjCBuiltinType() const; // 'id' or 'Class' 1542 bool isObjCARCBridgableType() const; 1543 bool isCARCBridgableType() const; 1544 bool isTemplateTypeParmType() const; // C++ template type parameter 1545 bool isNullPtrType() const; // C++0x nullptr_t 1546 bool isAtomicType() const; // C11 _Atomic() 1547 1548 /// Determines if this type, which must satisfy 1549 /// isObjCLifetimeType(), is implicitly __unsafe_unretained rather 1550 /// than implicitly __strong. 1551 bool isObjCARCImplicitlyUnretainedType() const; 1552 1553 /// Return the implicit lifetime for this type, which must not be dependent. 1554 Qualifiers::ObjCLifetime getObjCARCImplicitLifetime() const; 1555 1556 enum ScalarTypeKind { 1557 STK_CPointer, 1558 STK_BlockPointer, 1559 STK_ObjCObjectPointer, 1560 STK_MemberPointer, 1561 STK_Bool, 1562 STK_Integral, 1563 STK_Floating, 1564 STK_IntegralComplex, 1565 STK_FloatingComplex 1566 }; 1567 /// getScalarTypeKind - Given that this is a scalar type, classify it. 1568 ScalarTypeKind getScalarTypeKind() const; 1569 1570 /// isDependentType - Whether this type is a dependent type, meaning 1571 /// that its definition somehow depends on a template parameter 1572 /// (C++ [temp.dep.type]). 1573 bool isDependentType() const { return TypeBits.Dependent; } 1574 1575 /// \brief Determine whether this type is an instantiation-dependent type, 1576 /// meaning that the type involves a template parameter (even if the 1577 /// definition does not actually depend on the type substituted for that 1578 /// template parameter). 1579 bool isInstantiationDependentType() const { 1580 return TypeBits.InstantiationDependent; 1581 } 1582 1583 /// \brief Whether this type is a variably-modified type (C99 6.7.5). 1584 bool isVariablyModifiedType() const { return TypeBits.VariablyModified; } 1585 1586 /// \brief Whether this type involves a variable-length array type 1587 /// with a definite size. 1588 bool hasSizedVLAType() const; 1589 1590 /// \brief Whether this type is or contains a local or unnamed type. 1591 bool hasUnnamedOrLocalType() const; 1592 1593 bool isOverloadableType() const; 1594 1595 /// \brief Determine wither this type is a C++ elaborated-type-specifier. 1596 bool isElaboratedTypeSpecifier() const; 1597 1598 bool canDecayToPointerType() const; 1599 1600 /// hasPointerRepresentation - Whether this type is represented 1601 /// natively as a pointer; this includes pointers, references, block 1602 /// pointers, and Objective-C interface, qualified id, and qualified 1603 /// interface types, as well as nullptr_t. 1604 bool hasPointerRepresentation() const; 1605 1606 /// hasObjCPointerRepresentation - Whether this type can represent 1607 /// an objective pointer type for the purpose of GC'ability 1608 bool hasObjCPointerRepresentation() const; 1609 1610 /// \brief Determine whether this type has an integer representation 1611 /// of some sort, e.g., it is an integer type or a vector. 1612 bool hasIntegerRepresentation() const; 1613 1614 /// \brief Determine whether this type has an signed integer representation 1615 /// of some sort, e.g., it is an signed integer type or a vector. 1616 bool hasSignedIntegerRepresentation() const; 1617 1618 /// \brief Determine whether this type has an unsigned integer representation 1619 /// of some sort, e.g., it is an unsigned integer type or a vector. 1620 bool hasUnsignedIntegerRepresentation() const; 1621 1622 /// \brief Determine whether this type has a floating-point representation 1623 /// of some sort, e.g., it is a floating-point type or a vector thereof. 1624 bool hasFloatingRepresentation() const; 1625 1626 // Type Checking Functions: Check to see if this type is structurally the 1627 // specified type, ignoring typedefs and qualifiers, and return a pointer to 1628 // the best type we can. 1629 const RecordType *getAsStructureType() const; 1630 /// NOTE: getAs*ArrayType are methods on ASTContext. 1631 const RecordType *getAsUnionType() const; 1632 const ComplexType *getAsComplexIntegerType() const; // GCC complex int type. 1633 // The following is a convenience method that returns an ObjCObjectPointerType 1634 // for object declared using an interface. 1635 const ObjCObjectPointerType *getAsObjCInterfacePointerType() const; 1636 const ObjCObjectPointerType *getAsObjCQualifiedIdType() const; 1637 const ObjCObjectPointerType *getAsObjCQualifiedClassType() const; 1638 const ObjCObjectType *getAsObjCQualifiedInterfaceType() const; 1639 const CXXRecordDecl *getCXXRecordDeclForPointerType() const; 1640 1641 /// \brief Retrieves the CXXRecordDecl that this type refers to, either 1642 /// because the type is a RecordType or because it is the injected-class-name 1643 /// type of a class template or class template partial specialization. 1644 CXXRecordDecl *getAsCXXRecordDecl() const; 1645 1646 /// \brief Get the AutoType whose type will be deduced for a variable with 1647 /// an initializer of this type. This looks through declarators like pointer 1648 /// types, but not through decltype or typedefs. 1649 AutoType *getContainedAutoType() const; 1650 1651 /// Member-template getAs<specific type>'. Look through sugar for 1652 /// an instance of <specific type>. This scheme will eventually 1653 /// replace the specific getAsXXXX methods above. 1654 /// 1655 /// There are some specializations of this member template listed 1656 /// immediately following this class. 1657 template <typename T> const T *getAs() const; 1658 1659 /// A variant of getAs<> for array types which silently discards 1660 /// qualifiers from the outermost type. 1661 const ArrayType *getAsArrayTypeUnsafe() const; 1662 1663 /// Member-template castAs<specific type>. Look through sugar for 1664 /// the underlying instance of <specific type>. 1665 /// 1666 /// This method has the same relationship to getAs<T> as cast<T> has 1667 /// to dyn_cast<T>; which is to say, the underlying type *must* 1668 /// have the intended type, and this method will never return null. 1669 template <typename T> const T *castAs() const; 1670 1671 /// A variant of castAs<> for array type which silently discards 1672 /// qualifiers from the outermost type. 1673 const ArrayType *castAsArrayTypeUnsafe() const; 1674 1675 /// getBaseElementTypeUnsafe - Get the base element type of this 1676 /// type, potentially discarding type qualifiers. This method 1677 /// should never be used when type qualifiers are meaningful. 1678 const Type *getBaseElementTypeUnsafe() const; 1679 1680 /// getArrayElementTypeNoTypeQual - If this is an array type, return the 1681 /// element type of the array, potentially with type qualifiers missing. 1682 /// This method should never be used when type qualifiers are meaningful. 1683 const Type *getArrayElementTypeNoTypeQual() const; 1684 1685 /// getPointeeType - If this is a pointer, ObjC object pointer, or block 1686 /// pointer, this returns the respective pointee. 1687 QualType getPointeeType() const; 1688 1689 /// getUnqualifiedDesugaredType() - Return the specified type with 1690 /// any "sugar" removed from the type, removing any typedefs, 1691 /// typeofs, etc., as well as any qualifiers. 1692 const Type *getUnqualifiedDesugaredType() const; 1693 1694 /// More type predicates useful for type checking/promotion 1695 bool isPromotableIntegerType() const; // C99 6.3.1.1p2 1696 1697 /// isSignedIntegerType - Return true if this is an integer type that is 1698 /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], 1699 /// or an enum decl which has a signed representation. 1700 bool isSignedIntegerType() const; 1701 1702 /// isUnsignedIntegerType - Return true if this is an integer type that is 1703 /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], 1704 /// or an enum decl which has an unsigned representation. 1705 bool isUnsignedIntegerType() const; 1706 1707 /// Determines whether this is an integer type that is signed or an 1708 /// enumeration types whose underlying type is a signed integer type. 1709 bool isSignedIntegerOrEnumerationType() const; 1710 1711 /// Determines whether this is an integer type that is unsigned or an 1712 /// enumeration types whose underlying type is a unsigned integer type. 1713 bool isUnsignedIntegerOrEnumerationType() const; 1714 1715 /// isConstantSizeType - Return true if this is not a variable sized type, 1716 /// according to the rules of C99 6.7.5p3. It is not legal to call this on 1717 /// incomplete types. 1718 bool isConstantSizeType() const; 1719 1720 /// isSpecifierType - Returns true if this type can be represented by some 1721 /// set of type specifiers. 1722 bool isSpecifierType() const; 1723 1724 /// \brief Determine the linkage of this type. 1725 Linkage getLinkage() const; 1726 1727 /// \brief Determine the visibility of this type. 1728 Visibility getVisibility() const; 1729 1730 /// \brief Return true if the visibility was explicitly set is the code. 1731 bool isVisibilityExplicit() const; 1732 1733 /// \brief Determine the linkage and visibility of this type. 1734 std::pair<Linkage,Visibility> getLinkageAndVisibility() const; 1735 1736 /// \brief Note that the linkage is no longer known. 1737 void ClearLinkageCache(); 1738 1739 const char *getTypeClassName() const; 1740 1741 QualType getCanonicalTypeInternal() const { 1742 return CanonicalType; 1743 } 1744 CanQualType getCanonicalTypeUnqualified() const; // in CanonicalType.h 1745 LLVM_ATTRIBUTE_USED void dump() const; 1746 1747 static bool classof(const Type *) { return true; } 1748 1749 friend class ASTReader; 1750 friend class ASTWriter; 1751}; 1752 1753/// \brief This will check for a TypedefType by removing any existing sugar 1754/// until it reaches a TypedefType or a non-sugared type. 1755template <> const TypedefType *Type::getAs() const; 1756 1757// We can do canonical leaf types faster, because we don't have to 1758// worry about preserving child type decoration. 1759#define TYPE(Class, Base) 1760#define LEAF_TYPE(Class) \ 1761template <> inline const Class##Type *Type::getAs() const { \ 1762 return dyn_cast<Class##Type>(CanonicalType); \ 1763} \ 1764template <> inline const Class##Type *Type::castAs() const { \ 1765 return cast<Class##Type>(CanonicalType); \ 1766} 1767#include "clang/AST/TypeNodes.def" 1768 1769 1770/// BuiltinType - This class is used for builtin types like 'int'. Builtin 1771/// types are always canonical and have a literal name field. 1772class BuiltinType : public Type { 1773public: 1774 enum Kind { 1775#define BUILTIN_TYPE(Id, SingletonId) Id, 1776#define LAST_BUILTIN_TYPE(Id) LastKind = Id 1777#include "clang/AST/BuiltinTypes.def" 1778 }; 1779 1780public: 1781 BuiltinType(Kind K) 1782 : Type(Builtin, QualType(), /*Dependent=*/(K == Dependent), 1783 /*InstantiationDependent=*/(K == Dependent), 1784 /*VariablyModified=*/false, 1785 /*Unexpanded paramter pack=*/false) { 1786 BuiltinTypeBits.Kind = K; 1787 } 1788 1789 Kind getKind() const { return static_cast<Kind>(BuiltinTypeBits.Kind); } 1790 StringRef getName(const PrintingPolicy &Policy) const; 1791 const char *getNameAsCString(const PrintingPolicy &Policy) const { 1792 // The StringRef is null-terminated. 1793 StringRef str = getName(Policy); 1794 assert(!str.empty() && str.data()[str.size()] == '\0'); 1795 return str.data(); 1796 } 1797 1798 bool isSugared() const { return false; } 1799 QualType desugar() const { return QualType(this, 0); } 1800 1801 bool isInteger() const { 1802 return getKind() >= Bool && getKind() <= Int128; 1803 } 1804 1805 bool isSignedInteger() const { 1806 return getKind() >= Char_S && getKind() <= Int128; 1807 } 1808 1809 bool isUnsignedInteger() const { 1810 return getKind() >= Bool && getKind() <= UInt128; 1811 } 1812 1813 bool isFloatingPoint() const { 1814 return getKind() >= Half && getKind() <= LongDouble; 1815 } 1816 1817 /// Determines whether the given kind corresponds to a placeholder type. 1818 static bool isPlaceholderTypeKind(Kind K) { 1819 return K >= Overload; 1820 } 1821 1822 /// Determines whether this type is a placeholder type, i.e. a type 1823 /// which cannot appear in arbitrary positions in a fully-formed 1824 /// expression. 1825 bool isPlaceholderType() const { 1826 return isPlaceholderTypeKind(getKind()); 1827 } 1828 1829 /// Determines whether this type is a placeholder type other than 1830 /// Overload. Most placeholder types require only syntactic 1831 /// information about their context in order to be resolved (e.g. 1832 /// whether it is a call expression), which means they can (and 1833 /// should) be resolved in an earlier "phase" of analysis. 1834 /// Overload expressions sometimes pick up further information 1835 /// from their context, like whether the context expects a 1836 /// specific function-pointer type, and so frequently need 1837 /// special treatment. 1838 bool isNonOverloadPlaceholderType() const { 1839 return getKind() > Overload; 1840 } 1841 1842 static bool classof(const Type *T) { return T->getTypeClass() == Builtin; } 1843 static bool classof(const BuiltinType *) { return true; } 1844}; 1845 1846/// ComplexType - C99 6.2.5p11 - Complex values. This supports the C99 complex 1847/// types (_Complex float etc) as well as the GCC integer complex extensions. 1848/// 1849class ComplexType : public Type, public llvm::FoldingSetNode { 1850 QualType ElementType; 1851 ComplexType(QualType Element, QualType CanonicalPtr) : 1852 Type(Complex, CanonicalPtr, Element->isDependentType(), 1853 Element->isInstantiationDependentType(), 1854 Element->isVariablyModifiedType(), 1855 Element->containsUnexpandedParameterPack()), 1856 ElementType(Element) { 1857 } 1858 friend class ASTContext; // ASTContext creates these. 1859 1860public: 1861 QualType getElementType() const { return ElementType; } 1862 1863 bool isSugared() const { return false; } 1864 QualType desugar() const { return QualType(this, 0); } 1865 1866 void Profile(llvm::FoldingSetNodeID &ID) { 1867 Profile(ID, getElementType()); 1868 } 1869 static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) { 1870 ID.AddPointer(Element.getAsOpaquePtr()); 1871 } 1872 1873 static bool classof(const Type *T) { return T->getTypeClass() == Complex; } 1874 static bool classof(const ComplexType *) { return true; } 1875}; 1876 1877/// ParenType - Sugar for parentheses used when specifying types. 1878/// 1879class ParenType : public Type, public llvm::FoldingSetNode { 1880 QualType Inner; 1881 1882 ParenType(QualType InnerType, QualType CanonType) : 1883 Type(Paren, CanonType, InnerType->isDependentType(), 1884 InnerType->isInstantiationDependentType(), 1885 InnerType->isVariablyModifiedType(), 1886 InnerType->containsUnexpandedParameterPack()), 1887 Inner(InnerType) { 1888 } 1889 friend class ASTContext; // ASTContext creates these. 1890 1891public: 1892 1893 QualType getInnerType() const { return Inner; } 1894 1895 bool isSugared() const { return true; } 1896 QualType desugar() const { return getInnerType(); } 1897 1898 void Profile(llvm::FoldingSetNodeID &ID) { 1899 Profile(ID, getInnerType()); 1900 } 1901 static void Profile(llvm::FoldingSetNodeID &ID, QualType Inner) { 1902 Inner.Profile(ID); 1903 } 1904 1905 static bool classof(const Type *T) { return T->getTypeClass() == Paren; } 1906 static bool classof(const ParenType *) { return true; } 1907}; 1908 1909/// PointerType - C99 6.7.5.1 - Pointer Declarators. 1910/// 1911class PointerType : public Type, public llvm::FoldingSetNode { 1912 QualType PointeeType; 1913 1914 PointerType(QualType Pointee, QualType CanonicalPtr) : 1915 Type(Pointer, CanonicalPtr, Pointee->isDependentType(), 1916 Pointee->isInstantiationDependentType(), 1917 Pointee->isVariablyModifiedType(), 1918 Pointee->containsUnexpandedParameterPack()), 1919 PointeeType(Pointee) { 1920 } 1921 friend class ASTContext; // ASTContext creates these. 1922 1923public: 1924 1925 QualType getPointeeType() const { return PointeeType; } 1926 1927 bool isSugared() const { return false; } 1928 QualType desugar() const { return QualType(this, 0); } 1929 1930 void Profile(llvm::FoldingSetNodeID &ID) { 1931 Profile(ID, getPointeeType()); 1932 } 1933 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { 1934 ID.AddPointer(Pointee.getAsOpaquePtr()); 1935 } 1936 1937 static bool classof(const Type *T) { return T->getTypeClass() == Pointer; } 1938 static bool classof(const PointerType *) { return true; } 1939}; 1940 1941/// BlockPointerType - pointer to a block type. 1942/// This type is to represent types syntactically represented as 1943/// "void (^)(int)", etc. Pointee is required to always be a function type. 1944/// 1945class BlockPointerType : public Type, public llvm::FoldingSetNode { 1946 QualType PointeeType; // Block is some kind of pointer type 1947 BlockPointerType(QualType Pointee, QualType CanonicalCls) : 1948 Type(BlockPointer, CanonicalCls, Pointee->isDependentType(), 1949 Pointee->isInstantiationDependentType(), 1950 Pointee->isVariablyModifiedType(), 1951 Pointee->containsUnexpandedParameterPack()), 1952 PointeeType(Pointee) { 1953 } 1954 friend class ASTContext; // ASTContext creates these. 1955 1956public: 1957 1958 // Get the pointee type. Pointee is required to always be a function type. 1959 QualType getPointeeType() const { return PointeeType; } 1960 1961 bool isSugared() const { return false; } 1962 QualType desugar() const { return QualType(this, 0); } 1963 1964 void Profile(llvm::FoldingSetNodeID &ID) { 1965 Profile(ID, getPointeeType()); 1966 } 1967 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { 1968 ID.AddPointer(Pointee.getAsOpaquePtr()); 1969 } 1970 1971 static bool classof(const Type *T) { 1972 return T->getTypeClass() == BlockPointer; 1973 } 1974 static bool classof(const BlockPointerType *) { return true; } 1975}; 1976 1977/// ReferenceType - Base for LValueReferenceType and RValueReferenceType 1978/// 1979class ReferenceType : public Type, public llvm::FoldingSetNode { 1980 QualType PointeeType; 1981 1982protected: 1983 ReferenceType(TypeClass tc, QualType Referencee, QualType CanonicalRef, 1984 bool SpelledAsLValue) : 1985 Type(tc, CanonicalRef, Referencee->isDependentType(), 1986 Referencee->isInstantiationDependentType(), 1987 Referencee->isVariablyModifiedType(), 1988 Referencee->containsUnexpandedParameterPack()), 1989 PointeeType(Referencee) 1990 { 1991 ReferenceTypeBits.SpelledAsLValue = SpelledAsLValue; 1992 ReferenceTypeBits.InnerRef = Referencee->isReferenceType(); 1993 } 1994 1995public: 1996 bool isSpelledAsLValue() const { return ReferenceTypeBits.SpelledAsLValue; } 1997 bool isInnerRef() const { return ReferenceTypeBits.InnerRef; } 1998 1999 QualType getPointeeTypeAsWritten() const { return PointeeType; } 2000 QualType getPointeeType() const { 2001 // FIXME: this might strip inner qualifiers; okay? 2002 const ReferenceType *T = this; 2003 while (T->isInnerRef()) 2004 T = T->PointeeType->castAs<ReferenceType>(); 2005 return T->PointeeType; 2006 } 2007 2008 void Profile(llvm::FoldingSetNodeID &ID) { 2009 Profile(ID, PointeeType, isSpelledAsLValue()); 2010 } 2011 static void Profile(llvm::FoldingSetNodeID &ID, 2012 QualType Referencee, 2013 bool SpelledAsLValue) { 2014 ID.AddPointer(Referencee.getAsOpaquePtr()); 2015 ID.AddBoolean(SpelledAsLValue); 2016 } 2017 2018 static bool classof(const Type *T) { 2019 return T->getTypeClass() == LValueReference || 2020 T->getTypeClass() == RValueReference; 2021 } 2022 static bool classof(const ReferenceType *) { return true; } 2023}; 2024 2025/// LValueReferenceType - C++ [dcl.ref] - Lvalue reference 2026/// 2027class LValueReferenceType : public ReferenceType { 2028 LValueReferenceType(QualType Referencee, QualType CanonicalRef, 2029 bool SpelledAsLValue) : 2030 ReferenceType(LValueReference, Referencee, CanonicalRef, SpelledAsLValue) 2031 {} 2032 friend class ASTContext; // ASTContext creates these 2033public: 2034 bool isSugared() const { return false; } 2035 QualType desugar() const { return QualType(this, 0); } 2036 2037 static bool classof(const Type *T) { 2038 return T->getTypeClass() == LValueReference; 2039 } 2040 static bool classof(const LValueReferenceType *) { return true; } 2041}; 2042 2043/// RValueReferenceType - C++0x [dcl.ref] - Rvalue reference 2044/// 2045class RValueReferenceType : public ReferenceType { 2046 RValueReferenceType(QualType Referencee, QualType CanonicalRef) : 2047 ReferenceType(RValueReference, Referencee, CanonicalRef, false) { 2048 } 2049 friend class ASTContext; // ASTContext creates these 2050public: 2051 bool isSugared() const { return false; } 2052 QualType desugar() const { return QualType(this, 0); } 2053 2054 static bool classof(const Type *T) { 2055 return T->getTypeClass() == RValueReference; 2056 } 2057 static bool classof(const RValueReferenceType *) { return true; } 2058}; 2059 2060/// MemberPointerType - C++ 8.3.3 - Pointers to members 2061/// 2062class MemberPointerType : public Type, public llvm::FoldingSetNode { 2063 QualType PointeeType; 2064 /// The class of which the pointee is a member. Must ultimately be a 2065 /// RecordType, but could be a typedef or a template parameter too. 2066 const Type *Class; 2067 2068 MemberPointerType(QualType Pointee, const Type *Cls, QualType CanonicalPtr) : 2069 Type(MemberPointer, CanonicalPtr, 2070 Cls->isDependentType() || Pointee->isDependentType(), 2071 (Cls->isInstantiationDependentType() || 2072 Pointee->isInstantiationDependentType()), 2073 Pointee->isVariablyModifiedType(), 2074 (Cls->containsUnexpandedParameterPack() || 2075 Pointee->containsUnexpandedParameterPack())), 2076 PointeeType(Pointee), Class(Cls) { 2077 } 2078 friend class ASTContext; // ASTContext creates these. 2079 2080public: 2081 QualType getPointeeType() const { return PointeeType; } 2082 2083 /// Returns true if the member type (i.e. the pointee type) is a 2084 /// function type rather than a data-member type. 2085 bool isMemberFunctionPointer() const { 2086 return PointeeType->isFunctionProtoType(); 2087 } 2088 2089 /// Returns true if the member type (i.e. the pointee type) is a 2090 /// data type rather than a function type. 2091 bool isMemberDataPointer() const { 2092 return !PointeeType->isFunctionProtoType(); 2093 } 2094 2095 const Type *getClass() const { return Class; } 2096 2097 bool isSugared() const { return false; } 2098 QualType desugar() const { return QualType(this, 0); } 2099 2100 void Profile(llvm::FoldingSetNodeID &ID) { 2101 Profile(ID, getPointeeType(), getClass()); 2102 } 2103 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee, 2104 const Type *Class) { 2105 ID.AddPointer(Pointee.getAsOpaquePtr()); 2106 ID.AddPointer(Class); 2107 } 2108 2109 static bool classof(const Type *T) { 2110 return T->getTypeClass() == MemberPointer; 2111 } 2112 static bool classof(const MemberPointerType *) { return true; } 2113}; 2114 2115/// ArrayType - C99 6.7.5.2 - Array Declarators. 2116/// 2117class ArrayType : public Type, public llvm::FoldingSetNode { 2118public: 2119 /// ArraySizeModifier - Capture whether this is a normal array (e.g. int X[4]) 2120 /// an array with a static size (e.g. int X[static 4]), or an array 2121 /// with a star size (e.g. int X[*]). 2122 /// 'static' is only allowed on function parameters. 2123 enum ArraySizeModifier { 2124 Normal, Static, Star 2125 }; 2126private: 2127 /// ElementType - The element type of the array. 2128 QualType ElementType; 2129 2130protected: 2131 // C++ [temp.dep.type]p1: 2132 // A type is dependent if it is... 2133 // - an array type constructed from any dependent type or whose 2134 // size is specified by a constant expression that is 2135 // value-dependent, 2136 ArrayType(TypeClass tc, QualType et, QualType can, 2137 ArraySizeModifier sm, unsigned tq, 2138 bool ContainsUnexpandedParameterPack) 2139 : Type(tc, can, et->isDependentType() || tc == DependentSizedArray, 2140 et->isInstantiationDependentType() || tc == DependentSizedArray, 2141 (tc == VariableArray || et->isVariablyModifiedType()), 2142 ContainsUnexpandedParameterPack), 2143 ElementType(et) { 2144 ArrayTypeBits.IndexTypeQuals = tq; 2145 ArrayTypeBits.SizeModifier = sm; 2146 } 2147 2148 friend class ASTContext; // ASTContext creates these. 2149 2150public: 2151 QualType getElementType() const { return ElementType; } 2152 ArraySizeModifier getSizeModifier() const { 2153 return ArraySizeModifier(ArrayTypeBits.SizeModifier); 2154 } 2155 Qualifiers getIndexTypeQualifiers() const { 2156 return Qualifiers::fromCVRMask(getIndexTypeCVRQualifiers()); 2157 } 2158 unsigned getIndexTypeCVRQualifiers() const { 2159 return ArrayTypeBits.IndexTypeQuals; 2160 } 2161 2162 static bool classof(const Type *T) { 2163 return T->getTypeClass() == ConstantArray || 2164 T->getTypeClass() == VariableArray || 2165 T->getTypeClass() == IncompleteArray || 2166 T->getTypeClass() == DependentSizedArray; 2167 } 2168 static bool classof(const ArrayType *) { return true; } 2169}; 2170 2171/// ConstantArrayType - This class represents the canonical version of 2172/// C arrays with a specified constant size. For example, the canonical 2173/// type for 'int A[4 + 4*100]' is a ConstantArrayType where the element 2174/// type is 'int' and the size is 404. 2175class ConstantArrayType : public ArrayType { 2176 llvm::APInt Size; // Allows us to unique the type. 2177 2178 ConstantArrayType(QualType et, QualType can, const llvm::APInt &size, 2179 ArraySizeModifier sm, unsigned tq) 2180 : ArrayType(ConstantArray, et, can, sm, tq, 2181 et->containsUnexpandedParameterPack()), 2182 Size(size) {} 2183protected: 2184 ConstantArrayType(TypeClass tc, QualType et, QualType can, 2185 const llvm::APInt &size, ArraySizeModifier sm, unsigned tq) 2186 : ArrayType(tc, et, can, sm, tq, et->containsUnexpandedParameterPack()), 2187 Size(size) {} 2188 friend class ASTContext; // ASTContext creates these. 2189public: 2190 const llvm::APInt &getSize() const { return Size; } 2191 bool isSugared() const { return false; } 2192 QualType desugar() const { return QualType(this, 0); } 2193 2194 2195 /// \brief Determine the number of bits required to address a member of 2196 // an array with the given element type and number of elements. 2197 static unsigned getNumAddressingBits(ASTContext &Context, 2198 QualType ElementType, 2199 const llvm::APInt &NumElements); 2200 2201 /// \brief Determine the maximum number of active bits that an array's size 2202 /// can require, which limits the maximum size of the array. 2203 static unsigned getMaxSizeBits(ASTContext &Context); 2204 2205 void Profile(llvm::FoldingSetNodeID &ID) { 2206 Profile(ID, getElementType(), getSize(), 2207 getSizeModifier(), getIndexTypeCVRQualifiers()); 2208 } 2209 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, 2210 const llvm::APInt &ArraySize, ArraySizeModifier SizeMod, 2211 unsigned TypeQuals) { 2212 ID.AddPointer(ET.getAsOpaquePtr()); 2213 ID.AddInteger(ArraySize.getZExtValue()); 2214 ID.AddInteger(SizeMod); 2215 ID.AddInteger(TypeQuals); 2216 } 2217 static bool classof(const Type *T) { 2218 return T->getTypeClass() == ConstantArray; 2219 } 2220 static bool classof(const ConstantArrayType *) { return true; } 2221}; 2222 2223/// IncompleteArrayType - This class represents C arrays with an unspecified 2224/// size. For example 'int A[]' has an IncompleteArrayType where the element 2225/// type is 'int' and the size is unspecified. 2226class IncompleteArrayType : public ArrayType { 2227 2228 IncompleteArrayType(QualType et, QualType can, 2229 ArraySizeModifier sm, unsigned tq) 2230 : ArrayType(IncompleteArray, et, can, sm, tq, 2231 et->containsUnexpandedParameterPack()) {} 2232 friend class ASTContext; // ASTContext creates these. 2233public: 2234 bool isSugared() const { return false; } 2235 QualType desugar() const { return QualType(this, 0); } 2236 2237 static bool classof(const Type *T) { 2238 return T->getTypeClass() == IncompleteArray; 2239 } 2240 static bool classof(const IncompleteArrayType *) { return true; } 2241 2242 friend class StmtIteratorBase; 2243 2244 void Profile(llvm::FoldingSetNodeID &ID) { 2245 Profile(ID, getElementType(), getSizeModifier(), 2246 getIndexTypeCVRQualifiers()); 2247 } 2248 2249 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, 2250 ArraySizeModifier SizeMod, unsigned TypeQuals) { 2251 ID.AddPointer(ET.getAsOpaquePtr()); 2252 ID.AddInteger(SizeMod); 2253 ID.AddInteger(TypeQuals); 2254 } 2255}; 2256 2257/// VariableArrayType - This class represents C arrays with a specified size 2258/// which is not an integer-constant-expression. For example, 'int s[x+foo()]'. 2259/// Since the size expression is an arbitrary expression, we store it as such. 2260/// 2261/// Note: VariableArrayType's aren't uniqued (since the expressions aren't) and 2262/// should not be: two lexically equivalent variable array types could mean 2263/// different things, for example, these variables do not have the same type 2264/// dynamically: 2265/// 2266/// void foo(int x) { 2267/// int Y[x]; 2268/// ++x; 2269/// int Z[x]; 2270/// } 2271/// 2272class VariableArrayType : public ArrayType { 2273 /// SizeExpr - An assignment expression. VLA's are only permitted within 2274 /// a function block. 2275 Stmt *SizeExpr; 2276 /// Brackets - The left and right array brackets. 2277 SourceRange Brackets; 2278 2279 VariableArrayType(QualType et, QualType can, Expr *e, 2280 ArraySizeModifier sm, unsigned tq, 2281 SourceRange brackets) 2282 : ArrayType(VariableArray, et, can, sm, tq, 2283 et->containsUnexpandedParameterPack()), 2284 SizeExpr((Stmt*) e), Brackets(brackets) {} 2285 friend class ASTContext; // ASTContext creates these. 2286 2287public: 2288 Expr *getSizeExpr() const { 2289 // We use C-style casts instead of cast<> here because we do not wish 2290 // to have a dependency of Type.h on Stmt.h/Expr.h. 2291 return (Expr*) SizeExpr; 2292 } 2293 SourceRange getBracketsRange() const { return Brackets; } 2294 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } 2295 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } 2296 2297 bool isSugared() const { return false; } 2298 QualType desugar() const { return QualType(this, 0); } 2299 2300 static bool classof(const Type *T) { 2301 return T->getTypeClass() == VariableArray; 2302 } 2303 static bool classof(const VariableArrayType *) { return true; } 2304 2305 friend class StmtIteratorBase; 2306 2307 void Profile(llvm::FoldingSetNodeID &ID) { 2308 llvm_unreachable("Cannot unique VariableArrayTypes."); 2309 } 2310}; 2311 2312/// DependentSizedArrayType - This type represents an array type in 2313/// C++ whose size is a value-dependent expression. For example: 2314/// 2315/// \code 2316/// template<typename T, int Size> 2317/// class array { 2318/// T data[Size]; 2319/// }; 2320/// \endcode 2321/// 2322/// For these types, we won't actually know what the array bound is 2323/// until template instantiation occurs, at which point this will 2324/// become either a ConstantArrayType or a VariableArrayType. 2325class DependentSizedArrayType : public ArrayType { 2326 const ASTContext &Context; 2327 2328 /// \brief An assignment expression that will instantiate to the 2329 /// size of the array. 2330 /// 2331 /// The expression itself might be NULL, in which case the array 2332 /// type will have its size deduced from an initializer. 2333 Stmt *SizeExpr; 2334 2335 /// Brackets - The left and right array brackets. 2336 SourceRange Brackets; 2337 2338 DependentSizedArrayType(const ASTContext &Context, QualType et, QualType can, 2339 Expr *e, ArraySizeModifier sm, unsigned tq, 2340 SourceRange brackets); 2341 2342 friend class ASTContext; // ASTContext creates these. 2343 2344public: 2345 Expr *getSizeExpr() const { 2346 // We use C-style casts instead of cast<> here because we do not wish 2347 // to have a dependency of Type.h on Stmt.h/Expr.h. 2348 return (Expr*) SizeExpr; 2349 } 2350 SourceRange getBracketsRange() const { return Brackets; } 2351 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } 2352 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } 2353 2354 bool isSugared() const { return false; } 2355 QualType desugar() const { return QualType(this, 0); } 2356 2357 static bool classof(const Type *T) { 2358 return T->getTypeClass() == DependentSizedArray; 2359 } 2360 static bool classof(const DependentSizedArrayType *) { return true; } 2361 2362 friend class StmtIteratorBase; 2363 2364 2365 void Profile(llvm::FoldingSetNodeID &ID) { 2366 Profile(ID, Context, getElementType(), 2367 getSizeModifier(), getIndexTypeCVRQualifiers(), getSizeExpr()); 2368 } 2369 2370 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 2371 QualType ET, ArraySizeModifier SizeMod, 2372 unsigned TypeQuals, Expr *E); 2373}; 2374 2375/// DependentSizedExtVectorType - This type represent an extended vector type 2376/// where either the type or size is dependent. For example: 2377/// @code 2378/// template<typename T, int Size> 2379/// class vector { 2380/// typedef T __attribute__((ext_vector_type(Size))) type; 2381/// } 2382/// @endcode 2383class DependentSizedExtVectorType : public Type, public llvm::FoldingSetNode { 2384 const ASTContext &Context; 2385 Expr *SizeExpr; 2386 /// ElementType - The element type of the array. 2387 QualType ElementType; 2388 SourceLocation loc; 2389 2390 DependentSizedExtVectorType(const ASTContext &Context, QualType ElementType, 2391 QualType can, Expr *SizeExpr, SourceLocation loc); 2392 2393 friend class ASTContext; 2394 2395public: 2396 Expr *getSizeExpr() const { return SizeExpr; } 2397 QualType getElementType() const { return ElementType; } 2398 SourceLocation getAttributeLoc() const { return loc; } 2399 2400 bool isSugared() const { return false; } 2401 QualType desugar() const { return QualType(this, 0); } 2402 2403 static bool classof(const Type *T) { 2404 return T->getTypeClass() == DependentSizedExtVector; 2405 } 2406 static bool classof(const DependentSizedExtVectorType *) { return true; } 2407 2408 void Profile(llvm::FoldingSetNodeID &ID) { 2409 Profile(ID, Context, getElementType(), getSizeExpr()); 2410 } 2411 2412 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 2413 QualType ElementType, Expr *SizeExpr); 2414}; 2415 2416 2417/// VectorType - GCC generic vector type. This type is created using 2418/// __attribute__((vector_size(n)), where "n" specifies the vector size in 2419/// bytes; or from an Altivec __vector or vector declaration. 2420/// Since the constructor takes the number of vector elements, the 2421/// client is responsible for converting the size into the number of elements. 2422class VectorType : public Type, public llvm::FoldingSetNode { 2423public: 2424 enum VectorKind { 2425 GenericVector, // not a target-specific vector type 2426 AltiVecVector, // is AltiVec vector 2427 AltiVecPixel, // is AltiVec 'vector Pixel' 2428 AltiVecBool, // is AltiVec 'vector bool ...' 2429 NeonVector, // is ARM Neon vector 2430 NeonPolyVector // is ARM Neon polynomial vector 2431 }; 2432protected: 2433 /// ElementType - The element type of the vector. 2434 QualType ElementType; 2435 2436 VectorType(QualType vecType, unsigned nElements, QualType canonType, 2437 VectorKind vecKind); 2438 2439 VectorType(TypeClass tc, QualType vecType, unsigned nElements, 2440 QualType canonType, VectorKind vecKind); 2441 2442 friend class ASTContext; // ASTContext creates these. 2443 2444public: 2445 2446 QualType getElementType() const { return ElementType; } 2447 unsigned getNumElements() const { return VectorTypeBits.NumElements; } 2448 2449 bool isSugared() const { return false; } 2450 QualType desugar() const { return QualType(this, 0); } 2451 2452 VectorKind getVectorKind() const { 2453 return VectorKind(VectorTypeBits.VecKind); 2454 } 2455 2456 void Profile(llvm::FoldingSetNodeID &ID) { 2457 Profile(ID, getElementType(), getNumElements(), 2458 getTypeClass(), getVectorKind()); 2459 } 2460 static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, 2461 unsigned NumElements, TypeClass TypeClass, 2462 VectorKind VecKind) { 2463 ID.AddPointer(ElementType.getAsOpaquePtr()); 2464 ID.AddInteger(NumElements); 2465 ID.AddInteger(TypeClass); 2466 ID.AddInteger(VecKind); 2467 } 2468 2469 static bool classof(const Type *T) { 2470 return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector; 2471 } 2472 static bool classof(const VectorType *) { return true; } 2473}; 2474 2475/// ExtVectorType - Extended vector type. This type is created using 2476/// __attribute__((ext_vector_type(n)), where "n" is the number of elements. 2477/// Unlike vector_size, ext_vector_type is only allowed on typedef's. This 2478/// class enables syntactic extensions, like Vector Components for accessing 2479/// points, colors, and textures (modeled after OpenGL Shading Language). 2480class ExtVectorType : public VectorType { 2481 ExtVectorType(QualType vecType, unsigned nElements, QualType canonType) : 2482 VectorType(ExtVector, vecType, nElements, canonType, GenericVector) {} 2483 friend class ASTContext; // ASTContext creates these. 2484public: 2485 static int getPointAccessorIdx(char c) { 2486 switch (c) { 2487 default: return -1; 2488 case 'x': return 0; 2489 case 'y': return 1; 2490 case 'z': return 2; 2491 case 'w': return 3; 2492 } 2493 } 2494 static int getNumericAccessorIdx(char c) { 2495 switch (c) { 2496 default: return -1; 2497 case '0': return 0; 2498 case '1': return 1; 2499 case '2': return 2; 2500 case '3': return 3; 2501 case '4': return 4; 2502 case '5': return 5; 2503 case '6': return 6; 2504 case '7': return 7; 2505 case '8': return 8; 2506 case '9': return 9; 2507 case 'A': 2508 case 'a': return 10; 2509 case 'B': 2510 case 'b': return 11; 2511 case 'C': 2512 case 'c': return 12; 2513 case 'D': 2514 case 'd': return 13; 2515 case 'E': 2516 case 'e': return 14; 2517 case 'F': 2518 case 'f': return 15; 2519 } 2520 } 2521 2522 static int getAccessorIdx(char c) { 2523 if (int idx = getPointAccessorIdx(c)+1) return idx-1; 2524 return getNumericAccessorIdx(c); 2525 } 2526 2527 bool isAccessorWithinNumElements(char c) const { 2528 if (int idx = getAccessorIdx(c)+1) 2529 return unsigned(idx-1) < getNumElements(); 2530 return false; 2531 } 2532 bool isSugared() const { return false; } 2533 QualType desugar() const { return QualType(this, 0); } 2534 2535 static bool classof(const Type *T) { 2536 return T->getTypeClass() == ExtVector; 2537 } 2538 static bool classof(const ExtVectorType *) { return true; } 2539}; 2540 2541/// FunctionType - C99 6.7.5.3 - Function Declarators. This is the common base 2542/// class of FunctionNoProtoType and FunctionProtoType. 2543/// 2544class FunctionType : public Type { 2545 // The type returned by the function. 2546 QualType ResultType; 2547 2548 public: 2549 /// ExtInfo - A class which abstracts out some details necessary for 2550 /// making a call. 2551 /// 2552 /// It is not actually used directly for storing this information in 2553 /// a FunctionType, although FunctionType does currently use the 2554 /// same bit-pattern. 2555 /// 2556 // If you add a field (say Foo), other than the obvious places (both, 2557 // constructors, compile failures), what you need to update is 2558 // * Operator== 2559 // * getFoo 2560 // * withFoo 2561 // * functionType. Add Foo, getFoo. 2562 // * ASTContext::getFooType 2563 // * ASTContext::mergeFunctionTypes 2564 // * FunctionNoProtoType::Profile 2565 // * FunctionProtoType::Profile 2566 // * TypePrinter::PrintFunctionProto 2567 // * AST read and write 2568 // * Codegen 2569 class ExtInfo { 2570 // Feel free to rearrange or add bits, but if you go over 8, 2571 // you'll need to adjust both the Bits field below and 2572 // Type::FunctionTypeBitfields. 2573 2574 // | CC |noreturn|produces|regparm| 2575 // |0 .. 2| 3 | 4 | 5 .. 7| 2576 // 2577 // regparm is either 0 (no regparm attribute) or the regparm value+1. 2578 enum { CallConvMask = 0x7 }; 2579 enum { NoReturnMask = 0x8 }; 2580 enum { ProducesResultMask = 0x10 }; 2581 enum { RegParmMask = ~(CallConvMask | NoReturnMask | ProducesResultMask), 2582 RegParmOffset = 5 }; // Assumed to be the last field 2583 2584 uint16_t Bits; 2585 2586 ExtInfo(unsigned Bits) : Bits(static_cast<uint16_t>(Bits)) {} 2587 2588 friend class FunctionType; 2589 2590 public: 2591 // Constructor with no defaults. Use this when you know that you 2592 // have all the elements (when reading an AST file for example). 2593 ExtInfo(bool noReturn, bool hasRegParm, unsigned regParm, CallingConv cc, 2594 bool producesResult) { 2595 assert((!hasRegParm || regParm < 7) && "Invalid regparm value"); 2596 Bits = ((unsigned) cc) | 2597 (noReturn ? NoReturnMask : 0) | 2598 (producesResult ? ProducesResultMask : 0) | 2599 (hasRegParm ? ((regParm + 1) << RegParmOffset) : 0); 2600 } 2601 2602 // Constructor with all defaults. Use when for example creating a 2603 // function know to use defaults. 2604 ExtInfo() : Bits(0) {} 2605 2606 bool getNoReturn() const { return Bits & NoReturnMask; } 2607 bool getProducesResult() const { return Bits & ProducesResultMask; } 2608 bool getHasRegParm() const { return (Bits >> RegParmOffset) != 0; } 2609 unsigned getRegParm() const { 2610 unsigned RegParm = Bits >> RegParmOffset; 2611 if (RegParm > 0) 2612 --RegParm; 2613 return RegParm; 2614 } 2615 CallingConv getCC() const { return CallingConv(Bits & CallConvMask); } 2616 2617 bool operator==(ExtInfo Other) const { 2618 return Bits == Other.Bits; 2619 } 2620 bool operator!=(ExtInfo Other) const { 2621 return Bits != Other.Bits; 2622 } 2623 2624 // Note that we don't have setters. That is by design, use 2625 // the following with methods instead of mutating these objects. 2626 2627 ExtInfo withNoReturn(bool noReturn) const { 2628 if (noReturn) 2629 return ExtInfo(Bits | NoReturnMask); 2630 else 2631 return ExtInfo(Bits & ~NoReturnMask); 2632 } 2633 2634 ExtInfo withProducesResult(bool producesResult) const { 2635 if (producesResult) 2636 return ExtInfo(Bits | ProducesResultMask); 2637 else 2638 return ExtInfo(Bits & ~ProducesResultMask); 2639 } 2640 2641 ExtInfo withRegParm(unsigned RegParm) const { 2642 assert(RegParm < 7 && "Invalid regparm value"); 2643 return ExtInfo((Bits & ~RegParmMask) | 2644 ((RegParm + 1) << RegParmOffset)); 2645 } 2646 2647 ExtInfo withCallingConv(CallingConv cc) const { 2648 return ExtInfo((Bits & ~CallConvMask) | (unsigned) cc); 2649 } 2650 2651 void Profile(llvm::FoldingSetNodeID &ID) const { 2652 ID.AddInteger(Bits); 2653 } 2654 }; 2655 2656protected: 2657 FunctionType(TypeClass tc, QualType res, 2658 unsigned typeQuals, RefQualifierKind RefQualifier, 2659 QualType Canonical, bool Dependent, 2660 bool InstantiationDependent, 2661 bool VariablyModified, bool ContainsUnexpandedParameterPack, 2662 ExtInfo Info) 2663 : Type(tc, Canonical, Dependent, InstantiationDependent, VariablyModified, 2664 ContainsUnexpandedParameterPack), 2665 ResultType(res) { 2666 FunctionTypeBits.ExtInfo = Info.Bits; 2667 FunctionTypeBits.TypeQuals = typeQuals; 2668 FunctionTypeBits.RefQualifier = static_cast<unsigned>(RefQualifier); 2669 } 2670 unsigned getTypeQuals() const { return FunctionTypeBits.TypeQuals; } 2671 2672 RefQualifierKind getRefQualifier() const { 2673 return static_cast<RefQualifierKind>(FunctionTypeBits.RefQualifier); 2674 } 2675 2676public: 2677 2678 QualType getResultType() const { return ResultType; } 2679 2680 bool getHasRegParm() const { return getExtInfo().getHasRegParm(); } 2681 unsigned getRegParmType() const { return getExtInfo().getRegParm(); } 2682 bool getNoReturnAttr() const { return getExtInfo().getNoReturn(); } 2683 CallingConv getCallConv() const { return getExtInfo().getCC(); } 2684 ExtInfo getExtInfo() const { return ExtInfo(FunctionTypeBits.ExtInfo); } 2685 2686 /// \brief Determine the type of an expression that calls a function of 2687 /// this type. 2688 QualType getCallResultType(ASTContext &Context) const { 2689 return getResultType().getNonLValueExprType(Context); 2690 } 2691 2692 static StringRef getNameForCallConv(CallingConv CC); 2693 2694 static bool classof(const Type *T) { 2695 return T->getTypeClass() == FunctionNoProto || 2696 T->getTypeClass() == FunctionProto; 2697 } 2698 static bool classof(const FunctionType *) { return true; } 2699}; 2700 2701/// FunctionNoProtoType - Represents a K&R-style 'int foo()' function, which has 2702/// no information available about its arguments. 2703class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode { 2704 FunctionNoProtoType(QualType Result, QualType Canonical, ExtInfo Info) 2705 : FunctionType(FunctionNoProto, Result, 0, RQ_None, Canonical, 2706 /*Dependent=*/false, /*InstantiationDependent=*/false, 2707 Result->isVariablyModifiedType(), 2708 /*ContainsUnexpandedParameterPack=*/false, Info) {} 2709 2710 friend class ASTContext; // ASTContext creates these. 2711 2712public: 2713 // No additional state past what FunctionType provides. 2714 2715 bool isSugared() const { return false; } 2716 QualType desugar() const { return QualType(this, 0); } 2717 2718 void Profile(llvm::FoldingSetNodeID &ID) { 2719 Profile(ID, getResultType(), getExtInfo()); 2720 } 2721 static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType, 2722 ExtInfo Info) { 2723 Info.Profile(ID); 2724 ID.AddPointer(ResultType.getAsOpaquePtr()); 2725 } 2726 2727 static bool classof(const Type *T) { 2728 return T->getTypeClass() == FunctionNoProto; 2729 } 2730 static bool classof(const FunctionNoProtoType *) { return true; } 2731}; 2732 2733/// FunctionProtoType - Represents a prototype with argument type info, e.g. 2734/// 'int foo(int)' or 'int foo(void)'. 'void' is represented as having no 2735/// arguments, not as having a single void argument. Such a type can have an 2736/// exception specification, but this specification is not part of the canonical 2737/// type. 2738class FunctionProtoType : public FunctionType, public llvm::FoldingSetNode { 2739public: 2740 /// ExtProtoInfo - Extra information about a function prototype. 2741 struct ExtProtoInfo { 2742 ExtProtoInfo() : 2743 Variadic(false), HasTrailingReturn(false), TypeQuals(0), 2744 ExceptionSpecType(EST_None), RefQualifier(RQ_None), 2745 NumExceptions(0), Exceptions(0), NoexceptExpr(0), 2746 ExceptionSpecDecl(0), ExceptionSpecTemplate(0), 2747 ConsumedArguments(0) {} 2748 2749 FunctionType::ExtInfo ExtInfo; 2750 bool Variadic : 1; 2751 bool HasTrailingReturn : 1; 2752 unsigned char TypeQuals; 2753 ExceptionSpecificationType ExceptionSpecType; 2754 RefQualifierKind RefQualifier; 2755 unsigned NumExceptions; 2756 const QualType *Exceptions; 2757 Expr *NoexceptExpr; 2758 FunctionDecl *ExceptionSpecDecl; 2759 FunctionDecl *ExceptionSpecTemplate; 2760 const bool *ConsumedArguments; 2761 }; 2762 2763private: 2764 /// \brief Determine whether there are any argument types that 2765 /// contain an unexpanded parameter pack. 2766 static bool containsAnyUnexpandedParameterPack(const QualType *ArgArray, 2767 unsigned numArgs) { 2768 for (unsigned Idx = 0; Idx < numArgs; ++Idx) 2769 if (ArgArray[Idx]->containsUnexpandedParameterPack()) 2770 return true; 2771 2772 return false; 2773 } 2774 2775 FunctionProtoType(QualType result, const QualType *args, unsigned numArgs, 2776 QualType canonical, const ExtProtoInfo &epi); 2777 2778 /// NumArgs - The number of arguments this function has, not counting '...'. 2779 unsigned NumArgs : 17; 2780 2781 /// NumExceptions - The number of types in the exception spec, if any. 2782 unsigned NumExceptions : 9; 2783 2784 /// ExceptionSpecType - The type of exception specification this function has. 2785 unsigned ExceptionSpecType : 3; 2786 2787 /// HasAnyConsumedArgs - Whether this function has any consumed arguments. 2788 unsigned HasAnyConsumedArgs : 1; 2789 2790 /// Variadic - Whether the function is variadic. 2791 unsigned Variadic : 1; 2792 2793 /// HasTrailingReturn - Whether this function has a trailing return type. 2794 unsigned HasTrailingReturn : 1; 2795 2796 // ArgInfo - There is an variable size array after the class in memory that 2797 // holds the argument types. 2798 2799 // Exceptions - There is another variable size array after ArgInfo that 2800 // holds the exception types. 2801 2802 // NoexceptExpr - Instead of Exceptions, there may be a single Expr* pointing 2803 // to the expression in the noexcept() specifier. 2804 2805 // ExceptionSpecDecl, ExceptionSpecTemplate - Instead of Exceptions, there may 2806 // be a pair of FunctionDecl* pointing to the function which should be used to 2807 // instantiate this function type's exception specification, and the function 2808 // from which it should be instantiated. 2809 2810 // ConsumedArgs - A variable size array, following Exceptions 2811 // and of length NumArgs, holding flags indicating which arguments 2812 // are consumed. This only appears if HasAnyConsumedArgs is true. 2813 2814 friend class ASTContext; // ASTContext creates these. 2815 2816 const bool *getConsumedArgsBuffer() const { 2817 assert(hasAnyConsumedArgs()); 2818 2819 // Find the end of the exceptions. 2820 Expr * const *eh_end = reinterpret_cast<Expr * const *>(arg_type_end()); 2821 if (getExceptionSpecType() != EST_ComputedNoexcept) 2822 eh_end += NumExceptions; 2823 else 2824 eh_end += 1; // NoexceptExpr 2825 2826 return reinterpret_cast<const bool*>(eh_end); 2827 } 2828 2829public: 2830 unsigned getNumArgs() const { return NumArgs; } 2831 QualType getArgType(unsigned i) const { 2832 assert(i < NumArgs && "Invalid argument number!"); 2833 return arg_type_begin()[i]; 2834 } 2835 2836 ExtProtoInfo getExtProtoInfo() const { 2837 ExtProtoInfo EPI; 2838 EPI.ExtInfo = getExtInfo(); 2839 EPI.Variadic = isVariadic(); 2840 EPI.HasTrailingReturn = hasTrailingReturn(); 2841 EPI.ExceptionSpecType = getExceptionSpecType(); 2842 EPI.TypeQuals = static_cast<unsigned char>(getTypeQuals()); 2843 EPI.RefQualifier = getRefQualifier(); 2844 if (EPI.ExceptionSpecType == EST_Dynamic) { 2845 EPI.NumExceptions = NumExceptions; 2846 EPI.Exceptions = exception_begin(); 2847 } else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) { 2848 EPI.NoexceptExpr = getNoexceptExpr(); 2849 } else if (EPI.ExceptionSpecType == EST_Uninstantiated) { 2850 EPI.ExceptionSpecDecl = getExceptionSpecDecl(); 2851 EPI.ExceptionSpecTemplate = getExceptionSpecTemplate(); 2852 } 2853 if (hasAnyConsumedArgs()) 2854 EPI.ConsumedArguments = getConsumedArgsBuffer(); 2855 return EPI; 2856 } 2857 2858 /// \brief Get the kind of exception specification on this function. 2859 ExceptionSpecificationType getExceptionSpecType() const { 2860 return static_cast<ExceptionSpecificationType>(ExceptionSpecType); 2861 } 2862 /// \brief Return whether this function has any kind of exception spec. 2863 bool hasExceptionSpec() const { 2864 return getExceptionSpecType() != EST_None; 2865 } 2866 /// \brief Return whether this function has a dynamic (throw) exception spec. 2867 bool hasDynamicExceptionSpec() const { 2868 return isDynamicExceptionSpec(getExceptionSpecType()); 2869 } 2870 /// \brief Return whether this function has a noexcept exception spec. 2871 bool hasNoexceptExceptionSpec() const { 2872 return isNoexceptExceptionSpec(getExceptionSpecType()); 2873 } 2874 /// \brief Result type of getNoexceptSpec(). 2875 enum NoexceptResult { 2876 NR_NoNoexcept, ///< There is no noexcept specifier. 2877 NR_BadNoexcept, ///< The noexcept specifier has a bad expression. 2878 NR_Dependent, ///< The noexcept specifier is dependent. 2879 NR_Throw, ///< The noexcept specifier evaluates to false. 2880 NR_Nothrow ///< The noexcept specifier evaluates to true. 2881 }; 2882 /// \brief Get the meaning of the noexcept spec on this function, if any. 2883 NoexceptResult getNoexceptSpec(ASTContext &Ctx) const; 2884 unsigned getNumExceptions() const { return NumExceptions; } 2885 QualType getExceptionType(unsigned i) const { 2886 assert(i < NumExceptions && "Invalid exception number!"); 2887 return exception_begin()[i]; 2888 } 2889 Expr *getNoexceptExpr() const { 2890 if (getExceptionSpecType() != EST_ComputedNoexcept) 2891 return 0; 2892 // NoexceptExpr sits where the arguments end. 2893 return *reinterpret_cast<Expr *const *>(arg_type_end()); 2894 } 2895 /// \brief If this function type has an uninstantiated exception 2896 /// specification, this is the function whose exception specification 2897 /// is represented by this type. 2898 FunctionDecl *getExceptionSpecDecl() const { 2899 if (getExceptionSpecType() != EST_Uninstantiated) 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_Delayed && 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 /// \precondition @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=*/true, 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/// @class C; 4163/// @protocol P; 4164/// 4165/// 'C' is an ObjCInterfaceType C. It is sugar for an ObjCObjectType 4166/// with base C and no protocols. 4167/// 4168/// 'C<P>' is an ObjCObjectType with base C and protocol list [P]. 4169/// 4170/// 'id' is a TypedefType which is sugar for an ObjCPointerType whose 4171/// pointee is an ObjCObjectType with base BuiltinType::ObjCIdType 4172/// and no protocols. 4173/// 4174/// 'id<P>' is an ObjCPointerType whose pointee is an ObjCObjecType 4175/// with base BuiltinType::ObjCIdType and protocol list [P]. Eventually 4176/// this should get its own sugar class to better represent the source. 4177class ObjCObjectType : public Type { 4178 // ObjCObjectType.NumProtocols - the number of protocols stored 4179 // after the ObjCObjectPointerType node. 4180 // 4181 // These protocols are those written directly on the type. If 4182 // protocol qualifiers ever become additive, the iterators will need 4183 // to get kindof complicated. 4184 // 4185 // In the canonical object type, these are sorted alphabetically 4186 // and uniqued. 4187 4188 /// Either a BuiltinType or an InterfaceType or sugar for either. 4189 QualType BaseType; 4190 4191 ObjCProtocolDecl * const *getProtocolStorage() const { 4192 return const_cast<ObjCObjectType*>(this)->getProtocolStorage(); 4193 } 4194 4195 ObjCProtocolDecl **getProtocolStorage(); 4196 4197protected: 4198 ObjCObjectType(QualType Canonical, QualType Base, 4199 ObjCProtocolDecl * const *Protocols, unsigned NumProtocols); 4200 4201 enum Nonce_ObjCInterface { Nonce_ObjCInterface }; 4202 ObjCObjectType(enum Nonce_ObjCInterface) 4203 : Type(ObjCInterface, QualType(), false, false, false, false), 4204 BaseType(QualType(this_(), 0)) { 4205 ObjCObjectTypeBits.NumProtocols = 0; 4206 } 4207 4208public: 4209 /// getBaseType - Gets the base type of this object type. This is 4210 /// always (possibly sugar for) one of: 4211 /// - the 'id' builtin type (as opposed to the 'id' type visible to the 4212 /// user, which is a typedef for an ObjCPointerType) 4213 /// - the 'Class' builtin type (same caveat) 4214 /// - an ObjCObjectType (currently always an ObjCInterfaceType) 4215 QualType getBaseType() const { return BaseType; } 4216 4217 bool isObjCId() const { 4218 return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCId); 4219 } 4220 bool isObjCClass() const { 4221 return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCClass); 4222 } 4223 bool isObjCUnqualifiedId() const { return qual_empty() && isObjCId(); } 4224 bool isObjCUnqualifiedClass() const { return qual_empty() && isObjCClass(); } 4225 bool isObjCUnqualifiedIdOrClass() const { 4226 if (!qual_empty()) return false; 4227 if (const BuiltinType *T = getBaseType()->getAs<BuiltinType>()) 4228 return T->getKind() == BuiltinType::ObjCId || 4229 T->getKind() == BuiltinType::ObjCClass; 4230 return false; 4231 } 4232 bool isObjCQualifiedId() const { return !qual_empty() && isObjCId(); } 4233 bool isObjCQualifiedClass() const { return !qual_empty() && isObjCClass(); } 4234 4235 /// Gets the interface declaration for this object type, if the base type 4236 /// really is an interface. 4237 ObjCInterfaceDecl *getInterface() const; 4238 4239 typedef ObjCProtocolDecl * const *qual_iterator; 4240 4241 qual_iterator qual_begin() const { return getProtocolStorage(); } 4242 qual_iterator qual_end() const { return qual_begin() + getNumProtocols(); } 4243 4244 bool qual_empty() const { return getNumProtocols() == 0; } 4245 4246 /// getNumProtocols - Return the number of qualifying protocols in this 4247 /// interface type, or 0 if there are none. 4248 unsigned getNumProtocols() const { return ObjCObjectTypeBits.NumProtocols; } 4249 4250 /// \brief Fetch a protocol by index. 4251 ObjCProtocolDecl *getProtocol(unsigned I) const { 4252 assert(I < getNumProtocols() && "Out-of-range protocol access"); 4253 return qual_begin()[I]; 4254 } 4255 4256 bool isSugared() const { return false; } 4257 QualType desugar() const { return QualType(this, 0); } 4258 4259 static bool classof(const Type *T) { 4260 return T->getTypeClass() == ObjCObject || 4261 T->getTypeClass() == ObjCInterface; 4262 } 4263 static bool classof(const ObjCObjectType *) { return true; } 4264}; 4265 4266/// ObjCObjectTypeImpl - A class providing a concrete implementation 4267/// of ObjCObjectType, so as to not increase the footprint of 4268/// ObjCInterfaceType. Code outside of ASTContext and the core type 4269/// system should not reference this type. 4270class ObjCObjectTypeImpl : public ObjCObjectType, public llvm::FoldingSetNode { 4271 friend class ASTContext; 4272 4273 // If anyone adds fields here, ObjCObjectType::getProtocolStorage() 4274 // will need to be modified. 4275 4276 ObjCObjectTypeImpl(QualType Canonical, QualType Base, 4277 ObjCProtocolDecl * const *Protocols, 4278 unsigned NumProtocols) 4279 : ObjCObjectType(Canonical, Base, Protocols, NumProtocols) {} 4280 4281public: 4282 void Profile(llvm::FoldingSetNodeID &ID); 4283 static void Profile(llvm::FoldingSetNodeID &ID, 4284 QualType Base, 4285 ObjCProtocolDecl *const *protocols, 4286 unsigned NumProtocols); 4287}; 4288 4289inline ObjCProtocolDecl **ObjCObjectType::getProtocolStorage() { 4290 return reinterpret_cast<ObjCProtocolDecl**>( 4291 static_cast<ObjCObjectTypeImpl*>(this) + 1); 4292} 4293 4294/// ObjCInterfaceType - Interfaces are the core concept in Objective-C for 4295/// object oriented design. They basically correspond to C++ classes. There 4296/// are two kinds of interface types, normal interfaces like "NSString" and 4297/// qualified interfaces, which are qualified with a protocol list like 4298/// "NSString<NSCopyable, NSAmazing>". 4299/// 4300/// ObjCInterfaceType guarantees the following properties when considered 4301/// as a subtype of its superclass, ObjCObjectType: 4302/// - There are no protocol qualifiers. To reinforce this, code which 4303/// tries to invoke the protocol methods via an ObjCInterfaceType will 4304/// fail to compile. 4305/// - It is its own base type. That is, if T is an ObjCInterfaceType*, 4306/// T->getBaseType() == QualType(T, 0). 4307class ObjCInterfaceType : public ObjCObjectType { 4308 mutable ObjCInterfaceDecl *Decl; 4309 4310 ObjCInterfaceType(const ObjCInterfaceDecl *D) 4311 : ObjCObjectType(Nonce_ObjCInterface), 4312 Decl(const_cast<ObjCInterfaceDecl*>(D)) {} 4313 friend class ASTContext; // ASTContext creates these. 4314 friend class ASTReader; 4315 friend class ObjCInterfaceDecl; 4316 4317public: 4318 /// getDecl - Get the declaration of this interface. 4319 ObjCInterfaceDecl *getDecl() const { return Decl; } 4320 4321 bool isSugared() const { return false; } 4322 QualType desugar() const { return QualType(this, 0); } 4323 4324 static bool classof(const Type *T) { 4325 return T->getTypeClass() == ObjCInterface; 4326 } 4327 static bool classof(const ObjCInterfaceType *) { return true; } 4328 4329 // Nonsense to "hide" certain members of ObjCObjectType within this 4330 // class. People asking for protocols on an ObjCInterfaceType are 4331 // not going to get what they want: ObjCInterfaceTypes are 4332 // guaranteed to have no protocols. 4333 enum { 4334 qual_iterator, 4335 qual_begin, 4336 qual_end, 4337 getNumProtocols, 4338 getProtocol 4339 }; 4340}; 4341 4342inline ObjCInterfaceDecl *ObjCObjectType::getInterface() const { 4343 if (const ObjCInterfaceType *T = 4344 getBaseType()->getAs<ObjCInterfaceType>()) 4345 return T->getDecl(); 4346 return 0; 4347} 4348 4349/// ObjCObjectPointerType - Used to represent a pointer to an 4350/// Objective C object. These are constructed from pointer 4351/// declarators when the pointee type is an ObjCObjectType (or sugar 4352/// for one). In addition, the 'id' and 'Class' types are typedefs 4353/// for these, and the protocol-qualified types 'id<P>' and 'Class<P>' 4354/// are translated into these. 4355/// 4356/// Pointers to pointers to Objective C objects are still PointerTypes; 4357/// only the first level of pointer gets it own type implementation. 4358class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode { 4359 QualType PointeeType; 4360 4361 ObjCObjectPointerType(QualType Canonical, QualType Pointee) 4362 : Type(ObjCObjectPointer, Canonical, false, false, false, false), 4363 PointeeType(Pointee) {} 4364 friend class ASTContext; // ASTContext creates these. 4365 4366public: 4367 /// getPointeeType - Gets the type pointed to by this ObjC pointer. 4368 /// The result will always be an ObjCObjectType or sugar thereof. 4369 QualType getPointeeType() const { return PointeeType; } 4370 4371 /// getObjCObjectType - Gets the type pointed to by this ObjC 4372 /// pointer. This method always returns non-null. 4373 /// 4374 /// This method is equivalent to getPointeeType() except that 4375 /// it discards any typedefs (or other sugar) between this 4376 /// type and the "outermost" object type. So for: 4377 /// @class A; @protocol P; @protocol Q; 4378 /// typedef A<P> AP; 4379 /// typedef A A1; 4380 /// typedef A1<P> A1P; 4381 /// typedef A1P<Q> A1PQ; 4382 /// For 'A*', getObjectType() will return 'A'. 4383 /// For 'A<P>*', getObjectType() will return 'A<P>'. 4384 /// For 'AP*', getObjectType() will return 'A<P>'. 4385 /// For 'A1*', getObjectType() will return 'A'. 4386 /// For 'A1<P>*', getObjectType() will return 'A1<P>'. 4387 /// For 'A1P*', getObjectType() will return 'A1<P>'. 4388 /// For 'A1PQ*', getObjectType() will return 'A1<Q>', because 4389 /// adding protocols to a protocol-qualified base discards the 4390 /// old qualifiers (for now). But if it didn't, getObjectType() 4391 /// would return 'A1P<Q>' (and we'd have to make iterating over 4392 /// qualifiers more complicated). 4393 const ObjCObjectType *getObjectType() const { 4394 return PointeeType->castAs<ObjCObjectType>(); 4395 } 4396 4397 /// getInterfaceType - If this pointer points to an Objective C 4398 /// @interface type, gets the type for that interface. Any protocol 4399 /// qualifiers on the interface are ignored. 4400 /// 4401 /// \return null if the base type for this pointer is 'id' or 'Class' 4402 const ObjCInterfaceType *getInterfaceType() const { 4403 return getObjectType()->getBaseType()->getAs<ObjCInterfaceType>(); 4404 } 4405 4406 /// getInterfaceDecl - If this pointer points to an Objective @interface 4407 /// type, gets the declaration for that interface. 4408 /// 4409 /// \return null if the base type for this pointer is 'id' or 'Class' 4410 ObjCInterfaceDecl *getInterfaceDecl() const { 4411 return getObjectType()->getInterface(); 4412 } 4413 4414 /// isObjCIdType - True if this is equivalent to the 'id' type, i.e. if 4415 /// its object type is the primitive 'id' type with no protocols. 4416 bool isObjCIdType() const { 4417 return getObjectType()->isObjCUnqualifiedId(); 4418 } 4419 4420 /// isObjCClassType - True if this is equivalent to the 'Class' type, 4421 /// i.e. if its object tive is the primitive 'Class' type with no protocols. 4422 bool isObjCClassType() const { 4423 return getObjectType()->isObjCUnqualifiedClass(); 4424 } 4425 4426 /// isObjCQualifiedIdType - True if this is equivalent to 'id<P>' for some 4427 /// non-empty set of protocols. 4428 bool isObjCQualifiedIdType() const { 4429 return getObjectType()->isObjCQualifiedId(); 4430 } 4431 4432 /// isObjCQualifiedClassType - True if this is equivalent to 'Class<P>' for 4433 /// some non-empty set of protocols. 4434 bool isObjCQualifiedClassType() const { 4435 return getObjectType()->isObjCQualifiedClass(); 4436 } 4437 4438 /// An iterator over the qualifiers on the object type. Provided 4439 /// for convenience. This will always iterate over the full set of 4440 /// protocols on a type, not just those provided directly. 4441 typedef ObjCObjectType::qual_iterator qual_iterator; 4442 4443 qual_iterator qual_begin() const { 4444 return getObjectType()->qual_begin(); 4445 } 4446 qual_iterator qual_end() const { 4447 return getObjectType()->qual_end(); 4448 } 4449 bool qual_empty() const { return getObjectType()->qual_empty(); } 4450 4451 /// getNumProtocols - Return the number of qualifying protocols on 4452 /// the object type. 4453 unsigned getNumProtocols() const { 4454 return getObjectType()->getNumProtocols(); 4455 } 4456 4457 /// \brief Retrieve a qualifying protocol by index on the object 4458 /// type. 4459 ObjCProtocolDecl *getProtocol(unsigned I) const { 4460 return getObjectType()->getProtocol(I); 4461 } 4462 4463 bool isSugared() const { return false; } 4464 QualType desugar() const { return QualType(this, 0); } 4465 4466 void Profile(llvm::FoldingSetNodeID &ID) { 4467 Profile(ID, getPointeeType()); 4468 } 4469 static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { 4470 ID.AddPointer(T.getAsOpaquePtr()); 4471 } 4472 static bool classof(const Type *T) { 4473 return T->getTypeClass() == ObjCObjectPointer; 4474 } 4475 static bool classof(const ObjCObjectPointerType *) { return true; } 4476}; 4477 4478class AtomicType : public Type, public llvm::FoldingSetNode { 4479 QualType ValueType; 4480 4481 AtomicType(QualType ValTy, QualType Canonical) 4482 : Type(Atomic, Canonical, ValTy->isDependentType(), 4483 ValTy->isInstantiationDependentType(), 4484 ValTy->isVariablyModifiedType(), 4485 ValTy->containsUnexpandedParameterPack()), 4486 ValueType(ValTy) {} 4487 friend class ASTContext; // ASTContext creates these. 4488 4489 public: 4490 /// getValueType - Gets the type contained by this atomic type, i.e. 4491 /// the type returned by performing an atomic load of this atomic type. 4492 QualType getValueType() const { return ValueType; } 4493 4494 bool isSugared() const { return false; } 4495 QualType desugar() const { return QualType(this, 0); } 4496 4497 void Profile(llvm::FoldingSetNodeID &ID) { 4498 Profile(ID, getValueType()); 4499 } 4500 static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { 4501 ID.AddPointer(T.getAsOpaquePtr()); 4502 } 4503 static bool classof(const Type *T) { 4504 return T->getTypeClass() == Atomic; 4505 } 4506 static bool classof(const AtomicType *) { return true; } 4507}; 4508 4509/// A qualifier set is used to build a set of qualifiers. 4510class QualifierCollector : public Qualifiers { 4511public: 4512 QualifierCollector(Qualifiers Qs = Qualifiers()) : Qualifiers(Qs) {} 4513 4514 /// Collect any qualifiers on the given type and return an 4515 /// unqualified type. The qualifiers are assumed to be consistent 4516 /// with those already in the type. 4517 const Type *strip(QualType type) { 4518 addFastQualifiers(type.getLocalFastQualifiers()); 4519 if (!type.hasLocalNonFastQualifiers()) 4520 return type.getTypePtrUnsafe(); 4521 4522 const ExtQuals *extQuals = type.getExtQualsUnsafe(); 4523 addConsistentQualifiers(extQuals->getQualifiers()); 4524 return extQuals->getBaseType(); 4525 } 4526 4527 /// Apply the collected qualifiers to the given type. 4528 QualType apply(const ASTContext &Context, QualType QT) const; 4529 4530 /// Apply the collected qualifiers to the given type. 4531 QualType apply(const ASTContext &Context, const Type* T) const; 4532}; 4533 4534 4535// Inline function definitions. 4536 4537inline SplitQualType SplitQualType::getSingleStepDesugaredType() const { 4538 SplitQualType desugar = 4539 Ty->getLocallyUnqualifiedSingleStepDesugaredType().split(); 4540 desugar.Quals.addConsistentQualifiers(Quals); 4541 return desugar; 4542} 4543 4544inline const Type *QualType::getTypePtr() const { 4545 return getCommonPtr()->BaseType; 4546} 4547 4548inline const Type *QualType::getTypePtrOrNull() const { 4549 return (isNull() ? 0 : getCommonPtr()->BaseType); 4550} 4551 4552inline SplitQualType QualType::split() const { 4553 if (!hasLocalNonFastQualifiers()) 4554 return SplitQualType(getTypePtrUnsafe(), 4555 Qualifiers::fromFastMask(getLocalFastQualifiers())); 4556 4557 const ExtQuals *eq = getExtQualsUnsafe(); 4558 Qualifiers qs = eq->getQualifiers(); 4559 qs.addFastQualifiers(getLocalFastQualifiers()); 4560 return SplitQualType(eq->getBaseType(), qs); 4561} 4562 4563inline Qualifiers QualType::getLocalQualifiers() const { 4564 Qualifiers Quals; 4565 if (hasLocalNonFastQualifiers()) 4566 Quals = getExtQualsUnsafe()->getQualifiers(); 4567 Quals.addFastQualifiers(getLocalFastQualifiers()); 4568 return Quals; 4569} 4570 4571inline Qualifiers QualType::getQualifiers() const { 4572 Qualifiers quals = getCommonPtr()->CanonicalType.getLocalQualifiers(); 4573 quals.addFastQualifiers(getLocalFastQualifiers()); 4574 return quals; 4575} 4576 4577inline unsigned QualType::getCVRQualifiers() const { 4578 unsigned cvr = getCommonPtr()->CanonicalType.getLocalCVRQualifiers(); 4579 cvr |= getLocalCVRQualifiers(); 4580 return cvr; 4581} 4582 4583inline QualType QualType::getCanonicalType() const { 4584 QualType canon = getCommonPtr()->CanonicalType; 4585 return canon.withFastQualifiers(getLocalFastQualifiers()); 4586} 4587 4588inline bool QualType::isCanonical() const { 4589 return getTypePtr()->isCanonicalUnqualified(); 4590} 4591 4592inline bool QualType::isCanonicalAsParam() const { 4593 if (!isCanonical()) return false; 4594 if (hasLocalQualifiers()) return false; 4595 4596 const Type *T = getTypePtr(); 4597 if (T->isVariablyModifiedType() && T->hasSizedVLAType()) 4598 return false; 4599 4600 return !isa<FunctionType>(T) && !isa<ArrayType>(T); 4601} 4602 4603inline bool QualType::isConstQualified() const { 4604 return isLocalConstQualified() || 4605 getCommonPtr()->CanonicalType.isLocalConstQualified(); 4606} 4607 4608inline bool QualType::isRestrictQualified() const { 4609 return isLocalRestrictQualified() || 4610 getCommonPtr()->CanonicalType.isLocalRestrictQualified(); 4611} 4612 4613 4614inline bool QualType::isVolatileQualified() const { 4615 return isLocalVolatileQualified() || 4616 getCommonPtr()->CanonicalType.isLocalVolatileQualified(); 4617} 4618 4619inline bool QualType::hasQualifiers() const { 4620 return hasLocalQualifiers() || 4621 getCommonPtr()->CanonicalType.hasLocalQualifiers(); 4622} 4623 4624inline QualType QualType::getUnqualifiedType() const { 4625 if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) 4626 return QualType(getTypePtr(), 0); 4627 4628 return QualType(getSplitUnqualifiedTypeImpl(*this).Ty, 0); 4629} 4630 4631inline SplitQualType QualType::getSplitUnqualifiedType() const { 4632 if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) 4633 return split(); 4634 4635 return getSplitUnqualifiedTypeImpl(*this); 4636} 4637 4638inline void QualType::removeLocalConst() { 4639 removeLocalFastQualifiers(Qualifiers::Const); 4640} 4641 4642inline void QualType::removeLocalRestrict() { 4643 removeLocalFastQualifiers(Qualifiers::Restrict); 4644} 4645 4646inline void QualType::removeLocalVolatile() { 4647 removeLocalFastQualifiers(Qualifiers::Volatile); 4648} 4649 4650inline void QualType::removeLocalCVRQualifiers(unsigned Mask) { 4651 assert(!(Mask & ~Qualifiers::CVRMask) && "mask has non-CVR bits"); 4652 assert((int)Qualifiers::CVRMask == (int)Qualifiers::FastMask); 4653 4654 // Fast path: we don't need to touch the slow qualifiers. 4655 removeLocalFastQualifiers(Mask); 4656} 4657 4658/// getAddressSpace - Return the address space of this type. 4659inline unsigned QualType::getAddressSpace() const { 4660 return getQualifiers().getAddressSpace(); 4661} 4662 4663/// getObjCGCAttr - Return the gc attribute of this type. 4664inline Qualifiers::GC QualType::getObjCGCAttr() const { 4665 return getQualifiers().getObjCGCAttr(); 4666} 4667 4668inline FunctionType::ExtInfo getFunctionExtInfo(const Type &t) { 4669 if (const PointerType *PT = t.getAs<PointerType>()) { 4670 if (const FunctionType *FT = PT->getPointeeType()->getAs<FunctionType>()) 4671 return FT->getExtInfo(); 4672 } else if (const FunctionType *FT = t.getAs<FunctionType>()) 4673 return FT->getExtInfo(); 4674 4675 return FunctionType::ExtInfo(); 4676} 4677 4678inline FunctionType::ExtInfo getFunctionExtInfo(QualType t) { 4679 return getFunctionExtInfo(*t); 4680} 4681 4682/// isMoreQualifiedThan - Determine whether this type is more 4683/// qualified than the Other type. For example, "const volatile int" 4684/// is more qualified than "const int", "volatile int", and 4685/// "int". However, it is not more qualified than "const volatile 4686/// int". 4687inline bool QualType::isMoreQualifiedThan(QualType other) const { 4688 Qualifiers myQuals = getQualifiers(); 4689 Qualifiers otherQuals = other.getQualifiers(); 4690 return (myQuals != otherQuals && myQuals.compatiblyIncludes(otherQuals)); 4691} 4692 4693/// isAtLeastAsQualifiedAs - Determine whether this type is at last 4694/// as qualified as the Other type. For example, "const volatile 4695/// int" is at least as qualified as "const int", "volatile int", 4696/// "int", and "const volatile int". 4697inline bool QualType::isAtLeastAsQualifiedAs(QualType other) const { 4698 return getQualifiers().compatiblyIncludes(other.getQualifiers()); 4699} 4700 4701/// getNonReferenceType - If Type is a reference type (e.g., const 4702/// int&), returns the type that the reference refers to ("const 4703/// int"). Otherwise, returns the type itself. This routine is used 4704/// throughout Sema to implement C++ 5p6: 4705/// 4706/// If an expression initially has the type "reference to T" (8.3.2, 4707/// 8.5.3), the type is adjusted to "T" prior to any further 4708/// analysis, the expression designates the object or function 4709/// denoted by the reference, and the expression is an lvalue. 4710inline QualType QualType::getNonReferenceType() const { 4711 if (const ReferenceType *RefType = (*this)->getAs<ReferenceType>()) 4712 return RefType->getPointeeType(); 4713 else 4714 return *this; 4715} 4716 4717inline bool QualType::isCForbiddenLValueType() const { 4718 return ((getTypePtr()->isVoidType() && !hasQualifiers()) || 4719 getTypePtr()->isFunctionType()); 4720} 4721 4722/// \brief Tests whether the type is categorized as a fundamental type. 4723/// 4724/// \returns True for types specified in C++0x [basic.fundamental]. 4725inline bool Type::isFundamentalType() const { 4726 return isVoidType() || 4727 // FIXME: It's really annoying that we don't have an 4728 // 'isArithmeticType()' which agrees with the standard definition. 4729 (isArithmeticType() && !isEnumeralType()); 4730} 4731 4732/// \brief Tests whether the type is categorized as a compound type. 4733/// 4734/// \returns True for types specified in C++0x [basic.compound]. 4735inline bool Type::isCompoundType() const { 4736 // C++0x [basic.compound]p1: 4737 // Compound types can be constructed in the following ways: 4738 // -- arrays of objects of a given type [...]; 4739 return isArrayType() || 4740 // -- functions, which have parameters of given types [...]; 4741 isFunctionType() || 4742 // -- pointers to void or objects or functions [...]; 4743 isPointerType() || 4744 // -- references to objects or functions of a given type. [...] 4745 isReferenceType() || 4746 // -- classes containing a sequence of objects of various types, [...]; 4747 isRecordType() || 4748 // -- unions, which are classes capable of containing objects of different 4749 // types at different times; 4750 isUnionType() || 4751 // -- enumerations, which comprise a set of named constant values. [...]; 4752 isEnumeralType() || 4753 // -- pointers to non-static class members, [...]. 4754 isMemberPointerType(); 4755} 4756 4757inline bool Type::isFunctionType() const { 4758 return isa<FunctionType>(CanonicalType); 4759} 4760inline bool Type::isPointerType() const { 4761 return isa<PointerType>(CanonicalType); 4762} 4763inline bool Type::isAnyPointerType() const { 4764 return isPointerType() || isObjCObjectPointerType(); 4765} 4766inline bool Type::isBlockPointerType() const { 4767 return isa<BlockPointerType>(CanonicalType); 4768} 4769inline bool Type::isReferenceType() const { 4770 return isa<ReferenceType>(CanonicalType); 4771} 4772inline bool Type::isLValueReferenceType() const { 4773 return isa<LValueReferenceType>(CanonicalType); 4774} 4775inline bool Type::isRValueReferenceType() const { 4776 return isa<RValueReferenceType>(CanonicalType); 4777} 4778inline bool Type::isFunctionPointerType() const { 4779 if (const PointerType *T = getAs<PointerType>()) 4780 return T->getPointeeType()->isFunctionType(); 4781 else 4782 return false; 4783} 4784inline bool Type::isMemberPointerType() const { 4785 return isa<MemberPointerType>(CanonicalType); 4786} 4787inline bool Type::isMemberFunctionPointerType() const { 4788 if (const MemberPointerType* T = getAs<MemberPointerType>()) 4789 return T->isMemberFunctionPointer(); 4790 else 4791 return false; 4792} 4793inline bool Type::isMemberDataPointerType() const { 4794 if (const MemberPointerType* T = getAs<MemberPointerType>()) 4795 return T->isMemberDataPointer(); 4796 else 4797 return false; 4798} 4799inline bool Type::isArrayType() const { 4800 return isa<ArrayType>(CanonicalType); 4801} 4802inline bool Type::isConstantArrayType() const { 4803 return isa<ConstantArrayType>(CanonicalType); 4804} 4805inline bool Type::isIncompleteArrayType() const { 4806 return isa<IncompleteArrayType>(CanonicalType); 4807} 4808inline bool Type::isVariableArrayType() const { 4809 return isa<VariableArrayType>(CanonicalType); 4810} 4811inline bool Type::isDependentSizedArrayType() const { 4812 return isa<DependentSizedArrayType>(CanonicalType); 4813} 4814inline bool Type::isBuiltinType() const { 4815 return isa<BuiltinType>(CanonicalType); 4816} 4817inline bool Type::isRecordType() const { 4818 return isa<RecordType>(CanonicalType); 4819} 4820inline bool Type::isEnumeralType() const { 4821 return isa<EnumType>(CanonicalType); 4822} 4823inline bool Type::isAnyComplexType() const { 4824 return isa<ComplexType>(CanonicalType); 4825} 4826inline bool Type::isVectorType() const { 4827 return isa<VectorType>(CanonicalType); 4828} 4829inline bool Type::isExtVectorType() const { 4830 return isa<ExtVectorType>(CanonicalType); 4831} 4832inline bool Type::isObjCObjectPointerType() const { 4833 return isa<ObjCObjectPointerType>(CanonicalType); 4834} 4835inline bool Type::isObjCObjectType() const { 4836 return isa<ObjCObjectType>(CanonicalType); 4837} 4838inline bool Type::isObjCObjectOrInterfaceType() const { 4839 return isa<ObjCInterfaceType>(CanonicalType) || 4840 isa<ObjCObjectType>(CanonicalType); 4841} 4842inline bool Type::isAtomicType() const { 4843 return isa<AtomicType>(CanonicalType); 4844} 4845 4846inline bool Type::isObjCQualifiedIdType() const { 4847 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 4848 return OPT->isObjCQualifiedIdType(); 4849 return false; 4850} 4851inline bool Type::isObjCQualifiedClassType() const { 4852 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 4853 return OPT->isObjCQualifiedClassType(); 4854 return false; 4855} 4856inline bool Type::isObjCIdType() const { 4857 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 4858 return OPT->isObjCIdType(); 4859 return false; 4860} 4861inline bool Type::isObjCClassType() const { 4862 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 4863 return OPT->isObjCClassType(); 4864 return false; 4865} 4866inline bool Type::isObjCSelType() const { 4867 if (const PointerType *OPT = getAs<PointerType>()) 4868 return OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCSel); 4869 return false; 4870} 4871inline bool Type::isObjCBuiltinType() const { 4872 return isObjCIdType() || isObjCClassType() || isObjCSelType(); 4873} 4874inline bool Type::isTemplateTypeParmType() const { 4875 return isa<TemplateTypeParmType>(CanonicalType); 4876} 4877 4878inline bool Type::isSpecificBuiltinType(unsigned K) const { 4879 if (const BuiltinType *BT = getAs<BuiltinType>()) 4880 if (BT->getKind() == (BuiltinType::Kind) K) 4881 return true; 4882 return false; 4883} 4884 4885inline bool Type::isPlaceholderType() const { 4886 if (const BuiltinType *BT = dyn_cast<BuiltinType>(this)) 4887 return BT->isPlaceholderType(); 4888 return false; 4889} 4890 4891inline const BuiltinType *Type::getAsPlaceholderType() const { 4892 if (const BuiltinType *BT = dyn_cast<BuiltinType>(this)) 4893 if (BT->isPlaceholderType()) 4894 return BT; 4895 return 0; 4896} 4897 4898inline bool Type::isSpecificPlaceholderType(unsigned K) const { 4899 assert(BuiltinType::isPlaceholderTypeKind((BuiltinType::Kind) K)); 4900 if (const BuiltinType *BT = dyn_cast<BuiltinType>(this)) 4901 return (BT->getKind() == (BuiltinType::Kind) K); 4902 return false; 4903} 4904 4905inline bool Type::isNonOverloadPlaceholderType() const { 4906 if (const BuiltinType *BT = dyn_cast<BuiltinType>(this)) 4907 return BT->isNonOverloadPlaceholderType(); 4908 return false; 4909} 4910 4911inline bool Type::isVoidType() const { 4912 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 4913 return BT->getKind() == BuiltinType::Void; 4914 return false; 4915} 4916 4917inline bool Type::isHalfType() const { 4918 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 4919 return BT->getKind() == BuiltinType::Half; 4920 // FIXME: Should we allow complex __fp16? Probably not. 4921 return false; 4922} 4923 4924inline bool Type::isNullPtrType() const { 4925 if (const BuiltinType *BT = getAs<BuiltinType>()) 4926 return BT->getKind() == BuiltinType::NullPtr; 4927 return false; 4928} 4929 4930extern bool IsEnumDeclComplete(EnumDecl *); 4931extern bool IsEnumDeclScoped(EnumDecl *); 4932 4933inline bool Type::isIntegerType() const { 4934 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 4935 return BT->getKind() >= BuiltinType::Bool && 4936 BT->getKind() <= BuiltinType::Int128; 4937 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) { 4938 // Incomplete enum types are not treated as integer types. 4939 // FIXME: In C++, enum types are never integer types. 4940 return IsEnumDeclComplete(ET->getDecl()) && 4941 !IsEnumDeclScoped(ET->getDecl()); 4942 } 4943 return false; 4944} 4945 4946inline bool Type::isScalarType() const { 4947 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 4948 return BT->getKind() > BuiltinType::Void && 4949 BT->getKind() <= BuiltinType::NullPtr; 4950 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 4951 // Enums are scalar types, but only if they are defined. Incomplete enums 4952 // are not treated as scalar types. 4953 return IsEnumDeclComplete(ET->getDecl()); 4954 return isa<PointerType>(CanonicalType) || 4955 isa<BlockPointerType>(CanonicalType) || 4956 isa<MemberPointerType>(CanonicalType) || 4957 isa<ComplexType>(CanonicalType) || 4958 isa<ObjCObjectPointerType>(CanonicalType); 4959} 4960 4961inline bool Type::isIntegralOrEnumerationType() const { 4962 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 4963 return BT->getKind() >= BuiltinType::Bool && 4964 BT->getKind() <= BuiltinType::Int128; 4965 4966 // Check for a complete enum type; incomplete enum types are not properly an 4967 // enumeration type in the sense required here. 4968 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 4969 return IsEnumDeclComplete(ET->getDecl()); 4970 4971 return false; 4972} 4973 4974inline bool Type::isBooleanType() const { 4975 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 4976 return BT->getKind() == BuiltinType::Bool; 4977 return false; 4978} 4979 4980/// \brief Determines whether this is a type for which one can define 4981/// an overloaded operator. 4982inline bool Type::isOverloadableType() const { 4983 return isDependentType() || isRecordType() || isEnumeralType(); 4984} 4985 4986/// \brief Determines whether this type can decay to a pointer type. 4987inline bool Type::canDecayToPointerType() const { 4988 return isFunctionType() || isArrayType(); 4989} 4990 4991inline bool Type::hasPointerRepresentation() const { 4992 return (isPointerType() || isReferenceType() || isBlockPointerType() || 4993 isObjCObjectPointerType() || isNullPtrType()); 4994} 4995 4996inline bool Type::hasObjCPointerRepresentation() const { 4997 return isObjCObjectPointerType(); 4998} 4999 5000inline const Type *Type::getBaseElementTypeUnsafe() const { 5001 const Type *type = this; 5002 while (const ArrayType *arrayType = type->getAsArrayTypeUnsafe()) 5003 type = arrayType->getElementType().getTypePtr(); 5004 return type; 5005} 5006 5007/// Insertion operator for diagnostics. This allows sending QualType's into a 5008/// diagnostic with <<. 5009inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 5010 QualType T) { 5011 DB.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), 5012 DiagnosticsEngine::ak_qualtype); 5013 return DB; 5014} 5015 5016/// Insertion operator for partial diagnostics. This allows sending QualType's 5017/// into a diagnostic with <<. 5018inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD, 5019 QualType T) { 5020 PD.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), 5021 DiagnosticsEngine::ak_qualtype); 5022 return PD; 5023} 5024 5025// Helper class template that is used by Type::getAs to ensure that one does 5026// not try to look through a qualified type to get to an array type. 5027template<typename T, 5028 bool isArrayType = (llvm::is_same<T, ArrayType>::value || 5029 llvm::is_base_of<ArrayType, T>::value)> 5030struct ArrayType_cannot_be_used_with_getAs { }; 5031 5032template<typename T> 5033struct ArrayType_cannot_be_used_with_getAs<T, true>; 5034 5035/// Member-template getAs<specific type>'. 5036template <typename T> const T *Type::getAs() const { 5037 ArrayType_cannot_be_used_with_getAs<T> at; 5038 (void)at; 5039 5040 // If this is directly a T type, return it. 5041 if (const T *Ty = dyn_cast<T>(this)) 5042 return Ty; 5043 5044 // If the canonical form of this type isn't the right kind, reject it. 5045 if (!isa<T>(CanonicalType)) 5046 return 0; 5047 5048 // If this is a typedef for the type, strip the typedef off without 5049 // losing all typedef information. 5050 return cast<T>(getUnqualifiedDesugaredType()); 5051} 5052 5053inline const ArrayType *Type::getAsArrayTypeUnsafe() const { 5054 // If this is directly an array type, return it. 5055 if (const ArrayType *arr = dyn_cast<ArrayType>(this)) 5056 return arr; 5057 5058 // If the canonical form of this type isn't the right kind, reject it. 5059 if (!isa<ArrayType>(CanonicalType)) 5060 return 0; 5061 5062 // If this is a typedef for the type, strip the typedef off without 5063 // losing all typedef information. 5064 return cast<ArrayType>(getUnqualifiedDesugaredType()); 5065} 5066 5067template <typename T> const T *Type::castAs() const { 5068 ArrayType_cannot_be_used_with_getAs<T> at; 5069 (void) at; 5070 5071 assert(isa<T>(CanonicalType)); 5072 if (const T *ty = dyn_cast<T>(this)) return ty; 5073 return cast<T>(getUnqualifiedDesugaredType()); 5074} 5075 5076inline const ArrayType *Type::castAsArrayTypeUnsafe() const { 5077 assert(isa<ArrayType>(CanonicalType)); 5078 if (const ArrayType *arr = dyn_cast<ArrayType>(this)) return arr; 5079 return cast<ArrayType>(getUnqualifiedDesugaredType()); 5080} 5081 5082} // end namespace clang 5083 5084#endif 5085