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