1//===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Decl::print method, which pretty prints the
10// AST back out to C/Objective-C/C++/Objective-C++ code.
11//
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 "clang/Basic/SourceManager.h"
25#include "llvm/ADT/StringExtras.h"
26#include "llvm/Support/raw_ostream.h"
27using namespace clang;
28
29namespace {
30 class DeclPrinter : public DeclVisitor<DeclPrinter> {
31 raw_ostream &Out;
32 PrintingPolicy Policy;
33 const ASTContext &Context;
34 unsigned Indentation;
35 bool PrintInstantiation;
36
37 raw_ostream& Indent() { return Indent(Indentation); }
38 raw_ostream& Indent(unsigned Indentation);
39 void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls);
40
41 void Print(AccessSpecifier AS);
42 void PrintConstructorInitializers(CXXConstructorDecl *CDecl,
43 std::string &Proto);
44
45 /// Print an Objective-C method type in parentheses.
46 ///
47 /// \param Quals The Objective-C declaration qualifiers.
48 /// \param T The type to print.
49 void PrintObjCMethodType(ASTContext &Ctx, Decl::ObjCDeclQualifier Quals,
50 QualType T);
51
52 void PrintObjCTypeParams(ObjCTypeParamList *Params);
53 void PrintOpenACCRoutineOnLambda(Decl *D);
54
55 public:
56 DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy,
57 const ASTContext &Context, unsigned Indentation = 0,
58 bool PrintInstantiation = false)
59 : Out(Out), Policy(Policy), Context(Context), Indentation(Indentation),
60 PrintInstantiation(PrintInstantiation) {}
61
62 void VisitDeclContext(DeclContext *DC, bool Indent = true);
63
64 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
65 void VisitTypedefDecl(TypedefDecl *D);
66 void VisitTypeAliasDecl(TypeAliasDecl *D);
67 void VisitEnumDecl(EnumDecl *D);
68 void VisitRecordDecl(RecordDecl *D);
69 void VisitEnumConstantDecl(EnumConstantDecl *D);
70 void VisitEmptyDecl(EmptyDecl *D);
71 void VisitFunctionDecl(FunctionDecl *D);
72 void VisitFriendDecl(FriendDecl *D);
73 void VisitFieldDecl(FieldDecl *D);
74 void VisitVarDecl(VarDecl *D);
75 void VisitLabelDecl(LabelDecl *D);
76 void VisitParmVarDecl(ParmVarDecl *D);
77 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
78 void VisitTopLevelStmtDecl(TopLevelStmtDecl *D);
79 void VisitImportDecl(ImportDecl *D);
80 void VisitStaticAssertDecl(StaticAssertDecl *D);
81 void VisitNamespaceDecl(NamespaceDecl *D);
82 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
83 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
84 void VisitCXXRecordDecl(CXXRecordDecl *D);
85 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
86 void VisitTemplateDecl(const TemplateDecl *D);
87 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
88 void VisitClassTemplateDecl(ClassTemplateDecl *D);
89 void VisitExplicitInstantiationDecl(ExplicitInstantiationDecl *D);
90 void VisitClassTemplateSpecializationDecl(
91 ClassTemplateSpecializationDecl *D);
92 void VisitClassTemplatePartialSpecializationDecl(
93 ClassTemplatePartialSpecializationDecl *D);
94 void VisitObjCMethodDecl(ObjCMethodDecl *D);
95 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
96 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
97 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
98 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
99 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
100 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
101 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
102 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
103 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
104 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
105 void VisitUsingDecl(UsingDecl *D);
106 void VisitUsingEnumDecl(UsingEnumDecl *D);
107 void VisitUsingShadowDecl(UsingShadowDecl *D);
108 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
109 void VisitOMPAllocateDecl(OMPAllocateDecl *D);
110 void VisitOMPRequiresDecl(OMPRequiresDecl *D);
111 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
112 void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D);
113 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
114 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *TTP);
115 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *NTTP);
116 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *);
117 void VisitHLSLBufferDecl(HLSLBufferDecl *D);
118
119 void VisitOpenACCDeclareDecl(OpenACCDeclareDecl *D);
120 void VisitOpenACCRoutineDecl(OpenACCRoutineDecl *D);
121
122 void printTemplateParameters(const TemplateParameterList *Params,
123 bool OmitTemplateKW = false);
124 void printTemplateArguments(ArrayRef<TemplateArgument> Args,
125 const TemplateParameterList *Params);
126 void printTemplateArguments(ArrayRef<TemplateArgumentLoc> Args,
127 const TemplateParameterList *Params);
128 enum class AttrPosAsWritten { Default = 0, Left, Right };
129 std::optional<std::string>
130 prettyPrintAttributes(const Decl *D,
131 AttrPosAsWritten Pos = AttrPosAsWritten::Default);
132
133 void prettyPrintPragmas(Decl *D);
134 void printDeclType(QualType T, StringRef DeclName, bool Pack = false);
135 };
136}
137
138void Decl::print(raw_ostream &Out, unsigned Indentation,
139 bool PrintInstantiation) const {
140 print(Out, Policy: getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
141}
142
143void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
144 unsigned Indentation, bool PrintInstantiation) const {
145 DeclPrinter Printer(Out, Policy, getASTContext(), Indentation,
146 PrintInstantiation);
147 Printer.Visit(D: const_cast<Decl*>(this));
148}
149
150void TemplateParameterList::print(raw_ostream &Out, const ASTContext &Context,
151 bool OmitTemplateKW) const {
152 print(Out, Context, Policy: Context.getPrintingPolicy(), OmitTemplateKW);
153}
154
155void TemplateParameterList::print(raw_ostream &Out, const ASTContext &Context,
156 const PrintingPolicy &Policy,
157 bool OmitTemplateKW) const {
158 DeclPrinter Printer(Out, Policy, Context);
159 Printer.printTemplateParameters(Params: this, OmitTemplateKW);
160}
161
162static QualType GetBaseType(QualType T) {
163 // FIXME: This should be on the Type class!
164 QualType BaseType = T;
165 while (!BaseType->isSpecifierType()) {
166 if (const PointerType *PTy = BaseType->getAs<PointerType>())
167 BaseType = PTy->getPointeeType();
168 else if (const ObjCObjectPointerType *OPT =
169 BaseType->getAs<ObjCObjectPointerType>())
170 BaseType = OPT->getPointeeType();
171 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
172 BaseType = BPy->getPointeeType();
173 else if (const ArrayType *ATy = dyn_cast<ArrayType>(Val&: BaseType))
174 BaseType = ATy->getElementType();
175 else if (const FunctionType *FTy = BaseType->getAs<FunctionType>())
176 BaseType = FTy->getReturnType();
177 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
178 BaseType = VTy->getElementType();
179 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
180 BaseType = RTy->getPointeeType();
181 else if (const AutoType *ATy = BaseType->getAs<AutoType>())
182 BaseType = ATy->getDeducedType();
183 else if (const ParenType *PTy = BaseType->getAs<ParenType>())
184 BaseType = PTy->desugar();
185 else
186 // This must be a syntax error.
187 break;
188 }
189 return BaseType;
190}
191
192static QualType getDeclType(Decl* D) {
193 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(Val: D))
194 return TDD->getUnderlyingType();
195 if (ValueDecl* VD = dyn_cast<ValueDecl>(Val: D))
196 return VD->getType();
197 return QualType();
198}
199
200void Decl::printGroup(Decl** Begin, unsigned NumDecls,
201 raw_ostream &Out, const PrintingPolicy &Policy,
202 unsigned Indentation) {
203 if (NumDecls == 1) {
204 (*Begin)->print(Out, Policy, Indentation);
205 return;
206 }
207
208 Decl** End = Begin + NumDecls;
209 if (isa<TagDecl>(Val: *Begin))
210 ++Begin;
211
212 PrintingPolicy SubPolicy(Policy);
213
214 bool isFirst = true;
215 for ( ; Begin != End; ++Begin) {
216 if (isFirst) {
217 isFirst = false;
218 } else {
219 Out << ", ";
220 SubPolicy.SuppressSpecifiers = true;
221 }
222
223 (*Begin)->print(Out, Policy: SubPolicy, Indentation);
224 }
225}
226
227LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
228 // Get the translation unit
229 const DeclContext *DC = this;
230 while (!DC->isTranslationUnit())
231 DC = DC->getParent();
232
233 ASTContext &Ctx = cast<TranslationUnitDecl>(Val: DC)->getASTContext();
234 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), Ctx, 0);
235 Printer.VisitDeclContext(DC: const_cast<DeclContext *>(this), /*Indent=*/false);
236}
237
238raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
239 for (unsigned i = 0; i != Indentation; ++i)
240 Out << " ";
241 return Out;
242}
243
244static DeclPrinter::AttrPosAsWritten getPosAsWritten(const Attr *A,
245 const Decl *D) {
246 SourceLocation ALoc = A->getLoc();
247 SourceLocation DLoc = D->getLocation();
248 const ASTContext &C = D->getASTContext();
249 if (ALoc.isInvalid() || DLoc.isInvalid())
250 return DeclPrinter::AttrPosAsWritten::Left;
251
252 if (C.getSourceManager().isBeforeInTranslationUnit(LHS: ALoc, RHS: DLoc))
253 return DeclPrinter::AttrPosAsWritten::Left;
254
255 return DeclPrinter::AttrPosAsWritten::Right;
256}
257
258std::optional<std::string>
259DeclPrinter::prettyPrintAttributes(const Decl *D,
260 AttrPosAsWritten Pos /*=Default*/) {
261 if (Policy.SuppressDeclAttributes || !D->hasAttrs())
262 return std::nullopt;
263
264 std::string AttrStr;
265 llvm::raw_string_ostream AOut(AttrStr);
266 llvm::ListSeparator LS(" ");
267 for (auto *A : D->getAttrs()) {
268 if (A->isInherited() || A->isImplicit())
269 continue;
270 // Print out the keyword attributes, they aren't regular attributes.
271 if (Policy.PolishForDeclaration && !A->isKeywordAttribute())
272 continue;
273 switch (A->getKind()) {
274#define ATTR(X)
275#define PRAGMA_SPELLING_ATTR(X) case attr::X:
276#include "clang/Basic/AttrList.inc"
277 break;
278 default:
279 AttrPosAsWritten APos = getPosAsWritten(A, D);
280 assert(APos != AttrPosAsWritten::Default &&
281 "Default not a valid for an attribute location");
282 if (Pos == AttrPosAsWritten::Default || Pos == APos) {
283 AOut << LS;
284 A->printPretty(OS&: AOut, Policy);
285 }
286 break;
287 }
288 }
289 if (AttrStr.empty())
290 return std::nullopt;
291 return AttrStr;
292}
293
294void DeclPrinter::PrintOpenACCRoutineOnLambda(Decl *D) {
295 CXXRecordDecl *CXXRD = nullptr;
296 if (const auto *VD = dyn_cast<VarDecl>(Val: D)) {
297 if (const auto *Init = VD->getInit())
298 CXXRD = Init->getType().isNull() ? nullptr
299 : Init->getType()->getAsCXXRecordDecl();
300 } else if (const auto *FD = dyn_cast<FieldDecl>(Val: D)) {
301 CXXRD =
302 FD->getType().isNull() ? nullptr : FD->getType()->getAsCXXRecordDecl();
303 }
304
305 if (!CXXRD || !CXXRD->isLambda())
306 return;
307
308 if (const auto *Call = CXXRD->getLambdaCallOperator()) {
309 for (auto *A : Call->specific_attrs<OpenACCRoutineDeclAttr>()) {
310 A->printPretty(OS&: Out, Policy);
311 Indent();
312 }
313 }
314}
315
316void DeclPrinter::prettyPrintPragmas(Decl *D) {
317 if (Policy.PolishForDeclaration)
318 return;
319
320 PrintOpenACCRoutineOnLambda(D);
321
322 if (D->hasAttrs()) {
323 AttrVec &Attrs = D->getAttrs();
324 for (auto *A : Attrs) {
325 switch (A->getKind()) {
326#define ATTR(X)
327#define PRAGMA_SPELLING_ATTR(X) case attr::X:
328#include "clang/Basic/AttrList.inc"
329 A->printPretty(OS&: Out, Policy);
330 Indent();
331 break;
332 default:
333 break;
334 }
335 }
336 }
337}
338
339void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) {
340 // Normally, a PackExpansionType is written as T[3]... (for instance, as a
341 // template argument), but if it is the type of a declaration, the ellipsis
342 // is placed before the name being declared.
343 if (auto *PET = T->getAs<PackExpansionType>()) {
344 Pack = true;
345 T = PET->getPattern();
346 }
347 T.print(OS&: Out, Policy, PlaceHolder: (Pack ? "..." : "") + DeclName, Indentation);
348}
349
350void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
351 this->Indent();
352 Decl::printGroup(Begin: Decls.data(), NumDecls: Decls.size(), Out, Policy, Indentation);
353 Out << ";\n";
354 Decls.clear();
355
356}
357
358void DeclPrinter::Print(AccessSpecifier AS) {
359 const auto AccessSpelling = getAccessSpelling(AS);
360 if (AccessSpelling.empty())
361 llvm_unreachable("No access specifier!");
362 Out << AccessSpelling;
363}
364
365void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl,
366 std::string &Proto) {
367 bool HasInitializerList = false;
368 for (const auto *BMInitializer : CDecl->inits()) {
369 if (BMInitializer->isInClassMemberInitializer())
370 continue;
371 if (!BMInitializer->isWritten())
372 continue;
373
374 if (!HasInitializerList) {
375 Proto += " : ";
376 Out << Proto;
377 Proto.clear();
378 HasInitializerList = true;
379 } else
380 Out << ", ";
381
382 if (BMInitializer->isAnyMemberInitializer()) {
383 FieldDecl *FD = BMInitializer->getAnyMember();
384 Out << *FD;
385 } else if (BMInitializer->isDelegatingInitializer()) {
386 Out << CDecl->getNameAsString();
387 } else {
388 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
389 }
390
391 if (Expr *Init = BMInitializer->getInit()) {
392 bool OutParens = !isa<InitListExpr>(Val: Init);
393
394 if (OutParens)
395 Out << "(";
396
397 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Val: Init))
398 Init = Tmp->getSubExpr();
399
400 Init = Init->IgnoreParens();
401
402 Expr *SimpleInit = nullptr;
403 Expr **Args = nullptr;
404 unsigned NumArgs = 0;
405 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Val: Init)) {
406 Args = ParenList->getExprs();
407 NumArgs = ParenList->getNumExprs();
408 } else if (CXXConstructExpr *Construct =
409 dyn_cast<CXXConstructExpr>(Val: Init)) {
410 Args = Construct->getArgs();
411 NumArgs = Construct->getNumArgs();
412 } else
413 SimpleInit = Init;
414
415 if (SimpleInit)
416 SimpleInit->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation, NewlineSymbol: "\n",
417 Context: &Context);
418 else {
419 for (unsigned I = 0; I != NumArgs; ++I) {
420 assert(Args[I] != nullptr && "Expected non-null Expr");
421 if (isa<CXXDefaultArgExpr>(Val: Args[I]))
422 break;
423
424 if (I)
425 Out << ", ";
426 Args[I]->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation, NewlineSymbol: "\n",
427 Context: &Context);
428 }
429 }
430
431 if (OutParens)
432 Out << ")";
433 } else {
434 Out << "()";
435 }
436
437 if (BMInitializer->isPackExpansion())
438 Out << "...";
439 }
440}
441
442//----------------------------------------------------------------------------
443// Common C declarations
444//----------------------------------------------------------------------------
445
446void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
447 if (Policy.TerseOutput)
448 return;
449
450 if (Indent)
451 Indentation += Policy.Indentation;
452
453 SmallVector<Decl*, 2> Decls;
454 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
455 D != DEnd; ++D) {
456
457 // Don't print ObjCIvarDecls, as they are printed when visiting the
458 // containing ObjCInterfaceDecl.
459 if (isa<ObjCIvarDecl>(Val: *D))
460 continue;
461
462 // Skip over implicit declarations in pretty-printing mode.
463 if (D->isImplicit())
464 continue;
465
466 // Don't print implicit specializations, as they are printed when visiting
467 // corresponding templates.
468 if (auto FD = dyn_cast<FunctionDecl>(Val: *D))
469 if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation &&
470 !isa<ClassTemplateSpecializationDecl>(Val: DC))
471 continue;
472
473 // The next bits of code handle stuff like "struct {int x;} a,b"; we're
474 // forced to merge the declarations because there's no other way to
475 // refer to the struct in question. When that struct is named instead, we
476 // also need to merge to avoid splitting off a stand-alone struct
477 // declaration that produces the warning ext_no_declarators in some
478 // contexts.
479 //
480 // This limited merging is safe without a bunch of other checks because it
481 // only merges declarations directly referring to the tag, not typedefs.
482 //
483 // Check whether the current declaration should be grouped with a previous
484 // non-free-standing tag declaration.
485 QualType CurDeclType = getDeclType(D: *D);
486 if (!Decls.empty() && !CurDeclType.isNull()) {
487 QualType BaseType = GetBaseType(T: CurDeclType);
488 if (const auto *TT = dyn_cast_or_null<TagType>(Val&: BaseType);
489 TT && TT->isTagOwned()) {
490 if (TT->getDecl() == Decls[0]) {
491 Decls.push_back(Elt: *D);
492 continue;
493 }
494 }
495 }
496
497 // If we have a merged group waiting to be handled, handle it now.
498 if (!Decls.empty())
499 ProcessDeclGroup(Decls);
500
501 // If the current declaration is not a free standing declaration, save it
502 // so we can merge it with the subsequent declaration(s) using it.
503 if (isa<TagDecl>(Val: *D) && !cast<TagDecl>(Val: *D)->isFreeStanding()) {
504 Decls.push_back(Elt: *D);
505 continue;
506 }
507
508 if (isa<AccessSpecDecl>(Val: *D)) {
509 Indentation -= Policy.Indentation;
510 this->Indent();
511 Print(AS: D->getAccess());
512 Out << ":\n";
513 Indentation += Policy.Indentation;
514 continue;
515 }
516
517 this->Indent();
518 Visit(D: *D);
519
520 // FIXME: Need to be able to tell the DeclPrinter when
521 const char *Terminator = nullptr;
522 if (isa<OMPThreadPrivateDecl>(Val: *D) || isa<OMPDeclareReductionDecl>(Val: *D) ||
523 isa<OMPDeclareMapperDecl>(Val: *D) || isa<OMPRequiresDecl>(Val: *D) ||
524 isa<OMPAllocateDecl>(Val: *D))
525 Terminator = nullptr;
526 else if (isa<OpenACCDeclareDecl, OpenACCRoutineDecl>(Val: *D))
527 Terminator = nullptr;
528 else if (isa<ObjCMethodDecl>(Val: *D) && cast<ObjCMethodDecl>(Val: *D)->hasBody())
529 Terminator = nullptr;
530 else if (auto FD = dyn_cast<FunctionDecl>(Val: *D)) {
531 if (FD->doesThisDeclarationHaveABody() && !FD->isDefaulted())
532 Terminator = nullptr;
533 else
534 Terminator = ";";
535 } else if (auto TD = dyn_cast<FunctionTemplateDecl>(Val: *D)) {
536 if (TD->getTemplatedDecl()->doesThisDeclarationHaveABody())
537 Terminator = nullptr;
538 else
539 Terminator = ";";
540 } else if (isa<NamespaceDecl, LinkageSpecDecl, ObjCImplementationDecl,
541 ObjCInterfaceDecl, ObjCProtocolDecl, ObjCCategoryImplDecl,
542 ObjCCategoryDecl, HLSLBufferDecl>(Val: *D))
543 Terminator = nullptr;
544 else if (isa<EnumConstantDecl>(Val: *D)) {
545 DeclContext::decl_iterator Next = D;
546 ++Next;
547 if (Next != DEnd)
548 Terminator = ",";
549 } else
550 Terminator = ";";
551
552 if (Terminator)
553 Out << Terminator;
554 if (!Policy.TerseOutput &&
555 ((isa<FunctionDecl>(Val: *D) &&
556 cast<FunctionDecl>(Val: *D)->doesThisDeclarationHaveABody()) ||
557 (isa<FunctionTemplateDecl>(Val: *D) &&
558 cast<FunctionTemplateDecl>(Val: *D)->getTemplatedDecl()->doesThisDeclarationHaveABody())))
559 ; // StmtPrinter already added '\n' after CompoundStmt.
560 else
561 Out << "\n";
562
563 // Declare target attribute is special one, natural spelling for the pragma
564 // assumes "ending" construct so print it here.
565 if (D->hasAttr<OMPDeclareTargetDeclAttr>())
566 Out << "#pragma omp end declare target\n";
567 }
568
569 if (!Decls.empty())
570 ProcessDeclGroup(Decls);
571
572 if (Indent)
573 Indentation -= Policy.Indentation;
574}
575
576void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
577 VisitDeclContext(DC: D, Indent: false);
578}
579
580void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
581 if (!Policy.SuppressSpecifiers) {
582 Out << "typedef ";
583
584 if (D->isModulePrivate())
585 Out << "__module_private__ ";
586 }
587 QualType Ty = D->getTypeSourceInfo()->getType();
588 Ty.print(OS&: Out, Policy, PlaceHolder: D->getName(), Indentation);
589
590 if (std::optional<std::string> Attrs = prettyPrintAttributes(D))
591 Out << ' ' << *Attrs;
592}
593
594void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
595 Out << "using " << *D;
596 if (std::optional<std::string> Attrs = prettyPrintAttributes(D))
597 Out << ' ' << *Attrs;
598 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
599}
600
601void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
602 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
603 Out << "__module_private__ ";
604 Out << "enum";
605 if (D->isScoped()) {
606 if (D->isScopedUsingClassTag())
607 Out << " class";
608 else
609 Out << " struct";
610 }
611
612 if (std::optional<std::string> Attrs = prettyPrintAttributes(D))
613 Out << ' ' << *Attrs;
614
615 if (D->getDeclName())
616 Out << ' ' << D->getDeclName();
617
618 if (D->isFixed())
619 Out << " : " << D->getIntegerType().stream(Policy);
620
621 if (D->isCompleteDefinition()) {
622 Out << " {\n";
623 VisitDeclContext(DC: D);
624 Indent() << "}";
625 }
626}
627
628void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
629 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
630 Out << "__module_private__ ";
631 Out << D->getKindName();
632
633 if (std::optional<std::string> Attrs = prettyPrintAttributes(D))
634 Out << ' ' << *Attrs;
635
636 if (D->getIdentifier())
637 Out << ' ' << *D;
638
639 if (D->isCompleteDefinition()) {
640 Out << " {\n";
641 VisitDeclContext(DC: D);
642 Indent() << "}";
643 }
644}
645
646void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
647 Out << *D;
648 if (std::optional<std::string> Attrs = prettyPrintAttributes(D))
649 Out << ' ' << *Attrs;
650 if (Expr *Init = D->getInitExpr()) {
651 Out << " = ";
652 Init->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation, NewlineSymbol: "\n", Context: &Context);
653 }
654}
655
656static void printExplicitSpecifier(ExplicitSpecifier ES, llvm::raw_ostream &Out,
657 PrintingPolicy &Policy, unsigned Indentation,
658 const ASTContext &Context) {
659 std::string Proto = "explicit";
660 llvm::raw_string_ostream EOut(Proto);
661 if (ES.getExpr()) {
662 EOut << "(";
663 ES.getExpr()->printPretty(OS&: EOut, Helper: nullptr, Policy, Indentation, NewlineSymbol: "\n",
664 Context: &Context);
665 EOut << ")";
666 }
667 EOut << " ";
668 Out << Proto;
669}
670
671void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
672 if (!D->getDescribedFunctionTemplate() &&
673 !D->isFunctionTemplateSpecialization()) {
674 prettyPrintPragmas(D);
675 if (std::optional<std::string> Attrs =
676 prettyPrintAttributes(D, Pos: AttrPosAsWritten::Left))
677 Out << *Attrs << ' ';
678 }
679
680 if (D->isFunctionTemplateSpecialization())
681 Out << "template<> ";
682 else if (!D->getDescribedFunctionTemplate()) {
683 for (TemplateParameterList *TPL : D->getTemplateParameterLists())
684 printTemplateParameters(Params: TPL);
685 }
686
687 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(Val: D);
688 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(Val: D);
689 CXXDeductionGuideDecl *GuideDecl = dyn_cast<CXXDeductionGuideDecl>(Val: D);
690 if (!Policy.SuppressSpecifiers) {
691 switch (D->getStorageClass()) {
692 case SC_None: break;
693 case SC_Extern: Out << "extern "; break;
694 case SC_Static: Out << "static "; break;
695 case SC_PrivateExtern: Out << "__private_extern__ "; break;
696 case SC_Auto: case SC_Register:
697 llvm_unreachable("invalid for functions");
698 }
699
700 if (D->isInlineSpecified()) Out << "inline ";
701 if (D->isVirtualAsWritten()) Out << "virtual ";
702 if (D->isModulePrivate()) Out << "__module_private__ ";
703 if (D->isConstexprSpecified() && !D->isExplicitlyDefaulted())
704 Out << "constexpr ";
705 if (D->isConsteval()) Out << "consteval ";
706 else if (D->isImmediateFunction())
707 Out << "immediate ";
708 ExplicitSpecifier ExplicitSpec = ExplicitSpecifier::getFromDecl(Function: D);
709 if (ExplicitSpec.isSpecified())
710 printExplicitSpecifier(ES: ExplicitSpec, Out, Policy, Indentation, Context);
711 }
712
713 PrintingPolicy SubPolicy(Policy);
714 SubPolicy.SuppressSpecifiers = false;
715 std::string Proto;
716
717 if (Policy.FullyQualifiedName) {
718 Proto += D->getQualifiedNameAsString();
719 } else {
720 llvm::raw_string_ostream OS(Proto);
721 if (!Policy.SuppressScope)
722 D->getQualifier().print(OS, Policy);
723 D->getNameInfo().printName(OS, Policy);
724 }
725
726 if (GuideDecl)
727 Proto = GuideDecl->getDeducedTemplate()->getDeclName().getAsString();
728 if (D->isFunctionTemplateSpecialization()) {
729 llvm::raw_string_ostream POut(Proto);
730 DeclPrinter TArgPrinter(POut, SubPolicy, Context, Indentation);
731 const auto *TArgAsWritten = D->getTemplateSpecializationArgsAsWritten();
732 if (TArgAsWritten && !Policy.PrintAsCanonical)
733 TArgPrinter.printTemplateArguments(Args: TArgAsWritten->arguments(), Params: nullptr);
734 else if (const TemplateArgumentList *TArgs =
735 D->getTemplateSpecializationArgs())
736 TArgPrinter.printTemplateArguments(Args: TArgs->asArray(), Params: nullptr);
737 }
738
739 QualType Ty = D->getType();
740 while (const ParenType *PT = dyn_cast<ParenType>(Val&: Ty)) {
741 Proto = '(' + Proto + ')';
742 Ty = PT->getInnerType();
743 }
744
745 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
746 const FunctionProtoType *FT = nullptr;
747 if (D->hasWrittenPrototype())
748 FT = dyn_cast<FunctionProtoType>(Val: AFT);
749
750 Proto += "(";
751 if (FT) {
752 llvm::raw_string_ostream POut(Proto);
753 DeclPrinter ParamPrinter(POut, SubPolicy, Context, Indentation);
754 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
755 if (i) POut << ", ";
756 ParamPrinter.VisitParmVarDecl(D: D->getParamDecl(i));
757 }
758
759 if (FT->isVariadic()) {
760 if (D->getNumParams()) POut << ", ";
761 POut << "...";
762 } else if (!D->getNumParams() && !Context.getLangOpts().CPlusPlus) {
763 // The function has a prototype, so it needs to retain the prototype
764 // in C.
765 POut << "void";
766 }
767 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
768 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
769 if (i)
770 Proto += ", ";
771 Proto += D->getParamDecl(i)->getNameAsString();
772 }
773 }
774
775 Proto += ")";
776
777 if (FT) {
778 if (FT->isConst())
779 Proto += " const";
780 if (FT->isVolatile())
781 Proto += " volatile";
782 if (FT->isRestrict())
783 Proto += " restrict";
784
785 switch (FT->getRefQualifier()) {
786 case RQ_None:
787 break;
788 case RQ_LValue:
789 Proto += " &";
790 break;
791 case RQ_RValue:
792 Proto += " &&";
793 break;
794 }
795 }
796
797 if (FT && FT->hasDynamicExceptionSpec()) {
798 Proto += " throw(";
799 if (FT->getExceptionSpecType() == EST_MSAny)
800 Proto += "...";
801 else
802 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
803 if (I)
804 Proto += ", ";
805
806 Proto += FT->getExceptionType(i: I).getAsString(Policy: SubPolicy);
807 }
808 Proto += ")";
809 } else if (FT && isNoexceptExceptionSpec(ESpecType: FT->getExceptionSpecType())) {
810 Proto += " noexcept";
811 if (isComputedNoexcept(ESpecType: FT->getExceptionSpecType())) {
812 Proto += "(";
813 llvm::raw_string_ostream EOut(Proto);
814 FT->getNoexceptExpr()->printPretty(OS&: EOut, Helper: nullptr, Policy: SubPolicy,
815 Indentation, NewlineSymbol: "\n", Context: &Context);
816 Proto += ")";
817 }
818 }
819
820 if (CDecl) {
821 if (!Policy.TerseOutput)
822 PrintConstructorInitializers(CDecl, Proto);
823 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(Val: D)) {
824 if (FT && FT->hasTrailingReturn()) {
825 if (!GuideDecl)
826 Out << "auto ";
827 Out << Proto << " -> ";
828 Proto.clear();
829 }
830 AFT->getReturnType().print(OS&: Out, Policy, PlaceHolder: Proto);
831 Proto.clear();
832 }
833 Out << Proto;
834
835 if (const AssociatedConstraint &TrailingRequiresClause =
836 D->getTrailingRequiresClause()) {
837 Out << " requires ";
838 // FIXME: The printer could support printing expressions and types as if
839 // expanded by an index. Pass in the ArgumentPackSubstitutionIndex when
840 // that's supported.
841 TrailingRequiresClause.ConstraintExpr->printPretty(
842 OS&: Out, Helper: nullptr, Policy: SubPolicy, Indentation, NewlineSymbol: "\n", Context: &Context);
843 }
844 } else {
845 Ty.print(OS&: Out, Policy, PlaceHolder: Proto);
846 }
847
848 if (std::optional<std::string> Attrs =
849 prettyPrintAttributes(D, Pos: AttrPosAsWritten::Right))
850 Out << ' ' << *Attrs;
851
852 if (D->isPureVirtual())
853 Out << " = 0";
854 else if (D->isDeletedAsWritten()) {
855 Out << " = delete";
856 if (const StringLiteral *M = D->getDeletedMessage()) {
857 Out << "(";
858 M->outputString(OS&: Out);
859 Out << ")";
860 }
861 } else if (D->isExplicitlyDefaulted())
862 Out << " = default";
863 else if (D->doesThisDeclarationHaveABody()) {
864 if (!Policy.TerseOutput) {
865 if (!D->hasPrototype() && D->getNumParams()) {
866 // This is a K&R function definition, so we need to print the
867 // parameters.
868 Out << '\n';
869 DeclPrinter ParamPrinter(Out, SubPolicy, Context, Indentation);
870 Indentation += Policy.Indentation;
871 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
872 Indent();
873 ParamPrinter.VisitParmVarDecl(D: D->getParamDecl(i));
874 Out << ";\n";
875 }
876 Indentation -= Policy.Indentation;
877 }
878
879 if (D->getBody())
880 D->getBody()->printPrettyControlled(OS&: Out, Helper: nullptr, Policy: SubPolicy, Indentation, NewlineSymbol: "\n",
881 Context: &Context);
882 } else {
883 if (!Policy.TerseOutput && isa<CXXConstructorDecl>(Val: *D))
884 Out << " {}";
885 }
886 }
887}
888
889void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
890 if (TypeSourceInfo *TSI = D->getFriendType()) {
891 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
892 for (unsigned i = 0; i < NumTPLists; ++i)
893 printTemplateParameters(Params: D->getFriendTypeTemplateParameterList(N: i));
894 Out << "friend ";
895 Out << TSI->getType().getAsString(Policy);
896 }
897 else if (FunctionDecl *FD =
898 dyn_cast<FunctionDecl>(Val: D->getFriendDecl())) {
899 Out << "friend ";
900 VisitFunctionDecl(D: FD);
901 }
902 else if (FunctionTemplateDecl *FTD =
903 dyn_cast<FunctionTemplateDecl>(Val: D->getFriendDecl())) {
904 Out << "friend ";
905 VisitFunctionTemplateDecl(D: FTD);
906 }
907 else if (ClassTemplateDecl *CTD =
908 dyn_cast<ClassTemplateDecl>(Val: D->getFriendDecl())) {
909 Out << "friend ";
910 VisitRedeclarableTemplateDecl(D: CTD);
911 }
912
913 if (D->isPackExpansion())
914 Out << "...";
915}
916
917void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
918 prettyPrintPragmas(D);
919 // FIXME: add printing of pragma attributes if required.
920 if (!Policy.SuppressSpecifiers && D->isMutable())
921 Out << "mutable ";
922 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
923 Out << "__module_private__ ";
924
925 Out << D->getASTContext().getUnqualifiedObjCPointerType(type: D->getType()).
926 stream(Policy, PlaceHolder: D->getName(), Indentation);
927
928 if (D->isBitField()) {
929 Out << " : ";
930 D->getBitWidth()->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation, NewlineSymbol: "\n",
931 Context: &Context);
932 }
933
934 Expr *Init = D->getInClassInitializer();
935 if (!Policy.SuppressInitializers && Init) {
936 if (D->getInClassInitStyle() == ICIS_ListInit)
937 Out << " ";
938 else
939 Out << " = ";
940 Init->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation, NewlineSymbol: "\n", Context: &Context);
941 }
942 if (std::optional<std::string> Attrs = prettyPrintAttributes(D))
943 Out << ' ' << *Attrs;
944}
945
946void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
947 Out << *D << ":";
948}
949
950void DeclPrinter::VisitVarDecl(VarDecl *D) {
951 prettyPrintPragmas(D);
952
953 if (std::optional<std::string> Attrs =
954 prettyPrintAttributes(D, Pos: AttrPosAsWritten::Left))
955 Out << *Attrs << ' ';
956
957 if (const auto *Param = dyn_cast<ParmVarDecl>(Val: D);
958 Param && Param->isExplicitObjectParameter())
959 Out << "this ";
960
961 QualType T = D->getTypeSourceInfo()
962 ? D->getTypeSourceInfo()->getType()
963 : D->getASTContext().getUnqualifiedObjCPointerType(type: D->getType());
964
965 if (!Policy.SuppressSpecifiers) {
966 StorageClass SC = D->getStorageClass();
967 if (SC != SC_None)
968 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
969
970 switch (D->getTSCSpec()) {
971 case TSCS_unspecified:
972 break;
973 case TSCS___thread:
974 Out << "__thread ";
975 break;
976 case TSCS__Thread_local:
977 Out << "_Thread_local ";
978 break;
979 case TSCS_thread_local:
980 Out << "thread_local ";
981 break;
982 }
983
984 if (D->isModulePrivate())
985 Out << "__module_private__ ";
986
987 if (D->isConstexpr()) {
988 Out << "constexpr ";
989 T.removeLocalConst();
990 }
991 }
992
993 printDeclType(T, DeclName: (isa<ParmVarDecl>(Val: D) && Policy.CleanUglifiedParameters &&
994 D->getIdentifier())
995 ? D->getIdentifier()->deuglifiedName()
996 : D->getName());
997
998 if (std::optional<std::string> Attrs =
999 prettyPrintAttributes(D, Pos: AttrPosAsWritten::Right))
1000 Out << ' ' << *Attrs;
1001
1002 Expr *Init = D->getInit();
1003 if (!Policy.SuppressInitializers && Init) {
1004 bool ImplicitInit = false;
1005 if (D->isCXXForRangeDecl()) {
1006 // FIXME: We should print the range expression instead.
1007 ImplicitInit = true;
1008 } else if (CXXConstructExpr *Construct =
1009 dyn_cast<CXXConstructExpr>(Val: Init->IgnoreImplicit())) {
1010 if (D->getInitStyle() == VarDecl::CallInit &&
1011 !Construct->isListInitialization()) {
1012 ImplicitInit = Construct->getNumArgs() == 0 ||
1013 Construct->getArg(Arg: 0)->isDefaultArgument();
1014 }
1015 }
1016 if (!ImplicitInit) {
1017 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Val: Init))
1018 Out << "(";
1019 else if (D->getInitStyle() == VarDecl::CInit) {
1020 Out << " = ";
1021 }
1022 PrintingPolicy SubPolicy(Policy);
1023 SubPolicy.SuppressSpecifiers = false;
1024 Init->printPretty(OS&: Out, Helper: nullptr, Policy: SubPolicy, Indentation, NewlineSymbol: "\n", Context: &Context);
1025 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Val: Init))
1026 Out << ")";
1027 }
1028 }
1029}
1030
1031void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
1032 VisitVarDecl(D);
1033}
1034
1035void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
1036 Out << "__asm (";
1037 D->getAsmStringExpr()->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation, NewlineSymbol: "\n",
1038 Context: &Context);
1039 Out << ")";
1040}
1041
1042void DeclPrinter::VisitTopLevelStmtDecl(TopLevelStmtDecl *D) {
1043 assert(D->getStmt());
1044 D->getStmt()->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation, NewlineSymbol: "\n", Context: &Context);
1045}
1046
1047void DeclPrinter::VisitImportDecl(ImportDecl *D) {
1048 Out << "@import " << D->getImportedModule()->getFullModuleName()
1049 << ";\n";
1050}
1051
1052void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1053 Out << "static_assert(";
1054 D->getAssertExpr()->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation, NewlineSymbol: "\n",
1055 Context: &Context);
1056 if (Expr *E = D->getMessage()) {
1057 Out << ", ";
1058 E->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation, NewlineSymbol: "\n", Context: &Context);
1059 }
1060 Out << ")";
1061}
1062
1063//----------------------------------------------------------------------------
1064// C++ declarations
1065//----------------------------------------------------------------------------
1066void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
1067 if (D->isInline())
1068 Out << "inline ";
1069
1070 Out << "namespace ";
1071 if (D->getDeclName())
1072 Out << D->getDeclName() << ' ';
1073 Out << "{\n";
1074
1075 VisitDeclContext(DC: D);
1076 Indent() << "}";
1077}
1078
1079void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1080 Out << "using namespace ";
1081 D->getQualifier().print(OS&: Out, Policy);
1082 Out << *D->getNominatedNamespaceAsWritten();
1083}
1084
1085void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1086 Out << "namespace " << *D << " = ";
1087 D->getQualifier().print(OS&: Out, Policy);
1088 Out << *D->getAliasedNamespace();
1089}
1090
1091void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
1092 if (std::optional<std::string> Attrs = prettyPrintAttributes(D))
1093 Out << *Attrs;
1094}
1095
1096void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
1097 // FIXME: add printing of pragma attributes if required.
1098 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
1099 Out << "__module_private__ ";
1100
1101 Out << D->getKindName() << ' ';
1102
1103 if (std::optional<std::string> Attrs =
1104 prettyPrintAttributes(D, Pos: AttrPosAsWritten::Left))
1105 Out << *Attrs << ' ';
1106
1107 if (D->getIdentifier()) {
1108 D->getQualifier().print(OS&: Out, Policy);
1109 Out << *D;
1110
1111 if (auto *S = dyn_cast<ClassTemplateSpecializationDecl>(Val: D)) {
1112 const TemplateParameterList *TParams =
1113 S->getSpecializedTemplate()->getTemplateParameters();
1114 const ASTTemplateArgumentListInfo *TArgAsWritten =
1115 S->getTemplateArgsAsWritten();
1116 if (TArgAsWritten && !Policy.PrintAsCanonical)
1117 printTemplateArguments(Args: TArgAsWritten->arguments(), Params: TParams);
1118 else
1119 printTemplateArguments(Args: S->getTemplateArgs().asArray(), Params: TParams);
1120 }
1121 }
1122
1123 if (std::optional<std::string> Attrs =
1124 prettyPrintAttributes(D, Pos: AttrPosAsWritten::Right))
1125 Out << ' ' << *Attrs;
1126
1127 if (D->isCompleteDefinition()) {
1128 Out << ' ';
1129 // Print the base classes
1130 if (D->getNumBases()) {
1131 Out << ": ";
1132 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
1133 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
1134 if (Base != D->bases_begin())
1135 Out << ", ";
1136
1137 if (Base->isVirtual())
1138 Out << "virtual ";
1139
1140 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
1141 if (AS != AS_none) {
1142 Print(AS);
1143 Out << " ";
1144 }
1145 Out << Base->getType().getAsString(Policy);
1146
1147 if (Base->isPackExpansion())
1148 Out << "...";
1149 }
1150 Out << ' ';
1151 }
1152
1153 // Print the class definition
1154 // FIXME: Doesn't print access specifiers, e.g., "public:"
1155 if (Policy.TerseOutput) {
1156 Out << "{}";
1157 } else {
1158 Out << "{\n";
1159 VisitDeclContext(DC: D);
1160 Indent() << "}";
1161 }
1162 }
1163}
1164
1165void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1166 const char *l;
1167 if (D->getLanguage() == LinkageSpecLanguageIDs::C)
1168 l = "C";
1169 else {
1170 assert(D->getLanguage() == LinkageSpecLanguageIDs::CXX &&
1171 "unknown language in linkage specification");
1172 l = "C++";
1173 }
1174
1175 Out << "extern \"" << l << "\" ";
1176 if (D->hasBraces()) {
1177 Out << "{\n";
1178 VisitDeclContext(DC: D);
1179 Indent() << "}";
1180 } else
1181 Visit(D: *D->decls_begin());
1182}
1183
1184void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params,
1185 bool OmitTemplateKW) {
1186 assert(Params);
1187
1188 // Don't print invented template parameter lists.
1189 if (!Params->empty() && Params->getParam(Idx: 0)->isImplicit())
1190 return;
1191
1192 if (!OmitTemplateKW)
1193 Out << "template ";
1194 Out << '<';
1195
1196 bool NeedComma = false;
1197 for (const Decl *Param : *Params) {
1198 if (Param->isImplicit())
1199 continue;
1200
1201 if (NeedComma)
1202 Out << ", ";
1203 else
1204 NeedComma = true;
1205
1206 if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Val: Param)) {
1207 VisitTemplateTypeParmDecl(TTP);
1208 } else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Val: Param)) {
1209 VisitNonTypeTemplateParmDecl(NTTP);
1210 } else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Val: Param)) {
1211 VisitTemplateTemplateParmDecl(TTPD);
1212 }
1213 }
1214
1215 Out << '>';
1216
1217 if (const Expr *RequiresClause = Params->getRequiresClause()) {
1218 Out << " requires ";
1219 RequiresClause->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation, NewlineSymbol: "\n",
1220 Context: &Context);
1221 }
1222
1223 if (!OmitTemplateKW)
1224 Out << ' ';
1225}
1226
1227void DeclPrinter::printTemplateArguments(ArrayRef<TemplateArgument> Args,
1228 const TemplateParameterList *Params) {
1229 Out << "<";
1230 for (size_t I = 0, E = Args.size(); I < E; ++I) {
1231 if (I)
1232 Out << ", ";
1233 if (!Params)
1234 Args[I].print(Policy, Out, /*IncludeType*/ true);
1235 else
1236 Args[I].print(Policy, Out,
1237 IncludeType: TemplateParameterList::shouldIncludeTypeForArgument(
1238 Policy, TPL: Params, Idx: I));
1239 }
1240 Out << ">";
1241}
1242
1243void DeclPrinter::printTemplateArguments(ArrayRef<TemplateArgumentLoc> Args,
1244 const TemplateParameterList *Params) {
1245 Out << "<";
1246 for (size_t I = 0, E = Args.size(); I < E; ++I) {
1247 if (I)
1248 Out << ", ";
1249 if (!Params)
1250 Args[I].getArgument().print(Policy, Out, /*IncludeType*/ true);
1251 else
1252 Args[I].getArgument().print(
1253 Policy, Out,
1254 IncludeType: TemplateParameterList::shouldIncludeTypeForArgument(Policy, TPL: Params,
1255 Idx: I));
1256 }
1257 Out << ">";
1258}
1259
1260void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
1261 printTemplateParameters(Params: D->getTemplateParameters());
1262
1263 if (const TemplateTemplateParmDecl *TTP =
1264 dyn_cast<TemplateTemplateParmDecl>(Val: D)) {
1265 if (TTP->wasDeclaredWithTypename())
1266 Out << "typename";
1267 else
1268 Out << "class";
1269
1270 if (TTP->isParameterPack())
1271 Out << " ...";
1272 else if (TTP->getDeclName())
1273 Out << ' ';
1274
1275 if (TTP->getDeclName()) {
1276 if (Policy.CleanUglifiedParameters && TTP->getIdentifier())
1277 Out << TTP->getIdentifier()->deuglifiedName();
1278 else
1279 Out << TTP->getDeclName();
1280 }
1281 } else if (auto *TD = D->getTemplatedDecl())
1282 Visit(D: TD);
1283 else if (const auto *Concept = dyn_cast<ConceptDecl>(Val: D)) {
1284 Out << "concept " << Concept->getName() << " = " ;
1285 Concept->getConstraintExpr()->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation,
1286 NewlineSymbol: "\n", Context: &Context);
1287 }
1288}
1289
1290void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
1291 prettyPrintPragmas(D: D->getTemplatedDecl());
1292 // Print any leading template parameter lists.
1293 if (const FunctionDecl *FD = D->getTemplatedDecl())
1294 for (TemplateParameterList *TPL : FD->getTemplateParameterLists())
1295 printTemplateParameters(Params: TPL);
1296 VisitRedeclarableTemplateDecl(D);
1297 // Declare target attribute is special one, natural spelling for the pragma
1298 // assumes "ending" construct so print it here.
1299 if (D->getTemplatedDecl()->hasAttr<OMPDeclareTargetDeclAttr>())
1300 Out << "#pragma omp end declare target\n";
1301
1302 // Never print "instantiations" for deduction guides (they don't really
1303 // have them).
1304 if (PrintInstantiation &&
1305 !isa<CXXDeductionGuideDecl>(Val: D->getTemplatedDecl())) {
1306 FunctionDecl *PrevDecl = D->getTemplatedDecl();
1307 const FunctionDecl *Def;
1308 if (PrevDecl->isDefined(Definition&: Def) && Def != PrevDecl)
1309 return;
1310 for (auto *I : D->specializations())
1311 if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) {
1312 if (!PrevDecl->isThisDeclarationADefinition())
1313 Out << ";\n";
1314 Indent();
1315 prettyPrintPragmas(D: I);
1316 Visit(D: I);
1317 }
1318 }
1319}
1320
1321void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
1322 VisitRedeclarableTemplateDecl(D);
1323
1324 if (PrintInstantiation) {
1325 for (auto *I : D->specializations())
1326 if (I->getSpecializationKind() == TSK_ImplicitInstantiation) {
1327 if (D->isThisDeclarationADefinition())
1328 Out << ";";
1329 Out << "\n";
1330 Indent();
1331 Visit(D: I);
1332 }
1333 }
1334}
1335
1336void DeclPrinter::VisitExplicitInstantiationDecl(ExplicitInstantiationDecl *D) {
1337 if (D->isExternTemplate())
1338 Out << "extern ";
1339 Out << "template ";
1340
1341 NamedDecl *Spec = D->getSpecialization();
1342
1343 // Build the qualified name with template arguments.
1344 std::string Name;
1345 llvm::raw_string_ostream NameOS(Name);
1346 if (D->getQualifierLoc())
1347 D->getQualifierLoc().getNestedNameSpecifier().print(OS&: NameOS, Policy);
1348 Spec->printName(OS&: NameOS, Policy);
1349 if (auto NumArgs = D->getNumTemplateArgs(); NumArgs && *NumArgs > 0) {
1350 SmallVector<TemplateArgumentLoc, 4> Args;
1351 for (unsigned I = 0; I < *NumArgs; ++I)
1352 Args.push_back(Elt: D->getTemplateArg(I));
1353 printTemplateArgumentList(OS&: NameOS, Args, Policy);
1354 }
1355
1356 if (auto *RD = dyn_cast<RecordDecl>(Val: Spec)) {
1357 Out << RD->getKindName() << " " << Name;
1358 } else if (auto *FD = dyn_cast<FunctionDecl>(Val: Spec)) {
1359 FD->getReturnType().print(OS&: Out, Policy);
1360 Out << " " << Name << "(";
1361 llvm::ListSeparator LS;
1362 for (const ParmVarDecl *P : FD->parameters()) {
1363 Out << LS;
1364 P->print(Out, Policy);
1365 }
1366 if (FD->isVariadic()) {
1367 Out << LS;
1368 Out << "...";
1369 }
1370 Out << ")";
1371 } else if (auto *TSI = D->getTypeAsWritten()) {
1372 TSI->getType().print(OS&: Out, Policy, PlaceHolder: Name);
1373 } else {
1374 llvm_unreachable("unexpected specialization kind");
1375 }
1376}
1377
1378void DeclPrinter::VisitClassTemplateSpecializationDecl(
1379 ClassTemplateSpecializationDecl *D) {
1380 Out << "template<> ";
1381 VisitCXXRecordDecl(D);
1382}
1383
1384void DeclPrinter::VisitClassTemplatePartialSpecializationDecl(
1385 ClassTemplatePartialSpecializationDecl *D) {
1386 printTemplateParameters(Params: D->getTemplateParameters());
1387 VisitCXXRecordDecl(D);
1388}
1389
1390//----------------------------------------------------------------------------
1391// Objective-C declarations
1392//----------------------------------------------------------------------------
1393
1394void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
1395 Decl::ObjCDeclQualifier Quals,
1396 QualType T) {
1397 Out << '(';
1398 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
1399 Out << "in ";
1400 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
1401 Out << "inout ";
1402 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1403 Out << "out ";
1404 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1405 Out << "bycopy ";
1406 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1407 Out << "byref ";
1408 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1409 Out << "oneway ";
1410 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
1411 if (auto nullability = AttributedType::stripOuterNullability(T))
1412 Out << getNullabilitySpelling(kind: *nullability, isContextSensitive: true) << ' ';
1413 }
1414
1415 Out << Ctx.getUnqualifiedObjCPointerType(type: T).getAsString(Policy);
1416 Out << ')';
1417}
1418
1419void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1420 Out << "<";
1421 unsigned First = true;
1422 for (auto *Param : *Params) {
1423 if (First) {
1424 First = false;
1425 } else {
1426 Out << ", ";
1427 }
1428
1429 switch (Param->getVariance()) {
1430 case ObjCTypeParamVariance::Invariant:
1431 break;
1432
1433 case ObjCTypeParamVariance::Covariant:
1434 Out << "__covariant ";
1435 break;
1436
1437 case ObjCTypeParamVariance::Contravariant:
1438 Out << "__contravariant ";
1439 break;
1440 }
1441
1442 Out << Param->getDeclName();
1443
1444 if (Param->hasExplicitBound()) {
1445 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1446 }
1447 }
1448 Out << ">";
1449}
1450
1451void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1452 if (OMD->isInstanceMethod())
1453 Out << "- ";
1454 else
1455 Out << "+ ";
1456 if (!OMD->getReturnType().isNull()) {
1457 PrintObjCMethodType(Ctx&: OMD->getASTContext(), Quals: OMD->getObjCDeclQualifier(),
1458 T: OMD->getReturnType());
1459 }
1460
1461 std::string name = OMD->getSelector().getAsString();
1462 std::string::size_type pos, lastPos = 0;
1463 for (const auto *PI : OMD->parameters()) {
1464 // FIXME: selector is missing here!
1465 pos = name.find_first_of(c: ':', pos: lastPos);
1466 if (lastPos != 0)
1467 Out << " ";
1468 Out << name.substr(pos: lastPos, n: pos - lastPos) << ':';
1469 PrintObjCMethodType(Ctx&: OMD->getASTContext(),
1470 Quals: PI->getObjCDeclQualifier(),
1471 T: PI->getType());
1472 Out << *PI;
1473 lastPos = pos + 1;
1474 }
1475
1476 if (OMD->parameters().empty())
1477 Out << name;
1478
1479 if (OMD->isVariadic())
1480 Out << ", ...";
1481
1482 if (std::optional<std::string> Attrs = prettyPrintAttributes(D: OMD))
1483 Out << ' ' << *Attrs;
1484
1485 if (OMD->getBody() && !Policy.TerseOutput) {
1486 Out << ' ';
1487 OMD->getBody()->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation, NewlineSymbol: "\n",
1488 Context: &Context);
1489 }
1490 else if (Policy.PolishForDeclaration)
1491 Out << ';';
1492}
1493
1494void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1495 std::string I = OID->getNameAsString();
1496 ObjCInterfaceDecl *SID = OID->getSuperClass();
1497
1498 bool eolnOut = false;
1499 if (SID)
1500 Out << "@implementation " << I << " : " << *SID;
1501 else
1502 Out << "@implementation " << I;
1503
1504 if (OID->ivar_size() > 0) {
1505 Out << "{\n";
1506 eolnOut = true;
1507 Indentation += Policy.Indentation;
1508 for (const auto *I : OID->ivars()) {
1509 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(type: I->getType()).
1510 getAsString(Policy) << ' ' << *I << ";\n";
1511 }
1512 Indentation -= Policy.Indentation;
1513 Out << "}\n";
1514 } else if (SID || !OID->decls().empty()) {
1515 Out << "\n";
1516 eolnOut = true;
1517 }
1518 VisitDeclContext(DC: OID, Indent: false);
1519 if (!eolnOut)
1520 Out << "\n";
1521 Out << "@end";
1522}
1523
1524void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1525 std::string I = OID->getNameAsString();
1526 ObjCInterfaceDecl *SID = OID->getSuperClass();
1527
1528 if (!OID->isThisDeclarationADefinition()) {
1529 Out << "@class " << I;
1530
1531 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1532 PrintObjCTypeParams(Params: TypeParams);
1533 }
1534
1535 Out << ";";
1536 return;
1537 }
1538 bool eolnOut = false;
1539 if (std::optional<std::string> Attrs = prettyPrintAttributes(D: OID))
1540 Out << *Attrs << "\n";
1541
1542 Out << "@interface " << I;
1543
1544 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1545 PrintObjCTypeParams(Params: TypeParams);
1546 }
1547
1548 if (SID)
1549 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
1550
1551 // Protocols?
1552 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1553 if (!Protocols.empty()) {
1554 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1555 E = Protocols.end(); I != E; ++I)
1556 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1557 Out << "> ";
1558 }
1559
1560 if (OID->ivar_size() > 0) {
1561 Out << "{\n";
1562 eolnOut = true;
1563 Indentation += Policy.Indentation;
1564 for (const auto *I : OID->ivars()) {
1565 Indent() << I->getASTContext()
1566 .getUnqualifiedObjCPointerType(type: I->getType())
1567 .getAsString(Policy) << ' ' << *I << ";\n";
1568 }
1569 Indentation -= Policy.Indentation;
1570 Out << "}\n";
1571 } else if (SID || !OID->decls().empty()) {
1572 Out << "\n";
1573 eolnOut = true;
1574 }
1575
1576 VisitDeclContext(DC: OID, Indent: false);
1577 if (!eolnOut)
1578 Out << "\n";
1579 Out << "@end";
1580 // FIXME: implement the rest...
1581}
1582
1583void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
1584 if (!PID->isThisDeclarationADefinition()) {
1585 Out << "@protocol " << *PID << ";\n";
1586 return;
1587 }
1588 // Protocols?
1589 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1590 if (!Protocols.empty()) {
1591 Out << "@protocol " << *PID;
1592 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1593 E = Protocols.end(); I != E; ++I)
1594 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1595 Out << ">\n";
1596 } else
1597 Out << "@protocol " << *PID << '\n';
1598 VisitDeclContext(DC: PID, Indent: false);
1599 Out << "@end";
1600}
1601
1602void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
1603 Out << "@implementation ";
1604 if (const auto *CID = PID->getClassInterface())
1605 Out << *CID;
1606 else
1607 Out << "<<error-type>>";
1608 Out << '(' << *PID << ")\n";
1609
1610 VisitDeclContext(DC: PID, Indent: false);
1611 Out << "@end";
1612 // FIXME: implement the rest...
1613}
1614
1615void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
1616 Out << "@interface ";
1617 if (const auto *CID = PID->getClassInterface())
1618 Out << *CID;
1619 else
1620 Out << "<<error-type>>";
1621 if (auto TypeParams = PID->getTypeParamList()) {
1622 PrintObjCTypeParams(Params: TypeParams);
1623 }
1624 Out << "(" << *PID << ")\n";
1625 if (PID->ivar_size() > 0) {
1626 Out << "{\n";
1627 Indentation += Policy.Indentation;
1628 for (const auto *I : PID->ivars())
1629 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(type: I->getType()).
1630 getAsString(Policy) << ' ' << *I << ";\n";
1631 Indentation -= Policy.Indentation;
1632 Out << "}\n";
1633 }
1634
1635 VisitDeclContext(DC: PID, Indent: false);
1636 Out << "@end";
1637
1638 // FIXME: implement the rest...
1639}
1640
1641void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
1642 Out << "@compatibility_alias " << *AID
1643 << ' ' << *AID->getClassInterface() << ";\n";
1644}
1645
1646/// PrintObjCPropertyDecl - print a property declaration.
1647///
1648/// Print attributes in the following order:
1649/// - class
1650/// - nonatomic | atomic
1651/// - assign | retain | strong | copy | weak | unsafe_unretained
1652/// - readwrite | readonly
1653/// - getter & setter
1654/// - nullability
1655void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1656 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1657 Out << "@required\n";
1658 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1659 Out << "@optional\n";
1660
1661 QualType T = PDecl->getType();
1662
1663 Out << "@property";
1664 if (PDecl->getPropertyAttributes() != ObjCPropertyAttribute::kind_noattr) {
1665 bool first = true;
1666 Out << "(";
1667 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_class) {
1668 Out << (first ? "" : ", ") << "class";
1669 first = false;
1670 }
1671
1672 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_direct) {
1673 Out << (first ? "" : ", ") << "direct";
1674 first = false;
1675 }
1676
1677 if (PDecl->getPropertyAttributes() &
1678 ObjCPropertyAttribute::kind_nonatomic) {
1679 Out << (first ? "" : ", ") << "nonatomic";
1680 first = false;
1681 }
1682 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_atomic) {
1683 Out << (first ? "" : ", ") << "atomic";
1684 first = false;
1685 }
1686
1687 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_assign) {
1688 Out << (first ? "" : ", ") << "assign";
1689 first = false;
1690 }
1691 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_retain) {
1692 Out << (first ? "" : ", ") << "retain";
1693 first = false;
1694 }
1695
1696 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_strong) {
1697 Out << (first ? "" : ", ") << "strong";
1698 first = false;
1699 }
1700 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_copy) {
1701 Out << (first ? "" : ", ") << "copy";
1702 first = false;
1703 }
1704 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_weak) {
1705 Out << (first ? "" : ", ") << "weak";
1706 first = false;
1707 }
1708 if (PDecl->getPropertyAttributes() &
1709 ObjCPropertyAttribute::kind_unsafe_unretained) {
1710 Out << (first ? "" : ", ") << "unsafe_unretained";
1711 first = false;
1712 }
1713
1714 if (PDecl->getPropertyAttributes() &
1715 ObjCPropertyAttribute::kind_readwrite) {
1716 Out << (first ? "" : ", ") << "readwrite";
1717 first = false;
1718 }
1719 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_readonly) {
1720 Out << (first ? "" : ", ") << "readonly";
1721 first = false;
1722 }
1723
1724 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_getter) {
1725 Out << (first ? "" : ", ") << "getter = ";
1726 PDecl->getGetterName().print(OS&: Out);
1727 first = false;
1728 }
1729 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_setter) {
1730 Out << (first ? "" : ", ") << "setter = ";
1731 PDecl->getSetterName().print(OS&: Out);
1732 first = false;
1733 }
1734
1735 if (PDecl->getPropertyAttributes() &
1736 ObjCPropertyAttribute::kind_nullability) {
1737 if (auto nullability = AttributedType::stripOuterNullability(T)) {
1738 if (*nullability == NullabilityKind::Unspecified &&
1739 (PDecl->getPropertyAttributes() &
1740 ObjCPropertyAttribute::kind_null_resettable)) {
1741 Out << (first ? "" : ", ") << "null_resettable";
1742 } else {
1743 Out << (first ? "" : ", ")
1744 << getNullabilitySpelling(kind: *nullability, isContextSensitive: true);
1745 }
1746 first = false;
1747 }
1748 }
1749
1750 (void) first; // Silence dead store warning due to idiomatic code.
1751 Out << ")";
1752 }
1753 std::string TypeStr = PDecl->getASTContext().getUnqualifiedObjCPointerType(type: T).
1754 getAsString(Policy);
1755 Out << ' ' << TypeStr;
1756 if (!StringRef(TypeStr).ends_with(Suffix: "*"))
1757 Out << ' ';
1758 Out << *PDecl;
1759 if (Policy.PolishForDeclaration)
1760 Out << ';';
1761}
1762
1763void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1764 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1765 Out << "@synthesize ";
1766 else
1767 Out << "@dynamic ";
1768 Out << *PID->getPropertyDecl();
1769 if (PID->getPropertyIvarDecl())
1770 Out << '=' << *PID->getPropertyIvarDecl();
1771}
1772
1773void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1774 if (!D->isAccessDeclaration())
1775 Out << "using ";
1776 if (D->hasTypename())
1777 Out << "typename ";
1778 D->getQualifier().print(OS&: Out, Policy);
1779
1780 // Use the correct record name when the using declaration is used for
1781 // inheriting constructors.
1782 for (const auto *Shadow : D->shadows()) {
1783 if (const auto *ConstructorShadow =
1784 dyn_cast<ConstructorUsingShadowDecl>(Val: Shadow)) {
1785 assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext());
1786 Out << *ConstructorShadow->getNominatedBaseClass();
1787 return;
1788 }
1789 }
1790 Out << *D;
1791}
1792
1793void DeclPrinter::VisitUsingEnumDecl(UsingEnumDecl *D) {
1794 Out << "using enum " << D->getEnumDecl();
1795}
1796
1797void
1798DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1799 Out << "using typename ";
1800 D->getQualifier().print(OS&: Out, Policy);
1801 Out << D->getDeclName();
1802}
1803
1804void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1805 if (!D->isAccessDeclaration())
1806 Out << "using ";
1807 D->getQualifier().print(OS&: Out, Policy);
1808 Out << D->getDeclName();
1809}
1810
1811void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1812 // ignore
1813}
1814
1815void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1816 Out << "#pragma omp threadprivate";
1817 if (!D->varlist_empty()) {
1818 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1819 E = D->varlist_end();
1820 I != E; ++I) {
1821 Out << (I == D->varlist_begin() ? '(' : ',');
1822 NamedDecl *ND = cast<DeclRefExpr>(Val: *I)->getDecl();
1823 ND->printQualifiedName(OS&: Out);
1824 }
1825 Out << ")";
1826 }
1827}
1828
1829void DeclPrinter::VisitHLSLBufferDecl(HLSLBufferDecl *D) {
1830 if (D->isCBuffer())
1831 Out << "cbuffer ";
1832 else
1833 Out << "tbuffer ";
1834
1835 Out << *D;
1836
1837 if (std::optional<std::string> Attrs = prettyPrintAttributes(D))
1838 Out << ' ' << *Attrs;
1839
1840 Out << " {\n";
1841 VisitDeclContext(DC: D);
1842 Indent() << "}";
1843}
1844
1845void DeclPrinter::VisitOMPAllocateDecl(OMPAllocateDecl *D) {
1846 Out << "#pragma omp allocate";
1847 if (!D->varlist_empty()) {
1848 for (OMPAllocateDecl::varlist_iterator I = D->varlist_begin(),
1849 E = D->varlist_end();
1850 I != E; ++I) {
1851 Out << (I == D->varlist_begin() ? '(' : ',');
1852 NamedDecl *ND = cast<DeclRefExpr>(Val: *I)->getDecl();
1853 ND->printQualifiedName(OS&: Out);
1854 }
1855 Out << ")";
1856 }
1857 if (!D->clauselist_empty()) {
1858 OMPClausePrinter Printer(Out, Policy, Context.getLangOpts().OpenMP);
1859 for (OMPClause *C : D->clauselists()) {
1860 Out << " ";
1861 Printer.Visit(S: C);
1862 }
1863 }
1864}
1865
1866void DeclPrinter::VisitOMPRequiresDecl(OMPRequiresDecl *D) {
1867 Out << "#pragma omp requires ";
1868 if (!D->clauselist_empty()) {
1869 OMPClausePrinter Printer(Out, Policy, Context.getLangOpts().OpenMP);
1870 for (auto I = D->clauselist_begin(), E = D->clauselist_end(); I != E; ++I)
1871 Printer.Visit(S: *I);
1872 }
1873}
1874
1875void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1876 if (!D->isInvalidDecl()) {
1877 Out << "#pragma omp declare reduction (";
1878 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
1879 const char *OpName =
1880 getOperatorSpelling(Operator: D->getDeclName().getCXXOverloadedOperator());
1881 assert(OpName && "not an overloaded operator");
1882 Out << OpName;
1883 } else {
1884 assert(D->getDeclName().isIdentifier());
1885 D->printName(OS&: Out, Policy);
1886 }
1887 Out << " : ";
1888 D->getType().print(OS&: Out, Policy);
1889 Out << " : ";
1890 D->getCombiner()->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation: 0, NewlineSymbol: "\n", Context: &Context);
1891 Out << ")";
1892 if (auto *Init = D->getInitializer()) {
1893 Out << " initializer(";
1894 switch (D->getInitializerKind()) {
1895 case OMPDeclareReductionInitKind::Direct:
1896 Out << "omp_priv(";
1897 break;
1898 case OMPDeclareReductionInitKind::Copy:
1899 Out << "omp_priv = ";
1900 break;
1901 case OMPDeclareReductionInitKind::Call:
1902 break;
1903 }
1904 Init->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation: 0, NewlineSymbol: "\n", Context: &Context);
1905 if (D->getInitializerKind() == OMPDeclareReductionInitKind::Direct)
1906 Out << ")";
1907 Out << ")";
1908 }
1909 }
1910}
1911
1912void DeclPrinter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
1913 if (!D->isInvalidDecl()) {
1914 Out << "#pragma omp declare mapper (";
1915 D->printName(OS&: Out, Policy);
1916 Out << " : ";
1917 D->getType().print(OS&: Out, Policy);
1918 Out << " ";
1919 Out << D->getVarName();
1920 Out << ")";
1921 if (!D->clauselist_empty()) {
1922 OMPClausePrinter Printer(Out, Policy, Context.getLangOpts().OpenMP);
1923 for (auto *C : D->clauselists()) {
1924 Out << " ";
1925 Printer.Visit(S: C);
1926 }
1927 }
1928 }
1929}
1930
1931void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
1932 D->getInit()->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation, NewlineSymbol: "\n", Context: &Context);
1933}
1934
1935void DeclPrinter::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *TTP) {
1936 if (const TypeConstraint *TC = TTP->getTypeConstraint())
1937 TC->print(OS&: Out, Policy);
1938 else if (TTP->wasDeclaredWithTypename())
1939 Out << "typename";
1940 else
1941 Out << "class";
1942
1943 if (TTP->isParameterPack())
1944 Out << " ...";
1945 else if (TTP->getDeclName())
1946 Out << ' ';
1947
1948 if (TTP->getDeclName()) {
1949 if (Policy.CleanUglifiedParameters && TTP->getIdentifier())
1950 Out << TTP->getIdentifier()->deuglifiedName();
1951 else
1952 Out << TTP->getDeclName();
1953 }
1954
1955 if (TTP->hasDefaultArgument() && !TTP->defaultArgumentWasInherited()) {
1956 Out << " = ";
1957 TTP->getDefaultArgument().getArgument().print(Policy, Out,
1958 /*IncludeType=*/false);
1959 }
1960}
1961
1962void DeclPrinter::VisitNonTypeTemplateParmDecl(
1963 const NonTypeTemplateParmDecl *NTTP) {
1964 StringRef Name;
1965 if (IdentifierInfo *II = NTTP->getIdentifier())
1966 Name =
1967 Policy.CleanUglifiedParameters ? II->deuglifiedName() : II->getName();
1968 printDeclType(T: NTTP->getType(), DeclName: Name, Pack: NTTP->isParameterPack());
1969
1970 if (NTTP->hasDefaultArgument() && !NTTP->defaultArgumentWasInherited()) {
1971 Out << " = ";
1972 NTTP->getDefaultArgument().getArgument().print(Policy, Out,
1973 /*IncludeType=*/false);
1974 }
1975}
1976
1977void DeclPrinter::VisitTemplateTemplateParmDecl(
1978 const TemplateTemplateParmDecl *TTPD) {
1979 VisitTemplateDecl(D: TTPD);
1980 if (TTPD->hasDefaultArgument() && !TTPD->defaultArgumentWasInherited()) {
1981 Out << " = ";
1982 TTPD->getDefaultArgument().getArgument().print(Policy, Out,
1983 /*IncludeType=*/false);
1984 }
1985}
1986
1987void DeclPrinter::VisitOpenACCDeclareDecl(OpenACCDeclareDecl *D) {
1988 if (!D->isInvalidDecl()) {
1989 Out << "#pragma acc declare";
1990 if (!D->clauses().empty()) {
1991 Out << ' ';
1992 OpenACCClausePrinter Printer(Out, Policy);
1993 Printer.VisitClauseList(List: D->clauses());
1994 }
1995 }
1996}
1997void DeclPrinter::VisitOpenACCRoutineDecl(OpenACCRoutineDecl *D) {
1998 if (!D->isInvalidDecl()) {
1999 Out << "#pragma acc routine";
2000
2001 Out << "(";
2002
2003 // The referenced function was named here, but this makes us tolerant of
2004 // errors.
2005 if (D->getFunctionReference())
2006 D->getFunctionReference()->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation,
2007 NewlineSymbol: "\n", Context: &Context);
2008 else
2009 Out << "<error>";
2010
2011 Out << ")";
2012
2013 if (!D->clauses().empty()) {
2014 Out << ' ';
2015 OpenACCClausePrinter Printer(Out, Policy);
2016 Printer.VisitClauseList(List: D->clauses());
2017 }
2018 }
2019}
2020