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