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