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