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