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