]> granicus.if.org Git - clang/blob - include/clang/AST/Type.h
Alternate address spaces work:
[clang] / include / clang / AST / Type.h
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 "llvm/Support/Casting.h"
18 #include "llvm/ADT/FoldingSet.h"
19 #include "llvm/ADT/APSInt.h"
20 #include "llvm/Bitcode/SerializationFwd.h"
21
22 using llvm::isa;
23 using llvm::cast;
24 using llvm::cast_or_null;
25 using llvm::dyn_cast;
26 using llvm::dyn_cast_or_null;
27
28 namespace clang {
29   class ASTContext;
30   class Type;
31   class TypedefDecl;
32   class TagDecl;
33   class RecordDecl;
34   class EnumDecl;
35   class FieldDecl;
36   class ObjCInterfaceDecl;
37   class ObjCProtocolDecl;
38   class ObjCMethodDecl;
39   class Expr;
40   class SourceLocation;
41   class PointerType;
42   class ReferenceType;
43   class VectorType;
44   class ArrayType;
45   class ConstantArrayType;
46   class VariableArrayType;
47   class IncompleteArrayType;
48   class RecordType;
49   class ComplexType;
50   class TagType;
51   class FunctionType;
52   class OCUVectorType;
53   class BuiltinType;
54   class ObjCQualifiedInterfaceType;
55   class StmtIteratorBase;
56   
57 /// QualType - For efficiency, we don't store CVR-qualified types as nodes on
58 /// their own: instead each reference to a type stores the qualifiers.  This
59 /// greatly reduces the number of nodes we need to allocate for types (for
60 /// example we only need one for 'int', 'const int', 'volatile int',
61 /// 'const volatile int', etc).
62 ///
63 /// As an added efficiency bonus, instead of making this a pair, we just store
64 /// the three bits we care about in the low bits of the pointer.  To handle the
65 /// packing/unpacking, we make QualType be a simple wrapper class that acts like
66 /// a smart pointer.
67 class QualType {
68   uintptr_t ThePtr;
69 public:
70   enum TQ {   // NOTE: These flags must be kept in sync with DeclSpec::TQ.
71     Const    = 0x1,
72     Restrict = 0x2,
73     Volatile = 0x4,
74     CVRFlags = Const|Restrict|Volatile
75   };
76   
77   QualType() : ThePtr(0) {}
78   
79   QualType(Type *Ptr, unsigned Quals) {
80     assert((Quals & ~CVRFlags) == 0 && "Invalid type qualifiers!");
81     ThePtr = reinterpret_cast<uintptr_t>(Ptr);
82     assert((ThePtr & CVRFlags) == 0 && "Type pointer not 8-byte aligned?");
83     ThePtr |= Quals;
84   }
85
86   static QualType getFromOpaquePtr(void *Ptr) {
87     QualType T;
88     T.ThePtr = reinterpret_cast<uintptr_t>(Ptr);
89     return T;
90   }
91   
92   unsigned getCVRQualifiers() const {
93     return ThePtr & CVRFlags;
94   }
95   Type *getTypePtr() const {
96     return reinterpret_cast<Type*>(ThePtr & ~CVRFlags);
97   }
98   
99   void *getAsOpaquePtr() const {
100     return reinterpret_cast<void*>(ThePtr);
101   }
102   
103   Type &operator*() const {
104     return *getTypePtr();
105   }
106
107   Type *operator->() const {
108     return getTypePtr();
109   }
110   
111   /// isNull - Return true if this QualType doesn't point to a type yet.
112   bool isNull() const {
113     return ThePtr == 0;
114   }
115
116   bool isConstQualified() const {
117     return (ThePtr & Const) ? true : false;
118   }
119   bool isVolatileQualified() const {
120     return (ThePtr & Volatile) ? true : false;
121   }
122   bool isRestrictQualified() const {
123     return (ThePtr & Restrict) ? true : false;
124   }
125   
126   /// addConst/addVolatile/addRestrict - add the specified type qual to this
127   /// QualType.
128   void addConst()    { ThePtr |= Const; }
129   void addVolatile() { ThePtr |= Volatile; }
130   void addRestrict() { ThePtr |= Restrict; }
131
132   QualType getQualifiedType(unsigned TQs) const {
133     return QualType(getTypePtr(), TQs);
134   }
135   
136   inline QualType getUnqualifiedType() const;
137   
138   /// operator==/!= - Indicate whether the specified types and qualifiers are
139   /// identical.
140   bool operator==(const QualType &RHS) const {
141     return ThePtr == RHS.ThePtr;
142   }
143   bool operator!=(const QualType &RHS) const {
144     return ThePtr != RHS.ThePtr;
145   }
146   std::string getAsString() const {
147     std::string S;
148     getAsStringInternal(S);
149     return S;
150   }
151   void getAsStringInternal(std::string &Str) const;
152   
153   void dump(const char *s = 0) const;
154
155   /// getCanonicalType - Return the canonical version of this type, with the
156   /// appropriate type qualifiers on it.
157   inline QualType getCanonicalType() const;
158   
159   /// getAddressSpace - Return the address space of this type.
160   inline unsigned getAddressSpace() const;
161   
162   /// Emit - Serialize a QualType to Bitcode.
163   void Emit(llvm::Serializer& S) const;
164   
165   /// Read - Deserialize a QualType from Bitcode.
166   static QualType ReadVal(llvm::Deserializer& D);
167   
168 private:
169   void ReadBackpatch(llvm::Deserializer& D);
170   friend class FieldDecl;
171 };
172
173 } // end clang.
174
175 namespace llvm {
176 /// Implement simplify_type for QualType, so that we can dyn_cast from QualType
177 /// to a specific Type class.
178 template<> struct simplify_type<const ::clang::QualType> {
179   typedef ::clang::Type* SimpleType;
180   static SimpleType getSimplifiedValue(const ::clang::QualType &Val) {
181     return Val.getTypePtr();
182   }
183 };
184 template<> struct simplify_type< ::clang::QualType>
185   : public simplify_type<const ::clang::QualType> {};
186   
187 } // end namespace llvm
188
189 namespace clang {
190
191 /// Type - This is the base class of the type hierarchy.  A central concept
192 /// with types is that each type always has a canonical type.  A canonical type
193 /// is the type with any typedef names stripped out of it or the types it
194 /// references.  For example, consider:
195 ///
196 ///  typedef int  foo;
197 ///  typedef foo* bar;
198 ///    'int *'    'foo *'    'bar'
199 ///
200 /// There will be a Type object created for 'int'.  Since int is canonical, its
201 /// canonicaltype pointer points to itself.  There is also a Type for 'foo' (a
202 /// TypeNameType).  Its CanonicalType pointer points to the 'int' Type.  Next
203 /// there is a PointerType that represents 'int*', which, like 'int', is
204 /// canonical.  Finally, there is a PointerType type for 'foo*' whose canonical
205 /// type is 'int*', and there is a TypeNameType for 'bar', whose canonical type
206 /// is also 'int*'.
207 ///
208 /// Non-canonical types are useful for emitting diagnostics, without losing
209 /// information about typedefs being used.  Canonical types are useful for type
210 /// comparisons (they allow by-pointer equality tests) and useful for reasoning
211 /// about whether something has a particular form (e.g. is a function type),
212 /// because they implicitly, recursively, strip all typedefs out of a type.
213 ///
214 /// Types, once created, are immutable.
215 ///
216 class Type {
217 public:
218   enum TypeClass {
219     Builtin, Complex, Pointer, Reference, 
220     ConstantArray, VariableArray, IncompleteArray,
221     Vector, OCUVector,
222     FunctionNoProto, FunctionProto,
223     TypeName, Tagged, ASQual,
224     ObjCInterface, ObjCQualifiedInterface,
225     ObjCQualifiedId,
226     TypeOfExp, TypeOfTyp // GNU typeof extension.
227   };
228 private:
229   QualType CanonicalType;
230
231   /// TypeClass bitfield - Enum that specifies what subclass this belongs to.
232   /// Note that this should stay at the end of the ivars for Type so that
233   /// subclasses can pack their bitfields into the same word.
234   unsigned TC : 5;
235 protected:
236   // silence VC++ warning C4355: 'this' : used in base member initializer list
237   Type *this_() { return this; }
238   Type(TypeClass tc, QualType Canonical)
239     : CanonicalType(Canonical.isNull() ? QualType(this_(), 0) : Canonical),
240       TC(tc) {}
241   virtual ~Type();
242   friend class ASTContext;
243   
244   void EmitTypeInternal(llvm::Serializer& S) const;
245   void ReadTypeInternal(llvm::Deserializer& D);
246   
247 public:
248   TypeClass getTypeClass() const { return static_cast<TypeClass>(TC); }
249   
250   bool isCanonical() const { return CanonicalType.getTypePtr() == this; }
251
252   /// Types are partitioned into 3 broad categories (C99 6.2.5p1): 
253   /// object types, function types, and incomplete types.
254   
255   /// isObjectType - types that fully describe objects. An object is a region
256   /// of memory that can be examined and stored into (H&S).
257   bool isObjectType() const;
258
259   /// isIncompleteType - Return true if this is an incomplete type.
260   /// A type that can describe objects, but which lacks information needed to
261   /// determine its size (e.g. void, or a fwd declared struct). Clients of this
262   /// routine will need to determine if the size is actually required.  
263   bool isIncompleteType() const;
264   
265   /// isVariablyModifiedType (C99 6.7.5.2p2) - Return true for variable array
266   /// types that have a non-constant expression. This does not include "[]".
267   bool isVariablyModifiedType() const;
268   
269   /// isIncompleteArrayType (C99 6.2.5p22) - Return true for variable array
270   /// types that don't have any expression ("[]").
271   bool isIncompleteArrayType() const;
272   
273   /// Helper methods to distinguish type categories. All type predicates
274   /// operate on the canonical type, ignoring typedefs and qualifiers.
275   
276   /// isIntegerType() does *not* include complex integers (a GCC extension).
277   /// isComplexIntegerType() can be used to test for complex integers.
278   bool isIntegerType() const;     // C99 6.2.5p17 (int, char, bool, enum)
279   bool isEnumeralType() const;
280   bool isBooleanType() const;
281   bool isCharType() const;
282   bool isIntegralType() const;
283   
284   /// Floating point categories.
285   bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double)
286   /// isComplexType() does *not* include complex integers (a GCC extension).
287   /// isComplexIntegerType() can be used to test for complex integers.
288   bool isComplexType() const;      // C99 6.2.5p11 (complex)
289   bool isFloatingType() const;     // C99 6.2.5p11 (real floating + complex)
290   bool isRealType() const;         // C99 6.2.5p17 (real floating + integer)
291   bool isArithmeticType() const;   // C99 6.2.5p18 (integer + floating)
292   bool isVoidType() const;         // C99 6.2.5p19
293   bool isDerivedType() const;      // C99 6.2.5p20
294   bool isScalarType() const;       // C99 6.2.5p21 (arithmetic + pointers)
295   bool isAggregateType() const;    // C99 6.2.5p21 (arrays, structures)
296   
297   // Type Predicates: Check to see if this type is structurally the specified
298   // type, ignoring typedefs and qualifiers.
299   bool isFunctionType() const;
300   bool isPointerType() const;
301   bool isFunctionPointerType() const;
302   bool isReferenceType() const;
303   bool isArrayType() const;
304   bool isRecordType() const;
305   bool isStructureType() const;   
306   bool isUnionType() const;
307   bool isComplexIntegerType() const; // GCC complex int type.
308   bool isVectorType() const; // GCC vector type.
309   bool isOCUVectorType() const; // OCU vector type.
310   bool isObjCInterfaceType() const; // includes conforming protocol type
311   bool isObjCQualifiedIdType() const; // id includes conforming protocol type
312   
313   // Type Checking Functions: Check to see if this type is structurally the
314   // specified type, ignoring typedefs and qualifiers, and return a pointer to
315   // the best type we can.
316   const BuiltinType *getAsBuiltinType() const;   
317   const FunctionType *getAsFunctionType() const;   
318   const PointerType *getAsPointerType() const;
319   const ReferenceType *getAsReferenceType() const;
320   const ArrayType *getAsArrayType() const;
321   const ConstantArrayType *getAsConstantArrayType() const;
322   const VariableArrayType *getAsVariableArrayType() const;
323   const IncompleteArrayType *getAsIncompleteArrayType() const;
324   const RecordType *getAsRecordType() const;
325   const RecordType *getAsStructureType() const;   
326   const RecordType *getAsUnionType() const;
327   const VectorType *getAsVectorType() const; // GCC vector type.
328   const ComplexType *getAsComplexType() const;
329   const ComplexType *getAsComplexIntegerType() const; // GCC complex int type.
330   const OCUVectorType *getAsOCUVectorType() const; // OCU vector type.
331   
332   /// getDesugaredType - Return the specified type with any "sugar" removed from
333   /// type type.  This takes off typedefs, typeof's etc.  If the outer level of
334   /// the type is already concrete, it returns it unmodified.  This is similar
335   /// to getting the canonical type, but it doesn't remove *all* typedefs.  For
336   /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
337   /// concrete.
338   const Type *getDesugaredType() const;
339   
340   /// More type predicates useful for type checking/promotion
341   bool isPromotableIntegerType() const; // C99 6.3.1.1p2
342
343   /// isSignedIntegerType - Return true if this is an integer type that is
344   /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
345   /// an enum decl which has a signed representation, or a vector of signed
346   /// integer element type.
347   bool isSignedIntegerType() const;
348
349   /// isUnsignedIntegerType - Return true if this is an integer type that is
350   /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
351   /// decl which has an unsigned representation, or a vector of unsigned integer
352   /// element type.
353   bool isUnsignedIntegerType() const;
354
355   /// isConstantSizeType - Return true if this is not a variable sized type,
356   /// according to the rules of C99 6.7.5p3.  It is not legal to call this on
357   /// incomplete types.
358   bool isConstantSizeType() const;
359 private:  
360   QualType getCanonicalTypeInternal() const { return CanonicalType; }
361   friend class QualType;
362 public:
363   virtual void getAsStringInternal(std::string &InnerString) const = 0;
364   static bool classof(const Type *) { return true; }
365   
366 protected:  
367   /// Emit - Emit a Type to bitcode.  Used by ASTContext.
368   void Emit(llvm::Serializer& S) const;
369   
370   /// Create - Construct a Type from bitcode.  Used by ASTContext.
371   static void Create(ASTContext& Context, unsigned i, llvm::Deserializer& S);
372   
373   /// EmitImpl - Subclasses must implement this method in order to
374   ///  be serialized.
375   virtual void EmitImpl(llvm::Serializer& S) const;  
376 };
377
378 /// ASQualType - TR18037 (C embedded extensions) 6.2.5p26 
379 /// This supports address space qualified types.
380 ///
381 class ASQualType : public Type, public llvm::FoldingSetNode {
382   /// BaseType - This is the underlying type that this qualifies.  All CVR
383   /// qualifiers are stored on the QualType that references this type, so we
384   /// can't have any here.
385   Type *BaseType;
386   /// Address Space ID - The address space ID this type is qualified with.
387   unsigned AddressSpace;
388   ASQualType(Type *Base, QualType CanonicalPtr, unsigned AddrSpace) :
389     Type(ASQual, CanonicalPtr), BaseType(Base), AddressSpace(AddrSpace) {
390   }
391   friend class ASTContext;  // ASTContext creates these.
392 public:
393   Type *getBaseType() const { return BaseType; }
394   unsigned getAddressSpace() const { return AddressSpace; }
395   
396   virtual void getAsStringInternal(std::string &InnerString) const;
397   
398   void Profile(llvm::FoldingSetNodeID &ID) {
399     Profile(ID, getBaseType(), AddressSpace);
400   }
401   static void Profile(llvm::FoldingSetNodeID &ID, Type *Base, 
402                       unsigned AddrSpace) {
403     ID.AddPointer(Base);
404     ID.AddInteger(AddrSpace);
405   }
406   
407   static bool classof(const Type *T) { return T->getTypeClass() == ASQual; }
408   static bool classof(const ASQualType *) { return true; }
409   
410 protected:
411   virtual void EmitImpl(llvm::Serializer& S) const;
412   static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
413   friend class Type;
414 };
415
416
417 /// BuiltinType - This class is used for builtin types like 'int'.  Builtin
418 /// types are always canonical and have a literal name field.
419 class BuiltinType : public Type {
420 public:
421   enum Kind {
422     Void,
423     
424     Bool,     // This is bool and/or _Bool.
425     Char_U,   // This is 'char' for targets where char is unsigned.
426     UChar,    // This is explicitly qualified unsigned char.
427     UShort,
428     UInt,
429     ULong,
430     ULongLong,
431     
432     Char_S,   // This is 'char' for targets where char is signed.
433     SChar,    // This is explicitly qualified signed char.
434     Short,
435     Int,
436     Long,
437     LongLong,
438     
439     Float, Double, LongDouble
440   };
441 private:
442   Kind TypeKind;
443 public:
444   BuiltinType(Kind K) : Type(Builtin, QualType()), TypeKind(K) {}
445   
446   Kind getKind() const { return TypeKind; }
447   const char *getName() const;
448   
449   virtual void getAsStringInternal(std::string &InnerString) const;
450   
451   static bool classof(const Type *T) { return T->getTypeClass() == Builtin; }
452   static bool classof(const BuiltinType *) { return true; }
453 };
454
455 /// ComplexType - C99 6.2.5p11 - Complex values.  This supports the C99 complex
456 /// types (_Complex float etc) as well as the GCC integer complex extensions.
457 ///
458 class ComplexType : public Type, public llvm::FoldingSetNode {
459   QualType ElementType;
460   ComplexType(QualType Element, QualType CanonicalPtr) :
461     Type(Complex, CanonicalPtr), ElementType(Element) {
462   }
463   friend class ASTContext;  // ASTContext creates these.
464 public:
465   QualType getElementType() const { return ElementType; }
466   
467   virtual void getAsStringInternal(std::string &InnerString) const;
468     
469   void Profile(llvm::FoldingSetNodeID &ID) {
470     Profile(ID, getElementType());
471   }
472   static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) {
473     ID.AddPointer(Element.getAsOpaquePtr());
474   }
475   
476   static bool classof(const Type *T) { return T->getTypeClass() == Complex; }
477   static bool classof(const ComplexType *) { return true; }
478   
479 protected:  
480   virtual void EmitImpl(llvm::Serializer& S) const;
481   static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
482   friend class Type;
483 };
484
485
486 /// PointerType - C99 6.7.5.1 - Pointer Declarators.
487 ///
488 class PointerType : public Type, public llvm::FoldingSetNode {
489   QualType PointeeType;
490   PointerType(QualType Pointee, QualType CanonicalPtr) :
491     Type(Pointer, CanonicalPtr), PointeeType(Pointee) {
492   }
493   friend class ASTContext;  // ASTContext creates these.
494 public:
495     
496   QualType getPointeeType() const { return PointeeType; }
497   
498   virtual void getAsStringInternal(std::string &InnerString) const;
499   
500   
501   void Profile(llvm::FoldingSetNodeID &ID) {
502     Profile(ID, getPointeeType());
503   }
504   static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) {
505     ID.AddPointer(Pointee.getAsOpaquePtr());
506   }
507   
508   static bool classof(const Type *T) { return T->getTypeClass() == Pointer; }
509   static bool classof(const PointerType *) { return true; }
510
511 protected:  
512   virtual void EmitImpl(llvm::Serializer& S) const;
513   static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
514   friend class Type;
515 };
516
517 /// ReferenceType - C++ 8.3.2 - Reference Declarators.
518 ///
519 class ReferenceType : public Type, public llvm::FoldingSetNode {
520   QualType ReferenceeType;
521   ReferenceType(QualType Referencee, QualType CanonicalRef) :
522     Type(Reference, CanonicalRef), ReferenceeType(Referencee) {
523   }
524   friend class ASTContext;  // ASTContext creates these.
525 public:
526   virtual void getAsStringInternal(std::string &InnerString) const;
527
528   QualType getReferenceeType() const { return ReferenceeType; }
529
530   void Profile(llvm::FoldingSetNodeID &ID) {
531     Profile(ID, getReferenceeType());
532   }
533   static void Profile(llvm::FoldingSetNodeID &ID, QualType Referencee) {
534     ID.AddPointer(Referencee.getAsOpaquePtr());
535   }
536
537   static bool classof(const Type *T) { return T->getTypeClass() == Reference; }
538   static bool classof(const ReferenceType *) { return true; }
539 };
540
541 /// ArrayType - C99 6.7.5.2 - Array Declarators.
542 ///
543 class ArrayType : public Type, public llvm::FoldingSetNode {
544 public:
545   /// ArraySizeModifier - Capture whether this is a normal array (e.g. int X[4])
546   /// an array with a static size (e.g. int X[static 4]), or with a star size
547   /// (e.g. int X[*]). 'static' is only allowed on function parameters.
548   enum ArraySizeModifier {
549     Normal, Static, Star
550   };
551 private:
552   /// ElementType - The element type of the array.
553   QualType ElementType;
554   
555   /// NOTE: These fields are packed into the bitfields space in the Type class.
556   ArraySizeModifier SizeModifier : 2;
557   
558   /// IndexTypeQuals - Capture qualifiers in declarations like:
559   /// 'int X[static restrict 4]'. For function parameters only.
560   unsigned IndexTypeQuals : 3;
561   
562 protected:
563   ArrayType(TypeClass tc, QualType et, QualType can,
564             ArraySizeModifier sm, unsigned tq)
565     : Type(tc, can), ElementType(et), SizeModifier(sm), IndexTypeQuals(tq) {}
566   friend class ASTContext;  // ASTContext creates these.
567 public:
568   QualType getElementType() const { return ElementType; }
569   ArraySizeModifier getSizeModifier() const { return SizeModifier; }
570   unsigned getIndexTypeQualifier() const { return IndexTypeQuals; }
571   
572   QualType getBaseType() const {
573     const ArrayType *AT;
574     QualType ElmtType = getElementType();
575     // If we have a multi-dimensional array, navigate to the base type.
576     while ((AT = ElmtType->getAsArrayType()))
577       ElmtType = AT->getElementType();
578     return ElmtType;
579   }
580   static bool classof(const Type *T) {
581     return T->getTypeClass() == ConstantArray ||
582            T->getTypeClass() == VariableArray ||
583            T->getTypeClass() == IncompleteArray;
584   }
585   static bool classof(const ArrayType *) { return true; }
586 };
587
588 class ConstantArrayType : public ArrayType {
589   llvm::APInt Size; // Allows us to unique the type.
590   
591   ConstantArrayType(QualType et, QualType can, llvm::APInt sz,
592                     ArraySizeModifier sm, unsigned tq)
593     : ArrayType(ConstantArray, et, can, sm, tq), Size(sz) {}
594   friend class ASTContext;  // ASTContext creates these.
595 public:
596   llvm::APInt getSize() const { return Size; }
597   int getMaximumElements() const {
598     QualType ElmtType = getElementType();
599     int maxElements = static_cast<int>(getSize().getZExtValue());
600
601     const ConstantArrayType *CAT;
602     // If we have a multi-dimensional array, include it's elements.
603     while ((CAT = ElmtType->getAsConstantArrayType())) {
604       ElmtType = CAT->getElementType();
605       maxElements *= static_cast<int>(CAT->getSize().getZExtValue());
606     }
607     return maxElements;
608   }
609   virtual void getAsStringInternal(std::string &InnerString) const;
610   
611   void Profile(llvm::FoldingSetNodeID &ID) {
612     Profile(ID, getElementType(), getSize());
613   }
614   static void Profile(llvm::FoldingSetNodeID &ID, QualType ET,
615                       llvm::APInt ArraySize) {
616     ID.AddPointer(ET.getAsOpaquePtr());
617     ID.AddInteger(ArraySize.getZExtValue());
618   }
619   static bool classof(const Type *T) { 
620     return T->getTypeClass() == ConstantArray; 
621   }
622   static bool classof(const ConstantArrayType *) { return true; }
623   
624 protected:  
625   virtual void EmitImpl(llvm::Serializer& S) const;
626   static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D);
627   friend class Type;
628 };
629
630 class IncompleteArrayType : public ArrayType {
631   IncompleteArrayType(QualType et, QualType can,
632                     ArraySizeModifier sm, unsigned tq)
633     : ArrayType(IncompleteArray, et, can, sm, tq) {}
634   friend class ASTContext;  // ASTContext creates these.
635 public:
636
637   virtual void getAsStringInternal(std::string &InnerString) const;
638
639   static bool classof(const Type *T) { 
640     return T->getTypeClass() == IncompleteArray; 
641   }
642   static bool classof(const IncompleteArrayType *) { return true; }
643   
644   friend class StmtIteratorBase;
645   
646   void Profile(llvm::FoldingSetNodeID &ID) {
647     Profile(ID, getElementType());
648   }
649   
650   static void Profile(llvm::FoldingSetNodeID &ID, QualType ET) {
651     ID.AddPointer(ET.getAsOpaquePtr());
652   }
653   
654 protected:  
655   virtual void EmitImpl(llvm::Serializer& S) const;
656   static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
657   friend class Type;
658 };
659
660 // FIXME: VariableArrayType's aren't uniqued (since expressions aren't).
661 class VariableArrayType : public ArrayType {
662   /// SizeExpr - An assignment expression. VLA's are only permitted within 
663   /// a function block. 
664   Expr *SizeExpr;
665   
666   VariableArrayType(QualType et, QualType can, Expr *e,
667                     ArraySizeModifier sm, unsigned tq)
668     : ArrayType(VariableArray, et, can, sm, tq), SizeExpr(e) {}
669   friend class ASTContext;  // ASTContext creates these.
670 public:
671   const Expr *getSizeExpr() const { return SizeExpr; }
672   Expr *getSizeExpr() { return SizeExpr; }
673   
674   virtual void getAsStringInternal(std::string &InnerString) const;
675   
676   static bool classof(const Type *T) { 
677     return T->getTypeClass() == VariableArray; 
678   }
679   static bool classof(const VariableArrayType *) { return true; }
680   
681   friend class StmtIteratorBase;
682   
683   void Profile(llvm::FoldingSetNodeID &ID) {
684     assert (0 && "Cannnot unique VariableArrayTypes.");
685   }
686   
687 protected:  
688   virtual void EmitImpl(llvm::Serializer& S) const;
689   static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
690   friend class Type;
691 };
692
693 /// VectorType - GCC generic vector type. This type is created using
694 /// __attribute__((vector_size(n)), where "n" specifies the vector size in 
695 /// bytes. Since the constructor takes the number of vector elements, the 
696 /// client is responsible for converting the size into the number of elements.
697 class VectorType : public Type, public llvm::FoldingSetNode {
698 protected:
699   /// ElementType - The element type of the vector.
700   QualType ElementType;
701   
702   /// NumElements - The number of elements in the vector.
703   unsigned NumElements;
704   
705   VectorType(QualType vecType, unsigned nElements, QualType canonType) :
706     Type(Vector, canonType), ElementType(vecType), NumElements(nElements) {} 
707   VectorType(TypeClass tc, QualType vecType, unsigned nElements, 
708     QualType canonType) : Type(tc, canonType), ElementType(vecType), 
709     NumElements(nElements) {} 
710   friend class ASTContext;  // ASTContext creates these.
711 public:
712     
713   QualType getElementType() const { return ElementType; }
714   unsigned getNumElements() const { return NumElements; } 
715
716   virtual void getAsStringInternal(std::string &InnerString) const;
717   
718   void Profile(llvm::FoldingSetNodeID &ID) {
719     Profile(ID, getElementType(), getNumElements(), getTypeClass());
720   }
721   static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, 
722                       unsigned NumElements, TypeClass TypeClass) {
723     ID.AddPointer(ElementType.getAsOpaquePtr());
724     ID.AddInteger(NumElements);
725     ID.AddInteger(TypeClass);
726   }
727   static bool classof(const Type *T) { 
728     return T->getTypeClass() == Vector || T->getTypeClass() == OCUVector; 
729   }
730   static bool classof(const VectorType *) { return true; }
731 };
732
733 /// OCUVectorType - Extended vector type. This type is created using
734 /// __attribute__((ocu_vector_type(n)), where "n" is the number of elements.
735 /// Unlike vector_size, ocu_vector_type is only allowed on typedef's. This
736 /// class enables syntactic extensions, like Vector Components for accessing
737 /// points, colors, and textures (modeled after OpenGL Shading Language).
738 class OCUVectorType : public VectorType {
739   OCUVectorType(QualType vecType, unsigned nElements, QualType canonType) :
740     VectorType(OCUVector, vecType, nElements, canonType) {} 
741   friend class ASTContext;  // ASTContext creates these.
742 public:
743   static int getPointAccessorIdx(char c) {
744     switch (c) {
745     default: return -1;
746     case 'x': return 0;
747     case 'y': return 1;
748     case 'z': return 2;
749     case 'w': return 3;
750     }
751   }
752   static int getColorAccessorIdx(char c) {
753     switch (c) {
754     default: return -1;
755     case 'r': return 0;
756     case 'g': return 1;
757     case 'b': return 2;
758     case 'a': return 3;
759     }
760   }
761   static int getTextureAccessorIdx(char c) {
762     switch (c) {
763     default: return -1;
764     case 's': return 0;
765     case 't': return 1;
766     case 'p': return 2;
767     case 'q': return 3;
768     }
769   };
770   
771   static int getAccessorIdx(char c) {
772     if (int idx = getPointAccessorIdx(c)+1) return idx-1;
773     if (int idx = getColorAccessorIdx(c)+1) return idx-1;
774     return getTextureAccessorIdx(c);
775   }
776   
777   bool isAccessorWithinNumElements(char c) const {
778     if (int idx = getAccessorIdx(c)+1)
779       return unsigned(idx-1) < NumElements;
780     return false;
781   }
782   virtual void getAsStringInternal(std::string &InnerString) const;
783
784   static bool classof(const Type *T) { 
785     return T->getTypeClass() == OCUVector; 
786   }
787   static bool classof(const OCUVectorType *) { return true; }
788 };
789
790 /// FunctionType - C99 6.7.5.3 - Function Declarators.  This is the common base
791 /// class of FunctionTypeNoProto and FunctionTypeProto.
792 ///
793 class FunctionType : public Type {
794   /// SubClassData - This field is owned by the subclass, put here to pack
795   /// tightly with the ivars in Type.
796   bool SubClassData : 1;
797   
798   // The type returned by the function.
799   QualType ResultType;
800 protected:
801   FunctionType(TypeClass tc, QualType res, bool SubclassInfo,QualType Canonical)
802     : Type(tc, Canonical), SubClassData(SubclassInfo), ResultType(res) {}
803   bool getSubClassData() const { return SubClassData; }
804 public:
805   
806   QualType getResultType() const { return ResultType; }
807
808   
809   static bool classof(const Type *T) {
810     return T->getTypeClass() == FunctionNoProto ||
811            T->getTypeClass() == FunctionProto;
812   }
813   static bool classof(const FunctionType *) { return true; }
814 };
815
816 /// FunctionTypeNoProto - Represents a K&R-style 'int foo()' function, which has
817 /// no information available about its arguments.
818 class FunctionTypeNoProto : public FunctionType, public llvm::FoldingSetNode {
819   FunctionTypeNoProto(QualType Result, QualType Canonical)
820     : FunctionType(FunctionNoProto, Result, false, Canonical) {}
821   friend class ASTContext;  // ASTContext creates these.
822 public:
823   // No additional state past what FunctionType provides.
824   
825   virtual void getAsStringInternal(std::string &InnerString) const;
826
827   void Profile(llvm::FoldingSetNodeID &ID) {
828     Profile(ID, getResultType());
829   }
830   static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType) {
831     ID.AddPointer(ResultType.getAsOpaquePtr());
832   }
833   
834   static bool classof(const Type *T) {
835     return T->getTypeClass() == FunctionNoProto;
836   }
837   static bool classof(const FunctionTypeNoProto *) { return true; }
838   
839 protected:  
840   virtual void EmitImpl(llvm::Serializer& S) const;
841   static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
842   friend class Type;
843 };
844
845 /// FunctionTypeProto - Represents a prototype with argument type info, e.g.
846 /// 'int foo(int)' or 'int foo(void)'.  'void' is represented as having no
847 /// arguments, not as having a single void argument.
848 class FunctionTypeProto : public FunctionType, public llvm::FoldingSetNode {
849   FunctionTypeProto(QualType Result, QualType *ArgArray, unsigned numArgs,
850                     bool isVariadic, QualType Canonical)
851     : FunctionType(FunctionProto, Result, isVariadic, Canonical),
852       NumArgs(numArgs) {
853     // Fill in the trailing argument array.
854     QualType *ArgInfo = reinterpret_cast<QualType *>(this+1);;
855     for (unsigned i = 0; i != numArgs; ++i)
856       ArgInfo[i] = ArgArray[i];
857   }
858   
859   /// NumArgs - The number of arguments this function has, not counting '...'.
860   unsigned NumArgs;
861   
862   /// ArgInfo - There is an variable size array after the class in memory that
863   /// holds the argument types.
864   friend class ASTContext;  // ASTContext creates these.
865 public:
866   unsigned getNumArgs() const { return NumArgs; }
867   QualType getArgType(unsigned i) const {
868     assert(i < NumArgs && "Invalid argument number!");
869     return arg_type_begin()[i];
870   }
871     
872   bool isVariadic() const { return getSubClassData(); }
873   
874   typedef const QualType *arg_type_iterator;
875   arg_type_iterator arg_type_begin() const {
876     return reinterpret_cast<const QualType *>(this+1);
877   }
878   arg_type_iterator arg_type_end() const { return arg_type_begin()+NumArgs; }
879   
880   virtual void getAsStringInternal(std::string &InnerString) const;
881
882   static bool classof(const Type *T) {
883     return T->getTypeClass() == FunctionProto;
884   }
885   static bool classof(const FunctionTypeProto *) { return true; }
886   
887   void Profile(llvm::FoldingSetNodeID &ID);
888   static void Profile(llvm::FoldingSetNodeID &ID, QualType Result,
889                       arg_type_iterator ArgTys, unsigned NumArgs,
890                       bool isVariadic);
891
892 protected:  
893   virtual void EmitImpl(llvm::Serializer& S) const;
894   static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
895   friend class Type;
896 };
897
898
899 class TypedefType : public Type {
900   TypedefDecl *Decl;
901 protected:
902   TypedefType(TypeClass tc, TypedefDecl *D, QualType can) 
903     : Type(tc, can), Decl(D) {
904     assert(!isa<TypedefType>(can) && "Invalid canonical type");
905   }
906   friend class ASTContext;  // ASTContext creates these.
907 public:
908   
909   TypedefDecl *getDecl() const { return Decl; }
910   
911   /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
912   /// potentially looking through *all* consequtive typedefs.  This returns the
913   /// sum of the type qualifiers, so if you have:
914   ///   typedef const int A;
915   ///   typedef volatile A B;
916   /// looking through the typedefs for B will give you "const volatile A".
917   QualType LookThroughTypedefs() const;
918     
919   virtual void getAsStringInternal(std::string &InnerString) const;
920
921   static bool classof(const Type *T) { return T->getTypeClass() == TypeName; }
922   static bool classof(const TypedefType *) { return true; }
923   
924 protected:  
925   virtual void EmitImpl(llvm::Serializer& S) const;
926   static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
927   friend class Type;
928 };
929
930 /// TypeOfExpr (GCC extension).
931 class TypeOfExpr : public Type {
932   Expr *TOExpr;
933   TypeOfExpr(Expr *E, QualType can) : Type(TypeOfExp, can), TOExpr(E) {
934     assert(!isa<TypedefType>(can) && "Invalid canonical type");
935   }
936   friend class ASTContext;  // ASTContext creates these.
937 public:
938   Expr *getUnderlyingExpr() const { return TOExpr; }
939   
940   virtual void getAsStringInternal(std::string &InnerString) const;
941
942   static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExp; }
943   static bool classof(const TypeOfExpr *) { return true; }
944 };
945
946 /// TypeOfType (GCC extension).
947 class TypeOfType : public Type {
948   QualType TOType;
949   TypeOfType(QualType T, QualType can) : Type(TypeOfTyp, can), TOType(T) {
950     assert(!isa<TypedefType>(can) && "Invalid canonical type");
951   }
952   friend class ASTContext;  // ASTContext creates these.
953 public:
954   QualType getUnderlyingType() const { return TOType; }
955   
956   virtual void getAsStringInternal(std::string &InnerString) const;
957
958   static bool classof(const Type *T) { return T->getTypeClass() == TypeOfTyp; }
959   static bool classof(const TypeOfType *) { return true; }
960 };
961
962 class TagType : public Type {
963   TagDecl *decl;
964   TagType(TagDecl *D, QualType can) : Type(Tagged, can), decl(D) {}
965   friend class ASTContext;  // ASTContext creates these.
966 public:
967     
968   TagDecl *getDecl() const { return decl; }
969   
970   virtual void getAsStringInternal(std::string &InnerString) const;
971   
972   static bool classof(const Type *T) { return T->getTypeClass() == Tagged; }
973   static bool classof(const TagType *) { return true; }
974   
975 protected:  
976   virtual void EmitImpl(llvm::Serializer& S) const;
977   static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D);
978   friend class Type;
979 };
980
981 class ObjCInterfaceType : public Type {
982   ObjCInterfaceDecl *Decl;
983 protected:
984   ObjCInterfaceType(TypeClass tc, ObjCInterfaceDecl *D) : 
985     Type(tc, QualType()), Decl(D) { }
986   friend class ASTContext;  // ASTContext creates these.
987 public:
988   
989   ObjCInterfaceDecl *getDecl() const { return Decl; }
990   
991   virtual void getAsStringInternal(std::string &InnerString) const;
992   
993   static bool classof(const Type *T) { 
994     return T->getTypeClass() == ObjCInterface; 
995   }
996   static bool classof(const ObjCInterfaceType *) { return true; }
997 };
998
999 /// ObjCQualifiedInterfaceType - This class represents interface types 
1000 /// conforming to a list of protocols; such as, INTF<Proto1, Proto2, Proto1>.
1001 /// Duplicate protocols are removed and protocol list is canonicalized to be in
1002 /// alphabetical order.
1003 class ObjCQualifiedInterfaceType : public ObjCInterfaceType, 
1004                                    public llvm::FoldingSetNode {
1005                                      
1006   // List of protocols for this protocol conforming object type
1007   // List is sorted on protocol name. No protocol is enterred more than once.
1008   llvm::SmallVector<ObjCProtocolDecl*, 8> Protocols;
1009
1010   ObjCQualifiedInterfaceType(ObjCInterfaceDecl *D,
1011                              ObjCProtocolDecl **Protos,  unsigned NumP) : 
1012     ObjCInterfaceType(ObjCQualifiedInterface, D), 
1013     Protocols(Protos, Protos+NumP) { }
1014   friend class ASTContext;  // ASTContext creates these.
1015 public:
1016   
1017   ObjCProtocolDecl *getProtocols(unsigned i) const {
1018     return Protocols[i];
1019   }
1020   unsigned getNumProtocols() const {
1021     return Protocols.size();
1022   }
1023   ObjCProtocolDecl **getReferencedProtocols() {
1024     return &Protocols[0];
1025   }  
1026   virtual void getAsStringInternal(std::string &InnerString) const;
1027   
1028   void Profile(llvm::FoldingSetNodeID &ID);
1029   static void Profile(llvm::FoldingSetNodeID &ID, 
1030                       ObjCProtocolDecl **protocols, unsigned NumProtocols);
1031  
1032   static bool classof(const Type *T) { 
1033     return T->getTypeClass() == ObjCQualifiedInterface; 
1034   }
1035   static bool classof(const ObjCQualifiedInterfaceType *) { return true; }
1036 };
1037
1038 /// ObjCQualifiedIdType - to represent id<protocol-list>
1039 class ObjCQualifiedIdType : public Type,
1040                             public llvm::FoldingSetNode {
1041   // List of protocols for this protocol conforming 'id' type
1042   // List is sorted on protocol name. No protocol is enterred more than once.
1043   llvm::SmallVector<ObjCProtocolDecl*, 8> Protocols;
1044     
1045   ObjCQualifiedIdType(QualType can, ObjCProtocolDecl **Protos,  unsigned NumP)
1046   : Type(ObjCQualifiedId, can), 
1047   Protocols(Protos, Protos+NumP) { }
1048   friend class ASTContext;  // ASTContext creates these.
1049 public:
1050     
1051   ObjCProtocolDecl *getProtocols(unsigned i) const {
1052     return Protocols[i];
1053   }
1054   unsigned getNumProtocols() const {
1055     return Protocols.size();
1056   }
1057   ObjCProtocolDecl **getReferencedProtocols() {
1058     return &Protocols[0];
1059   } 
1060     
1061   virtual void getAsStringInternal(std::string &InnerString) const;
1062     
1063   void Profile(llvm::FoldingSetNodeID &ID);
1064   static void Profile(llvm::FoldingSetNodeID &ID, 
1065                       ObjCProtocolDecl **protocols, unsigned NumProtocols);
1066     
1067   static bool classof(const Type *T) { 
1068     return T->getTypeClass() == ObjCQualifiedId; 
1069   }
1070   static bool classof(const ObjCQualifiedIdType *) { return true; }
1071     
1072 };
1073   
1074 /// RecordType - This is a helper class that allows the use of isa/cast/dyncast
1075 /// to detect TagType objects of structs/unions/classes.
1076 class RecordType : public TagType {
1077   RecordType(); // DO NOT IMPLEMENT
1078 public:
1079     
1080   RecordDecl *getDecl() const {
1081     return reinterpret_cast<RecordDecl*>(TagType::getDecl());
1082   }
1083   
1084   // FIXME: This predicate is a helper to QualType/Type. It needs to 
1085   // recursively check all fields for const-ness. If any field is declared
1086   // const, it needs to return false. 
1087   bool hasConstFields() const { return false; } 
1088   
1089   static bool classof(const Type *T);
1090   static bool classof(const RecordType *) { return true; }
1091 };
1092
1093
1094 // Inline function definitions.
1095
1096 /// getCanonicalType - Return the canonical version of this type, with the
1097 /// appropriate type qualifiers on it.
1098 inline QualType QualType::getCanonicalType() const {
1099   QualType CanType = getTypePtr()->getCanonicalTypeInternal();
1100   return QualType(CanType.getTypePtr(),
1101                   getCVRQualifiers() | CanType.getCVRQualifiers());
1102 }
1103
1104 /// getUnqualifiedType - Return the type without any qualifiers.
1105 inline QualType QualType::getUnqualifiedType() const {
1106   Type *TP = getTypePtr();
1107   if (const ASQualType *ASQT = dyn_cast<ASQualType>(TP))
1108     TP = ASQT->getBaseType();
1109   return QualType(TP, 0);
1110 }
1111
1112 /// getAddressSpace - Return the address space of this type.
1113 inline unsigned QualType::getAddressSpace() const {
1114   if (const ASQualType *ASQT = dyn_cast<ASQualType>(getCanonicalType()))
1115     return ASQT->getAddressSpace();
1116   return 0;
1117 }
1118
1119 inline bool Type::isFunctionType() const {
1120   return isa<FunctionType>(CanonicalType.getUnqualifiedType());
1121 }
1122 inline bool Type::isPointerType() const {
1123   return isa<PointerType>(CanonicalType.getUnqualifiedType()); 
1124 }
1125 inline bool Type::isFunctionPointerType() const {
1126   if (const PointerType* T = getAsPointerType())
1127     return T->getPointeeType()->isFunctionType();
1128   else
1129     return false;
1130 }
1131 inline bool Type::isReferenceType() const {
1132   return isa<ReferenceType>(CanonicalType.getUnqualifiedType());
1133 }
1134 inline bool Type::isArrayType() const {
1135   return isa<ArrayType>(CanonicalType.getUnqualifiedType());
1136 }
1137 inline bool Type::isRecordType() const {
1138   return isa<RecordType>(CanonicalType.getUnqualifiedType());
1139 }
1140 inline bool Type::isVectorType() const {
1141   return isa<VectorType>(CanonicalType.getUnqualifiedType());
1142 }
1143 inline bool Type::isOCUVectorType() const {
1144   return isa<OCUVectorType>(CanonicalType.getUnqualifiedType());
1145 }
1146 inline bool Type::isObjCInterfaceType() const {
1147   return isa<ObjCInterfaceType>(CanonicalType)
1148            || isa<ObjCQualifiedInterfaceType>(CanonicalType);
1149 }
1150 inline bool Type::isObjCQualifiedIdType() const {
1151   return isa<ObjCQualifiedIdType>(CanonicalType);
1152 }
1153 }  // end namespace clang
1154
1155 #endif