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