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