1 //===--- TypePrinter.cpp - Pretty-Print Clang Types -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This contains code to print types from Clang's type system.
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/PrettyPrinter.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclObjC.h"
18 #include "clang/AST/DeclTemplate.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/Type.h"
21 #include "clang/Basic/LangOptions.h"
22 #include "clang/Basic/SourceManager.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/Support/SaveAndRestore.h"
26 #include "llvm/Support/raw_ostream.h"
27 using namespace clang;
30 /// \brief RAII object that enables printing of the ARC __strong lifetime
32 class IncludeStrongLifetimeRAII {
33 PrintingPolicy &Policy;
37 explicit IncludeStrongLifetimeRAII(PrintingPolicy &Policy)
38 : Policy(Policy), Old(Policy.SuppressStrongLifetime) {
39 if (!Policy.SuppressLifetimeQualifiers)
40 Policy.SuppressStrongLifetime = false;
43 ~IncludeStrongLifetimeRAII() {
44 Policy.SuppressStrongLifetime = Old;
48 class ParamPolicyRAII {
49 PrintingPolicy &Policy;
53 explicit ParamPolicyRAII(PrintingPolicy &Policy)
54 : Policy(Policy), Old(Policy.SuppressSpecifiers) {
55 Policy.SuppressSpecifiers = false;
59 Policy.SuppressSpecifiers = Old;
63 class ElaboratedTypePolicyRAII {
64 PrintingPolicy &Policy;
65 bool SuppressTagKeyword;
69 explicit ElaboratedTypePolicyRAII(PrintingPolicy &Policy) : Policy(Policy) {
70 SuppressTagKeyword = Policy.SuppressTagKeyword;
71 SuppressScope = Policy.SuppressScope;
72 Policy.SuppressTagKeyword = true;
73 Policy.SuppressScope = true;
76 ~ElaboratedTypePolicyRAII() {
77 Policy.SuppressTagKeyword = SuppressTagKeyword;
78 Policy.SuppressScope = SuppressScope;
83 PrintingPolicy Policy;
84 bool HasEmptyPlaceHolder;
85 bool InsideCCAttribute;
88 explicit TypePrinter(const PrintingPolicy &Policy)
89 : Policy(Policy), HasEmptyPlaceHolder(false), InsideCCAttribute(false) { }
91 void print(const Type *ty, Qualifiers qs, raw_ostream &OS,
92 StringRef PlaceHolder);
93 void print(QualType T, raw_ostream &OS, StringRef PlaceHolder);
95 static bool canPrefixQualifiers(const Type *T, bool &NeedARCStrongQualifier);
96 void spaceBeforePlaceHolder(raw_ostream &OS);
97 void printTypeSpec(const NamedDecl *D, raw_ostream &OS);
99 void printBefore(const Type *ty, Qualifiers qs, raw_ostream &OS);
100 void printBefore(QualType T, raw_ostream &OS);
101 void printAfter(const Type *ty, Qualifiers qs, raw_ostream &OS);
102 void printAfter(QualType T, raw_ostream &OS);
103 void AppendScope(DeclContext *DC, raw_ostream &OS);
104 void printTag(TagDecl *T, raw_ostream &OS);
105 #define ABSTRACT_TYPE(CLASS, PARENT)
106 #define TYPE(CLASS, PARENT) \
107 void print##CLASS##Before(const CLASS##Type *T, raw_ostream &OS); \
108 void print##CLASS##After(const CLASS##Type *T, raw_ostream &OS);
109 #include "clang/AST/TypeNodes.def"
113 static void AppendTypeQualList(raw_ostream &OS, unsigned TypeQuals, bool C99) {
114 bool appendSpace = false;
115 if (TypeQuals & Qualifiers::Const) {
119 if (TypeQuals & Qualifiers::Volatile) {
120 if (appendSpace) OS << ' ';
124 if (TypeQuals & Qualifiers::Restrict) {
125 if (appendSpace) OS << ' ';
134 void TypePrinter::spaceBeforePlaceHolder(raw_ostream &OS) {
135 if (!HasEmptyPlaceHolder)
139 void TypePrinter::print(QualType t, raw_ostream &OS, StringRef PlaceHolder) {
140 SplitQualType split = t.split();
141 print(split.Ty, split.Quals, OS, PlaceHolder);
144 void TypePrinter::print(const Type *T, Qualifiers Quals, raw_ostream &OS,
145 StringRef PlaceHolder) {
151 SaveAndRestore<bool> PHVal(HasEmptyPlaceHolder, PlaceHolder.empty());
153 printBefore(T, Quals, OS);
155 printAfter(T, Quals, OS);
158 bool TypePrinter::canPrefixQualifiers(const Type *T,
159 bool &NeedARCStrongQualifier) {
160 // CanPrefixQualifiers - We prefer to print type qualifiers before the type,
161 // so that we get "const int" instead of "int const", but we can't do this if
162 // the type is complex. For example if the type is "int*", we *must* print
163 // "int * const", printing "const int *" is different. Only do this when the
164 // type expands to a simple string.
165 bool CanPrefixQualifiers = false;
166 NeedARCStrongQualifier = false;
167 Type::TypeClass TC = T->getTypeClass();
168 if (const AutoType *AT = dyn_cast<AutoType>(T))
169 TC = AT->desugar()->getTypeClass();
170 if (const SubstTemplateTypeParmType *Subst
171 = dyn_cast<SubstTemplateTypeParmType>(T))
172 TC = Subst->getReplacementType()->getTypeClass();
178 case Type::UnresolvedUsing:
180 case Type::TypeOfExpr:
183 case Type::UnaryTransform:
186 case Type::Elaborated:
187 case Type::TemplateTypeParm:
188 case Type::SubstTemplateTypeParmPack:
189 case Type::TemplateSpecialization:
190 case Type::InjectedClassName:
191 case Type::DependentName:
192 case Type::DependentTemplateSpecialization:
193 case Type::ObjCObject:
194 case Type::ObjCInterface:
197 CanPrefixQualifiers = true;
200 case Type::ObjCObjectPointer:
201 CanPrefixQualifiers = T->isObjCIdType() || T->isObjCClassType() ||
202 T->isObjCQualifiedIdType() || T->isObjCQualifiedClassType();
205 case Type::ConstantArray:
206 case Type::IncompleteArray:
207 case Type::VariableArray:
208 case Type::DependentSizedArray:
209 NeedARCStrongQualifier = true;
215 case Type::BlockPointer:
216 case Type::LValueReference:
217 case Type::RValueReference:
218 case Type::MemberPointer:
219 case Type::DependentSizedExtVector:
221 case Type::ExtVector:
222 case Type::FunctionProto:
223 case Type::FunctionNoProto:
225 case Type::Attributed:
226 case Type::PackExpansion:
227 case Type::SubstTemplateTypeParm:
228 CanPrefixQualifiers = false;
232 return CanPrefixQualifiers;
235 void TypePrinter::printBefore(QualType T, raw_ostream &OS) {
236 SplitQualType Split = T.split();
238 // If we have cv1 T, where T is substituted for cv2 U, only print cv1 - cv2
240 Qualifiers Quals = Split.Quals;
241 if (const SubstTemplateTypeParmType *Subst =
242 dyn_cast<SubstTemplateTypeParmType>(Split.Ty))
243 Quals -= QualType(Subst, 0).getQualifiers();
245 printBefore(Split.Ty, Quals, OS);
248 /// \brief Prints the part of the type string before an identifier, e.g. for
249 /// "int foo[10]" it prints "int ".
250 void TypePrinter::printBefore(const Type *T,Qualifiers Quals, raw_ostream &OS) {
251 if (Policy.SuppressSpecifiers && T->isSpecifierType())
254 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder);
256 // Print qualifiers as appropriate.
258 bool CanPrefixQualifiers = false;
259 bool NeedARCStrongQualifier = false;
260 CanPrefixQualifiers = canPrefixQualifiers(T, NeedARCStrongQualifier);
262 if (CanPrefixQualifiers && !Quals.empty()) {
263 if (NeedARCStrongQualifier) {
264 IncludeStrongLifetimeRAII Strong(Policy);
265 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/true);
267 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/true);
271 bool hasAfterQuals = false;
272 if (!CanPrefixQualifiers && !Quals.empty()) {
273 hasAfterQuals = !Quals.isEmptyWhenPrinted(Policy);
275 HasEmptyPlaceHolder = false;
278 switch (T->getTypeClass()) {
279 #define ABSTRACT_TYPE(CLASS, PARENT)
280 #define TYPE(CLASS, PARENT) case Type::CLASS: \
281 print##CLASS##Before(cast<CLASS##Type>(T), OS); \
283 #include "clang/AST/TypeNodes.def"
287 if (NeedARCStrongQualifier) {
288 IncludeStrongLifetimeRAII Strong(Policy);
289 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/!PrevPHIsEmpty.get());
291 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/!PrevPHIsEmpty.get());
296 void TypePrinter::printAfter(QualType t, raw_ostream &OS) {
297 SplitQualType split = t.split();
298 printAfter(split.Ty, split.Quals, OS);
301 /// \brief Prints the part of the type string after an identifier, e.g. for
302 /// "int foo[10]" it prints "[10]".
303 void TypePrinter::printAfter(const Type *T, Qualifiers Quals, raw_ostream &OS) {
304 switch (T->getTypeClass()) {
305 #define ABSTRACT_TYPE(CLASS, PARENT)
306 #define TYPE(CLASS, PARENT) case Type::CLASS: \
307 print##CLASS##After(cast<CLASS##Type>(T), OS); \
309 #include "clang/AST/TypeNodes.def"
313 void TypePrinter::printBuiltinBefore(const BuiltinType *T, raw_ostream &OS) {
314 OS << T->getName(Policy);
315 spaceBeforePlaceHolder(OS);
317 void TypePrinter::printBuiltinAfter(const BuiltinType *T, raw_ostream &OS) { }
319 void TypePrinter::printComplexBefore(const ComplexType *T, raw_ostream &OS) {
321 printBefore(T->getElementType(), OS);
323 void TypePrinter::printComplexAfter(const ComplexType *T, raw_ostream &OS) {
324 printAfter(T->getElementType(), OS);
327 void TypePrinter::printPointerBefore(const PointerType *T, raw_ostream &OS) {
328 IncludeStrongLifetimeRAII Strong(Policy);
329 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
330 printBefore(T->getPointeeType(), OS);
331 // Handle things like 'int (*A)[4];' correctly.
332 // FIXME: this should include vectors, but vectors use attributes I guess.
333 if (isa<ArrayType>(T->getPointeeType()))
337 void TypePrinter::printPointerAfter(const PointerType *T, raw_ostream &OS) {
338 IncludeStrongLifetimeRAII Strong(Policy);
339 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
340 // Handle things like 'int (*A)[4];' correctly.
341 // FIXME: this should include vectors, but vectors use attributes I guess.
342 if (isa<ArrayType>(T->getPointeeType()))
344 printAfter(T->getPointeeType(), OS);
347 void TypePrinter::printBlockPointerBefore(const BlockPointerType *T,
349 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
350 printBefore(T->getPointeeType(), OS);
353 void TypePrinter::printBlockPointerAfter(const BlockPointerType *T,
355 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
356 printAfter(T->getPointeeType(), OS);
359 void TypePrinter::printLValueReferenceBefore(const LValueReferenceType *T,
361 IncludeStrongLifetimeRAII Strong(Policy);
362 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
363 printBefore(T->getPointeeTypeAsWritten(), OS);
364 // Handle things like 'int (&A)[4];' correctly.
365 // FIXME: this should include vectors, but vectors use attributes I guess.
366 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
370 void TypePrinter::printLValueReferenceAfter(const LValueReferenceType *T,
372 IncludeStrongLifetimeRAII Strong(Policy);
373 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
374 // Handle things like 'int (&A)[4];' correctly.
375 // FIXME: this should include vectors, but vectors use attributes I guess.
376 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
378 printAfter(T->getPointeeTypeAsWritten(), OS);
381 void TypePrinter::printRValueReferenceBefore(const RValueReferenceType *T,
383 IncludeStrongLifetimeRAII Strong(Policy);
384 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
385 printBefore(T->getPointeeTypeAsWritten(), OS);
386 // Handle things like 'int (&&A)[4];' correctly.
387 // FIXME: this should include vectors, but vectors use attributes I guess.
388 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
392 void TypePrinter::printRValueReferenceAfter(const RValueReferenceType *T,
394 IncludeStrongLifetimeRAII Strong(Policy);
395 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
396 // Handle things like 'int (&&A)[4];' correctly.
397 // FIXME: this should include vectors, but vectors use attributes I guess.
398 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
400 printAfter(T->getPointeeTypeAsWritten(), OS);
403 void TypePrinter::printMemberPointerBefore(const MemberPointerType *T,
405 IncludeStrongLifetimeRAII Strong(Policy);
406 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
407 printBefore(T->getPointeeType(), OS);
408 // Handle things like 'int (Cls::*A)[4];' correctly.
409 // FIXME: this should include vectors, but vectors use attributes I guess.
410 if (isa<ArrayType>(T->getPointeeType()))
413 PrintingPolicy InnerPolicy(Policy);
414 InnerPolicy.SuppressTag = false;
415 TypePrinter(InnerPolicy).print(QualType(T->getClass(), 0), OS, StringRef());
419 void TypePrinter::printMemberPointerAfter(const MemberPointerType *T,
421 IncludeStrongLifetimeRAII Strong(Policy);
422 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
423 // Handle things like 'int (Cls::*A)[4];' correctly.
424 // FIXME: this should include vectors, but vectors use attributes I guess.
425 if (isa<ArrayType>(T->getPointeeType()))
427 printAfter(T->getPointeeType(), OS);
430 void TypePrinter::printConstantArrayBefore(const ConstantArrayType *T,
432 IncludeStrongLifetimeRAII Strong(Policy);
433 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
434 printBefore(T->getElementType(), OS);
436 void TypePrinter::printConstantArrayAfter(const ConstantArrayType *T,
439 if (T->getIndexTypeQualifiers().hasQualifiers()) {
440 AppendTypeQualList(OS, T->getIndexTypeCVRQualifiers(), Policy.LangOpts.C99);
444 if (T->getSizeModifier() == ArrayType::Static)
447 OS << T->getSize().getZExtValue() << ']';
448 printAfter(T->getElementType(), OS);
451 void TypePrinter::printIncompleteArrayBefore(const IncompleteArrayType *T,
453 IncludeStrongLifetimeRAII Strong(Policy);
454 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
455 printBefore(T->getElementType(), OS);
457 void TypePrinter::printIncompleteArrayAfter(const IncompleteArrayType *T,
460 printAfter(T->getElementType(), OS);
463 void TypePrinter::printVariableArrayBefore(const VariableArrayType *T,
465 IncludeStrongLifetimeRAII Strong(Policy);
466 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
467 printBefore(T->getElementType(), OS);
469 void TypePrinter::printVariableArrayAfter(const VariableArrayType *T,
472 if (T->getIndexTypeQualifiers().hasQualifiers()) {
473 AppendTypeQualList(OS, T->getIndexTypeCVRQualifiers(), Policy.LangOpts.C99);
477 if (T->getSizeModifier() == VariableArrayType::Static)
479 else if (T->getSizeModifier() == VariableArrayType::Star)
482 if (T->getSizeExpr())
483 T->getSizeExpr()->printPretty(OS, nullptr, Policy);
486 printAfter(T->getElementType(), OS);
489 void TypePrinter::printAdjustedBefore(const AdjustedType *T, raw_ostream &OS) {
490 // Print the adjusted representation, otherwise the adjustment will be
492 printBefore(T->getAdjustedType(), OS);
494 void TypePrinter::printAdjustedAfter(const AdjustedType *T, raw_ostream &OS) {
495 printAfter(T->getAdjustedType(), OS);
498 void TypePrinter::printDecayedBefore(const DecayedType *T, raw_ostream &OS) {
499 // Print as though it's a pointer.
500 printAdjustedBefore(T, OS);
502 void TypePrinter::printDecayedAfter(const DecayedType *T, raw_ostream &OS) {
503 printAdjustedAfter(T, OS);
506 void TypePrinter::printDependentSizedArrayBefore(
507 const DependentSizedArrayType *T,
509 IncludeStrongLifetimeRAII Strong(Policy);
510 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
511 printBefore(T->getElementType(), OS);
513 void TypePrinter::printDependentSizedArrayAfter(
514 const DependentSizedArrayType *T,
517 if (T->getSizeExpr())
518 T->getSizeExpr()->printPretty(OS, nullptr, Policy);
520 printAfter(T->getElementType(), OS);
523 void TypePrinter::printDependentSizedExtVectorBefore(
524 const DependentSizedExtVectorType *T,
526 printBefore(T->getElementType(), OS);
528 void TypePrinter::printDependentSizedExtVectorAfter(
529 const DependentSizedExtVectorType *T,
531 OS << " __attribute__((ext_vector_type(";
532 if (T->getSizeExpr())
533 T->getSizeExpr()->printPretty(OS, nullptr, Policy);
535 printAfter(T->getElementType(), OS);
538 void TypePrinter::printVectorBefore(const VectorType *T, raw_ostream &OS) {
539 switch (T->getVectorKind()) {
540 case VectorType::AltiVecPixel:
541 OS << "__vector __pixel ";
543 case VectorType::AltiVecBool:
544 OS << "__vector __bool ";
545 printBefore(T->getElementType(), OS);
547 case VectorType::AltiVecVector:
549 printBefore(T->getElementType(), OS);
551 case VectorType::NeonVector:
552 OS << "__attribute__((neon_vector_type("
553 << T->getNumElements() << "))) ";
554 printBefore(T->getElementType(), OS);
556 case VectorType::NeonPolyVector:
557 OS << "__attribute__((neon_polyvector_type(" <<
558 T->getNumElements() << "))) ";
559 printBefore(T->getElementType(), OS);
561 case VectorType::GenericVector: {
562 // FIXME: We prefer to print the size directly here, but have no way
563 // to get the size of the type.
564 OS << "__attribute__((__vector_size__("
565 << T->getNumElements()
567 print(T->getElementType(), OS, StringRef());
569 printBefore(T->getElementType(), OS);
574 void TypePrinter::printVectorAfter(const VectorType *T, raw_ostream &OS) {
575 printAfter(T->getElementType(), OS);
578 void TypePrinter::printExtVectorBefore(const ExtVectorType *T,
580 printBefore(T->getElementType(), OS);
582 void TypePrinter::printExtVectorAfter(const ExtVectorType *T, raw_ostream &OS) {
583 printAfter(T->getElementType(), OS);
584 OS << " __attribute__((ext_vector_type(";
585 OS << T->getNumElements();
590 FunctionProtoType::printExceptionSpecification(raw_ostream &OS,
591 const PrintingPolicy &Policy)
594 if (hasDynamicExceptionSpec()) {
596 if (getExceptionSpecType() == EST_MSAny)
599 for (unsigned I = 0, N = getNumExceptions(); I != N; ++I) {
603 OS << getExceptionType(I).stream(Policy);
606 } else if (isNoexceptExceptionSpec(getExceptionSpecType())) {
608 if (getExceptionSpecType() == EST_ComputedNoexcept) {
610 if (getNoexceptExpr())
611 getNoexceptExpr()->printPretty(OS, nullptr, Policy);
617 void TypePrinter::printFunctionProtoBefore(const FunctionProtoType *T,
619 if (T->hasTrailingReturn()) {
621 if (!HasEmptyPlaceHolder)
624 // If needed for precedence reasons, wrap the inner part in grouping parens.
625 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false);
626 printBefore(T->getReturnType(), OS);
627 if (!PrevPHIsEmpty.get())
632 void TypePrinter::printFunctionProtoAfter(const FunctionProtoType *T,
634 // If needed for precedence reasons, wrap the inner part in grouping parens.
635 if (!HasEmptyPlaceHolder)
637 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
641 ParamPolicyRAII ParamPolicy(Policy);
642 for (unsigned i = 0, e = T->getNumParams(); i != e; ++i) {
644 print(T->getParamType(i), OS, StringRef());
648 if (T->isVariadic()) {
649 if (T->getNumParams())
652 } else if (T->getNumParams() == 0 && !Policy.LangOpts.CPlusPlus) {
653 // Do not emit int() if we have a proto, emit 'int(void)'.
659 FunctionType::ExtInfo Info = T->getExtInfo();
661 if (!InsideCCAttribute) {
662 switch (Info.getCC()) {
664 // The C calling convention is the default on the vast majority of platforms
665 // we support. If the user wrote it explicitly, it will usually be printed
666 // while traversing the AttributedType. If the type has been desugared, let
667 // the canonical spelling be the implicit calling convention.
668 // FIXME: It would be better to be explicit in certain contexts, such as a
669 // cdecl function typedef used to declare a member function with the
670 // Microsoft C++ ABI.
673 OS << " __attribute__((stdcall))";
676 OS << " __attribute__((fastcall))";
679 OS << " __attribute__((thiscall))";
681 case CC_X86VectorCall:
682 OS << " __attribute__((vectorcall))";
685 OS << " __attribute__((pascal))";
688 OS << " __attribute__((pcs(\"aapcs\")))";
691 OS << " __attribute__((pcs(\"aapcs-vfp\")))";
693 case CC_IntelOclBicc:
694 OS << " __attribute__((intel_ocl_bicc))";
697 OS << " __attribute__((ms_abi))";
700 OS << " __attribute__((sysv_abi))";
702 case CC_SpirFunction:
704 // Do nothing. These CCs are not available as attributes.
709 if (Info.getNoReturn())
710 OS << " __attribute__((noreturn))";
711 if (Info.getRegParm())
712 OS << " __attribute__((regparm ("
713 << Info.getRegParm() << ")))";
715 if (unsigned quals = T->getTypeQuals()) {
717 AppendTypeQualList(OS, quals, Policy.LangOpts.C99);
720 switch (T->getRefQualifier()) {
732 T->printExceptionSpecification(OS, Policy);
734 if (T->hasTrailingReturn()) {
736 print(T->getReturnType(), OS, StringRef());
738 printAfter(T->getReturnType(), OS);
741 void TypePrinter::printFunctionNoProtoBefore(const FunctionNoProtoType *T,
743 // If needed for precedence reasons, wrap the inner part in grouping parens.
744 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false);
745 printBefore(T->getReturnType(), OS);
746 if (!PrevPHIsEmpty.get())
749 void TypePrinter::printFunctionNoProtoAfter(const FunctionNoProtoType *T,
751 // If needed for precedence reasons, wrap the inner part in grouping parens.
752 if (!HasEmptyPlaceHolder)
754 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
757 if (T->getNoReturnAttr())
758 OS << " __attribute__((noreturn))";
759 printAfter(T->getReturnType(), OS);
762 void TypePrinter::printTypeSpec(const NamedDecl *D, raw_ostream &OS) {
763 IdentifierInfo *II = D->getIdentifier();
765 spaceBeforePlaceHolder(OS);
768 void TypePrinter::printUnresolvedUsingBefore(const UnresolvedUsingType *T,
770 printTypeSpec(T->getDecl(), OS);
772 void TypePrinter::printUnresolvedUsingAfter(const UnresolvedUsingType *T,
775 void TypePrinter::printTypedefBefore(const TypedefType *T, raw_ostream &OS) {
776 printTypeSpec(T->getDecl(), OS);
778 void TypePrinter::printTypedefAfter(const TypedefType *T, raw_ostream &OS) { }
780 void TypePrinter::printTypeOfExprBefore(const TypeOfExprType *T,
783 if (T->getUnderlyingExpr())
784 T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy);
785 spaceBeforePlaceHolder(OS);
787 void TypePrinter::printTypeOfExprAfter(const TypeOfExprType *T,
790 void TypePrinter::printTypeOfBefore(const TypeOfType *T, raw_ostream &OS) {
792 print(T->getUnderlyingType(), OS, StringRef());
794 spaceBeforePlaceHolder(OS);
796 void TypePrinter::printTypeOfAfter(const TypeOfType *T, raw_ostream &OS) { }
798 void TypePrinter::printDecltypeBefore(const DecltypeType *T, raw_ostream &OS) {
800 if (T->getUnderlyingExpr())
801 T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy);
803 spaceBeforePlaceHolder(OS);
805 void TypePrinter::printDecltypeAfter(const DecltypeType *T, raw_ostream &OS) { }
807 void TypePrinter::printUnaryTransformBefore(const UnaryTransformType *T,
809 IncludeStrongLifetimeRAII Strong(Policy);
811 switch (T->getUTTKind()) {
812 case UnaryTransformType::EnumUnderlyingType:
813 OS << "__underlying_type(";
814 print(T->getBaseType(), OS, StringRef());
816 spaceBeforePlaceHolder(OS);
820 printBefore(T->getBaseType(), OS);
822 void TypePrinter::printUnaryTransformAfter(const UnaryTransformType *T,
824 IncludeStrongLifetimeRAII Strong(Policy);
826 switch (T->getUTTKind()) {
827 case UnaryTransformType::EnumUnderlyingType:
831 printAfter(T->getBaseType(), OS);
834 void TypePrinter::printAutoBefore(const AutoType *T, raw_ostream &OS) {
835 // If the type has been deduced, do not print 'auto'.
836 if (!T->getDeducedType().isNull()) {
837 printBefore(T->getDeducedType(), OS);
839 switch (T->getKeyword()) {
840 case AutoTypeKeyword::Auto: OS << "auto"; break;
841 case AutoTypeKeyword::DecltypeAuto: OS << "decltype(auto)"; break;
842 case AutoTypeKeyword::GNUAutoType: OS << "__auto_type"; break;
844 spaceBeforePlaceHolder(OS);
847 void TypePrinter::printAutoAfter(const AutoType *T, raw_ostream &OS) {
848 // If the type has been deduced, do not print 'auto'.
849 if (!T->getDeducedType().isNull())
850 printAfter(T->getDeducedType(), OS);
853 void TypePrinter::printAtomicBefore(const AtomicType *T, raw_ostream &OS) {
854 IncludeStrongLifetimeRAII Strong(Policy);
857 print(T->getValueType(), OS, StringRef());
859 spaceBeforePlaceHolder(OS);
861 void TypePrinter::printAtomicAfter(const AtomicType *T, raw_ostream &OS) { }
863 void TypePrinter::printPipeBefore(const PipeType *T, raw_ostream &OS) {
864 IncludeStrongLifetimeRAII Strong(Policy);
867 spaceBeforePlaceHolder(OS);
870 void TypePrinter::printPipeAfter(const PipeType *T, raw_ostream &OS) {
872 /// Appends the given scope to the end of a string.
873 void TypePrinter::AppendScope(DeclContext *DC, raw_ostream &OS) {
874 if (DC->isTranslationUnit()) return;
875 if (DC->isFunctionOrMethod()) return;
876 AppendScope(DC->getParent(), OS);
878 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
879 if (Policy.SuppressUnwrittenScope &&
880 (NS->isAnonymousNamespace() || NS->isInline()))
882 if (NS->getIdentifier())
883 OS << NS->getName() << "::";
885 OS << "(anonymous namespace)::";
886 } else if (ClassTemplateSpecializationDecl *Spec
887 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
888 IncludeStrongLifetimeRAII Strong(Policy);
889 OS << Spec->getIdentifier()->getName();
890 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
891 TemplateSpecializationType::PrintTemplateArgumentList(OS,
896 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
897 if (TypedefNameDecl *Typedef = Tag->getTypedefNameForAnonDecl())
898 OS << Typedef->getIdentifier()->getName() << "::";
899 else if (Tag->getIdentifier())
900 OS << Tag->getIdentifier()->getName() << "::";
906 void TypePrinter::printTag(TagDecl *D, raw_ostream &OS) {
907 if (Policy.SuppressTag)
910 bool HasKindDecoration = false;
912 // bool SuppressTagKeyword
913 // = Policy.LangOpts.CPlusPlus || Policy.SuppressTagKeyword;
915 // We don't print tags unless this is an elaborated type.
916 // In C, we just assume every RecordType is an elaborated type.
917 if (!(Policy.LangOpts.CPlusPlus || Policy.SuppressTagKeyword ||
918 D->getTypedefNameForAnonDecl())) {
919 HasKindDecoration = true;
920 OS << D->getKindName();
924 // Compute the full nested-name-specifier for this type.
925 // In C, this will always be empty except when the type
926 // being printed is anonymous within other Record.
927 if (!Policy.SuppressScope)
928 AppendScope(D->getDeclContext(), OS);
930 if (const IdentifierInfo *II = D->getIdentifier())
932 else if (TypedefNameDecl *Typedef = D->getTypedefNameForAnonDecl()) {
933 assert(Typedef->getIdentifier() && "Typedef without identifier?");
934 OS << Typedef->getIdentifier()->getName();
936 // Make an unambiguous representation for anonymous types, e.g.
937 // (anonymous enum at /usr/include/string.h:120:9)
938 OS << (Policy.MSVCFormatting ? '`' : '(');
940 if (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda()) {
942 HasKindDecoration = true;
947 if (Policy.AnonymousTagLocations) {
948 // Suppress the redundant tag keyword if we just printed one.
949 // We don't have to worry about ElaboratedTypes here because you can't
950 // refer to an anonymous type with one.
951 if (!HasKindDecoration)
952 OS << " " << D->getKindName();
954 PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc(
956 if (PLoc.isValid()) {
957 OS << " at " << PLoc.getFilename()
958 << ':' << PLoc.getLine()
959 << ':' << PLoc.getColumn();
963 OS << (Policy.MSVCFormatting ? '\'' : ')');
966 // If this is a class template specialization, print the template
968 if (ClassTemplateSpecializationDecl *Spec
969 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
970 const TemplateArgument *Args;
972 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
973 const TemplateSpecializationType *TST =
974 cast<TemplateSpecializationType>(TAW->getType());
975 Args = TST->getArgs();
976 NumArgs = TST->getNumArgs();
978 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
979 Args = TemplateArgs.data();
980 NumArgs = TemplateArgs.size();
982 IncludeStrongLifetimeRAII Strong(Policy);
983 TemplateSpecializationType::PrintTemplateArgumentList(OS,
988 spaceBeforePlaceHolder(OS);
991 void TypePrinter::printRecordBefore(const RecordType *T, raw_ostream &OS) {
992 printTag(T->getDecl(), OS);
994 void TypePrinter::printRecordAfter(const RecordType *T, raw_ostream &OS) { }
996 void TypePrinter::printEnumBefore(const EnumType *T, raw_ostream &OS) {
997 printTag(T->getDecl(), OS);
999 void TypePrinter::printEnumAfter(const EnumType *T, raw_ostream &OS) { }
1001 void TypePrinter::printTemplateTypeParmBefore(const TemplateTypeParmType *T,
1003 if (IdentifierInfo *Id = T->getIdentifier())
1004 OS << Id->getName();
1006 OS << "type-parameter-" << T->getDepth() << '-' << T->getIndex();
1007 spaceBeforePlaceHolder(OS);
1009 void TypePrinter::printTemplateTypeParmAfter(const TemplateTypeParmType *T,
1010 raw_ostream &OS) { }
1012 void TypePrinter::printSubstTemplateTypeParmBefore(
1013 const SubstTemplateTypeParmType *T,
1015 IncludeStrongLifetimeRAII Strong(Policy);
1016 printBefore(T->getReplacementType(), OS);
1018 void TypePrinter::printSubstTemplateTypeParmAfter(
1019 const SubstTemplateTypeParmType *T,
1021 IncludeStrongLifetimeRAII Strong(Policy);
1022 printAfter(T->getReplacementType(), OS);
1025 void TypePrinter::printSubstTemplateTypeParmPackBefore(
1026 const SubstTemplateTypeParmPackType *T,
1028 IncludeStrongLifetimeRAII Strong(Policy);
1029 printTemplateTypeParmBefore(T->getReplacedParameter(), OS);
1031 void TypePrinter::printSubstTemplateTypeParmPackAfter(
1032 const SubstTemplateTypeParmPackType *T,
1034 IncludeStrongLifetimeRAII Strong(Policy);
1035 printTemplateTypeParmAfter(T->getReplacedParameter(), OS);
1038 void TypePrinter::printTemplateSpecializationBefore(
1039 const TemplateSpecializationType *T,
1041 IncludeStrongLifetimeRAII Strong(Policy);
1042 T->getTemplateName().print(OS, Policy);
1044 TemplateSpecializationType::PrintTemplateArgumentList(OS,
1048 spaceBeforePlaceHolder(OS);
1050 void TypePrinter::printTemplateSpecializationAfter(
1051 const TemplateSpecializationType *T,
1052 raw_ostream &OS) { }
1054 void TypePrinter::printInjectedClassNameBefore(const InjectedClassNameType *T,
1056 printTemplateSpecializationBefore(T->getInjectedTST(), OS);
1058 void TypePrinter::printInjectedClassNameAfter(const InjectedClassNameType *T,
1059 raw_ostream &OS) { }
1061 void TypePrinter::printElaboratedBefore(const ElaboratedType *T,
1063 if (Policy.SuppressTag && isa<TagType>(T->getNamedType()))
1065 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1066 if (T->getKeyword() != ETK_None)
1068 NestedNameSpecifier* Qualifier = T->getQualifier();
1070 Qualifier->print(OS, Policy);
1072 ElaboratedTypePolicyRAII PolicyRAII(Policy);
1073 printBefore(T->getNamedType(), OS);
1075 void TypePrinter::printElaboratedAfter(const ElaboratedType *T,
1077 ElaboratedTypePolicyRAII PolicyRAII(Policy);
1078 printAfter(T->getNamedType(), OS);
1081 void TypePrinter::printParenBefore(const ParenType *T, raw_ostream &OS) {
1082 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) {
1083 printBefore(T->getInnerType(), OS);
1086 printBefore(T->getInnerType(), OS);
1088 void TypePrinter::printParenAfter(const ParenType *T, raw_ostream &OS) {
1089 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) {
1091 printAfter(T->getInnerType(), OS);
1093 printAfter(T->getInnerType(), OS);
1096 void TypePrinter::printDependentNameBefore(const DependentNameType *T,
1098 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1099 if (T->getKeyword() != ETK_None)
1102 T->getQualifier()->print(OS, Policy);
1104 OS << T->getIdentifier()->getName();
1105 spaceBeforePlaceHolder(OS);
1107 void TypePrinter::printDependentNameAfter(const DependentNameType *T,
1108 raw_ostream &OS) { }
1110 void TypePrinter::printDependentTemplateSpecializationBefore(
1111 const DependentTemplateSpecializationType *T, raw_ostream &OS) {
1112 IncludeStrongLifetimeRAII Strong(Policy);
1114 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1115 if (T->getKeyword() != ETK_None)
1118 if (T->getQualifier())
1119 T->getQualifier()->print(OS, Policy);
1120 OS << T->getIdentifier()->getName();
1121 TemplateSpecializationType::PrintTemplateArgumentList(OS,
1125 spaceBeforePlaceHolder(OS);
1127 void TypePrinter::printDependentTemplateSpecializationAfter(
1128 const DependentTemplateSpecializationType *T, raw_ostream &OS) { }
1130 void TypePrinter::printPackExpansionBefore(const PackExpansionType *T,
1132 printBefore(T->getPattern(), OS);
1134 void TypePrinter::printPackExpansionAfter(const PackExpansionType *T,
1136 printAfter(T->getPattern(), OS);
1140 void TypePrinter::printAttributedBefore(const AttributedType *T,
1142 // Prefer the macro forms of the GC and ownership qualifiers.
1143 if (T->getAttrKind() == AttributedType::attr_objc_gc ||
1144 T->getAttrKind() == AttributedType::attr_objc_ownership)
1145 return printBefore(T->getEquivalentType(), OS);
1147 if (T->getAttrKind() == AttributedType::attr_objc_kindof)
1150 printBefore(T->getModifiedType(), OS);
1152 if (T->isMSTypeSpec()) {
1153 switch (T->getAttrKind()) {
1155 case AttributedType::attr_ptr32: OS << " __ptr32"; break;
1156 case AttributedType::attr_ptr64: OS << " __ptr64"; break;
1157 case AttributedType::attr_sptr: OS << " __sptr"; break;
1158 case AttributedType::attr_uptr: OS << " __uptr"; break;
1160 spaceBeforePlaceHolder(OS);
1163 // Print nullability type specifiers.
1164 if (T->getAttrKind() == AttributedType::attr_nonnull ||
1165 T->getAttrKind() == AttributedType::attr_nullable ||
1166 T->getAttrKind() == AttributedType::attr_null_unspecified) {
1167 if (T->getAttrKind() == AttributedType::attr_nonnull)
1169 else if (T->getAttrKind() == AttributedType::attr_nullable)
1171 else if (T->getAttrKind() == AttributedType::attr_null_unspecified)
1172 OS << " _Null_unspecified";
1174 llvm_unreachable("unhandled nullability");
1175 spaceBeforePlaceHolder(OS);
1179 void TypePrinter::printAttributedAfter(const AttributedType *T,
1181 // Prefer the macro forms of the GC and ownership qualifiers.
1182 if (T->getAttrKind() == AttributedType::attr_objc_gc ||
1183 T->getAttrKind() == AttributedType::attr_objc_ownership)
1184 return printAfter(T->getEquivalentType(), OS);
1186 if (T->getAttrKind() == AttributedType::attr_objc_kindof)
1189 // TODO: not all attributes are GCC-style attributes.
1190 if (T->isMSTypeSpec())
1193 // Nothing to print after.
1194 if (T->getAttrKind() == AttributedType::attr_nonnull ||
1195 T->getAttrKind() == AttributedType::attr_nullable ||
1196 T->getAttrKind() == AttributedType::attr_null_unspecified)
1197 return printAfter(T->getModifiedType(), OS);
1199 // If this is a calling convention attribute, don't print the implicit CC from
1200 // the modified type.
1201 SaveAndRestore<bool> MaybeSuppressCC(InsideCCAttribute, T->isCallingConv());
1203 printAfter(T->getModifiedType(), OS);
1205 // Don't print the inert __unsafe_unretained attribute at all.
1206 if (T->getAttrKind() == AttributedType::attr_objc_inert_unsafe_unretained)
1209 // Print nullability type specifiers that occur after
1210 if (T->getAttrKind() == AttributedType::attr_nonnull ||
1211 T->getAttrKind() == AttributedType::attr_nullable ||
1212 T->getAttrKind() == AttributedType::attr_null_unspecified) {
1213 if (T->getAttrKind() == AttributedType::attr_nonnull)
1215 else if (T->getAttrKind() == AttributedType::attr_nullable)
1217 else if (T->getAttrKind() == AttributedType::attr_null_unspecified)
1218 OS << " _Null_unspecified";
1220 llvm_unreachable("unhandled nullability");
1225 OS << " __attribute__((";
1226 switch (T->getAttrKind()) {
1227 default: llvm_unreachable("This attribute should have been handled already");
1228 case AttributedType::attr_address_space:
1229 OS << "address_space(";
1230 OS << T->getEquivalentType().getAddressSpace();
1234 case AttributedType::attr_vector_size: {
1235 OS << "__vector_size__(";
1236 if (const VectorType *vector =T->getEquivalentType()->getAs<VectorType>()) {
1237 OS << vector->getNumElements();
1239 print(vector->getElementType(), OS, StringRef());
1246 case AttributedType::attr_neon_vector_type:
1247 case AttributedType::attr_neon_polyvector_type: {
1248 if (T->getAttrKind() == AttributedType::attr_neon_vector_type)
1249 OS << "neon_vector_type(";
1251 OS << "neon_polyvector_type(";
1252 const VectorType *vector = T->getEquivalentType()->getAs<VectorType>();
1253 OS << vector->getNumElements();
1258 case AttributedType::attr_regparm: {
1259 // FIXME: When Sema learns to form this AttributedType, avoid printing the
1260 // attribute again in printFunctionProtoAfter.
1262 QualType t = T->getEquivalentType();
1263 while (!t->isFunctionType())
1264 t = t->getPointeeType();
1265 OS << t->getAs<FunctionType>()->getRegParmType();
1270 case AttributedType::attr_objc_gc: {
1273 QualType tmp = T->getEquivalentType();
1274 while (tmp.getObjCGCAttr() == Qualifiers::GCNone) {
1275 QualType next = tmp->getPointeeType();
1276 if (next == tmp) break;
1280 if (tmp.isObjCGCWeak())
1288 case AttributedType::attr_objc_ownership:
1289 OS << "objc_ownership(";
1290 switch (T->getEquivalentType().getObjCLifetime()) {
1291 case Qualifiers::OCL_None: llvm_unreachable("no ownership!");
1292 case Qualifiers::OCL_ExplicitNone: OS << "none"; break;
1293 case Qualifiers::OCL_Strong: OS << "strong"; break;
1294 case Qualifiers::OCL_Weak: OS << "weak"; break;
1295 case Qualifiers::OCL_Autoreleasing: OS << "autoreleasing"; break;
1300 // FIXME: When Sema learns to form this AttributedType, avoid printing the
1301 // attribute again in printFunctionProtoAfter.
1302 case AttributedType::attr_noreturn: OS << "noreturn"; break;
1304 case AttributedType::attr_cdecl: OS << "cdecl"; break;
1305 case AttributedType::attr_fastcall: OS << "fastcall"; break;
1306 case AttributedType::attr_stdcall: OS << "stdcall"; break;
1307 case AttributedType::attr_thiscall: OS << "thiscall"; break;
1308 case AttributedType::attr_vectorcall: OS << "vectorcall"; break;
1309 case AttributedType::attr_pascal: OS << "pascal"; break;
1310 case AttributedType::attr_ms_abi: OS << "ms_abi"; break;
1311 case AttributedType::attr_sysv_abi: OS << "sysv_abi"; break;
1312 case AttributedType::attr_pcs:
1313 case AttributedType::attr_pcs_vfp: {
1315 QualType t = T->getEquivalentType();
1316 while (!t->isFunctionType())
1317 t = t->getPointeeType();
1318 OS << (t->getAs<FunctionType>()->getCallConv() == CC_AAPCS ?
1319 "\"aapcs\"" : "\"aapcs-vfp\"");
1323 case AttributedType::attr_inteloclbicc: OS << "inteloclbicc"; break;
1328 void TypePrinter::printObjCInterfaceBefore(const ObjCInterfaceType *T,
1330 OS << T->getDecl()->getName();
1331 spaceBeforePlaceHolder(OS);
1333 void TypePrinter::printObjCInterfaceAfter(const ObjCInterfaceType *T,
1334 raw_ostream &OS) { }
1336 void TypePrinter::printObjCObjectBefore(const ObjCObjectType *T,
1338 if (T->qual_empty() && T->isUnspecializedAsWritten() &&
1339 !T->isKindOfTypeAsWritten())
1340 return printBefore(T->getBaseType(), OS);
1342 if (T->isKindOfTypeAsWritten())
1345 print(T->getBaseType(), OS, StringRef());
1347 if (T->isSpecializedAsWritten()) {
1348 bool isFirst = true;
1350 for (auto typeArg : T->getTypeArgsAsWritten()) {
1356 print(typeArg, OS, StringRef());
1361 if (!T->qual_empty()) {
1362 bool isFirst = true;
1364 for (const auto *I : T->quals()) {
1374 spaceBeforePlaceHolder(OS);
1376 void TypePrinter::printObjCObjectAfter(const ObjCObjectType *T,
1378 if (T->qual_empty() && T->isUnspecializedAsWritten() &&
1379 !T->isKindOfTypeAsWritten())
1380 return printAfter(T->getBaseType(), OS);
1383 void TypePrinter::printObjCObjectPointerBefore(const ObjCObjectPointerType *T,
1385 printBefore(T->getPointeeType(), OS);
1387 // If we need to print the pointer, print it now.
1388 if (!T->isObjCIdType() && !T->isObjCQualifiedIdType() &&
1389 !T->isObjCClassType() && !T->isObjCQualifiedClassType()) {
1390 if (HasEmptyPlaceHolder)
1395 void TypePrinter::printObjCObjectPointerAfter(const ObjCObjectPointerType *T,
1396 raw_ostream &OS) { }
1398 void TemplateSpecializationType::
1399 PrintTemplateArgumentList(raw_ostream &OS,
1400 const TemplateArgumentListInfo &Args,
1401 const PrintingPolicy &Policy) {
1402 return PrintTemplateArgumentList(OS,
1403 Args.getArgumentArray(),
1409 TemplateSpecializationType::PrintTemplateArgumentList(
1411 const TemplateArgument *Args,
1413 const PrintingPolicy &Policy,
1414 bool SkipBrackets) {
1415 const char *Comma = Policy.MSVCFormatting ? "," : ", ";
1419 bool needSpace = false;
1420 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1421 // Print the argument into a string.
1422 SmallString<128> Buf;
1423 llvm::raw_svector_ostream ArgOS(Buf);
1424 if (Args[Arg].getKind() == TemplateArgument::Pack) {
1425 if (Args[Arg].pack_size() && Arg > 0)
1427 PrintTemplateArgumentList(ArgOS,
1428 Args[Arg].pack_begin(),
1429 Args[Arg].pack_size(),
1434 Args[Arg].print(Policy, ArgOS);
1436 StringRef ArgString = ArgOS.str();
1438 // If this is the first argument and its string representation
1439 // begins with the global scope specifier ('::foo'), add a space
1440 // to avoid printing the diagraph '<:'.
1441 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1446 needSpace = (!ArgString.empty() && ArgString.back() == '>');
1449 // If the last character of our string is '>', add another space to
1450 // keep the two '>''s separate tokens. We don't *have* to do this in
1451 // C++0x, but it's still good hygiene.
1459 // Sadly, repeat all that with TemplateArgLoc.
1460 void TemplateSpecializationType::
1461 PrintTemplateArgumentList(raw_ostream &OS,
1462 const TemplateArgumentLoc *Args, unsigned NumArgs,
1463 const PrintingPolicy &Policy) {
1465 const char *Comma = Policy.MSVCFormatting ? "," : ", ";
1467 bool needSpace = false;
1468 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1472 // Print the argument into a string.
1473 SmallString<128> Buf;
1474 llvm::raw_svector_ostream ArgOS(Buf);
1475 if (Args[Arg].getArgument().getKind() == TemplateArgument::Pack) {
1476 PrintTemplateArgumentList(ArgOS,
1477 Args[Arg].getArgument().pack_begin(),
1478 Args[Arg].getArgument().pack_size(),
1481 Args[Arg].getArgument().print(Policy, ArgOS);
1483 StringRef ArgString = ArgOS.str();
1485 // If this is the first argument and its string representation
1486 // begins with the global scope specifier ('::foo'), add a space
1487 // to avoid printing the diagraph '<:'.
1488 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1493 needSpace = (!ArgString.empty() && ArgString.back() == '>');
1496 // If the last character of our string is '>', add another space to
1497 // keep the two '>''s separate tokens. We don't *have* to do this in
1498 // C++0x, but it's still good hygiene.
1505 std::string Qualifiers::getAsString() const {
1507 return getAsString(PrintingPolicy(LO));
1510 // Appends qualifiers to the given string, separated by spaces. Will
1511 // prefix a space if the string is non-empty. Will not append a final
1513 std::string Qualifiers::getAsString(const PrintingPolicy &Policy) const {
1514 SmallString<64> Buf;
1515 llvm::raw_svector_ostream StrOS(Buf);
1516 print(StrOS, Policy);
1520 bool Qualifiers::isEmptyWhenPrinted(const PrintingPolicy &Policy) const {
1521 if (getCVRQualifiers())
1524 if (getAddressSpace())
1527 if (getObjCGCAttr())
1530 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime())
1531 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime))
1537 // Appends qualifiers to the given string, separated by spaces. Will
1538 // prefix a space if the string is non-empty. Will not append a final
1540 void Qualifiers::print(raw_ostream &OS, const PrintingPolicy& Policy,
1541 bool appendSpaceIfNonEmpty) const {
1542 bool addSpace = false;
1544 unsigned quals = getCVRQualifiers();
1546 AppendTypeQualList(OS, quals, Policy.LangOpts.C99);
1549 if (unsigned addrspace = getAddressSpace()) {
1553 switch (addrspace) {
1554 case LangAS::opencl_global:
1557 case LangAS::opencl_local:
1560 case LangAS::opencl_constant:
1563 case LangAS::opencl_generic:
1567 OS << "__attribute__((address_space(";
1572 if (Qualifiers::GC gc = getObjCGCAttr()) {
1576 if (gc == Qualifiers::Weak)
1581 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) {
1582 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime)){
1589 case Qualifiers::OCL_None: llvm_unreachable("none but true");
1590 case Qualifiers::OCL_ExplicitNone: OS << "__unsafe_unretained"; break;
1591 case Qualifiers::OCL_Strong:
1592 if (!Policy.SuppressStrongLifetime)
1596 case Qualifiers::OCL_Weak: OS << "__weak"; break;
1597 case Qualifiers::OCL_Autoreleasing: OS << "__autoreleasing"; break;
1601 if (appendSpaceIfNonEmpty && addSpace)
1605 std::string QualType::getAsString(const PrintingPolicy &Policy) const {
1607 getAsStringInternal(S, Policy);
1611 std::string QualType::getAsString(const Type *ty, Qualifiers qs) {
1613 LangOptions options;
1614 getAsStringInternal(ty, qs, buffer, PrintingPolicy(options));
1618 void QualType::print(const Type *ty, Qualifiers qs,
1619 raw_ostream &OS, const PrintingPolicy &policy,
1620 const Twine &PlaceHolder) {
1621 SmallString<128> PHBuf;
1622 StringRef PH = PlaceHolder.toStringRef(PHBuf);
1624 TypePrinter(policy).print(ty, qs, OS, PH);
1627 void QualType::getAsStringInternal(const Type *ty, Qualifiers qs,
1628 std::string &buffer,
1629 const PrintingPolicy &policy) {
1630 SmallString<256> Buf;
1631 llvm::raw_svector_ostream StrOS(Buf);
1632 TypePrinter(policy).print(ty, qs, StrOS, buffer);
1633 std::string str = StrOS.str();