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 VisitOMPRequiresDecl(OMPRequiresDecl *D);
104 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
105 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
107 void printTemplateParameters(const TemplateParameterList *Params);
108 void printTemplateArguments(const TemplateArgumentList &Args,
109 const TemplateParameterList *Params = nullptr);
110 void prettyPrintAttributes(Decl *D);
111 void prettyPrintPragmas(Decl *D);
112 void printDeclType(QualType T, StringRef DeclName, bool Pack = false);
116 void Decl::print(raw_ostream &Out, unsigned Indentation,
117 bool PrintInstantiation) const {
118 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
121 void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
122 unsigned Indentation, bool PrintInstantiation) const {
123 DeclPrinter Printer(Out, Policy, getASTContext(), Indentation,
125 Printer.Visit(const_cast<Decl*>(this));
128 static QualType GetBaseType(QualType T) {
129 // FIXME: This should be on the Type class!
130 QualType BaseType = T;
131 while (!BaseType->isSpecifierType()) {
132 if (const PointerType *PTy = BaseType->getAs<PointerType>())
133 BaseType = PTy->getPointeeType();
134 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
135 BaseType = BPy->getPointeeType();
136 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
137 BaseType = ATy->getElementType();
138 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
139 BaseType = FTy->getReturnType();
140 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
141 BaseType = VTy->getElementType();
142 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
143 BaseType = RTy->getPointeeType();
144 else if (const AutoType *ATy = BaseType->getAs<AutoType>())
145 BaseType = ATy->getDeducedType();
146 else if (const ParenType *PTy = BaseType->getAs<ParenType>())
147 BaseType = PTy->desugar();
149 // This must be a syntax error.
155 static QualType getDeclType(Decl* D) {
156 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
157 return TDD->getUnderlyingType();
158 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
159 return VD->getType();
163 void Decl::printGroup(Decl** Begin, unsigned NumDecls,
164 raw_ostream &Out, const PrintingPolicy &Policy,
165 unsigned Indentation) {
167 (*Begin)->print(Out, Policy, Indentation);
171 Decl** End = Begin + NumDecls;
172 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
176 PrintingPolicy SubPolicy(Policy);
179 for ( ; Begin != End; ++Begin) {
182 SubPolicy.IncludeTagDefinition = true;
183 SubPolicy.SuppressSpecifiers = false;
186 if (!isFirst) Out << ", ";
187 SubPolicy.IncludeTagDefinition = false;
188 SubPolicy.SuppressSpecifiers = true;
191 (*Begin)->print(Out, SubPolicy, Indentation);
195 LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
196 // Get the translation unit
197 const DeclContext *DC = this;
198 while (!DC->isTranslationUnit())
199 DC = DC->getParent();
201 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
202 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), Ctx, 0);
203 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
206 raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
207 for (unsigned i = 0; i != Indentation; ++i)
212 void DeclPrinter::prettyPrintAttributes(Decl *D) {
213 if (Policy.PolishForDeclaration)
217 AttrVec &Attrs = D->getAttrs();
218 for (auto *A : Attrs) {
219 if (A->isInherited() || A->isImplicit())
221 switch (A->getKind()) {
223 #define PRAGMA_SPELLING_ATTR(X) case attr::X:
224 #include "clang/Basic/AttrList.inc"
227 A->printPretty(Out, Policy);
234 void DeclPrinter::prettyPrintPragmas(Decl *D) {
235 if (Policy.PolishForDeclaration)
239 AttrVec &Attrs = D->getAttrs();
240 for (auto *A : Attrs) {
241 switch (A->getKind()) {
243 #define PRAGMA_SPELLING_ATTR(X) case attr::X:
244 #include "clang/Basic/AttrList.inc"
245 A->printPretty(Out, Policy);
255 void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) {
256 // Normally, a PackExpansionType is written as T[3]... (for instance, as a
257 // template argument), but if it is the type of a declaration, the ellipsis
258 // is placed before the name being declared.
259 if (auto *PET = T->getAs<PackExpansionType>()) {
261 T = PET->getPattern();
263 T.print(Out, Policy, (Pack ? "..." : "") + DeclName, Indentation);
266 void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
268 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
274 void DeclPrinter::Print(AccessSpecifier AS) {
276 case AS_none: llvm_unreachable("No access specifier!");
277 case AS_public: Out << "public"; break;
278 case AS_protected: Out << "protected"; break;
279 case AS_private: Out << "private"; break;
283 void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl,
284 std::string &Proto) {
285 bool HasInitializerList = false;
286 for (const auto *BMInitializer : CDecl->inits()) {
287 if (BMInitializer->isInClassMemberInitializer())
290 if (!HasInitializerList) {
294 HasInitializerList = true;
298 if (BMInitializer->isAnyMemberInitializer()) {
299 FieldDecl *FD = BMInitializer->getAnyMember();
302 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
306 if (!BMInitializer->getInit()) {
309 Expr *Init = BMInitializer->getInit();
310 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
311 Init = Tmp->getSubExpr();
313 Init = Init->IgnoreParens();
315 Expr *SimpleInit = nullptr;
316 Expr **Args = nullptr;
317 unsigned NumArgs = 0;
318 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
319 Args = ParenList->getExprs();
320 NumArgs = ParenList->getNumExprs();
321 } else if (CXXConstructExpr *Construct =
322 dyn_cast<CXXConstructExpr>(Init)) {
323 Args = Construct->getArgs();
324 NumArgs = Construct->getNumArgs();
329 SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
331 for (unsigned I = 0; I != NumArgs; ++I) {
332 assert(Args[I] != nullptr && "Expected non-null Expr");
333 if (isa<CXXDefaultArgExpr>(Args[I]))
338 Args[I]->printPretty(Out, nullptr, Policy, Indentation);
343 if (BMInitializer->isPackExpansion())
348 //----------------------------------------------------------------------------
349 // Common C declarations
350 //----------------------------------------------------------------------------
352 void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
353 if (Policy.TerseOutput)
357 Indentation += Policy.Indentation;
359 SmallVector<Decl*, 2> Decls;
360 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
363 // Don't print ObjCIvarDecls, as they are printed when visiting the
364 // containing ObjCInterfaceDecl.
365 if (isa<ObjCIvarDecl>(*D))
368 // Skip over implicit declarations in pretty-printing mode.
372 // Don't print implicit specializations, as they are printed when visiting
373 // corresponding templates.
374 if (auto FD = dyn_cast<FunctionDecl>(*D))
375 if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation &&
376 !isa<ClassTemplateSpecializationDecl>(DC))
379 // The next bits of code handle stuff like "struct {int x;} a,b"; we're
380 // forced to merge the declarations because there's no other way to
381 // refer to the struct in question. When that struct is named instead, we
382 // also need to merge to avoid splitting off a stand-alone struct
383 // declaration that produces the warning ext_no_declarators in some
386 // This limited merging is safe without a bunch of other checks because it
387 // only merges declarations directly referring to the tag, not typedefs.
389 // Check whether the current declaration should be grouped with a previous
390 // non-free-standing tag declaration.
391 QualType CurDeclType = getDeclType(*D);
392 if (!Decls.empty() && !CurDeclType.isNull()) {
393 QualType BaseType = GetBaseType(CurDeclType);
394 if (!BaseType.isNull() && isa<ElaboratedType>(BaseType) &&
395 cast<ElaboratedType>(BaseType)->getOwnedTagDecl() == Decls[0]) {
401 // If we have a merged group waiting to be handled, handle it now.
403 ProcessDeclGroup(Decls);
405 // If the current declaration is not a free standing declaration, save it
406 // so we can merge it with the subsequent declaration(s) using it.
407 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->isFreeStanding()) {
412 if (isa<AccessSpecDecl>(*D)) {
413 Indentation -= Policy.Indentation;
415 Print(D->getAccess());
417 Indentation += Policy.Indentation;
424 // FIXME: Need to be able to tell the DeclPrinter when
425 const char *Terminator = nullptr;
426 if (isa<OMPThreadPrivateDecl>(*D) || isa<OMPDeclareReductionDecl>(*D) ||
427 isa<OMPRequiresDecl>(*D))
428 Terminator = nullptr;
429 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->hasBody())
430 Terminator = nullptr;
431 else if (auto FD = dyn_cast<FunctionDecl>(*D)) {
432 if (FD->isThisDeclarationADefinition())
433 Terminator = nullptr;
436 } else if (auto TD = dyn_cast<FunctionTemplateDecl>(*D)) {
437 if (TD->getTemplatedDecl()->isThisDeclarationADefinition())
438 Terminator = nullptr;
441 } else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
442 isa<ObjCImplementationDecl>(*D) ||
443 isa<ObjCInterfaceDecl>(*D) ||
444 isa<ObjCProtocolDecl>(*D) ||
445 isa<ObjCCategoryImplDecl>(*D) ||
446 isa<ObjCCategoryDecl>(*D))
447 Terminator = nullptr;
448 else if (isa<EnumConstantDecl>(*D)) {
449 DeclContext::decl_iterator Next = D;
458 if (!Policy.TerseOutput &&
459 ((isa<FunctionDecl>(*D) &&
460 cast<FunctionDecl>(*D)->doesThisDeclarationHaveABody()) ||
461 (isa<FunctionTemplateDecl>(*D) &&
462 cast<FunctionTemplateDecl>(*D)->getTemplatedDecl()->doesThisDeclarationHaveABody())))
463 ; // StmtPrinter already added '\n' after CompoundStmt.
467 // Declare target attribute is special one, natural spelling for the pragma
468 // assumes "ending" construct so print it here.
469 if (D->hasAttr<OMPDeclareTargetDeclAttr>())
470 Out << "#pragma omp end declare target\n";
474 ProcessDeclGroup(Decls);
477 Indentation -= Policy.Indentation;
480 void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
481 VisitDeclContext(D, false);
484 void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
485 if (!Policy.SuppressSpecifiers) {
488 if (D->isModulePrivate())
489 Out << "__module_private__ ";
491 QualType Ty = D->getTypeSourceInfo()->getType();
492 Ty.print(Out, Policy, D->getName(), Indentation);
493 prettyPrintAttributes(D);
496 void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
497 Out << "using " << *D;
498 prettyPrintAttributes(D);
499 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
502 void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
503 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
504 Out << "__module_private__ ";
507 if (D->isScopedUsingClassTag())
513 prettyPrintAttributes(D);
517 if (D->isFixed() && D->getASTContext().getLangOpts().CPlusPlus11)
518 Out << " : " << D->getIntegerType().stream(Policy);
520 if (D->isCompleteDefinition()) {
527 void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
528 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
529 Out << "__module_private__ ";
530 Out << D->getKindName();
532 prettyPrintAttributes(D);
534 if (D->getIdentifier())
537 if (D->isCompleteDefinition()) {
544 void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
546 prettyPrintAttributes(D);
547 if (Expr *Init = D->getInitExpr()) {
549 Init->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context);
553 void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
554 if (!D->getDescribedFunctionTemplate() &&
555 !D->isFunctionTemplateSpecialization())
556 prettyPrintPragmas(D);
558 if (D->isFunctionTemplateSpecialization())
559 Out << "template<> ";
560 else if (!D->getDescribedFunctionTemplate()) {
561 for (unsigned I = 0, NumTemplateParams = D->getNumTemplateParameterLists();
562 I < NumTemplateParams; ++I)
563 printTemplateParameters(D->getTemplateParameterList(I));
566 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
567 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
568 CXXDeductionGuideDecl *GuideDecl = dyn_cast<CXXDeductionGuideDecl>(D);
569 if (!Policy.SuppressSpecifiers) {
570 switch (D->getStorageClass()) {
572 case SC_Extern: Out << "extern "; break;
573 case SC_Static: Out << "static "; break;
574 case SC_PrivateExtern: Out << "__private_extern__ "; break;
575 case SC_Auto: case SC_Register:
576 llvm_unreachable("invalid for functions");
579 if (D->isInlineSpecified()) Out << "inline ";
580 if (D->isVirtualAsWritten()) Out << "virtual ";
581 if (D->isModulePrivate()) Out << "__module_private__ ";
582 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
583 if ((CDecl && CDecl->isExplicitSpecified()) ||
584 (ConversionDecl && ConversionDecl->isExplicitSpecified()) ||
585 (GuideDecl && GuideDecl->isExplicitSpecified()))
589 PrintingPolicy SubPolicy(Policy);
590 SubPolicy.SuppressSpecifiers = false;
593 if (Policy.FullyQualifiedName) {
594 Proto += D->getQualifiedNameAsString();
596 if (!Policy.SuppressScope) {
597 if (const NestedNameSpecifier *NS = D->getQualifier()) {
598 llvm::raw_string_ostream OS(Proto);
599 NS->print(OS, Policy);
602 Proto += D->getNameInfo().getAsString();
606 Proto = GuideDecl->getDeducedTemplate()->getDeclName().getAsString();
607 if (const TemplateArgumentList *TArgs = D->getTemplateSpecializationArgs()) {
608 llvm::raw_string_ostream POut(Proto);
609 DeclPrinter TArgPrinter(POut, SubPolicy, Context, Indentation);
610 TArgPrinter.printTemplateArguments(*TArgs);
613 QualType Ty = D->getType();
614 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
615 Proto = '(' + Proto + ')';
616 Ty = PT->getInnerType();
619 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
620 const FunctionProtoType *FT = nullptr;
621 if (D->hasWrittenPrototype())
622 FT = dyn_cast<FunctionProtoType>(AFT);
626 llvm::raw_string_ostream POut(Proto);
627 DeclPrinter ParamPrinter(POut, SubPolicy, Context, Indentation);
628 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
630 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
633 if (FT->isVariadic()) {
634 if (D->getNumParams()) POut << ", ";
637 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
638 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
641 Proto += D->getParamDecl(i)->getNameAsString();
650 if (FT->isVolatile())
651 Proto += " volatile";
652 if (FT->isRestrict())
653 Proto += " restrict";
655 switch (FT->getRefQualifier()) {
667 if (FT && FT->hasDynamicExceptionSpec()) {
669 if (FT->getExceptionSpecType() == EST_MSAny)
672 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
676 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
679 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
680 Proto += " noexcept";
681 if (isComputedNoexcept(FT->getExceptionSpecType())) {
683 llvm::raw_string_ostream EOut(Proto);
684 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
693 if (!Policy.TerseOutput)
694 PrintConstructorInitializers(CDecl, Proto);
695 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
696 if (FT && FT->hasTrailingReturn()) {
699 Out << Proto << " -> ";
702 AFT->getReturnType().print(Out, Policy, Proto);
707 Ty.print(Out, Policy, Proto);
710 prettyPrintAttributes(D);
714 else if (D->isDeletedAsWritten())
716 else if (D->isExplicitlyDefaulted())
718 else if (D->doesThisDeclarationHaveABody()) {
719 if (!Policy.TerseOutput) {
720 if (!D->hasPrototype() && D->getNumParams()) {
721 // This is a K&R function definition, so we need to print the
724 DeclPrinter ParamPrinter(Out, SubPolicy, Context, Indentation);
725 Indentation += Policy.Indentation;
726 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
728 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
731 Indentation -= Policy.Indentation;
736 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
738 if (!Policy.TerseOutput && isa<CXXConstructorDecl>(*D))
744 void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
745 if (TypeSourceInfo *TSI = D->getFriendType()) {
746 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
747 for (unsigned i = 0; i < NumTPLists; ++i)
748 printTemplateParameters(D->getFriendTypeTemplateParameterList(i));
750 Out << " " << TSI->getType().getAsString(Policy);
752 else if (FunctionDecl *FD =
753 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
755 VisitFunctionDecl(FD);
757 else if (FunctionTemplateDecl *FTD =
758 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
760 VisitFunctionTemplateDecl(FTD);
762 else if (ClassTemplateDecl *CTD =
763 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
765 VisitRedeclarableTemplateDecl(CTD);
769 void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
770 // FIXME: add printing of pragma attributes if required.
771 if (!Policy.SuppressSpecifiers && D->isMutable())
773 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
774 Out << "__module_private__ ";
776 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
777 stream(Policy, D->getName(), Indentation);
779 if (D->isBitField()) {
781 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
784 Expr *Init = D->getInClassInitializer();
785 if (!Policy.SuppressInitializers && Init) {
786 if (D->getInClassInitStyle() == ICIS_ListInit)
790 Init->printPretty(Out, nullptr, Policy, Indentation);
792 prettyPrintAttributes(D);
795 void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
799 void DeclPrinter::VisitVarDecl(VarDecl *D) {
800 prettyPrintPragmas(D);
802 QualType T = D->getTypeSourceInfo()
803 ? D->getTypeSourceInfo()->getType()
804 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
806 if (!Policy.SuppressSpecifiers) {
807 StorageClass SC = D->getStorageClass();
809 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
811 switch (D->getTSCSpec()) {
812 case TSCS_unspecified:
817 case TSCS__Thread_local:
818 Out << "_Thread_local ";
820 case TSCS_thread_local:
821 Out << "thread_local ";
825 if (D->isModulePrivate())
826 Out << "__module_private__ ";
828 if (D->isConstexpr()) {
830 T.removeLocalConst();
834 printDeclType(T, D->getName());
835 Expr *Init = D->getInit();
836 if (!Policy.SuppressInitializers && Init) {
837 bool ImplicitInit = false;
838 if (CXXConstructExpr *Construct =
839 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
840 if (D->getInitStyle() == VarDecl::CallInit &&
841 !Construct->isListInitialization()) {
842 ImplicitInit = Construct->getNumArgs() == 0 ||
843 Construct->getArg(0)->isDefaultArgument();
847 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
849 else if (D->getInitStyle() == VarDecl::CInit) {
852 PrintingPolicy SubPolicy(Policy);
853 SubPolicy.SuppressSpecifiers = false;
854 SubPolicy.IncludeTagDefinition = false;
855 Init->printPretty(Out, nullptr, SubPolicy, Indentation);
856 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
860 prettyPrintAttributes(D);
863 void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
867 void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
869 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
873 void DeclPrinter::VisitImportDecl(ImportDecl *D) {
874 Out << "@import " << D->getImportedModule()->getFullModuleName()
878 void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
879 Out << "static_assert(";
880 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
881 if (StringLiteral *SL = D->getMessage()) {
883 SL->printPretty(Out, nullptr, Policy, Indentation);
888 //----------------------------------------------------------------------------
890 //----------------------------------------------------------------------------
891 void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
894 Out << "namespace " << *D << " {\n";
899 void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
900 Out << "using namespace ";
901 if (D->getQualifier())
902 D->getQualifier()->print(Out, Policy);
903 Out << *D->getNominatedNamespaceAsWritten();
906 void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
907 Out << "namespace " << *D << " = ";
908 if (D->getQualifier())
909 D->getQualifier()->print(Out, Policy);
910 Out << *D->getAliasedNamespace();
913 void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
914 prettyPrintAttributes(D);
917 void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
918 // FIXME: add printing of pragma attributes if required.
919 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
920 Out << "__module_private__ ";
921 Out << D->getKindName();
923 prettyPrintAttributes(D);
925 if (D->getIdentifier()) {
928 if (auto S = dyn_cast<ClassTemplatePartialSpecializationDecl>(D))
929 printTemplateArguments(S->getTemplateArgs(), S->getTemplateParameters());
930 else if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D))
931 printTemplateArguments(S->getTemplateArgs());
934 if (D->isCompleteDefinition()) {
935 // Print the base classes
936 if (D->getNumBases()) {
938 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
939 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
940 if (Base != D->bases_begin())
943 if (Base->isVirtual())
946 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
951 Out << Base->getType().getAsString(Policy);
953 if (Base->isPackExpansion())
958 // Print the class definition
959 // FIXME: Doesn't print access specifiers, e.g., "public:"
960 if (Policy.TerseOutput) {
970 void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
972 if (D->getLanguage() == LinkageSpecDecl::lang_c)
975 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
976 "unknown language in linkage specification");
980 Out << "extern \"" << l << "\" ";
981 if (D->hasBraces()) {
986 Visit(*D->decls_begin());
989 void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params) {
994 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
998 const Decl *Param = Params->getParam(i);
999 if (auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
1001 if (TTP->wasDeclaredWithTypename())
1006 if (TTP->isParameterPack())
1011 if (TTP->hasDefaultArgument()) {
1013 Out << TTP->getDefaultArgument().getAsString(Policy);
1015 } else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1017 if (IdentifierInfo *II = NTTP->getIdentifier())
1018 Name = II->getName();
1019 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
1021 if (NTTP->hasDefaultArgument()) {
1023 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
1026 } else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
1027 VisitTemplateDecl(TTPD);
1028 // FIXME: print the default argument, if present.
1035 void DeclPrinter::printTemplateArguments(const TemplateArgumentList &Args,
1036 const TemplateParameterList *Params) {
1038 for (size_t I = 0, E = Args.size(); I < E; ++I) {
1039 const TemplateArgument &A = Args[I];
1043 if (A.getKind() == TemplateArgument::Type)
1044 if (auto T = A.getAsType()->getAs<TemplateTypeParmType>()) {
1045 auto P = cast<TemplateTypeParmDecl>(Params->getParam(T->getIndex()));
1049 if (A.getKind() == TemplateArgument::Template) {
1050 if (auto T = A.getAsTemplate().getAsTemplateDecl())
1051 if (auto TD = dyn_cast<TemplateTemplateParmDecl>(T)) {
1052 auto P = cast<TemplateTemplateParmDecl>(
1053 Params->getParam(TD->getIndex()));
1058 if (A.getKind() == TemplateArgument::Expression) {
1059 if (auto E = dyn_cast<DeclRefExpr>(A.getAsExpr()))
1060 if (auto N = dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1061 auto P = cast<NonTypeTemplateParmDecl>(
1062 Params->getParam(N->getIndex()));
1068 A.print(Policy, Out);
1073 void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
1074 printTemplateParameters(D->getTemplateParameters());
1076 if (const TemplateTemplateParmDecl *TTP =
1077 dyn_cast<TemplateTemplateParmDecl>(D)) {
1079 if (TTP->isParameterPack())
1081 Out << D->getName();
1083 Visit(D->getTemplatedDecl());
1087 void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
1088 prettyPrintPragmas(D->getTemplatedDecl());
1089 // Print any leading template parameter lists.
1090 if (const FunctionDecl *FD = D->getTemplatedDecl()) {
1091 for (unsigned I = 0, NumTemplateParams = FD->getNumTemplateParameterLists();
1092 I < NumTemplateParams; ++I)
1093 printTemplateParameters(FD->getTemplateParameterList(I));
1095 VisitRedeclarableTemplateDecl(D);
1096 // Declare target attribute is special one, natural spelling for the pragma
1097 // assumes "ending" construct so print it here.
1098 if (D->getTemplatedDecl()->hasAttr<OMPDeclareTargetDeclAttr>())
1099 Out << "#pragma omp end declare target\n";
1101 // Never print "instantiations" for deduction guides (they don't really
1103 if (PrintInstantiation &&
1104 !isa<CXXDeductionGuideDecl>(D->getTemplatedDecl())) {
1105 FunctionDecl *PrevDecl = D->getTemplatedDecl();
1106 const FunctionDecl *Def;
1107 if (PrevDecl->isDefined(Def) && Def != PrevDecl)
1109 for (auto *I : D->specializations())
1110 if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) {
1111 if (!PrevDecl->isThisDeclarationADefinition())
1114 prettyPrintPragmas(I);
1120 void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
1121 VisitRedeclarableTemplateDecl(D);
1123 if (PrintInstantiation) {
1124 for (auto *I : D->specializations())
1125 if (I->getSpecializationKind() == TSK_ImplicitInstantiation) {
1126 if (D->isThisDeclarationADefinition())
1134 void DeclPrinter::VisitClassTemplateSpecializationDecl(
1135 ClassTemplateSpecializationDecl *D) {
1136 Out << "template<> ";
1137 VisitCXXRecordDecl(D);
1140 void DeclPrinter::VisitClassTemplatePartialSpecializationDecl(
1141 ClassTemplatePartialSpecializationDecl *D) {
1142 printTemplateParameters(D->getTemplateParameters());
1143 VisitCXXRecordDecl(D);
1146 //----------------------------------------------------------------------------
1147 // Objective-C declarations
1148 //----------------------------------------------------------------------------
1150 void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
1151 Decl::ObjCDeclQualifier Quals,
1154 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
1156 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
1158 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1160 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1162 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1164 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1166 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
1167 if (auto nullability = AttributedType::stripOuterNullability(T))
1168 Out << getNullabilitySpelling(*nullability, true) << ' ';
1171 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1175 void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1177 unsigned First = true;
1178 for (auto *Param : *Params) {
1185 switch (Param->getVariance()) {
1186 case ObjCTypeParamVariance::Invariant:
1189 case ObjCTypeParamVariance::Covariant:
1190 Out << "__covariant ";
1193 case ObjCTypeParamVariance::Contravariant:
1194 Out << "__contravariant ";
1198 Out << Param->getDeclName().getAsString();
1200 if (Param->hasExplicitBound()) {
1201 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1207 void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1208 if (OMD->isInstanceMethod())
1212 if (!OMD->getReturnType().isNull()) {
1213 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1214 OMD->getReturnType());
1217 std::string name = OMD->getSelector().getAsString();
1218 std::string::size_type pos, lastPos = 0;
1219 for (const auto *PI : OMD->parameters()) {
1220 // FIXME: selector is missing here!
1221 pos = name.find_first_of(':', lastPos);
1224 Out << name.substr(lastPos, pos - lastPos) << ':';
1225 PrintObjCMethodType(OMD->getASTContext(),
1226 PI->getObjCDeclQualifier(),
1232 if (OMD->param_begin() == OMD->param_end())
1235 if (OMD->isVariadic())
1238 prettyPrintAttributes(OMD);
1240 if (OMD->getBody() && !Policy.TerseOutput) {
1242 OMD->getBody()->printPretty(Out, nullptr, Policy);
1244 else if (Policy.PolishForDeclaration)
1248 void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1249 std::string I = OID->getNameAsString();
1250 ObjCInterfaceDecl *SID = OID->getSuperClass();
1252 bool eolnOut = false;
1254 Out << "@implementation " << I << " : " << *SID;
1256 Out << "@implementation " << I;
1258 if (OID->ivar_size() > 0) {
1261 Indentation += Policy.Indentation;
1262 for (const auto *I : OID->ivars()) {
1263 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
1264 getAsString(Policy) << ' ' << *I << ";\n";
1266 Indentation -= Policy.Indentation;
1269 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1273 VisitDeclContext(OID, false);
1279 void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1280 std::string I = OID->getNameAsString();
1281 ObjCInterfaceDecl *SID = OID->getSuperClass();
1283 if (!OID->isThisDeclarationADefinition()) {
1284 Out << "@class " << I;
1286 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1287 PrintObjCTypeParams(TypeParams);
1293 bool eolnOut = false;
1294 Out << "@interface " << I;
1296 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1297 PrintObjCTypeParams(TypeParams);
1301 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
1304 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1305 if (!Protocols.empty()) {
1306 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1307 E = Protocols.end(); I != E; ++I)
1308 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1312 if (OID->ivar_size() > 0) {
1315 Indentation += Policy.Indentation;
1316 for (const auto *I : OID->ivars()) {
1317 Indent() << I->getASTContext()
1318 .getUnqualifiedObjCPointerType(I->getType())
1319 .getAsString(Policy) << ' ' << *I << ";\n";
1321 Indentation -= Policy.Indentation;
1324 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1329 VisitDeclContext(OID, false);
1333 // FIXME: implement the rest...
1336 void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
1337 if (!PID->isThisDeclarationADefinition()) {
1338 Out << "@protocol " << *PID << ";\n";
1342 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1343 if (!Protocols.empty()) {
1344 Out << "@protocol " << *PID;
1345 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1346 E = Protocols.end(); I != E; ++I)
1347 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1350 Out << "@protocol " << *PID << '\n';
1351 VisitDeclContext(PID, false);
1355 void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
1356 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
1358 VisitDeclContext(PID, false);
1360 // FIXME: implement the rest...
1363 void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
1364 Out << "@interface " << *PID->getClassInterface();
1365 if (auto TypeParams = PID->getTypeParamList()) {
1366 PrintObjCTypeParams(TypeParams);
1368 Out << "(" << *PID << ")\n";
1369 if (PID->ivar_size() > 0) {
1371 Indentation += Policy.Indentation;
1372 for (const auto *I : PID->ivars())
1373 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
1374 getAsString(Policy) << ' ' << *I << ";\n";
1375 Indentation -= Policy.Indentation;
1379 VisitDeclContext(PID, false);
1382 // FIXME: implement the rest...
1385 void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
1386 Out << "@compatibility_alias " << *AID
1387 << ' ' << *AID->getClassInterface() << ";\n";
1390 /// PrintObjCPropertyDecl - print a property declaration.
1392 void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1393 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1394 Out << "@required\n";
1395 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1396 Out << "@optional\n";
1398 QualType T = PDecl->getType();
1401 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1404 if (PDecl->getPropertyAttributes() &
1405 ObjCPropertyDecl::OBJC_PR_readonly) {
1406 Out << (first ? ' ' : ',') << "readonly";
1410 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1411 Out << (first ? ' ' : ',') << "getter = ";
1412 PDecl->getGetterName().print(Out);
1415 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1416 Out << (first ? ' ' : ',') << "setter = ";
1417 PDecl->getSetterName().print(Out);
1421 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1422 Out << (first ? ' ' : ',') << "assign";
1426 if (PDecl->getPropertyAttributes() &
1427 ObjCPropertyDecl::OBJC_PR_readwrite) {
1428 Out << (first ? ' ' : ',') << "readwrite";
1432 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1433 Out << (first ? ' ' : ',') << "retain";
1437 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1438 Out << (first ? ' ' : ',') << "strong";
1442 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1443 Out << (first ? ' ' : ',') << "copy";
1447 if (PDecl->getPropertyAttributes() &
1448 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1449 Out << (first ? ' ' : ',') << "nonatomic";
1452 if (PDecl->getPropertyAttributes() &
1453 ObjCPropertyDecl::OBJC_PR_atomic) {
1454 Out << (first ? ' ' : ',') << "atomic";
1458 if (PDecl->getPropertyAttributes() &
1459 ObjCPropertyDecl::OBJC_PR_nullability) {
1460 if (auto nullability = AttributedType::stripOuterNullability(T)) {
1461 if (*nullability == NullabilityKind::Unspecified &&
1462 (PDecl->getPropertyAttributes() &
1463 ObjCPropertyDecl::OBJC_PR_null_resettable)) {
1464 Out << (first ? ' ' : ',') << "null_resettable";
1466 Out << (first ? ' ' : ',')
1467 << getNullabilitySpelling(*nullability, true);
1473 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) {
1474 Out << (first ? ' ' : ',') << "class";
1478 (void) first; // Silence dead store warning due to idiomatic code.
1481 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
1482 getAsString(Policy) << ' ' << *PDecl;
1483 if (Policy.PolishForDeclaration)
1487 void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1488 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1489 Out << "@synthesize ";
1492 Out << *PID->getPropertyDecl();
1493 if (PID->getPropertyIvarDecl())
1494 Out << '=' << *PID->getPropertyIvarDecl();
1497 void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1498 if (!D->isAccessDeclaration())
1500 if (D->hasTypename())
1502 D->getQualifier()->print(Out, Policy);
1504 // Use the correct record name when the using declaration is used for
1505 // inheriting constructors.
1506 for (const auto *Shadow : D->shadows()) {
1507 if (const auto *ConstructorShadow =
1508 dyn_cast<ConstructorUsingShadowDecl>(Shadow)) {
1509 assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext());
1510 Out << *ConstructorShadow->getNominatedBaseClass();
1518 DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1519 Out << "using typename ";
1520 D->getQualifier()->print(Out, Policy);
1521 Out << D->getDeclName();
1524 void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1525 if (!D->isAccessDeclaration())
1527 D->getQualifier()->print(Out, Policy);
1528 Out << D->getDeclName();
1531 void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1535 void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1536 Out << "#pragma omp threadprivate";
1537 if (!D->varlist_empty()) {
1538 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1539 E = D->varlist_end();
1541 Out << (I == D->varlist_begin() ? '(' : ',');
1542 NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl();
1543 ND->printQualifiedName(Out);
1549 void DeclPrinter::VisitOMPRequiresDecl(OMPRequiresDecl *D) {
1550 Out << "#pragma omp requires ";
1551 if (!D->clauselist_empty()) {
1552 OMPClausePrinter Printer(Out, Policy);
1553 for (auto I = D->clauselist_begin(), E = D->clauselist_end(); I != E; ++I)
1558 void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1559 if (!D->isInvalidDecl()) {
1560 Out << "#pragma omp declare reduction (";
1561 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
1562 static const char *const OperatorNames[NUM_OVERLOADED_OPERATORS] = {
1564 #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1566 #include "clang/Basic/OperatorKinds.def"
1568 const char *OpName =
1569 OperatorNames[D->getDeclName().getCXXOverloadedOperator()];
1570 assert(OpName && "not an overloaded operator");
1573 assert(D->getDeclName().isIdentifier());
1577 D->getType().print(Out, Policy);
1579 D->getCombiner()->printPretty(Out, nullptr, Policy, 0);
1581 if (auto *Init = D->getInitializer()) {
1582 Out << " initializer(";
1583 switch (D->getInitializerKind()) {
1584 case OMPDeclareReductionDecl::DirectInit:
1587 case OMPDeclareReductionDecl::CopyInit:
1588 Out << "omp_priv = ";
1590 case OMPDeclareReductionDecl::CallInit:
1593 Init->printPretty(Out, nullptr, Policy, 0);
1594 if (D->getInitializerKind() == OMPDeclareReductionDecl::DirectInit)
1601 void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
1602 D->getInit()->printPretty(Out, nullptr, Policy, Indentation);