1//===- StmtPrinter.cpp - Printing implementation for Stmt 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 Stmt::dumpPretty/Stmt::printPretty methods, which
10// pretty print the AST back out to C code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Attr.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclBase.h"
18#include "clang/AST/DeclCXX.h"
19#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclOpenACC.h"
21#include "clang/AST/DeclOpenMP.h"
22#include "clang/AST/DeclTemplate.h"
23#include "clang/AST/Expr.h"
24#include "clang/AST/ExprCXX.h"
25#include "clang/AST/ExprObjC.h"
26#include "clang/AST/ExprOpenMP.h"
27#include "clang/AST/NestedNameSpecifier.h"
28#include "clang/AST/OpenMPClause.h"
29#include "clang/AST/PrettyPrinter.h"
30#include "clang/AST/Stmt.h"
31#include "clang/AST/StmtCXX.h"
32#include "clang/AST/StmtObjC.h"
33#include "clang/AST/StmtOpenMP.h"
34#include "clang/AST/StmtSYCL.h"
35#include "clang/AST/StmtVisitor.h"
36#include "clang/AST/TemplateBase.h"
37#include "clang/AST/Type.h"
38#include "clang/Basic/ExpressionTraits.h"
39#include "clang/Basic/IdentifierTable.h"
40#include "clang/Basic/JsonSupport.h"
41#include "clang/Basic/LLVM.h"
42#include "clang/Basic/Lambda.h"
43#include "clang/Basic/OpenMPKinds.h"
44#include "clang/Basic/OperatorKinds.h"
45#include "clang/Basic/SourceLocation.h"
46#include "clang/Basic/TypeTraits.h"
47#include "clang/Lex/Lexer.h"
48#include "llvm/ADT/ArrayRef.h"
49#include "llvm/ADT/STLExtras.h"
50#include "llvm/ADT/StringExtras.h"
51#include "llvm/ADT/StringRef.h"
52#include "llvm/Support/Compiler.h"
53#include "llvm/Support/ErrorHandling.h"
54#include "llvm/Support/raw_ostream.h"
55#include <cassert>
56#include <optional>
57#include <string>
58
59using namespace clang;
60
61//===----------------------------------------------------------------------===//
62// StmtPrinter Visitor
63//===----------------------------------------------------------------------===//
64
65namespace {
66
67 class StmtPrinter : public StmtVisitor<StmtPrinter> {
68 raw_ostream &OS;
69 unsigned IndentLevel;
70 PrinterHelper* Helper;
71 PrintingPolicy Policy;
72 std::string NL;
73 const ASTContext *Context;
74
75 public:
76 StmtPrinter(raw_ostream &os, PrinterHelper *helper,
77 const PrintingPolicy &Policy, unsigned Indentation = 0,
78 StringRef NL = "\n", const ASTContext *Context = nullptr)
79 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy),
80 NL(NL), Context(Context) {}
81
82 void PrintStmt(Stmt *S) { PrintStmt(S, SubIndent: Policy.Indentation); }
83
84 void PrintStmt(Stmt *S, int SubIndent) {
85 IndentLevel += SubIndent;
86 if (isa_and_nonnull<Expr>(Val: S)) {
87 // If this is an expr used in a stmt context, indent and newline it.
88 Indent();
89 Visit(S);
90 OS << ";" << NL;
91 } else if (S) {
92 Visit(S);
93 } else {
94 Indent() << "<<<NULL STATEMENT>>>" << NL;
95 }
96 IndentLevel -= SubIndent;
97 }
98
99 void PrintInitStmt(Stmt *S, unsigned PrefixWidth) {
100 // FIXME: Cope better with odd prefix widths.
101 IndentLevel += (PrefixWidth + 1) / 2;
102 if (auto *DS = dyn_cast<DeclStmt>(Val: S))
103 PrintRawDeclStmt(S: DS);
104 else
105 PrintExpr(E: cast<Expr>(Val: S));
106 OS << "; ";
107 IndentLevel -= (PrefixWidth + 1) / 2;
108 }
109
110 void PrintControlledStmt(Stmt *S) {
111 if (auto *CS = dyn_cast<CompoundStmt>(Val: S)) {
112 OS << " ";
113 PrintRawCompoundStmt(S: CS);
114 OS << NL;
115 } else {
116 OS << NL;
117 PrintStmt(S);
118 }
119 }
120
121 void PrintRawCompoundStmt(CompoundStmt *S);
122 void PrintRawDecl(Decl *D);
123 void PrintRawDeclStmt(const DeclStmt *S);
124 void PrintRawIfStmt(IfStmt *If);
125 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
126 void PrintCallArgs(CallExpr *E);
127 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
128 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
129 void PrintOMPExecutableDirective(OMPExecutableDirective *S,
130 bool ForceNoStmt = false);
131 void PrintFPPragmas(CompoundStmt *S);
132 void PrintOpenACCClauseList(OpenACCConstructStmt *S);
133 void PrintOpenACCConstruct(OpenACCConstructStmt *S);
134
135 void PrintExpr(Expr *E) {
136 if (E)
137 Visit(S: E);
138 else
139 OS << "<null expr>";
140 }
141
142 raw_ostream &Indent(int Delta = 0) {
143 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
144 OS << " ";
145 return OS;
146 }
147
148 void Visit(Stmt* S) {
149 if (Helper && Helper->handledStmt(E: S,OS))
150 return;
151 else StmtVisitor<StmtPrinter>::Visit(S);
152 }
153
154 [[maybe_unused]] void VisitStmt(Stmt *Node) {
155 Indent() << "<<unknown stmt type>>" << NL;
156 }
157
158 [[maybe_unused]] void VisitExpr(Expr *Node) {
159 OS << "<<unknown expr type>>";
160 }
161
162 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
163
164#define ABSTRACT_STMT(CLASS)
165#define STMT(CLASS, PARENT) \
166 void Visit##CLASS(CLASS *Node);
167#include "clang/AST/StmtNodes.inc"
168 };
169
170} // namespace
171
172//===----------------------------------------------------------------------===//
173// Stmt printing methods.
174//===----------------------------------------------------------------------===//
175
176/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
177/// with no newline after the }.
178void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
179 assert(Node && "Compound statement cannot be null");
180 OS << "{" << NL;
181 PrintFPPragmas(S: Node);
182 for (auto *I : Node->body())
183 PrintStmt(S: I);
184
185 Indent() << "}";
186}
187
188void StmtPrinter::PrintFPPragmas(CompoundStmt *S) {
189 if (!S->hasStoredFPFeatures())
190 return;
191 FPOptionsOverride FPO = S->getStoredFPFeatures();
192 bool FEnvAccess = false;
193 if (FPO.hasAllowFEnvAccessOverride()) {
194 FEnvAccess = FPO.getAllowFEnvAccessOverride();
195 Indent() << "#pragma STDC FENV_ACCESS " << (FEnvAccess ? "ON" : "OFF")
196 << NL;
197 }
198 if (FPO.hasSpecifiedExceptionModeOverride()) {
199 LangOptions::FPExceptionModeKind EM =
200 FPO.getSpecifiedExceptionModeOverride();
201 if (!FEnvAccess || EM != LangOptions::FPE_Strict) {
202 Indent() << "#pragma clang fp exceptions(";
203 switch (FPO.getSpecifiedExceptionModeOverride()) {
204 default:
205 break;
206 case LangOptions::FPE_Ignore:
207 OS << "ignore";
208 break;
209 case LangOptions::FPE_MayTrap:
210 OS << "maytrap";
211 break;
212 case LangOptions::FPE_Strict:
213 OS << "strict";
214 break;
215 }
216 OS << ")\n";
217 }
218 }
219 if (FPO.hasConstRoundingModeOverride()) {
220 LangOptions::RoundingMode RM = FPO.getConstRoundingModeOverride();
221 Indent() << "#pragma STDC FENV_ROUND ";
222 switch (RM) {
223 case llvm::RoundingMode::TowardZero:
224 OS << "FE_TOWARDZERO";
225 break;
226 case llvm::RoundingMode::NearestTiesToEven:
227 OS << "FE_TONEAREST";
228 break;
229 case llvm::RoundingMode::TowardPositive:
230 OS << "FE_UPWARD";
231 break;
232 case llvm::RoundingMode::TowardNegative:
233 OS << "FE_DOWNWARD";
234 break;
235 case llvm::RoundingMode::NearestTiesToAway:
236 OS << "FE_TONEARESTFROMZERO";
237 break;
238 case llvm::RoundingMode::Dynamic:
239 OS << "FE_DYNAMIC";
240 break;
241 default:
242 llvm_unreachable("Invalid rounding mode");
243 }
244 OS << NL;
245 }
246}
247
248void StmtPrinter::PrintRawDecl(Decl *D) {
249 D->print(Out&: OS, Policy, Indentation: IndentLevel);
250}
251
252void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
253 SmallVector<Decl *, 2> Decls(S->decls());
254 Decl::printGroup(Begin: Decls.data(), NumDecls: Decls.size(), Out&: OS, Policy, Indentation: IndentLevel);
255}
256
257void StmtPrinter::VisitNullStmt(NullStmt *Node) {
258 Indent() << ";" << NL;
259}
260
261void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
262 Indent();
263 PrintRawDeclStmt(S: Node);
264 // Certain pragma declarations shouldn't have a semi-colon after them.
265 if (!Node->isSingleDecl() ||
266 !isa<OpenACCDeclareDecl, OpenACCRoutineDecl>(Val: Node->getSingleDecl()))
267 OS << ";";
268 OS << NL;
269}
270
271void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
272 Indent();
273 PrintRawCompoundStmt(Node);
274 OS << "" << NL;
275}
276
277void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
278 Indent(Delta: -1) << "case ";
279 PrintExpr(E: Node->getLHS());
280 if (Node->getRHS()) {
281 OS << " ... ";
282 PrintExpr(E: Node->getRHS());
283 }
284 OS << ":" << NL;
285
286 PrintStmt(S: Node->getSubStmt(), SubIndent: 0);
287}
288
289void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
290 Indent(Delta: -1) << "default:" << NL;
291 PrintStmt(S: Node->getSubStmt(), SubIndent: 0);
292}
293
294void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
295 Indent(Delta: -1) << Node->getName() << ":" << NL;
296 PrintStmt(S: Node->getSubStmt(), SubIndent: 0);
297}
298
299void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
300 ArrayRef<const Attr *> Attrs = Node->getAttrs();
301 for (const auto *Attr : Attrs) {
302 Attr->printPretty(OS, Policy);
303 if (Attr != Attrs.back())
304 OS << ' ';
305 }
306
307 PrintStmt(S: Node->getSubStmt(), SubIndent: 0);
308}
309
310void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
311 if (If->isConsteval()) {
312 OS << "if ";
313 if (If->isNegatedConsteval())
314 OS << "!";
315 OS << "consteval";
316 OS << NL;
317 PrintStmt(S: If->getThen());
318 if (Stmt *Else = If->getElse()) {
319 Indent();
320 OS << "else";
321 PrintStmt(S: Else);
322 OS << NL;
323 }
324 return;
325 }
326
327 OS << "if (";
328 if (If->getInit())
329 PrintInitStmt(S: If->getInit(), PrefixWidth: 4);
330 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
331 PrintRawDeclStmt(S: DS);
332 else
333 PrintExpr(E: If->getCond());
334 OS << ')';
335
336 if (auto *CS = dyn_cast<CompoundStmt>(Val: If->getThen())) {
337 OS << ' ';
338 PrintRawCompoundStmt(Node: CS);
339 OS << (If->getElse() ? " " : NL);
340 } else {
341 OS << NL;
342 PrintStmt(S: If->getThen());
343 if (If->getElse()) Indent();
344 }
345
346 if (Stmt *Else = If->getElse()) {
347 OS << "else";
348
349 if (auto *CS = dyn_cast<CompoundStmt>(Val: Else)) {
350 OS << ' ';
351 PrintRawCompoundStmt(Node: CS);
352 OS << NL;
353 } else if (auto *ElseIf = dyn_cast<IfStmt>(Val: Else)) {
354 OS << ' ';
355 PrintRawIfStmt(If: ElseIf);
356 } else {
357 OS << NL;
358 PrintStmt(S: If->getElse());
359 }
360 }
361}
362
363void StmtPrinter::VisitIfStmt(IfStmt *If) {
364 Indent();
365 PrintRawIfStmt(If);
366}
367
368void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
369 Indent() << "switch (";
370 if (Node->getInit())
371 PrintInitStmt(S: Node->getInit(), PrefixWidth: 8);
372 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
373 PrintRawDeclStmt(S: DS);
374 else
375 PrintExpr(E: Node->getCond());
376 OS << ")";
377 PrintControlledStmt(S: Node->getBody());
378}
379
380void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
381 Indent() << "while (";
382 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
383 PrintRawDeclStmt(S: DS);
384 else
385 PrintExpr(E: Node->getCond());
386 OS << ")" << NL;
387 PrintStmt(S: Node->getBody());
388}
389
390void StmtPrinter::VisitDoStmt(DoStmt *Node) {
391 Indent() << "do ";
392 if (auto *CS = dyn_cast<CompoundStmt>(Val: Node->getBody())) {
393 PrintRawCompoundStmt(Node: CS);
394 OS << " ";
395 } else {
396 OS << NL;
397 PrintStmt(S: Node->getBody());
398 Indent();
399 }
400
401 OS << "while (";
402 PrintExpr(E: Node->getCond());
403 OS << ");" << NL;
404}
405
406void StmtPrinter::VisitForStmt(ForStmt *Node) {
407 Indent() << "for (";
408 if (Node->getInit())
409 PrintInitStmt(S: Node->getInit(), PrefixWidth: 5);
410 else
411 OS << (Node->getCond() ? "; " : ";");
412 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
413 PrintRawDeclStmt(S: DS);
414 else if (Node->getCond())
415 PrintExpr(E: Node->getCond());
416 OS << ";";
417 if (Node->getInc()) {
418 OS << " ";
419 PrintExpr(E: Node->getInc());
420 }
421 OS << ")";
422 PrintControlledStmt(S: Node->getBody());
423}
424
425void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
426 Indent() << "for (";
427 if (auto *DS = dyn_cast<DeclStmt>(Val: Node->getElement()))
428 PrintRawDeclStmt(S: DS);
429 else
430 PrintExpr(E: cast<Expr>(Val: Node->getElement()));
431 OS << " in ";
432 PrintExpr(E: Node->getCollection());
433 OS << ")";
434 PrintControlledStmt(S: Node->getBody());
435}
436
437void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
438 Indent() << "for (";
439 if (Node->getInit())
440 PrintInitStmt(S: Node->getInit(), PrefixWidth: 5);
441 PrintingPolicy SubPolicy(Policy);
442 SubPolicy.SuppressInitializers = true;
443 Node->getLoopVariable()->print(Out&: OS, Policy: SubPolicy, Indentation: IndentLevel);
444 OS << " : ";
445 PrintExpr(E: Node->getRangeInit());
446 OS << ")";
447 PrintControlledStmt(S: Node->getBody());
448}
449
450void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
451 Indent();
452 if (Node->isIfExists())
453 OS << "__if_exists (";
454 else
455 OS << "__if_not_exists (";
456
457 Node->getQualifierLoc().getNestedNameSpecifier().print(OS, Policy);
458 OS << Node->getNameInfo() << ") ";
459
460 PrintRawCompoundStmt(Node: Node->getSubStmt());
461}
462
463void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
464 Indent() << "goto " << Node->getLabel()->getName() << ";";
465 if (Policy.IncludeNewlines) OS << NL;
466}
467
468void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
469 Indent() << "goto *";
470 PrintExpr(E: Node->getTarget());
471 OS << ";";
472 if (Policy.IncludeNewlines) OS << NL;
473}
474
475void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
476 Indent();
477 if (Node->hasLabelTarget())
478 OS << "continue " << Node->getLabelDecl()->getIdentifier()->getName()
479 << ';';
480 else
481 OS << "continue;";
482 if (Policy.IncludeNewlines) OS << NL;
483}
484
485void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
486 Indent();
487 if (Node->hasLabelTarget())
488 OS << "break " << Node->getLabelDecl()->getIdentifier()->getName() << ';';
489 else
490 OS << "break;";
491 if (Policy.IncludeNewlines) OS << NL;
492}
493
494void StmtPrinter::VisitDeferStmt(DeferStmt *Node) {
495 Indent() << "_Defer";
496 PrintControlledStmt(S: Node->getBody());
497}
498
499void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
500 Indent() << "return";
501 if (Node->getRetValue()) {
502 OS << " ";
503 PrintExpr(E: Node->getRetValue());
504 }
505 OS << ";";
506 if (Policy.IncludeNewlines) OS << NL;
507}
508
509void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
510 Indent() << "asm ";
511
512 if (Node->isVolatile())
513 OS << "volatile ";
514
515 if (Node->isAsmGoto())
516 OS << "goto ";
517
518 OS << "(";
519 Visit(S: Node->getAsmStringExpr());
520
521 // Outputs
522 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
523 Node->getNumClobbers() != 0 || Node->getNumLabels() != 0)
524 OS << " : ";
525
526 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
527 if (i != 0)
528 OS << ", ";
529
530 if (!Node->getOutputName(i).empty()) {
531 OS << '[';
532 OS << Node->getOutputName(i);
533 OS << "] ";
534 }
535
536 Visit(S: Node->getOutputConstraintExpr(i));
537 OS << " (";
538 Visit(S: Node->getOutputExpr(i));
539 OS << ")";
540 }
541
542 // Inputs
543 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0 ||
544 Node->getNumLabels() != 0)
545 OS << " : ";
546
547 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
548 if (i != 0)
549 OS << ", ";
550
551 if (!Node->getInputName(i).empty()) {
552 OS << '[';
553 OS << Node->getInputName(i);
554 OS << "] ";
555 }
556
557 Visit(S: Node->getInputConstraintExpr(i));
558 OS << " (";
559 Visit(S: Node->getInputExpr(i));
560 OS << ")";
561 }
562
563 // Clobbers
564 if (Node->getNumClobbers() != 0 || Node->getNumLabels())
565 OS << " : ";
566
567 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
568 if (i != 0)
569 OS << ", ";
570
571 Visit(S: Node->getClobberExpr(i));
572 }
573
574 // Labels
575 if (Node->getNumLabels() != 0)
576 OS << " : ";
577
578 for (unsigned i = 0, e = Node->getNumLabels(); i != e; ++i) {
579 if (i != 0)
580 OS << ", ";
581 OS << Node->getLabelName(i);
582 }
583
584 OS << ");";
585 if (Policy.IncludeNewlines) OS << NL;
586}
587
588void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
589 // FIXME: Implement MS style inline asm statement printer.
590 Indent() << "__asm ";
591 if (Node->hasBraces())
592 OS << "{" << NL;
593 OS << Node->getAsmString() << NL;
594 if (Node->hasBraces())
595 Indent() << "}" << NL;
596}
597
598void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
599 PrintStmt(S: Node->getCapturedDecl()->getBody());
600}
601
602void StmtPrinter::VisitSYCLKernelCallStmt(SYCLKernelCallStmt *Node) {
603 PrintStmt(S: Node->getOutlinedFunctionDecl()->getBody());
604}
605
606void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
607 Indent() << "@try";
608 if (auto *TS = dyn_cast<CompoundStmt>(Val: Node->getTryBody())) {
609 PrintRawCompoundStmt(Node: TS);
610 OS << NL;
611 }
612
613 for (ObjCAtCatchStmt *catchStmt : Node->catch_stmts()) {
614 Indent() << "@catch(";
615 if (Decl *DS = catchStmt->getCatchParamDecl())
616 PrintRawDecl(D: DS);
617 OS << ")";
618 if (auto *CS = dyn_cast<CompoundStmt>(Val: catchStmt->getCatchBody())) {
619 PrintRawCompoundStmt(Node: CS);
620 OS << NL;
621 }
622 }
623
624 if (ObjCAtFinallyStmt *FS = Node->getFinallyStmt()) {
625 Indent() << "@finally";
626 if (auto *CS = dyn_cast<CompoundStmt>(Val: FS->getFinallyBody())) {
627 PrintRawCompoundStmt(Node: CS);
628 OS << NL;
629 }
630 }
631}
632
633void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
634}
635
636void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
637 Indent() << "@catch (...) { /* todo */ } " << NL;
638}
639
640void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
641 Indent() << "@throw";
642 if (Node->getThrowExpr()) {
643 OS << " ";
644 PrintExpr(E: Node->getThrowExpr());
645 }
646 OS << ";" << NL;
647}
648
649void StmtPrinter::VisitObjCAvailabilityCheckExpr(
650 ObjCAvailabilityCheckExpr *Node) {
651 OS << "@available(...)";
652}
653
654void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
655 Indent() << "@synchronized (";
656 PrintExpr(E: Node->getSynchExpr());
657 OS << ")";
658 PrintRawCompoundStmt(Node: Node->getSynchBody());
659 OS << NL;
660}
661
662void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
663 Indent() << "@autoreleasepool";
664 PrintRawCompoundStmt(Node: cast<CompoundStmt>(Val: Node->getSubStmt()));
665 OS << NL;
666}
667
668void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
669 OS << "catch (";
670 if (Decl *ExDecl = Node->getExceptionDecl())
671 PrintRawDecl(D: ExDecl);
672 else
673 OS << "...";
674 OS << ") ";
675 PrintRawCompoundStmt(Node: cast<CompoundStmt>(Val: Node->getHandlerBlock()));
676}
677
678void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
679 Indent();
680 PrintRawCXXCatchStmt(Node);
681 OS << NL;
682}
683
684void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
685 Indent() << "try ";
686 PrintRawCompoundStmt(Node: Node->getTryBlock());
687 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
688 OS << " ";
689 PrintRawCXXCatchStmt(Node: Node->getHandler(i));
690 }
691 OS << NL;
692}
693
694void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
695 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
696 PrintRawCompoundStmt(Node: Node->getTryBlock());
697 SEHExceptStmt *E = Node->getExceptHandler();
698 SEHFinallyStmt *F = Node->getFinallyHandler();
699 if(E)
700 PrintRawSEHExceptHandler(S: E);
701 else {
702 assert(F && "Must have a finally block...");
703 PrintRawSEHFinallyStmt(S: F);
704 }
705 OS << NL;
706}
707
708void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
709 OS << "__finally ";
710 PrintRawCompoundStmt(Node: Node->getBlock());
711 OS << NL;
712}
713
714void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
715 OS << "__except (";
716 VisitExpr(Node: Node->getFilterExpr());
717 OS << ")" << NL;
718 PrintRawCompoundStmt(Node: Node->getBlock());
719 OS << NL;
720}
721
722void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
723 Indent();
724 PrintRawSEHExceptHandler(Node);
725 OS << NL;
726}
727
728void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
729 Indent();
730 PrintRawSEHFinallyStmt(Node);
731 OS << NL;
732}
733
734void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
735 Indent() << "__leave;";
736 if (Policy.IncludeNewlines) OS << NL;
737}
738
739//===----------------------------------------------------------------------===//
740// OpenMP directives printing methods
741//===----------------------------------------------------------------------===//
742
743void StmtPrinter::VisitOMPCanonicalLoop(OMPCanonicalLoop *Node) {
744 PrintStmt(S: Node->getLoopStmt());
745}
746
747void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S,
748 bool ForceNoStmt) {
749 unsigned OpenMPVersion =
750 Context ? Context->getLangOpts().OpenMP : llvm::omp::FallbackVersion;
751 OMPClausePrinter Printer(OS, Policy, OpenMPVersion);
752 ArrayRef<OMPClause *> Clauses = S->clauses();
753 for (auto *Clause : Clauses)
754 if (Clause && !Clause->isImplicit()) {
755 OS << ' ';
756 Printer.Visit(S: Clause);
757 }
758 OS << NL;
759 if (!ForceNoStmt && S->hasAssociatedStmt())
760 PrintStmt(S: S->getRawStmt());
761}
762
763void StmtPrinter::VisitOMPMetaDirective(OMPMetaDirective *Node) {
764 Indent() << "#pragma omp metadirective";
765 PrintOMPExecutableDirective(S: Node);
766}
767
768void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
769 Indent() << "#pragma omp parallel";
770 PrintOMPExecutableDirective(S: Node);
771}
772
773void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
774 Indent() << "#pragma omp simd";
775 PrintOMPExecutableDirective(S: Node);
776}
777
778void StmtPrinter::VisitOMPTileDirective(OMPTileDirective *Node) {
779 Indent() << "#pragma omp tile";
780 PrintOMPExecutableDirective(S: Node);
781}
782
783void StmtPrinter::VisitOMPStripeDirective(OMPStripeDirective *Node) {
784 Indent() << "#pragma omp stripe";
785 PrintOMPExecutableDirective(S: Node);
786}
787
788void StmtPrinter::VisitOMPUnrollDirective(OMPUnrollDirective *Node) {
789 Indent() << "#pragma omp unroll";
790 PrintOMPExecutableDirective(S: Node);
791}
792
793void StmtPrinter::VisitOMPReverseDirective(OMPReverseDirective *Node) {
794 Indent() << "#pragma omp reverse";
795 PrintOMPExecutableDirective(S: Node);
796}
797
798void StmtPrinter::VisitOMPInterchangeDirective(OMPInterchangeDirective *Node) {
799 Indent() << "#pragma omp interchange";
800 PrintOMPExecutableDirective(S: Node);
801}
802
803void StmtPrinter::VisitOMPFuseDirective(OMPFuseDirective *Node) {
804 Indent() << "#pragma omp fuse";
805 PrintOMPExecutableDirective(S: Node);
806}
807
808void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
809 Indent() << "#pragma omp for";
810 PrintOMPExecutableDirective(S: Node);
811}
812
813void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
814 Indent() << "#pragma omp for simd";
815 PrintOMPExecutableDirective(S: Node);
816}
817
818void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
819 Indent() << "#pragma omp sections";
820 PrintOMPExecutableDirective(S: Node);
821}
822
823void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
824 Indent() << "#pragma omp section";
825 PrintOMPExecutableDirective(S: Node);
826}
827
828void StmtPrinter::VisitOMPScopeDirective(OMPScopeDirective *Node) {
829 Indent() << "#pragma omp scope";
830 PrintOMPExecutableDirective(S: Node);
831}
832
833void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
834 Indent() << "#pragma omp single";
835 PrintOMPExecutableDirective(S: Node);
836}
837
838void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
839 Indent() << "#pragma omp master";
840 PrintOMPExecutableDirective(S: Node);
841}
842
843void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
844 Indent() << "#pragma omp critical";
845 if (Node->getDirectiveName().getName()) {
846 OS << " (";
847 Node->getDirectiveName().printName(OS, Policy);
848 OS << ")";
849 }
850 PrintOMPExecutableDirective(S: Node);
851}
852
853void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
854 Indent() << "#pragma omp parallel for";
855 PrintOMPExecutableDirective(S: Node);
856}
857
858void StmtPrinter::VisitOMPParallelForSimdDirective(
859 OMPParallelForSimdDirective *Node) {
860 Indent() << "#pragma omp parallel for simd";
861 PrintOMPExecutableDirective(S: Node);
862}
863
864void StmtPrinter::VisitOMPParallelMasterDirective(
865 OMPParallelMasterDirective *Node) {
866 Indent() << "#pragma omp parallel master";
867 PrintOMPExecutableDirective(S: Node);
868}
869
870void StmtPrinter::VisitOMPParallelMaskedDirective(
871 OMPParallelMaskedDirective *Node) {
872 Indent() << "#pragma omp parallel masked";
873 PrintOMPExecutableDirective(S: Node);
874}
875
876void StmtPrinter::VisitOMPParallelSectionsDirective(
877 OMPParallelSectionsDirective *Node) {
878 Indent() << "#pragma omp parallel sections";
879 PrintOMPExecutableDirective(S: Node);
880}
881
882void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
883 Indent() << "#pragma omp task";
884 PrintOMPExecutableDirective(S: Node);
885}
886
887void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
888 Indent() << "#pragma omp taskyield";
889 PrintOMPExecutableDirective(S: Node);
890}
891
892void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
893 Indent() << "#pragma omp barrier";
894 PrintOMPExecutableDirective(S: Node);
895}
896
897void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
898 Indent() << "#pragma omp taskwait";
899 PrintOMPExecutableDirective(S: Node);
900}
901
902void StmtPrinter::VisitOMPAssumeDirective(OMPAssumeDirective *Node) {
903 Indent() << "#pragma omp assume";
904 PrintOMPExecutableDirective(S: Node);
905}
906
907void StmtPrinter::VisitOMPErrorDirective(OMPErrorDirective *Node) {
908 Indent() << "#pragma omp error";
909 PrintOMPExecutableDirective(S: Node);
910}
911
912void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
913 Indent() << "#pragma omp taskgroup";
914 PrintOMPExecutableDirective(S: Node);
915}
916
917void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
918 Indent() << "#pragma omp flush";
919 PrintOMPExecutableDirective(S: Node);
920}
921
922void StmtPrinter::VisitOMPDepobjDirective(OMPDepobjDirective *Node) {
923 Indent() << "#pragma omp depobj";
924 PrintOMPExecutableDirective(S: Node);
925}
926
927void StmtPrinter::VisitOMPScanDirective(OMPScanDirective *Node) {
928 Indent() << "#pragma omp scan";
929 PrintOMPExecutableDirective(S: Node);
930}
931
932void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
933 Indent() << "#pragma omp ordered";
934 PrintOMPExecutableDirective(S: Node, ForceNoStmt: Node->hasClausesOfKind<OMPDependClause>());
935}
936
937void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
938 Indent() << "#pragma omp atomic";
939 PrintOMPExecutableDirective(S: Node);
940}
941
942void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
943 Indent() << "#pragma omp target";
944 PrintOMPExecutableDirective(S: Node);
945}
946
947void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
948 Indent() << "#pragma omp target data";
949 PrintOMPExecutableDirective(S: Node);
950}
951
952void StmtPrinter::VisitOMPTargetEnterDataDirective(
953 OMPTargetEnterDataDirective *Node) {
954 Indent() << "#pragma omp target enter data";
955 PrintOMPExecutableDirective(S: Node, /*ForceNoStmt=*/true);
956}
957
958void StmtPrinter::VisitOMPTargetExitDataDirective(
959 OMPTargetExitDataDirective *Node) {
960 Indent() << "#pragma omp target exit data";
961 PrintOMPExecutableDirective(S: Node, /*ForceNoStmt=*/true);
962}
963
964void StmtPrinter::VisitOMPTargetParallelDirective(
965 OMPTargetParallelDirective *Node) {
966 Indent() << "#pragma omp target parallel";
967 PrintOMPExecutableDirective(S: Node);
968}
969
970void StmtPrinter::VisitOMPTargetParallelForDirective(
971 OMPTargetParallelForDirective *Node) {
972 Indent() << "#pragma omp target parallel for";
973 PrintOMPExecutableDirective(S: Node);
974}
975
976void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
977 Indent() << "#pragma omp teams";
978 PrintOMPExecutableDirective(S: Node);
979}
980
981void StmtPrinter::VisitOMPCancellationPointDirective(
982 OMPCancellationPointDirective *Node) {
983 unsigned OpenMPVersion =
984 Context ? Context->getLangOpts().OpenMP : llvm::omp::FallbackVersion;
985 Indent() << "#pragma omp cancellation point "
986 << getOpenMPDirectiveName(D: Node->getCancelRegion(), Ver: OpenMPVersion);
987 PrintOMPExecutableDirective(S: Node);
988}
989
990void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
991 unsigned OpenMPVersion =
992 Context ? Context->getLangOpts().OpenMP : llvm::omp::FallbackVersion;
993 Indent() << "#pragma omp cancel "
994 << getOpenMPDirectiveName(D: Node->getCancelRegion(), Ver: OpenMPVersion);
995 PrintOMPExecutableDirective(S: Node);
996}
997
998void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) {
999 Indent() << "#pragma omp taskloop";
1000 PrintOMPExecutableDirective(S: Node);
1001}
1002
1003void StmtPrinter::VisitOMPTaskLoopSimdDirective(
1004 OMPTaskLoopSimdDirective *Node) {
1005 Indent() << "#pragma omp taskloop simd";
1006 PrintOMPExecutableDirective(S: Node);
1007}
1008
1009void StmtPrinter::VisitOMPMasterTaskLoopDirective(
1010 OMPMasterTaskLoopDirective *Node) {
1011 Indent() << "#pragma omp master taskloop";
1012 PrintOMPExecutableDirective(S: Node);
1013}
1014
1015void StmtPrinter::VisitOMPMaskedTaskLoopDirective(
1016 OMPMaskedTaskLoopDirective *Node) {
1017 Indent() << "#pragma omp masked taskloop";
1018 PrintOMPExecutableDirective(S: Node);
1019}
1020
1021void StmtPrinter::VisitOMPMasterTaskLoopSimdDirective(
1022 OMPMasterTaskLoopSimdDirective *Node) {
1023 Indent() << "#pragma omp master taskloop simd";
1024 PrintOMPExecutableDirective(S: Node);
1025}
1026
1027void StmtPrinter::VisitOMPMaskedTaskLoopSimdDirective(
1028 OMPMaskedTaskLoopSimdDirective *Node) {
1029 Indent() << "#pragma omp masked taskloop simd";
1030 PrintOMPExecutableDirective(S: Node);
1031}
1032
1033void StmtPrinter::VisitOMPParallelMasterTaskLoopDirective(
1034 OMPParallelMasterTaskLoopDirective *Node) {
1035 Indent() << "#pragma omp parallel master taskloop";
1036 PrintOMPExecutableDirective(S: Node);
1037}
1038
1039void StmtPrinter::VisitOMPParallelMaskedTaskLoopDirective(
1040 OMPParallelMaskedTaskLoopDirective *Node) {
1041 Indent() << "#pragma omp parallel masked taskloop";
1042 PrintOMPExecutableDirective(S: Node);
1043}
1044
1045void StmtPrinter::VisitOMPParallelMasterTaskLoopSimdDirective(
1046 OMPParallelMasterTaskLoopSimdDirective *Node) {
1047 Indent() << "#pragma omp parallel master taskloop simd";
1048 PrintOMPExecutableDirective(S: Node);
1049}
1050
1051void StmtPrinter::VisitOMPParallelMaskedTaskLoopSimdDirective(
1052 OMPParallelMaskedTaskLoopSimdDirective *Node) {
1053 Indent() << "#pragma omp parallel masked taskloop simd";
1054 PrintOMPExecutableDirective(S: Node);
1055}
1056
1057void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) {
1058 Indent() << "#pragma omp distribute";
1059 PrintOMPExecutableDirective(S: Node);
1060}
1061
1062void StmtPrinter::VisitOMPTargetUpdateDirective(
1063 OMPTargetUpdateDirective *Node) {
1064 Indent() << "#pragma omp target update";
1065 PrintOMPExecutableDirective(S: Node, /*ForceNoStmt=*/true);
1066}
1067
1068void StmtPrinter::VisitOMPDistributeParallelForDirective(
1069 OMPDistributeParallelForDirective *Node) {
1070 Indent() << "#pragma omp distribute parallel for";
1071 PrintOMPExecutableDirective(S: Node);
1072}
1073
1074void StmtPrinter::VisitOMPDistributeParallelForSimdDirective(
1075 OMPDistributeParallelForSimdDirective *Node) {
1076 Indent() << "#pragma omp distribute parallel for simd";
1077 PrintOMPExecutableDirective(S: Node);
1078}
1079
1080void StmtPrinter::VisitOMPDistributeSimdDirective(
1081 OMPDistributeSimdDirective *Node) {
1082 Indent() << "#pragma omp distribute simd";
1083 PrintOMPExecutableDirective(S: Node);
1084}
1085
1086void StmtPrinter::VisitOMPTargetParallelForSimdDirective(
1087 OMPTargetParallelForSimdDirective *Node) {
1088 Indent() << "#pragma omp target parallel for simd";
1089 PrintOMPExecutableDirective(S: Node);
1090}
1091
1092void StmtPrinter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective *Node) {
1093 Indent() << "#pragma omp target simd";
1094 PrintOMPExecutableDirective(S: Node);
1095}
1096
1097void StmtPrinter::VisitOMPTeamsDistributeDirective(
1098 OMPTeamsDistributeDirective *Node) {
1099 Indent() << "#pragma omp teams distribute";
1100 PrintOMPExecutableDirective(S: Node);
1101}
1102
1103void StmtPrinter::VisitOMPTeamsDistributeSimdDirective(
1104 OMPTeamsDistributeSimdDirective *Node) {
1105 Indent() << "#pragma omp teams distribute simd";
1106 PrintOMPExecutableDirective(S: Node);
1107}
1108
1109void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective(
1110 OMPTeamsDistributeParallelForSimdDirective *Node) {
1111 Indent() << "#pragma omp teams distribute parallel for simd";
1112 PrintOMPExecutableDirective(S: Node);
1113}
1114
1115void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective(
1116 OMPTeamsDistributeParallelForDirective *Node) {
1117 Indent() << "#pragma omp teams distribute parallel for";
1118 PrintOMPExecutableDirective(S: Node);
1119}
1120
1121void StmtPrinter::VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective *Node) {
1122 Indent() << "#pragma omp target teams";
1123 PrintOMPExecutableDirective(S: Node);
1124}
1125
1126void StmtPrinter::VisitOMPTargetTeamsDistributeDirective(
1127 OMPTargetTeamsDistributeDirective *Node) {
1128 Indent() << "#pragma omp target teams distribute";
1129 PrintOMPExecutableDirective(S: Node);
1130}
1131
1132void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective(
1133 OMPTargetTeamsDistributeParallelForDirective *Node) {
1134 Indent() << "#pragma omp target teams distribute parallel for";
1135 PrintOMPExecutableDirective(S: Node);
1136}
1137
1138void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
1139 OMPTargetTeamsDistributeParallelForSimdDirective *Node) {
1140 Indent() << "#pragma omp target teams distribute parallel for simd";
1141 PrintOMPExecutableDirective(S: Node);
1142}
1143
1144void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective(
1145 OMPTargetTeamsDistributeSimdDirective *Node) {
1146 Indent() << "#pragma omp target teams distribute simd";
1147 PrintOMPExecutableDirective(S: Node);
1148}
1149
1150void StmtPrinter::VisitOMPInteropDirective(OMPInteropDirective *Node) {
1151 Indent() << "#pragma omp interop";
1152 PrintOMPExecutableDirective(S: Node);
1153}
1154
1155void StmtPrinter::VisitOMPDispatchDirective(OMPDispatchDirective *Node) {
1156 Indent() << "#pragma omp dispatch";
1157 PrintOMPExecutableDirective(S: Node);
1158}
1159
1160void StmtPrinter::VisitOMPMaskedDirective(OMPMaskedDirective *Node) {
1161 Indent() << "#pragma omp masked";
1162 PrintOMPExecutableDirective(S: Node);
1163}
1164
1165void StmtPrinter::VisitOMPGenericLoopDirective(OMPGenericLoopDirective *Node) {
1166 Indent() << "#pragma omp loop";
1167 PrintOMPExecutableDirective(S: Node);
1168}
1169
1170void StmtPrinter::VisitOMPTeamsGenericLoopDirective(
1171 OMPTeamsGenericLoopDirective *Node) {
1172 Indent() << "#pragma omp teams loop";
1173 PrintOMPExecutableDirective(S: Node);
1174}
1175
1176void StmtPrinter::VisitOMPTargetTeamsGenericLoopDirective(
1177 OMPTargetTeamsGenericLoopDirective *Node) {
1178 Indent() << "#pragma omp target teams loop";
1179 PrintOMPExecutableDirective(S: Node);
1180}
1181
1182void StmtPrinter::VisitOMPParallelGenericLoopDirective(
1183 OMPParallelGenericLoopDirective *Node) {
1184 Indent() << "#pragma omp parallel loop";
1185 PrintOMPExecutableDirective(S: Node);
1186}
1187
1188void StmtPrinter::VisitOMPTargetParallelGenericLoopDirective(
1189 OMPTargetParallelGenericLoopDirective *Node) {
1190 Indent() << "#pragma omp target parallel loop";
1191 PrintOMPExecutableDirective(S: Node);
1192}
1193
1194//===----------------------------------------------------------------------===//
1195// OpenACC construct printing methods
1196//===----------------------------------------------------------------------===//
1197void StmtPrinter::PrintOpenACCClauseList(OpenACCConstructStmt *S) {
1198 if (!S->clauses().empty()) {
1199 OS << ' ';
1200 OpenACCClausePrinter Printer(OS, Policy);
1201 Printer.VisitClauseList(List: S->clauses());
1202 }
1203}
1204void StmtPrinter::PrintOpenACCConstruct(OpenACCConstructStmt *S) {
1205 Indent() << "#pragma acc " << S->getDirectiveKind();
1206 PrintOpenACCClauseList(S);
1207 OS << '\n';
1208}
1209void StmtPrinter::VisitOpenACCComputeConstruct(OpenACCComputeConstruct *S) {
1210 PrintOpenACCConstruct(S);
1211 PrintStmt(S: S->getStructuredBlock());
1212}
1213
1214void StmtPrinter::VisitOpenACCLoopConstruct(OpenACCLoopConstruct *S) {
1215 PrintOpenACCConstruct(S);
1216 PrintStmt(S: S->getLoop());
1217}
1218
1219void StmtPrinter::VisitOpenACCCombinedConstruct(OpenACCCombinedConstruct *S) {
1220 PrintOpenACCConstruct(S);
1221 PrintStmt(S: S->getLoop());
1222}
1223
1224void StmtPrinter::VisitOpenACCDataConstruct(OpenACCDataConstruct *S) {
1225 PrintOpenACCConstruct(S);
1226 PrintStmt(S: S->getStructuredBlock());
1227}
1228void StmtPrinter::VisitOpenACCHostDataConstruct(OpenACCHostDataConstruct *S) {
1229 PrintOpenACCConstruct(S);
1230 PrintStmt(S: S->getStructuredBlock());
1231}
1232void StmtPrinter::VisitOpenACCEnterDataConstruct(OpenACCEnterDataConstruct *S) {
1233 PrintOpenACCConstruct(S);
1234}
1235void StmtPrinter::VisitOpenACCExitDataConstruct(OpenACCExitDataConstruct *S) {
1236 PrintOpenACCConstruct(S);
1237}
1238void StmtPrinter::VisitOpenACCInitConstruct(OpenACCInitConstruct *S) {
1239 PrintOpenACCConstruct(S);
1240}
1241void StmtPrinter::VisitOpenACCShutdownConstruct(OpenACCShutdownConstruct *S) {
1242 PrintOpenACCConstruct(S);
1243}
1244void StmtPrinter::VisitOpenACCSetConstruct(OpenACCSetConstruct *S) {
1245 PrintOpenACCConstruct(S);
1246}
1247void StmtPrinter::VisitOpenACCUpdateConstruct(OpenACCUpdateConstruct *S) {
1248 PrintOpenACCConstruct(S);
1249}
1250
1251void StmtPrinter::VisitOpenACCWaitConstruct(OpenACCWaitConstruct *S) {
1252 Indent() << "#pragma acc wait";
1253 if (!S->getLParenLoc().isInvalid()) {
1254 OS << "(";
1255 if (S->hasDevNumExpr()) {
1256 OS << "devnum: ";
1257 S->getDevNumExpr()->printPretty(OS, Helper: nullptr, Policy);
1258 OS << " : ";
1259 }
1260
1261 if (S->hasQueuesTag())
1262 OS << "queues: ";
1263
1264 llvm::interleaveComma(c: S->getQueueIdExprs(), os&: OS, each_fn: [&](const Expr *E) {
1265 E->printPretty(OS, Helper: nullptr, Policy);
1266 });
1267
1268 OS << ")";
1269 }
1270
1271 PrintOpenACCClauseList(S);
1272 OS << '\n';
1273}
1274
1275void StmtPrinter::VisitOpenACCAtomicConstruct(OpenACCAtomicConstruct *S) {
1276 Indent() << "#pragma acc atomic";
1277
1278 if (S->getAtomicKind() != OpenACCAtomicKind::None)
1279 OS << " " << S->getAtomicKind();
1280
1281 PrintOpenACCClauseList(S);
1282 OS << '\n';
1283 PrintStmt(S: S->getAssociatedStmt());
1284}
1285
1286void StmtPrinter::VisitOpenACCCacheConstruct(OpenACCCacheConstruct *S) {
1287 Indent() << "#pragma acc cache(";
1288 if (S->hasReadOnly())
1289 OS << "readonly: ";
1290
1291 llvm::interleaveComma(c: S->getVarList(), os&: OS, each_fn: [&](const Expr *E) {
1292 E->printPretty(OS, Helper: nullptr, Policy);
1293 });
1294
1295 OS << ")\n";
1296}
1297
1298//===----------------------------------------------------------------------===//
1299// Expr printing methods.
1300//===----------------------------------------------------------------------===//
1301
1302void StmtPrinter::VisitSourceLocExpr(SourceLocExpr *Node) {
1303 OS << Node->getBuiltinStr() << "()";
1304}
1305
1306void StmtPrinter::VisitEmbedExpr(EmbedExpr *Node) {
1307 // FIXME: Embed parameters are not reflected in the AST, so there is no way to
1308 // print them yet.
1309 OS << "#embed ";
1310 OS << Node->getFileName();
1311 OS << NL;
1312}
1313
1314void StmtPrinter::VisitConstantExpr(ConstantExpr *Node) {
1315 PrintExpr(E: Node->getSubExpr());
1316}
1317
1318void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
1319 ValueDecl *VD = Node->getDecl();
1320 if (const auto *OCED = dyn_cast<OMPCapturedExprDecl>(Val: VD)) {
1321 OCED->getInit()->IgnoreImpCasts()->printPretty(OS, Helper: nullptr, Policy);
1322 return;
1323 }
1324 if (const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(Val: VD)) {
1325 TPOD->printAsExpr(OS, Policy);
1326 return;
1327 }
1328 Node->getQualifier().print(OS, Policy);
1329 if (Node->hasTemplateKeyword())
1330 OS << "template ";
1331
1332 bool ForceAnonymous =
1333 Policy.PrintAsCanonical && VD->getKind() == Decl::NonTypeTemplateParm;
1334 DeclarationNameInfo NameInfo = Node->getNameInfo();
1335 if (IdentifierInfo *ID = NameInfo.getName().getAsIdentifierInfo();
1336 !ForceAnonymous &&
1337 (ID || NameInfo.getName().getNameKind() != DeclarationName::Identifier)) {
1338 if (Policy.CleanUglifiedParameters &&
1339 isa<ParmVarDecl, NonTypeTemplateParmDecl>(Val: VD) && ID)
1340 OS << ID->deuglifiedName();
1341 else
1342 NameInfo.printName(OS, Policy);
1343 } else {
1344 switch (VD->getKind()) {
1345 case Decl::NonTypeTemplateParm: {
1346 auto *TD = cast<NonTypeTemplateParmDecl>(Val: VD);
1347 OS << "value-parameter-" << TD->getDepth() << '-' << TD->getIndex() << "";
1348 break;
1349 }
1350 case Decl::ParmVar: {
1351 auto *PD = cast<ParmVarDecl>(Val: VD);
1352 OS << "function-parameter-" << PD->getFunctionScopeDepth() << '-'
1353 << PD->getFunctionScopeIndex();
1354 break;
1355 }
1356 case Decl::Decomposition:
1357 OS << "decomposition";
1358 for (const auto &I : cast<DecompositionDecl>(Val: VD)->bindings())
1359 OS << '-' << I->getName();
1360 break;
1361 default:
1362 OS << "unhandled-anonymous-" << VD->getDeclKindName();
1363 break;
1364 }
1365 }
1366 if (Node->hasExplicitTemplateArgs()) {
1367 const TemplateParameterList *TPL = nullptr;
1368 if (!Node->hadMultipleCandidates())
1369 if (auto *TD = dyn_cast<TemplateDecl>(Val: VD))
1370 TPL = TD->getTemplateParameters();
1371 printTemplateArgumentList(OS, Args: Node->template_arguments(), Policy, TPL);
1372 }
1373}
1374
1375void StmtPrinter::VisitDependentScopeDeclRefExpr(
1376 DependentScopeDeclRefExpr *Node) {
1377 Node->getQualifier().print(OS, Policy);
1378 if (Node->hasTemplateKeyword())
1379 OS << "template ";
1380 OS << Node->getNameInfo();
1381 if (Node->hasExplicitTemplateArgs())
1382 printTemplateArgumentList(OS, Args: Node->template_arguments(), Policy);
1383}
1384
1385void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
1386 Node->getQualifier().print(OS, Policy);
1387 if (Node->hasTemplateKeyword())
1388 OS << "template ";
1389 OS << Node->getNameInfo();
1390 if (Node->hasExplicitTemplateArgs())
1391 printTemplateArgumentList(OS, Args: Node->template_arguments(), Policy);
1392}
1393
1394static bool isImplicitSelf(const Expr *E) {
1395 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E)) {
1396 if (const auto *PD = dyn_cast<ImplicitParamDecl>(Val: DRE->getDecl())) {
1397 if (PD->getParameterKind() == ImplicitParamKind::ObjCSelf &&
1398 DRE->getBeginLoc().isInvalid())
1399 return true;
1400 }
1401 }
1402 return false;
1403}
1404
1405void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
1406 if (Node->getBase()) {
1407 if (!Policy.SuppressImplicitBase ||
1408 !isImplicitSelf(E: Node->getBase()->IgnoreImpCasts())) {
1409 PrintExpr(E: Node->getBase());
1410 OS << (Node->isArrow() ? "->" : ".");
1411 }
1412 }
1413 OS << *Node->getDecl();
1414}
1415
1416void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
1417 if (Node->isSuperReceiver())
1418 OS << "super.";
1419 else if (Node->isObjectReceiver() && Node->getBase()) {
1420 PrintExpr(E: Node->getBase());
1421 OS << ".";
1422 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1423 OS << Node->getClassReceiver()->getName() << ".";
1424 }
1425
1426 if (Node->isImplicitProperty()) {
1427 if (const auto *Getter = Node->getImplicitPropertyGetter())
1428 Getter->getSelector().print(OS);
1429 else
1430 OS << SelectorTable::getPropertyNameFromSetterSelector(
1431 Sel: Node->getImplicitPropertySetter()->getSelector());
1432 } else
1433 OS << Node->getExplicitProperty()->getName();
1434}
1435
1436void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1437 PrintExpr(E: Node->getBaseExpr());
1438 OS << "[";
1439 PrintExpr(E: Node->getKeyExpr());
1440 OS << "]";
1441}
1442
1443void StmtPrinter::VisitSYCLUniqueStableNameExpr(
1444 SYCLUniqueStableNameExpr *Node) {
1445 OS << "__builtin_sycl_unique_stable_name(";
1446 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1447 OS << ")";
1448}
1449
1450void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
1451 OS << PredefinedExpr::getIdentKindName(IK: Node->getIdentKind());
1452}
1453
1454void StmtPrinter::VisitOpenACCAsteriskSizeExpr(OpenACCAsteriskSizeExpr *Node) {
1455 OS << '*';
1456}
1457
1458void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
1459 CharacterLiteral::print(val: Node->getValue(), Kind: Node->getKind(), OS);
1460}
1461
1462/// Prints the given expression using the original source text. Returns true on
1463/// success, false otherwise.
1464static bool printExprAsWritten(raw_ostream &OS, Expr *E,
1465 const ASTContext *Context) {
1466 if (!Context)
1467 return false;
1468 bool Invalid = false;
1469 StringRef Source = Lexer::getSourceText(
1470 Range: CharSourceRange::getTokenRange(R: E->getSourceRange()),
1471 SM: Context->getSourceManager(), LangOpts: Context->getLangOpts(), Invalid: &Invalid);
1472 if (!Invalid) {
1473 OS << Source;
1474 return true;
1475 }
1476 return false;
1477}
1478
1479void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
1480 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, E: Node, Context))
1481 return;
1482 bool isSigned = Node->getType()->isSignedIntegerType();
1483 OS << toString(I: Node->getValue(), Radix: 10, Signed: isSigned);
1484
1485 if (isa<BitIntType>(Val: Node->getType())) {
1486 OS << (isSigned ? "wb" : "uwb");
1487 return;
1488 }
1489
1490 // Emit suffixes. Integer literals are always a builtin integer type.
1491 switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1492 default: llvm_unreachable("Unexpected type for integer literal!");
1493 case BuiltinType::Char_S:
1494 case BuiltinType::Char_U: OS << "i8"; break;
1495 case BuiltinType::UChar: OS << "Ui8"; break;
1496 case BuiltinType::SChar: OS << "i8"; break;
1497 case BuiltinType::Short: OS << "i16"; break;
1498 case BuiltinType::UShort: OS << "Ui16"; break;
1499 case BuiltinType::Int: break; // no suffix.
1500 case BuiltinType::UInt: OS << 'U'; break;
1501 case BuiltinType::Long: OS << 'L'; break;
1502 case BuiltinType::ULong: OS << "UL"; break;
1503 case BuiltinType::LongLong: OS << "LL"; break;
1504 case BuiltinType::ULongLong: OS << "ULL"; break;
1505 case BuiltinType::Int128:
1506 break; // no suffix.
1507 case BuiltinType::UInt128:
1508 break; // no suffix.
1509 case BuiltinType::WChar_S:
1510 case BuiltinType::WChar_U:
1511 break; // no suffix
1512 }
1513}
1514
1515void StmtPrinter::VisitFixedPointLiteral(FixedPointLiteral *Node) {
1516 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, E: Node, Context))
1517 return;
1518 OS << Node->getValueAsString(/*Radix=*/10);
1519
1520 switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1521 default: llvm_unreachable("Unexpected type for fixed point literal!");
1522 case BuiltinType::ShortFract: OS << "hr"; break;
1523 case BuiltinType::ShortAccum: OS << "hk"; break;
1524 case BuiltinType::UShortFract: OS << "uhr"; break;
1525 case BuiltinType::UShortAccum: OS << "uhk"; break;
1526 case BuiltinType::Fract: OS << "r"; break;
1527 case BuiltinType::Accum: OS << "k"; break;
1528 case BuiltinType::UFract: OS << "ur"; break;
1529 case BuiltinType::UAccum: OS << "uk"; break;
1530 case BuiltinType::LongFract: OS << "lr"; break;
1531 case BuiltinType::LongAccum: OS << "lk"; break;
1532 case BuiltinType::ULongFract: OS << "ulr"; break;
1533 case BuiltinType::ULongAccum: OS << "ulk"; break;
1534 }
1535}
1536
1537static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1538 bool PrintSuffix) {
1539 SmallString<16> Str;
1540 Node->getValue().toString(Str);
1541 OS << Str;
1542 if (Str.find_first_not_of(Chars: "-0123456789") == StringRef::npos)
1543 OS << '.'; // Trailing dot in order to separate from ints.
1544
1545 if (!PrintSuffix)
1546 return;
1547
1548 // Emit suffixes. Float literals are always a builtin float type.
1549 switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1550 default: llvm_unreachable("Unexpected type for float literal!");
1551 case BuiltinType::Half: break; // FIXME: suffix?
1552 case BuiltinType::Ibm128: break; // FIXME: No suffix for ibm128 literal
1553 case BuiltinType::Double: break; // no suffix.
1554 case BuiltinType::Float16: OS << "F16"; break;
1555 case BuiltinType::Float: OS << 'F'; break;
1556 case BuiltinType::LongDouble: OS << 'L'; break;
1557 case BuiltinType::Float128: OS << 'Q'; break;
1558 }
1559}
1560
1561void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1562 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, E: Node, Context))
1563 return;
1564 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
1565}
1566
1567void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1568 PrintExpr(E: Node->getSubExpr());
1569 OS << "i";
1570}
1571
1572void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
1573 Str->outputString(OS);
1574}
1575
1576void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1577 OS << "(";
1578 PrintExpr(E: Node->getSubExpr());
1579 OS << ")";
1580}
1581
1582void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
1583 if (!Node->isPostfix()) {
1584 OS << UnaryOperator::getOpcodeStr(Op: Node->getOpcode());
1585
1586 // Print a space if this is an "identifier operator" like __real, or if
1587 // it might be concatenated incorrectly like '+'.
1588 switch (Node->getOpcode()) {
1589 default: break;
1590 case UO_Real:
1591 case UO_Imag:
1592 case UO_Extension:
1593 OS << ' ';
1594 break;
1595 case UO_Plus:
1596 case UO_Minus:
1597 if (isa<UnaryOperator>(Val: Node->getSubExpr()))
1598 OS << ' ';
1599 break;
1600 }
1601 }
1602 PrintExpr(E: Node->getSubExpr());
1603
1604 if (Node->isPostfix())
1605 OS << UnaryOperator::getOpcodeStr(Op: Node->getOpcode());
1606}
1607
1608void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1609 OS << "__builtin_offsetof(";
1610 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1611 OS << ", ";
1612 bool PrintedSomething = false;
1613 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1614 OffsetOfNode ON = Node->getComponent(Idx: i);
1615 if (ON.getKind() == OffsetOfNode::Array) {
1616 // Array node
1617 OS << "[";
1618 PrintExpr(E: Node->getIndexExpr(Idx: ON.getArrayExprIndex()));
1619 OS << "]";
1620 PrintedSomething = true;
1621 continue;
1622 }
1623
1624 // Skip implicit base indirections.
1625 if (ON.getKind() == OffsetOfNode::Base)
1626 continue;
1627
1628 // Field or identifier node.
1629 const IdentifierInfo *Id = ON.getFieldName();
1630 if (!Id)
1631 continue;
1632
1633 if (PrintedSomething)
1634 OS << ".";
1635 else
1636 PrintedSomething = true;
1637 OS << Id->getName();
1638 }
1639 OS << ")";
1640}
1641
1642void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(
1643 UnaryExprOrTypeTraitExpr *Node) {
1644 const char *Spelling = getTraitSpelling(T: Node->getKind());
1645 if (Node->getKind() == UETT_AlignOf) {
1646 if (Policy.Alignof)
1647 Spelling = "alignof";
1648 else if (Policy.UnderscoreAlignof)
1649 Spelling = "_Alignof";
1650 else
1651 Spelling = "__alignof";
1652 }
1653
1654 OS << Spelling;
1655
1656 if (Node->isArgumentType()) {
1657 OS << '(';
1658 Node->getArgumentType().print(OS, Policy);
1659 OS << ')';
1660 } else {
1661 OS << " ";
1662 PrintExpr(E: Node->getArgumentExpr());
1663 }
1664}
1665
1666void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1667 OS << "_Generic(";
1668 if (Node->isExprPredicate())
1669 PrintExpr(E: Node->getControllingExpr());
1670 else
1671 Node->getControllingType()->getType().print(OS, Policy);
1672
1673 for (const GenericSelectionExpr::Association &Assoc : Node->associations()) {
1674 OS << ", ";
1675 QualType T = Assoc.getType();
1676 if (T.isNull())
1677 OS << "default";
1678 else
1679 T.print(OS, Policy);
1680 OS << ": ";
1681 PrintExpr(E: Assoc.getAssociationExpr());
1682 }
1683 OS << ")";
1684}
1685
1686void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
1687 PrintExpr(E: Node->getLHS());
1688 OS << "[";
1689 PrintExpr(E: Node->getRHS());
1690 OS << "]";
1691}
1692
1693void StmtPrinter::VisitMatrixSingleSubscriptExpr(
1694 MatrixSingleSubscriptExpr *Node) {
1695 PrintExpr(E: Node->getBase());
1696 OS << "[";
1697 PrintExpr(E: Node->getRowIdx());
1698 OS << "]";
1699}
1700
1701void StmtPrinter::VisitMatrixSubscriptExpr(MatrixSubscriptExpr *Node) {
1702 PrintExpr(E: Node->getBase());
1703 OS << "[";
1704 PrintExpr(E: Node->getRowIdx());
1705 OS << "]";
1706 OS << "[";
1707 PrintExpr(E: Node->getColumnIdx());
1708 OS << "]";
1709}
1710
1711void StmtPrinter::VisitArraySectionExpr(ArraySectionExpr *Node) {
1712 PrintExpr(E: Node->getBase());
1713 OS << "[";
1714 if (Node->getLowerBound())
1715 PrintExpr(E: Node->getLowerBound());
1716 if (Node->getColonLocFirst().isValid()) {
1717 OS << ":";
1718 if (Node->getLength())
1719 PrintExpr(E: Node->getLength());
1720 }
1721 if (Node->isOMPArraySection() && Node->getColonLocSecond().isValid()) {
1722 OS << ":";
1723 if (Node->getStride())
1724 PrintExpr(E: Node->getStride());
1725 }
1726 OS << "]";
1727}
1728
1729void StmtPrinter::VisitOMPArrayShapingExpr(OMPArrayShapingExpr *Node) {
1730 OS << "(";
1731 for (Expr *E : Node->getDimensions()) {
1732 OS << "[";
1733 PrintExpr(E);
1734 OS << "]";
1735 }
1736 OS << ")";
1737 PrintExpr(E: Node->getBase());
1738}
1739
1740void StmtPrinter::VisitOMPIteratorExpr(OMPIteratorExpr *Node) {
1741 OS << "iterator(";
1742 for (unsigned I = 0, E = Node->numOfIterators(); I < E; ++I) {
1743 auto *VD = cast<ValueDecl>(Val: Node->getIteratorDecl(I));
1744 VD->getType().print(OS, Policy);
1745 const OMPIteratorExpr::IteratorRange Range = Node->getIteratorRange(I);
1746 OS << " " << VD->getName() << " = ";
1747 PrintExpr(E: Range.Begin);
1748 OS << ":";
1749 PrintExpr(E: Range.End);
1750 if (Range.Step) {
1751 OS << ":";
1752 PrintExpr(E: Range.Step);
1753 }
1754 if (I < E - 1)
1755 OS << ", ";
1756 }
1757 OS << ")";
1758}
1759
1760void StmtPrinter::PrintCallArgs(CallExpr *Call) {
1761 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
1762 if (isa<CXXDefaultArgExpr>(Val: Call->getArg(Arg: i))) {
1763 // Don't print any defaulted arguments
1764 break;
1765 }
1766
1767 if (i) OS << ", ";
1768 PrintExpr(E: Call->getArg(Arg: i));
1769 }
1770}
1771
1772void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1773 PrintExpr(E: Call->getCallee());
1774 OS << "(";
1775 PrintCallArgs(Call);
1776 OS << ")";
1777}
1778
1779static bool isImplicitThis(const Expr *E) {
1780 if (const auto *TE = dyn_cast<CXXThisExpr>(Val: E))
1781 return TE->isImplicit();
1782 return false;
1783}
1784
1785void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1786 if (!Policy.SuppressImplicitBase || !isImplicitThis(E: Node->getBase())) {
1787 PrintExpr(E: Node->getBase());
1788
1789 auto *ParentMember = dyn_cast<MemberExpr>(Val: Node->getBase());
1790 FieldDecl *ParentDecl =
1791 ParentMember ? dyn_cast<FieldDecl>(Val: ParentMember->getMemberDecl())
1792 : nullptr;
1793
1794 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1795 OS << (Node->isArrow() ? "->" : ".");
1796 }
1797
1798 if (auto *FD = dyn_cast<FieldDecl>(Val: Node->getMemberDecl()))
1799 if (FD->isAnonymousStructOrUnion())
1800 return;
1801
1802 Node->getQualifier().print(OS, Policy);
1803 if (Node->hasTemplateKeyword())
1804 OS << "template ";
1805 OS << Node->getMemberNameInfo();
1806 const TemplateParameterList *TPL = nullptr;
1807 if (auto *FD = dyn_cast<FunctionDecl>(Val: Node->getMemberDecl())) {
1808 if (!Node->hadMultipleCandidates())
1809 if (auto *FTD = FD->getPrimaryTemplate())
1810 TPL = FTD->getTemplateParameters();
1811 } else if (auto *VTSD =
1812 dyn_cast<VarTemplateSpecializationDecl>(Val: Node->getMemberDecl()))
1813 TPL = VTSD->getSpecializedTemplate()->getTemplateParameters();
1814 if (Node->hasExplicitTemplateArgs())
1815 printTemplateArgumentList(OS, Args: Node->template_arguments(), Policy, TPL);
1816}
1817
1818void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1819 PrintExpr(E: Node->getBase());
1820 OS << (Node->isArrow() ? "->isa" : ".isa");
1821}
1822
1823void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1824 PrintExpr(E: Node->getBase());
1825 OS << ".";
1826 OS << Node->getAccessor().getName();
1827}
1828
1829void StmtPrinter::VisitMatrixElementExpr(MatrixElementExpr *Node) {
1830 PrintExpr(E: Node->getBase());
1831 OS << ".";
1832 OS << Node->getAccessor().getName();
1833}
1834
1835void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1836 OS << '(';
1837 Node->getTypeAsWritten().print(OS, Policy);
1838 OS << ')';
1839 PrintExpr(E: Node->getSubExpr());
1840}
1841
1842void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1843 OS << '(';
1844 Node->getType().print(OS, Policy);
1845 OS << ')';
1846 PrintExpr(E: Node->getInitializer());
1847}
1848
1849void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1850 // No need to print anything, simply forward to the subexpression.
1851 PrintExpr(E: Node->getSubExpr());
1852}
1853
1854void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1855 PrintExpr(E: Node->getLHS());
1856 OS << " " << BinaryOperator::getOpcodeStr(Op: Node->getOpcode()) << " ";
1857 PrintExpr(E: Node->getRHS());
1858}
1859
1860void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1861 PrintExpr(E: Node->getLHS());
1862 OS << " " << BinaryOperator::getOpcodeStr(Op: Node->getOpcode()) << " ";
1863 PrintExpr(E: Node->getRHS());
1864}
1865
1866void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1867 PrintExpr(E: Node->getCond());
1868 OS << " ? ";
1869 PrintExpr(E: Node->getLHS());
1870 OS << " : ";
1871 PrintExpr(E: Node->getRHS());
1872}
1873
1874// GNU extensions.
1875
1876void
1877StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1878 PrintExpr(E: Node->getCommon());
1879 OS << " ?: ";
1880 PrintExpr(E: Node->getFalseExpr());
1881}
1882
1883void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1884 OS << "&&" << Node->getLabel()->getName();
1885}
1886
1887void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1888 OS << "(";
1889 PrintRawCompoundStmt(Node: E->getSubStmt());
1890 OS << ")";
1891}
1892
1893void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1894 OS << "__builtin_choose_expr(";
1895 PrintExpr(E: Node->getCond());
1896 OS << ", ";
1897 PrintExpr(E: Node->getLHS());
1898 OS << ", ";
1899 PrintExpr(E: Node->getRHS());
1900 OS << ")";
1901}
1902
1903void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1904 OS << "__null";
1905}
1906
1907void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1908 OS << "__builtin_shufflevector(";
1909 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1910 if (i) OS << ", ";
1911 PrintExpr(E: Node->getExpr(Index: i));
1912 }
1913 OS << ")";
1914}
1915
1916void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1917 OS << "__builtin_convertvector(";
1918 PrintExpr(E: Node->getSrcExpr());
1919 OS << ", ";
1920 Node->getType().print(OS, Policy);
1921 OS << ")";
1922}
1923
1924void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1925 if (Node->getSyntacticForm()) {
1926 Visit(S: Node->getSyntacticForm());
1927 return;
1928 }
1929
1930 OS << "{";
1931 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1932 if (i) OS << ", ";
1933 if (Node->getInit(Init: i))
1934 PrintExpr(E: Node->getInit(Init: i));
1935 else
1936 OS << "{}";
1937 }
1938 OS << "}";
1939}
1940
1941void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) {
1942 // There's no way to express this expression in any of our supported
1943 // languages, so just emit something terse and (hopefully) clear.
1944 OS << "{";
1945 PrintExpr(E: Node->getSubExpr());
1946 OS << "}";
1947}
1948
1949void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) {
1950 OS << "*";
1951}
1952
1953void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1954 OS << "(";
1955 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1956 if (i) OS << ", ";
1957 PrintExpr(E: Node->getExpr(Init: i));
1958 }
1959 OS << ")";
1960}
1961
1962void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1963 bool NeedsEquals = true;
1964 for (const DesignatedInitExpr::Designator &D : Node->designators()) {
1965 if (D.isFieldDesignator()) {
1966 if (D.getDotLoc().isInvalid()) {
1967 if (const IdentifierInfo *II = D.getFieldName()) {
1968 OS << II->getName() << ":";
1969 NeedsEquals = false;
1970 }
1971 } else {
1972 OS << "." << D.getFieldName()->getName();
1973 }
1974 } else {
1975 OS << "[";
1976 if (D.isArrayDesignator()) {
1977 PrintExpr(E: Node->getArrayIndex(D));
1978 } else {
1979 PrintExpr(E: Node->getArrayRangeStart(D));
1980 OS << " ... ";
1981 PrintExpr(E: Node->getArrayRangeEnd(D));
1982 }
1983 OS << "]";
1984 }
1985 }
1986
1987 if (NeedsEquals)
1988 OS << " = ";
1989 else
1990 OS << " ";
1991 PrintExpr(E: Node->getInit());
1992}
1993
1994void StmtPrinter::VisitDesignatedInitUpdateExpr(
1995 DesignatedInitUpdateExpr *Node) {
1996 OS << "{";
1997 OS << "/*base*/";
1998 PrintExpr(E: Node->getBase());
1999 OS << ", ";
2000
2001 OS << "/*updater*/";
2002 PrintExpr(E: Node->getUpdater());
2003 OS << "}";
2004}
2005
2006void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
2007 OS << "/*no init*/";
2008}
2009
2010void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
2011 if (Node->getType()->getAsCXXRecordDecl()) {
2012 OS << "/*implicit*/";
2013 Node->getType().print(OS, Policy);
2014 OS << "()";
2015 } else {
2016 OS << "/*implicit*/(";
2017 Node->getType().print(OS, Policy);
2018 OS << ')';
2019 if (Node->getType()->isRecordType())
2020 OS << "{}";
2021 else
2022 OS << 0;
2023 }
2024}
2025
2026void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
2027 OS << "__builtin_va_arg(";
2028 PrintExpr(E: Node->getSubExpr());
2029 OS << ", ";
2030 Node->getType().print(OS, Policy);
2031 OS << ")";
2032}
2033
2034void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
2035 PrintExpr(E: Node->getSyntacticForm());
2036}
2037
2038void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
2039 const char *Name = nullptr;
2040 switch (Node->getOp()) {
2041#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
2042 case AtomicExpr::AO ## ID: \
2043 Name = #ID "("; \
2044 break;
2045#include "clang/Basic/Builtins.inc"
2046 }
2047 OS << Name;
2048
2049 // AtomicExpr stores its subexpressions in a permuted order.
2050 PrintExpr(E: Node->getPtr());
2051 if (Node->hasVal1Operand()) {
2052 OS << ", ";
2053 PrintExpr(E: Node->getVal1());
2054 }
2055 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
2056 Node->isCmpXChg()) {
2057 OS << ", ";
2058 PrintExpr(E: Node->getVal2());
2059 }
2060 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
2061 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
2062 OS << ", ";
2063 PrintExpr(E: Node->getWeak());
2064 }
2065 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init &&
2066 Node->getOp() != AtomicExpr::AO__opencl_atomic_init) {
2067 OS << ", ";
2068 PrintExpr(E: Node->getOrder());
2069 }
2070 if (Node->isCmpXChg()) {
2071 OS << ", ";
2072 PrintExpr(E: Node->getOrderFail());
2073 }
2074 OS << ")";
2075}
2076
2077// C++
2078void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
2079 OverloadedOperatorKind Kind = Node->getOperator();
2080 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
2081 if (Node->getNumArgs() == 1) {
2082 OS << getOperatorSpelling(Operator: Kind) << ' ';
2083 PrintExpr(E: Node->getArg(Arg: 0));
2084 } else {
2085 PrintExpr(E: Node->getArg(Arg: 0));
2086 OS << ' ' << getOperatorSpelling(Operator: Kind);
2087 }
2088 } else if (Kind == OO_Arrow) {
2089 PrintExpr(E: Node->getArg(Arg: 0));
2090 } else if (Kind == OO_Call || Kind == OO_Subscript) {
2091 PrintExpr(E: Node->getArg(Arg: 0));
2092 OS << (Kind == OO_Call ? '(' : '[');
2093 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
2094 if (ArgIdx > 1)
2095 OS << ", ";
2096 if (!isa<CXXDefaultArgExpr>(Val: Node->getArg(Arg: ArgIdx)))
2097 PrintExpr(E: Node->getArg(Arg: ArgIdx));
2098 }
2099 OS << (Kind == OO_Call ? ')' : ']');
2100 } else if (Node->getNumArgs() == 1) {
2101 OS << getOperatorSpelling(Operator: Kind) << ' ';
2102 PrintExpr(E: Node->getArg(Arg: 0));
2103 } else if (Node->getNumArgs() == 2) {
2104 PrintExpr(E: Node->getArg(Arg: 0));
2105 OS << ' ' << getOperatorSpelling(Operator: Kind) << ' ';
2106 PrintExpr(E: Node->getArg(Arg: 1));
2107 } else {
2108 llvm_unreachable("unknown overloaded operator");
2109 }
2110}
2111
2112void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
2113 // If we have a conversion operator call only print the argument.
2114 CXXMethodDecl *MD = Node->getMethodDecl();
2115 if (isa_and_nonnull<CXXConversionDecl>(Val: MD)) {
2116 PrintExpr(E: Node->getImplicitObjectArgument());
2117 return;
2118 }
2119 VisitCallExpr(Call: cast<CallExpr>(Val: Node));
2120}
2121
2122void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
2123 PrintExpr(E: Node->getCallee());
2124 OS << "<<<";
2125 PrintCallArgs(Call: Node->getConfig());
2126 OS << ">>>(";
2127 PrintCallArgs(Call: Node);
2128 OS << ")";
2129}
2130
2131void StmtPrinter::VisitCXXRewrittenBinaryOperator(
2132 CXXRewrittenBinaryOperator *Node) {
2133 CXXRewrittenBinaryOperator::DecomposedForm Decomposed =
2134 Node->getDecomposedForm();
2135 PrintExpr(E: const_cast<Expr*>(Decomposed.LHS));
2136 OS << ' ' << BinaryOperator::getOpcodeStr(Op: Decomposed.Opcode) << ' ';
2137 PrintExpr(E: const_cast<Expr*>(Decomposed.RHS));
2138}
2139
2140void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
2141 OS << Node->getCastName() << '<';
2142 Node->getTypeAsWritten().print(OS, Policy);
2143 OS << ">(";
2144 PrintExpr(E: Node->getSubExpr());
2145 OS << ")";
2146}
2147
2148void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
2149 VisitCXXNamedCastExpr(Node);
2150}
2151
2152void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
2153 VisitCXXNamedCastExpr(Node);
2154}
2155
2156void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
2157 VisitCXXNamedCastExpr(Node);
2158}
2159
2160void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
2161 VisitCXXNamedCastExpr(Node);
2162}
2163
2164void StmtPrinter::VisitBuiltinBitCastExpr(BuiltinBitCastExpr *Node) {
2165 OS << "__builtin_bit_cast(";
2166 Node->getTypeInfoAsWritten()->getType().print(OS, Policy);
2167 OS << ", ";
2168 PrintExpr(E: Node->getSubExpr());
2169 OS << ")";
2170}
2171
2172void StmtPrinter::VisitCXXAddrspaceCastExpr(CXXAddrspaceCastExpr *Node) {
2173 VisitCXXNamedCastExpr(Node);
2174}
2175
2176void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
2177 OS << "typeid(";
2178 if (Node->isTypeOperand()) {
2179 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
2180 } else {
2181 PrintExpr(E: Node->getExprOperand());
2182 }
2183 OS << ")";
2184}
2185
2186void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
2187 OS << "__uuidof(";
2188 if (Node->isTypeOperand()) {
2189 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
2190 } else {
2191 PrintExpr(E: Node->getExprOperand());
2192 }
2193 OS << ")";
2194}
2195
2196void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
2197 PrintExpr(E: Node->getBaseExpr());
2198 if (Node->isArrow())
2199 OS << "->";
2200 else
2201 OS << ".";
2202 Node->getQualifierLoc().getNestedNameSpecifier().print(OS, Policy);
2203 OS << Node->getPropertyDecl()->getDeclName();
2204}
2205
2206void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
2207 PrintExpr(E: Node->getBase());
2208 OS << "[";
2209 PrintExpr(E: Node->getIdx());
2210 OS << "]";
2211}
2212
2213void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
2214 switch (Node->getLiteralOperatorKind()) {
2215 case UserDefinedLiteral::LOK_Raw:
2216 OS << cast<StringLiteral>(Val: Node->getArg(Arg: 0)->IgnoreImpCasts())->getString();
2217 break;
2218 case UserDefinedLiteral::LOK_Template: {
2219 const auto *DRE = cast<DeclRefExpr>(Val: Node->getCallee()->IgnoreImpCasts());
2220 const TemplateArgumentList *Args =
2221 cast<FunctionDecl>(Val: DRE->getDecl())->getTemplateSpecializationArgs();
2222 assert(Args);
2223
2224 if (Args->size() != 1 || Args->get(Idx: 0).getKind() != TemplateArgument::Pack) {
2225 const TemplateParameterList *TPL = nullptr;
2226 if (!DRE->hadMultipleCandidates())
2227 if (const auto *TD = dyn_cast<TemplateDecl>(Val: DRE->getDecl()))
2228 TPL = TD->getTemplateParameters();
2229 OS << "operator\"\"" << Node->getUDSuffix()->getName();
2230 printTemplateArgumentList(OS, Args: Args->asArray(), Policy, TPL);
2231 OS << "()";
2232 return;
2233 }
2234
2235 const TemplateArgument &Pack = Args->get(Idx: 0);
2236 for (const auto &P : Pack.pack_elements()) {
2237 char C = (char)P.getAsIntegral().getZExtValue();
2238 OS << C;
2239 }
2240 break;
2241 }
2242 case UserDefinedLiteral::LOK_Integer: {
2243 // Print integer literal without suffix.
2244 const auto *Int = cast<IntegerLiteral>(Val: Node->getCookedLiteral());
2245 OS << toString(I: Int->getValue(), Radix: 10, /*isSigned*/Signed: false);
2246 break;
2247 }
2248 case UserDefinedLiteral::LOK_Floating: {
2249 // Print floating literal without suffix.
2250 auto *Float = cast<FloatingLiteral>(Val: Node->getCookedLiteral());
2251 PrintFloatingLiteral(OS, Node: Float, /*PrintSuffix=*/false);
2252 break;
2253 }
2254 case UserDefinedLiteral::LOK_String:
2255 case UserDefinedLiteral::LOK_Character:
2256 PrintExpr(E: Node->getCookedLiteral());
2257 break;
2258 }
2259 OS << Node->getUDSuffix()->getName();
2260}
2261
2262void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
2263 OS << (Node->getValue() ? "true" : "false");
2264}
2265
2266void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
2267 OS << "nullptr";
2268}
2269
2270void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
2271 OS << "this";
2272}
2273
2274void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
2275 if (!Node->getSubExpr())
2276 OS << "throw";
2277 else {
2278 OS << "throw ";
2279 PrintExpr(E: Node->getSubExpr());
2280 }
2281}
2282
2283void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
2284 // Nothing to print: we picked up the default argument.
2285}
2286
2287void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
2288 // Nothing to print: we picked up the default initializer.
2289}
2290
2291void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
2292 auto TargetType = Node->getType();
2293 auto *Auto = TargetType->getContainedDeducedType();
2294 bool Bare = Auto && Auto->isDeduced();
2295
2296 // Parenthesize deduced casts.
2297 if (Bare)
2298 OS << '(';
2299 TargetType.print(OS, Policy);
2300 if (Bare)
2301 OS << ')';
2302
2303 // No extra braces surrounding the inner construct.
2304 if (!Node->isListInitialization())
2305 OS << '(';
2306 PrintExpr(E: Node->getSubExpr());
2307 if (!Node->isListInitialization())
2308 OS << ')';
2309}
2310
2311void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
2312 PrintExpr(E: Node->getSubExpr());
2313}
2314
2315void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
2316 Node->getType().print(OS, Policy);
2317 if (Node->isStdInitListInitialization())
2318 /* Nothing to do; braces are part of creating the std::initializer_list. */;
2319 else if (Node->isListInitialization())
2320 OS << "{";
2321 else
2322 OS << "(";
2323 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
2324 ArgEnd = Node->arg_end();
2325 Arg != ArgEnd; ++Arg) {
2326 if ((*Arg)->isDefaultArgument())
2327 break;
2328 if (Arg != Node->arg_begin())
2329 OS << ", ";
2330 PrintExpr(E: *Arg);
2331 }
2332 if (Node->isStdInitListInitialization())
2333 /* See above. */;
2334 else if (Node->isListInitialization())
2335 OS << "}";
2336 else
2337 OS << ")";
2338}
2339
2340void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
2341 OS << '[';
2342 bool NeedComma = false;
2343 switch (Node->getCaptureDefault()) {
2344 case LCD_None:
2345 break;
2346
2347 case LCD_ByCopy:
2348 OS << '=';
2349 NeedComma = true;
2350 break;
2351
2352 case LCD_ByRef:
2353 OS << '&';
2354 NeedComma = true;
2355 break;
2356 }
2357 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
2358 CEnd = Node->explicit_capture_end();
2359 C != CEnd;
2360 ++C) {
2361 if (C->capturesVLAType())
2362 continue;
2363
2364 if (NeedComma)
2365 OS << ", ";
2366 NeedComma = true;
2367
2368 switch (C->getCaptureKind()) {
2369 case LCK_This:
2370 OS << "this";
2371 break;
2372
2373 case LCK_StarThis:
2374 OS << "*this";
2375 break;
2376
2377 case LCK_ByRef:
2378 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(Capture: C))
2379 OS << '&';
2380 OS << C->getCapturedVar()->getName();
2381 break;
2382
2383 case LCK_ByCopy:
2384 OS << C->getCapturedVar()->getName();
2385 break;
2386
2387 case LCK_VLAType:
2388 llvm_unreachable("VLA type in explicit captures.");
2389 }
2390
2391 if (C->isPackExpansion())
2392 OS << "...";
2393
2394 if (Node->isInitCapture(Capture: C)) {
2395 // Init captures are always VarDecl.
2396 auto *D = cast<VarDecl>(Val: C->getCapturedVar());
2397
2398 llvm::StringRef Pre;
2399 llvm::StringRef Post;
2400 if (D->getInitStyle() == VarDecl::CallInit &&
2401 !isa<ParenListExpr>(Val: D->getInit())) {
2402 Pre = "(";
2403 Post = ")";
2404 } else if (D->getInitStyle() == VarDecl::CInit) {
2405 Pre = " = ";
2406 }
2407
2408 OS << Pre;
2409 PrintExpr(E: D->getInit());
2410 OS << Post;
2411 }
2412 }
2413 OS << ']';
2414
2415 if (!Node->getExplicitTemplateParameters().empty()) {
2416 Node->getTemplateParameterList()->print(
2417 Out&: OS, Context: Node->getLambdaClass()->getASTContext(),
2418 /*OmitTemplateKW*/true);
2419 }
2420
2421 if (Node->hasExplicitParameters()) {
2422 OS << '(';
2423 CXXMethodDecl *Method = Node->getCallOperator();
2424 NeedComma = false;
2425 for (const auto *P : Method->parameters()) {
2426 if (NeedComma) {
2427 OS << ", ";
2428 } else {
2429 NeedComma = true;
2430 }
2431 std::string ParamStr =
2432 (Policy.CleanUglifiedParameters && P->getIdentifier())
2433 ? P->getIdentifier()->deuglifiedName().str()
2434 : P->getNameAsString();
2435 P->getOriginalType().print(OS, Policy, PlaceHolder: ParamStr);
2436 }
2437 if (Method->isVariadic()) {
2438 if (NeedComma)
2439 OS << ", ";
2440 OS << "...";
2441 }
2442 OS << ')';
2443
2444 if (Node->isMutable())
2445 OS << " mutable";
2446
2447 auto *Proto = Method->getType()->castAs<FunctionProtoType>();
2448 Proto->printExceptionSpecification(OS, Policy);
2449
2450 // FIXME: Attributes
2451
2452 // Print the trailing return type if it was specified in the source.
2453 if (Node->hasExplicitResultType()) {
2454 OS << " -> ";
2455 Proto->getReturnType().print(OS, Policy);
2456 }
2457 }
2458
2459 // Print the body.
2460 OS << ' ';
2461 if (Policy.TerseOutput)
2462 OS << "{}";
2463 else
2464 PrintRawCompoundStmt(Node: Node->getCompoundStmtBody());
2465}
2466
2467void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
2468 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
2469 TSInfo->getType().print(OS, Policy);
2470 else
2471 Node->getType().print(OS, Policy);
2472 OS << "()";
2473}
2474
2475void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
2476 if (E->isGlobalNew())
2477 OS << "::";
2478 OS << "new ";
2479 unsigned NumPlace = E->getNumPlacementArgs();
2480 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(Val: E->getPlacementArg(I: 0))) {
2481 OS << "(";
2482 PrintExpr(E: E->getPlacementArg(I: 0));
2483 for (unsigned i = 1; i < NumPlace; ++i) {
2484 if (isa<CXXDefaultArgExpr>(Val: E->getPlacementArg(I: i)))
2485 break;
2486 OS << ", ";
2487 PrintExpr(E: E->getPlacementArg(I: i));
2488 }
2489 OS << ") ";
2490 }
2491 if (E->isParenTypeId())
2492 OS << "(";
2493 std::string TypeS;
2494 if (E->isArray()) {
2495 llvm::raw_string_ostream s(TypeS);
2496 s << '[';
2497 if (std::optional<Expr *> Size = E->getArraySize())
2498 (*Size)->printPretty(OS&: s, Helper, Policy);
2499 s << ']';
2500 }
2501 E->getAllocatedType().print(OS, Policy, PlaceHolder: TypeS);
2502 if (E->isParenTypeId())
2503 OS << ")";
2504
2505 CXXNewInitializationStyle InitStyle = E->getInitializationStyle();
2506 if (InitStyle != CXXNewInitializationStyle::None) {
2507 bool Bare = InitStyle == CXXNewInitializationStyle::Parens &&
2508 !isa<ParenListExpr>(Val: E->getInitializer());
2509 if (Bare)
2510 OS << "(";
2511 PrintExpr(E: E->getInitializer());
2512 if (Bare)
2513 OS << ")";
2514 }
2515}
2516
2517void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2518 if (E->isGlobalDelete())
2519 OS << "::";
2520 OS << "delete ";
2521 if (E->isArrayForm())
2522 OS << "[] ";
2523 PrintExpr(E: E->getArgument());
2524}
2525
2526void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
2527 PrintExpr(E: E->getBase());
2528 if (E->isArrow())
2529 OS << "->";
2530 else
2531 OS << '.';
2532 E->getQualifier().print(OS, Policy);
2533 OS << "~";
2534
2535 if (const IdentifierInfo *II = E->getDestroyedTypeIdentifier())
2536 OS << II->getName();
2537 else
2538 E->getDestroyedType().print(OS, Policy);
2539}
2540
2541void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
2542 if (E->isListInitialization() && !E->isStdInitListInitialization())
2543 OS << "{";
2544
2545 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
2546 if (isa<CXXDefaultArgExpr>(Val: E->getArg(Arg: i))) {
2547 // Don't print any defaulted arguments
2548 break;
2549 }
2550
2551 if (i) OS << ", ";
2552 PrintExpr(E: E->getArg(Arg: i));
2553 }
2554
2555 if (E->isListInitialization() && !E->isStdInitListInitialization())
2556 OS << "}";
2557}
2558
2559void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) {
2560 // Parens are printed by the surrounding context.
2561 OS << "<forwarded>";
2562}
2563
2564void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
2565 PrintExpr(E: E->getSubExpr());
2566}
2567
2568void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
2569 // Just forward to the subexpression.
2570 PrintExpr(E: E->getSubExpr());
2571}
2572
2573void StmtPrinter::VisitCXXUnresolvedConstructExpr(
2574 CXXUnresolvedConstructExpr *Node) {
2575 Node->getTypeAsWritten().print(OS, Policy);
2576 if (!Node->isListInitialization())
2577 OS << '(';
2578 for (auto Arg = Node->arg_begin(), ArgEnd = Node->arg_end(); Arg != ArgEnd;
2579 ++Arg) {
2580 if (Arg != Node->arg_begin())
2581 OS << ", ";
2582 PrintExpr(E: *Arg);
2583 }
2584 if (!Node->isListInitialization())
2585 OS << ')';
2586}
2587
2588void StmtPrinter::VisitCXXReflectExpr(CXXReflectExpr *S) {
2589 // TODO(Reflection): Implement this.
2590 assert(false && "not implemented yet");
2591}
2592
2593void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2594 CXXDependentScopeMemberExpr *Node) {
2595 if (!Node->isImplicitAccess()) {
2596 PrintExpr(E: Node->getBase());
2597 OS << (Node->isArrow() ? "->" : ".");
2598 }
2599 Node->getQualifier().print(OS, Policy);
2600 if (Node->hasTemplateKeyword())
2601 OS << "template ";
2602 OS << Node->getMemberNameInfo();
2603 if (Node->hasExplicitTemplateArgs())
2604 printTemplateArgumentList(OS, Args: Node->template_arguments(), Policy);
2605}
2606
2607void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
2608 if (!Node->isImplicitAccess()) {
2609 PrintExpr(E: Node->getBase());
2610 OS << (Node->isArrow() ? "->" : ".");
2611 }
2612 Node->getQualifier().print(OS, Policy);
2613 if (Node->hasTemplateKeyword())
2614 OS << "template ";
2615 OS << Node->getMemberNameInfo();
2616 if (Node->hasExplicitTemplateArgs())
2617 printTemplateArgumentList(OS, Args: Node->template_arguments(), Policy);
2618}
2619
2620void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2621 OS << getTraitSpelling(T: E->getTrait()) << "(";
2622 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2623 if (I > 0)
2624 OS << ", ";
2625 E->getArg(I)->getType().print(OS, Policy);
2626 }
2627 OS << ")";
2628}
2629
2630void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
2631 OS << getTraitSpelling(T: E->getTrait()) << '(';
2632 E->getQueriedType().print(OS, Policy);
2633 OS << ')';
2634}
2635
2636void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
2637 OS << getTraitSpelling(T: E->getTrait()) << '(';
2638 PrintExpr(E: E->getQueriedExpression());
2639 OS << ')';
2640}
2641
2642void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2643 OS << "noexcept(";
2644 PrintExpr(E: E->getOperand());
2645 OS << ")";
2646}
2647
2648void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
2649 PrintExpr(E: E->getPattern());
2650 OS << "...";
2651}
2652
2653void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2654 OS << "sizeof...(" << *E->getPack() << ")";
2655}
2656
2657void StmtPrinter::VisitPackIndexingExpr(PackIndexingExpr *E) {
2658 PrintExpr(E: E->getPackIdExpression());
2659 OS << "...[";
2660 PrintExpr(E: E->getIndexExpr());
2661 OS << "]";
2662}
2663
2664void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2665 SubstNonTypeTemplateParmPackExpr *Node) {
2666 OS << *Node->getParameterPack();
2667}
2668
2669void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2670 SubstNonTypeTemplateParmExpr *Node) {
2671 Visit(S: Node->getReplacement());
2672}
2673
2674void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2675 OS << *E->getParameterPack();
2676}
2677
2678void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2679 PrintExpr(E: Node->getSubExpr());
2680}
2681
2682void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2683 OS << "(";
2684 if (E->getLHS()) {
2685 PrintExpr(E: E->getLHS());
2686 OS << " " << BinaryOperator::getOpcodeStr(Op: E->getOperator()) << " ";
2687 }
2688 OS << "...";
2689 if (E->getRHS()) {
2690 OS << " " << BinaryOperator::getOpcodeStr(Op: E->getOperator()) << " ";
2691 PrintExpr(E: E->getRHS());
2692 }
2693 OS << ")";
2694}
2695
2696void StmtPrinter::VisitCXXParenListInitExpr(CXXParenListInitExpr *Node) {
2697 llvm::interleaveComma(c: Node->getUserSpecifiedInitExprs(), os&: OS,
2698 each_fn: [&](Expr *E) { PrintExpr(E); });
2699}
2700
2701void StmtPrinter::VisitConceptSpecializationExpr(ConceptSpecializationExpr *E) {
2702 NestedNameSpecifierLoc NNS = E->getNestedNameSpecifierLoc();
2703 NNS.getNestedNameSpecifier().print(OS, Policy);
2704 if (E->getTemplateKWLoc().isValid())
2705 OS << "template ";
2706 OS << E->getFoundDecl()->getName();
2707 printTemplateArgumentList(OS, Args: E->getTemplateArgsAsWritten()->arguments(),
2708 Policy,
2709 TPL: E->getNamedConcept()->getTemplateParameters());
2710}
2711
2712void StmtPrinter::VisitRequiresExpr(RequiresExpr *E) {
2713 OS << "requires ";
2714 auto LocalParameters = E->getLocalParameters();
2715 if (!LocalParameters.empty()) {
2716 OS << "(";
2717 for (ParmVarDecl *LocalParam : LocalParameters) {
2718 PrintRawDecl(D: LocalParam);
2719 if (LocalParam != LocalParameters.back())
2720 OS << ", ";
2721 }
2722
2723 OS << ") ";
2724 }
2725 OS << "{ ";
2726 auto Requirements = E->getRequirements();
2727 for (concepts::Requirement *Req : Requirements) {
2728 if (auto *TypeReq = dyn_cast<concepts::TypeRequirement>(Val: Req)) {
2729 if (TypeReq->isSubstitutionFailure())
2730 OS << "<<error-type>>";
2731 else
2732 TypeReq->getType()->getType().print(OS, Policy);
2733 } else if (auto *ExprReq = dyn_cast<concepts::ExprRequirement>(Val: Req)) {
2734 if (ExprReq->isCompound())
2735 OS << "{ ";
2736 if (ExprReq->isExprSubstitutionFailure())
2737 OS << "<<error-expression>>";
2738 else
2739 PrintExpr(E: ExprReq->getExpr());
2740 if (ExprReq->isCompound()) {
2741 OS << " }";
2742 if (ExprReq->getNoexceptLoc().isValid())
2743 OS << " noexcept";
2744 const auto &RetReq = ExprReq->getReturnTypeRequirement();
2745 if (!RetReq.isEmpty()) {
2746 OS << " -> ";
2747 if (RetReq.isSubstitutionFailure())
2748 OS << "<<error-type>>";
2749 else if (RetReq.isTypeConstraint())
2750 RetReq.getTypeConstraint()->print(OS, Policy);
2751 }
2752 }
2753 } else {
2754 auto *NestedReq = cast<concepts::NestedRequirement>(Val: Req);
2755 OS << "requires ";
2756 if (NestedReq->hasInvalidConstraint())
2757 OS << "<<error-expression>>";
2758 else
2759 PrintExpr(E: NestedReq->getConstraintExpr());
2760 }
2761 OS << "; ";
2762 }
2763 OS << "}";
2764}
2765
2766// C++ Coroutines
2767
2768void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
2769 Visit(S: S->getBody());
2770}
2771
2772void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
2773 OS << "co_return";
2774 if (S->getOperand()) {
2775 OS << " ";
2776 Visit(S: S->getOperand());
2777 }
2778 OS << ";";
2779}
2780
2781void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
2782 OS << "co_await ";
2783 PrintExpr(E: S->getOperand());
2784}
2785
2786void StmtPrinter::VisitDependentCoawaitExpr(DependentCoawaitExpr *S) {
2787 OS << "co_await ";
2788 PrintExpr(E: S->getOperand());
2789}
2790
2791void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
2792 OS << "co_yield ";
2793 PrintExpr(E: S->getOperand());
2794}
2795
2796// Obj-C
2797
2798void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2799 OS << "@";
2800 VisitStringLiteral(Str: Node->getString());
2801}
2802
2803void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
2804 OS << "@";
2805 Visit(S: E->getSubExpr());
2806}
2807
2808void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2809 OS << "@[ ";
2810 ObjCArrayLiteral::child_range Ch = E->children();
2811 for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2812 if (I != Ch.begin())
2813 OS << ", ";
2814 Visit(S: *I);
2815 }
2816 OS << " ]";
2817}
2818
2819void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2820 OS << "@{ ";
2821 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2822 if (I > 0)
2823 OS << ", ";
2824
2825 ObjCDictionaryElement Element = E->getKeyValueElement(Index: I);
2826 Visit(S: Element.Key);
2827 OS << " : ";
2828 Visit(S: Element.Value);
2829 if (Element.isPackExpansion())
2830 OS << "...";
2831 }
2832 OS << " }";
2833}
2834
2835void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
2836 OS << "@encode(";
2837 Node->getEncodedType().print(OS, Policy);
2838 OS << ')';
2839}
2840
2841void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
2842 OS << "@selector(";
2843 Node->getSelector().print(OS);
2844 OS << ')';
2845}
2846
2847void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
2848 OS << "@protocol(" << *Node->getProtocol() << ')';
2849}
2850
2851void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2852 OS << "[";
2853 switch (Mess->getReceiverKind()) {
2854 case ObjCMessageExpr::Instance:
2855 PrintExpr(E: Mess->getInstanceReceiver());
2856 break;
2857
2858 case ObjCMessageExpr::Class:
2859 Mess->getClassReceiver().print(OS, Policy);
2860 break;
2861
2862 case ObjCMessageExpr::SuperInstance:
2863 case ObjCMessageExpr::SuperClass:
2864 OS << "Super";
2865 break;
2866 }
2867
2868 OS << ' ';
2869 Selector selector = Mess->getSelector();
2870 if (selector.isUnarySelector()) {
2871 OS << selector.getNameForSlot(argIndex: 0);
2872 } else {
2873 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
2874 if (i < selector.getNumArgs()) {
2875 if (i > 0) OS << ' ';
2876 if (selector.getIdentifierInfoForSlot(argIndex: i))
2877 OS << selector.getIdentifierInfoForSlot(argIndex: i)->getName() << ':';
2878 else
2879 OS << ":";
2880 }
2881 else OS << ", "; // Handle variadic methods.
2882
2883 PrintExpr(E: Mess->getArg(Arg: i));
2884 }
2885 }
2886 OS << "]";
2887}
2888
2889void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2890 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2891}
2892
2893void
2894StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2895 PrintExpr(E: E->getSubExpr());
2896}
2897
2898void
2899StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
2900 OS << '(' << E->getBridgeKindName();
2901 E->getType().print(OS, Policy);
2902 OS << ')';
2903 PrintExpr(E: E->getSubExpr());
2904}
2905
2906void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
2907 BlockDecl *BD = Node->getBlockDecl();
2908 OS << "^";
2909
2910 const FunctionType *AFT = Node->getFunctionType();
2911
2912 if (isa<FunctionNoProtoType>(Val: AFT)) {
2913 OS << "()";
2914 } else if (!BD->param_empty() || cast<FunctionProtoType>(Val: AFT)->isVariadic()) {
2915 OS << '(';
2916 for (BlockDecl::param_iterator AI = BD->param_begin(),
2917 E = BD->param_end(); AI != E; ++AI) {
2918 if (AI != BD->param_begin()) OS << ", ";
2919 std::string ParamStr = (*AI)->getNameAsString();
2920 (*AI)->getType().print(OS, Policy, PlaceHolder: ParamStr);
2921 }
2922
2923 const auto *FT = cast<FunctionProtoType>(Val: AFT);
2924 if (FT->isVariadic()) {
2925 if (!BD->param_empty()) OS << ", ";
2926 OS << "...";
2927 }
2928 OS << ')';
2929 }
2930 OS << "{ }";
2931}
2932
2933void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2934 PrintExpr(E: Node->getSourceExpr());
2935}
2936
2937void StmtPrinter::VisitRecoveryExpr(RecoveryExpr *Node) {
2938 OS << "<recovery-expr>(";
2939 const char *Sep = "";
2940 for (Expr *E : Node->subExpressions()) {
2941 OS << Sep;
2942 PrintExpr(E);
2943 Sep = ", ";
2944 }
2945 OS << ')';
2946}
2947
2948void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2949 OS << "__builtin_astype(";
2950 PrintExpr(E: Node->getSrcExpr());
2951 OS << ", ";
2952 Node->getType().print(OS, Policy);
2953 OS << ")";
2954}
2955
2956void StmtPrinter::VisitHLSLOutArgExpr(HLSLOutArgExpr *Node) {
2957 PrintExpr(E: Node->getArgLValue());
2958}
2959
2960//===----------------------------------------------------------------------===//
2961// Stmt method implementations
2962//===----------------------------------------------------------------------===//
2963
2964void Stmt::dumpPretty(const ASTContext &Context) const {
2965 printPretty(OS&: llvm::errs(), Helper: nullptr, Policy: PrintingPolicy(Context.getLangOpts()));
2966}
2967
2968void Stmt::printPretty(raw_ostream &Out, PrinterHelper *Helper,
2969 const PrintingPolicy &Policy, unsigned Indentation,
2970 StringRef NL, const ASTContext *Context) const {
2971 StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context);
2972 P.Visit(S: const_cast<Stmt *>(this));
2973}
2974
2975void Stmt::printPrettyControlled(raw_ostream &Out, PrinterHelper *Helper,
2976 const PrintingPolicy &Policy,
2977 unsigned Indentation, StringRef NL,
2978 const ASTContext *Context) const {
2979 StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context);
2980 P.PrintControlledStmt(S: const_cast<Stmt *>(this));
2981}
2982
2983void Stmt::printJson(raw_ostream &Out, PrinterHelper *Helper,
2984 const PrintingPolicy &Policy, bool AddQuotes) const {
2985 std::string Buf;
2986 llvm::raw_string_ostream TempOut(Buf);
2987
2988 printPretty(Out&: TempOut, Helper, Policy);
2989
2990 Out << JsonFormat(RawSR: TempOut.str(), AddQuotes);
2991}
2992
2993//===----------------------------------------------------------------------===//
2994// PrinterHelper
2995//===----------------------------------------------------------------------===//
2996
2997// Implement virtual destructor.
2998PrinterHelper::~PrinterHelper() = default;
2999