Type.cpp revision 672c91db00d28187600dd18ef6c524ff45e95ef2
1//===--- Type.cpp - Type representation and manipulation ------------------===//
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 implements type-related functionality.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Type.h"
16#include "clang/AST/DeclCXX.h"
17#include "clang/AST/DeclObjC.h"
18#include "clang/AST/DeclTemplate.h"
19#include "clang/AST/Expr.h"
20#include "llvm/ADT/StringExtras.h"
21
22using namespace clang;
23
24bool QualType::isConstant(ASTContext &Ctx) const {
25  if (isConstQualified())
26    return true;
27
28  if (getTypePtr()->isArrayType())
29    return Ctx.getAsArrayType(*this)->getElementType().isConstant(Ctx);
30
31  return false;
32}
33
34void Type::Destroy(ASTContext& C) {
35  this->~Type();
36  C.Deallocate(this);
37}
38
39void VariableArrayType::Destroy(ASTContext& C) {
40  SizeExpr->Destroy(C);
41  this->~VariableArrayType();
42  C.Deallocate(this);
43}
44
45void DependentSizedArrayType::Destroy(ASTContext& C) {
46  SizeExpr->Destroy(C);
47  this->~DependentSizedArrayType();
48  C.Deallocate(this);
49}
50
51/// getArrayElementTypeNoTypeQual - If this is an array type, return the
52/// element type of the array, potentially with type qualifiers missing.
53/// This method should never be used when type qualifiers are meaningful.
54const Type *Type::getArrayElementTypeNoTypeQual() const {
55  // If this is directly an array type, return it.
56  if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
57    return ATy->getElementType().getTypePtr();
58
59  // If the canonical form of this type isn't the right kind, reject it.
60  if (!isa<ArrayType>(CanonicalType)) {
61    // Look through type qualifiers
62    if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
63      return AT->getElementType().getTypePtr();
64    return 0;
65  }
66
67  // If this is a typedef for an array type, strip the typedef off without
68  // losing all typedef information.
69  return getDesugaredType()->getArrayElementTypeNoTypeQual();
70}
71
72/// getDesugaredType - Return the specified type with any "sugar" removed from
73/// type type.  This takes off typedefs, typeof's etc.  If the outer level of
74/// the type is already concrete, it returns it unmodified.  This is similar
75/// to getting the canonical type, but it doesn't remove *all* typedefs.  For
76/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
77/// concrete.
78QualType Type::getDesugaredType() const {
79  if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
80    return TDT->LookThroughTypedefs();
81  if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this))
82    return TOE->getUnderlyingExpr()->getType();
83  if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
84    return TOT->getUnderlyingType();
85  // FIXME: remove this cast.
86  return QualType(const_cast<Type*>(this), 0);
87}
88
89/// isVoidType - Helper method to determine if this is the 'void' type.
90bool Type::isVoidType() const {
91  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
92    return BT->getKind() == BuiltinType::Void;
93  if (const ASQualType *AS = dyn_cast<ASQualType>(CanonicalType))
94    return AS->getBaseType()->isVoidType();
95  return false;
96}
97
98bool Type::isObjectType() const {
99  if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType))
100    return false;
101  if (const ASQualType *AS = dyn_cast<ASQualType>(CanonicalType))
102    return AS->getBaseType()->isObjectType();
103  return !CanonicalType->isIncompleteType();
104}
105
106bool Type::isDerivedType() const {
107  switch (CanonicalType->getTypeClass()) {
108  case ASQual:
109    return cast<ASQualType>(CanonicalType)->getBaseType()->isDerivedType();
110  case Pointer:
111  case VariableArray:
112  case ConstantArray:
113  case IncompleteArray:
114  case FunctionProto:
115  case FunctionNoProto:
116  case Reference:
117    return true;
118  case Tagged:
119    return !cast<TagType>(CanonicalType)->getDecl()->isEnum();
120  default:
121    return false;
122  }
123}
124
125bool Type::isClassType() const {
126  if (const RecordType *RT = getAsRecordType())
127    return RT->getDecl()->isClass();
128  return false;
129}
130bool Type::isStructureType() const {
131  if (const RecordType *RT = getAsRecordType())
132    return RT->getDecl()->isStruct();
133  return false;
134}
135bool Type::isUnionType() const {
136  if (const RecordType *RT = getAsRecordType())
137    return RT->getDecl()->isUnion();
138  return false;
139}
140
141bool Type::isComplexType() const {
142  if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
143    return CT->getElementType()->isFloatingType();
144  if (const ASQualType *AS = dyn_cast<ASQualType>(CanonicalType))
145    return AS->getBaseType()->isComplexType();
146  return false;
147}
148
149bool Type::isComplexIntegerType() const {
150  // Check for GCC complex integer extension.
151  if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
152    return CT->getElementType()->isIntegerType();
153  if (const ASQualType *AS = dyn_cast<ASQualType>(CanonicalType))
154    return AS->getBaseType()->isComplexIntegerType();
155  return false;
156}
157
158const ComplexType *Type::getAsComplexIntegerType() const {
159  // Are we directly a complex type?
160  if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
161    if (CTy->getElementType()->isIntegerType())
162      return CTy;
163    return 0;
164  }
165
166  // If the canonical form of this type isn't what we want, reject it.
167  if (!isa<ComplexType>(CanonicalType)) {
168    // Look through type qualifiers (e.g. ASQualType's).
169    if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
170      return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
171    return 0;
172  }
173
174  // If this is a typedef for a complex type, strip the typedef off without
175  // losing all typedef information.
176  return getDesugaredType()->getAsComplexIntegerType();
177}
178
179const BuiltinType *Type::getAsBuiltinType() const {
180  // If this is directly a builtin type, return it.
181  if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
182    return BTy;
183
184  // If the canonical form of this type isn't a builtin type, reject it.
185  if (!isa<BuiltinType>(CanonicalType)) {
186    // Look through type qualifiers (e.g. ASQualType's).
187    if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
188      return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
189    return 0;
190  }
191
192  // If this is a typedef for a builtin type, strip the typedef off without
193  // losing all typedef information.
194  return getDesugaredType()->getAsBuiltinType();
195}
196
197const FunctionType *Type::getAsFunctionType() const {
198  // If this is directly a function type, return it.
199  if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
200    return FTy;
201
202  // If the canonical form of this type isn't the right kind, reject it.
203  if (!isa<FunctionType>(CanonicalType)) {
204    // Look through type qualifiers
205    if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
206      return CanonicalType.getUnqualifiedType()->getAsFunctionType();
207    return 0;
208  }
209
210  // If this is a typedef for a function type, strip the typedef off without
211  // losing all typedef information.
212  return getDesugaredType()->getAsFunctionType();
213}
214
215const FunctionTypeProto *Type::getAsFunctionTypeProto() const {
216  return dyn_cast_or_null<FunctionTypeProto>(getAsFunctionType());
217}
218
219
220const PointerLikeType *Type::getAsPointerLikeType() const {
221  // If this is directly a pointer-like type, return it.
222  if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this))
223    return PTy;
224
225  // If the canonical form of this type isn't the right kind, reject it.
226  if (!isa<PointerLikeType>(CanonicalType)) {
227    // Look through type qualifiers
228    if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType()))
229      return CanonicalType.getUnqualifiedType()->getAsPointerLikeType();
230    return 0;
231  }
232
233  // If this is a typedef for a pointer type, strip the typedef off without
234  // losing all typedef information.
235  return getDesugaredType()->getAsPointerLikeType();
236}
237
238const PointerType *Type::getAsPointerType() const {
239  // If this is directly a pointer type, return it.
240  if (const PointerType *PTy = dyn_cast<PointerType>(this))
241    return PTy;
242
243  // If the canonical form of this type isn't the right kind, reject it.
244  if (!isa<PointerType>(CanonicalType)) {
245    // Look through type qualifiers
246    if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
247      return CanonicalType.getUnqualifiedType()->getAsPointerType();
248    return 0;
249  }
250
251  // If this is a typedef for a pointer type, strip the typedef off without
252  // losing all typedef information.
253  return getDesugaredType()->getAsPointerType();
254}
255
256const BlockPointerType *Type::getAsBlockPointerType() const {
257  // If this is directly a block pointer type, return it.
258  if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
259    return PTy;
260
261  // If the canonical form of this type isn't the right kind, reject it.
262  if (!isa<BlockPointerType>(CanonicalType)) {
263    // Look through type qualifiers
264    if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType()))
265      return CanonicalType.getUnqualifiedType()->getAsBlockPointerType();
266    return 0;
267  }
268
269  // If this is a typedef for a block pointer type, strip the typedef off
270  // without losing all typedef information.
271  return getDesugaredType()->getAsBlockPointerType();
272}
273
274const ReferenceType *Type::getAsReferenceType() const {
275  // If this is directly a reference type, return it.
276  if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
277    return RTy;
278
279  // If the canonical form of this type isn't the right kind, reject it.
280  if (!isa<ReferenceType>(CanonicalType)) {
281    // Look through type qualifiers
282    if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
283      return CanonicalType.getUnqualifiedType()->getAsReferenceType();
284    return 0;
285  }
286
287  // If this is a typedef for a reference type, strip the typedef off without
288  // losing all typedef information.
289  return getDesugaredType()->getAsReferenceType();
290}
291
292const MemberPointerType *Type::getAsMemberPointerType() const {
293  // If this is directly a member pointer type, return it.
294  if (const MemberPointerType *MTy = dyn_cast<MemberPointerType>(this))
295    return MTy;
296
297  // If the canonical form of this type isn't the right kind, reject it.
298  if (!isa<MemberPointerType>(CanonicalType)) {
299    // Look through type qualifiers
300    if (isa<MemberPointerType>(CanonicalType.getUnqualifiedType()))
301      return CanonicalType.getUnqualifiedType()->getAsMemberPointerType();
302    return 0;
303  }
304
305  // If this is a typedef for a member pointer type, strip the typedef off
306  // without losing all typedef information.
307  return getDesugaredType()->getAsMemberPointerType();
308}
309
310/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
311/// array types and types that contain variable array types in their
312/// declarator
313bool Type::isVariablyModifiedType() const {
314  // A VLA is a variably modified type.
315  if (isVariableArrayType())
316    return true;
317
318  // An array can contain a variably modified type
319  if (const Type *T = getArrayElementTypeNoTypeQual())
320    return T->isVariablyModifiedType();
321
322  // A pointer can point to a variably modified type.
323  // Also, C++ references and member pointers can point to a variably modified
324  // type, where VLAs appear as an extension to C++, and should be treated
325  // correctly.
326  if (const PointerLikeType *PT = getAsPointerLikeType())
327    return PT->getPointeeType()->isVariablyModifiedType();
328  if (const MemberPointerType *PT = getAsMemberPointerType())
329    return PT->getPointeeType()->isVariablyModifiedType();
330
331  // A function can return a variably modified type
332  // This one isn't completely obvious, but it follows from the
333  // definition in C99 6.7.5p3. Because of this rule, it's
334  // illegal to declare a function returning a variably modified type.
335  if (const FunctionType *FT = getAsFunctionType())
336    return FT->getResultType()->isVariablyModifiedType();
337
338  return false;
339}
340
341const RecordType *Type::getAsRecordType() const {
342  // If this is directly a reference type, return it.
343  if (const RecordType *RTy = dyn_cast<RecordType>(this))
344    return RTy;
345
346  // If the canonical form of this type isn't the right kind, reject it.
347  if (!isa<RecordType>(CanonicalType)) {
348    // Look through type qualifiers
349    if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
350      return CanonicalType.getUnqualifiedType()->getAsRecordType();
351    return 0;
352  }
353
354  // If this is a typedef for a record type, strip the typedef off without
355  // losing all typedef information.
356  return getDesugaredType()->getAsRecordType();
357}
358
359const RecordType *Type::getAsStructureType() const {
360  // If this is directly a structure type, return it.
361  if (const RecordType *RT = dyn_cast<RecordType>(this)) {
362    if (RT->getDecl()->isStruct())
363      return RT;
364  }
365
366  // If the canonical form of this type isn't the right kind, reject it.
367  if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
368    if (!RT->getDecl()->isStruct())
369      return 0;
370
371    // If this is a typedef for a structure type, strip the typedef off without
372    // losing all typedef information.
373    return getDesugaredType()->getAsStructureType();
374  }
375  // Look through type qualifiers
376  if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
377    return CanonicalType.getUnqualifiedType()->getAsStructureType();
378  return 0;
379}
380
381const RecordType *Type::getAsUnionType() const {
382  // If this is directly a union type, return it.
383  if (const RecordType *RT = dyn_cast<RecordType>(this)) {
384    if (RT->getDecl()->isUnion())
385      return RT;
386  }
387
388  // If the canonical form of this type isn't the right kind, reject it.
389  if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
390    if (!RT->getDecl()->isUnion())
391      return 0;
392
393    // If this is a typedef for a union type, strip the typedef off without
394    // losing all typedef information.
395    return getDesugaredType()->getAsUnionType();
396  }
397
398  // Look through type qualifiers
399  if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
400    return CanonicalType.getUnqualifiedType()->getAsUnionType();
401  return 0;
402}
403
404const EnumType *Type::getAsEnumType() const {
405  // Check the canonicalized unqualified type directly; the more complex
406  // version is unnecessary because there isn't any typedef information
407  // to preserve.
408  return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
409}
410
411const ComplexType *Type::getAsComplexType() const {
412  // Are we directly a complex type?
413  if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
414    return CTy;
415
416  // If the canonical form of this type isn't the right kind, reject it.
417  if (!isa<ComplexType>(CanonicalType)) {
418    // Look through type qualifiers
419    if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
420      return CanonicalType.getUnqualifiedType()->getAsComplexType();
421    return 0;
422  }
423
424  // If this is a typedef for a complex type, strip the typedef off without
425  // losing all typedef information.
426  return getDesugaredType()->getAsComplexType();
427}
428
429const VectorType *Type::getAsVectorType() const {
430  // Are we directly a vector type?
431  if (const VectorType *VTy = dyn_cast<VectorType>(this))
432    return VTy;
433
434  // If the canonical form of this type isn't the right kind, reject it.
435  if (!isa<VectorType>(CanonicalType)) {
436    // Look through type qualifiers
437    if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
438      return CanonicalType.getUnqualifiedType()->getAsVectorType();
439    return 0;
440  }
441
442  // If this is a typedef for a vector type, strip the typedef off without
443  // losing all typedef information.
444  return getDesugaredType()->getAsVectorType();
445}
446
447const ExtVectorType *Type::getAsExtVectorType() const {
448  // Are we directly an OpenCU vector type?
449  if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
450    return VTy;
451
452  // If the canonical form of this type isn't the right kind, reject it.
453  if (!isa<ExtVectorType>(CanonicalType)) {
454    // Look through type qualifiers
455    if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
456      return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
457    return 0;
458  }
459
460  // If this is a typedef for an extended vector type, strip the typedef off
461  // without losing all typedef information.
462  return getDesugaredType()->getAsExtVectorType();
463}
464
465const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
466  // There is no sugar for ObjCInterfaceType's, just return the canonical
467  // type pointer if it is the right class.  There is no typedef information to
468  // return and these cannot be Address-space qualified.
469  return dyn_cast<ObjCInterfaceType>(CanonicalType);
470}
471
472const ObjCQualifiedInterfaceType *
473Type::getAsObjCQualifiedInterfaceType() const {
474  // There is no sugar for ObjCQualifiedInterfaceType's, just return the
475  // canonical type pointer if it is the right class.
476  return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
477}
478
479const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
480  // There is no sugar for ObjCQualifiedIdType's, just return the canonical
481  // type pointer if it is the right class.
482  return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
483}
484
485const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
486  // There is no sugar for template type parameters, so just return
487  // the canonical type pointer if it is the right class.
488  // FIXME: can these be address-space qualified?
489  return dyn_cast<TemplateTypeParmType>(CanonicalType);
490}
491
492const ClassTemplateSpecializationType *
493Type::getClassTemplateSpecializationType() const {
494  // There is no sugar for class template specialization types, so
495  // just return the canonical type pointer if it is the right class.
496  return dyn_cast<ClassTemplateSpecializationType>(CanonicalType);
497}
498
499
500bool Type::isIntegerType() const {
501  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
502    return BT->getKind() >= BuiltinType::Bool &&
503           BT->getKind() <= BuiltinType::LongLong;
504  if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
505    // Incomplete enum types are not treated as integer types.
506    // FIXME: In C++, enum types are never integer types.
507    if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
508      return true;
509  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
510    return VT->getElementType()->isIntegerType();
511  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
512    return ASQT->getBaseType()->isIntegerType();
513  return false;
514}
515
516bool Type::isIntegralType() const {
517  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
518    return BT->getKind() >= BuiltinType::Bool &&
519    BT->getKind() <= BuiltinType::LongLong;
520  if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
521    if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
522      return true;  // Complete enum types are integral.
523                    // FIXME: In C++, enum types are never integral.
524  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
525    return ASQT->getBaseType()->isIntegralType();
526  return false;
527}
528
529bool Type::isEnumeralType() const {
530  if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
531    return TT->getDecl()->isEnum();
532  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
533    return ASQT->getBaseType()->isEnumeralType();
534  return false;
535}
536
537bool Type::isBooleanType() const {
538  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
539    return BT->getKind() == BuiltinType::Bool;
540  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
541    return ASQT->getBaseType()->isBooleanType();
542  return false;
543}
544
545bool Type::isCharType() const {
546  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
547    return BT->getKind() == BuiltinType::Char_U ||
548           BT->getKind() == BuiltinType::UChar ||
549           BT->getKind() == BuiltinType::Char_S ||
550           BT->getKind() == BuiltinType::SChar;
551  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
552    return ASQT->getBaseType()->isCharType();
553  return false;
554}
555
556bool Type::isWideCharType() const {
557  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
558    return BT->getKind() == BuiltinType::WChar;
559  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
560    return ASQT->getBaseType()->isWideCharType();
561  return false;
562}
563
564/// isSignedIntegerType - Return true if this is an integer type that is
565/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
566/// an enum decl which has a signed representation, or a vector of signed
567/// integer element type.
568bool Type::isSignedIntegerType() const {
569  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
570    return BT->getKind() >= BuiltinType::Char_S &&
571           BT->getKind() <= BuiltinType::LongLong;
572  }
573
574  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
575    return ET->getDecl()->getIntegerType()->isSignedIntegerType();
576
577  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
578    return VT->getElementType()->isSignedIntegerType();
579  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
580    return ASQT->getBaseType()->isSignedIntegerType();
581  return false;
582}
583
584/// isUnsignedIntegerType - Return true if this is an integer type that is
585/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
586/// decl which has an unsigned representation, or a vector of unsigned integer
587/// element type.
588bool Type::isUnsignedIntegerType() const {
589  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
590    return BT->getKind() >= BuiltinType::Bool &&
591           BT->getKind() <= BuiltinType::ULongLong;
592  }
593
594  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
595    return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
596
597  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
598    return VT->getElementType()->isUnsignedIntegerType();
599  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
600    return ASQT->getBaseType()->isUnsignedIntegerType();
601  return false;
602}
603
604bool Type::isFloatingType() const {
605  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
606    return BT->getKind() >= BuiltinType::Float &&
607           BT->getKind() <= BuiltinType::LongDouble;
608  if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
609    return CT->getElementType()->isFloatingType();
610  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
611    return VT->getElementType()->isFloatingType();
612  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
613    return ASQT->getBaseType()->isFloatingType();
614  return false;
615}
616
617bool Type::isRealFloatingType() const {
618  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
619    return BT->getKind() >= BuiltinType::Float &&
620           BT->getKind() <= BuiltinType::LongDouble;
621  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
622    return VT->getElementType()->isRealFloatingType();
623  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
624    return ASQT->getBaseType()->isRealFloatingType();
625  return false;
626}
627
628bool Type::isRealType() const {
629  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
630    return BT->getKind() >= BuiltinType::Bool &&
631           BT->getKind() <= BuiltinType::LongDouble;
632  if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
633    return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
634  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
635    return VT->getElementType()->isRealType();
636  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
637    return ASQT->getBaseType()->isRealType();
638  return false;
639}
640
641bool Type::isArithmeticType() const {
642  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
643    return BT->getKind() >= BuiltinType::Bool &&
644           BT->getKind() <= BuiltinType::LongDouble;
645  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
646    // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
647    // If a body isn't seen by the time we get here, return false.
648    return ET->getDecl()->isDefinition();
649  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
650    return ASQT->getBaseType()->isArithmeticType();
651  return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
652}
653
654bool Type::isScalarType() const {
655  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
656    return BT->getKind() != BuiltinType::Void;
657  if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
658    // Enums are scalar types, but only if they are defined.  Incomplete enums
659    // are not treated as scalar types.
660    if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
661      return true;
662    return false;
663  }
664  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
665    return ASQT->getBaseType()->isScalarType();
666  return isa<PointerType>(CanonicalType) ||
667         isa<BlockPointerType>(CanonicalType) ||
668         isa<MemberPointerType>(CanonicalType) ||
669         isa<ComplexType>(CanonicalType) ||
670         isa<ObjCQualifiedIdType>(CanonicalType);
671}
672
673/// \brief Determines whether the type is a C++ aggregate type or C
674/// aggregate or union type.
675///
676/// An aggregate type is an array or a class type (struct, union, or
677/// class) that has no user-declared constructors, no private or
678/// protected non-static data members, no base classes, and no virtual
679/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
680/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
681/// includes union types.
682bool Type::isAggregateType() const {
683  if (const CXXRecordType *CXXClassType = dyn_cast<CXXRecordType>(CanonicalType))
684    return CXXClassType->getDecl()->isAggregate();
685  if (isa<RecordType>(CanonicalType))
686    return true;
687  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
688    return ASQT->getBaseType()->isAggregateType();
689  return isa<ArrayType>(CanonicalType);
690}
691
692/// isConstantSizeType - Return true if this is not a variable sized type,
693/// according to the rules of C99 6.7.5p3.  It is not legal to call this on
694/// incomplete types or dependent types.
695bool Type::isConstantSizeType() const {
696  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
697    return ASQT->getBaseType()->isConstantSizeType();
698  assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
699  assert(!isDependentType() && "This doesn't make sense for dependent types");
700  // The VAT must have a size, as it is known to be complete.
701  return !isa<VariableArrayType>(CanonicalType);
702}
703
704/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
705/// - a type that can describe objects, but which lacks information needed to
706/// determine its size.
707bool Type::isIncompleteType() const {
708  switch (CanonicalType->getTypeClass()) {
709  default: return false;
710  case ASQual:
711    return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
712  case Builtin:
713    // Void is the only incomplete builtin type.  Per C99 6.2.5p19, it can never
714    // be completed.
715    return isVoidType();
716  case Tagged:
717    // A tagged type (struct/union/enum/class) is incomplete if the decl is a
718    // forward declaration, but not a full definition (C99 6.2.5p22).
719    return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
720  case IncompleteArray:
721    // An array of unknown size is an incomplete type (C99 6.2.5p22).
722    return true;
723  }
724}
725
726/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
727bool Type::isPODType() const {
728  // The compiler shouldn't query this for incomplete types, but the user might.
729  // We return false for that case.
730  if (isIncompleteType())
731    return false;
732
733  switch (CanonicalType->getTypeClass()) {
734    // Everything not explicitly mentioned is not POD.
735  default: return false;
736  case ASQual:
737    return cast<ASQualType>(CanonicalType)->getBaseType()->isPODType();
738  case VariableArray:
739  case ConstantArray:
740    // IncompleteArray is caught by isIncompleteType() above.
741    return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
742
743  case Builtin:
744  case Complex:
745  case Pointer:
746  case MemberPointer:
747  case Vector:
748  case ExtVector:
749  case ObjCQualifiedId:
750    return true;
751
752  case Tagged:
753    if (isEnumeralType())
754      return true;
755    if (CXXRecordDecl *RDecl = dyn_cast<CXXRecordDecl>(
756          cast<TagType>(CanonicalType)->getDecl()))
757      return RDecl->isPOD();
758    // C struct/union is POD.
759    return true;
760  }
761}
762
763bool Type::isPromotableIntegerType() const {
764  if (const BuiltinType *BT = getAsBuiltinType())
765    switch (BT->getKind()) {
766    case BuiltinType::Bool:
767    case BuiltinType::Char_S:
768    case BuiltinType::Char_U:
769    case BuiltinType::SChar:
770    case BuiltinType::UChar:
771    case BuiltinType::Short:
772    case BuiltinType::UShort:
773      return true;
774    default:
775      return false;
776    }
777  return false;
778}
779
780const char *BuiltinType::getName() const {
781  switch (getKind()) {
782  default: assert(0 && "Unknown builtin type!");
783  case Void:              return "void";
784  case Bool:              return "_Bool";
785  case Char_S:            return "char";
786  case Char_U:            return "char";
787  case SChar:             return "signed char";
788  case Short:             return "short";
789  case Int:               return "int";
790  case Long:              return "long";
791  case LongLong:          return "long long";
792  case UChar:             return "unsigned char";
793  case UShort:            return "unsigned short";
794  case UInt:              return "unsigned int";
795  case ULong:             return "unsigned long";
796  case ULongLong:         return "unsigned long long";
797  case Float:             return "float";
798  case Double:            return "double";
799  case LongDouble:        return "long double";
800  case WChar:             return "wchar_t";
801  case Overload:          return "<overloaded function type>";
802  case Dependent:         return "<dependent type>";
803  }
804}
805
806void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
807                                arg_type_iterator ArgTys,
808                                unsigned NumArgs, bool isVariadic,
809                                unsigned TypeQuals) {
810  ID.AddPointer(Result.getAsOpaquePtr());
811  for (unsigned i = 0; i != NumArgs; ++i)
812    ID.AddPointer(ArgTys[i].getAsOpaquePtr());
813  ID.AddInteger(isVariadic);
814  ID.AddInteger(TypeQuals);
815}
816
817void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
818  Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
819          getTypeQuals());
820}
821
822void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
823                                         const ObjCInterfaceDecl *Decl,
824                                         ObjCProtocolDecl **protocols,
825                                         unsigned NumProtocols) {
826  ID.AddPointer(Decl);
827  for (unsigned i = 0; i != NumProtocols; i++)
828    ID.AddPointer(protocols[i]);
829}
830
831void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
832  Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
833}
834
835void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
836                                  ObjCProtocolDecl **protocols,
837                                  unsigned NumProtocols) {
838  for (unsigned i = 0; i != NumProtocols; i++)
839    ID.AddPointer(protocols[i]);
840}
841
842void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
843  Profile(ID, &Protocols[0], getNumProtocols());
844}
845
846/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
847/// potentially looking through *all* consequtive typedefs.  This returns the
848/// sum of the type qualifiers, so if you have:
849///   typedef const int A;
850///   typedef volatile A B;
851/// looking through the typedefs for B will give you "const volatile A".
852///
853QualType TypedefType::LookThroughTypedefs() const {
854  // Usually, there is only a single level of typedefs, be fast in that case.
855  QualType FirstType = getDecl()->getUnderlyingType();
856  if (!isa<TypedefType>(FirstType))
857    return FirstType;
858
859  // Otherwise, do the fully general loop.
860  unsigned TypeQuals = 0;
861  const TypedefType *TDT = this;
862  while (1) {
863    QualType CurType = TDT->getDecl()->getUnderlyingType();
864
865
866    /// FIXME:
867    /// FIXME: This is incorrect for ASQuals!
868    /// FIXME:
869    TypeQuals |= CurType.getCVRQualifiers();
870
871    TDT = dyn_cast<TypedefType>(CurType);
872    if (TDT == 0)
873      return QualType(CurType.getTypePtr(), TypeQuals);
874  }
875}
876
877TypeOfExpr::TypeOfExpr(Expr *E, QualType can)
878  : Type(TypeOfExp, can, E->isTypeDependent()), TOExpr(E) {
879  assert(!isa<TypedefType>(can) && "Invalid canonical type");
880}
881
882bool RecordType::classof(const TagType *TT) {
883  return isa<RecordDecl>(TT->getDecl());
884}
885
886bool CXXRecordType::classof(const TagType *TT) {
887  return isa<CXXRecordDecl>(TT->getDecl());
888}
889
890bool EnumType::classof(const TagType *TT) {
891  return isa<EnumDecl>(TT->getDecl());
892}
893
894void
895ClassTemplateSpecializationType::
896packBooleanValues(unsigned NumArgs, bool *Values, uintptr_t *Words) {
897  const unsigned BitsPerWord = sizeof(uintptr_t) * CHAR_BIT;
898
899  for (unsigned PW = 0, NumPackedWords = getNumPackedWords(NumArgs), Arg = 0;
900       PW != NumPackedWords; ++PW) {
901    uintptr_t Word = 0;
902    for (unsigned Bit = 0; Bit < BitsPerWord && Arg < NumArgs; ++Bit, ++Arg) {
903      Word <<= 1;
904      Word |= Values[Arg];
905    }
906    Words[PW] = Word;
907  }
908}
909
910ClassTemplateSpecializationType::
911ClassTemplateSpecializationType(TemplateDecl *T, unsigned NumArgs,
912                                uintptr_t *Args, bool *ArgIsType,
913                                QualType Canon)
914  : Type(ClassTemplateSpecialization, Canon, /*FIXME:Dependent=*/false),
915    Template(T), NumArgs(NumArgs)
916{
917  uintptr_t *Data = reinterpret_cast<uintptr_t *>(this + 1);
918
919  // Pack the argument-is-type values into the words just after the
920  // class template specialization type.
921  packBooleanValues(NumArgs, ArgIsType, Data);
922
923  // Copy the template arguments after the packed words.
924  Data += getNumPackedWords(NumArgs);
925  for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
926    Data[Arg] = Args[Arg];
927}
928
929void ClassTemplateSpecializationType::Destroy(ASTContext& C) {
930  for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
931    if (!isArgType(Arg))
932      getArgAsExpr(Arg)->Destroy(C);
933}
934
935uintptr_t
936ClassTemplateSpecializationType::getArgAsOpaqueValue(unsigned Arg) const {
937  const uintptr_t *Data = reinterpret_cast<const uintptr_t *>(this + 1);
938  Data += getNumPackedWords(NumArgs);
939  return Data[Arg];
940}
941
942bool ClassTemplateSpecializationType::isArgType(unsigned Arg) const {
943  const unsigned BitsPerWord = sizeof(uintptr_t) * CHAR_BIT;
944  const uintptr_t *Data = reinterpret_cast<const uintptr_t *>(this + 1);
945  Data += Arg / BitsPerWord;
946  return (*Data >> (Arg % BitsPerWord)) & 0x01;
947}
948
949//===----------------------------------------------------------------------===//
950// Type Printing
951//===----------------------------------------------------------------------===//
952
953void QualType::dump(const char *msg) const {
954  std::string R = "identifier";
955  getAsStringInternal(R);
956  if (msg)
957    fprintf(stderr, "%s: %s\n", msg, R.c_str());
958  else
959    fprintf(stderr, "%s\n", R.c_str());
960}
961void QualType::dump() const {
962  dump("");
963}
964
965void Type::dump() const {
966  std::string S = "identifier";
967  getAsStringInternal(S);
968  fprintf(stderr, "%s\n", S.c_str());
969}
970
971
972
973static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
974  // Note: funkiness to ensure we get a space only between quals.
975  bool NonePrinted = true;
976  if (TypeQuals & QualType::Const)
977    S += "const", NonePrinted = false;
978  if (TypeQuals & QualType::Volatile)
979    S += (NonePrinted+" volatile"), NonePrinted = false;
980  if (TypeQuals & QualType::Restrict)
981    S += (NonePrinted+" restrict"), NonePrinted = false;
982}
983
984void QualType::getAsStringInternal(std::string &S) const {
985  if (isNull()) {
986    S += "NULL TYPE";
987    return;
988  }
989
990  // Print qualifiers as appropriate.
991  if (unsigned Tq = getCVRQualifiers()) {
992    std::string TQS;
993    AppendTypeQualList(TQS, Tq);
994    if (!S.empty())
995      S = TQS + ' ' + S;
996    else
997      S = TQS;
998  }
999
1000  getTypePtr()->getAsStringInternal(S);
1001}
1002
1003void BuiltinType::getAsStringInternal(std::string &S) const {
1004  if (S.empty()) {
1005    S = getName();
1006  } else {
1007    // Prefix the basic type, e.g. 'int X'.
1008    S = ' ' + S;
1009    S = getName() + S;
1010  }
1011}
1012
1013void ComplexType::getAsStringInternal(std::string &S) const {
1014  ElementType->getAsStringInternal(S);
1015  S = "_Complex " + S;
1016}
1017
1018void ASQualType::getAsStringInternal(std::string &S) const {
1019  S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
1020  BaseType->getAsStringInternal(S);
1021}
1022
1023void PointerType::getAsStringInternal(std::string &S) const {
1024  S = '*' + S;
1025
1026  // Handle things like 'int (*A)[4];' correctly.
1027  // FIXME: this should include vectors, but vectors use attributes I guess.
1028  if (isa<ArrayType>(getPointeeType()))
1029    S = '(' + S + ')';
1030
1031  getPointeeType().getAsStringInternal(S);
1032}
1033
1034void BlockPointerType::getAsStringInternal(std::string &S) const {
1035  S = '^' + S;
1036  PointeeType.getAsStringInternal(S);
1037}
1038
1039void ReferenceType::getAsStringInternal(std::string &S) const {
1040  S = '&' + S;
1041
1042  // Handle things like 'int (&A)[4];' correctly.
1043  // FIXME: this should include vectors, but vectors use attributes I guess.
1044  if (isa<ArrayType>(getPointeeType()))
1045    S = '(' + S + ')';
1046
1047  getPointeeType().getAsStringInternal(S);
1048}
1049
1050void MemberPointerType::getAsStringInternal(std::string &S) const {
1051  std::string C;
1052  Class->getAsStringInternal(C);
1053  C += "::*";
1054  S = C + S;
1055
1056  // Handle things like 'int (&A)[4];' correctly.
1057  // FIXME: this should include vectors, but vectors use attributes I guess.
1058  if (isa<ArrayType>(getPointeeType()))
1059    S = '(' + S + ')';
1060
1061  getPointeeType().getAsStringInternal(S);
1062}
1063
1064void ConstantArrayType::getAsStringInternal(std::string &S) const {
1065  S += '[';
1066  S += llvm::utostr(getSize().getZExtValue());
1067  S += ']';
1068
1069  getElementType().getAsStringInternal(S);
1070}
1071
1072void IncompleteArrayType::getAsStringInternal(std::string &S) const {
1073  S += "[]";
1074
1075  getElementType().getAsStringInternal(S);
1076}
1077
1078void VariableArrayType::getAsStringInternal(std::string &S) const {
1079  S += '[';
1080
1081  if (getIndexTypeQualifier()) {
1082    AppendTypeQualList(S, getIndexTypeQualifier());
1083    S += ' ';
1084  }
1085
1086  if (getSizeModifier() == Static)
1087    S += "static";
1088  else if (getSizeModifier() == Star)
1089    S += '*';
1090
1091  if (getSizeExpr()) {
1092    std::string SStr;
1093    llvm::raw_string_ostream s(SStr);
1094    getSizeExpr()->printPretty(s);
1095    S += s.str();
1096  }
1097  S += ']';
1098
1099  getElementType().getAsStringInternal(S);
1100}
1101
1102void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
1103  S += '[';
1104
1105  if (getIndexTypeQualifier()) {
1106    AppendTypeQualList(S, getIndexTypeQualifier());
1107    S += ' ';
1108  }
1109
1110  if (getSizeModifier() == Static)
1111    S += "static";
1112  else if (getSizeModifier() == Star)
1113    S += '*';
1114
1115  if (getSizeExpr()) {
1116    std::string SStr;
1117    llvm::raw_string_ostream s(SStr);
1118    getSizeExpr()->printPretty(s);
1119    S += s.str();
1120  }
1121  S += ']';
1122
1123  getElementType().getAsStringInternal(S);
1124}
1125
1126void VectorType::getAsStringInternal(std::string &S) const {
1127  // FIXME: We prefer to print the size directly here, but have no way
1128  // to get the size of the type.
1129  S += " __attribute__((__vector_size__(";
1130  S += llvm::utostr_32(NumElements); // convert back to bytes.
1131  S += " * sizeof(" + ElementType.getAsString() + "))))";
1132}
1133
1134void ExtVectorType::getAsStringInternal(std::string &S) const {
1135  S += " __attribute__((ext_vector_type(";
1136  S += llvm::utostr_32(NumElements);
1137  S += ")))";
1138  ElementType.getAsStringInternal(S);
1139}
1140
1141void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
1142  if (!InnerString.empty())    // Prefix the basic type, e.g. 'typeof(e) X'.
1143    InnerString = ' ' + InnerString;
1144  std::string Str;
1145  llvm::raw_string_ostream s(Str);
1146  getUnderlyingExpr()->printPretty(s);
1147  InnerString = "typeof(" + s.str() + ")" + InnerString;
1148}
1149
1150void TypeOfType::getAsStringInternal(std::string &InnerString) const {
1151  if (!InnerString.empty())    // Prefix the basic type, e.g. 'typeof(t) X'.
1152    InnerString = ' ' + InnerString;
1153  std::string Tmp;
1154  getUnderlyingType().getAsStringInternal(Tmp);
1155  InnerString = "typeof(" + Tmp + ")" + InnerString;
1156}
1157
1158void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
1159  // If needed for precedence reasons, wrap the inner part in grouping parens.
1160  if (!S.empty())
1161    S = "(" + S + ")";
1162
1163  S += "()";
1164  getResultType().getAsStringInternal(S);
1165}
1166
1167void FunctionTypeProto::getAsStringInternal(std::string &S) const {
1168  // If needed for precedence reasons, wrap the inner part in grouping parens.
1169  if (!S.empty())
1170    S = "(" + S + ")";
1171
1172  S += "(";
1173  std::string Tmp;
1174  for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1175    if (i) S += ", ";
1176    getArgType(i).getAsStringInternal(Tmp);
1177    S += Tmp;
1178    Tmp.clear();
1179  }
1180
1181  if (isVariadic()) {
1182    if (getNumArgs())
1183      S += ", ";
1184    S += "...";
1185  } else if (getNumArgs() == 0) {
1186    // Do not emit int() if we have a proto, emit 'int(void)'.
1187    S += "void";
1188  }
1189
1190  S += ")";
1191  getResultType().getAsStringInternal(S);
1192}
1193
1194
1195void TypedefType::getAsStringInternal(std::string &InnerString) const {
1196  if (!InnerString.empty())    // Prefix the basic type, e.g. 'typedefname X'.
1197    InnerString = ' ' + InnerString;
1198  InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1199}
1200
1201void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1202  if (!InnerString.empty())    // Prefix the basic type, e.g. 'parmname X'.
1203    InnerString = ' ' + InnerString;
1204
1205  if (!Name)
1206    InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1207      llvm::utostr_32(Index) + InnerString;
1208  else
1209    InnerString = Name->getName() + InnerString;
1210}
1211
1212void
1213ClassTemplateSpecializationType::
1214getAsStringInternal(std::string &InnerString) const {
1215  std::string SpecString = Template->getNameAsString();
1216  SpecString += '<';
1217  for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1218    if (Arg)
1219      SpecString += ", ";
1220
1221    // Print the argument into a string.
1222    std::string ArgString;
1223    if (isArgType(Arg))
1224      getArgAsType(Arg).getAsStringInternal(ArgString);
1225    else {
1226      llvm::raw_string_ostream s(ArgString);
1227      getArgAsExpr(Arg)->printPretty(s);
1228    }
1229
1230    // If this is the first argument and its string representation
1231    // begins with the global scope specifier ('::foo'), add a space
1232    // to avoid printing the diagraph '<:'.
1233    if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1234      SpecString += ' ';
1235
1236    SpecString += ArgString;
1237  }
1238
1239  // If the last character of our string is '>', add another space to
1240  // keep the two '>''s separate tokens. We don't *have* to do this in
1241  // C++0x, but it's still good hygiene.
1242  if (SpecString[SpecString.size() - 1] == '>')
1243    SpecString += ' ';
1244
1245  SpecString += '>';
1246
1247  if (InnerString.empty())
1248    InnerString.swap(SpecString);
1249  else
1250    InnerString = SpecString + ' ' + InnerString;
1251}
1252
1253void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
1254  if (!InnerString.empty())    // Prefix the basic type, e.g. 'typedefname X'.
1255    InnerString = ' ' + InnerString;
1256  InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1257}
1258
1259void ObjCQualifiedInterfaceType::getAsStringInternal(
1260                                  std::string &InnerString) const {
1261  if (!InnerString.empty())    // Prefix the basic type, e.g. 'typedefname X'.
1262    InnerString = ' ' + InnerString;
1263  std::string ObjCQIString = getDecl()->getNameAsString();
1264  ObjCQIString += '<';
1265  bool isFirst = true;
1266  for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1267    if (isFirst)
1268      isFirst = false;
1269    else
1270      ObjCQIString += ',';
1271    ObjCQIString += (*I)->getNameAsString();
1272  }
1273  ObjCQIString += '>';
1274  InnerString = ObjCQIString + InnerString;
1275}
1276
1277void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
1278  if (!InnerString.empty())    // Prefix the basic type, e.g. 'typedefname X'.
1279    InnerString = ' ' + InnerString;
1280  std::string ObjCQIString = "id";
1281  ObjCQIString += '<';
1282  int num = getNumProtocols();
1283  for (int i = 0; i < num; i++) {
1284    ObjCQIString += getProtocols(i)->getNameAsString();
1285    if (i < num-1)
1286      ObjCQIString += ',';
1287  }
1288  ObjCQIString += '>';
1289  InnerString = ObjCQIString + InnerString;
1290}
1291
1292void TagType::getAsStringInternal(std::string &InnerString) const {
1293  if (!InnerString.empty())    // Prefix the basic type, e.g. 'typedefname X'.
1294    InnerString = ' ' + InnerString;
1295
1296  const char *Kind = getDecl()->getKindName();
1297  const char *ID;
1298  if (const IdentifierInfo *II = getDecl()->getIdentifier())
1299    ID = II->getName();
1300  else
1301    ID = "<anonymous>";
1302
1303  InnerString = std::string(Kind) + " " + ID + InnerString;
1304}
1305