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