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