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