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