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;
85 bool HasEmptyPlaceHolder;
86 bool InsideCCAttribute;
89 explicit TypePrinter(const PrintingPolicy &Policy, unsigned Indentation = 0)
90 : Policy(Policy), Indentation(Indentation),
91 HasEmptyPlaceHolder(false), InsideCCAttribute(false) { }
93 void print(const Type *ty, Qualifiers qs, raw_ostream &OS,
94 StringRef PlaceHolder);
95 void print(QualType T, raw_ostream &OS, StringRef PlaceHolder);
97 static bool canPrefixQualifiers(const Type *T, bool &NeedARCStrongQualifier);
98 void spaceBeforePlaceHolder(raw_ostream &OS);
99 void printTypeSpec(const NamedDecl *D, raw_ostream &OS);
101 void printBefore(const Type *ty, Qualifiers qs, raw_ostream &OS);
102 void printBefore(QualType T, raw_ostream &OS);
103 void printAfter(const Type *ty, Qualifiers qs, raw_ostream &OS);
104 void printAfter(QualType T, raw_ostream &OS);
105 void AppendScope(DeclContext *DC, raw_ostream &OS);
106 void printTag(TagDecl *T, raw_ostream &OS);
107 #define ABSTRACT_TYPE(CLASS, PARENT)
108 #define TYPE(CLASS, PARENT) \
109 void print##CLASS##Before(const CLASS##Type *T, raw_ostream &OS); \
110 void print##CLASS##After(const CLASS##Type *T, raw_ostream &OS);
111 #include "clang/AST/TypeNodes.def"
115 static void AppendTypeQualList(raw_ostream &OS, unsigned TypeQuals,
116 bool HasRestrictKeyword) {
117 bool appendSpace = false;
118 if (TypeQuals & Qualifiers::Const) {
122 if (TypeQuals & Qualifiers::Volatile) {
123 if (appendSpace) OS << ' ';
127 if (TypeQuals & Qualifiers::Restrict) {
128 if (appendSpace) OS << ' ';
129 if (HasRestrictKeyword) {
137 void TypePrinter::spaceBeforePlaceHolder(raw_ostream &OS) {
138 if (!HasEmptyPlaceHolder)
142 void TypePrinter::print(QualType t, raw_ostream &OS, StringRef PlaceHolder) {
143 SplitQualType split = t.split();
144 print(split.Ty, split.Quals, OS, PlaceHolder);
147 void TypePrinter::print(const Type *T, Qualifiers Quals, raw_ostream &OS,
148 StringRef PlaceHolder) {
154 SaveAndRestore<bool> PHVal(HasEmptyPlaceHolder, PlaceHolder.empty());
156 printBefore(T, Quals, OS);
158 printAfter(T, Quals, OS);
161 bool TypePrinter::canPrefixQualifiers(const Type *T,
162 bool &NeedARCStrongQualifier) {
163 // CanPrefixQualifiers - We prefer to print type qualifiers before the type,
164 // so that we get "const int" instead of "int const", but we can't do this if
165 // the type is complex. For example if the type is "int*", we *must* print
166 // "int * const", printing "const int *" is different. Only do this when the
167 // type expands to a simple string.
168 bool CanPrefixQualifiers = false;
169 NeedARCStrongQualifier = false;
170 Type::TypeClass TC = T->getTypeClass();
171 if (const AutoType *AT = dyn_cast<AutoType>(T))
172 TC = AT->desugar()->getTypeClass();
173 if (const SubstTemplateTypeParmType *Subst
174 = dyn_cast<SubstTemplateTypeParmType>(T))
175 TC = Subst->getReplacementType()->getTypeClass();
181 case Type::UnresolvedUsing:
183 case Type::TypeOfExpr:
186 case Type::UnaryTransform:
189 case Type::Elaborated:
190 case Type::TemplateTypeParm:
191 case Type::SubstTemplateTypeParmPack:
192 case Type::TemplateSpecialization:
193 case Type::InjectedClassName:
194 case Type::DependentName:
195 case Type::DependentTemplateSpecialization:
196 case Type::ObjCObject:
197 case Type::ObjCInterface:
200 CanPrefixQualifiers = true;
203 case Type::ObjCObjectPointer:
204 CanPrefixQualifiers = T->isObjCIdType() || T->isObjCClassType() ||
205 T->isObjCQualifiedIdType() || T->isObjCQualifiedClassType();
208 case Type::ConstantArray:
209 case Type::IncompleteArray:
210 case Type::VariableArray:
211 case Type::DependentSizedArray:
212 NeedARCStrongQualifier = true;
218 case Type::BlockPointer:
219 case Type::LValueReference:
220 case Type::RValueReference:
221 case Type::MemberPointer:
222 case Type::DependentSizedExtVector:
224 case Type::ExtVector:
225 case Type::FunctionProto:
226 case Type::FunctionNoProto:
228 case Type::Attributed:
229 case Type::PackExpansion:
230 case Type::SubstTemplateTypeParm:
231 CanPrefixQualifiers = false;
235 return CanPrefixQualifiers;
238 void TypePrinter::printBefore(QualType T, raw_ostream &OS) {
239 SplitQualType Split = T.split();
241 // If we have cv1 T, where T is substituted for cv2 U, only print cv1 - cv2
243 Qualifiers Quals = Split.Quals;
244 if (const SubstTemplateTypeParmType *Subst =
245 dyn_cast<SubstTemplateTypeParmType>(Split.Ty))
246 Quals -= QualType(Subst, 0).getQualifiers();
248 printBefore(Split.Ty, Quals, OS);
251 /// \brief Prints the part of the type string before an identifier, e.g. for
252 /// "int foo[10]" it prints "int ".
253 void TypePrinter::printBefore(const Type *T,Qualifiers Quals, raw_ostream &OS) {
254 if (Policy.SuppressSpecifiers && T->isSpecifierType())
257 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder);
259 // Print qualifiers as appropriate.
261 bool CanPrefixQualifiers = false;
262 bool NeedARCStrongQualifier = false;
263 CanPrefixQualifiers = canPrefixQualifiers(T, NeedARCStrongQualifier);
265 if (CanPrefixQualifiers && !Quals.empty()) {
266 if (NeedARCStrongQualifier) {
267 IncludeStrongLifetimeRAII Strong(Policy);
268 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/true);
270 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/true);
274 bool hasAfterQuals = false;
275 if (!CanPrefixQualifiers && !Quals.empty()) {
276 hasAfterQuals = !Quals.isEmptyWhenPrinted(Policy);
278 HasEmptyPlaceHolder = false;
281 switch (T->getTypeClass()) {
282 #define ABSTRACT_TYPE(CLASS, PARENT)
283 #define TYPE(CLASS, PARENT) case Type::CLASS: \
284 print##CLASS##Before(cast<CLASS##Type>(T), OS); \
286 #include "clang/AST/TypeNodes.def"
290 if (NeedARCStrongQualifier) {
291 IncludeStrongLifetimeRAII Strong(Policy);
292 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/!PrevPHIsEmpty.get());
294 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/!PrevPHIsEmpty.get());
299 void TypePrinter::printAfter(QualType t, raw_ostream &OS) {
300 SplitQualType split = t.split();
301 printAfter(split.Ty, split.Quals, OS);
304 /// \brief Prints the part of the type string after an identifier, e.g. for
305 /// "int foo[10]" it prints "[10]".
306 void TypePrinter::printAfter(const Type *T, Qualifiers Quals, raw_ostream &OS) {
307 switch (T->getTypeClass()) {
308 #define ABSTRACT_TYPE(CLASS, PARENT)
309 #define TYPE(CLASS, PARENT) case Type::CLASS: \
310 print##CLASS##After(cast<CLASS##Type>(T), OS); \
312 #include "clang/AST/TypeNodes.def"
316 void TypePrinter::printBuiltinBefore(const BuiltinType *T, raw_ostream &OS) {
317 OS << T->getName(Policy);
318 spaceBeforePlaceHolder(OS);
320 void TypePrinter::printBuiltinAfter(const BuiltinType *T, raw_ostream &OS) { }
322 void TypePrinter::printComplexBefore(const ComplexType *T, raw_ostream &OS) {
324 printBefore(T->getElementType(), OS);
326 void TypePrinter::printComplexAfter(const ComplexType *T, raw_ostream &OS) {
327 printAfter(T->getElementType(), OS);
330 void TypePrinter::printPointerBefore(const PointerType *T, raw_ostream &OS) {
331 IncludeStrongLifetimeRAII Strong(Policy);
332 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
333 printBefore(T->getPointeeType(), OS);
334 // Handle things like 'int (*A)[4];' correctly.
335 // FIXME: this should include vectors, but vectors use attributes I guess.
336 if (isa<ArrayType>(T->getPointeeType()))
340 void TypePrinter::printPointerAfter(const PointerType *T, raw_ostream &OS) {
341 IncludeStrongLifetimeRAII Strong(Policy);
342 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
343 // Handle things like 'int (*A)[4];' correctly.
344 // FIXME: this should include vectors, but vectors use attributes I guess.
345 if (isa<ArrayType>(T->getPointeeType()))
347 printAfter(T->getPointeeType(), OS);
350 void TypePrinter::printBlockPointerBefore(const BlockPointerType *T,
352 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
353 printBefore(T->getPointeeType(), OS);
356 void TypePrinter::printBlockPointerAfter(const BlockPointerType *T,
358 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
359 printAfter(T->getPointeeType(), OS);
362 void TypePrinter::printLValueReferenceBefore(const LValueReferenceType *T,
364 IncludeStrongLifetimeRAII Strong(Policy);
365 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
366 printBefore(T->getPointeeTypeAsWritten(), OS);
367 // Handle things like 'int (&A)[4];' correctly.
368 // FIXME: this should include vectors, but vectors use attributes I guess.
369 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
373 void TypePrinter::printLValueReferenceAfter(const LValueReferenceType *T,
375 IncludeStrongLifetimeRAII Strong(Policy);
376 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
377 // Handle things like 'int (&A)[4];' correctly.
378 // FIXME: this should include vectors, but vectors use attributes I guess.
379 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
381 printAfter(T->getPointeeTypeAsWritten(), OS);
384 void TypePrinter::printRValueReferenceBefore(const RValueReferenceType *T,
386 IncludeStrongLifetimeRAII Strong(Policy);
387 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
388 printBefore(T->getPointeeTypeAsWritten(), OS);
389 // Handle things like 'int (&&A)[4];' correctly.
390 // FIXME: this should include vectors, but vectors use attributes I guess.
391 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
395 void TypePrinter::printRValueReferenceAfter(const RValueReferenceType *T,
397 IncludeStrongLifetimeRAII Strong(Policy);
398 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
399 // Handle things like 'int (&&A)[4];' correctly.
400 // FIXME: this should include vectors, but vectors use attributes I guess.
401 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
403 printAfter(T->getPointeeTypeAsWritten(), OS);
406 void TypePrinter::printMemberPointerBefore(const MemberPointerType *T,
408 IncludeStrongLifetimeRAII Strong(Policy);
409 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
410 printBefore(T->getPointeeType(), OS);
411 // Handle things like 'int (Cls::*A)[4];' correctly.
412 // FIXME: this should include vectors, but vectors use attributes I guess.
413 if (isa<ArrayType>(T->getPointeeType()))
416 PrintingPolicy InnerPolicy(Policy);
417 InnerPolicy.IncludeTagDefinition = false;
418 TypePrinter(InnerPolicy).print(QualType(T->getClass(), 0), OS, StringRef());
422 void TypePrinter::printMemberPointerAfter(const MemberPointerType *T,
424 IncludeStrongLifetimeRAII Strong(Policy);
425 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
426 // Handle things like 'int (Cls::*A)[4];' correctly.
427 // FIXME: this should include vectors, but vectors use attributes I guess.
428 if (isa<ArrayType>(T->getPointeeType()))
430 printAfter(T->getPointeeType(), OS);
433 void TypePrinter::printConstantArrayBefore(const ConstantArrayType *T,
435 IncludeStrongLifetimeRAII Strong(Policy);
436 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
437 printBefore(T->getElementType(), OS);
439 void TypePrinter::printConstantArrayAfter(const ConstantArrayType *T,
442 if (T->getIndexTypeQualifiers().hasQualifiers()) {
443 AppendTypeQualList(OS, T->getIndexTypeCVRQualifiers(),
448 if (T->getSizeModifier() == ArrayType::Static)
451 OS << T->getSize().getZExtValue() << ']';
452 printAfter(T->getElementType(), OS);
455 void TypePrinter::printIncompleteArrayBefore(const IncompleteArrayType *T,
457 IncludeStrongLifetimeRAII Strong(Policy);
458 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
459 printBefore(T->getElementType(), OS);
461 void TypePrinter::printIncompleteArrayAfter(const IncompleteArrayType *T,
464 printAfter(T->getElementType(), OS);
467 void TypePrinter::printVariableArrayBefore(const VariableArrayType *T,
469 IncludeStrongLifetimeRAII Strong(Policy);
470 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
471 printBefore(T->getElementType(), OS);
473 void TypePrinter::printVariableArrayAfter(const VariableArrayType *T,
476 if (T->getIndexTypeQualifiers().hasQualifiers()) {
477 AppendTypeQualList(OS, T->getIndexTypeCVRQualifiers(), Policy.Restrict);
481 if (T->getSizeModifier() == VariableArrayType::Static)
483 else if (T->getSizeModifier() == VariableArrayType::Star)
486 if (T->getSizeExpr())
487 T->getSizeExpr()->printPretty(OS, nullptr, Policy);
490 printAfter(T->getElementType(), OS);
493 void TypePrinter::printAdjustedBefore(const AdjustedType *T, raw_ostream &OS) {
494 // Print the adjusted representation, otherwise the adjustment will be
496 printBefore(T->getAdjustedType(), OS);
498 void TypePrinter::printAdjustedAfter(const AdjustedType *T, raw_ostream &OS) {
499 printAfter(T->getAdjustedType(), OS);
502 void TypePrinter::printDecayedBefore(const DecayedType *T, raw_ostream &OS) {
503 // Print as though it's a pointer.
504 printAdjustedBefore(T, OS);
506 void TypePrinter::printDecayedAfter(const DecayedType *T, raw_ostream &OS) {
507 printAdjustedAfter(T, OS);
510 void TypePrinter::printDependentSizedArrayBefore(
511 const DependentSizedArrayType *T,
513 IncludeStrongLifetimeRAII Strong(Policy);
514 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
515 printBefore(T->getElementType(), OS);
517 void TypePrinter::printDependentSizedArrayAfter(
518 const DependentSizedArrayType *T,
521 if (T->getSizeExpr())
522 T->getSizeExpr()->printPretty(OS, nullptr, Policy);
524 printAfter(T->getElementType(), OS);
527 void TypePrinter::printDependentSizedExtVectorBefore(
528 const DependentSizedExtVectorType *T,
530 printBefore(T->getElementType(), OS);
532 void TypePrinter::printDependentSizedExtVectorAfter(
533 const DependentSizedExtVectorType *T,
535 OS << " __attribute__((ext_vector_type(";
536 if (T->getSizeExpr())
537 T->getSizeExpr()->printPretty(OS, nullptr, Policy);
539 printAfter(T->getElementType(), OS);
542 void TypePrinter::printVectorBefore(const VectorType *T, raw_ostream &OS) {
543 switch (T->getVectorKind()) {
544 case VectorType::AltiVecPixel:
545 OS << "__vector __pixel ";
547 case VectorType::AltiVecBool:
548 OS << "__vector __bool ";
549 printBefore(T->getElementType(), OS);
551 case VectorType::AltiVecVector:
553 printBefore(T->getElementType(), OS);
555 case VectorType::NeonVector:
556 OS << "__attribute__((neon_vector_type("
557 << T->getNumElements() << "))) ";
558 printBefore(T->getElementType(), OS);
560 case VectorType::NeonPolyVector:
561 OS << "__attribute__((neon_polyvector_type(" <<
562 T->getNumElements() << "))) ";
563 printBefore(T->getElementType(), OS);
565 case VectorType::GenericVector: {
566 // FIXME: We prefer to print the size directly here, but have no way
567 // to get the size of the type.
568 OS << "__attribute__((__vector_size__("
569 << T->getNumElements()
571 print(T->getElementType(), OS, StringRef());
573 printBefore(T->getElementType(), OS);
578 void TypePrinter::printVectorAfter(const VectorType *T, raw_ostream &OS) {
579 printAfter(T->getElementType(), OS);
582 void TypePrinter::printExtVectorBefore(const ExtVectorType *T,
584 printBefore(T->getElementType(), OS);
586 void TypePrinter::printExtVectorAfter(const ExtVectorType *T, raw_ostream &OS) {
587 printAfter(T->getElementType(), OS);
588 OS << " __attribute__((ext_vector_type(";
589 OS << T->getNumElements();
594 FunctionProtoType::printExceptionSpecification(raw_ostream &OS,
595 const PrintingPolicy &Policy)
598 if (hasDynamicExceptionSpec()) {
600 if (getExceptionSpecType() == EST_MSAny)
603 for (unsigned I = 0, N = getNumExceptions(); I != N; ++I) {
607 OS << getExceptionType(I).stream(Policy);
610 } else if (isNoexceptExceptionSpec(getExceptionSpecType())) {
612 if (getExceptionSpecType() == EST_ComputedNoexcept) {
614 if (getNoexceptExpr())
615 getNoexceptExpr()->printPretty(OS, nullptr, Policy);
621 void TypePrinter::printFunctionProtoBefore(const FunctionProtoType *T,
623 if (T->hasTrailingReturn()) {
625 if (!HasEmptyPlaceHolder)
628 // If needed for precedence reasons, wrap the inner part in grouping parens.
629 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false);
630 printBefore(T->getReturnType(), OS);
631 if (!PrevPHIsEmpty.get())
636 llvm::StringRef clang::getParameterABISpelling(ParameterABI ABI) {
638 case ParameterABI::Ordinary:
639 llvm_unreachable("asking for spelling of ordinary parameter ABI");
640 case ParameterABI::SwiftContext:
641 return "swift_context";
642 case ParameterABI::SwiftErrorResult:
643 return "swift_error_result";
644 case ParameterABI::SwiftIndirectResult:
645 return "swift_indirect_result";
647 llvm_unreachable("bad parameter ABI kind");
650 void TypePrinter::printFunctionProtoAfter(const FunctionProtoType *T,
652 // If needed for precedence reasons, wrap the inner part in grouping parens.
653 if (!HasEmptyPlaceHolder)
655 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
659 ParamPolicyRAII ParamPolicy(Policy);
660 for (unsigned i = 0, e = T->getNumParams(); i != e; ++i) {
663 auto EPI = T->getExtParameterInfo(i);
664 if (EPI.isConsumed()) OS << "__attribute__((ns_consumed)) ";
665 auto ABI = EPI.getABI();
666 if (ABI != ParameterABI::Ordinary)
667 OS << "__attribute__((" << getParameterABISpelling(ABI) << ")) ";
669 print(T->getParamType(i), OS, StringRef());
673 if (T->isVariadic()) {
674 if (T->getNumParams())
677 } else if (T->getNumParams() == 0 && Policy.UseVoidForZeroParams) {
678 // Do not emit int() if we have a proto, emit 'int(void)'.
684 FunctionType::ExtInfo Info = T->getExtInfo();
686 if (!InsideCCAttribute) {
687 switch (Info.getCC()) {
689 // The C calling convention is the default on the vast majority of platforms
690 // we support. If the user wrote it explicitly, it will usually be printed
691 // while traversing the AttributedType. If the type has been desugared, let
692 // the canonical spelling be the implicit calling convention.
693 // FIXME: It would be better to be explicit in certain contexts, such as a
694 // cdecl function typedef used to declare a member function with the
695 // Microsoft C++ ABI.
698 OS << " __attribute__((stdcall))";
701 OS << " __attribute__((fastcall))";
704 OS << " __attribute__((thiscall))";
706 case CC_X86VectorCall:
707 OS << " __attribute__((vectorcall))";
710 OS << " __attribute__((pascal))";
713 OS << " __attribute__((pcs(\"aapcs\")))";
716 OS << " __attribute__((pcs(\"aapcs-vfp\")))";
718 case CC_IntelOclBicc:
719 OS << " __attribute__((intel_ocl_bicc))";
722 OS << " __attribute__((ms_abi))";
725 OS << " __attribute__((sysv_abi))";
727 case CC_SpirFunction:
728 case CC_OpenCLKernel:
729 // Do nothing. These CCs are not available as attributes.
732 OS << " __attribute__((swiftcall))";
734 case CC_PreserveMost:
735 OS << " __attribute__((preserve_most))";
738 OS << " __attribute__((preserve_all))";
743 if (Info.getNoReturn())
744 OS << " __attribute__((noreturn))";
745 if (Info.getRegParm())
746 OS << " __attribute__((regparm ("
747 << Info.getRegParm() << ")))";
749 if (unsigned quals = T->getTypeQuals()) {
751 AppendTypeQualList(OS, quals, Policy.Restrict);
754 switch (T->getRefQualifier()) {
766 T->printExceptionSpecification(OS, Policy);
768 if (T->hasTrailingReturn()) {
770 print(T->getReturnType(), OS, StringRef());
772 printAfter(T->getReturnType(), OS);
775 void TypePrinter::printFunctionNoProtoBefore(const FunctionNoProtoType *T,
777 // If needed for precedence reasons, wrap the inner part in grouping parens.
778 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false);
779 printBefore(T->getReturnType(), OS);
780 if (!PrevPHIsEmpty.get())
783 void TypePrinter::printFunctionNoProtoAfter(const FunctionNoProtoType *T,
785 // If needed for precedence reasons, wrap the inner part in grouping parens.
786 if (!HasEmptyPlaceHolder)
788 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
791 if (T->getNoReturnAttr())
792 OS << " __attribute__((noreturn))";
793 printAfter(T->getReturnType(), OS);
796 void TypePrinter::printTypeSpec(const NamedDecl *D, raw_ostream &OS) {
797 IdentifierInfo *II = D->getIdentifier();
799 spaceBeforePlaceHolder(OS);
802 void TypePrinter::printUnresolvedUsingBefore(const UnresolvedUsingType *T,
804 printTypeSpec(T->getDecl(), OS);
806 void TypePrinter::printUnresolvedUsingAfter(const UnresolvedUsingType *T,
809 void TypePrinter::printTypedefBefore(const TypedefType *T, raw_ostream &OS) {
810 printTypeSpec(T->getDecl(), OS);
812 void TypePrinter::printTypedefAfter(const TypedefType *T, raw_ostream &OS) { }
814 void TypePrinter::printTypeOfExprBefore(const TypeOfExprType *T,
817 if (T->getUnderlyingExpr())
818 T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy);
819 spaceBeforePlaceHolder(OS);
821 void TypePrinter::printTypeOfExprAfter(const TypeOfExprType *T,
824 void TypePrinter::printTypeOfBefore(const TypeOfType *T, raw_ostream &OS) {
826 print(T->getUnderlyingType(), OS, StringRef());
828 spaceBeforePlaceHolder(OS);
830 void TypePrinter::printTypeOfAfter(const TypeOfType *T, raw_ostream &OS) { }
832 void TypePrinter::printDecltypeBefore(const DecltypeType *T, raw_ostream &OS) {
834 if (T->getUnderlyingExpr())
835 T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy);
837 spaceBeforePlaceHolder(OS);
839 void TypePrinter::printDecltypeAfter(const DecltypeType *T, raw_ostream &OS) { }
841 void TypePrinter::printUnaryTransformBefore(const UnaryTransformType *T,
843 IncludeStrongLifetimeRAII Strong(Policy);
845 switch (T->getUTTKind()) {
846 case UnaryTransformType::EnumUnderlyingType:
847 OS << "__underlying_type(";
848 print(T->getBaseType(), OS, StringRef());
850 spaceBeforePlaceHolder(OS);
854 printBefore(T->getBaseType(), OS);
856 void TypePrinter::printUnaryTransformAfter(const UnaryTransformType *T,
858 IncludeStrongLifetimeRAII Strong(Policy);
860 switch (T->getUTTKind()) {
861 case UnaryTransformType::EnumUnderlyingType:
865 printAfter(T->getBaseType(), OS);
868 void TypePrinter::printAutoBefore(const AutoType *T, raw_ostream &OS) {
869 // If the type has been deduced, do not print 'auto'.
870 if (!T->getDeducedType().isNull()) {
871 printBefore(T->getDeducedType(), OS);
873 switch (T->getKeyword()) {
874 case AutoTypeKeyword::Auto: OS << "auto"; break;
875 case AutoTypeKeyword::DecltypeAuto: OS << "decltype(auto)"; break;
876 case AutoTypeKeyword::GNUAutoType: OS << "__auto_type"; break;
878 spaceBeforePlaceHolder(OS);
881 void TypePrinter::printAutoAfter(const AutoType *T, raw_ostream &OS) {
882 // If the type has been deduced, do not print 'auto'.
883 if (!T->getDeducedType().isNull())
884 printAfter(T->getDeducedType(), OS);
887 void TypePrinter::printAtomicBefore(const AtomicType *T, raw_ostream &OS) {
888 IncludeStrongLifetimeRAII Strong(Policy);
891 print(T->getValueType(), OS, StringRef());
893 spaceBeforePlaceHolder(OS);
895 void TypePrinter::printAtomicAfter(const AtomicType *T, raw_ostream &OS) { }
897 void TypePrinter::printPipeBefore(const PipeType *T, raw_ostream &OS) {
898 IncludeStrongLifetimeRAII Strong(Policy);
901 print(T->getElementType(), OS, StringRef());
902 spaceBeforePlaceHolder(OS);
905 void TypePrinter::printPipeAfter(const PipeType *T, raw_ostream &OS) {
907 /// Appends the given scope to the end of a string.
908 void TypePrinter::AppendScope(DeclContext *DC, raw_ostream &OS) {
909 if (DC->isTranslationUnit()) return;
910 if (DC->isFunctionOrMethod()) return;
911 AppendScope(DC->getParent(), OS);
913 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
914 if (Policy.SuppressUnwrittenScope &&
915 (NS->isAnonymousNamespace() || NS->isInline()))
917 if (NS->getIdentifier())
918 OS << NS->getName() << "::";
920 OS << "(anonymous namespace)::";
921 } else if (ClassTemplateSpecializationDecl *Spec
922 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
923 IncludeStrongLifetimeRAII Strong(Policy);
924 OS << Spec->getIdentifier()->getName();
925 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
926 TemplateSpecializationType::PrintTemplateArgumentList(
927 OS, TemplateArgs.asArray(), Policy);
929 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
930 if (TypedefNameDecl *Typedef = Tag->getTypedefNameForAnonDecl())
931 OS << Typedef->getIdentifier()->getName() << "::";
932 else if (Tag->getIdentifier())
933 OS << Tag->getIdentifier()->getName() << "::";
939 void TypePrinter::printTag(TagDecl *D, raw_ostream &OS) {
940 if (Policy.IncludeTagDefinition) {
941 PrintingPolicy SubPolicy = Policy;
942 SubPolicy.IncludeTagDefinition = false;
943 D->print(OS, SubPolicy, Indentation);
944 spaceBeforePlaceHolder(OS);
948 bool HasKindDecoration = false;
950 // We don't print tags unless this is an elaborated type.
951 // In C, we just assume every RecordType is an elaborated type.
952 if (!Policy.SuppressTagKeyword && !D->getTypedefNameForAnonDecl()) {
953 HasKindDecoration = true;
954 OS << D->getKindName();
958 // Compute the full nested-name-specifier for this type.
959 // In C, this will always be empty except when the type
960 // being printed is anonymous within other Record.
961 if (!Policy.SuppressScope)
962 AppendScope(D->getDeclContext(), OS);
964 if (const IdentifierInfo *II = D->getIdentifier())
966 else if (TypedefNameDecl *Typedef = D->getTypedefNameForAnonDecl()) {
967 assert(Typedef->getIdentifier() && "Typedef without identifier?");
968 OS << Typedef->getIdentifier()->getName();
970 // Make an unambiguous representation for anonymous types, e.g.
971 // (anonymous enum at /usr/include/string.h:120:9)
972 OS << (Policy.MSVCFormatting ? '`' : '(');
974 if (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda()) {
976 HasKindDecoration = true;
981 if (Policy.AnonymousTagLocations) {
982 // Suppress the redundant tag keyword if we just printed one.
983 // We don't have to worry about ElaboratedTypes here because you can't
984 // refer to an anonymous type with one.
985 if (!HasKindDecoration)
986 OS << " " << D->getKindName();
988 PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc(
990 if (PLoc.isValid()) {
991 OS << " at " << PLoc.getFilename()
992 << ':' << PLoc.getLine()
993 << ':' << PLoc.getColumn();
997 OS << (Policy.MSVCFormatting ? '\'' : ')');
1000 // If this is a class template specialization, print the template
1002 if (ClassTemplateSpecializationDecl *Spec
1003 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
1004 ArrayRef<TemplateArgument> Args;
1005 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
1006 const TemplateSpecializationType *TST =
1007 cast<TemplateSpecializationType>(TAW->getType());
1008 Args = TST->template_arguments();
1010 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1011 Args = TemplateArgs.asArray();
1013 IncludeStrongLifetimeRAII Strong(Policy);
1014 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, Policy);
1017 spaceBeforePlaceHolder(OS);
1020 void TypePrinter::printRecordBefore(const RecordType *T, raw_ostream &OS) {
1021 printTag(T->getDecl(), OS);
1023 void TypePrinter::printRecordAfter(const RecordType *T, raw_ostream &OS) { }
1025 void TypePrinter::printEnumBefore(const EnumType *T, raw_ostream &OS) {
1026 printTag(T->getDecl(), OS);
1028 void TypePrinter::printEnumAfter(const EnumType *T, raw_ostream &OS) { }
1030 void TypePrinter::printTemplateTypeParmBefore(const TemplateTypeParmType *T,
1032 if (IdentifierInfo *Id = T->getIdentifier())
1033 OS << Id->getName();
1035 OS << "type-parameter-" << T->getDepth() << '-' << T->getIndex();
1036 spaceBeforePlaceHolder(OS);
1038 void TypePrinter::printTemplateTypeParmAfter(const TemplateTypeParmType *T,
1039 raw_ostream &OS) { }
1041 void TypePrinter::printSubstTemplateTypeParmBefore(
1042 const SubstTemplateTypeParmType *T,
1044 IncludeStrongLifetimeRAII Strong(Policy);
1045 printBefore(T->getReplacementType(), OS);
1047 void TypePrinter::printSubstTemplateTypeParmAfter(
1048 const SubstTemplateTypeParmType *T,
1050 IncludeStrongLifetimeRAII Strong(Policy);
1051 printAfter(T->getReplacementType(), OS);
1054 void TypePrinter::printSubstTemplateTypeParmPackBefore(
1055 const SubstTemplateTypeParmPackType *T,
1057 IncludeStrongLifetimeRAII Strong(Policy);
1058 printTemplateTypeParmBefore(T->getReplacedParameter(), OS);
1060 void TypePrinter::printSubstTemplateTypeParmPackAfter(
1061 const SubstTemplateTypeParmPackType *T,
1063 IncludeStrongLifetimeRAII Strong(Policy);
1064 printTemplateTypeParmAfter(T->getReplacedParameter(), OS);
1067 void TypePrinter::printTemplateSpecializationBefore(
1068 const TemplateSpecializationType *T,
1070 IncludeStrongLifetimeRAII Strong(Policy);
1071 T->getTemplateName().print(OS, Policy);
1073 TemplateSpecializationType::PrintTemplateArgumentList(
1074 OS, T->template_arguments(), Policy);
1075 spaceBeforePlaceHolder(OS);
1077 void TypePrinter::printTemplateSpecializationAfter(
1078 const TemplateSpecializationType *T,
1079 raw_ostream &OS) { }
1081 void TypePrinter::printInjectedClassNameBefore(const InjectedClassNameType *T,
1083 printTemplateSpecializationBefore(T->getInjectedTST(), OS);
1085 void TypePrinter::printInjectedClassNameAfter(const InjectedClassNameType *T,
1086 raw_ostream &OS) { }
1088 void TypePrinter::printElaboratedBefore(const ElaboratedType *T,
1090 // The tag definition will take care of these.
1091 if (!Policy.IncludeTagDefinition)
1093 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1094 if (T->getKeyword() != ETK_None)
1096 NestedNameSpecifier* Qualifier = T->getQualifier();
1098 Qualifier->print(OS, Policy);
1101 ElaboratedTypePolicyRAII PolicyRAII(Policy);
1102 printBefore(T->getNamedType(), OS);
1104 void TypePrinter::printElaboratedAfter(const ElaboratedType *T,
1106 ElaboratedTypePolicyRAII PolicyRAII(Policy);
1107 printAfter(T->getNamedType(), OS);
1110 void TypePrinter::printParenBefore(const ParenType *T, raw_ostream &OS) {
1111 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) {
1112 printBefore(T->getInnerType(), OS);
1115 printBefore(T->getInnerType(), OS);
1117 void TypePrinter::printParenAfter(const ParenType *T, raw_ostream &OS) {
1118 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) {
1120 printAfter(T->getInnerType(), OS);
1122 printAfter(T->getInnerType(), OS);
1125 void TypePrinter::printDependentNameBefore(const DependentNameType *T,
1127 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1128 if (T->getKeyword() != ETK_None)
1131 T->getQualifier()->print(OS, Policy);
1133 OS << T->getIdentifier()->getName();
1134 spaceBeforePlaceHolder(OS);
1136 void TypePrinter::printDependentNameAfter(const DependentNameType *T,
1137 raw_ostream &OS) { }
1139 void TypePrinter::printDependentTemplateSpecializationBefore(
1140 const DependentTemplateSpecializationType *T, raw_ostream &OS) {
1141 IncludeStrongLifetimeRAII Strong(Policy);
1143 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1144 if (T->getKeyword() != ETK_None)
1147 if (T->getQualifier())
1148 T->getQualifier()->print(OS, Policy);
1149 OS << T->getIdentifier()->getName();
1150 TemplateSpecializationType::PrintTemplateArgumentList(OS,
1151 T->template_arguments(),
1153 spaceBeforePlaceHolder(OS);
1155 void TypePrinter::printDependentTemplateSpecializationAfter(
1156 const DependentTemplateSpecializationType *T, raw_ostream &OS) { }
1158 void TypePrinter::printPackExpansionBefore(const PackExpansionType *T,
1160 printBefore(T->getPattern(), OS);
1162 void TypePrinter::printPackExpansionAfter(const PackExpansionType *T,
1164 printAfter(T->getPattern(), OS);
1168 void TypePrinter::printAttributedBefore(const AttributedType *T,
1170 // Prefer the macro forms of the GC and ownership qualifiers.
1171 if (T->getAttrKind() == AttributedType::attr_objc_gc ||
1172 T->getAttrKind() == AttributedType::attr_objc_ownership)
1173 return printBefore(T->getEquivalentType(), OS);
1175 if (T->getAttrKind() == AttributedType::attr_objc_kindof)
1178 printBefore(T->getModifiedType(), OS);
1180 if (T->isMSTypeSpec()) {
1181 switch (T->getAttrKind()) {
1183 case AttributedType::attr_ptr32: OS << " __ptr32"; break;
1184 case AttributedType::attr_ptr64: OS << " __ptr64"; break;
1185 case AttributedType::attr_sptr: OS << " __sptr"; break;
1186 case AttributedType::attr_uptr: OS << " __uptr"; break;
1188 spaceBeforePlaceHolder(OS);
1191 // Print nullability type specifiers.
1192 if (T->getAttrKind() == AttributedType::attr_nonnull ||
1193 T->getAttrKind() == AttributedType::attr_nullable ||
1194 T->getAttrKind() == AttributedType::attr_null_unspecified) {
1195 if (T->getAttrKind() == AttributedType::attr_nonnull)
1197 else if (T->getAttrKind() == AttributedType::attr_nullable)
1199 else if (T->getAttrKind() == AttributedType::attr_null_unspecified)
1200 OS << " _Null_unspecified";
1202 llvm_unreachable("unhandled nullability");
1203 spaceBeforePlaceHolder(OS);
1207 void TypePrinter::printAttributedAfter(const AttributedType *T,
1209 // Prefer the macro forms of the GC and ownership qualifiers.
1210 if (T->getAttrKind() == AttributedType::attr_objc_gc ||
1211 T->getAttrKind() == AttributedType::attr_objc_ownership)
1212 return printAfter(T->getEquivalentType(), OS);
1214 if (T->getAttrKind() == AttributedType::attr_objc_kindof)
1217 // TODO: not all attributes are GCC-style attributes.
1218 if (T->isMSTypeSpec())
1221 // Nothing to print after.
1222 if (T->getAttrKind() == AttributedType::attr_nonnull ||
1223 T->getAttrKind() == AttributedType::attr_nullable ||
1224 T->getAttrKind() == AttributedType::attr_null_unspecified)
1225 return printAfter(T->getModifiedType(), OS);
1227 // If this is a calling convention attribute, don't print the implicit CC from
1228 // the modified type.
1229 SaveAndRestore<bool> MaybeSuppressCC(InsideCCAttribute, T->isCallingConv());
1231 printAfter(T->getModifiedType(), OS);
1233 // Don't print the inert __unsafe_unretained attribute at all.
1234 if (T->getAttrKind() == AttributedType::attr_objc_inert_unsafe_unretained)
1237 // Print nullability type specifiers that occur after
1238 if (T->getAttrKind() == AttributedType::attr_nonnull ||
1239 T->getAttrKind() == AttributedType::attr_nullable ||
1240 T->getAttrKind() == AttributedType::attr_null_unspecified) {
1241 if (T->getAttrKind() == AttributedType::attr_nonnull)
1243 else if (T->getAttrKind() == AttributedType::attr_nullable)
1245 else if (T->getAttrKind() == AttributedType::attr_null_unspecified)
1246 OS << " _Null_unspecified";
1248 llvm_unreachable("unhandled nullability");
1253 OS << " __attribute__((";
1254 switch (T->getAttrKind()) {
1255 default: llvm_unreachable("This attribute should have been handled already");
1256 case AttributedType::attr_address_space:
1257 OS << "address_space(";
1258 OS << T->getEquivalentType().getAddressSpace();
1262 case AttributedType::attr_vector_size: {
1263 OS << "__vector_size__(";
1264 if (const VectorType *vector =T->getEquivalentType()->getAs<VectorType>()) {
1265 OS << vector->getNumElements();
1267 print(vector->getElementType(), OS, StringRef());
1274 case AttributedType::attr_neon_vector_type:
1275 case AttributedType::attr_neon_polyvector_type: {
1276 if (T->getAttrKind() == AttributedType::attr_neon_vector_type)
1277 OS << "neon_vector_type(";
1279 OS << "neon_polyvector_type(";
1280 const VectorType *vector = T->getEquivalentType()->getAs<VectorType>();
1281 OS << vector->getNumElements();
1286 case AttributedType::attr_regparm: {
1287 // FIXME: When Sema learns to form this AttributedType, avoid printing the
1288 // attribute again in printFunctionProtoAfter.
1290 QualType t = T->getEquivalentType();
1291 while (!t->isFunctionType())
1292 t = t->getPointeeType();
1293 OS << t->getAs<FunctionType>()->getRegParmType();
1298 case AttributedType::attr_objc_gc: {
1301 QualType tmp = T->getEquivalentType();
1302 while (tmp.getObjCGCAttr() == Qualifiers::GCNone) {
1303 QualType next = tmp->getPointeeType();
1304 if (next == tmp) break;
1308 if (tmp.isObjCGCWeak())
1316 case AttributedType::attr_objc_ownership:
1317 OS << "objc_ownership(";
1318 switch (T->getEquivalentType().getObjCLifetime()) {
1319 case Qualifiers::OCL_None: llvm_unreachable("no ownership!");
1320 case Qualifiers::OCL_ExplicitNone: OS << "none"; break;
1321 case Qualifiers::OCL_Strong: OS << "strong"; break;
1322 case Qualifiers::OCL_Weak: OS << "weak"; break;
1323 case Qualifiers::OCL_Autoreleasing: OS << "autoreleasing"; break;
1328 // FIXME: When Sema learns to form this AttributedType, avoid printing the
1329 // attribute again in printFunctionProtoAfter.
1330 case AttributedType::attr_noreturn: OS << "noreturn"; break;
1332 case AttributedType::attr_cdecl: OS << "cdecl"; break;
1333 case AttributedType::attr_fastcall: OS << "fastcall"; break;
1334 case AttributedType::attr_stdcall: OS << "stdcall"; break;
1335 case AttributedType::attr_thiscall: OS << "thiscall"; break;
1336 case AttributedType::attr_swiftcall: OS << "swiftcall"; break;
1337 case AttributedType::attr_vectorcall: OS << "vectorcall"; break;
1338 case AttributedType::attr_pascal: OS << "pascal"; break;
1339 case AttributedType::attr_ms_abi: OS << "ms_abi"; break;
1340 case AttributedType::attr_sysv_abi: OS << "sysv_abi"; break;
1341 case AttributedType::attr_pcs:
1342 case AttributedType::attr_pcs_vfp: {
1344 QualType t = T->getEquivalentType();
1345 while (!t->isFunctionType())
1346 t = t->getPointeeType();
1347 OS << (t->getAs<FunctionType>()->getCallConv() == CC_AAPCS ?
1348 "\"aapcs\"" : "\"aapcs-vfp\"");
1352 case AttributedType::attr_inteloclbicc: OS << "inteloclbicc"; break;
1353 case AttributedType::attr_preserve_most:
1354 OS << "preserve_most";
1356 case AttributedType::attr_preserve_all:
1357 OS << "preserve_all";
1363 void TypePrinter::printObjCInterfaceBefore(const ObjCInterfaceType *T,
1365 OS << T->getDecl()->getName();
1366 spaceBeforePlaceHolder(OS);
1368 void TypePrinter::printObjCInterfaceAfter(const ObjCInterfaceType *T,
1369 raw_ostream &OS) { }
1371 void TypePrinter::printObjCObjectBefore(const ObjCObjectType *T,
1373 if (T->qual_empty() && T->isUnspecializedAsWritten() &&
1374 !T->isKindOfTypeAsWritten())
1375 return printBefore(T->getBaseType(), OS);
1377 if (T->isKindOfTypeAsWritten())
1380 print(T->getBaseType(), OS, StringRef());
1382 if (T->isSpecializedAsWritten()) {
1383 bool isFirst = true;
1385 for (auto typeArg : T->getTypeArgsAsWritten()) {
1391 print(typeArg, OS, StringRef());
1396 if (!T->qual_empty()) {
1397 bool isFirst = true;
1399 for (const auto *I : T->quals()) {
1409 spaceBeforePlaceHolder(OS);
1411 void TypePrinter::printObjCObjectAfter(const ObjCObjectType *T,
1413 if (T->qual_empty() && T->isUnspecializedAsWritten() &&
1414 !T->isKindOfTypeAsWritten())
1415 return printAfter(T->getBaseType(), OS);
1418 void TypePrinter::printObjCObjectPointerBefore(const ObjCObjectPointerType *T,
1420 printBefore(T->getPointeeType(), OS);
1422 // If we need to print the pointer, print it now.
1423 if (!T->isObjCIdType() && !T->isObjCQualifiedIdType() &&
1424 !T->isObjCClassType() && !T->isObjCQualifiedClassType()) {
1425 if (HasEmptyPlaceHolder)
1430 void TypePrinter::printObjCObjectPointerAfter(const ObjCObjectPointerType *T,
1431 raw_ostream &OS) { }
1433 void TemplateSpecializationType::
1434 PrintTemplateArgumentList(raw_ostream &OS,
1435 const TemplateArgumentListInfo &Args,
1436 const PrintingPolicy &Policy) {
1437 return PrintTemplateArgumentList(OS,
1442 void TemplateSpecializationType::PrintTemplateArgumentList(
1443 raw_ostream &OS, ArrayRef<TemplateArgument> Args,
1444 const PrintingPolicy &Policy, bool SkipBrackets) {
1445 const char *Comma = Policy.MSVCFormatting ? "," : ", ";
1449 bool needSpace = false;
1450 bool FirstArg = true;
1451 for (const TemplateArgument &Arg : Args) {
1452 // Print the argument into a string.
1453 SmallString<128> Buf;
1454 llvm::raw_svector_ostream ArgOS(Buf);
1455 if (Arg.getKind() == TemplateArgument::Pack) {
1456 if (Arg.pack_size() && !FirstArg)
1458 PrintTemplateArgumentList(ArgOS,
1459 Arg.getPackAsArray(),
1464 Arg.print(Policy, ArgOS);
1466 StringRef ArgString = ArgOS.str();
1468 // If this is the first argument and its string representation
1469 // begins with the global scope specifier ('::foo'), add a space
1470 // to avoid printing the diagraph '<:'.
1471 if (FirstArg && !ArgString.empty() && ArgString[0] == ':')
1476 needSpace = (!ArgString.empty() && ArgString.back() == '>');
1480 // If the last character of our string is '>', add another space to
1481 // keep the two '>''s separate tokens. We don't *have* to do this in
1482 // C++0x, but it's still good hygiene.
1490 // Sadly, repeat all that with TemplateArgLoc.
1491 void TemplateSpecializationType::
1492 PrintTemplateArgumentList(raw_ostream &OS,
1493 ArrayRef<TemplateArgumentLoc> Args,
1494 const PrintingPolicy &Policy) {
1496 const char *Comma = Policy.MSVCFormatting ? "," : ", ";
1498 bool needSpace = false;
1499 bool FirstArg = true;
1500 for (const TemplateArgumentLoc &Arg : Args) {
1504 // Print the argument into a string.
1505 SmallString<128> Buf;
1506 llvm::raw_svector_ostream ArgOS(Buf);
1507 if (Arg.getArgument().getKind() == TemplateArgument::Pack) {
1508 PrintTemplateArgumentList(ArgOS,
1509 Arg.getArgument().getPackAsArray(),
1512 Arg.getArgument().print(Policy, ArgOS);
1514 StringRef ArgString = ArgOS.str();
1516 // If this is the first argument and its string representation
1517 // begins with the global scope specifier ('::foo'), add a space
1518 // to avoid printing the diagraph '<:'.
1519 if (FirstArg && !ArgString.empty() && ArgString[0] == ':')
1524 needSpace = (!ArgString.empty() && ArgString.back() == '>');
1528 // If the last character of our string is '>', add another space to
1529 // keep the two '>''s separate tokens. We don't *have* to do this in
1530 // C++0x, but it's still good hygiene.
1537 std::string Qualifiers::getAsString() const {
1539 return getAsString(PrintingPolicy(LO));
1542 // Appends qualifiers to the given string, separated by spaces. Will
1543 // prefix a space if the string is non-empty. Will not append a final
1545 std::string Qualifiers::getAsString(const PrintingPolicy &Policy) const {
1546 SmallString<64> Buf;
1547 llvm::raw_svector_ostream StrOS(Buf);
1548 print(StrOS, Policy);
1552 bool Qualifiers::isEmptyWhenPrinted(const PrintingPolicy &Policy) const {
1553 if (getCVRQualifiers())
1556 if (getAddressSpace())
1559 if (getObjCGCAttr())
1562 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime())
1563 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime))
1569 // Appends qualifiers to the given string, separated by spaces. Will
1570 // prefix a space if the string is non-empty. Will not append a final
1572 void Qualifiers::print(raw_ostream &OS, const PrintingPolicy& Policy,
1573 bool appendSpaceIfNonEmpty) const {
1574 bool addSpace = false;
1576 unsigned quals = getCVRQualifiers();
1578 AppendTypeQualList(OS, quals, Policy.Restrict);
1581 if (hasUnaligned()) {
1584 OS << "__unaligned";
1587 if (unsigned addrspace = getAddressSpace()) {
1591 switch (addrspace) {
1592 case LangAS::opencl_global:
1595 case LangAS::opencl_local:
1598 case LangAS::opencl_constant:
1601 case LangAS::opencl_generic:
1605 OS << "__attribute__((address_space(";
1610 if (Qualifiers::GC gc = getObjCGCAttr()) {
1614 if (gc == Qualifiers::Weak)
1619 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) {
1620 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime)){
1627 case Qualifiers::OCL_None: llvm_unreachable("none but true");
1628 case Qualifiers::OCL_ExplicitNone: OS << "__unsafe_unretained"; break;
1629 case Qualifiers::OCL_Strong:
1630 if (!Policy.SuppressStrongLifetime)
1634 case Qualifiers::OCL_Weak: OS << "__weak"; break;
1635 case Qualifiers::OCL_Autoreleasing: OS << "__autoreleasing"; break;
1639 if (appendSpaceIfNonEmpty && addSpace)
1643 std::string QualType::getAsString(const PrintingPolicy &Policy) const {
1645 getAsStringInternal(S, Policy);
1649 std::string QualType::getAsString(const Type *ty, Qualifiers qs) {
1651 LangOptions options;
1652 getAsStringInternal(ty, qs, buffer, PrintingPolicy(options));
1656 void QualType::print(const Type *ty, Qualifiers qs,
1657 raw_ostream &OS, const PrintingPolicy &policy,
1658 const Twine &PlaceHolder, unsigned Indentation) {
1659 SmallString<128> PHBuf;
1660 StringRef PH = PlaceHolder.toStringRef(PHBuf);
1662 TypePrinter(policy, Indentation).print(ty, qs, OS, PH);
1665 void QualType::getAsStringInternal(const Type *ty, Qualifiers qs,
1666 std::string &buffer,
1667 const PrintingPolicy &policy) {
1668 SmallString<256> Buf;
1669 llvm::raw_svector_ostream StrOS(Buf);
1670 TypePrinter(policy).print(ty, qs, StrOS, buffer);
1671 std::string str = StrOS.str();