1 //===--- Type.h - C Language Family Type Representation ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Type interface and subclasses.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_TYPE_H
15 #define LLVM_CLANG_AST_TYPE_H
17 #include "llvm/Support/Casting.h"
18 #include "llvm/ADT/FoldingSet.h"
19 #include "llvm/ADT/APSInt.h"
20 #include "llvm/Bitcode/SerializationFwd.h"
24 using llvm::cast_or_null;
26 using llvm::dyn_cast_or_null;
36 class ObjCInterfaceDecl;
37 class ObjCProtocolDecl;
45 class ConstantArrayType;
46 class VariableArrayType;
47 class IncompleteArrayType;
54 class ObjCQualifiedInterfaceType;
55 class StmtIteratorBase;
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).
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
70 enum TQ { // NOTE: These flags must be kept in sync with DeclSpec::TQ.
74 CVRFlags = Const|Restrict|Volatile
77 QualType() : ThePtr(0) {}
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?");
86 static QualType getFromOpaquePtr(void *Ptr) {
88 T.ThePtr = reinterpret_cast<uintptr_t>(Ptr);
92 unsigned getCVRQualifiers() const {
93 return ThePtr & CVRFlags;
95 Type *getTypePtr() const {
96 return reinterpret_cast<Type*>(ThePtr & ~CVRFlags);
99 void *getAsOpaquePtr() const {
100 return reinterpret_cast<void*>(ThePtr);
103 Type &operator*() const {
104 return *getTypePtr();
107 Type *operator->() const {
111 /// isNull - Return true if this QualType doesn't point to a type yet.
112 bool isNull() const {
116 bool isConstQualified() const {
117 return (ThePtr & Const) ? true : false;
119 bool isVolatileQualified() const {
120 return (ThePtr & Volatile) ? true : false;
122 bool isRestrictQualified() const {
123 return (ThePtr & Restrict) ? true : false;
126 /// addConst/addVolatile/addRestrict - add the specified type qual to this
128 void addConst() { ThePtr |= Const; }
129 void addVolatile() { ThePtr |= Volatile; }
130 void addRestrict() { ThePtr |= Restrict; }
132 QualType getQualifiedType(unsigned TQs) const {
133 return QualType(getTypePtr(), TQs);
136 inline QualType getUnqualifiedType() const;
138 /// operator==/!= - Indicate whether the specified types and qualifiers are
140 bool operator==(const QualType &RHS) const {
141 return ThePtr == RHS.ThePtr;
143 bool operator!=(const QualType &RHS) const {
144 return ThePtr != RHS.ThePtr;
146 std::string getAsString() const {
148 getAsStringInternal(S);
151 void getAsStringInternal(std::string &Str) const;
153 void dump(const char *s = 0) const;
155 /// getCanonicalType - Return the canonical version of this type, with the
156 /// appropriate type qualifiers on it.
157 inline QualType getCanonicalType() const;
159 /// getAddressSpace - Return the address space of this type.
160 inline unsigned getAddressSpace() const;
162 /// Emit - Serialize a QualType to Bitcode.
163 void Emit(llvm::Serializer& S) const;
165 /// Read - Deserialize a QualType from Bitcode.
166 static QualType ReadVal(llvm::Deserializer& D);
169 void ReadBackpatch(llvm::Deserializer& D);
170 friend class FieldDecl;
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();
184 template<> struct simplify_type< ::clang::QualType>
185 : public simplify_type<const ::clang::QualType> {};
187 } // end namespace llvm
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:
197 /// typedef foo* bar;
198 /// 'int *' 'foo *' 'bar'
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
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.
214 /// Types, once created, are immutable.
219 Builtin, Complex, Pointer, Reference,
220 ConstantArray, VariableArray, IncompleteArray,
222 FunctionNoProto, FunctionProto,
223 TypeName, Tagged, ASQual,
224 ObjCInterface, ObjCQualifiedInterface,
226 TypeOfExp, TypeOfTyp // GNU typeof extension.
229 QualType CanonicalType;
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.
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),
242 friend class ASTContext;
244 void EmitTypeInternal(llvm::Serializer& S) const;
245 void ReadTypeInternal(llvm::Deserializer& D);
248 TypeClass getTypeClass() const { return static_cast<TypeClass>(TC); }
250 bool isCanonical() const { return CanonicalType.getTypePtr() == this; }
252 /// Types are partitioned into 3 broad categories (C99 6.2.5p1):
253 /// object types, function types, and incomplete types.
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;
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;
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;
269 /// isIncompleteArrayType (C99 6.2.5p22) - Return true for variable array
270 /// types that don't have any expression ("[]").
271 bool isIncompleteArrayType() const;
273 /// Helper methods to distinguish type categories. All type predicates
274 /// operate on the canonical type, ignoring typedefs and qualifiers.
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;
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)
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
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.
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
338 const Type *getDesugaredType() const;
340 /// More type predicates useful for type checking/promotion
341 bool isPromotableIntegerType() const; // C99 6.3.1.1p2
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;
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
353 bool isUnsignedIntegerType() const;
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;
360 QualType getCanonicalTypeInternal() const { return CanonicalType; }
361 friend class QualType;
363 virtual void getAsStringInternal(std::string &InnerString) const = 0;
364 static bool classof(const Type *) { return true; }
367 /// Emit - Emit a Type to bitcode. Used by ASTContext.
368 void Emit(llvm::Serializer& S) const;
370 /// Create - Construct a Type from bitcode. Used by ASTContext.
371 static void Create(ASTContext& Context, unsigned i, llvm::Deserializer& S);
373 /// EmitImpl - Subclasses must implement this method in order to
375 virtual void EmitImpl(llvm::Serializer& S) const;
378 /// ASQualType - TR18037 (C embedded extensions) 6.2.5p26
379 /// This supports address space qualified types.
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.
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) {
391 friend class ASTContext; // ASTContext creates these.
393 Type *getBaseType() const { return BaseType; }
394 unsigned getAddressSpace() const { return AddressSpace; }
396 virtual void getAsStringInternal(std::string &InnerString) const;
398 void Profile(llvm::FoldingSetNodeID &ID) {
399 Profile(ID, getBaseType(), AddressSpace);
401 static void Profile(llvm::FoldingSetNodeID &ID, Type *Base,
402 unsigned AddrSpace) {
404 ID.AddInteger(AddrSpace);
407 static bool classof(const Type *T) { return T->getTypeClass() == ASQual; }
408 static bool classof(const ASQualType *) { return true; }
411 virtual void EmitImpl(llvm::Serializer& S) const;
412 static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
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 {
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.
432 Char_S, // This is 'char' for targets where char is signed.
433 SChar, // This is explicitly qualified signed char.
439 Float, Double, LongDouble
444 BuiltinType(Kind K) : Type(Builtin, QualType()), TypeKind(K) {}
446 Kind getKind() const { return TypeKind; }
447 const char *getName() const;
449 virtual void getAsStringInternal(std::string &InnerString) const;
451 static bool classof(const Type *T) { return T->getTypeClass() == Builtin; }
452 static bool classof(const BuiltinType *) { return true; }
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.
458 class ComplexType : public Type, public llvm::FoldingSetNode {
459 QualType ElementType;
460 ComplexType(QualType Element, QualType CanonicalPtr) :
461 Type(Complex, CanonicalPtr), ElementType(Element) {
463 friend class ASTContext; // ASTContext creates these.
465 QualType getElementType() const { return ElementType; }
467 virtual void getAsStringInternal(std::string &InnerString) const;
469 void Profile(llvm::FoldingSetNodeID &ID) {
470 Profile(ID, getElementType());
472 static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) {
473 ID.AddPointer(Element.getAsOpaquePtr());
476 static bool classof(const Type *T) { return T->getTypeClass() == Complex; }
477 static bool classof(const ComplexType *) { return true; }
480 virtual void EmitImpl(llvm::Serializer& S) const;
481 static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
486 /// PointerType - C99 6.7.5.1 - Pointer Declarators.
488 class PointerType : public Type, public llvm::FoldingSetNode {
489 QualType PointeeType;
490 PointerType(QualType Pointee, QualType CanonicalPtr) :
491 Type(Pointer, CanonicalPtr), PointeeType(Pointee) {
493 friend class ASTContext; // ASTContext creates these.
496 QualType getPointeeType() const { return PointeeType; }
498 virtual void getAsStringInternal(std::string &InnerString) const;
501 void Profile(llvm::FoldingSetNodeID &ID) {
502 Profile(ID, getPointeeType());
504 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) {
505 ID.AddPointer(Pointee.getAsOpaquePtr());
508 static bool classof(const Type *T) { return T->getTypeClass() == Pointer; }
509 static bool classof(const PointerType *) { return true; }
512 virtual void EmitImpl(llvm::Serializer& S) const;
513 static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
517 /// ReferenceType - C++ 8.3.2 - Reference Declarators.
519 class ReferenceType : public Type, public llvm::FoldingSetNode {
520 QualType ReferenceeType;
521 ReferenceType(QualType Referencee, QualType CanonicalRef) :
522 Type(Reference, CanonicalRef), ReferenceeType(Referencee) {
524 friend class ASTContext; // ASTContext creates these.
526 virtual void getAsStringInternal(std::string &InnerString) const;
528 QualType getReferenceeType() const { return ReferenceeType; }
530 void Profile(llvm::FoldingSetNodeID &ID) {
531 Profile(ID, getReferenceeType());
533 static void Profile(llvm::FoldingSetNodeID &ID, QualType Referencee) {
534 ID.AddPointer(Referencee.getAsOpaquePtr());
537 static bool classof(const Type *T) { return T->getTypeClass() == Reference; }
538 static bool classof(const ReferenceType *) { return true; }
541 /// ArrayType - C99 6.7.5.2 - Array Declarators.
543 class ArrayType : public Type, public llvm::FoldingSetNode {
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 {
552 /// ElementType - The element type of the array.
553 QualType ElementType;
555 /// NOTE: These fields are packed into the bitfields space in the Type class.
556 ArraySizeModifier SizeModifier : 2;
558 /// IndexTypeQuals - Capture qualifiers in declarations like:
559 /// 'int X[static restrict 4]'. For function parameters only.
560 unsigned IndexTypeQuals : 3;
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.
568 QualType getElementType() const { return ElementType; }
569 ArraySizeModifier getSizeModifier() const { return SizeModifier; }
570 unsigned getIndexTypeQualifier() const { return IndexTypeQuals; }
572 QualType getBaseType() const {
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();
580 static bool classof(const Type *T) {
581 return T->getTypeClass() == ConstantArray ||
582 T->getTypeClass() == VariableArray ||
583 T->getTypeClass() == IncompleteArray;
585 static bool classof(const ArrayType *) { return true; }
588 class ConstantArrayType : public ArrayType {
589 llvm::APInt Size; // Allows us to unique the type.
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.
596 llvm::APInt getSize() const { return Size; }
597 int getMaximumElements() const {
598 QualType ElmtType = getElementType();
599 int maxElements = static_cast<int>(getSize().getZExtValue());
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());
609 virtual void getAsStringInternal(std::string &InnerString) const;
611 void Profile(llvm::FoldingSetNodeID &ID) {
612 Profile(ID, getElementType(), getSize());
614 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET,
615 llvm::APInt ArraySize) {
616 ID.AddPointer(ET.getAsOpaquePtr());
617 ID.AddInteger(ArraySize.getZExtValue());
619 static bool classof(const Type *T) {
620 return T->getTypeClass() == ConstantArray;
622 static bool classof(const ConstantArrayType *) { return true; }
625 virtual void EmitImpl(llvm::Serializer& S) const;
626 static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D);
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.
637 virtual void getAsStringInternal(std::string &InnerString) const;
639 static bool classof(const Type *T) {
640 return T->getTypeClass() == IncompleteArray;
642 static bool classof(const IncompleteArrayType *) { return true; }
644 friend class StmtIteratorBase;
646 void Profile(llvm::FoldingSetNodeID &ID) {
647 Profile(ID, getElementType());
650 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET) {
651 ID.AddPointer(ET.getAsOpaquePtr());
655 virtual void EmitImpl(llvm::Serializer& S) const;
656 static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
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.
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.
671 const Expr *getSizeExpr() const { return SizeExpr; }
672 Expr *getSizeExpr() { return SizeExpr; }
674 virtual void getAsStringInternal(std::string &InnerString) const;
676 static bool classof(const Type *T) {
677 return T->getTypeClass() == VariableArray;
679 static bool classof(const VariableArrayType *) { return true; }
681 friend class StmtIteratorBase;
683 void Profile(llvm::FoldingSetNodeID &ID) {
684 assert (0 && "Cannnot unique VariableArrayTypes.");
688 virtual void EmitImpl(llvm::Serializer& S) const;
689 static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
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 {
699 /// ElementType - The element type of the vector.
700 QualType ElementType;
702 /// NumElements - The number of elements in the vector.
703 unsigned NumElements;
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.
713 QualType getElementType() const { return ElementType; }
714 unsigned getNumElements() const { return NumElements; }
716 virtual void getAsStringInternal(std::string &InnerString) const;
718 void Profile(llvm::FoldingSetNodeID &ID) {
719 Profile(ID, getElementType(), getNumElements(), getTypeClass());
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);
727 static bool classof(const Type *T) {
728 return T->getTypeClass() == Vector || T->getTypeClass() == OCUVector;
730 static bool classof(const VectorType *) { return true; }
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.
743 static int getPointAccessorIdx(char c) {
752 static int getColorAccessorIdx(char c) {
761 static int getTextureAccessorIdx(char c) {
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);
777 bool isAccessorWithinNumElements(char c) const {
778 if (int idx = getAccessorIdx(c)+1)
779 return unsigned(idx-1) < NumElements;
782 virtual void getAsStringInternal(std::string &InnerString) const;
784 static bool classof(const Type *T) {
785 return T->getTypeClass() == OCUVector;
787 static bool classof(const OCUVectorType *) { return true; }
790 /// FunctionType - C99 6.7.5.3 - Function Declarators. This is the common base
791 /// class of FunctionTypeNoProto and FunctionTypeProto.
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;
798 // The type returned by the function.
801 FunctionType(TypeClass tc, QualType res, bool SubclassInfo,QualType Canonical)
802 : Type(tc, Canonical), SubClassData(SubclassInfo), ResultType(res) {}
803 bool getSubClassData() const { return SubClassData; }
806 QualType getResultType() const { return ResultType; }
809 static bool classof(const Type *T) {
810 return T->getTypeClass() == FunctionNoProto ||
811 T->getTypeClass() == FunctionProto;
813 static bool classof(const FunctionType *) { return true; }
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.
823 // No additional state past what FunctionType provides.
825 virtual void getAsStringInternal(std::string &InnerString) const;
827 void Profile(llvm::FoldingSetNodeID &ID) {
828 Profile(ID, getResultType());
830 static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType) {
831 ID.AddPointer(ResultType.getAsOpaquePtr());
834 static bool classof(const Type *T) {
835 return T->getTypeClass() == FunctionNoProto;
837 static bool classof(const FunctionTypeNoProto *) { return true; }
840 virtual void EmitImpl(llvm::Serializer& S) const;
841 static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
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),
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];
859 /// NumArgs - The number of arguments this function has, not counting '...'.
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.
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];
872 bool isVariadic() const { return getSubClassData(); }
874 typedef const QualType *arg_type_iterator;
875 arg_type_iterator arg_type_begin() const {
876 return reinterpret_cast<const QualType *>(this+1);
878 arg_type_iterator arg_type_end() const { return arg_type_begin()+NumArgs; }
880 virtual void getAsStringInternal(std::string &InnerString) const;
882 static bool classof(const Type *T) {
883 return T->getTypeClass() == FunctionProto;
885 static bool classof(const FunctionTypeProto *) { return true; }
887 void Profile(llvm::FoldingSetNodeID &ID);
888 static void Profile(llvm::FoldingSetNodeID &ID, QualType Result,
889 arg_type_iterator ArgTys, unsigned NumArgs,
893 virtual void EmitImpl(llvm::Serializer& S) const;
894 static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
899 class TypedefType : public Type {
902 TypedefType(TypeClass tc, TypedefDecl *D, QualType can)
903 : Type(tc, can), Decl(D) {
904 assert(!isa<TypedefType>(can) && "Invalid canonical type");
906 friend class ASTContext; // ASTContext creates these.
909 TypedefDecl *getDecl() const { return Decl; }
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;
919 virtual void getAsStringInternal(std::string &InnerString) const;
921 static bool classof(const Type *T) { return T->getTypeClass() == TypeName; }
922 static bool classof(const TypedefType *) { return true; }
925 virtual void EmitImpl(llvm::Serializer& S) const;
926 static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
930 /// TypeOfExpr (GCC extension).
931 class TypeOfExpr : public Type {
933 TypeOfExpr(Expr *E, QualType can) : Type(TypeOfExp, can), TOExpr(E) {
934 assert(!isa<TypedefType>(can) && "Invalid canonical type");
936 friend class ASTContext; // ASTContext creates these.
938 Expr *getUnderlyingExpr() const { return TOExpr; }
940 virtual void getAsStringInternal(std::string &InnerString) const;
942 static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExp; }
943 static bool classof(const TypeOfExpr *) { return true; }
946 /// TypeOfType (GCC extension).
947 class TypeOfType : public Type {
949 TypeOfType(QualType T, QualType can) : Type(TypeOfTyp, can), TOType(T) {
950 assert(!isa<TypedefType>(can) && "Invalid canonical type");
952 friend class ASTContext; // ASTContext creates these.
954 QualType getUnderlyingType() const { return TOType; }
956 virtual void getAsStringInternal(std::string &InnerString) const;
958 static bool classof(const Type *T) { return T->getTypeClass() == TypeOfTyp; }
959 static bool classof(const TypeOfType *) { return true; }
962 class TagType : public Type {
964 TagType(TagDecl *D, QualType can) : Type(Tagged, can), decl(D) {}
965 friend class ASTContext; // ASTContext creates these.
968 TagDecl *getDecl() const { return decl; }
970 virtual void getAsStringInternal(std::string &InnerString) const;
972 static bool classof(const Type *T) { return T->getTypeClass() == Tagged; }
973 static bool classof(const TagType *) { return true; }
976 virtual void EmitImpl(llvm::Serializer& S) const;
977 static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D);
981 class ObjCInterfaceType : public Type {
982 ObjCInterfaceDecl *Decl;
984 ObjCInterfaceType(TypeClass tc, ObjCInterfaceDecl *D) :
985 Type(tc, QualType()), Decl(D) { }
986 friend class ASTContext; // ASTContext creates these.
989 ObjCInterfaceDecl *getDecl() const { return Decl; }
991 virtual void getAsStringInternal(std::string &InnerString) const;
993 static bool classof(const Type *T) {
994 return T->getTypeClass() == ObjCInterface;
996 static bool classof(const ObjCInterfaceType *) { return true; }
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 {
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;
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.
1017 ObjCProtocolDecl *getProtocols(unsigned i) const {
1018 return Protocols[i];
1020 unsigned getNumProtocols() const {
1021 return Protocols.size();
1023 ObjCProtocolDecl **getReferencedProtocols() {
1024 return &Protocols[0];
1026 virtual void getAsStringInternal(std::string &InnerString) const;
1028 void Profile(llvm::FoldingSetNodeID &ID);
1029 static void Profile(llvm::FoldingSetNodeID &ID,
1030 ObjCProtocolDecl **protocols, unsigned NumProtocols);
1032 static bool classof(const Type *T) {
1033 return T->getTypeClass() == ObjCQualifiedInterface;
1035 static bool classof(const ObjCQualifiedInterfaceType *) { return true; }
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;
1045 ObjCQualifiedIdType(QualType can, ObjCProtocolDecl **Protos, unsigned NumP)
1046 : Type(ObjCQualifiedId, can),
1047 Protocols(Protos, Protos+NumP) { }
1048 friend class ASTContext; // ASTContext creates these.
1051 ObjCProtocolDecl *getProtocols(unsigned i) const {
1052 return Protocols[i];
1054 unsigned getNumProtocols() const {
1055 return Protocols.size();
1057 ObjCProtocolDecl **getReferencedProtocols() {
1058 return &Protocols[0];
1061 virtual void getAsStringInternal(std::string &InnerString) const;
1063 void Profile(llvm::FoldingSetNodeID &ID);
1064 static void Profile(llvm::FoldingSetNodeID &ID,
1065 ObjCProtocolDecl **protocols, unsigned NumProtocols);
1067 static bool classof(const Type *T) {
1068 return T->getTypeClass() == ObjCQualifiedId;
1070 static bool classof(const ObjCQualifiedIdType *) { return true; }
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
1080 RecordDecl *getDecl() const {
1081 return reinterpret_cast<RecordDecl*>(TagType::getDecl());
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; }
1089 static bool classof(const Type *T);
1090 static bool classof(const RecordType *) { return true; }
1094 // Inline function definitions.
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());
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);
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();
1119 inline bool Type::isFunctionType() const {
1120 return isa<FunctionType>(CanonicalType.getUnqualifiedType());
1122 inline bool Type::isPointerType() const {
1123 return isa<PointerType>(CanonicalType.getUnqualifiedType());
1125 inline bool Type::isFunctionPointerType() const {
1126 if (const PointerType* T = getAsPointerType())
1127 return T->getPointeeType()->isFunctionType();
1131 inline bool Type::isReferenceType() const {
1132 return isa<ReferenceType>(CanonicalType.getUnqualifiedType());
1134 inline bool Type::isArrayType() const {
1135 return isa<ArrayType>(CanonicalType.getUnqualifiedType());
1137 inline bool Type::isRecordType() const {
1138 return isa<RecordType>(CanonicalType.getUnqualifiedType());
1140 inline bool Type::isVectorType() const {
1141 return isa<VectorType>(CanonicalType.getUnqualifiedType());
1143 inline bool Type::isOCUVectorType() const {
1144 return isa<OCUVectorType>(CanonicalType.getUnqualifiedType());
1146 inline bool Type::isObjCInterfaceType() const {
1147 return isa<ObjCInterfaceType>(CanonicalType)
1148 || isa<ObjCQualifiedInterfaceType>(CanonicalType);
1150 inline bool Type::isObjCQualifiedIdType() const {
1151 return isa<ObjCQualifiedIdType>(CanonicalType);
1153 } // end namespace clang