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 void OMPClausePrinter::VisitOMPDependClause(OMPDependClause *Node) {
804   if (!Node->varlist_empty()) {
805     OS << "depend(";
806     OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
807                                         Node->getDependencyKind())
808        << " :";
809     VisitOMPClauseList(Node, ' ');
810     OS << ")";
811   }
812 }
813 }
814 
815 //===----------------------------------------------------------------------===//
816 //  OpenMP directives printing methods
817 //===----------------------------------------------------------------------===//
818 
819 void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) {
820   OMPClausePrinter Printer(OS, Policy);
821   ArrayRef<OMPClause *> Clauses = S->clauses();
822   for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
823        I != E; ++I)
824     if (*I && !(*I)->isImplicit()) {
825       Printer.Visit(*I);
826       OS << ' ';
827     }
828   OS << "\n";
829   if (S->hasAssociatedStmt() && S->getAssociatedStmt()) {
830     assert(isa<CapturedStmt>(S->getAssociatedStmt()) &&
831            "Expected captured statement!");
832     Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt();
833     PrintStmt(CS);
834   }
835 }
836 
837 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
838   Indent() << "#pragma omp parallel ";
839   PrintOMPExecutableDirective(Node);
840 }
841 
842 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
843   Indent() << "#pragma omp simd ";
844   PrintOMPExecutableDirective(Node);
845 }
846 
847 void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
848   Indent() << "#pragma omp for ";
849   PrintOMPExecutableDirective(Node);
850 }
851 
852 void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
853   Indent() << "#pragma omp for simd ";
854   PrintOMPExecutableDirective(Node);
855 }
856 
857 void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
858   Indent() << "#pragma omp sections ";
859   PrintOMPExecutableDirective(Node);
860 }
861 
862 void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
863   Indent() << "#pragma omp section";
864   PrintOMPExecutableDirective(Node);
865 }
866 
867 void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
868   Indent() << "#pragma omp single ";
869   PrintOMPExecutableDirective(Node);
870 }
871 
872 void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
873   Indent() << "#pragma omp master";
874   PrintOMPExecutableDirective(Node);
875 }
876 
877 void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
878   Indent() << "#pragma omp critical";
879   if (Node->getDirectiveName().getName()) {
880     OS << " (";
881     Node->getDirectiveName().printName(OS);
882     OS << ")";
883   }
884   PrintOMPExecutableDirective(Node);
885 }
886 
887 void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
888   Indent() << "#pragma omp parallel for ";
889   PrintOMPExecutableDirective(Node);
890 }
891 
892 void StmtPrinter::VisitOMPParallelForSimdDirective(
893     OMPParallelForSimdDirective *Node) {
894   Indent() << "#pragma omp parallel for simd ";
895   PrintOMPExecutableDirective(Node);
896 }
897 
898 void StmtPrinter::VisitOMPParallelSectionsDirective(
899     OMPParallelSectionsDirective *Node) {
900   Indent() << "#pragma omp parallel sections ";
901   PrintOMPExecutableDirective(Node);
902 }
903 
904 void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
905   Indent() << "#pragma omp task ";
906   PrintOMPExecutableDirective(Node);
907 }
908 
909 void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
910   Indent() << "#pragma omp taskyield";
911   PrintOMPExecutableDirective(Node);
912 }
913 
914 void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
915   Indent() << "#pragma omp barrier";
916   PrintOMPExecutableDirective(Node);
917 }
918 
919 void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
920   Indent() << "#pragma omp taskwait";
921   PrintOMPExecutableDirective(Node);
922 }
923 
924 void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
925   Indent() << "#pragma omp taskgroup";
926   PrintOMPExecutableDirective(Node);
927 }
928 
929 void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
930   Indent() << "#pragma omp flush ";
931   PrintOMPExecutableDirective(Node);
932 }
933 
934 void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
935   Indent() << "#pragma omp ordered";
936   PrintOMPExecutableDirective(Node);
937 }
938 
939 void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
940   Indent() << "#pragma omp atomic ";
941   PrintOMPExecutableDirective(Node);
942 }
943 
944 void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
945   Indent() << "#pragma omp target ";
946   PrintOMPExecutableDirective(Node);
947 }
948 
949 void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
950   Indent() << "#pragma omp target data ";
951   PrintOMPExecutableDirective(Node);
952 }
953 
954 void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
955   Indent() << "#pragma omp teams ";
956   PrintOMPExecutableDirective(Node);
957 }
958 
959 void StmtPrinter::VisitOMPCancellationPointDirective(
960     OMPCancellationPointDirective *Node) {
961   Indent() << "#pragma omp cancellation point "
962            << getOpenMPDirectiveName(Node->getCancelRegion());
963   PrintOMPExecutableDirective(Node);
964 }
965 
966 void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
967   Indent() << "#pragma omp cancel "
968            << getOpenMPDirectiveName(Node->getCancelRegion());
969   PrintOMPExecutableDirective(Node);
970 }
971 //===----------------------------------------------------------------------===//
972 //  Expr printing methods.
973 //===----------------------------------------------------------------------===//
974 
975 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
976   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
977     Qualifier->print(OS, Policy);
978   if (Node->hasTemplateKeyword())
979     OS << "template ";
980   OS << Node->getNameInfo();
981   if (Node->hasExplicitTemplateArgs())
982     TemplateSpecializationType::PrintTemplateArgumentList(
983         OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
984 }
985 
986 void StmtPrinter::VisitDependentScopeDeclRefExpr(
987                                            DependentScopeDeclRefExpr *Node) {
988   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
989     Qualifier->print(OS, Policy);
990   if (Node->hasTemplateKeyword())
991     OS << "template ";
992   OS << Node->getNameInfo();
993   if (Node->hasExplicitTemplateArgs())
994     TemplateSpecializationType::PrintTemplateArgumentList(
995         OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
996 }
997 
998 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
999   if (Node->getQualifier())
1000     Node->getQualifier()->print(OS, Policy);
1001   if (Node->hasTemplateKeyword())
1002     OS << "template ";
1003   OS << Node->getNameInfo();
1004   if (Node->hasExplicitTemplateArgs())
1005     TemplateSpecializationType::PrintTemplateArgumentList(
1006         OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1007 }
1008 
1009 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
1010   if (Node->getBase()) {
1011     PrintExpr(Node->getBase());
1012     OS << (Node->isArrow() ? "->" : ".");
1013   }
1014   OS << *Node->getDecl();
1015 }
1016 
1017 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
1018   if (Node->isSuperReceiver())
1019     OS << "super.";
1020   else if (Node->isObjectReceiver() && Node->getBase()) {
1021     PrintExpr(Node->getBase());
1022     OS << ".";
1023   } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1024     OS << Node->getClassReceiver()->getName() << ".";
1025   }
1026 
1027   if (Node->isImplicitProperty())
1028     Node->getImplicitPropertyGetter()->getSelector().print(OS);
1029   else
1030     OS << Node->getExplicitProperty()->getName();
1031 }
1032 
1033 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1034 
1035   PrintExpr(Node->getBaseExpr());
1036   OS << "[";
1037   PrintExpr(Node->getKeyExpr());
1038   OS << "]";
1039 }
1040 
1041 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
1042   OS << PredefinedExpr::getIdentTypeName(Node->getIdentType());
1043 }
1044 
1045 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
1046   unsigned value = Node->getValue();
1047 
1048   switch (Node->getKind()) {
1049   case CharacterLiteral::Ascii: break; // no prefix.
1050   case CharacterLiteral::Wide:  OS << 'L'; break;
1051   case CharacterLiteral::UTF16: OS << 'u'; break;
1052   case CharacterLiteral::UTF32: OS << 'U'; break;
1053   }
1054 
1055   switch (value) {
1056   case '\\':
1057     OS << "'\\\\'";
1058     break;
1059   case '\'':
1060     OS << "'\\''";
1061     break;
1062   case '\a':
1063     // TODO: K&R: the meaning of '\\a' is different in traditional C
1064     OS << "'\\a'";
1065     break;
1066   case '\b':
1067     OS << "'\\b'";
1068     break;
1069   // Nonstandard escape sequence.
1070   /*case '\e':
1071     OS << "'\\e'";
1072     break;*/
1073   case '\f':
1074     OS << "'\\f'";
1075     break;
1076   case '\n':
1077     OS << "'\\n'";
1078     break;
1079   case '\r':
1080     OS << "'\\r'";
1081     break;
1082   case '\t':
1083     OS << "'\\t'";
1084     break;
1085   case '\v':
1086     OS << "'\\v'";
1087     break;
1088   default:
1089     if (value < 256 && isPrintable((unsigned char)value))
1090       OS << "'" << (char)value << "'";
1091     else if (value < 256)
1092       OS << "'\\x" << llvm::format("%02x", value) << "'";
1093     else if (value <= 0xFFFF)
1094       OS << "'\\u" << llvm::format("%04x", value) << "'";
1095     else
1096       OS << "'\\U" << llvm::format("%08x", value) << "'";
1097   }
1098 }
1099 
1100 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
1101   bool isSigned = Node->getType()->isSignedIntegerType();
1102   OS << Node->getValue().toString(10, isSigned);
1103 
1104   // Emit suffixes.  Integer literals are always a builtin integer type.
1105   switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1106   default: llvm_unreachable("Unexpected type for integer literal!");
1107   case BuiltinType::Char_S:
1108   case BuiltinType::Char_U:    OS << "i8"; break;
1109   case BuiltinType::UChar:     OS << "Ui8"; break;
1110   case BuiltinType::Short:     OS << "i16"; break;
1111   case BuiltinType::UShort:    OS << "Ui16"; break;
1112   case BuiltinType::Int:       break; // no suffix.
1113   case BuiltinType::UInt:      OS << 'U'; break;
1114   case BuiltinType::Long:      OS << 'L'; break;
1115   case BuiltinType::ULong:     OS << "UL"; break;
1116   case BuiltinType::LongLong:  OS << "LL"; break;
1117   case BuiltinType::ULongLong: OS << "ULL"; break;
1118   }
1119 }
1120 
1121 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1122                                  bool PrintSuffix) {
1123   SmallString<16> Str;
1124   Node->getValue().toString(Str);
1125   OS << Str;
1126   if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1127     OS << '.'; // Trailing dot in order to separate from ints.
1128 
1129   if (!PrintSuffix)
1130     return;
1131 
1132   // Emit suffixes.  Float literals are always a builtin float type.
1133   switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1134   default: llvm_unreachable("Unexpected type for float literal!");
1135   case BuiltinType::Half:       break; // FIXME: suffix?
1136   case BuiltinType::Double:     break; // no suffix.
1137   case BuiltinType::Float:      OS << 'F'; break;
1138   case BuiltinType::LongDouble: OS << 'L'; break;
1139   }
1140 }
1141 
1142 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1143   PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
1144 }
1145 
1146 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1147   PrintExpr(Node->getSubExpr());
1148   OS << "i";
1149 }
1150 
1151 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
1152   Str->outputString(OS);
1153 }
1154 void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1155   OS << "(";
1156   PrintExpr(Node->getSubExpr());
1157   OS << ")";
1158 }
1159 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
1160   if (!Node->isPostfix()) {
1161     OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1162 
1163     // Print a space if this is an "identifier operator" like __real, or if
1164     // it might be concatenated incorrectly like '+'.
1165     switch (Node->getOpcode()) {
1166     default: break;
1167     case UO_Real:
1168     case UO_Imag:
1169     case UO_Extension:
1170       OS << ' ';
1171       break;
1172     case UO_Plus:
1173     case UO_Minus:
1174       if (isa<UnaryOperator>(Node->getSubExpr()))
1175         OS << ' ';
1176       break;
1177     }
1178   }
1179   PrintExpr(Node->getSubExpr());
1180 
1181   if (Node->isPostfix())
1182     OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1183 }
1184 
1185 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1186   OS << "__builtin_offsetof(";
1187   Node->getTypeSourceInfo()->getType().print(OS, Policy);
1188   OS << ", ";
1189   bool PrintedSomething = false;
1190   for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1191     OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
1192     if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
1193       // Array node
1194       OS << "[";
1195       PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1196       OS << "]";
1197       PrintedSomething = true;
1198       continue;
1199     }
1200 
1201     // Skip implicit base indirections.
1202     if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
1203       continue;
1204 
1205     // Field or identifier node.
1206     IdentifierInfo *Id = ON.getFieldName();
1207     if (!Id)
1208       continue;
1209 
1210     if (PrintedSomething)
1211       OS << ".";
1212     else
1213       PrintedSomething = true;
1214     OS << Id->getName();
1215   }
1216   OS << ")";
1217 }
1218 
1219 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
1220   switch(Node->getKind()) {
1221   case UETT_SizeOf:
1222     OS << "sizeof";
1223     break;
1224   case UETT_AlignOf:
1225     if (Policy.LangOpts.CPlusPlus)
1226       OS << "alignof";
1227     else if (Policy.LangOpts.C11)
1228       OS << "_Alignof";
1229     else
1230       OS << "__alignof";
1231     break;
1232   case UETT_VecStep:
1233     OS << "vec_step";
1234     break;
1235   case UETT_OpenMPRequiredSimdAlign:
1236     OS << "__builtin_omp_required_simd_align";
1237     break;
1238   }
1239   if (Node->isArgumentType()) {
1240     OS << '(';
1241     Node->getArgumentType().print(OS, Policy);
1242     OS << ')';
1243   } else {
1244     OS << " ";
1245     PrintExpr(Node->getArgumentExpr());
1246   }
1247 }
1248 
1249 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1250   OS << "_Generic(";
1251   PrintExpr(Node->getControllingExpr());
1252   for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
1253     OS << ", ";
1254     QualType T = Node->getAssocType(i);
1255     if (T.isNull())
1256       OS << "default";
1257     else
1258       T.print(OS, Policy);
1259     OS << ": ";
1260     PrintExpr(Node->getAssocExpr(i));
1261   }
1262   OS << ")";
1263 }
1264 
1265 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
1266   PrintExpr(Node->getLHS());
1267   OS << "[";
1268   PrintExpr(Node->getRHS());
1269   OS << "]";
1270 }
1271 
1272 void StmtPrinter::PrintCallArgs(CallExpr *Call) {
1273   for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
1274     if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1275       // Don't print any defaulted arguments
1276       break;
1277     }
1278 
1279     if (i) OS << ", ";
1280     PrintExpr(Call->getArg(i));
1281   }
1282 }
1283 
1284 void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1285   PrintExpr(Call->getCallee());
1286   OS << "(";
1287   PrintCallArgs(Call);
1288   OS << ")";
1289 }
1290 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1291   // FIXME: Suppress printing implicit bases (like "this")
1292   PrintExpr(Node->getBase());
1293 
1294   MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1295   FieldDecl  *ParentDecl   = ParentMember
1296     ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr;
1297 
1298   if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1299     OS << (Node->isArrow() ? "->" : ".");
1300 
1301   if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1302     if (FD->isAnonymousStructOrUnion())
1303       return;
1304 
1305   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1306     Qualifier->print(OS, Policy);
1307   if (Node->hasTemplateKeyword())
1308     OS << "template ";
1309   OS << Node->getMemberNameInfo();
1310   if (Node->hasExplicitTemplateArgs())
1311     TemplateSpecializationType::PrintTemplateArgumentList(
1312         OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1313 }
1314 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1315   PrintExpr(Node->getBase());
1316   OS << (Node->isArrow() ? "->isa" : ".isa");
1317 }
1318 
1319 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1320   PrintExpr(Node->getBase());
1321   OS << ".";
1322   OS << Node->getAccessor().getName();
1323 }
1324 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1325   OS << '(';
1326   Node->getTypeAsWritten().print(OS, Policy);
1327   OS << ')';
1328   PrintExpr(Node->getSubExpr());
1329 }
1330 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1331   OS << '(';
1332   Node->getType().print(OS, Policy);
1333   OS << ')';
1334   PrintExpr(Node->getInitializer());
1335 }
1336 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1337   // No need to print anything, simply forward to the subexpression.
1338   PrintExpr(Node->getSubExpr());
1339 }
1340 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1341   PrintExpr(Node->getLHS());
1342   OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1343   PrintExpr(Node->getRHS());
1344 }
1345 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1346   PrintExpr(Node->getLHS());
1347   OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1348   PrintExpr(Node->getRHS());
1349 }
1350 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1351   PrintExpr(Node->getCond());
1352   OS << " ? ";
1353   PrintExpr(Node->getLHS());
1354   OS << " : ";
1355   PrintExpr(Node->getRHS());
1356 }
1357 
1358 // GNU extensions.
1359 
1360 void
1361 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1362   PrintExpr(Node->getCommon());
1363   OS << " ?: ";
1364   PrintExpr(Node->getFalseExpr());
1365 }
1366 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1367   OS << "&&" << Node->getLabel()->getName();
1368 }
1369 
1370 void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1371   OS << "(";
1372   PrintRawCompoundStmt(E->getSubStmt());
1373   OS << ")";
1374 }
1375 
1376 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1377   OS << "__builtin_choose_expr(";
1378   PrintExpr(Node->getCond());
1379   OS << ", ";
1380   PrintExpr(Node->getLHS());
1381   OS << ", ";
1382   PrintExpr(Node->getRHS());
1383   OS << ")";
1384 }
1385 
1386 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1387   OS << "__null";
1388 }
1389 
1390 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1391   OS << "__builtin_shufflevector(";
1392   for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1393     if (i) OS << ", ";
1394     PrintExpr(Node->getExpr(i));
1395   }
1396   OS << ")";
1397 }
1398 
1399 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1400   OS << "__builtin_convertvector(";
1401   PrintExpr(Node->getSrcExpr());
1402   OS << ", ";
1403   Node->getType().print(OS, Policy);
1404   OS << ")";
1405 }
1406 
1407 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1408   if (Node->getSyntacticForm()) {
1409     Visit(Node->getSyntacticForm());
1410     return;
1411   }
1412 
1413   OS << "{";
1414   for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1415     if (i) OS << ", ";
1416     if (Node->getInit(i))
1417       PrintExpr(Node->getInit(i));
1418     else
1419       OS << "{}";
1420   }
1421   OS << "}";
1422 }
1423 
1424 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1425   OS << "(";
1426   for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1427     if (i) OS << ", ";
1428     PrintExpr(Node->getExpr(i));
1429   }
1430   OS << ")";
1431 }
1432 
1433 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1434   bool NeedsEquals = true;
1435   for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1436                       DEnd = Node->designators_end();
1437        D != DEnd; ++D) {
1438     if (D->isFieldDesignator()) {
1439       if (D->getDotLoc().isInvalid()) {
1440         if (IdentifierInfo *II = D->getFieldName()) {
1441           OS << II->getName() << ":";
1442           NeedsEquals = false;
1443         }
1444       } else {
1445         OS << "." << D->getFieldName()->getName();
1446       }
1447     } else {
1448       OS << "[";
1449       if (D->isArrayDesignator()) {
1450         PrintExpr(Node->getArrayIndex(*D));
1451       } else {
1452         PrintExpr(Node->getArrayRangeStart(*D));
1453         OS << " ... ";
1454         PrintExpr(Node->getArrayRangeEnd(*D));
1455       }
1456       OS << "]";
1457     }
1458   }
1459 
1460   if (NeedsEquals)
1461     OS << " = ";
1462   else
1463     OS << " ";
1464   PrintExpr(Node->getInit());
1465 }
1466 
1467 void StmtPrinter::VisitDesignatedInitUpdateExpr(
1468     DesignatedInitUpdateExpr *Node) {
1469   OS << "{";
1470   OS << "/*base*/";
1471   PrintExpr(Node->getBase());
1472   OS << ", ";
1473 
1474   OS << "/*updater*/";
1475   PrintExpr(Node->getUpdater());
1476   OS << "}";
1477 }
1478 
1479 void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1480   OS << "/*no init*/";
1481 }
1482 
1483 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1484   if (Policy.LangOpts.CPlusPlus) {
1485     OS << "/*implicit*/";
1486     Node->getType().print(OS, Policy);
1487     OS << "()";
1488   } else {
1489     OS << "/*implicit*/(";
1490     Node->getType().print(OS, Policy);
1491     OS << ')';
1492     if (Node->getType()->isRecordType())
1493       OS << "{}";
1494     else
1495       OS << 0;
1496   }
1497 }
1498 
1499 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1500   OS << "__builtin_va_arg(";
1501   PrintExpr(Node->getSubExpr());
1502   OS << ", ";
1503   Node->getType().print(OS, Policy);
1504   OS << ")";
1505 }
1506 
1507 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1508   PrintExpr(Node->getSyntacticForm());
1509 }
1510 
1511 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1512   const char *Name = nullptr;
1513   switch (Node->getOp()) {
1514 #define BUILTIN(ID, TYPE, ATTRS)
1515 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1516   case AtomicExpr::AO ## ID: \
1517     Name = #ID "("; \
1518     break;
1519 #include "clang/Basic/Builtins.def"
1520   }
1521   OS << Name;
1522 
1523   // AtomicExpr stores its subexpressions in a permuted order.
1524   PrintExpr(Node->getPtr());
1525   if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1526       Node->getOp() != AtomicExpr::AO__atomic_load_n) {
1527     OS << ", ";
1528     PrintExpr(Node->getVal1());
1529   }
1530   if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1531       Node->isCmpXChg()) {
1532     OS << ", ";
1533     PrintExpr(Node->getVal2());
1534   }
1535   if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1536       Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1537     OS << ", ";
1538     PrintExpr(Node->getWeak());
1539   }
1540   if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1541     OS << ", ";
1542     PrintExpr(Node->getOrder());
1543   }
1544   if (Node->isCmpXChg()) {
1545     OS << ", ";
1546     PrintExpr(Node->getOrderFail());
1547   }
1548   OS << ")";
1549 }
1550 
1551 // C++
1552 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1553   const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1554     "",
1555 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1556     Spelling,
1557 #include "clang/Basic/OperatorKinds.def"
1558   };
1559 
1560   OverloadedOperatorKind Kind = Node->getOperator();
1561   if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1562     if (Node->getNumArgs() == 1) {
1563       OS << OpStrings[Kind] << ' ';
1564       PrintExpr(Node->getArg(0));
1565     } else {
1566       PrintExpr(Node->getArg(0));
1567       OS << ' ' << OpStrings[Kind];
1568     }
1569   } else if (Kind == OO_Arrow) {
1570     PrintExpr(Node->getArg(0));
1571   } else if (Kind == OO_Call) {
1572     PrintExpr(Node->getArg(0));
1573     OS << '(';
1574     for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1575       if (ArgIdx > 1)
1576         OS << ", ";
1577       if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1578         PrintExpr(Node->getArg(ArgIdx));
1579     }
1580     OS << ')';
1581   } else if (Kind == OO_Subscript) {
1582     PrintExpr(Node->getArg(0));
1583     OS << '[';
1584     PrintExpr(Node->getArg(1));
1585     OS << ']';
1586   } else if (Node->getNumArgs() == 1) {
1587     OS << OpStrings[Kind] << ' ';
1588     PrintExpr(Node->getArg(0));
1589   } else if (Node->getNumArgs() == 2) {
1590     PrintExpr(Node->getArg(0));
1591     OS << ' ' << OpStrings[Kind] << ' ';
1592     PrintExpr(Node->getArg(1));
1593   } else {
1594     llvm_unreachable("unknown overloaded operator");
1595   }
1596 }
1597 
1598 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1599   // If we have a conversion operator call only print the argument.
1600   CXXMethodDecl *MD = Node->getMethodDecl();
1601   if (MD && isa<CXXConversionDecl>(MD)) {
1602     PrintExpr(Node->getImplicitObjectArgument());
1603     return;
1604   }
1605   VisitCallExpr(cast<CallExpr>(Node));
1606 }
1607 
1608 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1609   PrintExpr(Node->getCallee());
1610   OS << "<<<";
1611   PrintCallArgs(Node->getConfig());
1612   OS << ">>>(";
1613   PrintCallArgs(Node);
1614   OS << ")";
1615 }
1616 
1617 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1618   OS << Node->getCastName() << '<';
1619   Node->getTypeAsWritten().print(OS, Policy);
1620   OS << ">(";
1621   PrintExpr(Node->getSubExpr());
1622   OS << ")";
1623 }
1624 
1625 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1626   VisitCXXNamedCastExpr(Node);
1627 }
1628 
1629 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1630   VisitCXXNamedCastExpr(Node);
1631 }
1632 
1633 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1634   VisitCXXNamedCastExpr(Node);
1635 }
1636 
1637 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1638   VisitCXXNamedCastExpr(Node);
1639 }
1640 
1641 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1642   OS << "typeid(";
1643   if (Node->isTypeOperand()) {
1644     Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1645   } else {
1646     PrintExpr(Node->getExprOperand());
1647   }
1648   OS << ")";
1649 }
1650 
1651 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1652   OS << "__uuidof(";
1653   if (Node->isTypeOperand()) {
1654     Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1655   } else {
1656     PrintExpr(Node->getExprOperand());
1657   }
1658   OS << ")";
1659 }
1660 
1661 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1662   PrintExpr(Node->getBaseExpr());
1663   if (Node->isArrow())
1664     OS << "->";
1665   else
1666     OS << ".";
1667   if (NestedNameSpecifier *Qualifier =
1668       Node->getQualifierLoc().getNestedNameSpecifier())
1669     Qualifier->print(OS, Policy);
1670   OS << Node->getPropertyDecl()->getDeclName();
1671 }
1672 
1673 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1674   switch (Node->getLiteralOperatorKind()) {
1675   case UserDefinedLiteral::LOK_Raw:
1676     OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
1677     break;
1678   case UserDefinedLiteral::LOK_Template: {
1679     DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1680     const TemplateArgumentList *Args =
1681       cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1682     assert(Args);
1683 
1684     if (Args->size() != 1) {
1685       OS << "operator \"\" " << Node->getUDSuffix()->getName();
1686       TemplateSpecializationType::PrintTemplateArgumentList(
1687           OS, Args->data(), Args->size(), Policy);
1688       OS << "()";
1689       return;
1690     }
1691 
1692     const TemplateArgument &Pack = Args->get(0);
1693     for (const auto &P : Pack.pack_elements()) {
1694       char C = (char)P.getAsIntegral().getZExtValue();
1695       OS << C;
1696     }
1697     break;
1698   }
1699   case UserDefinedLiteral::LOK_Integer: {
1700     // Print integer literal without suffix.
1701     IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1702     OS << Int->getValue().toString(10, /*isSigned*/false);
1703     break;
1704   }
1705   case UserDefinedLiteral::LOK_Floating: {
1706     // Print floating literal without suffix.
1707     FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1708     PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1709     break;
1710   }
1711   case UserDefinedLiteral::LOK_String:
1712   case UserDefinedLiteral::LOK_Character:
1713     PrintExpr(Node->getCookedLiteral());
1714     break;
1715   }
1716   OS << Node->getUDSuffix()->getName();
1717 }
1718 
1719 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1720   OS << (Node->getValue() ? "true" : "false");
1721 }
1722 
1723 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1724   OS << "nullptr";
1725 }
1726 
1727 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1728   OS << "this";
1729 }
1730 
1731 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1732   if (!Node->getSubExpr())
1733     OS << "throw";
1734   else {
1735     OS << "throw ";
1736     PrintExpr(Node->getSubExpr());
1737   }
1738 }
1739 
1740 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1741   // Nothing to print: we picked up the default argument.
1742 }
1743 
1744 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1745   // Nothing to print: we picked up the default initializer.
1746 }
1747 
1748 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1749   Node->getType().print(OS, Policy);
1750   // If there are no parens, this is list-initialization, and the braces are
1751   // part of the syntax of the inner construct.
1752   if (Node->getLParenLoc().isValid())
1753     OS << "(";
1754   PrintExpr(Node->getSubExpr());
1755   if (Node->getLParenLoc().isValid())
1756     OS << ")";
1757 }
1758 
1759 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1760   PrintExpr(Node->getSubExpr());
1761 }
1762 
1763 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1764   Node->getType().print(OS, Policy);
1765   if (Node->isStdInitListInitialization())
1766     /* Nothing to do; braces are part of creating the std::initializer_list. */;
1767   else if (Node->isListInitialization())
1768     OS << "{";
1769   else
1770     OS << "(";
1771   for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1772                                          ArgEnd = Node->arg_end();
1773        Arg != ArgEnd; ++Arg) {
1774     if ((*Arg)->isDefaultArgument())
1775       break;
1776     if (Arg != Node->arg_begin())
1777       OS << ", ";
1778     PrintExpr(*Arg);
1779   }
1780   if (Node->isStdInitListInitialization())
1781     /* See above. */;
1782   else if (Node->isListInitialization())
1783     OS << "}";
1784   else
1785     OS << ")";
1786 }
1787 
1788 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1789   OS << '[';
1790   bool NeedComma = false;
1791   switch (Node->getCaptureDefault()) {
1792   case LCD_None:
1793     break;
1794 
1795   case LCD_ByCopy:
1796     OS << '=';
1797     NeedComma = true;
1798     break;
1799 
1800   case LCD_ByRef:
1801     OS << '&';
1802     NeedComma = true;
1803     break;
1804   }
1805   for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1806                                  CEnd = Node->explicit_capture_end();
1807        C != CEnd;
1808        ++C) {
1809     if (NeedComma)
1810       OS << ", ";
1811     NeedComma = true;
1812 
1813     switch (C->getCaptureKind()) {
1814     case LCK_This:
1815       OS << "this";
1816       break;
1817 
1818     case LCK_ByRef:
1819       if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
1820         OS << '&';
1821       OS << C->getCapturedVar()->getName();
1822       break;
1823 
1824     case LCK_ByCopy:
1825       OS << C->getCapturedVar()->getName();
1826       break;
1827     case LCK_VLAType:
1828       llvm_unreachable("VLA type in explicit captures.");
1829     }
1830 
1831     if (Node->isInitCapture(C))
1832       PrintExpr(C->getCapturedVar()->getInit());
1833   }
1834   OS << ']';
1835 
1836   if (Node->hasExplicitParameters()) {
1837     OS << " (";
1838     CXXMethodDecl *Method = Node->getCallOperator();
1839     NeedComma = false;
1840     for (auto P : Method->params()) {
1841       if (NeedComma) {
1842         OS << ", ";
1843       } else {
1844         NeedComma = true;
1845       }
1846       std::string ParamStr = P->getNameAsString();
1847       P->getOriginalType().print(OS, Policy, ParamStr);
1848     }
1849     if (Method->isVariadic()) {
1850       if (NeedComma)
1851         OS << ", ";
1852       OS << "...";
1853     }
1854     OS << ')';
1855 
1856     if (Node->isMutable())
1857       OS << " mutable";
1858 
1859     const FunctionProtoType *Proto
1860       = Method->getType()->getAs<FunctionProtoType>();
1861     Proto->printExceptionSpecification(OS, Policy);
1862 
1863     // FIXME: Attributes
1864 
1865     // Print the trailing return type if it was specified in the source.
1866     if (Node->hasExplicitResultType()) {
1867       OS << " -> ";
1868       Proto->getReturnType().print(OS, Policy);
1869     }
1870   }
1871 
1872   // Print the body.
1873   CompoundStmt *Body = Node->getBody();
1874   OS << ' ';
1875   PrintStmt(Body);
1876 }
1877 
1878 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
1879   if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1880     TSInfo->getType().print(OS, Policy);
1881   else
1882     Node->getType().print(OS, Policy);
1883   OS << "()";
1884 }
1885 
1886 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1887   if (E->isGlobalNew())
1888     OS << "::";
1889   OS << "new ";
1890   unsigned NumPlace = E->getNumPlacementArgs();
1891   if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
1892     OS << "(";
1893     PrintExpr(E->getPlacementArg(0));
1894     for (unsigned i = 1; i < NumPlace; ++i) {
1895       if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1896         break;
1897       OS << ", ";
1898       PrintExpr(E->getPlacementArg(i));
1899     }
1900     OS << ") ";
1901   }
1902   if (E->isParenTypeId())
1903     OS << "(";
1904   std::string TypeS;
1905   if (Expr *Size = E->getArraySize()) {
1906     llvm::raw_string_ostream s(TypeS);
1907     s << '[';
1908     Size->printPretty(s, Helper, Policy);
1909     s << ']';
1910   }
1911   E->getAllocatedType().print(OS, Policy, TypeS);
1912   if (E->isParenTypeId())
1913     OS << ")";
1914 
1915   CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1916   if (InitStyle) {
1917     if (InitStyle == CXXNewExpr::CallInit)
1918       OS << "(";
1919     PrintExpr(E->getInitializer());
1920     if (InitStyle == CXXNewExpr::CallInit)
1921       OS << ")";
1922   }
1923 }
1924 
1925 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1926   if (E->isGlobalDelete())
1927     OS << "::";
1928   OS << "delete ";
1929   if (E->isArrayForm())
1930     OS << "[] ";
1931   PrintExpr(E->getArgument());
1932 }
1933 
1934 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1935   PrintExpr(E->getBase());
1936   if (E->isArrow())
1937     OS << "->";
1938   else
1939     OS << '.';
1940   if (E->getQualifier())
1941     E->getQualifier()->print(OS, Policy);
1942   OS << "~";
1943 
1944   if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1945     OS << II->getName();
1946   else
1947     E->getDestroyedType().print(OS, Policy);
1948 }
1949 
1950 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1951   if (E->isListInitialization() && !E->isStdInitListInitialization())
1952     OS << "{";
1953 
1954   for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1955     if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1956       // Don't print any defaulted arguments
1957       break;
1958     }
1959 
1960     if (i) OS << ", ";
1961     PrintExpr(E->getArg(i));
1962   }
1963 
1964   if (E->isListInitialization() && !E->isStdInitListInitialization())
1965     OS << "}";
1966 }
1967 
1968 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
1969   PrintExpr(E->getSubExpr());
1970 }
1971 
1972 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
1973   // Just forward to the subexpression.
1974   PrintExpr(E->getSubExpr());
1975 }
1976 
1977 void
1978 StmtPrinter::VisitCXXUnresolvedConstructExpr(
1979                                            CXXUnresolvedConstructExpr *Node) {
1980   Node->getTypeAsWritten().print(OS, Policy);
1981   OS << "(";
1982   for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
1983                                              ArgEnd = Node->arg_end();
1984        Arg != ArgEnd; ++Arg) {
1985     if (Arg != Node->arg_begin())
1986       OS << ", ";
1987     PrintExpr(*Arg);
1988   }
1989   OS << ")";
1990 }
1991 
1992 void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1993                                          CXXDependentScopeMemberExpr *Node) {
1994   if (!Node->isImplicitAccess()) {
1995     PrintExpr(Node->getBase());
1996     OS << (Node->isArrow() ? "->" : ".");
1997   }
1998   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1999     Qualifier->print(OS, Policy);
2000   if (Node->hasTemplateKeyword())
2001     OS << "template ";
2002   OS << Node->getMemberNameInfo();
2003   if (Node->hasExplicitTemplateArgs())
2004     TemplateSpecializationType::PrintTemplateArgumentList(
2005         OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
2006 }
2007 
2008 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
2009   if (!Node->isImplicitAccess()) {
2010     PrintExpr(Node->getBase());
2011     OS << (Node->isArrow() ? "->" : ".");
2012   }
2013   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2014     Qualifier->print(OS, Policy);
2015   if (Node->hasTemplateKeyword())
2016     OS << "template ";
2017   OS << Node->getMemberNameInfo();
2018   if (Node->hasExplicitTemplateArgs())
2019     TemplateSpecializationType::PrintTemplateArgumentList(
2020         OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
2021 }
2022 
2023 static const char *getTypeTraitName(TypeTrait TT) {
2024   switch (TT) {
2025 #define TYPE_TRAIT_1(Spelling, Name, Key) \
2026 case clang::UTT_##Name: return #Spelling;
2027 #define TYPE_TRAIT_2(Spelling, Name, Key) \
2028 case clang::BTT_##Name: return #Spelling;
2029 #define TYPE_TRAIT_N(Spelling, Name, Key) \
2030   case clang::TT_##Name: return #Spelling;
2031 #include "clang/Basic/TokenKinds.def"
2032   }
2033   llvm_unreachable("Type trait not covered by switch");
2034 }
2035 
2036 static const char *getTypeTraitName(ArrayTypeTrait ATT) {
2037   switch (ATT) {
2038   case ATT_ArrayRank:        return "__array_rank";
2039   case ATT_ArrayExtent:      return "__array_extent";
2040   }
2041   llvm_unreachable("Array type trait not covered by switch");
2042 }
2043 
2044 static const char *getExpressionTraitName(ExpressionTrait ET) {
2045   switch (ET) {
2046   case ET_IsLValueExpr:      return "__is_lvalue_expr";
2047   case ET_IsRValueExpr:      return "__is_rvalue_expr";
2048   }
2049   llvm_unreachable("Expression type trait not covered by switch");
2050 }
2051 
2052 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2053   OS << getTypeTraitName(E->getTrait()) << "(";
2054   for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2055     if (I > 0)
2056       OS << ", ";
2057     E->getArg(I)->getType().print(OS, Policy);
2058   }
2059   OS << ")";
2060 }
2061 
2062 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
2063   OS << getTypeTraitName(E->getTrait()) << '(';
2064   E->getQueriedType().print(OS, Policy);
2065   OS << ')';
2066 }
2067 
2068 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
2069   OS << getExpressionTraitName(E->getTrait()) << '(';
2070   PrintExpr(E->getQueriedExpression());
2071   OS << ')';
2072 }
2073 
2074 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2075   OS << "noexcept(";
2076   PrintExpr(E->getOperand());
2077   OS << ")";
2078 }
2079 
2080 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
2081   PrintExpr(E->getPattern());
2082   OS << "...";
2083 }
2084 
2085 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2086   OS << "sizeof...(" << *E->getPack() << ")";
2087 }
2088 
2089 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2090                                        SubstNonTypeTemplateParmPackExpr *Node) {
2091   OS << *Node->getParameterPack();
2092 }
2093 
2094 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2095                                        SubstNonTypeTemplateParmExpr *Node) {
2096   Visit(Node->getReplacement());
2097 }
2098 
2099 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2100   OS << *E->getParameterPack();
2101 }
2102 
2103 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2104   PrintExpr(Node->GetTemporaryExpr());
2105 }
2106 
2107 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2108   OS << "(";
2109   if (E->getLHS()) {
2110     PrintExpr(E->getLHS());
2111     OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2112   }
2113   OS << "...";
2114   if (E->getRHS()) {
2115     OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2116     PrintExpr(E->getRHS());
2117   }
2118   OS << ")";
2119 }
2120 
2121 // Obj-C
2122 
2123 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2124   OS << "@";
2125   VisitStringLiteral(Node->getString());
2126 }
2127 
2128 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
2129   OS << "@";
2130   Visit(E->getSubExpr());
2131 }
2132 
2133 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2134   OS << "@[ ";
2135   ObjCArrayLiteral::child_range Ch = E->children();
2136   for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2137     if (I != Ch.begin())
2138       OS << ", ";
2139     Visit(*I);
2140   }
2141   OS << " ]";
2142 }
2143 
2144 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2145   OS << "@{ ";
2146   for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2147     if (I > 0)
2148       OS << ", ";
2149 
2150     ObjCDictionaryElement Element = E->getKeyValueElement(I);
2151     Visit(Element.Key);
2152     OS << " : ";
2153     Visit(Element.Value);
2154     if (Element.isPackExpansion())
2155       OS << "...";
2156   }
2157   OS << " }";
2158 }
2159 
2160 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
2161   OS << "@encode(";
2162   Node->getEncodedType().print(OS, Policy);
2163   OS << ')';
2164 }
2165 
2166 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
2167   OS << "@selector(";
2168   Node->getSelector().print(OS);
2169   OS << ')';
2170 }
2171 
2172 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
2173   OS << "@protocol(" << *Node->getProtocol() << ')';
2174 }
2175 
2176 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2177   OS << "[";
2178   switch (Mess->getReceiverKind()) {
2179   case ObjCMessageExpr::Instance:
2180     PrintExpr(Mess->getInstanceReceiver());
2181     break;
2182 
2183   case ObjCMessageExpr::Class:
2184     Mess->getClassReceiver().print(OS, Policy);
2185     break;
2186 
2187   case ObjCMessageExpr::SuperInstance:
2188   case ObjCMessageExpr::SuperClass:
2189     OS << "Super";
2190     break;
2191   }
2192 
2193   OS << ' ';
2194   Selector selector = Mess->getSelector();
2195   if (selector.isUnarySelector()) {
2196     OS << selector.getNameForSlot(0);
2197   } else {
2198     for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
2199       if (i < selector.getNumArgs()) {
2200         if (i > 0) OS << ' ';
2201         if (selector.getIdentifierInfoForSlot(i))
2202           OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
2203         else
2204            OS << ":";
2205       }
2206       else OS << ", "; // Handle variadic methods.
2207 
2208       PrintExpr(Mess->getArg(i));
2209     }
2210   }
2211   OS << "]";
2212 }
2213 
2214 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2215   OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2216 }
2217 
2218 void
2219 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2220   PrintExpr(E->getSubExpr());
2221 }
2222 
2223 void
2224 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
2225   OS << '(' << E->getBridgeKindName();
2226   E->getType().print(OS, Policy);
2227   OS << ')';
2228   PrintExpr(E->getSubExpr());
2229 }
2230 
2231 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
2232   BlockDecl *BD = Node->getBlockDecl();
2233   OS << "^";
2234 
2235   const FunctionType *AFT = Node->getFunctionType();
2236 
2237   if (isa<FunctionNoProtoType>(AFT)) {
2238     OS << "()";
2239   } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
2240     OS << '(';
2241     for (BlockDecl::param_iterator AI = BD->param_begin(),
2242          E = BD->param_end(); AI != E; ++AI) {
2243       if (AI != BD->param_begin()) OS << ", ";
2244       std::string ParamStr = (*AI)->getNameAsString();
2245       (*AI)->getType().print(OS, Policy, ParamStr);
2246     }
2247 
2248     const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
2249     if (FT->isVariadic()) {
2250       if (!BD->param_empty()) OS << ", ";
2251       OS << "...";
2252     }
2253     OS << ')';
2254   }
2255   OS << "{ }";
2256 }
2257 
2258 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2259   PrintExpr(Node->getSourceExpr());
2260 }
2261 
2262 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2263   // TODO: Print something reasonable for a TypoExpr, if necessary.
2264   assert(false && "Cannot print TypoExpr nodes");
2265 }
2266 
2267 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2268   OS << "__builtin_astype(";
2269   PrintExpr(Node->getSrcExpr());
2270   OS << ", ";
2271   Node->getType().print(OS, Policy);
2272   OS << ")";
2273 }
2274 
2275 //===----------------------------------------------------------------------===//
2276 // Stmt method implementations
2277 //===----------------------------------------------------------------------===//
2278 
2279 void Stmt::dumpPretty(const ASTContext &Context) const {
2280   printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
2281 }
2282 
2283 void Stmt::printPretty(raw_ostream &OS,
2284                        PrinterHelper *Helper,
2285                        const PrintingPolicy &Policy,
2286                        unsigned Indentation) const {
2287   StmtPrinter P(OS, Helper, Policy, Indentation);
2288   P.Visit(const_cast<Stmt*>(this));
2289 }
2290 
2291 //===----------------------------------------------------------------------===//
2292 // PrinterHelper
2293 //===----------------------------------------------------------------------===//
2294 
2295 // Implement virtual destructor.
2296 PrinterHelper::~PrinterHelper() {}
2297