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