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