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() << ")))";
753 if (Info.getNoCallerSavedRegs())
754 OS << "__attribute__((no_caller_saved_registers))";
756 if (unsigned quals = T->getTypeQuals()) {
758 AppendTypeQualList(OS, quals, Policy.Restrict);
761 switch (T->getRefQualifier()) {
773 T->printExceptionSpecification(OS, Policy);
775 if (T->hasTrailingReturn()) {
777 print(T->getReturnType(), OS, StringRef());
779 printAfter(T->getReturnType(), OS);
782 void TypePrinter::printFunctionNoProtoBefore(const FunctionNoProtoType *T,
784 // If needed for precedence reasons, wrap the inner part in grouping parens.
785 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false);
786 printBefore(T->getReturnType(), OS);
787 if (!PrevPHIsEmpty.get())
790 void TypePrinter::printFunctionNoProtoAfter(const FunctionNoProtoType *T,
792 // If needed for precedence reasons, wrap the inner part in grouping parens.
793 if (!HasEmptyPlaceHolder)
795 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
798 if (T->getNoReturnAttr())
799 OS << " __attribute__((noreturn))";
800 printAfter(T->getReturnType(), OS);
803 void TypePrinter::printTypeSpec(NamedDecl *D, raw_ostream &OS) {
805 // Compute the full nested-name-specifier for this type.
806 // In C, this will always be empty except when the type
807 // being printed is anonymous within other Record.
808 if (!Policy.SuppressScope)
809 AppendScope(D->getDeclContext(), OS);
811 IdentifierInfo *II = D->getIdentifier();
813 spaceBeforePlaceHolder(OS);
816 void TypePrinter::printUnresolvedUsingBefore(const UnresolvedUsingType *T,
818 printTypeSpec(T->getDecl(), OS);
820 void TypePrinter::printUnresolvedUsingAfter(const UnresolvedUsingType *T,
823 void TypePrinter::printTypedefBefore(const TypedefType *T, raw_ostream &OS) {
824 printTypeSpec(T->getDecl(), OS);
826 void TypePrinter::printTypedefAfter(const TypedefType *T, raw_ostream &OS) { }
828 void TypePrinter::printTypeOfExprBefore(const TypeOfExprType *T,
831 if (T->getUnderlyingExpr())
832 T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy);
833 spaceBeforePlaceHolder(OS);
835 void TypePrinter::printTypeOfExprAfter(const TypeOfExprType *T,
838 void TypePrinter::printTypeOfBefore(const TypeOfType *T, raw_ostream &OS) {
840 print(T->getUnderlyingType(), OS, StringRef());
842 spaceBeforePlaceHolder(OS);
844 void TypePrinter::printTypeOfAfter(const TypeOfType *T, raw_ostream &OS) { }
846 void TypePrinter::printDecltypeBefore(const DecltypeType *T, raw_ostream &OS) {
848 if (T->getUnderlyingExpr())
849 T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy);
851 spaceBeforePlaceHolder(OS);
853 void TypePrinter::printDecltypeAfter(const DecltypeType *T, raw_ostream &OS) { }
855 void TypePrinter::printUnaryTransformBefore(const UnaryTransformType *T,
857 IncludeStrongLifetimeRAII Strong(Policy);
859 switch (T->getUTTKind()) {
860 case UnaryTransformType::EnumUnderlyingType:
861 OS << "__underlying_type(";
862 print(T->getBaseType(), OS, StringRef());
864 spaceBeforePlaceHolder(OS);
868 printBefore(T->getBaseType(), OS);
870 void TypePrinter::printUnaryTransformAfter(const UnaryTransformType *T,
872 IncludeStrongLifetimeRAII Strong(Policy);
874 switch (T->getUTTKind()) {
875 case UnaryTransformType::EnumUnderlyingType:
879 printAfter(T->getBaseType(), OS);
882 void TypePrinter::printAutoBefore(const AutoType *T, raw_ostream &OS) {
883 // If the type has been deduced, do not print 'auto'.
884 if (!T->getDeducedType().isNull()) {
885 printBefore(T->getDeducedType(), OS);
887 switch (T->getKeyword()) {
888 case AutoTypeKeyword::Auto: OS << "auto"; break;
889 case AutoTypeKeyword::DecltypeAuto: OS << "decltype(auto)"; break;
890 case AutoTypeKeyword::GNUAutoType: OS << "__auto_type"; break;
892 spaceBeforePlaceHolder(OS);
895 void TypePrinter::printAutoAfter(const AutoType *T, raw_ostream &OS) {
896 // If the type has been deduced, do not print 'auto'.
897 if (!T->getDeducedType().isNull())
898 printAfter(T->getDeducedType(), OS);
901 void TypePrinter::printDeducedTemplateSpecializationBefore(
902 const DeducedTemplateSpecializationType *T, raw_ostream &OS) {
903 // If the type has been deduced, print the deduced type.
904 if (!T->getDeducedType().isNull()) {
905 printBefore(T->getDeducedType(), OS);
907 IncludeStrongLifetimeRAII Strong(Policy);
908 T->getTemplateName().print(OS, Policy);
909 spaceBeforePlaceHolder(OS);
912 void TypePrinter::printDeducedTemplateSpecializationAfter(
913 const DeducedTemplateSpecializationType *T, raw_ostream &OS) {
914 // If the type has been deduced, print the deduced type.
915 if (!T->getDeducedType().isNull())
916 printAfter(T->getDeducedType(), OS);
919 void TypePrinter::printAtomicBefore(const AtomicType *T, raw_ostream &OS) {
920 IncludeStrongLifetimeRAII Strong(Policy);
923 print(T->getValueType(), OS, StringRef());
925 spaceBeforePlaceHolder(OS);
927 void TypePrinter::printAtomicAfter(const AtomicType *T, raw_ostream &OS) { }
929 void TypePrinter::printPipeBefore(const PipeType *T, raw_ostream &OS) {
930 IncludeStrongLifetimeRAII Strong(Policy);
937 print(T->getElementType(), OS, StringRef());
938 spaceBeforePlaceHolder(OS);
941 void TypePrinter::printPipeAfter(const PipeType *T, raw_ostream &OS) {
943 /// Appends the given scope to the end of a string.
944 void TypePrinter::AppendScope(DeclContext *DC, raw_ostream &OS) {
945 if (DC->isTranslationUnit()) return;
946 if (DC->isFunctionOrMethod()) return;
947 AppendScope(DC->getParent(), OS);
949 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
950 if (Policy.SuppressUnwrittenScope &&
951 (NS->isAnonymousNamespace() || NS->isInline()))
953 if (NS->getIdentifier())
954 OS << NS->getName() << "::";
956 OS << "(anonymous namespace)::";
957 } else if (ClassTemplateSpecializationDecl *Spec
958 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
959 IncludeStrongLifetimeRAII Strong(Policy);
960 OS << Spec->getIdentifier()->getName();
961 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
962 TemplateSpecializationType::PrintTemplateArgumentList(
963 OS, TemplateArgs.asArray(), Policy);
965 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
966 if (TypedefNameDecl *Typedef = Tag->getTypedefNameForAnonDecl())
967 OS << Typedef->getIdentifier()->getName() << "::";
968 else if (Tag->getIdentifier())
969 OS << Tag->getIdentifier()->getName() << "::";
975 void TypePrinter::printTag(TagDecl *D, raw_ostream &OS) {
976 if (Policy.IncludeTagDefinition) {
977 PrintingPolicy SubPolicy = Policy;
978 SubPolicy.IncludeTagDefinition = false;
979 D->print(OS, SubPolicy, Indentation);
980 spaceBeforePlaceHolder(OS);
984 bool HasKindDecoration = false;
986 // We don't print tags unless this is an elaborated type.
987 // In C, we just assume every RecordType is an elaborated type.
988 if (!Policy.SuppressTagKeyword && !D->getTypedefNameForAnonDecl()) {
989 HasKindDecoration = true;
990 OS << D->getKindName();
994 // Compute the full nested-name-specifier for this type.
995 // In C, this will always be empty except when the type
996 // being printed is anonymous within other Record.
997 if (!Policy.SuppressScope)
998 AppendScope(D->getDeclContext(), OS);
1000 if (const IdentifierInfo *II = D->getIdentifier())
1001 OS << II->getName();
1002 else if (TypedefNameDecl *Typedef = D->getTypedefNameForAnonDecl()) {
1003 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1004 OS << Typedef->getIdentifier()->getName();
1006 // Make an unambiguous representation for anonymous types, e.g.
1007 // (anonymous enum at /usr/include/string.h:120:9)
1008 OS << (Policy.MSVCFormatting ? '`' : '(');
1010 if (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda()) {
1012 HasKindDecoration = true;
1017 if (Policy.AnonymousTagLocations) {
1018 // Suppress the redundant tag keyword if we just printed one.
1019 // We don't have to worry about ElaboratedTypes here because you can't
1020 // refer to an anonymous type with one.
1021 if (!HasKindDecoration)
1022 OS << " " << D->getKindName();
1024 PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc(
1026 if (PLoc.isValid()) {
1027 OS << " at " << PLoc.getFilename()
1028 << ':' << PLoc.getLine()
1029 << ':' << PLoc.getColumn();
1033 OS << (Policy.MSVCFormatting ? '\'' : ')');
1036 // If this is a class template specialization, print the template
1038 if (ClassTemplateSpecializationDecl *Spec
1039 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
1040 ArrayRef<TemplateArgument> Args;
1041 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
1042 const TemplateSpecializationType *TST =
1043 cast<TemplateSpecializationType>(TAW->getType());
1044 Args = TST->template_arguments();
1046 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1047 Args = TemplateArgs.asArray();
1049 IncludeStrongLifetimeRAII Strong(Policy);
1050 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, Policy);
1053 spaceBeforePlaceHolder(OS);
1056 void TypePrinter::printRecordBefore(const RecordType *T, raw_ostream &OS) {
1057 printTag(T->getDecl(), OS);
1059 void TypePrinter::printRecordAfter(const RecordType *T, raw_ostream &OS) { }
1061 void TypePrinter::printEnumBefore(const EnumType *T, raw_ostream &OS) {
1062 printTag(T->getDecl(), OS);
1064 void TypePrinter::printEnumAfter(const EnumType *T, raw_ostream &OS) { }
1066 void TypePrinter::printTemplateTypeParmBefore(const TemplateTypeParmType *T,
1068 if (IdentifierInfo *Id = T->getIdentifier())
1069 OS << Id->getName();
1071 OS << "type-parameter-" << T->getDepth() << '-' << T->getIndex();
1072 spaceBeforePlaceHolder(OS);
1074 void TypePrinter::printTemplateTypeParmAfter(const TemplateTypeParmType *T,
1075 raw_ostream &OS) { }
1077 void TypePrinter::printSubstTemplateTypeParmBefore(
1078 const SubstTemplateTypeParmType *T,
1080 IncludeStrongLifetimeRAII Strong(Policy);
1081 printBefore(T->getReplacementType(), OS);
1083 void TypePrinter::printSubstTemplateTypeParmAfter(
1084 const SubstTemplateTypeParmType *T,
1086 IncludeStrongLifetimeRAII Strong(Policy);
1087 printAfter(T->getReplacementType(), OS);
1090 void TypePrinter::printSubstTemplateTypeParmPackBefore(
1091 const SubstTemplateTypeParmPackType *T,
1093 IncludeStrongLifetimeRAII Strong(Policy);
1094 printTemplateTypeParmBefore(T->getReplacedParameter(), OS);
1096 void TypePrinter::printSubstTemplateTypeParmPackAfter(
1097 const SubstTemplateTypeParmPackType *T,
1099 IncludeStrongLifetimeRAII Strong(Policy);
1100 printTemplateTypeParmAfter(T->getReplacedParameter(), OS);
1103 void TypePrinter::printTemplateSpecializationBefore(
1104 const TemplateSpecializationType *T,
1106 IncludeStrongLifetimeRAII Strong(Policy);
1107 T->getTemplateName().print(OS, Policy);
1109 TemplateSpecializationType::PrintTemplateArgumentList(
1110 OS, T->template_arguments(), Policy);
1111 spaceBeforePlaceHolder(OS);
1113 void TypePrinter::printTemplateSpecializationAfter(
1114 const TemplateSpecializationType *T,
1115 raw_ostream &OS) { }
1117 void TypePrinter::printInjectedClassNameBefore(const InjectedClassNameType *T,
1119 printTemplateSpecializationBefore(T->getInjectedTST(), OS);
1121 void TypePrinter::printInjectedClassNameAfter(const InjectedClassNameType *T,
1122 raw_ostream &OS) { }
1124 void TypePrinter::printElaboratedBefore(const ElaboratedType *T,
1126 // The tag definition will take care of these.
1127 if (!Policy.IncludeTagDefinition)
1129 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1130 if (T->getKeyword() != ETK_None)
1132 NestedNameSpecifier* Qualifier = T->getQualifier();
1134 Qualifier->print(OS, Policy);
1137 ElaboratedTypePolicyRAII PolicyRAII(Policy);
1138 printBefore(T->getNamedType(), OS);
1140 void TypePrinter::printElaboratedAfter(const ElaboratedType *T,
1142 ElaboratedTypePolicyRAII PolicyRAII(Policy);
1143 printAfter(T->getNamedType(), OS);
1146 void TypePrinter::printParenBefore(const ParenType *T, raw_ostream &OS) {
1147 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) {
1148 printBefore(T->getInnerType(), OS);
1151 printBefore(T->getInnerType(), OS);
1153 void TypePrinter::printParenAfter(const ParenType *T, raw_ostream &OS) {
1154 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) {
1156 printAfter(T->getInnerType(), OS);
1158 printAfter(T->getInnerType(), OS);
1161 void TypePrinter::printDependentNameBefore(const DependentNameType *T,
1163 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1164 if (T->getKeyword() != ETK_None)
1167 T->getQualifier()->print(OS, Policy);
1169 OS << T->getIdentifier()->getName();
1170 spaceBeforePlaceHolder(OS);
1172 void TypePrinter::printDependentNameAfter(const DependentNameType *T,
1173 raw_ostream &OS) { }
1175 void TypePrinter::printDependentTemplateSpecializationBefore(
1176 const DependentTemplateSpecializationType *T, raw_ostream &OS) {
1177 IncludeStrongLifetimeRAII Strong(Policy);
1179 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1180 if (T->getKeyword() != ETK_None)
1183 if (T->getQualifier())
1184 T->getQualifier()->print(OS, Policy);
1185 OS << T->getIdentifier()->getName();
1186 TemplateSpecializationType::PrintTemplateArgumentList(OS,
1187 T->template_arguments(),
1189 spaceBeforePlaceHolder(OS);
1191 void TypePrinter::printDependentTemplateSpecializationAfter(
1192 const DependentTemplateSpecializationType *T, raw_ostream &OS) { }
1194 void TypePrinter::printPackExpansionBefore(const PackExpansionType *T,
1196 printBefore(T->getPattern(), OS);
1198 void TypePrinter::printPackExpansionAfter(const PackExpansionType *T,
1200 printAfter(T->getPattern(), OS);
1204 void TypePrinter::printAttributedBefore(const AttributedType *T,
1206 // Prefer the macro forms of the GC and ownership qualifiers.
1207 if (T->getAttrKind() == AttributedType::attr_objc_gc ||
1208 T->getAttrKind() == AttributedType::attr_objc_ownership)
1209 return printBefore(T->getEquivalentType(), OS);
1211 if (T->getAttrKind() == AttributedType::attr_objc_kindof)
1214 printBefore(T->getModifiedType(), OS);
1216 if (T->isMSTypeSpec()) {
1217 switch (T->getAttrKind()) {
1219 case AttributedType::attr_ptr32: OS << " __ptr32"; break;
1220 case AttributedType::attr_ptr64: OS << " __ptr64"; break;
1221 case AttributedType::attr_sptr: OS << " __sptr"; break;
1222 case AttributedType::attr_uptr: OS << " __uptr"; break;
1224 spaceBeforePlaceHolder(OS);
1227 // Print nullability type specifiers.
1228 if (T->getAttrKind() == AttributedType::attr_nonnull ||
1229 T->getAttrKind() == AttributedType::attr_nullable ||
1230 T->getAttrKind() == AttributedType::attr_null_unspecified) {
1231 if (T->getAttrKind() == AttributedType::attr_nonnull)
1233 else if (T->getAttrKind() == AttributedType::attr_nullable)
1235 else if (T->getAttrKind() == AttributedType::attr_null_unspecified)
1236 OS << " _Null_unspecified";
1238 llvm_unreachable("unhandled nullability");
1239 spaceBeforePlaceHolder(OS);
1243 void TypePrinter::printAttributedAfter(const AttributedType *T,
1245 // Prefer the macro forms of the GC and ownership qualifiers.
1246 if (T->getAttrKind() == AttributedType::attr_objc_gc ||
1247 T->getAttrKind() == AttributedType::attr_objc_ownership)
1248 return printAfter(T->getEquivalentType(), OS);
1250 if (T->getAttrKind() == AttributedType::attr_objc_kindof)
1253 // TODO: not all attributes are GCC-style attributes.
1254 if (T->isMSTypeSpec())
1257 // Nothing to print after.
1258 if (T->getAttrKind() == AttributedType::attr_nonnull ||
1259 T->getAttrKind() == AttributedType::attr_nullable ||
1260 T->getAttrKind() == AttributedType::attr_null_unspecified)
1261 return printAfter(T->getModifiedType(), OS);
1263 // If this is a calling convention attribute, don't print the implicit CC from
1264 // the modified type.
1265 SaveAndRestore<bool> MaybeSuppressCC(InsideCCAttribute, T->isCallingConv());
1267 printAfter(T->getModifiedType(), OS);
1269 // Don't print the inert __unsafe_unretained attribute at all.
1270 if (T->getAttrKind() == AttributedType::attr_objc_inert_unsafe_unretained)
1273 // Print nullability type specifiers that occur after
1274 if (T->getAttrKind() == AttributedType::attr_nonnull ||
1275 T->getAttrKind() == AttributedType::attr_nullable ||
1276 T->getAttrKind() == AttributedType::attr_null_unspecified) {
1277 if (T->getAttrKind() == AttributedType::attr_nonnull)
1279 else if (T->getAttrKind() == AttributedType::attr_nullable)
1281 else if (T->getAttrKind() == AttributedType::attr_null_unspecified)
1282 OS << " _Null_unspecified";
1284 llvm_unreachable("unhandled nullability");
1289 OS << " __attribute__((";
1290 switch (T->getAttrKind()) {
1291 default: llvm_unreachable("This attribute should have been handled already");
1292 case AttributedType::attr_address_space:
1293 OS << "address_space(";
1294 OS << T->getEquivalentType().getAddressSpace();
1298 case AttributedType::attr_vector_size: {
1299 OS << "__vector_size__(";
1300 if (const VectorType *vector =T->getEquivalentType()->getAs<VectorType>()) {
1301 OS << vector->getNumElements();
1303 print(vector->getElementType(), OS, StringRef());
1310 case AttributedType::attr_neon_vector_type:
1311 case AttributedType::attr_neon_polyvector_type: {
1312 if (T->getAttrKind() == AttributedType::attr_neon_vector_type)
1313 OS << "neon_vector_type(";
1315 OS << "neon_polyvector_type(";
1316 const VectorType *vector = T->getEquivalentType()->getAs<VectorType>();
1317 OS << vector->getNumElements();
1322 case AttributedType::attr_regparm: {
1323 // FIXME: When Sema learns to form this AttributedType, avoid printing the
1324 // attribute again in printFunctionProtoAfter.
1326 QualType t = T->getEquivalentType();
1327 while (!t->isFunctionType())
1328 t = t->getPointeeType();
1329 OS << t->getAs<FunctionType>()->getRegParmType();
1334 case AttributedType::attr_objc_gc: {
1337 QualType tmp = T->getEquivalentType();
1338 while (tmp.getObjCGCAttr() == Qualifiers::GCNone) {
1339 QualType next = tmp->getPointeeType();
1340 if (next == tmp) break;
1344 if (tmp.isObjCGCWeak())
1352 case AttributedType::attr_objc_ownership:
1353 OS << "objc_ownership(";
1354 switch (T->getEquivalentType().getObjCLifetime()) {
1355 case Qualifiers::OCL_None: llvm_unreachable("no ownership!");
1356 case Qualifiers::OCL_ExplicitNone: OS << "none"; break;
1357 case Qualifiers::OCL_Strong: OS << "strong"; break;
1358 case Qualifiers::OCL_Weak: OS << "weak"; break;
1359 case Qualifiers::OCL_Autoreleasing: OS << "autoreleasing"; break;
1364 // FIXME: When Sema learns to form this AttributedType, avoid printing the
1365 // attribute again in printFunctionProtoAfter.
1366 case AttributedType::attr_noreturn: OS << "noreturn"; break;
1368 case AttributedType::attr_cdecl: OS << "cdecl"; break;
1369 case AttributedType::attr_fastcall: OS << "fastcall"; break;
1370 case AttributedType::attr_stdcall: OS << "stdcall"; break;
1371 case AttributedType::attr_thiscall: OS << "thiscall"; break;
1372 case AttributedType::attr_swiftcall: OS << "swiftcall"; break;
1373 case AttributedType::attr_vectorcall: OS << "vectorcall"; break;
1374 case AttributedType::attr_pascal: OS << "pascal"; break;
1375 case AttributedType::attr_ms_abi: OS << "ms_abi"; break;
1376 case AttributedType::attr_sysv_abi: OS << "sysv_abi"; break;
1377 case AttributedType::attr_regcall: OS << "regcall"; break;
1378 case AttributedType::attr_pcs:
1379 case AttributedType::attr_pcs_vfp: {
1381 QualType t = T->getEquivalentType();
1382 while (!t->isFunctionType())
1383 t = t->getPointeeType();
1384 OS << (t->getAs<FunctionType>()->getCallConv() == CC_AAPCS ?
1385 "\"aapcs\"" : "\"aapcs-vfp\"");
1389 case AttributedType::attr_inteloclbicc: OS << "inteloclbicc"; break;
1390 case AttributedType::attr_preserve_most:
1391 OS << "preserve_most";
1393 case AttributedType::attr_preserve_all:
1394 OS << "preserve_all";
1400 void TypePrinter::printObjCInterfaceBefore(const ObjCInterfaceType *T,
1402 OS << T->getDecl()->getName();
1403 spaceBeforePlaceHolder(OS);
1405 void TypePrinter::printObjCInterfaceAfter(const ObjCInterfaceType *T,
1406 raw_ostream &OS) { }
1408 void TypePrinter::printObjCTypeParamBefore(const ObjCTypeParamType *T,
1410 OS << T->getDecl()->getName();
1411 if (!T->qual_empty()) {
1412 bool isFirst = true;
1414 for (const auto *I : T->quals()) {
1424 spaceBeforePlaceHolder(OS);
1427 void TypePrinter::printObjCTypeParamAfter(const ObjCTypeParamType *T,
1428 raw_ostream &OS) { }
1430 void TypePrinter::printObjCObjectBefore(const ObjCObjectType *T,
1432 if (T->qual_empty() && T->isUnspecializedAsWritten() &&
1433 !T->isKindOfTypeAsWritten())
1434 return printBefore(T->getBaseType(), OS);
1436 if (T->isKindOfTypeAsWritten())
1439 print(T->getBaseType(), OS, StringRef());
1441 if (T->isSpecializedAsWritten()) {
1442 bool isFirst = true;
1444 for (auto typeArg : T->getTypeArgsAsWritten()) {
1450 print(typeArg, OS, StringRef());
1455 if (!T->qual_empty()) {
1456 bool isFirst = true;
1458 for (const auto *I : T->quals()) {
1468 spaceBeforePlaceHolder(OS);
1470 void TypePrinter::printObjCObjectAfter(const ObjCObjectType *T,
1472 if (T->qual_empty() && T->isUnspecializedAsWritten() &&
1473 !T->isKindOfTypeAsWritten())
1474 return printAfter(T->getBaseType(), OS);
1477 void TypePrinter::printObjCObjectPointerBefore(const ObjCObjectPointerType *T,
1479 printBefore(T->getPointeeType(), OS);
1481 // If we need to print the pointer, print it now.
1482 if (!T->isObjCIdType() && !T->isObjCQualifiedIdType() &&
1483 !T->isObjCClassType() && !T->isObjCQualifiedClassType()) {
1484 if (HasEmptyPlaceHolder)
1489 void TypePrinter::printObjCObjectPointerAfter(const ObjCObjectPointerType *T,
1490 raw_ostream &OS) { }
1492 void TemplateSpecializationType::
1493 PrintTemplateArgumentList(raw_ostream &OS,
1494 const TemplateArgumentListInfo &Args,
1495 const PrintingPolicy &Policy) {
1496 return PrintTemplateArgumentList(OS,
1501 void TemplateSpecializationType::PrintTemplateArgumentList(
1502 raw_ostream &OS, ArrayRef<TemplateArgument> Args,
1503 const PrintingPolicy &Policy, bool SkipBrackets) {
1504 const char *Comma = Policy.MSVCFormatting ? "," : ", ";
1508 bool needSpace = false;
1509 bool FirstArg = true;
1510 for (const TemplateArgument &Arg : Args) {
1511 // Print the argument into a string.
1512 SmallString<128> Buf;
1513 llvm::raw_svector_ostream ArgOS(Buf);
1514 if (Arg.getKind() == TemplateArgument::Pack) {
1515 if (Arg.pack_size() && !FirstArg)
1517 PrintTemplateArgumentList(ArgOS,
1518 Arg.getPackAsArray(),
1523 Arg.print(Policy, ArgOS);
1525 StringRef ArgString = ArgOS.str();
1527 // If this is the first argument and its string representation
1528 // begins with the global scope specifier ('::foo'), add a space
1529 // to avoid printing the diagraph '<:'.
1530 if (FirstArg && !ArgString.empty() && ArgString[0] == ':')
1535 needSpace = (!ArgString.empty() && ArgString.back() == '>');
1539 // If the last character of our string is '>', add another space to
1540 // keep the two '>''s separate tokens. We don't *have* to do this in
1541 // C++0x, but it's still good hygiene.
1549 // Sadly, repeat all that with TemplateArgLoc.
1550 void TemplateSpecializationType::
1551 PrintTemplateArgumentList(raw_ostream &OS,
1552 ArrayRef<TemplateArgumentLoc> Args,
1553 const PrintingPolicy &Policy) {
1555 const char *Comma = Policy.MSVCFormatting ? "," : ", ";
1557 bool needSpace = false;
1558 bool FirstArg = true;
1559 for (const TemplateArgumentLoc &Arg : Args) {
1563 // Print the argument into a string.
1564 SmallString<128> Buf;
1565 llvm::raw_svector_ostream ArgOS(Buf);
1566 if (Arg.getArgument().getKind() == TemplateArgument::Pack) {
1567 PrintTemplateArgumentList(ArgOS,
1568 Arg.getArgument().getPackAsArray(),
1571 Arg.getArgument().print(Policy, ArgOS);
1573 StringRef ArgString = ArgOS.str();
1575 // If this is the first argument and its string representation
1576 // begins with the global scope specifier ('::foo'), add a space
1577 // to avoid printing the diagraph '<:'.
1578 if (FirstArg && !ArgString.empty() && ArgString[0] == ':')
1583 needSpace = (!ArgString.empty() && ArgString.back() == '>');
1587 // If the last character of our string is '>', add another space to
1588 // keep the two '>''s separate tokens. We don't *have* to do this in
1589 // C++0x, but it's still good hygiene.
1596 std::string Qualifiers::getAsString() const {
1598 return getAsString(PrintingPolicy(LO));
1601 // Appends qualifiers to the given string, separated by spaces. Will
1602 // prefix a space if the string is non-empty. Will not append a final
1604 std::string Qualifiers::getAsString(const PrintingPolicy &Policy) const {
1605 SmallString<64> Buf;
1606 llvm::raw_svector_ostream StrOS(Buf);
1607 print(StrOS, Policy);
1611 bool Qualifiers::isEmptyWhenPrinted(const PrintingPolicy &Policy) const {
1612 if (getCVRQualifiers())
1615 if (getAddressSpace())
1618 if (getObjCGCAttr())
1621 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime())
1622 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime))
1628 // Appends qualifiers to the given string, separated by spaces. Will
1629 // prefix a space if the string is non-empty. Will not append a final
1631 void Qualifiers::print(raw_ostream &OS, const PrintingPolicy& Policy,
1632 bool appendSpaceIfNonEmpty) const {
1633 bool addSpace = false;
1635 unsigned quals = getCVRQualifiers();
1637 AppendTypeQualList(OS, quals, Policy.Restrict);
1640 if (hasUnaligned()) {
1643 OS << "__unaligned";
1646 if (unsigned addrspace = getAddressSpace()) {
1650 switch (addrspace) {
1651 case LangAS::opencl_global:
1654 case LangAS::opencl_local:
1657 case LangAS::opencl_constant:
1658 case LangAS::cuda_constant:
1661 case LangAS::opencl_generic:
1664 case LangAS::cuda_device:
1667 case LangAS::cuda_shared:
1671 assert(addrspace >= LangAS::Count);
1672 OS << "__attribute__((address_space(";
1673 OS << addrspace - LangAS::Count;
1677 if (Qualifiers::GC gc = getObjCGCAttr()) {
1681 if (gc == Qualifiers::Weak)
1686 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) {
1687 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime)){
1694 case Qualifiers::OCL_None: llvm_unreachable("none but true");
1695 case Qualifiers::OCL_ExplicitNone: OS << "__unsafe_unretained"; break;
1696 case Qualifiers::OCL_Strong:
1697 if (!Policy.SuppressStrongLifetime)
1701 case Qualifiers::OCL_Weak: OS << "__weak"; break;
1702 case Qualifiers::OCL_Autoreleasing: OS << "__autoreleasing"; break;
1706 if (appendSpaceIfNonEmpty && addSpace)
1710 std::string QualType::getAsString(const PrintingPolicy &Policy) const {
1712 getAsStringInternal(S, Policy);
1716 std::string QualType::getAsString(const Type *ty, Qualifiers qs) {
1718 LangOptions options;
1719 getAsStringInternal(ty, qs, buffer, PrintingPolicy(options));
1723 void QualType::print(const Type *ty, Qualifiers qs,
1724 raw_ostream &OS, const PrintingPolicy &policy,
1725 const Twine &PlaceHolder, unsigned Indentation) {
1726 SmallString<128> PHBuf;
1727 StringRef PH = PlaceHolder.toStringRef(PHBuf);
1729 TypePrinter(policy, Indentation).print(ty, qs, OS, PH);
1732 void QualType::getAsStringInternal(const Type *ty, Qualifiers qs,
1733 std::string &buffer,
1734 const PrintingPolicy &policy) {
1735 SmallString<256> Buf;
1736 llvm::raw_svector_ostream StrOS(Buf);
1737 TypePrinter(policy).print(ty, qs, StrOS, buffer);
1738 std::string str = StrOS.str();