1 //===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the Decl::print method, which pretty prints the
10 // AST back out to C/Objective-C/C++/Objective-C++ code.
12 //===----------------------------------------------------------------------===//
13 #include "clang/AST/ASTContext.h"
14 #include "clang/AST/Attr.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/AST/DeclCXX.h"
17 #include "clang/AST/DeclObjC.h"
18 #include "clang/AST/DeclTemplate.h"
19 #include "clang/AST/DeclVisitor.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/ExprCXX.h"
22 #include "clang/AST/PrettyPrinter.h"
23 #include "clang/Basic/Module.h"
24 #include "llvm/Support/raw_ostream.h"
25 using namespace clang;
28 class DeclPrinter : public DeclVisitor<DeclPrinter> {
30 PrintingPolicy Policy;
31 const ASTContext &Context;
33 bool PrintInstantiation;
35 raw_ostream& Indent() { return Indent(Indentation); }
36 raw_ostream& Indent(unsigned Indentation);
37 void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls);
39 void Print(AccessSpecifier AS);
40 void PrintConstructorInitializers(CXXConstructorDecl *CDecl,
43 /// Print an Objective-C method type in parentheses.
45 /// \param Quals The Objective-C declaration qualifiers.
46 /// \param T The type to print.
47 void PrintObjCMethodType(ASTContext &Ctx, Decl::ObjCDeclQualifier Quals,
50 void PrintObjCTypeParams(ObjCTypeParamList *Params);
53 DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy,
54 const ASTContext &Context, unsigned Indentation = 0,
55 bool PrintInstantiation = false)
56 : Out(Out), Policy(Policy), Context(Context), Indentation(Indentation),
57 PrintInstantiation(PrintInstantiation) {}
59 void VisitDeclContext(DeclContext *DC, bool Indent = true);
61 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
62 void VisitTypedefDecl(TypedefDecl *D);
63 void VisitTypeAliasDecl(TypeAliasDecl *D);
64 void VisitEnumDecl(EnumDecl *D);
65 void VisitRecordDecl(RecordDecl *D);
66 void VisitEnumConstantDecl(EnumConstantDecl *D);
67 void VisitEmptyDecl(EmptyDecl *D);
68 void VisitFunctionDecl(FunctionDecl *D);
69 void VisitFriendDecl(FriendDecl *D);
70 void VisitFieldDecl(FieldDecl *D);
71 void VisitVarDecl(VarDecl *D);
72 void VisitLabelDecl(LabelDecl *D);
73 void VisitParmVarDecl(ParmVarDecl *D);
74 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
75 void VisitImportDecl(ImportDecl *D);
76 void VisitStaticAssertDecl(StaticAssertDecl *D);
77 void VisitNamespaceDecl(NamespaceDecl *D);
78 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
79 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
80 void VisitCXXRecordDecl(CXXRecordDecl *D);
81 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
82 void VisitTemplateDecl(const TemplateDecl *D);
83 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
84 void VisitClassTemplateDecl(ClassTemplateDecl *D);
85 void VisitClassTemplateSpecializationDecl(
86 ClassTemplateSpecializationDecl *D);
87 void VisitClassTemplatePartialSpecializationDecl(
88 ClassTemplatePartialSpecializationDecl *D);
89 void VisitObjCMethodDecl(ObjCMethodDecl *D);
90 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
91 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
92 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
93 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
94 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
95 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
96 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
97 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
98 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
99 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
100 void VisitUsingDecl(UsingDecl *D);
101 void VisitUsingShadowDecl(UsingShadowDecl *D);
102 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
103 void VisitOMPAllocateDecl(OMPAllocateDecl *D);
104 void VisitOMPRequiresDecl(OMPRequiresDecl *D);
105 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
106 void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D);
107 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
109 void printTemplateParameters(const TemplateParameterList *Params,
110 bool OmitTemplateKW = false);
111 void printTemplateArguments(llvm::ArrayRef<TemplateArgument> Args);
112 void printTemplateArguments(llvm::ArrayRef<TemplateArgumentLoc> Args);
113 void prettyPrintAttributes(Decl *D);
114 void prettyPrintPragmas(Decl *D);
115 void printDeclType(QualType T, StringRef DeclName, bool Pack = false);
119 void Decl::print(raw_ostream &Out, unsigned Indentation,
120 bool PrintInstantiation) const {
121 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
124 void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
125 unsigned Indentation, bool PrintInstantiation) const {
126 DeclPrinter Printer(Out, Policy, getASTContext(), Indentation,
128 Printer.Visit(const_cast<Decl*>(this));
131 void TemplateParameterList::print(raw_ostream &Out, const ASTContext &Context,
132 bool OmitTemplateKW) const {
133 print(Out, Context, Context.getPrintingPolicy(), OmitTemplateKW);
136 void TemplateParameterList::print(raw_ostream &Out, const ASTContext &Context,
137 const PrintingPolicy &Policy,
138 bool OmitTemplateKW) const {
139 DeclPrinter Printer(Out, Policy, Context);
140 Printer.printTemplateParameters(this, OmitTemplateKW);
143 static QualType GetBaseType(QualType T) {
144 // FIXME: This should be on the Type class!
145 QualType BaseType = T;
146 while (!BaseType->isSpecifierType()) {
147 if (const PointerType *PTy = BaseType->getAs<PointerType>())
148 BaseType = PTy->getPointeeType();
149 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
150 BaseType = BPy->getPointeeType();
151 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
152 BaseType = ATy->getElementType();
153 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
154 BaseType = FTy->getReturnType();
155 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
156 BaseType = VTy->getElementType();
157 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
158 BaseType = RTy->getPointeeType();
159 else if (const AutoType *ATy = BaseType->getAs<AutoType>())
160 BaseType = ATy->getDeducedType();
161 else if (const ParenType *PTy = BaseType->getAs<ParenType>())
162 BaseType = PTy->desugar();
164 // This must be a syntax error.
170 static QualType getDeclType(Decl* D) {
171 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
172 return TDD->getUnderlyingType();
173 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
174 return VD->getType();
178 void Decl::printGroup(Decl** Begin, unsigned NumDecls,
179 raw_ostream &Out, const PrintingPolicy &Policy,
180 unsigned Indentation) {
182 (*Begin)->print(Out, Policy, Indentation);
186 Decl** End = Begin + NumDecls;
187 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
191 PrintingPolicy SubPolicy(Policy);
194 for ( ; Begin != End; ++Begin) {
197 SubPolicy.IncludeTagDefinition = true;
198 SubPolicy.SuppressSpecifiers = false;
201 if (!isFirst) Out << ", ";
202 SubPolicy.IncludeTagDefinition = false;
203 SubPolicy.SuppressSpecifiers = true;
206 (*Begin)->print(Out, SubPolicy, Indentation);
210 LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
211 // Get the translation unit
212 const DeclContext *DC = this;
213 while (!DC->isTranslationUnit())
214 DC = DC->getParent();
216 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
217 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), Ctx, 0);
218 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
221 raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
222 for (unsigned i = 0; i != Indentation; ++i)
227 void DeclPrinter::prettyPrintAttributes(Decl *D) {
228 if (Policy.PolishForDeclaration)
232 AttrVec &Attrs = D->getAttrs();
233 for (auto *A : Attrs) {
234 if (A->isInherited() || A->isImplicit())
236 switch (A->getKind()) {
238 #define PRAGMA_SPELLING_ATTR(X) case attr::X:
239 #include "clang/Basic/AttrList.inc"
242 A->printPretty(Out, Policy);
249 void DeclPrinter::prettyPrintPragmas(Decl *D) {
250 if (Policy.PolishForDeclaration)
254 AttrVec &Attrs = D->getAttrs();
255 for (auto *A : Attrs) {
256 switch (A->getKind()) {
258 #define PRAGMA_SPELLING_ATTR(X) case attr::X:
259 #include "clang/Basic/AttrList.inc"
260 A->printPretty(Out, Policy);
270 void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) {
271 // Normally, a PackExpansionType is written as T[3]... (for instance, as a
272 // template argument), but if it is the type of a declaration, the ellipsis
273 // is placed before the name being declared.
274 if (auto *PET = T->getAs<PackExpansionType>()) {
276 T = PET->getPattern();
278 T.print(Out, Policy, (Pack ? "..." : "") + DeclName, Indentation);
281 void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
283 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
289 void DeclPrinter::Print(AccessSpecifier AS) {
291 case AS_none: llvm_unreachable("No access specifier!");
292 case AS_public: Out << "public"; break;
293 case AS_protected: Out << "protected"; break;
294 case AS_private: Out << "private"; break;
298 void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl,
299 std::string &Proto) {
300 bool HasInitializerList = false;
301 for (const auto *BMInitializer : CDecl->inits()) {
302 if (BMInitializer->isInClassMemberInitializer())
305 if (!HasInitializerList) {
309 HasInitializerList = true;
313 if (BMInitializer->isAnyMemberInitializer()) {
314 FieldDecl *FD = BMInitializer->getAnyMember();
317 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
321 if (!BMInitializer->getInit()) {
324 Expr *Init = BMInitializer->getInit();
325 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
326 Init = Tmp->getSubExpr();
328 Init = Init->IgnoreParens();
330 Expr *SimpleInit = nullptr;
331 Expr **Args = nullptr;
332 unsigned NumArgs = 0;
333 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
334 Args = ParenList->getExprs();
335 NumArgs = ParenList->getNumExprs();
336 } else if (CXXConstructExpr *Construct =
337 dyn_cast<CXXConstructExpr>(Init)) {
338 Args = Construct->getArgs();
339 NumArgs = Construct->getNumArgs();
344 SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
346 for (unsigned I = 0; I != NumArgs; ++I) {
347 assert(Args[I] != nullptr && "Expected non-null Expr");
348 if (isa<CXXDefaultArgExpr>(Args[I]))
353 Args[I]->printPretty(Out, nullptr, Policy, Indentation);
358 if (BMInitializer->isPackExpansion())
363 //----------------------------------------------------------------------------
364 // Common C declarations
365 //----------------------------------------------------------------------------
367 void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
368 if (Policy.TerseOutput)
372 Indentation += Policy.Indentation;
374 SmallVector<Decl*, 2> Decls;
375 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
378 // Don't print ObjCIvarDecls, as they are printed when visiting the
379 // containing ObjCInterfaceDecl.
380 if (isa<ObjCIvarDecl>(*D))
383 // Skip over implicit declarations in pretty-printing mode.
387 // Don't print implicit specializations, as they are printed when visiting
388 // corresponding templates.
389 if (auto FD = dyn_cast<FunctionDecl>(*D))
390 if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation &&
391 !isa<ClassTemplateSpecializationDecl>(DC))
394 // The next bits of code handle stuff like "struct {int x;} a,b"; we're
395 // forced to merge the declarations because there's no other way to
396 // refer to the struct in question. When that struct is named instead, we
397 // also need to merge to avoid splitting off a stand-alone struct
398 // declaration that produces the warning ext_no_declarators in some
401 // This limited merging is safe without a bunch of other checks because it
402 // only merges declarations directly referring to the tag, not typedefs.
404 // Check whether the current declaration should be grouped with a previous
405 // non-free-standing tag declaration.
406 QualType CurDeclType = getDeclType(*D);
407 if (!Decls.empty() && !CurDeclType.isNull()) {
408 QualType BaseType = GetBaseType(CurDeclType);
409 if (!BaseType.isNull() && isa<ElaboratedType>(BaseType) &&
410 cast<ElaboratedType>(BaseType)->getOwnedTagDecl() == Decls[0]) {
416 // If we have a merged group waiting to be handled, handle it now.
418 ProcessDeclGroup(Decls);
420 // If the current declaration is not a free standing declaration, save it
421 // so we can merge it with the subsequent declaration(s) using it.
422 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->isFreeStanding()) {
427 if (isa<AccessSpecDecl>(*D)) {
428 Indentation -= Policy.Indentation;
430 Print(D->getAccess());
432 Indentation += Policy.Indentation;
439 // FIXME: Need to be able to tell the DeclPrinter when
440 const char *Terminator = nullptr;
441 if (isa<OMPThreadPrivateDecl>(*D) || isa<OMPDeclareReductionDecl>(*D) ||
442 isa<OMPDeclareMapperDecl>(*D) || isa<OMPRequiresDecl>(*D) ||
443 isa<OMPAllocateDecl>(*D))
444 Terminator = nullptr;
445 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->hasBody())
446 Terminator = nullptr;
447 else if (auto FD = dyn_cast<FunctionDecl>(*D)) {
448 if (FD->isThisDeclarationADefinition())
449 Terminator = nullptr;
452 } else if (auto TD = dyn_cast<FunctionTemplateDecl>(*D)) {
453 if (TD->getTemplatedDecl()->isThisDeclarationADefinition())
454 Terminator = nullptr;
457 } else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
458 isa<ObjCImplementationDecl>(*D) ||
459 isa<ObjCInterfaceDecl>(*D) ||
460 isa<ObjCProtocolDecl>(*D) ||
461 isa<ObjCCategoryImplDecl>(*D) ||
462 isa<ObjCCategoryDecl>(*D))
463 Terminator = nullptr;
464 else if (isa<EnumConstantDecl>(*D)) {
465 DeclContext::decl_iterator Next = D;
474 if (!Policy.TerseOutput &&
475 ((isa<FunctionDecl>(*D) &&
476 cast<FunctionDecl>(*D)->doesThisDeclarationHaveABody()) ||
477 (isa<FunctionTemplateDecl>(*D) &&
478 cast<FunctionTemplateDecl>(*D)->getTemplatedDecl()->doesThisDeclarationHaveABody())))
479 ; // StmtPrinter already added '\n' after CompoundStmt.
483 // Declare target attribute is special one, natural spelling for the pragma
484 // assumes "ending" construct so print it here.
485 if (D->hasAttr<OMPDeclareTargetDeclAttr>())
486 Out << "#pragma omp end declare target\n";
490 ProcessDeclGroup(Decls);
493 Indentation -= Policy.Indentation;
496 void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
497 VisitDeclContext(D, false);
500 void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
501 if (!Policy.SuppressSpecifiers) {
504 if (D->isModulePrivate())
505 Out << "__module_private__ ";
507 QualType Ty = D->getTypeSourceInfo()->getType();
508 Ty.print(Out, Policy, D->getName(), Indentation);
509 prettyPrintAttributes(D);
512 void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
513 Out << "using " << *D;
514 prettyPrintAttributes(D);
515 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
518 void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
519 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
520 Out << "__module_private__ ";
523 if (D->isScopedUsingClassTag())
529 prettyPrintAttributes(D);
533 if (D->isFixed() && D->getASTContext().getLangOpts().CPlusPlus11)
534 Out << " : " << D->getIntegerType().stream(Policy);
536 if (D->isCompleteDefinition()) {
543 void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
544 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
545 Out << "__module_private__ ";
546 Out << D->getKindName();
548 prettyPrintAttributes(D);
550 if (D->getIdentifier())
553 if (D->isCompleteDefinition()) {
560 void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
562 prettyPrintAttributes(D);
563 if (Expr *Init = D->getInitExpr()) {
565 Init->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context);
569 static void printExplicitSpecifier(ExplicitSpecifier ES, llvm::raw_ostream &Out,
570 PrintingPolicy &Policy,
571 unsigned Indentation) {
572 std::string Proto = "explicit";
573 llvm::raw_string_ostream EOut(Proto);
576 ES.getExpr()->printPretty(EOut, nullptr, Policy, Indentation);
584 void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
585 if (!D->getDescribedFunctionTemplate() &&
586 !D->isFunctionTemplateSpecialization())
587 prettyPrintPragmas(D);
589 if (D->isFunctionTemplateSpecialization())
590 Out << "template<> ";
591 else if (!D->getDescribedFunctionTemplate()) {
592 for (unsigned I = 0, NumTemplateParams = D->getNumTemplateParameterLists();
593 I < NumTemplateParams; ++I)
594 printTemplateParameters(D->getTemplateParameterList(I));
597 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
598 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
599 CXXDeductionGuideDecl *GuideDecl = dyn_cast<CXXDeductionGuideDecl>(D);
600 if (!Policy.SuppressSpecifiers) {
601 switch (D->getStorageClass()) {
603 case SC_Extern: Out << "extern "; break;
604 case SC_Static: Out << "static "; break;
605 case SC_PrivateExtern: Out << "__private_extern__ "; break;
606 case SC_Auto: case SC_Register:
607 llvm_unreachable("invalid for functions");
610 if (D->isInlineSpecified()) Out << "inline ";
611 if (D->isVirtualAsWritten()) Out << "virtual ";
612 if (D->isModulePrivate()) Out << "__module_private__ ";
613 if (D->isConstexprSpecified() && !D->isExplicitlyDefaulted())
615 if (D->isConsteval()) Out << "consteval ";
616 ExplicitSpecifier ExplicitSpec = ExplicitSpecifier::getFromDecl(D);
617 if (ExplicitSpec.isSpecified())
618 printExplicitSpecifier(ExplicitSpec, Out, Policy, Indentation);
621 PrintingPolicy SubPolicy(Policy);
622 SubPolicy.SuppressSpecifiers = false;
625 if (Policy.FullyQualifiedName) {
626 Proto += D->getQualifiedNameAsString();
628 llvm::raw_string_ostream OS(Proto);
629 if (!Policy.SuppressScope) {
630 if (const NestedNameSpecifier *NS = D->getQualifier()) {
631 NS->print(OS, Policy);
634 D->getNameInfo().printName(OS, Policy);
638 Proto = GuideDecl->getDeducedTemplate()->getDeclName().getAsString();
639 if (D->isFunctionTemplateSpecialization()) {
640 llvm::raw_string_ostream POut(Proto);
641 DeclPrinter TArgPrinter(POut, SubPolicy, Context, Indentation);
642 const auto *TArgAsWritten = D->getTemplateSpecializationArgsAsWritten();
643 if (TArgAsWritten && !Policy.PrintCanonicalTypes)
644 TArgPrinter.printTemplateArguments(TArgAsWritten->arguments());
645 else if (const TemplateArgumentList *TArgs =
646 D->getTemplateSpecializationArgs())
647 TArgPrinter.printTemplateArguments(TArgs->asArray());
650 QualType Ty = D->getType();
651 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
652 Proto = '(' + Proto + ')';
653 Ty = PT->getInnerType();
656 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
657 const FunctionProtoType *FT = nullptr;
658 if (D->hasWrittenPrototype())
659 FT = dyn_cast<FunctionProtoType>(AFT);
663 llvm::raw_string_ostream POut(Proto);
664 DeclPrinter ParamPrinter(POut, SubPolicy, Context, Indentation);
665 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
667 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
670 if (FT->isVariadic()) {
671 if (D->getNumParams()) POut << ", ";
674 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
675 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
678 Proto += D->getParamDecl(i)->getNameAsString();
687 if (FT->isVolatile())
688 Proto += " volatile";
689 if (FT->isRestrict())
690 Proto += " restrict";
692 switch (FT->getRefQualifier()) {
704 if (FT && FT->hasDynamicExceptionSpec()) {
706 if (FT->getExceptionSpecType() == EST_MSAny)
709 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
713 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
716 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
717 Proto += " noexcept";
718 if (isComputedNoexcept(FT->getExceptionSpecType())) {
720 llvm::raw_string_ostream EOut(Proto);
721 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
730 if (!Policy.TerseOutput)
731 PrintConstructorInitializers(CDecl, Proto);
732 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
733 if (FT && FT->hasTrailingReturn()) {
736 Out << Proto << " -> ";
739 AFT->getReturnType().print(Out, Policy, Proto);
744 if (Expr *TrailingRequiresClause = D->getTrailingRequiresClause()) {
746 TrailingRequiresClause->printPretty(Out, nullptr, SubPolicy, Indentation);
749 Ty.print(Out, Policy, Proto);
752 prettyPrintAttributes(D);
756 else if (D->isDeletedAsWritten())
758 else if (D->isExplicitlyDefaulted())
760 else if (D->doesThisDeclarationHaveABody()) {
761 if (!Policy.TerseOutput) {
762 if (!D->hasPrototype() && D->getNumParams()) {
763 // This is a K&R function definition, so we need to print the
766 DeclPrinter ParamPrinter(Out, SubPolicy, Context, Indentation);
767 Indentation += Policy.Indentation;
768 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
770 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
773 Indentation -= Policy.Indentation;
778 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
780 if (!Policy.TerseOutput && isa<CXXConstructorDecl>(*D))
786 void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
787 if (TypeSourceInfo *TSI = D->getFriendType()) {
788 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
789 for (unsigned i = 0; i < NumTPLists; ++i)
790 printTemplateParameters(D->getFriendTypeTemplateParameterList(i));
792 Out << " " << TSI->getType().getAsString(Policy);
794 else if (FunctionDecl *FD =
795 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
797 VisitFunctionDecl(FD);
799 else if (FunctionTemplateDecl *FTD =
800 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
802 VisitFunctionTemplateDecl(FTD);
804 else if (ClassTemplateDecl *CTD =
805 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
807 VisitRedeclarableTemplateDecl(CTD);
811 void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
812 // FIXME: add printing of pragma attributes if required.
813 if (!Policy.SuppressSpecifiers && D->isMutable())
815 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
816 Out << "__module_private__ ";
818 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
819 stream(Policy, D->getName(), Indentation);
821 if (D->isBitField()) {
823 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
826 Expr *Init = D->getInClassInitializer();
827 if (!Policy.SuppressInitializers && Init) {
828 if (D->getInClassInitStyle() == ICIS_ListInit)
832 Init->printPretty(Out, nullptr, Policy, Indentation);
834 prettyPrintAttributes(D);
837 void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
841 void DeclPrinter::VisitVarDecl(VarDecl *D) {
842 prettyPrintPragmas(D);
844 QualType T = D->getTypeSourceInfo()
845 ? D->getTypeSourceInfo()->getType()
846 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
848 if (!Policy.SuppressSpecifiers) {
849 StorageClass SC = D->getStorageClass();
851 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
853 switch (D->getTSCSpec()) {
854 case TSCS_unspecified:
859 case TSCS__Thread_local:
860 Out << "_Thread_local ";
862 case TSCS_thread_local:
863 Out << "thread_local ";
867 if (D->isModulePrivate())
868 Out << "__module_private__ ";
870 if (D->isConstexpr()) {
872 T.removeLocalConst();
876 printDeclType(T, D->getName());
877 Expr *Init = D->getInit();
878 if (!Policy.SuppressInitializers && Init) {
879 bool ImplicitInit = false;
880 if (CXXConstructExpr *Construct =
881 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
882 if (D->getInitStyle() == VarDecl::CallInit &&
883 !Construct->isListInitialization()) {
884 ImplicitInit = Construct->getNumArgs() == 0 ||
885 Construct->getArg(0)->isDefaultArgument();
889 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
891 else if (D->getInitStyle() == VarDecl::CInit) {
894 PrintingPolicy SubPolicy(Policy);
895 SubPolicy.SuppressSpecifiers = false;
896 SubPolicy.IncludeTagDefinition = false;
897 Init->printPretty(Out, nullptr, SubPolicy, Indentation);
898 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
902 prettyPrintAttributes(D);
905 void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
909 void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
911 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
915 void DeclPrinter::VisitImportDecl(ImportDecl *D) {
916 Out << "@import " << D->getImportedModule()->getFullModuleName()
920 void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
921 Out << "static_assert(";
922 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
923 if (StringLiteral *SL = D->getMessage()) {
925 SL->printPretty(Out, nullptr, Policy, Indentation);
930 //----------------------------------------------------------------------------
932 //----------------------------------------------------------------------------
933 void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
936 Out << "namespace " << *D << " {\n";
941 void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
942 Out << "using namespace ";
943 if (D->getQualifier())
944 D->getQualifier()->print(Out, Policy);
945 Out << *D->getNominatedNamespaceAsWritten();
948 void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
949 Out << "namespace " << *D << " = ";
950 if (D->getQualifier())
951 D->getQualifier()->print(Out, Policy);
952 Out << *D->getAliasedNamespace();
955 void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
956 prettyPrintAttributes(D);
959 void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
960 // FIXME: add printing of pragma attributes if required.
961 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
962 Out << "__module_private__ ";
963 Out << D->getKindName();
965 prettyPrintAttributes(D);
967 if (D->getIdentifier()) {
970 if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
971 ArrayRef<TemplateArgument> Args = S->getTemplateArgs().asArray();
972 if (!Policy.PrintCanonicalTypes)
973 if (const auto* TSI = S->getTypeAsWritten())
974 if (const auto *TST =
975 dyn_cast<TemplateSpecializationType>(TSI->getType()))
976 Args = TST->template_arguments();
977 printTemplateArguments(Args);
981 if (D->isCompleteDefinition()) {
982 // Print the base classes
983 if (D->getNumBases()) {
985 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
986 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
987 if (Base != D->bases_begin())
990 if (Base->isVirtual())
993 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
998 Out << Base->getType().getAsString(Policy);
1000 if (Base->isPackExpansion())
1005 // Print the class definition
1006 // FIXME: Doesn't print access specifiers, e.g., "public:"
1007 if (Policy.TerseOutput) {
1011 VisitDeclContext(D);
1017 void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1019 if (D->getLanguage() == LinkageSpecDecl::lang_c)
1022 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
1023 "unknown language in linkage specification");
1027 Out << "extern \"" << l << "\" ";
1028 if (D->hasBraces()) {
1030 VisitDeclContext(D);
1033 Visit(*D->decls_begin());
1036 void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params,
1037 bool OmitTemplateKW) {
1040 if (!OmitTemplateKW)
1044 bool NeedComma = false;
1045 for (const Decl *Param : *Params) {
1046 if (Param->isImplicit())
1054 if (auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
1056 if (const TypeConstraint *TC = TTP->getTypeConstraint())
1057 TC->print(Out, Policy);
1058 else if (TTP->wasDeclaredWithTypename())
1063 if (TTP->isParameterPack())
1065 else if (!TTP->getName().empty())
1070 if (TTP->hasDefaultArgument()) {
1072 Out << TTP->getDefaultArgument().getAsString(Policy);
1074 } else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1076 if (IdentifierInfo *II = NTTP->getIdentifier())
1077 Name = II->getName();
1078 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
1080 if (NTTP->hasDefaultArgument()) {
1082 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
1085 } else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
1086 VisitTemplateDecl(TTPD);
1087 // FIXME: print the default argument, if present.
1092 if (!OmitTemplateKW)
1096 void DeclPrinter::printTemplateArguments(ArrayRef<TemplateArgument> Args) {
1098 for (size_t I = 0, E = Args.size(); I < E; ++I) {
1101 Args[I].print(Policy, Out);
1106 void DeclPrinter::printTemplateArguments(ArrayRef<TemplateArgumentLoc> Args) {
1108 for (size_t I = 0, E = Args.size(); I < E; ++I) {
1111 Args[I].getArgument().print(Policy, Out);
1116 void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
1117 printTemplateParameters(D->getTemplateParameters());
1119 if (const TemplateTemplateParmDecl *TTP =
1120 dyn_cast<TemplateTemplateParmDecl>(D)) {
1122 if (TTP->isParameterPack())
1124 Out << D->getName();
1125 } else if (auto *TD = D->getTemplatedDecl())
1127 else if (const auto *Concept = dyn_cast<ConceptDecl>(D)) {
1128 Out << "concept " << Concept->getName() << " = " ;
1129 Concept->getConstraintExpr()->printPretty(Out, nullptr, Policy,
1135 void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
1136 prettyPrintPragmas(D->getTemplatedDecl());
1137 // Print any leading template parameter lists.
1138 if (const FunctionDecl *FD = D->getTemplatedDecl()) {
1139 for (unsigned I = 0, NumTemplateParams = FD->getNumTemplateParameterLists();
1140 I < NumTemplateParams; ++I)
1141 printTemplateParameters(FD->getTemplateParameterList(I));
1143 VisitRedeclarableTemplateDecl(D);
1144 // Declare target attribute is special one, natural spelling for the pragma
1145 // assumes "ending" construct so print it here.
1146 if (D->getTemplatedDecl()->hasAttr<OMPDeclareTargetDeclAttr>())
1147 Out << "#pragma omp end declare target\n";
1149 // Never print "instantiations" for deduction guides (they don't really
1151 if (PrintInstantiation &&
1152 !isa<CXXDeductionGuideDecl>(D->getTemplatedDecl())) {
1153 FunctionDecl *PrevDecl = D->getTemplatedDecl();
1154 const FunctionDecl *Def;
1155 if (PrevDecl->isDefined(Def) && Def != PrevDecl)
1157 for (auto *I : D->specializations())
1158 if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) {
1159 if (!PrevDecl->isThisDeclarationADefinition())
1162 prettyPrintPragmas(I);
1168 void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
1169 VisitRedeclarableTemplateDecl(D);
1171 if (PrintInstantiation) {
1172 for (auto *I : D->specializations())
1173 if (I->getSpecializationKind() == TSK_ImplicitInstantiation) {
1174 if (D->isThisDeclarationADefinition())
1182 void DeclPrinter::VisitClassTemplateSpecializationDecl(
1183 ClassTemplateSpecializationDecl *D) {
1184 Out << "template<> ";
1185 VisitCXXRecordDecl(D);
1188 void DeclPrinter::VisitClassTemplatePartialSpecializationDecl(
1189 ClassTemplatePartialSpecializationDecl *D) {
1190 printTemplateParameters(D->getTemplateParameters());
1191 VisitCXXRecordDecl(D);
1194 //----------------------------------------------------------------------------
1195 // Objective-C declarations
1196 //----------------------------------------------------------------------------
1198 void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
1199 Decl::ObjCDeclQualifier Quals,
1202 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
1204 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
1206 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1208 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1210 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1212 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1214 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
1215 if (auto nullability = AttributedType::stripOuterNullability(T))
1216 Out << getNullabilitySpelling(*nullability, true) << ' ';
1219 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1223 void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1225 unsigned First = true;
1226 for (auto *Param : *Params) {
1233 switch (Param->getVariance()) {
1234 case ObjCTypeParamVariance::Invariant:
1237 case ObjCTypeParamVariance::Covariant:
1238 Out << "__covariant ";
1241 case ObjCTypeParamVariance::Contravariant:
1242 Out << "__contravariant ";
1246 Out << Param->getDeclName().getAsString();
1248 if (Param->hasExplicitBound()) {
1249 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1255 void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1256 if (OMD->isInstanceMethod())
1260 if (!OMD->getReturnType().isNull()) {
1261 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1262 OMD->getReturnType());
1265 std::string name = OMD->getSelector().getAsString();
1266 std::string::size_type pos, lastPos = 0;
1267 for (const auto *PI : OMD->parameters()) {
1268 // FIXME: selector is missing here!
1269 pos = name.find_first_of(':', lastPos);
1272 Out << name.substr(lastPos, pos - lastPos) << ':';
1273 PrintObjCMethodType(OMD->getASTContext(),
1274 PI->getObjCDeclQualifier(),
1280 if (OMD->param_begin() == OMD->param_end())
1283 if (OMD->isVariadic())
1286 prettyPrintAttributes(OMD);
1288 if (OMD->getBody() && !Policy.TerseOutput) {
1290 OMD->getBody()->printPretty(Out, nullptr, Policy);
1292 else if (Policy.PolishForDeclaration)
1296 void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1297 std::string I = OID->getNameAsString();
1298 ObjCInterfaceDecl *SID = OID->getSuperClass();
1300 bool eolnOut = false;
1302 Out << "@implementation " << I << " : " << *SID;
1304 Out << "@implementation " << I;
1306 if (OID->ivar_size() > 0) {
1309 Indentation += Policy.Indentation;
1310 for (const auto *I : OID->ivars()) {
1311 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
1312 getAsString(Policy) << ' ' << *I << ";\n";
1314 Indentation -= Policy.Indentation;
1317 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1321 VisitDeclContext(OID, false);
1327 void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1328 std::string I = OID->getNameAsString();
1329 ObjCInterfaceDecl *SID = OID->getSuperClass();
1331 if (!OID->isThisDeclarationADefinition()) {
1332 Out << "@class " << I;
1334 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1335 PrintObjCTypeParams(TypeParams);
1341 bool eolnOut = false;
1342 Out << "@interface " << I;
1344 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1345 PrintObjCTypeParams(TypeParams);
1349 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
1352 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1353 if (!Protocols.empty()) {
1354 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1355 E = Protocols.end(); I != E; ++I)
1356 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1360 if (OID->ivar_size() > 0) {
1363 Indentation += Policy.Indentation;
1364 for (const auto *I : OID->ivars()) {
1365 Indent() << I->getASTContext()
1366 .getUnqualifiedObjCPointerType(I->getType())
1367 .getAsString(Policy) << ' ' << *I << ";\n";
1369 Indentation -= Policy.Indentation;
1372 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1377 VisitDeclContext(OID, false);
1381 // FIXME: implement the rest...
1384 void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
1385 if (!PID->isThisDeclarationADefinition()) {
1386 Out << "@protocol " << *PID << ";\n";
1390 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1391 if (!Protocols.empty()) {
1392 Out << "@protocol " << *PID;
1393 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1394 E = Protocols.end(); I != E; ++I)
1395 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1398 Out << "@protocol " << *PID << '\n';
1399 VisitDeclContext(PID, false);
1403 void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
1404 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
1406 VisitDeclContext(PID, false);
1408 // FIXME: implement the rest...
1411 void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
1412 Out << "@interface " << *PID->getClassInterface();
1413 if (auto TypeParams = PID->getTypeParamList()) {
1414 PrintObjCTypeParams(TypeParams);
1416 Out << "(" << *PID << ")\n";
1417 if (PID->ivar_size() > 0) {
1419 Indentation += Policy.Indentation;
1420 for (const auto *I : PID->ivars())
1421 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
1422 getAsString(Policy) << ' ' << *I << ";\n";
1423 Indentation -= Policy.Indentation;
1427 VisitDeclContext(PID, false);
1430 // FIXME: implement the rest...
1433 void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
1434 Out << "@compatibility_alias " << *AID
1435 << ' ' << *AID->getClassInterface() << ";\n";
1438 /// PrintObjCPropertyDecl - print a property declaration.
1440 /// Print attributes in the following order:
1442 /// - nonatomic | atomic
1443 /// - assign | retain | strong | copy | weak | unsafe_unretained
1444 /// - readwrite | readonly
1445 /// - getter & setter
1447 void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1448 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1449 Out << "@required\n";
1450 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1451 Out << "@optional\n";
1453 QualType T = PDecl->getType();
1456 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1459 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) {
1460 Out << (first ? "" : ", ") << "class";
1464 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_direct) {
1465 Out << (first ? "" : ", ") << "direct";
1469 if (PDecl->getPropertyAttributes() &
1470 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1471 Out << (first ? "" : ", ") << "nonatomic";
1474 if (PDecl->getPropertyAttributes() &
1475 ObjCPropertyDecl::OBJC_PR_atomic) {
1476 Out << (first ? "" : ", ") << "atomic";
1480 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1481 Out << (first ? "" : ", ") << "assign";
1484 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1485 Out << (first ? "" : ", ") << "retain";
1489 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1490 Out << (first ? "" : ", ") << "strong";
1493 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1494 Out << (first ? "" : ", ") << "copy";
1497 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak) {
1498 Out << (first ? "" : ", ") << "weak";
1501 if (PDecl->getPropertyAttributes()
1502 & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
1503 Out << (first ? "" : ", ") << "unsafe_unretained";
1507 if (PDecl->getPropertyAttributes() &
1508 ObjCPropertyDecl::OBJC_PR_readwrite) {
1509 Out << (first ? "" : ", ") << "readwrite";
1512 if (PDecl->getPropertyAttributes() &
1513 ObjCPropertyDecl::OBJC_PR_readonly) {
1514 Out << (first ? "" : ", ") << "readonly";
1518 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1519 Out << (first ? "" : ", ") << "getter = ";
1520 PDecl->getGetterName().print(Out);
1523 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1524 Out << (first ? "" : ", ") << "setter = ";
1525 PDecl->getSetterName().print(Out);
1529 if (PDecl->getPropertyAttributes() &
1530 ObjCPropertyDecl::OBJC_PR_nullability) {
1531 if (auto nullability = AttributedType::stripOuterNullability(T)) {
1532 if (*nullability == NullabilityKind::Unspecified &&
1533 (PDecl->getPropertyAttributes() &
1534 ObjCPropertyDecl::OBJC_PR_null_resettable)) {
1535 Out << (first ? "" : ", ") << "null_resettable";
1537 Out << (first ? "" : ", ")
1538 << getNullabilitySpelling(*nullability, true);
1544 (void) first; // Silence dead store warning due to idiomatic code.
1547 std::string TypeStr = PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
1548 getAsString(Policy);
1549 Out << ' ' << TypeStr;
1550 if (!StringRef(TypeStr).endswith("*"))
1553 if (Policy.PolishForDeclaration)
1557 void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1558 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1559 Out << "@synthesize ";
1562 Out << *PID->getPropertyDecl();
1563 if (PID->getPropertyIvarDecl())
1564 Out << '=' << *PID->getPropertyIvarDecl();
1567 void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1568 if (!D->isAccessDeclaration())
1570 if (D->hasTypename())
1572 D->getQualifier()->print(Out, Policy);
1574 // Use the correct record name when the using declaration is used for
1575 // inheriting constructors.
1576 for (const auto *Shadow : D->shadows()) {
1577 if (const auto *ConstructorShadow =
1578 dyn_cast<ConstructorUsingShadowDecl>(Shadow)) {
1579 assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext());
1580 Out << *ConstructorShadow->getNominatedBaseClass();
1588 DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1589 Out << "using typename ";
1590 D->getQualifier()->print(Out, Policy);
1591 Out << D->getDeclName();
1594 void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1595 if (!D->isAccessDeclaration())
1597 D->getQualifier()->print(Out, Policy);
1598 Out << D->getDeclName();
1601 void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1605 void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1606 Out << "#pragma omp threadprivate";
1607 if (!D->varlist_empty()) {
1608 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1609 E = D->varlist_end();
1611 Out << (I == D->varlist_begin() ? '(' : ',');
1612 NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl();
1613 ND->printQualifiedName(Out);
1619 void DeclPrinter::VisitOMPAllocateDecl(OMPAllocateDecl *D) {
1620 Out << "#pragma omp allocate";
1621 if (!D->varlist_empty()) {
1622 for (OMPAllocateDecl::varlist_iterator I = D->varlist_begin(),
1623 E = D->varlist_end();
1625 Out << (I == D->varlist_begin() ? '(' : ',');
1626 NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl();
1627 ND->printQualifiedName(Out);
1631 if (!D->clauselist_empty()) {
1633 OMPClausePrinter Printer(Out, Policy);
1634 for (OMPClause *C : D->clauselists())
1639 void DeclPrinter::VisitOMPRequiresDecl(OMPRequiresDecl *D) {
1640 Out << "#pragma omp requires ";
1641 if (!D->clauselist_empty()) {
1642 OMPClausePrinter Printer(Out, Policy);
1643 for (auto I = D->clauselist_begin(), E = D->clauselist_end(); I != E; ++I)
1648 void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1649 if (!D->isInvalidDecl()) {
1650 Out << "#pragma omp declare reduction (";
1651 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
1652 const char *OpName =
1653 getOperatorSpelling(D->getDeclName().getCXXOverloadedOperator());
1654 assert(OpName && "not an overloaded operator");
1657 assert(D->getDeclName().isIdentifier());
1661 D->getType().print(Out, Policy);
1663 D->getCombiner()->printPretty(Out, nullptr, Policy, 0);
1665 if (auto *Init = D->getInitializer()) {
1666 Out << " initializer(";
1667 switch (D->getInitializerKind()) {
1668 case OMPDeclareReductionDecl::DirectInit:
1671 case OMPDeclareReductionDecl::CopyInit:
1672 Out << "omp_priv = ";
1674 case OMPDeclareReductionDecl::CallInit:
1677 Init->printPretty(Out, nullptr, Policy, 0);
1678 if (D->getInitializerKind() == OMPDeclareReductionDecl::DirectInit)
1685 void DeclPrinter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
1686 if (!D->isInvalidDecl()) {
1687 Out << "#pragma omp declare mapper (";
1690 D->getType().print(Out, Policy);
1692 Out << D->getVarName();
1694 if (!D->clauselist_empty()) {
1695 OMPClausePrinter Printer(Out, Policy);
1696 for (auto *C : D->clauselists()) {
1704 void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
1705 D->getInit()->printPretty(Out, nullptr, Policy, Indentation);