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