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 VisitClassTemplateSpecializationDecl(
90 ClassTemplateSpecializationDecl *D);
91 void VisitClassTemplatePartialSpecializationDecl(
92 ClassTemplatePartialSpecializationDecl *D);
93 void VisitObjCMethodDecl(ObjCMethodDecl *D);
94 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
95 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
96 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
97 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
98 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
99 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
100 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
101 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
102 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
103 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
104 void VisitUsingDecl(UsingDecl *D);
105 void VisitUsingEnumDecl(UsingEnumDecl *D);
106 void VisitUsingShadowDecl(UsingShadowDecl *D);
107 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
108 void VisitOMPAllocateDecl(OMPAllocateDecl *D);
109 void VisitOMPRequiresDecl(OMPRequiresDecl *D);
110 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
111 void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D);
112 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
113 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *TTP);
114 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *NTTP);
115 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *);
116 void VisitHLSLBufferDecl(HLSLBufferDecl *D);
117
118 void VisitOpenACCDeclareDecl(OpenACCDeclareDecl *D);
119 void VisitOpenACCRoutineDecl(OpenACCRoutineDecl *D);
120
121 void printTemplateParameters(const TemplateParameterList *Params,
122 bool OmitTemplateKW = false);
123 void printTemplateArguments(ArrayRef<TemplateArgument> Args,
124 const TemplateParameterList *Params);
125 void printTemplateArguments(ArrayRef<TemplateArgumentLoc> Args,
126 const TemplateParameterList *Params);
127 enum class AttrPosAsWritten { Default = 0, Left, Right };
128 std::optional<std::string>
129 prettyPrintAttributes(const Decl *D,
130 AttrPosAsWritten Pos = AttrPosAsWritten::Default);
131
132 void prettyPrintPragmas(Decl *D);
133 void printDeclType(QualType T, StringRef DeclName, bool Pack = false);
134 };
135}
136
137void Decl::print(raw_ostream &Out, unsigned Indentation,
138 bool PrintInstantiation) const {
139 print(Out, Policy: getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
140}
141
142void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
143 unsigned Indentation, bool PrintInstantiation) const {
144 DeclPrinter Printer(Out, Policy, getASTContext(), Indentation,
145 PrintInstantiation);
146 Printer.Visit(D: const_cast<Decl*>(this));
147}
148
149void TemplateParameterList::print(raw_ostream &Out, const ASTContext &Context,
150 bool OmitTemplateKW) const {
151 print(Out, Context, Policy: Context.getPrintingPolicy(), OmitTemplateKW);
152}
153
154void TemplateParameterList::print(raw_ostream &Out, const ASTContext &Context,
155 const PrintingPolicy &Policy,
156 bool OmitTemplateKW) const {
157 DeclPrinter Printer(Out, Policy, Context);
158 Printer.printTemplateParameters(Params: this, OmitTemplateKW);
159}
160
161static QualType GetBaseType(QualType T) {
162 // FIXME: This should be on the Type class!
163 QualType BaseType = T;
164 while (!BaseType->isSpecifierType()) {
165 if (const PointerType *PTy = BaseType->getAs<PointerType>())
166 BaseType = PTy->getPointeeType();
167 else if (const ObjCObjectPointerType *OPT =
168 BaseType->getAs<ObjCObjectPointerType>())
169 BaseType = OPT->getPointeeType();
170 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
171 BaseType = BPy->getPointeeType();
172 else if (const ArrayType *ATy = dyn_cast<ArrayType>(Val&: BaseType))
173 BaseType = ATy->getElementType();
174 else if (const FunctionType *FTy = BaseType->getAs<FunctionType>())
175 BaseType = FTy->getReturnType();
176 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
177 BaseType = VTy->getElementType();
178 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
179 BaseType = RTy->getPointeeType();
180 else if (const AutoType *ATy = BaseType->getAs<AutoType>())
181 BaseType = ATy->getDeducedType();
182 else if (const ParenType *PTy = BaseType->getAs<ParenType>())
183 BaseType = PTy->desugar();
184 else
185 // This must be a syntax error.
186 break;
187 }
188 return BaseType;
189}
190
191static QualType getDeclType(Decl* D) {
192 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(Val: D))
193 return TDD->getUnderlyingType();
194 if (ValueDecl* VD = dyn_cast<ValueDecl>(Val: D))
195 return VD->getType();
196 return QualType();
197}
198
199void Decl::printGroup(Decl** Begin, unsigned NumDecls,
200 raw_ostream &Out, const PrintingPolicy &Policy,
201 unsigned Indentation) {
202 if (NumDecls == 1) {
203 (*Begin)->print(Out, Policy, Indentation);
204 return;
205 }
206
207 Decl** End = Begin + NumDecls;
208 if (isa<TagDecl>(Val: *Begin))
209 ++Begin;
210
211 PrintingPolicy SubPolicy(Policy);
212
213 bool isFirst = true;
214 for ( ; Begin != End; ++Begin) {
215 if (isFirst) {
216 isFirst = false;
217 } else {
218 Out << ", ";
219 SubPolicy.SuppressSpecifiers = true;
220 }
221
222 (*Begin)->print(Out, Policy: SubPolicy, Indentation);
223 }
224}
225
226LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
227 // Get the translation unit
228 const DeclContext *DC = this;
229 while (!DC->isTranslationUnit())
230 DC = DC->getParent();
231
232 ASTContext &Ctx = cast<TranslationUnitDecl>(Val: DC)->getASTContext();
233 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), Ctx, 0);
234 Printer.VisitDeclContext(DC: const_cast<DeclContext *>(this), /*Indent=*/false);
235}
236
237raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
238 for (unsigned i = 0; i != Indentation; ++i)
239 Out << " ";
240 return Out;
241}
242
243static DeclPrinter::AttrPosAsWritten getPosAsWritten(const Attr *A,
244 const Decl *D) {
245 SourceLocation ALoc = A->getLoc();
246 SourceLocation DLoc = D->getLocation();
247 const ASTContext &C = D->getASTContext();
248 if (ALoc.isInvalid() || DLoc.isInvalid())
249 return DeclPrinter::AttrPosAsWritten::Left;
250
251 if (C.getSourceManager().isBeforeInTranslationUnit(LHS: ALoc, RHS: DLoc))
252 return DeclPrinter::AttrPosAsWritten::Left;
253
254 return DeclPrinter::AttrPosAsWritten::Right;
255}
256
257std::optional<std::string>
258DeclPrinter::prettyPrintAttributes(const Decl *D,
259 AttrPosAsWritten Pos /*=Default*/) {
260 if (Policy.SuppressDeclAttributes || !D->hasAttrs())
261 return std::nullopt;
262
263 std::string AttrStr;
264 llvm::raw_string_ostream AOut(AttrStr);
265 llvm::ListSeparator LS(" ");
266 for (auto *A : D->getAttrs()) {
267 if (A->isInherited() || A->isImplicit())
268 continue;
269 // Print out the keyword attributes, they aren't regular attributes.
270 if (Policy.PolishForDeclaration && !A->isKeywordAttribute())
271 continue;
272 switch (A->getKind()) {
273#define ATTR(X)
274#define PRAGMA_SPELLING_ATTR(X) case attr::X:
275#include "clang/Basic/AttrList.inc"
276 break;
277 default:
278 AttrPosAsWritten APos = getPosAsWritten(A, D);
279 assert(APos != AttrPosAsWritten::Default &&
280 "Default not a valid for an attribute location");
281 if (Pos == AttrPosAsWritten::Default || Pos == APos) {
282 AOut << LS;
283 A->printPretty(OS&: AOut, Policy);
284 }
285 break;
286 }
287 }
288 if (AttrStr.empty())
289 return std::nullopt;
290 return AttrStr;
291}
292
293void DeclPrinter::PrintOpenACCRoutineOnLambda(Decl *D) {
294 CXXRecordDecl *CXXRD = nullptr;
295 if (const auto *VD = dyn_cast<VarDecl>(Val: D)) {
296 if (const auto *Init = VD->getInit())
297 CXXRD = Init->getType().isNull() ? nullptr
298 : Init->getType()->getAsCXXRecordDecl();
299 } else if (const auto *FD = dyn_cast<FieldDecl>(Val: D)) {
300 CXXRD =
301 FD->getType().isNull() ? nullptr : FD->getType()->getAsCXXRecordDecl();
302 }
303
304 if (!CXXRD || !CXXRD->isLambda())
305 return;
306
307 if (const auto *Call = CXXRD->getLambdaCallOperator()) {
308 for (auto *A : Call->specific_attrs<OpenACCRoutineDeclAttr>()) {
309 A->printPretty(OS&: Out, Policy);
310 Indent();
311 }
312 }
313}
314
315void DeclPrinter::prettyPrintPragmas(Decl *D) {
316 if (Policy.PolishForDeclaration)
317 return;
318
319 PrintOpenACCRoutineOnLambda(D);
320
321 if (D->hasAttrs()) {
322 AttrVec &Attrs = D->getAttrs();
323 for (auto *A : Attrs) {
324 switch (A->getKind()) {
325#define ATTR(X)
326#define PRAGMA_SPELLING_ATTR(X) case attr::X:
327#include "clang/Basic/AttrList.inc"
328 A->printPretty(OS&: Out, Policy);
329 Indent();
330 break;
331 default:
332 break;
333 }
334 }
335 }
336}
337
338void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) {
339 // Normally, a PackExpansionType is written as T[3]... (for instance, as a
340 // template argument), but if it is the type of a declaration, the ellipsis
341 // is placed before the name being declared.
342 if (auto *PET = T->getAs<PackExpansionType>()) {
343 Pack = true;
344 T = PET->getPattern();
345 }
346 T.print(OS&: Out, Policy, PlaceHolder: (Pack ? "..." : "") + DeclName, Indentation);
347}
348
349void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
350 this->Indent();
351 Decl::printGroup(Begin: Decls.data(), NumDecls: Decls.size(), Out, Policy, Indentation);
352 Out << ";\n";
353 Decls.clear();
354
355}
356
357void DeclPrinter::Print(AccessSpecifier AS) {
358 const auto AccessSpelling = getAccessSpelling(AS);
359 if (AccessSpelling.empty())
360 llvm_unreachable("No access specifier!");
361 Out << AccessSpelling;
362}
363
364void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl,
365 std::string &Proto) {
366 bool HasInitializerList = false;
367 for (const auto *BMInitializer : CDecl->inits()) {
368 if (BMInitializer->isInClassMemberInitializer())
369 continue;
370 if (!BMInitializer->isWritten())
371 continue;
372
373 if (!HasInitializerList) {
374 Proto += " : ";
375 Out << Proto;
376 Proto.clear();
377 HasInitializerList = true;
378 } else
379 Out << ", ";
380
381 if (BMInitializer->isAnyMemberInitializer()) {
382 FieldDecl *FD = BMInitializer->getAnyMember();
383 Out << *FD;
384 } else if (BMInitializer->isDelegatingInitializer()) {
385 Out << CDecl->getNameAsString();
386 } else {
387 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
388 }
389
390 if (Expr *Init = BMInitializer->getInit()) {
391 bool OutParens = !isa<InitListExpr>(Val: Init);
392
393 if (OutParens)
394 Out << "(";
395
396 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Val: Init))
397 Init = Tmp->getSubExpr();
398
399 Init = Init->IgnoreParens();
400
401 Expr *SimpleInit = nullptr;
402 Expr **Args = nullptr;
403 unsigned NumArgs = 0;
404 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Val: Init)) {
405 Args = ParenList->getExprs();
406 NumArgs = ParenList->getNumExprs();
407 } else if (CXXConstructExpr *Construct =
408 dyn_cast<CXXConstructExpr>(Val: Init)) {
409 Args = Construct->getArgs();
410 NumArgs = Construct->getNumArgs();
411 } else
412 SimpleInit = Init;
413
414 if (SimpleInit)
415 SimpleInit->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation, NewlineSymbol: "\n",
416 Context: &Context);
417 else {
418 for (unsigned I = 0; I != NumArgs; ++I) {
419 assert(Args[I] != nullptr && "Expected non-null Expr");
420 if (isa<CXXDefaultArgExpr>(Val: Args[I]))
421 break;
422
423 if (I)
424 Out << ", ";
425 Args[I]->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation, NewlineSymbol: "\n",
426 Context: &Context);
427 }
428 }
429
430 if (OutParens)
431 Out << ")";
432 } else {
433 Out << "()";
434 }
435
436 if (BMInitializer->isPackExpansion())
437 Out << "...";
438 }
439}
440
441//----------------------------------------------------------------------------
442// Common C declarations
443//----------------------------------------------------------------------------
444
445void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
446 if (Policy.TerseOutput)
447 return;
448
449 if (Indent)
450 Indentation += Policy.Indentation;
451
452 SmallVector<Decl*, 2> Decls;
453 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
454 D != DEnd; ++D) {
455
456 // Don't print ObjCIvarDecls, as they are printed when visiting the
457 // containing ObjCInterfaceDecl.
458 if (isa<ObjCIvarDecl>(Val: *D))
459 continue;
460
461 // Skip over implicit declarations in pretty-printing mode.
462 if (D->isImplicit())
463 continue;
464
465 // Don't print implicit specializations, as they are printed when visiting
466 // corresponding templates.
467 if (auto FD = dyn_cast<FunctionDecl>(Val: *D))
468 if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation &&
469 !isa<ClassTemplateSpecializationDecl>(Val: DC))
470 continue;
471
472 // The next bits of code handle stuff like "struct {int x;} a,b"; we're
473 // forced to merge the declarations because there's no other way to
474 // refer to the struct in question. When that struct is named instead, we
475 // also need to merge to avoid splitting off a stand-alone struct
476 // declaration that produces the warning ext_no_declarators in some
477 // contexts.
478 //
479 // This limited merging is safe without a bunch of other checks because it
480 // only merges declarations directly referring to the tag, not typedefs.
481 //
482 // Check whether the current declaration should be grouped with a previous
483 // non-free-standing tag declaration.
484 QualType CurDeclType = getDeclType(D: *D);
485 if (!Decls.empty() && !CurDeclType.isNull()) {
486 QualType BaseType = GetBaseType(T: CurDeclType);
487 if (const auto *TT = dyn_cast_or_null<TagType>(Val&: BaseType);
488 TT && TT->isTagOwned()) {
489 if (TT->getDecl() == Decls[0]) {
490 Decls.push_back(Elt: *D);
491 continue;
492 }
493 }
494 }
495
496 // If we have a merged group waiting to be handled, handle it now.
497 if (!Decls.empty())
498 ProcessDeclGroup(Decls);
499
500 // If the current declaration is not a free standing declaration, save it
501 // so we can merge it with the subsequent declaration(s) using it.
502 if (isa<TagDecl>(Val: *D) && !cast<TagDecl>(Val: *D)->isFreeStanding()) {
503 Decls.push_back(Elt: *D);
504 continue;
505 }
506
507 if (isa<AccessSpecDecl>(Val: *D)) {
508 Indentation -= Policy.Indentation;
509 this->Indent();
510 Print(AS: D->getAccess());
511 Out << ":\n";
512 Indentation += Policy.Indentation;
513 continue;
514 }
515
516 this->Indent();
517 Visit(D: *D);
518
519 // FIXME: Need to be able to tell the DeclPrinter when
520 const char *Terminator = nullptr;
521 if (isa<OMPThreadPrivateDecl>(Val: *D) || isa<OMPDeclareReductionDecl>(Val: *D) ||
522 isa<OMPDeclareMapperDecl>(Val: *D) || isa<OMPRequiresDecl>(Val: *D) ||
523 isa<OMPAllocateDecl>(Val: *D))
524 Terminator = nullptr;
525 else if (isa<OpenACCDeclareDecl, OpenACCRoutineDecl>(Val: *D))
526 Terminator = nullptr;
527 else if (isa<ObjCMethodDecl>(Val: *D) && cast<ObjCMethodDecl>(Val: *D)->hasBody())
528 Terminator = nullptr;
529 else if (auto FD = dyn_cast<FunctionDecl>(Val: *D)) {
530 if (FD->doesThisDeclarationHaveABody() && !FD->isDefaulted())
531 Terminator = nullptr;
532 else
533 Terminator = ";";
534 } else if (auto TD = dyn_cast<FunctionTemplateDecl>(Val: *D)) {
535 if (TD->getTemplatedDecl()->doesThisDeclarationHaveABody())
536 Terminator = nullptr;
537 else
538 Terminator = ";";
539 } else if (isa<NamespaceDecl, LinkageSpecDecl, ObjCImplementationDecl,
540 ObjCInterfaceDecl, ObjCProtocolDecl, ObjCCategoryImplDecl,
541 ObjCCategoryDecl, HLSLBufferDecl>(Val: *D))
542 Terminator = nullptr;
543 else if (isa<EnumConstantDecl>(Val: *D)) {
544 DeclContext::decl_iterator Next = D;
545 ++Next;
546 if (Next != DEnd)
547 Terminator = ",";
548 } else
549 Terminator = ";";
550
551 if (Terminator)
552 Out << Terminator;
553 if (!Policy.TerseOutput &&
554 ((isa<FunctionDecl>(Val: *D) &&
555 cast<FunctionDecl>(Val: *D)->doesThisDeclarationHaveABody()) ||
556 (isa<FunctionTemplateDecl>(Val: *D) &&
557 cast<FunctionTemplateDecl>(Val: *D)->getTemplatedDecl()->doesThisDeclarationHaveABody())))
558 ; // StmtPrinter already added '\n' after CompoundStmt.
559 else
560 Out << "\n";
561
562 // Declare target attribute is special one, natural spelling for the pragma
563 // assumes "ending" construct so print it here.
564 if (D->hasAttr<OMPDeclareTargetDeclAttr>())
565 Out << "#pragma omp end declare target\n";
566 }
567
568 if (!Decls.empty())
569 ProcessDeclGroup(Decls);
570
571 if (Indent)
572 Indentation -= Policy.Indentation;
573}
574
575void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
576 VisitDeclContext(DC: D, Indent: false);
577}
578
579void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
580 if (!Policy.SuppressSpecifiers) {
581 Out << "typedef ";
582
583 if (D->isModulePrivate())
584 Out << "__module_private__ ";
585 }
586 QualType Ty = D->getTypeSourceInfo()->getType();
587 Ty.print(OS&: Out, Policy, PlaceHolder: D->getName(), Indentation);
588
589 if (std::optional<std::string> Attrs = prettyPrintAttributes(D))
590 Out << ' ' << *Attrs;
591}
592
593void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
594 Out << "using " << *D;
595 if (std::optional<std::string> Attrs = prettyPrintAttributes(D))
596 Out << ' ' << *Attrs;
597 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
598}
599
600void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
601 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
602 Out << "__module_private__ ";
603 Out << "enum";
604 if (D->isScoped()) {
605 if (D->isScopedUsingClassTag())
606 Out << " class";
607 else
608 Out << " struct";
609 }
610
611 if (std::optional<std::string> Attrs = prettyPrintAttributes(D))
612 Out << ' ' << *Attrs;
613
614 if (D->getDeclName())
615 Out << ' ' << D->getDeclName();
616
617 if (D->isFixed())
618 Out << " : " << D->getIntegerType().stream(Policy);
619
620 if (D->isCompleteDefinition()) {
621 Out << " {\n";
622 VisitDeclContext(DC: D);
623 Indent() << "}";
624 }
625}
626
627void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
628 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
629 Out << "__module_private__ ";
630 Out << D->getKindName();
631
632 if (std::optional<std::string> Attrs = prettyPrintAttributes(D))
633 Out << ' ' << *Attrs;
634
635 if (D->getIdentifier())
636 Out << ' ' << *D;
637
638 if (D->isCompleteDefinition()) {
639 Out << " {\n";
640 VisitDeclContext(DC: D);
641 Indent() << "}";
642 }
643}
644
645void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
646 Out << *D;
647 if (std::optional<std::string> Attrs = prettyPrintAttributes(D))
648 Out << ' ' << *Attrs;
649 if (Expr *Init = D->getInitExpr()) {
650 Out << " = ";
651 Init->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation, NewlineSymbol: "\n", Context: &Context);
652 }
653}
654
655static void printExplicitSpecifier(ExplicitSpecifier ES, llvm::raw_ostream &Out,
656 PrintingPolicy &Policy, unsigned Indentation,
657 const ASTContext &Context) {
658 std::string Proto = "explicit";
659 llvm::raw_string_ostream EOut(Proto);
660 if (ES.getExpr()) {
661 EOut << "(";
662 ES.getExpr()->printPretty(OS&: EOut, Helper: nullptr, Policy, Indentation, NewlineSymbol: "\n",
663 Context: &Context);
664 EOut << ")";
665 }
666 EOut << " ";
667 Out << Proto;
668}
669
670void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
671 if (!D->getDescribedFunctionTemplate() &&
672 !D->isFunctionTemplateSpecialization()) {
673 prettyPrintPragmas(D);
674 if (std::optional<std::string> Attrs =
675 prettyPrintAttributes(D, Pos: AttrPosAsWritten::Left))
676 Out << *Attrs << ' ';
677 }
678
679 if (D->isFunctionTemplateSpecialization())
680 Out << "template<> ";
681 else if (!D->getDescribedFunctionTemplate()) {
682 for (unsigned I = 0, NumTemplateParams = D->getNumTemplateParameterLists();
683 I < NumTemplateParams; ++I)
684 printTemplateParameters(Params: D->getTemplateParameterList(index: I));
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 (unsigned I = 0, NumTemplateParams = FD->getNumTemplateParameterLists();
1295 I < NumTemplateParams; ++I)
1296 printTemplateParameters(Params: FD->getTemplateParameterList(index: I));
1297 }
1298 VisitRedeclarableTemplateDecl(D);
1299 // Declare target attribute is special one, natural spelling for the pragma
1300 // assumes "ending" construct so print it here.
1301 if (D->getTemplatedDecl()->hasAttr<OMPDeclareTargetDeclAttr>())
1302 Out << "#pragma omp end declare target\n";
1303
1304 // Never print "instantiations" for deduction guides (they don't really
1305 // have them).
1306 if (PrintInstantiation &&
1307 !isa<CXXDeductionGuideDecl>(Val: D->getTemplatedDecl())) {
1308 FunctionDecl *PrevDecl = D->getTemplatedDecl();
1309 const FunctionDecl *Def;
1310 if (PrevDecl->isDefined(Definition&: Def) && Def != PrevDecl)
1311 return;
1312 for (auto *I : D->specializations())
1313 if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) {
1314 if (!PrevDecl->isThisDeclarationADefinition())
1315 Out << ";\n";
1316 Indent();
1317 prettyPrintPragmas(D: I);
1318 Visit(D: I);
1319 }
1320 }
1321}
1322
1323void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
1324 VisitRedeclarableTemplateDecl(D);
1325
1326 if (PrintInstantiation) {
1327 for (auto *I : D->specializations())
1328 if (I->getSpecializationKind() == TSK_ImplicitInstantiation) {
1329 if (D->isThisDeclarationADefinition())
1330 Out << ";";
1331 Out << "\n";
1332 Indent();
1333 Visit(D: I);
1334 }
1335 }
1336}
1337
1338void DeclPrinter::VisitClassTemplateSpecializationDecl(
1339 ClassTemplateSpecializationDecl *D) {
1340 Out << "template<> ";
1341 VisitCXXRecordDecl(D);
1342}
1343
1344void DeclPrinter::VisitClassTemplatePartialSpecializationDecl(
1345 ClassTemplatePartialSpecializationDecl *D) {
1346 printTemplateParameters(Params: D->getTemplateParameters());
1347 VisitCXXRecordDecl(D);
1348}
1349
1350//----------------------------------------------------------------------------
1351// Objective-C declarations
1352//----------------------------------------------------------------------------
1353
1354void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
1355 Decl::ObjCDeclQualifier Quals,
1356 QualType T) {
1357 Out << '(';
1358 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
1359 Out << "in ";
1360 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
1361 Out << "inout ";
1362 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1363 Out << "out ";
1364 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1365 Out << "bycopy ";
1366 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1367 Out << "byref ";
1368 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1369 Out << "oneway ";
1370 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
1371 if (auto nullability = AttributedType::stripOuterNullability(T))
1372 Out << getNullabilitySpelling(kind: *nullability, isContextSensitive: true) << ' ';
1373 }
1374
1375 Out << Ctx.getUnqualifiedObjCPointerType(type: T).getAsString(Policy);
1376 Out << ')';
1377}
1378
1379void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1380 Out << "<";
1381 unsigned First = true;
1382 for (auto *Param : *Params) {
1383 if (First) {
1384 First = false;
1385 } else {
1386 Out << ", ";
1387 }
1388
1389 switch (Param->getVariance()) {
1390 case ObjCTypeParamVariance::Invariant:
1391 break;
1392
1393 case ObjCTypeParamVariance::Covariant:
1394 Out << "__covariant ";
1395 break;
1396
1397 case ObjCTypeParamVariance::Contravariant:
1398 Out << "__contravariant ";
1399 break;
1400 }
1401
1402 Out << Param->getDeclName();
1403
1404 if (Param->hasExplicitBound()) {
1405 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1406 }
1407 }
1408 Out << ">";
1409}
1410
1411void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1412 if (OMD->isInstanceMethod())
1413 Out << "- ";
1414 else
1415 Out << "+ ";
1416 if (!OMD->getReturnType().isNull()) {
1417 PrintObjCMethodType(Ctx&: OMD->getASTContext(), Quals: OMD->getObjCDeclQualifier(),
1418 T: OMD->getReturnType());
1419 }
1420
1421 std::string name = OMD->getSelector().getAsString();
1422 std::string::size_type pos, lastPos = 0;
1423 for (const auto *PI : OMD->parameters()) {
1424 // FIXME: selector is missing here!
1425 pos = name.find_first_of(c: ':', pos: lastPos);
1426 if (lastPos != 0)
1427 Out << " ";
1428 Out << name.substr(pos: lastPos, n: pos - lastPos) << ':';
1429 PrintObjCMethodType(Ctx&: OMD->getASTContext(),
1430 Quals: PI->getObjCDeclQualifier(),
1431 T: PI->getType());
1432 Out << *PI;
1433 lastPos = pos + 1;
1434 }
1435
1436 if (OMD->parameters().empty())
1437 Out << name;
1438
1439 if (OMD->isVariadic())
1440 Out << ", ...";
1441
1442 if (std::optional<std::string> Attrs = prettyPrintAttributes(D: OMD))
1443 Out << ' ' << *Attrs;
1444
1445 if (OMD->getBody() && !Policy.TerseOutput) {
1446 Out << ' ';
1447 OMD->getBody()->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation, NewlineSymbol: "\n",
1448 Context: &Context);
1449 }
1450 else if (Policy.PolishForDeclaration)
1451 Out << ';';
1452}
1453
1454void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1455 std::string I = OID->getNameAsString();
1456 ObjCInterfaceDecl *SID = OID->getSuperClass();
1457
1458 bool eolnOut = false;
1459 if (SID)
1460 Out << "@implementation " << I << " : " << *SID;
1461 else
1462 Out << "@implementation " << I;
1463
1464 if (OID->ivar_size() > 0) {
1465 Out << "{\n";
1466 eolnOut = true;
1467 Indentation += Policy.Indentation;
1468 for (const auto *I : OID->ivars()) {
1469 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(type: I->getType()).
1470 getAsString(Policy) << ' ' << *I << ";\n";
1471 }
1472 Indentation -= Policy.Indentation;
1473 Out << "}\n";
1474 } else if (SID || !OID->decls().empty()) {
1475 Out << "\n";
1476 eolnOut = true;
1477 }
1478 VisitDeclContext(DC: OID, Indent: false);
1479 if (!eolnOut)
1480 Out << "\n";
1481 Out << "@end";
1482}
1483
1484void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1485 std::string I = OID->getNameAsString();
1486 ObjCInterfaceDecl *SID = OID->getSuperClass();
1487
1488 if (!OID->isThisDeclarationADefinition()) {
1489 Out << "@class " << I;
1490
1491 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1492 PrintObjCTypeParams(Params: TypeParams);
1493 }
1494
1495 Out << ";";
1496 return;
1497 }
1498 bool eolnOut = false;
1499 if (std::optional<std::string> Attrs = prettyPrintAttributes(D: OID))
1500 Out << *Attrs << "\n";
1501
1502 Out << "@interface " << I;
1503
1504 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1505 PrintObjCTypeParams(Params: TypeParams);
1506 }
1507
1508 if (SID)
1509 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
1510
1511 // Protocols?
1512 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1513 if (!Protocols.empty()) {
1514 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1515 E = Protocols.end(); I != E; ++I)
1516 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1517 Out << "> ";
1518 }
1519
1520 if (OID->ivar_size() > 0) {
1521 Out << "{\n";
1522 eolnOut = true;
1523 Indentation += Policy.Indentation;
1524 for (const auto *I : OID->ivars()) {
1525 Indent() << I->getASTContext()
1526 .getUnqualifiedObjCPointerType(type: I->getType())
1527 .getAsString(Policy) << ' ' << *I << ";\n";
1528 }
1529 Indentation -= Policy.Indentation;
1530 Out << "}\n";
1531 } else if (SID || !OID->decls().empty()) {
1532 Out << "\n";
1533 eolnOut = true;
1534 }
1535
1536 VisitDeclContext(DC: OID, Indent: false);
1537 if (!eolnOut)
1538 Out << "\n";
1539 Out << "@end";
1540 // FIXME: implement the rest...
1541}
1542
1543void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
1544 if (!PID->isThisDeclarationADefinition()) {
1545 Out << "@protocol " << *PID << ";\n";
1546 return;
1547 }
1548 // Protocols?
1549 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1550 if (!Protocols.empty()) {
1551 Out << "@protocol " << *PID;
1552 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1553 E = Protocols.end(); I != E; ++I)
1554 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1555 Out << ">\n";
1556 } else
1557 Out << "@protocol " << *PID << '\n';
1558 VisitDeclContext(DC: PID, Indent: false);
1559 Out << "@end";
1560}
1561
1562void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
1563 Out << "@implementation ";
1564 if (const auto *CID = PID->getClassInterface())
1565 Out << *CID;
1566 else
1567 Out << "<<error-type>>";
1568 Out << '(' << *PID << ")\n";
1569
1570 VisitDeclContext(DC: PID, Indent: false);
1571 Out << "@end";
1572 // FIXME: implement the rest...
1573}
1574
1575void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
1576 Out << "@interface ";
1577 if (const auto *CID = PID->getClassInterface())
1578 Out << *CID;
1579 else
1580 Out << "<<error-type>>";
1581 if (auto TypeParams = PID->getTypeParamList()) {
1582 PrintObjCTypeParams(Params: TypeParams);
1583 }
1584 Out << "(" << *PID << ")\n";
1585 if (PID->ivar_size() > 0) {
1586 Out << "{\n";
1587 Indentation += Policy.Indentation;
1588 for (const auto *I : PID->ivars())
1589 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(type: I->getType()).
1590 getAsString(Policy) << ' ' << *I << ";\n";
1591 Indentation -= Policy.Indentation;
1592 Out << "}\n";
1593 }
1594
1595 VisitDeclContext(DC: PID, Indent: false);
1596 Out << "@end";
1597
1598 // FIXME: implement the rest...
1599}
1600
1601void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
1602 Out << "@compatibility_alias " << *AID
1603 << ' ' << *AID->getClassInterface() << ";\n";
1604}
1605
1606/// PrintObjCPropertyDecl - print a property declaration.
1607///
1608/// Print attributes in the following order:
1609/// - class
1610/// - nonatomic | atomic
1611/// - assign | retain | strong | copy | weak | unsafe_unretained
1612/// - readwrite | readonly
1613/// - getter & setter
1614/// - nullability
1615void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1616 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1617 Out << "@required\n";
1618 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1619 Out << "@optional\n";
1620
1621 QualType T = PDecl->getType();
1622
1623 Out << "@property";
1624 if (PDecl->getPropertyAttributes() != ObjCPropertyAttribute::kind_noattr) {
1625 bool first = true;
1626 Out << "(";
1627 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_class) {
1628 Out << (first ? "" : ", ") << "class";
1629 first = false;
1630 }
1631
1632 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_direct) {
1633 Out << (first ? "" : ", ") << "direct";
1634 first = false;
1635 }
1636
1637 if (PDecl->getPropertyAttributes() &
1638 ObjCPropertyAttribute::kind_nonatomic) {
1639 Out << (first ? "" : ", ") << "nonatomic";
1640 first = false;
1641 }
1642 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_atomic) {
1643 Out << (first ? "" : ", ") << "atomic";
1644 first = false;
1645 }
1646
1647 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_assign) {
1648 Out << (first ? "" : ", ") << "assign";
1649 first = false;
1650 }
1651 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_retain) {
1652 Out << (first ? "" : ", ") << "retain";
1653 first = false;
1654 }
1655
1656 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_strong) {
1657 Out << (first ? "" : ", ") << "strong";
1658 first = false;
1659 }
1660 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_copy) {
1661 Out << (first ? "" : ", ") << "copy";
1662 first = false;
1663 }
1664 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_weak) {
1665 Out << (first ? "" : ", ") << "weak";
1666 first = false;
1667 }
1668 if (PDecl->getPropertyAttributes() &
1669 ObjCPropertyAttribute::kind_unsafe_unretained) {
1670 Out << (first ? "" : ", ") << "unsafe_unretained";
1671 first = false;
1672 }
1673
1674 if (PDecl->getPropertyAttributes() &
1675 ObjCPropertyAttribute::kind_readwrite) {
1676 Out << (first ? "" : ", ") << "readwrite";
1677 first = false;
1678 }
1679 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_readonly) {
1680 Out << (first ? "" : ", ") << "readonly";
1681 first = false;
1682 }
1683
1684 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_getter) {
1685 Out << (first ? "" : ", ") << "getter = ";
1686 PDecl->getGetterName().print(OS&: Out);
1687 first = false;
1688 }
1689 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_setter) {
1690 Out << (first ? "" : ", ") << "setter = ";
1691 PDecl->getSetterName().print(OS&: Out);
1692 first = false;
1693 }
1694
1695 if (PDecl->getPropertyAttributes() &
1696 ObjCPropertyAttribute::kind_nullability) {
1697 if (auto nullability = AttributedType::stripOuterNullability(T)) {
1698 if (*nullability == NullabilityKind::Unspecified &&
1699 (PDecl->getPropertyAttributes() &
1700 ObjCPropertyAttribute::kind_null_resettable)) {
1701 Out << (first ? "" : ", ") << "null_resettable";
1702 } else {
1703 Out << (first ? "" : ", ")
1704 << getNullabilitySpelling(kind: *nullability, isContextSensitive: true);
1705 }
1706 first = false;
1707 }
1708 }
1709
1710 (void) first; // Silence dead store warning due to idiomatic code.
1711 Out << ")";
1712 }
1713 std::string TypeStr = PDecl->getASTContext().getUnqualifiedObjCPointerType(type: T).
1714 getAsString(Policy);
1715 Out << ' ' << TypeStr;
1716 if (!StringRef(TypeStr).ends_with(Suffix: "*"))
1717 Out << ' ';
1718 Out << *PDecl;
1719 if (Policy.PolishForDeclaration)
1720 Out << ';';
1721}
1722
1723void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1724 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1725 Out << "@synthesize ";
1726 else
1727 Out << "@dynamic ";
1728 Out << *PID->getPropertyDecl();
1729 if (PID->getPropertyIvarDecl())
1730 Out << '=' << *PID->getPropertyIvarDecl();
1731}
1732
1733void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1734 if (!D->isAccessDeclaration())
1735 Out << "using ";
1736 if (D->hasTypename())
1737 Out << "typename ";
1738 D->getQualifier().print(OS&: Out, Policy);
1739
1740 // Use the correct record name when the using declaration is used for
1741 // inheriting constructors.
1742 for (const auto *Shadow : D->shadows()) {
1743 if (const auto *ConstructorShadow =
1744 dyn_cast<ConstructorUsingShadowDecl>(Val: Shadow)) {
1745 assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext());
1746 Out << *ConstructorShadow->getNominatedBaseClass();
1747 return;
1748 }
1749 }
1750 Out << *D;
1751}
1752
1753void DeclPrinter::VisitUsingEnumDecl(UsingEnumDecl *D) {
1754 Out << "using enum " << D->getEnumDecl();
1755}
1756
1757void
1758DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1759 Out << "using typename ";
1760 D->getQualifier().print(OS&: Out, Policy);
1761 Out << D->getDeclName();
1762}
1763
1764void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1765 if (!D->isAccessDeclaration())
1766 Out << "using ";
1767 D->getQualifier().print(OS&: Out, Policy);
1768 Out << D->getDeclName();
1769}
1770
1771void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1772 // ignore
1773}
1774
1775void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1776 Out << "#pragma omp threadprivate";
1777 if (!D->varlist_empty()) {
1778 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1779 E = D->varlist_end();
1780 I != E; ++I) {
1781 Out << (I == D->varlist_begin() ? '(' : ',');
1782 NamedDecl *ND = cast<DeclRefExpr>(Val: *I)->getDecl();
1783 ND->printQualifiedName(OS&: Out);
1784 }
1785 Out << ")";
1786 }
1787}
1788
1789void DeclPrinter::VisitHLSLBufferDecl(HLSLBufferDecl *D) {
1790 if (D->isCBuffer())
1791 Out << "cbuffer ";
1792 else
1793 Out << "tbuffer ";
1794
1795 Out << *D;
1796
1797 if (std::optional<std::string> Attrs = prettyPrintAttributes(D))
1798 Out << ' ' << *Attrs;
1799
1800 Out << " {\n";
1801 VisitDeclContext(DC: D);
1802 Indent() << "}";
1803}
1804
1805void DeclPrinter::VisitOMPAllocateDecl(OMPAllocateDecl *D) {
1806 Out << "#pragma omp allocate";
1807 if (!D->varlist_empty()) {
1808 for (OMPAllocateDecl::varlist_iterator I = D->varlist_begin(),
1809 E = D->varlist_end();
1810 I != E; ++I) {
1811 Out << (I == D->varlist_begin() ? '(' : ',');
1812 NamedDecl *ND = cast<DeclRefExpr>(Val: *I)->getDecl();
1813 ND->printQualifiedName(OS&: Out);
1814 }
1815 Out << ")";
1816 }
1817 if (!D->clauselist_empty()) {
1818 OMPClausePrinter Printer(Out, Policy, Context.getLangOpts().OpenMP);
1819 for (OMPClause *C : D->clauselists()) {
1820 Out << " ";
1821 Printer.Visit(S: C);
1822 }
1823 }
1824}
1825
1826void DeclPrinter::VisitOMPRequiresDecl(OMPRequiresDecl *D) {
1827 Out << "#pragma omp requires ";
1828 if (!D->clauselist_empty()) {
1829 OMPClausePrinter Printer(Out, Policy, Context.getLangOpts().OpenMP);
1830 for (auto I = D->clauselist_begin(), E = D->clauselist_end(); I != E; ++I)
1831 Printer.Visit(S: *I);
1832 }
1833}
1834
1835void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1836 if (!D->isInvalidDecl()) {
1837 Out << "#pragma omp declare reduction (";
1838 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
1839 const char *OpName =
1840 getOperatorSpelling(Operator: D->getDeclName().getCXXOverloadedOperator());
1841 assert(OpName && "not an overloaded operator");
1842 Out << OpName;
1843 } else {
1844 assert(D->getDeclName().isIdentifier());
1845 D->printName(OS&: Out, Policy);
1846 }
1847 Out << " : ";
1848 D->getType().print(OS&: Out, Policy);
1849 Out << " : ";
1850 D->getCombiner()->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation: 0, NewlineSymbol: "\n", Context: &Context);
1851 Out << ")";
1852 if (auto *Init = D->getInitializer()) {
1853 Out << " initializer(";
1854 switch (D->getInitializerKind()) {
1855 case OMPDeclareReductionInitKind::Direct:
1856 Out << "omp_priv(";
1857 break;
1858 case OMPDeclareReductionInitKind::Copy:
1859 Out << "omp_priv = ";
1860 break;
1861 case OMPDeclareReductionInitKind::Call:
1862 break;
1863 }
1864 Init->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation: 0, NewlineSymbol: "\n", Context: &Context);
1865 if (D->getInitializerKind() == OMPDeclareReductionInitKind::Direct)
1866 Out << ")";
1867 Out << ")";
1868 }
1869 }
1870}
1871
1872void DeclPrinter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
1873 if (!D->isInvalidDecl()) {
1874 Out << "#pragma omp declare mapper (";
1875 D->printName(OS&: Out, Policy);
1876 Out << " : ";
1877 D->getType().print(OS&: Out, Policy);
1878 Out << " ";
1879 Out << D->getVarName();
1880 Out << ")";
1881 if (!D->clauselist_empty()) {
1882 OMPClausePrinter Printer(Out, Policy, Context.getLangOpts().OpenMP);
1883 for (auto *C : D->clauselists()) {
1884 Out << " ";
1885 Printer.Visit(S: C);
1886 }
1887 }
1888 }
1889}
1890
1891void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
1892 D->getInit()->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation, NewlineSymbol: "\n", Context: &Context);
1893}
1894
1895void DeclPrinter::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *TTP) {
1896 if (const TypeConstraint *TC = TTP->getTypeConstraint())
1897 TC->print(OS&: Out, Policy);
1898 else if (TTP->wasDeclaredWithTypename())
1899 Out << "typename";
1900 else
1901 Out << "class";
1902
1903 if (TTP->isParameterPack())
1904 Out << " ...";
1905 else if (TTP->getDeclName())
1906 Out << ' ';
1907
1908 if (TTP->getDeclName()) {
1909 if (Policy.CleanUglifiedParameters && TTP->getIdentifier())
1910 Out << TTP->getIdentifier()->deuglifiedName();
1911 else
1912 Out << TTP->getDeclName();
1913 }
1914
1915 if (TTP->hasDefaultArgument() && !TTP->defaultArgumentWasInherited()) {
1916 Out << " = ";
1917 TTP->getDefaultArgument().getArgument().print(Policy, Out,
1918 /*IncludeType=*/false);
1919 }
1920}
1921
1922void DeclPrinter::VisitNonTypeTemplateParmDecl(
1923 const NonTypeTemplateParmDecl *NTTP) {
1924 StringRef Name;
1925 if (IdentifierInfo *II = NTTP->getIdentifier())
1926 Name =
1927 Policy.CleanUglifiedParameters ? II->deuglifiedName() : II->getName();
1928 printDeclType(T: NTTP->getType(), DeclName: Name, Pack: NTTP->isParameterPack());
1929
1930 if (NTTP->hasDefaultArgument() && !NTTP->defaultArgumentWasInherited()) {
1931 Out << " = ";
1932 NTTP->getDefaultArgument().getArgument().print(Policy, Out,
1933 /*IncludeType=*/false);
1934 }
1935}
1936
1937void DeclPrinter::VisitTemplateTemplateParmDecl(
1938 const TemplateTemplateParmDecl *TTPD) {
1939 VisitTemplateDecl(D: TTPD);
1940 if (TTPD->hasDefaultArgument() && !TTPD->defaultArgumentWasInherited()) {
1941 Out << " = ";
1942 TTPD->getDefaultArgument().getArgument().print(Policy, Out,
1943 /*IncludeType=*/false);
1944 }
1945}
1946
1947void DeclPrinter::VisitOpenACCDeclareDecl(OpenACCDeclareDecl *D) {
1948 if (!D->isInvalidDecl()) {
1949 Out << "#pragma acc declare";
1950 if (!D->clauses().empty()) {
1951 Out << ' ';
1952 OpenACCClausePrinter Printer(Out, Policy);
1953 Printer.VisitClauseList(List: D->clauses());
1954 }
1955 }
1956}
1957void DeclPrinter::VisitOpenACCRoutineDecl(OpenACCRoutineDecl *D) {
1958 if (!D->isInvalidDecl()) {
1959 Out << "#pragma acc routine";
1960
1961 Out << "(";
1962
1963 // The referenced function was named here, but this makes us tolerant of
1964 // errors.
1965 if (D->getFunctionReference())
1966 D->getFunctionReference()->printPretty(OS&: Out, Helper: nullptr, Policy, Indentation,
1967 NewlineSymbol: "\n", Context: &Context);
1968 else
1969 Out << "<error>";
1970
1971 Out << ")";
1972
1973 if (!D->clauses().empty()) {
1974 Out << ' ';
1975 OpenACCClausePrinter Printer(Out, Policy);
1976 Printer.VisitClauseList(List: D->clauses());
1977 }
1978 }
1979}
1980