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