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