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