Type.h revision aed1df80bf64228e1429c1edc408397f9174847c
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/AST/NestedNameSpecifier.h" 20#include "clang/AST/TemplateName.h" 21#include "llvm/Support/Casting.h" 22#include "llvm/Support/type_traits.h" 23#include "llvm/ADT/APSInt.h" 24#include "llvm/ADT/FoldingSet.h" 25#include "llvm/ADT/PointerIntPair.h" 26#include "llvm/ADT/PointerUnion.h" 27 28using llvm::isa; 29using llvm::cast; 30using llvm::cast_or_null; 31using llvm::dyn_cast; 32using llvm::dyn_cast_or_null; 33namespace clang { 34 enum { 35 TypeAlignmentInBits = 3, 36 TypeAlignment = 1 << TypeAlignmentInBits 37 }; 38 class Type; 39 class ExtQuals; 40 class QualType; 41} 42 43namespace llvm { 44 template <typename T> 45 class PointerLikeTypeTraits; 46 template<> 47 class PointerLikeTypeTraits< ::clang::Type*> { 48 public: 49 static inline void *getAsVoidPointer(::clang::Type *P) { return P; } 50 static inline ::clang::Type *getFromVoidPointer(void *P) { 51 return static_cast< ::clang::Type*>(P); 52 } 53 enum { NumLowBitsAvailable = clang::TypeAlignmentInBits }; 54 }; 55 template<> 56 class PointerLikeTypeTraits< ::clang::ExtQuals*> { 57 public: 58 static inline void *getAsVoidPointer(::clang::ExtQuals *P) { return P; } 59 static inline ::clang::ExtQuals *getFromVoidPointer(void *P) { 60 return static_cast< ::clang::ExtQuals*>(P); 61 } 62 enum { NumLowBitsAvailable = clang::TypeAlignmentInBits }; 63 }; 64 65 template <> 66 struct isPodLike<clang::QualType> { static const bool value = true; }; 67} 68 69namespace clang { 70 class ASTContext; 71 class TypedefDecl; 72 class TemplateDecl; 73 class TemplateTypeParmDecl; 74 class NonTypeTemplateParmDecl; 75 class TemplateTemplateParmDecl; 76 class TagDecl; 77 class RecordDecl; 78 class CXXRecordDecl; 79 class EnumDecl; 80 class FieldDecl; 81 class ObjCInterfaceDecl; 82 class ObjCProtocolDecl; 83 class ObjCMethodDecl; 84 class UnresolvedUsingTypenameDecl; 85 class Expr; 86 class Stmt; 87 class SourceLocation; 88 class StmtIteratorBase; 89 class TemplateArgument; 90 class TemplateArgumentLoc; 91 class TemplateArgumentListInfo; 92 class QualifiedNameType; 93 struct PrintingPolicy; 94 95 // Provide forward declarations for all of the *Type classes 96#define TYPE(Class, Base) class Class##Type; 97#include "clang/AST/TypeNodes.def" 98 99/// Qualifiers - The collection of all-type qualifiers we support. 100/// Clang supports five independent qualifiers: 101/// * C99: const, volatile, and restrict 102/// * Embedded C (TR18037): address spaces 103/// * Objective C: the GC attributes (none, weak, or strong) 104class Qualifiers { 105public: 106 enum TQ { // NOTE: These flags must be kept in sync with DeclSpec::TQ. 107 Const = 0x1, 108 Restrict = 0x2, 109 Volatile = 0x4, 110 CVRMask = Const | Volatile | Restrict 111 }; 112 113 enum GC { 114 GCNone = 0, 115 Weak, 116 Strong 117 }; 118 119 enum { 120 /// The maximum supported address space number. 121 /// 24 bits should be enough for anyone. 122 MaxAddressSpace = 0xffffffu, 123 124 /// The width of the "fast" qualifier mask. 125 FastWidth = 2, 126 127 /// The fast qualifier mask. 128 FastMask = (1 << FastWidth) - 1 129 }; 130 131 Qualifiers() : Mask(0) {} 132 133 static Qualifiers fromFastMask(unsigned Mask) { 134 Qualifiers Qs; 135 Qs.addFastQualifiers(Mask); 136 return Qs; 137 } 138 139 static Qualifiers fromCVRMask(unsigned CVR) { 140 Qualifiers Qs; 141 Qs.addCVRQualifiers(CVR); 142 return Qs; 143 } 144 145 // Deserialize qualifiers from an opaque representation. 146 static Qualifiers fromOpaqueValue(unsigned opaque) { 147 Qualifiers Qs; 148 Qs.Mask = opaque; 149 return Qs; 150 } 151 152 // Serialize these qualifiers into an opaque representation. 153 unsigned getAsOpaqueValue() const { 154 return Mask; 155 } 156 157 bool hasConst() const { return Mask & Const; } 158 void setConst(bool flag) { 159 Mask = (Mask & ~Const) | (flag ? Const : 0); 160 } 161 void removeConst() { Mask &= ~Const; } 162 void addConst() { Mask |= Const; } 163 164 bool hasVolatile() const { return Mask & Volatile; } 165 void setVolatile(bool flag) { 166 Mask = (Mask & ~Volatile) | (flag ? Volatile : 0); 167 } 168 void removeVolatile() { Mask &= ~Volatile; } 169 void addVolatile() { Mask |= Volatile; } 170 171 bool hasRestrict() const { return Mask & Restrict; } 172 void setRestrict(bool flag) { 173 Mask = (Mask & ~Restrict) | (flag ? Restrict : 0); 174 } 175 void removeRestrict() { Mask &= ~Restrict; } 176 void addRestrict() { Mask |= Restrict; } 177 178 bool hasCVRQualifiers() const { return getCVRQualifiers(); } 179 unsigned getCVRQualifiers() const { return Mask & CVRMask; } 180 void setCVRQualifiers(unsigned mask) { 181 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); 182 Mask = (Mask & ~CVRMask) | mask; 183 } 184 void removeCVRQualifiers(unsigned mask) { 185 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); 186 Mask &= ~mask; 187 } 188 void removeCVRQualifiers() { 189 removeCVRQualifiers(CVRMask); 190 } 191 void addCVRQualifiers(unsigned mask) { 192 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); 193 Mask |= mask; 194 } 195 196 bool hasObjCGCAttr() const { return Mask & GCAttrMask; } 197 GC getObjCGCAttr() const { return GC((Mask & GCAttrMask) >> GCAttrShift); } 198 void setObjCGCAttr(GC type) { 199 Mask = (Mask & ~GCAttrMask) | (type << GCAttrShift); 200 } 201 void removeObjCGCAttr() { setObjCGCAttr(GCNone); } 202 void addObjCGCAttr(GC type) { 203 assert(type); 204 setObjCGCAttr(type); 205 } 206 207 bool hasAddressSpace() const { return Mask & AddressSpaceMask; } 208 unsigned getAddressSpace() const { return Mask >> AddressSpaceShift; } 209 void setAddressSpace(unsigned space) { 210 assert(space <= MaxAddressSpace); 211 Mask = (Mask & ~AddressSpaceMask) 212 | (((uint32_t) space) << AddressSpaceShift); 213 } 214 void removeAddressSpace() { setAddressSpace(0); } 215 void addAddressSpace(unsigned space) { 216 assert(space); 217 setAddressSpace(space); 218 } 219 220 // Fast qualifiers are those that can be allocated directly 221 // on a QualType object. 222 bool hasFastQualifiers() const { return getFastQualifiers(); } 223 unsigned getFastQualifiers() const { return Mask & FastMask; } 224 void setFastQualifiers(unsigned mask) { 225 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); 226 Mask = (Mask & ~FastMask) | mask; 227 } 228 void removeFastQualifiers(unsigned mask) { 229 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); 230 Mask &= ~mask; 231 } 232 void removeFastQualifiers() { 233 removeFastQualifiers(FastMask); 234 } 235 void addFastQualifiers(unsigned mask) { 236 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); 237 Mask |= mask; 238 } 239 240 /// hasNonFastQualifiers - Return true if the set contains any 241 /// qualifiers which require an ExtQuals node to be allocated. 242 bool hasNonFastQualifiers() const { return Mask & ~FastMask; } 243 Qualifiers getNonFastQualifiers() const { 244 Qualifiers Quals = *this; 245 Quals.setFastQualifiers(0); 246 return Quals; 247 } 248 249 /// hasQualifiers - Return true if the set contains any qualifiers. 250 bool hasQualifiers() const { return Mask; } 251 bool empty() const { return !Mask; } 252 253 /// \brief Add the qualifiers from the given set to this set. 254 void addQualifiers(Qualifiers Q) { 255 // If the other set doesn't have any non-boolean qualifiers, just 256 // bit-or it in. 257 if (!(Q.Mask & ~CVRMask)) 258 Mask |= Q.Mask; 259 else { 260 Mask |= (Q.Mask & CVRMask); 261 if (Q.hasAddressSpace()) 262 addAddressSpace(Q.getAddressSpace()); 263 if (Q.hasObjCGCAttr()) 264 addObjCGCAttr(Q.getObjCGCAttr()); 265 } 266 } 267 268 bool operator==(Qualifiers Other) const { return Mask == Other.Mask; } 269 bool operator!=(Qualifiers Other) const { return Mask != Other.Mask; } 270 271 operator bool() const { return hasQualifiers(); } 272 273 Qualifiers &operator+=(Qualifiers R) { 274 addQualifiers(R); 275 return *this; 276 } 277 278 // Union two qualifier sets. If an enumerated qualifier appears 279 // in both sets, use the one from the right. 280 friend Qualifiers operator+(Qualifiers L, Qualifiers R) { 281 L += R; 282 return L; 283 } 284 285 std::string getAsString() const; 286 std::string getAsString(const PrintingPolicy &Policy) const { 287 std::string Buffer; 288 getAsStringInternal(Buffer, Policy); 289 return Buffer; 290 } 291 void getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const; 292 293 void Profile(llvm::FoldingSetNodeID &ID) const { 294 ID.AddInteger(Mask); 295 } 296 297private: 298 299 // bits: |0 1 2|3 .. 4|5 .. 31| 300 // |C R V|GCAttr|AddrSpace| 301 uint32_t Mask; 302 303 static const uint32_t GCAttrMask = 0x18; 304 static const uint32_t GCAttrShift = 3; 305 static const uint32_t AddressSpaceMask = ~(CVRMask | GCAttrMask); 306 static const uint32_t AddressSpaceShift = 5; 307}; 308 309 310/// ExtQuals - We can encode up to three bits in the low bits of a 311/// type pointer, but there are many more type qualifiers that we want 312/// to be able to apply to an arbitrary type. Therefore we have this 313/// struct, intended to be heap-allocated and used by QualType to 314/// store qualifiers. 315/// 316/// The current design tags the 'const' and 'restrict' qualifiers in 317/// two low bits on the QualType pointer; a third bit records whether 318/// the pointer is an ExtQuals node. 'const' was chosen because it is 319/// orders of magnitude more common than the other two qualifiers, in 320/// both library and user code. It's relatively rare to see 321/// 'restrict' in user code, but many standard C headers are saturated 322/// with 'restrict' declarations, so that representing them efficiently 323/// is a critical goal of this representation. 324class ExtQuals : public llvm::FoldingSetNode { 325 // NOTE: changing the fast qualifiers should be straightforward as 326 // long as you don't make 'const' non-fast. 327 // 1. Qualifiers: 328 // a) Modify the bitmasks (Qualifiers::TQ and DeclSpec::TQ). 329 // Fast qualifiers must occupy the low-order bits. 330 // b) Update Qualifiers::FastWidth and FastMask. 331 // 2. QualType: 332 // a) Update is{Volatile,Restrict}Qualified(), defined inline. 333 // b) Update remove{Volatile,Restrict}, defined near the end of 334 // this header. 335 // 3. ASTContext: 336 // a) Update get{Volatile,Restrict}Type. 337 338 /// Context - the context to which this set belongs. We save this 339 /// here so that QualifierCollector can use it to reapply extended 340 /// qualifiers to an arbitrary type without requiring a context to 341 /// be pushed through every single API dealing with qualifiers. 342 ASTContext& Context; 343 344 /// BaseType - the underlying type that this qualifies 345 const Type *BaseType; 346 347 /// Quals - the immutable set of qualifiers applied by this 348 /// node; always contains extended qualifiers. 349 Qualifiers Quals; 350 351public: 352 ExtQuals(ASTContext& Context, const Type *Base, Qualifiers Quals) 353 : Context(Context), BaseType(Base), Quals(Quals) 354 { 355 assert(Quals.hasNonFastQualifiers() 356 && "ExtQuals created with no fast qualifiers"); 357 assert(!Quals.hasFastQualifiers() 358 && "ExtQuals created with fast qualifiers"); 359 } 360 361 Qualifiers getQualifiers() const { return Quals; } 362 363 bool hasVolatile() const { return Quals.hasVolatile(); } 364 365 bool hasObjCGCAttr() const { return Quals.hasObjCGCAttr(); } 366 Qualifiers::GC getObjCGCAttr() const { return Quals.getObjCGCAttr(); } 367 368 bool hasAddressSpace() const { return Quals.hasAddressSpace(); } 369 unsigned getAddressSpace() const { return Quals.getAddressSpace(); } 370 371 const Type *getBaseType() const { return BaseType; } 372 373 ASTContext &getContext() const { return Context; } 374 375public: 376 void Profile(llvm::FoldingSetNodeID &ID) const { 377 Profile(ID, getBaseType(), Quals); 378 } 379 static void Profile(llvm::FoldingSetNodeID &ID, 380 const Type *BaseType, 381 Qualifiers Quals) { 382 assert(!Quals.hasFastQualifiers() && "fast qualifiers in ExtQuals hash!"); 383 ID.AddPointer(BaseType); 384 Quals.Profile(ID); 385 } 386}; 387 388 389/// QualType - For efficiency, we don't store CV-qualified types as nodes on 390/// their own: instead each reference to a type stores the qualifiers. This 391/// greatly reduces the number of nodes we need to allocate for types (for 392/// example we only need one for 'int', 'const int', 'volatile int', 393/// 'const volatile int', etc). 394/// 395/// As an added efficiency bonus, instead of making this a pair, we 396/// just store the two bits we care about in the low bits of the 397/// pointer. To handle the packing/unpacking, we make QualType be a 398/// simple wrapper class that acts like a smart pointer. A third bit 399/// indicates whether there are extended qualifiers present, in which 400/// case the pointer points to a special structure. 401class QualType { 402 // Thankfully, these are efficiently composable. 403 llvm::PointerIntPair<llvm::PointerUnion<const Type*,const ExtQuals*>, 404 Qualifiers::FastWidth> Value; 405 406 const ExtQuals *getExtQualsUnsafe() const { 407 return Value.getPointer().get<const ExtQuals*>(); 408 } 409 410 const Type *getTypePtrUnsafe() const { 411 return Value.getPointer().get<const Type*>(); 412 } 413 414 QualType getUnqualifiedTypeSlow() const; 415 416 friend class QualifierCollector; 417public: 418 QualType() {} 419 420 QualType(const Type *Ptr, unsigned Quals) 421 : Value(Ptr, Quals) {} 422 QualType(const ExtQuals *Ptr, unsigned Quals) 423 : Value(Ptr, Quals) {} 424 425 unsigned getLocalFastQualifiers() const { return Value.getInt(); } 426 void setLocalFastQualifiers(unsigned Quals) { Value.setInt(Quals); } 427 428 /// Retrieves a pointer to the underlying (unqualified) type. 429 /// This should really return a const Type, but it's not worth 430 /// changing all the users right now. 431 Type *getTypePtr() const { 432 if (hasLocalNonFastQualifiers()) 433 return const_cast<Type*>(getExtQualsUnsafe()->getBaseType()); 434 return const_cast<Type*>(getTypePtrUnsafe()); 435 } 436 437 void *getAsOpaquePtr() const { return Value.getOpaqueValue(); } 438 static QualType getFromOpaquePtr(void *Ptr) { 439 QualType T; 440 T.Value.setFromOpaqueValue(Ptr); 441 return T; 442 } 443 444 Type &operator*() const { 445 return *getTypePtr(); 446 } 447 448 Type *operator->() const { 449 return getTypePtr(); 450 } 451 452 bool isCanonical() const; 453 bool isCanonicalAsParam() const; 454 455 /// isNull - Return true if this QualType doesn't point to a type yet. 456 bool isNull() const { 457 return Value.getPointer().isNull(); 458 } 459 460 /// \brief Determine whether this particular QualType instance has the 461 /// "const" qualifier set, without looking through typedefs that may have 462 /// added "const" at a different level. 463 bool isLocalConstQualified() const { 464 return (getLocalFastQualifiers() & Qualifiers::Const); 465 } 466 467 /// \brief Determine whether this type is const-qualified. 468 bool isConstQualified() const; 469 470 /// \brief Determine whether this particular QualType instance has the 471 /// "restrict" qualifier set, without looking through typedefs that may have 472 /// added "restrict" at a different level. 473 bool isLocalRestrictQualified() const { 474 return (getLocalFastQualifiers() & Qualifiers::Restrict); 475 } 476 477 /// \brief Determine whether this type is restrict-qualified. 478 bool isRestrictQualified() const; 479 480 /// \brief Determine whether this particular QualType instance has the 481 /// "volatile" qualifier set, without looking through typedefs that may have 482 /// added "volatile" at a different level. 483 bool isLocalVolatileQualified() const { 484 return (hasLocalNonFastQualifiers() && getExtQualsUnsafe()->hasVolatile()); 485 } 486 487 /// \brief Determine whether this type is volatile-qualified. 488 bool isVolatileQualified() const; 489 490 /// \brief Determine whether this particular QualType instance has any 491 /// qualifiers, without looking through any typedefs that might add 492 /// qualifiers at a different level. 493 bool hasLocalQualifiers() const { 494 return getLocalFastQualifiers() || hasLocalNonFastQualifiers(); 495 } 496 497 /// \brief Determine whether this type has any qualifiers. 498 bool hasQualifiers() const; 499 500 /// \brief Determine whether this particular QualType instance has any 501 /// "non-fast" qualifiers, e.g., those that are stored in an ExtQualType 502 /// instance. 503 bool hasLocalNonFastQualifiers() const { 504 return Value.getPointer().is<const ExtQuals*>(); 505 } 506 507 /// \brief Retrieve the set of qualifiers local to this particular QualType 508 /// instance, not including any qualifiers acquired through typedefs or 509 /// other sugar. 510 Qualifiers getLocalQualifiers() const { 511 Qualifiers Quals; 512 if (hasLocalNonFastQualifiers()) 513 Quals = getExtQualsUnsafe()->getQualifiers(); 514 Quals.addFastQualifiers(getLocalFastQualifiers()); 515 return Quals; 516 } 517 518 /// \brief Retrieve the set of qualifiers applied to this type. 519 Qualifiers getQualifiers() const; 520 521 /// \brief Retrieve the set of CVR (const-volatile-restrict) qualifiers 522 /// local to this particular QualType instance, not including any qualifiers 523 /// acquired through typedefs or other sugar. 524 unsigned getLocalCVRQualifiers() const { 525 unsigned CVR = getLocalFastQualifiers(); 526 if (isLocalVolatileQualified()) 527 CVR |= Qualifiers::Volatile; 528 return CVR; 529 } 530 531 /// \brief Retrieve the set of CVR (const-volatile-restrict) qualifiers 532 /// applied to this type. 533 unsigned getCVRQualifiers() const; 534 535 bool isConstant(ASTContext& Ctx) const { 536 return QualType::isConstant(*this, Ctx); 537 } 538 539 // Don't promise in the API that anything besides 'const' can be 540 // easily added. 541 542 /// addConst - add the specified type qualifier to this QualType. 543 void addConst() { 544 addFastQualifiers(Qualifiers::Const); 545 } 546 QualType withConst() const { 547 return withFastQualifiers(Qualifiers::Const); 548 } 549 550 void addFastQualifiers(unsigned TQs) { 551 assert(!(TQs & ~Qualifiers::FastMask) 552 && "non-fast qualifier bits set in mask!"); 553 Value.setInt(Value.getInt() | TQs); 554 } 555 556 // FIXME: The remove* functions are semantically broken, because they might 557 // not remove a qualifier stored on a typedef. Most of the with* functions 558 // have the same problem. 559 void removeConst(); 560 void removeVolatile(); 561 void removeRestrict(); 562 void removeCVRQualifiers(unsigned Mask); 563 564 void removeFastQualifiers() { Value.setInt(0); } 565 void removeFastQualifiers(unsigned Mask) { 566 assert(!(Mask & ~Qualifiers::FastMask) && "mask has non-fast qualifiers"); 567 Value.setInt(Value.getInt() & ~Mask); 568 } 569 570 // Creates a type with the given qualifiers in addition to any 571 // qualifiers already on this type. 572 QualType withFastQualifiers(unsigned TQs) const { 573 QualType T = *this; 574 T.addFastQualifiers(TQs); 575 return T; 576 } 577 578 // Creates a type with exactly the given fast qualifiers, removing 579 // any existing fast qualifiers. 580 QualType withExactFastQualifiers(unsigned TQs) const { 581 return withoutFastQualifiers().withFastQualifiers(TQs); 582 } 583 584 // Removes fast qualifiers, but leaves any extended qualifiers in place. 585 QualType withoutFastQualifiers() const { 586 QualType T = *this; 587 T.removeFastQualifiers(); 588 return T; 589 } 590 591 /// \brief Return this type with all of the instance-specific qualifiers 592 /// removed, but without removing any qualifiers that may have been applied 593 /// through typedefs. 594 QualType getLocalUnqualifiedType() const { return QualType(getTypePtr(), 0); } 595 596 /// \brief Return the unqualified form of the given type, which might be 597 /// desugared to eliminate qualifiers introduced via typedefs. 598 QualType getUnqualifiedType() const { 599 QualType T = getLocalUnqualifiedType(); 600 if (!T.hasQualifiers()) 601 return T; 602 603 return getUnqualifiedTypeSlow(); 604 } 605 606 bool isMoreQualifiedThan(QualType Other) const; 607 bool isAtLeastAsQualifiedAs(QualType Other) const; 608 QualType getNonReferenceType() const; 609 610 /// getDesugaredType - Return the specified type with any "sugar" removed from 611 /// the type. This takes off typedefs, typeof's etc. If the outer level of 612 /// the type is already concrete, it returns it unmodified. This is similar 613 /// to getting the canonical type, but it doesn't remove *all* typedefs. For 614 /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is 615 /// concrete. 616 /// 617 /// Qualifiers are left in place. 618 QualType getDesugaredType() const { 619 return QualType::getDesugaredType(*this); 620 } 621 622 /// operator==/!= - Indicate whether the specified types and qualifiers are 623 /// identical. 624 friend bool operator==(const QualType &LHS, const QualType &RHS) { 625 return LHS.Value == RHS.Value; 626 } 627 friend bool operator!=(const QualType &LHS, const QualType &RHS) { 628 return LHS.Value != RHS.Value; 629 } 630 std::string getAsString() const; 631 632 std::string getAsString(const PrintingPolicy &Policy) const { 633 std::string S; 634 getAsStringInternal(S, Policy); 635 return S; 636 } 637 void getAsStringInternal(std::string &Str, 638 const PrintingPolicy &Policy) const; 639 640 void dump(const char *s) const; 641 void dump() const; 642 643 void Profile(llvm::FoldingSetNodeID &ID) const { 644 ID.AddPointer(getAsOpaquePtr()); 645 } 646 647 /// getAddressSpace - Return the address space of this type. 648 inline unsigned getAddressSpace() const; 649 650 /// GCAttrTypesAttr - Returns gc attribute of this type. 651 inline Qualifiers::GC getObjCGCAttr() const; 652 653 /// isObjCGCWeak true when Type is objc's weak. 654 bool isObjCGCWeak() const { 655 return getObjCGCAttr() == Qualifiers::Weak; 656 } 657 658 /// isObjCGCStrong true when Type is objc's strong. 659 bool isObjCGCStrong() const { 660 return getObjCGCAttr() == Qualifiers::Strong; 661 } 662 663 /// getNoReturnAttr - Returns true if the type has the noreturn attribute, 664 /// false otherwise. 665 bool getNoReturnAttr() const; 666 667private: 668 // These methods are implemented in a separate translation unit; 669 // "static"-ize them to avoid creating temporary QualTypes in the 670 // caller. 671 static bool isConstant(QualType T, ASTContext& Ctx); 672 static QualType getDesugaredType(QualType T); 673}; 674 675} // end clang. 676 677namespace llvm { 678/// Implement simplify_type for QualType, so that we can dyn_cast from QualType 679/// to a specific Type class. 680template<> struct simplify_type<const ::clang::QualType> { 681 typedef ::clang::Type* SimpleType; 682 static SimpleType getSimplifiedValue(const ::clang::QualType &Val) { 683 return Val.getTypePtr(); 684 } 685}; 686template<> struct simplify_type< ::clang::QualType> 687 : public simplify_type<const ::clang::QualType> {}; 688 689// Teach SmallPtrSet that QualType is "basically a pointer". 690template<> 691class PointerLikeTypeTraits<clang::QualType> { 692public: 693 static inline void *getAsVoidPointer(clang::QualType P) { 694 return P.getAsOpaquePtr(); 695 } 696 static inline clang::QualType getFromVoidPointer(void *P) { 697 return clang::QualType::getFromOpaquePtr(P); 698 } 699 // Various qualifiers go in low bits. 700 enum { NumLowBitsAvailable = 0 }; 701}; 702 703} // end namespace llvm 704 705namespace clang { 706 707/// Type - This is the base class of the type hierarchy. A central concept 708/// with types is that each type always has a canonical type. A canonical type 709/// is the type with any typedef names stripped out of it or the types it 710/// references. For example, consider: 711/// 712/// typedef int foo; 713/// typedef foo* bar; 714/// 'int *' 'foo *' 'bar' 715/// 716/// There will be a Type object created for 'int'. Since int is canonical, its 717/// canonicaltype pointer points to itself. There is also a Type for 'foo' (a 718/// TypedefType). Its CanonicalType pointer points to the 'int' Type. Next 719/// there is a PointerType that represents 'int*', which, like 'int', is 720/// canonical. Finally, there is a PointerType type for 'foo*' whose canonical 721/// type is 'int*', and there is a TypedefType for 'bar', whose canonical type 722/// is also 'int*'. 723/// 724/// Non-canonical types are useful for emitting diagnostics, without losing 725/// information about typedefs being used. Canonical types are useful for type 726/// comparisons (they allow by-pointer equality tests) and useful for reasoning 727/// about whether something has a particular form (e.g. is a function type), 728/// because they implicitly, recursively, strip all typedefs out of a type. 729/// 730/// Types, once created, are immutable. 731/// 732class Type { 733public: 734 enum TypeClass { 735#define TYPE(Class, Base) Class, 736#define ABSTRACT_TYPE(Class, Base) 737#include "clang/AST/TypeNodes.def" 738 TagFirst = Record, TagLast = Enum 739 }; 740 741protected: 742 enum { TypeClassBitSize = 6 }; 743 744private: 745 QualType CanonicalType; 746 747 /// Dependent - Whether this type is a dependent type (C++ [temp.dep.type]). 748 bool Dependent : 1; 749 750 /// TypeClass bitfield - Enum that specifies what subclass this belongs to. 751 /// Note that this should stay at the end of the ivars for Type so that 752 /// subclasses can pack their bitfields into the same word. 753 unsigned TC : TypeClassBitSize; 754 755 Type(const Type&); // DO NOT IMPLEMENT. 756 void operator=(const Type&); // DO NOT IMPLEMENT. 757protected: 758 // silence VC++ warning C4355: 'this' : used in base member initializer list 759 Type *this_() { return this; } 760 Type(TypeClass tc, QualType Canonical, bool dependent) 761 : CanonicalType(Canonical.isNull() ? QualType(this_(), 0) : Canonical), 762 Dependent(dependent), TC(tc) {} 763 virtual ~Type() {} 764 virtual void Destroy(ASTContext& C); 765 friend class ASTContext; 766 767public: 768 TypeClass getTypeClass() const { return static_cast<TypeClass>(TC); } 769 770 bool isCanonicalUnqualified() const { 771 return CanonicalType.getTypePtr() == this; 772 } 773 774 /// Types are partitioned into 3 broad categories (C99 6.2.5p1): 775 /// object types, function types, and incomplete types. 776 777 /// \brief Determines whether the type describes an object in memory. 778 /// 779 /// Note that this definition of object type corresponds to the C++ 780 /// definition of object type, which includes incomplete types, as 781 /// opposed to the C definition (which does not include incomplete 782 /// types). 783 bool isObjectType() const; 784 785 /// isIncompleteType - Return true if this is an incomplete type. 786 /// A type that can describe objects, but which lacks information needed to 787 /// determine its size (e.g. void, or a fwd declared struct). Clients of this 788 /// routine will need to determine if the size is actually required. 789 bool isIncompleteType() const; 790 791 /// isIncompleteOrObjectType - Return true if this is an incomplete or object 792 /// type, in other words, not a function type. 793 bool isIncompleteOrObjectType() const { 794 return !isFunctionType(); 795 } 796 797 /// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10). 798 bool isPODType() const; 799 800 /// isLiteralType - Return true if this is a literal type 801 /// (C++0x [basic.types]p10) 802 bool isLiteralType() const; 803 804 /// isVariablyModifiedType (C99 6.7.5.2p2) - Return true for variable array 805 /// types that have a non-constant expression. This does not include "[]". 806 bool isVariablyModifiedType() const; 807 808 /// Helper methods to distinguish type categories. All type predicates 809 /// operate on the canonical type, ignoring typedefs and qualifiers. 810 811 /// isSpecificBuiltinType - Test for a particular builtin type. 812 bool isSpecificBuiltinType(unsigned K) const; 813 814 /// isIntegerType() does *not* include complex integers (a GCC extension). 815 /// isComplexIntegerType() can be used to test for complex integers. 816 bool isIntegerType() const; // C99 6.2.5p17 (int, char, bool, enum) 817 bool isEnumeralType() const; 818 bool isBooleanType() const; 819 bool isCharType() const; 820 bool isWideCharType() const; 821 bool isAnyCharacterType() const; 822 bool isIntegralType() const; 823 824 /// Floating point categories. 825 bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double) 826 /// isComplexType() does *not* include complex integers (a GCC extension). 827 /// isComplexIntegerType() can be used to test for complex integers. 828 bool isComplexType() const; // C99 6.2.5p11 (complex) 829 bool isAnyComplexType() const; // C99 6.2.5p11 (complex) + Complex Int. 830 bool isFloatingType() const; // C99 6.2.5p11 (real floating + complex) 831 bool isRealType() const; // C99 6.2.5p17 (real floating + integer) 832 bool isArithmeticType() const; // C99 6.2.5p18 (integer + floating) 833 bool isVoidType() const; // C99 6.2.5p19 834 bool isDerivedType() const; // C99 6.2.5p20 835 bool isScalarType() const; // C99 6.2.5p21 (arithmetic + pointers) 836 bool isAggregateType() const; 837 838 // Type Predicates: Check to see if this type is structurally the specified 839 // type, ignoring typedefs and qualifiers. 840 bool isFunctionType() const; 841 bool isFunctionNoProtoType() const { return getAs<FunctionNoProtoType>(); } 842 bool isFunctionProtoType() const { return getAs<FunctionProtoType>(); } 843 bool isPointerType() const; 844 bool isAnyPointerType() const; // Any C pointer or ObjC object pointer 845 bool isBlockPointerType() const; 846 bool isVoidPointerType() const; 847 bool isReferenceType() const; 848 bool isLValueReferenceType() const; 849 bool isRValueReferenceType() const; 850 bool isFunctionPointerType() const; 851 bool isMemberPointerType() const; 852 bool isMemberFunctionPointerType() const; 853 bool isArrayType() const; 854 bool isConstantArrayType() const; 855 bool isIncompleteArrayType() const; 856 bool isVariableArrayType() const; 857 bool isDependentSizedArrayType() const; 858 bool isRecordType() const; 859 bool isClassType() const; 860 bool isStructureType() const; 861 bool isUnionType() const; 862 bool isComplexIntegerType() const; // GCC _Complex integer type. 863 bool isVectorType() const; // GCC vector type. 864 bool isExtVectorType() const; // Extended vector type. 865 bool isObjCObjectPointerType() const; // Pointer to *any* ObjC object. 866 // FIXME: change this to 'raw' interface type, so we can used 'interface' type 867 // for the common case. 868 bool isObjCInterfaceType() const; // NSString or NSString<foo> 869 bool isObjCQualifiedInterfaceType() const; // NSString<foo> 870 bool isObjCQualifiedIdType() const; // id<foo> 871 bool isObjCQualifiedClassType() const; // Class<foo> 872 bool isObjCIdType() const; // id 873 bool isObjCClassType() const; // Class 874 bool isObjCSelType() const; // Class 875 bool isObjCBuiltinType() const; // 'id' or 'Class' 876 bool isTemplateTypeParmType() const; // C++ template type parameter 877 bool isNullPtrType() const; // C++0x nullptr_t 878 879 /// isDependentType - Whether this type is a dependent type, meaning 880 /// that its definition somehow depends on a template parameter 881 /// (C++ [temp.dep.type]). 882 bool isDependentType() const { return Dependent; } 883 bool isOverloadableType() const; 884 885 /// hasPointerRepresentation - Whether this type is represented 886 /// natively as a pointer; this includes pointers, references, block 887 /// pointers, and Objective-C interface, qualified id, and qualified 888 /// interface types, as well as nullptr_t. 889 bool hasPointerRepresentation() const; 890 891 /// hasObjCPointerRepresentation - Whether this type can represent 892 /// an objective pointer type for the purpose of GC'ability 893 bool hasObjCPointerRepresentation() const; 894 895 // Type Checking Functions: Check to see if this type is structurally the 896 // specified type, ignoring typedefs and qualifiers, and return a pointer to 897 // the best type we can. 898 const RecordType *getAsStructureType() const; 899 /// NOTE: getAs*ArrayType are methods on ASTContext. 900 const RecordType *getAsUnionType() const; 901 const ComplexType *getAsComplexIntegerType() const; // GCC complex int type. 902 // The following is a convenience method that returns an ObjCObjectPointerType 903 // for object declared using an interface. 904 const ObjCObjectPointerType *getAsObjCInterfacePointerType() const; 905 const ObjCObjectPointerType *getAsObjCQualifiedIdType() const; 906 const ObjCInterfaceType *getAsObjCQualifiedInterfaceType() const; 907 const CXXRecordDecl *getCXXRecordDeclForPointerType() const; 908 909 // Member-template getAs<specific type>'. This scheme will eventually 910 // replace the specific getAsXXXX methods above. 911 // 912 // There are some specializations of this member template listed 913 // immediately following this class. 914 template <typename T> const T *getAs() const; 915 916 /// getAsPointerToObjCInterfaceType - If this is a pointer to an ObjC 917 /// interface, return the interface type, otherwise return null. 918 const ObjCInterfaceType *getAsPointerToObjCInterfaceType() const; 919 920 /// getArrayElementTypeNoTypeQual - If this is an array type, return the 921 /// element type of the array, potentially with type qualifiers missing. 922 /// This method should never be used when type qualifiers are meaningful. 923 const Type *getArrayElementTypeNoTypeQual() const; 924 925 /// getPointeeType - If this is a pointer, ObjC object pointer, or block 926 /// pointer, this returns the respective pointee. 927 QualType getPointeeType() const; 928 929 /// getUnqualifiedDesugaredType() - Return the specified type with 930 /// any "sugar" removed from the type, removing any typedefs, 931 /// typeofs, etc., as well as any qualifiers. 932 const Type *getUnqualifiedDesugaredType() const; 933 934 /// More type predicates useful for type checking/promotion 935 bool isPromotableIntegerType() const; // C99 6.3.1.1p2 936 937 /// isSignedIntegerType - Return true if this is an integer type that is 938 /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], 939 /// an enum decl which has a signed representation, or a vector of signed 940 /// integer element type. 941 bool isSignedIntegerType() const; 942 943 /// isUnsignedIntegerType - Return true if this is an integer type that is 944 /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum 945 /// decl which has an unsigned representation, or a vector of unsigned integer 946 /// element type. 947 bool isUnsignedIntegerType() const; 948 949 /// isConstantSizeType - Return true if this is not a variable sized type, 950 /// according to the rules of C99 6.7.5p3. It is not legal to call this on 951 /// incomplete types. 952 bool isConstantSizeType() const; 953 954 /// isSpecifierType - Returns true if this type can be represented by some 955 /// set of type specifiers. 956 bool isSpecifierType() const; 957 958 const char *getTypeClassName() const; 959 960 QualType getCanonicalTypeInternal() const { return CanonicalType; } 961 void dump() const; 962 static bool classof(const Type *) { return true; } 963}; 964 965template <> inline const TypedefType *Type::getAs() const { 966 return dyn_cast<TypedefType>(this); 967} 968 969// We can do canonical leaf types faster, because we don't have to 970// worry about preserving child type decoration. 971#define TYPE(Class, Base) 972#define LEAF_TYPE(Class) \ 973template <> inline const Class##Type *Type::getAs() const { \ 974 return dyn_cast<Class##Type>(CanonicalType); \ 975} 976#include "clang/AST/TypeNodes.def" 977 978 979/// BuiltinType - This class is used for builtin types like 'int'. Builtin 980/// types are always canonical and have a literal name field. 981class BuiltinType : public Type { 982public: 983 enum Kind { 984 Void, 985 986 Bool, // This is bool and/or _Bool. 987 Char_U, // This is 'char' for targets where char is unsigned. 988 UChar, // This is explicitly qualified unsigned char. 989 Char16, // This is 'char16_t' for C++. 990 Char32, // This is 'char32_t' for C++. 991 UShort, 992 UInt, 993 ULong, 994 ULongLong, 995 UInt128, // __uint128_t 996 997 Char_S, // This is 'char' for targets where char is signed. 998 SChar, // This is explicitly qualified signed char. 999 WChar, // This is 'wchar_t' for C++. 1000 Short, 1001 Int, 1002 Long, 1003 LongLong, 1004 Int128, // __int128_t 1005 1006 Float, Double, LongDouble, 1007 1008 NullPtr, // This is the type of C++0x 'nullptr'. 1009 1010 Overload, // This represents the type of an overloaded function declaration. 1011 Dependent, // This represents the type of a type-dependent expression. 1012 1013 UndeducedAuto, // In C++0x, this represents the type of an auto variable 1014 // that has not been deduced yet. 1015 ObjCId, // This represents the ObjC 'id' type. 1016 ObjCClass, // This represents the ObjC 'Class' type. 1017 ObjCSel // This represents the ObjC 'SEL' type. 1018 }; 1019private: 1020 Kind TypeKind; 1021public: 1022 BuiltinType(Kind K) 1023 : Type(Builtin, QualType(), /*Dependent=*/(K == Dependent)), 1024 TypeKind(K) {} 1025 1026 Kind getKind() const { return TypeKind; } 1027 const char *getName(const LangOptions &LO) const; 1028 1029 bool isSugared() const { return false; } 1030 QualType desugar() const { return QualType(this, 0); } 1031 1032 bool isInteger() const { 1033 return TypeKind >= Bool && TypeKind <= Int128; 1034 } 1035 1036 bool isSignedInteger() const { 1037 return TypeKind >= Char_S && TypeKind <= Int128; 1038 } 1039 1040 bool isUnsignedInteger() const { 1041 return TypeKind >= Bool && TypeKind <= UInt128; 1042 } 1043 1044 bool isFloatingPoint() const { 1045 return TypeKind >= Float && TypeKind <= LongDouble; 1046 } 1047 1048 static bool classof(const Type *T) { return T->getTypeClass() == Builtin; } 1049 static bool classof(const BuiltinType *) { return true; } 1050}; 1051 1052/// FixedWidthIntType - Used for arbitrary width types that we either don't 1053/// want to or can't map to named integer types. These always have a lower 1054/// integer rank than builtin types of the same width. 1055class FixedWidthIntType : public Type { 1056private: 1057 unsigned Width; 1058 bool Signed; 1059public: 1060 FixedWidthIntType(unsigned W, bool S) : Type(FixedWidthInt, QualType(), false), 1061 Width(W), Signed(S) {} 1062 1063 unsigned getWidth() const { return Width; } 1064 bool isSigned() const { return Signed; } 1065 const char *getName() const; 1066 1067 bool isSugared() const { return false; } 1068 QualType desugar() const { return QualType(this, 0); } 1069 1070 static bool classof(const Type *T) { return T->getTypeClass() == FixedWidthInt; } 1071 static bool classof(const FixedWidthIntType *) { return true; } 1072}; 1073 1074/// ComplexType - C99 6.2.5p11 - Complex values. This supports the C99 complex 1075/// types (_Complex float etc) as well as the GCC integer complex extensions. 1076/// 1077class ComplexType : public Type, public llvm::FoldingSetNode { 1078 QualType ElementType; 1079 ComplexType(QualType Element, QualType CanonicalPtr) : 1080 Type(Complex, CanonicalPtr, Element->isDependentType()), 1081 ElementType(Element) { 1082 } 1083 friend class ASTContext; // ASTContext creates these. 1084public: 1085 QualType getElementType() const { return ElementType; } 1086 1087 bool isSugared() const { return false; } 1088 QualType desugar() const { return QualType(this, 0); } 1089 1090 void Profile(llvm::FoldingSetNodeID &ID) { 1091 Profile(ID, getElementType()); 1092 } 1093 static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) { 1094 ID.AddPointer(Element.getAsOpaquePtr()); 1095 } 1096 1097 static bool classof(const Type *T) { return T->getTypeClass() == Complex; } 1098 static bool classof(const ComplexType *) { return true; } 1099}; 1100 1101/// PointerType - C99 6.7.5.1 - Pointer Declarators. 1102/// 1103class PointerType : public Type, public llvm::FoldingSetNode { 1104 QualType PointeeType; 1105 1106 PointerType(QualType Pointee, QualType CanonicalPtr) : 1107 Type(Pointer, CanonicalPtr, Pointee->isDependentType()), PointeeType(Pointee) { 1108 } 1109 friend class ASTContext; // ASTContext creates these. 1110public: 1111 1112 QualType getPointeeType() const { return PointeeType; } 1113 1114 bool isSugared() const { return false; } 1115 QualType desugar() const { return QualType(this, 0); } 1116 1117 void Profile(llvm::FoldingSetNodeID &ID) { 1118 Profile(ID, getPointeeType()); 1119 } 1120 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { 1121 ID.AddPointer(Pointee.getAsOpaquePtr()); 1122 } 1123 1124 static bool classof(const Type *T) { return T->getTypeClass() == Pointer; } 1125 static bool classof(const PointerType *) { return true; } 1126}; 1127 1128/// BlockPointerType - pointer to a block type. 1129/// This type is to represent types syntactically represented as 1130/// "void (^)(int)", etc. Pointee is required to always be a function type. 1131/// 1132class BlockPointerType : public Type, public llvm::FoldingSetNode { 1133 QualType PointeeType; // Block is some kind of pointer type 1134 BlockPointerType(QualType Pointee, QualType CanonicalCls) : 1135 Type(BlockPointer, CanonicalCls, Pointee->isDependentType()), 1136 PointeeType(Pointee) { 1137 } 1138 friend class ASTContext; // ASTContext creates these. 1139public: 1140 1141 // Get the pointee type. Pointee is required to always be a function type. 1142 QualType getPointeeType() const { return PointeeType; } 1143 1144 bool isSugared() const { return false; } 1145 QualType desugar() const { return QualType(this, 0); } 1146 1147 void Profile(llvm::FoldingSetNodeID &ID) { 1148 Profile(ID, getPointeeType()); 1149 } 1150 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { 1151 ID.AddPointer(Pointee.getAsOpaquePtr()); 1152 } 1153 1154 static bool classof(const Type *T) { 1155 return T->getTypeClass() == BlockPointer; 1156 } 1157 static bool classof(const BlockPointerType *) { return true; } 1158}; 1159 1160/// ReferenceType - Base for LValueReferenceType and RValueReferenceType 1161/// 1162class ReferenceType : public Type, public llvm::FoldingSetNode { 1163 QualType PointeeType; 1164 1165 /// True if the type was originally spelled with an lvalue sigil. 1166 /// This is never true of rvalue references but can also be false 1167 /// on lvalue references because of C++0x [dcl.typedef]p9, 1168 /// as follows: 1169 /// 1170 /// typedef int &ref; // lvalue, spelled lvalue 1171 /// typedef int &&rvref; // rvalue 1172 /// ref &a; // lvalue, inner ref, spelled lvalue 1173 /// ref &&a; // lvalue, inner ref 1174 /// rvref &a; // lvalue, inner ref, spelled lvalue 1175 /// rvref &&a; // rvalue, inner ref 1176 bool SpelledAsLValue; 1177 1178 /// True if the inner type is a reference type. This only happens 1179 /// in non-canonical forms. 1180 bool InnerRef; 1181 1182protected: 1183 ReferenceType(TypeClass tc, QualType Referencee, QualType CanonicalRef, 1184 bool SpelledAsLValue) : 1185 Type(tc, CanonicalRef, Referencee->isDependentType()), 1186 PointeeType(Referencee), SpelledAsLValue(SpelledAsLValue), 1187 InnerRef(Referencee->isReferenceType()) { 1188 } 1189public: 1190 bool isSpelledAsLValue() const { return SpelledAsLValue; } 1191 1192 QualType getPointeeTypeAsWritten() const { return PointeeType; } 1193 QualType getPointeeType() const { 1194 // FIXME: this might strip inner qualifiers; okay? 1195 const ReferenceType *T = this; 1196 while (T->InnerRef) 1197 T = T->PointeeType->getAs<ReferenceType>(); 1198 return T->PointeeType; 1199 } 1200 1201 void Profile(llvm::FoldingSetNodeID &ID) { 1202 Profile(ID, PointeeType, SpelledAsLValue); 1203 } 1204 static void Profile(llvm::FoldingSetNodeID &ID, 1205 QualType Referencee, 1206 bool SpelledAsLValue) { 1207 ID.AddPointer(Referencee.getAsOpaquePtr()); 1208 ID.AddBoolean(SpelledAsLValue); 1209 } 1210 1211 static bool classof(const Type *T) { 1212 return T->getTypeClass() == LValueReference || 1213 T->getTypeClass() == RValueReference; 1214 } 1215 static bool classof(const ReferenceType *) { return true; } 1216}; 1217 1218/// LValueReferenceType - C++ [dcl.ref] - Lvalue reference 1219/// 1220class LValueReferenceType : public ReferenceType { 1221 LValueReferenceType(QualType Referencee, QualType CanonicalRef, 1222 bool SpelledAsLValue) : 1223 ReferenceType(LValueReference, Referencee, CanonicalRef, SpelledAsLValue) 1224 {} 1225 friend class ASTContext; // ASTContext creates these 1226public: 1227 bool isSugared() const { return false; } 1228 QualType desugar() const { return QualType(this, 0); } 1229 1230 static bool classof(const Type *T) { 1231 return T->getTypeClass() == LValueReference; 1232 } 1233 static bool classof(const LValueReferenceType *) { return true; } 1234}; 1235 1236/// RValueReferenceType - C++0x [dcl.ref] - Rvalue reference 1237/// 1238class RValueReferenceType : public ReferenceType { 1239 RValueReferenceType(QualType Referencee, QualType CanonicalRef) : 1240 ReferenceType(RValueReference, Referencee, CanonicalRef, false) { 1241 } 1242 friend class ASTContext; // ASTContext creates these 1243public: 1244 bool isSugared() const { return false; } 1245 QualType desugar() const { return QualType(this, 0); } 1246 1247 static bool classof(const Type *T) { 1248 return T->getTypeClass() == RValueReference; 1249 } 1250 static bool classof(const RValueReferenceType *) { return true; } 1251}; 1252 1253/// MemberPointerType - C++ 8.3.3 - Pointers to members 1254/// 1255class MemberPointerType : public Type, public llvm::FoldingSetNode { 1256 QualType PointeeType; 1257 /// The class of which the pointee is a member. Must ultimately be a 1258 /// RecordType, but could be a typedef or a template parameter too. 1259 const Type *Class; 1260 1261 MemberPointerType(QualType Pointee, const Type *Cls, QualType CanonicalPtr) : 1262 Type(MemberPointer, CanonicalPtr, 1263 Cls->isDependentType() || Pointee->isDependentType()), 1264 PointeeType(Pointee), Class(Cls) { 1265 } 1266 friend class ASTContext; // ASTContext creates these. 1267public: 1268 1269 QualType getPointeeType() const { return PointeeType; } 1270 1271 const Type *getClass() const { return Class; } 1272 1273 bool isSugared() const { return false; } 1274 QualType desugar() const { return QualType(this, 0); } 1275 1276 void Profile(llvm::FoldingSetNodeID &ID) { 1277 Profile(ID, getPointeeType(), getClass()); 1278 } 1279 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee, 1280 const Type *Class) { 1281 ID.AddPointer(Pointee.getAsOpaquePtr()); 1282 ID.AddPointer(Class); 1283 } 1284 1285 static bool classof(const Type *T) { 1286 return T->getTypeClass() == MemberPointer; 1287 } 1288 static bool classof(const MemberPointerType *) { return true; } 1289}; 1290 1291/// ArrayType - C99 6.7.5.2 - Array Declarators. 1292/// 1293class ArrayType : public Type, public llvm::FoldingSetNode { 1294public: 1295 /// ArraySizeModifier - Capture whether this is a normal array (e.g. int X[4]) 1296 /// an array with a static size (e.g. int X[static 4]), or an array 1297 /// with a star size (e.g. int X[*]). 1298 /// 'static' is only allowed on function parameters. 1299 enum ArraySizeModifier { 1300 Normal, Static, Star 1301 }; 1302private: 1303 /// ElementType - The element type of the array. 1304 QualType ElementType; 1305 1306 // NOTE: VC++ treats enums as signed, avoid using the ArraySizeModifier enum 1307 /// NOTE: These fields are packed into the bitfields space in the Type class. 1308 unsigned SizeModifier : 2; 1309 1310 /// IndexTypeQuals - Capture qualifiers in declarations like: 1311 /// 'int X[static restrict 4]'. For function parameters only. 1312 unsigned IndexTypeQuals : 3; 1313 1314protected: 1315 // C++ [temp.dep.type]p1: 1316 // A type is dependent if it is... 1317 // - an array type constructed from any dependent type or whose 1318 // size is specified by a constant expression that is 1319 // value-dependent, 1320 ArrayType(TypeClass tc, QualType et, QualType can, 1321 ArraySizeModifier sm, unsigned tq) 1322 : Type(tc, can, et->isDependentType() || tc == DependentSizedArray), 1323 ElementType(et), SizeModifier(sm), IndexTypeQuals(tq) {} 1324 1325 friend class ASTContext; // ASTContext creates these. 1326public: 1327 QualType getElementType() const { return ElementType; } 1328 ArraySizeModifier getSizeModifier() const { 1329 return ArraySizeModifier(SizeModifier); 1330 } 1331 Qualifiers getIndexTypeQualifiers() const { 1332 return Qualifiers::fromCVRMask(IndexTypeQuals); 1333 } 1334 unsigned getIndexTypeCVRQualifiers() const { return IndexTypeQuals; } 1335 1336 static bool classof(const Type *T) { 1337 return T->getTypeClass() == ConstantArray || 1338 T->getTypeClass() == VariableArray || 1339 T->getTypeClass() == IncompleteArray || 1340 T->getTypeClass() == DependentSizedArray; 1341 } 1342 static bool classof(const ArrayType *) { return true; } 1343}; 1344 1345/// ConstantArrayType - This class represents the canonical version of 1346/// C arrays with a specified constant size. For example, the canonical 1347/// type for 'int A[4 + 4*100]' is a ConstantArrayType where the element 1348/// type is 'int' and the size is 404. 1349class ConstantArrayType : public ArrayType { 1350 llvm::APInt Size; // Allows us to unique the type. 1351 1352 ConstantArrayType(QualType et, QualType can, const llvm::APInt &size, 1353 ArraySizeModifier sm, unsigned tq) 1354 : ArrayType(ConstantArray, et, can, sm, tq), 1355 Size(size) {} 1356protected: 1357 ConstantArrayType(TypeClass tc, QualType et, QualType can, 1358 const llvm::APInt &size, ArraySizeModifier sm, unsigned tq) 1359 : ArrayType(tc, et, can, sm, tq), Size(size) {} 1360 friend class ASTContext; // ASTContext creates these. 1361public: 1362 const llvm::APInt &getSize() const { return Size; } 1363 bool isSugared() const { return false; } 1364 QualType desugar() const { return QualType(this, 0); } 1365 1366 void Profile(llvm::FoldingSetNodeID &ID) { 1367 Profile(ID, getElementType(), getSize(), 1368 getSizeModifier(), getIndexTypeCVRQualifiers()); 1369 } 1370 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, 1371 const llvm::APInt &ArraySize, ArraySizeModifier SizeMod, 1372 unsigned TypeQuals) { 1373 ID.AddPointer(ET.getAsOpaquePtr()); 1374 ID.AddInteger(ArraySize.getZExtValue()); 1375 ID.AddInteger(SizeMod); 1376 ID.AddInteger(TypeQuals); 1377 } 1378 static bool classof(const Type *T) { 1379 return T->getTypeClass() == ConstantArray; 1380 } 1381 static bool classof(const ConstantArrayType *) { return true; } 1382}; 1383 1384/// IncompleteArrayType - This class represents C arrays with an unspecified 1385/// size. For example 'int A[]' has an IncompleteArrayType where the element 1386/// type is 'int' and the size is unspecified. 1387class IncompleteArrayType : public ArrayType { 1388 1389 IncompleteArrayType(QualType et, QualType can, 1390 ArraySizeModifier sm, unsigned tq) 1391 : ArrayType(IncompleteArray, et, can, sm, tq) {} 1392 friend class ASTContext; // ASTContext creates these. 1393public: 1394 bool isSugared() const { return false; } 1395 QualType desugar() const { return QualType(this, 0); } 1396 1397 static bool classof(const Type *T) { 1398 return T->getTypeClass() == IncompleteArray; 1399 } 1400 static bool classof(const IncompleteArrayType *) { return true; } 1401 1402 friend class StmtIteratorBase; 1403 1404 void Profile(llvm::FoldingSetNodeID &ID) { 1405 Profile(ID, getElementType(), getSizeModifier(), 1406 getIndexTypeCVRQualifiers()); 1407 } 1408 1409 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, 1410 ArraySizeModifier SizeMod, unsigned TypeQuals) { 1411 ID.AddPointer(ET.getAsOpaquePtr()); 1412 ID.AddInteger(SizeMod); 1413 ID.AddInteger(TypeQuals); 1414 } 1415}; 1416 1417/// VariableArrayType - This class represents C arrays with a specified size 1418/// which is not an integer-constant-expression. For example, 'int s[x+foo()]'. 1419/// Since the size expression is an arbitrary expression, we store it as such. 1420/// 1421/// Note: VariableArrayType's aren't uniqued (since the expressions aren't) and 1422/// should not be: two lexically equivalent variable array types could mean 1423/// different things, for example, these variables do not have the same type 1424/// dynamically: 1425/// 1426/// void foo(int x) { 1427/// int Y[x]; 1428/// ++x; 1429/// int Z[x]; 1430/// } 1431/// 1432class VariableArrayType : public ArrayType { 1433 /// SizeExpr - An assignment expression. VLA's are only permitted within 1434 /// a function block. 1435 Stmt *SizeExpr; 1436 /// Brackets - The left and right array brackets. 1437 SourceRange Brackets; 1438 1439 VariableArrayType(QualType et, QualType can, Expr *e, 1440 ArraySizeModifier sm, unsigned tq, 1441 SourceRange brackets) 1442 : ArrayType(VariableArray, et, can, sm, tq), 1443 SizeExpr((Stmt*) e), Brackets(brackets) {} 1444 friend class ASTContext; // ASTContext creates these. 1445 virtual void Destroy(ASTContext& C); 1446 1447public: 1448 Expr *getSizeExpr() const { 1449 // We use C-style casts instead of cast<> here because we do not wish 1450 // to have a dependency of Type.h on Stmt.h/Expr.h. 1451 return (Expr*) SizeExpr; 1452 } 1453 SourceRange getBracketsRange() const { return Brackets; } 1454 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } 1455 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } 1456 1457 bool isSugared() const { return false; } 1458 QualType desugar() const { return QualType(this, 0); } 1459 1460 static bool classof(const Type *T) { 1461 return T->getTypeClass() == VariableArray; 1462 } 1463 static bool classof(const VariableArrayType *) { return true; } 1464 1465 friend class StmtIteratorBase; 1466 1467 void Profile(llvm::FoldingSetNodeID &ID) { 1468 assert(0 && "Cannnot unique VariableArrayTypes."); 1469 } 1470}; 1471 1472/// DependentSizedArrayType - This type represents an array type in 1473/// C++ whose size is a value-dependent expression. For example: 1474/// 1475/// \code 1476/// template<typename T, int Size> 1477/// class array { 1478/// T data[Size]; 1479/// }; 1480/// \endcode 1481/// 1482/// For these types, we won't actually know what the array bound is 1483/// until template instantiation occurs, at which point this will 1484/// become either a ConstantArrayType or a VariableArrayType. 1485class DependentSizedArrayType : public ArrayType { 1486 ASTContext &Context; 1487 1488 /// \brief An assignment expression that will instantiate to the 1489 /// size of the array. 1490 /// 1491 /// The expression itself might be NULL, in which case the array 1492 /// type will have its size deduced from an initializer. 1493 Stmt *SizeExpr; 1494 1495 /// Brackets - The left and right array brackets. 1496 SourceRange Brackets; 1497 1498 DependentSizedArrayType(ASTContext &Context, QualType et, QualType can, 1499 Expr *e, ArraySizeModifier sm, unsigned tq, 1500 SourceRange brackets) 1501 : ArrayType(DependentSizedArray, et, can, sm, tq), 1502 Context(Context), SizeExpr((Stmt*) e), Brackets(brackets) {} 1503 friend class ASTContext; // ASTContext creates these. 1504 virtual void Destroy(ASTContext& C); 1505 1506public: 1507 Expr *getSizeExpr() const { 1508 // We use C-style casts instead of cast<> here because we do not wish 1509 // to have a dependency of Type.h on Stmt.h/Expr.h. 1510 return (Expr*) SizeExpr; 1511 } 1512 SourceRange getBracketsRange() const { return Brackets; } 1513 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } 1514 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } 1515 1516 bool isSugared() const { return false; } 1517 QualType desugar() const { return QualType(this, 0); } 1518 1519 static bool classof(const Type *T) { 1520 return T->getTypeClass() == DependentSizedArray; 1521 } 1522 static bool classof(const DependentSizedArrayType *) { return true; } 1523 1524 friend class StmtIteratorBase; 1525 1526 1527 void Profile(llvm::FoldingSetNodeID &ID) { 1528 Profile(ID, Context, getElementType(), 1529 getSizeModifier(), getIndexTypeCVRQualifiers(), getSizeExpr()); 1530 } 1531 1532 static void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context, 1533 QualType ET, ArraySizeModifier SizeMod, 1534 unsigned TypeQuals, Expr *E); 1535}; 1536 1537/// DependentSizedExtVectorType - This type represent an extended vector type 1538/// where either the type or size is dependent. For example: 1539/// @code 1540/// template<typename T, int Size> 1541/// class vector { 1542/// typedef T __attribute__((ext_vector_type(Size))) type; 1543/// } 1544/// @endcode 1545class DependentSizedExtVectorType : public Type, public llvm::FoldingSetNode { 1546 ASTContext &Context; 1547 Expr *SizeExpr; 1548 /// ElementType - The element type of the array. 1549 QualType ElementType; 1550 SourceLocation loc; 1551 1552 DependentSizedExtVectorType(ASTContext &Context, QualType ElementType, 1553 QualType can, Expr *SizeExpr, SourceLocation loc) 1554 : Type (DependentSizedExtVector, can, true), 1555 Context(Context), SizeExpr(SizeExpr), ElementType(ElementType), 1556 loc(loc) {} 1557 friend class ASTContext; 1558 virtual void Destroy(ASTContext& C); 1559 1560public: 1561 Expr *getSizeExpr() const { return SizeExpr; } 1562 QualType getElementType() const { return ElementType; } 1563 SourceLocation getAttributeLoc() const { return loc; } 1564 1565 bool isSugared() const { return false; } 1566 QualType desugar() const { return QualType(this, 0); } 1567 1568 static bool classof(const Type *T) { 1569 return T->getTypeClass() == DependentSizedExtVector; 1570 } 1571 static bool classof(const DependentSizedExtVectorType *) { return true; } 1572 1573 void Profile(llvm::FoldingSetNodeID &ID) { 1574 Profile(ID, Context, getElementType(), getSizeExpr()); 1575 } 1576 1577 static void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context, 1578 QualType ElementType, Expr *SizeExpr); 1579}; 1580 1581 1582/// VectorType - GCC generic vector type. This type is created using 1583/// __attribute__((vector_size(n)), where "n" specifies the vector size in 1584/// bytes. Since the constructor takes the number of vector elements, the 1585/// client is responsible for converting the size into the number of elements. 1586class VectorType : public Type, public llvm::FoldingSetNode { 1587protected: 1588 /// ElementType - The element type of the vector. 1589 QualType ElementType; 1590 1591 /// NumElements - The number of elements in the vector. 1592 unsigned NumElements; 1593 1594 VectorType(QualType vecType, unsigned nElements, QualType canonType) : 1595 Type(Vector, canonType, vecType->isDependentType()), 1596 ElementType(vecType), NumElements(nElements) {} 1597 VectorType(TypeClass tc, QualType vecType, unsigned nElements, 1598 QualType canonType) 1599 : Type(tc, canonType, vecType->isDependentType()), ElementType(vecType), 1600 NumElements(nElements) {} 1601 friend class ASTContext; // ASTContext creates these. 1602public: 1603 1604 QualType getElementType() const { return ElementType; } 1605 unsigned getNumElements() const { return NumElements; } 1606 1607 bool isSugared() const { return false; } 1608 QualType desugar() const { return QualType(this, 0); } 1609 1610 void Profile(llvm::FoldingSetNodeID &ID) { 1611 Profile(ID, getElementType(), getNumElements(), getTypeClass()); 1612 } 1613 static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, 1614 unsigned NumElements, TypeClass TypeClass) { 1615 ID.AddPointer(ElementType.getAsOpaquePtr()); 1616 ID.AddInteger(NumElements); 1617 ID.AddInteger(TypeClass); 1618 } 1619 static bool classof(const Type *T) { 1620 return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector; 1621 } 1622 static bool classof(const VectorType *) { return true; } 1623}; 1624 1625/// ExtVectorType - Extended vector type. This type is created using 1626/// __attribute__((ext_vector_type(n)), where "n" is the number of elements. 1627/// Unlike vector_size, ext_vector_type is only allowed on typedef's. This 1628/// class enables syntactic extensions, like Vector Components for accessing 1629/// points, colors, and textures (modeled after OpenGL Shading Language). 1630class ExtVectorType : public VectorType { 1631 ExtVectorType(QualType vecType, unsigned nElements, QualType canonType) : 1632 VectorType(ExtVector, vecType, nElements, canonType) {} 1633 friend class ASTContext; // ASTContext creates these. 1634public: 1635 static int getPointAccessorIdx(char c) { 1636 switch (c) { 1637 default: return -1; 1638 case 'x': return 0; 1639 case 'y': return 1; 1640 case 'z': return 2; 1641 case 'w': return 3; 1642 } 1643 } 1644 static int getNumericAccessorIdx(char c) { 1645 switch (c) { 1646 default: return -1; 1647 case '0': return 0; 1648 case '1': return 1; 1649 case '2': return 2; 1650 case '3': return 3; 1651 case '4': return 4; 1652 case '5': return 5; 1653 case '6': return 6; 1654 case '7': return 7; 1655 case '8': return 8; 1656 case '9': return 9; 1657 case 'A': 1658 case 'a': return 10; 1659 case 'B': 1660 case 'b': return 11; 1661 case 'C': 1662 case 'c': return 12; 1663 case 'D': 1664 case 'd': return 13; 1665 case 'E': 1666 case 'e': return 14; 1667 case 'F': 1668 case 'f': return 15; 1669 } 1670 } 1671 1672 static int getAccessorIdx(char c) { 1673 if (int idx = getPointAccessorIdx(c)+1) return idx-1; 1674 return getNumericAccessorIdx(c); 1675 } 1676 1677 bool isAccessorWithinNumElements(char c) const { 1678 if (int idx = getAccessorIdx(c)+1) 1679 return unsigned(idx-1) < NumElements; 1680 return false; 1681 } 1682 bool isSugared() const { return false; } 1683 QualType desugar() const { return QualType(this, 0); } 1684 1685 static bool classof(const Type *T) { 1686 return T->getTypeClass() == ExtVector; 1687 } 1688 static bool classof(const ExtVectorType *) { return true; } 1689}; 1690 1691/// FunctionType - C99 6.7.5.3 - Function Declarators. This is the common base 1692/// class of FunctionNoProtoType and FunctionProtoType. 1693/// 1694class FunctionType : public Type { 1695 /// SubClassData - This field is owned by the subclass, put here to pack 1696 /// tightly with the ivars in Type. 1697 bool SubClassData : 1; 1698 1699 /// TypeQuals - Used only by FunctionProtoType, put here to pack with the 1700 /// other bitfields. 1701 /// The qualifiers are part of FunctionProtoType because... 1702 /// 1703 /// C++ 8.3.5p4: The return type, the parameter type list and the 1704 /// cv-qualifier-seq, [...], are part of the function type. 1705 /// 1706 unsigned TypeQuals : 3; 1707 1708 /// NoReturn - Indicates if the function type is attribute noreturn. 1709 unsigned NoReturn : 1; 1710 1711 // The type returned by the function. 1712 QualType ResultType; 1713protected: 1714 FunctionType(TypeClass tc, QualType res, bool SubclassInfo, 1715 unsigned typeQuals, QualType Canonical, bool Dependent, 1716 bool noReturn = false) 1717 : Type(tc, Canonical, Dependent), 1718 SubClassData(SubclassInfo), TypeQuals(typeQuals), NoReturn(noReturn), 1719 ResultType(res) {} 1720 bool getSubClassData() const { return SubClassData; } 1721 unsigned getTypeQuals() const { return TypeQuals; } 1722public: 1723 1724 QualType getResultType() const { return ResultType; } 1725 bool getNoReturnAttr() const { return NoReturn; } 1726 1727 static bool classof(const Type *T) { 1728 return T->getTypeClass() == FunctionNoProto || 1729 T->getTypeClass() == FunctionProto; 1730 } 1731 static bool classof(const FunctionType *) { return true; } 1732}; 1733 1734/// FunctionNoProtoType - Represents a K&R-style 'int foo()' function, which has 1735/// no information available about its arguments. 1736class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode { 1737 FunctionNoProtoType(QualType Result, QualType Canonical, 1738 bool NoReturn = false) 1739 : FunctionType(FunctionNoProto, Result, false, 0, Canonical, 1740 /*Dependent=*/false, NoReturn) {} 1741 friend class ASTContext; // ASTContext creates these. 1742public: 1743 // No additional state past what FunctionType provides. 1744 1745 bool isSugared() const { return false; } 1746 QualType desugar() const { return QualType(this, 0); } 1747 1748 void Profile(llvm::FoldingSetNodeID &ID) { 1749 Profile(ID, getResultType(), getNoReturnAttr()); 1750 } 1751 static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType, 1752 bool NoReturn) { 1753 ID.AddInteger(NoReturn); 1754 ID.AddPointer(ResultType.getAsOpaquePtr()); 1755 } 1756 1757 static bool classof(const Type *T) { 1758 return T->getTypeClass() == FunctionNoProto; 1759 } 1760 static bool classof(const FunctionNoProtoType *) { return true; } 1761}; 1762 1763/// FunctionProtoType - Represents a prototype with argument type info, e.g. 1764/// 'int foo(int)' or 'int foo(void)'. 'void' is represented as having no 1765/// arguments, not as having a single void argument. Such a type can have an 1766/// exception specification, but this specification is not part of the canonical 1767/// type. 1768class FunctionProtoType : public FunctionType, public llvm::FoldingSetNode { 1769 /// hasAnyDependentType - Determine whether there are any dependent 1770 /// types within the arguments passed in. 1771 static bool hasAnyDependentType(const QualType *ArgArray, unsigned numArgs) { 1772 for (unsigned Idx = 0; Idx < numArgs; ++Idx) 1773 if (ArgArray[Idx]->isDependentType()) 1774 return true; 1775 1776 return false; 1777 } 1778 1779 FunctionProtoType(QualType Result, const QualType *ArgArray, unsigned numArgs, 1780 bool isVariadic, unsigned typeQuals, bool hasExs, 1781 bool hasAnyExs, const QualType *ExArray, 1782 unsigned numExs, QualType Canonical, bool NoReturn) 1783 : FunctionType(FunctionProto, Result, isVariadic, typeQuals, Canonical, 1784 (Result->isDependentType() || 1785 hasAnyDependentType(ArgArray, numArgs)), NoReturn), 1786 NumArgs(numArgs), NumExceptions(numExs), HasExceptionSpec(hasExs), 1787 AnyExceptionSpec(hasAnyExs) { 1788 // Fill in the trailing argument array. 1789 QualType *ArgInfo = reinterpret_cast<QualType*>(this+1); 1790 for (unsigned i = 0; i != numArgs; ++i) 1791 ArgInfo[i] = ArgArray[i]; 1792 // Fill in the exception array. 1793 QualType *Ex = ArgInfo + numArgs; 1794 for (unsigned i = 0; i != numExs; ++i) 1795 Ex[i] = ExArray[i]; 1796 } 1797 1798 /// NumArgs - The number of arguments this function has, not counting '...'. 1799 unsigned NumArgs : 20; 1800 1801 /// NumExceptions - The number of types in the exception spec, if any. 1802 unsigned NumExceptions : 10; 1803 1804 /// HasExceptionSpec - Whether this function has an exception spec at all. 1805 bool HasExceptionSpec : 1; 1806 1807 /// AnyExceptionSpec - Whether this function has a throw(...) spec. 1808 bool AnyExceptionSpec : 1; 1809 1810 /// ArgInfo - There is an variable size array after the class in memory that 1811 /// holds the argument types. 1812 1813 /// Exceptions - There is another variable size array after ArgInfo that 1814 /// holds the exception types. 1815 1816 friend class ASTContext; // ASTContext creates these. 1817 1818public: 1819 unsigned getNumArgs() const { return NumArgs; } 1820 QualType getArgType(unsigned i) const { 1821 assert(i < NumArgs && "Invalid argument number!"); 1822 return arg_type_begin()[i]; 1823 } 1824 1825 bool hasExceptionSpec() const { return HasExceptionSpec; } 1826 bool hasAnyExceptionSpec() const { return AnyExceptionSpec; } 1827 unsigned getNumExceptions() const { return NumExceptions; } 1828 QualType getExceptionType(unsigned i) const { 1829 assert(i < NumExceptions && "Invalid exception number!"); 1830 return exception_begin()[i]; 1831 } 1832 bool hasEmptyExceptionSpec() const { 1833 return hasExceptionSpec() && !hasAnyExceptionSpec() && 1834 getNumExceptions() == 0; 1835 } 1836 1837 bool isVariadic() const { return getSubClassData(); } 1838 unsigned getTypeQuals() const { return FunctionType::getTypeQuals(); } 1839 1840 typedef const QualType *arg_type_iterator; 1841 arg_type_iterator arg_type_begin() const { 1842 return reinterpret_cast<const QualType *>(this+1); 1843 } 1844 arg_type_iterator arg_type_end() const { return arg_type_begin()+NumArgs; } 1845 1846 typedef const QualType *exception_iterator; 1847 exception_iterator exception_begin() const { 1848 // exceptions begin where arguments end 1849 return arg_type_end(); 1850 } 1851 exception_iterator exception_end() const { 1852 return exception_begin() + NumExceptions; 1853 } 1854 1855 bool isSugared() const { return false; } 1856 QualType desugar() const { return QualType(this, 0); } 1857 1858 static bool classof(const Type *T) { 1859 return T->getTypeClass() == FunctionProto; 1860 } 1861 static bool classof(const FunctionProtoType *) { return true; } 1862 1863 void Profile(llvm::FoldingSetNodeID &ID); 1864 static void Profile(llvm::FoldingSetNodeID &ID, QualType Result, 1865 arg_type_iterator ArgTys, unsigned NumArgs, 1866 bool isVariadic, unsigned TypeQuals, 1867 bool hasExceptionSpec, bool anyExceptionSpec, 1868 unsigned NumExceptions, exception_iterator Exs, 1869 bool NoReturn); 1870}; 1871 1872 1873/// \brief Represents the dependent type named by a dependently-scoped 1874/// typename using declaration, e.g. 1875/// using typename Base<T>::foo; 1876/// Template instantiation turns these into the underlying type. 1877class UnresolvedUsingType : public Type { 1878 UnresolvedUsingTypenameDecl *Decl; 1879 1880 UnresolvedUsingType(UnresolvedUsingTypenameDecl *D) 1881 : Type(UnresolvedUsing, QualType(), true), Decl(D) {} 1882 friend class ASTContext; // ASTContext creates these. 1883public: 1884 1885 UnresolvedUsingTypenameDecl *getDecl() const { return Decl; } 1886 1887 bool isSugared() const { return false; } 1888 QualType desugar() const { return QualType(this, 0); } 1889 1890 static bool classof(const Type *T) { 1891 return T->getTypeClass() == UnresolvedUsing; 1892 } 1893 static bool classof(const UnresolvedUsingType *) { return true; } 1894 1895 void Profile(llvm::FoldingSetNodeID &ID) { 1896 return Profile(ID, Decl); 1897 } 1898 static void Profile(llvm::FoldingSetNodeID &ID, 1899 UnresolvedUsingTypenameDecl *D) { 1900 ID.AddPointer(D); 1901 } 1902}; 1903 1904 1905class TypedefType : public Type { 1906 TypedefDecl *Decl; 1907protected: 1908 TypedefType(TypeClass tc, TypedefDecl *D, QualType can) 1909 : Type(tc, can, can->isDependentType()), Decl(D) { 1910 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 1911 } 1912 friend class ASTContext; // ASTContext creates these. 1913public: 1914 1915 TypedefDecl *getDecl() const { return Decl; } 1916 1917 /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to 1918 /// potentially looking through *all* consecutive typedefs. This returns the 1919 /// sum of the type qualifiers, so if you have: 1920 /// typedef const int A; 1921 /// typedef volatile A B; 1922 /// looking through the typedefs for B will give you "const volatile A". 1923 QualType LookThroughTypedefs() const; 1924 1925 bool isSugared() const { return true; } 1926 QualType desugar() const; 1927 1928 static bool classof(const Type *T) { return T->getTypeClass() == Typedef; } 1929 static bool classof(const TypedefType *) { return true; } 1930}; 1931 1932/// TypeOfExprType (GCC extension). 1933class TypeOfExprType : public Type { 1934 Expr *TOExpr; 1935 1936protected: 1937 TypeOfExprType(Expr *E, QualType can = QualType()); 1938 friend class ASTContext; // ASTContext creates these. 1939public: 1940 Expr *getUnderlyingExpr() const { return TOExpr; } 1941 1942 /// \brief Remove a single level of sugar. 1943 QualType desugar() const; 1944 1945 /// \brief Returns whether this type directly provides sugar. 1946 bool isSugared() const { return true; } 1947 1948 static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; } 1949 static bool classof(const TypeOfExprType *) { return true; } 1950}; 1951 1952/// Subclass of TypeOfExprType that is used for canonical, dependent 1953/// typeof(expr) types. 1954class DependentTypeOfExprType 1955 : public TypeOfExprType, public llvm::FoldingSetNode { 1956 ASTContext &Context; 1957 1958public: 1959 DependentTypeOfExprType(ASTContext &Context, Expr *E) 1960 : TypeOfExprType(E), Context(Context) { } 1961 1962 bool isSugared() const { return false; } 1963 QualType desugar() const { return QualType(this, 0); } 1964 1965 void Profile(llvm::FoldingSetNodeID &ID) { 1966 Profile(ID, Context, getUnderlyingExpr()); 1967 } 1968 1969 static void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context, 1970 Expr *E); 1971}; 1972 1973/// TypeOfType (GCC extension). 1974class TypeOfType : public Type { 1975 QualType TOType; 1976 TypeOfType(QualType T, QualType can) 1977 : Type(TypeOf, can, T->isDependentType()), TOType(T) { 1978 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 1979 } 1980 friend class ASTContext; // ASTContext creates these. 1981public: 1982 QualType getUnderlyingType() const { return TOType; } 1983 1984 /// \brief Remove a single level of sugar. 1985 QualType desugar() const { return getUnderlyingType(); } 1986 1987 /// \brief Returns whether this type directly provides sugar. 1988 bool isSugared() const { return true; } 1989 1990 static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; } 1991 static bool classof(const TypeOfType *) { return true; } 1992}; 1993 1994/// DecltypeType (C++0x) 1995class DecltypeType : public Type { 1996 Expr *E; 1997 1998 // FIXME: We could get rid of UnderlyingType if we wanted to: We would have to 1999 // Move getDesugaredType to ASTContext so that it can call getDecltypeForExpr 2000 // from it. 2001 QualType UnderlyingType; 2002 2003protected: 2004 DecltypeType(Expr *E, QualType underlyingType, QualType can = QualType()); 2005 friend class ASTContext; // ASTContext creates these. 2006public: 2007 Expr *getUnderlyingExpr() const { return E; } 2008 QualType getUnderlyingType() const { return UnderlyingType; } 2009 2010 /// \brief Remove a single level of sugar. 2011 QualType desugar() const { return getUnderlyingType(); } 2012 2013 /// \brief Returns whether this type directly provides sugar. 2014 bool isSugared() const { return !isDependentType(); } 2015 2016 static bool classof(const Type *T) { return T->getTypeClass() == Decltype; } 2017 static bool classof(const DecltypeType *) { return true; } 2018}; 2019 2020/// Subclass of DecltypeType that is used for canonical, dependent 2021/// C++0x decltype types. 2022class DependentDecltypeType : public DecltypeType, public llvm::FoldingSetNode { 2023 ASTContext &Context; 2024 2025public: 2026 DependentDecltypeType(ASTContext &Context, Expr *E); 2027 2028 bool isSugared() const { return false; } 2029 QualType desugar() const { return QualType(this, 0); } 2030 2031 void Profile(llvm::FoldingSetNodeID &ID) { 2032 Profile(ID, Context, getUnderlyingExpr()); 2033 } 2034 2035 static void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context, 2036 Expr *E); 2037}; 2038 2039class TagType : public Type { 2040 /// Stores the TagDecl associated with this type. The decl will 2041 /// point to the TagDecl that actually defines the entity (or is a 2042 /// definition in progress), if there is such a definition. The 2043 /// single-bit value will be non-zero when this tag is in the 2044 /// process of being defined. 2045 mutable llvm::PointerIntPair<TagDecl *, 1> decl; 2046 friend class ASTContext; 2047 friend class TagDecl; 2048 2049protected: 2050 TagType(TypeClass TC, TagDecl *D, QualType can); 2051 2052public: 2053 TagDecl *getDecl() const { return decl.getPointer(); } 2054 2055 /// @brief Determines whether this type is in the process of being 2056 /// defined. 2057 bool isBeingDefined() const { return decl.getInt(); } 2058 void setBeingDefined(bool Def) const { decl.setInt(Def? 1 : 0); } 2059 2060 static bool classof(const Type *T) { 2061 return T->getTypeClass() >= TagFirst && T->getTypeClass() <= TagLast; 2062 } 2063 static bool classof(const TagType *) { return true; } 2064 static bool classof(const RecordType *) { return true; } 2065 static bool classof(const EnumType *) { return true; } 2066}; 2067 2068/// RecordType - This is a helper class that allows the use of isa/cast/dyncast 2069/// to detect TagType objects of structs/unions/classes. 2070class RecordType : public TagType { 2071protected: 2072 explicit RecordType(RecordDecl *D) 2073 : TagType(Record, reinterpret_cast<TagDecl*>(D), QualType()) { } 2074 explicit RecordType(TypeClass TC, RecordDecl *D) 2075 : TagType(TC, reinterpret_cast<TagDecl*>(D), QualType()) { } 2076 friend class ASTContext; // ASTContext creates these. 2077public: 2078 2079 RecordDecl *getDecl() const { 2080 return reinterpret_cast<RecordDecl*>(TagType::getDecl()); 2081 } 2082 2083 // FIXME: This predicate is a helper to QualType/Type. It needs to 2084 // recursively check all fields for const-ness. If any field is declared 2085 // const, it needs to return false. 2086 bool hasConstFields() const { return false; } 2087 2088 // FIXME: RecordType needs to check when it is created that all fields are in 2089 // the same address space, and return that. 2090 unsigned getAddressSpace() const { return 0; } 2091 2092 bool isSugared() const { return false; } 2093 QualType desugar() const { return QualType(this, 0); } 2094 2095 static bool classof(const TagType *T); 2096 static bool classof(const Type *T) { 2097 return isa<TagType>(T) && classof(cast<TagType>(T)); 2098 } 2099 static bool classof(const RecordType *) { return true; } 2100}; 2101 2102/// EnumType - This is a helper class that allows the use of isa/cast/dyncast 2103/// to detect TagType objects of enums. 2104class EnumType : public TagType { 2105 explicit EnumType(EnumDecl *D) 2106 : TagType(Enum, reinterpret_cast<TagDecl*>(D), QualType()) { } 2107 friend class ASTContext; // ASTContext creates these. 2108public: 2109 2110 EnumDecl *getDecl() const { 2111 return reinterpret_cast<EnumDecl*>(TagType::getDecl()); 2112 } 2113 2114 bool isSugared() const { return false; } 2115 QualType desugar() const { return QualType(this, 0); } 2116 2117 static bool classof(const TagType *T); 2118 static bool classof(const Type *T) { 2119 return isa<TagType>(T) && classof(cast<TagType>(T)); 2120 } 2121 static bool classof(const EnumType *) { return true; } 2122}; 2123 2124/// ElaboratedType - A non-canonical type used to represents uses of 2125/// elaborated type specifiers in C++. For example: 2126/// 2127/// void foo(union MyUnion); 2128/// ^^^^^^^^^^^^^ 2129/// 2130/// At the moment, for efficiency we do not create elaborated types in 2131/// C, since outside of typedefs all references to structs would 2132/// necessarily be elaborated. 2133class ElaboratedType : public Type, public llvm::FoldingSetNode { 2134public: 2135 enum TagKind { 2136 TK_struct, 2137 TK_union, 2138 TK_class, 2139 TK_enum 2140 }; 2141 2142private: 2143 /// The tag that was used in this elaborated type specifier. 2144 TagKind Tag; 2145 2146 /// The underlying type. 2147 QualType UnderlyingType; 2148 2149 explicit ElaboratedType(QualType Ty, TagKind Tag, QualType Canon) 2150 : Type(Elaborated, Canon, Canon->isDependentType()), 2151 Tag(Tag), UnderlyingType(Ty) { } 2152 friend class ASTContext; // ASTContext creates these. 2153 2154public: 2155 TagKind getTagKind() const { return Tag; } 2156 QualType getUnderlyingType() const { return UnderlyingType; } 2157 2158 /// \brief Remove a single level of sugar. 2159 QualType desugar() const { return getUnderlyingType(); } 2160 2161 /// \brief Returns whether this type directly provides sugar. 2162 bool isSugared() const { return true; } 2163 2164 static const char *getNameForTagKind(TagKind Kind) { 2165 switch (Kind) { 2166 default: assert(0 && "Unknown TagKind!"); 2167 case TK_struct: return "struct"; 2168 case TK_union: return "union"; 2169 case TK_class: return "class"; 2170 case TK_enum: return "enum"; 2171 } 2172 } 2173 2174 void Profile(llvm::FoldingSetNodeID &ID) { 2175 Profile(ID, getUnderlyingType(), getTagKind()); 2176 } 2177 static void Profile(llvm::FoldingSetNodeID &ID, QualType T, TagKind Tag) { 2178 ID.AddPointer(T.getAsOpaquePtr()); 2179 ID.AddInteger(Tag); 2180 } 2181 2182 static bool classof(const ElaboratedType*) { return true; } 2183 static bool classof(const Type *T) { return T->getTypeClass() == Elaborated; } 2184}; 2185 2186class TemplateTypeParmType : public Type, public llvm::FoldingSetNode { 2187 unsigned Depth : 15; 2188 unsigned Index : 16; 2189 unsigned ParameterPack : 1; 2190 IdentifierInfo *Name; 2191 2192 TemplateTypeParmType(unsigned D, unsigned I, bool PP, IdentifierInfo *N, 2193 QualType Canon) 2194 : Type(TemplateTypeParm, Canon, /*Dependent=*/true), 2195 Depth(D), Index(I), ParameterPack(PP), Name(N) { } 2196 2197 TemplateTypeParmType(unsigned D, unsigned I, bool PP) 2198 : Type(TemplateTypeParm, QualType(this, 0), /*Dependent=*/true), 2199 Depth(D), Index(I), ParameterPack(PP), Name(0) { } 2200 2201 friend class ASTContext; // ASTContext creates these 2202 2203public: 2204 unsigned getDepth() const { return Depth; } 2205 unsigned getIndex() const { return Index; } 2206 bool isParameterPack() const { return ParameterPack; } 2207 IdentifierInfo *getName() const { return Name; } 2208 2209 bool isSugared() const { return false; } 2210 QualType desugar() const { return QualType(this, 0); } 2211 2212 void Profile(llvm::FoldingSetNodeID &ID) { 2213 Profile(ID, Depth, Index, ParameterPack, Name); 2214 } 2215 2216 static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth, 2217 unsigned Index, bool ParameterPack, 2218 IdentifierInfo *Name) { 2219 ID.AddInteger(Depth); 2220 ID.AddInteger(Index); 2221 ID.AddBoolean(ParameterPack); 2222 ID.AddPointer(Name); 2223 } 2224 2225 static bool classof(const Type *T) { 2226 return T->getTypeClass() == TemplateTypeParm; 2227 } 2228 static bool classof(const TemplateTypeParmType *T) { return true; } 2229}; 2230 2231/// \brief Represents the result of substituting a type for a template 2232/// type parameter. 2233/// 2234/// Within an instantiated template, all template type parameters have 2235/// been replaced with these. They are used solely to record that a 2236/// type was originally written as a template type parameter; 2237/// therefore they are never canonical. 2238class SubstTemplateTypeParmType : public Type, public llvm::FoldingSetNode { 2239 // The original type parameter. 2240 const TemplateTypeParmType *Replaced; 2241 2242 SubstTemplateTypeParmType(const TemplateTypeParmType *Param, QualType Canon) 2243 : Type(SubstTemplateTypeParm, Canon, Canon->isDependentType()), 2244 Replaced(Param) { } 2245 2246 friend class ASTContext; 2247 2248public: 2249 IdentifierInfo *getName() const { return Replaced->getName(); } 2250 2251 /// Gets the template parameter that was substituted for. 2252 const TemplateTypeParmType *getReplacedParameter() const { 2253 return Replaced; 2254 } 2255 2256 /// Gets the type that was substituted for the template 2257 /// parameter. 2258 QualType getReplacementType() const { 2259 return getCanonicalTypeInternal(); 2260 } 2261 2262 bool isSugared() const { return true; } 2263 QualType desugar() const { return getReplacementType(); } 2264 2265 void Profile(llvm::FoldingSetNodeID &ID) { 2266 Profile(ID, getReplacedParameter(), getReplacementType()); 2267 } 2268 static void Profile(llvm::FoldingSetNodeID &ID, 2269 const TemplateTypeParmType *Replaced, 2270 QualType Replacement) { 2271 ID.AddPointer(Replaced); 2272 ID.AddPointer(Replacement.getAsOpaquePtr()); 2273 } 2274 2275 static bool classof(const Type *T) { 2276 return T->getTypeClass() == SubstTemplateTypeParm; 2277 } 2278 static bool classof(const SubstTemplateTypeParmType *T) { return true; } 2279}; 2280 2281/// \brief Represents the type of a template specialization as written 2282/// in the source code. 2283/// 2284/// Template specialization types represent the syntactic form of a 2285/// template-id that refers to a type, e.g., @c vector<int>. Some 2286/// template specialization types are syntactic sugar, whose canonical 2287/// type will point to some other type node that represents the 2288/// instantiation or class template specialization. For example, a 2289/// class template specialization type of @c vector<int> will refer to 2290/// a tag type for the instantiation 2291/// @c std::vector<int, std::allocator<int>>. 2292/// 2293/// Other template specialization types, for which the template name 2294/// is dependent, may be canonical types. These types are always 2295/// dependent. 2296class TemplateSpecializationType 2297 : public Type, public llvm::FoldingSetNode { 2298 2299 // FIXME: Currently needed for profiling expressions; can we avoid this? 2300 ASTContext &Context; 2301 2302 /// \brief The name of the template being specialized. 2303 TemplateName Template; 2304 2305 /// \brief - The number of template arguments named in this class 2306 /// template specialization. 2307 unsigned NumArgs; 2308 2309 TemplateSpecializationType(ASTContext &Context, 2310 TemplateName T, 2311 const TemplateArgument *Args, 2312 unsigned NumArgs, QualType Canon); 2313 2314 virtual void Destroy(ASTContext& C); 2315 2316 friend class ASTContext; // ASTContext creates these 2317 2318public: 2319 /// \brief Determine whether any of the given template arguments are 2320 /// dependent. 2321 static bool anyDependentTemplateArguments(const TemplateArgument *Args, 2322 unsigned NumArgs); 2323 2324 static bool anyDependentTemplateArguments(const TemplateArgumentLoc *Args, 2325 unsigned NumArgs); 2326 2327 static bool anyDependentTemplateArguments(const TemplateArgumentListInfo &); 2328 2329 /// \brief Print a template argument list, including the '<' and '>' 2330 /// enclosing the template arguments. 2331 static std::string PrintTemplateArgumentList(const TemplateArgument *Args, 2332 unsigned NumArgs, 2333 const PrintingPolicy &Policy); 2334 2335 static std::string PrintTemplateArgumentList(const TemplateArgumentLoc *Args, 2336 unsigned NumArgs, 2337 const PrintingPolicy &Policy); 2338 2339 static std::string PrintTemplateArgumentList(const TemplateArgumentListInfo &, 2340 const PrintingPolicy &Policy); 2341 2342 typedef const TemplateArgument * iterator; 2343 2344 iterator begin() const { return getArgs(); } 2345 iterator end() const; 2346 2347 /// \brief Retrieve the name of the template that we are specializing. 2348 TemplateName getTemplateName() const { return Template; } 2349 2350 /// \brief Retrieve the template arguments. 2351 const TemplateArgument *getArgs() const { 2352 return reinterpret_cast<const TemplateArgument *>(this + 1); 2353 } 2354 2355 /// \brief Retrieve the number of template arguments. 2356 unsigned getNumArgs() const { return NumArgs; } 2357 2358 /// \brief Retrieve a specific template argument as a type. 2359 /// \precondition @c isArgType(Arg) 2360 const TemplateArgument &getArg(unsigned Idx) const; 2361 2362 bool isSugared() const { return !isDependentType(); } 2363 QualType desugar() const { return getCanonicalTypeInternal(); } 2364 2365 void Profile(llvm::FoldingSetNodeID &ID) { 2366 Profile(ID, Template, getArgs(), NumArgs, Context); 2367 } 2368 2369 static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T, 2370 const TemplateArgument *Args, unsigned NumArgs, 2371 ASTContext &Context); 2372 2373 static bool classof(const Type *T) { 2374 return T->getTypeClass() == TemplateSpecialization; 2375 } 2376 static bool classof(const TemplateSpecializationType *T) { return true; } 2377}; 2378 2379/// \brief Represents a type that was referred to via a qualified 2380/// name, e.g., N::M::type. 2381/// 2382/// This type is used to keep track of a type name as written in the 2383/// source code, including any nested-name-specifiers. The type itself 2384/// is always "sugar", used to express what was written in the source 2385/// code but containing no additional semantic information. 2386class QualifiedNameType : public Type, public llvm::FoldingSetNode { 2387 /// \brief The nested name specifier containing the qualifier. 2388 NestedNameSpecifier *NNS; 2389 2390 /// \brief The type that this qualified name refers to. 2391 QualType NamedType; 2392 2393 QualifiedNameType(NestedNameSpecifier *NNS, QualType NamedType, 2394 QualType CanonType) 2395 : Type(QualifiedName, CanonType, NamedType->isDependentType()), 2396 NNS(NNS), NamedType(NamedType) { } 2397 2398 friend class ASTContext; // ASTContext creates these 2399 2400public: 2401 /// \brief Retrieve the qualification on this type. 2402 NestedNameSpecifier *getQualifier() const { return NNS; } 2403 2404 /// \brief Retrieve the type named by the qualified-id. 2405 QualType getNamedType() const { return NamedType; } 2406 2407 /// \brief Remove a single level of sugar. 2408 QualType desugar() const { return getNamedType(); } 2409 2410 /// \brief Returns whether this type directly provides sugar. 2411 bool isSugared() const { return true; } 2412 2413 void Profile(llvm::FoldingSetNodeID &ID) { 2414 Profile(ID, NNS, NamedType); 2415 } 2416 2417 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS, 2418 QualType NamedType) { 2419 ID.AddPointer(NNS); 2420 NamedType.Profile(ID); 2421 } 2422 2423 static bool classof(const Type *T) { 2424 return T->getTypeClass() == QualifiedName; 2425 } 2426 static bool classof(const QualifiedNameType *T) { return true; } 2427}; 2428 2429/// \brief Represents a 'typename' specifier that names a type within 2430/// a dependent type, e.g., "typename T::type". 2431/// 2432/// TypenameType has a very similar structure to QualifiedNameType, 2433/// which also involves a nested-name-specifier following by a type, 2434/// and (FIXME!) both can even be prefixed by the 'typename' 2435/// keyword. However, the two types serve very different roles: 2436/// QualifiedNameType is a non-semantic type that serves only as sugar 2437/// to show how a particular type was written in the source 2438/// code. TypenameType, on the other hand, only occurs when the 2439/// nested-name-specifier is dependent, such that we cannot resolve 2440/// the actual type until after instantiation. 2441class TypenameType : public Type, public llvm::FoldingSetNode { 2442 /// \brief The nested name specifier containing the qualifier. 2443 NestedNameSpecifier *NNS; 2444 2445 typedef llvm::PointerUnion<const IdentifierInfo *, 2446 const TemplateSpecializationType *> NameType; 2447 2448 /// \brief The type that this typename specifier refers to. 2449 NameType Name; 2450 2451 TypenameType(NestedNameSpecifier *NNS, const IdentifierInfo *Name, 2452 QualType CanonType) 2453 : Type(Typename, CanonType, true), NNS(NNS), Name(Name) { 2454 assert(NNS->isDependent() && 2455 "TypenameType requires a dependent nested-name-specifier"); 2456 } 2457 2458 TypenameType(NestedNameSpecifier *NNS, const TemplateSpecializationType *Ty, 2459 QualType CanonType) 2460 : Type(Typename, CanonType, true), NNS(NNS), Name(Ty) { 2461 assert(NNS->isDependent() && 2462 "TypenameType requires a dependent nested-name-specifier"); 2463 } 2464 2465 friend class ASTContext; // ASTContext creates these 2466 2467public: 2468 /// \brief Retrieve the qualification on this type. 2469 NestedNameSpecifier *getQualifier() const { return NNS; } 2470 2471 /// \brief Retrieve the type named by the typename specifier as an 2472 /// identifier. 2473 /// 2474 /// This routine will return a non-NULL identifier pointer when the 2475 /// form of the original typename was terminated by an identifier, 2476 /// e.g., "typename T::type". 2477 const IdentifierInfo *getIdentifier() const { 2478 return Name.dyn_cast<const IdentifierInfo *>(); 2479 } 2480 2481 /// \brief Retrieve the type named by the typename specifier as a 2482 /// type specialization. 2483 const TemplateSpecializationType *getTemplateId() const { 2484 return Name.dyn_cast<const TemplateSpecializationType *>(); 2485 } 2486 2487 bool isSugared() const { return false; } 2488 QualType desugar() const { return QualType(this, 0); } 2489 2490 void Profile(llvm::FoldingSetNodeID &ID) { 2491 Profile(ID, NNS, Name); 2492 } 2493 2494 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS, 2495 NameType Name) { 2496 ID.AddPointer(NNS); 2497 ID.AddPointer(Name.getOpaqueValue()); 2498 } 2499 2500 static bool classof(const Type *T) { 2501 return T->getTypeClass() == Typename; 2502 } 2503 static bool classof(const TypenameType *T) { return true; } 2504}; 2505 2506/// ObjCInterfaceType - Interfaces are the core concept in Objective-C for 2507/// object oriented design. They basically correspond to C++ classes. There 2508/// are two kinds of interface types, normal interfaces like "NSString" and 2509/// qualified interfaces, which are qualified with a protocol list like 2510/// "NSString<NSCopyable, NSAmazing>". 2511class ObjCInterfaceType : public Type, public llvm::FoldingSetNode { 2512 ObjCInterfaceDecl *Decl; 2513 2514 // List of protocols for this protocol conforming object type 2515 // List is sorted on protocol name. No protocol is enterred more than once. 2516 llvm::SmallVector<ObjCProtocolDecl*, 4> Protocols; 2517 2518 ObjCInterfaceType(QualType Canonical, ObjCInterfaceDecl *D, 2519 ObjCProtocolDecl **Protos, unsigned NumP) : 2520 Type(ObjCInterface, Canonical, /*Dependent=*/false), 2521 Decl(D), Protocols(Protos, Protos+NumP) { } 2522 friend class ASTContext; // ASTContext creates these. 2523public: 2524 ObjCInterfaceDecl *getDecl() const { return Decl; } 2525 2526 /// getNumProtocols - Return the number of qualifying protocols in this 2527 /// interface type, or 0 if there are none. 2528 unsigned getNumProtocols() const { return Protocols.size(); } 2529 2530 /// qual_iterator and friends: this provides access to the (potentially empty) 2531 /// list of protocols qualifying this interface. 2532 typedef llvm::SmallVector<ObjCProtocolDecl*, 8>::const_iterator qual_iterator; 2533 qual_iterator qual_begin() const { return Protocols.begin(); } 2534 qual_iterator qual_end() const { return Protocols.end(); } 2535 bool qual_empty() const { return Protocols.size() == 0; } 2536 2537 bool isSugared() const { return false; } 2538 QualType desugar() const { return QualType(this, 0); } 2539 2540 void Profile(llvm::FoldingSetNodeID &ID); 2541 static void Profile(llvm::FoldingSetNodeID &ID, 2542 const ObjCInterfaceDecl *Decl, 2543 ObjCProtocolDecl **protocols, unsigned NumProtocols); 2544 2545 static bool classof(const Type *T) { 2546 return T->getTypeClass() == ObjCInterface; 2547 } 2548 static bool classof(const ObjCInterfaceType *) { return true; } 2549}; 2550 2551/// ObjCObjectPointerType - Used to represent 'id', 'Interface *', 'id <p>', 2552/// and 'Interface <p> *'. 2553/// 2554/// Duplicate protocols are removed and protocol list is canonicalized to be in 2555/// alphabetical order. 2556class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode { 2557 QualType PointeeType; // A builtin or interface type. 2558 2559 // List of protocols for this protocol conforming object type 2560 // List is sorted on protocol name. No protocol is entered more than once. 2561 llvm::SmallVector<ObjCProtocolDecl*, 8> Protocols; 2562 2563 ObjCObjectPointerType(QualType Canonical, QualType T, 2564 ObjCProtocolDecl **Protos, unsigned NumP) : 2565 Type(ObjCObjectPointer, Canonical, /*Dependent=*/false), 2566 PointeeType(T), Protocols(Protos, Protos+NumP) { } 2567 friend class ASTContext; // ASTContext creates these. 2568 2569public: 2570 // Get the pointee type. Pointee will either be: 2571 // - a built-in type (for 'id' and 'Class'). 2572 // - an interface type (for user-defined types). 2573 // - a TypedefType whose canonical type is an interface (as in 'T' below). 2574 // For example: typedef NSObject T; T *var; 2575 QualType getPointeeType() const { return PointeeType; } 2576 2577 const ObjCInterfaceType *getInterfaceType() const { 2578 return PointeeType->getAs<ObjCInterfaceType>(); 2579 } 2580 /// getInterfaceDecl - returns an interface decl for user-defined types. 2581 ObjCInterfaceDecl *getInterfaceDecl() const { 2582 return getInterfaceType() ? getInterfaceType()->getDecl() : 0; 2583 } 2584 /// isObjCIdType - true for "id". 2585 bool isObjCIdType() const { 2586 return getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCId) && 2587 !Protocols.size(); 2588 } 2589 /// isObjCClassType - true for "Class". 2590 bool isObjCClassType() const { 2591 return getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCClass) && 2592 !Protocols.size(); 2593 } 2594 2595 /// isObjCQualifiedIdType - true for "id <p>". 2596 bool isObjCQualifiedIdType() const { 2597 return getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCId) && 2598 Protocols.size(); 2599 } 2600 /// isObjCQualifiedClassType - true for "Class <p>". 2601 bool isObjCQualifiedClassType() const { 2602 return getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCClass) && 2603 Protocols.size(); 2604 } 2605 /// qual_iterator and friends: this provides access to the (potentially empty) 2606 /// list of protocols qualifying this interface. 2607 typedef llvm::SmallVector<ObjCProtocolDecl*, 8>::const_iterator qual_iterator; 2608 2609 qual_iterator qual_begin() const { return Protocols.begin(); } 2610 qual_iterator qual_end() const { return Protocols.end(); } 2611 bool qual_empty() const { return Protocols.size() == 0; } 2612 2613 /// getNumProtocols - Return the number of qualifying protocols in this 2614 /// interface type, or 0 if there are none. 2615 unsigned getNumProtocols() const { return Protocols.size(); } 2616 2617 bool isSugared() const { return false; } 2618 QualType desugar() const { return QualType(this, 0); } 2619 2620 void Profile(llvm::FoldingSetNodeID &ID); 2621 static void Profile(llvm::FoldingSetNodeID &ID, QualType T, 2622 ObjCProtocolDecl **protocols, unsigned NumProtocols); 2623 static bool classof(const Type *T) { 2624 return T->getTypeClass() == ObjCObjectPointer; 2625 } 2626 static bool classof(const ObjCObjectPointerType *) { return true; } 2627}; 2628 2629/// A qualifier set is used to build a set of qualifiers. 2630class QualifierCollector : public Qualifiers { 2631 ASTContext *Context; 2632 2633public: 2634 QualifierCollector(Qualifiers Qs = Qualifiers()) 2635 : Qualifiers(Qs), Context(0) {} 2636 QualifierCollector(ASTContext &Context, Qualifiers Qs = Qualifiers()) 2637 : Qualifiers(Qs), Context(&Context) {} 2638 2639 void setContext(ASTContext &C) { Context = &C; } 2640 2641 /// Collect any qualifiers on the given type and return an 2642 /// unqualified type. 2643 const Type *strip(QualType QT) { 2644 addFastQualifiers(QT.getLocalFastQualifiers()); 2645 if (QT.hasLocalNonFastQualifiers()) { 2646 const ExtQuals *EQ = QT.getExtQualsUnsafe(); 2647 Context = &EQ->getContext(); 2648 addQualifiers(EQ->getQualifiers()); 2649 return EQ->getBaseType(); 2650 } 2651 return QT.getTypePtrUnsafe(); 2652 } 2653 2654 /// Apply the collected qualifiers to the given type. 2655 QualType apply(QualType QT) const; 2656 2657 /// Apply the collected qualifiers to the given type. 2658 QualType apply(const Type* T) const; 2659 2660}; 2661 2662 2663// Inline function definitions. 2664 2665inline bool QualType::isCanonical() const { 2666 const Type *T = getTypePtr(); 2667 if (hasLocalQualifiers()) 2668 return T->isCanonicalUnqualified() && !isa<ArrayType>(T); 2669 return T->isCanonicalUnqualified(); 2670} 2671 2672inline bool QualType::isCanonicalAsParam() const { 2673 if (hasLocalQualifiers()) return false; 2674 const Type *T = getTypePtr(); 2675 return T->isCanonicalUnqualified() && 2676 !isa<FunctionType>(T) && !isa<ArrayType>(T); 2677} 2678 2679inline bool QualType::isConstQualified() const { 2680 return isLocalConstQualified() || 2681 getTypePtr()->getCanonicalTypeInternal().isLocalConstQualified(); 2682} 2683 2684inline bool QualType::isRestrictQualified() const { 2685 return isLocalRestrictQualified() || 2686 getTypePtr()->getCanonicalTypeInternal().isLocalRestrictQualified(); 2687} 2688 2689 2690inline bool QualType::isVolatileQualified() const { 2691 return isLocalVolatileQualified() || 2692 getTypePtr()->getCanonicalTypeInternal().isLocalVolatileQualified(); 2693} 2694 2695inline bool QualType::hasQualifiers() const { 2696 return hasLocalQualifiers() || 2697 getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers(); 2698} 2699 2700inline Qualifiers QualType::getQualifiers() const { 2701 Qualifiers Quals = getLocalQualifiers(); 2702 Quals.addQualifiers( 2703 getTypePtr()->getCanonicalTypeInternal().getLocalQualifiers()); 2704 return Quals; 2705} 2706 2707inline unsigned QualType::getCVRQualifiers() const { 2708 return getLocalCVRQualifiers() | 2709 getTypePtr()->getCanonicalTypeInternal().getLocalCVRQualifiers(); 2710} 2711 2712inline void QualType::removeConst() { 2713 removeFastQualifiers(Qualifiers::Const); 2714} 2715 2716inline void QualType::removeRestrict() { 2717 removeFastQualifiers(Qualifiers::Restrict); 2718} 2719 2720inline void QualType::removeVolatile() { 2721 QualifierCollector Qc; 2722 const Type *Ty = Qc.strip(*this); 2723 if (Qc.hasVolatile()) { 2724 Qc.removeVolatile(); 2725 *this = Qc.apply(Ty); 2726 } 2727} 2728 2729inline void QualType::removeCVRQualifiers(unsigned Mask) { 2730 assert(!(Mask & ~Qualifiers::CVRMask) && "mask has non-CVR bits"); 2731 2732 // Fast path: we don't need to touch the slow qualifiers. 2733 if (!(Mask & ~Qualifiers::FastMask)) { 2734 removeFastQualifiers(Mask); 2735 return; 2736 } 2737 2738 QualifierCollector Qc; 2739 const Type *Ty = Qc.strip(*this); 2740 Qc.removeCVRQualifiers(Mask); 2741 *this = Qc.apply(Ty); 2742} 2743 2744/// getAddressSpace - Return the address space of this type. 2745inline unsigned QualType::getAddressSpace() const { 2746 if (hasLocalNonFastQualifiers()) { 2747 const ExtQuals *EQ = getExtQualsUnsafe(); 2748 if (EQ->hasAddressSpace()) 2749 return EQ->getAddressSpace(); 2750 } 2751 2752 QualType CT = getTypePtr()->getCanonicalTypeInternal(); 2753 if (CT.hasLocalNonFastQualifiers()) { 2754 const ExtQuals *EQ = CT.getExtQualsUnsafe(); 2755 if (EQ->hasAddressSpace()) 2756 return EQ->getAddressSpace(); 2757 } 2758 2759 if (const ArrayType *AT = dyn_cast<ArrayType>(CT)) 2760 return AT->getElementType().getAddressSpace(); 2761 if (const RecordType *RT = dyn_cast<RecordType>(CT)) 2762 return RT->getAddressSpace(); 2763 return 0; 2764} 2765 2766/// getObjCGCAttr - Return the gc attribute of this type. 2767inline Qualifiers::GC QualType::getObjCGCAttr() const { 2768 if (hasLocalNonFastQualifiers()) { 2769 const ExtQuals *EQ = getExtQualsUnsafe(); 2770 if (EQ->hasObjCGCAttr()) 2771 return EQ->getObjCGCAttr(); 2772 } 2773 2774 QualType CT = getTypePtr()->getCanonicalTypeInternal(); 2775 if (CT.hasLocalNonFastQualifiers()) { 2776 const ExtQuals *EQ = CT.getExtQualsUnsafe(); 2777 if (EQ->hasObjCGCAttr()) 2778 return EQ->getObjCGCAttr(); 2779 } 2780 2781 if (const ArrayType *AT = dyn_cast<ArrayType>(CT)) 2782 return AT->getElementType().getObjCGCAttr(); 2783 if (const ObjCObjectPointerType *PT = CT->getAs<ObjCObjectPointerType>()) 2784 return PT->getPointeeType().getObjCGCAttr(); 2785 // We most look at all pointer types, not just pointer to interface types. 2786 if (const PointerType *PT = CT->getAs<PointerType>()) 2787 return PT->getPointeeType().getObjCGCAttr(); 2788 return Qualifiers::GCNone; 2789} 2790 2791 /// getNoReturnAttr - Returns true if the type has the noreturn attribute, 2792 /// false otherwise. 2793inline bool QualType::getNoReturnAttr() const { 2794 QualType CT = getTypePtr()->getCanonicalTypeInternal(); 2795 if (const PointerType *PT = getTypePtr()->getAs<PointerType>()) { 2796 if (const FunctionType *FT = PT->getPointeeType()->getAs<FunctionType>()) 2797 return FT->getNoReturnAttr(); 2798 } else if (const FunctionType *FT = getTypePtr()->getAs<FunctionType>()) 2799 return FT->getNoReturnAttr(); 2800 2801 return false; 2802} 2803 2804/// isMoreQualifiedThan - Determine whether this type is more 2805/// qualified than the Other type. For example, "const volatile int" 2806/// is more qualified than "const int", "volatile int", and 2807/// "int". However, it is not more qualified than "const volatile 2808/// int". 2809inline bool QualType::isMoreQualifiedThan(QualType Other) const { 2810 // FIXME: work on arbitrary qualifiers 2811 unsigned MyQuals = this->getCVRQualifiers(); 2812 unsigned OtherQuals = Other.getCVRQualifiers(); 2813 if (getAddressSpace() != Other.getAddressSpace()) 2814 return false; 2815 return MyQuals != OtherQuals && (MyQuals | OtherQuals) == MyQuals; 2816} 2817 2818/// isAtLeastAsQualifiedAs - Determine whether this type is at last 2819/// as qualified as the Other type. For example, "const volatile 2820/// int" is at least as qualified as "const int", "volatile int", 2821/// "int", and "const volatile int". 2822inline bool QualType::isAtLeastAsQualifiedAs(QualType Other) const { 2823 // FIXME: work on arbitrary qualifiers 2824 unsigned MyQuals = this->getCVRQualifiers(); 2825 unsigned OtherQuals = Other.getCVRQualifiers(); 2826 if (getAddressSpace() != Other.getAddressSpace()) 2827 return false; 2828 return (MyQuals | OtherQuals) == MyQuals; 2829} 2830 2831/// getNonReferenceType - If Type is a reference type (e.g., const 2832/// int&), returns the type that the reference refers to ("const 2833/// int"). Otherwise, returns the type itself. This routine is used 2834/// throughout Sema to implement C++ 5p6: 2835/// 2836/// If an expression initially has the type "reference to T" (8.3.2, 2837/// 8.5.3), the type is adjusted to "T" prior to any further 2838/// analysis, the expression designates the object or function 2839/// denoted by the reference, and the expression is an lvalue. 2840inline QualType QualType::getNonReferenceType() const { 2841 if (const ReferenceType *RefType = (*this)->getAs<ReferenceType>()) 2842 return RefType->getPointeeType(); 2843 else 2844 return *this; 2845} 2846 2847inline const ObjCInterfaceType *Type::getAsPointerToObjCInterfaceType() const { 2848 if (const PointerType *PT = getAs<PointerType>()) 2849 return PT->getPointeeType()->getAs<ObjCInterfaceType>(); 2850 return 0; 2851} 2852 2853inline bool Type::isFunctionType() const { 2854 return isa<FunctionType>(CanonicalType); 2855} 2856inline bool Type::isPointerType() const { 2857 return isa<PointerType>(CanonicalType); 2858} 2859inline bool Type::isAnyPointerType() const { 2860 return isPointerType() || isObjCObjectPointerType(); 2861} 2862inline bool Type::isBlockPointerType() const { 2863 return isa<BlockPointerType>(CanonicalType); 2864} 2865inline bool Type::isReferenceType() const { 2866 return isa<ReferenceType>(CanonicalType); 2867} 2868inline bool Type::isLValueReferenceType() const { 2869 return isa<LValueReferenceType>(CanonicalType); 2870} 2871inline bool Type::isRValueReferenceType() const { 2872 return isa<RValueReferenceType>(CanonicalType); 2873} 2874inline bool Type::isFunctionPointerType() const { 2875 if (const PointerType* T = getAs<PointerType>()) 2876 return T->getPointeeType()->isFunctionType(); 2877 else 2878 return false; 2879} 2880inline bool Type::isMemberPointerType() const { 2881 return isa<MemberPointerType>(CanonicalType); 2882} 2883inline bool Type::isMemberFunctionPointerType() const { 2884 if (const MemberPointerType* T = getAs<MemberPointerType>()) 2885 return T->getPointeeType()->isFunctionType(); 2886 else 2887 return false; 2888} 2889inline bool Type::isArrayType() const { 2890 return isa<ArrayType>(CanonicalType); 2891} 2892inline bool Type::isConstantArrayType() const { 2893 return isa<ConstantArrayType>(CanonicalType); 2894} 2895inline bool Type::isIncompleteArrayType() const { 2896 return isa<IncompleteArrayType>(CanonicalType); 2897} 2898inline bool Type::isVariableArrayType() const { 2899 return isa<VariableArrayType>(CanonicalType); 2900} 2901inline bool Type::isDependentSizedArrayType() const { 2902 return isa<DependentSizedArrayType>(CanonicalType); 2903} 2904inline bool Type::isRecordType() const { 2905 return isa<RecordType>(CanonicalType); 2906} 2907inline bool Type::isAnyComplexType() const { 2908 return isa<ComplexType>(CanonicalType); 2909} 2910inline bool Type::isVectorType() const { 2911 return isa<VectorType>(CanonicalType); 2912} 2913inline bool Type::isExtVectorType() const { 2914 return isa<ExtVectorType>(CanonicalType); 2915} 2916inline bool Type::isObjCObjectPointerType() const { 2917 return isa<ObjCObjectPointerType>(CanonicalType); 2918} 2919inline bool Type::isObjCInterfaceType() const { 2920 return isa<ObjCInterfaceType>(CanonicalType); 2921} 2922inline bool Type::isObjCQualifiedIdType() const { 2923 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 2924 return OPT->isObjCQualifiedIdType(); 2925 return false; 2926} 2927inline bool Type::isObjCQualifiedClassType() const { 2928 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 2929 return OPT->isObjCQualifiedClassType(); 2930 return false; 2931} 2932inline bool Type::isObjCIdType() const { 2933 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 2934 return OPT->isObjCIdType(); 2935 return false; 2936} 2937inline bool Type::isObjCClassType() const { 2938 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 2939 return OPT->isObjCClassType(); 2940 return false; 2941} 2942inline bool Type::isObjCSelType() const { 2943 if (const PointerType *OPT = getAs<PointerType>()) 2944 return OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCSel); 2945 return false; 2946} 2947inline bool Type::isObjCBuiltinType() const { 2948 return isObjCIdType() || isObjCClassType() || isObjCSelType(); 2949} 2950inline bool Type::isTemplateTypeParmType() const { 2951 return isa<TemplateTypeParmType>(CanonicalType); 2952} 2953 2954inline bool Type::isSpecificBuiltinType(unsigned K) const { 2955 if (const BuiltinType *BT = getAs<BuiltinType>()) 2956 if (BT->getKind() == (BuiltinType::Kind) K) 2957 return true; 2958 return false; 2959} 2960 2961/// \brief Determines whether this is a type for which one can define 2962/// an overloaded operator. 2963inline bool Type::isOverloadableType() const { 2964 return isDependentType() || isRecordType() || isEnumeralType(); 2965} 2966 2967inline bool Type::hasPointerRepresentation() const { 2968 return (isPointerType() || isReferenceType() || isBlockPointerType() || 2969 isObjCInterfaceType() || isObjCObjectPointerType() || 2970 isObjCQualifiedInterfaceType() || isNullPtrType()); 2971} 2972 2973inline bool Type::hasObjCPointerRepresentation() const { 2974 return (isObjCInterfaceType() || isObjCObjectPointerType() || 2975 isObjCQualifiedInterfaceType()); 2976} 2977 2978/// Insertion operator for diagnostics. This allows sending QualType's into a 2979/// diagnostic with <<. 2980inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 2981 QualType T) { 2982 DB.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), 2983 Diagnostic::ak_qualtype); 2984 return DB; 2985} 2986 2987// Helper class template that is used by Type::getAs to ensure that one does 2988// not try to look through a qualified type to get to an array type. 2989template<typename T, 2990 bool isArrayType = (llvm::is_same<T, ArrayType>::value || 2991 llvm::is_base_of<ArrayType, T>::value)> 2992struct ArrayType_cannot_be_used_with_getAs { }; 2993 2994template<typename T> 2995struct ArrayType_cannot_be_used_with_getAs<T, true>; 2996 2997/// Member-template getAs<specific type>'. 2998template <typename T> const T *Type::getAs() const { 2999 ArrayType_cannot_be_used_with_getAs<T> at; 3000 (void)at; 3001 3002 // If this is directly a T type, return it. 3003 if (const T *Ty = dyn_cast<T>(this)) 3004 return Ty; 3005 3006 // If the canonical form of this type isn't the right kind, reject it. 3007 if (!isa<T>(CanonicalType)) 3008 return 0; 3009 3010 // If this is a typedef for the type, strip the typedef off without 3011 // losing all typedef information. 3012 return cast<T>(getUnqualifiedDesugaredType()); 3013} 3014 3015} // end namespace clang 3016 3017#endif 3018