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