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