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::ObjCTypeParam:
198 case Type::ObjCInterface:
201 CanPrefixQualifiers = true;
204 case Type::ObjCObjectPointer:
205 CanPrefixQualifiers = T->isObjCIdType() || T->isObjCClassType() ||
206 T->isObjCQualifiedIdType() || T->isObjCQualifiedClassType();
209 case Type::ConstantArray:
210 case Type::IncompleteArray:
211 case Type::VariableArray:
212 case Type::DependentSizedArray:
213 NeedARCStrongQualifier = true;
219 case Type::BlockPointer:
220 case Type::LValueReference:
221 case Type::RValueReference:
222 case Type::MemberPointer:
223 case Type::DependentSizedExtVector:
225 case Type::ExtVector:
226 case Type::FunctionProto:
227 case Type::FunctionNoProto:
229 case Type::Attributed:
230 case Type::PackExpansion:
231 case Type::SubstTemplateTypeParm:
232 CanPrefixQualifiers = false;
236 return CanPrefixQualifiers;
239 void TypePrinter::printBefore(QualType T, raw_ostream &OS) {
240 SplitQualType Split = T.split();
242 // If we have cv1 T, where T is substituted for cv2 U, only print cv1 - cv2
244 Qualifiers Quals = Split.Quals;
245 if (const SubstTemplateTypeParmType *Subst =
246 dyn_cast<SubstTemplateTypeParmType>(Split.Ty))
247 Quals -= QualType(Subst, 0).getQualifiers();
249 printBefore(Split.Ty, Quals, OS);
252 /// \brief Prints the part of the type string before an identifier, e.g. for
253 /// "int foo[10]" it prints "int ".
254 void TypePrinter::printBefore(const Type *T,Qualifiers Quals, raw_ostream &OS) {
255 if (Policy.SuppressSpecifiers && T->isSpecifierType())
258 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder);
260 // Print qualifiers as appropriate.
262 bool CanPrefixQualifiers = false;
263 bool NeedARCStrongQualifier = false;
264 CanPrefixQualifiers = canPrefixQualifiers(T, NeedARCStrongQualifier);
266 if (CanPrefixQualifiers && !Quals.empty()) {
267 if (NeedARCStrongQualifier) {
268 IncludeStrongLifetimeRAII Strong(Policy);
269 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/true);
271 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/true);
275 bool hasAfterQuals = false;
276 if (!CanPrefixQualifiers && !Quals.empty()) {
277 hasAfterQuals = !Quals.isEmptyWhenPrinted(Policy);
279 HasEmptyPlaceHolder = false;
282 switch (T->getTypeClass()) {
283 #define ABSTRACT_TYPE(CLASS, PARENT)
284 #define TYPE(CLASS, PARENT) case Type::CLASS: \
285 print##CLASS##Before(cast<CLASS##Type>(T), OS); \
287 #include "clang/AST/TypeNodes.def"
291 if (NeedARCStrongQualifier) {
292 IncludeStrongLifetimeRAII Strong(Policy);
293 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/!PrevPHIsEmpty.get());
295 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/!PrevPHIsEmpty.get());
300 void TypePrinter::printAfter(QualType t, raw_ostream &OS) {
301 SplitQualType split = t.split();
302 printAfter(split.Ty, split.Quals, OS);
305 /// \brief Prints the part of the type string after an identifier, e.g. for
306 /// "int foo[10]" it prints "[10]".
307 void TypePrinter::printAfter(const Type *T, Qualifiers Quals, raw_ostream &OS) {
308 switch (T->getTypeClass()) {
309 #define ABSTRACT_TYPE(CLASS, PARENT)
310 #define TYPE(CLASS, PARENT) case Type::CLASS: \
311 print##CLASS##After(cast<CLASS##Type>(T), OS); \
313 #include "clang/AST/TypeNodes.def"
317 void TypePrinter::printBuiltinBefore(const BuiltinType *T, raw_ostream &OS) {
318 OS << T->getName(Policy);
319 spaceBeforePlaceHolder(OS);
321 void TypePrinter::printBuiltinAfter(const BuiltinType *T, raw_ostream &OS) { }
323 void TypePrinter::printComplexBefore(const ComplexType *T, raw_ostream &OS) {
325 printBefore(T->getElementType(), OS);
327 void TypePrinter::printComplexAfter(const ComplexType *T, raw_ostream &OS) {
328 printAfter(T->getElementType(), OS);
331 void TypePrinter::printPointerBefore(const PointerType *T, raw_ostream &OS) {
332 IncludeStrongLifetimeRAII Strong(Policy);
333 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
334 printBefore(T->getPointeeType(), OS);
335 // Handle things like 'int (*A)[4];' correctly.
336 // FIXME: this should include vectors, but vectors use attributes I guess.
337 if (isa<ArrayType>(T->getPointeeType()))
341 void TypePrinter::printPointerAfter(const PointerType *T, raw_ostream &OS) {
342 IncludeStrongLifetimeRAII Strong(Policy);
343 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
344 // Handle things like 'int (*A)[4];' correctly.
345 // FIXME: this should include vectors, but vectors use attributes I guess.
346 if (isa<ArrayType>(T->getPointeeType()))
348 printAfter(T->getPointeeType(), OS);
351 void TypePrinter::printBlockPointerBefore(const BlockPointerType *T,
353 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
354 printBefore(T->getPointeeType(), OS);
357 void TypePrinter::printBlockPointerAfter(const BlockPointerType *T,
359 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
360 printAfter(T->getPointeeType(), OS);
363 void TypePrinter::printLValueReferenceBefore(const LValueReferenceType *T,
365 IncludeStrongLifetimeRAII Strong(Policy);
366 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
367 printBefore(T->getPointeeTypeAsWritten(), OS);
368 // Handle things like 'int (&A)[4];' correctly.
369 // FIXME: this should include vectors, but vectors use attributes I guess.
370 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
374 void TypePrinter::printLValueReferenceAfter(const LValueReferenceType *T,
376 IncludeStrongLifetimeRAII Strong(Policy);
377 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
378 // Handle things like 'int (&A)[4];' correctly.
379 // FIXME: this should include vectors, but vectors use attributes I guess.
380 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
382 printAfter(T->getPointeeTypeAsWritten(), OS);
385 void TypePrinter::printRValueReferenceBefore(const RValueReferenceType *T,
387 IncludeStrongLifetimeRAII Strong(Policy);
388 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
389 printBefore(T->getPointeeTypeAsWritten(), OS);
390 // Handle things like 'int (&&A)[4];' correctly.
391 // FIXME: this should include vectors, but vectors use attributes I guess.
392 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
396 void TypePrinter::printRValueReferenceAfter(const RValueReferenceType *T,
398 IncludeStrongLifetimeRAII Strong(Policy);
399 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
400 // Handle things like 'int (&&A)[4];' correctly.
401 // FIXME: this should include vectors, but vectors use attributes I guess.
402 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
404 printAfter(T->getPointeeTypeAsWritten(), OS);
407 void TypePrinter::printMemberPointerBefore(const MemberPointerType *T,
409 IncludeStrongLifetimeRAII Strong(Policy);
410 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
411 printBefore(T->getPointeeType(), OS);
412 // Handle things like 'int (Cls::*A)[4];' correctly.
413 // FIXME: this should include vectors, but vectors use attributes I guess.
414 if (isa<ArrayType>(T->getPointeeType()))
417 PrintingPolicy InnerPolicy(Policy);
418 InnerPolicy.IncludeTagDefinition = false;
419 TypePrinter(InnerPolicy).print(QualType(T->getClass(), 0), OS, StringRef());
423 void TypePrinter::printMemberPointerAfter(const MemberPointerType *T,
425 IncludeStrongLifetimeRAII Strong(Policy);
426 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
427 // Handle things like 'int (Cls::*A)[4];' correctly.
428 // FIXME: this should include vectors, but vectors use attributes I guess.
429 if (isa<ArrayType>(T->getPointeeType()))
431 printAfter(T->getPointeeType(), OS);
434 void TypePrinter::printConstantArrayBefore(const ConstantArrayType *T,
436 IncludeStrongLifetimeRAII Strong(Policy);
437 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
438 printBefore(T->getElementType(), OS);
440 void TypePrinter::printConstantArrayAfter(const ConstantArrayType *T,
443 if (T->getIndexTypeQualifiers().hasQualifiers()) {
444 AppendTypeQualList(OS, T->getIndexTypeCVRQualifiers(),
449 if (T->getSizeModifier() == ArrayType::Static)
452 OS << T->getSize().getZExtValue() << ']';
453 printAfter(T->getElementType(), OS);
456 void TypePrinter::printIncompleteArrayBefore(const IncompleteArrayType *T,
458 IncludeStrongLifetimeRAII Strong(Policy);
459 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
460 printBefore(T->getElementType(), OS);
462 void TypePrinter::printIncompleteArrayAfter(const IncompleteArrayType *T,
465 printAfter(T->getElementType(), OS);
468 void TypePrinter::printVariableArrayBefore(const VariableArrayType *T,
470 IncludeStrongLifetimeRAII Strong(Policy);
471 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
472 printBefore(T->getElementType(), OS);
474 void TypePrinter::printVariableArrayAfter(const VariableArrayType *T,
477 if (T->getIndexTypeQualifiers().hasQualifiers()) {
478 AppendTypeQualList(OS, T->getIndexTypeCVRQualifiers(), Policy.Restrict);
482 if (T->getSizeModifier() == VariableArrayType::Static)
484 else if (T->getSizeModifier() == VariableArrayType::Star)
487 if (T->getSizeExpr())
488 T->getSizeExpr()->printPretty(OS, nullptr, Policy);
491 printAfter(T->getElementType(), OS);
494 void TypePrinter::printAdjustedBefore(const AdjustedType *T, raw_ostream &OS) {
495 // Print the adjusted representation, otherwise the adjustment will be
497 printBefore(T->getAdjustedType(), OS);
499 void TypePrinter::printAdjustedAfter(const AdjustedType *T, raw_ostream &OS) {
500 printAfter(T->getAdjustedType(), OS);
503 void TypePrinter::printDecayedBefore(const DecayedType *T, raw_ostream &OS) {
504 // Print as though it's a pointer.
505 printAdjustedBefore(T, OS);
507 void TypePrinter::printDecayedAfter(const DecayedType *T, raw_ostream &OS) {
508 printAdjustedAfter(T, OS);
511 void TypePrinter::printDependentSizedArrayBefore(
512 const DependentSizedArrayType *T,
514 IncludeStrongLifetimeRAII Strong(Policy);
515 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
516 printBefore(T->getElementType(), OS);
518 void TypePrinter::printDependentSizedArrayAfter(
519 const DependentSizedArrayType *T,
522 if (T->getSizeExpr())
523 T->getSizeExpr()->printPretty(OS, nullptr, Policy);
525 printAfter(T->getElementType(), OS);
528 void TypePrinter::printDependentSizedExtVectorBefore(
529 const DependentSizedExtVectorType *T,
531 printBefore(T->getElementType(), OS);
533 void TypePrinter::printDependentSizedExtVectorAfter(
534 const DependentSizedExtVectorType *T,
536 OS << " __attribute__((ext_vector_type(";
537 if (T->getSizeExpr())
538 T->getSizeExpr()->printPretty(OS, nullptr, Policy);
540 printAfter(T->getElementType(), OS);
543 void TypePrinter::printVectorBefore(const VectorType *T, raw_ostream &OS) {
544 switch (T->getVectorKind()) {
545 case VectorType::AltiVecPixel:
546 OS << "__vector __pixel ";
548 case VectorType::AltiVecBool:
549 OS << "__vector __bool ";
550 printBefore(T->getElementType(), OS);
552 case VectorType::AltiVecVector:
554 printBefore(T->getElementType(), OS);
556 case VectorType::NeonVector:
557 OS << "__attribute__((neon_vector_type("
558 << T->getNumElements() << "))) ";
559 printBefore(T->getElementType(), OS);
561 case VectorType::NeonPolyVector:
562 OS << "__attribute__((neon_polyvector_type(" <<
563 T->getNumElements() << "))) ";
564 printBefore(T->getElementType(), OS);
566 case VectorType::GenericVector: {
567 // FIXME: We prefer to print the size directly here, but have no way
568 // to get the size of the type.
569 OS << "__attribute__((__vector_size__("
570 << T->getNumElements()
572 print(T->getElementType(), OS, StringRef());
574 printBefore(T->getElementType(), OS);
579 void TypePrinter::printVectorAfter(const VectorType *T, raw_ostream &OS) {
580 printAfter(T->getElementType(), OS);
583 void TypePrinter::printExtVectorBefore(const ExtVectorType *T,
585 printBefore(T->getElementType(), OS);
587 void TypePrinter::printExtVectorAfter(const ExtVectorType *T, raw_ostream &OS) {
588 printAfter(T->getElementType(), OS);
589 OS << " __attribute__((ext_vector_type(";
590 OS << T->getNumElements();
595 FunctionProtoType::printExceptionSpecification(raw_ostream &OS,
596 const PrintingPolicy &Policy)
599 if (hasDynamicExceptionSpec()) {
601 if (getExceptionSpecType() == EST_MSAny)
604 for (unsigned I = 0, N = getNumExceptions(); I != N; ++I) {
608 OS << getExceptionType(I).stream(Policy);
611 } else if (isNoexceptExceptionSpec(getExceptionSpecType())) {
613 if (getExceptionSpecType() == EST_ComputedNoexcept) {
615 if (getNoexceptExpr())
616 getNoexceptExpr()->printPretty(OS, nullptr, Policy);
622 void TypePrinter::printFunctionProtoBefore(const FunctionProtoType *T,
624 if (T->hasTrailingReturn()) {
626 if (!HasEmptyPlaceHolder)
629 // If needed for precedence reasons, wrap the inner part in grouping parens.
630 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false);
631 printBefore(T->getReturnType(), OS);
632 if (!PrevPHIsEmpty.get())
637 llvm::StringRef clang::getParameterABISpelling(ParameterABI ABI) {
639 case ParameterABI::Ordinary:
640 llvm_unreachable("asking for spelling of ordinary parameter ABI");
641 case ParameterABI::SwiftContext:
642 return "swift_context";
643 case ParameterABI::SwiftErrorResult:
644 return "swift_error_result";
645 case ParameterABI::SwiftIndirectResult:
646 return "swift_indirect_result";
648 llvm_unreachable("bad parameter ABI kind");
651 void TypePrinter::printFunctionProtoAfter(const FunctionProtoType *T,
653 // If needed for precedence reasons, wrap the inner part in grouping parens.
654 if (!HasEmptyPlaceHolder)
656 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
660 ParamPolicyRAII ParamPolicy(Policy);
661 for (unsigned i = 0, e = T->getNumParams(); i != e; ++i) {
664 auto EPI = T->getExtParameterInfo(i);
665 if (EPI.isConsumed()) OS << "__attribute__((ns_consumed)) ";
666 auto ABI = EPI.getABI();
667 if (ABI != ParameterABI::Ordinary)
668 OS << "__attribute__((" << getParameterABISpelling(ABI) << ")) ";
670 print(T->getParamType(i), OS, StringRef());
674 if (T->isVariadic()) {
675 if (T->getNumParams())
678 } else if (T->getNumParams() == 0 && Policy.UseVoidForZeroParams) {
679 // Do not emit int() if we have a proto, emit 'int(void)'.
685 FunctionType::ExtInfo Info = T->getExtInfo();
687 if (!InsideCCAttribute) {
688 switch (Info.getCC()) {
690 // The C calling convention is the default on the vast majority of platforms
691 // we support. If the user wrote it explicitly, it will usually be printed
692 // while traversing the AttributedType. If the type has been desugared, let
693 // the canonical spelling be the implicit calling convention.
694 // FIXME: It would be better to be explicit in certain contexts, such as a
695 // cdecl function typedef used to declare a member function with the
696 // Microsoft C++ ABI.
699 OS << " __attribute__((stdcall))";
702 OS << " __attribute__((fastcall))";
705 OS << " __attribute__((thiscall))";
707 case CC_X86VectorCall:
708 OS << " __attribute__((vectorcall))";
711 OS << " __attribute__((pascal))";
714 OS << " __attribute__((pcs(\"aapcs\")))";
717 OS << " __attribute__((pcs(\"aapcs-vfp\")))";
719 case CC_IntelOclBicc:
720 OS << " __attribute__((intel_ocl_bicc))";
723 OS << " __attribute__((ms_abi))";
726 OS << " __attribute__((sysv_abi))";
729 OS << " __attribute__((regcall))";
731 case CC_SpirFunction:
732 case CC_OpenCLKernel:
733 // Do nothing. These CCs are not available as attributes.
736 OS << " __attribute__((swiftcall))";
738 case CC_PreserveMost:
739 OS << " __attribute__((preserve_most))";
742 OS << " __attribute__((preserve_all))";
747 if (Info.getNoReturn())
748 OS << " __attribute__((noreturn))";
749 if (Info.getRegParm())
750 OS << " __attribute__((regparm ("
751 << Info.getRegParm() << ")))";
753 if (unsigned quals = T->getTypeQuals()) {
755 AppendTypeQualList(OS, quals, Policy.Restrict);
758 switch (T->getRefQualifier()) {
770 T->printExceptionSpecification(OS, Policy);
772 if (T->hasTrailingReturn()) {
774 print(T->getReturnType(), OS, StringRef());
776 printAfter(T->getReturnType(), OS);
779 void TypePrinter::printFunctionNoProtoBefore(const FunctionNoProtoType *T,
781 // If needed for precedence reasons, wrap the inner part in grouping parens.
782 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false);
783 printBefore(T->getReturnType(), OS);
784 if (!PrevPHIsEmpty.get())
787 void TypePrinter::printFunctionNoProtoAfter(const FunctionNoProtoType *T,
789 // If needed for precedence reasons, wrap the inner part in grouping parens.
790 if (!HasEmptyPlaceHolder)
792 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
795 if (T->getNoReturnAttr())
796 OS << " __attribute__((noreturn))";
797 printAfter(T->getReturnType(), OS);
800 void TypePrinter::printTypeSpec(const NamedDecl *D, raw_ostream &OS) {
801 IdentifierInfo *II = D->getIdentifier();
803 spaceBeforePlaceHolder(OS);
806 void TypePrinter::printUnresolvedUsingBefore(const UnresolvedUsingType *T,
808 printTypeSpec(T->getDecl(), OS);
810 void TypePrinter::printUnresolvedUsingAfter(const UnresolvedUsingType *T,
813 void TypePrinter::printTypedefBefore(const TypedefType *T, raw_ostream &OS) {
814 printTypeSpec(T->getDecl(), OS);
816 void TypePrinter::printTypedefAfter(const TypedefType *T, raw_ostream &OS) { }
818 void TypePrinter::printTypeOfExprBefore(const TypeOfExprType *T,
821 if (T->getUnderlyingExpr())
822 T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy);
823 spaceBeforePlaceHolder(OS);
825 void TypePrinter::printTypeOfExprAfter(const TypeOfExprType *T,
828 void TypePrinter::printTypeOfBefore(const TypeOfType *T, raw_ostream &OS) {
830 print(T->getUnderlyingType(), OS, StringRef());
832 spaceBeforePlaceHolder(OS);
834 void TypePrinter::printTypeOfAfter(const TypeOfType *T, raw_ostream &OS) { }
836 void TypePrinter::printDecltypeBefore(const DecltypeType *T, raw_ostream &OS) {
838 if (T->getUnderlyingExpr())
839 T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy);
841 spaceBeforePlaceHolder(OS);
843 void TypePrinter::printDecltypeAfter(const DecltypeType *T, raw_ostream &OS) { }
845 void TypePrinter::printUnaryTransformBefore(const UnaryTransformType *T,
847 IncludeStrongLifetimeRAII Strong(Policy);
849 switch (T->getUTTKind()) {
850 case UnaryTransformType::EnumUnderlyingType:
851 OS << "__underlying_type(";
852 print(T->getBaseType(), OS, StringRef());
854 spaceBeforePlaceHolder(OS);
858 printBefore(T->getBaseType(), OS);
860 void TypePrinter::printUnaryTransformAfter(const UnaryTransformType *T,
862 IncludeStrongLifetimeRAII Strong(Policy);
864 switch (T->getUTTKind()) {
865 case UnaryTransformType::EnumUnderlyingType:
869 printAfter(T->getBaseType(), OS);
872 void TypePrinter::printAutoBefore(const AutoType *T, raw_ostream &OS) {
873 // If the type has been deduced, do not print 'auto'.
874 if (!T->getDeducedType().isNull()) {
875 printBefore(T->getDeducedType(), OS);
877 switch (T->getKeyword()) {
878 case AutoTypeKeyword::Auto: OS << "auto"; break;
879 case AutoTypeKeyword::DecltypeAuto: OS << "decltype(auto)"; break;
880 case AutoTypeKeyword::GNUAutoType: OS << "__auto_type"; break;
882 spaceBeforePlaceHolder(OS);
885 void TypePrinter::printAutoAfter(const AutoType *T, raw_ostream &OS) {
886 // If the type has been deduced, do not print 'auto'.
887 if (!T->getDeducedType().isNull())
888 printAfter(T->getDeducedType(), OS);
891 void TypePrinter::printAtomicBefore(const AtomicType *T, raw_ostream &OS) {
892 IncludeStrongLifetimeRAII Strong(Policy);
895 print(T->getValueType(), OS, StringRef());
897 spaceBeforePlaceHolder(OS);
899 void TypePrinter::printAtomicAfter(const AtomicType *T, raw_ostream &OS) { }
901 void TypePrinter::printPipeBefore(const PipeType *T, raw_ostream &OS) {
902 IncludeStrongLifetimeRAII Strong(Policy);
909 print(T->getElementType(), OS, StringRef());
910 spaceBeforePlaceHolder(OS);
913 void TypePrinter::printPipeAfter(const PipeType *T, raw_ostream &OS) {
915 /// Appends the given scope to the end of a string.
916 void TypePrinter::AppendScope(DeclContext *DC, raw_ostream &OS) {
917 if (DC->isTranslationUnit()) return;
918 if (DC->isFunctionOrMethod()) return;
919 AppendScope(DC->getParent(), OS);
921 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
922 if (Policy.SuppressUnwrittenScope &&
923 (NS->isAnonymousNamespace() || NS->isInline()))
925 if (NS->getIdentifier())
926 OS << NS->getName() << "::";
928 OS << "(anonymous namespace)::";
929 } else if (ClassTemplateSpecializationDecl *Spec
930 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
931 IncludeStrongLifetimeRAII Strong(Policy);
932 OS << Spec->getIdentifier()->getName();
933 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
934 TemplateSpecializationType::PrintTemplateArgumentList(
935 OS, TemplateArgs.asArray(), Policy);
937 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
938 if (TypedefNameDecl *Typedef = Tag->getTypedefNameForAnonDecl())
939 OS << Typedef->getIdentifier()->getName() << "::";
940 else if (Tag->getIdentifier())
941 OS << Tag->getIdentifier()->getName() << "::";
947 void TypePrinter::printTag(TagDecl *D, raw_ostream &OS) {
948 if (Policy.IncludeTagDefinition) {
949 PrintingPolicy SubPolicy = Policy;
950 SubPolicy.IncludeTagDefinition = false;
951 D->print(OS, SubPolicy, Indentation);
952 spaceBeforePlaceHolder(OS);
956 bool HasKindDecoration = false;
958 // We don't print tags unless this is an elaborated type.
959 // In C, we just assume every RecordType is an elaborated type.
960 if (!Policy.SuppressTagKeyword && !D->getTypedefNameForAnonDecl()) {
961 HasKindDecoration = true;
962 OS << D->getKindName();
966 // Compute the full nested-name-specifier for this type.
967 // In C, this will always be empty except when the type
968 // being printed is anonymous within other Record.
969 if (!Policy.SuppressScope)
970 AppendScope(D->getDeclContext(), OS);
972 if (const IdentifierInfo *II = D->getIdentifier())
974 else if (TypedefNameDecl *Typedef = D->getTypedefNameForAnonDecl()) {
975 assert(Typedef->getIdentifier() && "Typedef without identifier?");
976 OS << Typedef->getIdentifier()->getName();
978 // Make an unambiguous representation for anonymous types, e.g.
979 // (anonymous enum at /usr/include/string.h:120:9)
980 OS << (Policy.MSVCFormatting ? '`' : '(');
982 if (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda()) {
984 HasKindDecoration = true;
989 if (Policy.AnonymousTagLocations) {
990 // Suppress the redundant tag keyword if we just printed one.
991 // We don't have to worry about ElaboratedTypes here because you can't
992 // refer to an anonymous type with one.
993 if (!HasKindDecoration)
994 OS << " " << D->getKindName();
996 PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc(
998 if (PLoc.isValid()) {
999 OS << " at " << PLoc.getFilename()
1000 << ':' << PLoc.getLine()
1001 << ':' << PLoc.getColumn();
1005 OS << (Policy.MSVCFormatting ? '\'' : ')');
1008 // If this is a class template specialization, print the template
1010 if (ClassTemplateSpecializationDecl *Spec
1011 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
1012 ArrayRef<TemplateArgument> Args;
1013 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
1014 const TemplateSpecializationType *TST =
1015 cast<TemplateSpecializationType>(TAW->getType());
1016 Args = TST->template_arguments();
1018 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1019 Args = TemplateArgs.asArray();
1021 IncludeStrongLifetimeRAII Strong(Policy);
1022 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, Policy);
1025 spaceBeforePlaceHolder(OS);
1028 void TypePrinter::printRecordBefore(const RecordType *T, raw_ostream &OS) {
1029 printTag(T->getDecl(), OS);
1031 void TypePrinter::printRecordAfter(const RecordType *T, raw_ostream &OS) { }
1033 void TypePrinter::printEnumBefore(const EnumType *T, raw_ostream &OS) {
1034 printTag(T->getDecl(), OS);
1036 void TypePrinter::printEnumAfter(const EnumType *T, raw_ostream &OS) { }
1038 void TypePrinter::printTemplateTypeParmBefore(const TemplateTypeParmType *T,
1040 if (IdentifierInfo *Id = T->getIdentifier())
1041 OS << Id->getName();
1043 OS << "type-parameter-" << T->getDepth() << '-' << T->getIndex();
1044 spaceBeforePlaceHolder(OS);
1046 void TypePrinter::printTemplateTypeParmAfter(const TemplateTypeParmType *T,
1047 raw_ostream &OS) { }
1049 void TypePrinter::printSubstTemplateTypeParmBefore(
1050 const SubstTemplateTypeParmType *T,
1052 IncludeStrongLifetimeRAII Strong(Policy);
1053 printBefore(T->getReplacementType(), OS);
1055 void TypePrinter::printSubstTemplateTypeParmAfter(
1056 const SubstTemplateTypeParmType *T,
1058 IncludeStrongLifetimeRAII Strong(Policy);
1059 printAfter(T->getReplacementType(), OS);
1062 void TypePrinter::printSubstTemplateTypeParmPackBefore(
1063 const SubstTemplateTypeParmPackType *T,
1065 IncludeStrongLifetimeRAII Strong(Policy);
1066 printTemplateTypeParmBefore(T->getReplacedParameter(), OS);
1068 void TypePrinter::printSubstTemplateTypeParmPackAfter(
1069 const SubstTemplateTypeParmPackType *T,
1071 IncludeStrongLifetimeRAII Strong(Policy);
1072 printTemplateTypeParmAfter(T->getReplacedParameter(), OS);
1075 void TypePrinter::printTemplateSpecializationBefore(
1076 const TemplateSpecializationType *T,
1078 IncludeStrongLifetimeRAII Strong(Policy);
1079 T->getTemplateName().print(OS, Policy);
1081 TemplateSpecializationType::PrintTemplateArgumentList(
1082 OS, T->template_arguments(), Policy);
1083 spaceBeforePlaceHolder(OS);
1085 void TypePrinter::printTemplateSpecializationAfter(
1086 const TemplateSpecializationType *T,
1087 raw_ostream &OS) { }
1089 void TypePrinter::printInjectedClassNameBefore(const InjectedClassNameType *T,
1091 printTemplateSpecializationBefore(T->getInjectedTST(), OS);
1093 void TypePrinter::printInjectedClassNameAfter(const InjectedClassNameType *T,
1094 raw_ostream &OS) { }
1096 void TypePrinter::printElaboratedBefore(const ElaboratedType *T,
1098 // The tag definition will take care of these.
1099 if (!Policy.IncludeTagDefinition)
1101 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1102 if (T->getKeyword() != ETK_None)
1104 NestedNameSpecifier* Qualifier = T->getQualifier();
1106 Qualifier->print(OS, Policy);
1109 ElaboratedTypePolicyRAII PolicyRAII(Policy);
1110 printBefore(T->getNamedType(), OS);
1112 void TypePrinter::printElaboratedAfter(const ElaboratedType *T,
1114 ElaboratedTypePolicyRAII PolicyRAII(Policy);
1115 printAfter(T->getNamedType(), OS);
1118 void TypePrinter::printParenBefore(const ParenType *T, raw_ostream &OS) {
1119 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) {
1120 printBefore(T->getInnerType(), OS);
1123 printBefore(T->getInnerType(), OS);
1125 void TypePrinter::printParenAfter(const ParenType *T, raw_ostream &OS) {
1126 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) {
1128 printAfter(T->getInnerType(), OS);
1130 printAfter(T->getInnerType(), OS);
1133 void TypePrinter::printDependentNameBefore(const DependentNameType *T,
1135 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1136 if (T->getKeyword() != ETK_None)
1139 T->getQualifier()->print(OS, Policy);
1141 OS << T->getIdentifier()->getName();
1142 spaceBeforePlaceHolder(OS);
1144 void TypePrinter::printDependentNameAfter(const DependentNameType *T,
1145 raw_ostream &OS) { }
1147 void TypePrinter::printDependentTemplateSpecializationBefore(
1148 const DependentTemplateSpecializationType *T, raw_ostream &OS) {
1149 IncludeStrongLifetimeRAII Strong(Policy);
1151 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1152 if (T->getKeyword() != ETK_None)
1155 if (T->getQualifier())
1156 T->getQualifier()->print(OS, Policy);
1157 OS << T->getIdentifier()->getName();
1158 TemplateSpecializationType::PrintTemplateArgumentList(OS,
1159 T->template_arguments(),
1161 spaceBeforePlaceHolder(OS);
1163 void TypePrinter::printDependentTemplateSpecializationAfter(
1164 const DependentTemplateSpecializationType *T, raw_ostream &OS) { }
1166 void TypePrinter::printPackExpansionBefore(const PackExpansionType *T,
1168 printBefore(T->getPattern(), OS);
1170 void TypePrinter::printPackExpansionAfter(const PackExpansionType *T,
1172 printAfter(T->getPattern(), OS);
1176 void TypePrinter::printAttributedBefore(const AttributedType *T,
1178 // Prefer the macro forms of the GC and ownership qualifiers.
1179 if (T->getAttrKind() == AttributedType::attr_objc_gc ||
1180 T->getAttrKind() == AttributedType::attr_objc_ownership)
1181 return printBefore(T->getEquivalentType(), OS);
1183 if (T->getAttrKind() == AttributedType::attr_objc_kindof)
1186 printBefore(T->getModifiedType(), OS);
1188 if (T->isMSTypeSpec()) {
1189 switch (T->getAttrKind()) {
1191 case AttributedType::attr_ptr32: OS << " __ptr32"; break;
1192 case AttributedType::attr_ptr64: OS << " __ptr64"; break;
1193 case AttributedType::attr_sptr: OS << " __sptr"; break;
1194 case AttributedType::attr_uptr: OS << " __uptr"; break;
1196 spaceBeforePlaceHolder(OS);
1199 // Print nullability type specifiers.
1200 if (T->getAttrKind() == AttributedType::attr_nonnull ||
1201 T->getAttrKind() == AttributedType::attr_nullable ||
1202 T->getAttrKind() == AttributedType::attr_null_unspecified) {
1203 if (T->getAttrKind() == AttributedType::attr_nonnull)
1205 else if (T->getAttrKind() == AttributedType::attr_nullable)
1207 else if (T->getAttrKind() == AttributedType::attr_null_unspecified)
1208 OS << " _Null_unspecified";
1210 llvm_unreachable("unhandled nullability");
1211 spaceBeforePlaceHolder(OS);
1215 void TypePrinter::printAttributedAfter(const AttributedType *T,
1217 // Prefer the macro forms of the GC and ownership qualifiers.
1218 if (T->getAttrKind() == AttributedType::attr_objc_gc ||
1219 T->getAttrKind() == AttributedType::attr_objc_ownership)
1220 return printAfter(T->getEquivalentType(), OS);
1222 if (T->getAttrKind() == AttributedType::attr_objc_kindof)
1225 // TODO: not all attributes are GCC-style attributes.
1226 if (T->isMSTypeSpec())
1229 // Nothing to print after.
1230 if (T->getAttrKind() == AttributedType::attr_nonnull ||
1231 T->getAttrKind() == AttributedType::attr_nullable ||
1232 T->getAttrKind() == AttributedType::attr_null_unspecified)
1233 return printAfter(T->getModifiedType(), OS);
1235 // If this is a calling convention attribute, don't print the implicit CC from
1236 // the modified type.
1237 SaveAndRestore<bool> MaybeSuppressCC(InsideCCAttribute, T->isCallingConv());
1239 printAfter(T->getModifiedType(), OS);
1241 // Don't print the inert __unsafe_unretained attribute at all.
1242 if (T->getAttrKind() == AttributedType::attr_objc_inert_unsafe_unretained)
1245 // Print nullability type specifiers that occur after
1246 if (T->getAttrKind() == AttributedType::attr_nonnull ||
1247 T->getAttrKind() == AttributedType::attr_nullable ||
1248 T->getAttrKind() == AttributedType::attr_null_unspecified) {
1249 if (T->getAttrKind() == AttributedType::attr_nonnull)
1251 else if (T->getAttrKind() == AttributedType::attr_nullable)
1253 else if (T->getAttrKind() == AttributedType::attr_null_unspecified)
1254 OS << " _Null_unspecified";
1256 llvm_unreachable("unhandled nullability");
1261 OS << " __attribute__((";
1262 switch (T->getAttrKind()) {
1263 default: llvm_unreachable("This attribute should have been handled already");
1264 case AttributedType::attr_address_space:
1265 OS << "address_space(";
1266 OS << T->getEquivalentType().getAddressSpace();
1270 case AttributedType::attr_vector_size: {
1271 OS << "__vector_size__(";
1272 if (const VectorType *vector =T->getEquivalentType()->getAs<VectorType>()) {
1273 OS << vector->getNumElements();
1275 print(vector->getElementType(), OS, StringRef());
1282 case AttributedType::attr_neon_vector_type:
1283 case AttributedType::attr_neon_polyvector_type: {
1284 if (T->getAttrKind() == AttributedType::attr_neon_vector_type)
1285 OS << "neon_vector_type(";
1287 OS << "neon_polyvector_type(";
1288 const VectorType *vector = T->getEquivalentType()->getAs<VectorType>();
1289 OS << vector->getNumElements();
1294 case AttributedType::attr_regparm: {
1295 // FIXME: When Sema learns to form this AttributedType, avoid printing the
1296 // attribute again in printFunctionProtoAfter.
1298 QualType t = T->getEquivalentType();
1299 while (!t->isFunctionType())
1300 t = t->getPointeeType();
1301 OS << t->getAs<FunctionType>()->getRegParmType();
1306 case AttributedType::attr_objc_gc: {
1309 QualType tmp = T->getEquivalentType();
1310 while (tmp.getObjCGCAttr() == Qualifiers::GCNone) {
1311 QualType next = tmp->getPointeeType();
1312 if (next == tmp) break;
1316 if (tmp.isObjCGCWeak())
1324 case AttributedType::attr_objc_ownership:
1325 OS << "objc_ownership(";
1326 switch (T->getEquivalentType().getObjCLifetime()) {
1327 case Qualifiers::OCL_None: llvm_unreachable("no ownership!");
1328 case Qualifiers::OCL_ExplicitNone: OS << "none"; break;
1329 case Qualifiers::OCL_Strong: OS << "strong"; break;
1330 case Qualifiers::OCL_Weak: OS << "weak"; break;
1331 case Qualifiers::OCL_Autoreleasing: OS << "autoreleasing"; break;
1336 // FIXME: When Sema learns to form this AttributedType, avoid printing the
1337 // attribute again in printFunctionProtoAfter.
1338 case AttributedType::attr_noreturn: OS << "noreturn"; break;
1340 case AttributedType::attr_cdecl: OS << "cdecl"; break;
1341 case AttributedType::attr_fastcall: OS << "fastcall"; break;
1342 case AttributedType::attr_stdcall: OS << "stdcall"; break;
1343 case AttributedType::attr_thiscall: OS << "thiscall"; break;
1344 case AttributedType::attr_swiftcall: OS << "swiftcall"; break;
1345 case AttributedType::attr_vectorcall: OS << "vectorcall"; break;
1346 case AttributedType::attr_pascal: OS << "pascal"; break;
1347 case AttributedType::attr_ms_abi: OS << "ms_abi"; break;
1348 case AttributedType::attr_sysv_abi: OS << "sysv_abi"; break;
1349 case AttributedType::attr_regcall: OS << "regcall"; break;
1350 case AttributedType::attr_pcs:
1351 case AttributedType::attr_pcs_vfp: {
1353 QualType t = T->getEquivalentType();
1354 while (!t->isFunctionType())
1355 t = t->getPointeeType();
1356 OS << (t->getAs<FunctionType>()->getCallConv() == CC_AAPCS ?
1357 "\"aapcs\"" : "\"aapcs-vfp\"");
1361 case AttributedType::attr_inteloclbicc: OS << "inteloclbicc"; break;
1362 case AttributedType::attr_preserve_most:
1363 OS << "preserve_most";
1365 case AttributedType::attr_preserve_all:
1366 OS << "preserve_all";
1372 void TypePrinter::printObjCInterfaceBefore(const ObjCInterfaceType *T,
1374 OS << T->getDecl()->getName();
1375 spaceBeforePlaceHolder(OS);
1377 void TypePrinter::printObjCInterfaceAfter(const ObjCInterfaceType *T,
1378 raw_ostream &OS) { }
1380 void TypePrinter::printObjCTypeParamBefore(const ObjCTypeParamType *T,
1382 OS << T->getDecl()->getName();
1383 if (!T->qual_empty()) {
1384 bool isFirst = true;
1386 for (const auto *I : T->quals()) {
1396 spaceBeforePlaceHolder(OS);
1399 void TypePrinter::printObjCTypeParamAfter(const ObjCTypeParamType *T,
1400 raw_ostream &OS) { }
1402 void TypePrinter::printObjCObjectBefore(const ObjCObjectType *T,
1404 if (T->qual_empty() && T->isUnspecializedAsWritten() &&
1405 !T->isKindOfTypeAsWritten())
1406 return printBefore(T->getBaseType(), OS);
1408 if (T->isKindOfTypeAsWritten())
1411 print(T->getBaseType(), OS, StringRef());
1413 if (T->isSpecializedAsWritten()) {
1414 bool isFirst = true;
1416 for (auto typeArg : T->getTypeArgsAsWritten()) {
1422 print(typeArg, OS, StringRef());
1427 if (!T->qual_empty()) {
1428 bool isFirst = true;
1430 for (const auto *I : T->quals()) {
1440 spaceBeforePlaceHolder(OS);
1442 void TypePrinter::printObjCObjectAfter(const ObjCObjectType *T,
1444 if (T->qual_empty() && T->isUnspecializedAsWritten() &&
1445 !T->isKindOfTypeAsWritten())
1446 return printAfter(T->getBaseType(), OS);
1449 void TypePrinter::printObjCObjectPointerBefore(const ObjCObjectPointerType *T,
1451 printBefore(T->getPointeeType(), OS);
1453 // If we need to print the pointer, print it now.
1454 if (!T->isObjCIdType() && !T->isObjCQualifiedIdType() &&
1455 !T->isObjCClassType() && !T->isObjCQualifiedClassType()) {
1456 if (HasEmptyPlaceHolder)
1461 void TypePrinter::printObjCObjectPointerAfter(const ObjCObjectPointerType *T,
1462 raw_ostream &OS) { }
1464 void TemplateSpecializationType::
1465 PrintTemplateArgumentList(raw_ostream &OS,
1466 const TemplateArgumentListInfo &Args,
1467 const PrintingPolicy &Policy) {
1468 return PrintTemplateArgumentList(OS,
1473 void TemplateSpecializationType::PrintTemplateArgumentList(
1474 raw_ostream &OS, ArrayRef<TemplateArgument> Args,
1475 const PrintingPolicy &Policy, bool SkipBrackets) {
1476 const char *Comma = Policy.MSVCFormatting ? "," : ", ";
1480 bool needSpace = false;
1481 bool FirstArg = true;
1482 for (const TemplateArgument &Arg : Args) {
1483 // Print the argument into a string.
1484 SmallString<128> Buf;
1485 llvm::raw_svector_ostream ArgOS(Buf);
1486 if (Arg.getKind() == TemplateArgument::Pack) {
1487 if (Arg.pack_size() && !FirstArg)
1489 PrintTemplateArgumentList(ArgOS,
1490 Arg.getPackAsArray(),
1495 Arg.print(Policy, ArgOS);
1497 StringRef ArgString = ArgOS.str();
1499 // If this is the first argument and its string representation
1500 // begins with the global scope specifier ('::foo'), add a space
1501 // to avoid printing the diagraph '<:'.
1502 if (FirstArg && !ArgString.empty() && ArgString[0] == ':')
1507 needSpace = (!ArgString.empty() && ArgString.back() == '>');
1511 // If the last character of our string is '>', add another space to
1512 // keep the two '>''s separate tokens. We don't *have* to do this in
1513 // C++0x, but it's still good hygiene.
1521 // Sadly, repeat all that with TemplateArgLoc.
1522 void TemplateSpecializationType::
1523 PrintTemplateArgumentList(raw_ostream &OS,
1524 ArrayRef<TemplateArgumentLoc> Args,
1525 const PrintingPolicy &Policy) {
1527 const char *Comma = Policy.MSVCFormatting ? "," : ", ";
1529 bool needSpace = false;
1530 bool FirstArg = true;
1531 for (const TemplateArgumentLoc &Arg : Args) {
1535 // Print the argument into a string.
1536 SmallString<128> Buf;
1537 llvm::raw_svector_ostream ArgOS(Buf);
1538 if (Arg.getArgument().getKind() == TemplateArgument::Pack) {
1539 PrintTemplateArgumentList(ArgOS,
1540 Arg.getArgument().getPackAsArray(),
1543 Arg.getArgument().print(Policy, ArgOS);
1545 StringRef ArgString = ArgOS.str();
1547 // If this is the first argument and its string representation
1548 // begins with the global scope specifier ('::foo'), add a space
1549 // to avoid printing the diagraph '<:'.
1550 if (FirstArg && !ArgString.empty() && ArgString[0] == ':')
1555 needSpace = (!ArgString.empty() && ArgString.back() == '>');
1559 // If the last character of our string is '>', add another space to
1560 // keep the two '>''s separate tokens. We don't *have* to do this in
1561 // C++0x, but it's still good hygiene.
1568 std::string Qualifiers::getAsString() const {
1570 return getAsString(PrintingPolicy(LO));
1573 // Appends qualifiers to the given string, separated by spaces. Will
1574 // prefix a space if the string is non-empty. Will not append a final
1576 std::string Qualifiers::getAsString(const PrintingPolicy &Policy) const {
1577 SmallString<64> Buf;
1578 llvm::raw_svector_ostream StrOS(Buf);
1579 print(StrOS, Policy);
1583 bool Qualifiers::isEmptyWhenPrinted(const PrintingPolicy &Policy) const {
1584 if (getCVRQualifiers())
1587 if (getAddressSpace())
1590 if (getObjCGCAttr())
1593 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime())
1594 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime))
1600 // Appends qualifiers to the given string, separated by spaces. Will
1601 // prefix a space if the string is non-empty. Will not append a final
1603 void Qualifiers::print(raw_ostream &OS, const PrintingPolicy& Policy,
1604 bool appendSpaceIfNonEmpty) const {
1605 bool addSpace = false;
1607 unsigned quals = getCVRQualifiers();
1609 AppendTypeQualList(OS, quals, Policy.Restrict);
1612 if (hasUnaligned()) {
1615 OS << "__unaligned";
1618 if (unsigned addrspace = getAddressSpace()) {
1622 switch (addrspace) {
1623 case LangAS::opencl_global:
1626 case LangAS::opencl_local:
1629 case LangAS::opencl_constant:
1632 case LangAS::opencl_generic:
1636 OS << "__attribute__((address_space(";
1641 if (Qualifiers::GC gc = getObjCGCAttr()) {
1645 if (gc == Qualifiers::Weak)
1650 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) {
1651 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime)){
1658 case Qualifiers::OCL_None: llvm_unreachable("none but true");
1659 case Qualifiers::OCL_ExplicitNone: OS << "__unsafe_unretained"; break;
1660 case Qualifiers::OCL_Strong:
1661 if (!Policy.SuppressStrongLifetime)
1665 case Qualifiers::OCL_Weak: OS << "__weak"; break;
1666 case Qualifiers::OCL_Autoreleasing: OS << "__autoreleasing"; break;
1670 if (appendSpaceIfNonEmpty && addSpace)
1674 std::string QualType::getAsString(const PrintingPolicy &Policy) const {
1676 getAsStringInternal(S, Policy);
1680 std::string QualType::getAsString(const Type *ty, Qualifiers qs) {
1682 LangOptions options;
1683 getAsStringInternal(ty, qs, buffer, PrintingPolicy(options));
1687 void QualType::print(const Type *ty, Qualifiers qs,
1688 raw_ostream &OS, const PrintingPolicy &policy,
1689 const Twine &PlaceHolder, unsigned Indentation) {
1690 SmallString<128> PHBuf;
1691 StringRef PH = PlaceHolder.toStringRef(PHBuf);
1693 TypePrinter(policy, Indentation).print(ty, qs, OS, PH);
1696 void QualType::getAsStringInternal(const Type *ty, Qualifiers qs,
1697 std::string &buffer,
1698 const PrintingPolicy &policy) {
1699 SmallString<256> Buf;
1700 llvm::raw_svector_ostream StrOS(Buf);
1701 TypePrinter(policy).print(ty, qs, StrOS, buffer);
1702 std::string str = StrOS.str();