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