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