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