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) {
645 auto EPI = T->getExtParameterInfo(i);
646 if (EPI.isConsumed()) OS << "__attribute__((ns_consumed)) ";
648 print(T->getParamType(i), OS, StringRef());
652 if (T->isVariadic()) {
653 if (T->getNumParams())
656 } else if (T->getNumParams() == 0 && !Policy.LangOpts.CPlusPlus) {
657 // Do not emit int() if we have a proto, emit 'int(void)'.
663 FunctionType::ExtInfo Info = T->getExtInfo();
665 if (!InsideCCAttribute) {
666 switch (Info.getCC()) {
668 // The C calling convention is the default on the vast majority of platforms
669 // we support. If the user wrote it explicitly, it will usually be printed
670 // while traversing the AttributedType. If the type has been desugared, let
671 // the canonical spelling be the implicit calling convention.
672 // FIXME: It would be better to be explicit in certain contexts, such as a
673 // cdecl function typedef used to declare a member function with the
674 // Microsoft C++ ABI.
677 OS << " __attribute__((stdcall))";
680 OS << " __attribute__((fastcall))";
683 OS << " __attribute__((thiscall))";
685 case CC_X86VectorCall:
686 OS << " __attribute__((vectorcall))";
689 OS << " __attribute__((pascal))";
692 OS << " __attribute__((pcs(\"aapcs\")))";
695 OS << " __attribute__((pcs(\"aapcs-vfp\")))";
697 case CC_IntelOclBicc:
698 OS << " __attribute__((intel_ocl_bicc))";
701 OS << " __attribute__((ms_abi))";
704 OS << " __attribute__((sysv_abi))";
706 case CC_SpirFunction:
708 // Do nothing. These CCs are not available as attributes.
713 if (Info.getNoReturn())
714 OS << " __attribute__((noreturn))";
715 if (Info.getRegParm())
716 OS << " __attribute__((regparm ("
717 << Info.getRegParm() << ")))";
719 if (unsigned quals = T->getTypeQuals()) {
721 AppendTypeQualList(OS, quals, Policy.LangOpts.C99);
724 switch (T->getRefQualifier()) {
736 T->printExceptionSpecification(OS, Policy);
738 if (T->hasTrailingReturn()) {
740 print(T->getReturnType(), OS, StringRef());
742 printAfter(T->getReturnType(), OS);
745 void TypePrinter::printFunctionNoProtoBefore(const FunctionNoProtoType *T,
747 // If needed for precedence reasons, wrap the inner part in grouping parens.
748 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false);
749 printBefore(T->getReturnType(), OS);
750 if (!PrevPHIsEmpty.get())
753 void TypePrinter::printFunctionNoProtoAfter(const FunctionNoProtoType *T,
755 // If needed for precedence reasons, wrap the inner part in grouping parens.
756 if (!HasEmptyPlaceHolder)
758 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
761 if (T->getNoReturnAttr())
762 OS << " __attribute__((noreturn))";
763 printAfter(T->getReturnType(), OS);
766 void TypePrinter::printTypeSpec(const NamedDecl *D, raw_ostream &OS) {
767 IdentifierInfo *II = D->getIdentifier();
769 spaceBeforePlaceHolder(OS);
772 void TypePrinter::printUnresolvedUsingBefore(const UnresolvedUsingType *T,
774 printTypeSpec(T->getDecl(), OS);
776 void TypePrinter::printUnresolvedUsingAfter(const UnresolvedUsingType *T,
779 void TypePrinter::printTypedefBefore(const TypedefType *T, raw_ostream &OS) {
780 printTypeSpec(T->getDecl(), OS);
782 void TypePrinter::printTypedefAfter(const TypedefType *T, raw_ostream &OS) { }
784 void TypePrinter::printTypeOfExprBefore(const TypeOfExprType *T,
787 if (T->getUnderlyingExpr())
788 T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy);
789 spaceBeforePlaceHolder(OS);
791 void TypePrinter::printTypeOfExprAfter(const TypeOfExprType *T,
794 void TypePrinter::printTypeOfBefore(const TypeOfType *T, raw_ostream &OS) {
796 print(T->getUnderlyingType(), OS, StringRef());
798 spaceBeforePlaceHolder(OS);
800 void TypePrinter::printTypeOfAfter(const TypeOfType *T, raw_ostream &OS) { }
802 void TypePrinter::printDecltypeBefore(const DecltypeType *T, raw_ostream &OS) {
804 if (T->getUnderlyingExpr())
805 T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy);
807 spaceBeforePlaceHolder(OS);
809 void TypePrinter::printDecltypeAfter(const DecltypeType *T, raw_ostream &OS) { }
811 void TypePrinter::printUnaryTransformBefore(const UnaryTransformType *T,
813 IncludeStrongLifetimeRAII Strong(Policy);
815 switch (T->getUTTKind()) {
816 case UnaryTransformType::EnumUnderlyingType:
817 OS << "__underlying_type(";
818 print(T->getBaseType(), OS, StringRef());
820 spaceBeforePlaceHolder(OS);
824 printBefore(T->getBaseType(), OS);
826 void TypePrinter::printUnaryTransformAfter(const UnaryTransformType *T,
828 IncludeStrongLifetimeRAII Strong(Policy);
830 switch (T->getUTTKind()) {
831 case UnaryTransformType::EnumUnderlyingType:
835 printAfter(T->getBaseType(), OS);
838 void TypePrinter::printAutoBefore(const AutoType *T, raw_ostream &OS) {
839 // If the type has been deduced, do not print 'auto'.
840 if (!T->getDeducedType().isNull()) {
841 printBefore(T->getDeducedType(), OS);
843 switch (T->getKeyword()) {
844 case AutoTypeKeyword::Auto: OS << "auto"; break;
845 case AutoTypeKeyword::DecltypeAuto: OS << "decltype(auto)"; break;
846 case AutoTypeKeyword::GNUAutoType: OS << "__auto_type"; break;
848 spaceBeforePlaceHolder(OS);
851 void TypePrinter::printAutoAfter(const AutoType *T, raw_ostream &OS) {
852 // If the type has been deduced, do not print 'auto'.
853 if (!T->getDeducedType().isNull())
854 printAfter(T->getDeducedType(), OS);
857 void TypePrinter::printAtomicBefore(const AtomicType *T, raw_ostream &OS) {
858 IncludeStrongLifetimeRAII Strong(Policy);
861 print(T->getValueType(), OS, StringRef());
863 spaceBeforePlaceHolder(OS);
865 void TypePrinter::printAtomicAfter(const AtomicType *T, raw_ostream &OS) { }
867 void TypePrinter::printPipeBefore(const PipeType *T, raw_ostream &OS) {
868 IncludeStrongLifetimeRAII Strong(Policy);
871 spaceBeforePlaceHolder(OS);
874 void TypePrinter::printPipeAfter(const PipeType *T, raw_ostream &OS) {
876 /// Appends the given scope to the end of a string.
877 void TypePrinter::AppendScope(DeclContext *DC, raw_ostream &OS) {
878 if (DC->isTranslationUnit()) return;
879 if (DC->isFunctionOrMethod()) return;
880 AppendScope(DC->getParent(), OS);
882 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
883 if (Policy.SuppressUnwrittenScope &&
884 (NS->isAnonymousNamespace() || NS->isInline()))
886 if (NS->getIdentifier())
887 OS << NS->getName() << "::";
889 OS << "(anonymous namespace)::";
890 } else if (ClassTemplateSpecializationDecl *Spec
891 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
892 IncludeStrongLifetimeRAII Strong(Policy);
893 OS << Spec->getIdentifier()->getName();
894 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
895 TemplateSpecializationType::PrintTemplateArgumentList(OS,
900 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
901 if (TypedefNameDecl *Typedef = Tag->getTypedefNameForAnonDecl())
902 OS << Typedef->getIdentifier()->getName() << "::";
903 else if (Tag->getIdentifier())
904 OS << Tag->getIdentifier()->getName() << "::";
910 void TypePrinter::printTag(TagDecl *D, raw_ostream &OS) {
911 if (Policy.SuppressTag)
914 bool HasKindDecoration = false;
916 // bool SuppressTagKeyword
917 // = Policy.LangOpts.CPlusPlus || Policy.SuppressTagKeyword;
919 // We don't print tags unless this is an elaborated type.
920 // In C, we just assume every RecordType is an elaborated type.
921 if (!(Policy.LangOpts.CPlusPlus || Policy.SuppressTagKeyword ||
922 D->getTypedefNameForAnonDecl())) {
923 HasKindDecoration = true;
924 OS << D->getKindName();
928 // Compute the full nested-name-specifier for this type.
929 // In C, this will always be empty except when the type
930 // being printed is anonymous within other Record.
931 if (!Policy.SuppressScope)
932 AppendScope(D->getDeclContext(), OS);
934 if (const IdentifierInfo *II = D->getIdentifier())
936 else if (TypedefNameDecl *Typedef = D->getTypedefNameForAnonDecl()) {
937 assert(Typedef->getIdentifier() && "Typedef without identifier?");
938 OS << Typedef->getIdentifier()->getName();
940 // Make an unambiguous representation for anonymous types, e.g.
941 // (anonymous enum at /usr/include/string.h:120:9)
942 OS << (Policy.MSVCFormatting ? '`' : '(');
944 if (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda()) {
946 HasKindDecoration = true;
951 if (Policy.AnonymousTagLocations) {
952 // Suppress the redundant tag keyword if we just printed one.
953 // We don't have to worry about ElaboratedTypes here because you can't
954 // refer to an anonymous type with one.
955 if (!HasKindDecoration)
956 OS << " " << D->getKindName();
958 PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc(
960 if (PLoc.isValid()) {
961 OS << " at " << PLoc.getFilename()
962 << ':' << PLoc.getLine()
963 << ':' << PLoc.getColumn();
967 OS << (Policy.MSVCFormatting ? '\'' : ')');
970 // If this is a class template specialization, print the template
972 if (ClassTemplateSpecializationDecl *Spec
973 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
974 const TemplateArgument *Args;
976 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
977 const TemplateSpecializationType *TST =
978 cast<TemplateSpecializationType>(TAW->getType());
979 Args = TST->getArgs();
980 NumArgs = TST->getNumArgs();
982 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
983 Args = TemplateArgs.data();
984 NumArgs = TemplateArgs.size();
986 IncludeStrongLifetimeRAII Strong(Policy);
987 TemplateSpecializationType::PrintTemplateArgumentList(OS,
992 spaceBeforePlaceHolder(OS);
995 void TypePrinter::printRecordBefore(const RecordType *T, raw_ostream &OS) {
996 printTag(T->getDecl(), OS);
998 void TypePrinter::printRecordAfter(const RecordType *T, raw_ostream &OS) { }
1000 void TypePrinter::printEnumBefore(const EnumType *T, raw_ostream &OS) {
1001 printTag(T->getDecl(), OS);
1003 void TypePrinter::printEnumAfter(const EnumType *T, raw_ostream &OS) { }
1005 void TypePrinter::printTemplateTypeParmBefore(const TemplateTypeParmType *T,
1007 if (IdentifierInfo *Id = T->getIdentifier())
1008 OS << Id->getName();
1010 OS << "type-parameter-" << T->getDepth() << '-' << T->getIndex();
1011 spaceBeforePlaceHolder(OS);
1013 void TypePrinter::printTemplateTypeParmAfter(const TemplateTypeParmType *T,
1014 raw_ostream &OS) { }
1016 void TypePrinter::printSubstTemplateTypeParmBefore(
1017 const SubstTemplateTypeParmType *T,
1019 IncludeStrongLifetimeRAII Strong(Policy);
1020 printBefore(T->getReplacementType(), OS);
1022 void TypePrinter::printSubstTemplateTypeParmAfter(
1023 const SubstTemplateTypeParmType *T,
1025 IncludeStrongLifetimeRAII Strong(Policy);
1026 printAfter(T->getReplacementType(), OS);
1029 void TypePrinter::printSubstTemplateTypeParmPackBefore(
1030 const SubstTemplateTypeParmPackType *T,
1032 IncludeStrongLifetimeRAII Strong(Policy);
1033 printTemplateTypeParmBefore(T->getReplacedParameter(), OS);
1035 void TypePrinter::printSubstTemplateTypeParmPackAfter(
1036 const SubstTemplateTypeParmPackType *T,
1038 IncludeStrongLifetimeRAII Strong(Policy);
1039 printTemplateTypeParmAfter(T->getReplacedParameter(), OS);
1042 void TypePrinter::printTemplateSpecializationBefore(
1043 const TemplateSpecializationType *T,
1045 IncludeStrongLifetimeRAII Strong(Policy);
1046 T->getTemplateName().print(OS, Policy);
1048 TemplateSpecializationType::PrintTemplateArgumentList(OS,
1052 spaceBeforePlaceHolder(OS);
1054 void TypePrinter::printTemplateSpecializationAfter(
1055 const TemplateSpecializationType *T,
1056 raw_ostream &OS) { }
1058 void TypePrinter::printInjectedClassNameBefore(const InjectedClassNameType *T,
1060 printTemplateSpecializationBefore(T->getInjectedTST(), OS);
1062 void TypePrinter::printInjectedClassNameAfter(const InjectedClassNameType *T,
1063 raw_ostream &OS) { }
1065 void TypePrinter::printElaboratedBefore(const ElaboratedType *T,
1067 if (Policy.SuppressTag && isa<TagType>(T->getNamedType()))
1069 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1070 if (T->getKeyword() != ETK_None)
1072 NestedNameSpecifier* Qualifier = T->getQualifier();
1074 Qualifier->print(OS, Policy);
1076 ElaboratedTypePolicyRAII PolicyRAII(Policy);
1077 printBefore(T->getNamedType(), OS);
1079 void TypePrinter::printElaboratedAfter(const ElaboratedType *T,
1081 ElaboratedTypePolicyRAII PolicyRAII(Policy);
1082 printAfter(T->getNamedType(), OS);
1085 void TypePrinter::printParenBefore(const ParenType *T, raw_ostream &OS) {
1086 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) {
1087 printBefore(T->getInnerType(), OS);
1090 printBefore(T->getInnerType(), OS);
1092 void TypePrinter::printParenAfter(const ParenType *T, raw_ostream &OS) {
1093 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) {
1095 printAfter(T->getInnerType(), OS);
1097 printAfter(T->getInnerType(), OS);
1100 void TypePrinter::printDependentNameBefore(const DependentNameType *T,
1102 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1103 if (T->getKeyword() != ETK_None)
1106 T->getQualifier()->print(OS, Policy);
1108 OS << T->getIdentifier()->getName();
1109 spaceBeforePlaceHolder(OS);
1111 void TypePrinter::printDependentNameAfter(const DependentNameType *T,
1112 raw_ostream &OS) { }
1114 void TypePrinter::printDependentTemplateSpecializationBefore(
1115 const DependentTemplateSpecializationType *T, raw_ostream &OS) {
1116 IncludeStrongLifetimeRAII Strong(Policy);
1118 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1119 if (T->getKeyword() != ETK_None)
1122 if (T->getQualifier())
1123 T->getQualifier()->print(OS, Policy);
1124 OS << T->getIdentifier()->getName();
1125 TemplateSpecializationType::PrintTemplateArgumentList(OS,
1129 spaceBeforePlaceHolder(OS);
1131 void TypePrinter::printDependentTemplateSpecializationAfter(
1132 const DependentTemplateSpecializationType *T, raw_ostream &OS) { }
1134 void TypePrinter::printPackExpansionBefore(const PackExpansionType *T,
1136 printBefore(T->getPattern(), OS);
1138 void TypePrinter::printPackExpansionAfter(const PackExpansionType *T,
1140 printAfter(T->getPattern(), OS);
1144 void TypePrinter::printAttributedBefore(const AttributedType *T,
1146 // Prefer the macro forms of the GC and ownership qualifiers.
1147 if (T->getAttrKind() == AttributedType::attr_objc_gc ||
1148 T->getAttrKind() == AttributedType::attr_objc_ownership)
1149 return printBefore(T->getEquivalentType(), OS);
1151 if (T->getAttrKind() == AttributedType::attr_objc_kindof)
1154 printBefore(T->getModifiedType(), OS);
1156 if (T->isMSTypeSpec()) {
1157 switch (T->getAttrKind()) {
1159 case AttributedType::attr_ptr32: OS << " __ptr32"; break;
1160 case AttributedType::attr_ptr64: OS << " __ptr64"; break;
1161 case AttributedType::attr_sptr: OS << " __sptr"; break;
1162 case AttributedType::attr_uptr: OS << " __uptr"; break;
1164 spaceBeforePlaceHolder(OS);
1167 // Print nullability type specifiers.
1168 if (T->getAttrKind() == AttributedType::attr_nonnull ||
1169 T->getAttrKind() == AttributedType::attr_nullable ||
1170 T->getAttrKind() == AttributedType::attr_null_unspecified) {
1171 if (T->getAttrKind() == AttributedType::attr_nonnull)
1173 else if (T->getAttrKind() == AttributedType::attr_nullable)
1175 else if (T->getAttrKind() == AttributedType::attr_null_unspecified)
1176 OS << " _Null_unspecified";
1178 llvm_unreachable("unhandled nullability");
1179 spaceBeforePlaceHolder(OS);
1183 void TypePrinter::printAttributedAfter(const AttributedType *T,
1185 // Prefer the macro forms of the GC and ownership qualifiers.
1186 if (T->getAttrKind() == AttributedType::attr_objc_gc ||
1187 T->getAttrKind() == AttributedType::attr_objc_ownership)
1188 return printAfter(T->getEquivalentType(), OS);
1190 if (T->getAttrKind() == AttributedType::attr_objc_kindof)
1193 // TODO: not all attributes are GCC-style attributes.
1194 if (T->isMSTypeSpec())
1197 // Nothing to print after.
1198 if (T->getAttrKind() == AttributedType::attr_nonnull ||
1199 T->getAttrKind() == AttributedType::attr_nullable ||
1200 T->getAttrKind() == AttributedType::attr_null_unspecified)
1201 return printAfter(T->getModifiedType(), OS);
1203 // If this is a calling convention attribute, don't print the implicit CC from
1204 // the modified type.
1205 SaveAndRestore<bool> MaybeSuppressCC(InsideCCAttribute, T->isCallingConv());
1207 printAfter(T->getModifiedType(), OS);
1209 // Don't print the inert __unsafe_unretained attribute at all.
1210 if (T->getAttrKind() == AttributedType::attr_objc_inert_unsafe_unretained)
1213 // Print nullability type specifiers that occur after
1214 if (T->getAttrKind() == AttributedType::attr_nonnull ||
1215 T->getAttrKind() == AttributedType::attr_nullable ||
1216 T->getAttrKind() == AttributedType::attr_null_unspecified) {
1217 if (T->getAttrKind() == AttributedType::attr_nonnull)
1219 else if (T->getAttrKind() == AttributedType::attr_nullable)
1221 else if (T->getAttrKind() == AttributedType::attr_null_unspecified)
1222 OS << " _Null_unspecified";
1224 llvm_unreachable("unhandled nullability");
1229 OS << " __attribute__((";
1230 switch (T->getAttrKind()) {
1231 default: llvm_unreachable("This attribute should have been handled already");
1232 case AttributedType::attr_address_space:
1233 OS << "address_space(";
1234 OS << T->getEquivalentType().getAddressSpace();
1238 case AttributedType::attr_vector_size: {
1239 OS << "__vector_size__(";
1240 if (const VectorType *vector =T->getEquivalentType()->getAs<VectorType>()) {
1241 OS << vector->getNumElements();
1243 print(vector->getElementType(), OS, StringRef());
1250 case AttributedType::attr_neon_vector_type:
1251 case AttributedType::attr_neon_polyvector_type: {
1252 if (T->getAttrKind() == AttributedType::attr_neon_vector_type)
1253 OS << "neon_vector_type(";
1255 OS << "neon_polyvector_type(";
1256 const VectorType *vector = T->getEquivalentType()->getAs<VectorType>();
1257 OS << vector->getNumElements();
1262 case AttributedType::attr_regparm: {
1263 // FIXME: When Sema learns to form this AttributedType, avoid printing the
1264 // attribute again in printFunctionProtoAfter.
1266 QualType t = T->getEquivalentType();
1267 while (!t->isFunctionType())
1268 t = t->getPointeeType();
1269 OS << t->getAs<FunctionType>()->getRegParmType();
1274 case AttributedType::attr_objc_gc: {
1277 QualType tmp = T->getEquivalentType();
1278 while (tmp.getObjCGCAttr() == Qualifiers::GCNone) {
1279 QualType next = tmp->getPointeeType();
1280 if (next == tmp) break;
1284 if (tmp.isObjCGCWeak())
1292 case AttributedType::attr_objc_ownership:
1293 OS << "objc_ownership(";
1294 switch (T->getEquivalentType().getObjCLifetime()) {
1295 case Qualifiers::OCL_None: llvm_unreachable("no ownership!");
1296 case Qualifiers::OCL_ExplicitNone: OS << "none"; break;
1297 case Qualifiers::OCL_Strong: OS << "strong"; break;
1298 case Qualifiers::OCL_Weak: OS << "weak"; break;
1299 case Qualifiers::OCL_Autoreleasing: OS << "autoreleasing"; break;
1304 // FIXME: When Sema learns to form this AttributedType, avoid printing the
1305 // attribute again in printFunctionProtoAfter.
1306 case AttributedType::attr_noreturn: OS << "noreturn"; break;
1308 case AttributedType::attr_cdecl: OS << "cdecl"; break;
1309 case AttributedType::attr_fastcall: OS << "fastcall"; break;
1310 case AttributedType::attr_stdcall: OS << "stdcall"; break;
1311 case AttributedType::attr_thiscall: OS << "thiscall"; break;
1312 case AttributedType::attr_vectorcall: OS << "vectorcall"; break;
1313 case AttributedType::attr_pascal: OS << "pascal"; break;
1314 case AttributedType::attr_ms_abi: OS << "ms_abi"; break;
1315 case AttributedType::attr_sysv_abi: OS << "sysv_abi"; break;
1316 case AttributedType::attr_pcs:
1317 case AttributedType::attr_pcs_vfp: {
1319 QualType t = T->getEquivalentType();
1320 while (!t->isFunctionType())
1321 t = t->getPointeeType();
1322 OS << (t->getAs<FunctionType>()->getCallConv() == CC_AAPCS ?
1323 "\"aapcs\"" : "\"aapcs-vfp\"");
1327 case AttributedType::attr_inteloclbicc: OS << "inteloclbicc"; break;
1332 void TypePrinter::printObjCInterfaceBefore(const ObjCInterfaceType *T,
1334 OS << T->getDecl()->getName();
1335 spaceBeforePlaceHolder(OS);
1337 void TypePrinter::printObjCInterfaceAfter(const ObjCInterfaceType *T,
1338 raw_ostream &OS) { }
1340 void TypePrinter::printObjCObjectBefore(const ObjCObjectType *T,
1342 if (T->qual_empty() && T->isUnspecializedAsWritten() &&
1343 !T->isKindOfTypeAsWritten())
1344 return printBefore(T->getBaseType(), OS);
1346 if (T->isKindOfTypeAsWritten())
1349 print(T->getBaseType(), OS, StringRef());
1351 if (T->isSpecializedAsWritten()) {
1352 bool isFirst = true;
1354 for (auto typeArg : T->getTypeArgsAsWritten()) {
1360 print(typeArg, OS, StringRef());
1365 if (!T->qual_empty()) {
1366 bool isFirst = true;
1368 for (const auto *I : T->quals()) {
1378 spaceBeforePlaceHolder(OS);
1380 void TypePrinter::printObjCObjectAfter(const ObjCObjectType *T,
1382 if (T->qual_empty() && T->isUnspecializedAsWritten() &&
1383 !T->isKindOfTypeAsWritten())
1384 return printAfter(T->getBaseType(), OS);
1387 void TypePrinter::printObjCObjectPointerBefore(const ObjCObjectPointerType *T,
1389 printBefore(T->getPointeeType(), OS);
1391 // If we need to print the pointer, print it now.
1392 if (!T->isObjCIdType() && !T->isObjCQualifiedIdType() &&
1393 !T->isObjCClassType() && !T->isObjCQualifiedClassType()) {
1394 if (HasEmptyPlaceHolder)
1399 void TypePrinter::printObjCObjectPointerAfter(const ObjCObjectPointerType *T,
1400 raw_ostream &OS) { }
1402 void TemplateSpecializationType::
1403 PrintTemplateArgumentList(raw_ostream &OS,
1404 const TemplateArgumentListInfo &Args,
1405 const PrintingPolicy &Policy) {
1406 return PrintTemplateArgumentList(OS,
1407 Args.getArgumentArray(),
1413 TemplateSpecializationType::PrintTemplateArgumentList(
1415 const TemplateArgument *Args,
1417 const PrintingPolicy &Policy,
1418 bool SkipBrackets) {
1419 const char *Comma = Policy.MSVCFormatting ? "," : ", ";
1423 bool needSpace = false;
1424 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1425 // Print the argument into a string.
1426 SmallString<128> Buf;
1427 llvm::raw_svector_ostream ArgOS(Buf);
1428 if (Args[Arg].getKind() == TemplateArgument::Pack) {
1429 if (Args[Arg].pack_size() && Arg > 0)
1431 PrintTemplateArgumentList(ArgOS,
1432 Args[Arg].pack_begin(),
1433 Args[Arg].pack_size(),
1438 Args[Arg].print(Policy, ArgOS);
1440 StringRef ArgString = ArgOS.str();
1442 // If this is the first argument and its string representation
1443 // begins with the global scope specifier ('::foo'), add a space
1444 // to avoid printing the diagraph '<:'.
1445 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1450 needSpace = (!ArgString.empty() && ArgString.back() == '>');
1453 // If the last character of our string is '>', add another space to
1454 // keep the two '>''s separate tokens. We don't *have* to do this in
1455 // C++0x, but it's still good hygiene.
1463 // Sadly, repeat all that with TemplateArgLoc.
1464 void TemplateSpecializationType::
1465 PrintTemplateArgumentList(raw_ostream &OS,
1466 const TemplateArgumentLoc *Args, unsigned NumArgs,
1467 const PrintingPolicy &Policy) {
1469 const char *Comma = Policy.MSVCFormatting ? "," : ", ";
1471 bool needSpace = false;
1472 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1476 // Print the argument into a string.
1477 SmallString<128> Buf;
1478 llvm::raw_svector_ostream ArgOS(Buf);
1479 if (Args[Arg].getArgument().getKind() == TemplateArgument::Pack) {
1480 PrintTemplateArgumentList(ArgOS,
1481 Args[Arg].getArgument().pack_begin(),
1482 Args[Arg].getArgument().pack_size(),
1485 Args[Arg].getArgument().print(Policy, ArgOS);
1487 StringRef ArgString = ArgOS.str();
1489 // If this is the first argument and its string representation
1490 // begins with the global scope specifier ('::foo'), add a space
1491 // to avoid printing the diagraph '<:'.
1492 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1497 needSpace = (!ArgString.empty() && ArgString.back() == '>');
1500 // If the last character of our string is '>', add another space to
1501 // keep the two '>''s separate tokens. We don't *have* to do this in
1502 // C++0x, but it's still good hygiene.
1509 std::string Qualifiers::getAsString() const {
1511 return getAsString(PrintingPolicy(LO));
1514 // Appends qualifiers to the given string, separated by spaces. Will
1515 // prefix a space if the string is non-empty. Will not append a final
1517 std::string Qualifiers::getAsString(const PrintingPolicy &Policy) const {
1518 SmallString<64> Buf;
1519 llvm::raw_svector_ostream StrOS(Buf);
1520 print(StrOS, Policy);
1524 bool Qualifiers::isEmptyWhenPrinted(const PrintingPolicy &Policy) const {
1525 if (getCVRQualifiers())
1528 if (getAddressSpace())
1531 if (getObjCGCAttr())
1534 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime())
1535 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime))
1541 // Appends qualifiers to the given string, separated by spaces. Will
1542 // prefix a space if the string is non-empty. Will not append a final
1544 void Qualifiers::print(raw_ostream &OS, const PrintingPolicy& Policy,
1545 bool appendSpaceIfNonEmpty) const {
1546 bool addSpace = false;
1548 unsigned quals = getCVRQualifiers();
1550 AppendTypeQualList(OS, quals, Policy.LangOpts.C99);
1553 if (unsigned addrspace = getAddressSpace()) {
1557 switch (addrspace) {
1558 case LangAS::opencl_global:
1561 case LangAS::opencl_local:
1564 case LangAS::opencl_constant:
1567 case LangAS::opencl_generic:
1571 OS << "__attribute__((address_space(";
1576 if (Qualifiers::GC gc = getObjCGCAttr()) {
1580 if (gc == Qualifiers::Weak)
1585 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) {
1586 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime)){
1593 case Qualifiers::OCL_None: llvm_unreachable("none but true");
1594 case Qualifiers::OCL_ExplicitNone: OS << "__unsafe_unretained"; break;
1595 case Qualifiers::OCL_Strong:
1596 if (!Policy.SuppressStrongLifetime)
1600 case Qualifiers::OCL_Weak: OS << "__weak"; break;
1601 case Qualifiers::OCL_Autoreleasing: OS << "__autoreleasing"; break;
1605 if (appendSpaceIfNonEmpty && addSpace)
1609 std::string QualType::getAsString(const PrintingPolicy &Policy) const {
1611 getAsStringInternal(S, Policy);
1615 std::string QualType::getAsString(const Type *ty, Qualifiers qs) {
1617 LangOptions options;
1618 getAsStringInternal(ty, qs, buffer, PrintingPolicy(options));
1622 void QualType::print(const Type *ty, Qualifiers qs,
1623 raw_ostream &OS, const PrintingPolicy &policy,
1624 const Twine &PlaceHolder) {
1625 SmallString<128> PHBuf;
1626 StringRef PH = PlaceHolder.toStringRef(PHBuf);
1628 TypePrinter(policy).print(ty, qs, OS, PH);
1631 void QualType::getAsStringInternal(const Type *ty, Qualifiers qs,
1632 std::string &buffer,
1633 const PrintingPolicy &policy) {
1634 SmallString<256> Buf;
1635 llvm::raw_svector_ostream StrOS(Buf);
1636 TypePrinter(policy).print(ty, qs, StrOS, buffer);
1637 std::string str = StrOS.str();