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