Type.h revision 0218936235b137bbdcd29a6c36d61d9215bb4edd
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  QualType getUnqualifiedType() const;
162  bool isMoreQualifiedThan(QualType Other) const;
163  bool isAtLeastAsQualifiedAs(QualType Other) const;
164  QualType getNonReferenceType() const;
165
166
167  /// operator==/!= - Indicate whether the specified types and qualifiers are
168  /// identical.
169  bool operator==(const QualType &RHS) const {
170    return ThePtr == RHS.ThePtr;
171  }
172  bool operator!=(const QualType &RHS) const {
173    return ThePtr != RHS.ThePtr;
174  }
175  std::string getAsString() const {
176    std::string S;
177    getAsStringInternal(S);
178    return S;
179  }
180  void getAsStringInternal(std::string &Str) const;
181
182  void dump(const char *s) const;
183  void dump() const;
184
185  /// viewInheritance - Renders and displays an inheritance diagram
186  /// for a C++ class and all of its base classes (transitively) using
187  /// GraphViz. Only available in debug builds.
188  void viewInheritance(ASTContext& Context);
189
190  void Profile(llvm::FoldingSetNodeID &ID) const {
191    ID.AddPointer(getAsOpaquePtr());
192  }
193
194public:
195
196  /// getAddressSpace - Return the address space of this type.
197  inline unsigned getAddressSpace() const;
198
199  /// Emit - Serialize a QualType to Bitcode.
200  void Emit(llvm::Serializer& S) const;
201
202  /// Read - Deserialize a QualType from Bitcode.
203  static QualType ReadVal(llvm::Deserializer& D);
204
205  void ReadBackpatch(llvm::Deserializer& D);
206};
207
208} // end clang.
209
210namespace llvm {
211/// Implement simplify_type for QualType, so that we can dyn_cast from QualType
212/// to a specific Type class.
213template<> struct simplify_type<const ::clang::QualType> {
214  typedef ::clang::Type* SimpleType;
215  static SimpleType getSimplifiedValue(const ::clang::QualType &Val) {
216    return Val.getTypePtr();
217  }
218};
219template<> struct simplify_type< ::clang::QualType>
220  : public simplify_type<const ::clang::QualType> {};
221
222} // end namespace llvm
223
224namespace clang {
225
226/// Type - This is the base class of the type hierarchy.  A central concept
227/// with types is that each type always has a canonical type.  A canonical type
228/// is the type with any typedef names stripped out of it or the types it
229/// references.  For example, consider:
230///
231///  typedef int  foo;
232///  typedef foo* bar;
233///    'int *'    'foo *'    'bar'
234///
235/// There will be a Type object created for 'int'.  Since int is canonical, its
236/// canonicaltype pointer points to itself.  There is also a Type for 'foo' (a
237/// TypeNameType).  Its CanonicalType pointer points to the 'int' Type.  Next
238/// there is a PointerType that represents 'int*', which, like 'int', is
239/// canonical.  Finally, there is a PointerType type for 'foo*' whose canonical
240/// type is 'int*', and there is a TypeNameType for 'bar', whose canonical type
241/// is also 'int*'.
242///
243/// Non-canonical types are useful for emitting diagnostics, without losing
244/// information about typedefs being used.  Canonical types are useful for type
245/// comparisons (they allow by-pointer equality tests) and useful for reasoning
246/// about whether something has a particular form (e.g. is a function type),
247/// because they implicitly, recursively, strip all typedefs out of a type.
248///
249/// Types, once created, are immutable.
250///
251class Type {
252public:
253  enum TypeClass {
254    Builtin, Complex, Pointer, Reference,
255    ConstantArray, VariableArray, IncompleteArray,
256    Vector, ExtVector,
257    FunctionNoProto, FunctionProto,
258    TypeName, Tagged, ASQual,
259    ObjCInterface, ObjCQualifiedInterface,
260    ObjCQualifiedId,
261    TypeOfExp, TypeOfTyp, // GNU typeof extension.
262    BlockPointer          // C extension
263  };
264private:
265  QualType CanonicalType;
266
267  /// TypeClass bitfield - Enum that specifies what subclass this belongs to.
268  /// Note that this should stay at the end of the ivars for Type so that
269  /// subclasses can pack their bitfields into the same word.
270  unsigned TC : 5;
271protected:
272  // silence VC++ warning C4355: 'this' : used in base member initializer list
273  Type *this_() { return this; }
274  Type(TypeClass tc, QualType Canonical)
275    : CanonicalType(Canonical.isNull() ? QualType(this_(), 0) : Canonical),
276      TC(tc) {}
277  virtual ~Type() {};
278  virtual void Destroy(ASTContext& C);
279  friend class ASTContext;
280
281  void EmitTypeInternal(llvm::Serializer& S) const;
282  void ReadTypeInternal(llvm::Deserializer& D);
283
284public:
285  TypeClass getTypeClass() const { return static_cast<TypeClass>(TC); }
286
287  bool isCanonical() const { return CanonicalType.getTypePtr() == this; }
288
289  /// Types are partitioned into 3 broad categories (C99 6.2.5p1):
290  /// object types, function types, and incomplete types.
291
292  /// isObjectType - types that fully describe objects. An object is a region
293  /// of memory that can be examined and stored into (H&S).
294  bool isObjectType() const;
295
296  /// isIncompleteType - Return true if this is an incomplete type.
297  /// A type that can describe objects, but which lacks information needed to
298  /// determine its size (e.g. void, or a fwd declared struct). Clients of this
299  /// routine will need to determine if the size is actually required.
300  bool isIncompleteType() const;
301
302  /// isIncompleteOrObjectType - Return true if this is an incomplete or object
303  /// type, in other words, not a function type.
304  bool isIncompleteOrObjectType() const {
305    return !isFunctionType();
306  }
307
308  /// isVariablyModifiedType (C99 6.7.5.2p2) - Return true for variable array
309  /// types that have a non-constant expression. This does not include "[]".
310  bool isVariablyModifiedType() const;
311
312  /// Helper methods to distinguish type categories. All type predicates
313  /// operate on the canonical type, ignoring typedefs and qualifiers.
314
315  /// isIntegerType() does *not* include complex integers (a GCC extension).
316  /// isComplexIntegerType() can be used to test for complex integers.
317  bool isIntegerType() const;     // C99 6.2.5p17 (int, char, bool, enum)
318  bool isEnumeralType() const;
319  bool isBooleanType() const;
320  bool isCharType() const;
321  bool isWideCharType() const;
322  bool isIntegralType() const;
323
324  /// Floating point categories.
325  bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double)
326  /// isComplexType() does *not* include complex integers (a GCC extension).
327  /// isComplexIntegerType() can be used to test for complex integers.
328  bool isComplexType() const;      // C99 6.2.5p11 (complex)
329  bool isAnyComplexType() const;   // C99 6.2.5p11 (complex) + Complex Int.
330  bool isFloatingType() const;     // C99 6.2.5p11 (real floating + complex)
331  bool isRealType() const;         // C99 6.2.5p17 (real floating + integer)
332  bool isArithmeticType() const;   // C99 6.2.5p18 (integer + floating)
333  bool isVoidType() const;         // C99 6.2.5p19
334  bool isDerivedType() const;      // C99 6.2.5p20
335  bool isScalarType() const;       // C99 6.2.5p21 (arithmetic + pointers)
336  bool isAggregateType() const;    // C99 6.2.5p21 (arrays, structures)
337
338  // Type Predicates: Check to see if this type is structurally the specified
339  // type, ignoring typedefs and qualifiers.
340  bool isFunctionType() const;
341  bool isPointerLikeType() const; // Pointer or Reference.
342  bool isPointerType() const;
343  bool isBlockPointerType() const;
344  bool isReferenceType() const;
345  bool isFunctionPointerType() const;
346  bool isArrayType() const;
347  bool isConstantArrayType() const;
348  bool isIncompleteArrayType() const;
349  bool isVariableArrayType() const;
350  bool isRecordType() const;
351  bool isClassType() const;
352  bool isStructureType() const;
353  bool isUnionType() const;
354  bool isComplexIntegerType() const;            // GCC _Complex integer type.
355  bool isVectorType() const;                    // GCC vector type.
356  bool isExtVectorType() const;                 // Extended vector type.
357  bool isObjCInterfaceType() const;             // NSString or NSString<foo>
358  bool isObjCQualifiedInterfaceType() const;    // NSString<foo>
359  bool isObjCQualifiedIdType() const;           // id<foo>
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  // The type returned by the function.
927  QualType ResultType;
928protected:
929  FunctionType(TypeClass tc, QualType res, bool SubclassInfo,QualType Canonical)
930    : Type(tc, Canonical), SubClassData(SubclassInfo), ResultType(res) {}
931  bool getSubClassData() const { return SubClassData; }
932public:
933
934  QualType getResultType() const { return ResultType; }
935
936
937  static bool classof(const Type *T) {
938    return T->getTypeClass() == FunctionNoProto ||
939           T->getTypeClass() == FunctionProto;
940  }
941  static bool classof(const FunctionType *) { return true; }
942};
943
944/// FunctionTypeNoProto - Represents a K&R-style 'int foo()' function, which has
945/// no information available about its arguments.
946class FunctionTypeNoProto : public FunctionType, public llvm::FoldingSetNode {
947  FunctionTypeNoProto(QualType Result, QualType Canonical)
948    : FunctionType(FunctionNoProto, Result, false, Canonical) {}
949  friend class ASTContext;  // ASTContext creates these.
950public:
951  // No additional state past what FunctionType provides.
952
953  virtual void getAsStringInternal(std::string &InnerString) const;
954
955  void Profile(llvm::FoldingSetNodeID &ID) {
956    Profile(ID, getResultType());
957  }
958  static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType) {
959    ID.AddPointer(ResultType.getAsOpaquePtr());
960  }
961
962  static bool classof(const Type *T) {
963    return T->getTypeClass() == FunctionNoProto;
964  }
965  static bool classof(const FunctionTypeNoProto *) { return true; }
966
967protected:
968  virtual void EmitImpl(llvm::Serializer& S) const;
969  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
970  friend class Type;
971};
972
973/// FunctionTypeProto - Represents a prototype with argument type info, e.g.
974/// 'int foo(int)' or 'int foo(void)'.  'void' is represented as having no
975/// arguments, not as having a single void argument.
976class FunctionTypeProto : public FunctionType, public llvm::FoldingSetNode {
977  FunctionTypeProto(QualType Result, const QualType *ArgArray, unsigned numArgs,
978                    bool isVariadic, QualType Canonical)
979    : FunctionType(FunctionProto, Result, isVariadic, Canonical),
980      NumArgs(numArgs) {
981    // Fill in the trailing argument array.
982    QualType *ArgInfo = reinterpret_cast<QualType *>(this+1);;
983    for (unsigned i = 0; i != numArgs; ++i)
984      ArgInfo[i] = ArgArray[i];
985  }
986
987  /// NumArgs - The number of arguments this function has, not counting '...'.
988  unsigned NumArgs;
989
990  /// ArgInfo - There is an variable size array after the class in memory that
991  /// holds the argument types.
992
993  friend class ASTContext;  // ASTContext creates these.
994  virtual void Destroy(ASTContext& C);
995
996public:
997  unsigned getNumArgs() const { return NumArgs; }
998  QualType getArgType(unsigned i) const {
999    assert(i < NumArgs && "Invalid argument number!");
1000    return arg_type_begin()[i];
1001  }
1002
1003  bool isVariadic() const { return getSubClassData(); }
1004
1005  typedef const QualType *arg_type_iterator;
1006  arg_type_iterator arg_type_begin() const {
1007    return reinterpret_cast<const QualType *>(this+1);
1008  }
1009  arg_type_iterator arg_type_end() const { return arg_type_begin()+NumArgs; }
1010
1011  virtual void getAsStringInternal(std::string &InnerString) const;
1012
1013  static bool classof(const Type *T) {
1014    return T->getTypeClass() == FunctionProto;
1015  }
1016  static bool classof(const FunctionTypeProto *) { return true; }
1017
1018  void Profile(llvm::FoldingSetNodeID &ID);
1019  static void Profile(llvm::FoldingSetNodeID &ID, QualType Result,
1020                      arg_type_iterator ArgTys, unsigned NumArgs,
1021                      bool isVariadic);
1022
1023protected:
1024  virtual void EmitImpl(llvm::Serializer& S) const;
1025  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
1026  friend class Type;
1027};
1028
1029
1030class TypedefType : public Type {
1031  TypedefDecl *Decl;
1032protected:
1033  TypedefType(TypeClass tc, TypedefDecl *D, QualType can)
1034    : Type(tc, can), Decl(D) {
1035    assert(!isa<TypedefType>(can) && "Invalid canonical type");
1036  }
1037  friend class ASTContext;  // ASTContext creates these.
1038public:
1039
1040  TypedefDecl *getDecl() const { return Decl; }
1041
1042  /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
1043  /// potentially looking through *all* consequtive typedefs.  This returns the
1044  /// sum of the type qualifiers, so if you have:
1045  ///   typedef const int A;
1046  ///   typedef volatile A B;
1047  /// looking through the typedefs for B will give you "const volatile A".
1048  QualType LookThroughTypedefs() const;
1049
1050  virtual void getAsStringInternal(std::string &InnerString) const;
1051
1052  static bool classof(const Type *T) { return T->getTypeClass() == TypeName; }
1053  static bool classof(const TypedefType *) { return true; }
1054
1055protected:
1056  virtual void EmitImpl(llvm::Serializer& S) const;
1057  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
1058  friend class Type;
1059};
1060
1061/// TypeOfExpr (GCC extension).
1062class TypeOfExpr : public Type {
1063  Expr *TOExpr;
1064  TypeOfExpr(Expr *E, QualType can) : Type(TypeOfExp, can), TOExpr(E) {
1065    assert(!isa<TypedefType>(can) && "Invalid canonical type");
1066  }
1067  friend class ASTContext;  // ASTContext creates these.
1068public:
1069  Expr *getUnderlyingExpr() const { return TOExpr; }
1070
1071  virtual void getAsStringInternal(std::string &InnerString) const;
1072
1073  static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExp; }
1074  static bool classof(const TypeOfExpr *) { return true; }
1075};
1076
1077/// TypeOfType (GCC extension).
1078class TypeOfType : public Type {
1079  QualType TOType;
1080  TypeOfType(QualType T, QualType can) : Type(TypeOfTyp, can), TOType(T) {
1081    assert(!isa<TypedefType>(can) && "Invalid canonical type");
1082  }
1083  friend class ASTContext;  // ASTContext creates these.
1084public:
1085  QualType getUnderlyingType() const { return TOType; }
1086
1087  virtual void getAsStringInternal(std::string &InnerString) const;
1088
1089  static bool classof(const Type *T) { return T->getTypeClass() == TypeOfTyp; }
1090  static bool classof(const TypeOfType *) { return true; }
1091};
1092
1093class TagType : public Type {
1094  TagDecl *decl;
1095  friend class ASTContext;
1096
1097protected:
1098  TagType(TagDecl *D, QualType can) : Type(Tagged, can), decl(D) {}
1099
1100public:
1101  TagDecl *getDecl() const { return decl; }
1102
1103  virtual void getAsStringInternal(std::string &InnerString) const;
1104
1105  static bool classof(const Type *T) { return T->getTypeClass() == Tagged; }
1106  static bool classof(const TagType *) { return true; }
1107
1108protected:
1109  virtual void EmitImpl(llvm::Serializer& S) const;
1110  static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D);
1111  friend class Type;
1112};
1113
1114/// RecordType - This is a helper class that allows the use of isa/cast/dyncast
1115/// to detect TagType objects of structs/unions/classes.
1116class RecordType : public TagType {
1117protected:
1118  explicit RecordType(RecordDecl *D)
1119    : TagType(reinterpret_cast<TagDecl*>(D), QualType()) { }
1120  friend class ASTContext;   // ASTContext creates these.
1121public:
1122
1123  RecordDecl *getDecl() const {
1124    return reinterpret_cast<RecordDecl*>(TagType::getDecl());
1125  }
1126
1127  // FIXME: This predicate is a helper to QualType/Type. It needs to
1128  // recursively check all fields for const-ness. If any field is declared
1129  // const, it needs to return false.
1130  bool hasConstFields() const { return false; }
1131
1132  // FIXME: RecordType needs to check when it is created that all fields are in
1133  // the same address space, and return that.
1134  unsigned getAddressSpace() const { return 0; }
1135
1136  static bool classof(const TagType *T);
1137  static bool classof(const Type *T) {
1138    return isa<TagType>(T) && classof(cast<TagType>(T));
1139  }
1140  static bool classof(const RecordType *) { return true; }
1141};
1142
1143/// CXXRecordType - This is a helper class that allows the use of
1144/// isa/cast/dyncast to detect TagType objects of C++ structs/unions/classes.
1145class CXXRecordType : public RecordType {
1146  explicit CXXRecordType(CXXRecordDecl *D)
1147    : RecordType(reinterpret_cast<RecordDecl*>(D)) { }
1148  friend class ASTContext;   // ASTContext creates these.
1149public:
1150
1151  CXXRecordDecl *getDecl() const {
1152    return reinterpret_cast<CXXRecordDecl*>(RecordType::getDecl());
1153  }
1154
1155  static bool classof(const TagType *T);
1156  static bool classof(const Type *T) {
1157    return isa<TagType>(T) && classof(cast<TagType>(T));
1158  }
1159  static bool classof(const CXXRecordType *) { return true; }
1160};
1161
1162/// EnumType - This is a helper class that allows the use of isa/cast/dyncast
1163/// to detect TagType objects of enums.
1164class EnumType : public TagType {
1165  explicit EnumType(EnumDecl *D)
1166    : TagType(reinterpret_cast<TagDecl*>(D), QualType()) { }
1167  friend class ASTContext;   // ASTContext creates these.
1168public:
1169
1170  EnumDecl *getDecl() const {
1171    return reinterpret_cast<EnumDecl*>(TagType::getDecl());
1172  }
1173
1174  static bool classof(const TagType *T);
1175  static bool classof(const Type *T) {
1176    return isa<TagType>(T) && classof(cast<TagType>(T));
1177  }
1178  static bool classof(const EnumType *) { return true; }
1179};
1180
1181
1182
1183/// ObjCInterfaceType - Interfaces are the core concept in Objective-C for
1184/// object oriented design.  They basically correspond to C++ classes.  There
1185/// are two kinds of interface types, normal interfaces like "NSString" and
1186/// qualified interfaces, which are qualified with a protocol list like
1187/// "NSString<NSCopyable, NSAmazing>".  Qualified interface types are instances
1188/// of ObjCQualifiedInterfaceType, which is a subclass of ObjCInterfaceType.
1189class ObjCInterfaceType : public Type {
1190  ObjCInterfaceDecl *Decl;
1191protected:
1192  ObjCInterfaceType(TypeClass tc, ObjCInterfaceDecl *D) :
1193    Type(tc, QualType()), Decl(D) { }
1194  friend class ASTContext;  // ASTContext creates these.
1195public:
1196
1197  ObjCInterfaceDecl *getDecl() const { return Decl; }
1198
1199  /// qual_iterator and friends: this provides access to the (potentially empty)
1200  /// list of protocols qualifying this interface.  If this is an instance of
1201  /// ObjCQualifiedInterfaceType it returns the list, otherwise it returns an
1202  /// empty list if there are no qualifying protocols.
1203  typedef llvm::SmallVector<ObjCProtocolDecl*, 8>::const_iterator qual_iterator;
1204  inline qual_iterator qual_begin() const;
1205  inline qual_iterator qual_end() const;
1206  bool qual_empty() const { return getTypeClass() != ObjCQualifiedInterface; }
1207
1208  /// getNumProtocols - Return the number of qualifying protocols in this
1209  /// interface type, or 0 if there are none.
1210  inline unsigned getNumProtocols() const;
1211
1212  /// getProtocol - Return the specified qualifying protocol.
1213  inline ObjCProtocolDecl *getProtocol(unsigned i) const;
1214
1215
1216  virtual void getAsStringInternal(std::string &InnerString) const;
1217  static bool classof(const Type *T) {
1218    return T->getTypeClass() == ObjCInterface ||
1219           T->getTypeClass() == ObjCQualifiedInterface;
1220  }
1221  static bool classof(const ObjCInterfaceType *) { return true; }
1222};
1223
1224/// ObjCQualifiedInterfaceType - This class represents interface types
1225/// conforming to a list of protocols, such as INTF<Proto1, Proto2, Proto1>.
1226///
1227/// Duplicate protocols are removed and protocol list is canonicalized to be in
1228/// alphabetical order.
1229class ObjCQualifiedInterfaceType : public ObjCInterfaceType,
1230                                   public llvm::FoldingSetNode {
1231
1232  // List of protocols for this protocol conforming object type
1233  // List is sorted on protocol name. No protocol is enterred more than once.
1234  llvm::SmallVector<ObjCProtocolDecl*, 4> Protocols;
1235
1236  ObjCQualifiedInterfaceType(ObjCInterfaceDecl *D,
1237                             ObjCProtocolDecl **Protos, unsigned NumP) :
1238    ObjCInterfaceType(ObjCQualifiedInterface, D),
1239    Protocols(Protos, Protos+NumP) { }
1240  friend class ASTContext;  // ASTContext creates these.
1241public:
1242
1243  ObjCProtocolDecl *getProtocol(unsigned i) const {
1244    return Protocols[i];
1245  }
1246  unsigned getNumProtocols() const {
1247    return Protocols.size();
1248  }
1249
1250  qual_iterator qual_begin() const { return Protocols.begin(); }
1251  qual_iterator qual_end() const   { return Protocols.end(); }
1252
1253  virtual void getAsStringInternal(std::string &InnerString) const;
1254
1255  void Profile(llvm::FoldingSetNodeID &ID);
1256  static void Profile(llvm::FoldingSetNodeID &ID,
1257                      const ObjCInterfaceDecl *Decl,
1258                      ObjCProtocolDecl **protocols, unsigned NumProtocols);
1259
1260  static bool classof(const Type *T) {
1261    return T->getTypeClass() == ObjCQualifiedInterface;
1262  }
1263  static bool classof(const ObjCQualifiedInterfaceType *) { return true; }
1264};
1265
1266inline ObjCInterfaceType::qual_iterator ObjCInterfaceType::qual_begin() const {
1267  if (const ObjCQualifiedInterfaceType *QIT =
1268         dyn_cast<ObjCQualifiedInterfaceType>(this))
1269    return QIT->qual_begin();
1270  return 0;
1271}
1272inline ObjCInterfaceType::qual_iterator ObjCInterfaceType::qual_end() const {
1273  if (const ObjCQualifiedInterfaceType *QIT =
1274         dyn_cast<ObjCQualifiedInterfaceType>(this))
1275    return QIT->qual_end();
1276  return 0;
1277}
1278
1279/// getNumProtocols - Return the number of qualifying protocols in this
1280/// interface type, or 0 if there are none.
1281inline unsigned ObjCInterfaceType::getNumProtocols() const {
1282  if (const ObjCQualifiedInterfaceType *QIT =
1283        dyn_cast<ObjCQualifiedInterfaceType>(this))
1284    return QIT->getNumProtocols();
1285  return 0;
1286}
1287
1288/// getProtocol - Return the specified qualifying protocol.
1289inline ObjCProtocolDecl *ObjCInterfaceType::getProtocol(unsigned i) const {
1290  return cast<ObjCQualifiedInterfaceType>(this)->getProtocol(i);
1291}
1292
1293
1294
1295/// ObjCQualifiedIdType - to represent id<protocol-list>.
1296///
1297/// Duplicate protocols are removed and protocol list is canonicalized to be in
1298/// alphabetical order.
1299class ObjCQualifiedIdType : public Type,
1300                            public llvm::FoldingSetNode {
1301  // List of protocols for this protocol conforming 'id' type
1302  // List is sorted on protocol name. No protocol is enterred more than once.
1303  llvm::SmallVector<ObjCProtocolDecl*, 8> Protocols;
1304
1305  ObjCQualifiedIdType(ObjCProtocolDecl **Protos, unsigned NumP)
1306    : Type(ObjCQualifiedId, QualType()/*these are always canonical*/),
1307  Protocols(Protos, Protos+NumP) { }
1308  friend class ASTContext;  // ASTContext creates these.
1309public:
1310
1311  ObjCProtocolDecl *getProtocols(unsigned i) const {
1312    return Protocols[i];
1313  }
1314  unsigned getNumProtocols() const {
1315    return Protocols.size();
1316  }
1317  ObjCProtocolDecl **getReferencedProtocols() {
1318    return &Protocols[0];
1319  }
1320
1321  typedef llvm::SmallVector<ObjCProtocolDecl*, 8>::const_iterator qual_iterator;
1322  qual_iterator qual_begin() const { return Protocols.begin(); }
1323  qual_iterator qual_end() const   { return Protocols.end(); }
1324
1325  virtual void getAsStringInternal(std::string &InnerString) const;
1326
1327  void Profile(llvm::FoldingSetNodeID &ID);
1328  static void Profile(llvm::FoldingSetNodeID &ID,
1329                      ObjCProtocolDecl **protocols, unsigned NumProtocols);
1330
1331  static bool classof(const Type *T) {
1332    return T->getTypeClass() == ObjCQualifiedId;
1333  }
1334  static bool classof(const ObjCQualifiedIdType *) { return true; }
1335
1336};
1337
1338
1339// Inline function definitions.
1340
1341/// getUnqualifiedType - Return the type without any qualifiers.
1342inline QualType QualType::getUnqualifiedType() const {
1343  Type *TP = getTypePtr();
1344  if (const ASQualType *ASQT = dyn_cast<ASQualType>(TP))
1345    TP = ASQT->getBaseType();
1346  return QualType(TP, 0);
1347}
1348
1349/// getAddressSpace - Return the address space of this type.
1350inline unsigned QualType::getAddressSpace() const {
1351  QualType CT = getTypePtr()->getCanonicalTypeInternal();
1352  if (const ArrayType *AT = dyn_cast<ArrayType>(CT))
1353    return AT->getElementType().getAddressSpace();
1354  if (const RecordType *RT = dyn_cast<RecordType>(CT))
1355    return RT->getAddressSpace();
1356  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CT))
1357    return ASQT->getAddressSpace();
1358  return 0;
1359}
1360
1361/// isMoreQualifiedThan - Determine whether this type is more
1362/// qualified than the Other type. For example, "const volatile int"
1363/// is more qualified than "const int", "volatile int", and
1364/// "int". However, it is not more qualified than "const volatile
1365/// int".
1366inline bool QualType::isMoreQualifiedThan(QualType Other) const {
1367  // FIXME: Handle address spaces
1368  unsigned MyQuals = this->getCVRQualifiers();
1369  unsigned OtherQuals = Other.getCVRQualifiers();
1370  assert(this->getAddressSpace() == 0 && "Address space not checked");
1371  assert(Other.getAddressSpace() == 0 && "Address space not checked");
1372  return MyQuals != OtherQuals && (MyQuals | OtherQuals) == MyQuals;
1373}
1374
1375/// isAtLeastAsQualifiedAs - Determine whether this type is at last
1376/// as qualified as the Other type. For example, "const volatile
1377/// int" is at least as qualified as "const int", "volatile int",
1378/// "int", and "const volatile int".
1379inline bool QualType::isAtLeastAsQualifiedAs(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;
1386}
1387
1388/// getNonReferenceType - If Type is a reference type (e.g., const
1389/// int&), returns the type that the reference refers to ("const
1390/// int"). Otherwise, returns the type itself. This routine is used
1391/// throughout Sema to implement C++ 5p6:
1392///
1393///   If an expression initially has the type "reference to T" (8.3.2,
1394///   8.5.3), the type is adjusted to "T" prior to any further
1395///   analysis, the expression designates the object or function
1396///   denoted by the reference, and the expression is an lvalue.
1397inline QualType QualType::getNonReferenceType() const {
1398  if (const ReferenceType *RefType = (*this)->getAsReferenceType())
1399    return RefType->getPointeeType();
1400  else
1401    return *this;
1402}
1403
1404inline const TypedefType* Type::getAsTypedefType() const {
1405  return dyn_cast<TypedefType>(this);
1406}
1407inline const ObjCInterfaceType *Type::getAsPointerToObjCInterfaceType() const {
1408  if (const PointerType *PT = getAsPointerType())
1409    return PT->getPointeeType()->getAsObjCInterfaceType();
1410  return 0;
1411}
1412
1413// NOTE: All of these methods use "getUnqualifiedType" to strip off address
1414// space qualifiers if present.
1415inline bool Type::isFunctionType() const {
1416  return isa<FunctionType>(CanonicalType.getUnqualifiedType());
1417}
1418inline bool Type::isPointerType() const {
1419  return isa<PointerType>(CanonicalType.getUnqualifiedType());
1420}
1421inline bool Type::isBlockPointerType() const {
1422    return isa<BlockPointerType>(CanonicalType);
1423}
1424inline bool Type::isReferenceType() const {
1425  return isa<ReferenceType>(CanonicalType.getUnqualifiedType());
1426}
1427inline bool Type::isPointerLikeType() const {
1428  return isa<PointerLikeType>(CanonicalType.getUnqualifiedType());
1429}
1430inline bool Type::isFunctionPointerType() const {
1431  if (const PointerType* T = getAsPointerType())
1432    return T->getPointeeType()->isFunctionType();
1433  else
1434    return false;
1435}
1436inline bool Type::isArrayType() const {
1437  return isa<ArrayType>(CanonicalType.getUnqualifiedType());
1438}
1439inline bool Type::isConstantArrayType() const {
1440  return isa<ConstantArrayType>(CanonicalType.getUnqualifiedType());
1441}
1442inline bool Type::isIncompleteArrayType() const {
1443  return isa<IncompleteArrayType>(CanonicalType.getUnqualifiedType());
1444}
1445inline bool Type::isVariableArrayType() const {
1446  return isa<VariableArrayType>(CanonicalType.getUnqualifiedType());
1447}
1448inline bool Type::isRecordType() const {
1449  return isa<RecordType>(CanonicalType.getUnqualifiedType());
1450}
1451inline bool Type::isAnyComplexType() const {
1452  return isa<ComplexType>(CanonicalType.getUnqualifiedType());
1453}
1454inline bool Type::isVectorType() const {
1455  return isa<VectorType>(CanonicalType.getUnqualifiedType());
1456}
1457inline bool Type::isExtVectorType() const {
1458  return isa<ExtVectorType>(CanonicalType.getUnqualifiedType());
1459}
1460inline bool Type::isObjCInterfaceType() const {
1461  return isa<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
1462}
1463inline bool Type::isObjCQualifiedInterfaceType() const {
1464  return isa<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType());
1465}
1466inline bool Type::isObjCQualifiedIdType() const {
1467  return isa<ObjCQualifiedIdType>(CanonicalType.getUnqualifiedType());
1468}
1469}  // end namespace clang
1470
1471#endif
1472