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