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