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