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