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