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