1 //===--- TypePrinter.cpp - Pretty-Print Clang Types -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This contains code to print types from Clang's type system.
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/PrettyPrinter.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclObjC.h"
18 #include "clang/AST/DeclTemplate.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/Type.h"
21 #include "clang/Basic/LangOptions.h"
22 #include "clang/Basic/SourceManager.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/Support/SaveAndRestore.h"
26 #include "llvm/Support/raw_ostream.h"
27 using namespace clang;
30 /// \brief RAII object that enables printing of the ARC __strong lifetime
32 class IncludeStrongLifetimeRAII {
33 PrintingPolicy &Policy;
37 explicit IncludeStrongLifetimeRAII(PrintingPolicy &Policy)
38 : Policy(Policy), Old(Policy.SuppressStrongLifetime) {
39 if (!Policy.SuppressLifetimeQualifiers)
40 Policy.SuppressStrongLifetime = false;
43 ~IncludeStrongLifetimeRAII() {
44 Policy.SuppressStrongLifetime = Old;
48 class ParamPolicyRAII {
49 PrintingPolicy &Policy;
53 explicit ParamPolicyRAII(PrintingPolicy &Policy)
54 : Policy(Policy), Old(Policy.SuppressSpecifiers) {
55 Policy.SuppressSpecifiers = false;
59 Policy.SuppressSpecifiers = Old;
63 class ElaboratedTypePolicyRAII {
64 PrintingPolicy &Policy;
65 bool SuppressTagKeyword;
69 explicit ElaboratedTypePolicyRAII(PrintingPolicy &Policy) : Policy(Policy) {
70 SuppressTagKeyword = Policy.SuppressTagKeyword;
71 SuppressScope = Policy.SuppressScope;
72 Policy.SuppressTagKeyword = true;
73 Policy.SuppressScope = true;
76 ~ElaboratedTypePolicyRAII() {
77 Policy.SuppressTagKeyword = SuppressTagKeyword;
78 Policy.SuppressScope = SuppressScope;
83 PrintingPolicy Policy;
84 bool HasEmptyPlaceHolder;
85 bool InsideCCAttribute;
88 explicit TypePrinter(const PrintingPolicy &Policy)
89 : Policy(Policy), HasEmptyPlaceHolder(false), InsideCCAttribute(false) { }
91 void print(const Type *ty, Qualifiers qs, raw_ostream &OS,
92 StringRef PlaceHolder);
93 void print(QualType T, raw_ostream &OS, StringRef PlaceHolder);
95 static bool canPrefixQualifiers(const Type *T, bool &NeedARCStrongQualifier);
96 void spaceBeforePlaceHolder(raw_ostream &OS);
97 void printTypeSpec(const NamedDecl *D, raw_ostream &OS);
99 void printBefore(const Type *ty, Qualifiers qs, raw_ostream &OS);
100 void printBefore(QualType T, raw_ostream &OS);
101 void printAfter(const Type *ty, Qualifiers qs, raw_ostream &OS);
102 void printAfter(QualType T, raw_ostream &OS);
103 void AppendScope(DeclContext *DC, raw_ostream &OS);
104 void printTag(TagDecl *T, raw_ostream &OS);
105 #define ABSTRACT_TYPE(CLASS, PARENT)
106 #define TYPE(CLASS, PARENT) \
107 void print##CLASS##Before(const CLASS##Type *T, raw_ostream &OS); \
108 void print##CLASS##After(const CLASS##Type *T, raw_ostream &OS);
109 #include "clang/AST/TypeNodes.def"
113 static void AppendTypeQualList(raw_ostream &OS, unsigned TypeQuals, bool C99) {
114 bool appendSpace = false;
115 if (TypeQuals & Qualifiers::Const) {
119 if (TypeQuals & Qualifiers::Volatile) {
120 if (appendSpace) OS << ' ';
124 if (TypeQuals & Qualifiers::Restrict) {
125 if (appendSpace) OS << ' ';
134 void TypePrinter::spaceBeforePlaceHolder(raw_ostream &OS) {
135 if (!HasEmptyPlaceHolder)
139 void TypePrinter::print(QualType t, raw_ostream &OS, StringRef PlaceHolder) {
140 SplitQualType split = t.split();
141 print(split.Ty, split.Quals, OS, PlaceHolder);
144 void TypePrinter::print(const Type *T, Qualifiers Quals, raw_ostream &OS,
145 StringRef PlaceHolder) {
151 SaveAndRestore<bool> PHVal(HasEmptyPlaceHolder, PlaceHolder.empty());
153 printBefore(T, Quals, OS);
155 printAfter(T, Quals, OS);
158 bool TypePrinter::canPrefixQualifiers(const Type *T,
159 bool &NeedARCStrongQualifier) {
160 // CanPrefixQualifiers - We prefer to print type qualifiers before the type,
161 // so that we get "const int" instead of "int const", but we can't do this if
162 // the type is complex. For example if the type is "int*", we *must* print
163 // "int * const", printing "const int *" is different. Only do this when the
164 // type expands to a simple string.
165 bool CanPrefixQualifiers = false;
166 NeedARCStrongQualifier = false;
167 Type::TypeClass TC = T->getTypeClass();
168 if (const AutoType *AT = dyn_cast<AutoType>(T))
169 TC = AT->desugar()->getTypeClass();
170 if (const SubstTemplateTypeParmType *Subst
171 = dyn_cast<SubstTemplateTypeParmType>(T))
172 TC = Subst->getReplacementType()->getTypeClass();
178 case Type::UnresolvedUsing:
180 case Type::TypeOfExpr:
183 case Type::UnaryTransform:
186 case Type::Elaborated:
187 case Type::TemplateTypeParm:
188 case Type::SubstTemplateTypeParmPack:
189 case Type::TemplateSpecialization:
190 case Type::InjectedClassName:
191 case Type::DependentName:
192 case Type::DependentTemplateSpecialization:
193 case Type::ObjCObject:
194 case Type::ObjCInterface:
196 CanPrefixQualifiers = true;
199 case Type::ObjCObjectPointer:
200 CanPrefixQualifiers = T->isObjCIdType() || T->isObjCClassType() ||
201 T->isObjCQualifiedIdType() || T->isObjCQualifiedClassType();
204 case Type::ConstantArray:
205 case Type::IncompleteArray:
206 case Type::VariableArray:
207 case Type::DependentSizedArray:
208 NeedARCStrongQualifier = true;
214 case Type::BlockPointer:
215 case Type::LValueReference:
216 case Type::RValueReference:
217 case Type::MemberPointer:
218 case Type::DependentSizedExtVector:
220 case Type::ExtVector:
221 case Type::FunctionProto:
222 case Type::FunctionNoProto:
224 case Type::Attributed:
225 case Type::PackExpansion:
226 case Type::SubstTemplateTypeParm:
227 CanPrefixQualifiers = false;
231 return CanPrefixQualifiers;
234 void TypePrinter::printBefore(QualType T, raw_ostream &OS) {
235 SplitQualType Split = T.split();
237 // If we have cv1 T, where T is substituted for cv2 U, only print cv1 - cv2
239 Qualifiers Quals = Split.Quals;
240 if (const SubstTemplateTypeParmType *Subst =
241 dyn_cast<SubstTemplateTypeParmType>(Split.Ty))
242 Quals -= QualType(Subst, 0).getQualifiers();
244 printBefore(Split.Ty, Quals, OS);
247 /// \brief Prints the part of the type string before an identifier, e.g. for
248 /// "int foo[10]" it prints "int ".
249 void TypePrinter::printBefore(const Type *T,Qualifiers Quals, raw_ostream &OS) {
250 if (Policy.SuppressSpecifiers && T->isSpecifierType())
253 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder);
255 // Print qualifiers as appropriate.
257 bool CanPrefixQualifiers = false;
258 bool NeedARCStrongQualifier = false;
259 CanPrefixQualifiers = canPrefixQualifiers(T, NeedARCStrongQualifier);
261 if (CanPrefixQualifiers && !Quals.empty()) {
262 if (NeedARCStrongQualifier) {
263 IncludeStrongLifetimeRAII Strong(Policy);
264 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/true);
266 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/true);
270 bool hasAfterQuals = false;
271 if (!CanPrefixQualifiers && !Quals.empty()) {
272 hasAfterQuals = !Quals.isEmptyWhenPrinted(Policy);
274 HasEmptyPlaceHolder = false;
277 switch (T->getTypeClass()) {
278 #define ABSTRACT_TYPE(CLASS, PARENT)
279 #define TYPE(CLASS, PARENT) case Type::CLASS: \
280 print##CLASS##Before(cast<CLASS##Type>(T), OS); \
282 #include "clang/AST/TypeNodes.def"
286 if (NeedARCStrongQualifier) {
287 IncludeStrongLifetimeRAII Strong(Policy);
288 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/!PrevPHIsEmpty.get());
290 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/!PrevPHIsEmpty.get());
295 void TypePrinter::printAfter(QualType t, raw_ostream &OS) {
296 SplitQualType split = t.split();
297 printAfter(split.Ty, split.Quals, OS);
300 /// \brief Prints the part of the type string after an identifier, e.g. for
301 /// "int foo[10]" it prints "[10]".
302 void TypePrinter::printAfter(const Type *T, Qualifiers Quals, raw_ostream &OS) {
303 switch (T->getTypeClass()) {
304 #define ABSTRACT_TYPE(CLASS, PARENT)
305 #define TYPE(CLASS, PARENT) case Type::CLASS: \
306 print##CLASS##After(cast<CLASS##Type>(T), OS); \
308 #include "clang/AST/TypeNodes.def"
312 void TypePrinter::printBuiltinBefore(const BuiltinType *T, raw_ostream &OS) {
313 OS << T->getName(Policy);
314 spaceBeforePlaceHolder(OS);
316 void TypePrinter::printBuiltinAfter(const BuiltinType *T, raw_ostream &OS) { }
318 void TypePrinter::printComplexBefore(const ComplexType *T, raw_ostream &OS) {
320 printBefore(T->getElementType(), OS);
322 void TypePrinter::printComplexAfter(const ComplexType *T, raw_ostream &OS) {
323 printAfter(T->getElementType(), OS);
326 void TypePrinter::printPointerBefore(const PointerType *T, raw_ostream &OS) {
327 IncludeStrongLifetimeRAII Strong(Policy);
328 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
329 printBefore(T->getPointeeType(), OS);
330 // Handle things like 'int (*A)[4];' correctly.
331 // FIXME: this should include vectors, but vectors use attributes I guess.
332 if (isa<ArrayType>(T->getPointeeType()))
336 void TypePrinter::printPointerAfter(const PointerType *T, raw_ostream &OS) {
337 IncludeStrongLifetimeRAII Strong(Policy);
338 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
339 // Handle things like 'int (*A)[4];' correctly.
340 // FIXME: this should include vectors, but vectors use attributes I guess.
341 if (isa<ArrayType>(T->getPointeeType()))
343 printAfter(T->getPointeeType(), OS);
346 void TypePrinter::printBlockPointerBefore(const BlockPointerType *T,
348 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
349 printBefore(T->getPointeeType(), OS);
352 void TypePrinter::printBlockPointerAfter(const BlockPointerType *T,
354 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
355 printAfter(T->getPointeeType(), OS);
358 void TypePrinter::printLValueReferenceBefore(const LValueReferenceType *T,
360 IncludeStrongLifetimeRAII Strong(Policy);
361 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
362 printBefore(T->getPointeeTypeAsWritten(), OS);
363 // Handle things like 'int (&A)[4];' correctly.
364 // FIXME: this should include vectors, but vectors use attributes I guess.
365 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
369 void TypePrinter::printLValueReferenceAfter(const LValueReferenceType *T,
371 IncludeStrongLifetimeRAII Strong(Policy);
372 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
373 // Handle things like 'int (&A)[4];' correctly.
374 // FIXME: this should include vectors, but vectors use attributes I guess.
375 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
377 printAfter(T->getPointeeTypeAsWritten(), OS);
380 void TypePrinter::printRValueReferenceBefore(const RValueReferenceType *T,
382 IncludeStrongLifetimeRAII Strong(Policy);
383 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
384 printBefore(T->getPointeeTypeAsWritten(), OS);
385 // Handle things like 'int (&&A)[4];' correctly.
386 // FIXME: this should include vectors, but vectors use attributes I guess.
387 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
391 void TypePrinter::printRValueReferenceAfter(const RValueReferenceType *T,
393 IncludeStrongLifetimeRAII Strong(Policy);
394 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
395 // Handle things like 'int (&&A)[4];' correctly.
396 // FIXME: this should include vectors, but vectors use attributes I guess.
397 if (isa<ArrayType>(T->getPointeeTypeAsWritten()))
399 printAfter(T->getPointeeTypeAsWritten(), OS);
402 void TypePrinter::printMemberPointerBefore(const MemberPointerType *T,
404 IncludeStrongLifetimeRAII Strong(Policy);
405 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
406 printBefore(T->getPointeeType(), OS);
407 // Handle things like 'int (Cls::*A)[4];' correctly.
408 // FIXME: this should include vectors, but vectors use attributes I guess.
409 if (isa<ArrayType>(T->getPointeeType()))
412 PrintingPolicy InnerPolicy(Policy);
413 InnerPolicy.SuppressTag = false;
414 TypePrinter(InnerPolicy).print(QualType(T->getClass(), 0), OS, StringRef());
418 void TypePrinter::printMemberPointerAfter(const MemberPointerType *T,
420 IncludeStrongLifetimeRAII Strong(Policy);
421 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
422 // Handle things like 'int (Cls::*A)[4];' correctly.
423 // FIXME: this should include vectors, but vectors use attributes I guess.
424 if (isa<ArrayType>(T->getPointeeType()))
426 printAfter(T->getPointeeType(), OS);
429 void TypePrinter::printConstantArrayBefore(const ConstantArrayType *T,
431 IncludeStrongLifetimeRAII Strong(Policy);
432 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
433 printBefore(T->getElementType(), OS);
435 void TypePrinter::printConstantArrayAfter(const ConstantArrayType *T,
438 if (T->getIndexTypeQualifiers().hasQualifiers()) {
439 AppendTypeQualList(OS, T->getIndexTypeCVRQualifiers(), Policy.LangOpts.C99);
443 if (T->getSizeModifier() == ArrayType::Static)
446 OS << T->getSize().getZExtValue() << ']';
447 printAfter(T->getElementType(), OS);
450 void TypePrinter::printIncompleteArrayBefore(const IncompleteArrayType *T,
452 IncludeStrongLifetimeRAII Strong(Policy);
453 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
454 printBefore(T->getElementType(), OS);
456 void TypePrinter::printIncompleteArrayAfter(const IncompleteArrayType *T,
459 printAfter(T->getElementType(), OS);
462 void TypePrinter::printVariableArrayBefore(const VariableArrayType *T,
464 IncludeStrongLifetimeRAII Strong(Policy);
465 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
466 printBefore(T->getElementType(), OS);
468 void TypePrinter::printVariableArrayAfter(const VariableArrayType *T,
471 if (T->getIndexTypeQualifiers().hasQualifiers()) {
472 AppendTypeQualList(OS, T->getIndexTypeCVRQualifiers(), Policy.LangOpts.C99);
476 if (T->getSizeModifier() == VariableArrayType::Static)
478 else if (T->getSizeModifier() == VariableArrayType::Star)
481 if (T->getSizeExpr())
482 T->getSizeExpr()->printPretty(OS, nullptr, Policy);
485 printAfter(T->getElementType(), OS);
488 void TypePrinter::printAdjustedBefore(const AdjustedType *T, raw_ostream &OS) {
489 // Print the adjusted representation, otherwise the adjustment will be
491 printBefore(T->getAdjustedType(), OS);
493 void TypePrinter::printAdjustedAfter(const AdjustedType *T, raw_ostream &OS) {
494 printAfter(T->getAdjustedType(), OS);
497 void TypePrinter::printDecayedBefore(const DecayedType *T, raw_ostream &OS) {
498 // Print as though it's a pointer.
499 printAdjustedBefore(T, OS);
501 void TypePrinter::printDecayedAfter(const DecayedType *T, raw_ostream &OS) {
502 printAdjustedAfter(T, OS);
505 void TypePrinter::printDependentSizedArrayBefore(
506 const DependentSizedArrayType *T,
508 IncludeStrongLifetimeRAII Strong(Policy);
509 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
510 printBefore(T->getElementType(), OS);
512 void TypePrinter::printDependentSizedArrayAfter(
513 const DependentSizedArrayType *T,
516 if (T->getSizeExpr())
517 T->getSizeExpr()->printPretty(OS, nullptr, Policy);
519 printAfter(T->getElementType(), OS);
522 void TypePrinter::printDependentSizedExtVectorBefore(
523 const DependentSizedExtVectorType *T,
525 printBefore(T->getElementType(), OS);
527 void TypePrinter::printDependentSizedExtVectorAfter(
528 const DependentSizedExtVectorType *T,
530 OS << " __attribute__((ext_vector_type(";
531 if (T->getSizeExpr())
532 T->getSizeExpr()->printPretty(OS, nullptr, Policy);
534 printAfter(T->getElementType(), OS);
537 void TypePrinter::printVectorBefore(const VectorType *T, raw_ostream &OS) {
538 switch (T->getVectorKind()) {
539 case VectorType::AltiVecPixel:
540 OS << "__vector __pixel ";
542 case VectorType::AltiVecBool:
543 OS << "__vector __bool ";
544 printBefore(T->getElementType(), OS);
546 case VectorType::AltiVecVector:
548 printBefore(T->getElementType(), OS);
550 case VectorType::NeonVector:
551 OS << "__attribute__((neon_vector_type("
552 << T->getNumElements() << "))) ";
553 printBefore(T->getElementType(), OS);
555 case VectorType::NeonPolyVector:
556 OS << "__attribute__((neon_polyvector_type(" <<
557 T->getNumElements() << "))) ";
558 printBefore(T->getElementType(), OS);
560 case VectorType::GenericVector: {
561 // FIXME: We prefer to print the size directly here, but have no way
562 // to get the size of the type.
563 OS << "__attribute__((__vector_size__("
564 << T->getNumElements()
566 print(T->getElementType(), OS, StringRef());
568 printBefore(T->getElementType(), OS);
573 void TypePrinter::printVectorAfter(const VectorType *T, raw_ostream &OS) {
574 printAfter(T->getElementType(), OS);
577 void TypePrinter::printExtVectorBefore(const ExtVectorType *T,
579 printBefore(T->getElementType(), OS);
581 void TypePrinter::printExtVectorAfter(const ExtVectorType *T, raw_ostream &OS) {
582 printAfter(T->getElementType(), OS);
583 OS << " __attribute__((ext_vector_type(";
584 OS << T->getNumElements();
589 FunctionProtoType::printExceptionSpecification(raw_ostream &OS,
590 const PrintingPolicy &Policy)
593 if (hasDynamicExceptionSpec()) {
595 if (getExceptionSpecType() == EST_MSAny)
598 for (unsigned I = 0, N = getNumExceptions(); I != N; ++I) {
602 OS << getExceptionType(I).stream(Policy);
605 } else if (isNoexceptExceptionSpec(getExceptionSpecType())) {
607 if (getExceptionSpecType() == EST_ComputedNoexcept) {
609 if (getNoexceptExpr())
610 getNoexceptExpr()->printPretty(OS, nullptr, Policy);
616 void TypePrinter::printFunctionProtoBefore(const FunctionProtoType *T,
618 if (T->hasTrailingReturn()) {
620 if (!HasEmptyPlaceHolder)
623 // If needed for precedence reasons, wrap the inner part in grouping parens.
624 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false);
625 printBefore(T->getReturnType(), OS);
626 if (!PrevPHIsEmpty.get())
631 void TypePrinter::printFunctionProtoAfter(const FunctionProtoType *T,
633 // If needed for precedence reasons, wrap the inner part in grouping parens.
634 if (!HasEmptyPlaceHolder)
636 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
640 ParamPolicyRAII ParamPolicy(Policy);
641 for (unsigned i = 0, e = T->getNumParams(); i != e; ++i) {
643 print(T->getParamType(i), OS, StringRef());
647 if (T->isVariadic()) {
648 if (T->getNumParams())
651 } else if (T->getNumParams() == 0 && !Policy.LangOpts.CPlusPlus) {
652 // Do not emit int() if we have a proto, emit 'int(void)'.
658 FunctionType::ExtInfo Info = T->getExtInfo();
660 if (!InsideCCAttribute) {
661 switch (Info.getCC()) {
663 // The C calling convention is the default on the vast majority of platforms
664 // we support. If the user wrote it explicitly, it will usually be printed
665 // while traversing the AttributedType. If the type has been desugared, let
666 // the canonical spelling be the implicit calling convention.
667 // FIXME: It would be better to be explicit in certain contexts, such as a
668 // cdecl function typedef used to declare a member function with the
669 // Microsoft C++ ABI.
672 OS << " __attribute__((stdcall))";
675 OS << " __attribute__((fastcall))";
678 OS << " __attribute__((thiscall))";
680 case CC_X86VectorCall:
681 OS << " __attribute__((vectorcall))";
684 OS << " __attribute__((pascal))";
687 OS << " __attribute__((pcs(\"aapcs\")))";
690 OS << " __attribute__((pcs(\"aapcs-vfp\")))";
692 case CC_IntelOclBicc:
693 OS << " __attribute__((intel_ocl_bicc))";
696 OS << " __attribute__((ms_abi))";
699 OS << " __attribute__((sysv_abi))";
701 case CC_SpirFunction:
703 // Do nothing. These CCs are not available as attributes.
708 if (Info.getNoReturn())
709 OS << " __attribute__((noreturn))";
710 if (Info.getRegParm())
711 OS << " __attribute__((regparm ("
712 << Info.getRegParm() << ")))";
714 if (unsigned quals = T->getTypeQuals()) {
716 AppendTypeQualList(OS, quals, Policy.LangOpts.C99);
719 switch (T->getRefQualifier()) {
731 T->printExceptionSpecification(OS, Policy);
733 if (T->hasTrailingReturn()) {
735 print(T->getReturnType(), OS, StringRef());
737 printAfter(T->getReturnType(), OS);
740 void TypePrinter::printFunctionNoProtoBefore(const FunctionNoProtoType *T,
742 // If needed for precedence reasons, wrap the inner part in grouping parens.
743 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false);
744 printBefore(T->getReturnType(), OS);
745 if (!PrevPHIsEmpty.get())
748 void TypePrinter::printFunctionNoProtoAfter(const FunctionNoProtoType *T,
750 // If needed for precedence reasons, wrap the inner part in grouping parens.
751 if (!HasEmptyPlaceHolder)
753 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false);
756 if (T->getNoReturnAttr())
757 OS << " __attribute__((noreturn))";
758 printAfter(T->getReturnType(), OS);
761 void TypePrinter::printTypeSpec(const NamedDecl *D, raw_ostream &OS) {
762 IdentifierInfo *II = D->getIdentifier();
764 spaceBeforePlaceHolder(OS);
767 void TypePrinter::printUnresolvedUsingBefore(const UnresolvedUsingType *T,
769 printTypeSpec(T->getDecl(), OS);
771 void TypePrinter::printUnresolvedUsingAfter(const UnresolvedUsingType *T,
774 void TypePrinter::printTypedefBefore(const TypedefType *T, raw_ostream &OS) {
775 printTypeSpec(T->getDecl(), OS);
777 void TypePrinter::printTypedefAfter(const TypedefType *T, raw_ostream &OS) { }
779 void TypePrinter::printTypeOfExprBefore(const TypeOfExprType *T,
782 if (T->getUnderlyingExpr())
783 T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy);
784 spaceBeforePlaceHolder(OS);
786 void TypePrinter::printTypeOfExprAfter(const TypeOfExprType *T,
789 void TypePrinter::printTypeOfBefore(const TypeOfType *T, raw_ostream &OS) {
791 print(T->getUnderlyingType(), OS, StringRef());
793 spaceBeforePlaceHolder(OS);
795 void TypePrinter::printTypeOfAfter(const TypeOfType *T, raw_ostream &OS) { }
797 void TypePrinter::printDecltypeBefore(const DecltypeType *T, raw_ostream &OS) {
799 if (T->getUnderlyingExpr())
800 T->getUnderlyingExpr()->printPretty(OS, nullptr, Policy);
802 spaceBeforePlaceHolder(OS);
804 void TypePrinter::printDecltypeAfter(const DecltypeType *T, raw_ostream &OS) { }
806 void TypePrinter::printUnaryTransformBefore(const UnaryTransformType *T,
808 IncludeStrongLifetimeRAII Strong(Policy);
810 switch (T->getUTTKind()) {
811 case UnaryTransformType::EnumUnderlyingType:
812 OS << "__underlying_type(";
813 print(T->getBaseType(), OS, StringRef());
815 spaceBeforePlaceHolder(OS);
819 printBefore(T->getBaseType(), OS);
821 void TypePrinter::printUnaryTransformAfter(const UnaryTransformType *T,
823 IncludeStrongLifetimeRAII Strong(Policy);
825 switch (T->getUTTKind()) {
826 case UnaryTransformType::EnumUnderlyingType:
830 printAfter(T->getBaseType(), OS);
833 void TypePrinter::printAutoBefore(const AutoType *T, raw_ostream &OS) {
834 // If the type has been deduced, do not print 'auto'.
835 if (!T->getDeducedType().isNull()) {
836 printBefore(T->getDeducedType(), OS);
838 OS << (T->isDecltypeAuto() ? "decltype(auto)" : "auto");
839 spaceBeforePlaceHolder(OS);
842 void TypePrinter::printAutoAfter(const AutoType *T, raw_ostream &OS) {
843 // If the type has been deduced, do not print 'auto'.
844 if (!T->getDeducedType().isNull())
845 printAfter(T->getDeducedType(), OS);
848 void TypePrinter::printAtomicBefore(const AtomicType *T, raw_ostream &OS) {
849 IncludeStrongLifetimeRAII Strong(Policy);
852 print(T->getValueType(), OS, StringRef());
854 spaceBeforePlaceHolder(OS);
856 void TypePrinter::printAtomicAfter(const AtomicType *T, raw_ostream &OS) { }
858 /// Appends the given scope to the end of a string.
859 void TypePrinter::AppendScope(DeclContext *DC, raw_ostream &OS) {
860 if (DC->isTranslationUnit()) return;
861 if (DC->isFunctionOrMethod()) return;
862 AppendScope(DC->getParent(), OS);
864 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
865 if (Policy.SuppressUnwrittenScope &&
866 (NS->isAnonymousNamespace() || NS->isInline()))
868 if (NS->getIdentifier())
869 OS << NS->getName() << "::";
871 OS << "(anonymous namespace)::";
872 } else if (ClassTemplateSpecializationDecl *Spec
873 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
874 IncludeStrongLifetimeRAII Strong(Policy);
875 OS << Spec->getIdentifier()->getName();
876 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
877 TemplateSpecializationType::PrintTemplateArgumentList(OS,
882 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
883 if (TypedefNameDecl *Typedef = Tag->getTypedefNameForAnonDecl())
884 OS << Typedef->getIdentifier()->getName() << "::";
885 else if (Tag->getIdentifier())
886 OS << Tag->getIdentifier()->getName() << "::";
892 void TypePrinter::printTag(TagDecl *D, raw_ostream &OS) {
893 if (Policy.SuppressTag)
896 bool HasKindDecoration = false;
898 // bool SuppressTagKeyword
899 // = Policy.LangOpts.CPlusPlus || Policy.SuppressTagKeyword;
901 // We don't print tags unless this is an elaborated type.
902 // In C, we just assume every RecordType is an elaborated type.
903 if (!(Policy.LangOpts.CPlusPlus || Policy.SuppressTagKeyword ||
904 D->getTypedefNameForAnonDecl())) {
905 HasKindDecoration = true;
906 OS << D->getKindName();
910 // Compute the full nested-name-specifier for this type.
911 // In C, this will always be empty except when the type
912 // being printed is anonymous within other Record.
913 if (!Policy.SuppressScope)
914 AppendScope(D->getDeclContext(), OS);
916 if (const IdentifierInfo *II = D->getIdentifier())
918 else if (TypedefNameDecl *Typedef = D->getTypedefNameForAnonDecl()) {
919 assert(Typedef->getIdentifier() && "Typedef without identifier?");
920 OS << Typedef->getIdentifier()->getName();
922 // Make an unambiguous representation for anonymous types, e.g.
923 // (anonymous enum at /usr/include/string.h:120:9)
925 if (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda()) {
927 HasKindDecoration = true;
932 if (Policy.AnonymousTagLocations) {
933 // Suppress the redundant tag keyword if we just printed one.
934 // We don't have to worry about ElaboratedTypes here because you can't
935 // refer to an anonymous type with one.
936 if (!HasKindDecoration)
937 OS << " " << D->getKindName();
939 PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc(
941 if (PLoc.isValid()) {
942 OS << " at " << PLoc.getFilename()
943 << ':' << PLoc.getLine()
944 << ':' << PLoc.getColumn();
951 // If this is a class template specialization, print the template
953 if (ClassTemplateSpecializationDecl *Spec
954 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
955 const TemplateArgument *Args;
957 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
958 const TemplateSpecializationType *TST =
959 cast<TemplateSpecializationType>(TAW->getType());
960 Args = TST->getArgs();
961 NumArgs = TST->getNumArgs();
963 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
964 Args = TemplateArgs.data();
965 NumArgs = TemplateArgs.size();
967 IncludeStrongLifetimeRAII Strong(Policy);
968 TemplateSpecializationType::PrintTemplateArgumentList(OS,
973 spaceBeforePlaceHolder(OS);
976 void TypePrinter::printRecordBefore(const RecordType *T, raw_ostream &OS) {
977 printTag(T->getDecl(), OS);
979 void TypePrinter::printRecordAfter(const RecordType *T, raw_ostream &OS) { }
981 void TypePrinter::printEnumBefore(const EnumType *T, raw_ostream &OS) {
982 printTag(T->getDecl(), OS);
984 void TypePrinter::printEnumAfter(const EnumType *T, raw_ostream &OS) { }
986 void TypePrinter::printTemplateTypeParmBefore(const TemplateTypeParmType *T,
988 if (IdentifierInfo *Id = T->getIdentifier())
991 OS << "type-parameter-" << T->getDepth() << '-' << T->getIndex();
992 spaceBeforePlaceHolder(OS);
994 void TypePrinter::printTemplateTypeParmAfter(const TemplateTypeParmType *T,
997 void TypePrinter::printSubstTemplateTypeParmBefore(
998 const SubstTemplateTypeParmType *T,
1000 IncludeStrongLifetimeRAII Strong(Policy);
1001 printBefore(T->getReplacementType(), OS);
1003 void TypePrinter::printSubstTemplateTypeParmAfter(
1004 const SubstTemplateTypeParmType *T,
1006 IncludeStrongLifetimeRAII Strong(Policy);
1007 printAfter(T->getReplacementType(), OS);
1010 void TypePrinter::printSubstTemplateTypeParmPackBefore(
1011 const SubstTemplateTypeParmPackType *T,
1013 IncludeStrongLifetimeRAII Strong(Policy);
1014 printTemplateTypeParmBefore(T->getReplacedParameter(), OS);
1016 void TypePrinter::printSubstTemplateTypeParmPackAfter(
1017 const SubstTemplateTypeParmPackType *T,
1019 IncludeStrongLifetimeRAII Strong(Policy);
1020 printTemplateTypeParmAfter(T->getReplacedParameter(), OS);
1023 void TypePrinter::printTemplateSpecializationBefore(
1024 const TemplateSpecializationType *T,
1026 IncludeStrongLifetimeRAII Strong(Policy);
1027 T->getTemplateName().print(OS, Policy);
1029 TemplateSpecializationType::PrintTemplateArgumentList(OS,
1033 spaceBeforePlaceHolder(OS);
1035 void TypePrinter::printTemplateSpecializationAfter(
1036 const TemplateSpecializationType *T,
1037 raw_ostream &OS) { }
1039 void TypePrinter::printInjectedClassNameBefore(const InjectedClassNameType *T,
1041 printTemplateSpecializationBefore(T->getInjectedTST(), OS);
1043 void TypePrinter::printInjectedClassNameAfter(const InjectedClassNameType *T,
1044 raw_ostream &OS) { }
1046 void TypePrinter::printElaboratedBefore(const ElaboratedType *T,
1048 if (Policy.SuppressTag && isa<TagType>(T->getNamedType()))
1050 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1051 if (T->getKeyword() != ETK_None)
1053 NestedNameSpecifier* Qualifier = T->getQualifier();
1055 Qualifier->print(OS, Policy);
1057 ElaboratedTypePolicyRAII PolicyRAII(Policy);
1058 printBefore(T->getNamedType(), OS);
1060 void TypePrinter::printElaboratedAfter(const ElaboratedType *T,
1062 ElaboratedTypePolicyRAII PolicyRAII(Policy);
1063 printAfter(T->getNamedType(), OS);
1066 void TypePrinter::printParenBefore(const ParenType *T, raw_ostream &OS) {
1067 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) {
1068 printBefore(T->getInnerType(), OS);
1071 printBefore(T->getInnerType(), OS);
1073 void TypePrinter::printParenAfter(const ParenType *T, raw_ostream &OS) {
1074 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) {
1076 printAfter(T->getInnerType(), OS);
1078 printAfter(T->getInnerType(), OS);
1081 void TypePrinter::printDependentNameBefore(const DependentNameType *T,
1083 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1084 if (T->getKeyword() != ETK_None)
1087 T->getQualifier()->print(OS, Policy);
1089 OS << T->getIdentifier()->getName();
1090 spaceBeforePlaceHolder(OS);
1092 void TypePrinter::printDependentNameAfter(const DependentNameType *T,
1093 raw_ostream &OS) { }
1095 void TypePrinter::printDependentTemplateSpecializationBefore(
1096 const DependentTemplateSpecializationType *T, raw_ostream &OS) {
1097 IncludeStrongLifetimeRAII Strong(Policy);
1099 OS << TypeWithKeyword::getKeywordName(T->getKeyword());
1100 if (T->getKeyword() != ETK_None)
1103 if (T->getQualifier())
1104 T->getQualifier()->print(OS, Policy);
1105 OS << T->getIdentifier()->getName();
1106 TemplateSpecializationType::PrintTemplateArgumentList(OS,
1110 spaceBeforePlaceHolder(OS);
1112 void TypePrinter::printDependentTemplateSpecializationAfter(
1113 const DependentTemplateSpecializationType *T, raw_ostream &OS) { }
1115 void TypePrinter::printPackExpansionBefore(const PackExpansionType *T,
1117 printBefore(T->getPattern(), OS);
1119 void TypePrinter::printPackExpansionAfter(const PackExpansionType *T,
1121 printAfter(T->getPattern(), OS);
1125 void TypePrinter::printAttributedBefore(const AttributedType *T,
1127 // Prefer the macro forms of the GC and ownership qualifiers.
1128 if (T->getAttrKind() == AttributedType::attr_objc_gc ||
1129 T->getAttrKind() == AttributedType::attr_objc_ownership)
1130 return printBefore(T->getEquivalentType(), OS);
1132 printBefore(T->getModifiedType(), OS);
1134 if (T->isMSTypeSpec()) {
1135 switch (T->getAttrKind()) {
1137 case AttributedType::attr_ptr32: OS << " __ptr32"; break;
1138 case AttributedType::attr_ptr64: OS << " __ptr64"; break;
1139 case AttributedType::attr_sptr: OS << " __sptr"; break;
1140 case AttributedType::attr_uptr: OS << " __uptr"; break;
1142 spaceBeforePlaceHolder(OS);
1146 void TypePrinter::printAttributedAfter(const AttributedType *T,
1148 // Prefer the macro forms of the GC and ownership qualifiers.
1149 if (T->getAttrKind() == AttributedType::attr_objc_gc ||
1150 T->getAttrKind() == AttributedType::attr_objc_ownership)
1151 return printAfter(T->getEquivalentType(), OS);
1153 // TODO: not all attributes are GCC-style attributes.
1154 if (T->isMSTypeSpec())
1157 // If this is a calling convention attribute, don't print the implicit CC from
1158 // the modified type.
1159 SaveAndRestore<bool> MaybeSuppressCC(InsideCCAttribute, T->isCallingConv());
1161 printAfter(T->getModifiedType(), OS);
1163 OS << " __attribute__((";
1164 switch (T->getAttrKind()) {
1165 default: llvm_unreachable("This attribute should have been handled already");
1166 case AttributedType::attr_address_space:
1167 OS << "address_space(";
1168 OS << T->getEquivalentType().getAddressSpace();
1172 case AttributedType::attr_vector_size: {
1173 OS << "__vector_size__(";
1174 if (const VectorType *vector =T->getEquivalentType()->getAs<VectorType>()) {
1175 OS << vector->getNumElements();
1177 print(vector->getElementType(), OS, StringRef());
1184 case AttributedType::attr_neon_vector_type:
1185 case AttributedType::attr_neon_polyvector_type: {
1186 if (T->getAttrKind() == AttributedType::attr_neon_vector_type)
1187 OS << "neon_vector_type(";
1189 OS << "neon_polyvector_type(";
1190 const VectorType *vector = T->getEquivalentType()->getAs<VectorType>();
1191 OS << vector->getNumElements();
1196 case AttributedType::attr_regparm: {
1197 // FIXME: When Sema learns to form this AttributedType, avoid printing the
1198 // attribute again in printFunctionProtoAfter.
1200 QualType t = T->getEquivalentType();
1201 while (!t->isFunctionType())
1202 t = t->getPointeeType();
1203 OS << t->getAs<FunctionType>()->getRegParmType();
1208 case AttributedType::attr_objc_gc: {
1211 QualType tmp = T->getEquivalentType();
1212 while (tmp.getObjCGCAttr() == Qualifiers::GCNone) {
1213 QualType next = tmp->getPointeeType();
1214 if (next == tmp) break;
1218 if (tmp.isObjCGCWeak())
1226 case AttributedType::attr_objc_ownership:
1227 OS << "objc_ownership(";
1228 switch (T->getEquivalentType().getObjCLifetime()) {
1229 case Qualifiers::OCL_None: llvm_unreachable("no ownership!");
1230 case Qualifiers::OCL_ExplicitNone: OS << "none"; break;
1231 case Qualifiers::OCL_Strong: OS << "strong"; break;
1232 case Qualifiers::OCL_Weak: OS << "weak"; break;
1233 case Qualifiers::OCL_Autoreleasing: OS << "autoreleasing"; break;
1238 // FIXME: When Sema learns to form this AttributedType, avoid printing the
1239 // attribute again in printFunctionProtoAfter.
1240 case AttributedType::attr_noreturn: OS << "noreturn"; break;
1242 case AttributedType::attr_cdecl: OS << "cdecl"; break;
1243 case AttributedType::attr_fastcall: OS << "fastcall"; break;
1244 case AttributedType::attr_stdcall: OS << "stdcall"; break;
1245 case AttributedType::attr_thiscall: OS << "thiscall"; break;
1246 case AttributedType::attr_vectorcall: OS << "vectorcall"; break;
1247 case AttributedType::attr_pascal: OS << "pascal"; break;
1248 case AttributedType::attr_ms_abi: OS << "ms_abi"; break;
1249 case AttributedType::attr_sysv_abi: OS << "sysv_abi"; break;
1250 case AttributedType::attr_pcs:
1251 case AttributedType::attr_pcs_vfp: {
1253 QualType t = T->getEquivalentType();
1254 while (!t->isFunctionType())
1255 t = t->getPointeeType();
1256 OS << (t->getAs<FunctionType>()->getCallConv() == CC_AAPCS ?
1257 "\"aapcs\"" : "\"aapcs-vfp\"");
1261 case AttributedType::attr_inteloclbicc: OS << "inteloclbicc"; break;
1266 void TypePrinter::printObjCInterfaceBefore(const ObjCInterfaceType *T,
1268 OS << T->getDecl()->getName();
1269 spaceBeforePlaceHolder(OS);
1271 void TypePrinter::printObjCInterfaceAfter(const ObjCInterfaceType *T,
1272 raw_ostream &OS) { }
1274 void TypePrinter::printObjCObjectBefore(const ObjCObjectType *T,
1276 if (T->qual_empty())
1277 return printBefore(T->getBaseType(), OS);
1279 print(T->getBaseType(), OS, StringRef());
1281 bool isFirst = true;
1282 for (const auto *I : T->quals()) {
1290 spaceBeforePlaceHolder(OS);
1292 void TypePrinter::printObjCObjectAfter(const ObjCObjectType *T,
1294 if (T->qual_empty())
1295 return printAfter(T->getBaseType(), OS);
1298 void TypePrinter::printObjCObjectPointerBefore(const ObjCObjectPointerType *T,
1300 T->getPointeeType().getLocalQualifiers().print(OS, Policy,
1301 /*appendSpaceIfNonEmpty=*/true);
1303 assert(!T->isObjCSelType());
1305 if (T->isObjCIdType() || T->isObjCQualifiedIdType())
1307 else if (T->isObjCClassType() || T->isObjCQualifiedClassType())
1310 OS << T->getInterfaceDecl()->getName();
1312 if (!T->qual_empty()) {
1314 for (ObjCObjectPointerType::qual_iterator I = T->qual_begin(),
1317 OS << (*I)->getName();
1324 if (!T->isObjCIdType() && !T->isObjCQualifiedIdType() &&
1325 !T->isObjCClassType() && !T->isObjCQualifiedClassType()) {
1326 OS << " *"; // Don't forget the implicit pointer.
1328 spaceBeforePlaceHolder(OS);
1331 void TypePrinter::printObjCObjectPointerAfter(const ObjCObjectPointerType *T,
1332 raw_ostream &OS) { }
1334 void TemplateSpecializationType::
1335 PrintTemplateArgumentList(raw_ostream &OS,
1336 const TemplateArgumentListInfo &Args,
1337 const PrintingPolicy &Policy) {
1338 return PrintTemplateArgumentList(OS,
1339 Args.getArgumentArray(),
1345 TemplateSpecializationType::PrintTemplateArgumentList(
1347 const TemplateArgument *Args,
1349 const PrintingPolicy &Policy,
1350 bool SkipBrackets) {
1354 bool needSpace = false;
1355 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1356 // Print the argument into a string.
1357 SmallString<128> Buf;
1358 llvm::raw_svector_ostream ArgOS(Buf);
1359 if (Args[Arg].getKind() == TemplateArgument::Pack) {
1360 if (Args[Arg].pack_size() && Arg > 0)
1362 PrintTemplateArgumentList(ArgOS,
1363 Args[Arg].pack_begin(),
1364 Args[Arg].pack_size(),
1369 Args[Arg].print(Policy, ArgOS);
1371 StringRef ArgString = ArgOS.str();
1373 // If this is the first argument and its string representation
1374 // begins with the global scope specifier ('::foo'), add a space
1375 // to avoid printing the diagraph '<:'.
1376 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1381 needSpace = (!ArgString.empty() && ArgString.back() == '>');
1384 // If the last character of our string is '>', add another space to
1385 // keep the two '>''s separate tokens. We don't *have* to do this in
1386 // C++0x, but it's still good hygiene.
1394 // Sadly, repeat all that with TemplateArgLoc.
1395 void TemplateSpecializationType::
1396 PrintTemplateArgumentList(raw_ostream &OS,
1397 const TemplateArgumentLoc *Args, unsigned NumArgs,
1398 const PrintingPolicy &Policy) {
1401 bool needSpace = false;
1402 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1406 // Print the argument into a string.
1407 SmallString<128> Buf;
1408 llvm::raw_svector_ostream ArgOS(Buf);
1409 if (Args[Arg].getArgument().getKind() == TemplateArgument::Pack) {
1410 PrintTemplateArgumentList(ArgOS,
1411 Args[Arg].getArgument().pack_begin(),
1412 Args[Arg].getArgument().pack_size(),
1415 Args[Arg].getArgument().print(Policy, ArgOS);
1417 StringRef ArgString = ArgOS.str();
1419 // If this is the first argument and its string representation
1420 // begins with the global scope specifier ('::foo'), add a space
1421 // to avoid printing the diagraph '<:'.
1422 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1427 needSpace = (!ArgString.empty() && ArgString.back() == '>');
1430 // If the last character of our string is '>', add another space to
1431 // keep the two '>''s separate tokens. We don't *have* to do this in
1432 // C++0x, but it's still good hygiene.
1439 std::string Qualifiers::getAsString() const {
1441 return getAsString(PrintingPolicy(LO));
1444 // Appends qualifiers to the given string, separated by spaces. Will
1445 // prefix a space if the string is non-empty. Will not append a final
1447 std::string Qualifiers::getAsString(const PrintingPolicy &Policy) const {
1448 SmallString<64> Buf;
1449 llvm::raw_svector_ostream StrOS(Buf);
1450 print(StrOS, Policy);
1454 bool Qualifiers::isEmptyWhenPrinted(const PrintingPolicy &Policy) const {
1455 if (getCVRQualifiers())
1458 if (getAddressSpace())
1461 if (getObjCGCAttr())
1464 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime())
1465 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime))
1471 // Appends qualifiers to the given string, separated by spaces. Will
1472 // prefix a space if the string is non-empty. Will not append a final
1474 void Qualifiers::print(raw_ostream &OS, const PrintingPolicy& Policy,
1475 bool appendSpaceIfNonEmpty) const {
1476 bool addSpace = false;
1478 unsigned quals = getCVRQualifiers();
1480 AppendTypeQualList(OS, quals, Policy.LangOpts.C99);
1483 if (unsigned addrspace = getAddressSpace()) {
1487 switch (addrspace) {
1488 case LangAS::opencl_global:
1491 case LangAS::opencl_local:
1494 case LangAS::opencl_constant:
1497 case LangAS::opencl_generic:
1501 OS << "__attribute__((address_space(";
1506 if (Qualifiers::GC gc = getObjCGCAttr()) {
1510 if (gc == Qualifiers::Weak)
1515 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) {
1516 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime)){
1523 case Qualifiers::OCL_None: llvm_unreachable("none but true");
1524 case Qualifiers::OCL_ExplicitNone: OS << "__unsafe_unretained"; break;
1525 case Qualifiers::OCL_Strong:
1526 if (!Policy.SuppressStrongLifetime)
1530 case Qualifiers::OCL_Weak: OS << "__weak"; break;
1531 case Qualifiers::OCL_Autoreleasing: OS << "__autoreleasing"; break;
1535 if (appendSpaceIfNonEmpty && addSpace)
1539 std::string QualType::getAsString(const PrintingPolicy &Policy) const {
1541 getAsStringInternal(S, Policy);
1545 std::string QualType::getAsString(const Type *ty, Qualifiers qs) {
1547 LangOptions options;
1548 getAsStringInternal(ty, qs, buffer, PrintingPolicy(options));
1552 void QualType::print(const Type *ty, Qualifiers qs,
1553 raw_ostream &OS, const PrintingPolicy &policy,
1554 const Twine &PlaceHolder) {
1555 SmallString<128> PHBuf;
1556 StringRef PH = PlaceHolder.toStringRef(PHBuf);
1558 TypePrinter(policy).print(ty, qs, OS, PH);
1561 void QualType::getAsStringInternal(const Type *ty, Qualifiers qs,
1562 std::string &buffer,
1563 const PrintingPolicy &policy) {
1564 SmallString<256> Buf;
1565 llvm::raw_svector_ostream StrOS(Buf);
1566 TypePrinter(policy).print(ty, qs, StrOS, buffer);
1567 std::string str = StrOS.str();