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