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(const TemplateArgumentList &Args,
112 const TemplateParameterList *Params = nullptr);
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 if (!Policy.SuppressScope) {
629 if (const NestedNameSpecifier *NS = D->getQualifier()) {
630 llvm::raw_string_ostream OS(Proto);
631 NS->print(OS, Policy);
634 Proto += D->getNameInfo().getAsString();
638 Proto = GuideDecl->getDeducedTemplate()->getDeclName().getAsString();
639 if (const TemplateArgumentList *TArgs = D->getTemplateSpecializationArgs()) {
640 llvm::raw_string_ostream POut(Proto);
641 DeclPrinter TArgPrinter(POut, SubPolicy, Context, Indentation);
642 TArgPrinter.printTemplateArguments(*TArgs);
645 QualType Ty = D->getType();
646 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
647 Proto = '(' + Proto + ')';
648 Ty = PT->getInnerType();
651 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
652 const FunctionProtoType *FT = nullptr;
653 if (D->hasWrittenPrototype())
654 FT = dyn_cast<FunctionProtoType>(AFT);
658 llvm::raw_string_ostream POut(Proto);
659 DeclPrinter ParamPrinter(POut, SubPolicy, Context, Indentation);
660 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
662 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
665 if (FT->isVariadic()) {
666 if (D->getNumParams()) POut << ", ";
669 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
670 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
673 Proto += D->getParamDecl(i)->getNameAsString();
682 if (FT->isVolatile())
683 Proto += " volatile";
684 if (FT->isRestrict())
685 Proto += " restrict";
687 switch (FT->getRefQualifier()) {
699 if (FT && FT->hasDynamicExceptionSpec()) {
701 if (FT->getExceptionSpecType() == EST_MSAny)
704 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
708 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
711 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
712 Proto += " noexcept";
713 if (isComputedNoexcept(FT->getExceptionSpecType())) {
715 llvm::raw_string_ostream EOut(Proto);
716 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
725 if (!Policy.TerseOutput)
726 PrintConstructorInitializers(CDecl, Proto);
727 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
728 if (FT && FT->hasTrailingReturn()) {
731 Out << Proto << " -> ";
734 AFT->getReturnType().print(Out, Policy, Proto);
739 Ty.print(Out, Policy, Proto);
742 prettyPrintAttributes(D);
746 else if (D->isDeletedAsWritten())
748 else if (D->isExplicitlyDefaulted())
750 else if (D->doesThisDeclarationHaveABody()) {
751 if (!Policy.TerseOutput) {
752 if (!D->hasPrototype() && D->getNumParams()) {
753 // This is a K&R function definition, so we need to print the
756 DeclPrinter ParamPrinter(Out, SubPolicy, Context, Indentation);
757 Indentation += Policy.Indentation;
758 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
760 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
763 Indentation -= Policy.Indentation;
768 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
770 if (!Policy.TerseOutput && isa<CXXConstructorDecl>(*D))
776 void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
777 if (TypeSourceInfo *TSI = D->getFriendType()) {
778 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
779 for (unsigned i = 0; i < NumTPLists; ++i)
780 printTemplateParameters(D->getFriendTypeTemplateParameterList(i));
782 Out << " " << TSI->getType().getAsString(Policy);
784 else if (FunctionDecl *FD =
785 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
787 VisitFunctionDecl(FD);
789 else if (FunctionTemplateDecl *FTD =
790 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
792 VisitFunctionTemplateDecl(FTD);
794 else if (ClassTemplateDecl *CTD =
795 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
797 VisitRedeclarableTemplateDecl(CTD);
801 void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
802 // FIXME: add printing of pragma attributes if required.
803 if (!Policy.SuppressSpecifiers && D->isMutable())
805 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
806 Out << "__module_private__ ";
808 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
809 stream(Policy, D->getName(), Indentation);
811 if (D->isBitField()) {
813 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
816 Expr *Init = D->getInClassInitializer();
817 if (!Policy.SuppressInitializers && Init) {
818 if (D->getInClassInitStyle() == ICIS_ListInit)
822 Init->printPretty(Out, nullptr, Policy, Indentation);
824 prettyPrintAttributes(D);
827 void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
831 void DeclPrinter::VisitVarDecl(VarDecl *D) {
832 prettyPrintPragmas(D);
834 QualType T = D->getTypeSourceInfo()
835 ? D->getTypeSourceInfo()->getType()
836 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
838 if (!Policy.SuppressSpecifiers) {
839 StorageClass SC = D->getStorageClass();
841 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
843 switch (D->getTSCSpec()) {
844 case TSCS_unspecified:
849 case TSCS__Thread_local:
850 Out << "_Thread_local ";
852 case TSCS_thread_local:
853 Out << "thread_local ";
857 if (D->isModulePrivate())
858 Out << "__module_private__ ";
860 if (D->isConstexpr()) {
862 T.removeLocalConst();
866 printDeclType(T, D->getName());
867 Expr *Init = D->getInit();
868 if (!Policy.SuppressInitializers && Init) {
869 bool ImplicitInit = false;
870 if (CXXConstructExpr *Construct =
871 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
872 if (D->getInitStyle() == VarDecl::CallInit &&
873 !Construct->isListInitialization()) {
874 ImplicitInit = Construct->getNumArgs() == 0 ||
875 Construct->getArg(0)->isDefaultArgument();
879 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
881 else if (D->getInitStyle() == VarDecl::CInit) {
884 PrintingPolicy SubPolicy(Policy);
885 SubPolicy.SuppressSpecifiers = false;
886 SubPolicy.IncludeTagDefinition = false;
887 Init->printPretty(Out, nullptr, SubPolicy, Indentation);
888 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
892 prettyPrintAttributes(D);
895 void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
899 void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
901 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
905 void DeclPrinter::VisitImportDecl(ImportDecl *D) {
906 Out << "@import " << D->getImportedModule()->getFullModuleName()
910 void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
911 Out << "static_assert(";
912 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
913 if (StringLiteral *SL = D->getMessage()) {
915 SL->printPretty(Out, nullptr, Policy, Indentation);
920 //----------------------------------------------------------------------------
922 //----------------------------------------------------------------------------
923 void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
926 Out << "namespace " << *D << " {\n";
931 void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
932 Out << "using namespace ";
933 if (D->getQualifier())
934 D->getQualifier()->print(Out, Policy);
935 Out << *D->getNominatedNamespaceAsWritten();
938 void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
939 Out << "namespace " << *D << " = ";
940 if (D->getQualifier())
941 D->getQualifier()->print(Out, Policy);
942 Out << *D->getAliasedNamespace();
945 void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
946 prettyPrintAttributes(D);
949 void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
950 // FIXME: add printing of pragma attributes if required.
951 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
952 Out << "__module_private__ ";
953 Out << D->getKindName();
955 prettyPrintAttributes(D);
957 if (D->getIdentifier()) {
960 if (auto S = dyn_cast<ClassTemplatePartialSpecializationDecl>(D))
961 printTemplateArguments(S->getTemplateArgs(), S->getTemplateParameters());
962 else if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D))
963 printTemplateArguments(S->getTemplateArgs());
966 if (D->isCompleteDefinition()) {
967 // Print the base classes
968 if (D->getNumBases()) {
970 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
971 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
972 if (Base != D->bases_begin())
975 if (Base->isVirtual())
978 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
983 Out << Base->getType().getAsString(Policy);
985 if (Base->isPackExpansion())
990 // Print the class definition
991 // FIXME: Doesn't print access specifiers, e.g., "public:"
992 if (Policy.TerseOutput) {
1002 void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1004 if (D->getLanguage() == LinkageSpecDecl::lang_c)
1007 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
1008 "unknown language in linkage specification");
1012 Out << "extern \"" << l << "\" ";
1013 if (D->hasBraces()) {
1015 VisitDeclContext(D);
1018 Visit(*D->decls_begin());
1021 void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params,
1022 bool OmitTemplateKW) {
1025 if (!OmitTemplateKW)
1029 bool NeedComma = false;
1030 for (const Decl *Param : *Params) {
1031 if (Param->isImplicit())
1039 if (auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
1041 if (TTP->wasDeclaredWithTypename())
1046 if (TTP->isParameterPack())
1048 else if (!TTP->getName().empty())
1053 if (TTP->hasDefaultArgument()) {
1055 Out << TTP->getDefaultArgument().getAsString(Policy);
1057 } else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1059 if (IdentifierInfo *II = NTTP->getIdentifier())
1060 Name = II->getName();
1061 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
1063 if (NTTP->hasDefaultArgument()) {
1065 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
1068 } else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
1069 VisitTemplateDecl(TTPD);
1070 // FIXME: print the default argument, if present.
1075 if (!OmitTemplateKW)
1079 void DeclPrinter::printTemplateArguments(const TemplateArgumentList &Args,
1080 const TemplateParameterList *Params) {
1082 for (size_t I = 0, E = Args.size(); I < E; ++I) {
1083 const TemplateArgument &A = Args[I];
1087 if (A.getKind() == TemplateArgument::Type)
1088 if (auto T = A.getAsType()->getAs<TemplateTypeParmType>()) {
1089 auto P = cast<TemplateTypeParmDecl>(Params->getParam(T->getIndex()));
1093 if (A.getKind() == TemplateArgument::Template) {
1094 if (auto T = A.getAsTemplate().getAsTemplateDecl())
1095 if (auto TD = dyn_cast<TemplateTemplateParmDecl>(T)) {
1096 auto P = cast<TemplateTemplateParmDecl>(
1097 Params->getParam(TD->getIndex()));
1102 if (A.getKind() == TemplateArgument::Expression) {
1103 if (auto E = dyn_cast<DeclRefExpr>(A.getAsExpr()))
1104 if (auto N = dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1105 auto P = cast<NonTypeTemplateParmDecl>(
1106 Params->getParam(N->getIndex()));
1112 A.print(Policy, Out);
1117 void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
1118 printTemplateParameters(D->getTemplateParameters());
1120 if (const TemplateTemplateParmDecl *TTP =
1121 dyn_cast<TemplateTemplateParmDecl>(D)) {
1123 if (TTP->isParameterPack())
1125 Out << D->getName();
1126 } else if (auto *TD = D->getTemplatedDecl())
1128 else if (const auto *Concept = dyn_cast<ConceptDecl>(D)) {
1129 Out << "concept " << Concept->getName() << " = " ;
1130 Concept->getConstraintExpr()->printPretty(Out, nullptr, Policy,
1136 void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
1137 prettyPrintPragmas(D->getTemplatedDecl());
1138 // Print any leading template parameter lists.
1139 if (const FunctionDecl *FD = D->getTemplatedDecl()) {
1140 for (unsigned I = 0, NumTemplateParams = FD->getNumTemplateParameterLists();
1141 I < NumTemplateParams; ++I)
1142 printTemplateParameters(FD->getTemplateParameterList(I));
1144 VisitRedeclarableTemplateDecl(D);
1145 // Declare target attribute is special one, natural spelling for the pragma
1146 // assumes "ending" construct so print it here.
1147 if (D->getTemplatedDecl()->hasAttr<OMPDeclareTargetDeclAttr>())
1148 Out << "#pragma omp end declare target\n";
1150 // Never print "instantiations" for deduction guides (they don't really
1152 if (PrintInstantiation &&
1153 !isa<CXXDeductionGuideDecl>(D->getTemplatedDecl())) {
1154 FunctionDecl *PrevDecl = D->getTemplatedDecl();
1155 const FunctionDecl *Def;
1156 if (PrevDecl->isDefined(Def) && Def != PrevDecl)
1158 for (auto *I : D->specializations())
1159 if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) {
1160 if (!PrevDecl->isThisDeclarationADefinition())
1163 prettyPrintPragmas(I);
1169 void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
1170 VisitRedeclarableTemplateDecl(D);
1172 if (PrintInstantiation) {
1173 for (auto *I : D->specializations())
1174 if (I->getSpecializationKind() == TSK_ImplicitInstantiation) {
1175 if (D->isThisDeclarationADefinition())
1183 void DeclPrinter::VisitClassTemplateSpecializationDecl(
1184 ClassTemplateSpecializationDecl *D) {
1185 Out << "template<> ";
1186 VisitCXXRecordDecl(D);
1189 void DeclPrinter::VisitClassTemplatePartialSpecializationDecl(
1190 ClassTemplatePartialSpecializationDecl *D) {
1191 printTemplateParameters(D->getTemplateParameters());
1192 VisitCXXRecordDecl(D);
1195 //----------------------------------------------------------------------------
1196 // Objective-C declarations
1197 //----------------------------------------------------------------------------
1199 void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
1200 Decl::ObjCDeclQualifier Quals,
1203 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
1205 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
1207 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1209 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1211 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1213 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1215 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
1216 if (auto nullability = AttributedType::stripOuterNullability(T))
1217 Out << getNullabilitySpelling(*nullability, true) << ' ';
1220 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1224 void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1226 unsigned First = true;
1227 for (auto *Param : *Params) {
1234 switch (Param->getVariance()) {
1235 case ObjCTypeParamVariance::Invariant:
1238 case ObjCTypeParamVariance::Covariant:
1239 Out << "__covariant ";
1242 case ObjCTypeParamVariance::Contravariant:
1243 Out << "__contravariant ";
1247 Out << Param->getDeclName().getAsString();
1249 if (Param->hasExplicitBound()) {
1250 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1256 void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1257 if (OMD->isInstanceMethod())
1261 if (!OMD->getReturnType().isNull()) {
1262 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1263 OMD->getReturnType());
1266 std::string name = OMD->getSelector().getAsString();
1267 std::string::size_type pos, lastPos = 0;
1268 for (const auto *PI : OMD->parameters()) {
1269 // FIXME: selector is missing here!
1270 pos = name.find_first_of(':', lastPos);
1273 Out << name.substr(lastPos, pos - lastPos) << ':';
1274 PrintObjCMethodType(OMD->getASTContext(),
1275 PI->getObjCDeclQualifier(),
1281 if (OMD->param_begin() == OMD->param_end())
1284 if (OMD->isVariadic())
1287 prettyPrintAttributes(OMD);
1289 if (OMD->getBody() && !Policy.TerseOutput) {
1291 OMD->getBody()->printPretty(Out, nullptr, Policy);
1293 else if (Policy.PolishForDeclaration)
1297 void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1298 std::string I = OID->getNameAsString();
1299 ObjCInterfaceDecl *SID = OID->getSuperClass();
1301 bool eolnOut = false;
1303 Out << "@implementation " << I << " : " << *SID;
1305 Out << "@implementation " << I;
1307 if (OID->ivar_size() > 0) {
1310 Indentation += Policy.Indentation;
1311 for (const auto *I : OID->ivars()) {
1312 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
1313 getAsString(Policy) << ' ' << *I << ";\n";
1315 Indentation -= Policy.Indentation;
1318 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1322 VisitDeclContext(OID, false);
1328 void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1329 std::string I = OID->getNameAsString();
1330 ObjCInterfaceDecl *SID = OID->getSuperClass();
1332 if (!OID->isThisDeclarationADefinition()) {
1333 Out << "@class " << I;
1335 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1336 PrintObjCTypeParams(TypeParams);
1342 bool eolnOut = false;
1343 Out << "@interface " << I;
1345 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1346 PrintObjCTypeParams(TypeParams);
1350 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
1353 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1354 if (!Protocols.empty()) {
1355 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1356 E = Protocols.end(); I != E; ++I)
1357 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1361 if (OID->ivar_size() > 0) {
1364 Indentation += Policy.Indentation;
1365 for (const auto *I : OID->ivars()) {
1366 Indent() << I->getASTContext()
1367 .getUnqualifiedObjCPointerType(I->getType())
1368 .getAsString(Policy) << ' ' << *I << ";\n";
1370 Indentation -= Policy.Indentation;
1373 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1378 VisitDeclContext(OID, false);
1382 // FIXME: implement the rest...
1385 void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
1386 if (!PID->isThisDeclarationADefinition()) {
1387 Out << "@protocol " << *PID << ";\n";
1391 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1392 if (!Protocols.empty()) {
1393 Out << "@protocol " << *PID;
1394 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1395 E = Protocols.end(); I != E; ++I)
1396 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1399 Out << "@protocol " << *PID << '\n';
1400 VisitDeclContext(PID, false);
1404 void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
1405 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
1407 VisitDeclContext(PID, false);
1409 // FIXME: implement the rest...
1412 void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
1413 Out << "@interface " << *PID->getClassInterface();
1414 if (auto TypeParams = PID->getTypeParamList()) {
1415 PrintObjCTypeParams(TypeParams);
1417 Out << "(" << *PID << ")\n";
1418 if (PID->ivar_size() > 0) {
1420 Indentation += Policy.Indentation;
1421 for (const auto *I : PID->ivars())
1422 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
1423 getAsString(Policy) << ' ' << *I << ";\n";
1424 Indentation -= Policy.Indentation;
1428 VisitDeclContext(PID, false);
1431 // FIXME: implement the rest...
1434 void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
1435 Out << "@compatibility_alias " << *AID
1436 << ' ' << *AID->getClassInterface() << ";\n";
1439 /// PrintObjCPropertyDecl - print a property declaration.
1441 /// Print attributes in the following order:
1443 /// - nonatomic | atomic
1444 /// - assign | retain | strong | copy | weak | unsafe_unretained
1445 /// - readwrite | readonly
1446 /// - getter & setter
1448 void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1449 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1450 Out << "@required\n";
1451 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1452 Out << "@optional\n";
1454 QualType T = PDecl->getType();
1457 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1460 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) {
1461 Out << (first ? "" : ", ") << "class";
1465 if (PDecl->getPropertyAttributes() &
1466 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1467 Out << (first ? "" : ", ") << "nonatomic";
1470 if (PDecl->getPropertyAttributes() &
1471 ObjCPropertyDecl::OBJC_PR_atomic) {
1472 Out << (first ? "" : ", ") << "atomic";
1476 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1477 Out << (first ? "" : ", ") << "assign";
1480 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1481 Out << (first ? "" : ", ") << "retain";
1485 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1486 Out << (first ? "" : ", ") << "strong";
1489 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1490 Out << (first ? "" : ", ") << "copy";
1493 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak) {
1494 Out << (first ? "" : ", ") << "weak";
1497 if (PDecl->getPropertyAttributes()
1498 & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
1499 Out << (first ? "" : ", ") << "unsafe_unretained";
1503 if (PDecl->getPropertyAttributes() &
1504 ObjCPropertyDecl::OBJC_PR_readwrite) {
1505 Out << (first ? "" : ", ") << "readwrite";
1508 if (PDecl->getPropertyAttributes() &
1509 ObjCPropertyDecl::OBJC_PR_readonly) {
1510 Out << (first ? "" : ", ") << "readonly";
1514 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1515 Out << (first ? "" : ", ") << "getter = ";
1516 PDecl->getGetterName().print(Out);
1519 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1520 Out << (first ? "" : ", ") << "setter = ";
1521 PDecl->getSetterName().print(Out);
1525 if (PDecl->getPropertyAttributes() &
1526 ObjCPropertyDecl::OBJC_PR_nullability) {
1527 if (auto nullability = AttributedType::stripOuterNullability(T)) {
1528 if (*nullability == NullabilityKind::Unspecified &&
1529 (PDecl->getPropertyAttributes() &
1530 ObjCPropertyDecl::OBJC_PR_null_resettable)) {
1531 Out << (first ? "" : ", ") << "null_resettable";
1533 Out << (first ? "" : ", ")
1534 << getNullabilitySpelling(*nullability, true);
1540 (void) first; // Silence dead store warning due to idiomatic code.
1543 std::string TypeStr = PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
1544 getAsString(Policy);
1545 Out << ' ' << TypeStr;
1546 if (!StringRef(TypeStr).endswith("*"))
1549 if (Policy.PolishForDeclaration)
1553 void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1554 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1555 Out << "@synthesize ";
1558 Out << *PID->getPropertyDecl();
1559 if (PID->getPropertyIvarDecl())
1560 Out << '=' << *PID->getPropertyIvarDecl();
1563 void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1564 if (!D->isAccessDeclaration())
1566 if (D->hasTypename())
1568 D->getQualifier()->print(Out, Policy);
1570 // Use the correct record name when the using declaration is used for
1571 // inheriting constructors.
1572 for (const auto *Shadow : D->shadows()) {
1573 if (const auto *ConstructorShadow =
1574 dyn_cast<ConstructorUsingShadowDecl>(Shadow)) {
1575 assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext());
1576 Out << *ConstructorShadow->getNominatedBaseClass();
1584 DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1585 Out << "using typename ";
1586 D->getQualifier()->print(Out, Policy);
1587 Out << D->getDeclName();
1590 void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1591 if (!D->isAccessDeclaration())
1593 D->getQualifier()->print(Out, Policy);
1594 Out << D->getDeclName();
1597 void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1601 void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1602 Out << "#pragma omp threadprivate";
1603 if (!D->varlist_empty()) {
1604 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1605 E = D->varlist_end();
1607 Out << (I == D->varlist_begin() ? '(' : ',');
1608 NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl();
1609 ND->printQualifiedName(Out);
1615 void DeclPrinter::VisitOMPAllocateDecl(OMPAllocateDecl *D) {
1616 Out << "#pragma omp allocate";
1617 if (!D->varlist_empty()) {
1618 for (OMPAllocateDecl::varlist_iterator I = D->varlist_begin(),
1619 E = D->varlist_end();
1621 Out << (I == D->varlist_begin() ? '(' : ',');
1622 NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl();
1623 ND->printQualifiedName(Out);
1627 if (!D->clauselist_empty()) {
1629 OMPClausePrinter Printer(Out, Policy);
1630 for (OMPClause *C : D->clauselists())
1635 void DeclPrinter::VisitOMPRequiresDecl(OMPRequiresDecl *D) {
1636 Out << "#pragma omp requires ";
1637 if (!D->clauselist_empty()) {
1638 OMPClausePrinter Printer(Out, Policy);
1639 for (auto I = D->clauselist_begin(), E = D->clauselist_end(); I != E; ++I)
1644 void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1645 if (!D->isInvalidDecl()) {
1646 Out << "#pragma omp declare reduction (";
1647 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
1648 const char *OpName =
1649 getOperatorSpelling(D->getDeclName().getCXXOverloadedOperator());
1650 assert(OpName && "not an overloaded operator");
1653 assert(D->getDeclName().isIdentifier());
1657 D->getType().print(Out, Policy);
1659 D->getCombiner()->printPretty(Out, nullptr, Policy, 0);
1661 if (auto *Init = D->getInitializer()) {
1662 Out << " initializer(";
1663 switch (D->getInitializerKind()) {
1664 case OMPDeclareReductionDecl::DirectInit:
1667 case OMPDeclareReductionDecl::CopyInit:
1668 Out << "omp_priv = ";
1670 case OMPDeclareReductionDecl::CallInit:
1673 Init->printPretty(Out, nullptr, Policy, 0);
1674 if (D->getInitializerKind() == OMPDeclareReductionDecl::DirectInit)
1681 void DeclPrinter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
1682 if (!D->isInvalidDecl()) {
1683 Out << "#pragma omp declare mapper (";
1686 D->getType().print(Out, Policy);
1688 Out << D->getVarName();
1690 if (!D->clauselist_empty()) {
1691 OMPClausePrinter Printer(Out, Policy);
1692 for (auto *C : D->clauselists()) {
1700 void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
1701 D->getInit()->printPretty(Out, nullptr, Policy, Indentation);