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