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