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