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