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