Type.h revision 7e7eb3da052a6d80ddf2377cab0384c798f73f75
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 : 6;
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() == ConstantArrayWithExpr ||
843           T->getTypeClass() == ConstantArrayWithoutExpr ||
844           T->getTypeClass() == VariableArray ||
845           T->getTypeClass() == IncompleteArray ||
846           T->getTypeClass() == DependentSizedArray;
847  }
848  static bool classof(const ArrayType *) { return true; }
849};
850
851/// ConstantArrayType - This class represents the canonical version of
852/// C arrays with a specified constant size.  For example, the canonical
853/// type for 'int A[4 + 4*100]' is a ConstantArrayType where the element
854/// type is 'int' and the size is 404.
855class ConstantArrayType : public ArrayType {
856  llvm::APInt Size; // Allows us to unique the type.
857
858  ConstantArrayType(QualType et, QualType can, const llvm::APInt &size,
859                    ArraySizeModifier sm, unsigned tq)
860    : ArrayType(ConstantArray, et, can, sm, tq),
861      Size(size) {}
862protected:
863  ConstantArrayType(TypeClass tc, QualType et, QualType can,
864                    const llvm::APInt &size, ArraySizeModifier sm, unsigned tq)
865    : ArrayType(tc, et, can, sm, tq), Size(size) {}
866  friend class ASTContext;  // ASTContext creates these.
867public:
868  const llvm::APInt &getSize() const { return Size; }
869  virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const;
870
871  void Profile(llvm::FoldingSetNodeID &ID) {
872    Profile(ID, getElementType(), getSize(),
873            getSizeModifier(), getIndexTypeQualifier());
874  }
875  static void Profile(llvm::FoldingSetNodeID &ID, QualType ET,
876                      const llvm::APInt &ArraySize, ArraySizeModifier SizeMod,
877                      unsigned TypeQuals) {
878    ID.AddPointer(ET.getAsOpaquePtr());
879    ID.AddInteger(ArraySize.getZExtValue());
880    ID.AddInteger(SizeMod);
881    ID.AddInteger(TypeQuals);
882  }
883  static bool classof(const Type *T) {
884    return T->getTypeClass() == ConstantArray ||
885           T->getTypeClass() == ConstantArrayWithExpr ||
886           T->getTypeClass() == ConstantArrayWithoutExpr;
887  }
888  static bool classof(const ConstantArrayType *) { return true; }
889};
890
891/// ConstantArrayWithExprType - This class represents C arrays with a
892/// constant size specified by means of an integer constant expression.
893/// For example 'int A[sizeof(int)]' has ConstantArrayWithExprType where
894/// the element type is 'int' and the size expression is 'sizeof(int)'.
895/// These types are non-canonical.
896class ConstantArrayWithExprType : public ConstantArrayType {
897  /// SizeExpr - The ICE occurring in the concrete syntax.
898  Expr *SizeExpr;
899  /// Brackets - The left and right array brackets.
900  SourceRange Brackets;
901
902  ConstantArrayWithExprType(QualType et, QualType can,
903                            const llvm::APInt &size, Expr *e,
904                            ArraySizeModifier sm, unsigned tq,
905                            SourceRange brackets)
906    : ConstantArrayType(ConstantArrayWithExpr, et, can, size, sm, tq),
907      SizeExpr(e), Brackets(brackets) {}
908  friend class ASTContext;  // ASTContext creates these.
909  virtual void Destroy(ASTContext& C);
910
911public:
912  Expr *getSizeExpr() const { return SizeExpr; }
913  SourceRange getBracketsRange() const { return Brackets; }
914  SourceLocation getLBracketLoc() const { return Brackets.getBegin(); }
915  SourceLocation getRBracketLoc() const { return Brackets.getEnd(); }
916
917  virtual void getAsStringInternal(std::string &InnerString,
918                                   const PrintingPolicy &Policy) const;
919
920  static bool classof(const Type *T) {
921    return T->getTypeClass() == ConstantArrayWithExpr;
922  }
923  static bool classof(const ConstantArrayWithExprType *) { return true; }
924
925  void Profile(llvm::FoldingSetNodeID &ID) {
926    assert(0 && "Cannot unique ConstantArrayWithExprTypes.");
927  }
928};
929
930/// ConstantArrayWithoutExprType - This class represents C arrays with a
931/// constant size that was not specified by an integer constant expression,
932/// but inferred by static semantics.
933/// For example 'int A[] = { 0, 1, 2 }' has ConstantArrayWithoutExprType.
934/// These types are non-canonical: the corresponding canonical type,
935/// having the size specified in an APInt object, is a ConstantArrayType.
936class ConstantArrayWithoutExprType : public ConstantArrayType {
937
938  ConstantArrayWithoutExprType(QualType et, QualType can,
939                               const llvm::APInt &size,
940                               ArraySizeModifier sm, unsigned tq)
941    : ConstantArrayType(ConstantArrayWithoutExpr, et, can, size, sm, tq) {}
942  friend class ASTContext;  // ASTContext creates these.
943
944public:
945  virtual void getAsStringInternal(std::string &InnerString,
946                                   const PrintingPolicy &Policy) const;
947
948  static bool classof(const Type *T) {
949    return T->getTypeClass() == ConstantArrayWithoutExpr;
950  }
951  static bool classof(const ConstantArrayWithoutExprType *) { return true; }
952
953  void Profile(llvm::FoldingSetNodeID &ID) {
954    assert(0 && "Cannot unique ConstantArrayWithoutExprTypes.");
955  }
956};
957
958/// IncompleteArrayType - This class represents C arrays with an unspecified
959/// size.  For example 'int A[]' has an IncompleteArrayType where the element
960/// type is 'int' and the size is unspecified.
961class IncompleteArrayType : public ArrayType {
962
963  IncompleteArrayType(QualType et, QualType can,
964                      ArraySizeModifier sm, unsigned tq)
965    : ArrayType(IncompleteArray, et, can, sm, tq) {}
966  friend class ASTContext;  // ASTContext creates these.
967public:
968  virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const;
969
970  static bool classof(const Type *T) {
971    return T->getTypeClass() == IncompleteArray;
972  }
973  static bool classof(const IncompleteArrayType *) { return true; }
974
975  friend class StmtIteratorBase;
976
977  void Profile(llvm::FoldingSetNodeID &ID) {
978    Profile(ID, getElementType(), getSizeModifier(), getIndexTypeQualifier());
979  }
980
981  static void Profile(llvm::FoldingSetNodeID &ID, QualType ET,
982                      ArraySizeModifier SizeMod, unsigned TypeQuals) {
983    ID.AddPointer(ET.getAsOpaquePtr());
984    ID.AddInteger(SizeMod);
985    ID.AddInteger(TypeQuals);
986  }
987};
988
989/// VariableArrayType - This class represents C arrays with a specified size
990/// which is not an integer-constant-expression.  For example, 'int s[x+foo()]'.
991/// Since the size expression is an arbitrary expression, we store it as such.
992///
993/// Note: VariableArrayType's aren't uniqued (since the expressions aren't) and
994/// should not be: two lexically equivalent variable array types could mean
995/// different things, for example, these variables do not have the same type
996/// dynamically:
997///
998/// void foo(int x) {
999///   int Y[x];
1000///   ++x;
1001///   int Z[x];
1002/// }
1003///
1004class VariableArrayType : public ArrayType {
1005  /// SizeExpr - An assignment expression. VLA's are only permitted within
1006  /// a function block.
1007  Stmt *SizeExpr;
1008  /// Brackets - The left and right array brackets.
1009  SourceRange Brackets;
1010
1011  VariableArrayType(QualType et, QualType can, Expr *e,
1012                    ArraySizeModifier sm, unsigned tq,
1013                    SourceRange brackets)
1014    : ArrayType(VariableArray, et, can, sm, tq),
1015      SizeExpr((Stmt*) e), Brackets(brackets) {}
1016  friend class ASTContext;  // ASTContext creates these.
1017  virtual void Destroy(ASTContext& C);
1018
1019public:
1020  Expr *getSizeExpr() const {
1021    // We use C-style casts instead of cast<> here because we do not wish
1022    // to have a dependency of Type.h on Stmt.h/Expr.h.
1023    return (Expr*) SizeExpr;
1024  }
1025  SourceRange getBracketsRange() const { return Brackets; }
1026  SourceLocation getLBracketLoc() const { return Brackets.getBegin(); }
1027  SourceLocation getRBracketLoc() const { return Brackets.getEnd(); }
1028
1029  virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const;
1030
1031  static bool classof(const Type *T) {
1032    return T->getTypeClass() == VariableArray;
1033  }
1034  static bool classof(const VariableArrayType *) { return true; }
1035
1036  friend class StmtIteratorBase;
1037
1038  void Profile(llvm::FoldingSetNodeID &ID) {
1039    assert(0 && "Cannnot unique VariableArrayTypes.");
1040  }
1041};
1042
1043/// DependentSizedArrayType - This type represents an array type in
1044/// C++ whose size is a value-dependent expression. For example:
1045/// @code
1046/// template<typename T, int Size>
1047/// class array {
1048///   T data[Size];
1049/// };
1050/// @endcode
1051/// For these types, we won't actually know what the array bound is
1052/// until template instantiation occurs, at which point this will
1053/// become either a ConstantArrayType or a VariableArrayType.
1054class DependentSizedArrayType : public ArrayType {
1055  /// SizeExpr - An assignment expression that will instantiate to the
1056  /// size of the array.
1057  Stmt *SizeExpr;
1058  /// Brackets - The left and right array brackets.
1059  SourceRange Brackets;
1060
1061  DependentSizedArrayType(QualType et, QualType can, Expr *e,
1062			  ArraySizeModifier sm, unsigned tq,
1063                          SourceRange brackets)
1064    : ArrayType(DependentSizedArray, et, can, sm, tq),
1065      SizeExpr((Stmt*) e), Brackets(brackets) {}
1066  friend class ASTContext;  // ASTContext creates these.
1067  virtual void Destroy(ASTContext& C);
1068
1069public:
1070  Expr *getSizeExpr() const {
1071    // We use C-style casts instead of cast<> here because we do not wish
1072    // to have a dependency of Type.h on Stmt.h/Expr.h.
1073    return (Expr*) SizeExpr;
1074  }
1075  SourceRange getBracketsRange() const { return Brackets; }
1076  SourceLocation getLBracketLoc() const { return Brackets.getBegin(); }
1077  SourceLocation getRBracketLoc() const { return Brackets.getEnd(); }
1078
1079  virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const;
1080
1081  static bool classof(const Type *T) {
1082    return T->getTypeClass() == DependentSizedArray;
1083  }
1084  static bool classof(const DependentSizedArrayType *) { return true; }
1085
1086  friend class StmtIteratorBase;
1087
1088  void Profile(llvm::FoldingSetNodeID &ID) {
1089    assert(0 && "Cannnot unique DependentSizedArrayTypes.");
1090  }
1091};
1092
1093/// DependentSizedExtVectorType - This type represent an extended vector type
1094/// where either the type or size is dependent. For example:
1095/// @code
1096/// template<typename T, int Size>
1097/// class vector {
1098///   typedef T __attribute__((ext_vector_type(Size))) type;
1099/// }
1100/// @endcode
1101class DependentSizedExtVectorType : public Type {
1102  Expr *SizeExpr;
1103  /// ElementType - The element type of the array.
1104  QualType ElementType;
1105  SourceLocation loc;
1106
1107  DependentSizedExtVectorType(QualType ElementType, QualType can,
1108                              Expr *SizeExpr, SourceLocation loc)
1109    : Type (DependentSizedExtVector, can, true),
1110    SizeExpr(SizeExpr), ElementType(ElementType), loc(loc) {}
1111  friend class ASTContext;
1112  virtual void Destroy(ASTContext& C);
1113
1114public:
1115  const Expr *getSizeExpr() const { return SizeExpr; }
1116  QualType getElementType() const { return ElementType; }
1117  SourceLocation getAttributeLoc() const { return loc; }
1118
1119  virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const;
1120
1121  static bool classof(const Type *T) {
1122    return T->getTypeClass() == DependentSizedExtVector;
1123  }
1124  static bool classof(const DependentSizedExtVectorType *) { return true; }
1125};
1126
1127
1128/// VectorType - GCC generic vector type. This type is created using
1129/// __attribute__((vector_size(n)), where "n" specifies the vector size in
1130/// bytes. Since the constructor takes the number of vector elements, the
1131/// client is responsible for converting the size into the number of elements.
1132class VectorType : public Type, public llvm::FoldingSetNode {
1133protected:
1134  /// ElementType - The element type of the vector.
1135  QualType ElementType;
1136
1137  /// NumElements - The number of elements in the vector.
1138  unsigned NumElements;
1139
1140  VectorType(QualType vecType, unsigned nElements, QualType canonType) :
1141    Type(Vector, canonType, vecType->isDependentType()),
1142    ElementType(vecType), NumElements(nElements) {}
1143  VectorType(TypeClass tc, QualType vecType, unsigned nElements,
1144             QualType canonType)
1145    : Type(tc, canonType, vecType->isDependentType()), ElementType(vecType),
1146      NumElements(nElements) {}
1147  friend class ASTContext;  // ASTContext creates these.
1148public:
1149
1150  QualType getElementType() const { return ElementType; }
1151  unsigned getNumElements() const { return NumElements; }
1152
1153  virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const;
1154
1155  void Profile(llvm::FoldingSetNodeID &ID) {
1156    Profile(ID, getElementType(), getNumElements(), getTypeClass());
1157  }
1158  static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType,
1159                      unsigned NumElements, TypeClass TypeClass) {
1160    ID.AddPointer(ElementType.getAsOpaquePtr());
1161    ID.AddInteger(NumElements);
1162    ID.AddInteger(TypeClass);
1163  }
1164  static bool classof(const Type *T) {
1165    return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector;
1166  }
1167  static bool classof(const VectorType *) { return true; }
1168};
1169
1170/// ExtVectorType - Extended vector type. This type is created using
1171/// __attribute__((ext_vector_type(n)), where "n" is the number of elements.
1172/// Unlike vector_size, ext_vector_type is only allowed on typedef's. This
1173/// class enables syntactic extensions, like Vector Components for accessing
1174/// points, colors, and textures (modeled after OpenGL Shading Language).
1175class ExtVectorType : public VectorType {
1176  ExtVectorType(QualType vecType, unsigned nElements, QualType canonType) :
1177    VectorType(ExtVector, vecType, nElements, canonType) {}
1178  friend class ASTContext;  // ASTContext creates these.
1179public:
1180  static int getPointAccessorIdx(char c) {
1181    switch (c) {
1182    default: return -1;
1183    case 'x': return 0;
1184    case 'y': return 1;
1185    case 'z': return 2;
1186    case 'w': return 3;
1187    }
1188  }
1189  static int getNumericAccessorIdx(char c) {
1190    switch (c) {
1191      default: return -1;
1192      case '0': return 0;
1193      case '1': return 1;
1194      case '2': return 2;
1195      case '3': return 3;
1196      case '4': return 4;
1197      case '5': return 5;
1198      case '6': return 6;
1199      case '7': return 7;
1200      case '8': return 8;
1201      case '9': return 9;
1202      case 'A':
1203      case 'a': return 10;
1204      case 'B':
1205      case 'b': return 11;
1206      case 'C':
1207      case 'c': return 12;
1208      case 'D':
1209      case 'd': return 13;
1210      case 'E':
1211      case 'e': return 14;
1212      case 'F':
1213      case 'f': return 15;
1214    }
1215  }
1216
1217  static int getAccessorIdx(char c) {
1218    if (int idx = getPointAccessorIdx(c)+1) return idx-1;
1219    return getNumericAccessorIdx(c);
1220  }
1221
1222  bool isAccessorWithinNumElements(char c) const {
1223    if (int idx = getAccessorIdx(c)+1)
1224      return unsigned(idx-1) < NumElements;
1225    return false;
1226  }
1227  virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const;
1228
1229  static bool classof(const Type *T) {
1230    return T->getTypeClass() == ExtVector;
1231  }
1232  static bool classof(const ExtVectorType *) { return true; }
1233};
1234
1235/// FunctionType - C99 6.7.5.3 - Function Declarators.  This is the common base
1236/// class of FunctionNoProtoType and FunctionProtoType.
1237///
1238class FunctionType : public Type {
1239  /// SubClassData - This field is owned by the subclass, put here to pack
1240  /// tightly with the ivars in Type.
1241  bool SubClassData : 1;
1242
1243  /// TypeQuals - Used only by FunctionProtoType, put here to pack with the
1244  /// other bitfields.
1245  /// The qualifiers are part of FunctionProtoType because...
1246  ///
1247  /// C++ 8.3.5p4: The return type, the parameter type list and the
1248  /// cv-qualifier-seq, [...], are part of the function type.
1249  ///
1250  unsigned TypeQuals : 3;
1251
1252  // The type returned by the function.
1253  QualType ResultType;
1254protected:
1255  FunctionType(TypeClass tc, QualType res, bool SubclassInfo,
1256               unsigned typeQuals, QualType Canonical, bool Dependent)
1257    : Type(tc, Canonical, Dependent),
1258      SubClassData(SubclassInfo), TypeQuals(typeQuals), ResultType(res) {}
1259  bool getSubClassData() const { return SubClassData; }
1260  unsigned getTypeQuals() const { return TypeQuals; }
1261public:
1262
1263  QualType getResultType() const { return ResultType; }
1264
1265
1266  static bool classof(const Type *T) {
1267    return T->getTypeClass() == FunctionNoProto ||
1268           T->getTypeClass() == FunctionProto;
1269  }
1270  static bool classof(const FunctionType *) { return true; }
1271};
1272
1273/// FunctionNoProtoType - Represents a K&R-style 'int foo()' function, which has
1274/// no information available about its arguments.
1275class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode {
1276  FunctionNoProtoType(QualType Result, QualType Canonical)
1277    : FunctionType(FunctionNoProto, Result, false, 0, Canonical,
1278                   /*Dependent=*/false) {}
1279  friend class ASTContext;  // ASTContext creates these.
1280public:
1281  // No additional state past what FunctionType provides.
1282
1283  virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const;
1284
1285  void Profile(llvm::FoldingSetNodeID &ID) {
1286    Profile(ID, getResultType());
1287  }
1288  static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType) {
1289    ID.AddPointer(ResultType.getAsOpaquePtr());
1290  }
1291
1292  static bool classof(const Type *T) {
1293    return T->getTypeClass() == FunctionNoProto;
1294  }
1295  static bool classof(const FunctionNoProtoType *) { return true; }
1296};
1297
1298/// FunctionProtoType - Represents a prototype with argument type info, e.g.
1299/// 'int foo(int)' or 'int foo(void)'.  'void' is represented as having no
1300/// arguments, not as having a single void argument. Such a type can have an
1301/// exception specification, but this specification is not part of the canonical
1302/// type.
1303class FunctionProtoType : public FunctionType, public llvm::FoldingSetNode {
1304  /// hasAnyDependentType - Determine whether there are any dependent
1305  /// types within the arguments passed in.
1306  static bool hasAnyDependentType(const QualType *ArgArray, unsigned numArgs) {
1307    for (unsigned Idx = 0; Idx < numArgs; ++Idx)
1308      if (ArgArray[Idx]->isDependentType())
1309    return true;
1310
1311    return false;
1312  }
1313
1314  FunctionProtoType(QualType Result, const QualType *ArgArray, unsigned numArgs,
1315                    bool isVariadic, unsigned typeQuals, bool hasExs,
1316                    bool hasAnyExs, const QualType *ExArray,
1317                    unsigned numExs, QualType Canonical)
1318    : FunctionType(FunctionProto, Result, isVariadic, typeQuals, Canonical,
1319                   (Result->isDependentType() ||
1320                    hasAnyDependentType(ArgArray, numArgs))),
1321      NumArgs(numArgs), NumExceptions(numExs), HasExceptionSpec(hasExs),
1322      AnyExceptionSpec(hasAnyExs) {
1323    // Fill in the trailing argument array.
1324    QualType *ArgInfo = reinterpret_cast<QualType*>(this+1);
1325    for (unsigned i = 0; i != numArgs; ++i)
1326      ArgInfo[i] = ArgArray[i];
1327    // Fill in the exception array.
1328    QualType *Ex = ArgInfo + numArgs;
1329    for (unsigned i = 0; i != numExs; ++i)
1330      Ex[i] = ExArray[i];
1331  }
1332
1333  /// NumArgs - The number of arguments this function has, not counting '...'.
1334  unsigned NumArgs : 20;
1335
1336  /// NumExceptions - The number of types in the exception spec, if any.
1337  unsigned NumExceptions : 10;
1338
1339  /// HasExceptionSpec - Whether this function has an exception spec at all.
1340  bool HasExceptionSpec : 1;
1341
1342  /// AnyExceptionSpec - Whether this function has a throw(...) spec.
1343  bool AnyExceptionSpec : 1;
1344
1345  /// ArgInfo - There is an variable size array after the class in memory that
1346  /// holds the argument types.
1347
1348  /// Exceptions - There is another variable size array after ArgInfo that
1349  /// holds the exception types.
1350
1351  friend class ASTContext;  // ASTContext creates these.
1352
1353public:
1354  unsigned getNumArgs() const { return NumArgs; }
1355  QualType getArgType(unsigned i) const {
1356    assert(i < NumArgs && "Invalid argument number!");
1357    return arg_type_begin()[i];
1358  }
1359
1360  bool hasExceptionSpec() const { return HasExceptionSpec; }
1361  bool hasAnyExceptionSpec() const { return AnyExceptionSpec; }
1362  unsigned getNumExceptions() const { return NumExceptions; }
1363  QualType getExceptionType(unsigned i) const {
1364    assert(i < NumExceptions && "Invalid exception number!");
1365    return exception_begin()[i];
1366  }
1367  bool hasEmptyExceptionSpec() const {
1368    return hasExceptionSpec() && !hasAnyExceptionSpec() &&
1369      getNumExceptions() == 0;
1370  }
1371
1372  bool isVariadic() const { return getSubClassData(); }
1373  unsigned getTypeQuals() const { return FunctionType::getTypeQuals(); }
1374
1375  typedef const QualType *arg_type_iterator;
1376  arg_type_iterator arg_type_begin() const {
1377    return reinterpret_cast<const QualType *>(this+1);
1378  }
1379  arg_type_iterator arg_type_end() const { return arg_type_begin()+NumArgs; }
1380
1381  typedef const QualType *exception_iterator;
1382  exception_iterator exception_begin() const {
1383    // exceptions begin where arguments end
1384    return arg_type_end();
1385  }
1386  exception_iterator exception_end() const {
1387    return exception_begin() + NumExceptions;
1388  }
1389
1390  virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const;
1391
1392  static bool classof(const Type *T) {
1393    return T->getTypeClass() == FunctionProto;
1394  }
1395  static bool classof(const FunctionProtoType *) { return true; }
1396
1397  void Profile(llvm::FoldingSetNodeID &ID);
1398  static void Profile(llvm::FoldingSetNodeID &ID, QualType Result,
1399                      arg_type_iterator ArgTys, unsigned NumArgs,
1400                      bool isVariadic, unsigned TypeQuals,
1401                      bool hasExceptionSpec, bool anyExceptionSpec,
1402                      unsigned NumExceptions, exception_iterator Exs);
1403};
1404
1405
1406class TypedefType : public Type {
1407  TypedefDecl *Decl;
1408protected:
1409  TypedefType(TypeClass tc, TypedefDecl *D, QualType can)
1410    : Type(tc, can, can->isDependentType()), Decl(D) {
1411    assert(!isa<TypedefType>(can) && "Invalid canonical type");
1412  }
1413  friend class ASTContext;  // ASTContext creates these.
1414public:
1415
1416  TypedefDecl *getDecl() const { return Decl; }
1417
1418  /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
1419  /// potentially looking through *all* consecutive typedefs.  This returns the
1420  /// sum of the type qualifiers, so if you have:
1421  ///   typedef const int A;
1422  ///   typedef volatile A B;
1423  /// looking through the typedefs for B will give you "const volatile A".
1424  QualType LookThroughTypedefs() const;
1425
1426  virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const;
1427
1428  static bool classof(const Type *T) { return T->getTypeClass() == Typedef; }
1429  static bool classof(const TypedefType *) { return true; }
1430};
1431
1432/// TypeOfExprType (GCC extension).
1433class TypeOfExprType : public Type {
1434  Expr *TOExpr;
1435  TypeOfExprType(Expr *E, QualType can);
1436  friend class ASTContext;  // ASTContext creates these.
1437public:
1438  Expr *getUnderlyingExpr() const { return TOExpr; }
1439
1440  virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const;
1441
1442  static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; }
1443  static bool classof(const TypeOfExprType *) { return true; }
1444};
1445
1446/// TypeOfType (GCC extension).
1447class TypeOfType : public Type {
1448  QualType TOType;
1449  TypeOfType(QualType T, QualType can)
1450    : Type(TypeOf, can, T->isDependentType()), TOType(T) {
1451    assert(!isa<TypedefType>(can) && "Invalid canonical type");
1452  }
1453  friend class ASTContext;  // ASTContext creates these.
1454public:
1455  QualType getUnderlyingType() const { return TOType; }
1456
1457  virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const;
1458
1459  static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; }
1460  static bool classof(const TypeOfType *) { return true; }
1461};
1462
1463/// DecltypeType (C++0x)
1464class DecltypeType : public Type {
1465  Expr *E;
1466  DecltypeType(Expr *E, QualType can);
1467  friend class ASTContext;  // ASTContext creates these.
1468public:
1469  Expr *getUnderlyingExpr() const { return E; }
1470
1471  virtual void getAsStringInternal(std::string &InnerString,
1472                                   const PrintingPolicy &Policy) const;
1473
1474  static bool classof(const Type *T) { return T->getTypeClass() == Decltype; }
1475  static bool classof(const DecltypeType *) { return true; }
1476};
1477
1478class TagType : public Type {
1479  /// Stores the TagDecl associated with this type. The decl will
1480  /// point to the TagDecl that actually defines the entity (or is a
1481  /// definition in progress), if there is such a definition. The
1482  /// single-bit value will be non-zero when this tag is in the
1483  /// process of being defined.
1484  mutable llvm::PointerIntPair<TagDecl *, 1> decl;
1485  friend class ASTContext;
1486  friend class TagDecl;
1487
1488protected:
1489  TagType(TypeClass TC, TagDecl *D, QualType can);
1490
1491public:
1492  TagDecl *getDecl() const { return decl.getPointer(); }
1493
1494  /// @brief Determines whether this type is in the process of being
1495  /// defined.
1496  bool isBeingDefined() const { return decl.getInt(); }
1497  void setBeingDefined(bool Def) { decl.setInt(Def? 1 : 0); }
1498
1499  virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const;
1500
1501  static bool classof(const Type *T) {
1502    return T->getTypeClass() >= TagFirst && T->getTypeClass() <= TagLast;
1503  }
1504  static bool classof(const TagType *) { return true; }
1505  static bool classof(const RecordType *) { return true; }
1506  static bool classof(const EnumType *) { return true; }
1507};
1508
1509/// RecordType - This is a helper class that allows the use of isa/cast/dyncast
1510/// to detect TagType objects of structs/unions/classes.
1511class RecordType : public TagType {
1512protected:
1513  explicit RecordType(RecordDecl *D)
1514    : TagType(Record, reinterpret_cast<TagDecl*>(D), QualType()) { }
1515  explicit RecordType(TypeClass TC, RecordDecl *D)
1516    : TagType(TC, reinterpret_cast<TagDecl*>(D), QualType()) { }
1517  friend class ASTContext;   // ASTContext creates these.
1518public:
1519
1520  RecordDecl *getDecl() const {
1521    return reinterpret_cast<RecordDecl*>(TagType::getDecl());
1522  }
1523
1524  // FIXME: This predicate is a helper to QualType/Type. It needs to
1525  // recursively check all fields for const-ness. If any field is declared
1526  // const, it needs to return false.
1527  bool hasConstFields() const { return false; }
1528
1529  // FIXME: RecordType needs to check when it is created that all fields are in
1530  // the same address space, and return that.
1531  unsigned getAddressSpace() const { return 0; }
1532
1533  static bool classof(const TagType *T);
1534  static bool classof(const Type *T) {
1535    return isa<TagType>(T) && classof(cast<TagType>(T));
1536  }
1537  static bool classof(const RecordType *) { return true; }
1538};
1539
1540/// EnumType - This is a helper class that allows the use of isa/cast/dyncast
1541/// to detect TagType objects of enums.
1542class EnumType : public TagType {
1543  explicit EnumType(EnumDecl *D)
1544    : TagType(Enum, reinterpret_cast<TagDecl*>(D), QualType()) { }
1545  friend class ASTContext;   // ASTContext creates these.
1546public:
1547
1548  EnumDecl *getDecl() const {
1549    return reinterpret_cast<EnumDecl*>(TagType::getDecl());
1550  }
1551
1552  static bool classof(const TagType *T);
1553  static bool classof(const Type *T) {
1554    return isa<TagType>(T) && classof(cast<TagType>(T));
1555  }
1556  static bool classof(const EnumType *) { return true; }
1557};
1558
1559class TemplateTypeParmType : public Type, public llvm::FoldingSetNode {
1560  unsigned Depth : 15;
1561  unsigned Index : 16;
1562  unsigned ParameterPack : 1;
1563  IdentifierInfo *Name;
1564
1565  TemplateTypeParmType(unsigned D, unsigned I, bool PP, IdentifierInfo *N,
1566                       QualType Canon)
1567    : Type(TemplateTypeParm, Canon, /*Dependent=*/true),
1568      Depth(D), Index(I), ParameterPack(PP), Name(N) { }
1569
1570  TemplateTypeParmType(unsigned D, unsigned I, bool PP)
1571    : Type(TemplateTypeParm, QualType(this, 0), /*Dependent=*/true),
1572      Depth(D), Index(I), ParameterPack(PP), Name(0) { }
1573
1574  friend class ASTContext;  // ASTContext creates these
1575
1576public:
1577  unsigned getDepth() const { return Depth; }
1578  unsigned getIndex() const { return Index; }
1579  bool isParameterPack() const { return ParameterPack; }
1580  IdentifierInfo *getName() const { return Name; }
1581
1582  virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const;
1583
1584  void Profile(llvm::FoldingSetNodeID &ID) {
1585    Profile(ID, Depth, Index, ParameterPack, Name);
1586  }
1587
1588  static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth,
1589                      unsigned Index, bool ParameterPack,
1590                      IdentifierInfo *Name) {
1591    ID.AddInteger(Depth);
1592    ID.AddInteger(Index);
1593    ID.AddBoolean(ParameterPack);
1594    ID.AddPointer(Name);
1595  }
1596
1597  static bool classof(const Type *T) {
1598    return T->getTypeClass() == TemplateTypeParm;
1599  }
1600  static bool classof(const TemplateTypeParmType *T) { return true; }
1601};
1602
1603/// \brief Represents the type of a template specialization as written
1604/// in the source code.
1605///
1606/// Template specialization types represent the syntactic form of a
1607/// template-id that refers to a type, e.g., @c vector<int>. Some
1608/// template specialization types are syntactic sugar, whose canonical
1609/// type will point to some other type node that represents the
1610/// instantiation or class template specialization. For example, a
1611/// class template specialization type of @c vector<int> will refer to
1612/// a tag type for the instantiation
1613/// @c std::vector<int, std::allocator<int>>.
1614///
1615/// Other template specialization types, for which the template name
1616/// is dependent, may be canonical types. These types are always
1617/// dependent.
1618class TemplateSpecializationType
1619  : public Type, public llvm::FoldingSetNode {
1620
1621  /// \brief The name of the template being specialized.
1622  TemplateName Template;
1623
1624  /// \brief - The number of template arguments named in this class
1625  /// template specialization.
1626  unsigned NumArgs;
1627
1628  TemplateSpecializationType(TemplateName T,
1629                             const TemplateArgument *Args,
1630                             unsigned NumArgs, QualType Canon);
1631
1632  virtual void Destroy(ASTContext& C);
1633
1634  friend class ASTContext;  // ASTContext creates these
1635
1636public:
1637  /// \brief Determine whether any of the given template arguments are
1638  /// dependent.
1639  static bool anyDependentTemplateArguments(const TemplateArgument *Args,
1640                                            unsigned NumArgs);
1641
1642  /// \brief Print a template argument list, including the '<' and '>'
1643  /// enclosing the template arguments.
1644  static std::string PrintTemplateArgumentList(const TemplateArgument *Args,
1645                                               unsigned NumArgs,
1646                                               const PrintingPolicy &Policy);
1647
1648  typedef const TemplateArgument * iterator;
1649
1650  iterator begin() const { return getArgs(); }
1651  iterator end() const;
1652
1653  /// \brief Retrieve the name of the template that we are specializing.
1654  TemplateName getTemplateName() const { return Template; }
1655
1656  /// \brief Retrieve the template arguments.
1657  const TemplateArgument *getArgs() const {
1658    return reinterpret_cast<const TemplateArgument *>(this + 1);
1659  }
1660
1661  /// \brief Retrieve the number of template arguments.
1662  unsigned getNumArgs() const { return NumArgs; }
1663
1664  /// \brief Retrieve a specific template argument as a type.
1665  /// \precondition @c isArgType(Arg)
1666  const TemplateArgument &getArg(unsigned Idx) const;
1667
1668  virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const;
1669
1670  void Profile(llvm::FoldingSetNodeID &ID) {
1671    Profile(ID, Template, getArgs(), NumArgs);
1672  }
1673
1674  static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T,
1675                      const TemplateArgument *Args, unsigned NumArgs);
1676
1677  static bool classof(const Type *T) {
1678    return T->getTypeClass() == TemplateSpecialization;
1679  }
1680  static bool classof(const TemplateSpecializationType *T) { return true; }
1681};
1682
1683/// \brief Represents a type that was referred to via a qualified
1684/// name, e.g., N::M::type.
1685///
1686/// This type is used to keep track of a type name as written in the
1687/// source code, including any nested-name-specifiers. The type itself
1688/// is always "sugar", used to express what was written in the source
1689/// code but containing no additional semantic information.
1690class QualifiedNameType : public Type, public llvm::FoldingSetNode {
1691  /// \brief The nested name specifier containing the qualifier.
1692  NestedNameSpecifier *NNS;
1693
1694  /// \brief The type that this qualified name refers to.
1695  QualType NamedType;
1696
1697  QualifiedNameType(NestedNameSpecifier *NNS, QualType NamedType,
1698                    QualType CanonType)
1699    : Type(QualifiedName, CanonType, NamedType->isDependentType()),
1700      NNS(NNS), NamedType(NamedType) { }
1701
1702  friend class ASTContext;  // ASTContext creates these
1703
1704public:
1705  /// \brief Retrieve the qualification on this type.
1706  NestedNameSpecifier *getQualifier() const { return NNS; }
1707
1708  /// \brief Retrieve the type named by the qualified-id.
1709  QualType getNamedType() const { return NamedType; }
1710
1711  virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const;
1712
1713  void Profile(llvm::FoldingSetNodeID &ID) {
1714    Profile(ID, NNS, NamedType);
1715  }
1716
1717  static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
1718                      QualType NamedType) {
1719    ID.AddPointer(NNS);
1720    NamedType.Profile(ID);
1721  }
1722
1723  static bool classof(const Type *T) {
1724    return T->getTypeClass() == QualifiedName;
1725  }
1726  static bool classof(const QualifiedNameType *T) { return true; }
1727};
1728
1729/// \brief Represents a 'typename' specifier that names a type within
1730/// a dependent type, e.g., "typename T::type".
1731///
1732/// TypenameType has a very similar structure to QualifiedNameType,
1733/// which also involves a nested-name-specifier following by a type,
1734/// and (FIXME!) both can even be prefixed by the 'typename'
1735/// keyword. However, the two types serve very different roles:
1736/// QualifiedNameType is a non-semantic type that serves only as sugar
1737/// to show how a particular type was written in the source
1738/// code. TypenameType, on the other hand, only occurs when the
1739/// nested-name-specifier is dependent, such that we cannot resolve
1740/// the actual type until after instantiation.
1741class TypenameType : public Type, public llvm::FoldingSetNode {
1742  /// \brief The nested name specifier containing the qualifier.
1743  NestedNameSpecifier *NNS;
1744
1745  typedef llvm::PointerUnion<const IdentifierInfo *,
1746                             const TemplateSpecializationType *> NameType;
1747
1748  /// \brief The type that this typename specifier refers to.
1749  NameType Name;
1750
1751  TypenameType(NestedNameSpecifier *NNS, const IdentifierInfo *Name,
1752               QualType CanonType)
1753    : Type(Typename, CanonType, true), NNS(NNS), Name(Name) {
1754    assert(NNS->isDependent() &&
1755           "TypenameType requires a dependent nested-name-specifier");
1756  }
1757
1758  TypenameType(NestedNameSpecifier *NNS, const TemplateSpecializationType *Ty,
1759               QualType CanonType)
1760    : Type(Typename, CanonType, true), NNS(NNS), Name(Ty) {
1761    assert(NNS->isDependent() &&
1762           "TypenameType requires a dependent nested-name-specifier");
1763  }
1764
1765  friend class ASTContext;  // ASTContext creates these
1766
1767public:
1768  /// \brief Retrieve the qualification on this type.
1769  NestedNameSpecifier *getQualifier() const { return NNS; }
1770
1771  /// \brief Retrieve the type named by the typename specifier as an
1772  /// identifier.
1773  ///
1774  /// This routine will return a non-NULL identifier pointer when the
1775  /// form of the original typename was terminated by an identifier,
1776  /// e.g., "typename T::type".
1777  const IdentifierInfo *getIdentifier() const {
1778    return Name.dyn_cast<const IdentifierInfo *>();
1779  }
1780
1781  /// \brief Retrieve the type named by the typename specifier as a
1782  /// type specialization.
1783  const TemplateSpecializationType *getTemplateId() const {
1784    return Name.dyn_cast<const TemplateSpecializationType *>();
1785  }
1786
1787  virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const;
1788
1789  void Profile(llvm::FoldingSetNodeID &ID) {
1790    Profile(ID, NNS, Name);
1791  }
1792
1793  static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
1794                      NameType Name) {
1795    ID.AddPointer(NNS);
1796    ID.AddPointer(Name.getOpaqueValue());
1797  }
1798
1799  static bool classof(const Type *T) {
1800    return T->getTypeClass() == Typename;
1801  }
1802  static bool classof(const TypenameType *T) { return true; }
1803};
1804
1805/// ObjCObjectPointerType - Used to represent 'id', 'Interface *', 'id <p>',
1806/// and 'Interface <p> *'.
1807///
1808/// Duplicate protocols are removed and protocol list is canonicalized to be in
1809/// alphabetical order.
1810class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode {
1811  ObjCInterfaceDecl *Decl;
1812  // List of protocols for this protocol conforming object type
1813  // List is sorted on protocol name. No protocol is entered more than once.
1814  llvm::SmallVector<ObjCProtocolDecl*, 8> Protocols;
1815
1816  ObjCObjectPointerType(ObjCInterfaceDecl *D,
1817                        ObjCProtocolDecl **Protos, unsigned NumP) :
1818    Type(ObjCObjectPointer, QualType(), /*Dependent=*/false),
1819    Decl(D), Protocols(Protos, Protos+NumP) { }
1820  friend class ASTContext;  // ASTContext creates these.
1821
1822public:
1823  ObjCInterfaceDecl *getDecl() const { return Decl; }
1824
1825  /// isObjCQualifiedIdType - true for "id <p>".
1826  bool isObjCQualifiedIdType() const { return Decl == 0 && Protocols.size(); }
1827
1828  /// qual_iterator and friends: this provides access to the (potentially empty)
1829  /// list of protocols qualifying this interface.
1830  typedef llvm::SmallVector<ObjCProtocolDecl*, 8>::const_iterator qual_iterator;
1831
1832  qual_iterator qual_begin() const { return Protocols.begin(); }
1833  qual_iterator qual_end() const   { return Protocols.end(); }
1834  bool qual_empty() const { return Protocols.size() == 0; }
1835
1836  /// getNumProtocols - Return the number of qualifying protocols in this
1837  /// interface type, or 0 if there are none.
1838  unsigned getNumProtocols() const { return Protocols.size(); }
1839
1840  void Profile(llvm::FoldingSetNodeID &ID);
1841  static void Profile(llvm::FoldingSetNodeID &ID,
1842                      const ObjCInterfaceDecl *Decl,
1843                      ObjCProtocolDecl **protocols, unsigned NumProtocols);
1844  virtual void getAsStringInternal(std::string &InnerString,
1845                                   const PrintingPolicy &Policy) const;
1846  static bool classof(const Type *T) {
1847    return T->getTypeClass() == ObjCObjectPointer;
1848  }
1849  static bool classof(const ObjCObjectPointerType *) { return true; }
1850};
1851
1852/// ObjCInterfaceType - Interfaces are the core concept in Objective-C for
1853/// object oriented design.  They basically correspond to C++ classes.  There
1854/// are two kinds of interface types, normal interfaces like "NSString" and
1855/// qualified interfaces, which are qualified with a protocol list like
1856/// "NSString<NSCopyable, NSAmazing>".  Qualified interface types are instances
1857/// of ObjCQualifiedInterfaceType, which is a subclass of ObjCInterfaceType.
1858class ObjCInterfaceType : public Type {
1859  ObjCInterfaceDecl *Decl;
1860protected:
1861  ObjCInterfaceType(TypeClass tc, ObjCInterfaceDecl *D) :
1862    Type(tc, QualType(), /*Dependent=*/false), Decl(D) { }
1863  friend class ASTContext;  // ASTContext creates these.
1864public:
1865
1866  ObjCInterfaceDecl *getDecl() const { return Decl; }
1867
1868  /// qual_iterator and friends: this provides access to the (potentially empty)
1869  /// list of protocols qualifying this interface.  If this is an instance of
1870  /// ObjCQualifiedInterfaceType it returns the list, otherwise it returns an
1871  /// empty list if there are no qualifying protocols.
1872  typedef llvm::SmallVector<ObjCProtocolDecl*, 8>::const_iterator qual_iterator;
1873  inline qual_iterator qual_begin() const;
1874  inline qual_iterator qual_end() const;
1875  bool qual_empty() const { return getTypeClass() != ObjCQualifiedInterface; }
1876
1877  /// getNumProtocols - Return the number of qualifying protocols in this
1878  /// interface type, or 0 if there are none.
1879  inline unsigned getNumProtocols() const;
1880
1881  virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const;
1882  static bool classof(const Type *T) {
1883    return T->getTypeClass() == ObjCInterface ||
1884           T->getTypeClass() == ObjCQualifiedInterface;
1885  }
1886  static bool classof(const ObjCInterfaceType *) { return true; }
1887};
1888
1889/// ObjCQualifiedInterfaceType - This class represents interface types
1890/// conforming to a list of protocols, such as INTF<Proto1, Proto2, Proto1>.
1891///
1892/// Duplicate protocols are removed and protocol list is canonicalized to be in
1893/// alphabetical order.
1894class ObjCQualifiedInterfaceType : public ObjCInterfaceType,
1895                                   public llvm::FoldingSetNode {
1896
1897  // List of protocols for this protocol conforming object type
1898  // List is sorted on protocol name. No protocol is enterred more than once.
1899  llvm::SmallVector<ObjCProtocolDecl*, 4> Protocols;
1900
1901  ObjCQualifiedInterfaceType(ObjCInterfaceDecl *D,
1902                             ObjCProtocolDecl **Protos, unsigned NumP) :
1903    ObjCInterfaceType(ObjCQualifiedInterface, D),
1904    Protocols(Protos, Protos+NumP) { }
1905  friend class ASTContext;  // ASTContext creates these.
1906public:
1907
1908  unsigned getNumProtocols() const {
1909    return Protocols.size();
1910  }
1911
1912  qual_iterator qual_begin() const { return Protocols.begin(); }
1913  qual_iterator qual_end() const   { return Protocols.end(); }
1914
1915  virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const;
1916
1917  void Profile(llvm::FoldingSetNodeID &ID);
1918  static void Profile(llvm::FoldingSetNodeID &ID,
1919                      const ObjCInterfaceDecl *Decl,
1920                      ObjCProtocolDecl **protocols, unsigned NumProtocols);
1921
1922  static bool classof(const Type *T) {
1923    return T->getTypeClass() == ObjCQualifiedInterface;
1924  }
1925  static bool classof(const ObjCQualifiedInterfaceType *) { return true; }
1926};
1927
1928inline ObjCInterfaceType::qual_iterator ObjCInterfaceType::qual_begin() const {
1929  if (const ObjCQualifiedInterfaceType *QIT =
1930         dyn_cast<ObjCQualifiedInterfaceType>(this))
1931    return QIT->qual_begin();
1932  return 0;
1933}
1934inline ObjCInterfaceType::qual_iterator ObjCInterfaceType::qual_end() const {
1935  if (const ObjCQualifiedInterfaceType *QIT =
1936         dyn_cast<ObjCQualifiedInterfaceType>(this))
1937    return QIT->qual_end();
1938  return 0;
1939}
1940
1941/// getNumProtocols - Return the number of qualifying protocols in this
1942/// interface type, or 0 if there are none.
1943inline unsigned ObjCInterfaceType::getNumProtocols() const {
1944  if (const ObjCQualifiedInterfaceType *QIT =
1945        dyn_cast<ObjCQualifiedInterfaceType>(this))
1946    return QIT->getNumProtocols();
1947  return 0;
1948}
1949
1950// Inline function definitions.
1951
1952/// getUnqualifiedType - Return the type without any qualifiers.
1953inline QualType QualType::getUnqualifiedType() const {
1954  Type *TP = getTypePtr();
1955  if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(TP))
1956    TP = EXTQT->getBaseType();
1957  return QualType(TP, 0);
1958}
1959
1960/// getAddressSpace - Return the address space of this type.
1961inline unsigned QualType::getAddressSpace() const {
1962  QualType CT = getTypePtr()->getCanonicalTypeInternal();
1963  if (const ArrayType *AT = dyn_cast<ArrayType>(CT))
1964    return AT->getElementType().getAddressSpace();
1965  if (const RecordType *RT = dyn_cast<RecordType>(CT))
1966    return RT->getAddressSpace();
1967  if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CT))
1968    return EXTQT->getAddressSpace();
1969  return 0;
1970}
1971
1972/// getObjCGCAttr - Return the gc attribute of this type.
1973inline QualType::GCAttrTypes QualType::getObjCGCAttr() const {
1974  QualType CT = getTypePtr()->getCanonicalTypeInternal();
1975  if (const ArrayType *AT = dyn_cast<ArrayType>(CT))
1976      return AT->getElementType().getObjCGCAttr();
1977  if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CT))
1978    return EXTQT->getObjCGCAttr();
1979  if (const PointerType *PT = CT->getAsPointerType())
1980    return PT->getPointeeType().getObjCGCAttr();
1981  return GCNone;
1982}
1983
1984/// isMoreQualifiedThan - Determine whether this type is more
1985/// qualified than the Other type. For example, "const volatile int"
1986/// is more qualified than "const int", "volatile int", and
1987/// "int". However, it is not more qualified than "const volatile
1988/// int".
1989inline bool QualType::isMoreQualifiedThan(QualType Other) const {
1990  unsigned MyQuals = this->getCVRQualifiers();
1991  unsigned OtherQuals = Other.getCVRQualifiers();
1992  if (getAddressSpace() != Other.getAddressSpace())
1993    return false;
1994  return MyQuals != OtherQuals && (MyQuals | OtherQuals) == MyQuals;
1995}
1996
1997/// isAtLeastAsQualifiedAs - Determine whether this type is at last
1998/// as qualified as the Other type. For example, "const volatile
1999/// int" is at least as qualified as "const int", "volatile int",
2000/// "int", and "const volatile int".
2001inline bool QualType::isAtLeastAsQualifiedAs(QualType Other) const {
2002  unsigned MyQuals = this->getCVRQualifiers();
2003  unsigned OtherQuals = Other.getCVRQualifiers();
2004  if (getAddressSpace() != Other.getAddressSpace())
2005    return false;
2006  return (MyQuals | OtherQuals) == MyQuals;
2007}
2008
2009/// getNonReferenceType - If Type is a reference type (e.g., const
2010/// int&), returns the type that the reference refers to ("const
2011/// int"). Otherwise, returns the type itself. This routine is used
2012/// throughout Sema to implement C++ 5p6:
2013///
2014///   If an expression initially has the type "reference to T" (8.3.2,
2015///   8.5.3), the type is adjusted to "T" prior to any further
2016///   analysis, the expression designates the object or function
2017///   denoted by the reference, and the expression is an lvalue.
2018inline QualType QualType::getNonReferenceType() const {
2019  if (const ReferenceType *RefType = (*this)->getAsReferenceType())
2020    return RefType->getPointeeType();
2021  else
2022    return *this;
2023}
2024
2025inline const TypedefType* Type::getAsTypedefType() const {
2026  return dyn_cast<TypedefType>(this);
2027}
2028inline const ObjCInterfaceType *Type::getAsPointerToObjCInterfaceType() const {
2029  if (const PointerType *PT = getAsPointerType())
2030    return PT->getPointeeType()->getAsObjCInterfaceType();
2031  return 0;
2032}
2033
2034// NOTE: All of these methods use "getUnqualifiedType" to strip off address
2035// space qualifiers if present.
2036inline bool Type::isFunctionType() const {
2037  return isa<FunctionType>(CanonicalType.getUnqualifiedType());
2038}
2039inline bool Type::isPointerType() const {
2040  return isa<PointerType>(CanonicalType.getUnqualifiedType());
2041}
2042inline bool Type::isBlockPointerType() const {
2043  return isa<BlockPointerType>(CanonicalType.getUnqualifiedType());
2044}
2045inline bool Type::isReferenceType() const {
2046  return isa<ReferenceType>(CanonicalType.getUnqualifiedType());
2047}
2048inline bool Type::isLValueReferenceType() const {
2049  return isa<LValueReferenceType>(CanonicalType.getUnqualifiedType());
2050}
2051inline bool Type::isRValueReferenceType() const {
2052  return isa<RValueReferenceType>(CanonicalType.getUnqualifiedType());
2053}
2054inline bool Type::isFunctionPointerType() const {
2055  if (const PointerType* T = getAsPointerType())
2056    return T->getPointeeType()->isFunctionType();
2057  else
2058    return false;
2059}
2060inline bool Type::isMemberPointerType() const {
2061  return isa<MemberPointerType>(CanonicalType.getUnqualifiedType());
2062}
2063inline bool Type::isMemberFunctionPointerType() const {
2064  if (const MemberPointerType* T = getAsMemberPointerType())
2065    return T->getPointeeType()->isFunctionType();
2066  else
2067    return false;
2068}
2069inline bool Type::isArrayType() const {
2070  return isa<ArrayType>(CanonicalType.getUnqualifiedType());
2071}
2072inline bool Type::isConstantArrayType() const {
2073  return isa<ConstantArrayType>(CanonicalType.getUnqualifiedType());
2074}
2075inline bool Type::isIncompleteArrayType() const {
2076  return isa<IncompleteArrayType>(CanonicalType.getUnqualifiedType());
2077}
2078inline bool Type::isVariableArrayType() const {
2079  return isa<VariableArrayType>(CanonicalType.getUnqualifiedType());
2080}
2081inline bool Type::isDependentSizedArrayType() const {
2082  return isa<DependentSizedArrayType>(CanonicalType.getUnqualifiedType());
2083}
2084inline bool Type::isRecordType() const {
2085  return isa<RecordType>(CanonicalType.getUnqualifiedType());
2086}
2087inline bool Type::isAnyComplexType() const {
2088  return isa<ComplexType>(CanonicalType.getUnqualifiedType());
2089}
2090inline bool Type::isVectorType() const {
2091  return isa<VectorType>(CanonicalType.getUnqualifiedType());
2092}
2093inline bool Type::isExtVectorType() const {
2094  return isa<ExtVectorType>(CanonicalType.getUnqualifiedType());
2095}
2096inline bool Type::isObjCObjectPointerType() const {
2097  return isa<ObjCObjectPointerType>(CanonicalType.getUnqualifiedType());
2098}
2099inline bool Type::isObjCInterfaceType() const {
2100  return isa<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
2101}
2102inline bool Type::isObjCQualifiedInterfaceType() const {
2103  return isa<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType());
2104}
2105inline bool Type::isObjCQualifiedIdType() const {
2106  if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) {
2107    return OPT->isObjCQualifiedIdType();
2108  }
2109  return false;
2110}
2111inline bool Type::isTemplateTypeParmType() const {
2112  return isa<TemplateTypeParmType>(CanonicalType.getUnqualifiedType());
2113}
2114
2115inline bool Type::isSpecificBuiltinType(unsigned K) const {
2116  if (const BuiltinType *BT = getAsBuiltinType())
2117    if (BT->getKind() == (BuiltinType::Kind) K)
2118      return true;
2119  return false;
2120}
2121
2122/// \brief Determines whether this is a type for which one can define
2123/// an overloaded operator.
2124inline bool Type::isOverloadableType() const {
2125  return isDependentType() || isRecordType() || isEnumeralType();
2126}
2127
2128inline bool Type::hasPointerRepresentation() const {
2129  return (isPointerType() || isReferenceType() || isBlockPointerType() ||
2130          isObjCInterfaceType() || isObjCQualifiedIdType() ||
2131          isObjCQualifiedInterfaceType() || isNullPtrType());
2132}
2133
2134inline bool Type::hasObjCPointerRepresentation() const {
2135  return (isObjCInterfaceType() || isObjCQualifiedIdType() ||
2136          isObjCQualifiedInterfaceType());
2137}
2138
2139/// Insertion operator for diagnostics.  This allows sending QualType's into a
2140/// diagnostic with <<.
2141inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
2142                                           QualType T) {
2143  DB.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()),
2144                  Diagnostic::ak_qualtype);
2145  return DB;
2146}
2147
2148}  // end namespace clang
2149
2150#endif
2151