Type.h revision 4459bbe1ad110c100d74af8c74fdc82d87317dfe
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 "llvm/Support/Casting.h" 18#include "llvm/ADT/FoldingSet.h" 19#include "llvm/ADT/APSInt.h" 20#include "llvm/Bitcode/SerializationFwd.h" 21 22using llvm::isa; 23using llvm::cast; 24using llvm::cast_or_null; 25using llvm::dyn_cast; 26using llvm::dyn_cast_or_null; 27 28namespace clang { 29 class ASTContext; 30 class Type; 31 class TypedefDecl; 32 class TagDecl; 33 class RecordDecl; 34 class CXXRecordDecl; 35 class EnumDecl; 36 class FieldDecl; 37 class ObjCInterfaceDecl; 38 class ObjCProtocolDecl; 39 class ObjCMethodDecl; 40 class Expr; 41 class Stmt; 42 class SourceLocation; 43 class PointerLikeType; 44 class PointerType; 45 class BlockPointerType; 46 class ReferenceType; 47 class VectorType; 48 class ArrayType; 49 class ConstantArrayType; 50 class VariableArrayType; 51 class IncompleteArrayType; 52 class RecordType; 53 class EnumType; 54 class ComplexType; 55 class TagType; 56 class TypedefType; 57 class FunctionType; 58 class FunctionTypeProto; 59 class ExtVectorType; 60 class BuiltinType; 61 class ObjCInterfaceType; 62 class ObjCQualifiedIdType; 63 class ObjCQualifiedInterfaceType; 64 class StmtIteratorBase; 65 66/// QualType - For efficiency, we don't store CVR-qualified types as nodes on 67/// their own: instead each reference to a type stores the qualifiers. This 68/// greatly reduces the number of nodes we need to allocate for types (for 69/// example we only need one for 'int', 'const int', 'volatile int', 70/// 'const volatile int', etc). 71/// 72/// As an added efficiency bonus, instead of making this a pair, we just store 73/// the three bits we care about in the low bits of the pointer. To handle the 74/// packing/unpacking, we make QualType be a simple wrapper class that acts like 75/// a smart pointer. 76class QualType { 77 uintptr_t ThePtr; 78public: 79 enum TQ { // NOTE: These flags must be kept in sync with DeclSpec::TQ. 80 Const = 0x1, 81 Restrict = 0x2, 82 Volatile = 0x4, 83 CVRFlags = Const|Restrict|Volatile 84 }; 85 86 QualType() : ThePtr(0) {} 87 88 QualType(Type *Ptr, unsigned Quals) { 89 assert((Quals & ~CVRFlags) == 0 && "Invalid type qualifiers!"); 90 ThePtr = reinterpret_cast<uintptr_t>(Ptr); 91 assert((ThePtr & CVRFlags) == 0 && "Type pointer not 8-byte aligned?"); 92 ThePtr |= Quals; 93 } 94 95 QualType(const Type *Ptr, unsigned Quals) { 96 assert((Quals & ~CVRFlags) == 0 && "Invalid type qualifiers!"); 97 ThePtr = reinterpret_cast<uintptr_t>(Ptr); 98 assert((ThePtr & CVRFlags) == 0 && "Type pointer not 8-byte aligned?"); 99 ThePtr |= Quals; 100 } 101 102 static QualType getFromOpaquePtr(void *Ptr) { 103 QualType T; 104 T.ThePtr = reinterpret_cast<uintptr_t>(Ptr); 105 return T; 106 } 107 108 unsigned getCVRQualifiers() const { 109 return ThePtr & CVRFlags; 110 } 111 Type *getTypePtr() const { 112 return reinterpret_cast<Type*>(ThePtr & ~CVRFlags); 113 } 114 115 void *getAsOpaquePtr() const { 116 return reinterpret_cast<void*>(ThePtr); 117 } 118 119 Type &operator*() const { 120 return *getTypePtr(); 121 } 122 123 Type *operator->() const { 124 return getTypePtr(); 125 } 126 127 /// isNull - Return true if this QualType doesn't point to a type yet. 128 bool isNull() const { 129 return ThePtr == 0; 130 } 131 132 bool isConstQualified() const { 133 return (ThePtr & Const) ? true : false; 134 } 135 bool isVolatileQualified() const { 136 return (ThePtr & Volatile) ? true : false; 137 } 138 bool isRestrictQualified() const { 139 return (ThePtr & Restrict) ? true : false; 140 } 141 142 bool isConstant(ASTContext& Ctx) const; 143 144 /// addConst/addVolatile/addRestrict - add the specified type qual to this 145 /// QualType. 146 void addConst() { ThePtr |= Const; } 147 void addVolatile() { ThePtr |= Volatile; } 148 void addRestrict() { ThePtr |= Restrict; } 149 150 void removeConst() { ThePtr &= ~Const; } 151 void removeVolatile() { ThePtr &= ~Volatile; } 152 void removeRestrict() { ThePtr &= ~Restrict; } 153 154 QualType getQualifiedType(unsigned TQs) const { 155 return QualType(getTypePtr(), TQs); 156 } 157 QualType getWithAdditionalQualifiers(unsigned TQs) const { 158 return QualType(getTypePtr(), TQs|getCVRQualifiers()); 159 } 160 161 QualType getUnqualifiedType() const; 162 bool isMoreQualifiedThan(QualType Other) const; 163 bool isAtLeastAsQualifiedAs(QualType Other) const; 164 QualType getNonReferenceType() const; 165 166 167 /// operator==/!= - Indicate whether the specified types and qualifiers are 168 /// identical. 169 bool operator==(const QualType &RHS) const { 170 return ThePtr == RHS.ThePtr; 171 } 172 bool operator!=(const QualType &RHS) const { 173 return ThePtr != RHS.ThePtr; 174 } 175 std::string getAsString() const { 176 std::string S; 177 getAsStringInternal(S); 178 return S; 179 } 180 void getAsStringInternal(std::string &Str) const; 181 182 void dump(const char *s) const; 183 void dump() const; 184 185 void Profile(llvm::FoldingSetNodeID &ID) const { 186 ID.AddPointer(getAsOpaquePtr()); 187 } 188 189public: 190 191 /// getAddressSpace - Return the address space of this type. 192 inline unsigned getAddressSpace() const; 193 194 /// Emit - Serialize a QualType to Bitcode. 195 void Emit(llvm::Serializer& S) const; 196 197 /// Read - Deserialize a QualType from Bitcode. 198 static QualType ReadVal(llvm::Deserializer& D); 199 200 void ReadBackpatch(llvm::Deserializer& D); 201}; 202 203} // end clang. 204 205namespace llvm { 206/// Implement simplify_type for QualType, so that we can dyn_cast from QualType 207/// to a specific Type class. 208template<> struct simplify_type<const ::clang::QualType> { 209 typedef ::clang::Type* SimpleType; 210 static SimpleType getSimplifiedValue(const ::clang::QualType &Val) { 211 return Val.getTypePtr(); 212 } 213}; 214template<> struct simplify_type< ::clang::QualType> 215 : public simplify_type<const ::clang::QualType> {}; 216 217} // end namespace llvm 218 219namespace clang { 220 221/// Type - This is the base class of the type hierarchy. A central concept 222/// with types is that each type always has a canonical type. A canonical type 223/// is the type with any typedef names stripped out of it or the types it 224/// references. For example, consider: 225/// 226/// typedef int foo; 227/// typedef foo* bar; 228/// 'int *' 'foo *' 'bar' 229/// 230/// There will be a Type object created for 'int'. Since int is canonical, its 231/// canonicaltype pointer points to itself. There is also a Type for 'foo' (a 232/// TypeNameType). Its CanonicalType pointer points to the 'int' Type. Next 233/// there is a PointerType that represents 'int*', which, like 'int', is 234/// canonical. Finally, there is a PointerType type for 'foo*' whose canonical 235/// type is 'int*', and there is a TypeNameType for 'bar', whose canonical type 236/// is also 'int*'. 237/// 238/// Non-canonical types are useful for emitting diagnostics, without losing 239/// information about typedefs being used. Canonical types are useful for type 240/// comparisons (they allow by-pointer equality tests) and useful for reasoning 241/// about whether something has a particular form (e.g. is a function type), 242/// because they implicitly, recursively, strip all typedefs out of a type. 243/// 244/// Types, once created, are immutable. 245/// 246class Type { 247public: 248 enum TypeClass { 249 Builtin, Complex, Pointer, Reference, 250 ConstantArray, VariableArray, IncompleteArray, 251 Vector, ExtVector, 252 FunctionNoProto, FunctionProto, 253 TypeName, Tagged, ASQual, 254 ObjCInterface, ObjCQualifiedInterface, 255 ObjCQualifiedId, 256 TypeOfExp, TypeOfTyp, // GNU typeof extension. 257 BlockPointer // C extension 258 }; 259private: 260 QualType CanonicalType; 261 262 /// TypeClass bitfield - Enum that specifies what subclass this belongs to. 263 /// Note that this should stay at the end of the ivars for Type so that 264 /// subclasses can pack their bitfields into the same word. 265 unsigned TC : 5; 266protected: 267 // silence VC++ warning C4355: 'this' : used in base member initializer list 268 Type *this_() { return this; } 269 Type(TypeClass tc, QualType Canonical) 270 : CanonicalType(Canonical.isNull() ? QualType(this_(), 0) : Canonical), 271 TC(tc) {} 272 virtual ~Type() {}; 273 virtual void Destroy(ASTContext& C); 274 friend class ASTContext; 275 276 void EmitTypeInternal(llvm::Serializer& S) const; 277 void ReadTypeInternal(llvm::Deserializer& D); 278 279public: 280 TypeClass getTypeClass() const { return static_cast<TypeClass>(TC); } 281 282 bool isCanonical() const { return CanonicalType.getTypePtr() == this; } 283 284 /// Types are partitioned into 3 broad categories (C99 6.2.5p1): 285 /// object types, function types, and incomplete types. 286 287 /// isObjectType - types that fully describe objects. An object is a region 288 /// of memory that can be examined and stored into (H&S). 289 bool isObjectType() const; 290 291 /// isIncompleteType - Return true if this is an incomplete type. 292 /// A type that can describe objects, but which lacks information needed to 293 /// determine its size (e.g. void, or a fwd declared struct). Clients of this 294 /// routine will need to determine if the size is actually required. 295 bool isIncompleteType() const; 296 297 /// isIncompleteOrObjectType - Return true if this is an incomplete or object 298 /// type, in other words, not a function type. 299 bool isIncompleteOrObjectType() const { 300 return !isFunctionType(); 301 } 302 303 /// isVariablyModifiedType (C99 6.7.5.2p2) - Return true for variable array 304 /// types that have a non-constant expression. This does not include "[]". 305 bool isVariablyModifiedType() const; 306 307 /// Helper methods to distinguish type categories. All type predicates 308 /// operate on the canonical type, ignoring typedefs and qualifiers. 309 310 /// isIntegerType() does *not* include complex integers (a GCC extension). 311 /// isComplexIntegerType() can be used to test for complex integers. 312 bool isIntegerType() const; // C99 6.2.5p17 (int, char, bool, enum) 313 bool isEnumeralType() const; 314 bool isBooleanType() const; 315 bool isCharType() const; 316 bool isWideCharType() const; 317 bool isIntegralType() const; 318 319 /// Floating point categories. 320 bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double) 321 /// isComplexType() does *not* include complex integers (a GCC extension). 322 /// isComplexIntegerType() can be used to test for complex integers. 323 bool isComplexType() const; // C99 6.2.5p11 (complex) 324 bool isAnyComplexType() const; // C99 6.2.5p11 (complex) + Complex Int. 325 bool isFloatingType() const; // C99 6.2.5p11 (real floating + complex) 326 bool isRealType() const; // C99 6.2.5p17 (real floating + integer) 327 bool isArithmeticType() const; // C99 6.2.5p18 (integer + floating) 328 bool isVoidType() const; // C99 6.2.5p19 329 bool isDerivedType() const; // C99 6.2.5p20 330 bool isScalarType() const; // C99 6.2.5p21 (arithmetic + pointers) 331 bool isAggregateType() const; // C99 6.2.5p21 (arrays, structures) 332 333 // Type Predicates: Check to see if this type is structurally the specified 334 // type, ignoring typedefs and qualifiers. 335 bool isFunctionType() const; 336 bool isPointerLikeType() const; // Pointer or Reference. 337 bool isPointerType() const; 338 bool isBlockPointerType() const; 339 bool isReferenceType() const; 340 bool isFunctionPointerType() const; 341 bool isArrayType() const; 342 bool isConstantArrayType() const; 343 bool isIncompleteArrayType() const; 344 bool isVariableArrayType() const; 345 bool isRecordType() const; 346 bool isClassType() const; 347 bool isStructureType() const; 348 bool isUnionType() const; 349 bool isComplexIntegerType() const; // GCC _Complex integer type. 350 bool isVectorType() const; // GCC vector type. 351 bool isExtVectorType() const; // Extended vector type. 352 bool isObjCInterfaceType() const; // NSString or NSString<foo> 353 bool isObjCQualifiedInterfaceType() const; // NSString<foo> 354 bool isObjCQualifiedIdType() const; // id<foo> 355 356 // Type Checking Functions: Check to see if this type is structurally the 357 // specified type, ignoring typedefs and qualifiers, and return a pointer to 358 // the best type we can. 359 const BuiltinType *getAsBuiltinType() const; 360 const FunctionType *getAsFunctionType() const; 361 const FunctionTypeProto *getAsFunctionTypeProto() const; 362 const PointerLikeType *getAsPointerLikeType() const; // Pointer or Reference. 363 const PointerType *getAsPointerType() const; 364 const BlockPointerType *getAsBlockPointerType() const; 365 const ReferenceType *getAsReferenceType() const; 366 const RecordType *getAsRecordType() const; 367 const RecordType *getAsStructureType() const; 368 /// NOTE: getAsArrayType* are methods on ASTContext. 369 const TypedefType *getAsTypedefType() const; 370 const RecordType *getAsUnionType() const; 371 const EnumType *getAsEnumType() const; 372 const VectorType *getAsVectorType() const; // GCC vector type. 373 const ComplexType *getAsComplexType() const; 374 const ComplexType *getAsComplexIntegerType() const; // GCC complex int type. 375 const ExtVectorType *getAsExtVectorType() const; // Extended vector type. 376 const ObjCInterfaceType *getAsObjCInterfaceType() const; 377 const ObjCQualifiedInterfaceType *getAsObjCQualifiedInterfaceType() const; 378 const ObjCQualifiedIdType *getAsObjCQualifiedIdType() const; 379 380 /// getAsPointerToObjCInterfaceType - If this is a pointer to an ObjC 381 /// interface, return the interface type, otherwise return null. 382 const ObjCInterfaceType *getAsPointerToObjCInterfaceType() const; 383 384 /// getArrayElementTypeNoTypeQual - If this is an array type, return the 385 /// element type of the array, potentially with type qualifiers missing. 386 /// This method should never be used when type qualifiers are meaningful. 387 const Type *getArrayElementTypeNoTypeQual() const; 388 389 390 391 /// getDesugaredType - Return the specified type with any "sugar" removed from 392 /// the type. This takes off typedefs, typeof's etc. If the outer level of 393 /// the type is already concrete, it returns it unmodified. This is similar 394 /// to getting the canonical type, but it doesn't remove *all* typedefs. For 395 /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is 396 /// concrete. 397 QualType getDesugaredType() const; 398 399 /// More type predicates useful for type checking/promotion 400 bool isPromotableIntegerType() const; // C99 6.3.1.1p2 401 402 /// isSignedIntegerType - Return true if this is an integer type that is 403 /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], 404 /// an enum decl which has a signed representation, or a vector of signed 405 /// integer element type. 406 bool isSignedIntegerType() const; 407 408 /// isUnsignedIntegerType - Return true if this is an integer type that is 409 /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum 410 /// decl which has an unsigned representation, or a vector of unsigned integer 411 /// element type. 412 bool isUnsignedIntegerType() const; 413 414 /// isConstantSizeType - Return true if this is not a variable sized type, 415 /// according to the rules of C99 6.7.5p3. It is not legal to call this on 416 /// incomplete types. 417 bool isConstantSizeType() const; 418 419 QualType getCanonicalTypeInternal() const { return CanonicalType; } 420 void dump() const; 421 virtual void getAsStringInternal(std::string &InnerString) const = 0; 422 static bool classof(const Type *) { return true; } 423 424protected: 425 /// Emit - Emit a Type to bitcode. Used by ASTContext. 426 void Emit(llvm::Serializer& S) const; 427 428 /// Create - Construct a Type from bitcode. Used by ASTContext. 429 static void Create(ASTContext& Context, unsigned i, llvm::Deserializer& S); 430 431 /// EmitImpl - Subclasses must implement this method in order to 432 /// be serialized. 433 // FIXME: Make this abstract once implemented. 434 virtual void EmitImpl(llvm::Serializer& S) const { 435 assert (false && "Serializization for type not supported."); 436 } 437}; 438 439/// ASQualType - TR18037 (C embedded extensions) 6.2.5p26 440/// This supports address space qualified types. 441/// 442class ASQualType : public Type, public llvm::FoldingSetNode { 443 /// BaseType - This is the underlying type that this qualifies. All CVR 444 /// qualifiers are stored on the QualType that references this type, so we 445 /// can't have any here. 446 Type *BaseType; 447 /// Address Space ID - The address space ID this type is qualified with. 448 unsigned AddressSpace; 449 ASQualType(Type *Base, QualType CanonicalPtr, unsigned AddrSpace) : 450 Type(ASQual, CanonicalPtr), BaseType(Base), AddressSpace(AddrSpace) { 451 } 452 friend class ASTContext; // ASTContext creates these. 453public: 454 Type *getBaseType() const { return BaseType; } 455 unsigned getAddressSpace() const { return AddressSpace; } 456 457 virtual void getAsStringInternal(std::string &InnerString) const; 458 459 void Profile(llvm::FoldingSetNodeID &ID) { 460 Profile(ID, getBaseType(), AddressSpace); 461 } 462 static void Profile(llvm::FoldingSetNodeID &ID, Type *Base, 463 unsigned AddrSpace) { 464 ID.AddPointer(Base); 465 ID.AddInteger(AddrSpace); 466 } 467 468 static bool classof(const Type *T) { return T->getTypeClass() == ASQual; } 469 static bool classof(const ASQualType *) { return true; } 470 471protected: 472 virtual void EmitImpl(llvm::Serializer& S) const; 473 static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D); 474 friend class Type; 475}; 476 477 478/// BuiltinType - This class is used for builtin types like 'int'. Builtin 479/// types are always canonical and have a literal name field. 480class BuiltinType : public Type { 481public: 482 enum Kind { 483 Void, 484 485 Bool, // This is bool and/or _Bool. 486 Char_U, // This is 'char' for targets where char is unsigned. 487 UChar, // This is explicitly qualified unsigned char. 488 UShort, 489 UInt, 490 ULong, 491 ULongLong, 492 493 Char_S, // This is 'char' for targets where char is signed. 494 SChar, // This is explicitly qualified signed char. 495 WChar, // This is 'wchar_t' for C++. 496 Short, 497 Int, 498 Long, 499 LongLong, 500 501 Float, Double, LongDouble, 502 503 Overload // This represents the type of an overloaded function declaration. 504 }; 505private: 506 Kind TypeKind; 507public: 508 BuiltinType(Kind K) : Type(Builtin, QualType()), TypeKind(K) {} 509 510 Kind getKind() const { return TypeKind; } 511 const char *getName() const; 512 513 virtual void getAsStringInternal(std::string &InnerString) const; 514 515 static bool classof(const Type *T) { return T->getTypeClass() == Builtin; } 516 static bool classof(const BuiltinType *) { return true; } 517}; 518 519/// ComplexType - C99 6.2.5p11 - Complex values. This supports the C99 complex 520/// types (_Complex float etc) as well as the GCC integer complex extensions. 521/// 522class ComplexType : public Type, public llvm::FoldingSetNode { 523 QualType ElementType; 524 ComplexType(QualType Element, QualType CanonicalPtr) : 525 Type(Complex, CanonicalPtr), ElementType(Element) { 526 } 527 friend class ASTContext; // ASTContext creates these. 528public: 529 QualType getElementType() const { return ElementType; } 530 531 virtual void getAsStringInternal(std::string &InnerString) const; 532 533 void Profile(llvm::FoldingSetNodeID &ID) { 534 Profile(ID, getElementType()); 535 } 536 static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) { 537 ID.AddPointer(Element.getAsOpaquePtr()); 538 } 539 540 static bool classof(const Type *T) { return T->getTypeClass() == Complex; } 541 static bool classof(const ComplexType *) { return true; } 542 543protected: 544 virtual void EmitImpl(llvm::Serializer& S) const; 545 static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D); 546 friend class Type; 547}; 548 549/// PointerLikeType - Common base class for pointers and references. 550/// FIXME: Add more documentation on this classes design point. For example, 551/// should BlockPointerType inherit from it? Is the concept of a PointerLikeType 552/// in the C++ standard? 553/// 554class PointerLikeType : public Type { 555 QualType PointeeType; 556protected: 557 PointerLikeType(TypeClass K, QualType Pointee, QualType CanonicalPtr) : 558 Type(K, CanonicalPtr), PointeeType(Pointee) { 559 } 560public: 561 562 QualType getPointeeType() const { return PointeeType; } 563 564 static bool classof(const Type *T) { 565 return T->getTypeClass() == Pointer || T->getTypeClass() == Reference; 566 } 567 static bool classof(const PointerLikeType *) { return true; } 568}; 569 570/// PointerType - C99 6.7.5.1 - Pointer Declarators. 571/// 572class PointerType : public PointerLikeType, public llvm::FoldingSetNode { 573 PointerType(QualType Pointee, QualType CanonicalPtr) : 574 PointerLikeType(Pointer, Pointee, CanonicalPtr) { 575 } 576 friend class ASTContext; // ASTContext creates these. 577public: 578 579 virtual void getAsStringInternal(std::string &InnerString) const; 580 581 void Profile(llvm::FoldingSetNodeID &ID) { 582 Profile(ID, getPointeeType()); 583 } 584 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { 585 ID.AddPointer(Pointee.getAsOpaquePtr()); 586 } 587 588 static bool classof(const Type *T) { return T->getTypeClass() == Pointer; } 589 static bool classof(const PointerType *) { return true; } 590 591protected: 592 virtual void EmitImpl(llvm::Serializer& S) const; 593 static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D); 594 friend class Type; 595}; 596 597/// BlockPointerType - pointer to a block type. 598/// This type is to represent types syntactically represented as 599/// "void (^)(int)", etc. Pointee is required to always be a function type. 600/// FIXME: Should BlockPointerType inherit from PointerLikeType? It could 601/// simplfy some type checking code, however PointerLikeType doesn't appear 602/// to be used by the type checker. 603/// 604class BlockPointerType : public Type, public llvm::FoldingSetNode { 605 QualType PointeeType; // Block is some kind of pointer type 606 BlockPointerType(QualType Pointee, QualType CanonicalCls) : 607 Type(BlockPointer, CanonicalCls), PointeeType(Pointee) { 608 } 609 friend class ASTContext; // ASTContext creates these. 610public: 611 612 // Get the pointee type. Pointee is required to always be a function type. 613 QualType getPointeeType() const { return PointeeType; } 614 615 virtual void getAsStringInternal(std::string &InnerString) const; 616 617 void Profile(llvm::FoldingSetNodeID &ID) { 618 Profile(ID, getPointeeType()); 619 } 620 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { 621 ID.AddPointer(Pointee.getAsOpaquePtr()); 622 } 623 624 static bool classof(const Type *T) { 625 return T->getTypeClass() == BlockPointer; 626 } 627 static bool classof(const BlockPointerType *) { return true; } 628 629 protected: 630 virtual void EmitImpl(llvm::Serializer& S) const; 631 static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D); 632 friend class Type; 633}; 634 635/// ReferenceType - C++ 8.3.2 - Reference Declarators. 636/// 637class ReferenceType : public PointerLikeType, public llvm::FoldingSetNode { 638 ReferenceType(QualType Referencee, QualType CanonicalRef) : 639 PointerLikeType(Reference, Referencee, CanonicalRef) { 640 } 641 friend class ASTContext; // ASTContext creates these. 642public: 643 virtual void getAsStringInternal(std::string &InnerString) const; 644 645 void Profile(llvm::FoldingSetNodeID &ID) { 646 Profile(ID, getPointeeType()); 647 } 648 static void Profile(llvm::FoldingSetNodeID &ID, QualType Referencee) { 649 ID.AddPointer(Referencee.getAsOpaquePtr()); 650 } 651 652 static bool classof(const Type *T) { return T->getTypeClass() == Reference; } 653 static bool classof(const ReferenceType *) { return true; } 654}; 655 656/// ArrayType - C99 6.7.5.2 - Array Declarators. 657/// 658class ArrayType : public Type, public llvm::FoldingSetNode { 659public: 660 /// ArraySizeModifier - Capture whether this is a normal array (e.g. int X[4]) 661 /// an array with a static size (e.g. int X[static 4]), or with a star size 662 /// (e.g. int X[*]). 'static' is only allowed on function parameters. 663 enum ArraySizeModifier { 664 Normal, Static, Star 665 }; 666private: 667 /// ElementType - The element type of the array. 668 QualType ElementType; 669 670 // NOTE: VC++ treats enums as signed, avoid using the ArraySizeModifier enum 671 /// NOTE: These fields are packed into the bitfields space in the Type class. 672 unsigned SizeModifier : 2; 673 674 /// IndexTypeQuals - Capture qualifiers in declarations like: 675 /// 'int X[static restrict 4]'. For function parameters only. 676 unsigned IndexTypeQuals : 3; 677 678protected: 679 ArrayType(TypeClass tc, QualType et, QualType can, 680 ArraySizeModifier sm, unsigned tq) 681 : Type(tc, can), ElementType(et), SizeModifier(sm), IndexTypeQuals(tq) {} 682 friend class ASTContext; // ASTContext creates these. 683public: 684 QualType getElementType() const { return ElementType; } 685 ArraySizeModifier getSizeModifier() const { 686 return ArraySizeModifier(SizeModifier); 687 } 688 unsigned getIndexTypeQualifier() const { return IndexTypeQuals; } 689 690 static bool classof(const Type *T) { 691 return T->getTypeClass() == ConstantArray || 692 T->getTypeClass() == VariableArray || 693 T->getTypeClass() == IncompleteArray; 694 } 695 static bool classof(const ArrayType *) { return true; } 696}; 697 698/// ConstantArrayType - This class represents C arrays with a specified constant 699/// size. For example 'int A[100]' has ConstantArrayType where the element type 700/// is 'int' and the size is 100. 701class ConstantArrayType : public ArrayType { 702 llvm::APInt Size; // Allows us to unique the type. 703 704 ConstantArrayType(QualType et, QualType can, llvm::APInt sz, 705 ArraySizeModifier sm, unsigned tq) 706 : ArrayType(ConstantArray, et, can, sm, tq), Size(sz) {} 707 friend class ASTContext; // ASTContext creates these. 708public: 709 const llvm::APInt &getSize() const { return Size; } 710 virtual void getAsStringInternal(std::string &InnerString) const; 711 712 void Profile(llvm::FoldingSetNodeID &ID) { 713 Profile(ID, getElementType(), getSize()); 714 } 715 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, 716 llvm::APInt ArraySize) { 717 ID.AddPointer(ET.getAsOpaquePtr()); 718 ID.AddInteger(ArraySize.getZExtValue()); 719 } 720 static bool classof(const Type *T) { 721 return T->getTypeClass() == ConstantArray; 722 } 723 static bool classof(const ConstantArrayType *) { return true; } 724 725protected: 726 virtual void EmitImpl(llvm::Serializer& S) const; 727 static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D); 728 friend class Type; 729}; 730 731/// IncompleteArrayType - This class represents C arrays with an unspecified 732/// size. For example 'int A[]' has an IncompleteArrayType where the element 733/// type is 'int' and the size is unspecified. 734class IncompleteArrayType : public ArrayType { 735 IncompleteArrayType(QualType et, QualType can, 736 ArraySizeModifier sm, unsigned tq) 737 : ArrayType(IncompleteArray, et, can, sm, tq) {} 738 friend class ASTContext; // ASTContext creates these. 739public: 740 741 virtual void getAsStringInternal(std::string &InnerString) const; 742 743 static bool classof(const Type *T) { 744 return T->getTypeClass() == IncompleteArray; 745 } 746 static bool classof(const IncompleteArrayType *) { return true; } 747 748 friend class StmtIteratorBase; 749 750 void Profile(llvm::FoldingSetNodeID &ID) { 751 Profile(ID, getElementType()); 752 } 753 754 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET) { 755 ID.AddPointer(ET.getAsOpaquePtr()); 756 } 757 758protected: 759 virtual void EmitImpl(llvm::Serializer& S) const; 760 static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D); 761 friend class Type; 762}; 763 764/// VariableArrayType - This class represents C arrays with a specified size 765/// which is not an integer-constant-expression. For example, 'int s[x+foo()]'. 766/// Since the size expression is an arbitrary expression, we store it as such. 767/// 768/// Note: VariableArrayType's aren't uniqued (since the expressions aren't) and 769/// should not be: two lexically equivalent variable array types could mean 770/// different things, for example, these variables do not have the same type 771/// dynamically: 772/// 773/// void foo(int x) { 774/// int Y[x]; 775/// ++x; 776/// int Z[x]; 777/// } 778/// 779class VariableArrayType : public ArrayType { 780 /// SizeExpr - An assignment expression. VLA's are only permitted within 781 /// a function block. 782 Stmt *SizeExpr; 783 784 VariableArrayType(QualType et, QualType can, Expr *e, 785 ArraySizeModifier sm, unsigned tq) 786 : ArrayType(VariableArray, et, can, sm, tq), SizeExpr((Stmt*) e) {} 787 friend class ASTContext; // ASTContext creates these. 788 virtual void Destroy(ASTContext& C); 789 790public: 791 Expr *getSizeExpr() const { 792 // We use C-style casts instead of cast<> here because we do not wish 793 // to have a dependency of Type.h on Stmt.h/Expr.h. 794 return (Expr*) SizeExpr; 795 } 796 797 virtual void getAsStringInternal(std::string &InnerString) const; 798 799 static bool classof(const Type *T) { 800 return T->getTypeClass() == VariableArray; 801 } 802 static bool classof(const VariableArrayType *) { return true; } 803 804 friend class StmtIteratorBase; 805 806 void Profile(llvm::FoldingSetNodeID &ID) { 807 assert (0 && "Cannnot unique VariableArrayTypes."); 808 } 809 810protected: 811 virtual void EmitImpl(llvm::Serializer& S) const; 812 static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D); 813 friend class Type; 814}; 815 816/// VectorType - GCC generic vector type. This type is created using 817/// __attribute__((vector_size(n)), where "n" specifies the vector size in 818/// bytes. Since the constructor takes the number of vector elements, the 819/// client is responsible for converting the size into the number of elements. 820class VectorType : public Type, public llvm::FoldingSetNode { 821protected: 822 /// ElementType - The element type of the vector. 823 QualType ElementType; 824 825 /// NumElements - The number of elements in the vector. 826 unsigned NumElements; 827 828 VectorType(QualType vecType, unsigned nElements, QualType canonType) : 829 Type(Vector, canonType), ElementType(vecType), NumElements(nElements) {} 830 VectorType(TypeClass tc, QualType vecType, unsigned nElements, 831 QualType canonType) : Type(tc, canonType), ElementType(vecType), 832 NumElements(nElements) {} 833 friend class ASTContext; // ASTContext creates these. 834public: 835 836 QualType getElementType() const { return ElementType; } 837 unsigned getNumElements() const { return NumElements; } 838 839 virtual void getAsStringInternal(std::string &InnerString) const; 840 841 void Profile(llvm::FoldingSetNodeID &ID) { 842 Profile(ID, getElementType(), getNumElements(), getTypeClass()); 843 } 844 static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, 845 unsigned NumElements, TypeClass TypeClass) { 846 ID.AddPointer(ElementType.getAsOpaquePtr()); 847 ID.AddInteger(NumElements); 848 ID.AddInteger(TypeClass); 849 } 850 static bool classof(const Type *T) { 851 return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector; 852 } 853 static bool classof(const VectorType *) { return true; } 854}; 855 856/// ExtVectorType - Extended vector type. This type is created using 857/// __attribute__((ext_vector_type(n)), where "n" is the number of elements. 858/// Unlike vector_size, ext_vector_type is only allowed on typedef's. This 859/// class enables syntactic extensions, like Vector Components for accessing 860/// points, colors, and textures (modeled after OpenGL Shading Language). 861class ExtVectorType : public VectorType { 862 ExtVectorType(QualType vecType, unsigned nElements, QualType canonType) : 863 VectorType(ExtVector, vecType, nElements, canonType) {} 864 friend class ASTContext; // ASTContext creates these. 865public: 866 static int getPointAccessorIdx(char c) { 867 switch (c) { 868 default: return -1; 869 case 'x': return 0; 870 case 'y': return 1; 871 case 'z': return 2; 872 case 'w': return 3; 873 } 874 } 875 static int getColorAccessorIdx(char c) { 876 switch (c) { 877 default: return -1; 878 case 'r': return 0; 879 case 'g': return 1; 880 case 'b': return 2; 881 case 'a': return 3; 882 } 883 } 884 static int getTextureAccessorIdx(char c) { 885 switch (c) { 886 default: return -1; 887 case 's': return 0; 888 case 't': return 1; 889 case 'p': return 2; 890 case 'q': return 3; 891 } 892 }; 893 894 static int getAccessorIdx(char c) { 895 if (int idx = getPointAccessorIdx(c)+1) return idx-1; 896 if (int idx = getColorAccessorIdx(c)+1) return idx-1; 897 return getTextureAccessorIdx(c); 898 } 899 900 bool isAccessorWithinNumElements(char c) const { 901 if (int idx = getAccessorIdx(c)+1) 902 return unsigned(idx-1) < NumElements; 903 return false; 904 } 905 virtual void getAsStringInternal(std::string &InnerString) const; 906 907 static bool classof(const Type *T) { 908 return T->getTypeClass() == ExtVector; 909 } 910 static bool classof(const ExtVectorType *) { return true; } 911}; 912 913/// FunctionType - C99 6.7.5.3 - Function Declarators. This is the common base 914/// class of FunctionTypeNoProto and FunctionTypeProto. 915/// 916class FunctionType : public Type { 917 /// SubClassData - This field is owned by the subclass, put here to pack 918 /// tightly with the ivars in Type. 919 bool SubClassData : 1; 920 921 // The type returned by the function. 922 QualType ResultType; 923protected: 924 FunctionType(TypeClass tc, QualType res, bool SubclassInfo,QualType Canonical) 925 : Type(tc, Canonical), SubClassData(SubclassInfo), ResultType(res) {} 926 bool getSubClassData() const { return SubClassData; } 927public: 928 929 QualType getResultType() const { return ResultType; } 930 931 932 static bool classof(const Type *T) { 933 return T->getTypeClass() == FunctionNoProto || 934 T->getTypeClass() == FunctionProto; 935 } 936 static bool classof(const FunctionType *) { return true; } 937}; 938 939/// FunctionTypeNoProto - Represents a K&R-style 'int foo()' function, which has 940/// no information available about its arguments. 941class FunctionTypeNoProto : public FunctionType, public llvm::FoldingSetNode { 942 FunctionTypeNoProto(QualType Result, QualType Canonical) 943 : FunctionType(FunctionNoProto, Result, false, Canonical) {} 944 friend class ASTContext; // ASTContext creates these. 945public: 946 // No additional state past what FunctionType provides. 947 948 virtual void getAsStringInternal(std::string &InnerString) const; 949 950 void Profile(llvm::FoldingSetNodeID &ID) { 951 Profile(ID, getResultType()); 952 } 953 static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType) { 954 ID.AddPointer(ResultType.getAsOpaquePtr()); 955 } 956 957 static bool classof(const Type *T) { 958 return T->getTypeClass() == FunctionNoProto; 959 } 960 static bool classof(const FunctionTypeNoProto *) { return true; } 961 962protected: 963 virtual void EmitImpl(llvm::Serializer& S) const; 964 static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D); 965 friend class Type; 966}; 967 968/// FunctionTypeProto - Represents a prototype with argument type info, e.g. 969/// 'int foo(int)' or 'int foo(void)'. 'void' is represented as having no 970/// arguments, not as having a single void argument. 971class FunctionTypeProto : public FunctionType, public llvm::FoldingSetNode { 972 FunctionTypeProto(QualType Result, const QualType *ArgArray, unsigned numArgs, 973 bool isVariadic, QualType Canonical) 974 : FunctionType(FunctionProto, Result, isVariadic, Canonical), 975 NumArgs(numArgs) { 976 // Fill in the trailing argument array. 977 QualType *ArgInfo = reinterpret_cast<QualType *>(this+1);; 978 for (unsigned i = 0; i != numArgs; ++i) 979 ArgInfo[i] = ArgArray[i]; 980 } 981 982 /// NumArgs - The number of arguments this function has, not counting '...'. 983 unsigned NumArgs; 984 985 /// ArgInfo - There is an variable size array after the class in memory that 986 /// holds the argument types. 987 988 friend class ASTContext; // ASTContext creates these. 989 virtual void Destroy(ASTContext& C); 990 991public: 992 unsigned getNumArgs() const { return NumArgs; } 993 QualType getArgType(unsigned i) const { 994 assert(i < NumArgs && "Invalid argument number!"); 995 return arg_type_begin()[i]; 996 } 997 998 bool isVariadic() const { return getSubClassData(); } 999 1000 typedef const QualType *arg_type_iterator; 1001 arg_type_iterator arg_type_begin() const { 1002 return reinterpret_cast<const QualType *>(this+1); 1003 } 1004 arg_type_iterator arg_type_end() const { return arg_type_begin()+NumArgs; } 1005 1006 virtual void getAsStringInternal(std::string &InnerString) const; 1007 1008 static bool classof(const Type *T) { 1009 return T->getTypeClass() == FunctionProto; 1010 } 1011 static bool classof(const FunctionTypeProto *) { return true; } 1012 1013 void Profile(llvm::FoldingSetNodeID &ID); 1014 static void Profile(llvm::FoldingSetNodeID &ID, QualType Result, 1015 arg_type_iterator ArgTys, unsigned NumArgs, 1016 bool isVariadic); 1017 1018protected: 1019 virtual void EmitImpl(llvm::Serializer& S) const; 1020 static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D); 1021 friend class Type; 1022}; 1023 1024 1025class TypedefType : public Type { 1026 TypedefDecl *Decl; 1027protected: 1028 TypedefType(TypeClass tc, TypedefDecl *D, QualType can) 1029 : Type(tc, can), Decl(D) { 1030 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 1031 } 1032 friend class ASTContext; // ASTContext creates these. 1033public: 1034 1035 TypedefDecl *getDecl() const { return Decl; } 1036 1037 /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to 1038 /// potentially looking through *all* consequtive typedefs. This returns the 1039 /// sum of the type qualifiers, so if you have: 1040 /// typedef const int A; 1041 /// typedef volatile A B; 1042 /// looking through the typedefs for B will give you "const volatile A". 1043 QualType LookThroughTypedefs() const; 1044 1045 virtual void getAsStringInternal(std::string &InnerString) const; 1046 1047 static bool classof(const Type *T) { return T->getTypeClass() == TypeName; } 1048 static bool classof(const TypedefType *) { return true; } 1049 1050protected: 1051 virtual void EmitImpl(llvm::Serializer& S) const; 1052 static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D); 1053 friend class Type; 1054}; 1055 1056/// TypeOfExpr (GCC extension). 1057class TypeOfExpr : public Type { 1058 Expr *TOExpr; 1059 TypeOfExpr(Expr *E, QualType can) : Type(TypeOfExp, can), TOExpr(E) { 1060 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 1061 } 1062 friend class ASTContext; // ASTContext creates these. 1063public: 1064 Expr *getUnderlyingExpr() const { return TOExpr; } 1065 1066 virtual void getAsStringInternal(std::string &InnerString) const; 1067 1068 static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExp; } 1069 static bool classof(const TypeOfExpr *) { return true; } 1070}; 1071 1072/// TypeOfType (GCC extension). 1073class TypeOfType : public Type { 1074 QualType TOType; 1075 TypeOfType(QualType T, QualType can) : Type(TypeOfTyp, can), TOType(T) { 1076 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 1077 } 1078 friend class ASTContext; // ASTContext creates these. 1079public: 1080 QualType getUnderlyingType() const { return TOType; } 1081 1082 virtual void getAsStringInternal(std::string &InnerString) const; 1083 1084 static bool classof(const Type *T) { return T->getTypeClass() == TypeOfTyp; } 1085 static bool classof(const TypeOfType *) { return true; } 1086}; 1087 1088class TagType : public Type { 1089 TagDecl *decl; 1090 friend class ASTContext; 1091 1092protected: 1093 TagType(TagDecl *D, QualType can) : Type(Tagged, can), decl(D) {} 1094 1095public: 1096 TagDecl *getDecl() const { return decl; } 1097 1098 virtual void getAsStringInternal(std::string &InnerString) const; 1099 1100 static bool classof(const Type *T) { return T->getTypeClass() == Tagged; } 1101 static bool classof(const TagType *) { return true; } 1102 1103protected: 1104 virtual void EmitImpl(llvm::Serializer& S) const; 1105 static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D); 1106 friend class Type; 1107}; 1108 1109/// RecordType - This is a helper class that allows the use of isa/cast/dyncast 1110/// to detect TagType objects of structs/unions/classes. 1111class RecordType : public TagType { 1112protected: 1113 explicit RecordType(RecordDecl *D) 1114 : TagType(reinterpret_cast<TagDecl*>(D), QualType()) { } 1115 friend class ASTContext; // ASTContext creates these. 1116public: 1117 1118 RecordDecl *getDecl() const { 1119 return reinterpret_cast<RecordDecl*>(TagType::getDecl()); 1120 } 1121 1122 // FIXME: This predicate is a helper to QualType/Type. It needs to 1123 // recursively check all fields for const-ness. If any field is declared 1124 // const, it needs to return false. 1125 bool hasConstFields() const { return false; } 1126 1127 // FIXME: RecordType needs to check when it is created that all fields are in 1128 // the same address space, and return that. 1129 unsigned getAddressSpace() const { return 0; } 1130 1131 static bool classof(const TagType *T); 1132 static bool classof(const Type *T) { 1133 return isa<TagType>(T) && classof(cast<TagType>(T)); 1134 } 1135 static bool classof(const RecordType *) { return true; } 1136}; 1137 1138/// CXXRecordType - This is a helper class that allows the use of 1139/// isa/cast/dyncast to detect TagType objects of C++ structs/unions/classes. 1140class CXXRecordType : public RecordType { 1141 explicit CXXRecordType(CXXRecordDecl *D) 1142 : RecordType(reinterpret_cast<RecordDecl*>(D)) { } 1143 friend class ASTContext; // ASTContext creates these. 1144public: 1145 1146 CXXRecordDecl *getDecl() const { 1147 return reinterpret_cast<CXXRecordDecl*>(RecordType::getDecl()); 1148 } 1149 1150 static bool classof(const TagType *T); 1151 static bool classof(const Type *T) { 1152 return isa<TagType>(T) && classof(cast<TagType>(T)); 1153 } 1154 static bool classof(const CXXRecordType *) { return true; } 1155}; 1156 1157/// EnumType - This is a helper class that allows the use of isa/cast/dyncast 1158/// to detect TagType objects of enums. 1159class EnumType : public TagType { 1160 explicit EnumType(EnumDecl *D) 1161 : TagType(reinterpret_cast<TagDecl*>(D), QualType()) { } 1162 friend class ASTContext; // ASTContext creates these. 1163public: 1164 1165 EnumDecl *getDecl() const { 1166 return reinterpret_cast<EnumDecl*>(TagType::getDecl()); 1167 } 1168 1169 static bool classof(const TagType *T); 1170 static bool classof(const Type *T) { 1171 return isa<TagType>(T) && classof(cast<TagType>(T)); 1172 } 1173 static bool classof(const EnumType *) { return true; } 1174}; 1175 1176 1177 1178/// ObjCInterfaceType - Interfaces are the core concept in Objective-C for 1179/// object oriented design. They basically correspond to C++ classes. There 1180/// are two kinds of interface types, normal interfaces like "NSString" and 1181/// qualified interfaces, which are qualified with a protocol list like 1182/// "NSString<NSCopyable, NSAmazing>". Qualified interface types are instances 1183/// of ObjCQualifiedInterfaceType, which is a subclass of ObjCInterfaceType. 1184class ObjCInterfaceType : public Type { 1185 ObjCInterfaceDecl *Decl; 1186protected: 1187 ObjCInterfaceType(TypeClass tc, ObjCInterfaceDecl *D) : 1188 Type(tc, QualType()), Decl(D) { } 1189 friend class ASTContext; // ASTContext creates these. 1190public: 1191 1192 ObjCInterfaceDecl *getDecl() const { return Decl; } 1193 1194 /// qual_iterator and friends: this provides access to the (potentially empty) 1195 /// list of protocols qualifying this interface. If this is an instance of 1196 /// ObjCQualifiedInterfaceType it returns the list, otherwise it returns an 1197 /// empty list if there are no qualifying protocols. 1198 typedef llvm::SmallVector<ObjCProtocolDecl*, 8>::const_iterator qual_iterator; 1199 inline qual_iterator qual_begin() const; 1200 inline qual_iterator qual_end() const; 1201 bool qual_empty() const { return getTypeClass() != ObjCQualifiedInterface; } 1202 1203 /// getNumProtocols - Return the number of qualifying protocols in this 1204 /// interface type, or 0 if there are none. 1205 inline unsigned getNumProtocols() const; 1206 1207 /// getProtocol - Return the specified qualifying protocol. 1208 inline ObjCProtocolDecl *getProtocol(unsigned i) const; 1209 1210 1211 virtual void getAsStringInternal(std::string &InnerString) const; 1212 static bool classof(const Type *T) { 1213 return T->getTypeClass() == ObjCInterface || 1214 T->getTypeClass() == ObjCQualifiedInterface; 1215 } 1216 static bool classof(const ObjCInterfaceType *) { return true; } 1217}; 1218 1219/// ObjCQualifiedInterfaceType - This class represents interface types 1220/// conforming to a list of protocols, such as INTF<Proto1, Proto2, Proto1>. 1221/// 1222/// Duplicate protocols are removed and protocol list is canonicalized to be in 1223/// alphabetical order. 1224class ObjCQualifiedInterfaceType : public ObjCInterfaceType, 1225 public llvm::FoldingSetNode { 1226 1227 // List of protocols for this protocol conforming object type 1228 // List is sorted on protocol name. No protocol is enterred more than once. 1229 llvm::SmallVector<ObjCProtocolDecl*, 4> Protocols; 1230 1231 ObjCQualifiedInterfaceType(ObjCInterfaceDecl *D, 1232 ObjCProtocolDecl **Protos, unsigned NumP) : 1233 ObjCInterfaceType(ObjCQualifiedInterface, D), 1234 Protocols(Protos, Protos+NumP) { } 1235 friend class ASTContext; // ASTContext creates these. 1236public: 1237 1238 ObjCProtocolDecl *getProtocol(unsigned i) const { 1239 return Protocols[i]; 1240 } 1241 unsigned getNumProtocols() const { 1242 return Protocols.size(); 1243 } 1244 1245 qual_iterator qual_begin() const { return Protocols.begin(); } 1246 qual_iterator qual_end() const { return Protocols.end(); } 1247 1248 virtual void getAsStringInternal(std::string &InnerString) const; 1249 1250 void Profile(llvm::FoldingSetNodeID &ID); 1251 static void Profile(llvm::FoldingSetNodeID &ID, 1252 const ObjCInterfaceDecl *Decl, 1253 ObjCProtocolDecl **protocols, unsigned NumProtocols); 1254 1255 static bool classof(const Type *T) { 1256 return T->getTypeClass() == ObjCQualifiedInterface; 1257 } 1258 static bool classof(const ObjCQualifiedInterfaceType *) { return true; } 1259}; 1260 1261inline ObjCInterfaceType::qual_iterator ObjCInterfaceType::qual_begin() const { 1262 if (const ObjCQualifiedInterfaceType *QIT = 1263 dyn_cast<ObjCQualifiedInterfaceType>(this)) 1264 return QIT->qual_begin(); 1265 return 0; 1266} 1267inline ObjCInterfaceType::qual_iterator ObjCInterfaceType::qual_end() const { 1268 if (const ObjCQualifiedInterfaceType *QIT = 1269 dyn_cast<ObjCQualifiedInterfaceType>(this)) 1270 return QIT->qual_end(); 1271 return 0; 1272} 1273 1274/// getNumProtocols - Return the number of qualifying protocols in this 1275/// interface type, or 0 if there are none. 1276inline unsigned ObjCInterfaceType::getNumProtocols() const { 1277 if (const ObjCQualifiedInterfaceType *QIT = 1278 dyn_cast<ObjCQualifiedInterfaceType>(this)) 1279 return QIT->getNumProtocols(); 1280 return 0; 1281} 1282 1283/// getProtocol - Return the specified qualifying protocol. 1284inline ObjCProtocolDecl *ObjCInterfaceType::getProtocol(unsigned i) const { 1285 return cast<ObjCQualifiedInterfaceType>(this)->getProtocol(i); 1286} 1287 1288 1289 1290/// ObjCQualifiedIdType - to represent id<protocol-list>. 1291/// 1292/// Duplicate protocols are removed and protocol list is canonicalized to be in 1293/// alphabetical order. 1294class ObjCQualifiedIdType : public Type, 1295 public llvm::FoldingSetNode { 1296 // List of protocols for this protocol conforming 'id' type 1297 // List is sorted on protocol name. No protocol is enterred more than once. 1298 llvm::SmallVector<ObjCProtocolDecl*, 8> Protocols; 1299 1300 ObjCQualifiedIdType(ObjCProtocolDecl **Protos, unsigned NumP) 1301 : Type(ObjCQualifiedId, QualType()/*these are always canonical*/), 1302 Protocols(Protos, Protos+NumP) { } 1303 friend class ASTContext; // ASTContext creates these. 1304public: 1305 1306 ObjCProtocolDecl *getProtocols(unsigned i) const { 1307 return Protocols[i]; 1308 } 1309 unsigned getNumProtocols() const { 1310 return Protocols.size(); 1311 } 1312 ObjCProtocolDecl **getReferencedProtocols() { 1313 return &Protocols[0]; 1314 } 1315 1316 typedef llvm::SmallVector<ObjCProtocolDecl*, 8>::const_iterator qual_iterator; 1317 qual_iterator qual_begin() const { return Protocols.begin(); } 1318 qual_iterator qual_end() const { return Protocols.end(); } 1319 1320 virtual void getAsStringInternal(std::string &InnerString) const; 1321 1322 void Profile(llvm::FoldingSetNodeID &ID); 1323 static void Profile(llvm::FoldingSetNodeID &ID, 1324 ObjCProtocolDecl **protocols, unsigned NumProtocols); 1325 1326 static bool classof(const Type *T) { 1327 return T->getTypeClass() == ObjCQualifiedId; 1328 } 1329 static bool classof(const ObjCQualifiedIdType *) { return true; } 1330 1331}; 1332 1333 1334// Inline function definitions. 1335 1336/// getUnqualifiedType - Return the type without any qualifiers. 1337inline QualType QualType::getUnqualifiedType() const { 1338 Type *TP = getTypePtr(); 1339 if (const ASQualType *ASQT = dyn_cast<ASQualType>(TP)) 1340 TP = ASQT->getBaseType(); 1341 return QualType(TP, 0); 1342} 1343 1344/// getAddressSpace - Return the address space of this type. 1345inline unsigned QualType::getAddressSpace() const { 1346 QualType CT = getTypePtr()->getCanonicalTypeInternal(); 1347 if (const ArrayType *AT = dyn_cast<ArrayType>(CT)) 1348 return AT->getElementType().getAddressSpace(); 1349 if (const RecordType *RT = dyn_cast<RecordType>(CT)) 1350 return RT->getAddressSpace(); 1351 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CT)) 1352 return ASQT->getAddressSpace(); 1353 return 0; 1354} 1355 1356/// isMoreQualifiedThan - Determine whether this type is more 1357/// qualified than the Other type. For example, "const volatile int" 1358/// is more qualified than "const int", "volatile int", and 1359/// "int". However, it is not more qualified than "const volatile 1360/// int". 1361inline bool QualType::isMoreQualifiedThan(QualType Other) const { 1362 // FIXME: Handle address spaces 1363 unsigned MyQuals = this->getCVRQualifiers(); 1364 unsigned OtherQuals = Other.getCVRQualifiers(); 1365 assert(this->getAddressSpace() == 0 && "Address space not checked"); 1366 assert(Other.getAddressSpace() == 0 && "Address space not checked"); 1367 return MyQuals != OtherQuals && (MyQuals | OtherQuals) == MyQuals; 1368} 1369 1370/// isAtLeastAsQualifiedAs - Determine whether this type is at last 1371/// as qualified as the Other type. For example, "const volatile 1372/// int" is at least as qualified as "const int", "volatile int", 1373/// "int", and "const volatile int". 1374inline bool QualType::isAtLeastAsQualifiedAs(QualType Other) const { 1375 // FIXME: Handle address spaces 1376 unsigned MyQuals = this->getCVRQualifiers(); 1377 unsigned OtherQuals = Other.getCVRQualifiers(); 1378 assert(this->getAddressSpace() == 0 && "Address space not checked"); 1379 assert(Other.getAddressSpace() == 0 && "Address space not checked"); 1380 return (MyQuals | OtherQuals) == MyQuals; 1381} 1382 1383/// getNonReferenceType - If Type is a reference type (e.g., const 1384/// int&), returns the type that the reference refers to ("const 1385/// int"). Otherwise, returns the type itself. This routine is used 1386/// throughout Sema to implement C++ 5p6: 1387/// 1388/// If an expression initially has the type "reference to T" (8.3.2, 1389/// 8.5.3), the type is adjusted to "T" prior to any further 1390/// analysis, the expression designates the object or function 1391/// denoted by the reference, and the expression is an lvalue. 1392inline QualType QualType::getNonReferenceType() const { 1393 if (const ReferenceType *RefType = (*this)->getAsReferenceType()) 1394 return RefType->getPointeeType(); 1395 else 1396 return *this; 1397} 1398 1399inline const TypedefType* Type::getAsTypedefType() const { 1400 return dyn_cast<TypedefType>(this); 1401} 1402inline const ObjCInterfaceType *Type::getAsPointerToObjCInterfaceType() const { 1403 if (const PointerType *PT = getAsPointerType()) 1404 return PT->getPointeeType()->getAsObjCInterfaceType(); 1405 return 0; 1406} 1407 1408// NOTE: All of these methods use "getUnqualifiedType" to strip off address 1409// space qualifiers if present. 1410inline bool Type::isFunctionType() const { 1411 return isa<FunctionType>(CanonicalType.getUnqualifiedType()); 1412} 1413inline bool Type::isPointerType() const { 1414 return isa<PointerType>(CanonicalType.getUnqualifiedType()); 1415} 1416inline bool Type::isBlockPointerType() const { 1417 return isa<BlockPointerType>(CanonicalType); 1418} 1419inline bool Type::isReferenceType() const { 1420 return isa<ReferenceType>(CanonicalType.getUnqualifiedType()); 1421} 1422inline bool Type::isPointerLikeType() const { 1423 return isa<PointerLikeType>(CanonicalType.getUnqualifiedType()); 1424} 1425inline bool Type::isFunctionPointerType() const { 1426 if (const PointerType* T = getAsPointerType()) 1427 return T->getPointeeType()->isFunctionType(); 1428 else 1429 return false; 1430} 1431inline bool Type::isArrayType() const { 1432 return isa<ArrayType>(CanonicalType.getUnqualifiedType()); 1433} 1434inline bool Type::isConstantArrayType() const { 1435 return isa<ConstantArrayType>(CanonicalType.getUnqualifiedType()); 1436} 1437inline bool Type::isIncompleteArrayType() const { 1438 return isa<IncompleteArrayType>(CanonicalType.getUnqualifiedType()); 1439} 1440inline bool Type::isVariableArrayType() const { 1441 return isa<VariableArrayType>(CanonicalType.getUnqualifiedType()); 1442} 1443inline bool Type::isRecordType() const { 1444 return isa<RecordType>(CanonicalType.getUnqualifiedType()); 1445} 1446inline bool Type::isAnyComplexType() const { 1447 return isa<ComplexType>(CanonicalType.getUnqualifiedType()); 1448} 1449inline bool Type::isVectorType() const { 1450 return isa<VectorType>(CanonicalType.getUnqualifiedType()); 1451} 1452inline bool Type::isExtVectorType() const { 1453 return isa<ExtVectorType>(CanonicalType.getUnqualifiedType()); 1454} 1455inline bool Type::isObjCInterfaceType() const { 1456 return isa<ObjCInterfaceType>(CanonicalType.getUnqualifiedType()); 1457} 1458inline bool Type::isObjCQualifiedInterfaceType() const { 1459 return isa<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType()); 1460} 1461inline bool Type::isObjCQualifiedIdType() const { 1462 return isa<ObjCQualifiedIdType>(CanonicalType.getUnqualifiedType()); 1463} 1464} // end namespace clang 1465 1466#endif 1467