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