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