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