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