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