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