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