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