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