Type.h revision 71af02f1cea2bcedacbff2a20d883bf2b8042eb0
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 "llvm/Support/Casting.h"
19#include "llvm/ADT/APSInt.h"
20#include "llvm/ADT/FoldingSet.h"
21#include "llvm/ADT/PointerIntPair.h"
22#include "llvm/Bitcode/SerializationFwd.h"
23using llvm::isa;
24using llvm::cast;
25using llvm::cast_or_null;
26using llvm::dyn_cast;
27using llvm::dyn_cast_or_null;
28
29namespace clang {
30  class ASTContext;
31  class Type;
32  class TypedefDecl;
33  class TemplateTypeParmDecl;
34  class TagDecl;
35  class RecordDecl;
36  class CXXRecordDecl;
37  class EnumDecl;
38  class FieldDecl;
39  class ObjCInterfaceDecl;
40  class ObjCProtocolDecl;
41  class ObjCMethodDecl;
42  class Expr;
43  class Stmt;
44  class SourceLocation;
45  class PointerLikeType;
46  class PointerType;
47  class BlockPointerType;
48  class ReferenceType;
49  class VectorType;
50  class ArrayType;
51  class ConstantArrayType;
52  class VariableArrayType;
53  class IncompleteArrayType;
54  class DependentSizedArrayType;
55  class RecordType;
56  class EnumType;
57  class ComplexType;
58  class TagType;
59  class TypedefType;
60  class TemplateTypeParmType;
61  class FunctionType;
62  class FunctionTypeProto;
63  class ExtVectorType;
64  class BuiltinType;
65  class ObjCInterfaceType;
66  class ObjCQualifiedIdType;
67  class ObjCQualifiedInterfaceType;
68  class StmtIteratorBase;
69
70/// QualType - For efficiency, we don't store CVR-qualified types as nodes on
71/// their own: instead each reference to a type stores the qualifiers.  This
72/// greatly reduces the number of nodes we need to allocate for types (for
73/// example we only need one for 'int', 'const int', 'volatile int',
74/// 'const volatile int', etc).
75///
76/// As an added efficiency bonus, instead of making this a pair, we just store
77/// the three bits we care about in the low bits of the pointer.  To handle the
78/// packing/unpacking, we make QualType be a simple wrapper class that acts like
79/// a smart pointer.
80class QualType {
81  llvm::PointerIntPair<Type*, 3> Value;
82public:
83  enum TQ {   // NOTE: These flags must be kept in sync with DeclSpec::TQ.
84    Const    = 0x1,
85    Restrict = 0x2,
86    Volatile = 0x4,
87    CVRFlags = Const|Restrict|Volatile
88  };
89
90  QualType() {}
91
92  QualType(const Type *Ptr, unsigned Quals)
93    : Value(const_cast<Type*>(Ptr), Quals) {}
94
95  unsigned getCVRQualifiers() const { return Value.getInt(); }
96  void setCVRQualifiers(unsigned Quals) { Value.setInt(Quals); }
97  Type *getTypePtr() const { return Value.getPointer(); }
98
99  void *getAsOpaquePtr() const { return Value.getOpaqueValue(); }
100  static QualType getFromOpaquePtr(void *Ptr) {
101    QualType T;
102    T.Value.setFromOpaqueValue(Ptr);
103    return T;
104  }
105
106  Type &operator*() const {
107    return *getTypePtr();
108  }
109
110  Type *operator->() const {
111    return getTypePtr();
112  }
113
114  /// isNull - Return true if this QualType doesn't point to a type yet.
115  bool isNull() const {
116    return getTypePtr() == 0;
117  }
118
119  bool isConstQualified() const {
120    return (getCVRQualifiers() & Const) ? true : false;
121  }
122  bool isVolatileQualified() const {
123    return (getCVRQualifiers() & Volatile) ? true : false;
124  }
125  bool isRestrictQualified() const {
126    return (getCVRQualifiers() & Restrict) ? true : false;
127  }
128
129  bool isConstant(ASTContext& Ctx) const;
130
131  /// addConst/addVolatile/addRestrict - add the specified type qual to this
132  /// QualType.
133  void addConst()    { Value.setInt(Value.getInt() | Const); }
134  void addVolatile() { Value.setInt(Value.getInt() | Volatile); }
135  void addRestrict() { Value.setInt(Value.getInt() | Restrict); }
136
137  void removeConst()    { Value.setInt(Value.getInt() & ~Const); }
138  void removeVolatile() { Value.setInt(Value.getInt() & ~Volatile); }
139  void removeRestrict() { Value.setInt(Value.getInt() & ~Restrict); }
140
141  QualType getQualifiedType(unsigned TQs) const {
142    return QualType(getTypePtr(), TQs);
143  }
144  QualType getWithAdditionalQualifiers(unsigned TQs) const {
145    return QualType(getTypePtr(), TQs|getCVRQualifiers());
146  }
147
148  QualType withConst() const { return getWithAdditionalQualifiers(Const); }
149  QualType withVolatile() const { return getWithAdditionalQualifiers(Volatile);}
150  QualType withRestrict() const { return getWithAdditionalQualifiers(Restrict);}
151
152  QualType getUnqualifiedType() const;
153  bool isMoreQualifiedThan(QualType Other) const;
154  bool isAtLeastAsQualifiedAs(QualType Other) const;
155  QualType getNonReferenceType() const;
156
157
158  /// operator==/!= - Indicate whether the specified types and qualifiers are
159  /// identical.
160  bool operator==(const QualType &RHS) const {
161    return Value == RHS.Value;
162  }
163  bool operator!=(const QualType &RHS) const {
164    return Value != RHS.Value;
165  }
166  std::string getAsString() const {
167    std::string S;
168    getAsStringInternal(S);
169    return S;
170  }
171  void getAsStringInternal(std::string &Str) const;
172
173  void dump(const char *s) const;
174  void dump() const;
175
176  void Profile(llvm::FoldingSetNodeID &ID) const {
177    ID.AddPointer(getAsOpaquePtr());
178  }
179
180public:
181
182  /// getAddressSpace - Return the address space of this type.
183  inline unsigned getAddressSpace() const;
184
185  /// Emit - Serialize a QualType to Bitcode.
186  void Emit(llvm::Serializer& S) const;
187
188  /// Read - Deserialize a QualType from Bitcode.
189  static QualType ReadVal(llvm::Deserializer& D);
190
191  void ReadBackpatch(llvm::Deserializer& D);
192};
193
194} // end clang.
195
196namespace llvm {
197/// Implement simplify_type for QualType, so that we can dyn_cast from QualType
198/// to a specific Type class.
199template<> struct simplify_type<const ::clang::QualType> {
200  typedef ::clang::Type* SimpleType;
201  static SimpleType getSimplifiedValue(const ::clang::QualType &Val) {
202    return Val.getTypePtr();
203  }
204};
205template<> struct simplify_type< ::clang::QualType>
206  : public simplify_type<const ::clang::QualType> {};
207
208} // end namespace llvm
209
210namespace clang {
211
212/// Type - This is the base class of the type hierarchy.  A central concept
213/// with types is that each type always has a canonical type.  A canonical type
214/// is the type with any typedef names stripped out of it or the types it
215/// references.  For example, consider:
216///
217///  typedef int  foo;
218///  typedef foo* bar;
219///    'int *'    'foo *'    'bar'
220///
221/// There will be a Type object created for 'int'.  Since int is canonical, its
222/// canonicaltype pointer points to itself.  There is also a Type for 'foo' (a
223/// TypeNameType).  Its CanonicalType pointer points to the 'int' Type.  Next
224/// there is a PointerType that represents 'int*', which, like 'int', is
225/// canonical.  Finally, there is a PointerType type for 'foo*' whose canonical
226/// type is 'int*', and there is a TypeNameType for 'bar', whose canonical type
227/// is also 'int*'.
228///
229/// Non-canonical types are useful for emitting diagnostics, without losing
230/// information about typedefs being used.  Canonical types are useful for type
231/// comparisons (they allow by-pointer equality tests) and useful for reasoning
232/// about whether something has a particular form (e.g. is a function type),
233/// because they implicitly, recursively, strip all typedefs out of a type.
234///
235/// Types, once created, are immutable.
236///
237class Type {
238public:
239  enum TypeClass {
240    Builtin, Complex, Pointer, Reference,
241    ConstantArray, VariableArray, IncompleteArray, DependentSizedArray,
242    Vector, ExtVector,
243    FunctionNoProto, FunctionProto,
244    TypeName, Tagged, ASQual,
245    TemplateTypeParm,
246    ObjCInterface, ObjCQualifiedInterface,
247    ObjCQualifiedId,
248    TypeOfExp, TypeOfTyp, // GNU typeof extension.
249    BlockPointer          // C extension
250  };
251private:
252  QualType CanonicalType;
253
254  /// Dependent - Whether this type is a dependent type (C++ [temp.dep.type]).
255  bool Dependent : 1;
256
257  /// TypeClass bitfield - Enum that specifies what subclass this belongs to.
258  /// Note that this should stay at the end of the ivars for Type so that
259  /// subclasses can pack their bitfields into the same word.
260  unsigned TC : 5;
261
262protected:
263  // silence VC++ warning C4355: 'this' : used in base member initializer list
264  Type *this_() { return this; }
265  Type(TypeClass tc, QualType Canonical, bool dependent)
266    : CanonicalType(Canonical.isNull() ? QualType(this_(), 0) : Canonical),
267      Dependent(dependent), TC(tc) {}
268  virtual ~Type() {}
269  virtual void Destroy(ASTContext& C);
270  friend class ASTContext;
271
272  void EmitTypeInternal(llvm::Serializer& S) const;
273  void ReadTypeInternal(llvm::Deserializer& D);
274
275public:
276  TypeClass getTypeClass() const { return static_cast<TypeClass>(TC); }
277
278  bool isCanonical() const { return CanonicalType.getTypePtr() == this; }
279
280  /// Types are partitioned into 3 broad categories (C99 6.2.5p1):
281  /// object types, function types, and incomplete types.
282
283  /// isObjectType - types that fully describe objects. An object is a region
284  /// of memory that can be examined and stored into (H&S).
285  bool isObjectType() const;
286
287  /// isIncompleteType - Return true if this is an incomplete type.
288  /// A type that can describe objects, but which lacks information needed to
289  /// determine its size (e.g. void, or a fwd declared struct). Clients of this
290  /// routine will need to determine if the size is actually required.
291  bool isIncompleteType() const;
292
293  /// isIncompleteOrObjectType - Return true if this is an incomplete or object
294  /// type, in other words, not a function type.
295  bool isIncompleteOrObjectType() const {
296    return !isFunctionType();
297  }
298
299  /// isVariablyModifiedType (C99 6.7.5.2p2) - Return true for variable array
300  /// types that have a non-constant expression. This does not include "[]".
301  bool isVariablyModifiedType() const;
302
303  /// Helper methods to distinguish type categories. All type predicates
304  /// operate on the canonical type, ignoring typedefs and qualifiers.
305
306  /// isIntegerType() does *not* include complex integers (a GCC extension).
307  /// isComplexIntegerType() can be used to test for complex integers.
308  bool isIntegerType() const;     // C99 6.2.5p17 (int, char, bool, enum)
309  bool isEnumeralType() const;
310  bool isBooleanType() const;
311  bool isCharType() const;
312  bool isWideCharType() const;
313  bool isIntegralType() const;
314
315  /// Floating point categories.
316  bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double)
317  /// isComplexType() does *not* include complex integers (a GCC extension).
318  /// isComplexIntegerType() can be used to test for complex integers.
319  bool isComplexType() const;      // C99 6.2.5p11 (complex)
320  bool isAnyComplexType() const;   // C99 6.2.5p11 (complex) + Complex Int.
321  bool isFloatingType() const;     // C99 6.2.5p11 (real floating + complex)
322  bool isRealType() const;         // C99 6.2.5p17 (real floating + integer)
323  bool isArithmeticType() const;   // C99 6.2.5p18 (integer + floating)
324  bool isVoidType() const;         // C99 6.2.5p19
325  bool isDerivedType() const;      // C99 6.2.5p20
326  bool isScalarType() const;       // C99 6.2.5p21 (arithmetic + pointers)
327  bool isAggregateType() const;    // C99 6.2.5p21 (arrays, structures)
328
329  // Type Predicates: Check to see if this type is structurally the specified
330  // type, ignoring typedefs and qualifiers.
331  bool isFunctionType() const;
332  bool isPointerLikeType() const; // Pointer or Reference.
333  bool isPointerType() const;
334  bool isBlockPointerType() const;
335  bool isReferenceType() const;
336  bool isFunctionPointerType() const;
337  bool isArrayType() const;
338  bool isConstantArrayType() const;
339  bool isIncompleteArrayType() const;
340  bool isVariableArrayType() const;
341  bool isDependentSizedArrayType() const;
342  bool isRecordType() const;
343  bool isClassType() const;
344  bool isStructureType() const;
345  bool isUnionType() const;
346  bool isComplexIntegerType() const;            // GCC _Complex integer type.
347  bool isVectorType() const;                    // GCC vector type.
348  bool isExtVectorType() const;                 // Extended vector type.
349  bool isObjCInterfaceType() const;             // NSString or NSString<foo>
350  bool isObjCQualifiedInterfaceType() const;    // NSString<foo>
351  bool isObjCQualifiedIdType() const;           // id<foo>
352  bool isTemplateTypeParmType() const;          // C++ template type parameter
353
354  /// isDependentType - Whether this type is a dependent type, meaning
355  /// that its definition somehow depends on a template parameter
356  /// (C++ [temp.dep.type]).
357  bool isDependentType() const { return Dependent; }
358  bool isOverloadType() const;                  // C++ overloaded function
359
360  // Type Checking Functions: Check to see if this type is structurally the
361  // specified type, ignoring typedefs and qualifiers, and return a pointer to
362  // the best type we can.
363  const BuiltinType *getAsBuiltinType() const;
364  const FunctionType *getAsFunctionType() const;
365  const FunctionTypeProto *getAsFunctionTypeProto() const;
366  const PointerLikeType *getAsPointerLikeType() const; // Pointer or Reference.
367  const PointerType *getAsPointerType() const;
368  const BlockPointerType *getAsBlockPointerType() const;
369  const ReferenceType *getAsReferenceType() const;
370  const RecordType *getAsRecordType() const;
371  const RecordType *getAsStructureType() const;
372  /// NOTE: getAs*ArrayType are methods on ASTContext.
373  const TypedefType *getAsTypedefType() const;
374  const RecordType *getAsUnionType() const;
375  const EnumType *getAsEnumType() const;
376  const VectorType *getAsVectorType() const; // GCC vector type.
377  const ComplexType *getAsComplexType() const;
378  const ComplexType *getAsComplexIntegerType() const; // GCC complex int type.
379  const ExtVectorType *getAsExtVectorType() const; // Extended vector type.
380  const ObjCInterfaceType *getAsObjCInterfaceType() const;
381  const ObjCQualifiedInterfaceType *getAsObjCQualifiedInterfaceType() const;
382  const ObjCQualifiedIdType *getAsObjCQualifiedIdType() const;
383  const TemplateTypeParmType *getAsTemplateTypeParmType() const;
384
385  /// getAsPointerToObjCInterfaceType - If this is a pointer to an ObjC
386  /// interface, return the interface type, otherwise return null.
387  const ObjCInterfaceType *getAsPointerToObjCInterfaceType() const;
388
389  /// getArrayElementTypeNoTypeQual - If this is an array type, return the
390  /// element type of the array, potentially with type qualifiers missing.
391  /// This method should never be used when type qualifiers are meaningful.
392  const Type *getArrayElementTypeNoTypeQual() const;
393
394
395
396  /// getDesugaredType - Return the specified type with any "sugar" removed from
397  /// the type.  This takes off typedefs, typeof's etc.  If the outer level of
398  /// the type is already concrete, it returns it unmodified.  This is similar
399  /// to getting the canonical type, but it doesn't remove *all* typedefs.  For
400  /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
401  /// concrete.
402  QualType getDesugaredType() const;
403
404  /// More type predicates useful for type checking/promotion
405  bool isPromotableIntegerType() const; // C99 6.3.1.1p2
406
407  /// isSignedIntegerType - Return true if this is an integer type that is
408  /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
409  /// an enum decl which has a signed representation, or a vector of signed
410  /// integer element type.
411  bool isSignedIntegerType() const;
412
413  /// isUnsignedIntegerType - Return true if this is an integer type that is
414  /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
415  /// decl which has an unsigned representation, or a vector of unsigned integer
416  /// element type.
417  bool isUnsignedIntegerType() const;
418
419  /// isConstantSizeType - Return true if this is not a variable sized type,
420  /// according to the rules of C99 6.7.5p3.  It is not legal to call this on
421  /// incomplete types.
422  bool isConstantSizeType() const;
423
424  QualType getCanonicalTypeInternal() const { return CanonicalType; }
425  void dump() const;
426  virtual void getAsStringInternal(std::string &InnerString) const = 0;
427  static bool classof(const Type *) { return true; }
428
429protected:
430  /// Emit - Emit a Type to bitcode.  Used by ASTContext.
431  void Emit(llvm::Serializer& S) const;
432
433  /// Create - Construct a Type from bitcode.  Used by ASTContext.
434  static void Create(ASTContext& Context, unsigned i, llvm::Deserializer& S);
435
436  /// EmitImpl - Subclasses must implement this method in order to
437  ///  be serialized.
438  // FIXME: Make this abstract once implemented.
439  virtual void EmitImpl(llvm::Serializer& S) const {
440    assert (false && "Serializization for type not supported.");
441  }
442};
443
444/// ASQualType - TR18037 (C embedded extensions) 6.2.5p26
445/// This supports address space qualified types.
446///
447class ASQualType : public Type, public llvm::FoldingSetNode {
448  /// BaseType - This is the underlying type that this qualifies.  All CVR
449  /// qualifiers are stored on the QualType that references this type, so we
450  /// can't have any here.
451  Type *BaseType;
452  /// Address Space ID - The address space ID this type is qualified with.
453  unsigned AddressSpace;
454  ASQualType(Type *Base, QualType CanonicalPtr, unsigned AddrSpace) :
455    Type(ASQual, CanonicalPtr, Base->isDependentType()), BaseType(Base),
456    AddressSpace(AddrSpace) {
457  }
458  friend class ASTContext;  // ASTContext creates these.
459public:
460  Type *getBaseType() const { return BaseType; }
461  unsigned getAddressSpace() const { return AddressSpace; }
462
463  virtual void getAsStringInternal(std::string &InnerString) const;
464
465  void Profile(llvm::FoldingSetNodeID &ID) {
466    Profile(ID, getBaseType(), AddressSpace);
467  }
468  static void Profile(llvm::FoldingSetNodeID &ID, Type *Base,
469                      unsigned AddrSpace) {
470    ID.AddPointer(Base);
471    ID.AddInteger(AddrSpace);
472  }
473
474  static bool classof(const Type *T) { return T->getTypeClass() == ASQual; }
475  static bool classof(const ASQualType *) { return true; }
476
477protected:
478  virtual void EmitImpl(llvm::Serializer& S) const;
479  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
480  friend class Type;
481};
482
483
484/// BuiltinType - This class is used for builtin types like 'int'.  Builtin
485/// types are always canonical and have a literal name field.
486class BuiltinType : public Type {
487public:
488  enum Kind {
489    Void,
490
491    Bool,     // This is bool and/or _Bool.
492    Char_U,   // This is 'char' for targets where char is unsigned.
493    UChar,    // This is explicitly qualified unsigned char.
494    UShort,
495    UInt,
496    ULong,
497    ULongLong,
498
499    Char_S,   // This is 'char' for targets where char is signed.
500    SChar,    // This is explicitly qualified signed char.
501    WChar,    // This is 'wchar_t' for C++.
502    Short,
503    Int,
504    Long,
505    LongLong,
506
507    Float, Double, LongDouble,
508
509    Overload,  // This represents the type of an overloaded function declaration.
510    Dependent  // This represents the type of a type-dependent expression.
511  };
512private:
513  Kind TypeKind;
514public:
515  BuiltinType(Kind K)
516    : Type(Builtin, QualType(), /*Dependent=*/(K == Dependent)),
517      TypeKind(K) {}
518
519  Kind getKind() const { return TypeKind; }
520  const char *getName() const;
521
522  virtual void getAsStringInternal(std::string &InnerString) const;
523
524  static bool classof(const Type *T) { return T->getTypeClass() == Builtin; }
525  static bool classof(const BuiltinType *) { return true; }
526};
527
528/// ComplexType - C99 6.2.5p11 - Complex values.  This supports the C99 complex
529/// types (_Complex float etc) as well as the GCC integer complex extensions.
530///
531class ComplexType : public Type, public llvm::FoldingSetNode {
532  QualType ElementType;
533  ComplexType(QualType Element, QualType CanonicalPtr) :
534    Type(Complex, CanonicalPtr, Element->isDependentType()),
535    ElementType(Element) {
536  }
537  friend class ASTContext;  // ASTContext creates these.
538public:
539  QualType getElementType() const { return ElementType; }
540
541  virtual void getAsStringInternal(std::string &InnerString) const;
542
543  void Profile(llvm::FoldingSetNodeID &ID) {
544    Profile(ID, getElementType());
545  }
546  static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) {
547    ID.AddPointer(Element.getAsOpaquePtr());
548  }
549
550  static bool classof(const Type *T) { return T->getTypeClass() == Complex; }
551  static bool classof(const ComplexType *) { return true; }
552
553protected:
554  virtual void EmitImpl(llvm::Serializer& S) const;
555  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
556  friend class Type;
557};
558
559/// PointerLikeType - Common base class for pointers and references.
560/// FIXME: Add more documentation on this classes design point. For example,
561/// should BlockPointerType inherit from it? Is the concept of a PointerLikeType
562/// in the C++ standard?
563///
564class PointerLikeType : public Type {
565  QualType PointeeType;
566protected:
567  PointerLikeType(TypeClass K, QualType Pointee, QualType CanonicalPtr) :
568    Type(K, CanonicalPtr, Pointee->isDependentType()), PointeeType(Pointee) {
569  }
570public:
571
572  QualType getPointeeType() const { return PointeeType; }
573
574  static bool classof(const Type *T) {
575    return T->getTypeClass() == Pointer || T->getTypeClass() == Reference;
576  }
577  static bool classof(const PointerLikeType *) { return true; }
578};
579
580/// PointerType - C99 6.7.5.1 - Pointer Declarators.
581///
582class PointerType : public PointerLikeType, public llvm::FoldingSetNode {
583  PointerType(QualType Pointee, QualType CanonicalPtr) :
584    PointerLikeType(Pointer, Pointee, CanonicalPtr) {
585  }
586  friend class ASTContext;  // ASTContext creates these.
587public:
588
589  virtual void getAsStringInternal(std::string &InnerString) const;
590
591  void Profile(llvm::FoldingSetNodeID &ID) {
592    Profile(ID, getPointeeType());
593  }
594  static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) {
595    ID.AddPointer(Pointee.getAsOpaquePtr());
596  }
597
598  static bool classof(const Type *T) { return T->getTypeClass() == Pointer; }
599  static bool classof(const PointerType *) { return true; }
600
601protected:
602  virtual void EmitImpl(llvm::Serializer& S) const;
603  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
604  friend class Type;
605};
606
607/// BlockPointerType - pointer to a block type.
608/// This type is to represent types syntactically represented as
609/// "void (^)(int)", etc. Pointee is required to always be a function type.
610/// FIXME: Should BlockPointerType inherit from PointerLikeType? It could
611/// simplfy some type checking code, however PointerLikeType doesn't appear
612/// to be used by the type checker.
613///
614class BlockPointerType : public Type, public llvm::FoldingSetNode {
615  QualType PointeeType;  // Block is some kind of pointer type
616  BlockPointerType(QualType Pointee, QualType CanonicalCls) :
617    Type(BlockPointer, CanonicalCls, Pointee->isDependentType()),
618    PointeeType(Pointee) {
619  }
620  friend class ASTContext;  // ASTContext creates these.
621public:
622
623  // Get the pointee type. Pointee is required to always be a function type.
624  QualType getPointeeType() const { return PointeeType; }
625
626  virtual void getAsStringInternal(std::string &InnerString) const;
627
628  void Profile(llvm::FoldingSetNodeID &ID) {
629      Profile(ID, getPointeeType());
630  }
631  static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) {
632      ID.AddPointer(Pointee.getAsOpaquePtr());
633  }
634
635  static bool classof(const Type *T) {
636    return T->getTypeClass() == BlockPointer;
637  }
638  static bool classof(const BlockPointerType *) { return true; }
639
640  protected:
641    virtual void EmitImpl(llvm::Serializer& S) const;
642    static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
643    friend class Type;
644};
645
646/// ReferenceType - C++ 8.3.2 - Reference Declarators.
647///
648class ReferenceType : public PointerLikeType, public llvm::FoldingSetNode {
649  ReferenceType(QualType Referencee, QualType CanonicalRef) :
650    PointerLikeType(Reference, Referencee, CanonicalRef) {
651  }
652  friend class ASTContext;  // ASTContext creates these.
653public:
654  virtual void getAsStringInternal(std::string &InnerString) const;
655
656  void Profile(llvm::FoldingSetNodeID &ID) {
657    Profile(ID, getPointeeType());
658  }
659  static void Profile(llvm::FoldingSetNodeID &ID, QualType Referencee) {
660    ID.AddPointer(Referencee.getAsOpaquePtr());
661  }
662
663  static bool classof(const Type *T) { return T->getTypeClass() == Reference; }
664  static bool classof(const ReferenceType *) { return true; }
665};
666
667/// ArrayType - C99 6.7.5.2 - Array Declarators.
668///
669class ArrayType : public Type, public llvm::FoldingSetNode {
670public:
671  /// ArraySizeModifier - Capture whether this is a normal array (e.g. int X[4])
672  /// an array with a static size (e.g. int X[static 4]), or an array
673  /// with a star size (e.g. int X[*]).
674  /// 'static' is only allowed on function parameters.
675  enum ArraySizeModifier {
676    Normal, Static, Star
677  };
678private:
679  /// ElementType - The element type of the array.
680  QualType ElementType;
681
682  // NOTE: VC++ treats enums as signed, avoid using the ArraySizeModifier enum
683  /// NOTE: These fields are packed into the bitfields space in the Type class.
684  unsigned SizeModifier : 2;
685
686  /// IndexTypeQuals - Capture qualifiers in declarations like:
687  /// 'int X[static restrict 4]'. For function parameters only.
688  unsigned IndexTypeQuals : 3;
689
690protected:
691  // C++ [temp.dep.type]p1:
692  //   A type is dependent if it is...
693  //     - an array type constructed from any dependent type or whose
694  //       size is specified by a constant expression that is
695  //       value-dependent,
696  ArrayType(TypeClass tc, QualType et, QualType can,
697            ArraySizeModifier sm, unsigned tq)
698    : Type(tc, can, et->isDependentType() || tc == DependentSizedArray),
699      ElementType(et), SizeModifier(sm), IndexTypeQuals(tq) {}
700
701  friend class ASTContext;  // ASTContext creates these.
702public:
703  QualType getElementType() const { return ElementType; }
704  ArraySizeModifier getSizeModifier() const {
705    return ArraySizeModifier(SizeModifier);
706  }
707  unsigned getIndexTypeQualifier() const { return IndexTypeQuals; }
708
709  static bool classof(const Type *T) {
710    return T->getTypeClass() == ConstantArray ||
711           T->getTypeClass() == VariableArray ||
712           T->getTypeClass() == IncompleteArray ||
713           T->getTypeClass() == DependentSizedArray;
714  }
715  static bool classof(const ArrayType *) { return true; }
716};
717
718/// ConstantArrayType - This class represents C arrays with a specified constant
719/// size.  For example 'int A[100]' has ConstantArrayType where the element type
720/// is 'int' and the size is 100.
721class ConstantArrayType : public ArrayType {
722  llvm::APInt Size; // Allows us to unique the type.
723
724  ConstantArrayType(QualType et, QualType can, llvm::APInt sz,
725                    ArraySizeModifier sm, unsigned tq)
726    : ArrayType(ConstantArray, et, can, sm, tq), Size(sz) {}
727  friend class ASTContext;  // ASTContext creates these.
728public:
729  const llvm::APInt &getSize() const { return Size; }
730  virtual void getAsStringInternal(std::string &InnerString) const;
731
732  void Profile(llvm::FoldingSetNodeID &ID) {
733    Profile(ID, getElementType(), getSize());
734  }
735  static void Profile(llvm::FoldingSetNodeID &ID, QualType ET,
736                      llvm::APInt ArraySize) {
737    ID.AddPointer(ET.getAsOpaquePtr());
738    ID.AddInteger(ArraySize.getZExtValue());
739  }
740  static bool classof(const Type *T) {
741    return T->getTypeClass() == ConstantArray;
742  }
743  static bool classof(const ConstantArrayType *) { return true; }
744
745protected:
746  virtual void EmitImpl(llvm::Serializer& S) const;
747  static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D);
748  friend class Type;
749};
750
751/// IncompleteArrayType - This class represents C arrays with an unspecified
752/// size.  For example 'int A[]' has an IncompleteArrayType where the element
753/// type is 'int' and the size is unspecified.
754class IncompleteArrayType : public ArrayType {
755  IncompleteArrayType(QualType et, QualType can,
756                    ArraySizeModifier sm, unsigned tq)
757    : ArrayType(IncompleteArray, et, can, sm, tq) {}
758  friend class ASTContext;  // ASTContext creates these.
759public:
760
761  virtual void getAsStringInternal(std::string &InnerString) const;
762
763  static bool classof(const Type *T) {
764    return T->getTypeClass() == IncompleteArray;
765  }
766  static bool classof(const IncompleteArrayType *) { return true; }
767
768  friend class StmtIteratorBase;
769
770  void Profile(llvm::FoldingSetNodeID &ID) {
771    Profile(ID, getElementType());
772  }
773
774  static void Profile(llvm::FoldingSetNodeID &ID, QualType ET) {
775    ID.AddPointer(ET.getAsOpaquePtr());
776  }
777
778protected:
779  virtual void EmitImpl(llvm::Serializer& S) const;
780  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
781  friend class Type;
782};
783
784/// VariableArrayType - This class represents C arrays with a specified size
785/// which is not an integer-constant-expression.  For example, 'int s[x+foo()]'.
786/// Since the size expression is an arbitrary expression, we store it as such.
787///
788/// Note: VariableArrayType's aren't uniqued (since the expressions aren't) and
789/// should not be: two lexically equivalent variable array types could mean
790/// different things, for example, these variables do not have the same type
791/// dynamically:
792///
793/// void foo(int x) {
794///   int Y[x];
795///   ++x;
796///   int Z[x];
797/// }
798///
799class VariableArrayType : public ArrayType {
800  /// SizeExpr - An assignment expression. VLA's are only permitted within
801  /// a function block.
802  Stmt *SizeExpr;
803
804  VariableArrayType(QualType et, QualType can, Expr *e,
805                    ArraySizeModifier sm, unsigned tq)
806    : ArrayType(VariableArray, et, can, sm, tq), SizeExpr((Stmt*) e) {}
807  friend class ASTContext;  // ASTContext creates these.
808  virtual void Destroy(ASTContext& C);
809
810public:
811  Expr *getSizeExpr() const {
812    // We use C-style casts instead of cast<> here because we do not wish
813    // to have a dependency of Type.h on Stmt.h/Expr.h.
814    return (Expr*) SizeExpr;
815  }
816
817  virtual void getAsStringInternal(std::string &InnerString) const;
818
819  static bool classof(const Type *T) {
820    return T->getTypeClass() == VariableArray;
821  }
822  static bool classof(const VariableArrayType *) { return true; }
823
824  friend class StmtIteratorBase;
825
826  void Profile(llvm::FoldingSetNodeID &ID) {
827    assert (0 && "Cannnot unique VariableArrayTypes.");
828  }
829
830protected:
831  virtual void EmitImpl(llvm::Serializer& S) const;
832  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
833  friend class Type;
834};
835
836/// DependentSizedArrayType - This type represents an array type in
837/// C++ whose size is a value-dependent expression. For example:
838/// @code
839/// template<typename T, int Size>
840/// class array {
841///   T data[Size];
842/// };
843/// @endcode
844/// For these types, we won't actually know what the array bound is
845/// until template instantiation occurs, at which point this will
846/// become either a ConstantArrayType or a VariableArrayType.
847class DependentSizedArrayType : public ArrayType {
848  /// SizeExpr - An assignment expression that will instantiate to the
849  /// size of the array.
850  Stmt *SizeExpr;
851
852  DependentSizedArrayType(QualType et, QualType can, Expr *e,
853			  ArraySizeModifier sm, unsigned tq)
854    : ArrayType(DependentSizedArray, et, can, sm, tq), SizeExpr((Stmt*) e) {}
855  friend class ASTContext;  // ASTContext creates these.
856  virtual void Destroy(ASTContext& C);
857
858public:
859  Expr *getSizeExpr() const {
860    // We use C-style casts instead of cast<> here because we do not wish
861    // to have a dependency of Type.h on Stmt.h/Expr.h.
862    return (Expr*) SizeExpr;
863  }
864
865  virtual void getAsStringInternal(std::string &InnerString) const;
866
867  static bool classof(const Type *T) {
868    return T->getTypeClass() == DependentSizedArray;
869  }
870  static bool classof(const DependentSizedArrayType *) { return true; }
871
872  friend class StmtIteratorBase;
873
874  void Profile(llvm::FoldingSetNodeID &ID) {
875    assert (0 && "Cannnot unique DependentSizedArrayTypes.");
876  }
877
878protected:
879  virtual void EmitImpl(llvm::Serializer& S) const;
880  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
881  friend class Type;
882};
883
884/// VectorType - GCC generic vector type. This type is created using
885/// __attribute__((vector_size(n)), where "n" specifies the vector size in
886/// bytes. Since the constructor takes the number of vector elements, the
887/// client is responsible for converting the size into the number of elements.
888class VectorType : public Type, public llvm::FoldingSetNode {
889protected:
890  /// ElementType - The element type of the vector.
891  QualType ElementType;
892
893  /// NumElements - The number of elements in the vector.
894  unsigned NumElements;
895
896  VectorType(QualType vecType, unsigned nElements, QualType canonType) :
897    Type(Vector, canonType, vecType->isDependentType()),
898    ElementType(vecType), NumElements(nElements) {}
899  VectorType(TypeClass tc, QualType vecType, unsigned nElements,
900	     QualType canonType)
901    : Type(tc, canonType, vecType->isDependentType()), ElementType(vecType),
902      NumElements(nElements) {}
903  friend class ASTContext;  // ASTContext creates these.
904public:
905
906  QualType getElementType() const { return ElementType; }
907  unsigned getNumElements() const { return NumElements; }
908
909  virtual void getAsStringInternal(std::string &InnerString) const;
910
911  void Profile(llvm::FoldingSetNodeID &ID) {
912    Profile(ID, getElementType(), getNumElements(), getTypeClass());
913  }
914  static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType,
915                      unsigned NumElements, TypeClass TypeClass) {
916    ID.AddPointer(ElementType.getAsOpaquePtr());
917    ID.AddInteger(NumElements);
918    ID.AddInteger(TypeClass);
919  }
920  static bool classof(const Type *T) {
921    return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector;
922  }
923  static bool classof(const VectorType *) { return true; }
924};
925
926/// ExtVectorType - Extended vector type. This type is created using
927/// __attribute__((ext_vector_type(n)), where "n" is the number of elements.
928/// Unlike vector_size, ext_vector_type is only allowed on typedef's. This
929/// class enables syntactic extensions, like Vector Components for accessing
930/// points, colors, and textures (modeled after OpenGL Shading Language).
931class ExtVectorType : public VectorType {
932  ExtVectorType(QualType vecType, unsigned nElements, QualType canonType) :
933    VectorType(ExtVector, vecType, nElements, canonType) {}
934  friend class ASTContext;  // ASTContext creates these.
935public:
936  static int getPointAccessorIdx(char c) {
937    switch (c) {
938    default: return -1;
939    case 'x': return 0;
940    case 'y': return 1;
941    case 'z': return 2;
942    case 'w': return 3;
943    }
944  }
945  static int getColorAccessorIdx(char c) {
946    switch (c) {
947    default: return -1;
948    case 'r': return 0;
949    case 'g': return 1;
950    case 'b': return 2;
951    case 'a': return 3;
952    }
953  }
954  static int getTextureAccessorIdx(char c) {
955    switch (c) {
956    default: return -1;
957    case 's': return 0;
958    case 't': return 1;
959    case 'p': return 2;
960    case 'q': return 3;
961    }
962  };
963
964  static int getAccessorIdx(char c) {
965    if (int idx = getPointAccessorIdx(c)+1) return idx-1;
966    if (int idx = getColorAccessorIdx(c)+1) return idx-1;
967    return getTextureAccessorIdx(c);
968  }
969
970  bool isAccessorWithinNumElements(char c) const {
971    if (int idx = getAccessorIdx(c)+1)
972      return unsigned(idx-1) < NumElements;
973    return false;
974  }
975  virtual void getAsStringInternal(std::string &InnerString) const;
976
977  static bool classof(const Type *T) {
978    return T->getTypeClass() == ExtVector;
979  }
980  static bool classof(const ExtVectorType *) { return true; }
981};
982
983/// FunctionType - C99 6.7.5.3 - Function Declarators.  This is the common base
984/// class of FunctionTypeNoProto and FunctionTypeProto.
985///
986class FunctionType : public Type {
987  /// SubClassData - This field is owned by the subclass, put here to pack
988  /// tightly with the ivars in Type.
989  bool SubClassData : 1;
990
991  /// TypeQuals - Used only by FunctionTypeProto, put here to pack with the
992  /// other bitfields.
993  /// The qualifiers are part of FunctionTypeProto because...
994  ///
995  /// C++ 8.3.5p4: The return type, the parameter type list and the
996  /// cv-qualifier-seq, [...], are part of the function type.
997  ///
998  unsigned TypeQuals : 3;
999
1000  // The type returned by the function.
1001  QualType ResultType;
1002protected:
1003  FunctionType(TypeClass tc, QualType res, bool SubclassInfo,
1004               unsigned typeQuals, QualType Canonical, bool Dependent)
1005    : Type(tc, Canonical, Dependent),
1006      SubClassData(SubclassInfo), TypeQuals(typeQuals), ResultType(res) {}
1007  bool getSubClassData() const { return SubClassData; }
1008  unsigned getTypeQuals() const { return TypeQuals; }
1009public:
1010
1011  QualType getResultType() const { return ResultType; }
1012
1013
1014  static bool classof(const Type *T) {
1015    return T->getTypeClass() == FunctionNoProto ||
1016           T->getTypeClass() == FunctionProto;
1017  }
1018  static bool classof(const FunctionType *) { return true; }
1019};
1020
1021/// FunctionTypeNoProto - Represents a K&R-style 'int foo()' function, which has
1022/// no information available about its arguments.
1023class FunctionTypeNoProto : public FunctionType, public llvm::FoldingSetNode {
1024  FunctionTypeNoProto(QualType Result, QualType Canonical)
1025    : FunctionType(FunctionNoProto, Result, false, 0, Canonical,
1026		   /*Dependent=*/false) {}
1027  friend class ASTContext;  // ASTContext creates these.
1028public:
1029  // No additional state past what FunctionType provides.
1030
1031  virtual void getAsStringInternal(std::string &InnerString) const;
1032
1033  void Profile(llvm::FoldingSetNodeID &ID) {
1034    Profile(ID, getResultType());
1035  }
1036  static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType) {
1037    ID.AddPointer(ResultType.getAsOpaquePtr());
1038  }
1039
1040  static bool classof(const Type *T) {
1041    return T->getTypeClass() == FunctionNoProto;
1042  }
1043  static bool classof(const FunctionTypeNoProto *) { return true; }
1044
1045protected:
1046  virtual void EmitImpl(llvm::Serializer& S) const;
1047  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
1048  friend class Type;
1049};
1050
1051/// FunctionTypeProto - Represents a prototype with argument type info, e.g.
1052/// 'int foo(int)' or 'int foo(void)'.  'void' is represented as having no
1053/// arguments, not as having a single void argument.
1054class FunctionTypeProto : public FunctionType, public llvm::FoldingSetNode {
1055  /// hasAnyDependentType - Determine whether there are any dependent
1056  /// types within the arguments passed in.
1057  static bool hasAnyDependentType(const QualType *ArgArray, unsigned numArgs) {
1058    for (unsigned Idx = 0; Idx < numArgs; ++Idx)
1059      if (ArgArray[Idx]->isDependentType())
1060	return true;
1061
1062    return false;
1063  }
1064
1065  FunctionTypeProto(QualType Result, const QualType *ArgArray, unsigned numArgs,
1066                    bool isVariadic, unsigned typeQuals, QualType Canonical)
1067    : FunctionType(FunctionProto, Result, isVariadic, typeQuals, Canonical,
1068		   (Result->isDependentType() ||
1069		    hasAnyDependentType(ArgArray, numArgs))),
1070      NumArgs(numArgs) {
1071    // Fill in the trailing argument array.
1072    QualType *ArgInfo = reinterpret_cast<QualType *>(this+1);;
1073    for (unsigned i = 0; i != numArgs; ++i)
1074      ArgInfo[i] = ArgArray[i];
1075  }
1076
1077  /// NumArgs - The number of arguments this function has, not counting '...'.
1078  unsigned NumArgs;
1079
1080  /// ArgInfo - There is an variable size array after the class in memory that
1081  /// holds the argument types.
1082
1083  friend class ASTContext;  // ASTContext creates these.
1084  virtual void Destroy(ASTContext& C);
1085
1086public:
1087  unsigned getNumArgs() const { return NumArgs; }
1088  QualType getArgType(unsigned i) const {
1089    assert(i < NumArgs && "Invalid argument number!");
1090    return arg_type_begin()[i];
1091  }
1092
1093  bool isVariadic() const { return getSubClassData(); }
1094  unsigned getTypeQuals() const { return FunctionType::getTypeQuals(); }
1095
1096  typedef const QualType *arg_type_iterator;
1097  arg_type_iterator arg_type_begin() const {
1098    return reinterpret_cast<const QualType *>(this+1);
1099  }
1100  arg_type_iterator arg_type_end() const { return arg_type_begin()+NumArgs; }
1101
1102  virtual void getAsStringInternal(std::string &InnerString) const;
1103
1104  static bool classof(const Type *T) {
1105    return T->getTypeClass() == FunctionProto;
1106  }
1107  static bool classof(const FunctionTypeProto *) { return true; }
1108
1109  void Profile(llvm::FoldingSetNodeID &ID);
1110  static void Profile(llvm::FoldingSetNodeID &ID, QualType Result,
1111                      arg_type_iterator ArgTys, unsigned NumArgs,
1112                      bool isVariadic, unsigned TypeQuals);
1113
1114protected:
1115  virtual void EmitImpl(llvm::Serializer& S) const;
1116  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
1117  friend class Type;
1118};
1119
1120
1121class TypedefType : public Type {
1122  TypedefDecl *Decl;
1123protected:
1124  TypedefType(TypeClass tc, TypedefDecl *D, QualType can)
1125    : Type(tc, can, can->isDependentType()), Decl(D) {
1126    assert(!isa<TypedefType>(can) && "Invalid canonical type");
1127  }
1128  friend class ASTContext;  // ASTContext creates these.
1129public:
1130
1131  TypedefDecl *getDecl() const { return Decl; }
1132
1133  /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
1134  /// potentially looking through *all* consequtive typedefs.  This returns the
1135  /// sum of the type qualifiers, so if you have:
1136  ///   typedef const int A;
1137  ///   typedef volatile A B;
1138  /// looking through the typedefs for B will give you "const volatile A".
1139  QualType LookThroughTypedefs() const;
1140
1141  virtual void getAsStringInternal(std::string &InnerString) const;
1142
1143  static bool classof(const Type *T) { return T->getTypeClass() == TypeName; }
1144  static bool classof(const TypedefType *) { return true; }
1145
1146protected:
1147  virtual void EmitImpl(llvm::Serializer& S) const;
1148  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
1149  friend class Type;
1150};
1151
1152/// TypeOfExpr (GCC extension).
1153class TypeOfExpr : public Type {
1154  Expr *TOExpr;
1155  TypeOfExpr(Expr *E, QualType can);
1156  friend class ASTContext;  // ASTContext creates these.
1157public:
1158  Expr *getUnderlyingExpr() const { return TOExpr; }
1159
1160  virtual void getAsStringInternal(std::string &InnerString) const;
1161
1162  static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExp; }
1163  static bool classof(const TypeOfExpr *) { return true; }
1164
1165protected:
1166  virtual void EmitImpl(llvm::Serializer& S) const;
1167  static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D);
1168  friend class Type;
1169};
1170
1171/// TypeOfType (GCC extension).
1172class TypeOfType : public Type {
1173  QualType TOType;
1174  TypeOfType(QualType T, QualType can)
1175    : Type(TypeOfTyp, can, T->isDependentType()), TOType(T) {
1176    assert(!isa<TypedefType>(can) && "Invalid canonical type");
1177  }
1178  friend class ASTContext;  // ASTContext creates these.
1179public:
1180  QualType getUnderlyingType() const { return TOType; }
1181
1182  virtual void getAsStringInternal(std::string &InnerString) const;
1183
1184  static bool classof(const Type *T) { return T->getTypeClass() == TypeOfTyp; }
1185  static bool classof(const TypeOfType *) { return true; }
1186};
1187
1188class TagType : public Type {
1189  TagDecl *decl;
1190  friend class ASTContext;
1191
1192protected:
1193  // FIXME: We'll need the user to pass in information about whether
1194  // this type is dependent or not, because we don't have enough
1195  // information to compute it here.
1196  TagType(TagDecl *D, QualType can)
1197    : Type(Tagged, can, /*Dependent=*/false), decl(D) {}
1198
1199public:
1200  TagDecl *getDecl() const { return decl; }
1201
1202  virtual void getAsStringInternal(std::string &InnerString) const;
1203
1204  static bool classof(const Type *T) { return T->getTypeClass() == Tagged; }
1205  static bool classof(const TagType *) { return true; }
1206
1207protected:
1208  virtual void EmitImpl(llvm::Serializer& S) const;
1209  static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D);
1210  friend class Type;
1211};
1212
1213/// RecordType - This is a helper class that allows the use of isa/cast/dyncast
1214/// to detect TagType objects of structs/unions/classes.
1215class RecordType : public TagType {
1216protected:
1217  explicit RecordType(RecordDecl *D)
1218    : TagType(reinterpret_cast<TagDecl*>(D), QualType()) { }
1219  friend class ASTContext;   // ASTContext creates these.
1220public:
1221
1222  RecordDecl *getDecl() const {
1223    return reinterpret_cast<RecordDecl*>(TagType::getDecl());
1224  }
1225
1226  // FIXME: This predicate is a helper to QualType/Type. It needs to
1227  // recursively check all fields for const-ness. If any field is declared
1228  // const, it needs to return false.
1229  bool hasConstFields() const { return false; }
1230
1231  // FIXME: RecordType needs to check when it is created that all fields are in
1232  // the same address space, and return that.
1233  unsigned getAddressSpace() const { return 0; }
1234
1235  static bool classof(const TagType *T);
1236  static bool classof(const Type *T) {
1237    return isa<TagType>(T) && classof(cast<TagType>(T));
1238  }
1239  static bool classof(const RecordType *) { return true; }
1240};
1241
1242/// CXXRecordType - This is a helper class that allows the use of
1243/// isa/cast/dyncast to detect TagType objects of C++ structs/unions/classes.
1244class CXXRecordType : public RecordType {
1245  explicit CXXRecordType(CXXRecordDecl *D)
1246    : RecordType(reinterpret_cast<RecordDecl*>(D)) { }
1247  friend class ASTContext;   // ASTContext creates these.
1248public:
1249
1250  CXXRecordDecl *getDecl() const {
1251    return reinterpret_cast<CXXRecordDecl*>(RecordType::getDecl());
1252  }
1253
1254  static bool classof(const TagType *T);
1255  static bool classof(const Type *T) {
1256    return isa<TagType>(T) && classof(cast<TagType>(T));
1257  }
1258  static bool classof(const CXXRecordType *) { return true; }
1259};
1260
1261/// EnumType - This is a helper class that allows the use of isa/cast/dyncast
1262/// to detect TagType objects of enums.
1263class EnumType : public TagType {
1264  explicit EnumType(EnumDecl *D)
1265    : TagType(reinterpret_cast<TagDecl*>(D), QualType()) { }
1266  friend class ASTContext;   // ASTContext creates these.
1267public:
1268
1269  EnumDecl *getDecl() const {
1270    return reinterpret_cast<EnumDecl*>(TagType::getDecl());
1271  }
1272
1273  static bool classof(const TagType *T);
1274  static bool classof(const Type *T) {
1275    return isa<TagType>(T) && classof(cast<TagType>(T));
1276  }
1277  static bool classof(const EnumType *) { return true; }
1278};
1279
1280class TemplateTypeParmType : public Type {
1281  TemplateTypeParmDecl *Decl;
1282
1283protected:
1284  TemplateTypeParmType(TemplateTypeParmDecl *D)
1285    : Type(TemplateTypeParm, QualType(this, 0), /*Dependent=*/true), Decl(D) { }
1286
1287  friend class ASTContext; // ASTContext creates these
1288
1289public:
1290  TemplateTypeParmDecl *getDecl() const { return Decl; }
1291
1292  virtual void getAsStringInternal(std::string &InnerString) const;
1293
1294  static bool classof(const Type *T) {
1295    return T->getTypeClass() == TemplateTypeParm;
1296  }
1297  static bool classof(const TemplateTypeParmType *T) { return true; }
1298
1299protected:
1300  virtual void EmitImpl(llvm::Serializer& S) const;
1301  static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D);
1302  friend class Type;
1303};
1304
1305/// ObjCInterfaceType - Interfaces are the core concept in Objective-C for
1306/// object oriented design.  They basically correspond to C++ classes.  There
1307/// are two kinds of interface types, normal interfaces like "NSString" and
1308/// qualified interfaces, which are qualified with a protocol list like
1309/// "NSString<NSCopyable, NSAmazing>".  Qualified interface types are instances
1310/// of ObjCQualifiedInterfaceType, which is a subclass of ObjCInterfaceType.
1311class ObjCInterfaceType : public Type {
1312  ObjCInterfaceDecl *Decl;
1313protected:
1314  ObjCInterfaceType(TypeClass tc, ObjCInterfaceDecl *D) :
1315    Type(tc, QualType(), /*Dependent=*/false), Decl(D) { }
1316  friend class ASTContext;  // ASTContext creates these.
1317public:
1318
1319  ObjCInterfaceDecl *getDecl() const { return Decl; }
1320
1321  /// qual_iterator and friends: this provides access to the (potentially empty)
1322  /// list of protocols qualifying this interface.  If this is an instance of
1323  /// ObjCQualifiedInterfaceType it returns the list, otherwise it returns an
1324  /// empty list if there are no qualifying protocols.
1325  typedef llvm::SmallVector<ObjCProtocolDecl*, 8>::const_iterator qual_iterator;
1326  inline qual_iterator qual_begin() const;
1327  inline qual_iterator qual_end() const;
1328  bool qual_empty() const { return getTypeClass() != ObjCQualifiedInterface; }
1329
1330  /// getNumProtocols - Return the number of qualifying protocols in this
1331  /// interface type, or 0 if there are none.
1332  inline unsigned getNumProtocols() const;
1333
1334  /// getProtocol - Return the specified qualifying protocol.
1335  inline ObjCProtocolDecl *getProtocol(unsigned i) const;
1336
1337
1338  virtual void getAsStringInternal(std::string &InnerString) const;
1339  static bool classof(const Type *T) {
1340    return T->getTypeClass() == ObjCInterface ||
1341           T->getTypeClass() == ObjCQualifiedInterface;
1342  }
1343  static bool classof(const ObjCInterfaceType *) { return true; }
1344};
1345
1346/// ObjCQualifiedInterfaceType - This class represents interface types
1347/// conforming to a list of protocols, such as INTF<Proto1, Proto2, Proto1>.
1348///
1349/// Duplicate protocols are removed and protocol list is canonicalized to be in
1350/// alphabetical order.
1351class ObjCQualifiedInterfaceType : public ObjCInterfaceType,
1352                                   public llvm::FoldingSetNode {
1353
1354  // List of protocols for this protocol conforming object type
1355  // List is sorted on protocol name. No protocol is enterred more than once.
1356  llvm::SmallVector<ObjCProtocolDecl*, 4> Protocols;
1357
1358  ObjCQualifiedInterfaceType(ObjCInterfaceDecl *D,
1359                             ObjCProtocolDecl **Protos, unsigned NumP) :
1360    ObjCInterfaceType(ObjCQualifiedInterface, D),
1361    Protocols(Protos, Protos+NumP) { }
1362  friend class ASTContext;  // ASTContext creates these.
1363public:
1364
1365  ObjCProtocolDecl *getProtocol(unsigned i) const {
1366    return Protocols[i];
1367  }
1368  unsigned getNumProtocols() const {
1369    return Protocols.size();
1370  }
1371
1372  qual_iterator qual_begin() const { return Protocols.begin(); }
1373  qual_iterator qual_end() const   { return Protocols.end(); }
1374
1375  virtual void getAsStringInternal(std::string &InnerString) const;
1376
1377  void Profile(llvm::FoldingSetNodeID &ID);
1378  static void Profile(llvm::FoldingSetNodeID &ID,
1379                      const ObjCInterfaceDecl *Decl,
1380                      ObjCProtocolDecl **protocols, unsigned NumProtocols);
1381
1382  static bool classof(const Type *T) {
1383    return T->getTypeClass() == ObjCQualifiedInterface;
1384  }
1385  static bool classof(const ObjCQualifiedInterfaceType *) { return true; }
1386};
1387
1388inline ObjCInterfaceType::qual_iterator ObjCInterfaceType::qual_begin() const {
1389  if (const ObjCQualifiedInterfaceType *QIT =
1390         dyn_cast<ObjCQualifiedInterfaceType>(this))
1391    return QIT->qual_begin();
1392  return 0;
1393}
1394inline ObjCInterfaceType::qual_iterator ObjCInterfaceType::qual_end() const {
1395  if (const ObjCQualifiedInterfaceType *QIT =
1396         dyn_cast<ObjCQualifiedInterfaceType>(this))
1397    return QIT->qual_end();
1398  return 0;
1399}
1400
1401/// getNumProtocols - Return the number of qualifying protocols in this
1402/// interface type, or 0 if there are none.
1403inline unsigned ObjCInterfaceType::getNumProtocols() const {
1404  if (const ObjCQualifiedInterfaceType *QIT =
1405        dyn_cast<ObjCQualifiedInterfaceType>(this))
1406    return QIT->getNumProtocols();
1407  return 0;
1408}
1409
1410/// getProtocol - Return the specified qualifying protocol.
1411inline ObjCProtocolDecl *ObjCInterfaceType::getProtocol(unsigned i) const {
1412  return cast<ObjCQualifiedInterfaceType>(this)->getProtocol(i);
1413}
1414
1415
1416
1417/// ObjCQualifiedIdType - to represent id<protocol-list>.
1418///
1419/// Duplicate protocols are removed and protocol list is canonicalized to be in
1420/// alphabetical order.
1421class ObjCQualifiedIdType : public Type,
1422                            public llvm::FoldingSetNode {
1423  // List of protocols for this protocol conforming 'id' type
1424  // List is sorted on protocol name. No protocol is enterred more than once.
1425  llvm::SmallVector<ObjCProtocolDecl*, 8> Protocols;
1426
1427  ObjCQualifiedIdType(ObjCProtocolDecl **Protos, unsigned NumP)
1428    : Type(ObjCQualifiedId, QualType()/*these are always canonical*/,
1429	   /*Dependent=*/false),
1430  Protocols(Protos, Protos+NumP) { }
1431  friend class ASTContext;  // ASTContext creates these.
1432public:
1433
1434  ObjCProtocolDecl *getProtocols(unsigned i) const {
1435    return Protocols[i];
1436  }
1437  unsigned getNumProtocols() const {
1438    return Protocols.size();
1439  }
1440  ObjCProtocolDecl **getReferencedProtocols() {
1441    return &Protocols[0];
1442  }
1443
1444  typedef llvm::SmallVector<ObjCProtocolDecl*, 8>::const_iterator qual_iterator;
1445  qual_iterator qual_begin() const { return Protocols.begin(); }
1446  qual_iterator qual_end() const   { return Protocols.end(); }
1447
1448  virtual void getAsStringInternal(std::string &InnerString) const;
1449
1450  void Profile(llvm::FoldingSetNodeID &ID);
1451  static void Profile(llvm::FoldingSetNodeID &ID,
1452                      ObjCProtocolDecl **protocols, unsigned NumProtocols);
1453
1454  static bool classof(const Type *T) {
1455    return T->getTypeClass() == ObjCQualifiedId;
1456  }
1457  static bool classof(const ObjCQualifiedIdType *) { return true; }
1458
1459};
1460
1461
1462// Inline function definitions.
1463
1464/// getUnqualifiedType - Return the type without any qualifiers.
1465inline QualType QualType::getUnqualifiedType() const {
1466  Type *TP = getTypePtr();
1467  if (const ASQualType *ASQT = dyn_cast<ASQualType>(TP))
1468    TP = ASQT->getBaseType();
1469  return QualType(TP, 0);
1470}
1471
1472/// getAddressSpace - Return the address space of this type.
1473inline unsigned QualType::getAddressSpace() const {
1474  QualType CT = getTypePtr()->getCanonicalTypeInternal();
1475  if (const ArrayType *AT = dyn_cast<ArrayType>(CT))
1476    return AT->getElementType().getAddressSpace();
1477  if (const RecordType *RT = dyn_cast<RecordType>(CT))
1478    return RT->getAddressSpace();
1479  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CT))
1480    return ASQT->getAddressSpace();
1481  return 0;
1482}
1483
1484/// isMoreQualifiedThan - Determine whether this type is more
1485/// qualified than the Other type. For example, "const volatile int"
1486/// is more qualified than "const int", "volatile int", and
1487/// "int". However, it is not more qualified than "const volatile
1488/// int".
1489inline bool QualType::isMoreQualifiedThan(QualType Other) const {
1490  // FIXME: Handle address spaces
1491  unsigned MyQuals = this->getCVRQualifiers();
1492  unsigned OtherQuals = Other.getCVRQualifiers();
1493  assert(this->getAddressSpace() == 0 && "Address space not checked");
1494  assert(Other.getAddressSpace() == 0 && "Address space not checked");
1495  return MyQuals != OtherQuals && (MyQuals | OtherQuals) == MyQuals;
1496}
1497
1498/// isAtLeastAsQualifiedAs - Determine whether this type is at last
1499/// as qualified as the Other type. For example, "const volatile
1500/// int" is at least as qualified as "const int", "volatile int",
1501/// "int", and "const volatile int".
1502inline bool QualType::isAtLeastAsQualifiedAs(QualType Other) const {
1503  // FIXME: Handle address spaces
1504  unsigned MyQuals = this->getCVRQualifiers();
1505  unsigned OtherQuals = Other.getCVRQualifiers();
1506  assert(this->getAddressSpace() == 0 && "Address space not checked");
1507  assert(Other.getAddressSpace() == 0 && "Address space not checked");
1508  return (MyQuals | OtherQuals) == MyQuals;
1509}
1510
1511/// getNonReferenceType - If Type is a reference type (e.g., const
1512/// int&), returns the type that the reference refers to ("const
1513/// int"). Otherwise, returns the type itself. This routine is used
1514/// throughout Sema to implement C++ 5p6:
1515///
1516///   If an expression initially has the type "reference to T" (8.3.2,
1517///   8.5.3), the type is adjusted to "T" prior to any further
1518///   analysis, the expression designates the object or function
1519///   denoted by the reference, and the expression is an lvalue.
1520inline QualType QualType::getNonReferenceType() const {
1521  if (const ReferenceType *RefType = (*this)->getAsReferenceType())
1522    return RefType->getPointeeType();
1523  else
1524    return *this;
1525}
1526
1527inline const TypedefType* Type::getAsTypedefType() const {
1528  return dyn_cast<TypedefType>(this);
1529}
1530inline const ObjCInterfaceType *Type::getAsPointerToObjCInterfaceType() const {
1531  if (const PointerType *PT = getAsPointerType())
1532    return PT->getPointeeType()->getAsObjCInterfaceType();
1533  return 0;
1534}
1535
1536// NOTE: All of these methods use "getUnqualifiedType" to strip off address
1537// space qualifiers if present.
1538inline bool Type::isFunctionType() const {
1539  return isa<FunctionType>(CanonicalType.getUnqualifiedType());
1540}
1541inline bool Type::isPointerType() const {
1542  return isa<PointerType>(CanonicalType.getUnqualifiedType());
1543}
1544inline bool Type::isBlockPointerType() const {
1545    return isa<BlockPointerType>(CanonicalType);
1546}
1547inline bool Type::isReferenceType() const {
1548  return isa<ReferenceType>(CanonicalType.getUnqualifiedType());
1549}
1550inline bool Type::isPointerLikeType() const {
1551  return isa<PointerLikeType>(CanonicalType.getUnqualifiedType());
1552}
1553inline bool Type::isFunctionPointerType() const {
1554  if (const PointerType* T = getAsPointerType())
1555    return T->getPointeeType()->isFunctionType();
1556  else
1557    return false;
1558}
1559inline bool Type::isArrayType() const {
1560  return isa<ArrayType>(CanonicalType.getUnqualifiedType());
1561}
1562inline bool Type::isConstantArrayType() const {
1563  return isa<ConstantArrayType>(CanonicalType.getUnqualifiedType());
1564}
1565inline bool Type::isIncompleteArrayType() const {
1566  return isa<IncompleteArrayType>(CanonicalType.getUnqualifiedType());
1567}
1568inline bool Type::isVariableArrayType() const {
1569  return isa<VariableArrayType>(CanonicalType.getUnqualifiedType());
1570}
1571inline bool Type::isDependentSizedArrayType() const {
1572  return isa<DependentSizedArrayType>(CanonicalType.getUnqualifiedType());
1573}
1574inline bool Type::isRecordType() const {
1575  return isa<RecordType>(CanonicalType.getUnqualifiedType());
1576}
1577inline bool Type::isAnyComplexType() const {
1578  return isa<ComplexType>(CanonicalType.getUnqualifiedType());
1579}
1580inline bool Type::isVectorType() const {
1581  return isa<VectorType>(CanonicalType.getUnqualifiedType());
1582}
1583inline bool Type::isExtVectorType() const {
1584  return isa<ExtVectorType>(CanonicalType.getUnqualifiedType());
1585}
1586inline bool Type::isObjCInterfaceType() const {
1587  return isa<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
1588}
1589inline bool Type::isObjCQualifiedInterfaceType() const {
1590  return isa<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType());
1591}
1592inline bool Type::isObjCQualifiedIdType() const {
1593  return isa<ObjCQualifiedIdType>(CanonicalType.getUnqualifiedType());
1594}
1595inline bool Type::isTemplateTypeParmType() const {
1596  return isa<TemplateTypeParmType>(CanonicalType.getUnqualifiedType());
1597}
1598
1599inline bool Type::isOverloadType() const {
1600  if (const BuiltinType *BT = getAsBuiltinType())
1601    return BT->getKind() == BuiltinType::Overload;
1602  else
1603    return false;
1604}
1605
1606/// Insertion operator for diagnostics.  This allows sending QualType's into a
1607/// diagnostic with <<.
1608inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
1609                                           QualType T) {
1610  DB.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()),
1611                  Diagnostic::ak_qualtype);
1612  return DB;
1613}
1614
1615}  // end namespace clang
1616
1617#endif
1618