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