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