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