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