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