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