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