Type.h revision 0f95d329cc0ba37faa0f3d522991a581a407482c
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 "clang/Basic/IdentifierTable.h" 19#include "clang/AST/NestedNameSpecifier.h" 20#include "clang/AST/TemplateName.h" 21#include "llvm/Support/Casting.h" 22#include "llvm/ADT/APSInt.h" 23#include "llvm/ADT/FoldingSet.h" 24#include "llvm/ADT/PointerIntPair.h" 25#include "llvm/ADT/PointerUnion.h" 26 27using llvm::isa; 28using llvm::cast; 29using llvm::cast_or_null; 30using llvm::dyn_cast; 31using llvm::dyn_cast_or_null; 32namespace clang { class Type; } 33 34namespace llvm { 35 template <typename T> 36 class PointerLikeTypeTraits; 37 template<> 38 class PointerLikeTypeTraits< ::clang::Type*> { 39 public: 40 static inline void *getAsVoidPointer(::clang::Type *P) { return P; } 41 static inline ::clang::Type *getFromVoidPointer(void *P) { 42 return static_cast< ::clang::Type*>(P); 43 } 44 enum { NumLowBitsAvailable = 3 }; 45 }; 46} 47 48namespace clang { 49 class ASTContext; 50 class TypedefDecl; 51 class TemplateDecl; 52 class TemplateTypeParmDecl; 53 class NonTypeTemplateParmDecl; 54 class TemplateTemplateParmDecl; 55 class TagDecl; 56 class RecordDecl; 57 class CXXRecordDecl; 58 class EnumDecl; 59 class FieldDecl; 60 class ObjCInterfaceDecl; 61 class ObjCProtocolDecl; 62 class ObjCMethodDecl; 63 class Expr; 64 class Stmt; 65 class SourceLocation; 66 class StmtIteratorBase; 67 class TemplateArgument; 68 class QualifiedNameType; 69 struct PrintingPolicy; 70 71 // Provide forward declarations for all of the *Type classes 72#define TYPE(Class, Base) class Class##Type; 73#include "clang/AST/TypeNodes.def" 74 75/// QualType - For efficiency, we don't store CVR-qualified types as nodes on 76/// their own: instead each reference to a type stores the qualifiers. This 77/// greatly reduces the number of nodes we need to allocate for types (for 78/// example we only need one for 'int', 'const int', 'volatile int', 79/// 'const volatile int', etc). 80/// 81/// As an added efficiency bonus, instead of making this a pair, we just store 82/// the three bits we care about in the low bits of the pointer. To handle the 83/// packing/unpacking, we make QualType be a simple wrapper class that acts like 84/// a smart pointer. 85class QualType { 86 llvm::PointerIntPair<Type*, 3> Value; 87public: 88 enum TQ { // NOTE: These flags must be kept in sync with DeclSpec::TQ. 89 Const = 0x1, 90 Restrict = 0x2, 91 Volatile = 0x4, 92 CVRFlags = Const|Restrict|Volatile 93 }; 94 95 enum GCAttrTypes { 96 GCNone = 0, 97 Weak, 98 Strong 99 }; 100 101 QualType() {} 102 103 QualType(const Type *Ptr, unsigned Quals) 104 : Value(const_cast<Type*>(Ptr), Quals) {} 105 106 unsigned getCVRQualifiers() const { return Value.getInt(); } 107 void setCVRQualifiers(unsigned Quals) { Value.setInt(Quals); } 108 Type *getTypePtr() const { return Value.getPointer(); } 109 110 void *getAsOpaquePtr() const { return Value.getOpaqueValue(); } 111 static QualType getFromOpaquePtr(void *Ptr) { 112 QualType T; 113 T.Value.setFromOpaqueValue(Ptr); 114 return T; 115 } 116 117 Type &operator*() const { 118 return *getTypePtr(); 119 } 120 121 Type *operator->() const { 122 return getTypePtr(); 123 } 124 125 /// isNull - Return true if this QualType doesn't point to a type yet. 126 bool isNull() const { 127 return getTypePtr() == 0; 128 } 129 130 bool isConstQualified() const { 131 return (getCVRQualifiers() & Const) ? true : false; 132 } 133 bool isVolatileQualified() const { 134 return (getCVRQualifiers() & Volatile) ? true : false; 135 } 136 bool isRestrictQualified() const { 137 return (getCVRQualifiers() & Restrict) ? true : false; 138 } 139 140 bool isConstant(ASTContext& Ctx) const; 141 142 /// addConst/addVolatile/addRestrict - add the specified type qual to this 143 /// QualType. 144 void addConst() { Value.setInt(Value.getInt() | Const); } 145 void addVolatile() { Value.setInt(Value.getInt() | Volatile); } 146 void addRestrict() { Value.setInt(Value.getInt() | Restrict); } 147 148 void removeConst() { Value.setInt(Value.getInt() & ~Const); } 149 void removeVolatile() { Value.setInt(Value.getInt() & ~Volatile); } 150 void removeRestrict() { Value.setInt(Value.getInt() & ~Restrict); } 151 152 QualType getQualifiedType(unsigned TQs) const { 153 return QualType(getTypePtr(), TQs); 154 } 155 QualType getWithAdditionalQualifiers(unsigned TQs) const { 156 return QualType(getTypePtr(), TQs|getCVRQualifiers()); 157 } 158 159 QualType withConst() const { return getWithAdditionalQualifiers(Const); } 160 QualType withVolatile() const { return getWithAdditionalQualifiers(Volatile);} 161 QualType withRestrict() const { return getWithAdditionalQualifiers(Restrict);} 162 163 QualType getUnqualifiedType() const; 164 bool isMoreQualifiedThan(QualType Other) const; 165 bool isAtLeastAsQualifiedAs(QualType Other) const; 166 QualType getNonReferenceType() const; 167 168 /// getDesugaredType - Return the specified type with any "sugar" removed from 169 /// the type. This takes off typedefs, typeof's etc. If the outer level of 170 /// the type is already concrete, it returns it unmodified. This is similar 171 /// to getting the canonical type, but it doesn't remove *all* typedefs. For 172 /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is 173 /// concrete. 174 QualType getDesugaredType(bool ForDisplay = false) const; 175 176 /// operator==/!= - Indicate whether the specified types and qualifiers are 177 /// identical. 178 bool operator==(const QualType &RHS) const { 179 return Value == RHS.Value; 180 } 181 bool operator!=(const QualType &RHS) const { 182 return Value != RHS.Value; 183 } 184 std::string getAsString() const; 185 186 std::string getAsString(const PrintingPolicy &Policy) const { 187 std::string S; 188 getAsStringInternal(S, Policy); 189 return S; 190 } 191 void getAsStringInternal(std::string &Str, 192 const PrintingPolicy &Policy) const; 193 194 void dump(const char *s) const; 195 void dump() const; 196 197 void Profile(llvm::FoldingSetNodeID &ID) const { 198 ID.AddPointer(getAsOpaquePtr()); 199 } 200 201public: 202 203 /// getAddressSpace - Return the address space of this type. 204 inline unsigned getAddressSpace() const; 205 206 /// GCAttrTypesAttr - Returns gc attribute of this type. 207 inline QualType::GCAttrTypes getObjCGCAttr() const; 208 209 /// isObjCGCWeak true when Type is objc's weak. 210 bool isObjCGCWeak() const { 211 return getObjCGCAttr() == Weak; 212 } 213 214 /// isObjCGCStrong true when Type is objc's strong. 215 bool isObjCGCStrong() const { 216 return getObjCGCAttr() == Strong; 217 } 218 219 /// getNoReturnAttr - Returns true if the type has the noreturn attribute, 220 /// false otherwise. 221 bool getNoReturnAttr() const; 222}; 223 224} // end clang. 225 226namespace llvm { 227/// Implement simplify_type for QualType, so that we can dyn_cast from QualType 228/// to a specific Type class. 229template<> struct simplify_type<const ::clang::QualType> { 230 typedef ::clang::Type* SimpleType; 231 static SimpleType getSimplifiedValue(const ::clang::QualType &Val) { 232 return Val.getTypePtr(); 233 } 234}; 235template<> struct simplify_type< ::clang::QualType> 236 : public simplify_type<const ::clang::QualType> {}; 237 238// Teach SmallPtrSet that QualType is "basically a pointer". 239template<> 240class PointerLikeTypeTraits<clang::QualType> { 241public: 242 static inline void *getAsVoidPointer(clang::QualType P) { 243 return P.getAsOpaquePtr(); 244 } 245 static inline clang::QualType getFromVoidPointer(void *P) { 246 return clang::QualType::getFromOpaquePtr(P); 247 } 248 // CVR qualifiers go in low bits. 249 enum { NumLowBitsAvailable = 0 }; 250}; 251} // end namespace llvm 252 253namespace clang { 254 255/// Type - This is the base class of the type hierarchy. A central concept 256/// with types is that each type always has a canonical type. A canonical type 257/// is the type with any typedef names stripped out of it or the types it 258/// references. For example, consider: 259/// 260/// typedef int foo; 261/// typedef foo* bar; 262/// 'int *' 'foo *' 'bar' 263/// 264/// There will be a Type object created for 'int'. Since int is canonical, its 265/// canonicaltype pointer points to itself. There is also a Type for 'foo' (a 266/// TypedefType). Its CanonicalType pointer points to the 'int' Type. Next 267/// there is a PointerType that represents 'int*', which, like 'int', is 268/// canonical. Finally, there is a PointerType type for 'foo*' whose canonical 269/// type is 'int*', and there is a TypedefType for 'bar', whose canonical type 270/// is also 'int*'. 271/// 272/// Non-canonical types are useful for emitting diagnostics, without losing 273/// information about typedefs being used. Canonical types are useful for type 274/// comparisons (they allow by-pointer equality tests) and useful for reasoning 275/// about whether something has a particular form (e.g. is a function type), 276/// because they implicitly, recursively, strip all typedefs out of a type. 277/// 278/// Types, once created, are immutable. 279/// 280class Type { 281public: 282 enum TypeClass { 283#define TYPE(Class, Base) Class, 284#define ABSTRACT_TYPE(Class, Base) 285#include "clang/AST/TypeNodes.def" 286 TagFirst = Record, TagLast = Enum 287 }; 288 289private: 290 QualType CanonicalType; 291 292 /// Dependent - Whether this type is a dependent type (C++ [temp.dep.type]). 293 bool Dependent : 1; 294 295 /// TypeClass bitfield - Enum that specifies what subclass this belongs to. 296 /// Note that this should stay at the end of the ivars for Type so that 297 /// subclasses can pack their bitfields into the same word. 298 unsigned TC : 6; 299 300 Type(const Type&); // DO NOT IMPLEMENT. 301 void operator=(const Type&); // DO NOT IMPLEMENT. 302protected: 303 // silence VC++ warning C4355: 'this' : used in base member initializer list 304 Type *this_() { return this; } 305 Type(TypeClass tc, QualType Canonical, bool dependent) 306 : CanonicalType(Canonical.isNull() ? QualType(this_(), 0) : Canonical), 307 Dependent(dependent), TC(tc) {} 308 virtual ~Type() {} 309 virtual void Destroy(ASTContext& C); 310 friend class ASTContext; 311 312public: 313 TypeClass getTypeClass() const { return static_cast<TypeClass>(TC); } 314 315 bool isCanonical() const { return CanonicalType.getTypePtr() == this; } 316 317 /// Types are partitioned into 3 broad categories (C99 6.2.5p1): 318 /// object types, function types, and incomplete types. 319 320 /// \brief Determines whether the type describes an object in memory. 321 /// 322 /// Note that this definition of object type corresponds to the C++ 323 /// definition of object type, which includes incomplete types, as 324 /// opposed to the C definition (which does not include incomplete 325 /// types). 326 bool isObjectType() const; 327 328 /// isIncompleteType - Return true if this is an incomplete type. 329 /// A type that can describe objects, but which lacks information needed to 330 /// determine its size (e.g. void, or a fwd declared struct). Clients of this 331 /// routine will need to determine if the size is actually required. 332 bool isIncompleteType() const; 333 334 /// isIncompleteOrObjectType - Return true if this is an incomplete or object 335 /// type, in other words, not a function type. 336 bool isIncompleteOrObjectType() const { 337 return !isFunctionType(); 338 } 339 340 /// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10). 341 bool isPODType() const; 342 343 /// isVariablyModifiedType (C99 6.7.5.2p2) - Return true for variable array 344 /// types that have a non-constant expression. This does not include "[]". 345 bool isVariablyModifiedType() const; 346 347 /// Helper methods to distinguish type categories. All type predicates 348 /// operate on the canonical type, ignoring typedefs and qualifiers. 349 350 /// isSpecificBuiltinType - Test for a particular builtin type. 351 bool isSpecificBuiltinType(unsigned K) const; 352 353 /// isIntegerType() does *not* include complex integers (a GCC extension). 354 /// isComplexIntegerType() can be used to test for complex integers. 355 bool isIntegerType() const; // C99 6.2.5p17 (int, char, bool, enum) 356 bool isEnumeralType() const; 357 bool isBooleanType() const; 358 bool isCharType() const; 359 bool isWideCharType() const; 360 bool isIntegralType() const; 361 362 /// Floating point categories. 363 bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double) 364 /// isComplexType() does *not* include complex integers (a GCC extension). 365 /// isComplexIntegerType() can be used to test for complex integers. 366 bool isComplexType() const; // C99 6.2.5p11 (complex) 367 bool isAnyComplexType() const; // C99 6.2.5p11 (complex) + Complex Int. 368 bool isFloatingType() const; // C99 6.2.5p11 (real floating + complex) 369 bool isRealType() const; // C99 6.2.5p17 (real floating + integer) 370 bool isArithmeticType() const; // C99 6.2.5p18 (integer + floating) 371 bool isVoidType() const; // C99 6.2.5p19 372 bool isDerivedType() const; // C99 6.2.5p20 373 bool isScalarType() const; // C99 6.2.5p21 (arithmetic + pointers) 374 bool isAggregateType() const; 375 376 // Type Predicates: Check to see if this type is structurally the specified 377 // type, ignoring typedefs and qualifiers. 378 bool isFunctionType() const; 379 bool isFunctionNoProtoType() const { return getAsFunctionNoProtoType() != 0; } 380 bool isFunctionProtoType() const { return getAsFunctionProtoType() != 0; } 381 bool isPointerType() const; 382 bool isAnyPointerType() const; // Any C pointer or ObjC object pointer 383 bool isBlockPointerType() const; 384 bool isVoidPointerType() const; 385 bool isReferenceType() const; 386 bool isLValueReferenceType() const; 387 bool isRValueReferenceType() const; 388 bool isFunctionPointerType() const; 389 bool isMemberPointerType() const; 390 bool isMemberFunctionPointerType() const; 391 bool isArrayType() const; 392 bool isConstantArrayType() const; 393 bool isIncompleteArrayType() const; 394 bool isVariableArrayType() const; 395 bool isDependentSizedArrayType() const; 396 bool isRecordType() const; 397 bool isClassType() const; 398 bool isStructureType() const; 399 bool isUnionType() const; 400 bool isComplexIntegerType() const; // GCC _Complex integer type. 401 bool isVectorType() const; // GCC vector type. 402 bool isExtVectorType() const; // Extended vector type. 403 bool isObjCObjectPointerType() const; // Pointer to *any* ObjC object. 404 // FIXME: change this to 'raw' interface type, so we can used 'interface' type 405 // for the common case. 406 bool isObjCInterfaceType() const; // NSString or NSString<foo> 407 bool isObjCQualifiedInterfaceType() const; // NSString<foo> 408 bool isObjCQualifiedIdType() const; // id<foo> 409 bool isObjCQualifiedClassType() const; // Class<foo> 410 bool isObjCIdType() const; // id 411 bool isObjCClassType() const; // Class 412 bool isObjCBuiltinType() const; // 'id' or 'Class' 413 bool isTemplateTypeParmType() const; // C++ template type parameter 414 bool isNullPtrType() const; // C++0x nullptr_t 415 416 /// isDependentType - Whether this type is a dependent type, meaning 417 /// that its definition somehow depends on a template parameter 418 /// (C++ [temp.dep.type]). 419 bool isDependentType() const { return Dependent; } 420 bool isOverloadableType() const; 421 422 /// hasPointerRepresentation - Whether this type is represented 423 /// natively as a pointer; this includes pointers, references, block 424 /// pointers, and Objective-C interface, qualified id, and qualified 425 /// interface types, as well as nullptr_t. 426 bool hasPointerRepresentation() const; 427 428 /// hasObjCPointerRepresentation - Whether this type can represent 429 /// an objective pointer type for the purpose of GC'ability 430 bool hasObjCPointerRepresentation() const; 431 432 // Type Checking Functions: Check to see if this type is structurally the 433 // specified type, ignoring typedefs and qualifiers, and return a pointer to 434 // the best type we can. 435 const BuiltinType *getAsBuiltinType() const; 436 const FunctionType *getAsFunctionType() const; 437 const FunctionNoProtoType *getAsFunctionNoProtoType() const; 438 const FunctionProtoType *getAsFunctionProtoType() const; 439 const PointerType *getAsPointerType() const; 440 const BlockPointerType *getAsBlockPointerType() const; 441 const ReferenceType *getAsReferenceType() const; 442 const LValueReferenceType *getAsLValueReferenceType() const; 443 const RValueReferenceType *getAsRValueReferenceType() const; 444 const MemberPointerType *getAsMemberPointerType() const; 445 const TagType *getAsTagType() const; 446 const RecordType *getAsRecordType() const; 447 const RecordType *getAsStructureType() const; 448 /// NOTE: getAs*ArrayType are methods on ASTContext. 449 const TypedefType *getAsTypedefType() const; 450 const RecordType *getAsUnionType() const; 451 const EnumType *getAsEnumType() const; 452 const VectorType *getAsVectorType() const; // GCC vector type. 453 const ComplexType *getAsComplexType() const; 454 const ComplexType *getAsComplexIntegerType() const; // GCC complex int type. 455 const ExtVectorType *getAsExtVectorType() const; // Extended vector type. 456 const ObjCObjectPointerType *getAsObjCObjectPointerType() const; 457 // The following is a convenience method that returns an ObjCObjectPointerType 458 // for object declared using an interface. 459 const ObjCObjectPointerType *getAsObjCInterfacePointerType() const; 460 const ObjCObjectPointerType *getAsObjCQualifiedIdType() const; 461 const ObjCInterfaceType *getAsObjCInterfaceType() const; 462 const ObjCInterfaceType *getAsObjCQualifiedInterfaceType() const; 463 const TemplateTypeParmType *getAsTemplateTypeParmType() const; 464 465 // Member-template getAs<specific type>'. This scheme will eventually 466 // replace the specific getAsXXXX methods above. 467 template <typename T> const T *getAs() const; 468 469 const TemplateSpecializationType * 470 getAsTemplateSpecializationType() const; 471 472 /// getAsPointerToObjCInterfaceType - If this is a pointer to an ObjC 473 /// interface, return the interface type, otherwise return null. 474 const ObjCInterfaceType *getAsPointerToObjCInterfaceType() const; 475 476 /// getArrayElementTypeNoTypeQual - If this is an array type, return the 477 /// element type of the array, potentially with type qualifiers missing. 478 /// This method should never be used when type qualifiers are meaningful. 479 const Type *getArrayElementTypeNoTypeQual() const; 480 481 /// getPointeeType - If this is a pointer, ObjC object pointer, or block 482 /// pointer, this returns the respective pointee. 483 QualType getPointeeType() const; 484 485 /// getDesugaredType - Return the specified type with any "sugar" removed from 486 /// the type. This takes off typedefs, typeof's etc. If the outer level of 487 /// the type is already concrete, it returns it unmodified. This is similar 488 /// to getting the canonical type, but it doesn't remove *all* typedefs. For 489 /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is 490 /// concrete. 491 QualType getDesugaredType(bool ForDisplay = false) const; 492 493 /// More type predicates useful for type checking/promotion 494 bool isPromotableIntegerType() const; // C99 6.3.1.1p2 495 496 /// isSignedIntegerType - Return true if this is an integer type that is 497 /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], 498 /// an enum decl which has a signed representation, or a vector of signed 499 /// integer element type. 500 bool isSignedIntegerType() const; 501 502 /// isUnsignedIntegerType - Return true if this is an integer type that is 503 /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum 504 /// decl which has an unsigned representation, or a vector of unsigned integer 505 /// element type. 506 bool isUnsignedIntegerType() const; 507 508 /// isConstantSizeType - Return true if this is not a variable sized type, 509 /// according to the rules of C99 6.7.5p3. It is not legal to call this on 510 /// incomplete types. 511 bool isConstantSizeType() const; 512 513 /// isSpecifierType - Returns true if this type can be represented by some 514 /// set of type specifiers. 515 bool isSpecifierType() const; 516 517 QualType getCanonicalTypeInternal() const { return CanonicalType; } 518 void dump() const; 519 virtual void getAsStringInternal(std::string &InnerString, 520 const PrintingPolicy &Policy) const = 0; 521 static bool classof(const Type *) { return true; } 522}; 523 524/// ExtQualType - TR18037 (C embedded extensions) 6.2.5p26 525/// This supports all kinds of type attributes; including, 526/// address space qualified types, objective-c's __weak and 527/// __strong attributes. 528/// 529class ExtQualType : public Type, public llvm::FoldingSetNode { 530 /// BaseType - This is the underlying type that this qualifies. All CVR 531 /// qualifiers are stored on the QualType that references this type, so we 532 /// can't have any here. 533 Type *BaseType; 534 535 /// Address Space ID - The address space ID this type is qualified with. 536 unsigned AddressSpace; 537 /// GC __weak/__strong attributes 538 QualType::GCAttrTypes GCAttrType; 539 540 ExtQualType(Type *Base, QualType CanonicalPtr, unsigned AddrSpace, 541 QualType::GCAttrTypes gcAttr) : 542 Type(ExtQual, CanonicalPtr, Base->isDependentType()), BaseType(Base), 543 AddressSpace(AddrSpace), GCAttrType(gcAttr) { 544 assert(!isa<ExtQualType>(BaseType) && 545 "Cannot have ExtQualType of ExtQualType"); 546 } 547 friend class ASTContext; // ASTContext creates these. 548public: 549 Type *getBaseType() const { return BaseType; } 550 QualType::GCAttrTypes getObjCGCAttr() const { return GCAttrType; } 551 unsigned getAddressSpace() const { return AddressSpace; } 552 553 virtual void getAsStringInternal(std::string &InnerString, 554 const PrintingPolicy &Policy) const; 555 556 void Profile(llvm::FoldingSetNodeID &ID) { 557 Profile(ID, getBaseType(), AddressSpace, GCAttrType); 558 } 559 static void Profile(llvm::FoldingSetNodeID &ID, Type *Base, 560 unsigned AddrSpace, QualType::GCAttrTypes gcAttr) { 561 ID.AddPointer(Base); 562 ID.AddInteger(AddrSpace); 563 ID.AddInteger(gcAttr); 564 } 565 566 static bool classof(const Type *T) { return T->getTypeClass() == ExtQual; } 567 static bool classof(const ExtQualType *) { return true; } 568}; 569 570 571/// QualifierSet - This class is used to collect qualifiers. 572class QualifierSet { 573public: 574 QualifierSet() : 575 CVRMask(0), AddressSpace(0), GCAttrType(QualType::GCNone) { 576 } 577 578 void removeConst() { removeCVR(QualType::Const); } 579 void removeVolatile() { removeCVR(QualType::Volatile); } 580 void removeRestrict() { removeCVR(QualType::Restrict); } 581 void removeCVR(unsigned mask) { CVRMask &= ~mask; } 582 void removeAddressSpace() { AddressSpace = 0; } 583 void removeGCAttrType() { GCAttrType = QualType::GCNone; } 584 585 void addConst() { addCVR(QualType::Const); } 586 void addVolatile() { addCVR(QualType::Volatile); } 587 void addRestrict() { addCVR(QualType::Restrict); } 588 void addCVR(unsigned mask) { CVRMask |= mask; } 589 void addAddressSpace(unsigned space) { 590 assert(space); 591 AddressSpace = space; 592 } 593 void addGCAttrType(QualType::GCAttrTypes type) { 594 assert(type); 595 GCAttrType = type; 596 } 597 598 bool empty() { 599 return !CVRMask && !AddressSpace && !GCAttrType; 600 } 601 602 /// Collect any qualifiers on the given type and return an 603 /// unqualified type. 604 const Type *strip(QualType QT) { 605 CVRMask |= QT.getCVRQualifiers(); 606 return strip(QT.getTypePtr()); 607 } 608 609 /// Collect any qualifiers on the given type and return an 610 /// unqualified type. 611 const Type *strip(const Type* T); 612 613 /// Apply the collected qualifiers to the given type. 614 QualType apply(QualType QT, ASTContext& C); 615 616 /// Apply the collected qualifiers to the given type. 617 QualType apply(const Type* T, ASTContext& C) { 618 return apply(QualType(T, 0), C); 619 } 620 621private: 622 unsigned CVRMask; 623 unsigned AddressSpace; 624 QualType::GCAttrTypes GCAttrType; 625}; 626 627 628/// BuiltinType - This class is used for builtin types like 'int'. Builtin 629/// types are always canonical and have a literal name field. 630class BuiltinType : public Type { 631public: 632 enum Kind { 633 Void, 634 635 Bool, // This is bool and/or _Bool. 636 Char_U, // This is 'char' for targets where char is unsigned. 637 UChar, // This is explicitly qualified unsigned char. 638 Char16, // This is 'char16_t' for C++. 639 Char32, // This is 'char32_t' for C++. 640 UShort, 641 UInt, 642 ULong, 643 ULongLong, 644 UInt128, // __uint128_t 645 646 Char_S, // This is 'char' for targets where char is signed. 647 SChar, // This is explicitly qualified signed char. 648 WChar, // This is 'wchar_t' for C++. 649 Short, 650 Int, 651 Long, 652 LongLong, 653 Int128, // __int128_t 654 655 Float, Double, LongDouble, 656 657 NullPtr, // This is the type of C++0x 'nullptr'. 658 659 Overload, // This represents the type of an overloaded function declaration. 660 Dependent, // This represents the type of a type-dependent expression. 661 662 UndeducedAuto, // In C++0x, this represents the type of an auto variable 663 // that has not been deduced yet. 664 ObjCId, // This represents the ObjC 'id' type. 665 ObjCClass // This represents the ObjC 'Class' type. 666 }; 667private: 668 Kind TypeKind; 669public: 670 BuiltinType(Kind K) 671 : Type(Builtin, QualType(), /*Dependent=*/(K == Dependent)), 672 TypeKind(K) {} 673 674 Kind getKind() const { return TypeKind; } 675 const char *getName(const LangOptions &LO) const; 676 677 virtual void getAsStringInternal(std::string &InnerString, 678 const PrintingPolicy &Policy) const; 679 680 static bool classof(const Type *T) { return T->getTypeClass() == Builtin; } 681 static bool classof(const BuiltinType *) { return true; } 682}; 683 684/// FixedWidthIntType - Used for arbitrary width types that we either don't 685/// want to or can't map to named integer types. These always have a lower 686/// integer rank than builtin types of the same width. 687class FixedWidthIntType : public Type { 688private: 689 unsigned Width; 690 bool Signed; 691public: 692 FixedWidthIntType(unsigned W, bool S) : Type(FixedWidthInt, QualType(), false), 693 Width(W), Signed(S) {} 694 695 unsigned getWidth() const { return Width; } 696 bool isSigned() const { return Signed; } 697 const char *getName() const; 698 699 virtual void getAsStringInternal(std::string &InnerString, 700 const PrintingPolicy &Policy) const; 701 702 static bool classof(const Type *T) { return T->getTypeClass() == FixedWidthInt; } 703 static bool classof(const FixedWidthIntType *) { return true; } 704}; 705 706/// ComplexType - C99 6.2.5p11 - Complex values. This supports the C99 complex 707/// types (_Complex float etc) as well as the GCC integer complex extensions. 708/// 709class ComplexType : public Type, public llvm::FoldingSetNode { 710 QualType ElementType; 711 ComplexType(QualType Element, QualType CanonicalPtr) : 712 Type(Complex, CanonicalPtr, Element->isDependentType()), 713 ElementType(Element) { 714 } 715 friend class ASTContext; // ASTContext creates these. 716public: 717 QualType getElementType() const { return ElementType; } 718 719 virtual void getAsStringInternal(std::string &InnerString, 720 const PrintingPolicy &Policy) const; 721 722 void Profile(llvm::FoldingSetNodeID &ID) { 723 Profile(ID, getElementType()); 724 } 725 static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) { 726 ID.AddPointer(Element.getAsOpaquePtr()); 727 } 728 729 static bool classof(const Type *T) { return T->getTypeClass() == Complex; } 730 static bool classof(const ComplexType *) { return true; } 731}; 732 733/// PointerType - C99 6.7.5.1 - Pointer Declarators. 734/// 735class PointerType : public Type, public llvm::FoldingSetNode { 736 QualType PointeeType; 737 738 PointerType(QualType Pointee, QualType CanonicalPtr) : 739 Type(Pointer, CanonicalPtr, Pointee->isDependentType()), PointeeType(Pointee) { 740 } 741 friend class ASTContext; // ASTContext creates these. 742public: 743 744 virtual void getAsStringInternal(std::string &InnerString, 745 const PrintingPolicy &Policy) const; 746 747 QualType getPointeeType() const { return PointeeType; } 748 749 void Profile(llvm::FoldingSetNodeID &ID) { 750 Profile(ID, getPointeeType()); 751 } 752 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { 753 ID.AddPointer(Pointee.getAsOpaquePtr()); 754 } 755 756 static bool classof(const Type *T) { return T->getTypeClass() == Pointer; } 757 static bool classof(const PointerType *) { return true; } 758}; 759 760/// BlockPointerType - pointer to a block type. 761/// This type is to represent types syntactically represented as 762/// "void (^)(int)", etc. Pointee is required to always be a function type. 763/// 764class BlockPointerType : public Type, public llvm::FoldingSetNode { 765 QualType PointeeType; // Block is some kind of pointer type 766 BlockPointerType(QualType Pointee, QualType CanonicalCls) : 767 Type(BlockPointer, CanonicalCls, Pointee->isDependentType()), 768 PointeeType(Pointee) { 769 } 770 friend class ASTContext; // ASTContext creates these. 771public: 772 773 // Get the pointee type. Pointee is required to always be a function type. 774 QualType getPointeeType() const { return PointeeType; } 775 776 virtual void getAsStringInternal(std::string &InnerString, 777 const PrintingPolicy &Policy) const; 778 779 void Profile(llvm::FoldingSetNodeID &ID) { 780 Profile(ID, getPointeeType()); 781 } 782 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { 783 ID.AddPointer(Pointee.getAsOpaquePtr()); 784 } 785 786 static bool classof(const Type *T) { 787 return T->getTypeClass() == BlockPointer; 788 } 789 static bool classof(const BlockPointerType *) { return true; } 790}; 791 792/// ReferenceType - Base for LValueReferenceType and RValueReferenceType 793/// 794class ReferenceType : public Type, public llvm::FoldingSetNode { 795 QualType PointeeType; 796 797protected: 798 ReferenceType(TypeClass tc, QualType Referencee, QualType CanonicalRef) : 799 Type(tc, CanonicalRef, Referencee->isDependentType()), 800 PointeeType(Referencee) { 801 } 802public: 803 QualType getPointeeType() const { return PointeeType; } 804 805 void Profile(llvm::FoldingSetNodeID &ID) { 806 Profile(ID, getPointeeType()); 807 } 808 static void Profile(llvm::FoldingSetNodeID &ID, QualType Referencee) { 809 ID.AddPointer(Referencee.getAsOpaquePtr()); 810 } 811 812 static bool classof(const Type *T) { 813 return T->getTypeClass() == LValueReference || 814 T->getTypeClass() == RValueReference; 815 } 816 static bool classof(const ReferenceType *) { return true; } 817}; 818 819/// LValueReferenceType - C++ [dcl.ref] - Lvalue reference 820/// 821class LValueReferenceType : public ReferenceType { 822 LValueReferenceType(QualType Referencee, QualType CanonicalRef) : 823 ReferenceType(LValueReference, Referencee, CanonicalRef) { 824 } 825 friend class ASTContext; // ASTContext creates these 826public: 827 virtual void getAsStringInternal(std::string &InnerString, 828 const PrintingPolicy &Policy) const; 829 830 static bool classof(const Type *T) { 831 return T->getTypeClass() == LValueReference; 832 } 833 static bool classof(const LValueReferenceType *) { return true; } 834}; 835 836/// RValueReferenceType - C++0x [dcl.ref] - Rvalue reference 837/// 838class RValueReferenceType : public ReferenceType { 839 RValueReferenceType(QualType Referencee, QualType CanonicalRef) : 840 ReferenceType(RValueReference, Referencee, CanonicalRef) { 841 } 842 friend class ASTContext; // ASTContext creates these 843public: 844 virtual void getAsStringInternal(std::string &InnerString, 845 const PrintingPolicy &Policy) const; 846 847 static bool classof(const Type *T) { 848 return T->getTypeClass() == RValueReference; 849 } 850 static bool classof(const RValueReferenceType *) { return true; } 851}; 852 853/// MemberPointerType - C++ 8.3.3 - Pointers to members 854/// 855class MemberPointerType : public Type, public llvm::FoldingSetNode { 856 QualType PointeeType; 857 /// The class of which the pointee is a member. Must ultimately be a 858 /// RecordType, but could be a typedef or a template parameter too. 859 const Type *Class; 860 861 MemberPointerType(QualType Pointee, const Type *Cls, QualType CanonicalPtr) : 862 Type(MemberPointer, CanonicalPtr, 863 Cls->isDependentType() || Pointee->isDependentType()), 864 PointeeType(Pointee), Class(Cls) { 865 } 866 friend class ASTContext; // ASTContext creates these. 867public: 868 869 QualType getPointeeType() const { return PointeeType; } 870 871 const Type *getClass() const { return Class; } 872 873 virtual void getAsStringInternal(std::string &InnerString, 874 const PrintingPolicy &Policy) const; 875 876 void Profile(llvm::FoldingSetNodeID &ID) { 877 Profile(ID, getPointeeType(), getClass()); 878 } 879 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee, 880 const Type *Class) { 881 ID.AddPointer(Pointee.getAsOpaquePtr()); 882 ID.AddPointer(Class); 883 } 884 885 static bool classof(const Type *T) { 886 return T->getTypeClass() == MemberPointer; 887 } 888 static bool classof(const MemberPointerType *) { return true; } 889}; 890 891/// ArrayType - C99 6.7.5.2 - Array Declarators. 892/// 893class ArrayType : public Type, public llvm::FoldingSetNode { 894public: 895 /// ArraySizeModifier - Capture whether this is a normal array (e.g. int X[4]) 896 /// an array with a static size (e.g. int X[static 4]), or an array 897 /// with a star size (e.g. int X[*]). 898 /// 'static' is only allowed on function parameters. 899 enum ArraySizeModifier { 900 Normal, Static, Star 901 }; 902private: 903 /// ElementType - The element type of the array. 904 QualType ElementType; 905 906 // NOTE: VC++ treats enums as signed, avoid using the ArraySizeModifier enum 907 /// NOTE: These fields are packed into the bitfields space in the Type class. 908 unsigned SizeModifier : 2; 909 910 /// IndexTypeQuals - Capture qualifiers in declarations like: 911 /// 'int X[static restrict 4]'. For function parameters only. 912 unsigned IndexTypeQuals : 3; 913 914protected: 915 // C++ [temp.dep.type]p1: 916 // A type is dependent if it is... 917 // - an array type constructed from any dependent type or whose 918 // size is specified by a constant expression that is 919 // value-dependent, 920 ArrayType(TypeClass tc, QualType et, QualType can, 921 ArraySizeModifier sm, unsigned tq) 922 : Type(tc, can, et->isDependentType() || tc == DependentSizedArray), 923 ElementType(et), SizeModifier(sm), IndexTypeQuals(tq) {} 924 925 friend class ASTContext; // ASTContext creates these. 926public: 927 QualType getElementType() const { return ElementType; } 928 ArraySizeModifier getSizeModifier() const { 929 return ArraySizeModifier(SizeModifier); 930 } 931 unsigned getIndexTypeQualifier() const { return IndexTypeQuals; } 932 933 static bool classof(const Type *T) { 934 return T->getTypeClass() == ConstantArray || 935 T->getTypeClass() == ConstantArrayWithExpr || 936 T->getTypeClass() == ConstantArrayWithoutExpr || 937 T->getTypeClass() == VariableArray || 938 T->getTypeClass() == IncompleteArray || 939 T->getTypeClass() == DependentSizedArray; 940 } 941 static bool classof(const ArrayType *) { return true; } 942}; 943 944/// ConstantArrayType - This class represents the canonical version of 945/// C arrays with a specified constant size. For example, the canonical 946/// type for 'int A[4 + 4*100]' is a ConstantArrayType where the element 947/// type is 'int' and the size is 404. 948class ConstantArrayType : public ArrayType { 949 llvm::APInt Size; // Allows us to unique the type. 950 951 ConstantArrayType(QualType et, QualType can, const llvm::APInt &size, 952 ArraySizeModifier sm, unsigned tq) 953 : ArrayType(ConstantArray, et, can, sm, tq), 954 Size(size) {} 955protected: 956 ConstantArrayType(TypeClass tc, QualType et, QualType can, 957 const llvm::APInt &size, ArraySizeModifier sm, unsigned tq) 958 : ArrayType(tc, et, can, sm, tq), Size(size) {} 959 friend class ASTContext; // ASTContext creates these. 960public: 961 const llvm::APInt &getSize() const { return Size; } 962 virtual void getAsStringInternal(std::string &InnerString, 963 const PrintingPolicy &Policy) const; 964 965 void Profile(llvm::FoldingSetNodeID &ID) { 966 Profile(ID, getElementType(), getSize(), 967 getSizeModifier(), getIndexTypeQualifier()); 968 } 969 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, 970 const llvm::APInt &ArraySize, ArraySizeModifier SizeMod, 971 unsigned TypeQuals) { 972 ID.AddPointer(ET.getAsOpaquePtr()); 973 ID.AddInteger(ArraySize.getZExtValue()); 974 ID.AddInteger(SizeMod); 975 ID.AddInteger(TypeQuals); 976 } 977 static bool classof(const Type *T) { 978 return T->getTypeClass() == ConstantArray || 979 T->getTypeClass() == ConstantArrayWithExpr || 980 T->getTypeClass() == ConstantArrayWithoutExpr; 981 } 982 static bool classof(const ConstantArrayType *) { return true; } 983}; 984 985/// ConstantArrayWithExprType - This class represents C arrays with a 986/// constant size specified by means of an integer constant expression. 987/// For example 'int A[sizeof(int)]' has ConstantArrayWithExprType where 988/// the element type is 'int' and the size expression is 'sizeof(int)'. 989/// These types are non-canonical. 990class ConstantArrayWithExprType : public ConstantArrayType { 991 /// SizeExpr - The ICE occurring in the concrete syntax. 992 Expr *SizeExpr; 993 /// Brackets - The left and right array brackets. 994 SourceRange Brackets; 995 996 ConstantArrayWithExprType(QualType et, QualType can, 997 const llvm::APInt &size, Expr *e, 998 ArraySizeModifier sm, unsigned tq, 999 SourceRange brackets) 1000 : ConstantArrayType(ConstantArrayWithExpr, et, can, size, sm, tq), 1001 SizeExpr(e), Brackets(brackets) {} 1002 friend class ASTContext; // ASTContext creates these. 1003 virtual void Destroy(ASTContext& C); 1004 1005public: 1006 Expr *getSizeExpr() const { return SizeExpr; } 1007 SourceRange getBracketsRange() const { return Brackets; } 1008 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } 1009 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } 1010 1011 virtual void getAsStringInternal(std::string &InnerString, 1012 const PrintingPolicy &Policy) const; 1013 1014 static bool classof(const Type *T) { 1015 return T->getTypeClass() == ConstantArrayWithExpr; 1016 } 1017 static bool classof(const ConstantArrayWithExprType *) { return true; } 1018 1019 void Profile(llvm::FoldingSetNodeID &ID) { 1020 assert(0 && "Cannot unique ConstantArrayWithExprTypes."); 1021 } 1022}; 1023 1024/// ConstantArrayWithoutExprType - This class represents C arrays with a 1025/// constant size that was not specified by an integer constant expression, 1026/// but inferred by static semantics. 1027/// For example 'int A[] = { 0, 1, 2 }' has ConstantArrayWithoutExprType. 1028/// These types are non-canonical: the corresponding canonical type, 1029/// having the size specified in an APInt object, is a ConstantArrayType. 1030class ConstantArrayWithoutExprType : public ConstantArrayType { 1031 1032 ConstantArrayWithoutExprType(QualType et, QualType can, 1033 const llvm::APInt &size, 1034 ArraySizeModifier sm, unsigned tq) 1035 : ConstantArrayType(ConstantArrayWithoutExpr, et, can, size, sm, tq) {} 1036 friend class ASTContext; // ASTContext creates these. 1037 1038public: 1039 virtual void getAsStringInternal(std::string &InnerString, 1040 const PrintingPolicy &Policy) const; 1041 1042 static bool classof(const Type *T) { 1043 return T->getTypeClass() == ConstantArrayWithoutExpr; 1044 } 1045 static bool classof(const ConstantArrayWithoutExprType *) { return true; } 1046 1047 void Profile(llvm::FoldingSetNodeID &ID) { 1048 assert(0 && "Cannot unique ConstantArrayWithoutExprTypes."); 1049 } 1050}; 1051 1052/// IncompleteArrayType - This class represents C arrays with an unspecified 1053/// size. For example 'int A[]' has an IncompleteArrayType where the element 1054/// type is 'int' and the size is unspecified. 1055class IncompleteArrayType : public ArrayType { 1056 1057 IncompleteArrayType(QualType et, QualType can, 1058 ArraySizeModifier sm, unsigned tq) 1059 : ArrayType(IncompleteArray, et, can, sm, tq) {} 1060 friend class ASTContext; // ASTContext creates these. 1061public: 1062 virtual void getAsStringInternal(std::string &InnerString, 1063 const PrintingPolicy &Policy) const; 1064 1065 static bool classof(const Type *T) { 1066 return T->getTypeClass() == IncompleteArray; 1067 } 1068 static bool classof(const IncompleteArrayType *) { return true; } 1069 1070 friend class StmtIteratorBase; 1071 1072 void Profile(llvm::FoldingSetNodeID &ID) { 1073 Profile(ID, getElementType(), getSizeModifier(), getIndexTypeQualifier()); 1074 } 1075 1076 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, 1077 ArraySizeModifier SizeMod, unsigned TypeQuals) { 1078 ID.AddPointer(ET.getAsOpaquePtr()); 1079 ID.AddInteger(SizeMod); 1080 ID.AddInteger(TypeQuals); 1081 } 1082}; 1083 1084/// VariableArrayType - This class represents C arrays with a specified size 1085/// which is not an integer-constant-expression. For example, 'int s[x+foo()]'. 1086/// Since the size expression is an arbitrary expression, we store it as such. 1087/// 1088/// Note: VariableArrayType's aren't uniqued (since the expressions aren't) and 1089/// should not be: two lexically equivalent variable array types could mean 1090/// different things, for example, these variables do not have the same type 1091/// dynamically: 1092/// 1093/// void foo(int x) { 1094/// int Y[x]; 1095/// ++x; 1096/// int Z[x]; 1097/// } 1098/// 1099class VariableArrayType : public ArrayType { 1100 /// SizeExpr - An assignment expression. VLA's are only permitted within 1101 /// a function block. 1102 Stmt *SizeExpr; 1103 /// Brackets - The left and right array brackets. 1104 SourceRange Brackets; 1105 1106 VariableArrayType(QualType et, QualType can, Expr *e, 1107 ArraySizeModifier sm, unsigned tq, 1108 SourceRange brackets) 1109 : ArrayType(VariableArray, et, can, sm, tq), 1110 SizeExpr((Stmt*) e), Brackets(brackets) {} 1111 friend class ASTContext; // ASTContext creates these. 1112 virtual void Destroy(ASTContext& C); 1113 1114public: 1115 Expr *getSizeExpr() const { 1116 // We use C-style casts instead of cast<> here because we do not wish 1117 // to have a dependency of Type.h on Stmt.h/Expr.h. 1118 return (Expr*) SizeExpr; 1119 } 1120 SourceRange getBracketsRange() const { return Brackets; } 1121 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } 1122 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } 1123 1124 virtual void getAsStringInternal(std::string &InnerString, 1125 const PrintingPolicy &Policy) const; 1126 1127 static bool classof(const Type *T) { 1128 return T->getTypeClass() == VariableArray; 1129 } 1130 static bool classof(const VariableArrayType *) { return true; } 1131 1132 friend class StmtIteratorBase; 1133 1134 void Profile(llvm::FoldingSetNodeID &ID) { 1135 assert(0 && "Cannnot unique VariableArrayTypes."); 1136 } 1137}; 1138 1139/// DependentSizedArrayType - This type represents an array type in 1140/// C++ whose size is a value-dependent expression. For example: 1141/// @code 1142/// template<typename T, int Size> 1143/// class array { 1144/// T data[Size]; 1145/// }; 1146/// @endcode 1147/// For these types, we won't actually know what the array bound is 1148/// until template instantiation occurs, at which point this will 1149/// become either a ConstantArrayType or a VariableArrayType. 1150class DependentSizedArrayType : public ArrayType { 1151 /// SizeExpr - An assignment expression that will instantiate to the 1152 /// size of the array. 1153 Stmt *SizeExpr; 1154 /// Brackets - The left and right array brackets. 1155 SourceRange Brackets; 1156 1157 DependentSizedArrayType(QualType et, QualType can, Expr *e, 1158 ArraySizeModifier sm, unsigned tq, 1159 SourceRange brackets) 1160 : ArrayType(DependentSizedArray, et, can, sm, tq), 1161 SizeExpr((Stmt*) e), Brackets(brackets) {} 1162 friend class ASTContext; // ASTContext creates these. 1163 virtual void Destroy(ASTContext& C); 1164 1165public: 1166 Expr *getSizeExpr() const { 1167 // We use C-style casts instead of cast<> here because we do not wish 1168 // to have a dependency of Type.h on Stmt.h/Expr.h. 1169 return (Expr*) SizeExpr; 1170 } 1171 SourceRange getBracketsRange() const { return Brackets; } 1172 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } 1173 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } 1174 1175 virtual void getAsStringInternal(std::string &InnerString, 1176 const PrintingPolicy &Policy) const; 1177 1178 static bool classof(const Type *T) { 1179 return T->getTypeClass() == DependentSizedArray; 1180 } 1181 static bool classof(const DependentSizedArrayType *) { return true; } 1182 1183 friend class StmtIteratorBase; 1184 1185 void Profile(llvm::FoldingSetNodeID &ID) { 1186 assert(0 && "Cannnot unique DependentSizedArrayTypes."); 1187 } 1188}; 1189 1190/// DependentSizedExtVectorType - This type represent an extended vector type 1191/// where either the type or size is dependent. For example: 1192/// @code 1193/// template<typename T, int Size> 1194/// class vector { 1195/// typedef T __attribute__((ext_vector_type(Size))) type; 1196/// } 1197/// @endcode 1198class DependentSizedExtVectorType : public Type { 1199 Expr *SizeExpr; 1200 /// ElementType - The element type of the array. 1201 QualType ElementType; 1202 SourceLocation loc; 1203 1204 DependentSizedExtVectorType(QualType ElementType, QualType can, 1205 Expr *SizeExpr, SourceLocation loc) 1206 : Type (DependentSizedExtVector, can, true), 1207 SizeExpr(SizeExpr), ElementType(ElementType), loc(loc) {} 1208 friend class ASTContext; 1209 virtual void Destroy(ASTContext& C); 1210 1211public: 1212 const Expr *getSizeExpr() const { return SizeExpr; } 1213 QualType getElementType() const { return ElementType; } 1214 SourceLocation getAttributeLoc() const { return loc; } 1215 1216 virtual void getAsStringInternal(std::string &InnerString, 1217 const PrintingPolicy &Policy) const; 1218 1219 static bool classof(const Type *T) { 1220 return T->getTypeClass() == DependentSizedExtVector; 1221 } 1222 static bool classof(const DependentSizedExtVectorType *) { return true; } 1223}; 1224 1225 1226/// VectorType - GCC generic vector type. This type is created using 1227/// __attribute__((vector_size(n)), where "n" specifies the vector size in 1228/// bytes. Since the constructor takes the number of vector elements, the 1229/// client is responsible for converting the size into the number of elements. 1230class VectorType : public Type, public llvm::FoldingSetNode { 1231protected: 1232 /// ElementType - The element type of the vector. 1233 QualType ElementType; 1234 1235 /// NumElements - The number of elements in the vector. 1236 unsigned NumElements; 1237 1238 VectorType(QualType vecType, unsigned nElements, QualType canonType) : 1239 Type(Vector, canonType, vecType->isDependentType()), 1240 ElementType(vecType), NumElements(nElements) {} 1241 VectorType(TypeClass tc, QualType vecType, unsigned nElements, 1242 QualType canonType) 1243 : Type(tc, canonType, vecType->isDependentType()), ElementType(vecType), 1244 NumElements(nElements) {} 1245 friend class ASTContext; // ASTContext creates these. 1246public: 1247 1248 QualType getElementType() const { return ElementType; } 1249 unsigned getNumElements() const { return NumElements; } 1250 1251 virtual void getAsStringInternal(std::string &InnerString, 1252 const PrintingPolicy &Policy) const; 1253 1254 void Profile(llvm::FoldingSetNodeID &ID) { 1255 Profile(ID, getElementType(), getNumElements(), getTypeClass()); 1256 } 1257 static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, 1258 unsigned NumElements, TypeClass TypeClass) { 1259 ID.AddPointer(ElementType.getAsOpaquePtr()); 1260 ID.AddInteger(NumElements); 1261 ID.AddInteger(TypeClass); 1262 } 1263 static bool classof(const Type *T) { 1264 return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector; 1265 } 1266 static bool classof(const VectorType *) { return true; } 1267}; 1268 1269/// ExtVectorType - Extended vector type. This type is created using 1270/// __attribute__((ext_vector_type(n)), where "n" is the number of elements. 1271/// Unlike vector_size, ext_vector_type is only allowed on typedef's. This 1272/// class enables syntactic extensions, like Vector Components for accessing 1273/// points, colors, and textures (modeled after OpenGL Shading Language). 1274class ExtVectorType : public VectorType { 1275 ExtVectorType(QualType vecType, unsigned nElements, QualType canonType) : 1276 VectorType(ExtVector, vecType, nElements, canonType) {} 1277 friend class ASTContext; // ASTContext creates these. 1278public: 1279 static int getPointAccessorIdx(char c) { 1280 switch (c) { 1281 default: return -1; 1282 case 'x': return 0; 1283 case 'y': return 1; 1284 case 'z': return 2; 1285 case 'w': return 3; 1286 } 1287 } 1288 static int getNumericAccessorIdx(char c) { 1289 switch (c) { 1290 default: return -1; 1291 case '0': return 0; 1292 case '1': return 1; 1293 case '2': return 2; 1294 case '3': return 3; 1295 case '4': return 4; 1296 case '5': return 5; 1297 case '6': return 6; 1298 case '7': return 7; 1299 case '8': return 8; 1300 case '9': return 9; 1301 case 'A': 1302 case 'a': return 10; 1303 case 'B': 1304 case 'b': return 11; 1305 case 'C': 1306 case 'c': return 12; 1307 case 'D': 1308 case 'd': return 13; 1309 case 'E': 1310 case 'e': return 14; 1311 case 'F': 1312 case 'f': return 15; 1313 } 1314 } 1315 1316 static int getAccessorIdx(char c) { 1317 if (int idx = getPointAccessorIdx(c)+1) return idx-1; 1318 return getNumericAccessorIdx(c); 1319 } 1320 1321 bool isAccessorWithinNumElements(char c) const { 1322 if (int idx = getAccessorIdx(c)+1) 1323 return unsigned(idx-1) < NumElements; 1324 return false; 1325 } 1326 virtual void getAsStringInternal(std::string &InnerString, 1327 const PrintingPolicy &Policy) const; 1328 1329 static bool classof(const Type *T) { 1330 return T->getTypeClass() == ExtVector; 1331 } 1332 static bool classof(const ExtVectorType *) { return true; } 1333}; 1334 1335/// FunctionType - C99 6.7.5.3 - Function Declarators. This is the common base 1336/// class of FunctionNoProtoType and FunctionProtoType. 1337/// 1338class FunctionType : public Type { 1339 /// SubClassData - This field is owned by the subclass, put here to pack 1340 /// tightly with the ivars in Type. 1341 bool SubClassData : 1; 1342 1343 /// TypeQuals - Used only by FunctionProtoType, put here to pack with the 1344 /// other bitfields. 1345 /// The qualifiers are part of FunctionProtoType because... 1346 /// 1347 /// C++ 8.3.5p4: The return type, the parameter type list and the 1348 /// cv-qualifier-seq, [...], are part of the function type. 1349 /// 1350 unsigned TypeQuals : 3; 1351 1352 /// NoReturn - Indicates if the function type is attribute noreturn. 1353 unsigned NoReturn : 1; 1354 1355 // The type returned by the function. 1356 QualType ResultType; 1357protected: 1358 FunctionType(TypeClass tc, QualType res, bool SubclassInfo, 1359 unsigned typeQuals, QualType Canonical, bool Dependent, 1360 bool noReturn = false) 1361 : Type(tc, Canonical, Dependent), 1362 SubClassData(SubclassInfo), TypeQuals(typeQuals), NoReturn(noReturn), 1363 ResultType(res) {} 1364 bool getSubClassData() const { return SubClassData; } 1365 unsigned getTypeQuals() const { return TypeQuals; } 1366public: 1367 1368 QualType getResultType() const { return ResultType; } 1369 bool getNoReturnAttr() const { return NoReturn; } 1370 1371 1372 static bool classof(const Type *T) { 1373 return T->getTypeClass() == FunctionNoProto || 1374 T->getTypeClass() == FunctionProto; 1375 } 1376 static bool classof(const FunctionType *) { return true; } 1377}; 1378 1379/// FunctionNoProtoType - Represents a K&R-style 'int foo()' function, which has 1380/// no information available about its arguments. 1381class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode { 1382 FunctionNoProtoType(QualType Result, QualType Canonical, 1383 bool NoReturn = false) 1384 : FunctionType(FunctionNoProto, Result, false, 0, Canonical, 1385 /*Dependent=*/false, NoReturn) {} 1386 friend class ASTContext; // ASTContext creates these. 1387public: 1388 // No additional state past what FunctionType provides. 1389 1390 virtual void getAsStringInternal(std::string &InnerString, 1391 const PrintingPolicy &Policy) const; 1392 1393 void Profile(llvm::FoldingSetNodeID &ID) { 1394 Profile(ID, getResultType(), getNoReturnAttr()); 1395 } 1396 static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType, 1397 bool NoReturn) { 1398 ID.AddInteger(NoReturn); 1399 ID.AddPointer(ResultType.getAsOpaquePtr()); 1400 } 1401 1402 static bool classof(const Type *T) { 1403 return T->getTypeClass() == FunctionNoProto; 1404 } 1405 static bool classof(const FunctionNoProtoType *) { return true; } 1406}; 1407 1408/// FunctionProtoType - Represents a prototype with argument type info, e.g. 1409/// 'int foo(int)' or 'int foo(void)'. 'void' is represented as having no 1410/// arguments, not as having a single void argument. Such a type can have an 1411/// exception specification, but this specification is not part of the canonical 1412/// type. 1413class FunctionProtoType : public FunctionType, public llvm::FoldingSetNode { 1414 /// hasAnyDependentType - Determine whether there are any dependent 1415 /// types within the arguments passed in. 1416 static bool hasAnyDependentType(const QualType *ArgArray, unsigned numArgs) { 1417 for (unsigned Idx = 0; Idx < numArgs; ++Idx) 1418 if (ArgArray[Idx]->isDependentType()) 1419 return true; 1420 1421 return false; 1422 } 1423 1424 FunctionProtoType(QualType Result, const QualType *ArgArray, unsigned numArgs, 1425 bool isVariadic, unsigned typeQuals, bool hasExs, 1426 bool hasAnyExs, const QualType *ExArray, 1427 unsigned numExs, QualType Canonical, bool NoReturn) 1428 : FunctionType(FunctionProto, Result, isVariadic, typeQuals, Canonical, 1429 (Result->isDependentType() || 1430 hasAnyDependentType(ArgArray, numArgs)), NoReturn), 1431 NumArgs(numArgs), NumExceptions(numExs), HasExceptionSpec(hasExs), 1432 AnyExceptionSpec(hasAnyExs) { 1433 // Fill in the trailing argument array. 1434 QualType *ArgInfo = reinterpret_cast<QualType*>(this+1); 1435 for (unsigned i = 0; i != numArgs; ++i) 1436 ArgInfo[i] = ArgArray[i]; 1437 // Fill in the exception array. 1438 QualType *Ex = ArgInfo + numArgs; 1439 for (unsigned i = 0; i != numExs; ++i) 1440 Ex[i] = ExArray[i]; 1441 } 1442 1443 /// NumArgs - The number of arguments this function has, not counting '...'. 1444 unsigned NumArgs : 20; 1445 1446 /// NumExceptions - The number of types in the exception spec, if any. 1447 unsigned NumExceptions : 10; 1448 1449 /// HasExceptionSpec - Whether this function has an exception spec at all. 1450 bool HasExceptionSpec : 1; 1451 1452 /// AnyExceptionSpec - Whether this function has a throw(...) spec. 1453 bool AnyExceptionSpec : 1; 1454 1455 /// ArgInfo - There is an variable size array after the class in memory that 1456 /// holds the argument types. 1457 1458 /// Exceptions - There is another variable size array after ArgInfo that 1459 /// holds the exception types. 1460 1461 friend class ASTContext; // ASTContext creates these. 1462 1463public: 1464 unsigned getNumArgs() const { return NumArgs; } 1465 QualType getArgType(unsigned i) const { 1466 assert(i < NumArgs && "Invalid argument number!"); 1467 return arg_type_begin()[i]; 1468 } 1469 1470 bool hasExceptionSpec() const { return HasExceptionSpec; } 1471 bool hasAnyExceptionSpec() const { return AnyExceptionSpec; } 1472 unsigned getNumExceptions() const { return NumExceptions; } 1473 QualType getExceptionType(unsigned i) const { 1474 assert(i < NumExceptions && "Invalid exception number!"); 1475 return exception_begin()[i]; 1476 } 1477 bool hasEmptyExceptionSpec() const { 1478 return hasExceptionSpec() && !hasAnyExceptionSpec() && 1479 getNumExceptions() == 0; 1480 } 1481 1482 bool isVariadic() const { return getSubClassData(); } 1483 unsigned getTypeQuals() const { return FunctionType::getTypeQuals(); } 1484 1485 typedef const QualType *arg_type_iterator; 1486 arg_type_iterator arg_type_begin() const { 1487 return reinterpret_cast<const QualType *>(this+1); 1488 } 1489 arg_type_iterator arg_type_end() const { return arg_type_begin()+NumArgs; } 1490 1491 typedef const QualType *exception_iterator; 1492 exception_iterator exception_begin() const { 1493 // exceptions begin where arguments end 1494 return arg_type_end(); 1495 } 1496 exception_iterator exception_end() const { 1497 return exception_begin() + NumExceptions; 1498 } 1499 1500 virtual void getAsStringInternal(std::string &InnerString, 1501 const PrintingPolicy &Policy) const; 1502 1503 static bool classof(const Type *T) { 1504 return T->getTypeClass() == FunctionProto; 1505 } 1506 static bool classof(const FunctionProtoType *) { return true; } 1507 1508 void Profile(llvm::FoldingSetNodeID &ID); 1509 static void Profile(llvm::FoldingSetNodeID &ID, QualType Result, 1510 arg_type_iterator ArgTys, unsigned NumArgs, 1511 bool isVariadic, unsigned TypeQuals, 1512 bool hasExceptionSpec, bool anyExceptionSpec, 1513 unsigned NumExceptions, exception_iterator Exs, 1514 bool NoReturn); 1515}; 1516 1517 1518class TypedefType : public Type { 1519 TypedefDecl *Decl; 1520protected: 1521 TypedefType(TypeClass tc, TypedefDecl *D, QualType can) 1522 : Type(tc, can, can->isDependentType()), Decl(D) { 1523 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 1524 } 1525 friend class ASTContext; // ASTContext creates these. 1526public: 1527 1528 TypedefDecl *getDecl() const { return Decl; } 1529 1530 /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to 1531 /// potentially looking through *all* consecutive typedefs. This returns the 1532 /// sum of the type qualifiers, so if you have: 1533 /// typedef const int A; 1534 /// typedef volatile A B; 1535 /// looking through the typedefs for B will give you "const volatile A". 1536 QualType LookThroughTypedefs() const; 1537 1538 virtual void getAsStringInternal(std::string &InnerString, 1539 const PrintingPolicy &Policy) const; 1540 1541 static bool classof(const Type *T) { return T->getTypeClass() == Typedef; } 1542 static bool classof(const TypedefType *) { return true; } 1543}; 1544 1545/// TypeOfExprType (GCC extension). 1546class TypeOfExprType : public Type { 1547 Expr *TOExpr; 1548 TypeOfExprType(Expr *E, QualType can = QualType()); 1549 friend class ASTContext; // ASTContext creates these. 1550public: 1551 Expr *getUnderlyingExpr() const { return TOExpr; } 1552 1553 virtual void getAsStringInternal(std::string &InnerString, 1554 const PrintingPolicy &Policy) const; 1555 1556 static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; } 1557 static bool classof(const TypeOfExprType *) { return true; } 1558}; 1559 1560/// TypeOfType (GCC extension). 1561class TypeOfType : public Type { 1562 QualType TOType; 1563 TypeOfType(QualType T, QualType can) 1564 : Type(TypeOf, can, T->isDependentType()), TOType(T) { 1565 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 1566 } 1567 friend class ASTContext; // ASTContext creates these. 1568public: 1569 QualType getUnderlyingType() const { return TOType; } 1570 1571 virtual void getAsStringInternal(std::string &InnerString, 1572 const PrintingPolicy &Policy) const; 1573 1574 static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; } 1575 static bool classof(const TypeOfType *) { return true; } 1576}; 1577 1578/// DecltypeType (C++0x) 1579class DecltypeType : public Type { 1580 Expr *E; 1581 1582 // FIXME: We could get rid of UnderlyingType if we wanted to: We would have to 1583 // Move getDesugaredType to ASTContext so that it can call getDecltypeForExpr 1584 // from it. 1585 QualType UnderlyingType; 1586 1587 DecltypeType(Expr *E, QualType underlyingType, QualType can = QualType()); 1588 friend class ASTContext; // ASTContext creates these. 1589public: 1590 Expr *getUnderlyingExpr() const { return E; } 1591 QualType getUnderlyingType() const { return UnderlyingType; } 1592 1593 virtual void getAsStringInternal(std::string &InnerString, 1594 const PrintingPolicy &Policy) const; 1595 1596 static bool classof(const Type *T) { return T->getTypeClass() == Decltype; } 1597 static bool classof(const DecltypeType *) { return true; } 1598}; 1599 1600class TagType : public Type { 1601 /// Stores the TagDecl associated with this type. The decl will 1602 /// point to the TagDecl that actually defines the entity (or is a 1603 /// definition in progress), if there is such a definition. The 1604 /// single-bit value will be non-zero when this tag is in the 1605 /// process of being defined. 1606 mutable llvm::PointerIntPair<TagDecl *, 1> decl; 1607 friend class ASTContext; 1608 friend class TagDecl; 1609 1610protected: 1611 TagType(TypeClass TC, TagDecl *D, QualType can); 1612 1613public: 1614 TagDecl *getDecl() const { return decl.getPointer(); } 1615 1616 /// @brief Determines whether this type is in the process of being 1617 /// defined. 1618 bool isBeingDefined() const { return decl.getInt(); } 1619 void setBeingDefined(bool Def) { decl.setInt(Def? 1 : 0); } 1620 1621 virtual void getAsStringInternal(std::string &InnerString, 1622 const PrintingPolicy &Policy) const; 1623 1624 static bool classof(const Type *T) { 1625 return T->getTypeClass() >= TagFirst && T->getTypeClass() <= TagLast; 1626 } 1627 static bool classof(const TagType *) { return true; } 1628 static bool classof(const RecordType *) { return true; } 1629 static bool classof(const EnumType *) { return true; } 1630}; 1631 1632/// RecordType - This is a helper class that allows the use of isa/cast/dyncast 1633/// to detect TagType objects of structs/unions/classes. 1634class RecordType : public TagType { 1635protected: 1636 explicit RecordType(RecordDecl *D) 1637 : TagType(Record, reinterpret_cast<TagDecl*>(D), QualType()) { } 1638 explicit RecordType(TypeClass TC, RecordDecl *D) 1639 : TagType(TC, reinterpret_cast<TagDecl*>(D), QualType()) { } 1640 friend class ASTContext; // ASTContext creates these. 1641public: 1642 1643 RecordDecl *getDecl() const { 1644 return reinterpret_cast<RecordDecl*>(TagType::getDecl()); 1645 } 1646 1647 // FIXME: This predicate is a helper to QualType/Type. It needs to 1648 // recursively check all fields for const-ness. If any field is declared 1649 // const, it needs to return false. 1650 bool hasConstFields() const { return false; } 1651 1652 // FIXME: RecordType needs to check when it is created that all fields are in 1653 // the same address space, and return that. 1654 unsigned getAddressSpace() const { return 0; } 1655 1656 static bool classof(const TagType *T); 1657 static bool classof(const Type *T) { 1658 return isa<TagType>(T) && classof(cast<TagType>(T)); 1659 } 1660 static bool classof(const RecordType *) { return true; } 1661}; 1662 1663/// EnumType - This is a helper class that allows the use of isa/cast/dyncast 1664/// to detect TagType objects of enums. 1665class EnumType : public TagType { 1666 explicit EnumType(EnumDecl *D) 1667 : TagType(Enum, reinterpret_cast<TagDecl*>(D), QualType()) { } 1668 friend class ASTContext; // ASTContext creates these. 1669public: 1670 1671 EnumDecl *getDecl() const { 1672 return reinterpret_cast<EnumDecl*>(TagType::getDecl()); 1673 } 1674 1675 static bool classof(const TagType *T); 1676 static bool classof(const Type *T) { 1677 return isa<TagType>(T) && classof(cast<TagType>(T)); 1678 } 1679 static bool classof(const EnumType *) { return true; } 1680}; 1681 1682class TemplateTypeParmType : public Type, public llvm::FoldingSetNode { 1683 unsigned Depth : 15; 1684 unsigned Index : 16; 1685 unsigned ParameterPack : 1; 1686 IdentifierInfo *Name; 1687 1688 TemplateTypeParmType(unsigned D, unsigned I, bool PP, IdentifierInfo *N, 1689 QualType Canon) 1690 : Type(TemplateTypeParm, Canon, /*Dependent=*/true), 1691 Depth(D), Index(I), ParameterPack(PP), Name(N) { } 1692 1693 TemplateTypeParmType(unsigned D, unsigned I, bool PP) 1694 : Type(TemplateTypeParm, QualType(this, 0), /*Dependent=*/true), 1695 Depth(D), Index(I), ParameterPack(PP), Name(0) { } 1696 1697 friend class ASTContext; // ASTContext creates these 1698 1699public: 1700 unsigned getDepth() const { return Depth; } 1701 unsigned getIndex() const { return Index; } 1702 bool isParameterPack() const { return ParameterPack; } 1703 IdentifierInfo *getName() const { return Name; } 1704 1705 virtual void getAsStringInternal(std::string &InnerString, 1706 const PrintingPolicy &Policy) const; 1707 1708 void Profile(llvm::FoldingSetNodeID &ID) { 1709 Profile(ID, Depth, Index, ParameterPack, Name); 1710 } 1711 1712 static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth, 1713 unsigned Index, bool ParameterPack, 1714 IdentifierInfo *Name) { 1715 ID.AddInteger(Depth); 1716 ID.AddInteger(Index); 1717 ID.AddBoolean(ParameterPack); 1718 ID.AddPointer(Name); 1719 } 1720 1721 static bool classof(const Type *T) { 1722 return T->getTypeClass() == TemplateTypeParm; 1723 } 1724 static bool classof(const TemplateTypeParmType *T) { return true; } 1725}; 1726 1727/// \brief Represents the type of a template specialization as written 1728/// in the source code. 1729/// 1730/// Template specialization types represent the syntactic form of a 1731/// template-id that refers to a type, e.g., @c vector<int>. Some 1732/// template specialization types are syntactic sugar, whose canonical 1733/// type will point to some other type node that represents the 1734/// instantiation or class template specialization. For example, a 1735/// class template specialization type of @c vector<int> will refer to 1736/// a tag type for the instantiation 1737/// @c std::vector<int, std::allocator<int>>. 1738/// 1739/// Other template specialization types, for which the template name 1740/// is dependent, may be canonical types. These types are always 1741/// dependent. 1742class TemplateSpecializationType 1743 : public Type, public llvm::FoldingSetNode { 1744 1745 /// \brief The name of the template being specialized. 1746 TemplateName Template; 1747 1748 /// \brief - The number of template arguments named in this class 1749 /// template specialization. 1750 unsigned NumArgs; 1751 1752 TemplateSpecializationType(TemplateName T, 1753 const TemplateArgument *Args, 1754 unsigned NumArgs, QualType Canon); 1755 1756 virtual void Destroy(ASTContext& C); 1757 1758 friend class ASTContext; // ASTContext creates these 1759 1760public: 1761 /// \brief Determine whether any of the given template arguments are 1762 /// dependent. 1763 static bool anyDependentTemplateArguments(const TemplateArgument *Args, 1764 unsigned NumArgs); 1765 1766 /// \brief Print a template argument list, including the '<' and '>' 1767 /// enclosing the template arguments. 1768 static std::string PrintTemplateArgumentList(const TemplateArgument *Args, 1769 unsigned NumArgs, 1770 const PrintingPolicy &Policy); 1771 1772 typedef const TemplateArgument * iterator; 1773 1774 iterator begin() const { return getArgs(); } 1775 iterator end() const; 1776 1777 /// \brief Retrieve the name of the template that we are specializing. 1778 TemplateName getTemplateName() const { return Template; } 1779 1780 /// \brief Retrieve the template arguments. 1781 const TemplateArgument *getArgs() const { 1782 return reinterpret_cast<const TemplateArgument *>(this + 1); 1783 } 1784 1785 /// \brief Retrieve the number of template arguments. 1786 unsigned getNumArgs() const { return NumArgs; } 1787 1788 /// \brief Retrieve a specific template argument as a type. 1789 /// \precondition @c isArgType(Arg) 1790 const TemplateArgument &getArg(unsigned Idx) const; 1791 1792 virtual void getAsStringInternal(std::string &InnerString, 1793 const PrintingPolicy &Policy) const; 1794 1795 void Profile(llvm::FoldingSetNodeID &ID) { 1796 Profile(ID, Template, getArgs(), NumArgs); 1797 } 1798 1799 static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T, 1800 const TemplateArgument *Args, unsigned NumArgs); 1801 1802 static bool classof(const Type *T) { 1803 return T->getTypeClass() == TemplateSpecialization; 1804 } 1805 static bool classof(const TemplateSpecializationType *T) { return true; } 1806}; 1807 1808/// \brief Represents a type that was referred to via a qualified 1809/// name, e.g., N::M::type. 1810/// 1811/// This type is used to keep track of a type name as written in the 1812/// source code, including any nested-name-specifiers. The type itself 1813/// is always "sugar", used to express what was written in the source 1814/// code but containing no additional semantic information. 1815class QualifiedNameType : public Type, public llvm::FoldingSetNode { 1816 /// \brief The nested name specifier containing the qualifier. 1817 NestedNameSpecifier *NNS; 1818 1819 /// \brief The type that this qualified name refers to. 1820 QualType NamedType; 1821 1822 QualifiedNameType(NestedNameSpecifier *NNS, QualType NamedType, 1823 QualType CanonType) 1824 : Type(QualifiedName, CanonType, NamedType->isDependentType()), 1825 NNS(NNS), NamedType(NamedType) { } 1826 1827 friend class ASTContext; // ASTContext creates these 1828 1829public: 1830 /// \brief Retrieve the qualification on this type. 1831 NestedNameSpecifier *getQualifier() const { return NNS; } 1832 1833 /// \brief Retrieve the type named by the qualified-id. 1834 QualType getNamedType() const { return NamedType; } 1835 1836 virtual void getAsStringInternal(std::string &InnerString, 1837 const PrintingPolicy &Policy) const; 1838 1839 void Profile(llvm::FoldingSetNodeID &ID) { 1840 Profile(ID, NNS, NamedType); 1841 } 1842 1843 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS, 1844 QualType NamedType) { 1845 ID.AddPointer(NNS); 1846 NamedType.Profile(ID); 1847 } 1848 1849 static bool classof(const Type *T) { 1850 return T->getTypeClass() == QualifiedName; 1851 } 1852 static bool classof(const QualifiedNameType *T) { return true; } 1853}; 1854 1855/// \brief Represents a 'typename' specifier that names a type within 1856/// a dependent type, e.g., "typename T::type". 1857/// 1858/// TypenameType has a very similar structure to QualifiedNameType, 1859/// which also involves a nested-name-specifier following by a type, 1860/// and (FIXME!) both can even be prefixed by the 'typename' 1861/// keyword. However, the two types serve very different roles: 1862/// QualifiedNameType is a non-semantic type that serves only as sugar 1863/// to show how a particular type was written in the source 1864/// code. TypenameType, on the other hand, only occurs when the 1865/// nested-name-specifier is dependent, such that we cannot resolve 1866/// the actual type until after instantiation. 1867class TypenameType : public Type, public llvm::FoldingSetNode { 1868 /// \brief The nested name specifier containing the qualifier. 1869 NestedNameSpecifier *NNS; 1870 1871 typedef llvm::PointerUnion<const IdentifierInfo *, 1872 const TemplateSpecializationType *> NameType; 1873 1874 /// \brief The type that this typename specifier refers to. 1875 NameType Name; 1876 1877 TypenameType(NestedNameSpecifier *NNS, const IdentifierInfo *Name, 1878 QualType CanonType) 1879 : Type(Typename, CanonType, true), NNS(NNS), Name(Name) { 1880 assert(NNS->isDependent() && 1881 "TypenameType requires a dependent nested-name-specifier"); 1882 } 1883 1884 TypenameType(NestedNameSpecifier *NNS, const TemplateSpecializationType *Ty, 1885 QualType CanonType) 1886 : Type(Typename, CanonType, true), NNS(NNS), Name(Ty) { 1887 assert(NNS->isDependent() && 1888 "TypenameType requires a dependent nested-name-specifier"); 1889 } 1890 1891 friend class ASTContext; // ASTContext creates these 1892 1893public: 1894 /// \brief Retrieve the qualification on this type. 1895 NestedNameSpecifier *getQualifier() const { return NNS; } 1896 1897 /// \brief Retrieve the type named by the typename specifier as an 1898 /// identifier. 1899 /// 1900 /// This routine will return a non-NULL identifier pointer when the 1901 /// form of the original typename was terminated by an identifier, 1902 /// e.g., "typename T::type". 1903 const IdentifierInfo *getIdentifier() const { 1904 return Name.dyn_cast<const IdentifierInfo *>(); 1905 } 1906 1907 /// \brief Retrieve the type named by the typename specifier as a 1908 /// type specialization. 1909 const TemplateSpecializationType *getTemplateId() const { 1910 return Name.dyn_cast<const TemplateSpecializationType *>(); 1911 } 1912 1913 virtual void getAsStringInternal(std::string &InnerString, 1914 const PrintingPolicy &Policy) const; 1915 1916 void Profile(llvm::FoldingSetNodeID &ID) { 1917 Profile(ID, NNS, Name); 1918 } 1919 1920 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS, 1921 NameType Name) { 1922 ID.AddPointer(NNS); 1923 ID.AddPointer(Name.getOpaqueValue()); 1924 } 1925 1926 static bool classof(const Type *T) { 1927 return T->getTypeClass() == Typename; 1928 } 1929 static bool classof(const TypenameType *T) { return true; } 1930}; 1931 1932/// ObjCInterfaceType - Interfaces are the core concept in Objective-C for 1933/// object oriented design. They basically correspond to C++ classes. There 1934/// are two kinds of interface types, normal interfaces like "NSString" and 1935/// qualified interfaces, which are qualified with a protocol list like 1936/// "NSString<NSCopyable, NSAmazing>". 1937class ObjCInterfaceType : public Type, public llvm::FoldingSetNode { 1938 ObjCInterfaceDecl *Decl; 1939 1940 // List of protocols for this protocol conforming object type 1941 // List is sorted on protocol name. No protocol is enterred more than once. 1942 llvm::SmallVector<ObjCProtocolDecl*, 4> Protocols; 1943 1944 ObjCInterfaceType(ObjCInterfaceDecl *D, 1945 ObjCProtocolDecl **Protos, unsigned NumP) : 1946 Type(ObjCInterface, QualType(), /*Dependent=*/false), 1947 Decl(D), Protocols(Protos, Protos+NumP) { } 1948 friend class ASTContext; // ASTContext creates these. 1949public: 1950 ObjCInterfaceDecl *getDecl() const { return Decl; } 1951 1952 /// getNumProtocols - Return the number of qualifying protocols in this 1953 /// interface type, or 0 if there are none. 1954 unsigned getNumProtocols() const { return Protocols.size(); } 1955 1956 /// qual_iterator and friends: this provides access to the (potentially empty) 1957 /// list of protocols qualifying this interface. 1958 typedef llvm::SmallVector<ObjCProtocolDecl*, 8>::const_iterator qual_iterator; 1959 qual_iterator qual_begin() const { return Protocols.begin(); } 1960 qual_iterator qual_end() const { return Protocols.end(); } 1961 bool qual_empty() const { return Protocols.size() == 0; } 1962 1963 virtual void getAsStringInternal(std::string &InnerString, 1964 const PrintingPolicy &Policy) const; 1965 1966 void Profile(llvm::FoldingSetNodeID &ID); 1967 static void Profile(llvm::FoldingSetNodeID &ID, 1968 const ObjCInterfaceDecl *Decl, 1969 ObjCProtocolDecl **protocols, unsigned NumProtocols); 1970 1971 static bool classof(const Type *T) { 1972 return T->getTypeClass() == ObjCInterface; 1973 } 1974 static bool classof(const ObjCInterfaceType *) { return true; } 1975}; 1976 1977/// ObjCObjectPointerType - Used to represent 'id', 'Interface *', 'id <p>', 1978/// and 'Interface <p> *'. 1979/// 1980/// Duplicate protocols are removed and protocol list is canonicalized to be in 1981/// alphabetical order. 1982class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode { 1983 QualType PointeeType; // A builtin or interface type. 1984 1985 // List of protocols for this protocol conforming object type 1986 // List is sorted on protocol name. No protocol is entered more than once. 1987 llvm::SmallVector<ObjCProtocolDecl*, 8> Protocols; 1988 1989 ObjCObjectPointerType(QualType T, ObjCProtocolDecl **Protos, unsigned NumP) : 1990 Type(ObjCObjectPointer, QualType(), /*Dependent=*/false), 1991 PointeeType(T), Protocols(Protos, Protos+NumP) { } 1992 friend class ASTContext; // ASTContext creates these. 1993 1994public: 1995 // Get the pointee type. Pointee will either be: 1996 // - a built-in type (for 'id' and 'Class'). 1997 // - an interface type (for user-defined types). 1998 // - a TypedefType whose canonical type is an interface (as in 'T' below). 1999 // For example: typedef NSObject T; T *var; 2000 QualType getPointeeType() const { return PointeeType; } 2001 2002 const ObjCInterfaceType *getInterfaceType() const { 2003 return PointeeType->getAsObjCInterfaceType(); 2004 } 2005 /// getInterfaceDecl - returns an interface decl for user-defined types. 2006 ObjCInterfaceDecl *getInterfaceDecl() const { 2007 return getInterfaceType() ? getInterfaceType()->getDecl() : 0; 2008 } 2009 /// isObjCIdType - true for "id". 2010 bool isObjCIdType() const { 2011 return getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCId) && 2012 !Protocols.size(); 2013 } 2014 /// isObjCClassType - true for "Class". 2015 bool isObjCClassType() const { 2016 return getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCClass) && 2017 !Protocols.size(); 2018 } 2019 /// isObjCQualifiedIdType - true for "id <p>". 2020 bool isObjCQualifiedIdType() const { 2021 return getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCId) && 2022 Protocols.size(); 2023 } 2024 /// isObjCQualifiedClassType - true for "Class <p>". 2025 bool isObjCQualifiedClassType() const { 2026 return getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCClass) && 2027 Protocols.size(); 2028 } 2029 /// qual_iterator and friends: this provides access to the (potentially empty) 2030 /// list of protocols qualifying this interface. 2031 typedef llvm::SmallVector<ObjCProtocolDecl*, 8>::const_iterator qual_iterator; 2032 2033 qual_iterator qual_begin() const { return Protocols.begin(); } 2034 qual_iterator qual_end() const { return Protocols.end(); } 2035 bool qual_empty() const { return Protocols.size() == 0; } 2036 2037 /// getNumProtocols - Return the number of qualifying protocols in this 2038 /// interface type, or 0 if there are none. 2039 unsigned getNumProtocols() const { return Protocols.size(); } 2040 2041 void Profile(llvm::FoldingSetNodeID &ID); 2042 static void Profile(llvm::FoldingSetNodeID &ID, QualType T, 2043 ObjCProtocolDecl **protocols, unsigned NumProtocols); 2044 virtual void getAsStringInternal(std::string &InnerString, 2045 const PrintingPolicy &Policy) const; 2046 static bool classof(const Type *T) { 2047 return T->getTypeClass() == ObjCObjectPointer; 2048 } 2049 static bool classof(const ObjCObjectPointerType *) { return true; } 2050}; 2051 2052// Inline function definitions. 2053 2054/// getUnqualifiedType - Return the type without any qualifiers. 2055inline QualType QualType::getUnqualifiedType() const { 2056 Type *TP = getTypePtr(); 2057 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(TP)) 2058 TP = EXTQT->getBaseType(); 2059 return QualType(TP, 0); 2060} 2061 2062/// getAddressSpace - Return the address space of this type. 2063inline unsigned QualType::getAddressSpace() const { 2064 QualType CT = getTypePtr()->getCanonicalTypeInternal(); 2065 if (const ArrayType *AT = dyn_cast<ArrayType>(CT)) 2066 return AT->getElementType().getAddressSpace(); 2067 if (const RecordType *RT = dyn_cast<RecordType>(CT)) 2068 return RT->getAddressSpace(); 2069 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CT)) 2070 return EXTQT->getAddressSpace(); 2071 return 0; 2072} 2073 2074/// getObjCGCAttr - Return the gc attribute of this type. 2075inline QualType::GCAttrTypes QualType::getObjCGCAttr() const { 2076 QualType CT = getTypePtr()->getCanonicalTypeInternal(); 2077 if (const ArrayType *AT = dyn_cast<ArrayType>(CT)) 2078 return AT->getElementType().getObjCGCAttr(); 2079 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CT)) 2080 return EXTQT->getObjCGCAttr(); 2081 if (const ObjCObjectPointerType *PT = CT->getAsObjCObjectPointerType()) 2082 return PT->getPointeeType().getObjCGCAttr(); 2083 return GCNone; 2084} 2085 2086 /// getNoReturnAttr - Returns true if the type has the noreturn attribute, 2087 /// false otherwise. 2088inline bool QualType::getNoReturnAttr() const { 2089 QualType CT = getTypePtr()->getCanonicalTypeInternal(); 2090 if (const PointerType *PT = getTypePtr()->getAsPointerType()) { 2091 if (const FunctionType *FT = PT->getPointeeType()->getAsFunctionType()) 2092 return FT->getNoReturnAttr(); 2093 } else if (const FunctionType *FT = getTypePtr()->getAsFunctionType()) 2094 return FT->getNoReturnAttr(); 2095 2096 return false; 2097} 2098 2099/// isMoreQualifiedThan - Determine whether this type is more 2100/// qualified than the Other type. For example, "const volatile int" 2101/// is more qualified than "const int", "volatile int", and 2102/// "int". However, it is not more qualified than "const volatile 2103/// int". 2104inline bool QualType::isMoreQualifiedThan(QualType Other) const { 2105 unsigned MyQuals = this->getCVRQualifiers(); 2106 unsigned OtherQuals = Other.getCVRQualifiers(); 2107 if (getAddressSpace() != Other.getAddressSpace()) 2108 return false; 2109 return MyQuals != OtherQuals && (MyQuals | OtherQuals) == MyQuals; 2110} 2111 2112/// isAtLeastAsQualifiedAs - Determine whether this type is at last 2113/// as qualified as the Other type. For example, "const volatile 2114/// int" is at least as qualified as "const int", "volatile int", 2115/// "int", and "const volatile int". 2116inline bool QualType::isAtLeastAsQualifiedAs(QualType Other) const { 2117 unsigned MyQuals = this->getCVRQualifiers(); 2118 unsigned OtherQuals = Other.getCVRQualifiers(); 2119 if (getAddressSpace() != Other.getAddressSpace()) 2120 return false; 2121 return (MyQuals | OtherQuals) == MyQuals; 2122} 2123 2124/// getNonReferenceType - If Type is a reference type (e.g., const 2125/// int&), returns the type that the reference refers to ("const 2126/// int"). Otherwise, returns the type itself. This routine is used 2127/// throughout Sema to implement C++ 5p6: 2128/// 2129/// If an expression initially has the type "reference to T" (8.3.2, 2130/// 8.5.3), the type is adjusted to "T" prior to any further 2131/// analysis, the expression designates the object or function 2132/// denoted by the reference, and the expression is an lvalue. 2133inline QualType QualType::getNonReferenceType() const { 2134 if (const ReferenceType *RefType = (*this)->getAsReferenceType()) 2135 return RefType->getPointeeType(); 2136 else 2137 return *this; 2138} 2139 2140inline const TypedefType* Type::getAsTypedefType() const { 2141 return dyn_cast<TypedefType>(this); 2142} 2143inline const ObjCInterfaceType *Type::getAsPointerToObjCInterfaceType() const { 2144 if (const PointerType *PT = getAsPointerType()) 2145 return PT->getPointeeType()->getAsObjCInterfaceType(); 2146 return 0; 2147} 2148 2149// NOTE: All of these methods use "getUnqualifiedType" to strip off address 2150// space qualifiers if present. 2151inline bool Type::isFunctionType() const { 2152 return isa<FunctionType>(CanonicalType.getUnqualifiedType()); 2153} 2154inline bool Type::isPointerType() const { 2155 return isa<PointerType>(CanonicalType.getUnqualifiedType()); 2156} 2157inline bool Type::isAnyPointerType() const { 2158 return isPointerType() || isObjCObjectPointerType(); 2159} 2160inline bool Type::isBlockPointerType() const { 2161 return isa<BlockPointerType>(CanonicalType.getUnqualifiedType()); 2162} 2163inline bool Type::isReferenceType() const { 2164 return isa<ReferenceType>(CanonicalType.getUnqualifiedType()); 2165} 2166inline bool Type::isLValueReferenceType() const { 2167 return isa<LValueReferenceType>(CanonicalType.getUnqualifiedType()); 2168} 2169inline bool Type::isRValueReferenceType() const { 2170 return isa<RValueReferenceType>(CanonicalType.getUnqualifiedType()); 2171} 2172inline bool Type::isFunctionPointerType() const { 2173 if (const PointerType* T = getAsPointerType()) 2174 return T->getPointeeType()->isFunctionType(); 2175 else 2176 return false; 2177} 2178inline bool Type::isMemberPointerType() const { 2179 return isa<MemberPointerType>(CanonicalType.getUnqualifiedType()); 2180} 2181inline bool Type::isMemberFunctionPointerType() const { 2182 if (const MemberPointerType* T = getAsMemberPointerType()) 2183 return T->getPointeeType()->isFunctionType(); 2184 else 2185 return false; 2186} 2187inline bool Type::isArrayType() const { 2188 return isa<ArrayType>(CanonicalType.getUnqualifiedType()); 2189} 2190inline bool Type::isConstantArrayType() const { 2191 return isa<ConstantArrayType>(CanonicalType.getUnqualifiedType()); 2192} 2193inline bool Type::isIncompleteArrayType() const { 2194 return isa<IncompleteArrayType>(CanonicalType.getUnqualifiedType()); 2195} 2196inline bool Type::isVariableArrayType() const { 2197 return isa<VariableArrayType>(CanonicalType.getUnqualifiedType()); 2198} 2199inline bool Type::isDependentSizedArrayType() const { 2200 return isa<DependentSizedArrayType>(CanonicalType.getUnqualifiedType()); 2201} 2202inline bool Type::isRecordType() const { 2203 return isa<RecordType>(CanonicalType.getUnqualifiedType()); 2204} 2205inline bool Type::isAnyComplexType() const { 2206 return isa<ComplexType>(CanonicalType.getUnqualifiedType()); 2207} 2208inline bool Type::isVectorType() const { 2209 return isa<VectorType>(CanonicalType.getUnqualifiedType()); 2210} 2211inline bool Type::isExtVectorType() const { 2212 return isa<ExtVectorType>(CanonicalType.getUnqualifiedType()); 2213} 2214inline bool Type::isObjCObjectPointerType() const { 2215 return isa<ObjCObjectPointerType>(CanonicalType.getUnqualifiedType()); 2216} 2217inline bool Type::isObjCInterfaceType() const { 2218 return isa<ObjCInterfaceType>(CanonicalType.getUnqualifiedType()); 2219} 2220inline bool Type::isObjCQualifiedIdType() const { 2221 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) 2222 return OPT->isObjCQualifiedIdType(); 2223 return false; 2224} 2225inline bool Type::isObjCQualifiedClassType() const { 2226 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) 2227 return OPT->isObjCQualifiedClassType(); 2228 return false; 2229} 2230inline bool Type::isObjCIdType() const { 2231 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) 2232 return OPT->isObjCIdType(); 2233 return false; 2234} 2235inline bool Type::isObjCClassType() const { 2236 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) 2237 return OPT->isObjCClassType(); 2238 return false; 2239} 2240inline bool Type::isObjCBuiltinType() const { 2241 return isObjCIdType() || isObjCClassType(); 2242} 2243inline bool Type::isTemplateTypeParmType() const { 2244 return isa<TemplateTypeParmType>(CanonicalType.getUnqualifiedType()); 2245} 2246 2247inline bool Type::isSpecificBuiltinType(unsigned K) const { 2248 if (const BuiltinType *BT = getAsBuiltinType()) 2249 if (BT->getKind() == (BuiltinType::Kind) K) 2250 return true; 2251 return false; 2252} 2253 2254/// \brief Determines whether this is a type for which one can define 2255/// an overloaded operator. 2256inline bool Type::isOverloadableType() const { 2257 return isDependentType() || isRecordType() || isEnumeralType(); 2258} 2259 2260inline bool Type::hasPointerRepresentation() const { 2261 return (isPointerType() || isReferenceType() || isBlockPointerType() || 2262 isObjCInterfaceType() || isObjCObjectPointerType() || 2263 isObjCQualifiedInterfaceType() || isNullPtrType()); 2264} 2265 2266inline bool Type::hasObjCPointerRepresentation() const { 2267 return (isObjCInterfaceType() || isObjCObjectPointerType() || 2268 isObjCQualifiedInterfaceType()); 2269} 2270 2271/// Insertion operator for diagnostics. This allows sending QualType's into a 2272/// diagnostic with <<. 2273inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 2274 QualType T) { 2275 DB.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), 2276 Diagnostic::ak_qualtype); 2277 return DB; 2278} 2279 2280/// Member-template getAs<specific type>'. 2281template <typename T> const T *Type::getAs() const { 2282 // If this is directly a T type, return it. 2283 if (const T *Ty = dyn_cast<T>(this)) 2284 return Ty; 2285 2286 // If the canonical form of this type isn't the right kind, reject it. 2287 if (!isa<T>(CanonicalType)) { 2288 // Look through type qualifiers 2289 if (isa<T>(CanonicalType.getUnqualifiedType())) 2290 return CanonicalType.getUnqualifiedType()->getAs<T>(); 2291 return 0; 2292 } 2293 2294 // If this is a typedef for a pointer type, strip the typedef off without 2295 // losing all typedef information. 2296 return cast<T>(getDesugaredType()); 2297} 2298 2299} // end namespace clang 2300 2301#endif 2302