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