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     E->printPretty(OS, nullptr, Policy);
670   }
671   OS << ")";
672 }
673 
674 void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *Node) {
675   OS << "ordered";
676   if (auto *Num = Node->getNumForLoops()) {
677     OS << "(";
678     Num->printPretty(OS, nullptr, Policy, 0);
679     OS << ")";
680   }
681 }
682 
683 void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) {
684   OS << "nowait";
685 }
686 
687 void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) {
688   OS << "untied";
689 }
690 
691 void OMPClausePrinter::VisitOMPNogroupClause(OMPNogroupClause *) {
692   OS << "nogroup";
693 }
694 
695 void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) {
696   OS << "mergeable";
697 }
698 
699 void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; }
700 
701 void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; }
702 
703 void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *) {
704   OS << "update";
705 }
706 
707 void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) {
708   OS << "capture";
709 }
710 
711 void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) {
712   OS << "seq_cst";
713 }
714 
715 void OMPClausePrinter::VisitOMPThreadsClause(OMPThreadsClause *) {
716   OS << "threads";
717 }
718 
719 void OMPClausePrinter::VisitOMPSIMDClause(OMPSIMDClause *) { OS << "simd"; }
720 
721 void OMPClausePrinter::VisitOMPDeviceClause(OMPDeviceClause *Node) {
722   OS << "device(";
723   Node->getDevice()->printPretty(OS, nullptr, Policy, 0);
724   OS << ")";
725 }
726 
727 void OMPClausePrinter::VisitOMPNumTeamsClause(OMPNumTeamsClause *Node) {
728   OS << "num_teams(";
729   Node->getNumTeams()->printPretty(OS, nullptr, Policy, 0);
730   OS << ")";
731 }
732 
733 void OMPClausePrinter::VisitOMPThreadLimitClause(OMPThreadLimitClause *Node) {
734   OS << "thread_limit(";
735   Node->getThreadLimit()->printPretty(OS, nullptr, Policy, 0);
736   OS << ")";
737 }
738 
739 void OMPClausePrinter::VisitOMPPriorityClause(OMPPriorityClause *Node) {
740   OS << "priority(";
741   Node->getPriority()->printPretty(OS, nullptr, Policy, 0);
742   OS << ")";
743 }
744 
745 void OMPClausePrinter::VisitOMPGrainsizeClause(OMPGrainsizeClause *Node) {
746   OS << "grainsize(";
747   Node->getGrainsize()->printPretty(OS, nullptr, Policy, 0);
748   OS << ")";
749 }
750 
751 void OMPClausePrinter::VisitOMPNumTasksClause(OMPNumTasksClause *Node) {
752   OS << "num_tasks(";
753   Node->getNumTasks()->printPretty(OS, nullptr, Policy, 0);
754   OS << ")";
755 }
756 
757 void OMPClausePrinter::VisitOMPHintClause(OMPHintClause *Node) {
758   OS << "hint(";
759   Node->getHint()->printPretty(OS, nullptr, Policy, 0);
760   OS << ")";
761 }
762 
763 template<typename T>
764 void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
765   for (typename T::varlist_iterator I = Node->varlist_begin(),
766                                     E = Node->varlist_end();
767        I != E; ++I) {
768     assert(*I && "Expected non-null Stmt");
769     OS << (I == Node->varlist_begin() ? StartSym : ',');
770     if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*I)) {
771       if (isa<OMPCapturedExprDecl>(DRE->getDecl()))
772         DRE->printPretty(OS, nullptr, Policy, 0);
773       else
774         DRE->getDecl()->printQualifiedName(OS);
775     } else
776       (*I)->printPretty(OS, nullptr, Policy, 0);
777   }
778 }
779 
780 void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
781   if (!Node->varlist_empty()) {
782     OS << "private";
783     VisitOMPClauseList(Node, '(');
784     OS << ")";
785   }
786 }
787 
788 void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
789   if (!Node->varlist_empty()) {
790     OS << "firstprivate";
791     VisitOMPClauseList(Node, '(');
792     OS << ")";
793   }
794 }
795 
796 void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) {
797   if (!Node->varlist_empty()) {
798     OS << "lastprivate";
799     VisitOMPClauseList(Node, '(');
800     OS << ")";
801   }
802 }
803 
804 void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
805   if (!Node->varlist_empty()) {
806     OS << "shared";
807     VisitOMPClauseList(Node, '(');
808     OS << ")";
809   }
810 }
811 
812 void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) {
813   if (!Node->varlist_empty()) {
814     OS << "reduction(";
815     NestedNameSpecifier *QualifierLoc =
816         Node->getQualifierLoc().getNestedNameSpecifier();
817     OverloadedOperatorKind OOK =
818         Node->getNameInfo().getName().getCXXOverloadedOperator();
819     if (QualifierLoc == nullptr && OOK != OO_None) {
820       // Print reduction identifier in C format
821       OS << getOperatorSpelling(OOK);
822     } else {
823       // Use C++ format
824       if (QualifierLoc != nullptr)
825         QualifierLoc->print(OS, Policy);
826       OS << Node->getNameInfo();
827     }
828     OS << ":";
829     VisitOMPClauseList(Node, ' ');
830     OS << ")";
831   }
832 }
833 
834 void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) {
835   if (!Node->varlist_empty()) {
836     OS << "linear";
837     if (Node->getModifierLoc().isValid()) {
838       OS << '('
839          << getOpenMPSimpleClauseTypeName(OMPC_linear, Node->getModifier());
840     }
841     VisitOMPClauseList(Node, '(');
842     if (Node->getModifierLoc().isValid())
843       OS << ')';
844     if (Node->getStep() != nullptr) {
845       OS << ": ";
846       Node->getStep()->printPretty(OS, nullptr, Policy, 0);
847     }
848     OS << ")";
849   }
850 }
851 
852 void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) {
853   if (!Node->varlist_empty()) {
854     OS << "aligned";
855     VisitOMPClauseList(Node, '(');
856     if (Node->getAlignment() != nullptr) {
857       OS << ": ";
858       Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
859     }
860     OS << ")";
861   }
862 }
863 
864 void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
865   if (!Node->varlist_empty()) {
866     OS << "copyin";
867     VisitOMPClauseList(Node, '(');
868     OS << ")";
869   }
870 }
871 
872 void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) {
873   if (!Node->varlist_empty()) {
874     OS << "copyprivate";
875     VisitOMPClauseList(Node, '(');
876     OS << ")";
877   }
878 }
879 
880 void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) {
881   if (!Node->varlist_empty()) {
882     VisitOMPClauseList(Node, '(');
883     OS << ")";
884   }
885 }
886 
887 void OMPClausePrinter::VisitOMPDependClause(OMPDependClause *Node) {
888   OS << "depend(";
889   OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
890                                       Node->getDependencyKind());
891   if (!Node->varlist_empty()) {
892     OS << " :";
893     VisitOMPClauseList(Node, ' ');
894   }
895   OS << ")";
896 }
897 
898 void OMPClausePrinter::VisitOMPMapClause(OMPMapClause *Node) {
899   if (!Node->varlist_empty()) {
900     OS << "map(";
901     if (Node->getMapType() != OMPC_MAP_unknown) {
902       if (Node->getMapTypeModifier() != OMPC_MAP_unknown) {
903         OS << getOpenMPSimpleClauseTypeName(OMPC_map,
904                                             Node->getMapTypeModifier());
905         OS << ',';
906       }
907       OS << getOpenMPSimpleClauseTypeName(OMPC_map, Node->getMapType());
908       OS << ':';
909     }
910     VisitOMPClauseList(Node, ' ');
911     OS << ")";
912   }
913 }
914 
915 void OMPClausePrinter::VisitOMPToClause(OMPToClause *Node) {
916   if (!Node->varlist_empty()) {
917     OS << "to";
918     VisitOMPClauseList(Node, '(');
919     OS << ")";
920   }
921 }
922 
923 void OMPClausePrinter::VisitOMPFromClause(OMPFromClause *Node) {
924   if (!Node->varlist_empty()) {
925     OS << "from";
926     VisitOMPClauseList(Node, '(');
927     OS << ")";
928   }
929 }
930 
931 void OMPClausePrinter::VisitOMPDistScheduleClause(OMPDistScheduleClause *Node) {
932   OS << "dist_schedule(" << getOpenMPSimpleClauseTypeName(
933                            OMPC_dist_schedule, Node->getDistScheduleKind());
934   if (auto *E = Node->getChunkSize()) {
935     OS << ", ";
936     E->printPretty(OS, nullptr, Policy);
937   }
938   OS << ")";
939 }
940 
941 void OMPClausePrinter::VisitOMPDefaultmapClause(OMPDefaultmapClause *Node) {
942   OS << "defaultmap(";
943   OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
944                                       Node->getDefaultmapModifier());
945   OS << ": ";
946   OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
947     Node->getDefaultmapKind());
948   OS << ")";
949 }
950 }
951 
952 //===----------------------------------------------------------------------===//
953 //  OpenMP directives printing methods
954 //===----------------------------------------------------------------------===//
955 
956 void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) {
957   OMPClausePrinter Printer(OS, Policy);
958   ArrayRef<OMPClause *> Clauses = S->clauses();
959   for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
960        I != E; ++I)
961     if (*I && !(*I)->isImplicit()) {
962       Printer.Visit(*I);
963       OS << ' ';
964     }
965   OS << "\n";
966   if (S->hasAssociatedStmt() && S->getAssociatedStmt()) {
967     assert(isa<CapturedStmt>(S->getAssociatedStmt()) &&
968            "Expected captured statement!");
969     Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt();
970     PrintStmt(CS);
971   }
972 }
973 
974 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
975   Indent() << "#pragma omp parallel ";
976   PrintOMPExecutableDirective(Node);
977 }
978 
979 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
980   Indent() << "#pragma omp simd ";
981   PrintOMPExecutableDirective(Node);
982 }
983 
984 void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
985   Indent() << "#pragma omp for ";
986   PrintOMPExecutableDirective(Node);
987 }
988 
989 void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
990   Indent() << "#pragma omp for simd ";
991   PrintOMPExecutableDirective(Node);
992 }
993 
994 void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
995   Indent() << "#pragma omp sections ";
996   PrintOMPExecutableDirective(Node);
997 }
998 
999 void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
1000   Indent() << "#pragma omp section";
1001   PrintOMPExecutableDirective(Node);
1002 }
1003 
1004 void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
1005   Indent() << "#pragma omp single ";
1006   PrintOMPExecutableDirective(Node);
1007 }
1008 
1009 void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
1010   Indent() << "#pragma omp master";
1011   PrintOMPExecutableDirective(Node);
1012 }
1013 
1014 void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
1015   Indent() << "#pragma omp critical";
1016   if (Node->getDirectiveName().getName()) {
1017     OS << " (";
1018     Node->getDirectiveName().printName(OS);
1019     OS << ")";
1020   }
1021   OS << " ";
1022   PrintOMPExecutableDirective(Node);
1023 }
1024 
1025 void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
1026   Indent() << "#pragma omp parallel for ";
1027   PrintOMPExecutableDirective(Node);
1028 }
1029 
1030 void StmtPrinter::VisitOMPParallelForSimdDirective(
1031     OMPParallelForSimdDirective *Node) {
1032   Indent() << "#pragma omp parallel for simd ";
1033   PrintOMPExecutableDirective(Node);
1034 }
1035 
1036 void StmtPrinter::VisitOMPParallelSectionsDirective(
1037     OMPParallelSectionsDirective *Node) {
1038   Indent() << "#pragma omp parallel sections ";
1039   PrintOMPExecutableDirective(Node);
1040 }
1041 
1042 void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
1043   Indent() << "#pragma omp task ";
1044   PrintOMPExecutableDirective(Node);
1045 }
1046 
1047 void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
1048   Indent() << "#pragma omp taskyield";
1049   PrintOMPExecutableDirective(Node);
1050 }
1051 
1052 void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
1053   Indent() << "#pragma omp barrier";
1054   PrintOMPExecutableDirective(Node);
1055 }
1056 
1057 void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
1058   Indent() << "#pragma omp taskwait";
1059   PrintOMPExecutableDirective(Node);
1060 }
1061 
1062 void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
1063   Indent() << "#pragma omp taskgroup";
1064   PrintOMPExecutableDirective(Node);
1065 }
1066 
1067 void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
1068   Indent() << "#pragma omp flush ";
1069   PrintOMPExecutableDirective(Node);
1070 }
1071 
1072 void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
1073   Indent() << "#pragma omp ordered ";
1074   PrintOMPExecutableDirective(Node);
1075 }
1076 
1077 void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
1078   Indent() << "#pragma omp atomic ";
1079   PrintOMPExecutableDirective(Node);
1080 }
1081 
1082 void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
1083   Indent() << "#pragma omp target ";
1084   PrintOMPExecutableDirective(Node);
1085 }
1086 
1087 void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
1088   Indent() << "#pragma omp target data ";
1089   PrintOMPExecutableDirective(Node);
1090 }
1091 
1092 void StmtPrinter::VisitOMPTargetEnterDataDirective(
1093     OMPTargetEnterDataDirective *Node) {
1094   Indent() << "#pragma omp target enter data ";
1095   PrintOMPExecutableDirective(Node);
1096 }
1097 
1098 void StmtPrinter::VisitOMPTargetExitDataDirective(
1099     OMPTargetExitDataDirective *Node) {
1100   Indent() << "#pragma omp target exit data ";
1101   PrintOMPExecutableDirective(Node);
1102 }
1103 
1104 void StmtPrinter::VisitOMPTargetParallelDirective(
1105     OMPTargetParallelDirective *Node) {
1106   Indent() << "#pragma omp target parallel ";
1107   PrintOMPExecutableDirective(Node);
1108 }
1109 
1110 void StmtPrinter::VisitOMPTargetParallelForDirective(
1111     OMPTargetParallelForDirective *Node) {
1112   Indent() << "#pragma omp target parallel for ";
1113   PrintOMPExecutableDirective(Node);
1114 }
1115 
1116 void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
1117   Indent() << "#pragma omp teams ";
1118   PrintOMPExecutableDirective(Node);
1119 }
1120 
1121 void StmtPrinter::VisitOMPCancellationPointDirective(
1122     OMPCancellationPointDirective *Node) {
1123   Indent() << "#pragma omp cancellation point "
1124            << getOpenMPDirectiveName(Node->getCancelRegion());
1125   PrintOMPExecutableDirective(Node);
1126 }
1127 
1128 void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
1129   Indent() << "#pragma omp cancel "
1130            << getOpenMPDirectiveName(Node->getCancelRegion()) << " ";
1131   PrintOMPExecutableDirective(Node);
1132 }
1133 
1134 void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) {
1135   Indent() << "#pragma omp taskloop ";
1136   PrintOMPExecutableDirective(Node);
1137 }
1138 
1139 void StmtPrinter::VisitOMPTaskLoopSimdDirective(
1140     OMPTaskLoopSimdDirective *Node) {
1141   Indent() << "#pragma omp taskloop simd ";
1142   PrintOMPExecutableDirective(Node);
1143 }
1144 
1145 void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) {
1146   Indent() << "#pragma omp distribute ";
1147   PrintOMPExecutableDirective(Node);
1148 }
1149 
1150 void StmtPrinter::VisitOMPTargetUpdateDirective(
1151     OMPTargetUpdateDirective *Node) {
1152   Indent() << "#pragma omp target update ";
1153   PrintOMPExecutableDirective(Node);
1154 }
1155 
1156 //===----------------------------------------------------------------------===//
1157 //  Expr printing methods.
1158 //===----------------------------------------------------------------------===//
1159 
1160 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
1161   if (auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) {
1162     OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy);
1163     return;
1164   }
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::VisitDependentScopeDeclRefExpr(
1176                                            DependentScopeDeclRefExpr *Node) {
1177   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1178     Qualifier->print(OS, Policy);
1179   if (Node->hasTemplateKeyword())
1180     OS << "template ";
1181   OS << Node->getNameInfo();
1182   if (Node->hasExplicitTemplateArgs())
1183     TemplateSpecializationType::PrintTemplateArgumentList(
1184         OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1185 }
1186 
1187 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
1188   if (Node->getQualifier())
1189     Node->getQualifier()->print(OS, Policy);
1190   if (Node->hasTemplateKeyword())
1191     OS << "template ";
1192   OS << Node->getNameInfo();
1193   if (Node->hasExplicitTemplateArgs())
1194     TemplateSpecializationType::PrintTemplateArgumentList(
1195         OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1196 }
1197 
1198 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
1199   if (Node->getBase()) {
1200     PrintExpr(Node->getBase());
1201     OS << (Node->isArrow() ? "->" : ".");
1202   }
1203   OS << *Node->getDecl();
1204 }
1205 
1206 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
1207   if (Node->isSuperReceiver())
1208     OS << "super.";
1209   else if (Node->isObjectReceiver() && Node->getBase()) {
1210     PrintExpr(Node->getBase());
1211     OS << ".";
1212   } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1213     OS << Node->getClassReceiver()->getName() << ".";
1214   }
1215 
1216   if (Node->isImplicitProperty())
1217     Node->getImplicitPropertyGetter()->getSelector().print(OS);
1218   else
1219     OS << Node->getExplicitProperty()->getName();
1220 }
1221 
1222 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1223 
1224   PrintExpr(Node->getBaseExpr());
1225   OS << "[";
1226   PrintExpr(Node->getKeyExpr());
1227   OS << "]";
1228 }
1229 
1230 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
1231   OS << PredefinedExpr::getIdentTypeName(Node->getIdentType());
1232 }
1233 
1234 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
1235   unsigned value = Node->getValue();
1236 
1237   switch (Node->getKind()) {
1238   case CharacterLiteral::Ascii: break; // no prefix.
1239   case CharacterLiteral::Wide:  OS << 'L'; break;
1240   case CharacterLiteral::UTF8:  OS << "u8"; break;
1241   case CharacterLiteral::UTF16: OS << 'u'; break;
1242   case CharacterLiteral::UTF32: OS << 'U'; break;
1243   }
1244 
1245   switch (value) {
1246   case '\\':
1247     OS << "'\\\\'";
1248     break;
1249   case '\'':
1250     OS << "'\\''";
1251     break;
1252   case '\a':
1253     // TODO: K&R: the meaning of '\\a' is different in traditional C
1254     OS << "'\\a'";
1255     break;
1256   case '\b':
1257     OS << "'\\b'";
1258     break;
1259   // Nonstandard escape sequence.
1260   /*case '\e':
1261     OS << "'\\e'";
1262     break;*/
1263   case '\f':
1264     OS << "'\\f'";
1265     break;
1266   case '\n':
1267     OS << "'\\n'";
1268     break;
1269   case '\r':
1270     OS << "'\\r'";
1271     break;
1272   case '\t':
1273     OS << "'\\t'";
1274     break;
1275   case '\v':
1276     OS << "'\\v'";
1277     break;
1278   default:
1279     // A character literal might be sign-extended, which
1280     // would result in an invalid \U escape sequence.
1281     // FIXME: multicharacter literals such as '\xFF\xFF\xFF\xFF'
1282     // are not correctly handled.
1283     if ((value & ~0xFFu) == ~0xFFu && Node->getKind() == CharacterLiteral::Ascii)
1284       value &= 0xFFu;
1285     if (value < 256 && isPrintable((unsigned char)value))
1286       OS << "'" << (char)value << "'";
1287     else if (value < 256)
1288       OS << "'\\x" << llvm::format("%02x", value) << "'";
1289     else if (value <= 0xFFFF)
1290       OS << "'\\u" << llvm::format("%04x", value) << "'";
1291     else
1292       OS << "'\\U" << llvm::format("%08x", value) << "'";
1293   }
1294 }
1295 
1296 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
1297   bool isSigned = Node->getType()->isSignedIntegerType();
1298   OS << Node->getValue().toString(10, isSigned);
1299 
1300   // Emit suffixes.  Integer literals are always a builtin integer type.
1301   switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1302   default: llvm_unreachable("Unexpected type for integer literal!");
1303   case BuiltinType::Char_S:
1304   case BuiltinType::Char_U:    OS << "i8"; break;
1305   case BuiltinType::UChar:     OS << "Ui8"; break;
1306   case BuiltinType::Short:     OS << "i16"; break;
1307   case BuiltinType::UShort:    OS << "Ui16"; break;
1308   case BuiltinType::Int:       break; // no suffix.
1309   case BuiltinType::UInt:      OS << 'U'; break;
1310   case BuiltinType::Long:      OS << 'L'; break;
1311   case BuiltinType::ULong:     OS << "UL"; break;
1312   case BuiltinType::LongLong:  OS << "LL"; break;
1313   case BuiltinType::ULongLong: OS << "ULL"; break;
1314   }
1315 }
1316 
1317 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1318                                  bool PrintSuffix) {
1319   SmallString<16> Str;
1320   Node->getValue().toString(Str);
1321   OS << Str;
1322   if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1323     OS << '.'; // Trailing dot in order to separate from ints.
1324 
1325   if (!PrintSuffix)
1326     return;
1327 
1328   // Emit suffixes.  Float literals are always a builtin float type.
1329   switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1330   default: llvm_unreachable("Unexpected type for float literal!");
1331   case BuiltinType::Half:       break; // FIXME: suffix?
1332   case BuiltinType::Double:     break; // no suffix.
1333   case BuiltinType::Float:      OS << 'F'; break;
1334   case BuiltinType::LongDouble: OS << 'L'; break;
1335   case BuiltinType::Float128:   OS << 'Q'; break;
1336   }
1337 }
1338 
1339 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1340   PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
1341 }
1342 
1343 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1344   PrintExpr(Node->getSubExpr());
1345   OS << "i";
1346 }
1347 
1348 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
1349   Str->outputString(OS);
1350 }
1351 void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1352   OS << "(";
1353   PrintExpr(Node->getSubExpr());
1354   OS << ")";
1355 }
1356 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
1357   if (!Node->isPostfix()) {
1358     OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1359 
1360     // Print a space if this is an "identifier operator" like __real, or if
1361     // it might be concatenated incorrectly like '+'.
1362     switch (Node->getOpcode()) {
1363     default: break;
1364     case UO_Real:
1365     case UO_Imag:
1366     case UO_Extension:
1367       OS << ' ';
1368       break;
1369     case UO_Plus:
1370     case UO_Minus:
1371       if (isa<UnaryOperator>(Node->getSubExpr()))
1372         OS << ' ';
1373       break;
1374     }
1375   }
1376   PrintExpr(Node->getSubExpr());
1377 
1378   if (Node->isPostfix())
1379     OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1380 }
1381 
1382 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1383   OS << "__builtin_offsetof(";
1384   Node->getTypeSourceInfo()->getType().print(OS, Policy);
1385   OS << ", ";
1386   bool PrintedSomething = false;
1387   for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1388     OffsetOfNode ON = Node->getComponent(i);
1389     if (ON.getKind() == OffsetOfNode::Array) {
1390       // Array node
1391       OS << "[";
1392       PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1393       OS << "]";
1394       PrintedSomething = true;
1395       continue;
1396     }
1397 
1398     // Skip implicit base indirections.
1399     if (ON.getKind() == OffsetOfNode::Base)
1400       continue;
1401 
1402     // Field or identifier node.
1403     IdentifierInfo *Id = ON.getFieldName();
1404     if (!Id)
1405       continue;
1406 
1407     if (PrintedSomething)
1408       OS << ".";
1409     else
1410       PrintedSomething = true;
1411     OS << Id->getName();
1412   }
1413   OS << ")";
1414 }
1415 
1416 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
1417   switch(Node->getKind()) {
1418   case UETT_SizeOf:
1419     OS << "sizeof";
1420     break;
1421   case UETT_AlignOf:
1422     if (Policy.Alignof)
1423       OS << "alignof";
1424     else if (Policy.UnderscoreAlignof)
1425       OS << "_Alignof";
1426     else
1427       OS << "__alignof";
1428     break;
1429   case UETT_VecStep:
1430     OS << "vec_step";
1431     break;
1432   case UETT_OpenMPRequiredSimdAlign:
1433     OS << "__builtin_omp_required_simd_align";
1434     break;
1435   }
1436   if (Node->isArgumentType()) {
1437     OS << '(';
1438     Node->getArgumentType().print(OS, Policy);
1439     OS << ')';
1440   } else {
1441     OS << " ";
1442     PrintExpr(Node->getArgumentExpr());
1443   }
1444 }
1445 
1446 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1447   OS << "_Generic(";
1448   PrintExpr(Node->getControllingExpr());
1449   for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
1450     OS << ", ";
1451     QualType T = Node->getAssocType(i);
1452     if (T.isNull())
1453       OS << "default";
1454     else
1455       T.print(OS, Policy);
1456     OS << ": ";
1457     PrintExpr(Node->getAssocExpr(i));
1458   }
1459   OS << ")";
1460 }
1461 
1462 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
1463   PrintExpr(Node->getLHS());
1464   OS << "[";
1465   PrintExpr(Node->getRHS());
1466   OS << "]";
1467 }
1468 
1469 void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) {
1470   PrintExpr(Node->getBase());
1471   OS << "[";
1472   if (Node->getLowerBound())
1473     PrintExpr(Node->getLowerBound());
1474   if (Node->getColonLoc().isValid()) {
1475     OS << ":";
1476     if (Node->getLength())
1477       PrintExpr(Node->getLength());
1478   }
1479   OS << "]";
1480 }
1481 
1482 void StmtPrinter::PrintCallArgs(CallExpr *Call) {
1483   for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
1484     if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1485       // Don't print any defaulted arguments
1486       break;
1487     }
1488 
1489     if (i) OS << ", ";
1490     PrintExpr(Call->getArg(i));
1491   }
1492 }
1493 
1494 void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1495   PrintExpr(Call->getCallee());
1496   OS << "(";
1497   PrintCallArgs(Call);
1498   OS << ")";
1499 }
1500 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1501   // FIXME: Suppress printing implicit bases (like "this")
1502   PrintExpr(Node->getBase());
1503 
1504   MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1505   FieldDecl  *ParentDecl   = ParentMember
1506     ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr;
1507 
1508   if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1509     OS << (Node->isArrow() ? "->" : ".");
1510 
1511   if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1512     if (FD->isAnonymousStructOrUnion())
1513       return;
1514 
1515   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1516     Qualifier->print(OS, Policy);
1517   if (Node->hasTemplateKeyword())
1518     OS << "template ";
1519   OS << Node->getMemberNameInfo();
1520   if (Node->hasExplicitTemplateArgs())
1521     TemplateSpecializationType::PrintTemplateArgumentList(
1522         OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1523 }
1524 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1525   PrintExpr(Node->getBase());
1526   OS << (Node->isArrow() ? "->isa" : ".isa");
1527 }
1528 
1529 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1530   PrintExpr(Node->getBase());
1531   OS << ".";
1532   OS << Node->getAccessor().getName();
1533 }
1534 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1535   OS << '(';
1536   Node->getTypeAsWritten().print(OS, Policy);
1537   OS << ')';
1538   PrintExpr(Node->getSubExpr());
1539 }
1540 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1541   OS << '(';
1542   Node->getType().print(OS, Policy);
1543   OS << ')';
1544   PrintExpr(Node->getInitializer());
1545 }
1546 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1547   // No need to print anything, simply forward to the subexpression.
1548   PrintExpr(Node->getSubExpr());
1549 }
1550 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1551   PrintExpr(Node->getLHS());
1552   OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1553   PrintExpr(Node->getRHS());
1554 }
1555 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1556   PrintExpr(Node->getLHS());
1557   OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1558   PrintExpr(Node->getRHS());
1559 }
1560 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1561   PrintExpr(Node->getCond());
1562   OS << " ? ";
1563   PrintExpr(Node->getLHS());
1564   OS << " : ";
1565   PrintExpr(Node->getRHS());
1566 }
1567 
1568 // GNU extensions.
1569 
1570 void
1571 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1572   PrintExpr(Node->getCommon());
1573   OS << " ?: ";
1574   PrintExpr(Node->getFalseExpr());
1575 }
1576 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1577   OS << "&&" << Node->getLabel()->getName();
1578 }
1579 
1580 void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1581   OS << "(";
1582   PrintRawCompoundStmt(E->getSubStmt());
1583   OS << ")";
1584 }
1585 
1586 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1587   OS << "__builtin_choose_expr(";
1588   PrintExpr(Node->getCond());
1589   OS << ", ";
1590   PrintExpr(Node->getLHS());
1591   OS << ", ";
1592   PrintExpr(Node->getRHS());
1593   OS << ")";
1594 }
1595 
1596 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1597   OS << "__null";
1598 }
1599 
1600 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1601   OS << "__builtin_shufflevector(";
1602   for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1603     if (i) OS << ", ";
1604     PrintExpr(Node->getExpr(i));
1605   }
1606   OS << ")";
1607 }
1608 
1609 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1610   OS << "__builtin_convertvector(";
1611   PrintExpr(Node->getSrcExpr());
1612   OS << ", ";
1613   Node->getType().print(OS, Policy);
1614   OS << ")";
1615 }
1616 
1617 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1618   if (Node->getSyntacticForm()) {
1619     Visit(Node->getSyntacticForm());
1620     return;
1621   }
1622 
1623   OS << "{";
1624   for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1625     if (i) OS << ", ";
1626     if (Node->getInit(i))
1627       PrintExpr(Node->getInit(i));
1628     else
1629       OS << "{}";
1630   }
1631   OS << "}";
1632 }
1633 
1634 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1635   OS << "(";
1636   for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1637     if (i) OS << ", ";
1638     PrintExpr(Node->getExpr(i));
1639   }
1640   OS << ")";
1641 }
1642 
1643 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1644   bool NeedsEquals = true;
1645   for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1646                       DEnd = Node->designators_end();
1647        D != DEnd; ++D) {
1648     if (D->isFieldDesignator()) {
1649       if (D->getDotLoc().isInvalid()) {
1650         if (IdentifierInfo *II = D->getFieldName()) {
1651           OS << II->getName() << ":";
1652           NeedsEquals = false;
1653         }
1654       } else {
1655         OS << "." << D->getFieldName()->getName();
1656       }
1657     } else {
1658       OS << "[";
1659       if (D->isArrayDesignator()) {
1660         PrintExpr(Node->getArrayIndex(*D));
1661       } else {
1662         PrintExpr(Node->getArrayRangeStart(*D));
1663         OS << " ... ";
1664         PrintExpr(Node->getArrayRangeEnd(*D));
1665       }
1666       OS << "]";
1667     }
1668   }
1669 
1670   if (NeedsEquals)
1671     OS << " = ";
1672   else
1673     OS << " ";
1674   PrintExpr(Node->getInit());
1675 }
1676 
1677 void StmtPrinter::VisitDesignatedInitUpdateExpr(
1678     DesignatedInitUpdateExpr *Node) {
1679   OS << "{";
1680   OS << "/*base*/";
1681   PrintExpr(Node->getBase());
1682   OS << ", ";
1683 
1684   OS << "/*updater*/";
1685   PrintExpr(Node->getUpdater());
1686   OS << "}";
1687 }
1688 
1689 void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1690   OS << "/*no init*/";
1691 }
1692 
1693 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1694   if (Node->getType()->getAsCXXRecordDecl()) {
1695     OS << "/*implicit*/";
1696     Node->getType().print(OS, Policy);
1697     OS << "()";
1698   } else {
1699     OS << "/*implicit*/(";
1700     Node->getType().print(OS, Policy);
1701     OS << ')';
1702     if (Node->getType()->isRecordType())
1703       OS << "{}";
1704     else
1705       OS << 0;
1706   }
1707 }
1708 
1709 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1710   OS << "__builtin_va_arg(";
1711   PrintExpr(Node->getSubExpr());
1712   OS << ", ";
1713   Node->getType().print(OS, Policy);
1714   OS << ")";
1715 }
1716 
1717 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1718   PrintExpr(Node->getSyntacticForm());
1719 }
1720 
1721 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1722   const char *Name = nullptr;
1723   switch (Node->getOp()) {
1724 #define BUILTIN(ID, TYPE, ATTRS)
1725 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1726   case AtomicExpr::AO ## ID: \
1727     Name = #ID "("; \
1728     break;
1729 #include "clang/Basic/Builtins.def"
1730   }
1731   OS << Name;
1732 
1733   // AtomicExpr stores its subexpressions in a permuted order.
1734   PrintExpr(Node->getPtr());
1735   if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1736       Node->getOp() != AtomicExpr::AO__atomic_load_n) {
1737     OS << ", ";
1738     PrintExpr(Node->getVal1());
1739   }
1740   if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1741       Node->isCmpXChg()) {
1742     OS << ", ";
1743     PrintExpr(Node->getVal2());
1744   }
1745   if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1746       Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1747     OS << ", ";
1748     PrintExpr(Node->getWeak());
1749   }
1750   if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1751     OS << ", ";
1752     PrintExpr(Node->getOrder());
1753   }
1754   if (Node->isCmpXChg()) {
1755     OS << ", ";
1756     PrintExpr(Node->getOrderFail());
1757   }
1758   OS << ")";
1759 }
1760 
1761 // C++
1762 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1763   const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1764     "",
1765 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1766     Spelling,
1767 #include "clang/Basic/OperatorKinds.def"
1768   };
1769 
1770   OverloadedOperatorKind Kind = Node->getOperator();
1771   if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1772     if (Node->getNumArgs() == 1) {
1773       OS << OpStrings[Kind] << ' ';
1774       PrintExpr(Node->getArg(0));
1775     } else {
1776       PrintExpr(Node->getArg(0));
1777       OS << ' ' << OpStrings[Kind];
1778     }
1779   } else if (Kind == OO_Arrow) {
1780     PrintExpr(Node->getArg(0));
1781   } else if (Kind == OO_Call) {
1782     PrintExpr(Node->getArg(0));
1783     OS << '(';
1784     for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1785       if (ArgIdx > 1)
1786         OS << ", ";
1787       if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1788         PrintExpr(Node->getArg(ArgIdx));
1789     }
1790     OS << ')';
1791   } else if (Kind == OO_Subscript) {
1792     PrintExpr(Node->getArg(0));
1793     OS << '[';
1794     PrintExpr(Node->getArg(1));
1795     OS << ']';
1796   } else if (Node->getNumArgs() == 1) {
1797     OS << OpStrings[Kind] << ' ';
1798     PrintExpr(Node->getArg(0));
1799   } else if (Node->getNumArgs() == 2) {
1800     PrintExpr(Node->getArg(0));
1801     OS << ' ' << OpStrings[Kind] << ' ';
1802     PrintExpr(Node->getArg(1));
1803   } else {
1804     llvm_unreachable("unknown overloaded operator");
1805   }
1806 }
1807 
1808 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1809   // If we have a conversion operator call only print the argument.
1810   CXXMethodDecl *MD = Node->getMethodDecl();
1811   if (MD && isa<CXXConversionDecl>(MD)) {
1812     PrintExpr(Node->getImplicitObjectArgument());
1813     return;
1814   }
1815   VisitCallExpr(cast<CallExpr>(Node));
1816 }
1817 
1818 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1819   PrintExpr(Node->getCallee());
1820   OS << "<<<";
1821   PrintCallArgs(Node->getConfig());
1822   OS << ">>>(";
1823   PrintCallArgs(Node);
1824   OS << ")";
1825 }
1826 
1827 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1828   OS << Node->getCastName() << '<';
1829   Node->getTypeAsWritten().print(OS, Policy);
1830   OS << ">(";
1831   PrintExpr(Node->getSubExpr());
1832   OS << ")";
1833 }
1834 
1835 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1836   VisitCXXNamedCastExpr(Node);
1837 }
1838 
1839 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1840   VisitCXXNamedCastExpr(Node);
1841 }
1842 
1843 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1844   VisitCXXNamedCastExpr(Node);
1845 }
1846 
1847 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1848   VisitCXXNamedCastExpr(Node);
1849 }
1850 
1851 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1852   OS << "typeid(";
1853   if (Node->isTypeOperand()) {
1854     Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1855   } else {
1856     PrintExpr(Node->getExprOperand());
1857   }
1858   OS << ")";
1859 }
1860 
1861 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1862   OS << "__uuidof(";
1863   if (Node->isTypeOperand()) {
1864     Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1865   } else {
1866     PrintExpr(Node->getExprOperand());
1867   }
1868   OS << ")";
1869 }
1870 
1871 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1872   PrintExpr(Node->getBaseExpr());
1873   if (Node->isArrow())
1874     OS << "->";
1875   else
1876     OS << ".";
1877   if (NestedNameSpecifier *Qualifier =
1878       Node->getQualifierLoc().getNestedNameSpecifier())
1879     Qualifier->print(OS, Policy);
1880   OS << Node->getPropertyDecl()->getDeclName();
1881 }
1882 
1883 void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
1884   PrintExpr(Node->getBase());
1885   OS << "[";
1886   PrintExpr(Node->getIdx());
1887   OS << "]";
1888 }
1889 
1890 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1891   switch (Node->getLiteralOperatorKind()) {
1892   case UserDefinedLiteral::LOK_Raw:
1893     OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
1894     break;
1895   case UserDefinedLiteral::LOK_Template: {
1896     DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1897     const TemplateArgumentList *Args =
1898       cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1899     assert(Args);
1900 
1901     if (Args->size() != 1) {
1902       OS << "operator\"\"" << Node->getUDSuffix()->getName();
1903       TemplateSpecializationType::PrintTemplateArgumentList(
1904           OS, Args->data(), Args->size(), Policy);
1905       OS << "()";
1906       return;
1907     }
1908 
1909     const TemplateArgument &Pack = Args->get(0);
1910     for (const auto &P : Pack.pack_elements()) {
1911       char C = (char)P.getAsIntegral().getZExtValue();
1912       OS << C;
1913     }
1914     break;
1915   }
1916   case UserDefinedLiteral::LOK_Integer: {
1917     // Print integer literal without suffix.
1918     IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1919     OS << Int->getValue().toString(10, /*isSigned*/false);
1920     break;
1921   }
1922   case UserDefinedLiteral::LOK_Floating: {
1923     // Print floating literal without suffix.
1924     FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1925     PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1926     break;
1927   }
1928   case UserDefinedLiteral::LOK_String:
1929   case UserDefinedLiteral::LOK_Character:
1930     PrintExpr(Node->getCookedLiteral());
1931     break;
1932   }
1933   OS << Node->getUDSuffix()->getName();
1934 }
1935 
1936 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1937   OS << (Node->getValue() ? "true" : "false");
1938 }
1939 
1940 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1941   OS << "nullptr";
1942 }
1943 
1944 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1945   OS << "this";
1946 }
1947 
1948 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1949   if (!Node->getSubExpr())
1950     OS << "throw";
1951   else {
1952     OS << "throw ";
1953     PrintExpr(Node->getSubExpr());
1954   }
1955 }
1956 
1957 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1958   // Nothing to print: we picked up the default argument.
1959 }
1960 
1961 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1962   // Nothing to print: we picked up the default initializer.
1963 }
1964 
1965 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1966   Node->getType().print(OS, Policy);
1967   // If there are no parens, this is list-initialization, and the braces are
1968   // part of the syntax of the inner construct.
1969   if (Node->getLParenLoc().isValid())
1970     OS << "(";
1971   PrintExpr(Node->getSubExpr());
1972   if (Node->getLParenLoc().isValid())
1973     OS << ")";
1974 }
1975 
1976 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1977   PrintExpr(Node->getSubExpr());
1978 }
1979 
1980 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1981   Node->getType().print(OS, Policy);
1982   if (Node->isStdInitListInitialization())
1983     /* Nothing to do; braces are part of creating the std::initializer_list. */;
1984   else if (Node->isListInitialization())
1985     OS << "{";
1986   else
1987     OS << "(";
1988   for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1989                                          ArgEnd = Node->arg_end();
1990        Arg != ArgEnd; ++Arg) {
1991     if ((*Arg)->isDefaultArgument())
1992       break;
1993     if (Arg != Node->arg_begin())
1994       OS << ", ";
1995     PrintExpr(*Arg);
1996   }
1997   if (Node->isStdInitListInitialization())
1998     /* See above. */;
1999   else if (Node->isListInitialization())
2000     OS << "}";
2001   else
2002     OS << ")";
2003 }
2004 
2005 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
2006   OS << '[';
2007   bool NeedComma = false;
2008   switch (Node->getCaptureDefault()) {
2009   case LCD_None:
2010     break;
2011 
2012   case LCD_ByCopy:
2013     OS << '=';
2014     NeedComma = true;
2015     break;
2016 
2017   case LCD_ByRef:
2018     OS << '&';
2019     NeedComma = true;
2020     break;
2021   }
2022   for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
2023                                  CEnd = Node->explicit_capture_end();
2024        C != CEnd;
2025        ++C) {
2026     if (NeedComma)
2027       OS << ", ";
2028     NeedComma = true;
2029 
2030     switch (C->getCaptureKind()) {
2031     case LCK_This:
2032       OS << "this";
2033       break;
2034     case LCK_StarThis:
2035       OS << "*this";
2036       break;
2037     case LCK_ByRef:
2038       if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
2039         OS << '&';
2040       OS << C->getCapturedVar()->getName();
2041       break;
2042 
2043     case LCK_ByCopy:
2044       OS << C->getCapturedVar()->getName();
2045       break;
2046     case LCK_VLAType:
2047       llvm_unreachable("VLA type in explicit captures.");
2048     }
2049 
2050     if (Node->isInitCapture(C))
2051       PrintExpr(C->getCapturedVar()->getInit());
2052   }
2053   OS << ']';
2054 
2055   if (Node->hasExplicitParameters()) {
2056     OS << " (";
2057     CXXMethodDecl *Method = Node->getCallOperator();
2058     NeedComma = false;
2059     for (auto P : Method->params()) {
2060       if (NeedComma) {
2061         OS << ", ";
2062       } else {
2063         NeedComma = true;
2064       }
2065       std::string ParamStr = P->getNameAsString();
2066       P->getOriginalType().print(OS, Policy, ParamStr);
2067     }
2068     if (Method->isVariadic()) {
2069       if (NeedComma)
2070         OS << ", ";
2071       OS << "...";
2072     }
2073     OS << ')';
2074 
2075     if (Node->isMutable())
2076       OS << " mutable";
2077 
2078     const FunctionProtoType *Proto
2079       = Method->getType()->getAs<FunctionProtoType>();
2080     Proto->printExceptionSpecification(OS, Policy);
2081 
2082     // FIXME: Attributes
2083 
2084     // Print the trailing return type if it was specified in the source.
2085     if (Node->hasExplicitResultType()) {
2086       OS << " -> ";
2087       Proto->getReturnType().print(OS, Policy);
2088     }
2089   }
2090 
2091   // Print the body.
2092   CompoundStmt *Body = Node->getBody();
2093   OS << ' ';
2094   PrintStmt(Body);
2095 }
2096 
2097 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
2098   if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
2099     TSInfo->getType().print(OS, Policy);
2100   else
2101     Node->getType().print(OS, Policy);
2102   OS << "()";
2103 }
2104 
2105 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
2106   if (E->isGlobalNew())
2107     OS << "::";
2108   OS << "new ";
2109   unsigned NumPlace = E->getNumPlacementArgs();
2110   if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
2111     OS << "(";
2112     PrintExpr(E->getPlacementArg(0));
2113     for (unsigned i = 1; i < NumPlace; ++i) {
2114       if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
2115         break;
2116       OS << ", ";
2117       PrintExpr(E->getPlacementArg(i));
2118     }
2119     OS << ") ";
2120   }
2121   if (E->isParenTypeId())
2122     OS << "(";
2123   std::string TypeS;
2124   if (Expr *Size = E->getArraySize()) {
2125     llvm::raw_string_ostream s(TypeS);
2126     s << '[';
2127     Size->printPretty(s, Helper, Policy);
2128     s << ']';
2129   }
2130   E->getAllocatedType().print(OS, Policy, TypeS);
2131   if (E->isParenTypeId())
2132     OS << ")";
2133 
2134   CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
2135   if (InitStyle) {
2136     if (InitStyle == CXXNewExpr::CallInit)
2137       OS << "(";
2138     PrintExpr(E->getInitializer());
2139     if (InitStyle == CXXNewExpr::CallInit)
2140       OS << ")";
2141   }
2142 }
2143 
2144 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2145   if (E->isGlobalDelete())
2146     OS << "::";
2147   OS << "delete ";
2148   if (E->isArrayForm())
2149     OS << "[] ";
2150   PrintExpr(E->getArgument());
2151 }
2152 
2153 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
2154   PrintExpr(E->getBase());
2155   if (E->isArrow())
2156     OS << "->";
2157   else
2158     OS << '.';
2159   if (E->getQualifier())
2160     E->getQualifier()->print(OS, Policy);
2161   OS << "~";
2162 
2163   if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
2164     OS << II->getName();
2165   else
2166     E->getDestroyedType().print(OS, Policy);
2167 }
2168 
2169 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
2170   if (E->isListInitialization() && !E->isStdInitListInitialization())
2171     OS << "{";
2172 
2173   for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
2174     if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
2175       // Don't print any defaulted arguments
2176       break;
2177     }
2178 
2179     if (i) OS << ", ";
2180     PrintExpr(E->getArg(i));
2181   }
2182 
2183   if (E->isListInitialization() && !E->isStdInitListInitialization())
2184     OS << "}";
2185 }
2186 
2187 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
2188   PrintExpr(E->getSubExpr());
2189 }
2190 
2191 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
2192   // Just forward to the subexpression.
2193   PrintExpr(E->getSubExpr());
2194 }
2195 
2196 void
2197 StmtPrinter::VisitCXXUnresolvedConstructExpr(
2198                                            CXXUnresolvedConstructExpr *Node) {
2199   Node->getTypeAsWritten().print(OS, Policy);
2200   OS << "(";
2201   for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
2202                                              ArgEnd = Node->arg_end();
2203        Arg != ArgEnd; ++Arg) {
2204     if (Arg != Node->arg_begin())
2205       OS << ", ";
2206     PrintExpr(*Arg);
2207   }
2208   OS << ")";
2209 }
2210 
2211 void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2212                                          CXXDependentScopeMemberExpr *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 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
2228   if (!Node->isImplicitAccess()) {
2229     PrintExpr(Node->getBase());
2230     OS << (Node->isArrow() ? "->" : ".");
2231   }
2232   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2233     Qualifier->print(OS, Policy);
2234   if (Node->hasTemplateKeyword())
2235     OS << "template ";
2236   OS << Node->getMemberNameInfo();
2237   if (Node->hasExplicitTemplateArgs())
2238     TemplateSpecializationType::PrintTemplateArgumentList(
2239         OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
2240 }
2241 
2242 static const char *getTypeTraitName(TypeTrait TT) {
2243   switch (TT) {
2244 #define TYPE_TRAIT_1(Spelling, Name, Key) \
2245 case clang::UTT_##Name: return #Spelling;
2246 #define TYPE_TRAIT_2(Spelling, Name, Key) \
2247 case clang::BTT_##Name: return #Spelling;
2248 #define TYPE_TRAIT_N(Spelling, Name, Key) \
2249   case clang::TT_##Name: return #Spelling;
2250 #include "clang/Basic/TokenKinds.def"
2251   }
2252   llvm_unreachable("Type trait not covered by switch");
2253 }
2254 
2255 static const char *getTypeTraitName(ArrayTypeTrait ATT) {
2256   switch (ATT) {
2257   case ATT_ArrayRank:        return "__array_rank";
2258   case ATT_ArrayExtent:      return "__array_extent";
2259   }
2260   llvm_unreachable("Array type trait not covered by switch");
2261 }
2262 
2263 static const char *getExpressionTraitName(ExpressionTrait ET) {
2264   switch (ET) {
2265   case ET_IsLValueExpr:      return "__is_lvalue_expr";
2266   case ET_IsRValueExpr:      return "__is_rvalue_expr";
2267   }
2268   llvm_unreachable("Expression type trait not covered by switch");
2269 }
2270 
2271 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2272   OS << getTypeTraitName(E->getTrait()) << "(";
2273   for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2274     if (I > 0)
2275       OS << ", ";
2276     E->getArg(I)->getType().print(OS, Policy);
2277   }
2278   OS << ")";
2279 }
2280 
2281 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
2282   OS << getTypeTraitName(E->getTrait()) << '(';
2283   E->getQueriedType().print(OS, Policy);
2284   OS << ')';
2285 }
2286 
2287 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
2288   OS << getExpressionTraitName(E->getTrait()) << '(';
2289   PrintExpr(E->getQueriedExpression());
2290   OS << ')';
2291 }
2292 
2293 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2294   OS << "noexcept(";
2295   PrintExpr(E->getOperand());
2296   OS << ")";
2297 }
2298 
2299 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
2300   PrintExpr(E->getPattern());
2301   OS << "...";
2302 }
2303 
2304 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2305   OS << "sizeof...(" << *E->getPack() << ")";
2306 }
2307 
2308 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2309                                        SubstNonTypeTemplateParmPackExpr *Node) {
2310   OS << *Node->getParameterPack();
2311 }
2312 
2313 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2314                                        SubstNonTypeTemplateParmExpr *Node) {
2315   Visit(Node->getReplacement());
2316 }
2317 
2318 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2319   OS << *E->getParameterPack();
2320 }
2321 
2322 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2323   PrintExpr(Node->GetTemporaryExpr());
2324 }
2325 
2326 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2327   OS << "(";
2328   if (E->getLHS()) {
2329     PrintExpr(E->getLHS());
2330     OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2331   }
2332   OS << "...";
2333   if (E->getRHS()) {
2334     OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2335     PrintExpr(E->getRHS());
2336   }
2337   OS << ")";
2338 }
2339 
2340 // C++ Coroutines TS
2341 
2342 void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
2343   Visit(S->getBody());
2344 }
2345 
2346 void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
2347   OS << "co_return";
2348   if (S->getOperand()) {
2349     OS << " ";
2350     Visit(S->getOperand());
2351   }
2352   OS << ";";
2353 }
2354 
2355 void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
2356   OS << "co_await ";
2357   PrintExpr(S->getOperand());
2358 }
2359 
2360 void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
2361   OS << "co_yield ";
2362   PrintExpr(S->getOperand());
2363 }
2364 
2365 // Obj-C
2366 
2367 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2368   OS << "@";
2369   VisitStringLiteral(Node->getString());
2370 }
2371 
2372 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
2373   OS << "@";
2374   Visit(E->getSubExpr());
2375 }
2376 
2377 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2378   OS << "@[ ";
2379   ObjCArrayLiteral::child_range Ch = E->children();
2380   for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2381     if (I != Ch.begin())
2382       OS << ", ";
2383     Visit(*I);
2384   }
2385   OS << " ]";
2386 }
2387 
2388 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2389   OS << "@{ ";
2390   for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2391     if (I > 0)
2392       OS << ", ";
2393 
2394     ObjCDictionaryElement Element = E->getKeyValueElement(I);
2395     Visit(Element.Key);
2396     OS << " : ";
2397     Visit(Element.Value);
2398     if (Element.isPackExpansion())
2399       OS << "...";
2400   }
2401   OS << " }";
2402 }
2403 
2404 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
2405   OS << "@encode(";
2406   Node->getEncodedType().print(OS, Policy);
2407   OS << ')';
2408 }
2409 
2410 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
2411   OS << "@selector(";
2412   Node->getSelector().print(OS);
2413   OS << ')';
2414 }
2415 
2416 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
2417   OS << "@protocol(" << *Node->getProtocol() << ')';
2418 }
2419 
2420 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2421   OS << "[";
2422   switch (Mess->getReceiverKind()) {
2423   case ObjCMessageExpr::Instance:
2424     PrintExpr(Mess->getInstanceReceiver());
2425     break;
2426 
2427   case ObjCMessageExpr::Class:
2428     Mess->getClassReceiver().print(OS, Policy);
2429     break;
2430 
2431   case ObjCMessageExpr::SuperInstance:
2432   case ObjCMessageExpr::SuperClass:
2433     OS << "Super";
2434     break;
2435   }
2436 
2437   OS << ' ';
2438   Selector selector = Mess->getSelector();
2439   if (selector.isUnarySelector()) {
2440     OS << selector.getNameForSlot(0);
2441   } else {
2442     for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
2443       if (i < selector.getNumArgs()) {
2444         if (i > 0) OS << ' ';
2445         if (selector.getIdentifierInfoForSlot(i))
2446           OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
2447         else
2448            OS << ":";
2449       }
2450       else OS << ", "; // Handle variadic methods.
2451 
2452       PrintExpr(Mess->getArg(i));
2453     }
2454   }
2455   OS << "]";
2456 }
2457 
2458 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2459   OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2460 }
2461 
2462 void
2463 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2464   PrintExpr(E->getSubExpr());
2465 }
2466 
2467 void
2468 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
2469   OS << '(' << E->getBridgeKindName();
2470   E->getType().print(OS, Policy);
2471   OS << ')';
2472   PrintExpr(E->getSubExpr());
2473 }
2474 
2475 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
2476   BlockDecl *BD = Node->getBlockDecl();
2477   OS << "^";
2478 
2479   const FunctionType *AFT = Node->getFunctionType();
2480 
2481   if (isa<FunctionNoProtoType>(AFT)) {
2482     OS << "()";
2483   } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
2484     OS << '(';
2485     for (BlockDecl::param_iterator AI = BD->param_begin(),
2486          E = BD->param_end(); AI != E; ++AI) {
2487       if (AI != BD->param_begin()) OS << ", ";
2488       std::string ParamStr = (*AI)->getNameAsString();
2489       (*AI)->getType().print(OS, Policy, ParamStr);
2490     }
2491 
2492     const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
2493     if (FT->isVariadic()) {
2494       if (!BD->param_empty()) OS << ", ";
2495       OS << "...";
2496     }
2497     OS << ')';
2498   }
2499   OS << "{ }";
2500 }
2501 
2502 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2503   PrintExpr(Node->getSourceExpr());
2504 }
2505 
2506 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2507   // TODO: Print something reasonable for a TypoExpr, if necessary.
2508   llvm_unreachable("Cannot print TypoExpr nodes");
2509 }
2510 
2511 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2512   OS << "__builtin_astype(";
2513   PrintExpr(Node->getSrcExpr());
2514   OS << ", ";
2515   Node->getType().print(OS, Policy);
2516   OS << ")";
2517 }
2518 
2519 //===----------------------------------------------------------------------===//
2520 // Stmt method implementations
2521 //===----------------------------------------------------------------------===//
2522 
2523 void Stmt::dumpPretty(const ASTContext &Context) const {
2524   printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
2525 }
2526 
2527 void Stmt::printPretty(raw_ostream &OS,
2528                        PrinterHelper *Helper,
2529                        const PrintingPolicy &Policy,
2530                        unsigned Indentation) const {
2531   StmtPrinter P(OS, Helper, Policy, Indentation);
2532   P.Visit(const_cast<Stmt*>(this));
2533 }
2534 
2535 //===----------------------------------------------------------------------===//
2536 // PrinterHelper
2537 //===----------------------------------------------------------------------===//
2538 
2539 // Implement virtual destructor.
2540 PrinterHelper::~PrinterHelper() {}
2541