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