Type.h revision 566c2ba743065ec090f5154d5c30cf593aa12a6e
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 getNumericAccessorIdx(char c) { 949 switch (c) { 950 default: return -1; 951 case '0': return 0; 952 case '1': return 1; 953 case '2': return 2; 954 case '3': return 3; 955 case '4': return 4; 956 case '5': return 5; 957 case '6': return 6; 958 case '7': return 7; 959 case '8': return 8; 960 case '9': return 9; 961 case 'a': return 10; 962 case 'b': return 11; 963 case 'c': return 12; 964 case 'd': return 13; 965 case 'e': return 14; 966 case 'f': return 15; 967 } 968 } 969 970 static int getAccessorIdx(char c) { 971 if (int idx = getPointAccessorIdx(c)+1) return idx-1; 972 return getNumericAccessorIdx(c); 973 } 974 975 bool isAccessorWithinNumElements(char c) const { 976 if (int idx = getAccessorIdx(c)+1) 977 return unsigned(idx-1) < NumElements; 978 return false; 979 } 980 virtual void getAsStringInternal(std::string &InnerString) const; 981 982 static bool classof(const Type *T) { 983 return T->getTypeClass() == ExtVector; 984 } 985 static bool classof(const ExtVectorType *) { return true; } 986}; 987 988/// FunctionType - C99 6.7.5.3 - Function Declarators. This is the common base 989/// class of FunctionTypeNoProto and FunctionTypeProto. 990/// 991class FunctionType : public Type { 992 /// SubClassData - This field is owned by the subclass, put here to pack 993 /// tightly with the ivars in Type. 994 bool SubClassData : 1; 995 996 /// TypeQuals - Used only by FunctionTypeProto, put here to pack with the 997 /// other bitfields. 998 /// The qualifiers are part of FunctionTypeProto because... 999 /// 1000 /// C++ 8.3.5p4: The return type, the parameter type list and the 1001 /// cv-qualifier-seq, [...], are part of the function type. 1002 /// 1003 unsigned TypeQuals : 3; 1004 1005 // The type returned by the function. 1006 QualType ResultType; 1007protected: 1008 FunctionType(TypeClass tc, QualType res, bool SubclassInfo, 1009 unsigned typeQuals, QualType Canonical, bool Dependent) 1010 : Type(tc, Canonical, Dependent), 1011 SubClassData(SubclassInfo), TypeQuals(typeQuals), ResultType(res) {} 1012 bool getSubClassData() const { return SubClassData; } 1013 unsigned getTypeQuals() const { return TypeQuals; } 1014public: 1015 1016 QualType getResultType() const { return ResultType; } 1017 1018 1019 static bool classof(const Type *T) { 1020 return T->getTypeClass() == FunctionNoProto || 1021 T->getTypeClass() == FunctionProto; 1022 } 1023 static bool classof(const FunctionType *) { return true; } 1024}; 1025 1026/// FunctionTypeNoProto - Represents a K&R-style 'int foo()' function, which has 1027/// no information available about its arguments. 1028class FunctionTypeNoProto : public FunctionType, public llvm::FoldingSetNode { 1029 FunctionTypeNoProto(QualType Result, QualType Canonical) 1030 : FunctionType(FunctionNoProto, Result, false, 0, Canonical, 1031 /*Dependent=*/false) {} 1032 friend class ASTContext; // ASTContext creates these. 1033public: 1034 // No additional state past what FunctionType provides. 1035 1036 virtual void getAsStringInternal(std::string &InnerString) const; 1037 1038 void Profile(llvm::FoldingSetNodeID &ID) { 1039 Profile(ID, getResultType()); 1040 } 1041 static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType) { 1042 ID.AddPointer(ResultType.getAsOpaquePtr()); 1043 } 1044 1045 static bool classof(const Type *T) { 1046 return T->getTypeClass() == FunctionNoProto; 1047 } 1048 static bool classof(const FunctionTypeNoProto *) { return true; } 1049 1050protected: 1051 virtual void EmitImpl(llvm::Serializer& S) const; 1052 static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D); 1053 friend class Type; 1054}; 1055 1056/// FunctionTypeProto - Represents a prototype with argument type info, e.g. 1057/// 'int foo(int)' or 'int foo(void)'. 'void' is represented as having no 1058/// arguments, not as having a single void argument. 1059class FunctionTypeProto : public FunctionType, public llvm::FoldingSetNode { 1060 /// hasAnyDependentType - Determine whether there are any dependent 1061 /// types within the arguments passed in. 1062 static bool hasAnyDependentType(const QualType *ArgArray, unsigned numArgs) { 1063 for (unsigned Idx = 0; Idx < numArgs; ++Idx) 1064 if (ArgArray[Idx]->isDependentType()) 1065 return true; 1066 1067 return false; 1068 } 1069 1070 FunctionTypeProto(QualType Result, const QualType *ArgArray, unsigned numArgs, 1071 bool isVariadic, unsigned typeQuals, QualType Canonical) 1072 : FunctionType(FunctionProto, Result, isVariadic, typeQuals, Canonical, 1073 (Result->isDependentType() || 1074 hasAnyDependentType(ArgArray, numArgs))), 1075 NumArgs(numArgs) { 1076 // Fill in the trailing argument array. 1077 QualType *ArgInfo = reinterpret_cast<QualType *>(this+1);; 1078 for (unsigned i = 0; i != numArgs; ++i) 1079 ArgInfo[i] = ArgArray[i]; 1080 } 1081 1082 /// NumArgs - The number of arguments this function has, not counting '...'. 1083 unsigned NumArgs; 1084 1085 /// ArgInfo - There is an variable size array after the class in memory that 1086 /// holds the argument types. 1087 1088 friend class ASTContext; // ASTContext creates these. 1089 1090public: 1091 unsigned getNumArgs() const { return NumArgs; } 1092 QualType getArgType(unsigned i) const { 1093 assert(i < NumArgs && "Invalid argument number!"); 1094 return arg_type_begin()[i]; 1095 } 1096 1097 bool isVariadic() const { return getSubClassData(); } 1098 unsigned getTypeQuals() const { return FunctionType::getTypeQuals(); } 1099 1100 typedef const QualType *arg_type_iterator; 1101 arg_type_iterator arg_type_begin() const { 1102 return reinterpret_cast<const QualType *>(this+1); 1103 } 1104 arg_type_iterator arg_type_end() const { return arg_type_begin()+NumArgs; } 1105 1106 virtual void getAsStringInternal(std::string &InnerString) const; 1107 1108 static bool classof(const Type *T) { 1109 return T->getTypeClass() == FunctionProto; 1110 } 1111 static bool classof(const FunctionTypeProto *) { return true; } 1112 1113 void Profile(llvm::FoldingSetNodeID &ID); 1114 static void Profile(llvm::FoldingSetNodeID &ID, QualType Result, 1115 arg_type_iterator ArgTys, unsigned NumArgs, 1116 bool isVariadic, unsigned TypeQuals); 1117 1118protected: 1119 virtual void EmitImpl(llvm::Serializer& S) const; 1120 static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D); 1121 friend class Type; 1122}; 1123 1124 1125class TypedefType : public Type { 1126 TypedefDecl *Decl; 1127protected: 1128 TypedefType(TypeClass tc, TypedefDecl *D, QualType can) 1129 : Type(tc, can, can->isDependentType()), Decl(D) { 1130 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 1131 } 1132 friend class ASTContext; // ASTContext creates these. 1133public: 1134 1135 TypedefDecl *getDecl() const { return Decl; } 1136 1137 /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to 1138 /// potentially looking through *all* consequtive typedefs. This returns the 1139 /// sum of the type qualifiers, so if you have: 1140 /// typedef const int A; 1141 /// typedef volatile A B; 1142 /// looking through the typedefs for B will give you "const volatile A". 1143 QualType LookThroughTypedefs() const; 1144 1145 virtual void getAsStringInternal(std::string &InnerString) const; 1146 1147 static bool classof(const Type *T) { return T->getTypeClass() == TypeName; } 1148 static bool classof(const TypedefType *) { return true; } 1149 1150protected: 1151 virtual void EmitImpl(llvm::Serializer& S) const; 1152 static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D); 1153 friend class Type; 1154}; 1155 1156/// TypeOfExpr (GCC extension). 1157class TypeOfExpr : public Type { 1158 Expr *TOExpr; 1159 TypeOfExpr(Expr *E, QualType can); 1160 friend class ASTContext; // ASTContext creates these. 1161public: 1162 Expr *getUnderlyingExpr() const { return TOExpr; } 1163 1164 virtual void getAsStringInternal(std::string &InnerString) const; 1165 1166 static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExp; } 1167 static bool classof(const TypeOfExpr *) { return true; } 1168 1169protected: 1170 virtual void EmitImpl(llvm::Serializer& S) const; 1171 static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D); 1172 friend class Type; 1173}; 1174 1175/// TypeOfType (GCC extension). 1176class TypeOfType : public Type { 1177 QualType TOType; 1178 TypeOfType(QualType T, QualType can) 1179 : Type(TypeOfTyp, can, T->isDependentType()), TOType(T) { 1180 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 1181 } 1182 friend class ASTContext; // ASTContext creates these. 1183public: 1184 QualType getUnderlyingType() const { return TOType; } 1185 1186 virtual void getAsStringInternal(std::string &InnerString) const; 1187 1188 static bool classof(const Type *T) { return T->getTypeClass() == TypeOfTyp; } 1189 static bool classof(const TypeOfType *) { return true; } 1190 1191protected: 1192 virtual void EmitImpl(llvm::Serializer& S) const; 1193 static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D); 1194 friend class Type; 1195}; 1196 1197class TagType : public Type { 1198 /// Stores the TagDecl associated with this type. The decl will 1199 /// point to the TagDecl that actually defines the entity (or is a 1200 /// definition in progress), if there is such a definition. The 1201 /// single-bit value will be non-zero when this tag is in the 1202 /// process of being defined. 1203 llvm::PointerIntPair<TagDecl *, 1> decl; 1204 friend class ASTContext; 1205 friend class TagDecl; 1206 1207protected: 1208 // FIXME: We'll need the user to pass in information about whether 1209 // this type is dependent or not, because we don't have enough 1210 // information to compute it here. 1211 TagType(TagDecl *D, QualType can) 1212 : Type(Tagged, can, /*Dependent=*/false), decl(D, 0) {} 1213 1214public: 1215 TagDecl *getDecl() const { return decl.getPointer(); } 1216 1217 /// @brief Determines whether this type is in the process of being 1218 /// defined. 1219 bool isBeingDefined() const { return decl.getInt(); } 1220 void setBeingDefined(bool Def) { decl.setInt(Def? 1 : 0); } 1221 1222 virtual void getAsStringInternal(std::string &InnerString) const; 1223 1224 static bool classof(const Type *T) { return T->getTypeClass() == Tagged; } 1225 static bool classof(const TagType *) { return true; } 1226 1227protected: 1228 virtual void EmitImpl(llvm::Serializer& S) const; 1229 static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D); 1230 friend class Type; 1231}; 1232 1233/// RecordType - This is a helper class that allows the use of isa/cast/dyncast 1234/// to detect TagType objects of structs/unions/classes. 1235class RecordType : public TagType { 1236protected: 1237 explicit RecordType(RecordDecl *D) 1238 : TagType(reinterpret_cast<TagDecl*>(D), QualType()) { } 1239 friend class ASTContext; // ASTContext creates these. 1240public: 1241 1242 RecordDecl *getDecl() const { 1243 return reinterpret_cast<RecordDecl*>(TagType::getDecl()); 1244 } 1245 1246 // FIXME: This predicate is a helper to QualType/Type. It needs to 1247 // recursively check all fields for const-ness. If any field is declared 1248 // const, it needs to return false. 1249 bool hasConstFields() const { return false; } 1250 1251 // FIXME: RecordType needs to check when it is created that all fields are in 1252 // the same address space, and return that. 1253 unsigned getAddressSpace() const { return 0; } 1254 1255 static bool classof(const TagType *T); 1256 static bool classof(const Type *T) { 1257 return isa<TagType>(T) && classof(cast<TagType>(T)); 1258 } 1259 static bool classof(const RecordType *) { return true; } 1260}; 1261 1262/// CXXRecordType - This is a helper class that allows the use of 1263/// isa/cast/dyncast to detect TagType objects of C++ structs/unions/classes. 1264class CXXRecordType : public RecordType { 1265 explicit CXXRecordType(CXXRecordDecl *D) 1266 : RecordType(reinterpret_cast<RecordDecl*>(D)) { } 1267 friend class ASTContext; // ASTContext creates these. 1268public: 1269 1270 CXXRecordDecl *getDecl() const { 1271 return reinterpret_cast<CXXRecordDecl*>(RecordType::getDecl()); 1272 } 1273 1274 static bool classof(const TagType *T); 1275 static bool classof(const Type *T) { 1276 return isa<TagType>(T) && classof(cast<TagType>(T)); 1277 } 1278 static bool classof(const CXXRecordType *) { return true; } 1279}; 1280 1281/// EnumType - This is a helper class that allows the use of isa/cast/dyncast 1282/// to detect TagType objects of enums. 1283class EnumType : public TagType { 1284 explicit EnumType(EnumDecl *D) 1285 : TagType(reinterpret_cast<TagDecl*>(D), QualType()) { } 1286 friend class ASTContext; // ASTContext creates these. 1287public: 1288 1289 EnumDecl *getDecl() const { 1290 return reinterpret_cast<EnumDecl*>(TagType::getDecl()); 1291 } 1292 1293 static bool classof(const TagType *T); 1294 static bool classof(const Type *T) { 1295 return isa<TagType>(T) && classof(cast<TagType>(T)); 1296 } 1297 static bool classof(const EnumType *) { return true; } 1298}; 1299 1300class TemplateTypeParmType : public Type { 1301 TemplateTypeParmDecl *Decl; 1302 1303protected: 1304 TemplateTypeParmType(TemplateTypeParmDecl *D) 1305 : Type(TemplateTypeParm, QualType(this, 0), /*Dependent=*/true), Decl(D) { } 1306 1307 friend class ASTContext; // ASTContext creates these 1308 1309public: 1310 TemplateTypeParmDecl *getDecl() const { return Decl; } 1311 1312 virtual void getAsStringInternal(std::string &InnerString) const; 1313 1314 static bool classof(const Type *T) { 1315 return T->getTypeClass() == TemplateTypeParm; 1316 } 1317 static bool classof(const TemplateTypeParmType *T) { return true; } 1318 1319protected: 1320 virtual void EmitImpl(llvm::Serializer& S) const; 1321 static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D); 1322 friend class Type; 1323}; 1324 1325/// ObjCInterfaceType - Interfaces are the core concept in Objective-C for 1326/// object oriented design. They basically correspond to C++ classes. There 1327/// are two kinds of interface types, normal interfaces like "NSString" and 1328/// qualified interfaces, which are qualified with a protocol list like 1329/// "NSString<NSCopyable, NSAmazing>". Qualified interface types are instances 1330/// of ObjCQualifiedInterfaceType, which is a subclass of ObjCInterfaceType. 1331class ObjCInterfaceType : public Type { 1332 ObjCInterfaceDecl *Decl; 1333protected: 1334 ObjCInterfaceType(TypeClass tc, ObjCInterfaceDecl *D) : 1335 Type(tc, QualType(), /*Dependent=*/false), Decl(D) { } 1336 friend class ASTContext; // ASTContext creates these. 1337public: 1338 1339 ObjCInterfaceDecl *getDecl() const { return Decl; } 1340 1341 /// qual_iterator and friends: this provides access to the (potentially empty) 1342 /// list of protocols qualifying this interface. If this is an instance of 1343 /// ObjCQualifiedInterfaceType it returns the list, otherwise it returns an 1344 /// empty list if there are no qualifying protocols. 1345 typedef llvm::SmallVector<ObjCProtocolDecl*, 8>::const_iterator qual_iterator; 1346 inline qual_iterator qual_begin() const; 1347 inline qual_iterator qual_end() const; 1348 bool qual_empty() const { return getTypeClass() != ObjCQualifiedInterface; } 1349 1350 /// getNumProtocols - Return the number of qualifying protocols in this 1351 /// interface type, or 0 if there are none. 1352 inline unsigned getNumProtocols() const; 1353 1354 /// getProtocol - Return the specified qualifying protocol. 1355 inline ObjCProtocolDecl *getProtocol(unsigned i) const; 1356 1357 1358 virtual void getAsStringInternal(std::string &InnerString) const; 1359 static bool classof(const Type *T) { 1360 return T->getTypeClass() == ObjCInterface || 1361 T->getTypeClass() == ObjCQualifiedInterface; 1362 } 1363 static bool classof(const ObjCInterfaceType *) { return true; } 1364}; 1365 1366/// ObjCQualifiedInterfaceType - This class represents interface types 1367/// conforming to a list of protocols, such as INTF<Proto1, Proto2, Proto1>. 1368/// 1369/// Duplicate protocols are removed and protocol list is canonicalized to be in 1370/// alphabetical order. 1371class ObjCQualifiedInterfaceType : public ObjCInterfaceType, 1372 public llvm::FoldingSetNode { 1373 1374 // List of protocols for this protocol conforming object type 1375 // List is sorted on protocol name. No protocol is enterred more than once. 1376 llvm::SmallVector<ObjCProtocolDecl*, 4> Protocols; 1377 1378 ObjCQualifiedInterfaceType(ObjCInterfaceDecl *D, 1379 ObjCProtocolDecl **Protos, unsigned NumP) : 1380 ObjCInterfaceType(ObjCQualifiedInterface, D), 1381 Protocols(Protos, Protos+NumP) { } 1382 friend class ASTContext; // ASTContext creates these. 1383public: 1384 1385 ObjCProtocolDecl *getProtocol(unsigned i) const { 1386 return Protocols[i]; 1387 } 1388 unsigned getNumProtocols() const { 1389 return Protocols.size(); 1390 } 1391 1392 qual_iterator qual_begin() const { return Protocols.begin(); } 1393 qual_iterator qual_end() const { return Protocols.end(); } 1394 1395 virtual void getAsStringInternal(std::string &InnerString) const; 1396 1397 void Profile(llvm::FoldingSetNodeID &ID); 1398 static void Profile(llvm::FoldingSetNodeID &ID, 1399 const ObjCInterfaceDecl *Decl, 1400 ObjCProtocolDecl **protocols, unsigned NumProtocols); 1401 1402 static bool classof(const Type *T) { 1403 return T->getTypeClass() == ObjCQualifiedInterface; 1404 } 1405 static bool classof(const ObjCQualifiedInterfaceType *) { return true; } 1406}; 1407 1408inline ObjCInterfaceType::qual_iterator ObjCInterfaceType::qual_begin() const { 1409 if (const ObjCQualifiedInterfaceType *QIT = 1410 dyn_cast<ObjCQualifiedInterfaceType>(this)) 1411 return QIT->qual_begin(); 1412 return 0; 1413} 1414inline ObjCInterfaceType::qual_iterator ObjCInterfaceType::qual_end() const { 1415 if (const ObjCQualifiedInterfaceType *QIT = 1416 dyn_cast<ObjCQualifiedInterfaceType>(this)) 1417 return QIT->qual_end(); 1418 return 0; 1419} 1420 1421/// getNumProtocols - Return the number of qualifying protocols in this 1422/// interface type, or 0 if there are none. 1423inline unsigned ObjCInterfaceType::getNumProtocols() const { 1424 if (const ObjCQualifiedInterfaceType *QIT = 1425 dyn_cast<ObjCQualifiedInterfaceType>(this)) 1426 return QIT->getNumProtocols(); 1427 return 0; 1428} 1429 1430/// getProtocol - Return the specified qualifying protocol. 1431inline ObjCProtocolDecl *ObjCInterfaceType::getProtocol(unsigned i) const { 1432 return cast<ObjCQualifiedInterfaceType>(this)->getProtocol(i); 1433} 1434 1435 1436 1437/// ObjCQualifiedIdType - to represent id<protocol-list>. 1438/// 1439/// Duplicate protocols are removed and protocol list is canonicalized to be in 1440/// alphabetical order. 1441class ObjCQualifiedIdType : public Type, 1442 public llvm::FoldingSetNode { 1443 // List of protocols for this protocol conforming 'id' type 1444 // List is sorted on protocol name. No protocol is enterred more than once. 1445 llvm::SmallVector<ObjCProtocolDecl*, 8> Protocols; 1446 1447 ObjCQualifiedIdType(ObjCProtocolDecl **Protos, unsigned NumP) 1448 : Type(ObjCQualifiedId, QualType()/*these are always canonical*/, 1449 /*Dependent=*/false), 1450 Protocols(Protos, Protos+NumP) { } 1451 friend class ASTContext; // ASTContext creates these. 1452public: 1453 1454 ObjCProtocolDecl *getProtocols(unsigned i) const { 1455 return Protocols[i]; 1456 } 1457 unsigned getNumProtocols() const { 1458 return Protocols.size(); 1459 } 1460 ObjCProtocolDecl **getReferencedProtocols() { 1461 return &Protocols[0]; 1462 } 1463 1464 typedef llvm::SmallVector<ObjCProtocolDecl*, 8>::const_iterator qual_iterator; 1465 qual_iterator qual_begin() const { return Protocols.begin(); } 1466 qual_iterator qual_end() const { return Protocols.end(); } 1467 1468 virtual void getAsStringInternal(std::string &InnerString) const; 1469 1470 void Profile(llvm::FoldingSetNodeID &ID); 1471 static void Profile(llvm::FoldingSetNodeID &ID, 1472 ObjCProtocolDecl **protocols, unsigned NumProtocols); 1473 1474 static bool classof(const Type *T) { 1475 return T->getTypeClass() == ObjCQualifiedId; 1476 } 1477 static bool classof(const ObjCQualifiedIdType *) { return true; } 1478 1479}; 1480 1481 1482// Inline function definitions. 1483 1484/// getUnqualifiedType - Return the type without any qualifiers. 1485inline QualType QualType::getUnqualifiedType() const { 1486 Type *TP = getTypePtr(); 1487 if (const ASQualType *ASQT = dyn_cast<ASQualType>(TP)) 1488 TP = ASQT->getBaseType(); 1489 return QualType(TP, 0); 1490} 1491 1492/// getAddressSpace - Return the address space of this type. 1493inline unsigned QualType::getAddressSpace() const { 1494 QualType CT = getTypePtr()->getCanonicalTypeInternal(); 1495 if (const ArrayType *AT = dyn_cast<ArrayType>(CT)) 1496 return AT->getElementType().getAddressSpace(); 1497 if (const RecordType *RT = dyn_cast<RecordType>(CT)) 1498 return RT->getAddressSpace(); 1499 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CT)) 1500 return ASQT->getAddressSpace(); 1501 return 0; 1502} 1503 1504/// isMoreQualifiedThan - Determine whether this type is more 1505/// qualified than the Other type. For example, "const volatile int" 1506/// is more qualified than "const int", "volatile int", and 1507/// "int". However, it is not more qualified than "const volatile 1508/// int". 1509inline bool QualType::isMoreQualifiedThan(QualType Other) const { 1510 // FIXME: Handle address spaces 1511 unsigned MyQuals = this->getCVRQualifiers(); 1512 unsigned OtherQuals = Other.getCVRQualifiers(); 1513 assert(this->getAddressSpace() == 0 && "Address space not checked"); 1514 assert(Other.getAddressSpace() == 0 && "Address space not checked"); 1515 return MyQuals != OtherQuals && (MyQuals | OtherQuals) == MyQuals; 1516} 1517 1518/// isAtLeastAsQualifiedAs - Determine whether this type is at last 1519/// as qualified as the Other type. For example, "const volatile 1520/// int" is at least as qualified as "const int", "volatile int", 1521/// "int", and "const volatile int". 1522inline bool QualType::isAtLeastAsQualifiedAs(QualType Other) const { 1523 // FIXME: Handle address spaces 1524 unsigned MyQuals = this->getCVRQualifiers(); 1525 unsigned OtherQuals = Other.getCVRQualifiers(); 1526 assert(this->getAddressSpace() == 0 && "Address space not checked"); 1527 assert(Other.getAddressSpace() == 0 && "Address space not checked"); 1528 return (MyQuals | OtherQuals) == MyQuals; 1529} 1530 1531/// getNonReferenceType - If Type is a reference type (e.g., const 1532/// int&), returns the type that the reference refers to ("const 1533/// int"). Otherwise, returns the type itself. This routine is used 1534/// throughout Sema to implement C++ 5p6: 1535/// 1536/// If an expression initially has the type "reference to T" (8.3.2, 1537/// 8.5.3), the type is adjusted to "T" prior to any further 1538/// analysis, the expression designates the object or function 1539/// denoted by the reference, and the expression is an lvalue. 1540inline QualType QualType::getNonReferenceType() const { 1541 if (const ReferenceType *RefType = (*this)->getAsReferenceType()) 1542 return RefType->getPointeeType(); 1543 else 1544 return *this; 1545} 1546 1547inline const TypedefType* Type::getAsTypedefType() const { 1548 return dyn_cast<TypedefType>(this); 1549} 1550inline const ObjCInterfaceType *Type::getAsPointerToObjCInterfaceType() const { 1551 if (const PointerType *PT = getAsPointerType()) 1552 return PT->getPointeeType()->getAsObjCInterfaceType(); 1553 return 0; 1554} 1555 1556// NOTE: All of these methods use "getUnqualifiedType" to strip off address 1557// space qualifiers if present. 1558inline bool Type::isFunctionType() const { 1559 return isa<FunctionType>(CanonicalType.getUnqualifiedType()); 1560} 1561inline bool Type::isPointerType() const { 1562 return isa<PointerType>(CanonicalType.getUnqualifiedType()); 1563} 1564inline bool Type::isBlockPointerType() const { 1565 return isa<BlockPointerType>(CanonicalType); 1566} 1567inline bool Type::isReferenceType() const { 1568 return isa<ReferenceType>(CanonicalType.getUnqualifiedType()); 1569} 1570inline bool Type::isPointerLikeType() const { 1571 return isa<PointerLikeType>(CanonicalType.getUnqualifiedType()); 1572} 1573inline bool Type::isFunctionPointerType() const { 1574 if (const PointerType* T = getAsPointerType()) 1575 return T->getPointeeType()->isFunctionType(); 1576 else 1577 return false; 1578} 1579inline bool Type::isArrayType() const { 1580 return isa<ArrayType>(CanonicalType.getUnqualifiedType()); 1581} 1582inline bool Type::isConstantArrayType() const { 1583 return isa<ConstantArrayType>(CanonicalType.getUnqualifiedType()); 1584} 1585inline bool Type::isIncompleteArrayType() const { 1586 return isa<IncompleteArrayType>(CanonicalType.getUnqualifiedType()); 1587} 1588inline bool Type::isVariableArrayType() const { 1589 return isa<VariableArrayType>(CanonicalType.getUnqualifiedType()); 1590} 1591inline bool Type::isDependentSizedArrayType() const { 1592 return isa<DependentSizedArrayType>(CanonicalType.getUnqualifiedType()); 1593} 1594inline bool Type::isRecordType() const { 1595 return isa<RecordType>(CanonicalType.getUnqualifiedType()); 1596} 1597inline bool Type::isAnyComplexType() const { 1598 return isa<ComplexType>(CanonicalType.getUnqualifiedType()); 1599} 1600inline bool Type::isVectorType() const { 1601 return isa<VectorType>(CanonicalType.getUnqualifiedType()); 1602} 1603inline bool Type::isExtVectorType() const { 1604 return isa<ExtVectorType>(CanonicalType.getUnqualifiedType()); 1605} 1606inline bool Type::isObjCInterfaceType() const { 1607 return isa<ObjCInterfaceType>(CanonicalType.getUnqualifiedType()); 1608} 1609inline bool Type::isObjCQualifiedInterfaceType() const { 1610 return isa<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType()); 1611} 1612inline bool Type::isObjCQualifiedIdType() const { 1613 return isa<ObjCQualifiedIdType>(CanonicalType.getUnqualifiedType()); 1614} 1615inline bool Type::isTemplateTypeParmType() const { 1616 return isa<TemplateTypeParmType>(CanonicalType.getUnqualifiedType()); 1617} 1618 1619inline bool Type::isOverloadType() const { 1620 if (const BuiltinType *BT = getAsBuiltinType()) 1621 return BT->getKind() == BuiltinType::Overload; 1622 else 1623 return false; 1624} 1625 1626/// Insertion operator for diagnostics. This allows sending QualType's into a 1627/// diagnostic with <<. 1628inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 1629 QualType T) { 1630 DB.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), 1631 Diagnostic::ak_qualtype); 1632 return DB; 1633} 1634 1635} // end namespace clang 1636 1637#endif 1638