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