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::PrintOMPExecutableDirective(OMPExecutableDirective *S,
640                                               bool ForceNoStmt) {
641   OMPClausePrinter Printer(OS, Policy);
642   ArrayRef<OMPClause *> Clauses = S->clauses();
643   for (auto *Clause : Clauses)
644     if (Clause && !Clause->isImplicit()) {
645       OS << ' ';
646       Printer.Visit(Clause);
647     }
648   OS << NL;
649   if (!ForceNoStmt && S->hasAssociatedStmt())
650     PrintStmt(S->getInnermostCapturedStmt()->getCapturedStmt());
651 }
652 
653 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
654   Indent() << "#pragma omp parallel";
655   PrintOMPExecutableDirective(Node);
656 }
657 
658 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
659   Indent() << "#pragma omp simd";
660   PrintOMPExecutableDirective(Node);
661 }
662 
663 void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
664   Indent() << "#pragma omp for";
665   PrintOMPExecutableDirective(Node);
666 }
667 
668 void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
669   Indent() << "#pragma omp for simd";
670   PrintOMPExecutableDirective(Node);
671 }
672 
673 void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
674   Indent() << "#pragma omp sections";
675   PrintOMPExecutableDirective(Node);
676 }
677 
678 void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
679   Indent() << "#pragma omp section";
680   PrintOMPExecutableDirective(Node);
681 }
682 
683 void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
684   Indent() << "#pragma omp single";
685   PrintOMPExecutableDirective(Node);
686 }
687 
688 void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
689   Indent() << "#pragma omp master";
690   PrintOMPExecutableDirective(Node);
691 }
692 
693 void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
694   Indent() << "#pragma omp critical";
695   if (Node->getDirectiveName().getName()) {
696     OS << " (";
697     Node->getDirectiveName().printName(OS, Policy);
698     OS << ")";
699   }
700   PrintOMPExecutableDirective(Node);
701 }
702 
703 void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
704   Indent() << "#pragma omp parallel for";
705   PrintOMPExecutableDirective(Node);
706 }
707 
708 void StmtPrinter::VisitOMPParallelForSimdDirective(
709     OMPParallelForSimdDirective *Node) {
710   Indent() << "#pragma omp parallel for simd";
711   PrintOMPExecutableDirective(Node);
712 }
713 
714 void StmtPrinter::VisitOMPParallelMasterDirective(
715     OMPParallelMasterDirective *Node) {
716   Indent() << "#pragma omp parallel master";
717   PrintOMPExecutableDirective(Node);
718 }
719 
720 void StmtPrinter::VisitOMPParallelSectionsDirective(
721     OMPParallelSectionsDirective *Node) {
722   Indent() << "#pragma omp parallel sections";
723   PrintOMPExecutableDirective(Node);
724 }
725 
726 void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
727   Indent() << "#pragma omp task";
728   PrintOMPExecutableDirective(Node);
729 }
730 
731 void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
732   Indent() << "#pragma omp taskyield";
733   PrintOMPExecutableDirective(Node);
734 }
735 
736 void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
737   Indent() << "#pragma omp barrier";
738   PrintOMPExecutableDirective(Node);
739 }
740 
741 void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
742   Indent() << "#pragma omp taskwait";
743   PrintOMPExecutableDirective(Node);
744 }
745 
746 void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
747   Indent() << "#pragma omp taskgroup";
748   PrintOMPExecutableDirective(Node);
749 }
750 
751 void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
752   Indent() << "#pragma omp flush";
753   PrintOMPExecutableDirective(Node);
754 }
755 
756 void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
757   Indent() << "#pragma omp ordered";
758   PrintOMPExecutableDirective(Node, Node->hasClausesOfKind<OMPDependClause>());
759 }
760 
761 void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
762   Indent() << "#pragma omp atomic";
763   PrintOMPExecutableDirective(Node);
764 }
765 
766 void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
767   Indent() << "#pragma omp target";
768   PrintOMPExecutableDirective(Node);
769 }
770 
771 void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
772   Indent() << "#pragma omp target data";
773   PrintOMPExecutableDirective(Node);
774 }
775 
776 void StmtPrinter::VisitOMPTargetEnterDataDirective(
777     OMPTargetEnterDataDirective *Node) {
778   Indent() << "#pragma omp target enter data";
779   PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
780 }
781 
782 void StmtPrinter::VisitOMPTargetExitDataDirective(
783     OMPTargetExitDataDirective *Node) {
784   Indent() << "#pragma omp target exit data";
785   PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
786 }
787 
788 void StmtPrinter::VisitOMPTargetParallelDirective(
789     OMPTargetParallelDirective *Node) {
790   Indent() << "#pragma omp target parallel";
791   PrintOMPExecutableDirective(Node);
792 }
793 
794 void StmtPrinter::VisitOMPTargetParallelForDirective(
795     OMPTargetParallelForDirective *Node) {
796   Indent() << "#pragma omp target parallel for";
797   PrintOMPExecutableDirective(Node);
798 }
799 
800 void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
801   Indent() << "#pragma omp teams";
802   PrintOMPExecutableDirective(Node);
803 }
804 
805 void StmtPrinter::VisitOMPCancellationPointDirective(
806     OMPCancellationPointDirective *Node) {
807   Indent() << "#pragma omp cancellation point "
808            << getOpenMPDirectiveName(Node->getCancelRegion());
809   PrintOMPExecutableDirective(Node);
810 }
811 
812 void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
813   Indent() << "#pragma omp cancel "
814            << getOpenMPDirectiveName(Node->getCancelRegion());
815   PrintOMPExecutableDirective(Node);
816 }
817 
818 void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) {
819   Indent() << "#pragma omp taskloop";
820   PrintOMPExecutableDirective(Node);
821 }
822 
823 void StmtPrinter::VisitOMPTaskLoopSimdDirective(
824     OMPTaskLoopSimdDirective *Node) {
825   Indent() << "#pragma omp taskloop simd";
826   PrintOMPExecutableDirective(Node);
827 }
828 
829 void StmtPrinter::VisitOMPMasterTaskLoopDirective(
830     OMPMasterTaskLoopDirective *Node) {
831   Indent() << "#pragma omp master taskloop";
832   PrintOMPExecutableDirective(Node);
833 }
834 
835 void StmtPrinter::VisitOMPMasterTaskLoopSimdDirective(
836     OMPMasterTaskLoopSimdDirective *Node) {
837   Indent() << "#pragma omp master taskloop simd";
838   PrintOMPExecutableDirective(Node);
839 }
840 
841 void StmtPrinter::VisitOMPParallelMasterTaskLoopDirective(
842     OMPParallelMasterTaskLoopDirective *Node) {
843   Indent() << "#pragma omp parallel master taskloop";
844   PrintOMPExecutableDirective(Node);
845 }
846 
847 void StmtPrinter::VisitOMPParallelMasterTaskLoopSimdDirective(
848     OMPParallelMasterTaskLoopSimdDirective *Node) {
849   Indent() << "#pragma omp parallel master taskloop simd";
850   PrintOMPExecutableDirective(Node);
851 }
852 
853 void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) {
854   Indent() << "#pragma omp distribute";
855   PrintOMPExecutableDirective(Node);
856 }
857 
858 void StmtPrinter::VisitOMPTargetUpdateDirective(
859     OMPTargetUpdateDirective *Node) {
860   Indent() << "#pragma omp target update";
861   PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
862 }
863 
864 void StmtPrinter::VisitOMPDistributeParallelForDirective(
865     OMPDistributeParallelForDirective *Node) {
866   Indent() << "#pragma omp distribute parallel for";
867   PrintOMPExecutableDirective(Node);
868 }
869 
870 void StmtPrinter::VisitOMPDistributeParallelForSimdDirective(
871     OMPDistributeParallelForSimdDirective *Node) {
872   Indent() << "#pragma omp distribute parallel for simd";
873   PrintOMPExecutableDirective(Node);
874 }
875 
876 void StmtPrinter::VisitOMPDistributeSimdDirective(
877     OMPDistributeSimdDirective *Node) {
878   Indent() << "#pragma omp distribute simd";
879   PrintOMPExecutableDirective(Node);
880 }
881 
882 void StmtPrinter::VisitOMPTargetParallelForSimdDirective(
883     OMPTargetParallelForSimdDirective *Node) {
884   Indent() << "#pragma omp target parallel for simd";
885   PrintOMPExecutableDirective(Node);
886 }
887 
888 void StmtPrinter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective *Node) {
889   Indent() << "#pragma omp target simd";
890   PrintOMPExecutableDirective(Node);
891 }
892 
893 void StmtPrinter::VisitOMPTeamsDistributeDirective(
894     OMPTeamsDistributeDirective *Node) {
895   Indent() << "#pragma omp teams distribute";
896   PrintOMPExecutableDirective(Node);
897 }
898 
899 void StmtPrinter::VisitOMPTeamsDistributeSimdDirective(
900     OMPTeamsDistributeSimdDirective *Node) {
901   Indent() << "#pragma omp teams distribute simd";
902   PrintOMPExecutableDirective(Node);
903 }
904 
905 void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective(
906     OMPTeamsDistributeParallelForSimdDirective *Node) {
907   Indent() << "#pragma omp teams distribute parallel for simd";
908   PrintOMPExecutableDirective(Node);
909 }
910 
911 void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective(
912     OMPTeamsDistributeParallelForDirective *Node) {
913   Indent() << "#pragma omp teams distribute parallel for";
914   PrintOMPExecutableDirective(Node);
915 }
916 
917 void StmtPrinter::VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective *Node) {
918   Indent() << "#pragma omp target teams";
919   PrintOMPExecutableDirective(Node);
920 }
921 
922 void StmtPrinter::VisitOMPTargetTeamsDistributeDirective(
923     OMPTargetTeamsDistributeDirective *Node) {
924   Indent() << "#pragma omp target teams distribute";
925   PrintOMPExecutableDirective(Node);
926 }
927 
928 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective(
929     OMPTargetTeamsDistributeParallelForDirective *Node) {
930   Indent() << "#pragma omp target teams distribute parallel for";
931   PrintOMPExecutableDirective(Node);
932 }
933 
934 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
935     OMPTargetTeamsDistributeParallelForSimdDirective *Node) {
936   Indent() << "#pragma omp target teams distribute parallel for simd";
937   PrintOMPExecutableDirective(Node);
938 }
939 
940 void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective(
941     OMPTargetTeamsDistributeSimdDirective *Node) {
942   Indent() << "#pragma omp target teams distribute simd";
943   PrintOMPExecutableDirective(Node);
944 }
945 
946 //===----------------------------------------------------------------------===//
947 //  Expr printing methods.
948 //===----------------------------------------------------------------------===//
949 
950 void StmtPrinter::VisitSourceLocExpr(SourceLocExpr *Node) {
951   OS << Node->getBuiltinStr() << "()";
952 }
953 
954 void StmtPrinter::VisitConstantExpr(ConstantExpr *Node) {
955   PrintExpr(Node->getSubExpr());
956 }
957 
958 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
959   if (const auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) {
960     OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy);
961     return;
962   }
963   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
964     Qualifier->print(OS, Policy);
965   if (Node->hasTemplateKeyword())
966     OS << "template ";
967   OS << Node->getNameInfo();
968   if (Node->hasExplicitTemplateArgs())
969     printTemplateArgumentList(OS, Node->template_arguments(), Policy);
970 }
971 
972 void StmtPrinter::VisitDependentScopeDeclRefExpr(
973                                            DependentScopeDeclRefExpr *Node) {
974   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
975     Qualifier->print(OS, Policy);
976   if (Node->hasTemplateKeyword())
977     OS << "template ";
978   OS << Node->getNameInfo();
979   if (Node->hasExplicitTemplateArgs())
980     printTemplateArgumentList(OS, Node->template_arguments(), Policy);
981 }
982 
983 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
984   if (Node->getQualifier())
985     Node->getQualifier()->print(OS, Policy);
986   if (Node->hasTemplateKeyword())
987     OS << "template ";
988   OS << Node->getNameInfo();
989   if (Node->hasExplicitTemplateArgs())
990     printTemplateArgumentList(OS, Node->template_arguments(), Policy);
991 }
992 
993 static bool isImplicitSelf(const Expr *E) {
994   if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
995     if (const auto *PD = dyn_cast<ImplicitParamDecl>(DRE->getDecl())) {
996       if (PD->getParameterKind() == ImplicitParamDecl::ObjCSelf &&
997           DRE->getBeginLoc().isInvalid())
998         return true;
999     }
1000   }
1001   return false;
1002 }
1003 
1004 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
1005   if (Node->getBase()) {
1006     if (!Policy.SuppressImplicitBase ||
1007         !isImplicitSelf(Node->getBase()->IgnoreImpCasts())) {
1008       PrintExpr(Node->getBase());
1009       OS << (Node->isArrow() ? "->" : ".");
1010     }
1011   }
1012   OS << *Node->getDecl();
1013 }
1014 
1015 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
1016   if (Node->isSuperReceiver())
1017     OS << "super.";
1018   else if (Node->isObjectReceiver() && Node->getBase()) {
1019     PrintExpr(Node->getBase());
1020     OS << ".";
1021   } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1022     OS << Node->getClassReceiver()->getName() << ".";
1023   }
1024 
1025   if (Node->isImplicitProperty()) {
1026     if (const auto *Getter = Node->getImplicitPropertyGetter())
1027       Getter->getSelector().print(OS);
1028     else
1029       OS << SelectorTable::getPropertyNameFromSetterSelector(
1030           Node->getImplicitPropertySetter()->getSelector());
1031   } else
1032     OS << Node->getExplicitProperty()->getName();
1033 }
1034 
1035 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1036   PrintExpr(Node->getBaseExpr());
1037   OS << "[";
1038   PrintExpr(Node->getKeyExpr());
1039   OS << "]";
1040 }
1041 
1042 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
1043   OS << PredefinedExpr::getIdentKindName(Node->getIdentKind());
1044 }
1045 
1046 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
1047   unsigned value = Node->getValue();
1048 
1049   switch (Node->getKind()) {
1050   case CharacterLiteral::Ascii: break; // no prefix.
1051   case CharacterLiteral::Wide:  OS << 'L'; break;
1052   case CharacterLiteral::UTF8:  OS << "u8"; break;
1053   case CharacterLiteral::UTF16: OS << 'u'; break;
1054   case CharacterLiteral::UTF32: OS << 'U'; break;
1055   }
1056 
1057   switch (value) {
1058   case '\\':
1059     OS << "'\\\\'";
1060     break;
1061   case '\'':
1062     OS << "'\\''";
1063     break;
1064   case '\a':
1065     // TODO: K&R: the meaning of '\\a' is different in traditional C
1066     OS << "'\\a'";
1067     break;
1068   case '\b':
1069     OS << "'\\b'";
1070     break;
1071   // Nonstandard escape sequence.
1072   /*case '\e':
1073     OS << "'\\e'";
1074     break;*/
1075   case '\f':
1076     OS << "'\\f'";
1077     break;
1078   case '\n':
1079     OS << "'\\n'";
1080     break;
1081   case '\r':
1082     OS << "'\\r'";
1083     break;
1084   case '\t':
1085     OS << "'\\t'";
1086     break;
1087   case '\v':
1088     OS << "'\\v'";
1089     break;
1090   default:
1091     // A character literal might be sign-extended, which
1092     // would result in an invalid \U escape sequence.
1093     // FIXME: multicharacter literals such as '\xFF\xFF\xFF\xFF'
1094     // are not correctly handled.
1095     if ((value & ~0xFFu) == ~0xFFu && Node->getKind() == CharacterLiteral::Ascii)
1096       value &= 0xFFu;
1097     if (value < 256 && isPrintable((unsigned char)value))
1098       OS << "'" << (char)value << "'";
1099     else if (value < 256)
1100       OS << "'\\x" << llvm::format("%02x", value) << "'";
1101     else if (value <= 0xFFFF)
1102       OS << "'\\u" << llvm::format("%04x", value) << "'";
1103     else
1104       OS << "'\\U" << llvm::format("%08x", value) << "'";
1105   }
1106 }
1107 
1108 /// Prints the given expression using the original source text. Returns true on
1109 /// success, false otherwise.
1110 static bool printExprAsWritten(raw_ostream &OS, Expr *E,
1111                                const ASTContext *Context) {
1112   if (!Context)
1113     return false;
1114   bool Invalid = false;
1115   StringRef Source = Lexer::getSourceText(
1116       CharSourceRange::getTokenRange(E->getSourceRange()),
1117       Context->getSourceManager(), Context->getLangOpts(), &Invalid);
1118   if (!Invalid) {
1119     OS << Source;
1120     return true;
1121   }
1122   return false;
1123 }
1124 
1125 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
1126   if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1127     return;
1128   bool isSigned = Node->getType()->isSignedIntegerType();
1129   OS << Node->getValue().toString(10, isSigned);
1130 
1131   // Emit suffixes.  Integer literals are always a builtin integer type.
1132   switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1133   default: llvm_unreachable("Unexpected type for integer literal!");
1134   case BuiltinType::Char_S:
1135   case BuiltinType::Char_U:    OS << "i8"; break;
1136   case BuiltinType::UChar:     OS << "Ui8"; break;
1137   case BuiltinType::Short:     OS << "i16"; break;
1138   case BuiltinType::UShort:    OS << "Ui16"; break;
1139   case BuiltinType::Int:       break; // no suffix.
1140   case BuiltinType::UInt:      OS << 'U'; break;
1141   case BuiltinType::Long:      OS << 'L'; break;
1142   case BuiltinType::ULong:     OS << "UL"; break;
1143   case BuiltinType::LongLong:  OS << "LL"; break;
1144   case BuiltinType::ULongLong: OS << "ULL"; break;
1145   }
1146 }
1147 
1148 void StmtPrinter::VisitFixedPointLiteral(FixedPointLiteral *Node) {
1149   if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1150     return;
1151   OS << Node->getValueAsString(/*Radix=*/10);
1152 
1153   switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1154     default: llvm_unreachable("Unexpected type for fixed point literal!");
1155     case BuiltinType::ShortFract:   OS << "hr"; break;
1156     case BuiltinType::ShortAccum:   OS << "hk"; break;
1157     case BuiltinType::UShortFract:  OS << "uhr"; break;
1158     case BuiltinType::UShortAccum:  OS << "uhk"; break;
1159     case BuiltinType::Fract:        OS << "r"; break;
1160     case BuiltinType::Accum:        OS << "k"; break;
1161     case BuiltinType::UFract:       OS << "ur"; break;
1162     case BuiltinType::UAccum:       OS << "uk"; break;
1163     case BuiltinType::LongFract:    OS << "lr"; break;
1164     case BuiltinType::LongAccum:    OS << "lk"; break;
1165     case BuiltinType::ULongFract:   OS << "ulr"; break;
1166     case BuiltinType::ULongAccum:   OS << "ulk"; break;
1167   }
1168 }
1169 
1170 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1171                                  bool PrintSuffix) {
1172   SmallString<16> Str;
1173   Node->getValue().toString(Str);
1174   OS << Str;
1175   if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1176     OS << '.'; // Trailing dot in order to separate from ints.
1177 
1178   if (!PrintSuffix)
1179     return;
1180 
1181   // Emit suffixes.  Float literals are always a builtin float type.
1182   switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1183   default: llvm_unreachable("Unexpected type for float literal!");
1184   case BuiltinType::Half:       break; // FIXME: suffix?
1185   case BuiltinType::Double:     break; // no suffix.
1186   case BuiltinType::Float16:    OS << "F16"; break;
1187   case BuiltinType::Float:      OS << 'F'; break;
1188   case BuiltinType::LongDouble: OS << 'L'; break;
1189   case BuiltinType::Float128:   OS << 'Q'; break;
1190   }
1191 }
1192 
1193 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1194   if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1195     return;
1196   PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
1197 }
1198 
1199 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1200   PrintExpr(Node->getSubExpr());
1201   OS << "i";
1202 }
1203 
1204 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
1205   Str->outputString(OS);
1206 }
1207 
1208 void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1209   OS << "(";
1210   PrintExpr(Node->getSubExpr());
1211   OS << ")";
1212 }
1213 
1214 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
1215   if (!Node->isPostfix()) {
1216     OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1217 
1218     // Print a space if this is an "identifier operator" like __real, or if
1219     // it might be concatenated incorrectly like '+'.
1220     switch (Node->getOpcode()) {
1221     default: break;
1222     case UO_Real:
1223     case UO_Imag:
1224     case UO_Extension:
1225       OS << ' ';
1226       break;
1227     case UO_Plus:
1228     case UO_Minus:
1229       if (isa<UnaryOperator>(Node->getSubExpr()))
1230         OS << ' ';
1231       break;
1232     }
1233   }
1234   PrintExpr(Node->getSubExpr());
1235 
1236   if (Node->isPostfix())
1237     OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1238 }
1239 
1240 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1241   OS << "__builtin_offsetof(";
1242   Node->getTypeSourceInfo()->getType().print(OS, Policy);
1243   OS << ", ";
1244   bool PrintedSomething = false;
1245   for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1246     OffsetOfNode ON = Node->getComponent(i);
1247     if (ON.getKind() == OffsetOfNode::Array) {
1248       // Array node
1249       OS << "[";
1250       PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1251       OS << "]";
1252       PrintedSomething = true;
1253       continue;
1254     }
1255 
1256     // Skip implicit base indirections.
1257     if (ON.getKind() == OffsetOfNode::Base)
1258       continue;
1259 
1260     // Field or identifier node.
1261     IdentifierInfo *Id = ON.getFieldName();
1262     if (!Id)
1263       continue;
1264 
1265     if (PrintedSomething)
1266       OS << ".";
1267     else
1268       PrintedSomething = true;
1269     OS << Id->getName();
1270   }
1271   OS << ")";
1272 }
1273 
1274 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
1275   switch(Node->getKind()) {
1276   case UETT_SizeOf:
1277     OS << "sizeof";
1278     break;
1279   case UETT_AlignOf:
1280     if (Policy.Alignof)
1281       OS << "alignof";
1282     else if (Policy.UnderscoreAlignof)
1283       OS << "_Alignof";
1284     else
1285       OS << "__alignof";
1286     break;
1287   case UETT_PreferredAlignOf:
1288     OS << "__alignof";
1289     break;
1290   case UETT_VecStep:
1291     OS << "vec_step";
1292     break;
1293   case UETT_OpenMPRequiredSimdAlign:
1294     OS << "__builtin_omp_required_simd_align";
1295     break;
1296   }
1297   if (Node->isArgumentType()) {
1298     OS << '(';
1299     Node->getArgumentType().print(OS, Policy);
1300     OS << ')';
1301   } else {
1302     OS << " ";
1303     PrintExpr(Node->getArgumentExpr());
1304   }
1305 }
1306 
1307 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1308   OS << "_Generic(";
1309   PrintExpr(Node->getControllingExpr());
1310   for (const GenericSelectionExpr::Association Assoc : Node->associations()) {
1311     OS << ", ";
1312     QualType T = Assoc.getType();
1313     if (T.isNull())
1314       OS << "default";
1315     else
1316       T.print(OS, Policy);
1317     OS << ": ";
1318     PrintExpr(Assoc.getAssociationExpr());
1319   }
1320   OS << ")";
1321 }
1322 
1323 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
1324   PrintExpr(Node->getLHS());
1325   OS << "[";
1326   PrintExpr(Node->getRHS());
1327   OS << "]";
1328 }
1329 
1330 void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) {
1331   PrintExpr(Node->getBase());
1332   OS << "[";
1333   if (Node->getLowerBound())
1334     PrintExpr(Node->getLowerBound());
1335   if (Node->getColonLoc().isValid()) {
1336     OS << ":";
1337     if (Node->getLength())
1338       PrintExpr(Node->getLength());
1339   }
1340   OS << "]";
1341 }
1342 
1343 void StmtPrinter::PrintCallArgs(CallExpr *Call) {
1344   for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
1345     if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1346       // Don't print any defaulted arguments
1347       break;
1348     }
1349 
1350     if (i) OS << ", ";
1351     PrintExpr(Call->getArg(i));
1352   }
1353 }
1354 
1355 void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1356   PrintExpr(Call->getCallee());
1357   OS << "(";
1358   PrintCallArgs(Call);
1359   OS << ")";
1360 }
1361 
1362 static bool isImplicitThis(const Expr *E) {
1363   if (const auto *TE = dyn_cast<CXXThisExpr>(E))
1364     return TE->isImplicit();
1365   return false;
1366 }
1367 
1368 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1369   if (!Policy.SuppressImplicitBase || !isImplicitThis(Node->getBase())) {
1370     PrintExpr(Node->getBase());
1371 
1372     auto *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1373     FieldDecl *ParentDecl =
1374         ParentMember ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl())
1375                      : nullptr;
1376 
1377     if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1378       OS << (Node->isArrow() ? "->" : ".");
1379   }
1380 
1381   if (auto *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1382     if (FD->isAnonymousStructOrUnion())
1383       return;
1384 
1385   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1386     Qualifier->print(OS, Policy);
1387   if (Node->hasTemplateKeyword())
1388     OS << "template ";
1389   OS << Node->getMemberNameInfo();
1390   if (Node->hasExplicitTemplateArgs())
1391     printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1392 }
1393 
1394 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1395   PrintExpr(Node->getBase());
1396   OS << (Node->isArrow() ? "->isa" : ".isa");
1397 }
1398 
1399 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1400   PrintExpr(Node->getBase());
1401   OS << ".";
1402   OS << Node->getAccessor().getName();
1403 }
1404 
1405 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1406   OS << '(';
1407   Node->getTypeAsWritten().print(OS, Policy);
1408   OS << ')';
1409   PrintExpr(Node->getSubExpr());
1410 }
1411 
1412 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1413   OS << '(';
1414   Node->getType().print(OS, Policy);
1415   OS << ')';
1416   PrintExpr(Node->getInitializer());
1417 }
1418 
1419 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1420   // No need to print anything, simply forward to the subexpression.
1421   PrintExpr(Node->getSubExpr());
1422 }
1423 
1424 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1425   PrintExpr(Node->getLHS());
1426   OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1427   PrintExpr(Node->getRHS());
1428 }
1429 
1430 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1431   PrintExpr(Node->getLHS());
1432   OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1433   PrintExpr(Node->getRHS());
1434 }
1435 
1436 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1437   PrintExpr(Node->getCond());
1438   OS << " ? ";
1439   PrintExpr(Node->getLHS());
1440   OS << " : ";
1441   PrintExpr(Node->getRHS());
1442 }
1443 
1444 // GNU extensions.
1445 
1446 void
1447 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1448   PrintExpr(Node->getCommon());
1449   OS << " ?: ";
1450   PrintExpr(Node->getFalseExpr());
1451 }
1452 
1453 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1454   OS << "&&" << Node->getLabel()->getName();
1455 }
1456 
1457 void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1458   OS << "(";
1459   PrintRawCompoundStmt(E->getSubStmt());
1460   OS << ")";
1461 }
1462 
1463 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1464   OS << "__builtin_choose_expr(";
1465   PrintExpr(Node->getCond());
1466   OS << ", ";
1467   PrintExpr(Node->getLHS());
1468   OS << ", ";
1469   PrintExpr(Node->getRHS());
1470   OS << ")";
1471 }
1472 
1473 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1474   OS << "__null";
1475 }
1476 
1477 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1478   OS << "__builtin_shufflevector(";
1479   for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1480     if (i) OS << ", ";
1481     PrintExpr(Node->getExpr(i));
1482   }
1483   OS << ")";
1484 }
1485 
1486 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1487   OS << "__builtin_convertvector(";
1488   PrintExpr(Node->getSrcExpr());
1489   OS << ", ";
1490   Node->getType().print(OS, Policy);
1491   OS << ")";
1492 }
1493 
1494 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1495   if (Node->getSyntacticForm()) {
1496     Visit(Node->getSyntacticForm());
1497     return;
1498   }
1499 
1500   OS << "{";
1501   for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1502     if (i) OS << ", ";
1503     if (Node->getInit(i))
1504       PrintExpr(Node->getInit(i));
1505     else
1506       OS << "{}";
1507   }
1508   OS << "}";
1509 }
1510 
1511 void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) {
1512   // There's no way to express this expression in any of our supported
1513   // languages, so just emit something terse and (hopefully) clear.
1514   OS << "{";
1515   PrintExpr(Node->getSubExpr());
1516   OS << "}";
1517 }
1518 
1519 void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) {
1520   OS << "*";
1521 }
1522 
1523 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1524   OS << "(";
1525   for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1526     if (i) OS << ", ";
1527     PrintExpr(Node->getExpr(i));
1528   }
1529   OS << ")";
1530 }
1531 
1532 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1533   bool NeedsEquals = true;
1534   for (const DesignatedInitExpr::Designator &D : Node->designators()) {
1535     if (D.isFieldDesignator()) {
1536       if (D.getDotLoc().isInvalid()) {
1537         if (IdentifierInfo *II = D.getFieldName()) {
1538           OS << II->getName() << ":";
1539           NeedsEquals = false;
1540         }
1541       } else {
1542         OS << "." << D.getFieldName()->getName();
1543       }
1544     } else {
1545       OS << "[";
1546       if (D.isArrayDesignator()) {
1547         PrintExpr(Node->getArrayIndex(D));
1548       } else {
1549         PrintExpr(Node->getArrayRangeStart(D));
1550         OS << " ... ";
1551         PrintExpr(Node->getArrayRangeEnd(D));
1552       }
1553       OS << "]";
1554     }
1555   }
1556 
1557   if (NeedsEquals)
1558     OS << " = ";
1559   else
1560     OS << " ";
1561   PrintExpr(Node->getInit());
1562 }
1563 
1564 void StmtPrinter::VisitDesignatedInitUpdateExpr(
1565     DesignatedInitUpdateExpr *Node) {
1566   OS << "{";
1567   OS << "/*base*/";
1568   PrintExpr(Node->getBase());
1569   OS << ", ";
1570 
1571   OS << "/*updater*/";
1572   PrintExpr(Node->getUpdater());
1573   OS << "}";
1574 }
1575 
1576 void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1577   OS << "/*no init*/";
1578 }
1579 
1580 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1581   if (Node->getType()->getAsCXXRecordDecl()) {
1582     OS << "/*implicit*/";
1583     Node->getType().print(OS, Policy);
1584     OS << "()";
1585   } else {
1586     OS << "/*implicit*/(";
1587     Node->getType().print(OS, Policy);
1588     OS << ')';
1589     if (Node->getType()->isRecordType())
1590       OS << "{}";
1591     else
1592       OS << 0;
1593   }
1594 }
1595 
1596 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1597   OS << "__builtin_va_arg(";
1598   PrintExpr(Node->getSubExpr());
1599   OS << ", ";
1600   Node->getType().print(OS, Policy);
1601   OS << ")";
1602 }
1603 
1604 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1605   PrintExpr(Node->getSyntacticForm());
1606 }
1607 
1608 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1609   const char *Name = nullptr;
1610   switch (Node->getOp()) {
1611 #define BUILTIN(ID, TYPE, ATTRS)
1612 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1613   case AtomicExpr::AO ## ID: \
1614     Name = #ID "("; \
1615     break;
1616 #include "clang/Basic/Builtins.def"
1617   }
1618   OS << Name;
1619 
1620   // AtomicExpr stores its subexpressions in a permuted order.
1621   PrintExpr(Node->getPtr());
1622   if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1623       Node->getOp() != AtomicExpr::AO__atomic_load_n &&
1624       Node->getOp() != AtomicExpr::AO__opencl_atomic_load) {
1625     OS << ", ";
1626     PrintExpr(Node->getVal1());
1627   }
1628   if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1629       Node->isCmpXChg()) {
1630     OS << ", ";
1631     PrintExpr(Node->getVal2());
1632   }
1633   if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1634       Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1635     OS << ", ";
1636     PrintExpr(Node->getWeak());
1637   }
1638   if (Node->getOp() != AtomicExpr::AO__c11_atomic_init &&
1639       Node->getOp() != AtomicExpr::AO__opencl_atomic_init) {
1640     OS << ", ";
1641     PrintExpr(Node->getOrder());
1642   }
1643   if (Node->isCmpXChg()) {
1644     OS << ", ";
1645     PrintExpr(Node->getOrderFail());
1646   }
1647   OS << ")";
1648 }
1649 
1650 // C++
1651 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1652   OverloadedOperatorKind Kind = Node->getOperator();
1653   if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1654     if (Node->getNumArgs() == 1) {
1655       OS << getOperatorSpelling(Kind) << ' ';
1656       PrintExpr(Node->getArg(0));
1657     } else {
1658       PrintExpr(Node->getArg(0));
1659       OS << ' ' << getOperatorSpelling(Kind);
1660     }
1661   } else if (Kind == OO_Arrow) {
1662     PrintExpr(Node->getArg(0));
1663   } else if (Kind == OO_Call) {
1664     PrintExpr(Node->getArg(0));
1665     OS << '(';
1666     for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1667       if (ArgIdx > 1)
1668         OS << ", ";
1669       if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1670         PrintExpr(Node->getArg(ArgIdx));
1671     }
1672     OS << ')';
1673   } else if (Kind == OO_Subscript) {
1674     PrintExpr(Node->getArg(0));
1675     OS << '[';
1676     PrintExpr(Node->getArg(1));
1677     OS << ']';
1678   } else if (Node->getNumArgs() == 1) {
1679     OS << getOperatorSpelling(Kind) << ' ';
1680     PrintExpr(Node->getArg(0));
1681   } else if (Node->getNumArgs() == 2) {
1682     PrintExpr(Node->getArg(0));
1683     OS << ' ' << getOperatorSpelling(Kind) << ' ';
1684     PrintExpr(Node->getArg(1));
1685   } else {
1686     llvm_unreachable("unknown overloaded operator");
1687   }
1688 }
1689 
1690 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1691   // If we have a conversion operator call only print the argument.
1692   CXXMethodDecl *MD = Node->getMethodDecl();
1693   if (MD && isa<CXXConversionDecl>(MD)) {
1694     PrintExpr(Node->getImplicitObjectArgument());
1695     return;
1696   }
1697   VisitCallExpr(cast<CallExpr>(Node));
1698 }
1699 
1700 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1701   PrintExpr(Node->getCallee());
1702   OS << "<<<";
1703   PrintCallArgs(Node->getConfig());
1704   OS << ">>>(";
1705   PrintCallArgs(Node);
1706   OS << ")";
1707 }
1708 
1709 void StmtPrinter::VisitCXXRewrittenBinaryOperator(
1710     CXXRewrittenBinaryOperator *Node) {
1711   CXXRewrittenBinaryOperator::DecomposedForm Decomposed =
1712       Node->getDecomposedForm();
1713   PrintExpr(const_cast<Expr*>(Decomposed.LHS));
1714   OS << ' ' << BinaryOperator::getOpcodeStr(Decomposed.Opcode) << ' ';
1715   PrintExpr(const_cast<Expr*>(Decomposed.RHS));
1716 }
1717 
1718 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1719   OS << Node->getCastName() << '<';
1720   Node->getTypeAsWritten().print(OS, Policy);
1721   OS << ">(";
1722   PrintExpr(Node->getSubExpr());
1723   OS << ")";
1724 }
1725 
1726 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1727   VisitCXXNamedCastExpr(Node);
1728 }
1729 
1730 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1731   VisitCXXNamedCastExpr(Node);
1732 }
1733 
1734 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1735   VisitCXXNamedCastExpr(Node);
1736 }
1737 
1738 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1739   VisitCXXNamedCastExpr(Node);
1740 }
1741 
1742 void StmtPrinter::VisitBuiltinBitCastExpr(BuiltinBitCastExpr *Node) {
1743   OS << "__builtin_bit_cast(";
1744   Node->getTypeInfoAsWritten()->getType().print(OS, Policy);
1745   OS << ", ";
1746   PrintExpr(Node->getSubExpr());
1747   OS << ")";
1748 }
1749 
1750 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1751   OS << "typeid(";
1752   if (Node->isTypeOperand()) {
1753     Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1754   } else {
1755     PrintExpr(Node->getExprOperand());
1756   }
1757   OS << ")";
1758 }
1759 
1760 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1761   OS << "__uuidof(";
1762   if (Node->isTypeOperand()) {
1763     Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1764   } else {
1765     PrintExpr(Node->getExprOperand());
1766   }
1767   OS << ")";
1768 }
1769 
1770 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1771   PrintExpr(Node->getBaseExpr());
1772   if (Node->isArrow())
1773     OS << "->";
1774   else
1775     OS << ".";
1776   if (NestedNameSpecifier *Qualifier =
1777       Node->getQualifierLoc().getNestedNameSpecifier())
1778     Qualifier->print(OS, Policy);
1779   OS << Node->getPropertyDecl()->getDeclName();
1780 }
1781 
1782 void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
1783   PrintExpr(Node->getBase());
1784   OS << "[";
1785   PrintExpr(Node->getIdx());
1786   OS << "]";
1787 }
1788 
1789 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1790   switch (Node->getLiteralOperatorKind()) {
1791   case UserDefinedLiteral::LOK_Raw:
1792     OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
1793     break;
1794   case UserDefinedLiteral::LOK_Template: {
1795     const auto *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1796     const TemplateArgumentList *Args =
1797       cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1798     assert(Args);
1799 
1800     if (Args->size() != 1) {
1801       OS << "operator\"\"" << Node->getUDSuffix()->getName();
1802       printTemplateArgumentList(OS, Args->asArray(), Policy);
1803       OS << "()";
1804       return;
1805     }
1806 
1807     const TemplateArgument &Pack = Args->get(0);
1808     for (const auto &P : Pack.pack_elements()) {
1809       char C = (char)P.getAsIntegral().getZExtValue();
1810       OS << C;
1811     }
1812     break;
1813   }
1814   case UserDefinedLiteral::LOK_Integer: {
1815     // Print integer literal without suffix.
1816     const auto *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1817     OS << Int->getValue().toString(10, /*isSigned*/false);
1818     break;
1819   }
1820   case UserDefinedLiteral::LOK_Floating: {
1821     // Print floating literal without suffix.
1822     auto *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1823     PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1824     break;
1825   }
1826   case UserDefinedLiteral::LOK_String:
1827   case UserDefinedLiteral::LOK_Character:
1828     PrintExpr(Node->getCookedLiteral());
1829     break;
1830   }
1831   OS << Node->getUDSuffix()->getName();
1832 }
1833 
1834 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1835   OS << (Node->getValue() ? "true" : "false");
1836 }
1837 
1838 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1839   OS << "nullptr";
1840 }
1841 
1842 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1843   OS << "this";
1844 }
1845 
1846 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1847   if (!Node->getSubExpr())
1848     OS << "throw";
1849   else {
1850     OS << "throw ";
1851     PrintExpr(Node->getSubExpr());
1852   }
1853 }
1854 
1855 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1856   // Nothing to print: we picked up the default argument.
1857 }
1858 
1859 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1860   // Nothing to print: we picked up the default initializer.
1861 }
1862 
1863 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1864   Node->getType().print(OS, Policy);
1865   // If there are no parens, this is list-initialization, and the braces are
1866   // part of the syntax of the inner construct.
1867   if (Node->getLParenLoc().isValid())
1868     OS << "(";
1869   PrintExpr(Node->getSubExpr());
1870   if (Node->getLParenLoc().isValid())
1871     OS << ")";
1872 }
1873 
1874 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1875   PrintExpr(Node->getSubExpr());
1876 }
1877 
1878 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1879   Node->getType().print(OS, Policy);
1880   if (Node->isStdInitListInitialization())
1881     /* Nothing to do; braces are part of creating the std::initializer_list. */;
1882   else if (Node->isListInitialization())
1883     OS << "{";
1884   else
1885     OS << "(";
1886   for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1887                                          ArgEnd = Node->arg_end();
1888        Arg != ArgEnd; ++Arg) {
1889     if ((*Arg)->isDefaultArgument())
1890       break;
1891     if (Arg != Node->arg_begin())
1892       OS << ", ";
1893     PrintExpr(*Arg);
1894   }
1895   if (Node->isStdInitListInitialization())
1896     /* See above. */;
1897   else if (Node->isListInitialization())
1898     OS << "}";
1899   else
1900     OS << ")";
1901 }
1902 
1903 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1904   OS << '[';
1905   bool NeedComma = false;
1906   switch (Node->getCaptureDefault()) {
1907   case LCD_None:
1908     break;
1909 
1910   case LCD_ByCopy:
1911     OS << '=';
1912     NeedComma = true;
1913     break;
1914 
1915   case LCD_ByRef:
1916     OS << '&';
1917     NeedComma = true;
1918     break;
1919   }
1920   for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1921                                  CEnd = Node->explicit_capture_end();
1922        C != CEnd;
1923        ++C) {
1924     if (C->capturesVLAType())
1925       continue;
1926 
1927     if (NeedComma)
1928       OS << ", ";
1929     NeedComma = true;
1930 
1931     switch (C->getCaptureKind()) {
1932     case LCK_This:
1933       OS << "this";
1934       break;
1935 
1936     case LCK_StarThis:
1937       OS << "*this";
1938       break;
1939 
1940     case LCK_ByRef:
1941       if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
1942         OS << '&';
1943       OS << C->getCapturedVar()->getName();
1944       break;
1945 
1946     case LCK_ByCopy:
1947       OS << C->getCapturedVar()->getName();
1948       break;
1949 
1950     case LCK_VLAType:
1951       llvm_unreachable("VLA type in explicit captures.");
1952     }
1953 
1954     if (C->isPackExpansion())
1955       OS << "...";
1956 
1957     if (Node->isInitCapture(C))
1958       PrintExpr(C->getCapturedVar()->getInit());
1959   }
1960   OS << ']';
1961 
1962   if (!Node->getExplicitTemplateParameters().empty()) {
1963     Node->getTemplateParameterList()->print(
1964         OS, Node->getLambdaClass()->getASTContext(),
1965         /*OmitTemplateKW*/true);
1966   }
1967 
1968   if (Node->hasExplicitParameters()) {
1969     OS << '(';
1970     CXXMethodDecl *Method = Node->getCallOperator();
1971     NeedComma = false;
1972     for (const auto *P : Method->parameters()) {
1973       if (NeedComma) {
1974         OS << ", ";
1975       } else {
1976         NeedComma = true;
1977       }
1978       std::string ParamStr = P->getNameAsString();
1979       P->getOriginalType().print(OS, Policy, ParamStr);
1980     }
1981     if (Method->isVariadic()) {
1982       if (NeedComma)
1983         OS << ", ";
1984       OS << "...";
1985     }
1986     OS << ')';
1987 
1988     if (Node->isMutable())
1989       OS << " mutable";
1990 
1991     auto *Proto = Method->getType()->castAs<FunctionProtoType>();
1992     Proto->printExceptionSpecification(OS, Policy);
1993 
1994     // FIXME: Attributes
1995 
1996     // Print the trailing return type if it was specified in the source.
1997     if (Node->hasExplicitResultType()) {
1998       OS << " -> ";
1999       Proto->getReturnType().print(OS, Policy);
2000     }
2001   }
2002 
2003   // Print the body.
2004   OS << ' ';
2005   if (Policy.TerseOutput)
2006     OS << "{}";
2007   else
2008     PrintRawCompoundStmt(Node->getBody());
2009 }
2010 
2011 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
2012   if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
2013     TSInfo->getType().print(OS, Policy);
2014   else
2015     Node->getType().print(OS, Policy);
2016   OS << "()";
2017 }
2018 
2019 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
2020   if (E->isGlobalNew())
2021     OS << "::";
2022   OS << "new ";
2023   unsigned NumPlace = E->getNumPlacementArgs();
2024   if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
2025     OS << "(";
2026     PrintExpr(E->getPlacementArg(0));
2027     for (unsigned i = 1; i < NumPlace; ++i) {
2028       if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
2029         break;
2030       OS << ", ";
2031       PrintExpr(E->getPlacementArg(i));
2032     }
2033     OS << ") ";
2034   }
2035   if (E->isParenTypeId())
2036     OS << "(";
2037   std::string TypeS;
2038   if (Optional<Expr *> Size = E->getArraySize()) {
2039     llvm::raw_string_ostream s(TypeS);
2040     s << '[';
2041     if (*Size)
2042       (*Size)->printPretty(s, Helper, Policy);
2043     s << ']';
2044   }
2045   E->getAllocatedType().print(OS, Policy, TypeS);
2046   if (E->isParenTypeId())
2047     OS << ")";
2048 
2049   CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
2050   if (InitStyle) {
2051     if (InitStyle == CXXNewExpr::CallInit)
2052       OS << "(";
2053     PrintExpr(E->getInitializer());
2054     if (InitStyle == CXXNewExpr::CallInit)
2055       OS << ")";
2056   }
2057 }
2058 
2059 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2060   if (E->isGlobalDelete())
2061     OS << "::";
2062   OS << "delete ";
2063   if (E->isArrayForm())
2064     OS << "[] ";
2065   PrintExpr(E->getArgument());
2066 }
2067 
2068 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
2069   PrintExpr(E->getBase());
2070   if (E->isArrow())
2071     OS << "->";
2072   else
2073     OS << '.';
2074   if (E->getQualifier())
2075     E->getQualifier()->print(OS, Policy);
2076   OS << "~";
2077 
2078   if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
2079     OS << II->getName();
2080   else
2081     E->getDestroyedType().print(OS, Policy);
2082 }
2083 
2084 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
2085   if (E->isListInitialization() && !E->isStdInitListInitialization())
2086     OS << "{";
2087 
2088   for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
2089     if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
2090       // Don't print any defaulted arguments
2091       break;
2092     }
2093 
2094     if (i) OS << ", ";
2095     PrintExpr(E->getArg(i));
2096   }
2097 
2098   if (E->isListInitialization() && !E->isStdInitListInitialization())
2099     OS << "}";
2100 }
2101 
2102 void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) {
2103   // Parens are printed by the surrounding context.
2104   OS << "<forwarded>";
2105 }
2106 
2107 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
2108   PrintExpr(E->getSubExpr());
2109 }
2110 
2111 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
2112   // Just forward to the subexpression.
2113   PrintExpr(E->getSubExpr());
2114 }
2115 
2116 void
2117 StmtPrinter::VisitCXXUnresolvedConstructExpr(
2118                                            CXXUnresolvedConstructExpr *Node) {
2119   Node->getTypeAsWritten().print(OS, Policy);
2120   OS << "(";
2121   for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
2122                                              ArgEnd = Node->arg_end();
2123        Arg != ArgEnd; ++Arg) {
2124     if (Arg != Node->arg_begin())
2125       OS << ", ";
2126     PrintExpr(*Arg);
2127   }
2128   OS << ")";
2129 }
2130 
2131 void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2132                                          CXXDependentScopeMemberExpr *Node) {
2133   if (!Node->isImplicitAccess()) {
2134     PrintExpr(Node->getBase());
2135     OS << (Node->isArrow() ? "->" : ".");
2136   }
2137   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2138     Qualifier->print(OS, Policy);
2139   if (Node->hasTemplateKeyword())
2140     OS << "template ";
2141   OS << Node->getMemberNameInfo();
2142   if (Node->hasExplicitTemplateArgs())
2143     printTemplateArgumentList(OS, Node->template_arguments(), Policy);
2144 }
2145 
2146 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
2147   if (!Node->isImplicitAccess()) {
2148     PrintExpr(Node->getBase());
2149     OS << (Node->isArrow() ? "->" : ".");
2150   }
2151   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2152     Qualifier->print(OS, Policy);
2153   if (Node->hasTemplateKeyword())
2154     OS << "template ";
2155   OS << Node->getMemberNameInfo();
2156   if (Node->hasExplicitTemplateArgs())
2157     printTemplateArgumentList(OS, Node->template_arguments(), Policy);
2158 }
2159 
2160 static const char *getTypeTraitName(TypeTrait TT) {
2161   switch (TT) {
2162 #define TYPE_TRAIT_1(Spelling, Name, Key) \
2163 case clang::UTT_##Name: return #Spelling;
2164 #define TYPE_TRAIT_2(Spelling, Name, Key) \
2165 case clang::BTT_##Name: return #Spelling;
2166 #define TYPE_TRAIT_N(Spelling, Name, Key) \
2167   case clang::TT_##Name: return #Spelling;
2168 #include "clang/Basic/TokenKinds.def"
2169   }
2170   llvm_unreachable("Type trait not covered by switch");
2171 }
2172 
2173 static const char *getTypeTraitName(ArrayTypeTrait ATT) {
2174   switch (ATT) {
2175   case ATT_ArrayRank:        return "__array_rank";
2176   case ATT_ArrayExtent:      return "__array_extent";
2177   }
2178   llvm_unreachable("Array type trait not covered by switch");
2179 }
2180 
2181 static const char *getExpressionTraitName(ExpressionTrait ET) {
2182   switch (ET) {
2183   case ET_IsLValueExpr:      return "__is_lvalue_expr";
2184   case ET_IsRValueExpr:      return "__is_rvalue_expr";
2185   }
2186   llvm_unreachable("Expression type trait not covered by switch");
2187 }
2188 
2189 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2190   OS << getTypeTraitName(E->getTrait()) << "(";
2191   for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2192     if (I > 0)
2193       OS << ", ";
2194     E->getArg(I)->getType().print(OS, Policy);
2195   }
2196   OS << ")";
2197 }
2198 
2199 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
2200   OS << getTypeTraitName(E->getTrait()) << '(';
2201   E->getQueriedType().print(OS, Policy);
2202   OS << ')';
2203 }
2204 
2205 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
2206   OS << getExpressionTraitName(E->getTrait()) << '(';
2207   PrintExpr(E->getQueriedExpression());
2208   OS << ')';
2209 }
2210 
2211 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2212   OS << "noexcept(";
2213   PrintExpr(E->getOperand());
2214   OS << ")";
2215 }
2216 
2217 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
2218   PrintExpr(E->getPattern());
2219   OS << "...";
2220 }
2221 
2222 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2223   OS << "sizeof...(" << *E->getPack() << ")";
2224 }
2225 
2226 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2227                                        SubstNonTypeTemplateParmPackExpr *Node) {
2228   OS << *Node->getParameterPack();
2229 }
2230 
2231 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2232                                        SubstNonTypeTemplateParmExpr *Node) {
2233   Visit(Node->getReplacement());
2234 }
2235 
2236 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2237   OS << *E->getParameterPack();
2238 }
2239 
2240 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2241   PrintExpr(Node->getSubExpr());
2242 }
2243 
2244 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2245   OS << "(";
2246   if (E->getLHS()) {
2247     PrintExpr(E->getLHS());
2248     OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2249   }
2250   OS << "...";
2251   if (E->getRHS()) {
2252     OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2253     PrintExpr(E->getRHS());
2254   }
2255   OS << ")";
2256 }
2257 
2258 void StmtPrinter::VisitConceptSpecializationExpr(ConceptSpecializationExpr *E) {
2259   NestedNameSpecifierLoc NNS = E->getNestedNameSpecifierLoc();
2260   if (NNS)
2261     NNS.getNestedNameSpecifier()->print(OS, Policy);
2262   if (E->getTemplateKWLoc().isValid())
2263     OS << "template ";
2264   OS << E->getFoundDecl()->getName();
2265   printTemplateArgumentList(OS, E->getTemplateArgsAsWritten()->arguments(),
2266                             Policy);
2267 }
2268 
2269 void StmtPrinter::VisitRequiresExpr(RequiresExpr *E) {
2270   OS << "requires ";
2271   auto LocalParameters = E->getLocalParameters();
2272   if (!LocalParameters.empty()) {
2273     OS << "(";
2274     for (ParmVarDecl *LocalParam : LocalParameters) {
2275       PrintRawDecl(LocalParam);
2276       if (LocalParam != LocalParameters.back())
2277         OS << ", ";
2278     }
2279 
2280     OS << ") ";
2281   }
2282   OS << "{ ";
2283   auto Requirements = E->getRequirements();
2284   for (concepts::Requirement *Req : Requirements) {
2285     if (auto *TypeReq = dyn_cast<concepts::TypeRequirement>(Req)) {
2286       if (TypeReq->isSubstitutionFailure())
2287         OS << "<<error-type>>";
2288       else
2289         TypeReq->getType()->getType().print(OS, Policy);
2290     } else if (auto *ExprReq = dyn_cast<concepts::ExprRequirement>(Req)) {
2291       if (ExprReq->isCompound())
2292         OS << "{ ";
2293       if (ExprReq->isExprSubstitutionFailure())
2294         OS << "<<error-expression>>";
2295       else
2296         PrintExpr(ExprReq->getExpr());
2297       if (ExprReq->isCompound()) {
2298         OS << " }";
2299         if (ExprReq->getNoexceptLoc().isValid())
2300           OS << " noexcept";
2301         const auto &RetReq = ExprReq->getReturnTypeRequirement();
2302         if (!RetReq.isEmpty()) {
2303           OS << " -> ";
2304           if (RetReq.isSubstitutionFailure())
2305             OS << "<<error-type>>";
2306           else if (RetReq.isTypeConstraint())
2307             RetReq.getTypeConstraint()->print(OS, Policy);
2308         }
2309       }
2310     } else {
2311       auto *NestedReq = cast<concepts::NestedRequirement>(Req);
2312       OS << "requires ";
2313       if (NestedReq->isSubstitutionFailure())
2314         OS << "<<error-expression>>";
2315       else
2316         PrintExpr(NestedReq->getConstraintExpr());
2317     }
2318     OS << "; ";
2319   }
2320   OS << "}";
2321 }
2322 
2323 // C++ Coroutines TS
2324 
2325 void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
2326   Visit(S->getBody());
2327 }
2328 
2329 void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
2330   OS << "co_return";
2331   if (S->getOperand()) {
2332     OS << " ";
2333     Visit(S->getOperand());
2334   }
2335   OS << ";";
2336 }
2337 
2338 void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
2339   OS << "co_await ";
2340   PrintExpr(S->getOperand());
2341 }
2342 
2343 void StmtPrinter::VisitDependentCoawaitExpr(DependentCoawaitExpr *S) {
2344   OS << "co_await ";
2345   PrintExpr(S->getOperand());
2346 }
2347 
2348 void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
2349   OS << "co_yield ";
2350   PrintExpr(S->getOperand());
2351 }
2352 
2353 // Obj-C
2354 
2355 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2356   OS << "@";
2357   VisitStringLiteral(Node->getString());
2358 }
2359 
2360 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
2361   OS << "@";
2362   Visit(E->getSubExpr());
2363 }
2364 
2365 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2366   OS << "@[ ";
2367   ObjCArrayLiteral::child_range Ch = E->children();
2368   for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2369     if (I != Ch.begin())
2370       OS << ", ";
2371     Visit(*I);
2372   }
2373   OS << " ]";
2374 }
2375 
2376 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2377   OS << "@{ ";
2378   for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2379     if (I > 0)
2380       OS << ", ";
2381 
2382     ObjCDictionaryElement Element = E->getKeyValueElement(I);
2383     Visit(Element.Key);
2384     OS << " : ";
2385     Visit(Element.Value);
2386     if (Element.isPackExpansion())
2387       OS << "...";
2388   }
2389   OS << " }";
2390 }
2391 
2392 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
2393   OS << "@encode(";
2394   Node->getEncodedType().print(OS, Policy);
2395   OS << ')';
2396 }
2397 
2398 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
2399   OS << "@selector(";
2400   Node->getSelector().print(OS);
2401   OS << ')';
2402 }
2403 
2404 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
2405   OS << "@protocol(" << *Node->getProtocol() << ')';
2406 }
2407 
2408 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2409   OS << "[";
2410   switch (Mess->getReceiverKind()) {
2411   case ObjCMessageExpr::Instance:
2412     PrintExpr(Mess->getInstanceReceiver());
2413     break;
2414 
2415   case ObjCMessageExpr::Class:
2416     Mess->getClassReceiver().print(OS, Policy);
2417     break;
2418 
2419   case ObjCMessageExpr::SuperInstance:
2420   case ObjCMessageExpr::SuperClass:
2421     OS << "Super";
2422     break;
2423   }
2424 
2425   OS << ' ';
2426   Selector selector = Mess->getSelector();
2427   if (selector.isUnarySelector()) {
2428     OS << selector.getNameForSlot(0);
2429   } else {
2430     for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
2431       if (i < selector.getNumArgs()) {
2432         if (i > 0) OS << ' ';
2433         if (selector.getIdentifierInfoForSlot(i))
2434           OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
2435         else
2436            OS << ":";
2437       }
2438       else OS << ", "; // Handle variadic methods.
2439 
2440       PrintExpr(Mess->getArg(i));
2441     }
2442   }
2443   OS << "]";
2444 }
2445 
2446 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2447   OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2448 }
2449 
2450 void
2451 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2452   PrintExpr(E->getSubExpr());
2453 }
2454 
2455 void
2456 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
2457   OS << '(' << E->getBridgeKindName();
2458   E->getType().print(OS, Policy);
2459   OS << ')';
2460   PrintExpr(E->getSubExpr());
2461 }
2462 
2463 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
2464   BlockDecl *BD = Node->getBlockDecl();
2465   OS << "^";
2466 
2467   const FunctionType *AFT = Node->getFunctionType();
2468 
2469   if (isa<FunctionNoProtoType>(AFT)) {
2470     OS << "()";
2471   } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
2472     OS << '(';
2473     for (BlockDecl::param_iterator AI = BD->param_begin(),
2474          E = BD->param_end(); AI != E; ++AI) {
2475       if (AI != BD->param_begin()) OS << ", ";
2476       std::string ParamStr = (*AI)->getNameAsString();
2477       (*AI)->getType().print(OS, Policy, ParamStr);
2478     }
2479 
2480     const auto *FT = cast<FunctionProtoType>(AFT);
2481     if (FT->isVariadic()) {
2482       if (!BD->param_empty()) OS << ", ";
2483       OS << "...";
2484     }
2485     OS << ')';
2486   }
2487   OS << "{ }";
2488 }
2489 
2490 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2491   PrintExpr(Node->getSourceExpr());
2492 }
2493 
2494 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2495   // TODO: Print something reasonable for a TypoExpr, if necessary.
2496   llvm_unreachable("Cannot print TypoExpr nodes");
2497 }
2498 
2499 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2500   OS << "__builtin_astype(";
2501   PrintExpr(Node->getSrcExpr());
2502   OS << ", ";
2503   Node->getType().print(OS, Policy);
2504   OS << ")";
2505 }
2506 
2507 //===----------------------------------------------------------------------===//
2508 // Stmt method implementations
2509 //===----------------------------------------------------------------------===//
2510 
2511 void Stmt::dumpPretty(const ASTContext &Context) const {
2512   printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
2513 }
2514 
2515 void Stmt::printPretty(raw_ostream &Out, PrinterHelper *Helper,
2516                        const PrintingPolicy &Policy, unsigned Indentation,
2517                        StringRef NL, const ASTContext *Context) const {
2518   StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context);
2519   P.Visit(const_cast<Stmt *>(this));
2520 }
2521 
2522 void Stmt::printJson(raw_ostream &Out, PrinterHelper *Helper,
2523                      const PrintingPolicy &Policy, bool AddQuotes) const {
2524   std::string Buf;
2525   llvm::raw_string_ostream TempOut(Buf);
2526 
2527   printPretty(TempOut, Helper, Policy);
2528 
2529   Out << JsonFormat(TempOut.str(), AddQuotes);
2530 }
2531 
2532 //===----------------------------------------------------------------------===//
2533 // PrinterHelper
2534 //===----------------------------------------------------------------------===//
2535 
2536 // Implement virtual destructor.
2537 PrinterHelper::~PrinterHelper() = default;
2538