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