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