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