1 //===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
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 file implements the Decl::print method, which pretty prints the
11 // AST back out to C/Objective-C/C++/Objective-C++ code.
13 //===----------------------------------------------------------------------===//
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/Attr.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/DeclObjC.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 VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
104 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
106 void printTemplateParameters(const TemplateParameterList *Params);
107 void printTemplateArguments(const TemplateArgumentList &Args,
108 const TemplateParameterList *Params = nullptr);
109 void prettyPrintAttributes(Decl *D);
110 void prettyPrintPragmas(Decl *D);
111 void printDeclType(QualType T, StringRef DeclName, bool Pack = false);
115 void Decl::print(raw_ostream &Out, unsigned Indentation,
116 bool PrintInstantiation) const {
117 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
120 void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
121 unsigned Indentation, bool PrintInstantiation) const {
122 DeclPrinter Printer(Out, Policy, getASTContext(), Indentation,
124 Printer.Visit(const_cast<Decl*>(this));
127 static QualType GetBaseType(QualType T) {
128 // FIXME: This should be on the Type class!
129 QualType BaseType = T;
130 while (!BaseType->isSpecifierType()) {
131 if (const PointerType *PTy = BaseType->getAs<PointerType>())
132 BaseType = PTy->getPointeeType();
133 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
134 BaseType = BPy->getPointeeType();
135 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
136 BaseType = ATy->getElementType();
137 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
138 BaseType = FTy->getReturnType();
139 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
140 BaseType = VTy->getElementType();
141 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
142 BaseType = RTy->getPointeeType();
143 else if (const AutoType *ATy = BaseType->getAs<AutoType>())
144 BaseType = ATy->getDeducedType();
145 else if (const ParenType *PTy = BaseType->getAs<ParenType>())
146 BaseType = PTy->desugar();
148 // This must be a syntax error.
154 static QualType getDeclType(Decl* D) {
155 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
156 return TDD->getUnderlyingType();
157 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
158 return VD->getType();
162 void Decl::printGroup(Decl** Begin, unsigned NumDecls,
163 raw_ostream &Out, const PrintingPolicy &Policy,
164 unsigned Indentation) {
166 (*Begin)->print(Out, Policy, Indentation);
170 Decl** End = Begin + NumDecls;
171 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
175 PrintingPolicy SubPolicy(Policy);
178 for ( ; Begin != End; ++Begin) {
181 SubPolicy.IncludeTagDefinition = true;
182 SubPolicy.SuppressSpecifiers = false;
185 if (!isFirst) Out << ", ";
186 SubPolicy.IncludeTagDefinition = false;
187 SubPolicy.SuppressSpecifiers = true;
190 (*Begin)->print(Out, SubPolicy, Indentation);
194 LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
195 // Get the translation unit
196 const DeclContext *DC = this;
197 while (!DC->isTranslationUnit())
198 DC = DC->getParent();
200 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
201 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), Ctx, 0);
202 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
205 raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
206 for (unsigned i = 0; i != Indentation; ++i)
211 void DeclPrinter::prettyPrintAttributes(Decl *D) {
212 if (Policy.PolishForDeclaration)
216 AttrVec &Attrs = D->getAttrs();
217 for (auto *A : Attrs) {
218 if (A->isInherited() || A->isImplicit())
220 switch (A->getKind()) {
222 #define PRAGMA_SPELLING_ATTR(X) case attr::X:
223 #include "clang/Basic/AttrList.inc"
226 A->printPretty(Out, Policy);
233 void DeclPrinter::prettyPrintPragmas(Decl *D) {
234 if (Policy.PolishForDeclaration)
238 AttrVec &Attrs = D->getAttrs();
239 for (auto *A : Attrs) {
240 switch (A->getKind()) {
242 #define PRAGMA_SPELLING_ATTR(X) case attr::X:
243 #include "clang/Basic/AttrList.inc"
244 A->printPretty(Out, Policy);
254 void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) {
255 // Normally, a PackExpansionType is written as T[3]... (for instance, as a
256 // template argument), but if it is the type of a declaration, the ellipsis
257 // is placed before the name being declared.
258 if (auto *PET = T->getAs<PackExpansionType>()) {
260 T = PET->getPattern();
262 T.print(Out, Policy, (Pack ? "..." : "") + DeclName, Indentation);
265 void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
267 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
273 void DeclPrinter::Print(AccessSpecifier AS) {
275 case AS_none: llvm_unreachable("No access specifier!");
276 case AS_public: Out << "public"; break;
277 case AS_protected: Out << "protected"; break;
278 case AS_private: Out << "private"; break;
282 void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl,
283 std::string &Proto) {
284 bool HasInitializerList = false;
285 for (const auto *BMInitializer : CDecl->inits()) {
286 if (BMInitializer->isInClassMemberInitializer())
289 if (!HasInitializerList) {
293 HasInitializerList = true;
297 if (BMInitializer->isAnyMemberInitializer()) {
298 FieldDecl *FD = BMInitializer->getAnyMember();
301 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
305 if (!BMInitializer->getInit()) {
308 Expr *Init = BMInitializer->getInit();
309 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
310 Init = Tmp->getSubExpr();
312 Init = Init->IgnoreParens();
314 Expr *SimpleInit = nullptr;
315 Expr **Args = nullptr;
316 unsigned NumArgs = 0;
317 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
318 Args = ParenList->getExprs();
319 NumArgs = ParenList->getNumExprs();
320 } else if (CXXConstructExpr *Construct =
321 dyn_cast<CXXConstructExpr>(Init)) {
322 Args = Construct->getArgs();
323 NumArgs = Construct->getNumArgs();
328 SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
330 for (unsigned I = 0; I != NumArgs; ++I) {
331 assert(Args[I] != nullptr && "Expected non-null Expr");
332 if (isa<CXXDefaultArgExpr>(Args[I]))
337 Args[I]->printPretty(Out, nullptr, Policy, Indentation);
342 if (BMInitializer->isPackExpansion())
347 //----------------------------------------------------------------------------
348 // Common C declarations
349 //----------------------------------------------------------------------------
351 void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
352 if (Policy.TerseOutput)
356 Indentation += Policy.Indentation;
358 SmallVector<Decl*, 2> Decls;
359 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
362 // Don't print ObjCIvarDecls, as they are printed when visiting the
363 // containing ObjCInterfaceDecl.
364 if (isa<ObjCIvarDecl>(*D))
367 // Skip over implicit declarations in pretty-printing mode.
371 // Don't print implicit specializations, as they are printed when visiting
372 // corresponding templates.
373 if (auto FD = dyn_cast<FunctionDecl>(*D))
374 if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation &&
375 !isa<ClassTemplateSpecializationDecl>(DC))
378 // The next bits of code handle stuff like "struct {int x;} a,b"; we're
379 // forced to merge the declarations because there's no other way to
380 // refer to the struct in question. When that struct is named instead, we
381 // also need to merge to avoid splitting off a stand-alone struct
382 // declaration that produces the warning ext_no_declarators in some
385 // This limited merging is safe without a bunch of other checks because it
386 // only merges declarations directly referring to the tag, not typedefs.
388 // Check whether the current declaration should be grouped with a previous
389 // non-free-standing tag declaration.
390 QualType CurDeclType = getDeclType(*D);
391 if (!Decls.empty() && !CurDeclType.isNull()) {
392 QualType BaseType = GetBaseType(CurDeclType);
393 if (!BaseType.isNull() && isa<ElaboratedType>(BaseType) &&
394 cast<ElaboratedType>(BaseType)->getOwnedTagDecl() == Decls[0]) {
400 // If we have a merged group waiting to be handled, handle it now.
402 ProcessDeclGroup(Decls);
404 // If the current declaration is not a free standing declaration, save it
405 // so we can merge it with the subsequent declaration(s) using it.
406 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->isFreeStanding()) {
411 if (isa<AccessSpecDecl>(*D)) {
412 Indentation -= Policy.Indentation;
414 Print(D->getAccess());
416 Indentation += Policy.Indentation;
423 // FIXME: Need to be able to tell the DeclPrinter when
424 const char *Terminator = nullptr;
425 if (isa<OMPThreadPrivateDecl>(*D) || isa<OMPDeclareReductionDecl>(*D))
426 Terminator = nullptr;
427 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->hasBody())
428 Terminator = nullptr;
429 else if (auto FD = dyn_cast<FunctionDecl>(*D)) {
430 if (FD->isThisDeclarationADefinition())
431 Terminator = nullptr;
434 } else if (auto TD = dyn_cast<FunctionTemplateDecl>(*D)) {
435 if (TD->getTemplatedDecl()->isThisDeclarationADefinition())
436 Terminator = nullptr;
439 } else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
440 isa<ObjCImplementationDecl>(*D) ||
441 isa<ObjCInterfaceDecl>(*D) ||
442 isa<ObjCProtocolDecl>(*D) ||
443 isa<ObjCCategoryImplDecl>(*D) ||
444 isa<ObjCCategoryDecl>(*D))
445 Terminator = nullptr;
446 else if (isa<EnumConstantDecl>(*D)) {
447 DeclContext::decl_iterator Next = D;
456 if (!Policy.TerseOutput &&
457 ((isa<FunctionDecl>(*D) &&
458 cast<FunctionDecl>(*D)->doesThisDeclarationHaveABody()) ||
459 (isa<FunctionTemplateDecl>(*D) &&
460 cast<FunctionTemplateDecl>(*D)->getTemplatedDecl()->doesThisDeclarationHaveABody())))
461 ; // StmtPrinter already added '\n' after CompoundStmt.
465 // Declare target attribute is special one, natural spelling for the pragma
466 // assumes "ending" construct so print it here.
467 if (D->hasAttr<OMPDeclareTargetDeclAttr>())
468 Out << "#pragma omp end declare target\n";
472 ProcessDeclGroup(Decls);
475 Indentation -= Policy.Indentation;
478 void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
479 VisitDeclContext(D, false);
482 void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
483 if (!Policy.SuppressSpecifiers) {
486 if (D->isModulePrivate())
487 Out << "__module_private__ ";
489 QualType Ty = D->getTypeSourceInfo()->getType();
490 Ty.print(Out, Policy, D->getName(), Indentation);
491 prettyPrintAttributes(D);
494 void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
495 Out << "using " << *D;
496 prettyPrintAttributes(D);
497 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
500 void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
501 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
502 Out << "__module_private__ ";
505 if (D->isScopedUsingClassTag())
511 prettyPrintAttributes(D);
515 if (D->isFixed() && D->getASTContext().getLangOpts().CPlusPlus11)
516 Out << " : " << D->getIntegerType().stream(Policy);
518 if (D->isCompleteDefinition()) {
525 void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
526 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
527 Out << "__module_private__ ";
528 Out << D->getKindName();
530 prettyPrintAttributes(D);
532 if (D->getIdentifier())
535 if (D->isCompleteDefinition()) {
542 void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
544 prettyPrintAttributes(D);
545 if (Expr *Init = D->getInitExpr()) {
547 Init->printPretty(Out, nullptr, Policy, Indentation, &Context);
551 void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
552 if (!D->getDescribedFunctionTemplate() &&
553 !D->isFunctionTemplateSpecialization())
554 prettyPrintPragmas(D);
556 if (D->isFunctionTemplateSpecialization())
557 Out << "template<> ";
558 else if (!D->getDescribedFunctionTemplate()) {
559 for (unsigned I = 0, NumTemplateParams = D->getNumTemplateParameterLists();
560 I < NumTemplateParams; ++I)
561 printTemplateParameters(D->getTemplateParameterList(I));
564 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
565 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
566 CXXDeductionGuideDecl *GuideDecl = dyn_cast<CXXDeductionGuideDecl>(D);
567 if (!Policy.SuppressSpecifiers) {
568 switch (D->getStorageClass()) {
570 case SC_Extern: Out << "extern "; break;
571 case SC_Static: Out << "static "; break;
572 case SC_PrivateExtern: Out << "__private_extern__ "; break;
573 case SC_Auto: case SC_Register:
574 llvm_unreachable("invalid for functions");
577 if (D->isInlineSpecified()) Out << "inline ";
578 if (D->isVirtualAsWritten()) Out << "virtual ";
579 if (D->isModulePrivate()) Out << "__module_private__ ";
580 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
581 if ((CDecl && CDecl->isExplicitSpecified()) ||
582 (ConversionDecl && ConversionDecl->isExplicitSpecified()) ||
583 (GuideDecl && GuideDecl->isExplicitSpecified()))
587 PrintingPolicy SubPolicy(Policy);
588 SubPolicy.SuppressSpecifiers = false;
591 if (Policy.FullyQualifiedName) {
592 Proto += D->getQualifiedNameAsString();
594 if (!Policy.SuppressScope) {
595 if (const NestedNameSpecifier *NS = D->getQualifier()) {
596 llvm::raw_string_ostream OS(Proto);
597 NS->print(OS, Policy);
600 Proto += D->getNameInfo().getAsString();
604 Proto = GuideDecl->getDeducedTemplate()->getDeclName().getAsString();
605 if (const TemplateArgumentList *TArgs = D->getTemplateSpecializationArgs()) {
606 llvm::raw_string_ostream POut(Proto);
607 DeclPrinter TArgPrinter(POut, SubPolicy, Context, Indentation);
608 TArgPrinter.printTemplateArguments(*TArgs);
611 QualType Ty = D->getType();
612 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
613 Proto = '(' + Proto + ')';
614 Ty = PT->getInnerType();
617 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
618 const FunctionProtoType *FT = nullptr;
619 if (D->hasWrittenPrototype())
620 FT = dyn_cast<FunctionProtoType>(AFT);
624 llvm::raw_string_ostream POut(Proto);
625 DeclPrinter ParamPrinter(POut, SubPolicy, Context, Indentation);
626 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
628 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
631 if (FT->isVariadic()) {
632 if (D->getNumParams()) POut << ", ";
635 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
636 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
639 Proto += D->getParamDecl(i)->getNameAsString();
648 if (FT->isVolatile())
649 Proto += " volatile";
650 if (FT->isRestrict())
651 Proto += " restrict";
653 switch (FT->getRefQualifier()) {
665 if (FT && FT->hasDynamicExceptionSpec()) {
667 if (FT->getExceptionSpecType() == EST_MSAny)
670 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
674 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
677 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
678 Proto += " noexcept";
679 if (isComputedNoexcept(FT->getExceptionSpecType())) {
681 llvm::raw_string_ostream EOut(Proto);
682 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
691 if (!Policy.TerseOutput)
692 PrintConstructorInitializers(CDecl, Proto);
693 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
694 if (FT && FT->hasTrailingReturn()) {
697 Out << Proto << " -> ";
700 AFT->getReturnType().print(Out, Policy, Proto);
705 Ty.print(Out, Policy, Proto);
708 prettyPrintAttributes(D);
712 else if (D->isDeletedAsWritten())
714 else if (D->isExplicitlyDefaulted())
716 else if (D->doesThisDeclarationHaveABody()) {
717 if (!Policy.TerseOutput) {
718 if (!D->hasPrototype() && D->getNumParams()) {
719 // This is a K&R function definition, so we need to print the
722 DeclPrinter ParamPrinter(Out, SubPolicy, Context, Indentation);
723 Indentation += Policy.Indentation;
724 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
726 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
729 Indentation -= Policy.Indentation;
734 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
736 if (!Policy.TerseOutput && isa<CXXConstructorDecl>(*D))
742 void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
743 if (TypeSourceInfo *TSI = D->getFriendType()) {
744 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
745 for (unsigned i = 0; i < NumTPLists; ++i)
746 printTemplateParameters(D->getFriendTypeTemplateParameterList(i));
748 Out << " " << TSI->getType().getAsString(Policy);
750 else if (FunctionDecl *FD =
751 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
753 VisitFunctionDecl(FD);
755 else if (FunctionTemplateDecl *FTD =
756 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
758 VisitFunctionTemplateDecl(FTD);
760 else if (ClassTemplateDecl *CTD =
761 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
763 VisitRedeclarableTemplateDecl(CTD);
767 void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
768 // FIXME: add printing of pragma attributes if required.
769 if (!Policy.SuppressSpecifiers && D->isMutable())
771 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
772 Out << "__module_private__ ";
774 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
775 stream(Policy, D->getName(), Indentation);
777 if (D->isBitField()) {
779 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
782 Expr *Init = D->getInClassInitializer();
783 if (!Policy.SuppressInitializers && Init) {
784 if (D->getInClassInitStyle() == ICIS_ListInit)
788 Init->printPretty(Out, nullptr, Policy, Indentation);
790 prettyPrintAttributes(D);
793 void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
797 void DeclPrinter::VisitVarDecl(VarDecl *D) {
798 prettyPrintPragmas(D);
800 QualType T = D->getTypeSourceInfo()
801 ? D->getTypeSourceInfo()->getType()
802 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
804 if (!Policy.SuppressSpecifiers) {
805 StorageClass SC = D->getStorageClass();
807 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
809 switch (D->getTSCSpec()) {
810 case TSCS_unspecified:
815 case TSCS__Thread_local:
816 Out << "_Thread_local ";
818 case TSCS_thread_local:
819 Out << "thread_local ";
823 if (D->isModulePrivate())
824 Out << "__module_private__ ";
826 if (D->isConstexpr()) {
828 T.removeLocalConst();
832 printDeclType(T, D->getName());
833 Expr *Init = D->getInit();
834 if (!Policy.SuppressInitializers && Init) {
835 bool ImplicitInit = false;
836 if (CXXConstructExpr *Construct =
837 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
838 if (D->getInitStyle() == VarDecl::CallInit &&
839 !Construct->isListInitialization()) {
840 ImplicitInit = Construct->getNumArgs() == 0 ||
841 Construct->getArg(0)->isDefaultArgument();
845 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
847 else if (D->getInitStyle() == VarDecl::CInit) {
850 PrintingPolicy SubPolicy(Policy);
851 SubPolicy.SuppressSpecifiers = false;
852 SubPolicy.IncludeTagDefinition = false;
853 Init->printPretty(Out, nullptr, SubPolicy, Indentation);
854 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
858 prettyPrintAttributes(D);
861 void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
865 void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
867 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
871 void DeclPrinter::VisitImportDecl(ImportDecl *D) {
872 Out << "@import " << D->getImportedModule()->getFullModuleName()
876 void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
877 Out << "static_assert(";
878 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
879 if (StringLiteral *SL = D->getMessage()) {
881 SL->printPretty(Out, nullptr, Policy, Indentation);
886 //----------------------------------------------------------------------------
888 //----------------------------------------------------------------------------
889 void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
892 Out << "namespace " << *D << " {\n";
897 void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
898 Out << "using namespace ";
899 if (D->getQualifier())
900 D->getQualifier()->print(Out, Policy);
901 Out << *D->getNominatedNamespaceAsWritten();
904 void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
905 Out << "namespace " << *D << " = ";
906 if (D->getQualifier())
907 D->getQualifier()->print(Out, Policy);
908 Out << *D->getAliasedNamespace();
911 void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
912 prettyPrintAttributes(D);
915 void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
916 // FIXME: add printing of pragma attributes if required.
917 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
918 Out << "__module_private__ ";
919 Out << D->getKindName();
921 prettyPrintAttributes(D);
923 if (D->getIdentifier()) {
926 if (auto S = dyn_cast<ClassTemplatePartialSpecializationDecl>(D))
927 printTemplateArguments(S->getTemplateArgs(), S->getTemplateParameters());
928 else if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D))
929 printTemplateArguments(S->getTemplateArgs());
932 if (D->isCompleteDefinition()) {
933 // Print the base classes
934 if (D->getNumBases()) {
936 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
937 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
938 if (Base != D->bases_begin())
941 if (Base->isVirtual())
944 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
949 Out << Base->getType().getAsString(Policy);
951 if (Base->isPackExpansion())
956 // Print the class definition
957 // FIXME: Doesn't print access specifiers, e.g., "public:"
958 if (Policy.TerseOutput) {
968 void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
970 if (D->getLanguage() == LinkageSpecDecl::lang_c)
973 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
974 "unknown language in linkage specification");
978 Out << "extern \"" << l << "\" ";
979 if (D->hasBraces()) {
984 Visit(*D->decls_begin());
987 void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params) {
992 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
996 const Decl *Param = Params->getParam(i);
997 if (auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
999 if (TTP->wasDeclaredWithTypename())
1004 if (TTP->isParameterPack())
1009 if (TTP->hasDefaultArgument()) {
1011 Out << TTP->getDefaultArgument().getAsString(Policy);
1013 } else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1015 if (IdentifierInfo *II = NTTP->getIdentifier())
1016 Name = II->getName();
1017 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
1019 if (NTTP->hasDefaultArgument()) {
1021 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
1024 } else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
1025 VisitTemplateDecl(TTPD);
1026 // FIXME: print the default argument, if present.
1033 void DeclPrinter::printTemplateArguments(const TemplateArgumentList &Args,
1034 const TemplateParameterList *Params) {
1036 for (size_t I = 0, E = Args.size(); I < E; ++I) {
1037 const TemplateArgument &A = Args[I];
1041 if (A.getKind() == TemplateArgument::Type)
1042 if (auto T = A.getAsType()->getAs<TemplateTypeParmType>()) {
1043 auto P = cast<TemplateTypeParmDecl>(Params->getParam(T->getIndex()));
1047 if (A.getKind() == TemplateArgument::Template) {
1048 if (auto T = A.getAsTemplate().getAsTemplateDecl())
1049 if (auto TD = dyn_cast<TemplateTemplateParmDecl>(T)) {
1050 auto P = cast<TemplateTemplateParmDecl>(
1051 Params->getParam(TD->getIndex()));
1056 if (A.getKind() == TemplateArgument::Expression) {
1057 if (auto E = dyn_cast<DeclRefExpr>(A.getAsExpr()))
1058 if (auto N = dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1059 auto P = cast<NonTypeTemplateParmDecl>(
1060 Params->getParam(N->getIndex()));
1066 A.print(Policy, Out);
1071 void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
1072 printTemplateParameters(D->getTemplateParameters());
1074 if (const TemplateTemplateParmDecl *TTP =
1075 dyn_cast<TemplateTemplateParmDecl>(D)) {
1077 if (TTP->isParameterPack())
1079 Out << D->getName();
1081 Visit(D->getTemplatedDecl());
1085 void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
1086 prettyPrintPragmas(D->getTemplatedDecl());
1087 // Print any leading template parameter lists.
1088 if (const FunctionDecl *FD = D->getTemplatedDecl()) {
1089 for (unsigned I = 0, NumTemplateParams = FD->getNumTemplateParameterLists();
1090 I < NumTemplateParams; ++I)
1091 printTemplateParameters(FD->getTemplateParameterList(I));
1093 VisitRedeclarableTemplateDecl(D);
1094 // Declare target attribute is special one, natural spelling for the pragma
1095 // assumes "ending" construct so print it here.
1096 if (D->getTemplatedDecl()->hasAttr<OMPDeclareTargetDeclAttr>())
1097 Out << "#pragma omp end declare target\n";
1099 // Never print "instantiations" for deduction guides (they don't really
1101 if (PrintInstantiation &&
1102 !isa<CXXDeductionGuideDecl>(D->getTemplatedDecl())) {
1103 FunctionDecl *PrevDecl = D->getTemplatedDecl();
1104 const FunctionDecl *Def;
1105 if (PrevDecl->isDefined(Def) && Def != PrevDecl)
1107 for (auto *I : D->specializations())
1108 if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) {
1109 if (!PrevDecl->isThisDeclarationADefinition())
1112 prettyPrintPragmas(I);
1118 void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
1119 VisitRedeclarableTemplateDecl(D);
1121 if (PrintInstantiation) {
1122 for (auto *I : D->specializations())
1123 if (I->getSpecializationKind() == TSK_ImplicitInstantiation) {
1124 if (D->isThisDeclarationADefinition())
1132 void DeclPrinter::VisitClassTemplateSpecializationDecl(
1133 ClassTemplateSpecializationDecl *D) {
1134 Out << "template<> ";
1135 VisitCXXRecordDecl(D);
1138 void DeclPrinter::VisitClassTemplatePartialSpecializationDecl(
1139 ClassTemplatePartialSpecializationDecl *D) {
1140 printTemplateParameters(D->getTemplateParameters());
1141 VisitCXXRecordDecl(D);
1144 //----------------------------------------------------------------------------
1145 // Objective-C declarations
1146 //----------------------------------------------------------------------------
1148 void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
1149 Decl::ObjCDeclQualifier Quals,
1152 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
1154 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
1156 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1158 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1160 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1162 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1164 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
1165 if (auto nullability = AttributedType::stripOuterNullability(T))
1166 Out << getNullabilitySpelling(*nullability, true) << ' ';
1169 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1173 void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1175 unsigned First = true;
1176 for (auto *Param : *Params) {
1183 switch (Param->getVariance()) {
1184 case ObjCTypeParamVariance::Invariant:
1187 case ObjCTypeParamVariance::Covariant:
1188 Out << "__covariant ";
1191 case ObjCTypeParamVariance::Contravariant:
1192 Out << "__contravariant ";
1196 Out << Param->getDeclName().getAsString();
1198 if (Param->hasExplicitBound()) {
1199 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1205 void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1206 if (OMD->isInstanceMethod())
1210 if (!OMD->getReturnType().isNull()) {
1211 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1212 OMD->getReturnType());
1215 std::string name = OMD->getSelector().getAsString();
1216 std::string::size_type pos, lastPos = 0;
1217 for (const auto *PI : OMD->parameters()) {
1218 // FIXME: selector is missing here!
1219 pos = name.find_first_of(':', lastPos);
1222 Out << name.substr(lastPos, pos - lastPos) << ':';
1223 PrintObjCMethodType(OMD->getASTContext(),
1224 PI->getObjCDeclQualifier(),
1230 if (OMD->param_begin() == OMD->param_end())
1233 if (OMD->isVariadic())
1236 prettyPrintAttributes(OMD);
1238 if (OMD->getBody() && !Policy.TerseOutput) {
1240 OMD->getBody()->printPretty(Out, nullptr, Policy);
1242 else if (Policy.PolishForDeclaration)
1246 void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1247 std::string I = OID->getNameAsString();
1248 ObjCInterfaceDecl *SID = OID->getSuperClass();
1250 bool eolnOut = false;
1252 Out << "@implementation " << I << " : " << *SID;
1254 Out << "@implementation " << I;
1256 if (OID->ivar_size() > 0) {
1259 Indentation += Policy.Indentation;
1260 for (const auto *I : OID->ivars()) {
1261 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
1262 getAsString(Policy) << ' ' << *I << ";\n";
1264 Indentation -= Policy.Indentation;
1267 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1271 VisitDeclContext(OID, false);
1277 void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1278 std::string I = OID->getNameAsString();
1279 ObjCInterfaceDecl *SID = OID->getSuperClass();
1281 if (!OID->isThisDeclarationADefinition()) {
1282 Out << "@class " << I;
1284 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1285 PrintObjCTypeParams(TypeParams);
1291 bool eolnOut = false;
1292 Out << "@interface " << I;
1294 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1295 PrintObjCTypeParams(TypeParams);
1299 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
1302 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1303 if (!Protocols.empty()) {
1304 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1305 E = Protocols.end(); I != E; ++I)
1306 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1310 if (OID->ivar_size() > 0) {
1313 Indentation += Policy.Indentation;
1314 for (const auto *I : OID->ivars()) {
1315 Indent() << I->getASTContext()
1316 .getUnqualifiedObjCPointerType(I->getType())
1317 .getAsString(Policy) << ' ' << *I << ";\n";
1319 Indentation -= Policy.Indentation;
1322 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1327 VisitDeclContext(OID, false);
1331 // FIXME: implement the rest...
1334 void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
1335 if (!PID->isThisDeclarationADefinition()) {
1336 Out << "@protocol " << *PID << ";\n";
1340 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1341 if (!Protocols.empty()) {
1342 Out << "@protocol " << *PID;
1343 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1344 E = Protocols.end(); I != E; ++I)
1345 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1348 Out << "@protocol " << *PID << '\n';
1349 VisitDeclContext(PID, false);
1353 void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
1354 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
1356 VisitDeclContext(PID, false);
1358 // FIXME: implement the rest...
1361 void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
1362 Out << "@interface " << *PID->getClassInterface();
1363 if (auto TypeParams = PID->getTypeParamList()) {
1364 PrintObjCTypeParams(TypeParams);
1366 Out << "(" << *PID << ")\n";
1367 if (PID->ivar_size() > 0) {
1369 Indentation += Policy.Indentation;
1370 for (const auto *I : PID->ivars())
1371 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
1372 getAsString(Policy) << ' ' << *I << ";\n";
1373 Indentation -= Policy.Indentation;
1377 VisitDeclContext(PID, false);
1380 // FIXME: implement the rest...
1383 void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
1384 Out << "@compatibility_alias " << *AID
1385 << ' ' << *AID->getClassInterface() << ";\n";
1388 /// PrintObjCPropertyDecl - print a property declaration.
1390 void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1391 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1392 Out << "@required\n";
1393 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1394 Out << "@optional\n";
1396 QualType T = PDecl->getType();
1399 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1402 if (PDecl->getPropertyAttributes() &
1403 ObjCPropertyDecl::OBJC_PR_readonly) {
1404 Out << (first ? ' ' : ',') << "readonly";
1408 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1409 Out << (first ? ' ' : ',') << "getter = ";
1410 PDecl->getGetterName().print(Out);
1413 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1414 Out << (first ? ' ' : ',') << "setter = ";
1415 PDecl->getSetterName().print(Out);
1419 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1420 Out << (first ? ' ' : ',') << "assign";
1424 if (PDecl->getPropertyAttributes() &
1425 ObjCPropertyDecl::OBJC_PR_readwrite) {
1426 Out << (first ? ' ' : ',') << "readwrite";
1430 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1431 Out << (first ? ' ' : ',') << "retain";
1435 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1436 Out << (first ? ' ' : ',') << "strong";
1440 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1441 Out << (first ? ' ' : ',') << "copy";
1445 if (PDecl->getPropertyAttributes() &
1446 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1447 Out << (first ? ' ' : ',') << "nonatomic";
1450 if (PDecl->getPropertyAttributes() &
1451 ObjCPropertyDecl::OBJC_PR_atomic) {
1452 Out << (first ? ' ' : ',') << "atomic";
1456 if (PDecl->getPropertyAttributes() &
1457 ObjCPropertyDecl::OBJC_PR_nullability) {
1458 if (auto nullability = AttributedType::stripOuterNullability(T)) {
1459 if (*nullability == NullabilityKind::Unspecified &&
1460 (PDecl->getPropertyAttributes() &
1461 ObjCPropertyDecl::OBJC_PR_null_resettable)) {
1462 Out << (first ? ' ' : ',') << "null_resettable";
1464 Out << (first ? ' ' : ',')
1465 << getNullabilitySpelling(*nullability, true);
1471 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) {
1472 Out << (first ? ' ' : ',') << "class";
1476 (void) first; // Silence dead store warning due to idiomatic code.
1479 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
1480 getAsString(Policy) << ' ' << *PDecl;
1481 if (Policy.PolishForDeclaration)
1485 void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1486 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1487 Out << "@synthesize ";
1490 Out << *PID->getPropertyDecl();
1491 if (PID->getPropertyIvarDecl())
1492 Out << '=' << *PID->getPropertyIvarDecl();
1495 void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1496 if (!D->isAccessDeclaration())
1498 if (D->hasTypename())
1500 D->getQualifier()->print(Out, Policy);
1502 // Use the correct record name when the using declaration is used for
1503 // inheriting constructors.
1504 for (const auto *Shadow : D->shadows()) {
1505 if (const auto *ConstructorShadow =
1506 dyn_cast<ConstructorUsingShadowDecl>(Shadow)) {
1507 assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext());
1508 Out << *ConstructorShadow->getNominatedBaseClass();
1516 DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1517 Out << "using typename ";
1518 D->getQualifier()->print(Out, Policy);
1519 Out << D->getDeclName();
1522 void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1523 if (!D->isAccessDeclaration())
1525 D->getQualifier()->print(Out, Policy);
1526 Out << D->getDeclName();
1529 void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1533 void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1534 Out << "#pragma omp threadprivate";
1535 if (!D->varlist_empty()) {
1536 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1537 E = D->varlist_end();
1539 Out << (I == D->varlist_begin() ? '(' : ',');
1540 NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl();
1541 ND->printQualifiedName(Out);
1547 void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1548 if (!D->isInvalidDecl()) {
1549 Out << "#pragma omp declare reduction (";
1550 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
1551 static const char *const OperatorNames[NUM_OVERLOADED_OPERATORS] = {
1553 #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1555 #include "clang/Basic/OperatorKinds.def"
1557 const char *OpName =
1558 OperatorNames[D->getDeclName().getCXXOverloadedOperator()];
1559 assert(OpName && "not an overloaded operator");
1562 assert(D->getDeclName().isIdentifier());
1566 D->getType().print(Out, Policy);
1568 D->getCombiner()->printPretty(Out, nullptr, Policy, 0);
1570 if (auto *Init = D->getInitializer()) {
1571 Out << " initializer(";
1572 switch (D->getInitializerKind()) {
1573 case OMPDeclareReductionDecl::DirectInit:
1576 case OMPDeclareReductionDecl::CopyInit:
1577 Out << "omp_priv = ";
1579 case OMPDeclareReductionDecl::CallInit:
1582 Init->printPretty(Out, nullptr, Policy, 0);
1583 if (D->getInitializerKind() == OMPDeclareReductionDecl::DirectInit)
1590 void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
1591 D->getInit()->printPretty(Out, nullptr, Policy, Indentation);