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(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::DeducedTemplateSpecialization:
193 case Type::TemplateSpecialization:
194 case Type::InjectedClassName:
195 case Type::DependentName:
196 case Type::DependentTemplateSpecialization:
197 case Type::ObjCObject:
198 case Type::ObjCTypeParam:
199 case Type::ObjCInterface:
202 CanPrefixQualifiers = true;
205 case Type::ObjCObjectPointer:
206 CanPrefixQualifiers = T->isObjCIdType() || T->isObjCClassType() ||
207 T->isObjCQualifiedIdType() || T->isObjCQualifiedClassType();
210 case Type::ConstantArray:
211 case Type::IncompleteArray:
212 case Type::VariableArray:
213 case Type::DependentSizedArray:
214 NeedARCStrongQualifier = true;
220 case Type::BlockPointer:
221 case Type::LValueReference:
222 case Type::RValueReference:
223 case Type::MemberPointer:
224 case Type::DependentSizedExtVector:
226 case Type::ExtVector:
227 case Type::FunctionProto:
228 case Type::FunctionNoProto:
230 case Type::Attributed:
231 case Type::PackExpansion:
232 case Type::SubstTemplateTypeParm:
233 CanPrefixQualifiers = false;
237 return CanPrefixQualifiers;
240 void TypePrinter::printBefore(QualType T, raw_ostream &OS) {
241 SplitQualType Split = T.split();
243 // If we have cv1 T, where T is substituted for cv2 U, only print cv1 - cv2
245 Qualifiers Quals = Split.Quals;
246 if (const SubstTemplateTypeParmType *Subst =
247 dyn_cast<SubstTemplateTypeParmType>(Split.Ty))
248 Quals -= QualType(Subst, 0).getQualifiers();
250 printBefore(Split.Ty, Quals, OS);
253 /// \brief Prints the part of the type string before an identifier, e.g. for
254 /// "int foo[10]" it prints "int ".
255 void TypePrinter::printBefore(const Type *T,Qualifiers Quals, raw_ostream &OS) {
256 if (Policy.SuppressSpecifiers && T->isSpecifierType())
259 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder);
261 // Print qualifiers as appropriate.
263 bool CanPrefixQualifiers = false;
264 bool NeedARCStrongQualifier = false;
265 CanPrefixQualifiers = canPrefixQualifiers(T, NeedARCStrongQualifier);
267 if (CanPrefixQualifiers && !Quals.empty()) {
268 if (NeedARCStrongQualifier) {
269 IncludeStrongLifetimeRAII Strong(Policy);
270 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/true);
272 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/true);
276 bool hasAfterQuals = false;
277 if (!CanPrefixQualifiers && !Quals.empty()) {
278 hasAfterQuals = !Quals.isEmptyWhenPrinted(Policy);
280 HasEmptyPlaceHolder = false;
283 switch (T->getTypeClass()) {
284 #define ABSTRACT_TYPE(CLASS, PARENT)
285 #define TYPE(CLASS, PARENT) case Type::CLASS: \
286 print##CLASS##Before(cast<CLASS##Type>(T), OS); \
288 #include "clang/AST/TypeNodes.def"
292 if (NeedARCStrongQualifier) {
293 IncludeStrongLifetimeRAII Strong(Policy);
294 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/!PrevPHIsEmpty.get());
296 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/!PrevPHIsEmpty.get());
301 void TypePrinter::printAfter(QualType t, raw_ostream &OS) {
302 SplitQualType split = t.split();
303 printAfter(split.Ty, split.Quals, OS);
306 /// \brief Prints the part of the type string after an identifier, e.g. for
307 /// "int foo[10]" it prints "[10]".
308 void TypePrinter::printAfter(const Type *T, Qualifiers Quals, raw_ostream &OS) {
309 switch (T->getTypeClass()) {
310 #define ABSTRACT_TYPE(CLASS, PARENT)
311 #define TYPE(CLASS, PARENT) case Type::CLASS: \
312 print##CLASS##After(cast<CLASS##Type>(T), OS); \
314 #include "clang/AST/TypeNodes.def"
318 void TypePrinter::printBuiltinBefore(const BuiltinType *T, raw_ostream &OS) {
319 OS << T->getName(Policy);
320 spaceBeforePlaceHolder(OS);
322 void TypePrinter::printBuiltinAfter(const BuiltinType *T, raw_ostream &OS) { }
324 void TypePrinter::printComplexBefore(const ComplexType *T, raw_ostream &OS) {
326 printBefore(T->getElementType(), OS);
328 void TypePrinter::printComplexAfter(const ComplexType *T, raw_ostream &OS) {
329 printAfter(T->getElementType(), OS);
332 void TypePrinter::printPointerBefore(const PointerType *T, raw_ostream &OS) {
333 IncludeStrongLifetimeRAII Strong(Policy);
334 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
335 printBefore(T->getPointeeType(), OS);
336 // Handle things like 'int (*A)[4];' correctly.
337 // FIXME: this should include vectors, but vectors use attributes I guess.
338 if (isa<ArrayType>(T->getPointeeType()))
342 void TypePrinter::printPointerAfter(const PointerType *T, raw_ostream &OS) {
343 IncludeStrongLifetimeRAII Strong(Policy);
344 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
345 // Handle things like 'int (*A)[4];' correctly.
346 // FIXME: this should include vectors, but vectors use attributes I guess.
347 if (isa<ArrayType>(T->getPointeeType()))
349 printAfter(T->getPointeeType(), OS);
352 void TypePrinter::printBlockPointerBefore(const BlockPointerType *T,
354 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
355 printBefore(T->getPointeeType(), OS);
358 void TypePrinter::printBlockPointerAfter(const BlockPointerType *T,
360 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
361 printAfter(T->getPointeeType(), OS);
364 void TypePrinter::printLValueReferenceBefore(const LValueReferenceType *T,
366 IncludeStrongLifetimeRAII Strong(Policy);
367 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
368 printBefore(T->getPointeeTypeAsWritten(), OS);
369 // Handle things like 'int (&A)[4];' correctly.
370 // FIXME: this should include vectors, but vectors use attributes I guess.
371 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
375 void TypePrinter::printLValueReferenceAfter(const LValueReferenceType *T,
377 IncludeStrongLifetimeRAII Strong(Policy);
378 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
379 // Handle things like 'int (&A)[4];' correctly.
380 // FIXME: this should include vectors, but vectors use attributes I guess.
381 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
383 printAfter(T->getPointeeTypeAsWritten(), OS);
386 void TypePrinter::printRValueReferenceBefore(const RValueReferenceType *T,
388 IncludeStrongLifetimeRAII Strong(Policy);
389 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
390 printBefore(T->getPointeeTypeAsWritten(), OS);
391 // Handle things like 'int (&&A)[4];' correctly.
392 // FIXME: this should include vectors, but vectors use attributes I guess.
393 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
397 void TypePrinter::printRValueReferenceAfter(const RValueReferenceType *T,
399 IncludeStrongLifetimeRAII Strong(Policy);
400 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
401 // Handle things like 'int (&&A)[4];' correctly.
402 // FIXME: this should include vectors, but vectors use attributes I guess.
403 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
405 printAfter(T->getPointeeTypeAsWritten(), OS);
408 void TypePrinter::printMemberPointerBefore(const MemberPointerType *T,
410 IncludeStrongLifetimeRAII Strong(Policy);
411 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
412 printBefore(T->getPointeeType(), OS);
413 // Handle things like 'int (Cls::*A)[4];' correctly.
414 // FIXME: this should include vectors, but vectors use attributes I guess.
415 if (isa<ArrayType>(T->getPointeeType()))
418 PrintingPolicy InnerPolicy(Policy);
419 InnerPolicy.IncludeTagDefinition = false;
420 TypePrinter(InnerPolicy).print(QualType(T->getClass(), 0), OS, StringRef());
424 void TypePrinter::printMemberPointerAfter(const MemberPointerType *T,
426 IncludeStrongLifetimeRAII Strong(Policy);
427 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
428 // Handle things like 'int (Cls::*A)[4];' correctly.
429 // FIXME: this should include vectors, but vectors use attributes I guess.
430 if (isa<ArrayType>(T->getPointeeType()))
432 printAfter(T->getPointeeType(), OS);
435 void TypePrinter::printConstantArrayBefore(const ConstantArrayType *T,
437 IncludeStrongLifetimeRAII Strong(Policy);
438 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
439 printBefore(T->getElementType(), OS);
441 void TypePrinter::printConstantArrayAfter(const ConstantArrayType *T,
444 if (T->getIndexTypeQualifiers().hasQualifiers()) {
445 AppendTypeQualList(OS, T->getIndexTypeCVRQualifiers(),
450 if (T->getSizeModifier() == ArrayType::Static)
453 OS << T->getSize().getZExtValue() << ']';
454 printAfter(T->getElementType(), OS);
457 void TypePrinter::printIncompleteArrayBefore(const IncompleteArrayType *T,
459 IncludeStrongLifetimeRAII Strong(Policy);
460 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
461 printBefore(T->getElementType(), OS);
463 void TypePrinter::printIncompleteArrayAfter(const IncompleteArrayType *T,
466 printAfter(T->getElementType(), OS);
469 void TypePrinter::printVariableArrayBefore(const VariableArrayType *T,
471 IncludeStrongLifetimeRAII Strong(Policy);
472 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
473 printBefore(T->getElementType(), OS);
475 void TypePrinter::printVariableArrayAfter(const VariableArrayType *T,
478 if (T->getIndexTypeQualifiers().hasQualifiers()) {
479 AppendTypeQualList(OS, T->getIndexTypeCVRQualifiers(), Policy.Restrict);
483 if (T->getSizeModifier() == VariableArrayType::Static)
485 else if (T->getSizeModifier() == VariableArrayType::Star)
488 if (T->getSizeExpr())
489 T->getSizeExpr()->printPretty(OS, nullptr, Policy);
492 printAfter(T->getElementType(), OS);
495 void TypePrinter::printAdjustedBefore(const AdjustedType *T, raw_ostream &OS) {
496 // Print the adjusted representation, otherwise the adjustment will be
498 printBefore(T->getAdjustedType(), OS);
500 void TypePrinter::printAdjustedAfter(const AdjustedType *T, raw_ostream &OS) {
501 printAfter(T->getAdjustedType(), OS);
504 void TypePrinter::printDecayedBefore(const DecayedType *T, raw_ostream &OS) {
505 // Print as though it's a pointer.
506 printAdjustedBefore(T, OS);
508 void TypePrinter::printDecayedAfter(const DecayedType *T, raw_ostream &OS) {
509 printAdjustedAfter(T, OS);
512 void TypePrinter::printDependentSizedArrayBefore(
513 const DependentSizedArrayType *T,
515 IncludeStrongLifetimeRAII Strong(Policy);
516 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
517 printBefore(T->getElementType(), OS);
519 void TypePrinter::printDependentSizedArrayAfter(
520 const DependentSizedArrayType *T,
523 if (T->getSizeExpr())
524 T->getSizeExpr()->printPretty(OS, nullptr, Policy);
526 printAfter(T->getElementType(), OS);
529 void TypePrinter::printDependentSizedExtVectorBefore(
530 const DependentSizedExtVectorType *T,
532 printBefore(T->getElementType(), OS);
534 void TypePrinter::printDependentSizedExtVectorAfter(
535 const DependentSizedExtVectorType *T,
537 OS << " __attribute__((ext_vector_type(";
538 if (T->getSizeExpr())
539 T->getSizeExpr()->printPretty(OS, nullptr, Policy);
541 printAfter(T->getElementType(), OS);
544 void TypePrinter::printVectorBefore(const VectorType *T, raw_ostream &OS) {
545 switch (T->getVectorKind()) {
546 case VectorType::AltiVecPixel:
547 OS << "__vector __pixel ";
549 case VectorType::AltiVecBool:
550 OS << "__vector __bool ";
551 printBefore(T->getElementType(), OS);
553 case VectorType::AltiVecVector:
555 printBefore(T->getElementType(), OS);
557 case VectorType::NeonVector:
558 OS << "__attribute__((neon_vector_type("
559 << T->getNumElements() << "))) ";
560 printBefore(T->getElementType(), OS);
562 case VectorType::NeonPolyVector:
563 OS << "__attribute__((neon_polyvector_type(" <<
564 T->getNumElements() << "))) ";
565 printBefore(T->getElementType(), OS);
567 case VectorType::GenericVector: {
568 // FIXME: We prefer to print the size directly here, but have no way
569 // to get the size of the type.
570 OS << "__attribute__((__vector_size__("
571 << T->getNumElements()
573 print(T->getElementType(), OS, StringRef());
575 printBefore(T->getElementType(), OS);
580 void TypePrinter::printVectorAfter(const VectorType *T, raw_ostream &OS) {
581 printAfter(T->getElementType(), OS);
584 void TypePrinter::printExtVectorBefore(const ExtVectorType *T,
586 printBefore(T->getElementType(), OS);
588 void TypePrinter::printExtVectorAfter(const ExtVectorType *T, raw_ostream &OS) {
589 printAfter(T->getElementType(), OS);
590 OS << " __attribute__((ext_vector_type(";
591 OS << T->getNumElements();
596 FunctionProtoType::printExceptionSpecification(raw_ostream &OS,
597 const PrintingPolicy &Policy)
600 if (hasDynamicExceptionSpec()) {
602 if (getExceptionSpecType() == EST_MSAny)
605 for (unsigned I = 0, N = getNumExceptions(); I != N; ++I) {
609 OS << getExceptionType(I).stream(Policy);
612 } else if (isNoexceptExceptionSpec(getExceptionSpecType())) {
614 if (getExceptionSpecType() == EST_ComputedNoexcept) {
616 if (getNoexceptExpr())
617 getNoexceptExpr()->printPretty(OS, nullptr, Policy);
623 void TypePrinter::printFunctionProtoBefore(const FunctionProtoType *T,
625 if (T->hasTrailingReturn()) {
627 if (!HasEmptyPlaceHolder)
630 // If needed for precedence reasons, wrap the inner part in grouping parens.
631 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false);
632 printBefore(T->getReturnType(), OS);
633 if (!PrevPHIsEmpty.get())
638 llvm::StringRef clang::getParameterABISpelling(ParameterABI ABI) {
640 case ParameterABI::Ordinary:
641 llvm_unreachable("asking for spelling of ordinary parameter ABI");
642 case ParameterABI::SwiftContext:
643 return "swift_context";
644 case ParameterABI::SwiftErrorResult:
645 return "swift_error_result";
646 case ParameterABI::SwiftIndirectResult:
647 return "swift_indirect_result";
649 llvm_unreachable("bad parameter ABI kind");
652 void TypePrinter::printFunctionProtoAfter(const FunctionProtoType *T,
654 // If needed for precedence reasons, wrap the inner part in grouping parens.
655 if (!HasEmptyPlaceHolder)
657 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
661 ParamPolicyRAII ParamPolicy(Policy);
662 for (unsigned i = 0, e = T->getNumParams(); i != e; ++i) {
665 auto EPI = T->getExtParameterInfo(i);
666 if (EPI.isConsumed()) OS << "__attribute__((ns_consumed)) ";
667 auto ABI = EPI.getABI();
668 if (ABI != ParameterABI::Ordinary)
669 OS << "__attribute__((" << getParameterABISpelling(ABI) << ")) ";
671 print(T->getParamType(i), OS, StringRef());
675 if (T->isVariadic()) {
676 if (T->getNumParams())
679 } else if (T->getNumParams() == 0 && Policy.UseVoidForZeroParams) {
680 // Do not emit int() if we have a proto, emit 'int(void)'.
686 FunctionType::ExtInfo Info = T->getExtInfo();
688 if (!InsideCCAttribute) {
689 switch (Info.getCC()) {
691 // The C calling convention is the default on the vast majority of platforms
692 // we support. If the user wrote it explicitly, it will usually be printed
693 // while traversing the AttributedType. If the type has been desugared, let
694 // the canonical spelling be the implicit calling convention.
695 // FIXME: It would be better to be explicit in certain contexts, such as a
696 // cdecl function typedef used to declare a member function with the
697 // Microsoft C++ ABI.
700 OS << " __attribute__((stdcall))";
703 OS << " __attribute__((fastcall))";
706 OS << " __attribute__((thiscall))";
708 case CC_X86VectorCall:
709 OS << " __attribute__((vectorcall))";
712 OS << " __attribute__((pascal))";
715 OS << " __attribute__((pcs(\"aapcs\")))";
718 OS << " __attribute__((pcs(\"aapcs-vfp\")))";
720 case CC_IntelOclBicc:
721 OS << " __attribute__((intel_ocl_bicc))";
724 OS << " __attribute__((ms_abi))";
727 OS << " __attribute__((sysv_abi))";
730 OS << " __attribute__((regcall))";
732 case CC_SpirFunction:
733 case CC_OpenCLKernel:
734 // Do nothing. These CCs are not available as attributes.
737 OS << " __attribute__((swiftcall))";
739 case CC_PreserveMost:
740 OS << " __attribute__((preserve_most))";
743 OS << " __attribute__((preserve_all))";
748 if (Info.getNoReturn())
749 OS << " __attribute__((noreturn))";
750 if (Info.getRegParm())
751 OS << " __attribute__((regparm ("
752 << Info.getRegParm() << ")))";
754 if (unsigned quals = T->getTypeQuals()) {
756 AppendTypeQualList(OS, quals, Policy.Restrict);
759 switch (T->getRefQualifier()) {
771 T->printExceptionSpecification(OS, Policy);
773 if (T->hasTrailingReturn()) {
775 print(T->getReturnType(), OS, StringRef());
777 printAfter(T->getReturnType(), OS);
780 void TypePrinter::printFunctionNoProtoBefore(const FunctionNoProtoType *T,
782 // If needed for precedence reasons, wrap the inner part in grouping parens.
783 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false);
784 printBefore(T->getReturnType(), OS);
785 if (!PrevPHIsEmpty.get())
788 void TypePrinter::printFunctionNoProtoAfter(const FunctionNoProtoType *T,
790 // If needed for precedence reasons, wrap the inner part in grouping parens.
791 if (!HasEmptyPlaceHolder)
793 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
796 if (T->getNoReturnAttr())
797 OS << " __attribute__((noreturn))";
798 printAfter(T->getReturnType(), OS);
801 void TypePrinter::printTypeSpec(NamedDecl *D, raw_ostream &OS) {
803 // Compute the full nested-name-specifier for this type.
804 // In C, this will always be empty except when the type
805 // being printed is anonymous within other Record.
806 if (!Policy.SuppressScope)
807 AppendScope(D->getDeclContext(), OS);
809 IdentifierInfo *II = D->getIdentifier();
811 spaceBeforePlaceHolder(OS);
814 void TypePrinter::printUnresolvedUsingBefore(const UnresolvedUsingType *T,
816 printTypeSpec(T->getDecl(), OS);
818 void TypePrinter::printUnresolvedUsingAfter(const UnresolvedUsingType *T,
821 void TypePrinter::printTypedefBefore(const TypedefType *T, raw_ostream &OS) {
822 printTypeSpec(T->getDecl(), OS);
824 void TypePrinter::printTypedefAfter(const TypedefType *T, raw_ostream &OS) { }
826 void TypePrinter::printTypeOfExprBefore(const TypeOfExprType *T,
829 if (T->getUnderlyingExpr())
830 T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy);
831 spaceBeforePlaceHolder(OS);
833 void TypePrinter::printTypeOfExprAfter(const TypeOfExprType *T,
836 void TypePrinter::printTypeOfBefore(const TypeOfType *T, raw_ostream &OS) {
838 print(T->getUnderlyingType(), OS, StringRef());
840 spaceBeforePlaceHolder(OS);
842 void TypePrinter::printTypeOfAfter(const TypeOfType *T, raw_ostream &OS) { }
844 void TypePrinter::printDecltypeBefore(const DecltypeType *T, raw_ostream &OS) {
846 if (T->getUnderlyingExpr())
847 T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy);
849 spaceBeforePlaceHolder(OS);
851 void TypePrinter::printDecltypeAfter(const DecltypeType *T, raw_ostream &OS) { }
853 void TypePrinter::printUnaryTransformBefore(const UnaryTransformType *T,
855 IncludeStrongLifetimeRAII Strong(Policy);
857 switch (T->getUTTKind()) {
858 case UnaryTransformType::EnumUnderlyingType:
859 OS << "__underlying_type(";
860 print(T->getBaseType(), OS, StringRef());
862 spaceBeforePlaceHolder(OS);
866 printBefore(T->getBaseType(), OS);
868 void TypePrinter::printUnaryTransformAfter(const UnaryTransformType *T,
870 IncludeStrongLifetimeRAII Strong(Policy);
872 switch (T->getUTTKind()) {
873 case UnaryTransformType::EnumUnderlyingType:
877 printAfter(T->getBaseType(), OS);
880 void TypePrinter::printAutoBefore(const AutoType *T, raw_ostream &OS) {
881 // If the type has been deduced, do not print 'auto'.
882 if (!T->getDeducedType().isNull()) {
883 printBefore(T->getDeducedType(), OS);
885 switch (T->getKeyword()) {
886 case AutoTypeKeyword::Auto: OS << "auto"; break;
887 case AutoTypeKeyword::DecltypeAuto: OS << "decltype(auto)"; break;
888 case AutoTypeKeyword::GNUAutoType: OS << "__auto_type"; break;
890 spaceBeforePlaceHolder(OS);
893 void TypePrinter::printAutoAfter(const AutoType *T, raw_ostream &OS) {
894 // If the type has been deduced, do not print 'auto'.
895 if (!T->getDeducedType().isNull())
896 printAfter(T->getDeducedType(), OS);
899 void TypePrinter::printDeducedTemplateSpecializationBefore(
900 const DeducedTemplateSpecializationType *T, raw_ostream &OS) {
901 // If the type has been deduced, print the deduced type.
902 if (!T->getDeducedType().isNull()) {
903 printBefore(T->getDeducedType(), OS);
905 IncludeStrongLifetimeRAII Strong(Policy);
906 T->getTemplateName().print(OS, Policy);
907 spaceBeforePlaceHolder(OS);
910 void TypePrinter::printDeducedTemplateSpecializationAfter(
911 const DeducedTemplateSpecializationType *T, raw_ostream &OS) {
912 // If the type has been deduced, print the deduced type.
913 if (!T->getDeducedType().isNull())
914 printAfter(T->getDeducedType(), OS);
917 void TypePrinter::printAtomicBefore(const AtomicType *T, raw_ostream &OS) {
918 IncludeStrongLifetimeRAII Strong(Policy);
921 print(T->getValueType(), OS, StringRef());
923 spaceBeforePlaceHolder(OS);
925 void TypePrinter::printAtomicAfter(const AtomicType *T, raw_ostream &OS) { }
927 void TypePrinter::printPipeBefore(const PipeType *T, raw_ostream &OS) {
928 IncludeStrongLifetimeRAII Strong(Policy);
935 print(T->getElementType(), OS, StringRef());
936 spaceBeforePlaceHolder(OS);
939 void TypePrinter::printPipeAfter(const PipeType *T, raw_ostream &OS) {
941 /// Appends the given scope to the end of a string.
942 void TypePrinter::AppendScope(DeclContext *DC, raw_ostream &OS) {
943 if (DC->isTranslationUnit()) return;
944 if (DC->isFunctionOrMethod()) return;
945 AppendScope(DC->getParent(), OS);
947 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
948 if (Policy.SuppressUnwrittenScope &&
949 (NS->isAnonymousNamespace() || NS->isInline()))
951 if (NS->getIdentifier())
952 OS << NS->getName() << "::";
954 OS << "(anonymous namespace)::";
955 } else if (ClassTemplateSpecializationDecl *Spec
956 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
957 IncludeStrongLifetimeRAII Strong(Policy);
958 OS << Spec->getIdentifier()->getName();
959 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
960 TemplateSpecializationType::PrintTemplateArgumentList(
961 OS, TemplateArgs.asArray(), Policy);
963 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
964 if (TypedefNameDecl *Typedef = Tag->getTypedefNameForAnonDecl())
965 OS << Typedef->getIdentifier()->getName() << "::";
966 else if (Tag->getIdentifier())
967 OS << Tag->getIdentifier()->getName() << "::";
973 void TypePrinter::printTag(TagDecl *D, raw_ostream &OS) {
974 if (Policy.IncludeTagDefinition) {
975 PrintingPolicy SubPolicy = Policy;
976 SubPolicy.IncludeTagDefinition = false;
977 D->print(OS, SubPolicy, Indentation);
978 spaceBeforePlaceHolder(OS);
982 bool HasKindDecoration = false;
984 // We don't print tags unless this is an elaborated type.
985 // In C, we just assume every RecordType is an elaborated type.
986 if (!Policy.SuppressTagKeyword && !D->getTypedefNameForAnonDecl()) {
987 HasKindDecoration = true;
988 OS << D->getKindName();
992 // Compute the full nested-name-specifier for this type.
993 // In C, this will always be empty except when the type
994 // being printed is anonymous within other Record.
995 if (!Policy.SuppressScope)
996 AppendScope(D->getDeclContext(), OS);
998 if (const IdentifierInfo *II = D->getIdentifier())
1000 else if (TypedefNameDecl *Typedef = D->getTypedefNameForAnonDecl()) {
1001 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1002 OS << Typedef->getIdentifier()->getName();
1004 // Make an unambiguous representation for anonymous types, e.g.
1005 // (anonymous enum at /usr/include/string.h:120:9)
1006 OS << (Policy.MSVCFormatting ? '`' : '(');
1008 if (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda()) {
1010 HasKindDecoration = true;
1015 if (Policy.AnonymousTagLocations) {
1016 // Suppress the redundant tag keyword if we just printed one.
1017 // We don't have to worry about ElaboratedTypes here because you can't
1018 // refer to an anonymous type with one.
1019 if (!HasKindDecoration)
1020 OS << " " << D->getKindName();
1022 PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc(
1024 if (PLoc.isValid()) {
1025 OS << " at " << PLoc.getFilename()
1026 << ':' << PLoc.getLine()
1027 << ':' << PLoc.getColumn();
1031 OS << (Policy.MSVCFormatting ? '\'' : ')');
1034 // If this is a class template specialization, print the template
1036 if (ClassTemplateSpecializationDecl *Spec
1037 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
1038 ArrayRef<TemplateArgument> Args;
1039 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
1040 const TemplateSpecializationType *TST =
1041 cast<TemplateSpecializationType>(TAW->getType());
1042 Args = TST->template_arguments();
1044 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1045 Args = TemplateArgs.asArray();
1047 IncludeStrongLifetimeRAII Strong(Policy);
1048 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, Policy);
1051 spaceBeforePlaceHolder(OS);
1054 void TypePrinter::printRecordBefore(const RecordType *T, raw_ostream &OS) {
1055 printTag(T->getDecl(), OS);
1057 void TypePrinter::printRecordAfter(const RecordType *T, raw_ostream &OS) { }
1059 void TypePrinter::printEnumBefore(const EnumType *T, raw_ostream &OS) {
1060 printTag(T->getDecl(), OS);
1062 void TypePrinter::printEnumAfter(const EnumType *T, raw_ostream &OS) { }
1064 void TypePrinter::printTemplateTypeParmBefore(const TemplateTypeParmType *T,
1066 if (IdentifierInfo *Id = T->getIdentifier())
1067 OS << Id->getName();
1069 OS << "type-parameter-" << T->getDepth() << '-' << T->getIndex();
1070 spaceBeforePlaceHolder(OS);
1072 void TypePrinter::printTemplateTypeParmAfter(const TemplateTypeParmType *T,
1073 raw_ostream &OS) { }
1075 void TypePrinter::printSubstTemplateTypeParmBefore(
1076 const SubstTemplateTypeParmType *T,
1078 IncludeStrongLifetimeRAII Strong(Policy);
1079 printBefore(T->getReplacementType(), OS);
1081 void TypePrinter::printSubstTemplateTypeParmAfter(
1082 const SubstTemplateTypeParmType *T,
1084 IncludeStrongLifetimeRAII Strong(Policy);
1085 printAfter(T->getReplacementType(), OS);
1088 void TypePrinter::printSubstTemplateTypeParmPackBefore(
1089 const SubstTemplateTypeParmPackType *T,
1091 IncludeStrongLifetimeRAII Strong(Policy);
1092 printTemplateTypeParmBefore(T->getReplacedParameter(), OS);
1094 void TypePrinter::printSubstTemplateTypeParmPackAfter(
1095 const SubstTemplateTypeParmPackType *T,
1097 IncludeStrongLifetimeRAII Strong(Policy);
1098 printTemplateTypeParmAfter(T->getReplacedParameter(), OS);
1101 void TypePrinter::printTemplateSpecializationBefore(
1102 const TemplateSpecializationType *T,
1104 IncludeStrongLifetimeRAII Strong(Policy);
1105 T->getTemplateName().print(OS, Policy);
1107 TemplateSpecializationType::PrintTemplateArgumentList(
1108 OS, T->template_arguments(), Policy);
1109 spaceBeforePlaceHolder(OS);
1111 void TypePrinter::printTemplateSpecializationAfter(
1112 const TemplateSpecializationType *T,
1113 raw_ostream &OS) { }
1115 void TypePrinter::printInjectedClassNameBefore(const InjectedClassNameType *T,
1117 printTemplateSpecializationBefore(T->getInjectedTST(), OS);
1119 void TypePrinter::printInjectedClassNameAfter(const InjectedClassNameType *T,
1120 raw_ostream &OS) { }
1122 void TypePrinter::printElaboratedBefore(const ElaboratedType *T,
1124 // The tag definition will take care of these.
1125 if (!Policy.IncludeTagDefinition)
1127 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1128 if (T->getKeyword() != ETK_None)
1130 NestedNameSpecifier* Qualifier = T->getQualifier();
1132 Qualifier->print(OS, Policy);
1135 ElaboratedTypePolicyRAII PolicyRAII(Policy);
1136 printBefore(T->getNamedType(), OS);
1138 void TypePrinter::printElaboratedAfter(const ElaboratedType *T,
1140 ElaboratedTypePolicyRAII PolicyRAII(Policy);
1141 printAfter(T->getNamedType(), OS);
1144 void TypePrinter::printParenBefore(const ParenType *T, raw_ostream &OS) {
1145 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) {
1146 printBefore(T->getInnerType(), OS);
1149 printBefore(T->getInnerType(), OS);
1151 void TypePrinter::printParenAfter(const ParenType *T, raw_ostream &OS) {
1152 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) {
1154 printAfter(T->getInnerType(), OS);
1156 printAfter(T->getInnerType(), OS);
1159 void TypePrinter::printDependentNameBefore(const DependentNameType *T,
1161 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1162 if (T->getKeyword() != ETK_None)
1165 T->getQualifier()->print(OS, Policy);
1167 OS << T->getIdentifier()->getName();
1168 spaceBeforePlaceHolder(OS);
1170 void TypePrinter::printDependentNameAfter(const DependentNameType *T,
1171 raw_ostream &OS) { }
1173 void TypePrinter::printDependentTemplateSpecializationBefore(
1174 const DependentTemplateSpecializationType *T, raw_ostream &OS) {
1175 IncludeStrongLifetimeRAII Strong(Policy);
1177 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1178 if (T->getKeyword() != ETK_None)
1181 if (T->getQualifier())
1182 T->getQualifier()->print(OS, Policy);
1183 OS << T->getIdentifier()->getName();
1184 TemplateSpecializationType::PrintTemplateArgumentList(OS,
1185 T->template_arguments(),
1187 spaceBeforePlaceHolder(OS);
1189 void TypePrinter::printDependentTemplateSpecializationAfter(
1190 const DependentTemplateSpecializationType *T, raw_ostream &OS) { }
1192 void TypePrinter::printPackExpansionBefore(const PackExpansionType *T,
1194 printBefore(T->getPattern(), OS);
1196 void TypePrinter::printPackExpansionAfter(const PackExpansionType *T,
1198 printAfter(T->getPattern(), OS);
1202 void TypePrinter::printAttributedBefore(const AttributedType *T,
1204 // Prefer the macro forms of the GC and ownership qualifiers.
1205 if (T->getAttrKind() == AttributedType::attr_objc_gc ||
1206 T->getAttrKind() == AttributedType::attr_objc_ownership)
1207 return printBefore(T->getEquivalentType(), OS);
1209 if (T->getAttrKind() == AttributedType::attr_objc_kindof)
1212 printBefore(T->getModifiedType(), OS);
1214 if (T->isMSTypeSpec()) {
1215 switch (T->getAttrKind()) {
1217 case AttributedType::attr_ptr32: OS << " __ptr32"; break;
1218 case AttributedType::attr_ptr64: OS << " __ptr64"; break;
1219 case AttributedType::attr_sptr: OS << " __sptr"; break;
1220 case AttributedType::attr_uptr: OS << " __uptr"; break;
1222 spaceBeforePlaceHolder(OS);
1225 // Print nullability type specifiers.
1226 if (T->getAttrKind() == AttributedType::attr_nonnull ||
1227 T->getAttrKind() == AttributedType::attr_nullable ||
1228 T->getAttrKind() == AttributedType::attr_null_unspecified) {
1229 if (T->getAttrKind() == AttributedType::attr_nonnull)
1231 else if (T->getAttrKind() == AttributedType::attr_nullable)
1233 else if (T->getAttrKind() == AttributedType::attr_null_unspecified)
1234 OS << " _Null_unspecified";
1236 llvm_unreachable("unhandled nullability");
1237 spaceBeforePlaceHolder(OS);
1241 void TypePrinter::printAttributedAfter(const AttributedType *T,
1243 // Prefer the macro forms of the GC and ownership qualifiers.
1244 if (T->getAttrKind() == AttributedType::attr_objc_gc ||
1245 T->getAttrKind() == AttributedType::attr_objc_ownership)
1246 return printAfter(T->getEquivalentType(), OS);
1248 if (T->getAttrKind() == AttributedType::attr_objc_kindof)
1251 // TODO: not all attributes are GCC-style attributes.
1252 if (T->isMSTypeSpec())
1255 // Nothing to print after.
1256 if (T->getAttrKind() == AttributedType::attr_nonnull ||
1257 T->getAttrKind() == AttributedType::attr_nullable ||
1258 T->getAttrKind() == AttributedType::attr_null_unspecified)
1259 return printAfter(T->getModifiedType(), OS);
1261 // If this is a calling convention attribute, don't print the implicit CC from
1262 // the modified type.
1263 SaveAndRestore<bool> MaybeSuppressCC(InsideCCAttribute, T->isCallingConv());
1265 printAfter(T->getModifiedType(), OS);
1267 // Don't print the inert __unsafe_unretained attribute at all.
1268 if (T->getAttrKind() == AttributedType::attr_objc_inert_unsafe_unretained)
1271 // Print nullability type specifiers that occur after
1272 if (T->getAttrKind() == AttributedType::attr_nonnull ||
1273 T->getAttrKind() == AttributedType::attr_nullable ||
1274 T->getAttrKind() == AttributedType::attr_null_unspecified) {
1275 if (T->getAttrKind() == AttributedType::attr_nonnull)
1277 else if (T->getAttrKind() == AttributedType::attr_nullable)
1279 else if (T->getAttrKind() == AttributedType::attr_null_unspecified)
1280 OS << " _Null_unspecified";
1282 llvm_unreachable("unhandled nullability");
1287 OS << " __attribute__((";
1288 switch (T->getAttrKind()) {
1289 default: llvm_unreachable("This attribute should have been handled already");
1290 case AttributedType::attr_address_space:
1291 OS << "address_space(";
1292 OS << T->getEquivalentType().getAddressSpace();
1296 case AttributedType::attr_vector_size: {
1297 OS << "__vector_size__(";
1298 if (const VectorType *vector =T->getEquivalentType()->getAs<VectorType>()) {
1299 OS << vector->getNumElements();
1301 print(vector->getElementType(), OS, StringRef());
1308 case AttributedType::attr_neon_vector_type:
1309 case AttributedType::attr_neon_polyvector_type: {
1310 if (T->getAttrKind() == AttributedType::attr_neon_vector_type)
1311 OS << "neon_vector_type(";
1313 OS << "neon_polyvector_type(";
1314 const VectorType *vector = T->getEquivalentType()->getAs<VectorType>();
1315 OS << vector->getNumElements();
1320 case AttributedType::attr_regparm: {
1321 // FIXME: When Sema learns to form this AttributedType, avoid printing the
1322 // attribute again in printFunctionProtoAfter.
1324 QualType t = T->getEquivalentType();
1325 while (!t->isFunctionType())
1326 t = t->getPointeeType();
1327 OS << t->getAs<FunctionType>()->getRegParmType();
1332 case AttributedType::attr_objc_gc: {
1335 QualType tmp = T->getEquivalentType();
1336 while (tmp.getObjCGCAttr() == Qualifiers::GCNone) {
1337 QualType next = tmp->getPointeeType();
1338 if (next == tmp) break;
1342 if (tmp.isObjCGCWeak())
1350 case AttributedType::attr_objc_ownership:
1351 OS << "objc_ownership(";
1352 switch (T->getEquivalentType().getObjCLifetime()) {
1353 case Qualifiers::OCL_None: llvm_unreachable("no ownership!");
1354 case Qualifiers::OCL_ExplicitNone: OS << "none"; break;
1355 case Qualifiers::OCL_Strong: OS << "strong"; break;
1356 case Qualifiers::OCL_Weak: OS << "weak"; break;
1357 case Qualifiers::OCL_Autoreleasing: OS << "autoreleasing"; break;
1362 // FIXME: When Sema learns to form this AttributedType, avoid printing the
1363 // attribute again in printFunctionProtoAfter.
1364 case AttributedType::attr_noreturn: OS << "noreturn"; break;
1366 case AttributedType::attr_cdecl: OS << "cdecl"; break;
1367 case AttributedType::attr_fastcall: OS << "fastcall"; break;
1368 case AttributedType::attr_stdcall: OS << "stdcall"; break;
1369 case AttributedType::attr_thiscall: OS << "thiscall"; break;
1370 case AttributedType::attr_swiftcall: OS << "swiftcall"; break;
1371 case AttributedType::attr_vectorcall: OS << "vectorcall"; break;
1372 case AttributedType::attr_pascal: OS << "pascal"; break;
1373 case AttributedType::attr_ms_abi: OS << "ms_abi"; break;
1374 case AttributedType::attr_sysv_abi: OS << "sysv_abi"; break;
1375 case AttributedType::attr_regcall: OS << "regcall"; break;
1376 case AttributedType::attr_pcs:
1377 case AttributedType::attr_pcs_vfp: {
1379 QualType t = T->getEquivalentType();
1380 while (!t->isFunctionType())
1381 t = t->getPointeeType();
1382 OS << (t->getAs<FunctionType>()->getCallConv() == CC_AAPCS ?
1383 "\"aapcs\"" : "\"aapcs-vfp\"");
1387 case AttributedType::attr_inteloclbicc: OS << "inteloclbicc"; break;
1388 case AttributedType::attr_preserve_most:
1389 OS << "preserve_most";
1391 case AttributedType::attr_preserve_all:
1392 OS << "preserve_all";
1398 void TypePrinter::printObjCInterfaceBefore(const ObjCInterfaceType *T,
1400 OS << T->getDecl()->getName();
1401 spaceBeforePlaceHolder(OS);
1403 void TypePrinter::printObjCInterfaceAfter(const ObjCInterfaceType *T,
1404 raw_ostream &OS) { }
1406 void TypePrinter::printObjCTypeParamBefore(const ObjCTypeParamType *T,
1408 OS << T->getDecl()->getName();
1409 if (!T->qual_empty()) {
1410 bool isFirst = true;
1412 for (const auto *I : T->quals()) {
1422 spaceBeforePlaceHolder(OS);
1425 void TypePrinter::printObjCTypeParamAfter(const ObjCTypeParamType *T,
1426 raw_ostream &OS) { }
1428 void TypePrinter::printObjCObjectBefore(const ObjCObjectType *T,
1430 if (T->qual_empty() && T->isUnspecializedAsWritten() &&
1431 !T->isKindOfTypeAsWritten())
1432 return printBefore(T->getBaseType(), OS);
1434 if (T->isKindOfTypeAsWritten())
1437 print(T->getBaseType(), OS, StringRef());
1439 if (T->isSpecializedAsWritten()) {
1440 bool isFirst = true;
1442 for (auto typeArg : T->getTypeArgsAsWritten()) {
1448 print(typeArg, OS, StringRef());
1453 if (!T->qual_empty()) {
1454 bool isFirst = true;
1456 for (const auto *I : T->quals()) {
1466 spaceBeforePlaceHolder(OS);
1468 void TypePrinter::printObjCObjectAfter(const ObjCObjectType *T,
1470 if (T->qual_empty() && T->isUnspecializedAsWritten() &&
1471 !T->isKindOfTypeAsWritten())
1472 return printAfter(T->getBaseType(), OS);
1475 void TypePrinter::printObjCObjectPointerBefore(const ObjCObjectPointerType *T,
1477 printBefore(T->getPointeeType(), OS);
1479 // If we need to print the pointer, print it now.
1480 if (!T->isObjCIdType() && !T->isObjCQualifiedIdType() &&
1481 !T->isObjCClassType() && !T->isObjCQualifiedClassType()) {
1482 if (HasEmptyPlaceHolder)
1487 void TypePrinter::printObjCObjectPointerAfter(const ObjCObjectPointerType *T,
1488 raw_ostream &OS) { }
1490 void TemplateSpecializationType::
1491 PrintTemplateArgumentList(raw_ostream &OS,
1492 const TemplateArgumentListInfo &Args,
1493 const PrintingPolicy &Policy) {
1494 return PrintTemplateArgumentList(OS,
1499 void TemplateSpecializationType::PrintTemplateArgumentList(
1500 raw_ostream &OS, ArrayRef<TemplateArgument> Args,
1501 const PrintingPolicy &Policy, bool SkipBrackets) {
1502 const char *Comma = Policy.MSVCFormatting ? "," : ", ";
1506 bool needSpace = false;
1507 bool FirstArg = true;
1508 for (const TemplateArgument &Arg : Args) {
1509 // Print the argument into a string.
1510 SmallString<128> Buf;
1511 llvm::raw_svector_ostream ArgOS(Buf);
1512 if (Arg.getKind() == TemplateArgument::Pack) {
1513 if (Arg.pack_size() && !FirstArg)
1515 PrintTemplateArgumentList(ArgOS,
1516 Arg.getPackAsArray(),
1521 Arg.print(Policy, ArgOS);
1523 StringRef ArgString = ArgOS.str();
1525 // If this is the first argument and its string representation
1526 // begins with the global scope specifier ('::foo'), add a space
1527 // to avoid printing the diagraph '<:'.
1528 if (FirstArg && !ArgString.empty() && ArgString[0] == ':')
1533 needSpace = (!ArgString.empty() && ArgString.back() == '>');
1537 // If the last character of our string is '>', add another space to
1538 // keep the two '>''s separate tokens. We don't *have* to do this in
1539 // C++0x, but it's still good hygiene.
1547 // Sadly, repeat all that with TemplateArgLoc.
1548 void TemplateSpecializationType::
1549 PrintTemplateArgumentList(raw_ostream &OS,
1550 ArrayRef<TemplateArgumentLoc> Args,
1551 const PrintingPolicy &Policy) {
1553 const char *Comma = Policy.MSVCFormatting ? "," : ", ";
1555 bool needSpace = false;
1556 bool FirstArg = true;
1557 for (const TemplateArgumentLoc &Arg : Args) {
1561 // Print the argument into a string.
1562 SmallString<128> Buf;
1563 llvm::raw_svector_ostream ArgOS(Buf);
1564 if (Arg.getArgument().getKind() == TemplateArgument::Pack) {
1565 PrintTemplateArgumentList(ArgOS,
1566 Arg.getArgument().getPackAsArray(),
1569 Arg.getArgument().print(Policy, ArgOS);
1571 StringRef ArgString = ArgOS.str();
1573 // If this is the first argument and its string representation
1574 // begins with the global scope specifier ('::foo'), add a space
1575 // to avoid printing the diagraph '<:'.
1576 if (FirstArg && !ArgString.empty() && ArgString[0] == ':')
1581 needSpace = (!ArgString.empty() && ArgString.back() == '>');
1585 // If the last character of our string is '>', add another space to
1586 // keep the two '>''s separate tokens. We don't *have* to do this in
1587 // C++0x, but it's still good hygiene.
1594 std::string Qualifiers::getAsString() const {
1596 return getAsString(PrintingPolicy(LO));
1599 // Appends qualifiers to the given string, separated by spaces. Will
1600 // prefix a space if the string is non-empty. Will not append a final
1602 std::string Qualifiers::getAsString(const PrintingPolicy &Policy) const {
1603 SmallString<64> Buf;
1604 llvm::raw_svector_ostream StrOS(Buf);
1605 print(StrOS, Policy);
1609 bool Qualifiers::isEmptyWhenPrinted(const PrintingPolicy &Policy) const {
1610 if (getCVRQualifiers())
1613 if (getAddressSpace())
1616 if (getObjCGCAttr())
1619 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime())
1620 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime))
1626 // Appends qualifiers to the given string, separated by spaces. Will
1627 // prefix a space if the string is non-empty. Will not append a final
1629 void Qualifiers::print(raw_ostream &OS, const PrintingPolicy& Policy,
1630 bool appendSpaceIfNonEmpty) const {
1631 bool addSpace = false;
1633 unsigned quals = getCVRQualifiers();
1635 AppendTypeQualList(OS, quals, Policy.Restrict);
1638 if (hasUnaligned()) {
1641 OS << "__unaligned";
1644 if (unsigned addrspace = getAddressSpace()) {
1648 switch (addrspace) {
1649 case LangAS::opencl_global:
1652 case LangAS::opencl_local:
1655 case LangAS::opencl_constant:
1656 case LangAS::cuda_constant:
1659 case LangAS::opencl_generic:
1662 case LangAS::cuda_device:
1665 case LangAS::cuda_shared:
1669 assert(addrspace >= LangAS::Count);
1670 OS << "__attribute__((address_space(";
1671 OS << addrspace - LangAS::Count;
1675 if (Qualifiers::GC gc = getObjCGCAttr()) {
1679 if (gc == Qualifiers::Weak)
1684 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) {
1685 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime)){
1692 case Qualifiers::OCL_None: llvm_unreachable("none but true");
1693 case Qualifiers::OCL_ExplicitNone: OS << "__unsafe_unretained"; break;
1694 case Qualifiers::OCL_Strong:
1695 if (!Policy.SuppressStrongLifetime)
1699 case Qualifiers::OCL_Weak: OS << "__weak"; break;
1700 case Qualifiers::OCL_Autoreleasing: OS << "__autoreleasing"; break;
1704 if (appendSpaceIfNonEmpty && addSpace)
1708 std::string QualType::getAsString(const PrintingPolicy &Policy) const {
1710 getAsStringInternal(S, Policy);
1714 std::string QualType::getAsString(const Type *ty, Qualifiers qs) {
1716 LangOptions options;
1717 getAsStringInternal(ty, qs, buffer, PrintingPolicy(options));
1721 void QualType::print(const Type *ty, Qualifiers qs,
1722 raw_ostream &OS, const PrintingPolicy &policy,
1723 const Twine &PlaceHolder, unsigned Indentation) {
1724 SmallString<128> PHBuf;
1725 StringRef PH = PlaceHolder.toStringRef(PHBuf);
1727 TypePrinter(policy, Indentation).print(ty, qs, OS, PH);
1730 void QualType::getAsStringInternal(const Type *ty, Qualifiers qs,
1731 std::string &buffer,
1732 const PrintingPolicy &policy) {
1733 SmallString<256> Buf;
1734 llvm::raw_svector_ostream StrOS(Buf);
1735 TypePrinter(policy).print(ty, qs, StrOS, buffer);
1736 std::string str = StrOS.str();