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