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