Type.h revision 35366a67baa970c287c714c957cf78a4131cf60d
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  struct 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 isAnyPointerType() const;   // Any C pointer or ObjC object pointer
379  bool isBlockPointerType() const;
380  bool isVoidPointerType() const;
381  bool isReferenceType() const;
382  bool isLValueReferenceType() const;
383  bool isRValueReferenceType() const;
384  bool isFunctionPointerType() const;
385  bool isMemberPointerType() const;
386  bool isMemberFunctionPointerType() const;
387  bool isArrayType() const;
388  bool isConstantArrayType() const;
389  bool isIncompleteArrayType() const;
390  bool isVariableArrayType() const;
391  bool isDependentSizedArrayType() const;
392  bool isRecordType() const;
393  bool isClassType() const;
394  bool isStructureType() const;
395  bool isUnionType() const;
396  bool isComplexIntegerType() const;            // GCC _Complex integer type.
397  bool isVectorType() const;                    // GCC vector type.
398  bool isExtVectorType() const;                 // Extended vector type.
399  bool isObjCObjectPointerType() const;         // Pointer to *any* ObjC object.
400  // FIXME: change this to 'raw' interface type, so we can used 'interface' type
401  // for the common case.
402  bool isObjCInterfaceType() const;             // NSString or NSString<foo>
403  bool isObjCQualifiedInterfaceType() const;    // NSString<foo>
404  bool isObjCQualifiedIdType() const;           // id<foo>
405  bool isObjCIdType() const;                    // id
406  bool isObjCClassType() const;                 // Class
407  bool isObjCBuiltinType() const;               // 'id' or 'Class'
408  bool isTemplateTypeParmType() const;          // C++ template type parameter
409  bool isNullPtrType() const;                   // C++0x nullptr_t
410
411  /// isDependentType - Whether this type is a dependent type, meaning
412  /// that its definition somehow depends on a template parameter
413  /// (C++ [temp.dep.type]).
414  bool isDependentType() const { return Dependent; }
415  bool isOverloadableType() const;
416
417  /// hasPointerRepresentation - Whether this type is represented
418  /// natively as a pointer; this includes pointers, references, block
419  /// pointers, and Objective-C interface, qualified id, and qualified
420  /// interface types, as well as nullptr_t.
421  bool hasPointerRepresentation() const;
422
423  /// hasObjCPointerRepresentation - Whether this type can represent
424  /// an objective pointer type for the purpose of GC'ability
425  bool hasObjCPointerRepresentation() const;
426
427  // Type Checking Functions: Check to see if this type is structurally the
428  // specified type, ignoring typedefs and qualifiers, and return a pointer to
429  // the best type we can.
430  const BuiltinType *getAsBuiltinType() const;
431  const FunctionType *getAsFunctionType() const;
432  const FunctionNoProtoType *getAsFunctionNoProtoType() const;
433  const FunctionProtoType *getAsFunctionProtoType() const;
434  const PointerType *getAsPointerType() const;
435  const BlockPointerType *getAsBlockPointerType() const;
436  const ReferenceType *getAsReferenceType() const;
437  const LValueReferenceType *getAsLValueReferenceType() const;
438  const RValueReferenceType *getAsRValueReferenceType() const;
439  const MemberPointerType *getAsMemberPointerType() const;
440  const TagType *getAsTagType() const;
441  const RecordType *getAsRecordType() const;
442  const RecordType *getAsStructureType() const;
443  /// NOTE: getAs*ArrayType are methods on ASTContext.
444  const TypedefType *getAsTypedefType() const;
445  const RecordType *getAsUnionType() const;
446  const EnumType *getAsEnumType() const;
447  const VectorType *getAsVectorType() const; // GCC vector type.
448  const ComplexType *getAsComplexType() const;
449  const ComplexType *getAsComplexIntegerType() const; // GCC complex int type.
450  const ExtVectorType *getAsExtVectorType() const; // Extended vector type.
451  const ObjCObjectPointerType *getAsObjCObjectPointerType() const;
452  // The following is a convenience method that returns an ObjCObjectPointerType
453  // for object declared using an interface.
454  const ObjCObjectPointerType *getAsObjCInterfacePointerType() const;
455  const ObjCObjectPointerType *getAsObjCQualifiedIdType() const;
456  const ObjCInterfaceType *getAsObjCInterfaceType() const;
457  const ObjCQualifiedInterfaceType *getAsObjCQualifiedInterfaceType() const;
458  const TemplateTypeParmType *getAsTemplateTypeParmType() const;
459
460  // Member-template getAs<specific type>'.  This scheme will eventually
461  // replace the specific getAsXXXX methods above.
462  template <typename T> const T *getAs() const;
463
464  const TemplateSpecializationType *
465    getAsTemplateSpecializationType() const;
466
467  /// getAsPointerToObjCInterfaceType - If this is a pointer to an ObjC
468  /// interface, return the interface type, otherwise return null.
469  const ObjCInterfaceType *getAsPointerToObjCInterfaceType() const;
470
471  /// getArrayElementTypeNoTypeQual - If this is an array type, return the
472  /// element type of the array, potentially with type qualifiers missing.
473  /// This method should never be used when type qualifiers are meaningful.
474  const Type *getArrayElementTypeNoTypeQual() const;
475
476  /// getPointeeType - If this is a pointer, ObjC object pointer, or block
477  /// pointer, this returns the respective pointee.
478  QualType getPointeeType() const;
479
480  /// getDesugaredType - Return the specified type with any "sugar" removed from
481  /// the type.  This takes off typedefs, typeof's etc.  If the outer level of
482  /// the type is already concrete, it returns it unmodified.  This is similar
483  /// to getting the canonical type, but it doesn't remove *all* typedefs.  For
484  /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
485  /// concrete.
486  QualType getDesugaredType(bool ForDisplay = false) const;
487
488  /// More type predicates useful for type checking/promotion
489  bool isPromotableIntegerType() const; // C99 6.3.1.1p2
490
491  /// isSignedIntegerType - Return true if this is an integer type that is
492  /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
493  /// an enum decl which has a signed representation, or a vector of signed
494  /// integer element type.
495  bool isSignedIntegerType() const;
496
497  /// isUnsignedIntegerType - Return true if this is an integer type that is
498  /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
499  /// decl which has an unsigned representation, or a vector of unsigned integer
500  /// element type.
501  bool isUnsignedIntegerType() const;
502
503  /// isConstantSizeType - Return true if this is not a variable sized type,
504  /// according to the rules of C99 6.7.5p3.  It is not legal to call this on
505  /// incomplete types.
506  bool isConstantSizeType() const;
507
508  /// isSpecifierType - Returns true if this type can be represented by some
509  /// set of type specifiers.
510  bool isSpecifierType() const;
511
512  QualType getCanonicalTypeInternal() const { return CanonicalType; }
513  void dump() const;
514  virtual void getAsStringInternal(std::string &InnerString,
515                                   const PrintingPolicy &Policy) const = 0;
516  static bool classof(const Type *) { return true; }
517};
518
519/// ExtQualType - TR18037 (C embedded extensions) 6.2.5p26
520/// This supports all kinds of type attributes; including,
521/// address space qualified types, objective-c's __weak and
522/// __strong attributes.
523///
524class ExtQualType : public Type, public llvm::FoldingSetNode {
525  /// BaseType - This is the underlying type that this qualifies.  All CVR
526  /// qualifiers are stored on the QualType that references this type, so we
527  /// can't have any here.
528  Type *BaseType;
529
530  /// Address Space ID - The address space ID this type is qualified with.
531  unsigned AddressSpace;
532  /// GC __weak/__strong attributes
533  QualType::GCAttrTypes GCAttrType;
534
535  ExtQualType(Type *Base, QualType CanonicalPtr, unsigned AddrSpace,
536              QualType::GCAttrTypes gcAttr) :
537      Type(ExtQual, CanonicalPtr, Base->isDependentType()), BaseType(Base),
538      AddressSpace(AddrSpace), GCAttrType(gcAttr) {
539    assert(!isa<ExtQualType>(BaseType) &&
540           "Cannot have ExtQualType of ExtQualType");
541  }
542  friend class ASTContext;  // ASTContext creates these.
543public:
544  Type *getBaseType() const { return BaseType; }
545  QualType::GCAttrTypes getObjCGCAttr() const { return GCAttrType; }
546  unsigned getAddressSpace() const { return AddressSpace; }
547
548  virtual void getAsStringInternal(std::string &InnerString,
549                                   const PrintingPolicy &Policy) const;
550
551  void Profile(llvm::FoldingSetNodeID &ID) {
552    Profile(ID, getBaseType(), AddressSpace, GCAttrType);
553  }
554  static void Profile(llvm::FoldingSetNodeID &ID, Type *Base,
555                      unsigned AddrSpace, QualType::GCAttrTypes gcAttr) {
556    ID.AddPointer(Base);
557    ID.AddInteger(AddrSpace);
558    ID.AddInteger(gcAttr);
559  }
560
561  static bool classof(const Type *T) { return T->getTypeClass() == ExtQual; }
562  static bool classof(const ExtQualType *) { return true; }
563};
564
565
566/// BuiltinType - This class is used for builtin types like 'int'.  Builtin
567/// types are always canonical and have a literal name field.
568class BuiltinType : public Type {
569public:
570  enum Kind {
571    Void,
572
573    Bool,     // This is bool and/or _Bool.
574    Char_U,   // This is 'char' for targets where char is unsigned.
575    UChar,    // This is explicitly qualified unsigned char.
576    Char16,   // This is 'char16_t' for C++.
577    Char32,   // This is 'char32_t' for C++.
578    UShort,
579    UInt,
580    ULong,
581    ULongLong,
582    UInt128,  // __uint128_t
583
584    Char_S,   // This is 'char' for targets where char is signed.
585    SChar,    // This is explicitly qualified signed char.
586    WChar,    // This is 'wchar_t' for C++.
587    Short,
588    Int,
589    Long,
590    LongLong,
591    Int128,   // __int128_t
592
593    Float, Double, LongDouble,
594
595    NullPtr,  // This is the type of C++0x 'nullptr'.
596
597    Overload,  // This represents the type of an overloaded function declaration.
598    Dependent, // This represents the type of a type-dependent expression.
599
600    UndeducedAuto, // In C++0x, this represents the type of an auto variable
601                   // that has not been deduced yet.
602    ObjCId,    // This represents the ObjC 'id' type.
603    ObjCClass  // This represents the ObjC 'Class' type.
604  };
605private:
606  Kind TypeKind;
607public:
608  BuiltinType(Kind K)
609    : Type(Builtin, QualType(), /*Dependent=*/(K == Dependent)),
610      TypeKind(K) {}
611
612  Kind getKind() const { return TypeKind; }
613  const char *getName(const LangOptions &LO) const;
614
615  virtual void getAsStringInternal(std::string &InnerString,
616                                   const PrintingPolicy &Policy) const;
617
618  static bool classof(const Type *T) { return T->getTypeClass() == Builtin; }
619  static bool classof(const BuiltinType *) { return true; }
620};
621
622/// FixedWidthIntType - Used for arbitrary width types that we either don't
623/// want to or can't map to named integer types.  These always have a lower
624/// integer rank than builtin types of the same width.
625class FixedWidthIntType : public Type {
626private:
627  unsigned Width;
628  bool Signed;
629public:
630  FixedWidthIntType(unsigned W, bool S) : Type(FixedWidthInt, QualType(), false),
631                                          Width(W), Signed(S) {}
632
633  unsigned getWidth() const { return Width; }
634  bool isSigned() const { return Signed; }
635  const char *getName() const;
636
637  virtual void getAsStringInternal(std::string &InnerString,
638                                   const PrintingPolicy &Policy) const;
639
640  static bool classof(const Type *T) { return T->getTypeClass() == FixedWidthInt; }
641  static bool classof(const FixedWidthIntType *) { return true; }
642};
643
644/// ComplexType - C99 6.2.5p11 - Complex values.  This supports the C99 complex
645/// types (_Complex float etc) as well as the GCC integer complex extensions.
646///
647class ComplexType : public Type, public llvm::FoldingSetNode {
648  QualType ElementType;
649  ComplexType(QualType Element, QualType CanonicalPtr) :
650    Type(Complex, CanonicalPtr, Element->isDependentType()),
651    ElementType(Element) {
652  }
653  friend class ASTContext;  // ASTContext creates these.
654public:
655  QualType getElementType() const { return ElementType; }
656
657  virtual void getAsStringInternal(std::string &InnerString,
658                                   const PrintingPolicy &Policy) const;
659
660  void Profile(llvm::FoldingSetNodeID &ID) {
661    Profile(ID, getElementType());
662  }
663  static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) {
664    ID.AddPointer(Element.getAsOpaquePtr());
665  }
666
667  static bool classof(const Type *T) { return T->getTypeClass() == Complex; }
668  static bool classof(const ComplexType *) { return true; }
669};
670
671/// PointerType - C99 6.7.5.1 - Pointer Declarators.
672///
673class PointerType : public Type, public llvm::FoldingSetNode {
674  QualType PointeeType;
675
676  PointerType(QualType Pointee, QualType CanonicalPtr) :
677    Type(Pointer, CanonicalPtr, Pointee->isDependentType()), PointeeType(Pointee) {
678  }
679  friend class ASTContext;  // ASTContext creates these.
680public:
681
682  virtual void getAsStringInternal(std::string &InnerString,
683                                   const PrintingPolicy &Policy) const;
684
685  QualType getPointeeType() const { return PointeeType; }
686
687  void Profile(llvm::FoldingSetNodeID &ID) {
688    Profile(ID, getPointeeType());
689  }
690  static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) {
691    ID.AddPointer(Pointee.getAsOpaquePtr());
692  }
693
694  static bool classof(const Type *T) { return T->getTypeClass() == Pointer; }
695  static bool classof(const PointerType *) { return true; }
696};
697
698/// BlockPointerType - pointer to a block type.
699/// This type is to represent types syntactically represented as
700/// "void (^)(int)", etc. Pointee is required to always be a function type.
701///
702class BlockPointerType : public Type, public llvm::FoldingSetNode {
703  QualType PointeeType;  // Block is some kind of pointer type
704  BlockPointerType(QualType Pointee, QualType CanonicalCls) :
705    Type(BlockPointer, CanonicalCls, Pointee->isDependentType()),
706    PointeeType(Pointee) {
707  }
708  friend class ASTContext;  // ASTContext creates these.
709public:
710
711  // Get the pointee type. Pointee is required to always be a function type.
712  QualType getPointeeType() const { return PointeeType; }
713
714  virtual void getAsStringInternal(std::string &InnerString,
715                                   const PrintingPolicy &Policy) const;
716
717  void Profile(llvm::FoldingSetNodeID &ID) {
718      Profile(ID, getPointeeType());
719  }
720  static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) {
721      ID.AddPointer(Pointee.getAsOpaquePtr());
722  }
723
724  static bool classof(const Type *T) {
725    return T->getTypeClass() == BlockPointer;
726  }
727  static bool classof(const BlockPointerType *) { return true; }
728};
729
730/// ReferenceType - Base for LValueReferenceType and RValueReferenceType
731///
732class ReferenceType : public Type, public llvm::FoldingSetNode {
733  QualType PointeeType;
734
735protected:
736  ReferenceType(TypeClass tc, QualType Referencee, QualType CanonicalRef) :
737    Type(tc, CanonicalRef, Referencee->isDependentType()),
738    PointeeType(Referencee) {
739  }
740public:
741  QualType getPointeeType() const { return PointeeType; }
742
743  void Profile(llvm::FoldingSetNodeID &ID) {
744    Profile(ID, getPointeeType());
745  }
746  static void Profile(llvm::FoldingSetNodeID &ID, QualType Referencee) {
747    ID.AddPointer(Referencee.getAsOpaquePtr());
748  }
749
750  static bool classof(const Type *T) {
751    return T->getTypeClass() == LValueReference ||
752           T->getTypeClass() == RValueReference;
753  }
754  static bool classof(const ReferenceType *) { return true; }
755};
756
757/// LValueReferenceType - C++ [dcl.ref] - Lvalue reference
758///
759class LValueReferenceType : public ReferenceType {
760  LValueReferenceType(QualType Referencee, QualType CanonicalRef) :
761    ReferenceType(LValueReference, Referencee, CanonicalRef) {
762  }
763  friend class ASTContext; // ASTContext creates these
764public:
765  virtual void getAsStringInternal(std::string &InnerString,
766                                   const PrintingPolicy &Policy) const;
767
768  static bool classof(const Type *T) {
769    return T->getTypeClass() == LValueReference;
770  }
771  static bool classof(const LValueReferenceType *) { return true; }
772};
773
774/// RValueReferenceType - C++0x [dcl.ref] - Rvalue reference
775///
776class RValueReferenceType : public ReferenceType {
777  RValueReferenceType(QualType Referencee, QualType CanonicalRef) :
778    ReferenceType(RValueReference, Referencee, CanonicalRef) {
779  }
780  friend class ASTContext; // ASTContext creates these
781public:
782  virtual void getAsStringInternal(std::string &InnerString,
783                                   const PrintingPolicy &Policy) const;
784
785  static bool classof(const Type *T) {
786    return T->getTypeClass() == RValueReference;
787  }
788  static bool classof(const RValueReferenceType *) { return true; }
789};
790
791/// MemberPointerType - C++ 8.3.3 - Pointers to members
792///
793class MemberPointerType : public Type, public llvm::FoldingSetNode {
794  QualType PointeeType;
795  /// The class of which the pointee is a member. Must ultimately be a
796  /// RecordType, but could be a typedef or a template parameter too.
797  const Type *Class;
798
799  MemberPointerType(QualType Pointee, const Type *Cls, QualType CanonicalPtr) :
800    Type(MemberPointer, CanonicalPtr,
801         Cls->isDependentType() || Pointee->isDependentType()),
802    PointeeType(Pointee), Class(Cls) {
803  }
804  friend class ASTContext; // ASTContext creates these.
805public:
806
807  QualType getPointeeType() const { return PointeeType; }
808
809  const Type *getClass() const { return Class; }
810
811  virtual void getAsStringInternal(std::string &InnerString,
812                                   const PrintingPolicy &Policy) const;
813
814  void Profile(llvm::FoldingSetNodeID &ID) {
815    Profile(ID, getPointeeType(), getClass());
816  }
817  static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee,
818                      const Type *Class) {
819    ID.AddPointer(Pointee.getAsOpaquePtr());
820    ID.AddPointer(Class);
821  }
822
823  static bool classof(const Type *T) {
824    return T->getTypeClass() == MemberPointer;
825  }
826  static bool classof(const MemberPointerType *) { return true; }
827};
828
829/// ArrayType - C99 6.7.5.2 - Array Declarators.
830///
831class ArrayType : public Type, public llvm::FoldingSetNode {
832public:
833  /// ArraySizeModifier - Capture whether this is a normal array (e.g. int X[4])
834  /// an array with a static size (e.g. int X[static 4]), or an array
835  /// with a star size (e.g. int X[*]).
836  /// 'static' is only allowed on function parameters.
837  enum ArraySizeModifier {
838    Normal, Static, Star
839  };
840private:
841  /// ElementType - The element type of the array.
842  QualType ElementType;
843
844  // NOTE: VC++ treats enums as signed, avoid using the ArraySizeModifier enum
845  /// NOTE: These fields are packed into the bitfields space in the Type class.
846  unsigned SizeModifier : 2;
847
848  /// IndexTypeQuals - Capture qualifiers in declarations like:
849  /// 'int X[static restrict 4]'. For function parameters only.
850  unsigned IndexTypeQuals : 3;
851
852protected:
853  // C++ [temp.dep.type]p1:
854  //   A type is dependent if it is...
855  //     - an array type constructed from any dependent type or whose
856  //       size is specified by a constant expression that is
857  //       value-dependent,
858  ArrayType(TypeClass tc, QualType et, QualType can,
859            ArraySizeModifier sm, unsigned tq)
860    : Type(tc, can, et->isDependentType() || tc == DependentSizedArray),
861      ElementType(et), SizeModifier(sm), IndexTypeQuals(tq) {}
862
863  friend class ASTContext;  // ASTContext creates these.
864public:
865  QualType getElementType() const { return ElementType; }
866  ArraySizeModifier getSizeModifier() const {
867    return ArraySizeModifier(SizeModifier);
868  }
869  unsigned getIndexTypeQualifier() const { return IndexTypeQuals; }
870
871  static bool classof(const Type *T) {
872    return T->getTypeClass() == ConstantArray ||
873           T->getTypeClass() == ConstantArrayWithExpr ||
874           T->getTypeClass() == ConstantArrayWithoutExpr ||
875           T->getTypeClass() == VariableArray ||
876           T->getTypeClass() == IncompleteArray ||
877           T->getTypeClass() == DependentSizedArray;
878  }
879  static bool classof(const ArrayType *) { return true; }
880};
881
882/// ConstantArrayType - This class represents the canonical version of
883/// C arrays with a specified constant size.  For example, the canonical
884/// type for 'int A[4 + 4*100]' is a ConstantArrayType where the element
885/// type is 'int' and the size is 404.
886class ConstantArrayType : public ArrayType {
887  llvm::APInt Size; // Allows us to unique the type.
888
889  ConstantArrayType(QualType et, QualType can, const llvm::APInt &size,
890                    ArraySizeModifier sm, unsigned tq)
891    : ArrayType(ConstantArray, et, can, sm, tq),
892      Size(size) {}
893protected:
894  ConstantArrayType(TypeClass tc, QualType et, QualType can,
895                    const llvm::APInt &size, ArraySizeModifier sm, unsigned tq)
896    : ArrayType(tc, et, can, sm, tq), Size(size) {}
897  friend class ASTContext;  // ASTContext creates these.
898public:
899  const llvm::APInt &getSize() const { return Size; }
900  virtual void getAsStringInternal(std::string &InnerString,
901                                   const PrintingPolicy &Policy) const;
902
903  void Profile(llvm::FoldingSetNodeID &ID) {
904    Profile(ID, getElementType(), getSize(),
905            getSizeModifier(), getIndexTypeQualifier());
906  }
907  static void Profile(llvm::FoldingSetNodeID &ID, QualType ET,
908                      const llvm::APInt &ArraySize, ArraySizeModifier SizeMod,
909                      unsigned TypeQuals) {
910    ID.AddPointer(ET.getAsOpaquePtr());
911    ID.AddInteger(ArraySize.getZExtValue());
912    ID.AddInteger(SizeMod);
913    ID.AddInteger(TypeQuals);
914  }
915  static bool classof(const Type *T) {
916    return T->getTypeClass() == ConstantArray ||
917           T->getTypeClass() == ConstantArrayWithExpr ||
918           T->getTypeClass() == ConstantArrayWithoutExpr;
919  }
920  static bool classof(const ConstantArrayType *) { return true; }
921};
922
923/// ConstantArrayWithExprType - This class represents C arrays with a
924/// constant size specified by means of an integer constant expression.
925/// For example 'int A[sizeof(int)]' has ConstantArrayWithExprType where
926/// the element type is 'int' and the size expression is 'sizeof(int)'.
927/// These types are non-canonical.
928class ConstantArrayWithExprType : public ConstantArrayType {
929  /// SizeExpr - The ICE occurring in the concrete syntax.
930  Expr *SizeExpr;
931  /// Brackets - The left and right array brackets.
932  SourceRange Brackets;
933
934  ConstantArrayWithExprType(QualType et, QualType can,
935                            const llvm::APInt &size, Expr *e,
936                            ArraySizeModifier sm, unsigned tq,
937                            SourceRange brackets)
938    : ConstantArrayType(ConstantArrayWithExpr, et, can, size, sm, tq),
939      SizeExpr(e), Brackets(brackets) {}
940  friend class ASTContext;  // ASTContext creates these.
941  virtual void Destroy(ASTContext& C);
942
943public:
944  Expr *getSizeExpr() const { return SizeExpr; }
945  SourceRange getBracketsRange() const { return Brackets; }
946  SourceLocation getLBracketLoc() const { return Brackets.getBegin(); }
947  SourceLocation getRBracketLoc() const { return Brackets.getEnd(); }
948
949  virtual void getAsStringInternal(std::string &InnerString,
950                                   const PrintingPolicy &Policy) const;
951
952  static bool classof(const Type *T) {
953    return T->getTypeClass() == ConstantArrayWithExpr;
954  }
955  static bool classof(const ConstantArrayWithExprType *) { return true; }
956
957  void Profile(llvm::FoldingSetNodeID &ID) {
958    assert(0 && "Cannot unique ConstantArrayWithExprTypes.");
959  }
960};
961
962/// ConstantArrayWithoutExprType - This class represents C arrays with a
963/// constant size that was not specified by an integer constant expression,
964/// but inferred by static semantics.
965/// For example 'int A[] = { 0, 1, 2 }' has ConstantArrayWithoutExprType.
966/// These types are non-canonical: the corresponding canonical type,
967/// having the size specified in an APInt object, is a ConstantArrayType.
968class ConstantArrayWithoutExprType : public ConstantArrayType {
969
970  ConstantArrayWithoutExprType(QualType et, QualType can,
971                               const llvm::APInt &size,
972                               ArraySizeModifier sm, unsigned tq)
973    : ConstantArrayType(ConstantArrayWithoutExpr, et, can, size, sm, tq) {}
974  friend class ASTContext;  // ASTContext creates these.
975
976public:
977  virtual void getAsStringInternal(std::string &InnerString,
978                                   const PrintingPolicy &Policy) const;
979
980  static bool classof(const Type *T) {
981    return T->getTypeClass() == ConstantArrayWithoutExpr;
982  }
983  static bool classof(const ConstantArrayWithoutExprType *) { return true; }
984
985  void Profile(llvm::FoldingSetNodeID &ID) {
986    assert(0 && "Cannot unique ConstantArrayWithoutExprTypes.");
987  }
988};
989
990/// IncompleteArrayType - This class represents C arrays with an unspecified
991/// size.  For example 'int A[]' has an IncompleteArrayType where the element
992/// type is 'int' and the size is unspecified.
993class IncompleteArrayType : public ArrayType {
994
995  IncompleteArrayType(QualType et, QualType can,
996                      ArraySizeModifier sm, unsigned tq)
997    : ArrayType(IncompleteArray, et, can, sm, tq) {}
998  friend class ASTContext;  // ASTContext creates these.
999public:
1000  virtual void getAsStringInternal(std::string &InnerString,
1001                                   const PrintingPolicy &Policy) const;
1002
1003  static bool classof(const Type *T) {
1004    return T->getTypeClass() == IncompleteArray;
1005  }
1006  static bool classof(const IncompleteArrayType *) { return true; }
1007
1008  friend class StmtIteratorBase;
1009
1010  void Profile(llvm::FoldingSetNodeID &ID) {
1011    Profile(ID, getElementType(), getSizeModifier(), getIndexTypeQualifier());
1012  }
1013
1014  static void Profile(llvm::FoldingSetNodeID &ID, QualType ET,
1015                      ArraySizeModifier SizeMod, unsigned TypeQuals) {
1016    ID.AddPointer(ET.getAsOpaquePtr());
1017    ID.AddInteger(SizeMod);
1018    ID.AddInteger(TypeQuals);
1019  }
1020};
1021
1022/// VariableArrayType - This class represents C arrays with a specified size
1023/// which is not an integer-constant-expression.  For example, 'int s[x+foo()]'.
1024/// Since the size expression is an arbitrary expression, we store it as such.
1025///
1026/// Note: VariableArrayType's aren't uniqued (since the expressions aren't) and
1027/// should not be: two lexically equivalent variable array types could mean
1028/// different things, for example, these variables do not have the same type
1029/// dynamically:
1030///
1031/// void foo(int x) {
1032///   int Y[x];
1033///   ++x;
1034///   int Z[x];
1035/// }
1036///
1037class VariableArrayType : public ArrayType {
1038  /// SizeExpr - An assignment expression. VLA's are only permitted within
1039  /// a function block.
1040  Stmt *SizeExpr;
1041  /// Brackets - The left and right array brackets.
1042  SourceRange Brackets;
1043
1044  VariableArrayType(QualType et, QualType can, Expr *e,
1045                    ArraySizeModifier sm, unsigned tq,
1046                    SourceRange brackets)
1047    : ArrayType(VariableArray, et, can, sm, tq),
1048      SizeExpr((Stmt*) e), Brackets(brackets) {}
1049  friend class ASTContext;  // ASTContext creates these.
1050  virtual void Destroy(ASTContext& C);
1051
1052public:
1053  Expr *getSizeExpr() const {
1054    // We use C-style casts instead of cast<> here because we do not wish
1055    // to have a dependency of Type.h on Stmt.h/Expr.h.
1056    return (Expr*) SizeExpr;
1057  }
1058  SourceRange getBracketsRange() const { return Brackets; }
1059  SourceLocation getLBracketLoc() const { return Brackets.getBegin(); }
1060  SourceLocation getRBracketLoc() const { return Brackets.getEnd(); }
1061
1062  virtual void getAsStringInternal(std::string &InnerString,
1063                                   const PrintingPolicy &Policy) const;
1064
1065  static bool classof(const Type *T) {
1066    return T->getTypeClass() == VariableArray;
1067  }
1068  static bool classof(const VariableArrayType *) { return true; }
1069
1070  friend class StmtIteratorBase;
1071
1072  void Profile(llvm::FoldingSetNodeID &ID) {
1073    assert(0 && "Cannnot unique VariableArrayTypes.");
1074  }
1075};
1076
1077/// DependentSizedArrayType - This type represents an array type in
1078/// C++ whose size is a value-dependent expression. For example:
1079/// @code
1080/// template<typename T, int Size>
1081/// class array {
1082///   T data[Size];
1083/// };
1084/// @endcode
1085/// For these types, we won't actually know what the array bound is
1086/// until template instantiation occurs, at which point this will
1087/// become either a ConstantArrayType or a VariableArrayType.
1088class DependentSizedArrayType : public ArrayType {
1089  /// SizeExpr - An assignment expression that will instantiate to the
1090  /// size of the array.
1091  Stmt *SizeExpr;
1092  /// Brackets - The left and right array brackets.
1093  SourceRange Brackets;
1094
1095  DependentSizedArrayType(QualType et, QualType can, Expr *e,
1096			  ArraySizeModifier sm, unsigned tq,
1097                          SourceRange brackets)
1098    : ArrayType(DependentSizedArray, et, can, sm, tq),
1099      SizeExpr((Stmt*) e), Brackets(brackets) {}
1100  friend class ASTContext;  // ASTContext creates these.
1101  virtual void Destroy(ASTContext& C);
1102
1103public:
1104  Expr *getSizeExpr() const {
1105    // We use C-style casts instead of cast<> here because we do not wish
1106    // to have a dependency of Type.h on Stmt.h/Expr.h.
1107    return (Expr*) SizeExpr;
1108  }
1109  SourceRange getBracketsRange() const { return Brackets; }
1110  SourceLocation getLBracketLoc() const { return Brackets.getBegin(); }
1111  SourceLocation getRBracketLoc() const { return Brackets.getEnd(); }
1112
1113  virtual void getAsStringInternal(std::string &InnerString,
1114                                   const PrintingPolicy &Policy) const;
1115
1116  static bool classof(const Type *T) {
1117    return T->getTypeClass() == DependentSizedArray;
1118  }
1119  static bool classof(const DependentSizedArrayType *) { return true; }
1120
1121  friend class StmtIteratorBase;
1122
1123  void Profile(llvm::FoldingSetNodeID &ID) {
1124    assert(0 && "Cannnot unique DependentSizedArrayTypes.");
1125  }
1126};
1127
1128/// DependentSizedExtVectorType - This type represent an extended vector type
1129/// where either the type or size is dependent. For example:
1130/// @code
1131/// template<typename T, int Size>
1132/// class vector {
1133///   typedef T __attribute__((ext_vector_type(Size))) type;
1134/// }
1135/// @endcode
1136class DependentSizedExtVectorType : public Type {
1137  Expr *SizeExpr;
1138  /// ElementType - The element type of the array.
1139  QualType ElementType;
1140  SourceLocation loc;
1141
1142  DependentSizedExtVectorType(QualType ElementType, QualType can,
1143                              Expr *SizeExpr, SourceLocation loc)
1144    : Type (DependentSizedExtVector, can, true),
1145    SizeExpr(SizeExpr), ElementType(ElementType), loc(loc) {}
1146  friend class ASTContext;
1147  virtual void Destroy(ASTContext& C);
1148
1149public:
1150  const Expr *getSizeExpr() const { return SizeExpr; }
1151  QualType getElementType() const { return ElementType; }
1152  SourceLocation getAttributeLoc() const { return loc; }
1153
1154  virtual void getAsStringInternal(std::string &InnerString,
1155                                   const PrintingPolicy &Policy) const;
1156
1157  static bool classof(const Type *T) {
1158    return T->getTypeClass() == DependentSizedExtVector;
1159  }
1160  static bool classof(const DependentSizedExtVectorType *) { return true; }
1161};
1162
1163
1164/// VectorType - GCC generic vector type. This type is created using
1165/// __attribute__((vector_size(n)), where "n" specifies the vector size in
1166/// bytes. Since the constructor takes the number of vector elements, the
1167/// client is responsible for converting the size into the number of elements.
1168class VectorType : public Type, public llvm::FoldingSetNode {
1169protected:
1170  /// ElementType - The element type of the vector.
1171  QualType ElementType;
1172
1173  /// NumElements - The number of elements in the vector.
1174  unsigned NumElements;
1175
1176  VectorType(QualType vecType, unsigned nElements, QualType canonType) :
1177    Type(Vector, canonType, vecType->isDependentType()),
1178    ElementType(vecType), NumElements(nElements) {}
1179  VectorType(TypeClass tc, QualType vecType, unsigned nElements,
1180             QualType canonType)
1181    : Type(tc, canonType, vecType->isDependentType()), ElementType(vecType),
1182      NumElements(nElements) {}
1183  friend class ASTContext;  // ASTContext creates these.
1184public:
1185
1186  QualType getElementType() const { return ElementType; }
1187  unsigned getNumElements() const { return NumElements; }
1188
1189  virtual void getAsStringInternal(std::string &InnerString,
1190                                   const PrintingPolicy &Policy) const;
1191
1192  void Profile(llvm::FoldingSetNodeID &ID) {
1193    Profile(ID, getElementType(), getNumElements(), getTypeClass());
1194  }
1195  static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType,
1196                      unsigned NumElements, TypeClass TypeClass) {
1197    ID.AddPointer(ElementType.getAsOpaquePtr());
1198    ID.AddInteger(NumElements);
1199    ID.AddInteger(TypeClass);
1200  }
1201  static bool classof(const Type *T) {
1202    return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector;
1203  }
1204  static bool classof(const VectorType *) { return true; }
1205};
1206
1207/// ExtVectorType - Extended vector type. This type is created using
1208/// __attribute__((ext_vector_type(n)), where "n" is the number of elements.
1209/// Unlike vector_size, ext_vector_type is only allowed on typedef's. This
1210/// class enables syntactic extensions, like Vector Components for accessing
1211/// points, colors, and textures (modeled after OpenGL Shading Language).
1212class ExtVectorType : public VectorType {
1213  ExtVectorType(QualType vecType, unsigned nElements, QualType canonType) :
1214    VectorType(ExtVector, vecType, nElements, canonType) {}
1215  friend class ASTContext;  // ASTContext creates these.
1216public:
1217  static int getPointAccessorIdx(char c) {
1218    switch (c) {
1219    default: return -1;
1220    case 'x': return 0;
1221    case 'y': return 1;
1222    case 'z': return 2;
1223    case 'w': return 3;
1224    }
1225  }
1226  static int getNumericAccessorIdx(char c) {
1227    switch (c) {
1228      default: return -1;
1229      case '0': return 0;
1230      case '1': return 1;
1231      case '2': return 2;
1232      case '3': return 3;
1233      case '4': return 4;
1234      case '5': return 5;
1235      case '6': return 6;
1236      case '7': return 7;
1237      case '8': return 8;
1238      case '9': return 9;
1239      case 'A':
1240      case 'a': return 10;
1241      case 'B':
1242      case 'b': return 11;
1243      case 'C':
1244      case 'c': return 12;
1245      case 'D':
1246      case 'd': return 13;
1247      case 'E':
1248      case 'e': return 14;
1249      case 'F':
1250      case 'f': return 15;
1251    }
1252  }
1253
1254  static int getAccessorIdx(char c) {
1255    if (int idx = getPointAccessorIdx(c)+1) return idx-1;
1256    return getNumericAccessorIdx(c);
1257  }
1258
1259  bool isAccessorWithinNumElements(char c) const {
1260    if (int idx = getAccessorIdx(c)+1)
1261      return unsigned(idx-1) < NumElements;
1262    return false;
1263  }
1264  virtual void getAsStringInternal(std::string &InnerString,
1265                                   const PrintingPolicy &Policy) const;
1266
1267  static bool classof(const Type *T) {
1268    return T->getTypeClass() == ExtVector;
1269  }
1270  static bool classof(const ExtVectorType *) { return true; }
1271};
1272
1273/// FunctionType - C99 6.7.5.3 - Function Declarators.  This is the common base
1274/// class of FunctionNoProtoType and FunctionProtoType.
1275///
1276class FunctionType : public Type {
1277  /// SubClassData - This field is owned by the subclass, put here to pack
1278  /// tightly with the ivars in Type.
1279  bool SubClassData : 1;
1280
1281  /// TypeQuals - Used only by FunctionProtoType, put here to pack with the
1282  /// other bitfields.
1283  /// The qualifiers are part of FunctionProtoType because...
1284  ///
1285  /// C++ 8.3.5p4: The return type, the parameter type list and the
1286  /// cv-qualifier-seq, [...], are part of the function type.
1287  ///
1288  unsigned TypeQuals : 3;
1289
1290  // The type returned by the function.
1291  QualType ResultType;
1292protected:
1293  FunctionType(TypeClass tc, QualType res, bool SubclassInfo,
1294               unsigned typeQuals, QualType Canonical, bool Dependent)
1295    : Type(tc, Canonical, Dependent),
1296      SubClassData(SubclassInfo), TypeQuals(typeQuals), ResultType(res) {}
1297  bool getSubClassData() const { return SubClassData; }
1298  unsigned getTypeQuals() const { return TypeQuals; }
1299public:
1300
1301  QualType getResultType() const { return ResultType; }
1302
1303
1304  static bool classof(const Type *T) {
1305    return T->getTypeClass() == FunctionNoProto ||
1306           T->getTypeClass() == FunctionProto;
1307  }
1308  static bool classof(const FunctionType *) { return true; }
1309};
1310
1311/// FunctionNoProtoType - Represents a K&R-style 'int foo()' function, which has
1312/// no information available about its arguments.
1313class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode {
1314  FunctionNoProtoType(QualType Result, QualType Canonical)
1315    : FunctionType(FunctionNoProto, Result, false, 0, Canonical,
1316                   /*Dependent=*/false) {}
1317  friend class ASTContext;  // ASTContext creates these.
1318public:
1319  // No additional state past what FunctionType provides.
1320
1321  virtual void getAsStringInternal(std::string &InnerString,
1322                                   const PrintingPolicy &Policy) const;
1323
1324  void Profile(llvm::FoldingSetNodeID &ID) {
1325    Profile(ID, getResultType());
1326  }
1327  static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType) {
1328    ID.AddPointer(ResultType.getAsOpaquePtr());
1329  }
1330
1331  static bool classof(const Type *T) {
1332    return T->getTypeClass() == FunctionNoProto;
1333  }
1334  static bool classof(const FunctionNoProtoType *) { return true; }
1335};
1336
1337/// FunctionProtoType - Represents a prototype with argument type info, e.g.
1338/// 'int foo(int)' or 'int foo(void)'.  'void' is represented as having no
1339/// arguments, not as having a single void argument. Such a type can have an
1340/// exception specification, but this specification is not part of the canonical
1341/// type.
1342class FunctionProtoType : public FunctionType, public llvm::FoldingSetNode {
1343  /// hasAnyDependentType - Determine whether there are any dependent
1344  /// types within the arguments passed in.
1345  static bool hasAnyDependentType(const QualType *ArgArray, unsigned numArgs) {
1346    for (unsigned Idx = 0; Idx < numArgs; ++Idx)
1347      if (ArgArray[Idx]->isDependentType())
1348    return true;
1349
1350    return false;
1351  }
1352
1353  FunctionProtoType(QualType Result, const QualType *ArgArray, unsigned numArgs,
1354                    bool isVariadic, unsigned typeQuals, bool hasExs,
1355                    bool hasAnyExs, const QualType *ExArray,
1356                    unsigned numExs, QualType Canonical)
1357    : FunctionType(FunctionProto, Result, isVariadic, typeQuals, Canonical,
1358                   (Result->isDependentType() ||
1359                    hasAnyDependentType(ArgArray, numArgs))),
1360      NumArgs(numArgs), NumExceptions(numExs), HasExceptionSpec(hasExs),
1361      AnyExceptionSpec(hasAnyExs) {
1362    // Fill in the trailing argument array.
1363    QualType *ArgInfo = reinterpret_cast<QualType*>(this+1);
1364    for (unsigned i = 0; i != numArgs; ++i)
1365      ArgInfo[i] = ArgArray[i];
1366    // Fill in the exception array.
1367    QualType *Ex = ArgInfo + numArgs;
1368    for (unsigned i = 0; i != numExs; ++i)
1369      Ex[i] = ExArray[i];
1370  }
1371
1372  /// NumArgs - The number of arguments this function has, not counting '...'.
1373  unsigned NumArgs : 20;
1374
1375  /// NumExceptions - The number of types in the exception spec, if any.
1376  unsigned NumExceptions : 10;
1377
1378  /// HasExceptionSpec - Whether this function has an exception spec at all.
1379  bool HasExceptionSpec : 1;
1380
1381  /// AnyExceptionSpec - Whether this function has a throw(...) spec.
1382  bool AnyExceptionSpec : 1;
1383
1384  /// ArgInfo - There is an variable size array after the class in memory that
1385  /// holds the argument types.
1386
1387  /// Exceptions - There is another variable size array after ArgInfo that
1388  /// holds the exception types.
1389
1390  friend class ASTContext;  // ASTContext creates these.
1391
1392public:
1393  unsigned getNumArgs() const { return NumArgs; }
1394  QualType getArgType(unsigned i) const {
1395    assert(i < NumArgs && "Invalid argument number!");
1396    return arg_type_begin()[i];
1397  }
1398
1399  bool hasExceptionSpec() const { return HasExceptionSpec; }
1400  bool hasAnyExceptionSpec() const { return AnyExceptionSpec; }
1401  unsigned getNumExceptions() const { return NumExceptions; }
1402  QualType getExceptionType(unsigned i) const {
1403    assert(i < NumExceptions && "Invalid exception number!");
1404    return exception_begin()[i];
1405  }
1406  bool hasEmptyExceptionSpec() const {
1407    return hasExceptionSpec() && !hasAnyExceptionSpec() &&
1408      getNumExceptions() == 0;
1409  }
1410
1411  bool isVariadic() const { return getSubClassData(); }
1412  unsigned getTypeQuals() const { return FunctionType::getTypeQuals(); }
1413
1414  typedef const QualType *arg_type_iterator;
1415  arg_type_iterator arg_type_begin() const {
1416    return reinterpret_cast<const QualType *>(this+1);
1417  }
1418  arg_type_iterator arg_type_end() const { return arg_type_begin()+NumArgs; }
1419
1420  typedef const QualType *exception_iterator;
1421  exception_iterator exception_begin() const {
1422    // exceptions begin where arguments end
1423    return arg_type_end();
1424  }
1425  exception_iterator exception_end() const {
1426    return exception_begin() + NumExceptions;
1427  }
1428
1429  virtual void getAsStringInternal(std::string &InnerString,
1430                                   const PrintingPolicy &Policy) const;
1431
1432  static bool classof(const Type *T) {
1433    return T->getTypeClass() == FunctionProto;
1434  }
1435  static bool classof(const FunctionProtoType *) { return true; }
1436
1437  void Profile(llvm::FoldingSetNodeID &ID);
1438  static void Profile(llvm::FoldingSetNodeID &ID, QualType Result,
1439                      arg_type_iterator ArgTys, unsigned NumArgs,
1440                      bool isVariadic, unsigned TypeQuals,
1441                      bool hasExceptionSpec, bool anyExceptionSpec,
1442                      unsigned NumExceptions, exception_iterator Exs);
1443};
1444
1445
1446class TypedefType : public Type {
1447  TypedefDecl *Decl;
1448protected:
1449  TypedefType(TypeClass tc, TypedefDecl *D, QualType can)
1450    : Type(tc, can, can->isDependentType()), Decl(D) {
1451    assert(!isa<TypedefType>(can) && "Invalid canonical type");
1452  }
1453  friend class ASTContext;  // ASTContext creates these.
1454public:
1455
1456  TypedefDecl *getDecl() const { return Decl; }
1457
1458  /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
1459  /// potentially looking through *all* consecutive typedefs.  This returns the
1460  /// sum of the type qualifiers, so if you have:
1461  ///   typedef const int A;
1462  ///   typedef volatile A B;
1463  /// looking through the typedefs for B will give you "const volatile A".
1464  QualType LookThroughTypedefs() const;
1465
1466  virtual void getAsStringInternal(std::string &InnerString,
1467                                   const PrintingPolicy &Policy) const;
1468
1469  static bool classof(const Type *T) { return T->getTypeClass() == Typedef; }
1470  static bool classof(const TypedefType *) { return true; }
1471};
1472
1473/// TypeOfExprType (GCC extension).
1474class TypeOfExprType : public Type {
1475  Expr *TOExpr;
1476  TypeOfExprType(Expr *E, QualType can = QualType());
1477  friend class ASTContext;  // ASTContext creates these.
1478public:
1479  Expr *getUnderlyingExpr() const { return TOExpr; }
1480
1481  virtual void getAsStringInternal(std::string &InnerString,
1482                                   const PrintingPolicy &Policy) const;
1483
1484  static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; }
1485  static bool classof(const TypeOfExprType *) { return true; }
1486};
1487
1488/// TypeOfType (GCC extension).
1489class TypeOfType : public Type {
1490  QualType TOType;
1491  TypeOfType(QualType T, QualType can)
1492    : Type(TypeOf, can, T->isDependentType()), TOType(T) {
1493    assert(!isa<TypedefType>(can) && "Invalid canonical type");
1494  }
1495  friend class ASTContext;  // ASTContext creates these.
1496public:
1497  QualType getUnderlyingType() const { return TOType; }
1498
1499  virtual void getAsStringInternal(std::string &InnerString,
1500                                   const PrintingPolicy &Policy) const;
1501
1502  static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; }
1503  static bool classof(const TypeOfType *) { return true; }
1504};
1505
1506/// DecltypeType (C++0x)
1507class DecltypeType : public Type {
1508  Expr *E;
1509
1510  // FIXME: We could get rid of UnderlyingType if we wanted to: We would have to
1511  // Move getDesugaredType to ASTContext so that it can call getDecltypeForExpr
1512  // from it.
1513  QualType UnderlyingType;
1514
1515  DecltypeType(Expr *E, QualType underlyingType, QualType can = QualType());
1516  friend class ASTContext;  // ASTContext creates these.
1517public:
1518  Expr *getUnderlyingExpr() const { return E; }
1519  QualType getUnderlyingType() const { return UnderlyingType; }
1520
1521  virtual void getAsStringInternal(std::string &InnerString,
1522                                   const PrintingPolicy &Policy) const;
1523
1524  static bool classof(const Type *T) { return T->getTypeClass() == Decltype; }
1525  static bool classof(const DecltypeType *) { return true; }
1526};
1527
1528class TagType : public Type {
1529  /// Stores the TagDecl associated with this type. The decl will
1530  /// point to the TagDecl that actually defines the entity (or is a
1531  /// definition in progress), if there is such a definition. The
1532  /// single-bit value will be non-zero when this tag is in the
1533  /// process of being defined.
1534  mutable llvm::PointerIntPair<TagDecl *, 1> decl;
1535  friend class ASTContext;
1536  friend class TagDecl;
1537
1538protected:
1539  TagType(TypeClass TC, TagDecl *D, QualType can);
1540
1541public:
1542  TagDecl *getDecl() const { return decl.getPointer(); }
1543
1544  /// @brief Determines whether this type is in the process of being
1545  /// defined.
1546  bool isBeingDefined() const { return decl.getInt(); }
1547  void setBeingDefined(bool Def) { decl.setInt(Def? 1 : 0); }
1548
1549  virtual void getAsStringInternal(std::string &InnerString,
1550                                   const PrintingPolicy &Policy) const;
1551
1552  static bool classof(const Type *T) {
1553    return T->getTypeClass() >= TagFirst && T->getTypeClass() <= TagLast;
1554  }
1555  static bool classof(const TagType *) { return true; }
1556  static bool classof(const RecordType *) { return true; }
1557  static bool classof(const EnumType *) { return true; }
1558};
1559
1560/// RecordType - This is a helper class that allows the use of isa/cast/dyncast
1561/// to detect TagType objects of structs/unions/classes.
1562class RecordType : public TagType {
1563protected:
1564  explicit RecordType(RecordDecl *D)
1565    : TagType(Record, reinterpret_cast<TagDecl*>(D), QualType()) { }
1566  explicit RecordType(TypeClass TC, RecordDecl *D)
1567    : TagType(TC, reinterpret_cast<TagDecl*>(D), QualType()) { }
1568  friend class ASTContext;   // ASTContext creates these.
1569public:
1570
1571  RecordDecl *getDecl() const {
1572    return reinterpret_cast<RecordDecl*>(TagType::getDecl());
1573  }
1574
1575  // FIXME: This predicate is a helper to QualType/Type. It needs to
1576  // recursively check all fields for const-ness. If any field is declared
1577  // const, it needs to return false.
1578  bool hasConstFields() const { return false; }
1579
1580  // FIXME: RecordType needs to check when it is created that all fields are in
1581  // the same address space, and return that.
1582  unsigned getAddressSpace() const { return 0; }
1583
1584  static bool classof(const TagType *T);
1585  static bool classof(const Type *T) {
1586    return isa<TagType>(T) && classof(cast<TagType>(T));
1587  }
1588  static bool classof(const RecordType *) { return true; }
1589};
1590
1591/// EnumType - This is a helper class that allows the use of isa/cast/dyncast
1592/// to detect TagType objects of enums.
1593class EnumType : public TagType {
1594  explicit EnumType(EnumDecl *D)
1595    : TagType(Enum, reinterpret_cast<TagDecl*>(D), QualType()) { }
1596  friend class ASTContext;   // ASTContext creates these.
1597public:
1598
1599  EnumDecl *getDecl() const {
1600    return reinterpret_cast<EnumDecl*>(TagType::getDecl());
1601  }
1602
1603  static bool classof(const TagType *T);
1604  static bool classof(const Type *T) {
1605    return isa<TagType>(T) && classof(cast<TagType>(T));
1606  }
1607  static bool classof(const EnumType *) { return true; }
1608};
1609
1610class TemplateTypeParmType : public Type, public llvm::FoldingSetNode {
1611  unsigned Depth : 15;
1612  unsigned Index : 16;
1613  unsigned ParameterPack : 1;
1614  IdentifierInfo *Name;
1615
1616  TemplateTypeParmType(unsigned D, unsigned I, bool PP, IdentifierInfo *N,
1617                       QualType Canon)
1618    : Type(TemplateTypeParm, Canon, /*Dependent=*/true),
1619      Depth(D), Index(I), ParameterPack(PP), Name(N) { }
1620
1621  TemplateTypeParmType(unsigned D, unsigned I, bool PP)
1622    : Type(TemplateTypeParm, QualType(this, 0), /*Dependent=*/true),
1623      Depth(D), Index(I), ParameterPack(PP), Name(0) { }
1624
1625  friend class ASTContext;  // ASTContext creates these
1626
1627public:
1628  unsigned getDepth() const { return Depth; }
1629  unsigned getIndex() const { return Index; }
1630  bool isParameterPack() const { return ParameterPack; }
1631  IdentifierInfo *getName() const { return Name; }
1632
1633  virtual void getAsStringInternal(std::string &InnerString,
1634                                   const PrintingPolicy &Policy) const;
1635
1636  void Profile(llvm::FoldingSetNodeID &ID) {
1637    Profile(ID, Depth, Index, ParameterPack, Name);
1638  }
1639
1640  static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth,
1641                      unsigned Index, bool ParameterPack,
1642                      IdentifierInfo *Name) {
1643    ID.AddInteger(Depth);
1644    ID.AddInteger(Index);
1645    ID.AddBoolean(ParameterPack);
1646    ID.AddPointer(Name);
1647  }
1648
1649  static bool classof(const Type *T) {
1650    return T->getTypeClass() == TemplateTypeParm;
1651  }
1652  static bool classof(const TemplateTypeParmType *T) { return true; }
1653};
1654
1655/// \brief Represents the type of a template specialization as written
1656/// in the source code.
1657///
1658/// Template specialization types represent the syntactic form of a
1659/// template-id that refers to a type, e.g., @c vector<int>. Some
1660/// template specialization types are syntactic sugar, whose canonical
1661/// type will point to some other type node that represents the
1662/// instantiation or class template specialization. For example, a
1663/// class template specialization type of @c vector<int> will refer to
1664/// a tag type for the instantiation
1665/// @c std::vector<int, std::allocator<int>>.
1666///
1667/// Other template specialization types, for which the template name
1668/// is dependent, may be canonical types. These types are always
1669/// dependent.
1670class TemplateSpecializationType
1671  : public Type, public llvm::FoldingSetNode {
1672
1673  /// \brief The name of the template being specialized.
1674  TemplateName Template;
1675
1676  /// \brief - The number of template arguments named in this class
1677  /// template specialization.
1678  unsigned NumArgs;
1679
1680  TemplateSpecializationType(TemplateName T,
1681                             const TemplateArgument *Args,
1682                             unsigned NumArgs, QualType Canon);
1683
1684  virtual void Destroy(ASTContext& C);
1685
1686  friend class ASTContext;  // ASTContext creates these
1687
1688public:
1689  /// \brief Determine whether any of the given template arguments are
1690  /// dependent.
1691  static bool anyDependentTemplateArguments(const TemplateArgument *Args,
1692                                            unsigned NumArgs);
1693
1694  /// \brief Print a template argument list, including the '<' and '>'
1695  /// enclosing the template arguments.
1696  static std::string PrintTemplateArgumentList(const TemplateArgument *Args,
1697                                               unsigned NumArgs,
1698                                               const PrintingPolicy &Policy);
1699
1700  typedef const TemplateArgument * iterator;
1701
1702  iterator begin() const { return getArgs(); }
1703  iterator end() const;
1704
1705  /// \brief Retrieve the name of the template that we are specializing.
1706  TemplateName getTemplateName() const { return Template; }
1707
1708  /// \brief Retrieve the template arguments.
1709  const TemplateArgument *getArgs() const {
1710    return reinterpret_cast<const TemplateArgument *>(this + 1);
1711  }
1712
1713  /// \brief Retrieve the number of template arguments.
1714  unsigned getNumArgs() const { return NumArgs; }
1715
1716  /// \brief Retrieve a specific template argument as a type.
1717  /// \precondition @c isArgType(Arg)
1718  const TemplateArgument &getArg(unsigned Idx) const;
1719
1720  virtual void getAsStringInternal(std::string &InnerString,
1721                                   const PrintingPolicy &Policy) const;
1722
1723  void Profile(llvm::FoldingSetNodeID &ID) {
1724    Profile(ID, Template, getArgs(), NumArgs);
1725  }
1726
1727  static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T,
1728                      const TemplateArgument *Args, unsigned NumArgs);
1729
1730  static bool classof(const Type *T) {
1731    return T->getTypeClass() == TemplateSpecialization;
1732  }
1733  static bool classof(const TemplateSpecializationType *T) { return true; }
1734};
1735
1736/// \brief Represents a type that was referred to via a qualified
1737/// name, e.g., N::M::type.
1738///
1739/// This type is used to keep track of a type name as written in the
1740/// source code, including any nested-name-specifiers. The type itself
1741/// is always "sugar", used to express what was written in the source
1742/// code but containing no additional semantic information.
1743class QualifiedNameType : public Type, public llvm::FoldingSetNode {
1744  /// \brief The nested name specifier containing the qualifier.
1745  NestedNameSpecifier *NNS;
1746
1747  /// \brief The type that this qualified name refers to.
1748  QualType NamedType;
1749
1750  QualifiedNameType(NestedNameSpecifier *NNS, QualType NamedType,
1751                    QualType CanonType)
1752    : Type(QualifiedName, CanonType, NamedType->isDependentType()),
1753      NNS(NNS), NamedType(NamedType) { }
1754
1755  friend class ASTContext;  // ASTContext creates these
1756
1757public:
1758  /// \brief Retrieve the qualification on this type.
1759  NestedNameSpecifier *getQualifier() const { return NNS; }
1760
1761  /// \brief Retrieve the type named by the qualified-id.
1762  QualType getNamedType() const { return NamedType; }
1763
1764  virtual void getAsStringInternal(std::string &InnerString,
1765                                   const PrintingPolicy &Policy) const;
1766
1767  void Profile(llvm::FoldingSetNodeID &ID) {
1768    Profile(ID, NNS, NamedType);
1769  }
1770
1771  static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
1772                      QualType NamedType) {
1773    ID.AddPointer(NNS);
1774    NamedType.Profile(ID);
1775  }
1776
1777  static bool classof(const Type *T) {
1778    return T->getTypeClass() == QualifiedName;
1779  }
1780  static bool classof(const QualifiedNameType *T) { return true; }
1781};
1782
1783/// \brief Represents a 'typename' specifier that names a type within
1784/// a dependent type, e.g., "typename T::type".
1785///
1786/// TypenameType has a very similar structure to QualifiedNameType,
1787/// which also involves a nested-name-specifier following by a type,
1788/// and (FIXME!) both can even be prefixed by the 'typename'
1789/// keyword. However, the two types serve very different roles:
1790/// QualifiedNameType is a non-semantic type that serves only as sugar
1791/// to show how a particular type was written in the source
1792/// code. TypenameType, on the other hand, only occurs when the
1793/// nested-name-specifier is dependent, such that we cannot resolve
1794/// the actual type until after instantiation.
1795class TypenameType : public Type, public llvm::FoldingSetNode {
1796  /// \brief The nested name specifier containing the qualifier.
1797  NestedNameSpecifier *NNS;
1798
1799  typedef llvm::PointerUnion<const IdentifierInfo *,
1800                             const TemplateSpecializationType *> NameType;
1801
1802  /// \brief The type that this typename specifier refers to.
1803  NameType Name;
1804
1805  TypenameType(NestedNameSpecifier *NNS, const IdentifierInfo *Name,
1806               QualType CanonType)
1807    : Type(Typename, CanonType, true), NNS(NNS), Name(Name) {
1808    assert(NNS->isDependent() &&
1809           "TypenameType requires a dependent nested-name-specifier");
1810  }
1811
1812  TypenameType(NestedNameSpecifier *NNS, const TemplateSpecializationType *Ty,
1813               QualType CanonType)
1814    : Type(Typename, CanonType, true), NNS(NNS), Name(Ty) {
1815    assert(NNS->isDependent() &&
1816           "TypenameType requires a dependent nested-name-specifier");
1817  }
1818
1819  friend class ASTContext;  // ASTContext creates these
1820
1821public:
1822  /// \brief Retrieve the qualification on this type.
1823  NestedNameSpecifier *getQualifier() const { return NNS; }
1824
1825  /// \brief Retrieve the type named by the typename specifier as an
1826  /// identifier.
1827  ///
1828  /// This routine will return a non-NULL identifier pointer when the
1829  /// form of the original typename was terminated by an identifier,
1830  /// e.g., "typename T::type".
1831  const IdentifierInfo *getIdentifier() const {
1832    return Name.dyn_cast<const IdentifierInfo *>();
1833  }
1834
1835  /// \brief Retrieve the type named by the typename specifier as a
1836  /// type specialization.
1837  const TemplateSpecializationType *getTemplateId() const {
1838    return Name.dyn_cast<const TemplateSpecializationType *>();
1839  }
1840
1841  virtual void getAsStringInternal(std::string &InnerString,
1842                                   const PrintingPolicy &Policy) const;
1843
1844  void Profile(llvm::FoldingSetNodeID &ID) {
1845    Profile(ID, NNS, Name);
1846  }
1847
1848  static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
1849                      NameType Name) {
1850    ID.AddPointer(NNS);
1851    ID.AddPointer(Name.getOpaqueValue());
1852  }
1853
1854  static bool classof(const Type *T) {
1855    return T->getTypeClass() == Typename;
1856  }
1857  static bool classof(const TypenameType *T) { return true; }
1858};
1859
1860/// ObjCInterfaceType - Interfaces are the core concept in Objective-C for
1861/// object oriented design.  They basically correspond to C++ classes.  There
1862/// are two kinds of interface types, normal interfaces like "NSString" and
1863/// qualified interfaces, which are qualified with a protocol list like
1864/// "NSString<NSCopyable, NSAmazing>".  Qualified interface types are instances
1865/// of ObjCQualifiedInterfaceType, which is a subclass of ObjCInterfaceType.
1866class ObjCInterfaceType : public Type {
1867  ObjCInterfaceDecl *Decl;
1868protected:
1869  ObjCInterfaceType(TypeClass tc, ObjCInterfaceDecl *D) :
1870    Type(tc, QualType(), /*Dependent=*/false), Decl(D) { }
1871  friend class ASTContext;  // ASTContext creates these.
1872
1873  // FIXME: These can go away when we move ASTContext::canAssignObjCInterfaces
1874  // to this class (as a static helper).
1875  bool isObjCIdInterface() const;
1876  bool isObjCClassInterface() const;
1877public:
1878
1879  ObjCInterfaceDecl *getDecl() const { return Decl; }
1880
1881  /// qual_iterator and friends: this provides access to the (potentially empty)
1882  /// list of protocols qualifying this interface.  If this is an instance of
1883  /// ObjCQualifiedInterfaceType it returns the list, otherwise it returns an
1884  /// empty list if there are no qualifying protocols.
1885  typedef llvm::SmallVector<ObjCProtocolDecl*, 8>::const_iterator qual_iterator;
1886  inline qual_iterator qual_begin() const;
1887  inline qual_iterator qual_end() const;
1888  bool qual_empty() const { return getTypeClass() != ObjCQualifiedInterface; }
1889
1890  /// getNumProtocols - Return the number of qualifying protocols in this
1891  /// interface type, or 0 if there are none.
1892  inline unsigned getNumProtocols() const;
1893
1894  virtual void getAsStringInternal(std::string &InnerString,
1895                                   const PrintingPolicy &Policy) const;
1896  static bool classof(const Type *T) {
1897    return T->getTypeClass() == ObjCInterface ||
1898           T->getTypeClass() == ObjCQualifiedInterface;
1899  }
1900  static bool classof(const ObjCInterfaceType *) { return true; }
1901};
1902
1903/// ObjCObjectPointerType - Used to represent 'id', 'Interface *', 'id <p>',
1904/// and 'Interface <p> *'.
1905///
1906/// Duplicate protocols are removed and protocol list is canonicalized to be in
1907/// alphabetical order.
1908class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode {
1909  QualType PointeeType; // A builtin or interface type.
1910
1911  // List of protocols for this protocol conforming object type
1912  // List is sorted on protocol name. No protocol is entered more than once.
1913  llvm::SmallVector<ObjCProtocolDecl*, 8> Protocols;
1914
1915  ObjCObjectPointerType(QualType T, ObjCProtocolDecl **Protos, unsigned NumP) :
1916    Type(ObjCObjectPointer, QualType(), /*Dependent=*/false),
1917    PointeeType(T), Protocols(Protos, Protos+NumP) { }
1918  friend class ASTContext;  // ASTContext creates these.
1919
1920public:
1921  // Get the pointee type. Pointee will either be:
1922  // - a built-in type (for 'id' and 'Class').
1923  // - an interface type (for user-defined types).
1924  // - a TypedefType whose canonical type is an interface (as in 'T' below).
1925  //   For example: typedef NSObject T; T *var;
1926  QualType getPointeeType() const { return PointeeType; }
1927
1928  const ObjCInterfaceType *getInterfaceType() const {
1929    return PointeeType->getAsObjCInterfaceType();
1930  }
1931  /// getInterfaceDecl - returns an interface decl for user-defined types.
1932  ObjCInterfaceDecl *getInterfaceDecl() const {
1933    return getInterfaceType() ? getInterfaceType()->getDecl() : 0;
1934  }
1935  /// isObjCIdType - true for "id".
1936  bool isObjCIdType() const {
1937    return getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCId) &&
1938           !Protocols.size();
1939  }
1940  /// isObjCClassType - true for "Class".
1941  bool isObjCClassType() const {
1942    return getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCClass) &&
1943           !Protocols.size();
1944  }
1945  /// isObjCQualifiedIdType - true for "id <p>".
1946  bool isObjCQualifiedIdType() const {
1947    return getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCId) &&
1948           Protocols.size();
1949  }
1950  /// isObjCQualifiedClassType - true for "Class <p>".
1951  bool isQualifiedClassType() const {
1952    return getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCClass) &&
1953           Protocols.size();
1954  }
1955  /// qual_iterator and friends: this provides access to the (potentially empty)
1956  /// list of protocols qualifying this interface.
1957  typedef llvm::SmallVector<ObjCProtocolDecl*, 8>::const_iterator qual_iterator;
1958
1959  qual_iterator qual_begin() const { return Protocols.begin(); }
1960  qual_iterator qual_end() const   { return Protocols.end(); }
1961  bool qual_empty() const { return Protocols.size() == 0; }
1962
1963  /// getNumProtocols - Return the number of qualifying protocols in this
1964  /// interface type, or 0 if there are none.
1965  unsigned getNumProtocols() const { return Protocols.size(); }
1966
1967  void Profile(llvm::FoldingSetNodeID &ID);
1968  static void Profile(llvm::FoldingSetNodeID &ID, QualType T,
1969                      ObjCProtocolDecl **protocols, unsigned NumProtocols);
1970  virtual void getAsStringInternal(std::string &InnerString,
1971                                   const PrintingPolicy &Policy) const;
1972  static bool classof(const Type *T) {
1973    return T->getTypeClass() == ObjCObjectPointer;
1974  }
1975  static bool classof(const ObjCObjectPointerType *) { return true; }
1976};
1977
1978/// ObjCQualifiedInterfaceType - This class represents interface types
1979/// conforming to a list of protocols, such as INTF<Proto1, Proto2, Proto1>.
1980///
1981/// Duplicate protocols are removed and protocol list is canonicalized to be in
1982/// alphabetical order.
1983/// FIXME: Remove this class (converting uses to ObjCObjectPointerType).
1984class ObjCQualifiedInterfaceType : public ObjCInterfaceType,
1985                                   public llvm::FoldingSetNode {
1986
1987  // List of protocols for this protocol conforming object type
1988  // List is sorted on protocol name. No protocol is enterred more than once.
1989  llvm::SmallVector<ObjCProtocolDecl*, 4> Protocols;
1990
1991  ObjCQualifiedInterfaceType(ObjCInterfaceDecl *D,
1992                             ObjCProtocolDecl **Protos, unsigned NumP) :
1993    ObjCInterfaceType(ObjCQualifiedInterface, D),
1994    Protocols(Protos, Protos+NumP) { }
1995  friend class ASTContext;  // ASTContext creates these.
1996public:
1997
1998  unsigned getNumProtocols() const {
1999    return Protocols.size();
2000  }
2001
2002  qual_iterator qual_begin() const { return Protocols.begin(); }
2003  qual_iterator qual_end() const   { return Protocols.end(); }
2004
2005  virtual void getAsStringInternal(std::string &InnerString,
2006                                   const PrintingPolicy &Policy) const;
2007
2008  void Profile(llvm::FoldingSetNodeID &ID);
2009  static void Profile(llvm::FoldingSetNodeID &ID,
2010                      const ObjCInterfaceDecl *Decl,
2011                      ObjCProtocolDecl **protocols, unsigned NumProtocols);
2012
2013  static bool classof(const Type *T) {
2014    return T->getTypeClass() == ObjCQualifiedInterface;
2015  }
2016  static bool classof(const ObjCQualifiedInterfaceType *) { return true; }
2017};
2018
2019inline ObjCInterfaceType::qual_iterator ObjCInterfaceType::qual_begin() const {
2020  if (const ObjCQualifiedInterfaceType *QIT =
2021         dyn_cast<ObjCQualifiedInterfaceType>(this))
2022    return QIT->qual_begin();
2023  return 0;
2024}
2025inline ObjCInterfaceType::qual_iterator ObjCInterfaceType::qual_end() const {
2026  if (const ObjCQualifiedInterfaceType *QIT =
2027         dyn_cast<ObjCQualifiedInterfaceType>(this))
2028    return QIT->qual_end();
2029  return 0;
2030}
2031
2032/// getNumProtocols - Return the number of qualifying protocols in this
2033/// interface type, or 0 if there are none.
2034inline unsigned ObjCInterfaceType::getNumProtocols() const {
2035  if (const ObjCQualifiedInterfaceType *QIT =
2036        dyn_cast<ObjCQualifiedInterfaceType>(this))
2037    return QIT->getNumProtocols();
2038  return 0;
2039}
2040
2041// Inline function definitions.
2042
2043/// getUnqualifiedType - Return the type without any qualifiers.
2044inline QualType QualType::getUnqualifiedType() const {
2045  Type *TP = getTypePtr();
2046  if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(TP))
2047    TP = EXTQT->getBaseType();
2048  return QualType(TP, 0);
2049}
2050
2051/// getAddressSpace - Return the address space of this type.
2052inline unsigned QualType::getAddressSpace() const {
2053  QualType CT = getTypePtr()->getCanonicalTypeInternal();
2054  if (const ArrayType *AT = dyn_cast<ArrayType>(CT))
2055    return AT->getElementType().getAddressSpace();
2056  if (const RecordType *RT = dyn_cast<RecordType>(CT))
2057    return RT->getAddressSpace();
2058  if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CT))
2059    return EXTQT->getAddressSpace();
2060  return 0;
2061}
2062
2063/// getObjCGCAttr - Return the gc attribute of this type.
2064inline QualType::GCAttrTypes QualType::getObjCGCAttr() const {
2065  QualType CT = getTypePtr()->getCanonicalTypeInternal();
2066  if (const ArrayType *AT = dyn_cast<ArrayType>(CT))
2067      return AT->getElementType().getObjCGCAttr();
2068  if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CT))
2069    return EXTQT->getObjCGCAttr();
2070  if (const ObjCObjectPointerType *PT = CT->getAsObjCObjectPointerType())
2071    return PT->getPointeeType().getObjCGCAttr();
2072  return GCNone;
2073}
2074
2075/// isMoreQualifiedThan - Determine whether this type is more
2076/// qualified than the Other type. For example, "const volatile int"
2077/// is more qualified than "const int", "volatile int", and
2078/// "int". However, it is not more qualified than "const volatile
2079/// int".
2080inline bool QualType::isMoreQualifiedThan(QualType Other) const {
2081  unsigned MyQuals = this->getCVRQualifiers();
2082  unsigned OtherQuals = Other.getCVRQualifiers();
2083  if (getAddressSpace() != Other.getAddressSpace())
2084    return false;
2085  return MyQuals != OtherQuals && (MyQuals | OtherQuals) == MyQuals;
2086}
2087
2088/// isAtLeastAsQualifiedAs - Determine whether this type is at last
2089/// as qualified as the Other type. For example, "const volatile
2090/// int" is at least as qualified as "const int", "volatile int",
2091/// "int", and "const volatile int".
2092inline bool QualType::isAtLeastAsQualifiedAs(QualType Other) const {
2093  unsigned MyQuals = this->getCVRQualifiers();
2094  unsigned OtherQuals = Other.getCVRQualifiers();
2095  if (getAddressSpace() != Other.getAddressSpace())
2096    return false;
2097  return (MyQuals | OtherQuals) == MyQuals;
2098}
2099
2100/// getNonReferenceType - If Type is a reference type (e.g., const
2101/// int&), returns the type that the reference refers to ("const
2102/// int"). Otherwise, returns the type itself. This routine is used
2103/// throughout Sema to implement C++ 5p6:
2104///
2105///   If an expression initially has the type "reference to T" (8.3.2,
2106///   8.5.3), the type is adjusted to "T" prior to any further
2107///   analysis, the expression designates the object or function
2108///   denoted by the reference, and the expression is an lvalue.
2109inline QualType QualType::getNonReferenceType() const {
2110  if (const ReferenceType *RefType = (*this)->getAsReferenceType())
2111    return RefType->getPointeeType();
2112  else
2113    return *this;
2114}
2115
2116inline const TypedefType* Type::getAsTypedefType() const {
2117  return dyn_cast<TypedefType>(this);
2118}
2119inline const ObjCInterfaceType *Type::getAsPointerToObjCInterfaceType() const {
2120  if (const PointerType *PT = getAsPointerType())
2121    return PT->getPointeeType()->getAsObjCInterfaceType();
2122  return 0;
2123}
2124
2125// NOTE: All of these methods use "getUnqualifiedType" to strip off address
2126// space qualifiers if present.
2127inline bool Type::isFunctionType() const {
2128  return isa<FunctionType>(CanonicalType.getUnqualifiedType());
2129}
2130inline bool Type::isPointerType() const {
2131  return isa<PointerType>(CanonicalType.getUnqualifiedType());
2132}
2133inline bool Type::isAnyPointerType() const {
2134  return isPointerType() || isObjCObjectPointerType();
2135}
2136inline bool Type::isBlockPointerType() const {
2137  return isa<BlockPointerType>(CanonicalType.getUnqualifiedType());
2138}
2139inline bool Type::isReferenceType() const {
2140  return isa<ReferenceType>(CanonicalType.getUnqualifiedType());
2141}
2142inline bool Type::isLValueReferenceType() const {
2143  return isa<LValueReferenceType>(CanonicalType.getUnqualifiedType());
2144}
2145inline bool Type::isRValueReferenceType() const {
2146  return isa<RValueReferenceType>(CanonicalType.getUnqualifiedType());
2147}
2148inline bool Type::isFunctionPointerType() const {
2149  if (const PointerType* T = getAsPointerType())
2150    return T->getPointeeType()->isFunctionType();
2151  else
2152    return false;
2153}
2154inline bool Type::isMemberPointerType() const {
2155  return isa<MemberPointerType>(CanonicalType.getUnqualifiedType());
2156}
2157inline bool Type::isMemberFunctionPointerType() const {
2158  if (const MemberPointerType* T = getAsMemberPointerType())
2159    return T->getPointeeType()->isFunctionType();
2160  else
2161    return false;
2162}
2163inline bool Type::isArrayType() const {
2164  return isa<ArrayType>(CanonicalType.getUnqualifiedType());
2165}
2166inline bool Type::isConstantArrayType() const {
2167  return isa<ConstantArrayType>(CanonicalType.getUnqualifiedType());
2168}
2169inline bool Type::isIncompleteArrayType() const {
2170  return isa<IncompleteArrayType>(CanonicalType.getUnqualifiedType());
2171}
2172inline bool Type::isVariableArrayType() const {
2173  return isa<VariableArrayType>(CanonicalType.getUnqualifiedType());
2174}
2175inline bool Type::isDependentSizedArrayType() const {
2176  return isa<DependentSizedArrayType>(CanonicalType.getUnqualifiedType());
2177}
2178inline bool Type::isRecordType() const {
2179  return isa<RecordType>(CanonicalType.getUnqualifiedType());
2180}
2181inline bool Type::isAnyComplexType() const {
2182  return isa<ComplexType>(CanonicalType.getUnqualifiedType());
2183}
2184inline bool Type::isVectorType() const {
2185  return isa<VectorType>(CanonicalType.getUnqualifiedType());
2186}
2187inline bool Type::isExtVectorType() const {
2188  return isa<ExtVectorType>(CanonicalType.getUnqualifiedType());
2189}
2190inline bool Type::isObjCObjectPointerType() const {
2191  return isa<ObjCObjectPointerType>(CanonicalType.getUnqualifiedType());
2192}
2193inline bool Type::isObjCInterfaceType() const {
2194  return isa<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
2195}
2196inline bool Type::isObjCQualifiedInterfaceType() const {
2197  return isa<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType());
2198}
2199inline bool Type::isObjCQualifiedIdType() const {
2200  if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType())
2201    return OPT->isObjCQualifiedIdType();
2202  return false;
2203}
2204inline bool Type::isObjCIdType() const {
2205  if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType())
2206    return OPT->isObjCIdType();
2207  return false;
2208}
2209inline bool Type::isObjCClassType() const {
2210  if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType())
2211    return OPT->isObjCClassType();
2212  return false;
2213}
2214inline bool Type::isObjCBuiltinType() const {
2215  return isObjCIdType() || isObjCClassType();
2216}
2217
2218inline bool Type::isTemplateTypeParmType() const {
2219  return isa<TemplateTypeParmType>(CanonicalType.getUnqualifiedType());
2220}
2221
2222inline bool Type::isSpecificBuiltinType(unsigned K) const {
2223  if (const BuiltinType *BT = getAsBuiltinType())
2224    if (BT->getKind() == (BuiltinType::Kind) K)
2225      return true;
2226  return false;
2227}
2228
2229/// \brief Determines whether this is a type for which one can define
2230/// an overloaded operator.
2231inline bool Type::isOverloadableType() const {
2232  return isDependentType() || isRecordType() || isEnumeralType();
2233}
2234
2235inline bool Type::hasPointerRepresentation() const {
2236  return (isPointerType() || isReferenceType() || isBlockPointerType() ||
2237          isObjCInterfaceType() || isObjCObjectPointerType() ||
2238          isObjCQualifiedInterfaceType() || isNullPtrType());
2239}
2240
2241inline bool Type::hasObjCPointerRepresentation() const {
2242  return (isObjCInterfaceType() || isObjCObjectPointerType() ||
2243          isObjCQualifiedInterfaceType());
2244}
2245
2246/// Insertion operator for diagnostics.  This allows sending QualType's into a
2247/// diagnostic with <<.
2248inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
2249                                           QualType T) {
2250  DB.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()),
2251                  Diagnostic::ak_qualtype);
2252  return DB;
2253}
2254
2255/// Member-template getAs<specific type>'.
2256template <typename T> const T *Type::getAs() const {
2257  // If this is directly a T type, return it.
2258  if (const T *Ty = dyn_cast<T>(this))
2259    return Ty;
2260
2261  // If the canonical form of this type isn't the right kind, reject it.
2262  if (!isa<T>(CanonicalType)) {
2263    // Look through type qualifiers
2264    if (isa<T>(CanonicalType.getUnqualifiedType()))
2265      return CanonicalType.getUnqualifiedType()->getAs<T>();
2266    return 0;
2267  }
2268
2269  // If this is a typedef for a pointer type, strip the typedef off without
2270  // losing all typedef information.
2271  return cast<T>(getDesugaredType());
2272}
2273
2274}  // end namespace clang
2275
2276#endif
2277