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