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