Type.h revision 1e75edda2f03c9416845f86c8f69dfaff6eb262b
1//===--- Type.h - C Language Family Type Representation ---------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the Type interface and subclasses. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_AST_TYPE_H 15#define LLVM_CLANG_AST_TYPE_H 16 17#include "clang/Basic/Diagnostic.h" 18#include "clang/Basic/IdentifierTable.h" 19#include "clang/Basic/Linkage.h" 20#include "clang/Basic/PartialDiagnostic.h" 21#include "clang/AST/NestedNameSpecifier.h" 22#include "clang/AST/TemplateName.h" 23#include "llvm/Support/Casting.h" 24#include "llvm/Support/type_traits.h" 25#include "llvm/ADT/APSInt.h" 26#include "llvm/ADT/FoldingSet.h" 27#include "llvm/ADT/PointerIntPair.h" 28#include "llvm/ADT/PointerUnion.h" 29 30using llvm::isa; 31using llvm::cast; 32using llvm::cast_or_null; 33using llvm::dyn_cast; 34using llvm::dyn_cast_or_null; 35namespace clang { 36 enum { 37 TypeAlignmentInBits = 3, 38 TypeAlignment = 1 << TypeAlignmentInBits 39 }; 40 class Type; 41 class ExtQuals; 42 class QualType; 43} 44 45namespace llvm { 46 template <typename T> 47 class PointerLikeTypeTraits; 48 template<> 49 class PointerLikeTypeTraits< ::clang::Type*> { 50 public: 51 static inline void *getAsVoidPointer(::clang::Type *P) { return P; } 52 static inline ::clang::Type *getFromVoidPointer(void *P) { 53 return static_cast< ::clang::Type*>(P); 54 } 55 enum { NumLowBitsAvailable = clang::TypeAlignmentInBits }; 56 }; 57 template<> 58 class PointerLikeTypeTraits< ::clang::ExtQuals*> { 59 public: 60 static inline void *getAsVoidPointer(::clang::ExtQuals *P) { return P; } 61 static inline ::clang::ExtQuals *getFromVoidPointer(void *P) { 62 return static_cast< ::clang::ExtQuals*>(P); 63 } 64 enum { NumLowBitsAvailable = clang::TypeAlignmentInBits }; 65 }; 66 67 template <> 68 struct isPodLike<clang::QualType> { static const bool value = true; }; 69} 70 71namespace clang { 72 class ASTContext; 73 class TypedefDecl; 74 class TemplateDecl; 75 class TemplateTypeParmDecl; 76 class NonTypeTemplateParmDecl; 77 class TemplateTemplateParmDecl; 78 class TagDecl; 79 class RecordDecl; 80 class CXXRecordDecl; 81 class EnumDecl; 82 class FieldDecl; 83 class ObjCInterfaceDecl; 84 class ObjCProtocolDecl; 85 class ObjCMethodDecl; 86 class UnresolvedUsingTypenameDecl; 87 class Expr; 88 class Stmt; 89 class SourceLocation; 90 class StmtIteratorBase; 91 class TemplateArgument; 92 class TemplateArgumentLoc; 93 class TemplateArgumentListInfo; 94 class Type; 95 class ElaboratedType; 96 struct PrintingPolicy; 97 98 template <typename> class CanQual; 99 typedef CanQual<Type> CanQualType; 100 101 // Provide forward declarations for all of the *Type classes 102#define TYPE(Class, Base) class Class##Type; 103#include "clang/AST/TypeNodes.def" 104 105/// Qualifiers - The collection of all-type qualifiers we support. 106/// Clang supports five independent qualifiers: 107/// * C99: const, volatile, and restrict 108/// * Embedded C (TR18037): address spaces 109/// * Objective C: the GC attributes (none, weak, or strong) 110class Qualifiers { 111public: 112 enum TQ { // NOTE: These flags must be kept in sync with DeclSpec::TQ. 113 Const = 0x1, 114 Restrict = 0x2, 115 Volatile = 0x4, 116 CVRMask = Const | Volatile | Restrict 117 }; 118 119 enum GC { 120 GCNone = 0, 121 Weak, 122 Strong 123 }; 124 125 enum { 126 /// The maximum supported address space number. 127 /// 24 bits should be enough for anyone. 128 MaxAddressSpace = 0xffffffu, 129 130 /// The width of the "fast" qualifier mask. 131 FastWidth = 2, 132 133 /// The fast qualifier mask. 134 FastMask = (1 << FastWidth) - 1 135 }; 136 137 Qualifiers() : Mask(0) {} 138 139 static Qualifiers fromFastMask(unsigned Mask) { 140 Qualifiers Qs; 141 Qs.addFastQualifiers(Mask); 142 return Qs; 143 } 144 145 static Qualifiers fromCVRMask(unsigned CVR) { 146 Qualifiers Qs; 147 Qs.addCVRQualifiers(CVR); 148 return Qs; 149 } 150 151 // Deserialize qualifiers from an opaque representation. 152 static Qualifiers fromOpaqueValue(unsigned opaque) { 153 Qualifiers Qs; 154 Qs.Mask = opaque; 155 return Qs; 156 } 157 158 // Serialize these qualifiers into an opaque representation. 159 unsigned getAsOpaqueValue() const { 160 return Mask; 161 } 162 163 bool hasConst() const { return Mask & Const; } 164 void setConst(bool flag) { 165 Mask = (Mask & ~Const) | (flag ? Const : 0); 166 } 167 void removeConst() { Mask &= ~Const; } 168 void addConst() { Mask |= Const; } 169 170 bool hasVolatile() const { return Mask & Volatile; } 171 void setVolatile(bool flag) { 172 Mask = (Mask & ~Volatile) | (flag ? Volatile : 0); 173 } 174 void removeVolatile() { Mask &= ~Volatile; } 175 void addVolatile() { Mask |= Volatile; } 176 177 bool hasRestrict() const { return Mask & Restrict; } 178 void setRestrict(bool flag) { 179 Mask = (Mask & ~Restrict) | (flag ? Restrict : 0); 180 } 181 void removeRestrict() { Mask &= ~Restrict; } 182 void addRestrict() { Mask |= Restrict; } 183 184 bool hasCVRQualifiers() const { return getCVRQualifiers(); } 185 unsigned getCVRQualifiers() const { return Mask & CVRMask; } 186 void setCVRQualifiers(unsigned mask) { 187 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); 188 Mask = (Mask & ~CVRMask) | mask; 189 } 190 void removeCVRQualifiers(unsigned mask) { 191 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); 192 Mask &= ~mask; 193 } 194 void removeCVRQualifiers() { 195 removeCVRQualifiers(CVRMask); 196 } 197 void addCVRQualifiers(unsigned mask) { 198 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); 199 Mask |= mask; 200 } 201 202 bool hasObjCGCAttr() const { return Mask & GCAttrMask; } 203 GC getObjCGCAttr() const { return GC((Mask & GCAttrMask) >> GCAttrShift); } 204 void setObjCGCAttr(GC type) { 205 Mask = (Mask & ~GCAttrMask) | (type << GCAttrShift); 206 } 207 void removeObjCGCAttr() { setObjCGCAttr(GCNone); } 208 void addObjCGCAttr(GC type) { 209 assert(type); 210 setObjCGCAttr(type); 211 } 212 213 bool hasAddressSpace() const { return Mask & AddressSpaceMask; } 214 unsigned getAddressSpace() const { return Mask >> AddressSpaceShift; } 215 void setAddressSpace(unsigned space) { 216 assert(space <= MaxAddressSpace); 217 Mask = (Mask & ~AddressSpaceMask) 218 | (((uint32_t) space) << AddressSpaceShift); 219 } 220 void removeAddressSpace() { setAddressSpace(0); } 221 void addAddressSpace(unsigned space) { 222 assert(space); 223 setAddressSpace(space); 224 } 225 226 // Fast qualifiers are those that can be allocated directly 227 // on a QualType object. 228 bool hasFastQualifiers() const { return getFastQualifiers(); } 229 unsigned getFastQualifiers() const { return Mask & FastMask; } 230 void setFastQualifiers(unsigned mask) { 231 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); 232 Mask = (Mask & ~FastMask) | mask; 233 } 234 void removeFastQualifiers(unsigned mask) { 235 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); 236 Mask &= ~mask; 237 } 238 void removeFastQualifiers() { 239 removeFastQualifiers(FastMask); 240 } 241 void addFastQualifiers(unsigned mask) { 242 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); 243 Mask |= mask; 244 } 245 246 /// hasNonFastQualifiers - Return true if the set contains any 247 /// qualifiers which require an ExtQuals node to be allocated. 248 bool hasNonFastQualifiers() const { return Mask & ~FastMask; } 249 Qualifiers getNonFastQualifiers() const { 250 Qualifiers Quals = *this; 251 Quals.setFastQualifiers(0); 252 return Quals; 253 } 254 255 /// hasQualifiers - Return true if the set contains any qualifiers. 256 bool hasQualifiers() const { return Mask; } 257 bool empty() const { return !Mask; } 258 259 /// \brief Add the qualifiers from the given set to this set. 260 void addQualifiers(Qualifiers Q) { 261 // If the other set doesn't have any non-boolean qualifiers, just 262 // bit-or it in. 263 if (!(Q.Mask & ~CVRMask)) 264 Mask |= Q.Mask; 265 else { 266 Mask |= (Q.Mask & CVRMask); 267 if (Q.hasAddressSpace()) 268 addAddressSpace(Q.getAddressSpace()); 269 if (Q.hasObjCGCAttr()) 270 addObjCGCAttr(Q.getObjCGCAttr()); 271 } 272 } 273 274 bool isSupersetOf(Qualifiers Other) const; 275 276 bool operator==(Qualifiers Other) const { return Mask == Other.Mask; } 277 bool operator!=(Qualifiers Other) const { return Mask != Other.Mask; } 278 279 operator bool() const { return hasQualifiers(); } 280 281 Qualifiers &operator+=(Qualifiers R) { 282 addQualifiers(R); 283 return *this; 284 } 285 286 // Union two qualifier sets. If an enumerated qualifier appears 287 // in both sets, use the one from the right. 288 friend Qualifiers operator+(Qualifiers L, Qualifiers R) { 289 L += R; 290 return L; 291 } 292 293 Qualifiers &operator-=(Qualifiers R) { 294 Mask = Mask & ~(R.Mask); 295 return *this; 296 } 297 298 /// \brief Compute the difference between two qualifier sets. 299 friend Qualifiers operator-(Qualifiers L, Qualifiers R) { 300 L -= R; 301 return L; 302 } 303 304 std::string getAsString() const; 305 std::string getAsString(const PrintingPolicy &Policy) const { 306 std::string Buffer; 307 getAsStringInternal(Buffer, Policy); 308 return Buffer; 309 } 310 void getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const; 311 312 void Profile(llvm::FoldingSetNodeID &ID) const { 313 ID.AddInteger(Mask); 314 } 315 316private: 317 318 // bits: |0 1 2|3 .. 4|5 .. 31| 319 // |C R V|GCAttr|AddrSpace| 320 uint32_t Mask; 321 322 static const uint32_t GCAttrMask = 0x18; 323 static const uint32_t GCAttrShift = 3; 324 static const uint32_t AddressSpaceMask = ~(CVRMask | GCAttrMask); 325 static const uint32_t AddressSpaceShift = 5; 326}; 327 328 329/// ExtQuals - We can encode up to three bits in the low bits of a 330/// type pointer, but there are many more type qualifiers that we want 331/// to be able to apply to an arbitrary type. Therefore we have this 332/// struct, intended to be heap-allocated and used by QualType to 333/// store qualifiers. 334/// 335/// The current design tags the 'const' and 'restrict' qualifiers in 336/// two low bits on the QualType pointer; a third bit records whether 337/// the pointer is an ExtQuals node. 'const' was chosen because it is 338/// orders of magnitude more common than the other two qualifiers, in 339/// both library and user code. It's relatively rare to see 340/// 'restrict' in user code, but many standard C headers are saturated 341/// with 'restrict' declarations, so that representing them efficiently 342/// is a critical goal of this representation. 343class ExtQuals : public llvm::FoldingSetNode { 344 // NOTE: changing the fast qualifiers should be straightforward as 345 // long as you don't make 'const' non-fast. 346 // 1. Qualifiers: 347 // a) Modify the bitmasks (Qualifiers::TQ and DeclSpec::TQ). 348 // Fast qualifiers must occupy the low-order bits. 349 // b) Update Qualifiers::FastWidth and FastMask. 350 // 2. QualType: 351 // a) Update is{Volatile,Restrict}Qualified(), defined inline. 352 // b) Update remove{Volatile,Restrict}, defined near the end of 353 // this header. 354 // 3. ASTContext: 355 // a) Update get{Volatile,Restrict}Type. 356 357 /// Context - the context to which this set belongs. We save this 358 /// here so that QualifierCollector can use it to reapply extended 359 /// qualifiers to an arbitrary type without requiring a context to 360 /// be pushed through every single API dealing with qualifiers. 361 ASTContext& Context; 362 363 /// BaseType - the underlying type that this qualifies 364 const Type *BaseType; 365 366 /// Quals - the immutable set of qualifiers applied by this 367 /// node; always contains extended qualifiers. 368 Qualifiers Quals; 369 370public: 371 ExtQuals(ASTContext& Context, const Type *Base, Qualifiers Quals) 372 : Context(Context), BaseType(Base), Quals(Quals) 373 { 374 assert(Quals.hasNonFastQualifiers() 375 && "ExtQuals created with no fast qualifiers"); 376 assert(!Quals.hasFastQualifiers() 377 && "ExtQuals created with fast qualifiers"); 378 } 379 380 Qualifiers getQualifiers() const { return Quals; } 381 382 bool hasVolatile() const { return Quals.hasVolatile(); } 383 384 bool hasObjCGCAttr() const { return Quals.hasObjCGCAttr(); } 385 Qualifiers::GC getObjCGCAttr() const { return Quals.getObjCGCAttr(); } 386 387 bool hasAddressSpace() const { return Quals.hasAddressSpace(); } 388 unsigned getAddressSpace() const { return Quals.getAddressSpace(); } 389 390 const Type *getBaseType() const { return BaseType; } 391 392 ASTContext &getContext() const { return Context; } 393 394public: 395 void Profile(llvm::FoldingSetNodeID &ID) const { 396 Profile(ID, getBaseType(), Quals); 397 } 398 static void Profile(llvm::FoldingSetNodeID &ID, 399 const Type *BaseType, 400 Qualifiers Quals) { 401 assert(!Quals.hasFastQualifiers() && "fast qualifiers in ExtQuals hash!"); 402 ID.AddPointer(BaseType); 403 Quals.Profile(ID); 404 } 405}; 406 407/// CallingConv - Specifies the calling convention that a function uses. 408enum CallingConv { 409 CC_Default, 410 CC_C, // __attribute__((cdecl)) 411 CC_X86StdCall, // __attribute__((stdcall)) 412 CC_X86FastCall, // __attribute__((fastcall)) 413 CC_X86ThisCall, // __attribute__((thiscall)) 414 CC_X86Pascal // __attribute__((pascal)) 415}; 416 417 418/// QualType - For efficiency, we don't store CV-qualified types as nodes on 419/// their own: instead each reference to a type stores the qualifiers. This 420/// greatly reduces the number of nodes we need to allocate for types (for 421/// example we only need one for 'int', 'const int', 'volatile int', 422/// 'const volatile int', etc). 423/// 424/// As an added efficiency bonus, instead of making this a pair, we 425/// just store the two bits we care about in the low bits of the 426/// pointer. To handle the packing/unpacking, we make QualType be a 427/// simple wrapper class that acts like a smart pointer. A third bit 428/// indicates whether there are extended qualifiers present, in which 429/// case the pointer points to a special structure. 430class QualType { 431 // Thankfully, these are efficiently composable. 432 llvm::PointerIntPair<llvm::PointerUnion<const Type*,const ExtQuals*>, 433 Qualifiers::FastWidth> Value; 434 435 const ExtQuals *getExtQualsUnsafe() const { 436 return Value.getPointer().get<const ExtQuals*>(); 437 } 438 439 const Type *getTypePtrUnsafe() const { 440 return Value.getPointer().get<const Type*>(); 441 } 442 443 QualType getUnqualifiedTypeSlow() const; 444 445 friend class QualifierCollector; 446public: 447 QualType() {} 448 449 QualType(const Type *Ptr, unsigned Quals) 450 : Value(Ptr, Quals) {} 451 QualType(const ExtQuals *Ptr, unsigned Quals) 452 : Value(Ptr, Quals) {} 453 454 unsigned getLocalFastQualifiers() const { return Value.getInt(); } 455 void setLocalFastQualifiers(unsigned Quals) { Value.setInt(Quals); } 456 457 /// Retrieves a pointer to the underlying (unqualified) type. 458 /// This should really return a const Type, but it's not worth 459 /// changing all the users right now. 460 Type *getTypePtr() const { 461 if (hasLocalNonFastQualifiers()) 462 return const_cast<Type*>(getExtQualsUnsafe()->getBaseType()); 463 return const_cast<Type*>(getTypePtrUnsafe()); 464 } 465 466 void *getAsOpaquePtr() const { return Value.getOpaqueValue(); } 467 static QualType getFromOpaquePtr(void *Ptr) { 468 QualType T; 469 T.Value.setFromOpaqueValue(Ptr); 470 return T; 471 } 472 473 Type &operator*() const { 474 return *getTypePtr(); 475 } 476 477 Type *operator->() const { 478 return getTypePtr(); 479 } 480 481 bool isCanonical() const; 482 bool isCanonicalAsParam() const; 483 484 /// isNull - Return true if this QualType doesn't point to a type yet. 485 bool isNull() const { 486 return Value.getPointer().isNull(); 487 } 488 489 /// \brief Determine whether this particular QualType instance has the 490 /// "const" qualifier set, without looking through typedefs that may have 491 /// added "const" at a different level. 492 bool isLocalConstQualified() const { 493 return (getLocalFastQualifiers() & Qualifiers::Const); 494 } 495 496 /// \brief Determine whether this type is const-qualified. 497 bool isConstQualified() const; 498 499 /// \brief Determine whether this particular QualType instance has the 500 /// "restrict" qualifier set, without looking through typedefs that may have 501 /// added "restrict" at a different level. 502 bool isLocalRestrictQualified() const { 503 return (getLocalFastQualifiers() & Qualifiers::Restrict); 504 } 505 506 /// \brief Determine whether this type is restrict-qualified. 507 bool isRestrictQualified() const; 508 509 /// \brief Determine whether this particular QualType instance has the 510 /// "volatile" qualifier set, without looking through typedefs that may have 511 /// added "volatile" at a different level. 512 bool isLocalVolatileQualified() const { 513 return (hasLocalNonFastQualifiers() && getExtQualsUnsafe()->hasVolatile()); 514 } 515 516 /// \brief Determine whether this type is volatile-qualified. 517 bool isVolatileQualified() const; 518 519 /// \brief Determine whether this particular QualType instance has any 520 /// qualifiers, without looking through any typedefs that might add 521 /// qualifiers at a different level. 522 bool hasLocalQualifiers() const { 523 return getLocalFastQualifiers() || hasLocalNonFastQualifiers(); 524 } 525 526 /// \brief Determine whether this type has any qualifiers. 527 bool hasQualifiers() const; 528 529 /// \brief Determine whether this particular QualType instance has any 530 /// "non-fast" qualifiers, e.g., those that are stored in an ExtQualType 531 /// instance. 532 bool hasLocalNonFastQualifiers() const { 533 return Value.getPointer().is<const ExtQuals*>(); 534 } 535 536 /// \brief Retrieve the set of qualifiers local to this particular QualType 537 /// instance, not including any qualifiers acquired through typedefs or 538 /// other sugar. 539 Qualifiers getLocalQualifiers() const { 540 Qualifiers Quals; 541 if (hasLocalNonFastQualifiers()) 542 Quals = getExtQualsUnsafe()->getQualifiers(); 543 Quals.addFastQualifiers(getLocalFastQualifiers()); 544 return Quals; 545 } 546 547 /// \brief Retrieve the set of qualifiers applied to this type. 548 Qualifiers getQualifiers() const; 549 550 /// \brief Retrieve the set of CVR (const-volatile-restrict) qualifiers 551 /// local to this particular QualType instance, not including any qualifiers 552 /// acquired through typedefs or other sugar. 553 unsigned getLocalCVRQualifiers() const { 554 unsigned CVR = getLocalFastQualifiers(); 555 if (isLocalVolatileQualified()) 556 CVR |= Qualifiers::Volatile; 557 return CVR; 558 } 559 560 /// \brief Retrieve the set of CVR (const-volatile-restrict) qualifiers 561 /// applied to this type. 562 unsigned getCVRQualifiers() const; 563 564 /// \brief Retrieve the set of CVR (const-volatile-restrict) qualifiers 565 /// applied to this type, looking through any number of unqualified array 566 /// types to their element types' qualifiers. 567 unsigned getCVRQualifiersThroughArrayTypes() const; 568 569 bool isConstant(ASTContext& Ctx) const { 570 return QualType::isConstant(*this, Ctx); 571 } 572 573 // Don't promise in the API that anything besides 'const' can be 574 // easily added. 575 576 /// addConst - add the specified type qualifier to this QualType. 577 void addConst() { 578 addFastQualifiers(Qualifiers::Const); 579 } 580 QualType withConst() const { 581 return withFastQualifiers(Qualifiers::Const); 582 } 583 584 void addFastQualifiers(unsigned TQs) { 585 assert(!(TQs & ~Qualifiers::FastMask) 586 && "non-fast qualifier bits set in mask!"); 587 Value.setInt(Value.getInt() | TQs); 588 } 589 590 // FIXME: The remove* functions are semantically broken, because they might 591 // not remove a qualifier stored on a typedef. Most of the with* functions 592 // have the same problem. 593 void removeConst(); 594 void removeVolatile(); 595 void removeRestrict(); 596 void removeCVRQualifiers(unsigned Mask); 597 598 void removeFastQualifiers() { Value.setInt(0); } 599 void removeFastQualifiers(unsigned Mask) { 600 assert(!(Mask & ~Qualifiers::FastMask) && "mask has non-fast qualifiers"); 601 Value.setInt(Value.getInt() & ~Mask); 602 } 603 604 // Creates a type with the given qualifiers in addition to any 605 // qualifiers already on this type. 606 QualType withFastQualifiers(unsigned TQs) const { 607 QualType T = *this; 608 T.addFastQualifiers(TQs); 609 return T; 610 } 611 612 // Creates a type with exactly the given fast qualifiers, removing 613 // any existing fast qualifiers. 614 QualType withExactFastQualifiers(unsigned TQs) const { 615 return withoutFastQualifiers().withFastQualifiers(TQs); 616 } 617 618 // Removes fast qualifiers, but leaves any extended qualifiers in place. 619 QualType withoutFastQualifiers() const { 620 QualType T = *this; 621 T.removeFastQualifiers(); 622 return T; 623 } 624 625 /// \brief Return this type with all of the instance-specific qualifiers 626 /// removed, but without removing any qualifiers that may have been applied 627 /// through typedefs. 628 QualType getLocalUnqualifiedType() const { return QualType(getTypePtr(), 0); } 629 630 /// \brief Return the unqualified form of the given type, which might be 631 /// desugared to eliminate qualifiers introduced via typedefs. 632 QualType getUnqualifiedType() const { 633 QualType T = getLocalUnqualifiedType(); 634 if (!T.hasQualifiers()) 635 return T; 636 637 return getUnqualifiedTypeSlow(); 638 } 639 640 bool isMoreQualifiedThan(QualType Other) const; 641 bool isAtLeastAsQualifiedAs(QualType Other) const; 642 QualType getNonReferenceType() const; 643 644 /// \brief Determine the type of a (typically non-lvalue) expression with the 645 /// specified result type. 646 /// 647 /// This routine should be used for expressions for which the return type is 648 /// explicitly specified (e.g., in a cast or call) and isn't necessarily 649 /// an lvalue. It removes a top-level reference (since there are no 650 /// expressions of reference type) and deletes top-level cvr-qualifiers 651 /// from non-class types (in C++) or all types (in C). 652 QualType getNonLValueExprType(ASTContext &Context) const; 653 654 /// getDesugaredType - Return the specified type with any "sugar" removed from 655 /// the type. This takes off typedefs, typeof's etc. If the outer level of 656 /// the type is already concrete, it returns it unmodified. This is similar 657 /// to getting the canonical type, but it doesn't remove *all* typedefs. For 658 /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is 659 /// concrete. 660 /// 661 /// Qualifiers are left in place. 662 QualType getDesugaredType() const { 663 return QualType::getDesugaredType(*this); 664 } 665 666 /// operator==/!= - Indicate whether the specified types and qualifiers are 667 /// identical. 668 friend bool operator==(const QualType &LHS, const QualType &RHS) { 669 return LHS.Value == RHS.Value; 670 } 671 friend bool operator!=(const QualType &LHS, const QualType &RHS) { 672 return LHS.Value != RHS.Value; 673 } 674 std::string getAsString() const; 675 676 std::string getAsString(const PrintingPolicy &Policy) const { 677 std::string S; 678 getAsStringInternal(S, Policy); 679 return S; 680 } 681 void getAsStringInternal(std::string &Str, 682 const PrintingPolicy &Policy) const; 683 684 void dump(const char *s) const; 685 void dump() const; 686 687 void Profile(llvm::FoldingSetNodeID &ID) const { 688 ID.AddPointer(getAsOpaquePtr()); 689 } 690 691 /// getAddressSpace - Return the address space of this type. 692 inline unsigned getAddressSpace() const; 693 694 /// GCAttrTypesAttr - Returns gc attribute of this type. 695 inline Qualifiers::GC getObjCGCAttr() const; 696 697 /// isObjCGCWeak true when Type is objc's weak. 698 bool isObjCGCWeak() const { 699 return getObjCGCAttr() == Qualifiers::Weak; 700 } 701 702 /// isObjCGCStrong true when Type is objc's strong. 703 bool isObjCGCStrong() const { 704 return getObjCGCAttr() == Qualifiers::Strong; 705 } 706 707private: 708 // These methods are implemented in a separate translation unit; 709 // "static"-ize them to avoid creating temporary QualTypes in the 710 // caller. 711 static bool isConstant(QualType T, ASTContext& Ctx); 712 static QualType getDesugaredType(QualType T); 713}; 714 715} // end clang. 716 717namespace llvm { 718/// Implement simplify_type for QualType, so that we can dyn_cast from QualType 719/// to a specific Type class. 720template<> struct simplify_type<const ::clang::QualType> { 721 typedef ::clang::Type* SimpleType; 722 static SimpleType getSimplifiedValue(const ::clang::QualType &Val) { 723 return Val.getTypePtr(); 724 } 725}; 726template<> struct simplify_type< ::clang::QualType> 727 : public simplify_type<const ::clang::QualType> {}; 728 729// Teach SmallPtrSet that QualType is "basically a pointer". 730template<> 731class PointerLikeTypeTraits<clang::QualType> { 732public: 733 static inline void *getAsVoidPointer(clang::QualType P) { 734 return P.getAsOpaquePtr(); 735 } 736 static inline clang::QualType getFromVoidPointer(void *P) { 737 return clang::QualType::getFromOpaquePtr(P); 738 } 739 // Various qualifiers go in low bits. 740 enum { NumLowBitsAvailable = 0 }; 741}; 742 743} // end namespace llvm 744 745namespace clang { 746 747/// Type - This is the base class of the type hierarchy. A central concept 748/// with types is that each type always has a canonical type. A canonical type 749/// is the type with any typedef names stripped out of it or the types it 750/// references. For example, consider: 751/// 752/// typedef int foo; 753/// typedef foo* bar; 754/// 'int *' 'foo *' 'bar' 755/// 756/// There will be a Type object created for 'int'. Since int is canonical, its 757/// canonicaltype pointer points to itself. There is also a Type for 'foo' (a 758/// TypedefType). Its CanonicalType pointer points to the 'int' Type. Next 759/// there is a PointerType that represents 'int*', which, like 'int', is 760/// canonical. Finally, there is a PointerType type for 'foo*' whose canonical 761/// type is 'int*', and there is a TypedefType for 'bar', whose canonical type 762/// is also 'int*'. 763/// 764/// Non-canonical types are useful for emitting diagnostics, without losing 765/// information about typedefs being used. Canonical types are useful for type 766/// comparisons (they allow by-pointer equality tests) and useful for reasoning 767/// about whether something has a particular form (e.g. is a function type), 768/// because they implicitly, recursively, strip all typedefs out of a type. 769/// 770/// Types, once created, are immutable. 771/// 772class Type { 773public: 774 enum TypeClass { 775#define TYPE(Class, Base) Class, 776#define LAST_TYPE(Class) TypeLast = Class, 777#define ABSTRACT_TYPE(Class, Base) 778#include "clang/AST/TypeNodes.def" 779 TagFirst = Record, TagLast = Enum 780 }; 781 782private: 783 Type(const Type&); // DO NOT IMPLEMENT. 784 void operator=(const Type&); // DO NOT IMPLEMENT. 785 786 QualType CanonicalType; 787 788 /// Bitfields required by the Type class. 789 class TypeBitfields { 790 friend class Type; 791 792 /// TypeClass bitfield - Enum that specifies what subclass this belongs to. 793 unsigned TC : 8; 794 795 /// Dependent - Whether this type is a dependent type (C++ [temp.dep.type]). 796 /// Note that this should stay at the end of the ivars for Type so that 797 /// subclasses can pack their bitfields into the same word. 798 unsigned Dependent : 1; 799 800 /// \brief Whether this type is a variably-modified type (C99 6.7.5). 801 unsigned VariablyModified : 1; 802 803 /// \brief Whether the linkage of this type along with the presence of any 804 /// local or unnamed types is already known. 805 mutable unsigned LinkageKnown : 1; 806 807 /// \brief Linkage of this type. 808 mutable unsigned CachedLinkage : 2; 809 810 /// \brief Whether this type involves and local or unnamed types. 811 mutable unsigned CachedLocalOrUnnamed : 1; 812 813 /// \brief FromAST - Whether this type comes from an AST file. 814 mutable unsigned FromAST : 1; 815 816 unsigned SpareBit : 1; 817 }; 818 enum { NumTypeBits = 16 }; 819 820protected: 821 // These classes allow subclasses to somewhat cleanly pack bitfields 822 // into Type. 823 824 class ArrayTypeBitfields { 825 friend class ArrayType; 826 827 unsigned : NumTypeBits; 828 829 /// IndexTypeQuals - CVR qualifiers from declarations like 830 /// 'int X[static restrict 4]'. For function parameters only. 831 unsigned IndexTypeQuals : 3; 832 833 /// SizeModifier - storage class qualifiers from declarations like 834 /// 'int X[static restrict 4]'. For function parameters only. 835 /// Actually an ArrayType::ArraySizeModifier. 836 unsigned SizeModifier : 3; 837 }; 838 839 class BuiltinTypeBitfields { 840 friend class BuiltinType; 841 842 unsigned : NumTypeBits; 843 844 /// The kind (BuiltinType::Kind) of builtin type this is. 845 unsigned Kind : 8; 846 }; 847 848 class FunctionTypeBitfields { 849 friend class FunctionType; 850 851 unsigned : NumTypeBits; 852 853 /// Extra information which affects how the function is called, like 854 /// regparm and the calling convention. 855 unsigned ExtInfo : 8; 856 857 /// A bit to be used by the subclass. 858 unsigned SubclassInfo : 1; 859 860 /// TypeQuals - Used only by FunctionProtoType, put here to pack with the 861 /// other bitfields. 862 /// The qualifiers are part of FunctionProtoType because... 863 /// 864 /// C++ 8.3.5p4: The return type, the parameter type list and the 865 /// cv-qualifier-seq, [...], are part of the function type. 866 unsigned TypeQuals : 3; 867 }; 868 869 class ObjCObjectTypeBitfields { 870 friend class ObjCObjectType; 871 872 unsigned : NumTypeBits; 873 874 /// NumProtocols - The number of protocols stored directly on this 875 /// object type. 876 unsigned NumProtocols : 32 - NumTypeBits; 877 }; 878 879 class ReferenceTypeBitfields { 880 friend class ReferenceType; 881 882 unsigned : NumTypeBits; 883 884 /// True if the type was originally spelled with an lvalue sigil. 885 /// This is never true of rvalue references but can also be false 886 /// on lvalue references because of C++0x [dcl.typedef]p9, 887 /// as follows: 888 /// 889 /// typedef int &ref; // lvalue, spelled lvalue 890 /// typedef int &&rvref; // rvalue 891 /// ref &a; // lvalue, inner ref, spelled lvalue 892 /// ref &&a; // lvalue, inner ref 893 /// rvref &a; // lvalue, inner ref, spelled lvalue 894 /// rvref &&a; // rvalue, inner ref 895 unsigned SpelledAsLValue : 1; 896 897 /// True if the inner type is a reference type. This only happens 898 /// in non-canonical forms. 899 unsigned InnerRef : 1; 900 }; 901 902 class TypeWithKeywordBitfields { 903 friend class TypeWithKeyword; 904 905 unsigned : NumTypeBits; 906 907 /// An ElaboratedTypeKeyword. 8 bits for efficient access. 908 unsigned Keyword : 8; 909 }; 910 911 class VectorTypeBitfields { 912 friend class VectorType; 913 914 unsigned : NumTypeBits; 915 916 /// AltiVecSpec - AltiVec-specific vector information, used 917 /// to differentiate things like 'pixel'. 918 unsigned AltiVecSpec : 2; 919 920 /// NumElements - The number of elements in the vector. 921 unsigned NumElements : 30 - NumTypeBits; 922 }; 923 924 union { 925 TypeBitfields TypeBits; 926 ArrayTypeBitfields ArrayTypeBits; 927 BuiltinTypeBitfields BuiltinTypeBits; 928 FunctionTypeBitfields FunctionTypeBits; 929 ObjCObjectTypeBitfields ObjCObjectTypeBits; 930 ReferenceTypeBitfields ReferenceTypeBits; 931 TypeWithKeywordBitfields TypeWithKeywordBits; 932 VectorTypeBitfields VectorTypeBits; 933 }; 934 935private: 936 /// \brief Set whether this type comes from an AST file. 937 void setFromAST(bool V = true) const { 938 TypeBits.FromAST = V; 939 } 940 941protected: 942 /// \brief Compute the linkage of this type along with the presence of 943 /// any local or unnamed types. 944 virtual std::pair<Linkage, bool> getLinkageUnnamedLocalImpl() const; 945 946 // silence VC++ warning C4355: 'this' : used in base member initializer list 947 Type *this_() { return this; } 948 Type(TypeClass tc, QualType Canonical, bool Dependent, bool VariablyModified) 949 : CanonicalType(Canonical.isNull() ? QualType(this_(), 0) : Canonical) { 950 TypeBits.TC = tc; 951 TypeBits.Dependent = Dependent; 952 TypeBits.VariablyModified = VariablyModified; 953 TypeBits.LinkageKnown = false; 954 TypeBits.CachedLocalOrUnnamed = false; 955 TypeBits.CachedLinkage = NoLinkage; 956 TypeBits.FromAST = false; 957 } 958 virtual ~Type(); 959 friend class ASTContext; 960 961 void setDependent(bool D = true) { TypeBits.Dependent = D; } 962 void setVariablyModified(bool VM = true) { TypeBits.VariablyModified = VM; } 963 964public: 965 TypeClass getTypeClass() const { return static_cast<TypeClass>(TypeBits.TC); } 966 967 /// \brief Whether this type comes from an AST file. 968 bool isFromAST() const { return TypeBits.FromAST; } 969 970 bool isCanonicalUnqualified() const { 971 return CanonicalType.getTypePtr() == this; 972 } 973 974 /// Types are partitioned into 3 broad categories (C99 6.2.5p1): 975 /// object types, function types, and incomplete types. 976 977 /// isIncompleteType - Return true if this is an incomplete type. 978 /// A type that can describe objects, but which lacks information needed to 979 /// determine its size (e.g. void, or a fwd declared struct). Clients of this 980 /// routine will need to determine if the size is actually required. 981 bool isIncompleteType() const; 982 983 /// isIncompleteOrObjectType - Return true if this is an incomplete or object 984 /// type, in other words, not a function type. 985 bool isIncompleteOrObjectType() const { 986 return !isFunctionType(); 987 } 988 989 /// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10). 990 bool isPODType() const; 991 992 /// isLiteralType - Return true if this is a literal type 993 /// (C++0x [basic.types]p10) 994 bool isLiteralType() const; 995 996 /// Helper methods to distinguish type categories. All type predicates 997 /// operate on the canonical type, ignoring typedefs and qualifiers. 998 999 /// isBuiltinType - returns true if the type is a builtin type. 1000 bool isBuiltinType() const; 1001 1002 /// isSpecificBuiltinType - Test for a particular builtin type. 1003 bool isSpecificBuiltinType(unsigned K) const; 1004 1005 /// isPlaceholderType - Test for a type which does not represent an 1006 /// actual type-system type but is instead used as a placeholder for 1007 /// various convenient purposes within Clang. All such types are 1008 /// BuiltinTypes. 1009 bool isPlaceholderType() const; 1010 1011 /// isIntegerType() does *not* include complex integers (a GCC extension). 1012 /// isComplexIntegerType() can be used to test for complex integers. 1013 bool isIntegerType() const; // C99 6.2.5p17 (int, char, bool, enum) 1014 bool isEnumeralType() const; 1015 bool isBooleanType() const; 1016 bool isCharType() const; 1017 bool isWideCharType() const; 1018 bool isAnyCharacterType() const; 1019 bool isIntegralType(ASTContext &Ctx) const; 1020 1021 /// \brief Determine whether this type is an integral or enumeration type. 1022 bool isIntegralOrEnumerationType() const; 1023 /// \brief Determine whether this type is an integral or unscoped enumeration 1024 /// type. 1025 bool isIntegralOrUnscopedEnumerationType() const; 1026 1027 /// Floating point categories. 1028 bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double) 1029 /// isComplexType() does *not* include complex integers (a GCC extension). 1030 /// isComplexIntegerType() can be used to test for complex integers. 1031 bool isComplexType() const; // C99 6.2.5p11 (complex) 1032 bool isAnyComplexType() const; // C99 6.2.5p11 (complex) + Complex Int. 1033 bool isFloatingType() const; // C99 6.2.5p11 (real floating + complex) 1034 bool isRealType() const; // C99 6.2.5p17 (real floating + integer) 1035 bool isArithmeticType() const; // C99 6.2.5p18 (integer + floating) 1036 bool isVoidType() const; // C99 6.2.5p19 1037 bool isDerivedType() const; // C99 6.2.5p20 1038 bool isScalarType() const; // C99 6.2.5p21 (arithmetic + pointers) 1039 bool isAggregateType() const; 1040 1041 // Type Predicates: Check to see if this type is structurally the specified 1042 // type, ignoring typedefs and qualifiers. 1043 bool isFunctionType() const; 1044 bool isFunctionNoProtoType() const { return getAs<FunctionNoProtoType>(); } 1045 bool isFunctionProtoType() const { return getAs<FunctionProtoType>(); } 1046 bool isPointerType() const; 1047 bool isAnyPointerType() const; // Any C pointer or ObjC object pointer 1048 bool isBlockPointerType() const; 1049 bool isVoidPointerType() const; 1050 bool isReferenceType() const; 1051 bool isLValueReferenceType() const; 1052 bool isRValueReferenceType() const; 1053 bool isFunctionPointerType() const; 1054 bool isMemberPointerType() const; 1055 bool isMemberFunctionPointerType() const; 1056 bool isMemberDataPointerType() const; 1057 bool isArrayType() const; 1058 bool isConstantArrayType() const; 1059 bool isIncompleteArrayType() const; 1060 bool isVariableArrayType() const; 1061 bool isDependentSizedArrayType() const; 1062 bool isRecordType() const; 1063 bool isClassType() const; 1064 bool isStructureType() const; 1065 bool isStructureOrClassType() const; 1066 bool isUnionType() const; 1067 bool isComplexIntegerType() const; // GCC _Complex integer type. 1068 bool isVectorType() const; // GCC vector type. 1069 bool isExtVectorType() const; // Extended vector type. 1070 bool isObjCObjectPointerType() const; // Pointer to *any* ObjC object. 1071 // FIXME: change this to 'raw' interface type, so we can used 'interface' type 1072 // for the common case. 1073 bool isObjCObjectType() const; // NSString or typeof(*(id)0) 1074 bool isObjCQualifiedInterfaceType() const; // NSString<foo> 1075 bool isObjCQualifiedIdType() const; // id<foo> 1076 bool isObjCQualifiedClassType() const; // Class<foo> 1077 bool isObjCObjectOrInterfaceType() const; 1078 bool isObjCIdType() const; // id 1079 bool isObjCClassType() const; // Class 1080 bool isObjCSelType() const; // Class 1081 bool isObjCBuiltinType() const; // 'id' or 'Class' 1082 bool isTemplateTypeParmType() const; // C++ template type parameter 1083 bool isNullPtrType() const; // C++0x nullptr_t 1084 1085 /// isDependentType - Whether this type is a dependent type, meaning 1086 /// that its definition somehow depends on a template parameter 1087 /// (C++ [temp.dep.type]). 1088 bool isDependentType() const { return TypeBits.Dependent; } 1089 1090 /// \brief Whether this type is a variably-modified type (C99 6.7.5). 1091 bool isVariablyModifiedType() const { return TypeBits.VariablyModified; } 1092 1093 /// \brief Whether this type is or contains a local or unnamed type. 1094 bool hasUnnamedOrLocalType() const; 1095 1096 bool isOverloadableType() const; 1097 1098 /// \brief Determine wither this type is a C++ elaborated-type-specifier. 1099 bool isElaboratedTypeSpecifier() const; 1100 1101 /// hasPointerRepresentation - Whether this type is represented 1102 /// natively as a pointer; this includes pointers, references, block 1103 /// pointers, and Objective-C interface, qualified id, and qualified 1104 /// interface types, as well as nullptr_t. 1105 bool hasPointerRepresentation() const; 1106 1107 /// hasObjCPointerRepresentation - Whether this type can represent 1108 /// an objective pointer type for the purpose of GC'ability 1109 bool hasObjCPointerRepresentation() const; 1110 1111 /// \brief Determine whether this type has an integer representation 1112 /// of some sort, e.g., it is an integer type or a vector. 1113 bool hasIntegerRepresentation() const; 1114 1115 /// \brief Determine whether this type has an signed integer representation 1116 /// of some sort, e.g., it is an signed integer type or a vector. 1117 bool hasSignedIntegerRepresentation() const; 1118 1119 /// \brief Determine whether this type has an unsigned integer representation 1120 /// of some sort, e.g., it is an unsigned integer type or a vector. 1121 bool hasUnsignedIntegerRepresentation() const; 1122 1123 /// \brief Determine whether this type has a floating-point representation 1124 /// of some sort, e.g., it is a floating-point type or a vector thereof. 1125 bool hasFloatingRepresentation() const; 1126 1127 // Type Checking Functions: Check to see if this type is structurally the 1128 // specified type, ignoring typedefs and qualifiers, and return a pointer to 1129 // the best type we can. 1130 const RecordType *getAsStructureType() const; 1131 /// NOTE: getAs*ArrayType are methods on ASTContext. 1132 const RecordType *getAsUnionType() const; 1133 const ComplexType *getAsComplexIntegerType() const; // GCC complex int type. 1134 // The following is a convenience method that returns an ObjCObjectPointerType 1135 // for object declared using an interface. 1136 const ObjCObjectPointerType *getAsObjCInterfacePointerType() const; 1137 const ObjCObjectPointerType *getAsObjCQualifiedIdType() const; 1138 const ObjCObjectType *getAsObjCQualifiedInterfaceType() const; 1139 const CXXRecordDecl *getCXXRecordDeclForPointerType() const; 1140 1141 /// \brief Retrieves the CXXRecordDecl that this type refers to, either 1142 /// because the type is a RecordType or because it is the injected-class-name 1143 /// type of a class template or class template partial specialization. 1144 CXXRecordDecl *getAsCXXRecordDecl() const; 1145 1146 // Member-template getAs<specific type>'. Look through sugar for 1147 // an instance of <specific type>. This scheme will eventually 1148 // replace the specific getAsXXXX methods above. 1149 // 1150 // There are some specializations of this member template listed 1151 // immediately following this class. 1152 template <typename T> const T *getAs() const; 1153 1154 /// getArrayElementTypeNoTypeQual - If this is an array type, return the 1155 /// element type of the array, potentially with type qualifiers missing. 1156 /// This method should never be used when type qualifiers are meaningful. 1157 const Type *getArrayElementTypeNoTypeQual() const; 1158 1159 /// getPointeeType - If this is a pointer, ObjC object pointer, or block 1160 /// pointer, this returns the respective pointee. 1161 QualType getPointeeType() const; 1162 1163 /// getUnqualifiedDesugaredType() - Return the specified type with 1164 /// any "sugar" removed from the type, removing any typedefs, 1165 /// typeofs, etc., as well as any qualifiers. 1166 const Type *getUnqualifiedDesugaredType() const; 1167 1168 /// More type predicates useful for type checking/promotion 1169 bool isPromotableIntegerType() const; // C99 6.3.1.1p2 1170 1171 /// isSignedIntegerType - Return true if this is an integer type that is 1172 /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], 1173 /// an enum decl which has a signed representation, or a vector of signed 1174 /// integer element type. 1175 bool isSignedIntegerType() const; 1176 1177 /// isUnsignedIntegerType - Return true if this is an integer type that is 1178 /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum 1179 /// decl which has an unsigned representation, or a vector of unsigned integer 1180 /// element type. 1181 bool isUnsignedIntegerType() const; 1182 1183 /// isConstantSizeType - Return true if this is not a variable sized type, 1184 /// according to the rules of C99 6.7.5p3. It is not legal to call this on 1185 /// incomplete types. 1186 bool isConstantSizeType() const; 1187 1188 /// isSpecifierType - Returns true if this type can be represented by some 1189 /// set of type specifiers. 1190 bool isSpecifierType() const; 1191 1192 /// \brief Determine the linkage of this type. 1193 Linkage getLinkage() const; 1194 1195 /// \brief Note that the linkage is no longer known. 1196 void ClearLinkageCache(); 1197 1198 const char *getTypeClassName() const; 1199 1200 QualType getCanonicalTypeInternal() const { 1201 return CanonicalType; 1202 } 1203 CanQualType getCanonicalTypeUnqualified() const; // in CanonicalType.h 1204 void dump() const; 1205 static bool classof(const Type *) { return true; } 1206 1207 friend class ASTReader; 1208 friend class ASTWriter; 1209}; 1210 1211template <> inline const TypedefType *Type::getAs() const { 1212 return dyn_cast<TypedefType>(this); 1213} 1214 1215// We can do canonical leaf types faster, because we don't have to 1216// worry about preserving child type decoration. 1217#define TYPE(Class, Base) 1218#define LEAF_TYPE(Class) \ 1219template <> inline const Class##Type *Type::getAs() const { \ 1220 return dyn_cast<Class##Type>(CanonicalType); \ 1221} 1222#include "clang/AST/TypeNodes.def" 1223 1224 1225/// BuiltinType - This class is used for builtin types like 'int'. Builtin 1226/// types are always canonical and have a literal name field. 1227class BuiltinType : public Type { 1228public: 1229 enum Kind { 1230 Void, 1231 1232 Bool, // This is bool and/or _Bool. 1233 Char_U, // This is 'char' for targets where char is unsigned. 1234 UChar, // This is explicitly qualified unsigned char. 1235 Char16, // This is 'char16_t' for C++. 1236 Char32, // This is 'char32_t' for C++. 1237 UShort, 1238 UInt, 1239 ULong, 1240 ULongLong, 1241 UInt128, // __uint128_t 1242 1243 Char_S, // This is 'char' for targets where char is signed. 1244 SChar, // This is explicitly qualified signed char. 1245 WChar, // This is 'wchar_t' for C++. 1246 Short, 1247 Int, 1248 Long, 1249 LongLong, 1250 Int128, // __int128_t 1251 1252 Float, Double, LongDouble, 1253 1254 NullPtr, // This is the type of C++0x 'nullptr'. 1255 1256 /// This represents the type of an expression whose type is 1257 /// totally unknown, e.g. 'T::foo'. It is permitted for this to 1258 /// appear in situations where the structure of the type is 1259 /// theoretically deducible. 1260 Dependent, 1261 1262 Overload, // This represents the type of an overloaded function declaration. 1263 1264 UndeducedAuto, // In C++0x, this represents the type of an auto variable 1265 // that has not been deduced yet. 1266 1267 /// The primitive Objective C 'id' type. The type pointed to by the 1268 /// user-visible 'id' type. Only ever shows up in an AST as the base 1269 /// type of an ObjCObjectType. 1270 ObjCId, 1271 1272 /// The primitive Objective C 'Class' type. The type pointed to by the 1273 /// user-visible 'Class' type. Only ever shows up in an AST as the 1274 /// base type of an ObjCObjectType. 1275 ObjCClass, 1276 1277 ObjCSel // This represents the ObjC 'SEL' type. 1278 }; 1279 1280protected: 1281 virtual std::pair<Linkage, bool> getLinkageUnnamedLocalImpl() const; 1282 1283public: 1284 BuiltinType(Kind K) 1285 : Type(Builtin, QualType(), /*Dependent=*/(K == Dependent), 1286 /*VariablyModified=*/false) { 1287 BuiltinTypeBits.Kind = K; 1288 } 1289 1290 Kind getKind() const { return static_cast<Kind>(BuiltinTypeBits.Kind); } 1291 const char *getName(const LangOptions &LO) const; 1292 1293 bool isSugared() const { return false; } 1294 QualType desugar() const { return QualType(this, 0); } 1295 1296 bool isInteger() const { 1297 return getKind() >= Bool && getKind() <= Int128; 1298 } 1299 1300 bool isSignedInteger() const { 1301 return getKind() >= Char_S && getKind() <= Int128; 1302 } 1303 1304 bool isUnsignedInteger() const { 1305 return getKind() >= Bool && getKind() <= UInt128; 1306 } 1307 1308 bool isFloatingPoint() const { 1309 return getKind() >= Float && getKind() <= LongDouble; 1310 } 1311 1312 /// Determines whether this type is a "forbidden" placeholder type, 1313 /// i.e. a type which cannot appear in arbitrary positions in a 1314 /// fully-formed expression. 1315 bool isPlaceholderType() const { 1316 return getKind() == Overload || 1317 getKind() == UndeducedAuto; 1318 } 1319 1320 static bool classof(const Type *T) { return T->getTypeClass() == Builtin; } 1321 static bool classof(const BuiltinType *) { return true; } 1322}; 1323 1324/// ComplexType - C99 6.2.5p11 - Complex values. This supports the C99 complex 1325/// types (_Complex float etc) as well as the GCC integer complex extensions. 1326/// 1327class ComplexType : public Type, public llvm::FoldingSetNode { 1328 QualType ElementType; 1329 ComplexType(QualType Element, QualType CanonicalPtr) : 1330 Type(Complex, CanonicalPtr, Element->isDependentType(), 1331 Element->isVariablyModifiedType()), 1332 ElementType(Element) { 1333 } 1334 friend class ASTContext; // ASTContext creates these. 1335 1336protected: 1337 virtual std::pair<Linkage, bool> getLinkageUnnamedLocalImpl() const; 1338 1339public: 1340 QualType getElementType() const { return ElementType; } 1341 1342 bool isSugared() const { return false; } 1343 QualType desugar() const { return QualType(this, 0); } 1344 1345 void Profile(llvm::FoldingSetNodeID &ID) { 1346 Profile(ID, getElementType()); 1347 } 1348 static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) { 1349 ID.AddPointer(Element.getAsOpaquePtr()); 1350 } 1351 1352 static bool classof(const Type *T) { return T->getTypeClass() == Complex; } 1353 static bool classof(const ComplexType *) { return true; } 1354}; 1355 1356/// PointerType - C99 6.7.5.1 - Pointer Declarators. 1357/// 1358class PointerType : public Type, public llvm::FoldingSetNode { 1359 QualType PointeeType; 1360 1361 PointerType(QualType Pointee, QualType CanonicalPtr) : 1362 Type(Pointer, CanonicalPtr, Pointee->isDependentType(), 1363 Pointee->isVariablyModifiedType()), 1364 PointeeType(Pointee) { 1365 } 1366 friend class ASTContext; // ASTContext creates these. 1367 1368protected: 1369 virtual std::pair<Linkage, bool> getLinkageUnnamedLocalImpl() const; 1370 1371public: 1372 1373 QualType getPointeeType() const { return PointeeType; } 1374 1375 bool isSugared() const { return false; } 1376 QualType desugar() const { return QualType(this, 0); } 1377 1378 void Profile(llvm::FoldingSetNodeID &ID) { 1379 Profile(ID, getPointeeType()); 1380 } 1381 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { 1382 ID.AddPointer(Pointee.getAsOpaquePtr()); 1383 } 1384 1385 static bool classof(const Type *T) { return T->getTypeClass() == Pointer; } 1386 static bool classof(const PointerType *) { return true; } 1387}; 1388 1389/// BlockPointerType - pointer to a block type. 1390/// This type is to represent types syntactically represented as 1391/// "void (^)(int)", etc. Pointee is required to always be a function type. 1392/// 1393class BlockPointerType : public Type, public llvm::FoldingSetNode { 1394 QualType PointeeType; // Block is some kind of pointer type 1395 BlockPointerType(QualType Pointee, QualType CanonicalCls) : 1396 Type(BlockPointer, CanonicalCls, Pointee->isDependentType(), 1397 Pointee->isVariablyModifiedType()), 1398 PointeeType(Pointee) { 1399 } 1400 friend class ASTContext; // ASTContext creates these. 1401 1402protected: 1403 virtual std::pair<Linkage, bool> getLinkageUnnamedLocalImpl() const; 1404 1405public: 1406 1407 // Get the pointee type. Pointee is required to always be a function type. 1408 QualType getPointeeType() const { return PointeeType; } 1409 1410 bool isSugared() const { return false; } 1411 QualType desugar() const { return QualType(this, 0); } 1412 1413 void Profile(llvm::FoldingSetNodeID &ID) { 1414 Profile(ID, getPointeeType()); 1415 } 1416 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { 1417 ID.AddPointer(Pointee.getAsOpaquePtr()); 1418 } 1419 1420 static bool classof(const Type *T) { 1421 return T->getTypeClass() == BlockPointer; 1422 } 1423 static bool classof(const BlockPointerType *) { return true; } 1424}; 1425 1426/// ReferenceType - Base for LValueReferenceType and RValueReferenceType 1427/// 1428class ReferenceType : public Type, public llvm::FoldingSetNode { 1429 QualType PointeeType; 1430 1431protected: 1432 ReferenceType(TypeClass tc, QualType Referencee, QualType CanonicalRef, 1433 bool SpelledAsLValue) : 1434 Type(tc, CanonicalRef, Referencee->isDependentType(), 1435 Referencee->isVariablyModifiedType()), PointeeType(Referencee) { 1436 ReferenceTypeBits.SpelledAsLValue = SpelledAsLValue; 1437 ReferenceTypeBits.InnerRef = Referencee->isReferenceType(); 1438 } 1439 1440 virtual std::pair<Linkage, bool> getLinkageUnnamedLocalImpl() const; 1441 1442public: 1443 bool isSpelledAsLValue() const { return ReferenceTypeBits.SpelledAsLValue; } 1444 bool isInnerRef() const { return ReferenceTypeBits.InnerRef; } 1445 1446 QualType getPointeeTypeAsWritten() const { return PointeeType; } 1447 QualType getPointeeType() const { 1448 // FIXME: this might strip inner qualifiers; okay? 1449 const ReferenceType *T = this; 1450 while (T->isInnerRef()) 1451 T = T->PointeeType->getAs<ReferenceType>(); 1452 return T->PointeeType; 1453 } 1454 1455 void Profile(llvm::FoldingSetNodeID &ID) { 1456 Profile(ID, PointeeType, isSpelledAsLValue()); 1457 } 1458 static void Profile(llvm::FoldingSetNodeID &ID, 1459 QualType Referencee, 1460 bool SpelledAsLValue) { 1461 ID.AddPointer(Referencee.getAsOpaquePtr()); 1462 ID.AddBoolean(SpelledAsLValue); 1463 } 1464 1465 static bool classof(const Type *T) { 1466 return T->getTypeClass() == LValueReference || 1467 T->getTypeClass() == RValueReference; 1468 } 1469 static bool classof(const ReferenceType *) { return true; } 1470}; 1471 1472/// LValueReferenceType - C++ [dcl.ref] - Lvalue reference 1473/// 1474class LValueReferenceType : public ReferenceType { 1475 LValueReferenceType(QualType Referencee, QualType CanonicalRef, 1476 bool SpelledAsLValue) : 1477 ReferenceType(LValueReference, Referencee, CanonicalRef, SpelledAsLValue) 1478 {} 1479 friend class ASTContext; // ASTContext creates these 1480public: 1481 bool isSugared() const { return false; } 1482 QualType desugar() const { return QualType(this, 0); } 1483 1484 static bool classof(const Type *T) { 1485 return T->getTypeClass() == LValueReference; 1486 } 1487 static bool classof(const LValueReferenceType *) { return true; } 1488}; 1489 1490/// RValueReferenceType - C++0x [dcl.ref] - Rvalue reference 1491/// 1492class RValueReferenceType : public ReferenceType { 1493 RValueReferenceType(QualType Referencee, QualType CanonicalRef) : 1494 ReferenceType(RValueReference, Referencee, CanonicalRef, false) { 1495 } 1496 friend class ASTContext; // ASTContext creates these 1497public: 1498 bool isSugared() const { return false; } 1499 QualType desugar() const { return QualType(this, 0); } 1500 1501 static bool classof(const Type *T) { 1502 return T->getTypeClass() == RValueReference; 1503 } 1504 static bool classof(const RValueReferenceType *) { return true; } 1505}; 1506 1507/// MemberPointerType - C++ 8.3.3 - Pointers to members 1508/// 1509class MemberPointerType : public Type, public llvm::FoldingSetNode { 1510 QualType PointeeType; 1511 /// The class of which the pointee is a member. Must ultimately be a 1512 /// RecordType, but could be a typedef or a template parameter too. 1513 const Type *Class; 1514 1515 MemberPointerType(QualType Pointee, const Type *Cls, QualType CanonicalPtr) : 1516 Type(MemberPointer, CanonicalPtr, 1517 Cls->isDependentType() || Pointee->isDependentType(), 1518 Pointee->isVariablyModifiedType()), 1519 PointeeType(Pointee), Class(Cls) { 1520 } 1521 friend class ASTContext; // ASTContext creates these. 1522 1523protected: 1524 virtual std::pair<Linkage, bool> getLinkageUnnamedLocalImpl() const; 1525 1526public: 1527 QualType getPointeeType() const { return PointeeType; } 1528 1529 /// Returns true if the member type (i.e. the pointee type) is a 1530 /// function type rather than a data-member type. 1531 bool isMemberFunctionPointer() const { 1532 return PointeeType->isFunctionProtoType(); 1533 } 1534 1535 /// Returns true if the member type (i.e. the pointee type) is a 1536 /// data type rather than a function type. 1537 bool isMemberDataPointer() const { 1538 return !PointeeType->isFunctionProtoType(); 1539 } 1540 1541 const Type *getClass() const { return Class; } 1542 1543 bool isSugared() const { return false; } 1544 QualType desugar() const { return QualType(this, 0); } 1545 1546 void Profile(llvm::FoldingSetNodeID &ID) { 1547 Profile(ID, getPointeeType(), getClass()); 1548 } 1549 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee, 1550 const Type *Class) { 1551 ID.AddPointer(Pointee.getAsOpaquePtr()); 1552 ID.AddPointer(Class); 1553 } 1554 1555 static bool classof(const Type *T) { 1556 return T->getTypeClass() == MemberPointer; 1557 } 1558 static bool classof(const MemberPointerType *) { return true; } 1559}; 1560 1561/// ArrayType - C99 6.7.5.2 - Array Declarators. 1562/// 1563class ArrayType : public Type, public llvm::FoldingSetNode { 1564public: 1565 /// ArraySizeModifier - Capture whether this is a normal array (e.g. int X[4]) 1566 /// an array with a static size (e.g. int X[static 4]), or an array 1567 /// with a star size (e.g. int X[*]). 1568 /// 'static' is only allowed on function parameters. 1569 enum ArraySizeModifier { 1570 Normal, Static, Star 1571 }; 1572private: 1573 /// ElementType - The element type of the array. 1574 QualType ElementType; 1575 1576protected: 1577 // C++ [temp.dep.type]p1: 1578 // A type is dependent if it is... 1579 // - an array type constructed from any dependent type or whose 1580 // size is specified by a constant expression that is 1581 // value-dependent, 1582 ArrayType(TypeClass tc, QualType et, QualType can, 1583 ArraySizeModifier sm, unsigned tq) 1584 : Type(tc, can, et->isDependentType() || tc == DependentSizedArray, 1585 (tc == VariableArray || et->isVariablyModifiedType())), 1586 ElementType(et) { 1587 ArrayTypeBits.IndexTypeQuals = tq; 1588 ArrayTypeBits.SizeModifier = sm; 1589 } 1590 1591 friend class ASTContext; // ASTContext creates these. 1592 1593 virtual std::pair<Linkage, bool> getLinkageUnnamedLocalImpl() const; 1594 1595public: 1596 QualType getElementType() const { return ElementType; } 1597 ArraySizeModifier getSizeModifier() const { 1598 return ArraySizeModifier(ArrayTypeBits.SizeModifier); 1599 } 1600 Qualifiers getIndexTypeQualifiers() const { 1601 return Qualifiers::fromCVRMask(getIndexTypeCVRQualifiers()); 1602 } 1603 unsigned getIndexTypeCVRQualifiers() const { 1604 return ArrayTypeBits.IndexTypeQuals; 1605 } 1606 1607 static bool classof(const Type *T) { 1608 return T->getTypeClass() == ConstantArray || 1609 T->getTypeClass() == VariableArray || 1610 T->getTypeClass() == IncompleteArray || 1611 T->getTypeClass() == DependentSizedArray; 1612 } 1613 static bool classof(const ArrayType *) { return true; } 1614}; 1615 1616/// ConstantArrayType - This class represents the canonical version of 1617/// C arrays with a specified constant size. For example, the canonical 1618/// type for 'int A[4 + 4*100]' is a ConstantArrayType where the element 1619/// type is 'int' and the size is 404. 1620class ConstantArrayType : public ArrayType { 1621 llvm::APInt Size; // Allows us to unique the type. 1622 1623 ConstantArrayType(QualType et, QualType can, const llvm::APInt &size, 1624 ArraySizeModifier sm, unsigned tq) 1625 : ArrayType(ConstantArray, et, can, sm, tq), 1626 Size(size) {} 1627protected: 1628 ConstantArrayType(TypeClass tc, QualType et, QualType can, 1629 const llvm::APInt &size, ArraySizeModifier sm, unsigned tq) 1630 : ArrayType(tc, et, can, sm, tq), Size(size) {} 1631 friend class ASTContext; // ASTContext creates these. 1632public: 1633 const llvm::APInt &getSize() const { return Size; } 1634 bool isSugared() const { return false; } 1635 QualType desugar() const { return QualType(this, 0); } 1636 1637 1638 /// \brief Determine the number of bits required to address a member of 1639 // an array with the given element type and number of elements. 1640 static unsigned getNumAddressingBits(ASTContext &Context, 1641 QualType ElementType, 1642 const llvm::APInt &NumElements); 1643 1644 /// \brief Determine the maximum number of active bits that an array's size 1645 /// can require, which limits the maximum size of the array. 1646 static unsigned getMaxSizeBits(ASTContext &Context); 1647 1648 void Profile(llvm::FoldingSetNodeID &ID) { 1649 Profile(ID, getElementType(), getSize(), 1650 getSizeModifier(), getIndexTypeCVRQualifiers()); 1651 } 1652 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, 1653 const llvm::APInt &ArraySize, ArraySizeModifier SizeMod, 1654 unsigned TypeQuals) { 1655 ID.AddPointer(ET.getAsOpaquePtr()); 1656 ID.AddInteger(ArraySize.getZExtValue()); 1657 ID.AddInteger(SizeMod); 1658 ID.AddInteger(TypeQuals); 1659 } 1660 static bool classof(const Type *T) { 1661 return T->getTypeClass() == ConstantArray; 1662 } 1663 static bool classof(const ConstantArrayType *) { return true; } 1664}; 1665 1666/// IncompleteArrayType - This class represents C arrays with an unspecified 1667/// size. For example 'int A[]' has an IncompleteArrayType where the element 1668/// type is 'int' and the size is unspecified. 1669class IncompleteArrayType : public ArrayType { 1670 1671 IncompleteArrayType(QualType et, QualType can, 1672 ArraySizeModifier sm, unsigned tq) 1673 : ArrayType(IncompleteArray, et, can, sm, tq) {} 1674 friend class ASTContext; // ASTContext creates these. 1675public: 1676 bool isSugared() const { return false; } 1677 QualType desugar() const { return QualType(this, 0); } 1678 1679 static bool classof(const Type *T) { 1680 return T->getTypeClass() == IncompleteArray; 1681 } 1682 static bool classof(const IncompleteArrayType *) { return true; } 1683 1684 friend class StmtIteratorBase; 1685 1686 void Profile(llvm::FoldingSetNodeID &ID) { 1687 Profile(ID, getElementType(), getSizeModifier(), 1688 getIndexTypeCVRQualifiers()); 1689 } 1690 1691 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, 1692 ArraySizeModifier SizeMod, unsigned TypeQuals) { 1693 ID.AddPointer(ET.getAsOpaquePtr()); 1694 ID.AddInteger(SizeMod); 1695 ID.AddInteger(TypeQuals); 1696 } 1697}; 1698 1699/// VariableArrayType - This class represents C arrays with a specified size 1700/// which is not an integer-constant-expression. For example, 'int s[x+foo()]'. 1701/// Since the size expression is an arbitrary expression, we store it as such. 1702/// 1703/// Note: VariableArrayType's aren't uniqued (since the expressions aren't) and 1704/// should not be: two lexically equivalent variable array types could mean 1705/// different things, for example, these variables do not have the same type 1706/// dynamically: 1707/// 1708/// void foo(int x) { 1709/// int Y[x]; 1710/// ++x; 1711/// int Z[x]; 1712/// } 1713/// 1714class VariableArrayType : public ArrayType { 1715 /// SizeExpr - An assignment expression. VLA's are only permitted within 1716 /// a function block. 1717 Stmt *SizeExpr; 1718 /// Brackets - The left and right array brackets. 1719 SourceRange Brackets; 1720 1721 VariableArrayType(QualType et, QualType can, Expr *e, 1722 ArraySizeModifier sm, unsigned tq, 1723 SourceRange brackets) 1724 : ArrayType(VariableArray, et, can, sm, tq), 1725 SizeExpr((Stmt*) e), Brackets(brackets) {} 1726 friend class ASTContext; // ASTContext creates these. 1727 1728public: 1729 Expr *getSizeExpr() const { 1730 // We use C-style casts instead of cast<> here because we do not wish 1731 // to have a dependency of Type.h on Stmt.h/Expr.h. 1732 return (Expr*) SizeExpr; 1733 } 1734 SourceRange getBracketsRange() const { return Brackets; } 1735 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } 1736 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } 1737 1738 bool isSugared() const { return false; } 1739 QualType desugar() const { return QualType(this, 0); } 1740 1741 static bool classof(const Type *T) { 1742 return T->getTypeClass() == VariableArray; 1743 } 1744 static bool classof(const VariableArrayType *) { return true; } 1745 1746 friend class StmtIteratorBase; 1747 1748 void Profile(llvm::FoldingSetNodeID &ID) { 1749 assert(0 && "Cannnot unique VariableArrayTypes."); 1750 } 1751}; 1752 1753/// DependentSizedArrayType - This type represents an array type in 1754/// C++ whose size is a value-dependent expression. For example: 1755/// 1756/// \code 1757/// template<typename T, int Size> 1758/// class array { 1759/// T data[Size]; 1760/// }; 1761/// \endcode 1762/// 1763/// For these types, we won't actually know what the array bound is 1764/// until template instantiation occurs, at which point this will 1765/// become either a ConstantArrayType or a VariableArrayType. 1766class DependentSizedArrayType : public ArrayType { 1767 ASTContext &Context; 1768 1769 /// \brief An assignment expression that will instantiate to the 1770 /// size of the array. 1771 /// 1772 /// The expression itself might be NULL, in which case the array 1773 /// type will have its size deduced from an initializer. 1774 Stmt *SizeExpr; 1775 1776 /// Brackets - The left and right array brackets. 1777 SourceRange Brackets; 1778 1779 DependentSizedArrayType(ASTContext &Context, QualType et, QualType can, 1780 Expr *e, ArraySizeModifier sm, unsigned tq, 1781 SourceRange brackets) 1782 : ArrayType(DependentSizedArray, et, can, sm, tq), 1783 Context(Context), SizeExpr((Stmt*) e), Brackets(brackets) {} 1784 friend class ASTContext; // ASTContext creates these. 1785 1786public: 1787 Expr *getSizeExpr() const { 1788 // We use C-style casts instead of cast<> here because we do not wish 1789 // to have a dependency of Type.h on Stmt.h/Expr.h. 1790 return (Expr*) SizeExpr; 1791 } 1792 SourceRange getBracketsRange() const { return Brackets; } 1793 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } 1794 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } 1795 1796 bool isSugared() const { return false; } 1797 QualType desugar() const { return QualType(this, 0); } 1798 1799 static bool classof(const Type *T) { 1800 return T->getTypeClass() == DependentSizedArray; 1801 } 1802 static bool classof(const DependentSizedArrayType *) { return true; } 1803 1804 friend class StmtIteratorBase; 1805 1806 1807 void Profile(llvm::FoldingSetNodeID &ID) { 1808 Profile(ID, Context, getElementType(), 1809 getSizeModifier(), getIndexTypeCVRQualifiers(), getSizeExpr()); 1810 } 1811 1812 static void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context, 1813 QualType ET, ArraySizeModifier SizeMod, 1814 unsigned TypeQuals, Expr *E); 1815}; 1816 1817/// DependentSizedExtVectorType - This type represent an extended vector type 1818/// where either the type or size is dependent. For example: 1819/// @code 1820/// template<typename T, int Size> 1821/// class vector { 1822/// typedef T __attribute__((ext_vector_type(Size))) type; 1823/// } 1824/// @endcode 1825class DependentSizedExtVectorType : public Type, public llvm::FoldingSetNode { 1826 ASTContext &Context; 1827 Expr *SizeExpr; 1828 /// ElementType - The element type of the array. 1829 QualType ElementType; 1830 SourceLocation loc; 1831 1832 DependentSizedExtVectorType(ASTContext &Context, QualType ElementType, 1833 QualType can, Expr *SizeExpr, SourceLocation loc) 1834 : Type(DependentSizedExtVector, can, /*Dependent=*/true, 1835 ElementType->isVariablyModifiedType()), 1836 Context(Context), SizeExpr(SizeExpr), ElementType(ElementType), 1837 loc(loc) {} 1838 friend class ASTContext; 1839 1840public: 1841 Expr *getSizeExpr() const { return SizeExpr; } 1842 QualType getElementType() const { return ElementType; } 1843 SourceLocation getAttributeLoc() const { return loc; } 1844 1845 bool isSugared() const { return false; } 1846 QualType desugar() const { return QualType(this, 0); } 1847 1848 static bool classof(const Type *T) { 1849 return T->getTypeClass() == DependentSizedExtVector; 1850 } 1851 static bool classof(const DependentSizedExtVectorType *) { return true; } 1852 1853 void Profile(llvm::FoldingSetNodeID &ID) { 1854 Profile(ID, Context, getElementType(), getSizeExpr()); 1855 } 1856 1857 static void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context, 1858 QualType ElementType, Expr *SizeExpr); 1859}; 1860 1861 1862/// VectorType - GCC generic vector type. This type is created using 1863/// __attribute__((vector_size(n)), where "n" specifies the vector size in 1864/// bytes; or from an Altivec __vector or vector declaration. 1865/// Since the constructor takes the number of vector elements, the 1866/// client is responsible for converting the size into the number of elements. 1867class VectorType : public Type, public llvm::FoldingSetNode { 1868public: 1869 enum AltiVecSpecific { 1870 NotAltiVec, // is not AltiVec vector 1871 AltiVec, // is AltiVec vector 1872 Pixel, // is AltiVec 'vector Pixel' 1873 Bool // is AltiVec 'vector bool ...' 1874 }; 1875protected: 1876 /// ElementType - The element type of the vector. 1877 QualType ElementType; 1878 1879 VectorType(QualType vecType, unsigned nElements, QualType canonType, 1880 AltiVecSpecific altiVecSpec) : 1881 Type(Vector, canonType, vecType->isDependentType(), 1882 vecType->isVariablyModifiedType()), ElementType(vecType) { 1883 VectorTypeBits.AltiVecSpec = altiVecSpec; 1884 VectorTypeBits.NumElements = nElements; 1885 } 1886 1887 VectorType(TypeClass tc, QualType vecType, unsigned nElements, 1888 QualType canonType, AltiVecSpecific altiVecSpec) 1889 : Type(tc, canonType, vecType->isDependentType(), 1890 vecType->isVariablyModifiedType()), ElementType(vecType) { 1891 VectorTypeBits.AltiVecSpec = altiVecSpec; 1892 VectorTypeBits.NumElements = nElements; 1893 } 1894 friend class ASTContext; // ASTContext creates these. 1895 1896 virtual std::pair<Linkage, bool> getLinkageUnnamedLocalImpl() const; 1897 1898public: 1899 1900 QualType getElementType() const { return ElementType; } 1901 unsigned getNumElements() const { return VectorTypeBits.NumElements; } 1902 1903 bool isSugared() const { return false; } 1904 QualType desugar() const { return QualType(this, 0); } 1905 1906 AltiVecSpecific getAltiVecSpecific() const { 1907 return AltiVecSpecific(VectorTypeBits.AltiVecSpec); 1908 } 1909 1910 void Profile(llvm::FoldingSetNodeID &ID) { 1911 Profile(ID, getElementType(), getNumElements(), 1912 getTypeClass(), getAltiVecSpecific()); 1913 } 1914 static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, 1915 unsigned NumElements, TypeClass TypeClass, 1916 AltiVecSpecific AltiVecSpec) { 1917 ID.AddPointer(ElementType.getAsOpaquePtr()); 1918 ID.AddInteger(NumElements); 1919 ID.AddInteger(TypeClass); 1920 ID.AddInteger(AltiVecSpec); 1921 } 1922 1923 static bool classof(const Type *T) { 1924 return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector; 1925 } 1926 static bool classof(const VectorType *) { return true; } 1927}; 1928 1929/// ExtVectorType - Extended vector type. This type is created using 1930/// __attribute__((ext_vector_type(n)), where "n" is the number of elements. 1931/// Unlike vector_size, ext_vector_type is only allowed on typedef's. This 1932/// class enables syntactic extensions, like Vector Components for accessing 1933/// points, colors, and textures (modeled after OpenGL Shading Language). 1934class ExtVectorType : public VectorType { 1935 ExtVectorType(QualType vecType, unsigned nElements, QualType canonType) : 1936 VectorType(ExtVector, vecType, nElements, canonType, NotAltiVec) {} 1937 friend class ASTContext; // ASTContext creates these. 1938public: 1939 static int getPointAccessorIdx(char c) { 1940 switch (c) { 1941 default: return -1; 1942 case 'x': return 0; 1943 case 'y': return 1; 1944 case 'z': return 2; 1945 case 'w': return 3; 1946 } 1947 } 1948 static int getNumericAccessorIdx(char c) { 1949 switch (c) { 1950 default: return -1; 1951 case '0': return 0; 1952 case '1': return 1; 1953 case '2': return 2; 1954 case '3': return 3; 1955 case '4': return 4; 1956 case '5': return 5; 1957 case '6': return 6; 1958 case '7': return 7; 1959 case '8': return 8; 1960 case '9': return 9; 1961 case 'A': 1962 case 'a': return 10; 1963 case 'B': 1964 case 'b': return 11; 1965 case 'C': 1966 case 'c': return 12; 1967 case 'D': 1968 case 'd': return 13; 1969 case 'E': 1970 case 'e': return 14; 1971 case 'F': 1972 case 'f': return 15; 1973 } 1974 } 1975 1976 static int getAccessorIdx(char c) { 1977 if (int idx = getPointAccessorIdx(c)+1) return idx-1; 1978 return getNumericAccessorIdx(c); 1979 } 1980 1981 bool isAccessorWithinNumElements(char c) const { 1982 if (int idx = getAccessorIdx(c)+1) 1983 return unsigned(idx-1) < getNumElements(); 1984 return false; 1985 } 1986 bool isSugared() const { return false; } 1987 QualType desugar() const { return QualType(this, 0); } 1988 1989 static bool classof(const Type *T) { 1990 return T->getTypeClass() == ExtVector; 1991 } 1992 static bool classof(const ExtVectorType *) { return true; } 1993}; 1994 1995/// FunctionType - C99 6.7.5.3 - Function Declarators. This is the common base 1996/// class of FunctionNoProtoType and FunctionProtoType. 1997/// 1998class FunctionType : public Type { 1999 virtual void ANCHOR(); // Key function for FunctionType. 2000 2001 // The type returned by the function. 2002 QualType ResultType; 2003 2004 public: 2005 // This class is used for passing arround the information needed to 2006 // construct a call. It is not actually used for storage, just for 2007 // factoring together common arguments. 2008 // If you add a field (say Foo), other than the obvious places (both, constructors, 2009 // compile failures), what you need to update is 2010 // * Operetor== 2011 // * getFoo 2012 // * withFoo 2013 // * functionType. Add Foo, getFoo. 2014 // * ASTContext::getFooType 2015 // * ASTContext::mergeFunctionTypes 2016 // * FunctionNoProtoType::Profile 2017 // * FunctionProtoType::Profile 2018 // * TypePrinter::PrintFunctionProto 2019 // * AST read and write 2020 // * Codegen 2021 2022 class ExtInfo { 2023 enum { CallConvMask = 0x7 }; 2024 enum { NoReturnMask = 0x8 }; 2025 enum { RegParmMask = ~(CallConvMask | NoReturnMask), 2026 RegParmOffset = 4 }; 2027 2028 unsigned Bits; 2029 2030 ExtInfo(unsigned Bits) : Bits(Bits) {} 2031 2032 friend class FunctionType; 2033 2034 public: 2035 // Constructor with no defaults. Use this when you know that you 2036 // have all the elements (when reading an AST file for example). 2037 ExtInfo(bool noReturn, unsigned regParm, CallingConv cc) { 2038 Bits = ((unsigned) cc) | 2039 (noReturn ? NoReturnMask : 0) | 2040 (regParm << RegParmOffset); 2041 } 2042 2043 // Constructor with all defaults. Use when for example creating a 2044 // function know to use defaults. 2045 ExtInfo() : Bits(0) {} 2046 2047 bool getNoReturn() const { return Bits & NoReturnMask; } 2048 unsigned getRegParm() const { return Bits >> RegParmOffset; } 2049 CallingConv getCC() const { return CallingConv(Bits & CallConvMask); } 2050 2051 bool operator==(ExtInfo Other) const { 2052 return Bits == Other.Bits; 2053 } 2054 bool operator!=(ExtInfo Other) const { 2055 return Bits != Other.Bits; 2056 } 2057 2058 // Note that we don't have setters. That is by design, use 2059 // the following with methods instead of mutating these objects. 2060 2061 ExtInfo withNoReturn(bool noReturn) const { 2062 if (noReturn) 2063 return ExtInfo(Bits | NoReturnMask); 2064 else 2065 return ExtInfo(Bits & ~NoReturnMask); 2066 } 2067 2068 ExtInfo withRegParm(unsigned RegParm) const { 2069 return ExtInfo((Bits & ~RegParmMask) | (RegParm << RegParmOffset)); 2070 } 2071 2072 ExtInfo withCallingConv(CallingConv cc) const { 2073 return ExtInfo((Bits & ~CallConvMask) | (unsigned) cc); 2074 } 2075 2076 void Profile(llvm::FoldingSetNodeID &ID) { 2077 ID.AddInteger(Bits); 2078 } 2079 }; 2080 2081protected: 2082 FunctionType(TypeClass tc, QualType res, bool SubclassInfo, 2083 unsigned typeQuals, QualType Canonical, bool Dependent, 2084 bool VariablyModified, ExtInfo Info) 2085 : Type(tc, Canonical, Dependent, VariablyModified), ResultType(res) { 2086 FunctionTypeBits.ExtInfo = Info.Bits; 2087 FunctionTypeBits.SubclassInfo = SubclassInfo; 2088 FunctionTypeBits.TypeQuals = typeQuals; 2089 } 2090 bool getSubClassData() const { return FunctionTypeBits.SubclassInfo; } 2091 unsigned getTypeQuals() const { return FunctionTypeBits.TypeQuals; } 2092public: 2093 2094 QualType getResultType() const { return ResultType; } 2095 2096 unsigned getRegParmType() const { return getExtInfo().getRegParm(); } 2097 bool getNoReturnAttr() const { return getExtInfo().getNoReturn(); } 2098 CallingConv getCallConv() const { return getExtInfo().getCC(); } 2099 ExtInfo getExtInfo() const { return ExtInfo(FunctionTypeBits.ExtInfo); } 2100 2101 /// \brief Determine the type of an expression that calls a function of 2102 /// this type. 2103 QualType getCallResultType(ASTContext &Context) const { 2104 return getResultType().getNonLValueExprType(Context); 2105 } 2106 2107 static llvm::StringRef getNameForCallConv(CallingConv CC); 2108 2109 static bool classof(const Type *T) { 2110 return T->getTypeClass() == FunctionNoProto || 2111 T->getTypeClass() == FunctionProto; 2112 } 2113 static bool classof(const FunctionType *) { return true; } 2114}; 2115 2116/// FunctionNoProtoType - Represents a K&R-style 'int foo()' function, which has 2117/// no information available about its arguments. 2118class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode { 2119 FunctionNoProtoType(QualType Result, QualType Canonical, ExtInfo Info) 2120 : FunctionType(FunctionNoProto, Result, false, 0, Canonical, 2121 /*Dependent=*/false, Result->isVariablyModifiedType(), 2122 Info) {} 2123 friend class ASTContext; // ASTContext creates these. 2124 2125protected: 2126 virtual std::pair<Linkage, bool> getLinkageUnnamedLocalImpl() const; 2127 2128public: 2129 // No additional state past what FunctionType provides. 2130 2131 bool isSugared() const { return false; } 2132 QualType desugar() const { return QualType(this, 0); } 2133 2134 void Profile(llvm::FoldingSetNodeID &ID) { 2135 Profile(ID, getResultType(), getExtInfo()); 2136 } 2137 static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType, 2138 ExtInfo Info) { 2139 Info.Profile(ID); 2140 ID.AddPointer(ResultType.getAsOpaquePtr()); 2141 } 2142 2143 static bool classof(const Type *T) { 2144 return T->getTypeClass() == FunctionNoProto; 2145 } 2146 static bool classof(const FunctionNoProtoType *) { return true; } 2147}; 2148 2149/// FunctionProtoType - Represents a prototype with argument type info, e.g. 2150/// 'int foo(int)' or 'int foo(void)'. 'void' is represented as having no 2151/// arguments, not as having a single void argument. Such a type can have an 2152/// exception specification, but this specification is not part of the canonical 2153/// type. 2154class FunctionProtoType : public FunctionType, public llvm::FoldingSetNode { 2155 FunctionProtoType(QualType Result, const QualType *ArgArray, unsigned numArgs, 2156 bool isVariadic, unsigned typeQuals, bool hasExs, 2157 bool hasAnyExs, const QualType *ExArray, 2158 unsigned numExs, QualType Canonical, 2159 const ExtInfo &Info); 2160 2161 /// NumArgs - The number of arguments this function has, not counting '...'. 2162 unsigned NumArgs : 20; 2163 2164 /// NumExceptions - The number of types in the exception spec, if any. 2165 unsigned NumExceptions : 10; 2166 2167 /// HasExceptionSpec - Whether this function has an exception spec at all. 2168 bool HasExceptionSpec : 1; 2169 2170 /// AnyExceptionSpec - Whether this function has a throw(...) spec. 2171 bool AnyExceptionSpec : 1; 2172 2173 /// ArgInfo - There is an variable size array after the class in memory that 2174 /// holds the argument types. 2175 2176 /// Exceptions - There is another variable size array after ArgInfo that 2177 /// holds the exception types. 2178 2179 friend class ASTContext; // ASTContext creates these. 2180 2181protected: 2182 virtual std::pair<Linkage, bool> getLinkageUnnamedLocalImpl() const; 2183 2184public: 2185 unsigned getNumArgs() const { return NumArgs; } 2186 QualType getArgType(unsigned i) const { 2187 assert(i < NumArgs && "Invalid argument number!"); 2188 return arg_type_begin()[i]; 2189 } 2190 2191 bool hasExceptionSpec() const { return HasExceptionSpec; } 2192 bool hasAnyExceptionSpec() const { return AnyExceptionSpec; } 2193 unsigned getNumExceptions() const { return NumExceptions; } 2194 QualType getExceptionType(unsigned i) const { 2195 assert(i < NumExceptions && "Invalid exception number!"); 2196 return exception_begin()[i]; 2197 } 2198 bool hasEmptyExceptionSpec() const { 2199 return hasExceptionSpec() && !hasAnyExceptionSpec() && 2200 getNumExceptions() == 0; 2201 } 2202 2203 bool isVariadic() const { return getSubClassData(); } 2204 unsigned getTypeQuals() const { return FunctionType::getTypeQuals(); } 2205 2206 typedef const QualType *arg_type_iterator; 2207 arg_type_iterator arg_type_begin() const { 2208 return reinterpret_cast<const QualType *>(this+1); 2209 } 2210 arg_type_iterator arg_type_end() const { return arg_type_begin()+NumArgs; } 2211 2212 typedef const QualType *exception_iterator; 2213 exception_iterator exception_begin() const { 2214 // exceptions begin where arguments end 2215 return arg_type_end(); 2216 } 2217 exception_iterator exception_end() const { 2218 return exception_begin() + NumExceptions; 2219 } 2220 2221 bool isSugared() const { return false; } 2222 QualType desugar() const { return QualType(this, 0); } 2223 2224 static bool classof(const Type *T) { 2225 return T->getTypeClass() == FunctionProto; 2226 } 2227 static bool classof(const FunctionProtoType *) { return true; } 2228 2229 void Profile(llvm::FoldingSetNodeID &ID); 2230 static void Profile(llvm::FoldingSetNodeID &ID, QualType Result, 2231 arg_type_iterator ArgTys, unsigned NumArgs, 2232 bool isVariadic, unsigned TypeQuals, 2233 bool hasExceptionSpec, bool anyExceptionSpec, 2234 unsigned NumExceptions, exception_iterator Exs, 2235 ExtInfo ExtInfo); 2236}; 2237 2238 2239/// \brief Represents the dependent type named by a dependently-scoped 2240/// typename using declaration, e.g. 2241/// using typename Base<T>::foo; 2242/// Template instantiation turns these into the underlying type. 2243class UnresolvedUsingType : public Type { 2244 UnresolvedUsingTypenameDecl *Decl; 2245 2246 UnresolvedUsingType(const UnresolvedUsingTypenameDecl *D) 2247 : Type(UnresolvedUsing, QualType(), true, false), 2248 Decl(const_cast<UnresolvedUsingTypenameDecl*>(D)) {} 2249 friend class ASTContext; // ASTContext creates these. 2250public: 2251 2252 UnresolvedUsingTypenameDecl *getDecl() const { return Decl; } 2253 2254 bool isSugared() const { return false; } 2255 QualType desugar() const { return QualType(this, 0); } 2256 2257 static bool classof(const Type *T) { 2258 return T->getTypeClass() == UnresolvedUsing; 2259 } 2260 static bool classof(const UnresolvedUsingType *) { return true; } 2261 2262 void Profile(llvm::FoldingSetNodeID &ID) { 2263 return Profile(ID, Decl); 2264 } 2265 static void Profile(llvm::FoldingSetNodeID &ID, 2266 UnresolvedUsingTypenameDecl *D) { 2267 ID.AddPointer(D); 2268 } 2269}; 2270 2271 2272class TypedefType : public Type { 2273 TypedefDecl *Decl; 2274protected: 2275 TypedefType(TypeClass tc, const TypedefDecl *D, QualType can) 2276 : Type(tc, can, can->isDependentType(), can->isVariablyModifiedType()), 2277 Decl(const_cast<TypedefDecl*>(D)) { 2278 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 2279 } 2280 friend class ASTContext; // ASTContext creates these. 2281public: 2282 2283 TypedefDecl *getDecl() const { return Decl; } 2284 2285 /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to 2286 /// potentially looking through *all* consecutive typedefs. This returns the 2287 /// sum of the type qualifiers, so if you have: 2288 /// typedef const int A; 2289 /// typedef volatile A B; 2290 /// looking through the typedefs for B will give you "const volatile A". 2291 QualType LookThroughTypedefs() const; 2292 2293 bool isSugared() const { return true; } 2294 QualType desugar() const; 2295 2296 static bool classof(const Type *T) { return T->getTypeClass() == Typedef; } 2297 static bool classof(const TypedefType *) { return true; } 2298}; 2299 2300/// TypeOfExprType (GCC extension). 2301class TypeOfExprType : public Type { 2302 Expr *TOExpr; 2303 2304protected: 2305 TypeOfExprType(Expr *E, QualType can = QualType()); 2306 friend class ASTContext; // ASTContext creates these. 2307public: 2308 Expr *getUnderlyingExpr() const { return TOExpr; } 2309 2310 /// \brief Remove a single level of sugar. 2311 QualType desugar() const; 2312 2313 /// \brief Returns whether this type directly provides sugar. 2314 bool isSugared() const { return true; } 2315 2316 static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; } 2317 static bool classof(const TypeOfExprType *) { return true; } 2318}; 2319 2320/// \brief Internal representation of canonical, dependent 2321/// typeof(expr) types. 2322/// 2323/// This class is used internally by the ASTContext to manage 2324/// canonical, dependent types, only. Clients will only see instances 2325/// of this class via TypeOfExprType nodes. 2326class DependentTypeOfExprType 2327 : public TypeOfExprType, public llvm::FoldingSetNode { 2328 ASTContext &Context; 2329 2330public: 2331 DependentTypeOfExprType(ASTContext &Context, Expr *E) 2332 : TypeOfExprType(E), Context(Context) { } 2333 2334 bool isSugared() const { return false; } 2335 QualType desugar() const { return QualType(this, 0); } 2336 2337 void Profile(llvm::FoldingSetNodeID &ID) { 2338 Profile(ID, Context, getUnderlyingExpr()); 2339 } 2340 2341 static void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context, 2342 Expr *E); 2343}; 2344 2345/// TypeOfType (GCC extension). 2346class TypeOfType : public Type { 2347 QualType TOType; 2348 TypeOfType(QualType T, QualType can) 2349 : Type(TypeOf, can, T->isDependentType(), T->isVariablyModifiedType()), 2350 TOType(T) { 2351 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 2352 } 2353 friend class ASTContext; // ASTContext creates these. 2354public: 2355 QualType getUnderlyingType() const { return TOType; } 2356 2357 /// \brief Remove a single level of sugar. 2358 QualType desugar() const { return getUnderlyingType(); } 2359 2360 /// \brief Returns whether this type directly provides sugar. 2361 bool isSugared() const { return true; } 2362 2363 static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; } 2364 static bool classof(const TypeOfType *) { return true; } 2365}; 2366 2367/// DecltypeType (C++0x) 2368class DecltypeType : public Type { 2369 Expr *E; 2370 2371 // FIXME: We could get rid of UnderlyingType if we wanted to: We would have to 2372 // Move getDesugaredType to ASTContext so that it can call getDecltypeForExpr 2373 // from it. 2374 QualType UnderlyingType; 2375 2376protected: 2377 DecltypeType(Expr *E, QualType underlyingType, QualType can = QualType()); 2378 friend class ASTContext; // ASTContext creates these. 2379public: 2380 Expr *getUnderlyingExpr() const { return E; } 2381 QualType getUnderlyingType() const { return UnderlyingType; } 2382 2383 /// \brief Remove a single level of sugar. 2384 QualType desugar() const { return getUnderlyingType(); } 2385 2386 /// \brief Returns whether this type directly provides sugar. 2387 bool isSugared() const { return !isDependentType(); } 2388 2389 static bool classof(const Type *T) { return T->getTypeClass() == Decltype; } 2390 static bool classof(const DecltypeType *) { return true; } 2391}; 2392 2393/// \brief Internal representation of canonical, dependent 2394/// decltype(expr) types. 2395/// 2396/// This class is used internally by the ASTContext to manage 2397/// canonical, dependent types, only. Clients will only see instances 2398/// of this class via DecltypeType nodes. 2399class DependentDecltypeType : public DecltypeType, public llvm::FoldingSetNode { 2400 ASTContext &Context; 2401 2402public: 2403 DependentDecltypeType(ASTContext &Context, Expr *E); 2404 2405 bool isSugared() const { return false; } 2406 QualType desugar() const { return QualType(this, 0); } 2407 2408 void Profile(llvm::FoldingSetNodeID &ID) { 2409 Profile(ID, Context, getUnderlyingExpr()); 2410 } 2411 2412 static void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context, 2413 Expr *E); 2414}; 2415 2416class TagType : public Type { 2417 /// Stores the TagDecl associated with this type. The decl may point to any 2418 /// TagDecl that declares the entity. 2419 TagDecl * decl; 2420 2421protected: 2422 TagType(TypeClass TC, const TagDecl *D, QualType can); 2423 2424 virtual std::pair<Linkage, bool> getLinkageUnnamedLocalImpl() const; 2425 2426public: 2427 TagDecl *getDecl() const; 2428 2429 /// @brief Determines whether this type is in the process of being 2430 /// defined. 2431 bool isBeingDefined() const; 2432 2433 static bool classof(const Type *T) { 2434 return T->getTypeClass() >= TagFirst && T->getTypeClass() <= TagLast; 2435 } 2436 static bool classof(const TagType *) { return true; } 2437 static bool classof(const RecordType *) { return true; } 2438 static bool classof(const EnumType *) { return true; } 2439}; 2440 2441/// RecordType - This is a helper class that allows the use of isa/cast/dyncast 2442/// to detect TagType objects of structs/unions/classes. 2443class RecordType : public TagType { 2444protected: 2445 explicit RecordType(const RecordDecl *D) 2446 : TagType(Record, reinterpret_cast<const TagDecl*>(D), QualType()) { } 2447 explicit RecordType(TypeClass TC, RecordDecl *D) 2448 : TagType(TC, reinterpret_cast<const TagDecl*>(D), QualType()) { } 2449 friend class ASTContext; // ASTContext creates these. 2450public: 2451 2452 RecordDecl *getDecl() const { 2453 return reinterpret_cast<RecordDecl*>(TagType::getDecl()); 2454 } 2455 2456 // FIXME: This predicate is a helper to QualType/Type. It needs to 2457 // recursively check all fields for const-ness. If any field is declared 2458 // const, it needs to return false. 2459 bool hasConstFields() const { return false; } 2460 2461 // FIXME: RecordType needs to check when it is created that all fields are in 2462 // the same address space, and return that. 2463 unsigned getAddressSpace() const { return 0; } 2464 2465 bool isSugared() const { return false; } 2466 QualType desugar() const { return QualType(this, 0); } 2467 2468 static bool classof(const TagType *T); 2469 static bool classof(const Type *T) { 2470 return isa<TagType>(T) && classof(cast<TagType>(T)); 2471 } 2472 static bool classof(const RecordType *) { return true; } 2473}; 2474 2475/// EnumType - This is a helper class that allows the use of isa/cast/dyncast 2476/// to detect TagType objects of enums. 2477class EnumType : public TagType { 2478 explicit EnumType(const EnumDecl *D) 2479 : TagType(Enum, reinterpret_cast<const TagDecl*>(D), QualType()) { } 2480 friend class ASTContext; // ASTContext creates these. 2481public: 2482 2483 EnumDecl *getDecl() const { 2484 return reinterpret_cast<EnumDecl*>(TagType::getDecl()); 2485 } 2486 2487 bool isSugared() const { return false; } 2488 QualType desugar() const { return QualType(this, 0); } 2489 2490 static bool classof(const TagType *T); 2491 static bool classof(const Type *T) { 2492 return isa<TagType>(T) && classof(cast<TagType>(T)); 2493 } 2494 static bool classof(const EnumType *) { return true; } 2495}; 2496 2497class TemplateTypeParmType : public Type, public llvm::FoldingSetNode { 2498 unsigned Depth : 15; 2499 unsigned ParameterPack : 1; 2500 unsigned Index : 16; 2501 IdentifierInfo *Name; 2502 2503 TemplateTypeParmType(unsigned D, unsigned I, bool PP, IdentifierInfo *N, 2504 QualType Canon) 2505 : Type(TemplateTypeParm, Canon, /*Dependent=*/true, 2506 /*VariablyModified=*/false), 2507 Depth(D), ParameterPack(PP), Index(I), Name(N) { } 2508 2509 TemplateTypeParmType(unsigned D, unsigned I, bool PP) 2510 : Type(TemplateTypeParm, QualType(this, 0), /*Dependent=*/true, 2511 /*VariablyModified=*/false), 2512 Depth(D), ParameterPack(PP), Index(I), Name(0) { } 2513 2514 friend class ASTContext; // ASTContext creates these 2515 2516public: 2517 unsigned getDepth() const { return Depth; } 2518 unsigned getIndex() const { return Index; } 2519 bool isParameterPack() const { return ParameterPack; } 2520 IdentifierInfo *getName() const { return Name; } 2521 2522 bool isSugared() const { return false; } 2523 QualType desugar() const { return QualType(this, 0); } 2524 2525 void Profile(llvm::FoldingSetNodeID &ID) { 2526 Profile(ID, Depth, Index, ParameterPack, Name); 2527 } 2528 2529 static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth, 2530 unsigned Index, bool ParameterPack, 2531 IdentifierInfo *Name) { 2532 ID.AddInteger(Depth); 2533 ID.AddInteger(Index); 2534 ID.AddBoolean(ParameterPack); 2535 ID.AddPointer(Name); 2536 } 2537 2538 static bool classof(const Type *T) { 2539 return T->getTypeClass() == TemplateTypeParm; 2540 } 2541 static bool classof(const TemplateTypeParmType *T) { return true; } 2542}; 2543 2544/// \brief Represents the result of substituting a type for a template 2545/// type parameter. 2546/// 2547/// Within an instantiated template, all template type parameters have 2548/// been replaced with these. They are used solely to record that a 2549/// type was originally written as a template type parameter; 2550/// therefore they are never canonical. 2551class SubstTemplateTypeParmType : public Type, public llvm::FoldingSetNode { 2552 // The original type parameter. 2553 const TemplateTypeParmType *Replaced; 2554 2555 SubstTemplateTypeParmType(const TemplateTypeParmType *Param, QualType Canon) 2556 : Type(SubstTemplateTypeParm, Canon, Canon->isDependentType(), 2557 Canon->isVariablyModifiedType()), 2558 Replaced(Param) { } 2559 2560 friend class ASTContext; 2561 2562public: 2563 IdentifierInfo *getName() const { return Replaced->getName(); } 2564 2565 /// Gets the template parameter that was substituted for. 2566 const TemplateTypeParmType *getReplacedParameter() const { 2567 return Replaced; 2568 } 2569 2570 /// Gets the type that was substituted for the template 2571 /// parameter. 2572 QualType getReplacementType() const { 2573 return getCanonicalTypeInternal(); 2574 } 2575 2576 bool isSugared() const { return true; } 2577 QualType desugar() const { return getReplacementType(); } 2578 2579 void Profile(llvm::FoldingSetNodeID &ID) { 2580 Profile(ID, getReplacedParameter(), getReplacementType()); 2581 } 2582 static void Profile(llvm::FoldingSetNodeID &ID, 2583 const TemplateTypeParmType *Replaced, 2584 QualType Replacement) { 2585 ID.AddPointer(Replaced); 2586 ID.AddPointer(Replacement.getAsOpaquePtr()); 2587 } 2588 2589 static bool classof(const Type *T) { 2590 return T->getTypeClass() == SubstTemplateTypeParm; 2591 } 2592 static bool classof(const SubstTemplateTypeParmType *T) { return true; } 2593}; 2594 2595/// \brief Represents the type of a template specialization as written 2596/// in the source code. 2597/// 2598/// Template specialization types represent the syntactic form of a 2599/// template-id that refers to a type, e.g., @c vector<int>. Some 2600/// template specialization types are syntactic sugar, whose canonical 2601/// type will point to some other type node that represents the 2602/// instantiation or class template specialization. For example, a 2603/// class template specialization type of @c vector<int> will refer to 2604/// a tag type for the instantiation 2605/// @c std::vector<int, std::allocator<int>>. 2606/// 2607/// Other template specialization types, for which the template name 2608/// is dependent, may be canonical types. These types are always 2609/// dependent. 2610class TemplateSpecializationType 2611 : public Type, public llvm::FoldingSetNode { 2612 /// \brief The name of the template being specialized. 2613 TemplateName Template; 2614 2615 /// \brief - The number of template arguments named in this class 2616 /// template specialization. 2617 unsigned NumArgs; 2618 2619 TemplateSpecializationType(TemplateName T, 2620 const TemplateArgument *Args, 2621 unsigned NumArgs, QualType Canon); 2622 2623 friend class ASTContext; // ASTContext creates these 2624 2625public: 2626 /// \brief Determine whether any of the given template arguments are 2627 /// dependent. 2628 static bool anyDependentTemplateArguments(const TemplateArgument *Args, 2629 unsigned NumArgs); 2630 2631 static bool anyDependentTemplateArguments(const TemplateArgumentLoc *Args, 2632 unsigned NumArgs); 2633 2634 static bool anyDependentTemplateArguments(const TemplateArgumentListInfo &); 2635 2636 /// \brief Print a template argument list, including the '<' and '>' 2637 /// enclosing the template arguments. 2638 static std::string PrintTemplateArgumentList(const TemplateArgument *Args, 2639 unsigned NumArgs, 2640 const PrintingPolicy &Policy); 2641 2642 static std::string PrintTemplateArgumentList(const TemplateArgumentLoc *Args, 2643 unsigned NumArgs, 2644 const PrintingPolicy &Policy); 2645 2646 static std::string PrintTemplateArgumentList(const TemplateArgumentListInfo &, 2647 const PrintingPolicy &Policy); 2648 2649 /// True if this template specialization type matches a current 2650 /// instantiation in the context in which it is found. 2651 bool isCurrentInstantiation() const { 2652 return isa<InjectedClassNameType>(getCanonicalTypeInternal()); 2653 } 2654 2655 typedef const TemplateArgument * iterator; 2656 2657 iterator begin() const { return getArgs(); } 2658 iterator end() const; // defined inline in TemplateBase.h 2659 2660 /// \brief Retrieve the name of the template that we are specializing. 2661 TemplateName getTemplateName() const { return Template; } 2662 2663 /// \brief Retrieve the template arguments. 2664 const TemplateArgument *getArgs() const { 2665 return reinterpret_cast<const TemplateArgument *>(this + 1); 2666 } 2667 2668 /// \brief Retrieve the number of template arguments. 2669 unsigned getNumArgs() const { return NumArgs; } 2670 2671 /// \brief Retrieve a specific template argument as a type. 2672 /// \precondition @c isArgType(Arg) 2673 const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h 2674 2675 bool isSugared() const { 2676 return !isDependentType() || isCurrentInstantiation(); 2677 } 2678 QualType desugar() const { return getCanonicalTypeInternal(); } 2679 2680 void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Ctx) { 2681 Profile(ID, Template, getArgs(), NumArgs, Ctx); 2682 } 2683 2684 static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T, 2685 const TemplateArgument *Args, 2686 unsigned NumArgs, 2687 ASTContext &Context); 2688 2689 static bool classof(const Type *T) { 2690 return T->getTypeClass() == TemplateSpecialization; 2691 } 2692 static bool classof(const TemplateSpecializationType *T) { return true; } 2693}; 2694 2695/// \brief The injected class name of a C++ class template or class 2696/// template partial specialization. Used to record that a type was 2697/// spelled with a bare identifier rather than as a template-id; the 2698/// equivalent for non-templated classes is just RecordType. 2699/// 2700/// Injected class name types are always dependent. Template 2701/// instantiation turns these into RecordTypes. 2702/// 2703/// Injected class name types are always canonical. This works 2704/// because it is impossible to compare an injected class name type 2705/// with the corresponding non-injected template type, for the same 2706/// reason that it is impossible to directly compare template 2707/// parameters from different dependent contexts: injected class name 2708/// types can only occur within the scope of a particular templated 2709/// declaration, and within that scope every template specialization 2710/// will canonicalize to the injected class name (when appropriate 2711/// according to the rules of the language). 2712class InjectedClassNameType : public Type { 2713 CXXRecordDecl *Decl; 2714 2715 /// The template specialization which this type represents. 2716 /// For example, in 2717 /// template <class T> class A { ... }; 2718 /// this is A<T>, whereas in 2719 /// template <class X, class Y> class A<B<X,Y> > { ... }; 2720 /// this is A<B<X,Y> >. 2721 /// 2722 /// It is always unqualified, always a template specialization type, 2723 /// and always dependent. 2724 QualType InjectedType; 2725 2726 friend class ASTContext; // ASTContext creates these. 2727 friend class ASTReader; // FIXME: ASTContext::getInjectedClassNameType is not 2728 // currently suitable for AST reading, too much 2729 // interdependencies. 2730 InjectedClassNameType(CXXRecordDecl *D, QualType TST) 2731 : Type(InjectedClassName, QualType(), /*Dependent=*/true, 2732 /*VariablyModified=*/false), 2733 Decl(D), InjectedType(TST) { 2734 assert(isa<TemplateSpecializationType>(TST)); 2735 assert(!TST.hasQualifiers()); 2736 assert(TST->isDependentType()); 2737 } 2738 2739public: 2740 QualType getInjectedSpecializationType() const { return InjectedType; } 2741 const TemplateSpecializationType *getInjectedTST() const { 2742 return cast<TemplateSpecializationType>(InjectedType.getTypePtr()); 2743 } 2744 2745 CXXRecordDecl *getDecl() const; 2746 2747 bool isSugared() const { return false; } 2748 QualType desugar() const { return QualType(this, 0); } 2749 2750 static bool classof(const Type *T) { 2751 return T->getTypeClass() == InjectedClassName; 2752 } 2753 static bool classof(const InjectedClassNameType *T) { return true; } 2754}; 2755 2756/// \brief The kind of a tag type. 2757enum TagTypeKind { 2758 /// \brief The "struct" keyword. 2759 TTK_Struct, 2760 /// \brief The "union" keyword. 2761 TTK_Union, 2762 /// \brief The "class" keyword. 2763 TTK_Class, 2764 /// \brief The "enum" keyword. 2765 TTK_Enum 2766}; 2767 2768/// \brief The elaboration keyword that precedes a qualified type name or 2769/// introduces an elaborated-type-specifier. 2770enum ElaboratedTypeKeyword { 2771 /// \brief The "struct" keyword introduces the elaborated-type-specifier. 2772 ETK_Struct, 2773 /// \brief The "union" keyword introduces the elaborated-type-specifier. 2774 ETK_Union, 2775 /// \brief The "class" keyword introduces the elaborated-type-specifier. 2776 ETK_Class, 2777 /// \brief The "enum" keyword introduces the elaborated-type-specifier. 2778 ETK_Enum, 2779 /// \brief The "typename" keyword precedes the qualified type name, e.g., 2780 /// \c typename T::type. 2781 ETK_Typename, 2782 /// \brief No keyword precedes the qualified type name. 2783 ETK_None 2784}; 2785 2786/// A helper class for Type nodes having an ElaboratedTypeKeyword. 2787/// The keyword in stored in the free bits of the base class. 2788/// Also provides a few static helpers for converting and printing 2789/// elaborated type keyword and tag type kind enumerations. 2790class TypeWithKeyword : public Type { 2791protected: 2792 TypeWithKeyword(ElaboratedTypeKeyword Keyword, TypeClass tc, 2793 QualType Canonical, bool Dependent, bool VariablyModified) 2794 : Type(tc, Canonical, Dependent, VariablyModified) { 2795 TypeWithKeywordBits.Keyword = Keyword; 2796 } 2797 2798public: 2799 virtual ~TypeWithKeyword(); // pin vtable to Type.cpp 2800 2801 ElaboratedTypeKeyword getKeyword() const { 2802 return static_cast<ElaboratedTypeKeyword>(TypeWithKeywordBits.Keyword); 2803 } 2804 2805 /// getKeywordForTypeSpec - Converts a type specifier (DeclSpec::TST) 2806 /// into an elaborated type keyword. 2807 static ElaboratedTypeKeyword getKeywordForTypeSpec(unsigned TypeSpec); 2808 2809 /// getTagTypeKindForTypeSpec - Converts a type specifier (DeclSpec::TST) 2810 /// into a tag type kind. It is an error to provide a type specifier 2811 /// which *isn't* a tag kind here. 2812 static TagTypeKind getTagTypeKindForTypeSpec(unsigned TypeSpec); 2813 2814 /// getKeywordForTagDeclKind - Converts a TagTypeKind into an 2815 /// elaborated type keyword. 2816 static ElaboratedTypeKeyword getKeywordForTagTypeKind(TagTypeKind Tag); 2817 2818 /// getTagTypeKindForKeyword - Converts an elaborated type keyword into 2819 // a TagTypeKind. It is an error to provide an elaborated type keyword 2820 /// which *isn't* a tag kind here. 2821 static TagTypeKind getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword); 2822 2823 static bool KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword); 2824 2825 static const char *getKeywordName(ElaboratedTypeKeyword Keyword); 2826 2827 static const char *getTagTypeKindName(TagTypeKind Kind) { 2828 return getKeywordName(getKeywordForTagTypeKind(Kind)); 2829 } 2830 2831 class CannotCastToThisType {}; 2832 static CannotCastToThisType classof(const Type *); 2833}; 2834 2835/// \brief Represents a type that was referred to using an elaborated type 2836/// keyword, e.g., struct S, or via a qualified name, e.g., N::M::type, 2837/// or both. 2838/// 2839/// This type is used to keep track of a type name as written in the 2840/// source code, including tag keywords and any nested-name-specifiers. 2841/// The type itself is always "sugar", used to express what was written 2842/// in the source code but containing no additional semantic information. 2843class ElaboratedType : public TypeWithKeyword, public llvm::FoldingSetNode { 2844 2845 /// \brief The nested name specifier containing the qualifier. 2846 NestedNameSpecifier *NNS; 2847 2848 /// \brief The type that this qualified name refers to. 2849 QualType NamedType; 2850 2851 ElaboratedType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, 2852 QualType NamedType, QualType CanonType) 2853 : TypeWithKeyword(Keyword, Elaborated, CanonType, 2854 NamedType->isDependentType(), 2855 NamedType->isVariablyModifiedType()), 2856 NNS(NNS), NamedType(NamedType) { 2857 assert(!(Keyword == ETK_None && NNS == 0) && 2858 "ElaboratedType cannot have elaborated type keyword " 2859 "and name qualifier both null."); 2860 } 2861 2862 friend class ASTContext; // ASTContext creates these 2863 2864public: 2865 ~ElaboratedType(); 2866 2867 /// \brief Retrieve the qualification on this type. 2868 NestedNameSpecifier *getQualifier() const { return NNS; } 2869 2870 /// \brief Retrieve the type named by the qualified-id. 2871 QualType getNamedType() const { return NamedType; } 2872 2873 /// \brief Remove a single level of sugar. 2874 QualType desugar() const { return getNamedType(); } 2875 2876 /// \brief Returns whether this type directly provides sugar. 2877 bool isSugared() const { return true; } 2878 2879 void Profile(llvm::FoldingSetNodeID &ID) { 2880 Profile(ID, getKeyword(), NNS, NamedType); 2881 } 2882 2883 static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, 2884 NestedNameSpecifier *NNS, QualType NamedType) { 2885 ID.AddInteger(Keyword); 2886 ID.AddPointer(NNS); 2887 NamedType.Profile(ID); 2888 } 2889 2890 static bool classof(const Type *T) { 2891 return T->getTypeClass() == Elaborated; 2892 } 2893 static bool classof(const ElaboratedType *T) { return true; } 2894}; 2895 2896/// \brief Represents a qualified type name for which the type name is 2897/// dependent. 2898/// 2899/// DependentNameType represents a class of dependent types that involve a 2900/// dependent nested-name-specifier (e.g., "T::") followed by a (dependent) 2901/// name of a type. The DependentNameType may start with a "typename" (for a 2902/// typename-specifier), "class", "struct", "union", or "enum" (for a 2903/// dependent elaborated-type-specifier), or nothing (in contexts where we 2904/// know that we must be referring to a type, e.g., in a base class specifier). 2905class DependentNameType : public TypeWithKeyword, public llvm::FoldingSetNode { 2906 2907 /// \brief The nested name specifier containing the qualifier. 2908 NestedNameSpecifier *NNS; 2909 2910 /// \brief The type that this typename specifier refers to. 2911 const IdentifierInfo *Name; 2912 2913 DependentNameType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, 2914 const IdentifierInfo *Name, QualType CanonType) 2915 : TypeWithKeyword(Keyword, DependentName, CanonType, /*Dependent=*/true, 2916 /*VariablyModified=*/false), 2917 NNS(NNS), Name(Name) { 2918 assert(NNS->isDependent() && 2919 "DependentNameType requires a dependent nested-name-specifier"); 2920 } 2921 2922 friend class ASTContext; // ASTContext creates these 2923 2924public: 2925 virtual ~DependentNameType(); 2926 2927 /// \brief Retrieve the qualification on this type. 2928 NestedNameSpecifier *getQualifier() const { return NNS; } 2929 2930 /// \brief Retrieve the type named by the typename specifier as an 2931 /// identifier. 2932 /// 2933 /// This routine will return a non-NULL identifier pointer when the 2934 /// form of the original typename was terminated by an identifier, 2935 /// e.g., "typename T::type". 2936 const IdentifierInfo *getIdentifier() const { 2937 return Name; 2938 } 2939 2940 bool isSugared() const { return false; } 2941 QualType desugar() const { return QualType(this, 0); } 2942 2943 void Profile(llvm::FoldingSetNodeID &ID) { 2944 Profile(ID, getKeyword(), NNS, Name); 2945 } 2946 2947 static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, 2948 NestedNameSpecifier *NNS, const IdentifierInfo *Name) { 2949 ID.AddInteger(Keyword); 2950 ID.AddPointer(NNS); 2951 ID.AddPointer(Name); 2952 } 2953 2954 static bool classof(const Type *T) { 2955 return T->getTypeClass() == DependentName; 2956 } 2957 static bool classof(const DependentNameType *T) { return true; } 2958}; 2959 2960/// DependentTemplateSpecializationType - Represents a template 2961/// specialization type whose template cannot be resolved, e.g. 2962/// A<T>::template B<T> 2963class DependentTemplateSpecializationType : 2964 public TypeWithKeyword, public llvm::FoldingSetNode { 2965 2966 /// \brief The nested name specifier containing the qualifier. 2967 NestedNameSpecifier *NNS; 2968 2969 /// \brief The identifier of the template. 2970 const IdentifierInfo *Name; 2971 2972 /// \brief - The number of template arguments named in this class 2973 /// template specialization. 2974 unsigned NumArgs; 2975 2976 const TemplateArgument *getArgBuffer() const { 2977 return reinterpret_cast<const TemplateArgument*>(this+1); 2978 } 2979 TemplateArgument *getArgBuffer() { 2980 return reinterpret_cast<TemplateArgument*>(this+1); 2981 } 2982 2983 DependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, 2984 NestedNameSpecifier *NNS, 2985 const IdentifierInfo *Name, 2986 unsigned NumArgs, 2987 const TemplateArgument *Args, 2988 QualType Canon); 2989 2990 friend class ASTContext; // ASTContext creates these 2991 2992public: 2993 virtual ~DependentTemplateSpecializationType(); 2994 2995 NestedNameSpecifier *getQualifier() const { return NNS; } 2996 const IdentifierInfo *getIdentifier() const { return Name; } 2997 2998 /// \brief Retrieve the template arguments. 2999 const TemplateArgument *getArgs() const { 3000 return getArgBuffer(); 3001 } 3002 3003 /// \brief Retrieve the number of template arguments. 3004 unsigned getNumArgs() const { return NumArgs; } 3005 3006 const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h 3007 3008 typedef const TemplateArgument * iterator; 3009 iterator begin() const { return getArgs(); } 3010 iterator end() const; // inline in TemplateBase.h 3011 3012 bool isSugared() const { return false; } 3013 QualType desugar() const { return QualType(this, 0); } 3014 3015 void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context) { 3016 Profile(ID, Context, getKeyword(), NNS, Name, NumArgs, getArgs()); 3017 } 3018 3019 static void Profile(llvm::FoldingSetNodeID &ID, 3020 ASTContext &Context, 3021 ElaboratedTypeKeyword Keyword, 3022 NestedNameSpecifier *Qualifier, 3023 const IdentifierInfo *Name, 3024 unsigned NumArgs, 3025 const TemplateArgument *Args); 3026 3027 static bool classof(const Type *T) { 3028 return T->getTypeClass() == DependentTemplateSpecialization; 3029 } 3030 static bool classof(const DependentTemplateSpecializationType *T) { 3031 return true; 3032 } 3033}; 3034 3035/// ObjCObjectType - Represents a class type in Objective C. 3036/// Every Objective C type is a combination of a base type and a 3037/// list of protocols. 3038/// 3039/// Given the following declarations: 3040/// @class C; 3041/// @protocol P; 3042/// 3043/// 'C' is an ObjCInterfaceType C. It is sugar for an ObjCObjectType 3044/// with base C and no protocols. 3045/// 3046/// 'C<P>' is an ObjCObjectType with base C and protocol list [P]. 3047/// 3048/// 'id' is a TypedefType which is sugar for an ObjCPointerType whose 3049/// pointee is an ObjCObjectType with base BuiltinType::ObjCIdType 3050/// and no protocols. 3051/// 3052/// 'id<P>' is an ObjCPointerType whose pointee is an ObjCObjecType 3053/// with base BuiltinType::ObjCIdType and protocol list [P]. Eventually 3054/// this should get its own sugar class to better represent the source. 3055class ObjCObjectType : public Type { 3056 // ObjCObjectType.NumProtocols - the number of protocols stored 3057 // after the ObjCObjectPointerType node. 3058 // 3059 // These protocols are those written directly on the type. If 3060 // protocol qualifiers ever become additive, the iterators will need 3061 // to get kindof complicated. 3062 // 3063 // In the canonical object type, these are sorted alphabetically 3064 // and uniqued. 3065 3066 /// Either a BuiltinType or an InterfaceType or sugar for either. 3067 QualType BaseType; 3068 3069 ObjCProtocolDecl * const *getProtocolStorage() const { 3070 return const_cast<ObjCObjectType*>(this)->getProtocolStorage(); 3071 } 3072 3073 ObjCProtocolDecl **getProtocolStorage(); 3074 3075protected: 3076 ObjCObjectType(QualType Canonical, QualType Base, 3077 ObjCProtocolDecl * const *Protocols, unsigned NumProtocols); 3078 3079 enum Nonce_ObjCInterface { Nonce_ObjCInterface }; 3080 ObjCObjectType(enum Nonce_ObjCInterface) 3081 : Type(ObjCInterface, QualType(), false, false), 3082 BaseType(QualType(this_(), 0)) { 3083 ObjCObjectTypeBits.NumProtocols = 0; 3084 } 3085 3086protected: 3087 std::pair<Linkage, bool> getLinkageUnnamedLocalImpl() const; // key function 3088 3089public: 3090 /// getBaseType - Gets the base type of this object type. This is 3091 /// always (possibly sugar for) one of: 3092 /// - the 'id' builtin type (as opposed to the 'id' type visible to the 3093 /// user, which is a typedef for an ObjCPointerType) 3094 /// - the 'Class' builtin type (same caveat) 3095 /// - an ObjCObjectType (currently always an ObjCInterfaceType) 3096 QualType getBaseType() const { return BaseType; } 3097 3098 bool isObjCId() const { 3099 return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCId); 3100 } 3101 bool isObjCClass() const { 3102 return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCClass); 3103 } 3104 bool isObjCUnqualifiedId() const { return qual_empty() && isObjCId(); } 3105 bool isObjCUnqualifiedClass() const { return qual_empty() && isObjCClass(); } 3106 bool isObjCUnqualifiedIdOrClass() const { 3107 if (!qual_empty()) return false; 3108 if (const BuiltinType *T = getBaseType()->getAs<BuiltinType>()) 3109 return T->getKind() == BuiltinType::ObjCId || 3110 T->getKind() == BuiltinType::ObjCClass; 3111 return false; 3112 } 3113 bool isObjCQualifiedId() const { return !qual_empty() && isObjCId(); } 3114 bool isObjCQualifiedClass() const { return !qual_empty() && isObjCClass(); } 3115 3116 /// Gets the interface declaration for this object type, if the base type 3117 /// really is an interface. 3118 ObjCInterfaceDecl *getInterface() const; 3119 3120 typedef ObjCProtocolDecl * const *qual_iterator; 3121 3122 qual_iterator qual_begin() const { return getProtocolStorage(); } 3123 qual_iterator qual_end() const { return qual_begin() + getNumProtocols(); } 3124 3125 bool qual_empty() const { return getNumProtocols() == 0; } 3126 3127 /// getNumProtocols - Return the number of qualifying protocols in this 3128 /// interface type, or 0 if there are none. 3129 unsigned getNumProtocols() const { return ObjCObjectTypeBits.NumProtocols; } 3130 3131 /// \brief Fetch a protocol by index. 3132 ObjCProtocolDecl *getProtocol(unsigned I) const { 3133 assert(I < getNumProtocols() && "Out-of-range protocol access"); 3134 return qual_begin()[I]; 3135 } 3136 3137 bool isSugared() const { return false; } 3138 QualType desugar() const { return QualType(this, 0); } 3139 3140 static bool classof(const Type *T) { 3141 return T->getTypeClass() == ObjCObject || 3142 T->getTypeClass() == ObjCInterface; 3143 } 3144 static bool classof(const ObjCObjectType *) { return true; } 3145}; 3146 3147/// ObjCObjectTypeImpl - A class providing a concrete implementation 3148/// of ObjCObjectType, so as to not increase the footprint of 3149/// ObjCInterfaceType. Code outside of ASTContext and the core type 3150/// system should not reference this type. 3151class ObjCObjectTypeImpl : public ObjCObjectType, public llvm::FoldingSetNode { 3152 friend class ASTContext; 3153 3154 // If anyone adds fields here, ObjCObjectType::getProtocolStorage() 3155 // will need to be modified. 3156 3157 ObjCObjectTypeImpl(QualType Canonical, QualType Base, 3158 ObjCProtocolDecl * const *Protocols, 3159 unsigned NumProtocols) 3160 : ObjCObjectType(Canonical, Base, Protocols, NumProtocols) {} 3161 3162public: 3163 void Profile(llvm::FoldingSetNodeID &ID); 3164 static void Profile(llvm::FoldingSetNodeID &ID, 3165 QualType Base, 3166 ObjCProtocolDecl *const *protocols, 3167 unsigned NumProtocols); 3168}; 3169 3170inline ObjCProtocolDecl **ObjCObjectType::getProtocolStorage() { 3171 return reinterpret_cast<ObjCProtocolDecl**>( 3172 static_cast<ObjCObjectTypeImpl*>(this) + 1); 3173} 3174 3175/// ObjCInterfaceType - Interfaces are the core concept in Objective-C for 3176/// object oriented design. They basically correspond to C++ classes. There 3177/// are two kinds of interface types, normal interfaces like "NSString" and 3178/// qualified interfaces, which are qualified with a protocol list like 3179/// "NSString<NSCopyable, NSAmazing>". 3180/// 3181/// ObjCInterfaceType guarantees the following properties when considered 3182/// as a subtype of its superclass, ObjCObjectType: 3183/// - There are no protocol qualifiers. To reinforce this, code which 3184/// tries to invoke the protocol methods via an ObjCInterfaceType will 3185/// fail to compile. 3186/// - It is its own base type. That is, if T is an ObjCInterfaceType*, 3187/// T->getBaseType() == QualType(T, 0). 3188class ObjCInterfaceType : public ObjCObjectType { 3189 ObjCInterfaceDecl *Decl; 3190 3191 ObjCInterfaceType(const ObjCInterfaceDecl *D) 3192 : ObjCObjectType(Nonce_ObjCInterface), 3193 Decl(const_cast<ObjCInterfaceDecl*>(D)) {} 3194 friend class ASTContext; // ASTContext creates these. 3195public: 3196 /// getDecl - Get the declaration of this interface. 3197 ObjCInterfaceDecl *getDecl() const { return Decl; } 3198 3199 bool isSugared() const { return false; } 3200 QualType desugar() const { return QualType(this, 0); } 3201 3202 static bool classof(const Type *T) { 3203 return T->getTypeClass() == ObjCInterface; 3204 } 3205 static bool classof(const ObjCInterfaceType *) { return true; } 3206 3207 // Nonsense to "hide" certain members of ObjCObjectType within this 3208 // class. People asking for protocols on an ObjCInterfaceType are 3209 // not going to get what they want: ObjCInterfaceTypes are 3210 // guaranteed to have no protocols. 3211 enum { 3212 qual_iterator, 3213 qual_begin, 3214 qual_end, 3215 getNumProtocols, 3216 getProtocol 3217 }; 3218}; 3219 3220inline ObjCInterfaceDecl *ObjCObjectType::getInterface() const { 3221 if (const ObjCInterfaceType *T = 3222 getBaseType()->getAs<ObjCInterfaceType>()) 3223 return T->getDecl(); 3224 return 0; 3225} 3226 3227/// ObjCObjectPointerType - Used to represent a pointer to an 3228/// Objective C object. These are constructed from pointer 3229/// declarators when the pointee type is an ObjCObjectType (or sugar 3230/// for one). In addition, the 'id' and 'Class' types are typedefs 3231/// for these, and the protocol-qualified types 'id<P>' and 'Class<P>' 3232/// are translated into these. 3233/// 3234/// Pointers to pointers to Objective C objects are still PointerTypes; 3235/// only the first level of pointer gets it own type implementation. 3236class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode { 3237 QualType PointeeType; 3238 3239 ObjCObjectPointerType(QualType Canonical, QualType Pointee) 3240 : Type(ObjCObjectPointer, Canonical, false, false), 3241 PointeeType(Pointee) {} 3242 friend class ASTContext; // ASTContext creates these. 3243 3244protected: 3245 virtual std::pair<Linkage, bool> getLinkageUnnamedLocalImpl() const; 3246 3247public: 3248 /// getPointeeType - Gets the type pointed to by this ObjC pointer. 3249 /// The result will always be an ObjCObjectType or sugar thereof. 3250 QualType getPointeeType() const { return PointeeType; } 3251 3252 /// getObjCObjectType - Gets the type pointed to by this ObjC 3253 /// pointer. This method always returns non-null. 3254 /// 3255 /// This method is equivalent to getPointeeType() except that 3256 /// it discards any typedefs (or other sugar) between this 3257 /// type and the "outermost" object type. So for: 3258 /// @class A; @protocol P; @protocol Q; 3259 /// typedef A<P> AP; 3260 /// typedef A A1; 3261 /// typedef A1<P> A1P; 3262 /// typedef A1P<Q> A1PQ; 3263 /// For 'A*', getObjectType() will return 'A'. 3264 /// For 'A<P>*', getObjectType() will return 'A<P>'. 3265 /// For 'AP*', getObjectType() will return 'A<P>'. 3266 /// For 'A1*', getObjectType() will return 'A'. 3267 /// For 'A1<P>*', getObjectType() will return 'A1<P>'. 3268 /// For 'A1P*', getObjectType() will return 'A1<P>'. 3269 /// For 'A1PQ*', getObjectType() will return 'A1<Q>', because 3270 /// adding protocols to a protocol-qualified base discards the 3271 /// old qualifiers (for now). But if it didn't, getObjectType() 3272 /// would return 'A1P<Q>' (and we'd have to make iterating over 3273 /// qualifiers more complicated). 3274 const ObjCObjectType *getObjectType() const { 3275 return PointeeType->getAs<ObjCObjectType>(); 3276 } 3277 3278 /// getInterfaceType - If this pointer points to an Objective C 3279 /// @interface type, gets the type for that interface. Any protocol 3280 /// qualifiers on the interface are ignored. 3281 /// 3282 /// \return null if the base type for this pointer is 'id' or 'Class' 3283 const ObjCInterfaceType *getInterfaceType() const { 3284 return getObjectType()->getBaseType()->getAs<ObjCInterfaceType>(); 3285 } 3286 3287 /// getInterfaceDecl - If this pointer points to an Objective @interface 3288 /// type, gets the declaration for that interface. 3289 /// 3290 /// \return null if the base type for this pointer is 'id' or 'Class' 3291 ObjCInterfaceDecl *getInterfaceDecl() const { 3292 return getObjectType()->getInterface(); 3293 } 3294 3295 /// isObjCIdType - True if this is equivalent to the 'id' type, i.e. if 3296 /// its object type is the primitive 'id' type with no protocols. 3297 bool isObjCIdType() const { 3298 return getObjectType()->isObjCUnqualifiedId(); 3299 } 3300 3301 /// isObjCClassType - True if this is equivalent to the 'Class' type, 3302 /// i.e. if its object tive is the primitive 'Class' type with no protocols. 3303 bool isObjCClassType() const { 3304 return getObjectType()->isObjCUnqualifiedClass(); 3305 } 3306 3307 /// isObjCQualifiedIdType - True if this is equivalent to 'id<P>' for some 3308 /// non-empty set of protocols. 3309 bool isObjCQualifiedIdType() const { 3310 return getObjectType()->isObjCQualifiedId(); 3311 } 3312 3313 /// isObjCQualifiedClassType - True if this is equivalent to 'Class<P>' for 3314 /// some non-empty set of protocols. 3315 bool isObjCQualifiedClassType() const { 3316 return getObjectType()->isObjCQualifiedClass(); 3317 } 3318 3319 /// An iterator over the qualifiers on the object type. Provided 3320 /// for convenience. This will always iterate over the full set of 3321 /// protocols on a type, not just those provided directly. 3322 typedef ObjCObjectType::qual_iterator qual_iterator; 3323 3324 qual_iterator qual_begin() const { 3325 return getObjectType()->qual_begin(); 3326 } 3327 qual_iterator qual_end() const { 3328 return getObjectType()->qual_end(); 3329 } 3330 bool qual_empty() const { return getObjectType()->qual_empty(); } 3331 3332 /// getNumProtocols - Return the number of qualifying protocols on 3333 /// the object type. 3334 unsigned getNumProtocols() const { 3335 return getObjectType()->getNumProtocols(); 3336 } 3337 3338 /// \brief Retrieve a qualifying protocol by index on the object 3339 /// type. 3340 ObjCProtocolDecl *getProtocol(unsigned I) const { 3341 return getObjectType()->getProtocol(I); 3342 } 3343 3344 bool isSugared() const { return false; } 3345 QualType desugar() const { return QualType(this, 0); } 3346 3347 void Profile(llvm::FoldingSetNodeID &ID) { 3348 Profile(ID, getPointeeType()); 3349 } 3350 static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { 3351 ID.AddPointer(T.getAsOpaquePtr()); 3352 } 3353 static bool classof(const Type *T) { 3354 return T->getTypeClass() == ObjCObjectPointer; 3355 } 3356 static bool classof(const ObjCObjectPointerType *) { return true; } 3357}; 3358 3359/// A qualifier set is used to build a set of qualifiers. 3360class QualifierCollector : public Qualifiers { 3361 ASTContext *Context; 3362 3363public: 3364 QualifierCollector(Qualifiers Qs = Qualifiers()) 3365 : Qualifiers(Qs), Context(0) {} 3366 QualifierCollector(ASTContext &Context, Qualifiers Qs = Qualifiers()) 3367 : Qualifiers(Qs), Context(&Context) {} 3368 3369 void setContext(ASTContext &C) { Context = &C; } 3370 3371 /// Collect any qualifiers on the given type and return an 3372 /// unqualified type. 3373 const Type *strip(QualType QT) { 3374 addFastQualifiers(QT.getLocalFastQualifiers()); 3375 if (QT.hasLocalNonFastQualifiers()) { 3376 const ExtQuals *EQ = QT.getExtQualsUnsafe(); 3377 Context = &EQ->getContext(); 3378 addQualifiers(EQ->getQualifiers()); 3379 return EQ->getBaseType(); 3380 } 3381 return QT.getTypePtrUnsafe(); 3382 } 3383 3384 /// Apply the collected qualifiers to the given type. 3385 QualType apply(QualType QT) const; 3386 3387 /// Apply the collected qualifiers to the given type. 3388 QualType apply(const Type* T) const; 3389 3390}; 3391 3392 3393// Inline function definitions. 3394 3395inline bool QualType::isCanonical() const { 3396 const Type *T = getTypePtr(); 3397 if (hasLocalQualifiers()) 3398 return T->isCanonicalUnqualified() && !isa<ArrayType>(T); 3399 return T->isCanonicalUnqualified(); 3400} 3401 3402inline bool QualType::isCanonicalAsParam() const { 3403 if (hasLocalQualifiers()) return false; 3404 3405 const Type *T = getTypePtr(); 3406 if ((*this)->isPointerType()) { 3407 QualType BaseType = (*this)->getAs<PointerType>()->getPointeeType(); 3408 if (isa<VariableArrayType>(BaseType)) { 3409 ArrayType *AT = dyn_cast<ArrayType>(BaseType); 3410 VariableArrayType *VAT = cast<VariableArrayType>(AT); 3411 if (VAT->getSizeExpr()) 3412 T = BaseType.getTypePtr(); 3413 } 3414 } 3415 return T->isCanonicalUnqualified() && 3416 !isa<FunctionType>(T) && !isa<ArrayType>(T); 3417} 3418 3419inline bool QualType::isConstQualified() const { 3420 return isLocalConstQualified() || 3421 getTypePtr()->getCanonicalTypeInternal().isLocalConstQualified(); 3422} 3423 3424inline bool QualType::isRestrictQualified() const { 3425 return isLocalRestrictQualified() || 3426 getTypePtr()->getCanonicalTypeInternal().isLocalRestrictQualified(); 3427} 3428 3429 3430inline bool QualType::isVolatileQualified() const { 3431 return isLocalVolatileQualified() || 3432 getTypePtr()->getCanonicalTypeInternal().isLocalVolatileQualified(); 3433} 3434 3435inline bool QualType::hasQualifiers() const { 3436 return hasLocalQualifiers() || 3437 getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers(); 3438} 3439 3440inline Qualifiers QualType::getQualifiers() const { 3441 Qualifiers Quals = getLocalQualifiers(); 3442 Quals.addQualifiers( 3443 getTypePtr()->getCanonicalTypeInternal().getLocalQualifiers()); 3444 return Quals; 3445} 3446 3447inline unsigned QualType::getCVRQualifiers() const { 3448 return getLocalCVRQualifiers() | 3449 getTypePtr()->getCanonicalTypeInternal().getLocalCVRQualifiers(); 3450} 3451 3452/// getCVRQualifiersThroughArrayTypes - If there are CVR qualifiers for this 3453/// type, returns them. Otherwise, if this is an array type, recurses 3454/// on the element type until some qualifiers have been found or a non-array 3455/// type reached. 3456inline unsigned QualType::getCVRQualifiersThroughArrayTypes() const { 3457 if (unsigned Quals = getCVRQualifiers()) 3458 return Quals; 3459 QualType CT = getTypePtr()->getCanonicalTypeInternal(); 3460 if (const ArrayType *AT = dyn_cast<ArrayType>(CT)) 3461 return AT->getElementType().getCVRQualifiersThroughArrayTypes(); 3462 return 0; 3463} 3464 3465inline void QualType::removeConst() { 3466 removeFastQualifiers(Qualifiers::Const); 3467} 3468 3469inline void QualType::removeRestrict() { 3470 removeFastQualifiers(Qualifiers::Restrict); 3471} 3472 3473inline void QualType::removeVolatile() { 3474 QualifierCollector Qc; 3475 const Type *Ty = Qc.strip(*this); 3476 if (Qc.hasVolatile()) { 3477 Qc.removeVolatile(); 3478 *this = Qc.apply(Ty); 3479 } 3480} 3481 3482inline void QualType::removeCVRQualifiers(unsigned Mask) { 3483 assert(!(Mask & ~Qualifiers::CVRMask) && "mask has non-CVR bits"); 3484 3485 // Fast path: we don't need to touch the slow qualifiers. 3486 if (!(Mask & ~Qualifiers::FastMask)) { 3487 removeFastQualifiers(Mask); 3488 return; 3489 } 3490 3491 QualifierCollector Qc; 3492 const Type *Ty = Qc.strip(*this); 3493 Qc.removeCVRQualifiers(Mask); 3494 *this = Qc.apply(Ty); 3495} 3496 3497/// getAddressSpace - Return the address space of this type. 3498inline unsigned QualType::getAddressSpace() const { 3499 if (hasLocalNonFastQualifiers()) { 3500 const ExtQuals *EQ = getExtQualsUnsafe(); 3501 if (EQ->hasAddressSpace()) 3502 return EQ->getAddressSpace(); 3503 } 3504 3505 QualType CT = getTypePtr()->getCanonicalTypeInternal(); 3506 if (CT.hasLocalNonFastQualifiers()) { 3507 const ExtQuals *EQ = CT.getExtQualsUnsafe(); 3508 if (EQ->hasAddressSpace()) 3509 return EQ->getAddressSpace(); 3510 } 3511 3512 if (const ArrayType *AT = dyn_cast<ArrayType>(CT)) 3513 return AT->getElementType().getAddressSpace(); 3514 if (const RecordType *RT = dyn_cast<RecordType>(CT)) 3515 return RT->getAddressSpace(); 3516 return 0; 3517} 3518 3519/// getObjCGCAttr - Return the gc attribute of this type. 3520inline Qualifiers::GC QualType::getObjCGCAttr() const { 3521 if (hasLocalNonFastQualifiers()) { 3522 const ExtQuals *EQ = getExtQualsUnsafe(); 3523 if (EQ->hasObjCGCAttr()) 3524 return EQ->getObjCGCAttr(); 3525 } 3526 3527 QualType CT = getTypePtr()->getCanonicalTypeInternal(); 3528 if (CT.hasLocalNonFastQualifiers()) { 3529 const ExtQuals *EQ = CT.getExtQualsUnsafe(); 3530 if (EQ->hasObjCGCAttr()) 3531 return EQ->getObjCGCAttr(); 3532 } 3533 3534 if (const ArrayType *AT = dyn_cast<ArrayType>(CT)) 3535 return AT->getElementType().getObjCGCAttr(); 3536 if (const ObjCObjectPointerType *PT = CT->getAs<ObjCObjectPointerType>()) 3537 return PT->getPointeeType().getObjCGCAttr(); 3538 // We most look at all pointer types, not just pointer to interface types. 3539 if (const PointerType *PT = CT->getAs<PointerType>()) 3540 return PT->getPointeeType().getObjCGCAttr(); 3541 return Qualifiers::GCNone; 3542} 3543 3544inline FunctionType::ExtInfo getFunctionExtInfo(const Type &t) { 3545 if (const PointerType *PT = t.getAs<PointerType>()) { 3546 if (const FunctionType *FT = PT->getPointeeType()->getAs<FunctionType>()) 3547 return FT->getExtInfo(); 3548 } else if (const FunctionType *FT = t.getAs<FunctionType>()) 3549 return FT->getExtInfo(); 3550 3551 return FunctionType::ExtInfo(); 3552} 3553 3554inline FunctionType::ExtInfo getFunctionExtInfo(QualType t) { 3555 return getFunctionExtInfo(*t); 3556} 3557 3558/// \brief Determine whether this set of qualifiers is a superset of the given 3559/// set of qualifiers. 3560inline bool Qualifiers::isSupersetOf(Qualifiers Other) const { 3561 return Mask != Other.Mask && (Mask | Other.Mask) == Mask; 3562} 3563 3564/// isMoreQualifiedThan - Determine whether this type is more 3565/// qualified than the Other type. For example, "const volatile int" 3566/// is more qualified than "const int", "volatile int", and 3567/// "int". However, it is not more qualified than "const volatile 3568/// int". 3569inline bool QualType::isMoreQualifiedThan(QualType Other) const { 3570 // FIXME: work on arbitrary qualifiers 3571 unsigned MyQuals = this->getCVRQualifiersThroughArrayTypes(); 3572 unsigned OtherQuals = Other.getCVRQualifiersThroughArrayTypes(); 3573 if (getAddressSpace() != Other.getAddressSpace()) 3574 return false; 3575 return MyQuals != OtherQuals && (MyQuals | OtherQuals) == MyQuals; 3576} 3577 3578/// isAtLeastAsQualifiedAs - Determine whether this type is at last 3579/// as qualified as the Other type. For example, "const volatile 3580/// int" is at least as qualified as "const int", "volatile int", 3581/// "int", and "const volatile int". 3582inline bool QualType::isAtLeastAsQualifiedAs(QualType Other) const { 3583 // FIXME: work on arbitrary qualifiers 3584 unsigned MyQuals = this->getCVRQualifiersThroughArrayTypes(); 3585 unsigned OtherQuals = Other.getCVRQualifiersThroughArrayTypes(); 3586 if (getAddressSpace() != Other.getAddressSpace()) 3587 return false; 3588 return (MyQuals | OtherQuals) == MyQuals; 3589} 3590 3591/// getNonReferenceType - If Type is a reference type (e.g., const 3592/// int&), returns the type that the reference refers to ("const 3593/// int"). Otherwise, returns the type itself. This routine is used 3594/// throughout Sema to implement C++ 5p6: 3595/// 3596/// If an expression initially has the type "reference to T" (8.3.2, 3597/// 8.5.3), the type is adjusted to "T" prior to any further 3598/// analysis, the expression designates the object or function 3599/// denoted by the reference, and the expression is an lvalue. 3600inline QualType QualType::getNonReferenceType() const { 3601 if (const ReferenceType *RefType = (*this)->getAs<ReferenceType>()) 3602 return RefType->getPointeeType(); 3603 else 3604 return *this; 3605} 3606 3607inline bool Type::isFunctionType() const { 3608 return isa<FunctionType>(CanonicalType); 3609} 3610inline bool Type::isPointerType() const { 3611 return isa<PointerType>(CanonicalType); 3612} 3613inline bool Type::isAnyPointerType() const { 3614 return isPointerType() || isObjCObjectPointerType(); 3615} 3616inline bool Type::isBlockPointerType() const { 3617 return isa<BlockPointerType>(CanonicalType); 3618} 3619inline bool Type::isReferenceType() const { 3620 return isa<ReferenceType>(CanonicalType); 3621} 3622inline bool Type::isLValueReferenceType() const { 3623 return isa<LValueReferenceType>(CanonicalType); 3624} 3625inline bool Type::isRValueReferenceType() const { 3626 return isa<RValueReferenceType>(CanonicalType); 3627} 3628inline bool Type::isFunctionPointerType() const { 3629 if (const PointerType* T = getAs<PointerType>()) 3630 return T->getPointeeType()->isFunctionType(); 3631 else 3632 return false; 3633} 3634inline bool Type::isMemberPointerType() const { 3635 return isa<MemberPointerType>(CanonicalType); 3636} 3637inline bool Type::isMemberFunctionPointerType() const { 3638 if (const MemberPointerType* T = getAs<MemberPointerType>()) 3639 return T->isMemberFunctionPointer(); 3640 else 3641 return false; 3642} 3643inline bool Type::isMemberDataPointerType() const { 3644 if (const MemberPointerType* T = getAs<MemberPointerType>()) 3645 return T->isMemberDataPointer(); 3646 else 3647 return false; 3648} 3649inline bool Type::isArrayType() const { 3650 return isa<ArrayType>(CanonicalType); 3651} 3652inline bool Type::isConstantArrayType() const { 3653 return isa<ConstantArrayType>(CanonicalType); 3654} 3655inline bool Type::isIncompleteArrayType() const { 3656 return isa<IncompleteArrayType>(CanonicalType); 3657} 3658inline bool Type::isVariableArrayType() const { 3659 return isa<VariableArrayType>(CanonicalType); 3660} 3661inline bool Type::isDependentSizedArrayType() const { 3662 return isa<DependentSizedArrayType>(CanonicalType); 3663} 3664inline bool Type::isBuiltinType() const { 3665 return isa<BuiltinType>(CanonicalType); 3666} 3667inline bool Type::isRecordType() const { 3668 return isa<RecordType>(CanonicalType); 3669} 3670inline bool Type::isEnumeralType() const { 3671 return isa<EnumType>(CanonicalType); 3672} 3673inline bool Type::isAnyComplexType() const { 3674 return isa<ComplexType>(CanonicalType); 3675} 3676inline bool Type::isVectorType() const { 3677 return isa<VectorType>(CanonicalType); 3678} 3679inline bool Type::isExtVectorType() const { 3680 return isa<ExtVectorType>(CanonicalType); 3681} 3682inline bool Type::isObjCObjectPointerType() const { 3683 return isa<ObjCObjectPointerType>(CanonicalType); 3684} 3685inline bool Type::isObjCObjectType() const { 3686 return isa<ObjCObjectType>(CanonicalType); 3687} 3688inline bool Type::isObjCObjectOrInterfaceType() const { 3689 return isa<ObjCInterfaceType>(CanonicalType) || 3690 isa<ObjCObjectType>(CanonicalType); 3691} 3692 3693inline bool Type::isObjCQualifiedIdType() const { 3694 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 3695 return OPT->isObjCQualifiedIdType(); 3696 return false; 3697} 3698inline bool Type::isObjCQualifiedClassType() const { 3699 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 3700 return OPT->isObjCQualifiedClassType(); 3701 return false; 3702} 3703inline bool Type::isObjCIdType() const { 3704 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 3705 return OPT->isObjCIdType(); 3706 return false; 3707} 3708inline bool Type::isObjCClassType() const { 3709 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 3710 return OPT->isObjCClassType(); 3711 return false; 3712} 3713inline bool Type::isObjCSelType() const { 3714 if (const PointerType *OPT = getAs<PointerType>()) 3715 return OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCSel); 3716 return false; 3717} 3718inline bool Type::isObjCBuiltinType() const { 3719 return isObjCIdType() || isObjCClassType() || isObjCSelType(); 3720} 3721inline bool Type::isTemplateTypeParmType() const { 3722 return isa<TemplateTypeParmType>(CanonicalType); 3723} 3724 3725inline bool Type::isSpecificBuiltinType(unsigned K) const { 3726 if (const BuiltinType *BT = getAs<BuiltinType>()) 3727 if (BT->getKind() == (BuiltinType::Kind) K) 3728 return true; 3729 return false; 3730} 3731 3732inline bool Type::isPlaceholderType() const { 3733 if (const BuiltinType *BT = getAs<BuiltinType>()) 3734 return BT->isPlaceholderType(); 3735 return false; 3736} 3737 3738/// \brief Determines whether this is a type for which one can define 3739/// an overloaded operator. 3740inline bool Type::isOverloadableType() const { 3741 return isDependentType() || isRecordType() || isEnumeralType(); 3742} 3743 3744inline bool Type::hasPointerRepresentation() const { 3745 return (isPointerType() || isReferenceType() || isBlockPointerType() || 3746 isObjCObjectPointerType() || isNullPtrType()); 3747} 3748 3749inline bool Type::hasObjCPointerRepresentation() const { 3750 return isObjCObjectPointerType(); 3751} 3752 3753/// Insertion operator for diagnostics. This allows sending QualType's into a 3754/// diagnostic with <<. 3755inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 3756 QualType T) { 3757 DB.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), 3758 Diagnostic::ak_qualtype); 3759 return DB; 3760} 3761 3762/// Insertion operator for partial diagnostics. This allows sending QualType's 3763/// into a diagnostic with <<. 3764inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD, 3765 QualType T) { 3766 PD.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), 3767 Diagnostic::ak_qualtype); 3768 return PD; 3769} 3770 3771// Helper class template that is used by Type::getAs to ensure that one does 3772// not try to look through a qualified type to get to an array type. 3773template<typename T, 3774 bool isArrayType = (llvm::is_same<T, ArrayType>::value || 3775 llvm::is_base_of<ArrayType, T>::value)> 3776struct ArrayType_cannot_be_used_with_getAs { }; 3777 3778template<typename T> 3779struct ArrayType_cannot_be_used_with_getAs<T, true>; 3780 3781/// Member-template getAs<specific type>'. 3782template <typename T> const T *Type::getAs() const { 3783 ArrayType_cannot_be_used_with_getAs<T> at; 3784 (void)at; 3785 3786 // If this is directly a T type, return it. 3787 if (const T *Ty = dyn_cast<T>(this)) 3788 return Ty; 3789 3790 // If the canonical form of this type isn't the right kind, reject it. 3791 if (!isa<T>(CanonicalType)) 3792 return 0; 3793 3794 // If this is a typedef for the type, strip the typedef off without 3795 // losing all typedef information. 3796 return cast<T>(getUnqualifiedDesugaredType()); 3797} 3798 3799} // end namespace clang 3800 3801#endif 3802