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 //===----------------------------------------------------------------------===//
1009 //  Expr printing methods.
1010 //===----------------------------------------------------------------------===//
1011 
1012 void StmtPrinter::VisitSourceLocExpr(SourceLocExpr *Node) {
1013   OS << Node->getBuiltinStr() << "()";
1014 }
1015 
1016 void StmtPrinter::VisitConstantExpr(ConstantExpr *Node) {
1017   PrintExpr(Node->getSubExpr());
1018 }
1019 
1020 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
1021   if (const auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) {
1022     OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy);
1023     return;
1024   }
1025   if (const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(Node->getDecl())) {
1026     TPOD->printAsExpr(OS);
1027     return;
1028   }
1029   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1030     Qualifier->print(OS, Policy);
1031   if (Node->hasTemplateKeyword())
1032     OS << "template ";
1033   if (Policy.CleanUglifiedParameters &&
1034       isa<ParmVarDecl, NonTypeTemplateParmDecl>(Node->getDecl()) &&
1035       Node->getDecl()->getIdentifier())
1036     OS << Node->getDecl()->getIdentifier()->deuglifiedName();
1037   else
1038     Node->getNameInfo().printName(OS, Policy);
1039   if (Node->hasExplicitTemplateArgs()) {
1040     const TemplateParameterList *TPL = nullptr;
1041     if (!Node->hadMultipleCandidates())
1042       if (auto *TD = dyn_cast<TemplateDecl>(Node->getDecl()))
1043         TPL = TD->getTemplateParameters();
1044     printTemplateArgumentList(OS, Node->template_arguments(), Policy, TPL);
1045   }
1046 }
1047 
1048 void StmtPrinter::VisitDependentScopeDeclRefExpr(
1049                                            DependentScopeDeclRefExpr *Node) {
1050   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1051     Qualifier->print(OS, Policy);
1052   if (Node->hasTemplateKeyword())
1053     OS << "template ";
1054   OS << Node->getNameInfo();
1055   if (Node->hasExplicitTemplateArgs())
1056     printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1057 }
1058 
1059 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
1060   if (Node->getQualifier())
1061     Node->getQualifier()->print(OS, Policy);
1062   if (Node->hasTemplateKeyword())
1063     OS << "template ";
1064   OS << Node->getNameInfo();
1065   if (Node->hasExplicitTemplateArgs())
1066     printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1067 }
1068 
1069 static bool isImplicitSelf(const Expr *E) {
1070   if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
1071     if (const auto *PD = dyn_cast<ImplicitParamDecl>(DRE->getDecl())) {
1072       if (PD->getParameterKind() == ImplicitParamDecl::ObjCSelf &&
1073           DRE->getBeginLoc().isInvalid())
1074         return true;
1075     }
1076   }
1077   return false;
1078 }
1079 
1080 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
1081   if (Node->getBase()) {
1082     if (!Policy.SuppressImplicitBase ||
1083         !isImplicitSelf(Node->getBase()->IgnoreImpCasts())) {
1084       PrintExpr(Node->getBase());
1085       OS << (Node->isArrow() ? "->" : ".");
1086     }
1087   }
1088   OS << *Node->getDecl();
1089 }
1090 
1091 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
1092   if (Node->isSuperReceiver())
1093     OS << "super.";
1094   else if (Node->isObjectReceiver() && Node->getBase()) {
1095     PrintExpr(Node->getBase());
1096     OS << ".";
1097   } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1098     OS << Node->getClassReceiver()->getName() << ".";
1099   }
1100 
1101   if (Node->isImplicitProperty()) {
1102     if (const auto *Getter = Node->getImplicitPropertyGetter())
1103       Getter->getSelector().print(OS);
1104     else
1105       OS << SelectorTable::getPropertyNameFromSetterSelector(
1106           Node->getImplicitPropertySetter()->getSelector());
1107   } else
1108     OS << Node->getExplicitProperty()->getName();
1109 }
1110 
1111 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1112   PrintExpr(Node->getBaseExpr());
1113   OS << "[";
1114   PrintExpr(Node->getKeyExpr());
1115   OS << "]";
1116 }
1117 
1118 void StmtPrinter::VisitSYCLUniqueStableNameExpr(
1119     SYCLUniqueStableNameExpr *Node) {
1120   OS << "__builtin_sycl_unique_stable_name(";
1121   Node->getTypeSourceInfo()->getType().print(OS, Policy);
1122   OS << ")";
1123 }
1124 
1125 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
1126   OS << PredefinedExpr::getIdentKindName(Node->getIdentKind());
1127 }
1128 
1129 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
1130   CharacterLiteral::print(Node->getValue(), Node->getKind(), OS);
1131 }
1132 
1133 /// Prints the given expression using the original source text. Returns true on
1134 /// success, false otherwise.
1135 static bool printExprAsWritten(raw_ostream &OS, Expr *E,
1136                                const ASTContext *Context) {
1137   if (!Context)
1138     return false;
1139   bool Invalid = false;
1140   StringRef Source = Lexer::getSourceText(
1141       CharSourceRange::getTokenRange(E->getSourceRange()),
1142       Context->getSourceManager(), Context->getLangOpts(), &Invalid);
1143   if (!Invalid) {
1144     OS << Source;
1145     return true;
1146   }
1147   return false;
1148 }
1149 
1150 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
1151   if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1152     return;
1153   bool isSigned = Node->getType()->isSignedIntegerType();
1154   OS << toString(Node->getValue(), 10, isSigned);
1155 
1156   // Emit suffixes.  Integer literals are always a builtin integer type.
1157   switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1158   default: llvm_unreachable("Unexpected type for integer literal!");
1159   case BuiltinType::Char_S:
1160   case BuiltinType::Char_U:    OS << "i8"; break;
1161   case BuiltinType::UChar:     OS << "Ui8"; break;
1162   case BuiltinType::Short:     OS << "i16"; break;
1163   case BuiltinType::UShort:    OS << "Ui16"; break;
1164   case BuiltinType::Int:       break; // no suffix.
1165   case BuiltinType::UInt:      OS << 'U'; break;
1166   case BuiltinType::Long:      OS << 'L'; break;
1167   case BuiltinType::ULong:     OS << "UL"; break;
1168   case BuiltinType::LongLong:  OS << "LL"; break;
1169   case BuiltinType::ULongLong: OS << "ULL"; break;
1170   case BuiltinType::Int128:
1171     break; // no suffix.
1172   case BuiltinType::UInt128:
1173     break; // no suffix.
1174   }
1175 }
1176 
1177 void StmtPrinter::VisitFixedPointLiteral(FixedPointLiteral *Node) {
1178   if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1179     return;
1180   OS << Node->getValueAsString(/*Radix=*/10);
1181 
1182   switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1183     default: llvm_unreachable("Unexpected type for fixed point literal!");
1184     case BuiltinType::ShortFract:   OS << "hr"; break;
1185     case BuiltinType::ShortAccum:   OS << "hk"; break;
1186     case BuiltinType::UShortFract:  OS << "uhr"; break;
1187     case BuiltinType::UShortAccum:  OS << "uhk"; break;
1188     case BuiltinType::Fract:        OS << "r"; break;
1189     case BuiltinType::Accum:        OS << "k"; break;
1190     case BuiltinType::UFract:       OS << "ur"; break;
1191     case BuiltinType::UAccum:       OS << "uk"; break;
1192     case BuiltinType::LongFract:    OS << "lr"; break;
1193     case BuiltinType::LongAccum:    OS << "lk"; break;
1194     case BuiltinType::ULongFract:   OS << "ulr"; break;
1195     case BuiltinType::ULongAccum:   OS << "ulk"; break;
1196   }
1197 }
1198 
1199 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1200                                  bool PrintSuffix) {
1201   SmallString<16> Str;
1202   Node->getValue().toString(Str);
1203   OS << Str;
1204   if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1205     OS << '.'; // Trailing dot in order to separate from ints.
1206 
1207   if (!PrintSuffix)
1208     return;
1209 
1210   // Emit suffixes.  Float literals are always a builtin float type.
1211   switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1212   default: llvm_unreachable("Unexpected type for float literal!");
1213   case BuiltinType::Half:       break; // FIXME: suffix?
1214   case BuiltinType::Ibm128:     break; // FIXME: No suffix for ibm128 literal
1215   case BuiltinType::Double:     break; // no suffix.
1216   case BuiltinType::Float16:    OS << "F16"; break;
1217   case BuiltinType::Float:      OS << 'F'; break;
1218   case BuiltinType::LongDouble: OS << 'L'; break;
1219   case BuiltinType::Float128:   OS << 'Q'; break;
1220   }
1221 }
1222 
1223 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1224   if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1225     return;
1226   PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
1227 }
1228 
1229 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1230   PrintExpr(Node->getSubExpr());
1231   OS << "i";
1232 }
1233 
1234 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
1235   Str->outputString(OS);
1236 }
1237 
1238 void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1239   OS << "(";
1240   PrintExpr(Node->getSubExpr());
1241   OS << ")";
1242 }
1243 
1244 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
1245   if (!Node->isPostfix()) {
1246     OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1247 
1248     // Print a space if this is an "identifier operator" like __real, or if
1249     // it might be concatenated incorrectly like '+'.
1250     switch (Node->getOpcode()) {
1251     default: break;
1252     case UO_Real:
1253     case UO_Imag:
1254     case UO_Extension:
1255       OS << ' ';
1256       break;
1257     case UO_Plus:
1258     case UO_Minus:
1259       if (isa<UnaryOperator>(Node->getSubExpr()))
1260         OS << ' ';
1261       break;
1262     }
1263   }
1264   PrintExpr(Node->getSubExpr());
1265 
1266   if (Node->isPostfix())
1267     OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1268 }
1269 
1270 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1271   OS << "__builtin_offsetof(";
1272   Node->getTypeSourceInfo()->getType().print(OS, Policy);
1273   OS << ", ";
1274   bool PrintedSomething = false;
1275   for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1276     OffsetOfNode ON = Node->getComponent(i);
1277     if (ON.getKind() == OffsetOfNode::Array) {
1278       // Array node
1279       OS << "[";
1280       PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1281       OS << "]";
1282       PrintedSomething = true;
1283       continue;
1284     }
1285 
1286     // Skip implicit base indirections.
1287     if (ON.getKind() == OffsetOfNode::Base)
1288       continue;
1289 
1290     // Field or identifier node.
1291     IdentifierInfo *Id = ON.getFieldName();
1292     if (!Id)
1293       continue;
1294 
1295     if (PrintedSomething)
1296       OS << ".";
1297     else
1298       PrintedSomething = true;
1299     OS << Id->getName();
1300   }
1301   OS << ")";
1302 }
1303 
1304 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(
1305     UnaryExprOrTypeTraitExpr *Node) {
1306   const char *Spelling = getTraitSpelling(Node->getKind());
1307   if (Node->getKind() == UETT_AlignOf) {
1308     if (Policy.Alignof)
1309       Spelling = "alignof";
1310     else if (Policy.UnderscoreAlignof)
1311       Spelling = "_Alignof";
1312     else
1313       Spelling = "__alignof";
1314   }
1315 
1316   OS << Spelling;
1317 
1318   if (Node->isArgumentType()) {
1319     OS << '(';
1320     Node->getArgumentType().print(OS, Policy);
1321     OS << ')';
1322   } else {
1323     OS << " ";
1324     PrintExpr(Node->getArgumentExpr());
1325   }
1326 }
1327 
1328 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1329   OS << "_Generic(";
1330   PrintExpr(Node->getControllingExpr());
1331   for (const GenericSelectionExpr::Association Assoc : Node->associations()) {
1332     OS << ", ";
1333     QualType T = Assoc.getType();
1334     if (T.isNull())
1335       OS << "default";
1336     else
1337       T.print(OS, Policy);
1338     OS << ": ";
1339     PrintExpr(Assoc.getAssociationExpr());
1340   }
1341   OS << ")";
1342 }
1343 
1344 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
1345   PrintExpr(Node->getLHS());
1346   OS << "[";
1347   PrintExpr(Node->getRHS());
1348   OS << "]";
1349 }
1350 
1351 void StmtPrinter::VisitMatrixSubscriptExpr(MatrixSubscriptExpr *Node) {
1352   PrintExpr(Node->getBase());
1353   OS << "[";
1354   PrintExpr(Node->getRowIdx());
1355   OS << "]";
1356   OS << "[";
1357   PrintExpr(Node->getColumnIdx());
1358   OS << "]";
1359 }
1360 
1361 void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) {
1362   PrintExpr(Node->getBase());
1363   OS << "[";
1364   if (Node->getLowerBound())
1365     PrintExpr(Node->getLowerBound());
1366   if (Node->getColonLocFirst().isValid()) {
1367     OS << ":";
1368     if (Node->getLength())
1369       PrintExpr(Node->getLength());
1370   }
1371   if (Node->getColonLocSecond().isValid()) {
1372     OS << ":";
1373     if (Node->getStride())
1374       PrintExpr(Node->getStride());
1375   }
1376   OS << "]";
1377 }
1378 
1379 void StmtPrinter::VisitOMPArrayShapingExpr(OMPArrayShapingExpr *Node) {
1380   OS << "(";
1381   for (Expr *E : Node->getDimensions()) {
1382     OS << "[";
1383     PrintExpr(E);
1384     OS << "]";
1385   }
1386   OS << ")";
1387   PrintExpr(Node->getBase());
1388 }
1389 
1390 void StmtPrinter::VisitOMPIteratorExpr(OMPIteratorExpr *Node) {
1391   OS << "iterator(";
1392   for (unsigned I = 0, E = Node->numOfIterators(); I < E; ++I) {
1393     auto *VD = cast<ValueDecl>(Node->getIteratorDecl(I));
1394     VD->getType().print(OS, Policy);
1395     const OMPIteratorExpr::IteratorRange Range = Node->getIteratorRange(I);
1396     OS << " " << VD->getName() << " = ";
1397     PrintExpr(Range.Begin);
1398     OS << ":";
1399     PrintExpr(Range.End);
1400     if (Range.Step) {
1401       OS << ":";
1402       PrintExpr(Range.Step);
1403     }
1404     if (I < E - 1)
1405       OS << ", ";
1406   }
1407   OS << ")";
1408 }
1409 
1410 void StmtPrinter::PrintCallArgs(CallExpr *Call) {
1411   for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
1412     if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1413       // Don't print any defaulted arguments
1414       break;
1415     }
1416 
1417     if (i) OS << ", ";
1418     PrintExpr(Call->getArg(i));
1419   }
1420 }
1421 
1422 void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1423   PrintExpr(Call->getCallee());
1424   OS << "(";
1425   PrintCallArgs(Call);
1426   OS << ")";
1427 }
1428 
1429 static bool isImplicitThis(const Expr *E) {
1430   if (const auto *TE = dyn_cast<CXXThisExpr>(E))
1431     return TE->isImplicit();
1432   return false;
1433 }
1434 
1435 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1436   if (!Policy.SuppressImplicitBase || !isImplicitThis(Node->getBase())) {
1437     PrintExpr(Node->getBase());
1438 
1439     auto *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1440     FieldDecl *ParentDecl =
1441         ParentMember ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl())
1442                      : nullptr;
1443 
1444     if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1445       OS << (Node->isArrow() ? "->" : ".");
1446   }
1447 
1448   if (auto *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1449     if (FD->isAnonymousStructOrUnion())
1450       return;
1451 
1452   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1453     Qualifier->print(OS, Policy);
1454   if (Node->hasTemplateKeyword())
1455     OS << "template ";
1456   OS << Node->getMemberNameInfo();
1457   const TemplateParameterList *TPL = nullptr;
1458   if (auto *FD = dyn_cast<FunctionDecl>(Node->getMemberDecl())) {
1459     if (!Node->hadMultipleCandidates())
1460       if (auto *FTD = FD->getPrimaryTemplate())
1461         TPL = FTD->getTemplateParameters();
1462   } else if (auto *VTSD =
1463                  dyn_cast<VarTemplateSpecializationDecl>(Node->getMemberDecl()))
1464     TPL = VTSD->getSpecializedTemplate()->getTemplateParameters();
1465   if (Node->hasExplicitTemplateArgs())
1466     printTemplateArgumentList(OS, Node->template_arguments(), Policy, TPL);
1467 }
1468 
1469 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1470   PrintExpr(Node->getBase());
1471   OS << (Node->isArrow() ? "->isa" : ".isa");
1472 }
1473 
1474 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1475   PrintExpr(Node->getBase());
1476   OS << ".";
1477   OS << Node->getAccessor().getName();
1478 }
1479 
1480 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1481   OS << '(';
1482   Node->getTypeAsWritten().print(OS, Policy);
1483   OS << ')';
1484   PrintExpr(Node->getSubExpr());
1485 }
1486 
1487 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1488   OS << '(';
1489   Node->getType().print(OS, Policy);
1490   OS << ')';
1491   PrintExpr(Node->getInitializer());
1492 }
1493 
1494 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1495   // No need to print anything, simply forward to the subexpression.
1496   PrintExpr(Node->getSubExpr());
1497 }
1498 
1499 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1500   PrintExpr(Node->getLHS());
1501   OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1502   PrintExpr(Node->getRHS());
1503 }
1504 
1505 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1506   PrintExpr(Node->getLHS());
1507   OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1508   PrintExpr(Node->getRHS());
1509 }
1510 
1511 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1512   PrintExpr(Node->getCond());
1513   OS << " ? ";
1514   PrintExpr(Node->getLHS());
1515   OS << " : ";
1516   PrintExpr(Node->getRHS());
1517 }
1518 
1519 // GNU extensions.
1520 
1521 void
1522 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1523   PrintExpr(Node->getCommon());
1524   OS << " ?: ";
1525   PrintExpr(Node->getFalseExpr());
1526 }
1527 
1528 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1529   OS << "&&" << Node->getLabel()->getName();
1530 }
1531 
1532 void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1533   OS << "(";
1534   PrintRawCompoundStmt(E->getSubStmt());
1535   OS << ")";
1536 }
1537 
1538 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1539   OS << "__builtin_choose_expr(";
1540   PrintExpr(Node->getCond());
1541   OS << ", ";
1542   PrintExpr(Node->getLHS());
1543   OS << ", ";
1544   PrintExpr(Node->getRHS());
1545   OS << ")";
1546 }
1547 
1548 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1549   OS << "__null";
1550 }
1551 
1552 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1553   OS << "__builtin_shufflevector(";
1554   for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1555     if (i) OS << ", ";
1556     PrintExpr(Node->getExpr(i));
1557   }
1558   OS << ")";
1559 }
1560 
1561 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1562   OS << "__builtin_convertvector(";
1563   PrintExpr(Node->getSrcExpr());
1564   OS << ", ";
1565   Node->getType().print(OS, Policy);
1566   OS << ")";
1567 }
1568 
1569 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1570   if (Node->getSyntacticForm()) {
1571     Visit(Node->getSyntacticForm());
1572     return;
1573   }
1574 
1575   OS << "{";
1576   for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1577     if (i) OS << ", ";
1578     if (Node->getInit(i))
1579       PrintExpr(Node->getInit(i));
1580     else
1581       OS << "{}";
1582   }
1583   OS << "}";
1584 }
1585 
1586 void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) {
1587   // There's no way to express this expression in any of our supported
1588   // languages, so just emit something terse and (hopefully) clear.
1589   OS << "{";
1590   PrintExpr(Node->getSubExpr());
1591   OS << "}";
1592 }
1593 
1594 void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) {
1595   OS << "*";
1596 }
1597 
1598 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1599   OS << "(";
1600   for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1601     if (i) OS << ", ";
1602     PrintExpr(Node->getExpr(i));
1603   }
1604   OS << ")";
1605 }
1606 
1607 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1608   bool NeedsEquals = true;
1609   for (const DesignatedInitExpr::Designator &D : Node->designators()) {
1610     if (D.isFieldDesignator()) {
1611       if (D.getDotLoc().isInvalid()) {
1612         if (IdentifierInfo *II = D.getFieldName()) {
1613           OS << II->getName() << ":";
1614           NeedsEquals = false;
1615         }
1616       } else {
1617         OS << "." << D.getFieldName()->getName();
1618       }
1619     } else {
1620       OS << "[";
1621       if (D.isArrayDesignator()) {
1622         PrintExpr(Node->getArrayIndex(D));
1623       } else {
1624         PrintExpr(Node->getArrayRangeStart(D));
1625         OS << " ... ";
1626         PrintExpr(Node->getArrayRangeEnd(D));
1627       }
1628       OS << "]";
1629     }
1630   }
1631 
1632   if (NeedsEquals)
1633     OS << " = ";
1634   else
1635     OS << " ";
1636   PrintExpr(Node->getInit());
1637 }
1638 
1639 void StmtPrinter::VisitDesignatedInitUpdateExpr(
1640     DesignatedInitUpdateExpr *Node) {
1641   OS << "{";
1642   OS << "/*base*/";
1643   PrintExpr(Node->getBase());
1644   OS << ", ";
1645 
1646   OS << "/*updater*/";
1647   PrintExpr(Node->getUpdater());
1648   OS << "}";
1649 }
1650 
1651 void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1652   OS << "/*no init*/";
1653 }
1654 
1655 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1656   if (Node->getType()->getAsCXXRecordDecl()) {
1657     OS << "/*implicit*/";
1658     Node->getType().print(OS, Policy);
1659     OS << "()";
1660   } else {
1661     OS << "/*implicit*/(";
1662     Node->getType().print(OS, Policy);
1663     OS << ')';
1664     if (Node->getType()->isRecordType())
1665       OS << "{}";
1666     else
1667       OS << 0;
1668   }
1669 }
1670 
1671 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1672   OS << "__builtin_va_arg(";
1673   PrintExpr(Node->getSubExpr());
1674   OS << ", ";
1675   Node->getType().print(OS, Policy);
1676   OS << ")";
1677 }
1678 
1679 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1680   PrintExpr(Node->getSyntacticForm());
1681 }
1682 
1683 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1684   const char *Name = nullptr;
1685   switch (Node->getOp()) {
1686 #define BUILTIN(ID, TYPE, ATTRS)
1687 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1688   case AtomicExpr::AO ## ID: \
1689     Name = #ID "("; \
1690     break;
1691 #include "clang/Basic/Builtins.def"
1692   }
1693   OS << Name;
1694 
1695   // AtomicExpr stores its subexpressions in a permuted order.
1696   PrintExpr(Node->getPtr());
1697   if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1698       Node->getOp() != AtomicExpr::AO__atomic_load_n &&
1699       Node->getOp() != AtomicExpr::AO__opencl_atomic_load &&
1700       Node->getOp() != AtomicExpr::AO__hip_atomic_load) {
1701     OS << ", ";
1702     PrintExpr(Node->getVal1());
1703   }
1704   if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1705       Node->isCmpXChg()) {
1706     OS << ", ";
1707     PrintExpr(Node->getVal2());
1708   }
1709   if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1710       Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1711     OS << ", ";
1712     PrintExpr(Node->getWeak());
1713   }
1714   if (Node->getOp() != AtomicExpr::AO__c11_atomic_init &&
1715       Node->getOp() != AtomicExpr::AO__opencl_atomic_init) {
1716     OS << ", ";
1717     PrintExpr(Node->getOrder());
1718   }
1719   if (Node->isCmpXChg()) {
1720     OS << ", ";
1721     PrintExpr(Node->getOrderFail());
1722   }
1723   OS << ")";
1724 }
1725 
1726 // C++
1727 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1728   OverloadedOperatorKind Kind = Node->getOperator();
1729   if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1730     if (Node->getNumArgs() == 1) {
1731       OS << getOperatorSpelling(Kind) << ' ';
1732       PrintExpr(Node->getArg(0));
1733     } else {
1734       PrintExpr(Node->getArg(0));
1735       OS << ' ' << getOperatorSpelling(Kind);
1736     }
1737   } else if (Kind == OO_Arrow) {
1738     PrintExpr(Node->getArg(0));
1739   } else if (Kind == OO_Call || Kind == OO_Subscript) {
1740     PrintExpr(Node->getArg(0));
1741     OS << (Kind == OO_Call ? '(' : '[');
1742     for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1743       if (ArgIdx > 1)
1744         OS << ", ";
1745       if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1746         PrintExpr(Node->getArg(ArgIdx));
1747     }
1748     OS << (Kind == OO_Call ? ')' : ']');
1749   } else if (Node->getNumArgs() == 1) {
1750     OS << getOperatorSpelling(Kind) << ' ';
1751     PrintExpr(Node->getArg(0));
1752   } else if (Node->getNumArgs() == 2) {
1753     PrintExpr(Node->getArg(0));
1754     OS << ' ' << getOperatorSpelling(Kind) << ' ';
1755     PrintExpr(Node->getArg(1));
1756   } else {
1757     llvm_unreachable("unknown overloaded operator");
1758   }
1759 }
1760 
1761 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1762   // If we have a conversion operator call only print the argument.
1763   CXXMethodDecl *MD = Node->getMethodDecl();
1764   if (MD && isa<CXXConversionDecl>(MD)) {
1765     PrintExpr(Node->getImplicitObjectArgument());
1766     return;
1767   }
1768   VisitCallExpr(cast<CallExpr>(Node));
1769 }
1770 
1771 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1772   PrintExpr(Node->getCallee());
1773   OS << "<<<";
1774   PrintCallArgs(Node->getConfig());
1775   OS << ">>>(";
1776   PrintCallArgs(Node);
1777   OS << ")";
1778 }
1779 
1780 void StmtPrinter::VisitCXXRewrittenBinaryOperator(
1781     CXXRewrittenBinaryOperator *Node) {
1782   CXXRewrittenBinaryOperator::DecomposedForm Decomposed =
1783       Node->getDecomposedForm();
1784   PrintExpr(const_cast<Expr*>(Decomposed.LHS));
1785   OS << ' ' << BinaryOperator::getOpcodeStr(Decomposed.Opcode) << ' ';
1786   PrintExpr(const_cast<Expr*>(Decomposed.RHS));
1787 }
1788 
1789 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1790   OS << Node->getCastName() << '<';
1791   Node->getTypeAsWritten().print(OS, Policy);
1792   OS << ">(";
1793   PrintExpr(Node->getSubExpr());
1794   OS << ")";
1795 }
1796 
1797 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1798   VisitCXXNamedCastExpr(Node);
1799 }
1800 
1801 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1802   VisitCXXNamedCastExpr(Node);
1803 }
1804 
1805 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1806   VisitCXXNamedCastExpr(Node);
1807 }
1808 
1809 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1810   VisitCXXNamedCastExpr(Node);
1811 }
1812 
1813 void StmtPrinter::VisitBuiltinBitCastExpr(BuiltinBitCastExpr *Node) {
1814   OS << "__builtin_bit_cast(";
1815   Node->getTypeInfoAsWritten()->getType().print(OS, Policy);
1816   OS << ", ";
1817   PrintExpr(Node->getSubExpr());
1818   OS << ")";
1819 }
1820 
1821 void StmtPrinter::VisitCXXAddrspaceCastExpr(CXXAddrspaceCastExpr *Node) {
1822   VisitCXXNamedCastExpr(Node);
1823 }
1824 
1825 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1826   OS << "typeid(";
1827   if (Node->isTypeOperand()) {
1828     Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1829   } else {
1830     PrintExpr(Node->getExprOperand());
1831   }
1832   OS << ")";
1833 }
1834 
1835 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1836   OS << "__uuidof(";
1837   if (Node->isTypeOperand()) {
1838     Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1839   } else {
1840     PrintExpr(Node->getExprOperand());
1841   }
1842   OS << ")";
1843 }
1844 
1845 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1846   PrintExpr(Node->getBaseExpr());
1847   if (Node->isArrow())
1848     OS << "->";
1849   else
1850     OS << ".";
1851   if (NestedNameSpecifier *Qualifier =
1852       Node->getQualifierLoc().getNestedNameSpecifier())
1853     Qualifier->print(OS, Policy);
1854   OS << Node->getPropertyDecl()->getDeclName();
1855 }
1856 
1857 void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
1858   PrintExpr(Node->getBase());
1859   OS << "[";
1860   PrintExpr(Node->getIdx());
1861   OS << "]";
1862 }
1863 
1864 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1865   switch (Node->getLiteralOperatorKind()) {
1866   case UserDefinedLiteral::LOK_Raw:
1867     OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
1868     break;
1869   case UserDefinedLiteral::LOK_Template: {
1870     const auto *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1871     const TemplateArgumentList *Args =
1872       cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1873     assert(Args);
1874 
1875     if (Args->size() != 1) {
1876       const TemplateParameterList *TPL = nullptr;
1877       if (!DRE->hadMultipleCandidates())
1878         if (const auto *TD = dyn_cast<TemplateDecl>(DRE->getDecl()))
1879           TPL = TD->getTemplateParameters();
1880       OS << "operator\"\"" << Node->getUDSuffix()->getName();
1881       printTemplateArgumentList(OS, Args->asArray(), Policy, TPL);
1882       OS << "()";
1883       return;
1884     }
1885 
1886     const TemplateArgument &Pack = Args->get(0);
1887     for (const auto &P : Pack.pack_elements()) {
1888       char C = (char)P.getAsIntegral().getZExtValue();
1889       OS << C;
1890     }
1891     break;
1892   }
1893   case UserDefinedLiteral::LOK_Integer: {
1894     // Print integer literal without suffix.
1895     const auto *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1896     OS << toString(Int->getValue(), 10, /*isSigned*/false);
1897     break;
1898   }
1899   case UserDefinedLiteral::LOK_Floating: {
1900     // Print floating literal without suffix.
1901     auto *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1902     PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1903     break;
1904   }
1905   case UserDefinedLiteral::LOK_String:
1906   case UserDefinedLiteral::LOK_Character:
1907     PrintExpr(Node->getCookedLiteral());
1908     break;
1909   }
1910   OS << Node->getUDSuffix()->getName();
1911 }
1912 
1913 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1914   OS << (Node->getValue() ? "true" : "false");
1915 }
1916 
1917 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1918   OS << "nullptr";
1919 }
1920 
1921 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1922   OS << "this";
1923 }
1924 
1925 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1926   if (!Node->getSubExpr())
1927     OS << "throw";
1928   else {
1929     OS << "throw ";
1930     PrintExpr(Node->getSubExpr());
1931   }
1932 }
1933 
1934 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1935   // Nothing to print: we picked up the default argument.
1936 }
1937 
1938 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1939   // Nothing to print: we picked up the default initializer.
1940 }
1941 
1942 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1943   Node->getType().print(OS, Policy);
1944   // If there are no parens, this is list-initialization, and the braces are
1945   // part of the syntax of the inner construct.
1946   if (Node->getLParenLoc().isValid())
1947     OS << "(";
1948   PrintExpr(Node->getSubExpr());
1949   if (Node->getLParenLoc().isValid())
1950     OS << ")";
1951 }
1952 
1953 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1954   PrintExpr(Node->getSubExpr());
1955 }
1956 
1957 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1958   Node->getType().print(OS, Policy);
1959   if (Node->isStdInitListInitialization())
1960     /* Nothing to do; braces are part of creating the std::initializer_list. */;
1961   else if (Node->isListInitialization())
1962     OS << "{";
1963   else
1964     OS << "(";
1965   for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1966                                          ArgEnd = Node->arg_end();
1967        Arg != ArgEnd; ++Arg) {
1968     if ((*Arg)->isDefaultArgument())
1969       break;
1970     if (Arg != Node->arg_begin())
1971       OS << ", ";
1972     PrintExpr(*Arg);
1973   }
1974   if (Node->isStdInitListInitialization())
1975     /* See above. */;
1976   else if (Node->isListInitialization())
1977     OS << "}";
1978   else
1979     OS << ")";
1980 }
1981 
1982 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1983   OS << '[';
1984   bool NeedComma = false;
1985   switch (Node->getCaptureDefault()) {
1986   case LCD_None:
1987     break;
1988 
1989   case LCD_ByCopy:
1990     OS << '=';
1991     NeedComma = true;
1992     break;
1993 
1994   case LCD_ByRef:
1995     OS << '&';
1996     NeedComma = true;
1997     break;
1998   }
1999   for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
2000                                  CEnd = Node->explicit_capture_end();
2001        C != CEnd;
2002        ++C) {
2003     if (C->capturesVLAType())
2004       continue;
2005 
2006     if (NeedComma)
2007       OS << ", ";
2008     NeedComma = true;
2009 
2010     switch (C->getCaptureKind()) {
2011     case LCK_This:
2012       OS << "this";
2013       break;
2014 
2015     case LCK_StarThis:
2016       OS << "*this";
2017       break;
2018 
2019     case LCK_ByRef:
2020       if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
2021         OS << '&';
2022       OS << C->getCapturedVar()->getName();
2023       break;
2024 
2025     case LCK_ByCopy:
2026       OS << C->getCapturedVar()->getName();
2027       break;
2028 
2029     case LCK_VLAType:
2030       llvm_unreachable("VLA type in explicit captures.");
2031     }
2032 
2033     if (C->isPackExpansion())
2034       OS << "...";
2035 
2036     if (Node->isInitCapture(C)) {
2037       VarDecl *D = C->getCapturedVar();
2038 
2039       llvm::StringRef Pre;
2040       llvm::StringRef Post;
2041       if (D->getInitStyle() == VarDecl::CallInit &&
2042           !isa<ParenListExpr>(D->getInit())) {
2043         Pre = "(";
2044         Post = ")";
2045       } else if (D->getInitStyle() == VarDecl::CInit) {
2046         Pre = " = ";
2047       }
2048 
2049       OS << Pre;
2050       PrintExpr(D->getInit());
2051       OS << Post;
2052     }
2053   }
2054   OS << ']';
2055 
2056   if (!Node->getExplicitTemplateParameters().empty()) {
2057     Node->getTemplateParameterList()->print(
2058         OS, Node->getLambdaClass()->getASTContext(),
2059         /*OmitTemplateKW*/true);
2060   }
2061 
2062   if (Node->hasExplicitParameters()) {
2063     OS << '(';
2064     CXXMethodDecl *Method = Node->getCallOperator();
2065     NeedComma = false;
2066     for (const auto *P : Method->parameters()) {
2067       if (NeedComma) {
2068         OS << ", ";
2069       } else {
2070         NeedComma = true;
2071       }
2072       std::string ParamStr =
2073           (Policy.CleanUglifiedParameters && P->getIdentifier())
2074               ? P->getIdentifier()->deuglifiedName().str()
2075               : P->getNameAsString();
2076       P->getOriginalType().print(OS, Policy, ParamStr);
2077     }
2078     if (Method->isVariadic()) {
2079       if (NeedComma)
2080         OS << ", ";
2081       OS << "...";
2082     }
2083     OS << ')';
2084 
2085     if (Node->isMutable())
2086       OS << " mutable";
2087 
2088     auto *Proto = Method->getType()->castAs<FunctionProtoType>();
2089     Proto->printExceptionSpecification(OS, Policy);
2090 
2091     // FIXME: Attributes
2092 
2093     // Print the trailing return type if it was specified in the source.
2094     if (Node->hasExplicitResultType()) {
2095       OS << " -> ";
2096       Proto->getReturnType().print(OS, Policy);
2097     }
2098   }
2099 
2100   // Print the body.
2101   OS << ' ';
2102   if (Policy.TerseOutput)
2103     OS << "{}";
2104   else
2105     PrintRawCompoundStmt(Node->getCompoundStmtBody());
2106 }
2107 
2108 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
2109   if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
2110     TSInfo->getType().print(OS, Policy);
2111   else
2112     Node->getType().print(OS, Policy);
2113   OS << "()";
2114 }
2115 
2116 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
2117   if (E->isGlobalNew())
2118     OS << "::";
2119   OS << "new ";
2120   unsigned NumPlace = E->getNumPlacementArgs();
2121   if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
2122     OS << "(";
2123     PrintExpr(E->getPlacementArg(0));
2124     for (unsigned i = 1; i < NumPlace; ++i) {
2125       if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
2126         break;
2127       OS << ", ";
2128       PrintExpr(E->getPlacementArg(i));
2129     }
2130     OS << ") ";
2131   }
2132   if (E->isParenTypeId())
2133     OS << "(";
2134   std::string TypeS;
2135   if (Optional<Expr *> Size = E->getArraySize()) {
2136     llvm::raw_string_ostream s(TypeS);
2137     s << '[';
2138     if (*Size)
2139       (*Size)->printPretty(s, Helper, Policy);
2140     s << ']';
2141   }
2142   E->getAllocatedType().print(OS, Policy, TypeS);
2143   if (E->isParenTypeId())
2144     OS << ")";
2145 
2146   CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
2147   if (InitStyle) {
2148     if (InitStyle == CXXNewExpr::CallInit)
2149       OS << "(";
2150     PrintExpr(E->getInitializer());
2151     if (InitStyle == CXXNewExpr::CallInit)
2152       OS << ")";
2153   }
2154 }
2155 
2156 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2157   if (E->isGlobalDelete())
2158     OS << "::";
2159   OS << "delete ";
2160   if (E->isArrayForm())
2161     OS << "[] ";
2162   PrintExpr(E->getArgument());
2163 }
2164 
2165 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
2166   PrintExpr(E->getBase());
2167   if (E->isArrow())
2168     OS << "->";
2169   else
2170     OS << '.';
2171   if (E->getQualifier())
2172     E->getQualifier()->print(OS, Policy);
2173   OS << "~";
2174 
2175   if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
2176     OS << II->getName();
2177   else
2178     E->getDestroyedType().print(OS, Policy);
2179 }
2180 
2181 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
2182   if (E->isListInitialization() && !E->isStdInitListInitialization())
2183     OS << "{";
2184 
2185   for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
2186     if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
2187       // Don't print any defaulted arguments
2188       break;
2189     }
2190 
2191     if (i) OS << ", ";
2192     PrintExpr(E->getArg(i));
2193   }
2194 
2195   if (E->isListInitialization() && !E->isStdInitListInitialization())
2196     OS << "}";
2197 }
2198 
2199 void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) {
2200   // Parens are printed by the surrounding context.
2201   OS << "<forwarded>";
2202 }
2203 
2204 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
2205   PrintExpr(E->getSubExpr());
2206 }
2207 
2208 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
2209   // Just forward to the subexpression.
2210   PrintExpr(E->getSubExpr());
2211 }
2212 
2213 void
2214 StmtPrinter::VisitCXXUnresolvedConstructExpr(
2215                                            CXXUnresolvedConstructExpr *Node) {
2216   Node->getTypeAsWritten().print(OS, Policy);
2217   OS << "(";
2218   for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
2219                                              ArgEnd = Node->arg_end();
2220        Arg != ArgEnd; ++Arg) {
2221     if (Arg != Node->arg_begin())
2222       OS << ", ";
2223     PrintExpr(*Arg);
2224   }
2225   OS << ")";
2226 }
2227 
2228 void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2229                                          CXXDependentScopeMemberExpr *Node) {
2230   if (!Node->isImplicitAccess()) {
2231     PrintExpr(Node->getBase());
2232     OS << (Node->isArrow() ? "->" : ".");
2233   }
2234   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2235     Qualifier->print(OS, Policy);
2236   if (Node->hasTemplateKeyword())
2237     OS << "template ";
2238   OS << Node->getMemberNameInfo();
2239   if (Node->hasExplicitTemplateArgs())
2240     printTemplateArgumentList(OS, Node->template_arguments(), Policy);
2241 }
2242 
2243 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
2244   if (!Node->isImplicitAccess()) {
2245     PrintExpr(Node->getBase());
2246     OS << (Node->isArrow() ? "->" : ".");
2247   }
2248   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2249     Qualifier->print(OS, Policy);
2250   if (Node->hasTemplateKeyword())
2251     OS << "template ";
2252   OS << Node->getMemberNameInfo();
2253   if (Node->hasExplicitTemplateArgs())
2254     printTemplateArgumentList(OS, Node->template_arguments(), Policy);
2255 }
2256 
2257 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2258   OS << getTraitSpelling(E->getTrait()) << "(";
2259   for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2260     if (I > 0)
2261       OS << ", ";
2262     E->getArg(I)->getType().print(OS, Policy);
2263   }
2264   OS << ")";
2265 }
2266 
2267 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
2268   OS << getTraitSpelling(E->getTrait()) << '(';
2269   E->getQueriedType().print(OS, Policy);
2270   OS << ')';
2271 }
2272 
2273 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
2274   OS << getTraitSpelling(E->getTrait()) << '(';
2275   PrintExpr(E->getQueriedExpression());
2276   OS << ')';
2277 }
2278 
2279 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2280   OS << "noexcept(";
2281   PrintExpr(E->getOperand());
2282   OS << ")";
2283 }
2284 
2285 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
2286   PrintExpr(E->getPattern());
2287   OS << "...";
2288 }
2289 
2290 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2291   OS << "sizeof...(" << *E->getPack() << ")";
2292 }
2293 
2294 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2295                                        SubstNonTypeTemplateParmPackExpr *Node) {
2296   OS << *Node->getParameterPack();
2297 }
2298 
2299 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2300                                        SubstNonTypeTemplateParmExpr *Node) {
2301   Visit(Node->getReplacement());
2302 }
2303 
2304 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2305   OS << *E->getParameterPack();
2306 }
2307 
2308 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2309   PrintExpr(Node->getSubExpr());
2310 }
2311 
2312 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2313   OS << "(";
2314   if (E->getLHS()) {
2315     PrintExpr(E->getLHS());
2316     OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2317   }
2318   OS << "...";
2319   if (E->getRHS()) {
2320     OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2321     PrintExpr(E->getRHS());
2322   }
2323   OS << ")";
2324 }
2325 
2326 void StmtPrinter::VisitConceptSpecializationExpr(ConceptSpecializationExpr *E) {
2327   NestedNameSpecifierLoc NNS = E->getNestedNameSpecifierLoc();
2328   if (NNS)
2329     NNS.getNestedNameSpecifier()->print(OS, Policy);
2330   if (E->getTemplateKWLoc().isValid())
2331     OS << "template ";
2332   OS << E->getFoundDecl()->getName();
2333   printTemplateArgumentList(OS, E->getTemplateArgsAsWritten()->arguments(),
2334                             Policy,
2335                             E->getNamedConcept()->getTemplateParameters());
2336 }
2337 
2338 void StmtPrinter::VisitRequiresExpr(RequiresExpr *E) {
2339   OS << "requires ";
2340   auto LocalParameters = E->getLocalParameters();
2341   if (!LocalParameters.empty()) {
2342     OS << "(";
2343     for (ParmVarDecl *LocalParam : LocalParameters) {
2344       PrintRawDecl(LocalParam);
2345       if (LocalParam != LocalParameters.back())
2346         OS << ", ";
2347     }
2348 
2349     OS << ") ";
2350   }
2351   OS << "{ ";
2352   auto Requirements = E->getRequirements();
2353   for (concepts::Requirement *Req : Requirements) {
2354     if (auto *TypeReq = dyn_cast<concepts::TypeRequirement>(Req)) {
2355       if (TypeReq->isSubstitutionFailure())
2356         OS << "<<error-type>>";
2357       else
2358         TypeReq->getType()->getType().print(OS, Policy);
2359     } else if (auto *ExprReq = dyn_cast<concepts::ExprRequirement>(Req)) {
2360       if (ExprReq->isCompound())
2361         OS << "{ ";
2362       if (ExprReq->isExprSubstitutionFailure())
2363         OS << "<<error-expression>>";
2364       else
2365         PrintExpr(ExprReq->getExpr());
2366       if (ExprReq->isCompound()) {
2367         OS << " }";
2368         if (ExprReq->getNoexceptLoc().isValid())
2369           OS << " noexcept";
2370         const auto &RetReq = ExprReq->getReturnTypeRequirement();
2371         if (!RetReq.isEmpty()) {
2372           OS << " -> ";
2373           if (RetReq.isSubstitutionFailure())
2374             OS << "<<error-type>>";
2375           else if (RetReq.isTypeConstraint())
2376             RetReq.getTypeConstraint()->print(OS, Policy);
2377         }
2378       }
2379     } else {
2380       auto *NestedReq = cast<concepts::NestedRequirement>(Req);
2381       OS << "requires ";
2382       if (NestedReq->isSubstitutionFailure())
2383         OS << "<<error-expression>>";
2384       else
2385         PrintExpr(NestedReq->getConstraintExpr());
2386     }
2387     OS << "; ";
2388   }
2389   OS << "}";
2390 }
2391 
2392 // C++ Coroutines TS
2393 
2394 void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
2395   Visit(S->getBody());
2396 }
2397 
2398 void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
2399   OS << "co_return";
2400   if (S->getOperand()) {
2401     OS << " ";
2402     Visit(S->getOperand());
2403   }
2404   OS << ";";
2405 }
2406 
2407 void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
2408   OS << "co_await ";
2409   PrintExpr(S->getOperand());
2410 }
2411 
2412 void StmtPrinter::VisitDependentCoawaitExpr(DependentCoawaitExpr *S) {
2413   OS << "co_await ";
2414   PrintExpr(S->getOperand());
2415 }
2416 
2417 void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
2418   OS << "co_yield ";
2419   PrintExpr(S->getOperand());
2420 }
2421 
2422 // Obj-C
2423 
2424 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2425   OS << "@";
2426   VisitStringLiteral(Node->getString());
2427 }
2428 
2429 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
2430   OS << "@";
2431   Visit(E->getSubExpr());
2432 }
2433 
2434 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2435   OS << "@[ ";
2436   ObjCArrayLiteral::child_range Ch = E->children();
2437   for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2438     if (I != Ch.begin())
2439       OS << ", ";
2440     Visit(*I);
2441   }
2442   OS << " ]";
2443 }
2444 
2445 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2446   OS << "@{ ";
2447   for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2448     if (I > 0)
2449       OS << ", ";
2450 
2451     ObjCDictionaryElement Element = E->getKeyValueElement(I);
2452     Visit(Element.Key);
2453     OS << " : ";
2454     Visit(Element.Value);
2455     if (Element.isPackExpansion())
2456       OS << "...";
2457   }
2458   OS << " }";
2459 }
2460 
2461 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
2462   OS << "@encode(";
2463   Node->getEncodedType().print(OS, Policy);
2464   OS << ')';
2465 }
2466 
2467 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
2468   OS << "@selector(";
2469   Node->getSelector().print(OS);
2470   OS << ')';
2471 }
2472 
2473 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
2474   OS << "@protocol(" << *Node->getProtocol() << ')';
2475 }
2476 
2477 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2478   OS << "[";
2479   switch (Mess->getReceiverKind()) {
2480   case ObjCMessageExpr::Instance:
2481     PrintExpr(Mess->getInstanceReceiver());
2482     break;
2483 
2484   case ObjCMessageExpr::Class:
2485     Mess->getClassReceiver().print(OS, Policy);
2486     break;
2487 
2488   case ObjCMessageExpr::SuperInstance:
2489   case ObjCMessageExpr::SuperClass:
2490     OS << "Super";
2491     break;
2492   }
2493 
2494   OS << ' ';
2495   Selector selector = Mess->getSelector();
2496   if (selector.isUnarySelector()) {
2497     OS << selector.getNameForSlot(0);
2498   } else {
2499     for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
2500       if (i < selector.getNumArgs()) {
2501         if (i > 0) OS << ' ';
2502         if (selector.getIdentifierInfoForSlot(i))
2503           OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
2504         else
2505            OS << ":";
2506       }
2507       else OS << ", "; // Handle variadic methods.
2508 
2509       PrintExpr(Mess->getArg(i));
2510     }
2511   }
2512   OS << "]";
2513 }
2514 
2515 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2516   OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2517 }
2518 
2519 void
2520 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2521   PrintExpr(E->getSubExpr());
2522 }
2523 
2524 void
2525 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
2526   OS << '(' << E->getBridgeKindName();
2527   E->getType().print(OS, Policy);
2528   OS << ')';
2529   PrintExpr(E->getSubExpr());
2530 }
2531 
2532 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
2533   BlockDecl *BD = Node->getBlockDecl();
2534   OS << "^";
2535 
2536   const FunctionType *AFT = Node->getFunctionType();
2537 
2538   if (isa<FunctionNoProtoType>(AFT)) {
2539     OS << "()";
2540   } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
2541     OS << '(';
2542     for (BlockDecl::param_iterator AI = BD->param_begin(),
2543          E = BD->param_end(); AI != E; ++AI) {
2544       if (AI != BD->param_begin()) OS << ", ";
2545       std::string ParamStr = (*AI)->getNameAsString();
2546       (*AI)->getType().print(OS, Policy, ParamStr);
2547     }
2548 
2549     const auto *FT = cast<FunctionProtoType>(AFT);
2550     if (FT->isVariadic()) {
2551       if (!BD->param_empty()) OS << ", ";
2552       OS << "...";
2553     }
2554     OS << ')';
2555   }
2556   OS << "{ }";
2557 }
2558 
2559 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2560   PrintExpr(Node->getSourceExpr());
2561 }
2562 
2563 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2564   // TODO: Print something reasonable for a TypoExpr, if necessary.
2565   llvm_unreachable("Cannot print TypoExpr nodes");
2566 }
2567 
2568 void StmtPrinter::VisitRecoveryExpr(RecoveryExpr *Node) {
2569   OS << "<recovery-expr>(";
2570   const char *Sep = "";
2571   for (Expr *E : Node->subExpressions()) {
2572     OS << Sep;
2573     PrintExpr(E);
2574     Sep = ", ";
2575   }
2576   OS << ')';
2577 }
2578 
2579 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2580   OS << "__builtin_astype(";
2581   PrintExpr(Node->getSrcExpr());
2582   OS << ", ";
2583   Node->getType().print(OS, Policy);
2584   OS << ")";
2585 }
2586 
2587 //===----------------------------------------------------------------------===//
2588 // Stmt method implementations
2589 //===----------------------------------------------------------------------===//
2590 
2591 void Stmt::dumpPretty(const ASTContext &Context) const {
2592   printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
2593 }
2594 
2595 void Stmt::printPretty(raw_ostream &Out, PrinterHelper *Helper,
2596                        const PrintingPolicy &Policy, unsigned Indentation,
2597                        StringRef NL, const ASTContext *Context) const {
2598   StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context);
2599   P.Visit(const_cast<Stmt *>(this));
2600 }
2601 
2602 void Stmt::printPrettyControlled(raw_ostream &Out, PrinterHelper *Helper,
2603                                  const PrintingPolicy &Policy,
2604                                  unsigned Indentation, StringRef NL,
2605                                  const ASTContext *Context) const {
2606   StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context);
2607   P.PrintControlledStmt(const_cast<Stmt *>(this));
2608 }
2609 
2610 void Stmt::printJson(raw_ostream &Out, PrinterHelper *Helper,
2611                      const PrintingPolicy &Policy, bool AddQuotes) const {
2612   std::string Buf;
2613   llvm::raw_string_ostream TempOut(Buf);
2614 
2615   printPretty(TempOut, Helper, Policy);
2616 
2617   Out << JsonFormat(TempOut.str(), AddQuotes);
2618 }
2619 
2620 //===----------------------------------------------------------------------===//
2621 // PrinterHelper
2622 //===----------------------------------------------------------------------===//
2623 
2624 // Implement virtual destructor.
2625 PrinterHelper::~PrinterHelper() = default;
2626