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