Type.h revision 152f6b7be508fbc61543f3736ebd390d7ac84bd1
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 const char *getName(const PrintingPolicy &Policy) const; 1761 1762 bool isSugared() const { return false; } 1763 QualType desugar() const { return QualType(this, 0); } 1764 1765 bool isInteger() const { 1766 return getKind() >= Bool && getKind() <= Int128; 1767 } 1768 1769 bool isSignedInteger() const { 1770 return getKind() >= Char_S && getKind() <= Int128; 1771 } 1772 1773 bool isUnsignedInteger() const { 1774 return getKind() >= Bool && getKind() <= UInt128; 1775 } 1776 1777 bool isFloatingPoint() const { 1778 return getKind() >= Half && getKind() <= LongDouble; 1779 } 1780 1781 /// Determines whether the given kind corresponds to a placeholder type. 1782 static bool isPlaceholderTypeKind(Kind K) { 1783 return K >= Overload; 1784 } 1785 1786 /// Determines whether this type is a placeholder type, i.e. a type 1787 /// which cannot appear in arbitrary positions in a fully-formed 1788 /// expression. 1789 bool isPlaceholderType() const { 1790 return isPlaceholderTypeKind(getKind()); 1791 } 1792 1793 /// Determines whether this type is a placeholder type other than 1794 /// Overload. Most placeholder types require only syntactic 1795 /// information about their context in order to be resolved (e.g. 1796 /// whether it is a call expression), which means they can (and 1797 /// should) be resolved in an earlier "phase" of analysis. 1798 /// Overload expressions sometimes pick up further information 1799 /// from their context, like whether the context expects a 1800 /// specific function-pointer type, and so frequently need 1801 /// special treatment. 1802 bool isNonOverloadPlaceholderType() const { 1803 return getKind() > Overload; 1804 } 1805 1806 static bool classof(const Type *T) { return T->getTypeClass() == Builtin; } 1807 static bool classof(const BuiltinType *) { return true; } 1808}; 1809 1810/// ComplexType - C99 6.2.5p11 - Complex values. This supports the C99 complex 1811/// types (_Complex float etc) as well as the GCC integer complex extensions. 1812/// 1813class ComplexType : public Type, public llvm::FoldingSetNode { 1814 QualType ElementType; 1815 ComplexType(QualType Element, QualType CanonicalPtr) : 1816 Type(Complex, CanonicalPtr, Element->isDependentType(), 1817 Element->isInstantiationDependentType(), 1818 Element->isVariablyModifiedType(), 1819 Element->containsUnexpandedParameterPack()), 1820 ElementType(Element) { 1821 } 1822 friend class ASTContext; // ASTContext creates these. 1823 1824public: 1825 QualType getElementType() const { return ElementType; } 1826 1827 bool isSugared() const { return false; } 1828 QualType desugar() const { return QualType(this, 0); } 1829 1830 void Profile(llvm::FoldingSetNodeID &ID) { 1831 Profile(ID, getElementType()); 1832 } 1833 static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) { 1834 ID.AddPointer(Element.getAsOpaquePtr()); 1835 } 1836 1837 static bool classof(const Type *T) { return T->getTypeClass() == Complex; } 1838 static bool classof(const ComplexType *) { return true; } 1839}; 1840 1841/// ParenType - Sugar for parentheses used when specifying types. 1842/// 1843class ParenType : public Type, public llvm::FoldingSetNode { 1844 QualType Inner; 1845 1846 ParenType(QualType InnerType, QualType CanonType) : 1847 Type(Paren, CanonType, InnerType->isDependentType(), 1848 InnerType->isInstantiationDependentType(), 1849 InnerType->isVariablyModifiedType(), 1850 InnerType->containsUnexpandedParameterPack()), 1851 Inner(InnerType) { 1852 } 1853 friend class ASTContext; // ASTContext creates these. 1854 1855public: 1856 1857 QualType getInnerType() const { return Inner; } 1858 1859 bool isSugared() const { return true; } 1860 QualType desugar() const { return getInnerType(); } 1861 1862 void Profile(llvm::FoldingSetNodeID &ID) { 1863 Profile(ID, getInnerType()); 1864 } 1865 static void Profile(llvm::FoldingSetNodeID &ID, QualType Inner) { 1866 Inner.Profile(ID); 1867 } 1868 1869 static bool classof(const Type *T) { return T->getTypeClass() == Paren; } 1870 static bool classof(const ParenType *) { return true; } 1871}; 1872 1873/// PointerType - C99 6.7.5.1 - Pointer Declarators. 1874/// 1875class PointerType : public Type, public llvm::FoldingSetNode { 1876 QualType PointeeType; 1877 1878 PointerType(QualType Pointee, QualType CanonicalPtr) : 1879 Type(Pointer, CanonicalPtr, Pointee->isDependentType(), 1880 Pointee->isInstantiationDependentType(), 1881 Pointee->isVariablyModifiedType(), 1882 Pointee->containsUnexpandedParameterPack()), 1883 PointeeType(Pointee) { 1884 } 1885 friend class ASTContext; // ASTContext creates these. 1886 1887public: 1888 1889 QualType getPointeeType() const { return PointeeType; } 1890 1891 bool isSugared() const { return false; } 1892 QualType desugar() const { return QualType(this, 0); } 1893 1894 void Profile(llvm::FoldingSetNodeID &ID) { 1895 Profile(ID, getPointeeType()); 1896 } 1897 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { 1898 ID.AddPointer(Pointee.getAsOpaquePtr()); 1899 } 1900 1901 static bool classof(const Type *T) { return T->getTypeClass() == Pointer; } 1902 static bool classof(const PointerType *) { return true; } 1903}; 1904 1905/// BlockPointerType - pointer to a block type. 1906/// This type is to represent types syntactically represented as 1907/// "void (^)(int)", etc. Pointee is required to always be a function type. 1908/// 1909class BlockPointerType : public Type, public llvm::FoldingSetNode { 1910 QualType PointeeType; // Block is some kind of pointer type 1911 BlockPointerType(QualType Pointee, QualType CanonicalCls) : 1912 Type(BlockPointer, CanonicalCls, Pointee->isDependentType(), 1913 Pointee->isInstantiationDependentType(), 1914 Pointee->isVariablyModifiedType(), 1915 Pointee->containsUnexpandedParameterPack()), 1916 PointeeType(Pointee) { 1917 } 1918 friend class ASTContext; // ASTContext creates these. 1919 1920public: 1921 1922 // Get the pointee type. Pointee is required to always be a function type. 1923 QualType getPointeeType() const { return PointeeType; } 1924 1925 bool isSugared() const { return false; } 1926 QualType desugar() const { return QualType(this, 0); } 1927 1928 void Profile(llvm::FoldingSetNodeID &ID) { 1929 Profile(ID, getPointeeType()); 1930 } 1931 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { 1932 ID.AddPointer(Pointee.getAsOpaquePtr()); 1933 } 1934 1935 static bool classof(const Type *T) { 1936 return T->getTypeClass() == BlockPointer; 1937 } 1938 static bool classof(const BlockPointerType *) { return true; } 1939}; 1940 1941/// ReferenceType - Base for LValueReferenceType and RValueReferenceType 1942/// 1943class ReferenceType : public Type, public llvm::FoldingSetNode { 1944 QualType PointeeType; 1945 1946protected: 1947 ReferenceType(TypeClass tc, QualType Referencee, QualType CanonicalRef, 1948 bool SpelledAsLValue) : 1949 Type(tc, CanonicalRef, Referencee->isDependentType(), 1950 Referencee->isInstantiationDependentType(), 1951 Referencee->isVariablyModifiedType(), 1952 Referencee->containsUnexpandedParameterPack()), 1953 PointeeType(Referencee) 1954 { 1955 ReferenceTypeBits.SpelledAsLValue = SpelledAsLValue; 1956 ReferenceTypeBits.InnerRef = Referencee->isReferenceType(); 1957 } 1958 1959public: 1960 bool isSpelledAsLValue() const { return ReferenceTypeBits.SpelledAsLValue; } 1961 bool isInnerRef() const { return ReferenceTypeBits.InnerRef; } 1962 1963 QualType getPointeeTypeAsWritten() const { return PointeeType; } 1964 QualType getPointeeType() const { 1965 // FIXME: this might strip inner qualifiers; okay? 1966 const ReferenceType *T = this; 1967 while (T->isInnerRef()) 1968 T = T->PointeeType->castAs<ReferenceType>(); 1969 return T->PointeeType; 1970 } 1971 1972 void Profile(llvm::FoldingSetNodeID &ID) { 1973 Profile(ID, PointeeType, isSpelledAsLValue()); 1974 } 1975 static void Profile(llvm::FoldingSetNodeID &ID, 1976 QualType Referencee, 1977 bool SpelledAsLValue) { 1978 ID.AddPointer(Referencee.getAsOpaquePtr()); 1979 ID.AddBoolean(SpelledAsLValue); 1980 } 1981 1982 static bool classof(const Type *T) { 1983 return T->getTypeClass() == LValueReference || 1984 T->getTypeClass() == RValueReference; 1985 } 1986 static bool classof(const ReferenceType *) { return true; } 1987}; 1988 1989/// LValueReferenceType - C++ [dcl.ref] - Lvalue reference 1990/// 1991class LValueReferenceType : public ReferenceType { 1992 LValueReferenceType(QualType Referencee, QualType CanonicalRef, 1993 bool SpelledAsLValue) : 1994 ReferenceType(LValueReference, Referencee, CanonicalRef, SpelledAsLValue) 1995 {} 1996 friend class ASTContext; // ASTContext creates these 1997public: 1998 bool isSugared() const { return false; } 1999 QualType desugar() const { return QualType(this, 0); } 2000 2001 static bool classof(const Type *T) { 2002 return T->getTypeClass() == LValueReference; 2003 } 2004 static bool classof(const LValueReferenceType *) { return true; } 2005}; 2006 2007/// RValueReferenceType - C++0x [dcl.ref] - Rvalue reference 2008/// 2009class RValueReferenceType : public ReferenceType { 2010 RValueReferenceType(QualType Referencee, QualType CanonicalRef) : 2011 ReferenceType(RValueReference, Referencee, CanonicalRef, false) { 2012 } 2013 friend class ASTContext; // ASTContext creates these 2014public: 2015 bool isSugared() const { return false; } 2016 QualType desugar() const { return QualType(this, 0); } 2017 2018 static bool classof(const Type *T) { 2019 return T->getTypeClass() == RValueReference; 2020 } 2021 static bool classof(const RValueReferenceType *) { return true; } 2022}; 2023 2024/// MemberPointerType - C++ 8.3.3 - Pointers to members 2025/// 2026class MemberPointerType : public Type, public llvm::FoldingSetNode { 2027 QualType PointeeType; 2028 /// The class of which the pointee is a member. Must ultimately be a 2029 /// RecordType, but could be a typedef or a template parameter too. 2030 const Type *Class; 2031 2032 MemberPointerType(QualType Pointee, const Type *Cls, QualType CanonicalPtr) : 2033 Type(MemberPointer, CanonicalPtr, 2034 Cls->isDependentType() || Pointee->isDependentType(), 2035 (Cls->isInstantiationDependentType() || 2036 Pointee->isInstantiationDependentType()), 2037 Pointee->isVariablyModifiedType(), 2038 (Cls->containsUnexpandedParameterPack() || 2039 Pointee->containsUnexpandedParameterPack())), 2040 PointeeType(Pointee), Class(Cls) { 2041 } 2042 friend class ASTContext; // ASTContext creates these. 2043 2044public: 2045 QualType getPointeeType() const { return PointeeType; } 2046 2047 /// Returns true if the member type (i.e. the pointee type) is a 2048 /// function type rather than a data-member type. 2049 bool isMemberFunctionPointer() const { 2050 return PointeeType->isFunctionProtoType(); 2051 } 2052 2053 /// Returns true if the member type (i.e. the pointee type) is a 2054 /// data type rather than a function type. 2055 bool isMemberDataPointer() const { 2056 return !PointeeType->isFunctionProtoType(); 2057 } 2058 2059 const Type *getClass() const { return Class; } 2060 2061 bool isSugared() const { return false; } 2062 QualType desugar() const { return QualType(this, 0); } 2063 2064 void Profile(llvm::FoldingSetNodeID &ID) { 2065 Profile(ID, getPointeeType(), getClass()); 2066 } 2067 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee, 2068 const Type *Class) { 2069 ID.AddPointer(Pointee.getAsOpaquePtr()); 2070 ID.AddPointer(Class); 2071 } 2072 2073 static bool classof(const Type *T) { 2074 return T->getTypeClass() == MemberPointer; 2075 } 2076 static bool classof(const MemberPointerType *) { return true; } 2077}; 2078 2079/// ArrayType - C99 6.7.5.2 - Array Declarators. 2080/// 2081class ArrayType : public Type, public llvm::FoldingSetNode { 2082public: 2083 /// ArraySizeModifier - Capture whether this is a normal array (e.g. int X[4]) 2084 /// an array with a static size (e.g. int X[static 4]), or an array 2085 /// with a star size (e.g. int X[*]). 2086 /// 'static' is only allowed on function parameters. 2087 enum ArraySizeModifier { 2088 Normal, Static, Star 2089 }; 2090private: 2091 /// ElementType - The element type of the array. 2092 QualType ElementType; 2093 2094protected: 2095 // C++ [temp.dep.type]p1: 2096 // A type is dependent if it is... 2097 // - an array type constructed from any dependent type or whose 2098 // size is specified by a constant expression that is 2099 // value-dependent, 2100 ArrayType(TypeClass tc, QualType et, QualType can, 2101 ArraySizeModifier sm, unsigned tq, 2102 bool ContainsUnexpandedParameterPack) 2103 : Type(tc, can, et->isDependentType() || tc == DependentSizedArray, 2104 et->isInstantiationDependentType() || tc == DependentSizedArray, 2105 (tc == VariableArray || et->isVariablyModifiedType()), 2106 ContainsUnexpandedParameterPack), 2107 ElementType(et) { 2108 ArrayTypeBits.IndexTypeQuals = tq; 2109 ArrayTypeBits.SizeModifier = sm; 2110 } 2111 2112 friend class ASTContext; // ASTContext creates these. 2113 2114public: 2115 QualType getElementType() const { return ElementType; } 2116 ArraySizeModifier getSizeModifier() const { 2117 return ArraySizeModifier(ArrayTypeBits.SizeModifier); 2118 } 2119 Qualifiers getIndexTypeQualifiers() const { 2120 return Qualifiers::fromCVRMask(getIndexTypeCVRQualifiers()); 2121 } 2122 unsigned getIndexTypeCVRQualifiers() const { 2123 return ArrayTypeBits.IndexTypeQuals; 2124 } 2125 2126 static bool classof(const Type *T) { 2127 return T->getTypeClass() == ConstantArray || 2128 T->getTypeClass() == VariableArray || 2129 T->getTypeClass() == IncompleteArray || 2130 T->getTypeClass() == DependentSizedArray; 2131 } 2132 static bool classof(const ArrayType *) { return true; } 2133}; 2134 2135/// ConstantArrayType - This class represents the canonical version of 2136/// C arrays with a specified constant size. For example, the canonical 2137/// type for 'int A[4 + 4*100]' is a ConstantArrayType where the element 2138/// type is 'int' and the size is 404. 2139class ConstantArrayType : public ArrayType { 2140 llvm::APInt Size; // Allows us to unique the type. 2141 2142 ConstantArrayType(QualType et, QualType can, const llvm::APInt &size, 2143 ArraySizeModifier sm, unsigned tq) 2144 : ArrayType(ConstantArray, et, can, sm, tq, 2145 et->containsUnexpandedParameterPack()), 2146 Size(size) {} 2147protected: 2148 ConstantArrayType(TypeClass tc, QualType et, QualType can, 2149 const llvm::APInt &size, ArraySizeModifier sm, unsigned tq) 2150 : ArrayType(tc, et, can, sm, tq, et->containsUnexpandedParameterPack()), 2151 Size(size) {} 2152 friend class ASTContext; // ASTContext creates these. 2153public: 2154 const llvm::APInt &getSize() const { return Size; } 2155 bool isSugared() const { return false; } 2156 QualType desugar() const { return QualType(this, 0); } 2157 2158 2159 /// \brief Determine the number of bits required to address a member of 2160 // an array with the given element type and number of elements. 2161 static unsigned getNumAddressingBits(ASTContext &Context, 2162 QualType ElementType, 2163 const llvm::APInt &NumElements); 2164 2165 /// \brief Determine the maximum number of active bits that an array's size 2166 /// can require, which limits the maximum size of the array. 2167 static unsigned getMaxSizeBits(ASTContext &Context); 2168 2169 void Profile(llvm::FoldingSetNodeID &ID) { 2170 Profile(ID, getElementType(), getSize(), 2171 getSizeModifier(), getIndexTypeCVRQualifiers()); 2172 } 2173 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, 2174 const llvm::APInt &ArraySize, ArraySizeModifier SizeMod, 2175 unsigned TypeQuals) { 2176 ID.AddPointer(ET.getAsOpaquePtr()); 2177 ID.AddInteger(ArraySize.getZExtValue()); 2178 ID.AddInteger(SizeMod); 2179 ID.AddInteger(TypeQuals); 2180 } 2181 static bool classof(const Type *T) { 2182 return T->getTypeClass() == ConstantArray; 2183 } 2184 static bool classof(const ConstantArrayType *) { return true; } 2185}; 2186 2187/// IncompleteArrayType - This class represents C arrays with an unspecified 2188/// size. For example 'int A[]' has an IncompleteArrayType where the element 2189/// type is 'int' and the size is unspecified. 2190class IncompleteArrayType : public ArrayType { 2191 2192 IncompleteArrayType(QualType et, QualType can, 2193 ArraySizeModifier sm, unsigned tq) 2194 : ArrayType(IncompleteArray, et, can, sm, tq, 2195 et->containsUnexpandedParameterPack()) {} 2196 friend class ASTContext; // ASTContext creates these. 2197public: 2198 bool isSugared() const { return false; } 2199 QualType desugar() const { return QualType(this, 0); } 2200 2201 static bool classof(const Type *T) { 2202 return T->getTypeClass() == IncompleteArray; 2203 } 2204 static bool classof(const IncompleteArrayType *) { return true; } 2205 2206 friend class StmtIteratorBase; 2207 2208 void Profile(llvm::FoldingSetNodeID &ID) { 2209 Profile(ID, getElementType(), getSizeModifier(), 2210 getIndexTypeCVRQualifiers()); 2211 } 2212 2213 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, 2214 ArraySizeModifier SizeMod, unsigned TypeQuals) { 2215 ID.AddPointer(ET.getAsOpaquePtr()); 2216 ID.AddInteger(SizeMod); 2217 ID.AddInteger(TypeQuals); 2218 } 2219}; 2220 2221/// VariableArrayType - This class represents C arrays with a specified size 2222/// which is not an integer-constant-expression. For example, 'int s[x+foo()]'. 2223/// Since the size expression is an arbitrary expression, we store it as such. 2224/// 2225/// Note: VariableArrayType's aren't uniqued (since the expressions aren't) and 2226/// should not be: two lexically equivalent variable array types could mean 2227/// different things, for example, these variables do not have the same type 2228/// dynamically: 2229/// 2230/// void foo(int x) { 2231/// int Y[x]; 2232/// ++x; 2233/// int Z[x]; 2234/// } 2235/// 2236class VariableArrayType : public ArrayType { 2237 /// SizeExpr - An assignment expression. VLA's are only permitted within 2238 /// a function block. 2239 Stmt *SizeExpr; 2240 /// Brackets - The left and right array brackets. 2241 SourceRange Brackets; 2242 2243 VariableArrayType(QualType et, QualType can, Expr *e, 2244 ArraySizeModifier sm, unsigned tq, 2245 SourceRange brackets) 2246 : ArrayType(VariableArray, et, can, sm, tq, 2247 et->containsUnexpandedParameterPack()), 2248 SizeExpr((Stmt*) e), Brackets(brackets) {} 2249 friend class ASTContext; // ASTContext creates these. 2250 2251public: 2252 Expr *getSizeExpr() const { 2253 // We use C-style casts instead of cast<> here because we do not wish 2254 // to have a dependency of Type.h on Stmt.h/Expr.h. 2255 return (Expr*) SizeExpr; 2256 } 2257 SourceRange getBracketsRange() const { return Brackets; } 2258 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } 2259 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } 2260 2261 bool isSugared() const { return false; } 2262 QualType desugar() const { return QualType(this, 0); } 2263 2264 static bool classof(const Type *T) { 2265 return T->getTypeClass() == VariableArray; 2266 } 2267 static bool classof(const VariableArrayType *) { return true; } 2268 2269 friend class StmtIteratorBase; 2270 2271 void Profile(llvm::FoldingSetNodeID &ID) { 2272 llvm_unreachable("Cannot unique VariableArrayTypes."); 2273 } 2274}; 2275 2276/// DependentSizedArrayType - This type represents an array type in 2277/// C++ whose size is a value-dependent expression. For example: 2278/// 2279/// \code 2280/// template<typename T, int Size> 2281/// class array { 2282/// T data[Size]; 2283/// }; 2284/// \endcode 2285/// 2286/// For these types, we won't actually know what the array bound is 2287/// until template instantiation occurs, at which point this will 2288/// become either a ConstantArrayType or a VariableArrayType. 2289class DependentSizedArrayType : public ArrayType { 2290 const ASTContext &Context; 2291 2292 /// \brief An assignment expression that will instantiate to the 2293 /// size of the array. 2294 /// 2295 /// The expression itself might be NULL, in which case the array 2296 /// type will have its size deduced from an initializer. 2297 Stmt *SizeExpr; 2298 2299 /// Brackets - The left and right array brackets. 2300 SourceRange Brackets; 2301 2302 DependentSizedArrayType(const ASTContext &Context, QualType et, QualType can, 2303 Expr *e, ArraySizeModifier sm, unsigned tq, 2304 SourceRange brackets); 2305 2306 friend class ASTContext; // ASTContext creates these. 2307 2308public: 2309 Expr *getSizeExpr() const { 2310 // We use C-style casts instead of cast<> here because we do not wish 2311 // to have a dependency of Type.h on Stmt.h/Expr.h. 2312 return (Expr*) SizeExpr; 2313 } 2314 SourceRange getBracketsRange() const { return Brackets; } 2315 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } 2316 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } 2317 2318 bool isSugared() const { return false; } 2319 QualType desugar() const { return QualType(this, 0); } 2320 2321 static bool classof(const Type *T) { 2322 return T->getTypeClass() == DependentSizedArray; 2323 } 2324 static bool classof(const DependentSizedArrayType *) { return true; } 2325 2326 friend class StmtIteratorBase; 2327 2328 2329 void Profile(llvm::FoldingSetNodeID &ID) { 2330 Profile(ID, Context, getElementType(), 2331 getSizeModifier(), getIndexTypeCVRQualifiers(), getSizeExpr()); 2332 } 2333 2334 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 2335 QualType ET, ArraySizeModifier SizeMod, 2336 unsigned TypeQuals, Expr *E); 2337}; 2338 2339/// DependentSizedExtVectorType - This type represent an extended vector type 2340/// where either the type or size is dependent. For example: 2341/// @code 2342/// template<typename T, int Size> 2343/// class vector { 2344/// typedef T __attribute__((ext_vector_type(Size))) type; 2345/// } 2346/// @endcode 2347class DependentSizedExtVectorType : public Type, public llvm::FoldingSetNode { 2348 const ASTContext &Context; 2349 Expr *SizeExpr; 2350 /// ElementType - The element type of the array. 2351 QualType ElementType; 2352 SourceLocation loc; 2353 2354 DependentSizedExtVectorType(const ASTContext &Context, QualType ElementType, 2355 QualType can, Expr *SizeExpr, SourceLocation loc); 2356 2357 friend class ASTContext; 2358 2359public: 2360 Expr *getSizeExpr() const { return SizeExpr; } 2361 QualType getElementType() const { return ElementType; } 2362 SourceLocation getAttributeLoc() const { return loc; } 2363 2364 bool isSugared() const { return false; } 2365 QualType desugar() const { return QualType(this, 0); } 2366 2367 static bool classof(const Type *T) { 2368 return T->getTypeClass() == DependentSizedExtVector; 2369 } 2370 static bool classof(const DependentSizedExtVectorType *) { return true; } 2371 2372 void Profile(llvm::FoldingSetNodeID &ID) { 2373 Profile(ID, Context, getElementType(), getSizeExpr()); 2374 } 2375 2376 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 2377 QualType ElementType, Expr *SizeExpr); 2378}; 2379 2380 2381/// VectorType - GCC generic vector type. This type is created using 2382/// __attribute__((vector_size(n)), where "n" specifies the vector size in 2383/// bytes; or from an Altivec __vector or vector declaration. 2384/// Since the constructor takes the number of vector elements, the 2385/// client is responsible for converting the size into the number of elements. 2386class VectorType : public Type, public llvm::FoldingSetNode { 2387public: 2388 enum VectorKind { 2389 GenericVector, // not a target-specific vector type 2390 AltiVecVector, // is AltiVec vector 2391 AltiVecPixel, // is AltiVec 'vector Pixel' 2392 AltiVecBool, // is AltiVec 'vector bool ...' 2393 NeonVector, // is ARM Neon vector 2394 NeonPolyVector // is ARM Neon polynomial vector 2395 }; 2396protected: 2397 /// ElementType - The element type of the vector. 2398 QualType ElementType; 2399 2400 VectorType(QualType vecType, unsigned nElements, QualType canonType, 2401 VectorKind vecKind); 2402 2403 VectorType(TypeClass tc, QualType vecType, unsigned nElements, 2404 QualType canonType, VectorKind vecKind); 2405 2406 friend class ASTContext; // ASTContext creates these. 2407 2408public: 2409 2410 QualType getElementType() const { return ElementType; } 2411 unsigned getNumElements() const { return VectorTypeBits.NumElements; } 2412 2413 bool isSugared() const { return false; } 2414 QualType desugar() const { return QualType(this, 0); } 2415 2416 VectorKind getVectorKind() const { 2417 return VectorKind(VectorTypeBits.VecKind); 2418 } 2419 2420 void Profile(llvm::FoldingSetNodeID &ID) { 2421 Profile(ID, getElementType(), getNumElements(), 2422 getTypeClass(), getVectorKind()); 2423 } 2424 static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, 2425 unsigned NumElements, TypeClass TypeClass, 2426 VectorKind VecKind) { 2427 ID.AddPointer(ElementType.getAsOpaquePtr()); 2428 ID.AddInteger(NumElements); 2429 ID.AddInteger(TypeClass); 2430 ID.AddInteger(VecKind); 2431 } 2432 2433 static bool classof(const Type *T) { 2434 return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector; 2435 } 2436 static bool classof(const VectorType *) { return true; } 2437}; 2438 2439/// ExtVectorType - Extended vector type. This type is created using 2440/// __attribute__((ext_vector_type(n)), where "n" is the number of elements. 2441/// Unlike vector_size, ext_vector_type is only allowed on typedef's. This 2442/// class enables syntactic extensions, like Vector Components for accessing 2443/// points, colors, and textures (modeled after OpenGL Shading Language). 2444class ExtVectorType : public VectorType { 2445 ExtVectorType(QualType vecType, unsigned nElements, QualType canonType) : 2446 VectorType(ExtVector, vecType, nElements, canonType, GenericVector) {} 2447 friend class ASTContext; // ASTContext creates these. 2448public: 2449 static int getPointAccessorIdx(char c) { 2450 switch (c) { 2451 default: return -1; 2452 case 'x': return 0; 2453 case 'y': return 1; 2454 case 'z': return 2; 2455 case 'w': return 3; 2456 } 2457 } 2458 static int getNumericAccessorIdx(char c) { 2459 switch (c) { 2460 default: return -1; 2461 case '0': return 0; 2462 case '1': return 1; 2463 case '2': return 2; 2464 case '3': return 3; 2465 case '4': return 4; 2466 case '5': return 5; 2467 case '6': return 6; 2468 case '7': return 7; 2469 case '8': return 8; 2470 case '9': return 9; 2471 case 'A': 2472 case 'a': return 10; 2473 case 'B': 2474 case 'b': return 11; 2475 case 'C': 2476 case 'c': return 12; 2477 case 'D': 2478 case 'd': return 13; 2479 case 'E': 2480 case 'e': return 14; 2481 case 'F': 2482 case 'f': return 15; 2483 } 2484 } 2485 2486 static int getAccessorIdx(char c) { 2487 if (int idx = getPointAccessorIdx(c)+1) return idx-1; 2488 return getNumericAccessorIdx(c); 2489 } 2490 2491 bool isAccessorWithinNumElements(char c) const { 2492 if (int idx = getAccessorIdx(c)+1) 2493 return unsigned(idx-1) < getNumElements(); 2494 return false; 2495 } 2496 bool isSugared() const { return false; } 2497 QualType desugar() const { return QualType(this, 0); } 2498 2499 static bool classof(const Type *T) { 2500 return T->getTypeClass() == ExtVector; 2501 } 2502 static bool classof(const ExtVectorType *) { return true; } 2503}; 2504 2505/// FunctionType - C99 6.7.5.3 - Function Declarators. This is the common base 2506/// class of FunctionNoProtoType and FunctionProtoType. 2507/// 2508class FunctionType : public Type { 2509 // The type returned by the function. 2510 QualType ResultType; 2511 2512 public: 2513 /// ExtInfo - A class which abstracts out some details necessary for 2514 /// making a call. 2515 /// 2516 /// It is not actually used directly for storing this information in 2517 /// a FunctionType, although FunctionType does currently use the 2518 /// same bit-pattern. 2519 /// 2520 // If you add a field (say Foo), other than the obvious places (both, 2521 // constructors, compile failures), what you need to update is 2522 // * Operator== 2523 // * getFoo 2524 // * withFoo 2525 // * functionType. Add Foo, getFoo. 2526 // * ASTContext::getFooType 2527 // * ASTContext::mergeFunctionTypes 2528 // * FunctionNoProtoType::Profile 2529 // * FunctionProtoType::Profile 2530 // * TypePrinter::PrintFunctionProto 2531 // * AST read and write 2532 // * Codegen 2533 class ExtInfo { 2534 // Feel free to rearrange or add bits, but if you go over 8, 2535 // you'll need to adjust both the Bits field below and 2536 // Type::FunctionTypeBitfields. 2537 2538 // | CC |noreturn|produces|regparm| 2539 // |0 .. 2| 3 | 4 | 5 .. 7| 2540 // 2541 // regparm is either 0 (no regparm attribute) or the regparm value+1. 2542 enum { CallConvMask = 0x7 }; 2543 enum { NoReturnMask = 0x8 }; 2544 enum { ProducesResultMask = 0x10 }; 2545 enum { RegParmMask = ~(CallConvMask | NoReturnMask | ProducesResultMask), 2546 RegParmOffset = 5 }; // Assumed to be the last field 2547 2548 uint16_t Bits; 2549 2550 ExtInfo(unsigned Bits) : Bits(static_cast<uint16_t>(Bits)) {} 2551 2552 friend class FunctionType; 2553 2554 public: 2555 // Constructor with no defaults. Use this when you know that you 2556 // have all the elements (when reading an AST file for example). 2557 ExtInfo(bool noReturn, bool hasRegParm, unsigned regParm, CallingConv cc, 2558 bool producesResult) { 2559 assert((!hasRegParm || regParm < 7) && "Invalid regparm value"); 2560 Bits = ((unsigned) cc) | 2561 (noReturn ? NoReturnMask : 0) | 2562 (producesResult ? ProducesResultMask : 0) | 2563 (hasRegParm ? ((regParm + 1) << RegParmOffset) : 0); 2564 } 2565 2566 // Constructor with all defaults. Use when for example creating a 2567 // function know to use defaults. 2568 ExtInfo() : Bits(0) {} 2569 2570 bool getNoReturn() const { return Bits & NoReturnMask; } 2571 bool getProducesResult() const { return Bits & ProducesResultMask; } 2572 bool getHasRegParm() const { return (Bits >> RegParmOffset) != 0; } 2573 unsigned getRegParm() const { 2574 unsigned RegParm = Bits >> RegParmOffset; 2575 if (RegParm > 0) 2576 --RegParm; 2577 return RegParm; 2578 } 2579 CallingConv getCC() const { return CallingConv(Bits & CallConvMask); } 2580 2581 bool operator==(ExtInfo Other) const { 2582 return Bits == Other.Bits; 2583 } 2584 bool operator!=(ExtInfo Other) const { 2585 return Bits != Other.Bits; 2586 } 2587 2588 // Note that we don't have setters. That is by design, use 2589 // the following with methods instead of mutating these objects. 2590 2591 ExtInfo withNoReturn(bool noReturn) const { 2592 if (noReturn) 2593 return ExtInfo(Bits | NoReturnMask); 2594 else 2595 return ExtInfo(Bits & ~NoReturnMask); 2596 } 2597 2598 ExtInfo withProducesResult(bool producesResult) const { 2599 if (producesResult) 2600 return ExtInfo(Bits | ProducesResultMask); 2601 else 2602 return ExtInfo(Bits & ~ProducesResultMask); 2603 } 2604 2605 ExtInfo withRegParm(unsigned RegParm) const { 2606 assert(RegParm < 7 && "Invalid regparm value"); 2607 return ExtInfo((Bits & ~RegParmMask) | 2608 ((RegParm + 1) << RegParmOffset)); 2609 } 2610 2611 ExtInfo withCallingConv(CallingConv cc) const { 2612 return ExtInfo((Bits & ~CallConvMask) | (unsigned) cc); 2613 } 2614 2615 void Profile(llvm::FoldingSetNodeID &ID) const { 2616 ID.AddInteger(Bits); 2617 } 2618 }; 2619 2620protected: 2621 FunctionType(TypeClass tc, QualType res, 2622 unsigned typeQuals, RefQualifierKind RefQualifier, 2623 QualType Canonical, bool Dependent, 2624 bool InstantiationDependent, 2625 bool VariablyModified, bool ContainsUnexpandedParameterPack, 2626 ExtInfo Info) 2627 : Type(tc, Canonical, Dependent, InstantiationDependent, VariablyModified, 2628 ContainsUnexpandedParameterPack), 2629 ResultType(res) { 2630 FunctionTypeBits.ExtInfo = Info.Bits; 2631 FunctionTypeBits.TypeQuals = typeQuals; 2632 FunctionTypeBits.RefQualifier = static_cast<unsigned>(RefQualifier); 2633 } 2634 unsigned getTypeQuals() const { return FunctionTypeBits.TypeQuals; } 2635 2636 RefQualifierKind getRefQualifier() const { 2637 return static_cast<RefQualifierKind>(FunctionTypeBits.RefQualifier); 2638 } 2639 2640public: 2641 2642 QualType getResultType() const { return ResultType; } 2643 2644 bool getHasRegParm() const { return getExtInfo().getHasRegParm(); } 2645 unsigned getRegParmType() const { return getExtInfo().getRegParm(); } 2646 bool getNoReturnAttr() const { return getExtInfo().getNoReturn(); } 2647 CallingConv getCallConv() const { return getExtInfo().getCC(); } 2648 ExtInfo getExtInfo() const { return ExtInfo(FunctionTypeBits.ExtInfo); } 2649 2650 /// \brief Determine the type of an expression that calls a function of 2651 /// this type. 2652 QualType getCallResultType(ASTContext &Context) const { 2653 return getResultType().getNonLValueExprType(Context); 2654 } 2655 2656 static StringRef getNameForCallConv(CallingConv CC); 2657 2658 static bool classof(const Type *T) { 2659 return T->getTypeClass() == FunctionNoProto || 2660 T->getTypeClass() == FunctionProto; 2661 } 2662 static bool classof(const FunctionType *) { return true; } 2663}; 2664 2665/// FunctionNoProtoType - Represents a K&R-style 'int foo()' function, which has 2666/// no information available about its arguments. 2667class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode { 2668 FunctionNoProtoType(QualType Result, QualType Canonical, ExtInfo Info) 2669 : FunctionType(FunctionNoProto, Result, 0, RQ_None, Canonical, 2670 /*Dependent=*/false, /*InstantiationDependent=*/false, 2671 Result->isVariablyModifiedType(), 2672 /*ContainsUnexpandedParameterPack=*/false, Info) {} 2673 2674 friend class ASTContext; // ASTContext creates these. 2675 2676public: 2677 // No additional state past what FunctionType provides. 2678 2679 bool isSugared() const { return false; } 2680 QualType desugar() const { return QualType(this, 0); } 2681 2682 void Profile(llvm::FoldingSetNodeID &ID) { 2683 Profile(ID, getResultType(), getExtInfo()); 2684 } 2685 static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType, 2686 ExtInfo Info) { 2687 Info.Profile(ID); 2688 ID.AddPointer(ResultType.getAsOpaquePtr()); 2689 } 2690 2691 static bool classof(const Type *T) { 2692 return T->getTypeClass() == FunctionNoProto; 2693 } 2694 static bool classof(const FunctionNoProtoType *) { return true; } 2695}; 2696 2697/// FunctionProtoType - Represents a prototype with argument type info, e.g. 2698/// 'int foo(int)' or 'int foo(void)'. 'void' is represented as having no 2699/// arguments, not as having a single void argument. Such a type can have an 2700/// exception specification, but this specification is not part of the canonical 2701/// type. 2702class FunctionProtoType : public FunctionType, public llvm::FoldingSetNode { 2703public: 2704 /// ExtProtoInfo - Extra information about a function prototype. 2705 struct ExtProtoInfo { 2706 ExtProtoInfo() : 2707 Variadic(false), HasTrailingReturn(false), TypeQuals(0), 2708 ExceptionSpecType(EST_None), RefQualifier(RQ_None), 2709 NumExceptions(0), Exceptions(0), NoexceptExpr(0), 2710 ExceptionSpecDecl(0), ExceptionSpecTemplate(0), 2711 ConsumedArguments(0) {} 2712 2713 FunctionType::ExtInfo ExtInfo; 2714 bool Variadic : 1; 2715 bool HasTrailingReturn : 1; 2716 unsigned char TypeQuals; 2717 ExceptionSpecificationType ExceptionSpecType; 2718 RefQualifierKind RefQualifier; 2719 unsigned NumExceptions; 2720 const QualType *Exceptions; 2721 Expr *NoexceptExpr; 2722 FunctionDecl *ExceptionSpecDecl; 2723 FunctionDecl *ExceptionSpecTemplate; 2724 const bool *ConsumedArguments; 2725 }; 2726 2727private: 2728 /// \brief Determine whether there are any argument types that 2729 /// contain an unexpanded parameter pack. 2730 static bool containsAnyUnexpandedParameterPack(const QualType *ArgArray, 2731 unsigned numArgs) { 2732 for (unsigned Idx = 0; Idx < numArgs; ++Idx) 2733 if (ArgArray[Idx]->containsUnexpandedParameterPack()) 2734 return true; 2735 2736 return false; 2737 } 2738 2739 FunctionProtoType(QualType result, const QualType *args, unsigned numArgs, 2740 QualType canonical, const ExtProtoInfo &epi); 2741 2742 /// NumArgs - The number of arguments this function has, not counting '...'. 2743 unsigned NumArgs : 17; 2744 2745 /// NumExceptions - The number of types in the exception spec, if any. 2746 unsigned NumExceptions : 9; 2747 2748 /// ExceptionSpecType - The type of exception specification this function has. 2749 unsigned ExceptionSpecType : 3; 2750 2751 /// HasAnyConsumedArgs - Whether this function has any consumed arguments. 2752 unsigned HasAnyConsumedArgs : 1; 2753 2754 /// Variadic - Whether the function is variadic. 2755 unsigned Variadic : 1; 2756 2757 /// HasTrailingReturn - Whether this function has a trailing return type. 2758 unsigned HasTrailingReturn : 1; 2759 2760 // ArgInfo - There is an variable size array after the class in memory that 2761 // holds the argument types. 2762 2763 // Exceptions - There is another variable size array after ArgInfo that 2764 // holds the exception types. 2765 2766 // NoexceptExpr - Instead of Exceptions, there may be a single Expr* pointing 2767 // to the expression in the noexcept() specifier. 2768 2769 // ExceptionSpecDecl, ExceptionSpecTemplate - Instead of Exceptions, there may 2770 // be a pair of FunctionDecl* pointing to the function which should be used to 2771 // instantiate this function type's exception specification, and the function 2772 // from which it should be instantiated. 2773 2774 // ConsumedArgs - A variable size array, following Exceptions 2775 // and of length NumArgs, holding flags indicating which arguments 2776 // are consumed. This only appears if HasAnyConsumedArgs is true. 2777 2778 friend class ASTContext; // ASTContext creates these. 2779 2780 const bool *getConsumedArgsBuffer() const { 2781 assert(hasAnyConsumedArgs()); 2782 2783 // Find the end of the exceptions. 2784 Expr * const *eh_end = reinterpret_cast<Expr * const *>(arg_type_end()); 2785 if (getExceptionSpecType() != EST_ComputedNoexcept) 2786 eh_end += NumExceptions; 2787 else 2788 eh_end += 1; // NoexceptExpr 2789 2790 return reinterpret_cast<const bool*>(eh_end); 2791 } 2792 2793public: 2794 unsigned getNumArgs() const { return NumArgs; } 2795 QualType getArgType(unsigned i) const { 2796 assert(i < NumArgs && "Invalid argument number!"); 2797 return arg_type_begin()[i]; 2798 } 2799 2800 ExtProtoInfo getExtProtoInfo() const { 2801 ExtProtoInfo EPI; 2802 EPI.ExtInfo = getExtInfo(); 2803 EPI.Variadic = isVariadic(); 2804 EPI.HasTrailingReturn = hasTrailingReturn(); 2805 EPI.ExceptionSpecType = getExceptionSpecType(); 2806 EPI.TypeQuals = static_cast<unsigned char>(getTypeQuals()); 2807 EPI.RefQualifier = getRefQualifier(); 2808 if (EPI.ExceptionSpecType == EST_Dynamic) { 2809 EPI.NumExceptions = NumExceptions; 2810 EPI.Exceptions = exception_begin(); 2811 } else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) { 2812 EPI.NoexceptExpr = getNoexceptExpr(); 2813 } else if (EPI.ExceptionSpecType == EST_Uninstantiated) { 2814 EPI.ExceptionSpecDecl = getExceptionSpecDecl(); 2815 EPI.ExceptionSpecTemplate = getExceptionSpecTemplate(); 2816 } 2817 if (hasAnyConsumedArgs()) 2818 EPI.ConsumedArguments = getConsumedArgsBuffer(); 2819 return EPI; 2820 } 2821 2822 /// \brief Get the kind of exception specification on this function. 2823 ExceptionSpecificationType getExceptionSpecType() const { 2824 return static_cast<ExceptionSpecificationType>(ExceptionSpecType); 2825 } 2826 /// \brief Return whether this function has any kind of exception spec. 2827 bool hasExceptionSpec() const { 2828 return getExceptionSpecType() != EST_None; 2829 } 2830 /// \brief Return whether this function has a dynamic (throw) exception spec. 2831 bool hasDynamicExceptionSpec() const { 2832 return isDynamicExceptionSpec(getExceptionSpecType()); 2833 } 2834 /// \brief Return whether this function has a noexcept exception spec. 2835 bool hasNoexceptExceptionSpec() const { 2836 return isNoexceptExceptionSpec(getExceptionSpecType()); 2837 } 2838 /// \brief Result type of getNoexceptSpec(). 2839 enum NoexceptResult { 2840 NR_NoNoexcept, ///< There is no noexcept specifier. 2841 NR_BadNoexcept, ///< The noexcept specifier has a bad expression. 2842 NR_Dependent, ///< The noexcept specifier is dependent. 2843 NR_Throw, ///< The noexcept specifier evaluates to false. 2844 NR_Nothrow ///< The noexcept specifier evaluates to true. 2845 }; 2846 /// \brief Get the meaning of the noexcept spec on this function, if any. 2847 NoexceptResult getNoexceptSpec(ASTContext &Ctx) const; 2848 unsigned getNumExceptions() const { return NumExceptions; } 2849 QualType getExceptionType(unsigned i) const { 2850 assert(i < NumExceptions && "Invalid exception number!"); 2851 return exception_begin()[i]; 2852 } 2853 Expr *getNoexceptExpr() const { 2854 if (getExceptionSpecType() != EST_ComputedNoexcept) 2855 return 0; 2856 // NoexceptExpr sits where the arguments end. 2857 return *reinterpret_cast<Expr *const *>(arg_type_end()); 2858 } 2859 /// \brief If this function type has an uninstantiated exception 2860 /// specification, this is the function whose exception specification 2861 /// is represented by this type. 2862 FunctionDecl *getExceptionSpecDecl() const { 2863 if (getExceptionSpecType() != EST_Uninstantiated) 2864 return 0; 2865 return reinterpret_cast<FunctionDecl * const *>(arg_type_end())[0]; 2866 } 2867 /// \brief If this function type has an uninstantiated exception 2868 /// specification, this is the function whose exception specification 2869 /// should be instantiated to find the exception specification for 2870 /// this type. 2871 FunctionDecl *getExceptionSpecTemplate() const { 2872 if (getExceptionSpecType() != EST_Uninstantiated) 2873 return 0; 2874 return reinterpret_cast<FunctionDecl * const *>(arg_type_end())[1]; 2875 } 2876 bool isNothrow(ASTContext &Ctx) const { 2877 ExceptionSpecificationType EST = getExceptionSpecType(); 2878 assert(EST != EST_Delayed && EST != EST_Uninstantiated); 2879 if (EST == EST_DynamicNone || EST == EST_BasicNoexcept) 2880 return true; 2881 if (EST != EST_ComputedNoexcept) 2882 return false; 2883 return getNoexceptSpec(Ctx) == NR_Nothrow; 2884 } 2885 2886 bool isVariadic() const { return Variadic; } 2887 2888 /// \brief Determines whether this function prototype contains a 2889 /// parameter pack at the end. 2890 /// 2891 /// A function template whose last parameter is a parameter pack can be 2892 /// called with an arbitrary number of arguments, much like a variadic 2893 /// function. 2894 bool isTemplateVariadic() const; 2895 2896 bool hasTrailingReturn() const { return HasTrailingReturn; } 2897 2898 unsigned getTypeQuals() const { return FunctionType::getTypeQuals(); } 2899 2900 2901 /// \brief Retrieve the ref-qualifier associated with this function type. 2902 RefQualifierKind getRefQualifier() const { 2903 return FunctionType::getRefQualifier(); 2904 } 2905 2906 typedef const QualType *arg_type_iterator; 2907 arg_type_iterator arg_type_begin() const { 2908 return reinterpret_cast<const QualType *>(this+1); 2909 } 2910 arg_type_iterator arg_type_end() const { return arg_type_begin()+NumArgs; } 2911 2912 typedef const QualType *exception_iterator; 2913 exception_iterator exception_begin() const { 2914 // exceptions begin where arguments end 2915 return arg_type_end(); 2916 } 2917 exception_iterator exception_end() const { 2918 if (getExceptionSpecType() != EST_Dynamic) 2919 return exception_begin(); 2920 return exception_begin() + NumExceptions; 2921 } 2922 2923 bool hasAnyConsumedArgs() const { 2924 return HasAnyConsumedArgs; 2925 } 2926 bool isArgConsumed(unsigned I) const { 2927 assert(I < getNumArgs() && "argument index out of range!"); 2928 if (hasAnyConsumedArgs()) 2929 return getConsumedArgsBuffer()[I]; 2930 return false; 2931 } 2932 2933 bool isSugared() const { return false; } 2934 QualType desugar() const { return QualType(this, 0); } 2935 2936 void printExceptionSpecification(std::string &S, 2937 PrintingPolicy Policy) const; 2938 2939 static bool classof(const Type *T) { 2940 return T->getTypeClass() == FunctionProto; 2941 } 2942 static bool classof(const FunctionProtoType *) { return true; } 2943 2944 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx); 2945 static void Profile(llvm::FoldingSetNodeID &ID, QualType Result, 2946 arg_type_iterator ArgTys, unsigned NumArgs, 2947 const ExtProtoInfo &EPI, const ASTContext &Context); 2948}; 2949 2950 2951/// \brief Represents the dependent type named by a dependently-scoped 2952/// typename using declaration, e.g. 2953/// using typename Base<T>::foo; 2954/// Template instantiation turns these into the underlying type. 2955class UnresolvedUsingType : public Type { 2956 UnresolvedUsingTypenameDecl *Decl; 2957 2958 UnresolvedUsingType(const UnresolvedUsingTypenameDecl *D) 2959 : Type(UnresolvedUsing, QualType(), true, true, false, 2960 /*ContainsUnexpandedParameterPack=*/false), 2961 Decl(const_cast<UnresolvedUsingTypenameDecl*>(D)) {} 2962 friend class ASTContext; // ASTContext creates these. 2963public: 2964 2965 UnresolvedUsingTypenameDecl *getDecl() const { return Decl; } 2966 2967 bool isSugared() const { return false; } 2968 QualType desugar() const { return QualType(this, 0); } 2969 2970 static bool classof(const Type *T) { 2971 return T->getTypeClass() == UnresolvedUsing; 2972 } 2973 static bool classof(const UnresolvedUsingType *) { return true; } 2974 2975 void Profile(llvm::FoldingSetNodeID &ID) { 2976 return Profile(ID, Decl); 2977 } 2978 static void Profile(llvm::FoldingSetNodeID &ID, 2979 UnresolvedUsingTypenameDecl *D) { 2980 ID.AddPointer(D); 2981 } 2982}; 2983 2984 2985class TypedefType : public Type { 2986 TypedefNameDecl *Decl; 2987protected: 2988 TypedefType(TypeClass tc, const TypedefNameDecl *D, QualType can) 2989 : Type(tc, can, can->isDependentType(), 2990 can->isInstantiationDependentType(), 2991 can->isVariablyModifiedType(), 2992 /*ContainsUnexpandedParameterPack=*/false), 2993 Decl(const_cast<TypedefNameDecl*>(D)) { 2994 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 2995 } 2996 friend class ASTContext; // ASTContext creates these. 2997public: 2998 2999 TypedefNameDecl *getDecl() const { return Decl; } 3000 3001 bool isSugared() const { return true; } 3002 QualType desugar() const; 3003 3004 static bool classof(const Type *T) { return T->getTypeClass() == Typedef; } 3005 static bool classof(const TypedefType *) { return true; } 3006}; 3007 3008/// TypeOfExprType (GCC extension). 3009class TypeOfExprType : public Type { 3010 Expr *TOExpr; 3011 3012protected: 3013 TypeOfExprType(Expr *E, QualType can = QualType()); 3014 friend class ASTContext; // ASTContext creates these. 3015public: 3016 Expr *getUnderlyingExpr() const { return TOExpr; } 3017 3018 /// \brief Remove a single level of sugar. 3019 QualType desugar() const; 3020 3021 /// \brief Returns whether this type directly provides sugar. 3022 bool isSugared() const; 3023 3024 static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; } 3025 static bool classof(const TypeOfExprType *) { return true; } 3026}; 3027 3028/// \brief Internal representation of canonical, dependent 3029/// typeof(expr) types. 3030/// 3031/// This class is used internally by the ASTContext to manage 3032/// canonical, dependent types, only. Clients will only see instances 3033/// of this class via TypeOfExprType nodes. 3034class DependentTypeOfExprType 3035 : public TypeOfExprType, public llvm::FoldingSetNode { 3036 const ASTContext &Context; 3037 3038public: 3039 DependentTypeOfExprType(const ASTContext &Context, Expr *E) 3040 : TypeOfExprType(E), Context(Context) { } 3041 3042 void Profile(llvm::FoldingSetNodeID &ID) { 3043 Profile(ID, Context, getUnderlyingExpr()); 3044 } 3045 3046 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 3047 Expr *E); 3048}; 3049 3050/// TypeOfType (GCC extension). 3051class TypeOfType : public Type { 3052 QualType TOType; 3053 TypeOfType(QualType T, QualType can) 3054 : Type(TypeOf, can, T->isDependentType(), 3055 T->isInstantiationDependentType(), 3056 T->isVariablyModifiedType(), 3057 T->containsUnexpandedParameterPack()), 3058 TOType(T) { 3059 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 3060 } 3061 friend class ASTContext; // ASTContext creates these. 3062public: 3063 QualType getUnderlyingType() const { return TOType; } 3064 3065 /// \brief Remove a single level of sugar. 3066 QualType desugar() const { return getUnderlyingType(); } 3067 3068 /// \brief Returns whether this type directly provides sugar. 3069 bool isSugared() const { return true; } 3070 3071 static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; } 3072 static bool classof(const TypeOfType *) { return true; } 3073}; 3074 3075/// DecltypeType (C++0x) 3076class DecltypeType : public Type { 3077 Expr *E; 3078 QualType UnderlyingType; 3079 3080protected: 3081 DecltypeType(Expr *E, QualType underlyingType, QualType can = QualType()); 3082 friend class ASTContext; // ASTContext creates these. 3083public: 3084 Expr *getUnderlyingExpr() const { return E; } 3085 QualType getUnderlyingType() const { return UnderlyingType; } 3086 3087 /// \brief Remove a single level of sugar. 3088 QualType desugar() const; 3089 3090 /// \brief Returns whether this type directly provides sugar. 3091 bool isSugared() const; 3092 3093 static bool classof(const Type *T) { return T->getTypeClass() == Decltype; } 3094 static bool classof(const DecltypeType *) { return true; } 3095}; 3096 3097/// \brief Internal representation of canonical, dependent 3098/// decltype(expr) types. 3099/// 3100/// This class is used internally by the ASTContext to manage 3101/// canonical, dependent types, only. Clients will only see instances 3102/// of this class via DecltypeType nodes. 3103class DependentDecltypeType : public DecltypeType, public llvm::FoldingSetNode { 3104 const ASTContext &Context; 3105 3106public: 3107 DependentDecltypeType(const ASTContext &Context, Expr *E); 3108 3109 void Profile(llvm::FoldingSetNodeID &ID) { 3110 Profile(ID, Context, getUnderlyingExpr()); 3111 } 3112 3113 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 3114 Expr *E); 3115}; 3116 3117/// \brief A unary type transform, which is a type constructed from another 3118class UnaryTransformType : public Type { 3119public: 3120 enum UTTKind { 3121 EnumUnderlyingType 3122 }; 3123 3124private: 3125 /// The untransformed type. 3126 QualType BaseType; 3127 /// The transformed type if not dependent, otherwise the same as BaseType. 3128 QualType UnderlyingType; 3129 3130 UTTKind UKind; 3131protected: 3132 UnaryTransformType(QualType BaseTy, QualType UnderlyingTy, UTTKind UKind, 3133 QualType CanonicalTy); 3134 friend class ASTContext; 3135public: 3136 bool isSugared() const { return !isDependentType(); } 3137 QualType desugar() const { return UnderlyingType; } 3138 3139 QualType getUnderlyingType() const { return UnderlyingType; } 3140 QualType getBaseType() const { return BaseType; } 3141 3142 UTTKind getUTTKind() const { return UKind; } 3143 3144 static bool classof(const Type *T) { 3145 return T->getTypeClass() == UnaryTransform; 3146 } 3147 static bool classof(const UnaryTransformType *) { return true; } 3148}; 3149 3150class TagType : public Type { 3151 /// Stores the TagDecl associated with this type. The decl may point to any 3152 /// TagDecl that declares the entity. 3153 TagDecl * decl; 3154 3155 friend class ASTReader; 3156 3157protected: 3158 TagType(TypeClass TC, const TagDecl *D, QualType can); 3159 3160public: 3161 TagDecl *getDecl() const; 3162 3163 /// @brief Determines whether this type is in the process of being 3164 /// defined. 3165 bool isBeingDefined() const; 3166 3167 static bool classof(const Type *T) { 3168 return T->getTypeClass() >= TagFirst && T->getTypeClass() <= TagLast; 3169 } 3170 static bool classof(const TagType *) { return true; } 3171}; 3172 3173/// RecordType - This is a helper class that allows the use of isa/cast/dyncast 3174/// to detect TagType objects of structs/unions/classes. 3175class RecordType : public TagType { 3176protected: 3177 explicit RecordType(const RecordDecl *D) 3178 : TagType(Record, reinterpret_cast<const TagDecl*>(D), QualType()) { } 3179 explicit RecordType(TypeClass TC, RecordDecl *D) 3180 : TagType(TC, reinterpret_cast<const TagDecl*>(D), QualType()) { } 3181 friend class ASTContext; // ASTContext creates these. 3182public: 3183 3184 RecordDecl *getDecl() const { 3185 return reinterpret_cast<RecordDecl*>(TagType::getDecl()); 3186 } 3187 3188 // FIXME: This predicate is a helper to QualType/Type. It needs to 3189 // recursively check all fields for const-ness. If any field is declared 3190 // const, it needs to return false. 3191 bool hasConstFields() const { return false; } 3192 3193 bool isSugared() const { return false; } 3194 QualType desugar() const { return QualType(this, 0); } 3195 3196 static bool classof(const Type *T) { return T->getTypeClass() == Record; } 3197 static bool classof(const RecordType *) { return true; } 3198}; 3199 3200/// EnumType - This is a helper class that allows the use of isa/cast/dyncast 3201/// to detect TagType objects of enums. 3202class EnumType : public TagType { 3203 explicit EnumType(const EnumDecl *D) 3204 : TagType(Enum, reinterpret_cast<const TagDecl*>(D), QualType()) { } 3205 friend class ASTContext; // ASTContext creates these. 3206public: 3207 3208 EnumDecl *getDecl() const { 3209 return reinterpret_cast<EnumDecl*>(TagType::getDecl()); 3210 } 3211 3212 bool isSugared() const { return false; } 3213 QualType desugar() const { return QualType(this, 0); } 3214 3215 static bool classof(const Type *T) { return T->getTypeClass() == Enum; } 3216 static bool classof(const EnumType *) { return true; } 3217}; 3218 3219/// AttributedType - An attributed type is a type to which a type 3220/// attribute has been applied. The "modified type" is the 3221/// fully-sugared type to which the attributed type was applied; 3222/// generally it is not canonically equivalent to the attributed type. 3223/// The "equivalent type" is the minimally-desugared type which the 3224/// type is canonically equivalent to. 3225/// 3226/// For example, in the following attributed type: 3227/// int32_t __attribute__((vector_size(16))) 3228/// - the modified type is the TypedefType for int32_t 3229/// - the equivalent type is VectorType(16, int32_t) 3230/// - the canonical type is VectorType(16, int) 3231class AttributedType : public Type, public llvm::FoldingSetNode { 3232public: 3233 // It is really silly to have yet another attribute-kind enum, but 3234 // clang::attr::Kind doesn't currently cover the pure type attrs. 3235 enum Kind { 3236 // Expression operand. 3237 attr_address_space, 3238 attr_regparm, 3239 attr_vector_size, 3240 attr_neon_vector_type, 3241 attr_neon_polyvector_type, 3242 3243 FirstExprOperandKind = attr_address_space, 3244 LastExprOperandKind = attr_neon_polyvector_type, 3245 3246 // Enumerated operand (string or keyword). 3247 attr_objc_gc, 3248 attr_objc_ownership, 3249 attr_pcs, 3250 3251 FirstEnumOperandKind = attr_objc_gc, 3252 LastEnumOperandKind = attr_pcs, 3253 3254 // No operand. 3255 attr_noreturn, 3256 attr_cdecl, 3257 attr_fastcall, 3258 attr_stdcall, 3259 attr_thiscall, 3260 attr_pascal 3261 }; 3262 3263private: 3264 QualType ModifiedType; 3265 QualType EquivalentType; 3266 3267 friend class ASTContext; // creates these 3268 3269 AttributedType(QualType canon, Kind attrKind, 3270 QualType modified, QualType equivalent) 3271 : Type(Attributed, canon, canon->isDependentType(), 3272 canon->isInstantiationDependentType(), 3273 canon->isVariablyModifiedType(), 3274 canon->containsUnexpandedParameterPack()), 3275 ModifiedType(modified), EquivalentType(equivalent) { 3276 AttributedTypeBits.AttrKind = attrKind; 3277 } 3278 3279public: 3280 Kind getAttrKind() const { 3281 return static_cast<Kind>(AttributedTypeBits.AttrKind); 3282 } 3283 3284 QualType getModifiedType() const { return ModifiedType; } 3285 QualType getEquivalentType() const { return EquivalentType; } 3286 3287 bool isSugared() const { return true; } 3288 QualType desugar() const { return getEquivalentType(); } 3289 3290 void Profile(llvm::FoldingSetNodeID &ID) { 3291 Profile(ID, getAttrKind(), ModifiedType, EquivalentType); 3292 } 3293 3294 static void Profile(llvm::FoldingSetNodeID &ID, Kind attrKind, 3295 QualType modified, QualType equivalent) { 3296 ID.AddInteger(attrKind); 3297 ID.AddPointer(modified.getAsOpaquePtr()); 3298 ID.AddPointer(equivalent.getAsOpaquePtr()); 3299 } 3300 3301 static bool classof(const Type *T) { 3302 return T->getTypeClass() == Attributed; 3303 } 3304 static bool classof(const AttributedType *T) { return true; } 3305}; 3306 3307class TemplateTypeParmType : public Type, public llvm::FoldingSetNode { 3308 // Helper data collector for canonical types. 3309 struct CanonicalTTPTInfo { 3310 unsigned Depth : 15; 3311 unsigned ParameterPack : 1; 3312 unsigned Index : 16; 3313 }; 3314 3315 union { 3316 // Info for the canonical type. 3317 CanonicalTTPTInfo CanTTPTInfo; 3318 // Info for the non-canonical type. 3319 TemplateTypeParmDecl *TTPDecl; 3320 }; 3321 3322 /// Build a non-canonical type. 3323 TemplateTypeParmType(TemplateTypeParmDecl *TTPDecl, QualType Canon) 3324 : Type(TemplateTypeParm, Canon, /*Dependent=*/true, 3325 /*InstantiationDependent=*/true, 3326 /*VariablyModified=*/false, 3327 Canon->containsUnexpandedParameterPack()), 3328 TTPDecl(TTPDecl) { } 3329 3330 /// Build the canonical type. 3331 TemplateTypeParmType(unsigned D, unsigned I, bool PP) 3332 : Type(TemplateTypeParm, QualType(this, 0), 3333 /*Dependent=*/true, 3334 /*InstantiationDependent=*/true, 3335 /*VariablyModified=*/false, PP) { 3336 CanTTPTInfo.Depth = D; 3337 CanTTPTInfo.Index = I; 3338 CanTTPTInfo.ParameterPack = PP; 3339 } 3340 3341 friend class ASTContext; // ASTContext creates these 3342 3343 const CanonicalTTPTInfo& getCanTTPTInfo() const { 3344 QualType Can = getCanonicalTypeInternal(); 3345 return Can->castAs<TemplateTypeParmType>()->CanTTPTInfo; 3346 } 3347 3348public: 3349 unsigned getDepth() const { return getCanTTPTInfo().Depth; } 3350 unsigned getIndex() const { return getCanTTPTInfo().Index; } 3351 bool isParameterPack() const { return getCanTTPTInfo().ParameterPack; } 3352 3353 TemplateTypeParmDecl *getDecl() const { 3354 return isCanonicalUnqualified() ? 0 : TTPDecl; 3355 } 3356 3357 IdentifierInfo *getIdentifier() const; 3358 3359 bool isSugared() const { return false; } 3360 QualType desugar() const { return QualType(this, 0); } 3361 3362 void Profile(llvm::FoldingSetNodeID &ID) { 3363 Profile(ID, getDepth(), getIndex(), isParameterPack(), getDecl()); 3364 } 3365 3366 static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth, 3367 unsigned Index, bool ParameterPack, 3368 TemplateTypeParmDecl *TTPDecl) { 3369 ID.AddInteger(Depth); 3370 ID.AddInteger(Index); 3371 ID.AddBoolean(ParameterPack); 3372 ID.AddPointer(TTPDecl); 3373 } 3374 3375 static bool classof(const Type *T) { 3376 return T->getTypeClass() == TemplateTypeParm; 3377 } 3378 static bool classof(const TemplateTypeParmType *T) { return true; } 3379}; 3380 3381/// \brief Represents the result of substituting a type for a template 3382/// type parameter. 3383/// 3384/// Within an instantiated template, all template type parameters have 3385/// been replaced with these. They are used solely to record that a 3386/// type was originally written as a template type parameter; 3387/// therefore they are never canonical. 3388class SubstTemplateTypeParmType : public Type, public llvm::FoldingSetNode { 3389 // The original type parameter. 3390 const TemplateTypeParmType *Replaced; 3391 3392 SubstTemplateTypeParmType(const TemplateTypeParmType *Param, QualType Canon) 3393 : Type(SubstTemplateTypeParm, Canon, Canon->isDependentType(), 3394 Canon->isInstantiationDependentType(), 3395 Canon->isVariablyModifiedType(), 3396 Canon->containsUnexpandedParameterPack()), 3397 Replaced(Param) { } 3398 3399 friend class ASTContext; 3400 3401public: 3402 /// Gets the template parameter that was substituted for. 3403 const TemplateTypeParmType *getReplacedParameter() const { 3404 return Replaced; 3405 } 3406 3407 /// Gets the type that was substituted for the template 3408 /// parameter. 3409 QualType getReplacementType() const { 3410 return getCanonicalTypeInternal(); 3411 } 3412 3413 bool isSugared() const { return true; } 3414 QualType desugar() const { return getReplacementType(); } 3415 3416 void Profile(llvm::FoldingSetNodeID &ID) { 3417 Profile(ID, getReplacedParameter(), getReplacementType()); 3418 } 3419 static void Profile(llvm::FoldingSetNodeID &ID, 3420 const TemplateTypeParmType *Replaced, 3421 QualType Replacement) { 3422 ID.AddPointer(Replaced); 3423 ID.AddPointer(Replacement.getAsOpaquePtr()); 3424 } 3425 3426 static bool classof(const Type *T) { 3427 return T->getTypeClass() == SubstTemplateTypeParm; 3428 } 3429 static bool classof(const SubstTemplateTypeParmType *T) { return true; } 3430}; 3431 3432/// \brief Represents the result of substituting a set of types for a template 3433/// type parameter pack. 3434/// 3435/// When a pack expansion in the source code contains multiple parameter packs 3436/// and those parameter packs correspond to different levels of template 3437/// parameter lists, this type node is used to represent a template type 3438/// parameter pack from an outer level, which has already had its argument pack 3439/// substituted but that still lives within a pack expansion that itself 3440/// could not be instantiated. When actually performing a substitution into 3441/// that pack expansion (e.g., when all template parameters have corresponding 3442/// arguments), this type will be replaced with the \c SubstTemplateTypeParmType 3443/// at the current pack substitution index. 3444class SubstTemplateTypeParmPackType : public Type, public llvm::FoldingSetNode { 3445 /// \brief The original type parameter. 3446 const TemplateTypeParmType *Replaced; 3447 3448 /// \brief A pointer to the set of template arguments that this 3449 /// parameter pack is instantiated with. 3450 const TemplateArgument *Arguments; 3451 3452 /// \brief The number of template arguments in \c Arguments. 3453 unsigned NumArguments; 3454 3455 SubstTemplateTypeParmPackType(const TemplateTypeParmType *Param, 3456 QualType Canon, 3457 const TemplateArgument &ArgPack); 3458 3459 friend class ASTContext; 3460 3461public: 3462 IdentifierInfo *getIdentifier() const { return Replaced->getIdentifier(); } 3463 3464 /// Gets the template parameter that was substituted for. 3465 const TemplateTypeParmType *getReplacedParameter() const { 3466 return Replaced; 3467 } 3468 3469 bool isSugared() const { return false; } 3470 QualType desugar() const { return QualType(this, 0); } 3471 3472 TemplateArgument getArgumentPack() const; 3473 3474 void Profile(llvm::FoldingSetNodeID &ID); 3475 static void Profile(llvm::FoldingSetNodeID &ID, 3476 const TemplateTypeParmType *Replaced, 3477 const TemplateArgument &ArgPack); 3478 3479 static bool classof(const Type *T) { 3480 return T->getTypeClass() == SubstTemplateTypeParmPack; 3481 } 3482 static bool classof(const SubstTemplateTypeParmPackType *T) { return true; } 3483}; 3484 3485/// \brief Represents a C++0x auto type. 3486/// 3487/// These types are usually a placeholder for a deduced type. However, within 3488/// templates and before the initializer is attached, there is no deduced type 3489/// and an auto type is type-dependent and canonical. 3490class AutoType : public Type, public llvm::FoldingSetNode { 3491 AutoType(QualType DeducedType) 3492 : Type(Auto, DeducedType.isNull() ? QualType(this, 0) : DeducedType, 3493 /*Dependent=*/DeducedType.isNull(), 3494 /*InstantiationDependent=*/DeducedType.isNull(), 3495 /*VariablyModified=*/false, /*ContainsParameterPack=*/false) { 3496 assert((DeducedType.isNull() || !DeducedType->isDependentType()) && 3497 "deduced a dependent type for auto"); 3498 } 3499 3500 friend class ASTContext; // ASTContext creates these 3501 3502public: 3503 bool isSugared() const { return isDeduced(); } 3504 QualType desugar() const { return getCanonicalTypeInternal(); } 3505 3506 QualType getDeducedType() const { 3507 return isDeduced() ? getCanonicalTypeInternal() : QualType(); 3508 } 3509 bool isDeduced() const { 3510 return !isDependentType(); 3511 } 3512 3513 void Profile(llvm::FoldingSetNodeID &ID) { 3514 Profile(ID, getDeducedType()); 3515 } 3516 3517 static void Profile(llvm::FoldingSetNodeID &ID, 3518 QualType Deduced) { 3519 ID.AddPointer(Deduced.getAsOpaquePtr()); 3520 } 3521 3522 static bool classof(const Type *T) { 3523 return T->getTypeClass() == Auto; 3524 } 3525 static bool classof(const AutoType *T) { return true; } 3526}; 3527 3528/// \brief Represents a type template specialization; the template 3529/// must be a class template, a type alias template, or a template 3530/// template parameter. A template which cannot be resolved to one of 3531/// these, e.g. because it is written with a dependent scope 3532/// specifier, is instead represented as a 3533/// @c DependentTemplateSpecializationType. 3534/// 3535/// A non-dependent template specialization type is always "sugar", 3536/// typically for a @c RecordType. For example, a class template 3537/// specialization type of @c vector<int> will refer to a tag type for 3538/// the instantiation @c std::vector<int, std::allocator<int>> 3539/// 3540/// Template specializations are dependent if either the template or 3541/// any of the template arguments are dependent, in which case the 3542/// type may also be canonical. 3543/// 3544/// Instances of this type are allocated with a trailing array of 3545/// TemplateArguments, followed by a QualType representing the 3546/// non-canonical aliased type when the template is a type alias 3547/// template. 3548class TemplateSpecializationType 3549 : public Type, public llvm::FoldingSetNode { 3550 /// \brief The name of the template being specialized. This is 3551 /// either a TemplateName::Template (in which case it is a 3552 /// ClassTemplateDecl*, a TemplateTemplateParmDecl*, or a 3553 /// TypeAliasTemplateDecl*), a 3554 /// TemplateName::SubstTemplateTemplateParmPack, or a 3555 /// TemplateName::SubstTemplateTemplateParm (in which case the 3556 /// replacement must, recursively, be one of these). 3557 TemplateName Template; 3558 3559 /// \brief - The number of template arguments named in this class 3560 /// template specialization. 3561 unsigned NumArgs : 31; 3562 3563 /// \brief Whether this template specialization type is a substituted 3564 /// type alias. 3565 bool TypeAlias : 1; 3566 3567 TemplateSpecializationType(TemplateName T, 3568 const TemplateArgument *Args, 3569 unsigned NumArgs, QualType Canon, 3570 QualType Aliased); 3571 3572 friend class ASTContext; // ASTContext creates these 3573 3574public: 3575 /// \brief Determine whether any of the given template arguments are 3576 /// dependent. 3577 static bool anyDependentTemplateArguments(const TemplateArgument *Args, 3578 unsigned NumArgs, 3579 bool &InstantiationDependent); 3580 3581 static bool anyDependentTemplateArguments(const TemplateArgumentLoc *Args, 3582 unsigned NumArgs, 3583 bool &InstantiationDependent); 3584 3585 static bool anyDependentTemplateArguments(const TemplateArgumentListInfo &, 3586 bool &InstantiationDependent); 3587 3588 /// \brief Print a template argument list, including the '<' and '>' 3589 /// enclosing the template arguments. 3590 static std::string PrintTemplateArgumentList(const TemplateArgument *Args, 3591 unsigned NumArgs, 3592 const PrintingPolicy &Policy, 3593 bool SkipBrackets = false); 3594 3595 static std::string PrintTemplateArgumentList(const TemplateArgumentLoc *Args, 3596 unsigned NumArgs, 3597 const PrintingPolicy &Policy); 3598 3599 static std::string PrintTemplateArgumentList(const TemplateArgumentListInfo &, 3600 const PrintingPolicy &Policy); 3601 3602 /// True if this template specialization type matches a current 3603 /// instantiation in the context in which it is found. 3604 bool isCurrentInstantiation() const { 3605 return isa<InjectedClassNameType>(getCanonicalTypeInternal()); 3606 } 3607 3608 /// \brief Determine if this template specialization type is for a type alias 3609 /// template that has been substituted. 3610 /// 3611 /// Nearly every template specialization type whose template is an alias 3612 /// template will be substituted. However, this is not the case when 3613 /// the specialization contains a pack expansion but the template alias 3614 /// does not have a corresponding parameter pack, e.g., 3615 /// 3616 /// \code 3617 /// template<typename T, typename U, typename V> struct S; 3618 /// template<typename T, typename U> using A = S<T, int, U>; 3619 /// template<typename... Ts> struct X { 3620 /// typedef A<Ts...> type; // not a type alias 3621 /// }; 3622 /// \endcode 3623 bool isTypeAlias() const { return TypeAlias; } 3624 3625 /// Get the aliased type, if this is a specialization of a type alias 3626 /// template. 3627 QualType getAliasedType() const { 3628 assert(isTypeAlias() && "not a type alias template specialization"); 3629 return *reinterpret_cast<const QualType*>(end()); 3630 } 3631 3632 typedef const TemplateArgument * iterator; 3633 3634 iterator begin() const { return getArgs(); } 3635 iterator end() const; // defined inline in TemplateBase.h 3636 3637 /// \brief Retrieve the name of the template that we are specializing. 3638 TemplateName getTemplateName() const { return Template; } 3639 3640 /// \brief Retrieve the template arguments. 3641 const TemplateArgument *getArgs() const { 3642 return reinterpret_cast<const TemplateArgument *>(this + 1); 3643 } 3644 3645 /// \brief Retrieve the number of template arguments. 3646 unsigned getNumArgs() const { return NumArgs; } 3647 3648 /// \brief Retrieve a specific template argument as a type. 3649 /// \precondition @c isArgType(Arg) 3650 const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h 3651 3652 bool isSugared() const { 3653 return !isDependentType() || isCurrentInstantiation() || isTypeAlias(); 3654 } 3655 QualType desugar() const { return getCanonicalTypeInternal(); } 3656 3657 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) { 3658 Profile(ID, Template, getArgs(), NumArgs, Ctx); 3659 if (isTypeAlias()) 3660 getAliasedType().Profile(ID); 3661 } 3662 3663 static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T, 3664 const TemplateArgument *Args, 3665 unsigned NumArgs, 3666 const ASTContext &Context); 3667 3668 static bool classof(const Type *T) { 3669 return T->getTypeClass() == TemplateSpecialization; 3670 } 3671 static bool classof(const TemplateSpecializationType *T) { return true; } 3672}; 3673 3674/// \brief The injected class name of a C++ class template or class 3675/// template partial specialization. Used to record that a type was 3676/// spelled with a bare identifier rather than as a template-id; the 3677/// equivalent for non-templated classes is just RecordType. 3678/// 3679/// Injected class name types are always dependent. Template 3680/// instantiation turns these into RecordTypes. 3681/// 3682/// Injected class name types are always canonical. This works 3683/// because it is impossible to compare an injected class name type 3684/// with the corresponding non-injected template type, for the same 3685/// reason that it is impossible to directly compare template 3686/// parameters from different dependent contexts: injected class name 3687/// types can only occur within the scope of a particular templated 3688/// declaration, and within that scope every template specialization 3689/// will canonicalize to the injected class name (when appropriate 3690/// according to the rules of the language). 3691class InjectedClassNameType : public Type { 3692 CXXRecordDecl *Decl; 3693 3694 /// The template specialization which this type represents. 3695 /// For example, in 3696 /// template <class T> class A { ... }; 3697 /// this is A<T>, whereas in 3698 /// template <class X, class Y> class A<B<X,Y> > { ... }; 3699 /// this is A<B<X,Y> >. 3700 /// 3701 /// It is always unqualified, always a template specialization type, 3702 /// and always dependent. 3703 QualType InjectedType; 3704 3705 friend class ASTContext; // ASTContext creates these. 3706 friend class ASTReader; // FIXME: ASTContext::getInjectedClassNameType is not 3707 // currently suitable for AST reading, too much 3708 // interdependencies. 3709 InjectedClassNameType(CXXRecordDecl *D, QualType TST) 3710 : Type(InjectedClassName, QualType(), /*Dependent=*/true, 3711 /*InstantiationDependent=*/true, 3712 /*VariablyModified=*/false, 3713 /*ContainsUnexpandedParameterPack=*/false), 3714 Decl(D), InjectedType(TST) { 3715 assert(isa<TemplateSpecializationType>(TST)); 3716 assert(!TST.hasQualifiers()); 3717 assert(TST->isDependentType()); 3718 } 3719 3720public: 3721 QualType getInjectedSpecializationType() const { return InjectedType; } 3722 const TemplateSpecializationType *getInjectedTST() const { 3723 return cast<TemplateSpecializationType>(InjectedType.getTypePtr()); 3724 } 3725 3726 CXXRecordDecl *getDecl() const; 3727 3728 bool isSugared() const { return false; } 3729 QualType desugar() const { return QualType(this, 0); } 3730 3731 static bool classof(const Type *T) { 3732 return T->getTypeClass() == InjectedClassName; 3733 } 3734 static bool classof(const InjectedClassNameType *T) { return true; } 3735}; 3736 3737/// \brief The kind of a tag type. 3738enum TagTypeKind { 3739 /// \brief The "struct" keyword. 3740 TTK_Struct, 3741 /// \brief The "union" keyword. 3742 TTK_Union, 3743 /// \brief The "class" keyword. 3744 TTK_Class, 3745 /// \brief The "enum" keyword. 3746 TTK_Enum 3747}; 3748 3749/// \brief The elaboration keyword that precedes a qualified type name or 3750/// introduces an elaborated-type-specifier. 3751enum ElaboratedTypeKeyword { 3752 /// \brief The "struct" keyword introduces the elaborated-type-specifier. 3753 ETK_Struct, 3754 /// \brief The "union" keyword introduces the elaborated-type-specifier. 3755 ETK_Union, 3756 /// \brief The "class" keyword introduces the elaborated-type-specifier. 3757 ETK_Class, 3758 /// \brief The "enum" keyword introduces the elaborated-type-specifier. 3759 ETK_Enum, 3760 /// \brief The "typename" keyword precedes the qualified type name, e.g., 3761 /// \c typename T::type. 3762 ETK_Typename, 3763 /// \brief No keyword precedes the qualified type name. 3764 ETK_None 3765}; 3766 3767/// A helper class for Type nodes having an ElaboratedTypeKeyword. 3768/// The keyword in stored in the free bits of the base class. 3769/// Also provides a few static helpers for converting and printing 3770/// elaborated type keyword and tag type kind enumerations. 3771class TypeWithKeyword : public Type { 3772protected: 3773 TypeWithKeyword(ElaboratedTypeKeyword Keyword, TypeClass tc, 3774 QualType Canonical, bool Dependent, 3775 bool InstantiationDependent, bool VariablyModified, 3776 bool ContainsUnexpandedParameterPack) 3777 : Type(tc, Canonical, Dependent, InstantiationDependent, VariablyModified, 3778 ContainsUnexpandedParameterPack) { 3779 TypeWithKeywordBits.Keyword = Keyword; 3780 } 3781 3782public: 3783 ElaboratedTypeKeyword getKeyword() const { 3784 return static_cast<ElaboratedTypeKeyword>(TypeWithKeywordBits.Keyword); 3785 } 3786 3787 /// getKeywordForTypeSpec - Converts a type specifier (DeclSpec::TST) 3788 /// into an elaborated type keyword. 3789 static ElaboratedTypeKeyword getKeywordForTypeSpec(unsigned TypeSpec); 3790 3791 /// getTagTypeKindForTypeSpec - Converts a type specifier (DeclSpec::TST) 3792 /// into a tag type kind. It is an error to provide a type specifier 3793 /// which *isn't* a tag kind here. 3794 static TagTypeKind getTagTypeKindForTypeSpec(unsigned TypeSpec); 3795 3796 /// getKeywordForTagDeclKind - Converts a TagTypeKind into an 3797 /// elaborated type keyword. 3798 static ElaboratedTypeKeyword getKeywordForTagTypeKind(TagTypeKind Tag); 3799 3800 /// getTagTypeKindForKeyword - Converts an elaborated type keyword into 3801 // a TagTypeKind. It is an error to provide an elaborated type keyword 3802 /// which *isn't* a tag kind here. 3803 static TagTypeKind getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword); 3804 3805 static bool KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword); 3806 3807 static const char *getKeywordName(ElaboratedTypeKeyword Keyword); 3808 3809 static const char *getTagTypeKindName(TagTypeKind Kind) { 3810 return getKeywordName(getKeywordForTagTypeKind(Kind)); 3811 } 3812 3813 class CannotCastToThisType {}; 3814 static CannotCastToThisType classof(const Type *); 3815}; 3816 3817/// \brief Represents a type that was referred to using an elaborated type 3818/// keyword, e.g., struct S, or via a qualified name, e.g., N::M::type, 3819/// or both. 3820/// 3821/// This type is used to keep track of a type name as written in the 3822/// source code, including tag keywords and any nested-name-specifiers. 3823/// The type itself is always "sugar", used to express what was written 3824/// in the source code but containing no additional semantic information. 3825class ElaboratedType : public TypeWithKeyword, public llvm::FoldingSetNode { 3826 3827 /// \brief The nested name specifier containing the qualifier. 3828 NestedNameSpecifier *NNS; 3829 3830 /// \brief The type that this qualified name refers to. 3831 QualType NamedType; 3832 3833 ElaboratedType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, 3834 QualType NamedType, QualType CanonType) 3835 : TypeWithKeyword(Keyword, Elaborated, CanonType, 3836 NamedType->isDependentType(), 3837 NamedType->isInstantiationDependentType(), 3838 NamedType->isVariablyModifiedType(), 3839 NamedType->containsUnexpandedParameterPack()), 3840 NNS(NNS), NamedType(NamedType) { 3841 assert(!(Keyword == ETK_None && NNS == 0) && 3842 "ElaboratedType cannot have elaborated type keyword " 3843 "and name qualifier both null."); 3844 } 3845 3846 friend class ASTContext; // ASTContext creates these 3847 3848public: 3849 ~ElaboratedType(); 3850 3851 /// \brief Retrieve the qualification on this type. 3852 NestedNameSpecifier *getQualifier() const { return NNS; } 3853 3854 /// \brief Retrieve the type named by the qualified-id. 3855 QualType getNamedType() const { return NamedType; } 3856 3857 /// \brief Remove a single level of sugar. 3858 QualType desugar() const { return getNamedType(); } 3859 3860 /// \brief Returns whether this type directly provides sugar. 3861 bool isSugared() const { return true; } 3862 3863 void Profile(llvm::FoldingSetNodeID &ID) { 3864 Profile(ID, getKeyword(), NNS, NamedType); 3865 } 3866 3867 static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, 3868 NestedNameSpecifier *NNS, QualType NamedType) { 3869 ID.AddInteger(Keyword); 3870 ID.AddPointer(NNS); 3871 NamedType.Profile(ID); 3872 } 3873 3874 static bool classof(const Type *T) { 3875 return T->getTypeClass() == Elaborated; 3876 } 3877 static bool classof(const ElaboratedType *T) { return true; } 3878}; 3879 3880/// \brief Represents a qualified type name for which the type name is 3881/// dependent. 3882/// 3883/// DependentNameType represents a class of dependent types that involve a 3884/// dependent nested-name-specifier (e.g., "T::") followed by a (dependent) 3885/// name of a type. The DependentNameType may start with a "typename" (for a 3886/// typename-specifier), "class", "struct", "union", or "enum" (for a 3887/// dependent elaborated-type-specifier), or nothing (in contexts where we 3888/// know that we must be referring to a type, e.g., in a base class specifier). 3889class DependentNameType : public TypeWithKeyword, public llvm::FoldingSetNode { 3890 3891 /// \brief The nested name specifier containing the qualifier. 3892 NestedNameSpecifier *NNS; 3893 3894 /// \brief The type that this typename specifier refers to. 3895 const IdentifierInfo *Name; 3896 3897 DependentNameType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, 3898 const IdentifierInfo *Name, QualType CanonType) 3899 : TypeWithKeyword(Keyword, DependentName, CanonType, /*Dependent=*/true, 3900 /*InstantiationDependent=*/true, 3901 /*VariablyModified=*/false, 3902 NNS->containsUnexpandedParameterPack()), 3903 NNS(NNS), Name(Name) { 3904 assert(NNS->isDependent() && 3905 "DependentNameType requires a dependent nested-name-specifier"); 3906 } 3907 3908 friend class ASTContext; // ASTContext creates these 3909 3910public: 3911 /// \brief Retrieve the qualification on this type. 3912 NestedNameSpecifier *getQualifier() const { return NNS; } 3913 3914 /// \brief Retrieve the type named by the typename specifier as an 3915 /// identifier. 3916 /// 3917 /// This routine will return a non-NULL identifier pointer when the 3918 /// form of the original typename was terminated by an identifier, 3919 /// e.g., "typename T::type". 3920 const IdentifierInfo *getIdentifier() const { 3921 return Name; 3922 } 3923 3924 bool isSugared() const { return false; } 3925 QualType desugar() const { return QualType(this, 0); } 3926 3927 void Profile(llvm::FoldingSetNodeID &ID) { 3928 Profile(ID, getKeyword(), NNS, Name); 3929 } 3930 3931 static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, 3932 NestedNameSpecifier *NNS, const IdentifierInfo *Name) { 3933 ID.AddInteger(Keyword); 3934 ID.AddPointer(NNS); 3935 ID.AddPointer(Name); 3936 } 3937 3938 static bool classof(const Type *T) { 3939 return T->getTypeClass() == DependentName; 3940 } 3941 static bool classof(const DependentNameType *T) { return true; } 3942}; 3943 3944/// DependentTemplateSpecializationType - Represents a template 3945/// specialization type whose template cannot be resolved, e.g. 3946/// A<T>::template B<T> 3947class DependentTemplateSpecializationType : 3948 public TypeWithKeyword, public llvm::FoldingSetNode { 3949 3950 /// \brief The nested name specifier containing the qualifier. 3951 NestedNameSpecifier *NNS; 3952 3953 /// \brief The identifier of the template. 3954 const IdentifierInfo *Name; 3955 3956 /// \brief - The number of template arguments named in this class 3957 /// template specialization. 3958 unsigned NumArgs; 3959 3960 const TemplateArgument *getArgBuffer() const { 3961 return reinterpret_cast<const TemplateArgument*>(this+1); 3962 } 3963 TemplateArgument *getArgBuffer() { 3964 return reinterpret_cast<TemplateArgument*>(this+1); 3965 } 3966 3967 DependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, 3968 NestedNameSpecifier *NNS, 3969 const IdentifierInfo *Name, 3970 unsigned NumArgs, 3971 const TemplateArgument *Args, 3972 QualType Canon); 3973 3974 friend class ASTContext; // ASTContext creates these 3975 3976public: 3977 NestedNameSpecifier *getQualifier() const { return NNS; } 3978 const IdentifierInfo *getIdentifier() const { return Name; } 3979 3980 /// \brief Retrieve the template arguments. 3981 const TemplateArgument *getArgs() const { 3982 return getArgBuffer(); 3983 } 3984 3985 /// \brief Retrieve the number of template arguments. 3986 unsigned getNumArgs() const { return NumArgs; } 3987 3988 const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h 3989 3990 typedef const TemplateArgument * iterator; 3991 iterator begin() const { return getArgs(); } 3992 iterator end() const; // inline in TemplateBase.h 3993 3994 bool isSugared() const { return false; } 3995 QualType desugar() const { return QualType(this, 0); } 3996 3997 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { 3998 Profile(ID, Context, getKeyword(), NNS, Name, NumArgs, getArgs()); 3999 } 4000 4001 static void Profile(llvm::FoldingSetNodeID &ID, 4002 const ASTContext &Context, 4003 ElaboratedTypeKeyword Keyword, 4004 NestedNameSpecifier *Qualifier, 4005 const IdentifierInfo *Name, 4006 unsigned NumArgs, 4007 const TemplateArgument *Args); 4008 4009 static bool classof(const Type *T) { 4010 return T->getTypeClass() == DependentTemplateSpecialization; 4011 } 4012 static bool classof(const DependentTemplateSpecializationType *T) { 4013 return true; 4014 } 4015}; 4016 4017/// \brief Represents a pack expansion of types. 4018/// 4019/// Pack expansions are part of C++0x variadic templates. A pack 4020/// expansion contains a pattern, which itself contains one or more 4021/// "unexpanded" parameter packs. When instantiated, a pack expansion 4022/// produces a series of types, each instantiated from the pattern of 4023/// the expansion, where the Ith instantiation of the pattern uses the 4024/// Ith arguments bound to each of the unexpanded parameter packs. The 4025/// pack expansion is considered to "expand" these unexpanded 4026/// parameter packs. 4027/// 4028/// \code 4029/// template<typename ...Types> struct tuple; 4030/// 4031/// template<typename ...Types> 4032/// struct tuple_of_references { 4033/// typedef tuple<Types&...> type; 4034/// }; 4035/// \endcode 4036/// 4037/// Here, the pack expansion \c Types&... is represented via a 4038/// PackExpansionType whose pattern is Types&. 4039class PackExpansionType : public Type, public llvm::FoldingSetNode { 4040 /// \brief The pattern of the pack expansion. 4041 QualType Pattern; 4042 4043 /// \brief The number of expansions that this pack expansion will 4044 /// generate when substituted (+1), or indicates that 4045 /// 4046 /// This field will only have a non-zero value when some of the parameter 4047 /// packs that occur within the pattern have been substituted but others have 4048 /// not. 4049 unsigned NumExpansions; 4050 4051 PackExpansionType(QualType Pattern, QualType Canon, 4052 llvm::Optional<unsigned> NumExpansions) 4053 : Type(PackExpansion, Canon, /*Dependent=*/true, 4054 /*InstantiationDependent=*/true, 4055 /*VariableModified=*/Pattern->isVariablyModifiedType(), 4056 /*ContainsUnexpandedParameterPack=*/false), 4057 Pattern(Pattern), 4058 NumExpansions(NumExpansions? *NumExpansions + 1: 0) { } 4059 4060 friend class ASTContext; // ASTContext creates these 4061 4062public: 4063 /// \brief Retrieve the pattern of this pack expansion, which is the 4064 /// type that will be repeatedly instantiated when instantiating the 4065 /// pack expansion itself. 4066 QualType getPattern() const { return Pattern; } 4067 4068 /// \brief Retrieve the number of expansions that this pack expansion will 4069 /// generate, if known. 4070 llvm::Optional<unsigned> getNumExpansions() const { 4071 if (NumExpansions) 4072 return NumExpansions - 1; 4073 4074 return llvm::Optional<unsigned>(); 4075 } 4076 4077 bool isSugared() const { return false; } 4078 QualType desugar() const { return QualType(this, 0); } 4079 4080 void Profile(llvm::FoldingSetNodeID &ID) { 4081 Profile(ID, getPattern(), getNumExpansions()); 4082 } 4083 4084 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pattern, 4085 llvm::Optional<unsigned> NumExpansions) { 4086 ID.AddPointer(Pattern.getAsOpaquePtr()); 4087 ID.AddBoolean(NumExpansions); 4088 if (NumExpansions) 4089 ID.AddInteger(*NumExpansions); 4090 } 4091 4092 static bool classof(const Type *T) { 4093 return T->getTypeClass() == PackExpansion; 4094 } 4095 static bool classof(const PackExpansionType *T) { 4096 return true; 4097 } 4098}; 4099 4100/// ObjCObjectType - Represents a class type in Objective C. 4101/// Every Objective C type is a combination of a base type and a 4102/// list of protocols. 4103/// 4104/// Given the following declarations: 4105/// @class C; 4106/// @protocol P; 4107/// 4108/// 'C' is an ObjCInterfaceType C. It is sugar for an ObjCObjectType 4109/// with base C and no protocols. 4110/// 4111/// 'C<P>' is an ObjCObjectType with base C and protocol list [P]. 4112/// 4113/// 'id' is a TypedefType which is sugar for an ObjCPointerType whose 4114/// pointee is an ObjCObjectType with base BuiltinType::ObjCIdType 4115/// and no protocols. 4116/// 4117/// 'id<P>' is an ObjCPointerType whose pointee is an ObjCObjecType 4118/// with base BuiltinType::ObjCIdType and protocol list [P]. Eventually 4119/// this should get its own sugar class to better represent the source. 4120class ObjCObjectType : public Type { 4121 // ObjCObjectType.NumProtocols - the number of protocols stored 4122 // after the ObjCObjectPointerType node. 4123 // 4124 // These protocols are those written directly on the type. If 4125 // protocol qualifiers ever become additive, the iterators will need 4126 // to get kindof complicated. 4127 // 4128 // In the canonical object type, these are sorted alphabetically 4129 // and uniqued. 4130 4131 /// Either a BuiltinType or an InterfaceType or sugar for either. 4132 QualType BaseType; 4133 4134 ObjCProtocolDecl * const *getProtocolStorage() const { 4135 return const_cast<ObjCObjectType*>(this)->getProtocolStorage(); 4136 } 4137 4138 ObjCProtocolDecl **getProtocolStorage(); 4139 4140protected: 4141 ObjCObjectType(QualType Canonical, QualType Base, 4142 ObjCProtocolDecl * const *Protocols, unsigned NumProtocols); 4143 4144 enum Nonce_ObjCInterface { Nonce_ObjCInterface }; 4145 ObjCObjectType(enum Nonce_ObjCInterface) 4146 : Type(ObjCInterface, QualType(), false, false, false, false), 4147 BaseType(QualType(this_(), 0)) { 4148 ObjCObjectTypeBits.NumProtocols = 0; 4149 } 4150 4151public: 4152 /// getBaseType - Gets the base type of this object type. This is 4153 /// always (possibly sugar for) one of: 4154 /// - the 'id' builtin type (as opposed to the 'id' type visible to the 4155 /// user, which is a typedef for an ObjCPointerType) 4156 /// - the 'Class' builtin type (same caveat) 4157 /// - an ObjCObjectType (currently always an ObjCInterfaceType) 4158 QualType getBaseType() const { return BaseType; } 4159 4160 bool isObjCId() const { 4161 return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCId); 4162 } 4163 bool isObjCClass() const { 4164 return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCClass); 4165 } 4166 bool isObjCUnqualifiedId() const { return qual_empty() && isObjCId(); } 4167 bool isObjCUnqualifiedClass() const { return qual_empty() && isObjCClass(); } 4168 bool isObjCUnqualifiedIdOrClass() const { 4169 if (!qual_empty()) return false; 4170 if (const BuiltinType *T = getBaseType()->getAs<BuiltinType>()) 4171 return T->getKind() == BuiltinType::ObjCId || 4172 T->getKind() == BuiltinType::ObjCClass; 4173 return false; 4174 } 4175 bool isObjCQualifiedId() const { return !qual_empty() && isObjCId(); } 4176 bool isObjCQualifiedClass() const { return !qual_empty() && isObjCClass(); } 4177 4178 /// Gets the interface declaration for this object type, if the base type 4179 /// really is an interface. 4180 ObjCInterfaceDecl *getInterface() const; 4181 4182 typedef ObjCProtocolDecl * const *qual_iterator; 4183 4184 qual_iterator qual_begin() const { return getProtocolStorage(); } 4185 qual_iterator qual_end() const { return qual_begin() + getNumProtocols(); } 4186 4187 bool qual_empty() const { return getNumProtocols() == 0; } 4188 4189 /// getNumProtocols - Return the number of qualifying protocols in this 4190 /// interface type, or 0 if there are none. 4191 unsigned getNumProtocols() const { return ObjCObjectTypeBits.NumProtocols; } 4192 4193 /// \brief Fetch a protocol by index. 4194 ObjCProtocolDecl *getProtocol(unsigned I) const { 4195 assert(I < getNumProtocols() && "Out-of-range protocol access"); 4196 return qual_begin()[I]; 4197 } 4198 4199 bool isSugared() const { return false; } 4200 QualType desugar() const { return QualType(this, 0); } 4201 4202 static bool classof(const Type *T) { 4203 return T->getTypeClass() == ObjCObject || 4204 T->getTypeClass() == ObjCInterface; 4205 } 4206 static bool classof(const ObjCObjectType *) { return true; } 4207}; 4208 4209/// ObjCObjectTypeImpl - A class providing a concrete implementation 4210/// of ObjCObjectType, so as to not increase the footprint of 4211/// ObjCInterfaceType. Code outside of ASTContext and the core type 4212/// system should not reference this type. 4213class ObjCObjectTypeImpl : public ObjCObjectType, public llvm::FoldingSetNode { 4214 friend class ASTContext; 4215 4216 // If anyone adds fields here, ObjCObjectType::getProtocolStorage() 4217 // will need to be modified. 4218 4219 ObjCObjectTypeImpl(QualType Canonical, QualType Base, 4220 ObjCProtocolDecl * const *Protocols, 4221 unsigned NumProtocols) 4222 : ObjCObjectType(Canonical, Base, Protocols, NumProtocols) {} 4223 4224public: 4225 void Profile(llvm::FoldingSetNodeID &ID); 4226 static void Profile(llvm::FoldingSetNodeID &ID, 4227 QualType Base, 4228 ObjCProtocolDecl *const *protocols, 4229 unsigned NumProtocols); 4230}; 4231 4232inline ObjCProtocolDecl **ObjCObjectType::getProtocolStorage() { 4233 return reinterpret_cast<ObjCProtocolDecl**>( 4234 static_cast<ObjCObjectTypeImpl*>(this) + 1); 4235} 4236 4237/// ObjCInterfaceType - Interfaces are the core concept in Objective-C for 4238/// object oriented design. They basically correspond to C++ classes. There 4239/// are two kinds of interface types, normal interfaces like "NSString" and 4240/// qualified interfaces, which are qualified with a protocol list like 4241/// "NSString<NSCopyable, NSAmazing>". 4242/// 4243/// ObjCInterfaceType guarantees the following properties when considered 4244/// as a subtype of its superclass, ObjCObjectType: 4245/// - There are no protocol qualifiers. To reinforce this, code which 4246/// tries to invoke the protocol methods via an ObjCInterfaceType will 4247/// fail to compile. 4248/// - It is its own base type. That is, if T is an ObjCInterfaceType*, 4249/// T->getBaseType() == QualType(T, 0). 4250class ObjCInterfaceType : public ObjCObjectType { 4251 mutable ObjCInterfaceDecl *Decl; 4252 4253 ObjCInterfaceType(const ObjCInterfaceDecl *D) 4254 : ObjCObjectType(Nonce_ObjCInterface), 4255 Decl(const_cast<ObjCInterfaceDecl*>(D)) {} 4256 friend class ASTContext; // ASTContext creates these. 4257 friend class ASTReader; 4258 friend class ObjCInterfaceDecl; 4259 4260public: 4261 /// getDecl - Get the declaration of this interface. 4262 ObjCInterfaceDecl *getDecl() const { return Decl; } 4263 4264 bool isSugared() const { return false; } 4265 QualType desugar() const { return QualType(this, 0); } 4266 4267 static bool classof(const Type *T) { 4268 return T->getTypeClass() == ObjCInterface; 4269 } 4270 static bool classof(const ObjCInterfaceType *) { return true; } 4271 4272 // Nonsense to "hide" certain members of ObjCObjectType within this 4273 // class. People asking for protocols on an ObjCInterfaceType are 4274 // not going to get what they want: ObjCInterfaceTypes are 4275 // guaranteed to have no protocols. 4276 enum { 4277 qual_iterator, 4278 qual_begin, 4279 qual_end, 4280 getNumProtocols, 4281 getProtocol 4282 }; 4283}; 4284 4285inline ObjCInterfaceDecl *ObjCObjectType::getInterface() const { 4286 if (const ObjCInterfaceType *T = 4287 getBaseType()->getAs<ObjCInterfaceType>()) 4288 return T->getDecl(); 4289 return 0; 4290} 4291 4292/// ObjCObjectPointerType - Used to represent a pointer to an 4293/// Objective C object. These are constructed from pointer 4294/// declarators when the pointee type is an ObjCObjectType (or sugar 4295/// for one). In addition, the 'id' and 'Class' types are typedefs 4296/// for these, and the protocol-qualified types 'id<P>' and 'Class<P>' 4297/// are translated into these. 4298/// 4299/// Pointers to pointers to Objective C objects are still PointerTypes; 4300/// only the first level of pointer gets it own type implementation. 4301class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode { 4302 QualType PointeeType; 4303 4304 ObjCObjectPointerType(QualType Canonical, QualType Pointee) 4305 : Type(ObjCObjectPointer, Canonical, false, false, false, false), 4306 PointeeType(Pointee) {} 4307 friend class ASTContext; // ASTContext creates these. 4308 4309public: 4310 /// getPointeeType - Gets the type pointed to by this ObjC pointer. 4311 /// The result will always be an ObjCObjectType or sugar thereof. 4312 QualType getPointeeType() const { return PointeeType; } 4313 4314 /// getObjCObjectType - Gets the type pointed to by this ObjC 4315 /// pointer. This method always returns non-null. 4316 /// 4317 /// This method is equivalent to getPointeeType() except that 4318 /// it discards any typedefs (or other sugar) between this 4319 /// type and the "outermost" object type. So for: 4320 /// @class A; @protocol P; @protocol Q; 4321 /// typedef A<P> AP; 4322 /// typedef A A1; 4323 /// typedef A1<P> A1P; 4324 /// typedef A1P<Q> A1PQ; 4325 /// For 'A*', getObjectType() will return 'A'. 4326 /// For 'A<P>*', getObjectType() will return 'A<P>'. 4327 /// For 'AP*', getObjectType() will return 'A<P>'. 4328 /// For 'A1*', getObjectType() will return 'A'. 4329 /// For 'A1<P>*', getObjectType() will return 'A1<P>'. 4330 /// For 'A1P*', getObjectType() will return 'A1<P>'. 4331 /// For 'A1PQ*', getObjectType() will return 'A1<Q>', because 4332 /// adding protocols to a protocol-qualified base discards the 4333 /// old qualifiers (for now). But if it didn't, getObjectType() 4334 /// would return 'A1P<Q>' (and we'd have to make iterating over 4335 /// qualifiers more complicated). 4336 const ObjCObjectType *getObjectType() const { 4337 return PointeeType->castAs<ObjCObjectType>(); 4338 } 4339 4340 /// getInterfaceType - If this pointer points to an Objective C 4341 /// @interface type, gets the type for that interface. Any protocol 4342 /// qualifiers on the interface are ignored. 4343 /// 4344 /// \return null if the base type for this pointer is 'id' or 'Class' 4345 const ObjCInterfaceType *getInterfaceType() const { 4346 return getObjectType()->getBaseType()->getAs<ObjCInterfaceType>(); 4347 } 4348 4349 /// getInterfaceDecl - If this pointer points to an Objective @interface 4350 /// type, gets the declaration for that interface. 4351 /// 4352 /// \return null if the base type for this pointer is 'id' or 'Class' 4353 ObjCInterfaceDecl *getInterfaceDecl() const { 4354 return getObjectType()->getInterface(); 4355 } 4356 4357 /// isObjCIdType - True if this is equivalent to the 'id' type, i.e. if 4358 /// its object type is the primitive 'id' type with no protocols. 4359 bool isObjCIdType() const { 4360 return getObjectType()->isObjCUnqualifiedId(); 4361 } 4362 4363 /// isObjCClassType - True if this is equivalent to the 'Class' type, 4364 /// i.e. if its object tive is the primitive 'Class' type with no protocols. 4365 bool isObjCClassType() const { 4366 return getObjectType()->isObjCUnqualifiedClass(); 4367 } 4368 4369 /// isObjCQualifiedIdType - True if this is equivalent to 'id<P>' for some 4370 /// non-empty set of protocols. 4371 bool isObjCQualifiedIdType() const { 4372 return getObjectType()->isObjCQualifiedId(); 4373 } 4374 4375 /// isObjCQualifiedClassType - True if this is equivalent to 'Class<P>' for 4376 /// some non-empty set of protocols. 4377 bool isObjCQualifiedClassType() const { 4378 return getObjectType()->isObjCQualifiedClass(); 4379 } 4380 4381 /// An iterator over the qualifiers on the object type. Provided 4382 /// for convenience. This will always iterate over the full set of 4383 /// protocols on a type, not just those provided directly. 4384 typedef ObjCObjectType::qual_iterator qual_iterator; 4385 4386 qual_iterator qual_begin() const { 4387 return getObjectType()->qual_begin(); 4388 } 4389 qual_iterator qual_end() const { 4390 return getObjectType()->qual_end(); 4391 } 4392 bool qual_empty() const { return getObjectType()->qual_empty(); } 4393 4394 /// getNumProtocols - Return the number of qualifying protocols on 4395 /// the object type. 4396 unsigned getNumProtocols() const { 4397 return getObjectType()->getNumProtocols(); 4398 } 4399 4400 /// \brief Retrieve a qualifying protocol by index on the object 4401 /// type. 4402 ObjCProtocolDecl *getProtocol(unsigned I) const { 4403 return getObjectType()->getProtocol(I); 4404 } 4405 4406 bool isSugared() const { return false; } 4407 QualType desugar() const { return QualType(this, 0); } 4408 4409 void Profile(llvm::FoldingSetNodeID &ID) { 4410 Profile(ID, getPointeeType()); 4411 } 4412 static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { 4413 ID.AddPointer(T.getAsOpaquePtr()); 4414 } 4415 static bool classof(const Type *T) { 4416 return T->getTypeClass() == ObjCObjectPointer; 4417 } 4418 static bool classof(const ObjCObjectPointerType *) { return true; } 4419}; 4420 4421class AtomicType : public Type, public llvm::FoldingSetNode { 4422 QualType ValueType; 4423 4424 AtomicType(QualType ValTy, QualType Canonical) 4425 : Type(Atomic, Canonical, ValTy->isDependentType(), 4426 ValTy->isInstantiationDependentType(), 4427 ValTy->isVariablyModifiedType(), 4428 ValTy->containsUnexpandedParameterPack()), 4429 ValueType(ValTy) {} 4430 friend class ASTContext; // ASTContext creates these. 4431 4432 public: 4433 /// getValueType - Gets the type contained by this atomic type, i.e. 4434 /// the type returned by performing an atomic load of this atomic type. 4435 QualType getValueType() const { return ValueType; } 4436 4437 bool isSugared() const { return false; } 4438 QualType desugar() const { return QualType(this, 0); } 4439 4440 void Profile(llvm::FoldingSetNodeID &ID) { 4441 Profile(ID, getValueType()); 4442 } 4443 static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { 4444 ID.AddPointer(T.getAsOpaquePtr()); 4445 } 4446 static bool classof(const Type *T) { 4447 return T->getTypeClass() == Atomic; 4448 } 4449 static bool classof(const AtomicType *) { return true; } 4450}; 4451 4452/// A qualifier set is used to build a set of qualifiers. 4453class QualifierCollector : public Qualifiers { 4454public: 4455 QualifierCollector(Qualifiers Qs = Qualifiers()) : Qualifiers(Qs) {} 4456 4457 /// Collect any qualifiers on the given type and return an 4458 /// unqualified type. The qualifiers are assumed to be consistent 4459 /// with those already in the type. 4460 const Type *strip(QualType type) { 4461 addFastQualifiers(type.getLocalFastQualifiers()); 4462 if (!type.hasLocalNonFastQualifiers()) 4463 return type.getTypePtrUnsafe(); 4464 4465 const ExtQuals *extQuals = type.getExtQualsUnsafe(); 4466 addConsistentQualifiers(extQuals->getQualifiers()); 4467 return extQuals->getBaseType(); 4468 } 4469 4470 /// Apply the collected qualifiers to the given type. 4471 QualType apply(const ASTContext &Context, QualType QT) const; 4472 4473 /// Apply the collected qualifiers to the given type. 4474 QualType apply(const ASTContext &Context, const Type* T) const; 4475}; 4476 4477 4478// Inline function definitions. 4479 4480inline SplitQualType SplitQualType::getSingleStepDesugaredType() const { 4481 SplitQualType desugar = 4482 Ty->getLocallyUnqualifiedSingleStepDesugaredType().split(); 4483 desugar.Quals.addConsistentQualifiers(Quals); 4484 return desugar; 4485} 4486 4487inline const Type *QualType::getTypePtr() const { 4488 return getCommonPtr()->BaseType; 4489} 4490 4491inline const Type *QualType::getTypePtrOrNull() const { 4492 return (isNull() ? 0 : getCommonPtr()->BaseType); 4493} 4494 4495inline SplitQualType QualType::split() const { 4496 if (!hasLocalNonFastQualifiers()) 4497 return SplitQualType(getTypePtrUnsafe(), 4498 Qualifiers::fromFastMask(getLocalFastQualifiers())); 4499 4500 const ExtQuals *eq = getExtQualsUnsafe(); 4501 Qualifiers qs = eq->getQualifiers(); 4502 qs.addFastQualifiers(getLocalFastQualifiers()); 4503 return SplitQualType(eq->getBaseType(), qs); 4504} 4505 4506inline Qualifiers QualType::getLocalQualifiers() const { 4507 Qualifiers Quals; 4508 if (hasLocalNonFastQualifiers()) 4509 Quals = getExtQualsUnsafe()->getQualifiers(); 4510 Quals.addFastQualifiers(getLocalFastQualifiers()); 4511 return Quals; 4512} 4513 4514inline Qualifiers QualType::getQualifiers() const { 4515 Qualifiers quals = getCommonPtr()->CanonicalType.getLocalQualifiers(); 4516 quals.addFastQualifiers(getLocalFastQualifiers()); 4517 return quals; 4518} 4519 4520inline unsigned QualType::getCVRQualifiers() const { 4521 unsigned cvr = getCommonPtr()->CanonicalType.getLocalCVRQualifiers(); 4522 cvr |= getLocalCVRQualifiers(); 4523 return cvr; 4524} 4525 4526inline QualType QualType::getCanonicalType() const { 4527 QualType canon = getCommonPtr()->CanonicalType; 4528 return canon.withFastQualifiers(getLocalFastQualifiers()); 4529} 4530 4531inline bool QualType::isCanonical() const { 4532 return getTypePtr()->isCanonicalUnqualified(); 4533} 4534 4535inline bool QualType::isCanonicalAsParam() const { 4536 if (!isCanonical()) return false; 4537 if (hasLocalQualifiers()) return false; 4538 4539 const Type *T = getTypePtr(); 4540 if (T->isVariablyModifiedType() && T->hasSizedVLAType()) 4541 return false; 4542 4543 return !isa<FunctionType>(T) && !isa<ArrayType>(T); 4544} 4545 4546inline bool QualType::isConstQualified() const { 4547 return isLocalConstQualified() || 4548 getCommonPtr()->CanonicalType.isLocalConstQualified(); 4549} 4550 4551inline bool QualType::isRestrictQualified() const { 4552 return isLocalRestrictQualified() || 4553 getCommonPtr()->CanonicalType.isLocalRestrictQualified(); 4554} 4555 4556 4557inline bool QualType::isVolatileQualified() const { 4558 return isLocalVolatileQualified() || 4559 getCommonPtr()->CanonicalType.isLocalVolatileQualified(); 4560} 4561 4562inline bool QualType::hasQualifiers() const { 4563 return hasLocalQualifiers() || 4564 getCommonPtr()->CanonicalType.hasLocalQualifiers(); 4565} 4566 4567inline QualType QualType::getUnqualifiedType() const { 4568 if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) 4569 return QualType(getTypePtr(), 0); 4570 4571 return QualType(getSplitUnqualifiedTypeImpl(*this).Ty, 0); 4572} 4573 4574inline SplitQualType QualType::getSplitUnqualifiedType() const { 4575 if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) 4576 return split(); 4577 4578 return getSplitUnqualifiedTypeImpl(*this); 4579} 4580 4581inline void QualType::removeLocalConst() { 4582 removeLocalFastQualifiers(Qualifiers::Const); 4583} 4584 4585inline void QualType::removeLocalRestrict() { 4586 removeLocalFastQualifiers(Qualifiers::Restrict); 4587} 4588 4589inline void QualType::removeLocalVolatile() { 4590 removeLocalFastQualifiers(Qualifiers::Volatile); 4591} 4592 4593inline void QualType::removeLocalCVRQualifiers(unsigned Mask) { 4594 assert(!(Mask & ~Qualifiers::CVRMask) && "mask has non-CVR bits"); 4595 assert((int)Qualifiers::CVRMask == (int)Qualifiers::FastMask); 4596 4597 // Fast path: we don't need to touch the slow qualifiers. 4598 removeLocalFastQualifiers(Mask); 4599} 4600 4601/// getAddressSpace - Return the address space of this type. 4602inline unsigned QualType::getAddressSpace() const { 4603 return getQualifiers().getAddressSpace(); 4604} 4605 4606/// getObjCGCAttr - Return the gc attribute of this type. 4607inline Qualifiers::GC QualType::getObjCGCAttr() const { 4608 return getQualifiers().getObjCGCAttr(); 4609} 4610 4611inline FunctionType::ExtInfo getFunctionExtInfo(const Type &t) { 4612 if (const PointerType *PT = t.getAs<PointerType>()) { 4613 if (const FunctionType *FT = PT->getPointeeType()->getAs<FunctionType>()) 4614 return FT->getExtInfo(); 4615 } else if (const FunctionType *FT = t.getAs<FunctionType>()) 4616 return FT->getExtInfo(); 4617 4618 return FunctionType::ExtInfo(); 4619} 4620 4621inline FunctionType::ExtInfo getFunctionExtInfo(QualType t) { 4622 return getFunctionExtInfo(*t); 4623} 4624 4625/// isMoreQualifiedThan - Determine whether this type is more 4626/// qualified than the Other type. For example, "const volatile int" 4627/// is more qualified than "const int", "volatile int", and 4628/// "int". However, it is not more qualified than "const volatile 4629/// int". 4630inline bool QualType::isMoreQualifiedThan(QualType other) const { 4631 Qualifiers myQuals = getQualifiers(); 4632 Qualifiers otherQuals = other.getQualifiers(); 4633 return (myQuals != otherQuals && myQuals.compatiblyIncludes(otherQuals)); 4634} 4635 4636/// isAtLeastAsQualifiedAs - Determine whether this type is at last 4637/// as qualified as the Other type. For example, "const volatile 4638/// int" is at least as qualified as "const int", "volatile int", 4639/// "int", and "const volatile int". 4640inline bool QualType::isAtLeastAsQualifiedAs(QualType other) const { 4641 return getQualifiers().compatiblyIncludes(other.getQualifiers()); 4642} 4643 4644/// getNonReferenceType - If Type is a reference type (e.g., const 4645/// int&), returns the type that the reference refers to ("const 4646/// int"). Otherwise, returns the type itself. This routine is used 4647/// throughout Sema to implement C++ 5p6: 4648/// 4649/// If an expression initially has the type "reference to T" (8.3.2, 4650/// 8.5.3), the type is adjusted to "T" prior to any further 4651/// analysis, the expression designates the object or function 4652/// denoted by the reference, and the expression is an lvalue. 4653inline QualType QualType::getNonReferenceType() const { 4654 if (const ReferenceType *RefType = (*this)->getAs<ReferenceType>()) 4655 return RefType->getPointeeType(); 4656 else 4657 return *this; 4658} 4659 4660inline bool QualType::isCForbiddenLValueType() const { 4661 return ((getTypePtr()->isVoidType() && !hasQualifiers()) || 4662 getTypePtr()->isFunctionType()); 4663} 4664 4665/// \brief Tests whether the type is categorized as a fundamental type. 4666/// 4667/// \returns True for types specified in C++0x [basic.fundamental]. 4668inline bool Type::isFundamentalType() const { 4669 return isVoidType() || 4670 // FIXME: It's really annoying that we don't have an 4671 // 'isArithmeticType()' which agrees with the standard definition. 4672 (isArithmeticType() && !isEnumeralType()); 4673} 4674 4675/// \brief Tests whether the type is categorized as a compound type. 4676/// 4677/// \returns True for types specified in C++0x [basic.compound]. 4678inline bool Type::isCompoundType() const { 4679 // C++0x [basic.compound]p1: 4680 // Compound types can be constructed in the following ways: 4681 // -- arrays of objects of a given type [...]; 4682 return isArrayType() || 4683 // -- functions, which have parameters of given types [...]; 4684 isFunctionType() || 4685 // -- pointers to void or objects or functions [...]; 4686 isPointerType() || 4687 // -- references to objects or functions of a given type. [...] 4688 isReferenceType() || 4689 // -- classes containing a sequence of objects of various types, [...]; 4690 isRecordType() || 4691 // -- unions, which are classes capable of containing objects of different 4692 // types at different times; 4693 isUnionType() || 4694 // -- enumerations, which comprise a set of named constant values. [...]; 4695 isEnumeralType() || 4696 // -- pointers to non-static class members, [...]. 4697 isMemberPointerType(); 4698} 4699 4700inline bool Type::isFunctionType() const { 4701 return isa<FunctionType>(CanonicalType); 4702} 4703inline bool Type::isPointerType() const { 4704 return isa<PointerType>(CanonicalType); 4705} 4706inline bool Type::isAnyPointerType() const { 4707 return isPointerType() || isObjCObjectPointerType(); 4708} 4709inline bool Type::isBlockPointerType() const { 4710 return isa<BlockPointerType>(CanonicalType); 4711} 4712inline bool Type::isReferenceType() const { 4713 return isa<ReferenceType>(CanonicalType); 4714} 4715inline bool Type::isLValueReferenceType() const { 4716 return isa<LValueReferenceType>(CanonicalType); 4717} 4718inline bool Type::isRValueReferenceType() const { 4719 return isa<RValueReferenceType>(CanonicalType); 4720} 4721inline bool Type::isFunctionPointerType() const { 4722 if (const PointerType *T = getAs<PointerType>()) 4723 return T->getPointeeType()->isFunctionType(); 4724 else 4725 return false; 4726} 4727inline bool Type::isMemberPointerType() const { 4728 return isa<MemberPointerType>(CanonicalType); 4729} 4730inline bool Type::isMemberFunctionPointerType() const { 4731 if (const MemberPointerType* T = getAs<MemberPointerType>()) 4732 return T->isMemberFunctionPointer(); 4733 else 4734 return false; 4735} 4736inline bool Type::isMemberDataPointerType() const { 4737 if (const MemberPointerType* T = getAs<MemberPointerType>()) 4738 return T->isMemberDataPointer(); 4739 else 4740 return false; 4741} 4742inline bool Type::isArrayType() const { 4743 return isa<ArrayType>(CanonicalType); 4744} 4745inline bool Type::isConstantArrayType() const { 4746 return isa<ConstantArrayType>(CanonicalType); 4747} 4748inline bool Type::isIncompleteArrayType() const { 4749 return isa<IncompleteArrayType>(CanonicalType); 4750} 4751inline bool Type::isVariableArrayType() const { 4752 return isa<VariableArrayType>(CanonicalType); 4753} 4754inline bool Type::isDependentSizedArrayType() const { 4755 return isa<DependentSizedArrayType>(CanonicalType); 4756} 4757inline bool Type::isBuiltinType() const { 4758 return isa<BuiltinType>(CanonicalType); 4759} 4760inline bool Type::isRecordType() const { 4761 return isa<RecordType>(CanonicalType); 4762} 4763inline bool Type::isEnumeralType() const { 4764 return isa<EnumType>(CanonicalType); 4765} 4766inline bool Type::isAnyComplexType() const { 4767 return isa<ComplexType>(CanonicalType); 4768} 4769inline bool Type::isVectorType() const { 4770 return isa<VectorType>(CanonicalType); 4771} 4772inline bool Type::isExtVectorType() const { 4773 return isa<ExtVectorType>(CanonicalType); 4774} 4775inline bool Type::isObjCObjectPointerType() const { 4776 return isa<ObjCObjectPointerType>(CanonicalType); 4777} 4778inline bool Type::isObjCObjectType() const { 4779 return isa<ObjCObjectType>(CanonicalType); 4780} 4781inline bool Type::isObjCObjectOrInterfaceType() const { 4782 return isa<ObjCInterfaceType>(CanonicalType) || 4783 isa<ObjCObjectType>(CanonicalType); 4784} 4785inline bool Type::isAtomicType() const { 4786 return isa<AtomicType>(CanonicalType); 4787} 4788 4789inline bool Type::isObjCQualifiedIdType() const { 4790 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 4791 return OPT->isObjCQualifiedIdType(); 4792 return false; 4793} 4794inline bool Type::isObjCQualifiedClassType() const { 4795 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 4796 return OPT->isObjCQualifiedClassType(); 4797 return false; 4798} 4799inline bool Type::isObjCIdType() const { 4800 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 4801 return OPT->isObjCIdType(); 4802 return false; 4803} 4804inline bool Type::isObjCClassType() const { 4805 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 4806 return OPT->isObjCClassType(); 4807 return false; 4808} 4809inline bool Type::isObjCSelType() const { 4810 if (const PointerType *OPT = getAs<PointerType>()) 4811 return OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCSel); 4812 return false; 4813} 4814inline bool Type::isObjCBuiltinType() const { 4815 return isObjCIdType() || isObjCClassType() || isObjCSelType(); 4816} 4817inline bool Type::isTemplateTypeParmType() const { 4818 return isa<TemplateTypeParmType>(CanonicalType); 4819} 4820 4821inline bool Type::isSpecificBuiltinType(unsigned K) const { 4822 if (const BuiltinType *BT = getAs<BuiltinType>()) 4823 if (BT->getKind() == (BuiltinType::Kind) K) 4824 return true; 4825 return false; 4826} 4827 4828inline bool Type::isPlaceholderType() const { 4829 if (const BuiltinType *BT = dyn_cast<BuiltinType>(this)) 4830 return BT->isPlaceholderType(); 4831 return false; 4832} 4833 4834inline const BuiltinType *Type::getAsPlaceholderType() const { 4835 if (const BuiltinType *BT = dyn_cast<BuiltinType>(this)) 4836 if (BT->isPlaceholderType()) 4837 return BT; 4838 return 0; 4839} 4840 4841inline bool Type::isSpecificPlaceholderType(unsigned K) const { 4842 assert(BuiltinType::isPlaceholderTypeKind((BuiltinType::Kind) K)); 4843 if (const BuiltinType *BT = dyn_cast<BuiltinType>(this)) 4844 return (BT->getKind() == (BuiltinType::Kind) K); 4845 return false; 4846} 4847 4848inline bool Type::isNonOverloadPlaceholderType() const { 4849 if (const BuiltinType *BT = dyn_cast<BuiltinType>(this)) 4850 return BT->isNonOverloadPlaceholderType(); 4851 return false; 4852} 4853 4854inline bool Type::isVoidType() const { 4855 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 4856 return BT->getKind() == BuiltinType::Void; 4857 return false; 4858} 4859 4860inline bool Type::isHalfType() const { 4861 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 4862 return BT->getKind() == BuiltinType::Half; 4863 // FIXME: Should we allow complex __fp16? Probably not. 4864 return false; 4865} 4866 4867inline bool Type::isNullPtrType() const { 4868 if (const BuiltinType *BT = getAs<BuiltinType>()) 4869 return BT->getKind() == BuiltinType::NullPtr; 4870 return false; 4871} 4872 4873extern bool IsEnumDeclComplete(EnumDecl *); 4874extern bool IsEnumDeclScoped(EnumDecl *); 4875 4876inline bool Type::isIntegerType() const { 4877 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 4878 return BT->getKind() >= BuiltinType::Bool && 4879 BT->getKind() <= BuiltinType::Int128; 4880 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) { 4881 // Incomplete enum types are not treated as integer types. 4882 // FIXME: In C++, enum types are never integer types. 4883 return IsEnumDeclComplete(ET->getDecl()) && 4884 !IsEnumDeclScoped(ET->getDecl()); 4885 } 4886 return false; 4887} 4888 4889inline bool Type::isScalarType() const { 4890 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 4891 return BT->getKind() > BuiltinType::Void && 4892 BT->getKind() <= BuiltinType::NullPtr; 4893 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 4894 // Enums are scalar types, but only if they are defined. Incomplete enums 4895 // are not treated as scalar types. 4896 return IsEnumDeclComplete(ET->getDecl()); 4897 return isa<PointerType>(CanonicalType) || 4898 isa<BlockPointerType>(CanonicalType) || 4899 isa<MemberPointerType>(CanonicalType) || 4900 isa<ComplexType>(CanonicalType) || 4901 isa<ObjCObjectPointerType>(CanonicalType); 4902} 4903 4904inline bool Type::isIntegralOrEnumerationType() const { 4905 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 4906 return BT->getKind() >= BuiltinType::Bool && 4907 BT->getKind() <= BuiltinType::Int128; 4908 4909 // Check for a complete enum type; incomplete enum types are not properly an 4910 // enumeration type in the sense required here. 4911 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) 4912 return IsEnumDeclComplete(ET->getDecl()); 4913 4914 return false; 4915} 4916 4917inline bool Type::isBooleanType() const { 4918 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) 4919 return BT->getKind() == BuiltinType::Bool; 4920 return false; 4921} 4922 4923/// \brief Determines whether this is a type for which one can define 4924/// an overloaded operator. 4925inline bool Type::isOverloadableType() const { 4926 return isDependentType() || isRecordType() || isEnumeralType(); 4927} 4928 4929/// \brief Determines whether this type can decay to a pointer type. 4930inline bool Type::canDecayToPointerType() const { 4931 return isFunctionType() || isArrayType(); 4932} 4933 4934inline bool Type::hasPointerRepresentation() const { 4935 return (isPointerType() || isReferenceType() || isBlockPointerType() || 4936 isObjCObjectPointerType() || isNullPtrType()); 4937} 4938 4939inline bool Type::hasObjCPointerRepresentation() const { 4940 return isObjCObjectPointerType(); 4941} 4942 4943inline const Type *Type::getBaseElementTypeUnsafe() const { 4944 const Type *type = this; 4945 while (const ArrayType *arrayType = type->getAsArrayTypeUnsafe()) 4946 type = arrayType->getElementType().getTypePtr(); 4947 return type; 4948} 4949 4950/// Insertion operator for diagnostics. This allows sending QualType's into a 4951/// diagnostic with <<. 4952inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 4953 QualType T) { 4954 DB.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), 4955 DiagnosticsEngine::ak_qualtype); 4956 return DB; 4957} 4958 4959/// Insertion operator for partial diagnostics. This allows sending QualType's 4960/// into a diagnostic with <<. 4961inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD, 4962 QualType T) { 4963 PD.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), 4964 DiagnosticsEngine::ak_qualtype); 4965 return PD; 4966} 4967 4968// Helper class template that is used by Type::getAs to ensure that one does 4969// not try to look through a qualified type to get to an array type. 4970template<typename T, 4971 bool isArrayType = (llvm::is_same<T, ArrayType>::value || 4972 llvm::is_base_of<ArrayType, T>::value)> 4973struct ArrayType_cannot_be_used_with_getAs { }; 4974 4975template<typename T> 4976struct ArrayType_cannot_be_used_with_getAs<T, true>; 4977 4978/// Member-template getAs<specific type>'. 4979template <typename T> const T *Type::getAs() const { 4980 ArrayType_cannot_be_used_with_getAs<T> at; 4981 (void)at; 4982 4983 // If this is directly a T type, return it. 4984 if (const T *Ty = dyn_cast<T>(this)) 4985 return Ty; 4986 4987 // If the canonical form of this type isn't the right kind, reject it. 4988 if (!isa<T>(CanonicalType)) 4989 return 0; 4990 4991 // If this is a typedef for the type, strip the typedef off without 4992 // losing all typedef information. 4993 return cast<T>(getUnqualifiedDesugaredType()); 4994} 4995 4996inline const ArrayType *Type::getAsArrayTypeUnsafe() const { 4997 // If this is directly an array type, return it. 4998 if (const ArrayType *arr = dyn_cast<ArrayType>(this)) 4999 return arr; 5000 5001 // If the canonical form of this type isn't the right kind, reject it. 5002 if (!isa<ArrayType>(CanonicalType)) 5003 return 0; 5004 5005 // If this is a typedef for the type, strip the typedef off without 5006 // losing all typedef information. 5007 return cast<ArrayType>(getUnqualifiedDesugaredType()); 5008} 5009 5010template <typename T> const T *Type::castAs() const { 5011 ArrayType_cannot_be_used_with_getAs<T> at; 5012 (void) at; 5013 5014 assert(isa<T>(CanonicalType)); 5015 if (const T *ty = dyn_cast<T>(this)) return ty; 5016 return cast<T>(getUnqualifiedDesugaredType()); 5017} 5018 5019inline const ArrayType *Type::castAsArrayTypeUnsafe() const { 5020 assert(isa<ArrayType>(CanonicalType)); 5021 if (const ArrayType *arr = dyn_cast<ArrayType>(this)) return arr; 5022 return cast<ArrayType>(getUnqualifiedDesugaredType()); 5023} 5024 5025} // end namespace clang 5026 5027#endif 5028