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