Type.h revision e37882ad335896dedf345102bb425383e6221c37
1//===--- Type.h - C Language Family Type Representation ---------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by Chris Lattner and is distributed under 6// the University of Illinois Open Source 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 "llvm/Support/Casting.h" 18#include "llvm/ADT/FoldingSet.h" 19#include "llvm/ADT/APSInt.h" 20 21using llvm::isa; 22using llvm::cast; 23using llvm::cast_or_null; 24using llvm::dyn_cast; 25using llvm::dyn_cast_or_null; 26 27namespace clang { 28 class ASTContext; 29 class Type; 30 class TypedefDecl; 31 class TagDecl; 32 class RecordDecl; 33 class EnumDecl; 34 class ObjcInterfaceDecl; 35 class ObjcProtocolDecl; 36 class Expr; 37 class SourceLocation; 38 class PointerType; 39 class ReferenceType; 40 class VectorType; 41 class ArrayType; 42 class ConstantArrayType; 43 class VariableArrayType; 44 class RecordType; 45 class ComplexType; 46 class TagType; 47 class FunctionType; 48 class OCUVectorType; 49 class BuiltinType; 50 51/// QualType - For efficiency, we don't store CVR-qualified types as nodes on 52/// their own: instead each reference to a type stores the qualifiers. This 53/// greatly reduces the number of nodes we need to allocate for types (for 54/// example we only need one for 'int', 'const int', 'volatile int', 55/// 'const volatile int', etc). 56/// 57/// As an added efficiency bonus, instead of making this a pair, we just store 58/// the three bits we care about in the low bits of the pointer. To handle the 59/// packing/unpacking, we make QualType be a simple wrapper class that acts like 60/// a smart pointer. 61class QualType { 62 uintptr_t ThePtr; 63public: 64 enum TQ { // NOTE: These flags must be kept in sync with DeclSpec::TQ. 65 Const = 0x1, 66 Restrict = 0x2, 67 Volatile = 0x4, 68 CVRFlags = Const|Restrict|Volatile 69 }; 70 71 QualType() : ThePtr(0) {} 72 73 QualType(Type *Ptr, unsigned Quals) { 74 assert((Quals & ~CVRFlags) == 0 && "Invalid type qualifiers!"); 75 ThePtr = reinterpret_cast<uintptr_t>(Ptr); 76 assert((ThePtr & CVRFlags) == 0 && "Type pointer not 8-byte aligned?"); 77 ThePtr |= Quals; 78 } 79 80 static QualType getFromOpaquePtr(void *Ptr) { 81 QualType T; 82 T.ThePtr = reinterpret_cast<uintptr_t>(Ptr); 83 return T; 84 } 85 86 unsigned getQualifiers() const { 87 return ThePtr & CVRFlags; 88 } 89 Type *getTypePtr() const { 90 return reinterpret_cast<Type*>(ThePtr & ~CVRFlags); 91 } 92 93 void *getAsOpaquePtr() const { 94 return reinterpret_cast<void*>(ThePtr); 95 } 96 97 Type &operator*() const { 98 return *getTypePtr(); 99 } 100 101 Type *operator->() const { 102 return getTypePtr(); 103 } 104 105 /// isNull - Return true if this QualType doesn't point to a type yet. 106 bool isNull() const { 107 return ThePtr == 0; 108 } 109 110 bool isConstQualified() const { 111 return (ThePtr & Const) ? true : false; 112 } 113 bool isVolatileQualified() const { 114 return (ThePtr & Volatile) ? true : false; 115 } 116 bool isRestrictQualified() const { 117 return (ThePtr & Restrict) ? true : false; 118 } 119 120 /// addConst/addVolatile/addRestrict - add the specified type qual to this 121 /// QualType. 122 void addConst() { ThePtr |= Const; } 123 void addVolatile() { ThePtr |= Volatile; } 124 void addRestrict() { ThePtr |= Restrict; } 125 126 QualType getQualifiedType(unsigned TQs) const { 127 return QualType(getTypePtr(), TQs); 128 } 129 130 QualType getUnqualifiedType() const { 131 return QualType(getTypePtr(), 0); 132 } 133 134 /// operator==/!= - Indicate whether the specified types and qualifiers are 135 /// identical. 136 bool operator==(const QualType &RHS) const { 137 return ThePtr == RHS.ThePtr; 138 } 139 bool operator!=(const QualType &RHS) const { 140 return ThePtr != RHS.ThePtr; 141 } 142 std::string getAsString() const { 143 std::string S; 144 getAsStringInternal(S); 145 return S; 146 } 147 void getAsStringInternal(std::string &Str) const; 148 149 void dump(const char *s = 0) const; 150 151 /// getCanonicalType - Return the canonical version of this type, with the 152 /// appropriate type qualifiers on it. 153 inline QualType getCanonicalType() const; 154 155private: 156}; 157 158} // end clang. 159 160namespace llvm { 161/// Implement simplify_type for QualType, so that we can dyn_cast from QualType 162/// to a specific Type class. 163template<> struct simplify_type<const ::clang::QualType> { 164 typedef ::clang::Type* SimpleType; 165 static SimpleType getSimplifiedValue(const ::clang::QualType &Val) { 166 return Val.getTypePtr(); 167 } 168}; 169template<> struct simplify_type< ::clang::QualType> 170 : public simplify_type<const ::clang::QualType> {}; 171} 172 173namespace clang { 174 175/// Type - This is the base class of the type hierarchy. A central concept 176/// with types is that each type always has a canonical type. A canonical type 177/// is the type with any typedef names stripped out of it or the types it 178/// references. For example, consider: 179/// 180/// typedef int foo; 181/// typedef foo* bar; 182/// 'int *' 'foo *' 'bar' 183/// 184/// There will be a Type object created for 'int'. Since int is canonical, its 185/// canonicaltype pointer points to itself. There is also a Type for 'foo' (a 186/// TypeNameType). Its CanonicalType pointer points to the 'int' Type. Next 187/// there is a PointerType that represents 'int*', which, like 'int', is 188/// canonical. Finally, there is a PointerType type for 'foo*' whose canonical 189/// type is 'int*', and there is a TypeNameType for 'bar', whose canonical type 190/// is also 'int*'. 191/// 192/// Non-canonical types are useful for emitting diagnostics, without losing 193/// information about typedefs being used. Canonical types are useful for type 194/// comparisons (they allow by-pointer equality tests) and useful for reasoning 195/// about whether something has a particular form (e.g. is a function type), 196/// because they implicitly, recursively, strip all typedefs out of a type. 197/// 198/// Types, once created, are immutable. 199/// 200class Type { 201public: 202 enum TypeClass { 203 Builtin, Complex, Pointer, Reference, 204 ConstantArray, VariableArray, 205 Vector, OCUVector, 206 FunctionNoProto, FunctionProto, 207 TypeName, Tagged, 208 ObjcInterface, ObjcQualifiedInterface, 209 TypeOfExp, TypeOfTyp // GNU typeof extension. 210 }; 211private: 212 QualType CanonicalType; 213 214 /// TypeClass bitfield - Enum that specifies what subclass this belongs to. 215 /// Note that this should stay at the end of the ivars for Type so that 216 /// subclasses can pack their bitfields into the same word. 217 unsigned TC : 4; 218protected: 219 // silence VC++ warning C4355: 'this' : used in base member initializer list 220 Type *this_() { return this; } 221 Type(TypeClass tc, QualType Canonical) 222 : CanonicalType(Canonical.isNull() ? QualType(this_(),0) : Canonical), TC(tc){} 223 virtual ~Type(); 224 friend class ASTContext; 225public: 226 TypeClass getTypeClass() const { return static_cast<TypeClass>(TC); } 227 228 bool isCanonical() const { return CanonicalType.getTypePtr() == this; } 229 230 /// Types are partitioned into 3 broad categories (C99 6.2.5p1): 231 /// object types, function types, and incomplete types. 232 233 /// isObjectType - types that fully describe objects. An object is a region 234 /// of memory that can be examined and stored into (H&S). 235 bool isObjectType() const; 236 237 /// isIncompleteType - Return true if this is an incomplete type. 238 /// A type that can describe objects, but which lacks information needed to 239 /// determine its size (e.g. void, or a fwd declared struct). Clients of this 240 /// routine will need to determine if the size is actually required. 241 bool isIncompleteType() const; 242 243 /// isVariablyModifiedType (C99 6.7.5.2p2) - Return true for variable array 244 /// types that have a non-constant expression. This does not include "[]". 245 bool isVariablyModifiedType() const; 246 247 /// Helper methods to distinguish type categories. All type predicates 248 /// operate on the canonical type, ignoring typedefs. 249 bool isIntegerType() const; // C99 6.2.5p17 (int, char, bool, enum) 250 bool isEnumeralType() const; 251 bool isBooleanType() const; 252 bool isCharType() const; 253 254 /// Floating point categories. 255 bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double) 256 bool isComplexType() const; // C99 6.2.5p11 (complex) 257 bool isFloatingType() const; // C99 6.2.5p11 (real floating + complex) 258 bool isRealType() const; // C99 6.2.5p17 (real floating + integer) 259 bool isArithmeticType() const; // C99 6.2.5p18 (integer + floating) 260 bool isVoidType() const; // C99 6.2.5p19 261 bool isDerivedType() const; // C99 6.2.5p20 262 bool isScalarType() const; // C99 6.2.5p21 (arithmetic + pointers) 263 bool isAggregateType() const; // C99 6.2.5p21 (arrays, structures) 264 265 // Type Predicates: Check to see if this type is structurally the specified 266 // type, ignoring typedefs. 267 bool isFunctionType() const; 268 bool isPointerType() const; 269 bool isFunctionPointerType() const; 270 bool isReferenceType() const; 271 bool isArrayType() const; 272 bool isRecordType() const; 273 bool isStructureType() const; 274 bool isUnionType() const; 275 bool isVectorType() const; // GCC vector type. 276 bool isOCUVectorType() const; // OCU vector type. 277 278 // Type Checking Functions: Check to see if this type is structurally the 279 // specified type, ignoring typedefs, and return a pointer to the best type 280 // we can. 281 const BuiltinType *getAsBuiltinType() const; 282 const FunctionType *getAsFunctionType() const; 283 const PointerType *getAsPointerType() const; 284 const ReferenceType *getAsReferenceType() const; 285 const ArrayType *getAsArrayType() const; 286 const ConstantArrayType *getAsConstantArrayType() const; 287 const VariableArrayType *getAsVariableArrayType() const; 288 const VariableArrayType *getAsVariablyModifiedType() const; 289 const RecordType *getAsRecordType() const; 290 const RecordType *getAsStructureType() const; 291 const RecordType *getAsUnionType() const; 292 const VectorType *getAsVectorType() const; // GCC vector type. 293 const ComplexType *getAsComplexType() const; 294 const OCUVectorType *getAsOCUVectorType() const; // OCU vector type. 295 296 /// More type predicates useful for type checking/promotion 297 bool isPromotableIntegerType() const; // C99 6.3.1.1p2 298 299 /// isSignedIntegerType - Return true if this is an integer type that is 300 /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], 301 /// an enum decl which has a signed representation, or a vector of signed 302 /// integer element type. 303 bool isSignedIntegerType() const; 304 305 /// isUnsignedIntegerType - Return true if this is an integer type that is 306 /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum 307 /// decl which has an unsigned representation, or a vector of unsigned integer 308 /// element type. 309 bool isUnsignedIntegerType() const; 310 311 /// isConstantSizeType - Return true if this is not a variable sized type, 312 /// according to the rules of C99 6.7.5p3. If Loc is non-null, it is set to 313 /// the location of the subexpression that makes it a vla type. It is not 314 /// legal to call this on incomplete types. 315 bool isConstantSizeType(ASTContext &Ctx, SourceLocation *Loc = 0) const; 316 317 /// Compatibility predicates used to check assignment expressions. 318 static bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1 319 static bool tagTypesAreCompatible(QualType, QualType); // C99 6.2.7p1 320 static bool pointerTypesAreCompatible(QualType, QualType); // C99 6.7.5.1p2 321 static bool referenceTypesAreCompatible(QualType, QualType); // C++ 5.17p6 322 static bool functionTypesAreCompatible(QualType, QualType); // C99 6.7.5.3p15 323 static bool arrayTypesAreCompatible(QualType, QualType); // C99 6.7.5.2p6 324 static bool builtinTypesAreCompatible(QualType, QualType); 325private: 326 QualType getCanonicalTypeInternal() const { return CanonicalType; } 327 friend class QualType; 328public: 329 virtual void getAsStringInternal(std::string &InnerString) const = 0; 330 static bool classof(const Type *) { return true; } 331}; 332 333/// BuiltinType - This class is used for builtin types like 'int'. Builtin 334/// types are always canonical and have a literal name field. 335class BuiltinType : public Type { 336public: 337 enum Kind { 338 Void, 339 340 Bool, // This is bool and/or _Bool. 341 Char_U, // This is 'char' for targets where char is unsigned. 342 UChar, // This is explicitly qualified unsigned char. 343 UShort, 344 UInt, 345 ULong, 346 ULongLong, 347 348 Char_S, // This is 'char' for targets where char is signed. 349 SChar, // This is explicitly qualified signed char. 350 Short, 351 Int, 352 Long, 353 LongLong, 354 355 Float, Double, LongDouble 356 }; 357private: 358 Kind TypeKind; 359public: 360 BuiltinType(Kind K) : Type(Builtin, QualType()), TypeKind(K) {} 361 362 Kind getKind() const { return TypeKind; } 363 const char *getName() const; 364 365 virtual void getAsStringInternal(std::string &InnerString) const; 366 367 static bool classof(const Type *T) { return T->getTypeClass() == Builtin; } 368 static bool classof(const BuiltinType *) { return true; } 369}; 370 371/// ComplexType - C99 6.2.5p11 - Complex values. This supports the C99 complex 372/// types (_Complex float etc) as well as the GCC integer complex extensions. 373/// 374class ComplexType : public Type, public llvm::FoldingSetNode { 375 QualType ElementType; 376 ComplexType(QualType Element, QualType CanonicalPtr) : 377 Type(Complex, CanonicalPtr), ElementType(Element) { 378 } 379 friend class ASTContext; // ASTContext creates these. 380public: 381 QualType getElementType() const { return ElementType; } 382 383 virtual void getAsStringInternal(std::string &InnerString) const; 384 385 386 void Profile(llvm::FoldingSetNodeID &ID) { 387 Profile(ID, getElementType()); 388 } 389 static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) { 390 ID.AddPointer(Element.getAsOpaquePtr()); 391 } 392 393 static bool classof(const Type *T) { return T->getTypeClass() == Complex; } 394 static bool classof(const ComplexType *) { return true; } 395}; 396 397 398/// PointerType - C99 6.7.5.1 - Pointer Declarators. 399/// 400class PointerType : public Type, public llvm::FoldingSetNode { 401 QualType PointeeType; 402 PointerType(QualType Pointee, QualType CanonicalPtr) : 403 Type(Pointer, CanonicalPtr), PointeeType(Pointee) { 404 } 405 friend class ASTContext; // ASTContext creates these. 406public: 407 408 QualType getPointeeType() const { return PointeeType; } 409 410 virtual void getAsStringInternal(std::string &InnerString) const; 411 412 413 void Profile(llvm::FoldingSetNodeID &ID) { 414 Profile(ID, getPointeeType()); 415 } 416 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { 417 ID.AddPointer(Pointee.getAsOpaquePtr()); 418 } 419 420 static bool classof(const Type *T) { return T->getTypeClass() == Pointer; } 421 static bool classof(const PointerType *) { return true; } 422}; 423 424/// ReferenceType - C++ 8.3.2 - Reference Declarators. 425/// 426class ReferenceType : public Type, public llvm::FoldingSetNode { 427 QualType ReferenceeType; 428 ReferenceType(QualType Referencee, QualType CanonicalRef) : 429 Type(Reference, CanonicalRef), ReferenceeType(Referencee) { 430 } 431 friend class ASTContext; // ASTContext creates these. 432public: 433 virtual void getAsStringInternal(std::string &InnerString) const; 434 435 QualType getReferenceeType() const { return ReferenceeType; } 436 437 void Profile(llvm::FoldingSetNodeID &ID) { 438 Profile(ID, getReferenceeType()); 439 } 440 static void Profile(llvm::FoldingSetNodeID &ID, QualType Referencee) { 441 ID.AddPointer(Referencee.getAsOpaquePtr()); 442 } 443 444 static bool classof(const Type *T) { return T->getTypeClass() == Reference; } 445 static bool classof(const ReferenceType *) { return true; } 446}; 447 448/// ArrayType - C99 6.7.5.2 - Array Declarators. 449/// 450class ArrayType : public Type { 451public: 452 /// ArraySizeModifier - Capture whether this is a normal array (e.g. int X[4]) 453 /// an array with a static size (e.g. int X[static 4]), or with a star size 454 /// (e.g. int X[*]). 'static' is only allowed on function parameters. 455 enum ArraySizeModifier { 456 Normal, Static, Star 457 }; 458private: 459 /// ElementType - The element type of the array. 460 QualType ElementType; 461 462 /// NOTE: These fields are packed into the bitfields space in the Type class. 463 ArraySizeModifier SizeModifier : 2; 464 465 /// IndexTypeQuals - Capture qualifiers in declarations like: 466 /// 'int X[static restrict 4]'. For function parameters only. 467 unsigned IndexTypeQuals : 3; 468 469protected: 470 ArrayType(TypeClass tc, QualType et, QualType can, 471 ArraySizeModifier sm, unsigned tq) 472 : Type(tc, can), ElementType(et), SizeModifier(sm), IndexTypeQuals(tq) {} 473 friend class ASTContext; // ASTContext creates these. 474public: 475 QualType getElementType() const { return ElementType; } 476 ArraySizeModifier getSizeModifier() const { return SizeModifier; } 477 unsigned getIndexTypeQualifier() const { return IndexTypeQuals; } 478 479 QualType getBaseType() const { 480 const ArrayType *AT; 481 QualType ElmtType = getElementType(); 482 // If we have a multi-dimensional array, navigate to the base type. 483 while ((AT = ElmtType->getAsArrayType())) 484 ElmtType = AT->getElementType(); 485 return ElmtType; 486 } 487 static bool classof(const Type *T) { 488 return T->getTypeClass() == ConstantArray || 489 T->getTypeClass() == VariableArray; 490 } 491 static bool classof(const ArrayType *) { return true; } 492}; 493 494class ConstantArrayType : public ArrayType, public llvm::FoldingSetNode { 495 llvm::APInt Size; // Allows us to unique the type. 496 497 ConstantArrayType(QualType et, QualType can, llvm::APInt sz, 498 ArraySizeModifier sm, unsigned tq) 499 : ArrayType(ConstantArray, et, can, sm, tq), Size(sz) {} 500 friend class ASTContext; // ASTContext creates these. 501public: 502 llvm::APInt getSize() const { return Size; } 503 int getMaximumElements() const { 504 QualType ElmtType = getElementType(); 505 int maxElements = static_cast<int>(getSize().getZExtValue()); 506 507 const ConstantArrayType *CAT; 508 // If we have a multi-dimensional array, include it's elements. 509 while ((CAT = ElmtType->getAsConstantArrayType())) { 510 ElmtType = CAT->getElementType(); 511 maxElements *= static_cast<int>(CAT->getSize().getZExtValue()); 512 } 513 return maxElements; 514 } 515 virtual void getAsStringInternal(std::string &InnerString) const; 516 517 void Profile(llvm::FoldingSetNodeID &ID) { 518 Profile(ID, getElementType(), getSize()); 519 } 520 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, 521 llvm::APInt ArraySize) { 522 ID.AddPointer(ET.getAsOpaquePtr()); 523 ID.AddInteger(ArraySize.getZExtValue()); 524 } 525 static bool classof(const Type *T) { 526 return T->getTypeClass() == ConstantArray; 527 } 528 static bool classof(const ConstantArrayType *) { return true; } 529}; 530 531// FIXME: VariableArrayType's aren't uniqued (since expressions aren't). 532class VariableArrayType : public ArrayType { 533 /// SizeExpr - An assignment expression. VLA's are only permitted within 534 /// a function block. 535 Expr *SizeExpr; 536 537 VariableArrayType(QualType et, QualType can, Expr *e, 538 ArraySizeModifier sm, unsigned tq) 539 : ArrayType(VariableArray, et, can, sm, tq), SizeExpr(e) {} 540 friend class ASTContext; // ASTContext creates these. 541public: 542 Expr *getSizeExpr() const { return SizeExpr; } 543 544 virtual void getAsStringInternal(std::string &InnerString) const; 545 546 static bool classof(const Type *T) { 547 return T->getTypeClass() == VariableArray; 548 } 549 static bool classof(const VariableArrayType *) { return true; } 550}; 551 552/// VectorType - GCC generic vector type. This type is created using 553/// __attribute__((vector_size(n)), where "n" specifies the vector size in 554/// bytes. Since the constructor takes the number of vector elements, the 555/// client is responsible for converting the size into the number of elements. 556class VectorType : public Type, public llvm::FoldingSetNode { 557protected: 558 /// ElementType - The element type of the vector. 559 QualType ElementType; 560 561 /// NumElements - The number of elements in the vector. 562 unsigned NumElements; 563 564 VectorType(QualType vecType, unsigned nElements, QualType canonType) : 565 Type(Vector, canonType), ElementType(vecType), NumElements(nElements) {} 566 VectorType(TypeClass tc, QualType vecType, unsigned nElements, 567 QualType canonType) : Type(tc, canonType), ElementType(vecType), 568 NumElements(nElements) {} 569 friend class ASTContext; // ASTContext creates these. 570public: 571 572 QualType getElementType() const { return ElementType; } 573 unsigned getNumElements() const { return NumElements; } 574 575 virtual void getAsStringInternal(std::string &InnerString) const; 576 577 void Profile(llvm::FoldingSetNodeID &ID) { 578 Profile(ID, getElementType(), getNumElements(), getTypeClass()); 579 } 580 static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, 581 unsigned NumElements, TypeClass TypeClass) { 582 ID.AddPointer(ElementType.getAsOpaquePtr()); 583 ID.AddInteger(NumElements); 584 ID.AddInteger(TypeClass); 585 } 586 static bool classof(const Type *T) { 587 return T->getTypeClass() == Vector || T->getTypeClass() == OCUVector; 588 } 589 static bool classof(const VectorType *) { return true; } 590}; 591 592/// OCUVectorType - Extended vector type. This type is created using 593/// __attribute__((ocu_vector_type(n)), where "n" is the number of elements. 594/// Unlike vector_size, ocu_vector_type is only allowed on typedef's. This 595/// class enables syntactic extensions, like Vector Components for accessing 596/// points, colors, and textures (modeled after OpenGL Shading Language). 597class OCUVectorType : public VectorType { 598 OCUVectorType(QualType vecType, unsigned nElements, QualType canonType) : 599 VectorType(OCUVector, vecType, nElements, canonType) {} 600 friend class ASTContext; // ASTContext creates these. 601public: 602 static int getPointAccessorIdx(char c) { 603 switch (c) { 604 default: return -1; 605 case 'x': return 0; 606 case 'y': return 1; 607 case 'z': return 2; 608 case 'w': return 3; 609 } 610 } 611 static int getColorAccessorIdx(char c) { 612 switch (c) { 613 default: return -1; 614 case 'r': return 0; 615 case 'g': return 1; 616 case 'b': return 2; 617 case 'a': return 3; 618 } 619 } 620 static int getTextureAccessorIdx(char c) { 621 switch (c) { 622 default: return -1; 623 case 's': return 0; 624 case 't': return 1; 625 case 'p': return 2; 626 case 'q': return 3; 627 } 628 }; 629 630 static int getAccessorIdx(char c) { 631 if (int idx = getPointAccessorIdx(c)+1) return idx-1; 632 if (int idx = getColorAccessorIdx(c)+1) return idx-1; 633 return getTextureAccessorIdx(c); 634 } 635 636 bool isAccessorWithinNumElements(char c) const { 637 if (int idx = getAccessorIdx(c)+1) 638 return unsigned(idx-1) < NumElements; 639 return false; 640 } 641 virtual void getAsStringInternal(std::string &InnerString) const; 642 643 static bool classof(const Type *T) { 644 return T->getTypeClass() == OCUVector; 645 } 646 static bool classof(const OCUVectorType *) { return true; } 647}; 648 649/// FunctionType - C99 6.7.5.3 - Function Declarators. This is the common base 650/// class of FunctionTypeNoProto and FunctionTypeProto. 651/// 652class FunctionType : public Type { 653 /// SubClassData - This field is owned by the subclass, put here to pack 654 /// tightly with the ivars in Type. 655 bool SubClassData : 1; 656 657 // The type returned by the function. 658 QualType ResultType; 659protected: 660 FunctionType(TypeClass tc, QualType res, bool SubclassInfo,QualType Canonical) 661 : Type(tc, Canonical), SubClassData(SubclassInfo), ResultType(res) {} 662 bool getSubClassData() const { return SubClassData; } 663public: 664 665 QualType getResultType() const { return ResultType; } 666 667 668 static bool classof(const Type *T) { 669 return T->getTypeClass() == FunctionNoProto || 670 T->getTypeClass() == FunctionProto; 671 } 672 static bool classof(const FunctionType *) { return true; } 673}; 674 675/// FunctionTypeNoProto - Represents a K&R-style 'int foo()' function, which has 676/// no information available about its arguments. 677class FunctionTypeNoProto : public FunctionType, public llvm::FoldingSetNode { 678 FunctionTypeNoProto(QualType Result, QualType Canonical) 679 : FunctionType(FunctionNoProto, Result, false, Canonical) {} 680 friend class ASTContext; // ASTContext creates these. 681public: 682 // No additional state past what FunctionType provides. 683 684 virtual void getAsStringInternal(std::string &InnerString) const; 685 686 void Profile(llvm::FoldingSetNodeID &ID) { 687 Profile(ID, getResultType()); 688 } 689 static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType) { 690 ID.AddPointer(ResultType.getAsOpaquePtr()); 691 } 692 693 static bool classof(const Type *T) { 694 return T->getTypeClass() == FunctionNoProto; 695 } 696 static bool classof(const FunctionTypeNoProto *) { return true; } 697}; 698 699/// FunctionTypeProto - Represents a prototype with argument type info, e.g. 700/// 'int foo(int)' or 'int foo(void)'. 'void' is represented as having no 701/// arguments, not as having a single void argument. 702class FunctionTypeProto : public FunctionType, public llvm::FoldingSetNode { 703 FunctionTypeProto(QualType Result, QualType *ArgArray, unsigned numArgs, 704 bool isVariadic, QualType Canonical) 705 : FunctionType(FunctionProto, Result, isVariadic, Canonical), 706 NumArgs(numArgs) { 707 // Fill in the trailing argument array. 708 QualType *ArgInfo = reinterpret_cast<QualType *>(this+1);; 709 for (unsigned i = 0; i != numArgs; ++i) 710 ArgInfo[i] = ArgArray[i]; 711 } 712 713 /// NumArgs - The number of arguments this function has, not counting '...'. 714 unsigned NumArgs; 715 716 /// ArgInfo - There is an variable size array after the class in memory that 717 /// holds the argument types. 718 friend class ASTContext; // ASTContext creates these. 719public: 720 unsigned getNumArgs() const { return NumArgs; } 721 QualType getArgType(unsigned i) const { 722 assert(i < NumArgs && "Invalid argument number!"); 723 return arg_type_begin()[i]; 724 } 725 726 bool isVariadic() const { return getSubClassData(); } 727 728 typedef const QualType *arg_type_iterator; 729 arg_type_iterator arg_type_begin() const { 730 return reinterpret_cast<const QualType *>(this+1); 731 } 732 arg_type_iterator arg_type_end() const { return arg_type_begin()+NumArgs; } 733 734 virtual void getAsStringInternal(std::string &InnerString) const; 735 736 static bool classof(const Type *T) { 737 return T->getTypeClass() == FunctionProto; 738 } 739 static bool classof(const FunctionTypeProto *) { return true; } 740 741 void Profile(llvm::FoldingSetNodeID &ID); 742 static void Profile(llvm::FoldingSetNodeID &ID, QualType Result, 743 arg_type_iterator ArgTys, unsigned NumArgs, 744 bool isVariadic); 745}; 746 747 748class TypedefType : public Type { 749 TypedefDecl *Decl; 750 TypedefType(TypedefDecl *D, QualType can) : Type(TypeName, can), Decl(D) { 751 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 752 } 753 friend class ASTContext; // ASTContext creates these. 754public: 755 756 TypedefDecl *getDecl() const { return Decl; } 757 758 /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to 759 /// potentially looking through *all* consequtive typedefs. This returns the 760 /// sum of the type qualifiers, so if you have: 761 /// typedef const int A; 762 /// typedef volatile A B; 763 /// looking through the typedefs for B will give you "const volatile A". 764 QualType LookThroughTypedefs() const; 765 766 virtual void getAsStringInternal(std::string &InnerString) const; 767 768 static bool classof(const Type *T) { return T->getTypeClass() == TypeName; } 769 static bool classof(const TypedefType *) { return true; } 770}; 771 772/// TypeOfExpr (GCC extension). 773class TypeOfExpr : public Type { 774 Expr *TOExpr; 775 TypeOfExpr(Expr *E, QualType can) : Type(TypeOfExp, can), TOExpr(E) { 776 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 777 } 778 friend class ASTContext; // ASTContext creates these. 779public: 780 Expr *getUnderlyingExpr() const { return TOExpr; } 781 782 virtual void getAsStringInternal(std::string &InnerString) const; 783 784 static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExp; } 785 static bool classof(const TypeOfExpr *) { return true; } 786}; 787 788/// TypeOfType (GCC extension). 789class TypeOfType : public Type { 790 QualType TOType; 791 TypeOfType(QualType T, QualType can) : Type(TypeOfTyp, can), TOType(T) { 792 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 793 } 794 friend class ASTContext; // ASTContext creates these. 795public: 796 QualType getUnderlyingType() const { return TOType; } 797 798 virtual void getAsStringInternal(std::string &InnerString) const; 799 800 static bool classof(const Type *T) { return T->getTypeClass() == TypeOfTyp; } 801 static bool classof(const TypeOfType *) { return true; } 802}; 803 804class TagType : public Type { 805 TagDecl *Decl; 806 TagType(TagDecl *D, QualType can) : Type(Tagged, can), Decl(D) {} 807 friend class ASTContext; // ASTContext creates these. 808public: 809 810 TagDecl *getDecl() const { return Decl; } 811 812 virtual void getAsStringInternal(std::string &InnerString) const; 813 814 static bool classof(const Type *T) { return T->getTypeClass() == Tagged; } 815 static bool classof(const TagType *) { return true; } 816}; 817 818class ObjcInterfaceType : public Type { 819 ObjcInterfaceDecl *Decl; 820 ObjcInterfaceType(ObjcInterfaceDecl *D) : 821 Type(ObjcInterface, QualType()), Decl(D) { } 822 friend class ASTContext; // ASTContext creates these. 823public: 824 825 ObjcInterfaceDecl *getDecl() const { return Decl; } 826 827 virtual void getAsStringInternal(std::string &InnerString) const; 828 829 static bool classof(const Type *T) { 830 return T->getTypeClass() == ObjcInterface; 831 } 832 static bool classof(const ObjcInterfaceType *) { return true; } 833}; 834 835/// - ObjcQualifiedInterfaceType - This class represense interface types 836/// conforming to a list of protocols; such as, INTF<Proto1, Proto2, Proto1>. 837class ObjcQualifiedInterfaceType : public Type { 838 // Interface type for this protocol conforming object type 839 ObjcInterfaceType *InterfaceType; 840 841 // List of protocols for this protocol conforming object type 842 // List is sorted on protocol name. No protocol is enterred more than once. 843 llvm::SmallVector<ObjcProtocolDecl*, 8> Protocols; 844 845 ObjcQualifiedInterfaceType(ObjcInterfaceType *T) : 846 Type(ObjcQualifiedInterface, QualType()), InterfaceType(T) { } 847public: 848 849 ObjcInterfaceType *getInterfaceType() const { return InterfaceType; } 850 851 static bool classof(const Type *T) { 852 return T->getTypeClass() == ObjcQualifiedInterface; 853 } 854 static bool classof(const ObjcQualifiedInterfaceType *) { return true; } 855}; 856 857/// RecordType - This is a helper class that allows the use of isa/cast/dyncast 858/// to detect TagType objects of structs/unions/classes. 859class RecordType : public TagType { 860 RecordType(); // DO NOT IMPLEMENT 861public: 862 863 RecordDecl *getDecl() const { 864 return reinterpret_cast<RecordDecl*>(TagType::getDecl()); 865 } 866 867 // FIXME: This predicate is a helper to QualType/Type. It needs to 868 // recursively check all fields for const-ness. If any field is declared 869 // const, it needs to return false. 870 bool hasConstFields() const { return false; } 871 872 static bool classof(const Type *T); 873 static bool classof(const RecordType *) { return true; } 874}; 875 876 877// Inline function definitions. 878 879/// getCanonicalType - Return the canonical version of this type, with the 880/// appropriate type qualifiers on it. 881inline QualType QualType::getCanonicalType() const { 882 return QualType(getTypePtr()->getCanonicalTypeInternal().getTypePtr(), 883 getQualifiers() | 884 getTypePtr()->getCanonicalTypeInternal().getQualifiers()); 885} 886 887 888inline bool Type::isFunctionType() const { 889 return isa<FunctionType>(CanonicalType); 890} 891inline bool Type::isPointerType() const { 892 return isa<PointerType>(CanonicalType); 893} 894inline bool Type::isFunctionPointerType() const { 895 if (const PointerType* T = getAsPointerType()) 896 return T->getPointeeType()->isFunctionType(); 897 else 898 return false; 899} 900inline bool Type::isReferenceType() const { 901 return isa<ReferenceType>(CanonicalType); 902} 903inline bool Type::isArrayType() const { 904 return isa<ArrayType>(CanonicalType); 905} 906inline bool Type::isRecordType() const { 907 return isa<RecordType>(CanonicalType); 908} 909inline bool Type::isVectorType() const { 910 return isa<VectorType>(CanonicalType); 911} 912inline bool Type::isOCUVectorType() const { 913 return isa<OCUVectorType>(CanonicalType); 914} 915 916} // end namespace clang 917 918#endif 919