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