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