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