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