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/StmtVisitor.h"
16 #include "clang/AST/DeclCXX.h"
17 #include "clang/AST/DeclObjC.h"
18 #include "clang/AST/DeclTemplate.h"
19 #include "clang/AST/PrettyPrinter.h"
20 #include "llvm/Support/Format.h"
21 #include "clang/AST/Expr.h"
22 #include "clang/AST/ExprCXX.h"
23 using namespace clang;
24 
25 //===----------------------------------------------------------------------===//
26 // StmtPrinter Visitor
27 //===----------------------------------------------------------------------===//
28 
29 namespace  {
30   class StmtPrinter : public StmtVisitor<StmtPrinter> {
31     raw_ostream &OS;
32     ASTContext &Context;
33     unsigned IndentLevel;
34     clang::PrinterHelper* Helper;
35     PrintingPolicy Policy;
36 
37   public:
38     StmtPrinter(raw_ostream &os, ASTContext &C, PrinterHelper* helper,
39                 const PrintingPolicy &Policy,
40                 unsigned Indentation = 0)
41       : OS(os), Context(C), IndentLevel(Indentation), Helper(helper),
42         Policy(Policy) {}
43 
44     void PrintStmt(Stmt *S) {
45       PrintStmt(S, Policy.Indentation);
46     }
47 
48     void PrintStmt(Stmt *S, int SubIndent) {
49       IndentLevel += SubIndent;
50       if (S && isa<Expr>(S)) {
51         // If this is an expr used in a stmt context, indent and newline it.
52         Indent();
53         Visit(S);
54         OS << ";\n";
55       } else if (S) {
56         Visit(S);
57       } else {
58         Indent() << "<<<NULL STATEMENT>>>\n";
59       }
60       IndentLevel -= SubIndent;
61     }
62 
63     void PrintRawCompoundStmt(CompoundStmt *S);
64     void PrintRawDecl(Decl *D);
65     void PrintRawDeclStmt(DeclStmt *S);
66     void PrintRawIfStmt(IfStmt *If);
67     void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
68     void PrintCallArgs(CallExpr *E);
69     void PrintRawSEHExceptHandler(SEHExceptStmt *S);
70     void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
71 
72     void PrintExpr(Expr *E) {
73       if (E)
74         Visit(E);
75       else
76         OS << "<null expr>";
77     }
78 
79     raw_ostream &Indent(int Delta = 0) {
80       for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
81         OS << "  ";
82       return OS;
83     }
84 
85     void Visit(Stmt* S) {
86       if (Helper && Helper->handledStmt(S,OS))
87           return;
88       else StmtVisitor<StmtPrinter>::Visit(S);
89     }
90 
91     void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
92       Indent() << "<<unknown stmt type>>\n";
93     }
94     void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
95       OS << "<<unknown expr type>>";
96     }
97     void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
98 
99 #define ABSTRACT_STMT(CLASS)
100 #define STMT(CLASS, PARENT) \
101     void Visit##CLASS(CLASS *Node);
102 #include "clang/AST/StmtNodes.inc"
103   };
104 }
105 
106 //===----------------------------------------------------------------------===//
107 //  Stmt printing methods.
108 //===----------------------------------------------------------------------===//
109 
110 /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
111 /// with no newline after the }.
112 void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
113   OS << "{\n";
114   for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
115        I != E; ++I)
116     PrintStmt(*I);
117 
118   Indent() << "}";
119 }
120 
121 void StmtPrinter::PrintRawDecl(Decl *D) {
122   D->print(OS, Policy, IndentLevel);
123 }
124 
125 void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
126   DeclStmt::decl_iterator Begin = S->decl_begin(), End = S->decl_end();
127   SmallVector<Decl*, 2> Decls;
128   for ( ; Begin != End; ++Begin)
129     Decls.push_back(*Begin);
130 
131   Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
132 }
133 
134 void StmtPrinter::VisitNullStmt(NullStmt *Node) {
135   Indent() << ";\n";
136 }
137 
138 void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
139   Indent();
140   PrintRawDeclStmt(Node);
141   OS << ";\n";
142 }
143 
144 void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
145   Indent();
146   PrintRawCompoundStmt(Node);
147   OS << "\n";
148 }
149 
150 void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
151   Indent(-1) << "case ";
152   PrintExpr(Node->getLHS());
153   if (Node->getRHS()) {
154     OS << " ... ";
155     PrintExpr(Node->getRHS());
156   }
157   OS << ":\n";
158 
159   PrintStmt(Node->getSubStmt(), 0);
160 }
161 
162 void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
163   Indent(-1) << "default:\n";
164   PrintStmt(Node->getSubStmt(), 0);
165 }
166 
167 void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
168   Indent(-1) << Node->getName() << ":\n";
169   PrintStmt(Node->getSubStmt(), 0);
170 }
171 
172 void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
173   OS << "if (";
174   PrintExpr(If->getCond());
175   OS << ')';
176 
177   if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
178     OS << ' ';
179     PrintRawCompoundStmt(CS);
180     OS << (If->getElse() ? ' ' : '\n');
181   } else {
182     OS << '\n';
183     PrintStmt(If->getThen());
184     if (If->getElse()) Indent();
185   }
186 
187   if (Stmt *Else = If->getElse()) {
188     OS << "else";
189 
190     if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
191       OS << ' ';
192       PrintRawCompoundStmt(CS);
193       OS << '\n';
194     } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
195       OS << ' ';
196       PrintRawIfStmt(ElseIf);
197     } else {
198       OS << '\n';
199       PrintStmt(If->getElse());
200     }
201   }
202 }
203 
204 void StmtPrinter::VisitIfStmt(IfStmt *If) {
205   Indent();
206   PrintRawIfStmt(If);
207 }
208 
209 void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
210   Indent() << "switch (";
211   PrintExpr(Node->getCond());
212   OS << ")";
213 
214   // Pretty print compoundstmt bodies (very common).
215   if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
216     OS << " ";
217     PrintRawCompoundStmt(CS);
218     OS << "\n";
219   } else {
220     OS << "\n";
221     PrintStmt(Node->getBody());
222   }
223 }
224 
225 void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
226   Indent() << "while (";
227   PrintExpr(Node->getCond());
228   OS << ")\n";
229   PrintStmt(Node->getBody());
230 }
231 
232 void StmtPrinter::VisitDoStmt(DoStmt *Node) {
233   Indent() << "do ";
234   if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
235     PrintRawCompoundStmt(CS);
236     OS << " ";
237   } else {
238     OS << "\n";
239     PrintStmt(Node->getBody());
240     Indent();
241   }
242 
243   OS << "while (";
244   PrintExpr(Node->getCond());
245   OS << ");\n";
246 }
247 
248 void StmtPrinter::VisitForStmt(ForStmt *Node) {
249   Indent() << "for (";
250   if (Node->getInit()) {
251     if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
252       PrintRawDeclStmt(DS);
253     else
254       PrintExpr(cast<Expr>(Node->getInit()));
255   }
256   OS << ";";
257   if (Node->getCond()) {
258     OS << " ";
259     PrintExpr(Node->getCond());
260   }
261   OS << ";";
262   if (Node->getInc()) {
263     OS << " ";
264     PrintExpr(Node->getInc());
265   }
266   OS << ") ";
267 
268   if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
269     PrintRawCompoundStmt(CS);
270     OS << "\n";
271   } else {
272     OS << "\n";
273     PrintStmt(Node->getBody());
274   }
275 }
276 
277 void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
278   Indent() << "for (";
279   if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
280     PrintRawDeclStmt(DS);
281   else
282     PrintExpr(cast<Expr>(Node->getElement()));
283   OS << " in ";
284   PrintExpr(Node->getCollection());
285   OS << ") ";
286 
287   if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
288     PrintRawCompoundStmt(CS);
289     OS << "\n";
290   } else {
291     OS << "\n";
292     PrintStmt(Node->getBody());
293   }
294 }
295 
296 void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
297   Indent() << "for (";
298   PrintingPolicy SubPolicy(Policy);
299   SubPolicy.SuppressInitializers = true;
300   Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
301   OS << " : ";
302   PrintExpr(Node->getRangeInit());
303   OS << ") {\n";
304   PrintStmt(Node->getBody());
305   Indent() << "}\n";
306 }
307 
308 void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
309   Indent();
310   if (Node->isIfExists())
311     OS << "__if_exists (";
312   else
313     OS << "__if_not_exists (";
314 
315   if (NestedNameSpecifier *Qualifier
316         = Node->getQualifierLoc().getNestedNameSpecifier())
317     Qualifier->print(OS, Policy);
318 
319   OS << Node->getNameInfo() << ") ";
320 
321   PrintRawCompoundStmt(Node->getSubStmt());
322 }
323 
324 void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
325   Indent() << "goto " << Node->getLabel()->getName() << ";\n";
326 }
327 
328 void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
329   Indent() << "goto *";
330   PrintExpr(Node->getTarget());
331   OS << ";\n";
332 }
333 
334 void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
335   Indent() << "continue;\n";
336 }
337 
338 void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
339   Indent() << "break;\n";
340 }
341 
342 
343 void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
344   Indent() << "return";
345   if (Node->getRetValue()) {
346     OS << " ";
347     PrintExpr(Node->getRetValue());
348   }
349   OS << ";\n";
350 }
351 
352 
353 void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
354   Indent() << "asm ";
355 
356   if (Node->isVolatile())
357     OS << "volatile ";
358 
359   OS << "(";
360   VisitStringLiteral(Node->getAsmString());
361 
362   // Outputs
363   if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
364       Node->getNumClobbers() != 0)
365     OS << " : ";
366 
367   for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
368     if (i != 0)
369       OS << ", ";
370 
371     if (!Node->getOutputName(i).empty()) {
372       OS << '[';
373       OS << Node->getOutputName(i);
374       OS << "] ";
375     }
376 
377     VisitStringLiteral(Node->getOutputConstraintLiteral(i));
378     OS << " ";
379     Visit(Node->getOutputExpr(i));
380   }
381 
382   // Inputs
383   if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
384     OS << " : ";
385 
386   for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
387     if (i != 0)
388       OS << ", ";
389 
390     if (!Node->getInputName(i).empty()) {
391       OS << '[';
392       OS << Node->getInputName(i);
393       OS << "] ";
394     }
395 
396     VisitStringLiteral(Node->getInputConstraintLiteral(i));
397     OS << " ";
398     Visit(Node->getInputExpr(i));
399   }
400 
401   // Clobbers
402   if (Node->getNumClobbers() != 0)
403     OS << " : ";
404 
405   for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
406     if (i != 0)
407       OS << ", ";
408 
409     VisitStringLiteral(Node->getClobber(i));
410   }
411 
412   OS << ");\n";
413 }
414 
415 void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
416   Indent() << "@try";
417   if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
418     PrintRawCompoundStmt(TS);
419     OS << "\n";
420   }
421 
422   for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
423     ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
424     Indent() << "@catch(";
425     if (catchStmt->getCatchParamDecl()) {
426       if (Decl *DS = catchStmt->getCatchParamDecl())
427         PrintRawDecl(DS);
428     }
429     OS << ")";
430     if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
431       PrintRawCompoundStmt(CS);
432       OS << "\n";
433     }
434   }
435 
436   if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
437         Node->getFinallyStmt())) {
438     Indent() << "@finally";
439     PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
440     OS << "\n";
441   }
442 }
443 
444 void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
445 }
446 
447 void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
448   Indent() << "@catch (...) { /* todo */ } \n";
449 }
450 
451 void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
452   Indent() << "@throw";
453   if (Node->getThrowExpr()) {
454     OS << " ";
455     PrintExpr(Node->getThrowExpr());
456   }
457   OS << ";\n";
458 }
459 
460 void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
461   Indent() << "@synchronized (";
462   PrintExpr(Node->getSynchExpr());
463   OS << ")";
464   PrintRawCompoundStmt(Node->getSynchBody());
465   OS << "\n";
466 }
467 
468 void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
469   Indent() << "@autoreleasepool";
470   PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
471   OS << "\n";
472 }
473 
474 void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
475   OS << "catch (";
476   if (Decl *ExDecl = Node->getExceptionDecl())
477     PrintRawDecl(ExDecl);
478   else
479     OS << "...";
480   OS << ") ";
481   PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
482 }
483 
484 void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
485   Indent();
486   PrintRawCXXCatchStmt(Node);
487   OS << "\n";
488 }
489 
490 void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
491   Indent() << "try ";
492   PrintRawCompoundStmt(Node->getTryBlock());
493   for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
494     OS << " ";
495     PrintRawCXXCatchStmt(Node->getHandler(i));
496   }
497   OS << "\n";
498 }
499 
500 void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
501   Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
502   PrintRawCompoundStmt(Node->getTryBlock());
503   SEHExceptStmt *E = Node->getExceptHandler();
504   SEHFinallyStmt *F = Node->getFinallyHandler();
505   if(E)
506     PrintRawSEHExceptHandler(E);
507   else {
508     assert(F && "Must have a finally block...");
509     PrintRawSEHFinallyStmt(F);
510   }
511   OS << "\n";
512 }
513 
514 void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
515   OS << "__finally ";
516   PrintRawCompoundStmt(Node->getBlock());
517   OS << "\n";
518 }
519 
520 void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
521   OS << "__except (";
522   VisitExpr(Node->getFilterExpr());
523   OS << ")\n";
524   PrintRawCompoundStmt(Node->getBlock());
525   OS << "\n";
526 }
527 
528 void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
529   Indent();
530   PrintRawSEHExceptHandler(Node);
531   OS << "\n";
532 }
533 
534 void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
535   Indent();
536   PrintRawSEHFinallyStmt(Node);
537   OS << "\n";
538 }
539 
540 //===----------------------------------------------------------------------===//
541 //  Expr printing methods.
542 //===----------------------------------------------------------------------===//
543 
544 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
545   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
546     Qualifier->print(OS, Policy);
547   OS << Node->getNameInfo();
548   if (Node->hasExplicitTemplateArgs())
549     OS << TemplateSpecializationType::PrintTemplateArgumentList(
550                                                     Node->getTemplateArgs(),
551                                                     Node->getNumTemplateArgs(),
552                                                     Policy);
553 }
554 
555 void StmtPrinter::VisitDependentScopeDeclRefExpr(
556                                            DependentScopeDeclRefExpr *Node) {
557   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
558     Qualifier->print(OS, Policy);
559   OS << Node->getNameInfo();
560   if (Node->hasExplicitTemplateArgs())
561     OS << TemplateSpecializationType::PrintTemplateArgumentList(
562                                                    Node->getTemplateArgs(),
563                                                    Node->getNumTemplateArgs(),
564                                                    Policy);
565 }
566 
567 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
568   if (Node->getQualifier())
569     Node->getQualifier()->print(OS, Policy);
570   OS << Node->getNameInfo();
571   if (Node->hasExplicitTemplateArgs())
572     OS << TemplateSpecializationType::PrintTemplateArgumentList(
573                                                    Node->getTemplateArgs(),
574                                                    Node->getNumTemplateArgs(),
575                                                    Policy);
576 }
577 
578 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
579   if (Node->getBase()) {
580     PrintExpr(Node->getBase());
581     OS << (Node->isArrow() ? "->" : ".");
582   }
583   OS << *Node->getDecl();
584 }
585 
586 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
587   if (Node->isSuperReceiver())
588     OS << "super.";
589   else if (Node->getBase()) {
590     PrintExpr(Node->getBase());
591     OS << ".";
592   }
593 
594   if (Node->isImplicitProperty())
595     OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
596   else
597     OS << Node->getExplicitProperty()->getName();
598 }
599 
600 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
601   switch (Node->getIdentType()) {
602     default:
603       llvm_unreachable("unknown case");
604     case PredefinedExpr::Func:
605       OS << "__func__";
606       break;
607     case PredefinedExpr::Function:
608       OS << "__FUNCTION__";
609       break;
610     case PredefinedExpr::PrettyFunction:
611       OS << "__PRETTY_FUNCTION__";
612       break;
613   }
614 }
615 
616 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
617   unsigned value = Node->getValue();
618 
619   switch (Node->getKind()) {
620   case CharacterLiteral::Ascii: break; // no prefix.
621   case CharacterLiteral::Wide:  OS << 'L'; break;
622   case CharacterLiteral::UTF16: OS << 'u'; break;
623   case CharacterLiteral::UTF32: OS << 'U'; break;
624   }
625 
626   switch (value) {
627   case '\\':
628     OS << "'\\\\'";
629     break;
630   case '\'':
631     OS << "'\\''";
632     break;
633   case '\a':
634     // TODO: K&R: the meaning of '\\a' is different in traditional C
635     OS << "'\\a'";
636     break;
637   case '\b':
638     OS << "'\\b'";
639     break;
640   // Nonstandard escape sequence.
641   /*case '\e':
642     OS << "'\\e'";
643     break;*/
644   case '\f':
645     OS << "'\\f'";
646     break;
647   case '\n':
648     OS << "'\\n'";
649     break;
650   case '\r':
651     OS << "'\\r'";
652     break;
653   case '\t':
654     OS << "'\\t'";
655     break;
656   case '\v':
657     OS << "'\\v'";
658     break;
659   default:
660     if (value < 256 && isprint(value)) {
661       OS << "'" << (char)value << "'";
662     } else if (value < 256) {
663       OS << "'\\x" << llvm::format("%x", value) << "'";
664     } else {
665       // FIXME what to really do here?
666       OS << value;
667     }
668   }
669 }
670 
671 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
672   bool isSigned = Node->getType()->isSignedIntegerType();
673   OS << Node->getValue().toString(10, isSigned);
674 
675   // Emit suffixes.  Integer literals are always a builtin integer type.
676   switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
677   default: llvm_unreachable("Unexpected type for integer literal!");
678   // FIXME: The Short and UShort cases are to handle cases where a short
679   // integeral literal is formed during template instantiation.  They should
680   // be removed when template instantiation no longer needs integer literals.
681   case BuiltinType::Short:
682   case BuiltinType::UShort:
683   case BuiltinType::Int:       break; // no suffix.
684   case BuiltinType::UInt:      OS << 'U'; break;
685   case BuiltinType::Long:      OS << 'L'; break;
686   case BuiltinType::ULong:     OS << "UL"; break;
687   case BuiltinType::LongLong:  OS << "LL"; break;
688   case BuiltinType::ULongLong: OS << "ULL"; break;
689   case BuiltinType::Int128:    OS << "i128"; break;
690   case BuiltinType::UInt128:   OS << "Ui128"; break;
691   }
692 }
693 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
694   llvm::SmallString<16> Str;
695   Node->getValue().toString(Str);
696   OS << Str;
697 }
698 
699 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
700   PrintExpr(Node->getSubExpr());
701   OS << "i";
702 }
703 
704 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
705   switch (Str->getKind()) {
706   case StringLiteral::Ascii: break; // no prefix.
707   case StringLiteral::Wide:  OS << 'L'; break;
708   case StringLiteral::UTF8:  OS << "u8"; break;
709   case StringLiteral::UTF16: OS << 'u'; break;
710   case StringLiteral::UTF32: OS << 'U'; break;
711   }
712   OS << '"';
713   static char Hex[] = "0123456789ABCDEF";
714 
715   for (unsigned I = 0, N = Str->getLength(); I != N; ++I) {
716     switch (uint32_t Char = Str->getCodeUnit(I)) {
717     default:
718       // FIXME: Is this the best way to print wchar_t?
719       if (Char > 0xff) {
720         assert(Char <= 0x10ffff && "invalid unicode codepoint");
721         if (Char > 0xffff)
722           OS << "\\U00"
723              << Hex[(Char >> 20) & 15]
724              << Hex[(Char >> 16) & 15];
725         else
726           OS << "\\u";
727         OS << Hex[(Char >> 12) & 15]
728            << Hex[(Char >>  8) & 15]
729            << Hex[(Char >>  4) & 15]
730            << Hex[(Char >>  0) & 15];
731         break;
732       }
733       if (Char <= 0xff && isprint(Char))
734         OS << (char)Char;
735       else  // Output anything hard as an octal escape.
736         OS << '\\'
737         << (char)('0'+ ((Char >> 6) & 7))
738         << (char)('0'+ ((Char >> 3) & 7))
739         << (char)('0'+ ((Char >> 0) & 7));
740       break;
741     // Handle some common non-printable cases to make dumps prettier.
742     case '\\': OS << "\\\\"; break;
743     case '"': OS << "\\\""; break;
744     case '\n': OS << "\\n"; break;
745     case '\t': OS << "\\t"; break;
746     case '\a': OS << "\\a"; break;
747     case '\b': OS << "\\b"; break;
748     }
749   }
750   OS << '"';
751 }
752 void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
753   OS << "(";
754   PrintExpr(Node->getSubExpr());
755   OS << ")";
756 }
757 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
758   if (!Node->isPostfix()) {
759     OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
760 
761     // Print a space if this is an "identifier operator" like __real, or if
762     // it might be concatenated incorrectly like '+'.
763     switch (Node->getOpcode()) {
764     default: break;
765     case UO_Real:
766     case UO_Imag:
767     case UO_Extension:
768       OS << ' ';
769       break;
770     case UO_Plus:
771     case UO_Minus:
772       if (isa<UnaryOperator>(Node->getSubExpr()))
773         OS << ' ';
774       break;
775     }
776   }
777   PrintExpr(Node->getSubExpr());
778 
779   if (Node->isPostfix())
780     OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
781 }
782 
783 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
784   OS << "__builtin_offsetof(";
785   OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", ";
786   bool PrintedSomething = false;
787   for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
788     OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
789     if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
790       // Array node
791       OS << "[";
792       PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
793       OS << "]";
794       PrintedSomething = true;
795       continue;
796     }
797 
798     // Skip implicit base indirections.
799     if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
800       continue;
801 
802     // Field or identifier node.
803     IdentifierInfo *Id = ON.getFieldName();
804     if (!Id)
805       continue;
806 
807     if (PrintedSomething)
808       OS << ".";
809     else
810       PrintedSomething = true;
811     OS << Id->getName();
812   }
813   OS << ")";
814 }
815 
816 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
817   switch(Node->getKind()) {
818   case UETT_SizeOf:
819     OS << "sizeof";
820     break;
821   case UETT_AlignOf:
822     OS << "__alignof";
823     break;
824   case UETT_VecStep:
825     OS << "vec_step";
826     break;
827   }
828   if (Node->isArgumentType())
829     OS << "(" << Node->getArgumentType().getAsString(Policy) << ")";
830   else {
831     OS << " ";
832     PrintExpr(Node->getArgumentExpr());
833   }
834 }
835 
836 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
837   OS << "_Generic(";
838   PrintExpr(Node->getControllingExpr());
839   for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
840     OS << ", ";
841     QualType T = Node->getAssocType(i);
842     if (T.isNull())
843       OS << "default";
844     else
845       OS << T.getAsString(Policy);
846     OS << ": ";
847     PrintExpr(Node->getAssocExpr(i));
848   }
849   OS << ")";
850 }
851 
852 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
853   PrintExpr(Node->getLHS());
854   OS << "[";
855   PrintExpr(Node->getRHS());
856   OS << "]";
857 }
858 
859 void StmtPrinter::PrintCallArgs(CallExpr *Call) {
860   for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
861     if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
862       // Don't print any defaulted arguments
863       break;
864     }
865 
866     if (i) OS << ", ";
867     PrintExpr(Call->getArg(i));
868   }
869 }
870 
871 void StmtPrinter::VisitCallExpr(CallExpr *Call) {
872   PrintExpr(Call->getCallee());
873   OS << "(";
874   PrintCallArgs(Call);
875   OS << ")";
876 }
877 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
878   // FIXME: Suppress printing implicit bases (like "this")
879   PrintExpr(Node->getBase());
880   if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
881     if (FD->isAnonymousStructOrUnion())
882       return;
883   OS << (Node->isArrow() ? "->" : ".");
884   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
885     Qualifier->print(OS, Policy);
886 
887   OS << Node->getMemberNameInfo();
888 
889   if (Node->hasExplicitTemplateArgs())
890     OS << TemplateSpecializationType::PrintTemplateArgumentList(
891                                                     Node->getTemplateArgs(),
892                                                     Node->getNumTemplateArgs(),
893                                                                 Policy);
894 }
895 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
896   PrintExpr(Node->getBase());
897   OS << (Node->isArrow() ? "->isa" : ".isa");
898 }
899 
900 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
901   PrintExpr(Node->getBase());
902   OS << ".";
903   OS << Node->getAccessor().getName();
904 }
905 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
906   OS << "(" << Node->getType().getAsString(Policy) << ")";
907   PrintExpr(Node->getSubExpr());
908 }
909 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
910   OS << "(" << Node->getType().getAsString(Policy) << ")";
911   PrintExpr(Node->getInitializer());
912 }
913 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
914   // No need to print anything, simply forward to the sub expression.
915   PrintExpr(Node->getSubExpr());
916 }
917 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
918   PrintExpr(Node->getLHS());
919   OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
920   PrintExpr(Node->getRHS());
921 }
922 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
923   PrintExpr(Node->getLHS());
924   OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
925   PrintExpr(Node->getRHS());
926 }
927 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
928   PrintExpr(Node->getCond());
929   OS << " ? ";
930   PrintExpr(Node->getLHS());
931   OS << " : ";
932   PrintExpr(Node->getRHS());
933 }
934 
935 // GNU extensions.
936 
937 void
938 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
939   PrintExpr(Node->getCommon());
940   OS << " ?: ";
941   PrintExpr(Node->getFalseExpr());
942 }
943 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
944   OS << "&&" << Node->getLabel()->getName();
945 }
946 
947 void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
948   OS << "(";
949   PrintRawCompoundStmt(E->getSubStmt());
950   OS << ")";
951 }
952 
953 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
954   OS << "__builtin_choose_expr(";
955   PrintExpr(Node->getCond());
956   OS << ", ";
957   PrintExpr(Node->getLHS());
958   OS << ", ";
959   PrintExpr(Node->getRHS());
960   OS << ")";
961 }
962 
963 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
964   OS << "__null";
965 }
966 
967 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
968   OS << "__builtin_shufflevector(";
969   for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
970     if (i) OS << ", ";
971     PrintExpr(Node->getExpr(i));
972   }
973   OS << ")";
974 }
975 
976 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
977   if (Node->getSyntacticForm()) {
978     Visit(Node->getSyntacticForm());
979     return;
980   }
981 
982   OS << "{ ";
983   for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
984     if (i) OS << ", ";
985     if (Node->getInit(i))
986       PrintExpr(Node->getInit(i));
987     else
988       OS << "0";
989   }
990   OS << " }";
991 }
992 
993 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
994   OS << "( ";
995   for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
996     if (i) OS << ", ";
997     PrintExpr(Node->getExpr(i));
998   }
999   OS << " )";
1000 }
1001 
1002 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1003   for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1004                       DEnd = Node->designators_end();
1005        D != DEnd; ++D) {
1006     if (D->isFieldDesignator()) {
1007       if (D->getDotLoc().isInvalid())
1008         OS << D->getFieldName()->getName() << ":";
1009       else
1010         OS << "." << D->getFieldName()->getName();
1011     } else {
1012       OS << "[";
1013       if (D->isArrayDesignator()) {
1014         PrintExpr(Node->getArrayIndex(*D));
1015       } else {
1016         PrintExpr(Node->getArrayRangeStart(*D));
1017         OS << " ... ";
1018         PrintExpr(Node->getArrayRangeEnd(*D));
1019       }
1020       OS << "]";
1021     }
1022   }
1023 
1024   OS << " = ";
1025   PrintExpr(Node->getInit());
1026 }
1027 
1028 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1029   if (Policy.LangOpts.CPlusPlus)
1030     OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
1031   else {
1032     OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
1033     if (Node->getType()->isRecordType())
1034       OS << "{}";
1035     else
1036       OS << 0;
1037   }
1038 }
1039 
1040 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1041   OS << "__builtin_va_arg(";
1042   PrintExpr(Node->getSubExpr());
1043   OS << ", ";
1044   OS << Node->getType().getAsString(Policy);
1045   OS << ")";
1046 }
1047 
1048 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1049   PrintExpr(Node->getSyntacticForm());
1050 }
1051 
1052 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1053   const char *Name = 0;
1054   switch (Node->getOp()) {
1055     case AtomicExpr::Init:
1056       Name = "__atomic_init(";
1057       break;
1058     case AtomicExpr::Load:
1059       Name = "__atomic_load(";
1060       break;
1061     case AtomicExpr::Store:
1062       Name = "__atomic_store(";
1063       break;
1064     case AtomicExpr::CmpXchgStrong:
1065       Name = "__atomic_compare_exchange_strong(";
1066       break;
1067     case AtomicExpr::CmpXchgWeak:
1068       Name = "__atomic_compare_exchange_weak(";
1069       break;
1070     case AtomicExpr::Xchg:
1071       Name = "__atomic_exchange(";
1072       break;
1073     case AtomicExpr::Add:
1074       Name = "__atomic_fetch_add(";
1075       break;
1076     case AtomicExpr::Sub:
1077       Name = "__atomic_fetch_sub(";
1078       break;
1079     case AtomicExpr::And:
1080       Name = "__atomic_fetch_and(";
1081       break;
1082     case AtomicExpr::Or:
1083       Name = "__atomic_fetch_or(";
1084       break;
1085     case AtomicExpr::Xor:
1086       Name = "__atomic_fetch_xor(";
1087       break;
1088   }
1089   OS << Name;
1090   PrintExpr(Node->getPtr());
1091   OS << ", ";
1092   if (Node->getOp() != AtomicExpr::Load) {
1093     PrintExpr(Node->getVal1());
1094     OS << ", ";
1095   }
1096   if (Node->isCmpXChg()) {
1097     PrintExpr(Node->getVal2());
1098     OS << ", ";
1099   }
1100   if (Node->getOp() != AtomicExpr::Init)
1101     PrintExpr(Node->getOrder());
1102   if (Node->isCmpXChg()) {
1103     OS << ", ";
1104     PrintExpr(Node->getOrderFail());
1105   }
1106   OS << ")";
1107 }
1108 
1109 // C++
1110 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1111   const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1112     "",
1113 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1114     Spelling,
1115 #include "clang/Basic/OperatorKinds.def"
1116   };
1117 
1118   OverloadedOperatorKind Kind = Node->getOperator();
1119   if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1120     if (Node->getNumArgs() == 1) {
1121       OS << OpStrings[Kind] << ' ';
1122       PrintExpr(Node->getArg(0));
1123     } else {
1124       PrintExpr(Node->getArg(0));
1125       OS << ' ' << OpStrings[Kind];
1126     }
1127   } else if (Kind == OO_Call) {
1128     PrintExpr(Node->getArg(0));
1129     OS << '(';
1130     for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1131       if (ArgIdx > 1)
1132         OS << ", ";
1133       if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1134         PrintExpr(Node->getArg(ArgIdx));
1135     }
1136     OS << ')';
1137   } else if (Kind == OO_Subscript) {
1138     PrintExpr(Node->getArg(0));
1139     OS << '[';
1140     PrintExpr(Node->getArg(1));
1141     OS << ']';
1142   } else if (Node->getNumArgs() == 1) {
1143     OS << OpStrings[Kind] << ' ';
1144     PrintExpr(Node->getArg(0));
1145   } else if (Node->getNumArgs() == 2) {
1146     PrintExpr(Node->getArg(0));
1147     OS << ' ' << OpStrings[Kind] << ' ';
1148     PrintExpr(Node->getArg(1));
1149   } else {
1150     llvm_unreachable("unknown overloaded operator");
1151   }
1152 }
1153 
1154 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1155   VisitCallExpr(cast<CallExpr>(Node));
1156 }
1157 
1158 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1159   PrintExpr(Node->getCallee());
1160   OS << "<<<";
1161   PrintCallArgs(Node->getConfig());
1162   OS << ">>>(";
1163   PrintCallArgs(Node);
1164   OS << ")";
1165 }
1166 
1167 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1168   OS << Node->getCastName() << '<';
1169   OS << Node->getTypeAsWritten().getAsString(Policy) << ">(";
1170   PrintExpr(Node->getSubExpr());
1171   OS << ")";
1172 }
1173 
1174 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1175   VisitCXXNamedCastExpr(Node);
1176 }
1177 
1178 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1179   VisitCXXNamedCastExpr(Node);
1180 }
1181 
1182 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1183   VisitCXXNamedCastExpr(Node);
1184 }
1185 
1186 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1187   VisitCXXNamedCastExpr(Node);
1188 }
1189 
1190 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1191   OS << "typeid(";
1192   if (Node->isTypeOperand()) {
1193     OS << Node->getTypeOperand().getAsString(Policy);
1194   } else {
1195     PrintExpr(Node->getExprOperand());
1196   }
1197   OS << ")";
1198 }
1199 
1200 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1201   OS << "__uuidof(";
1202   if (Node->isTypeOperand()) {
1203     OS << Node->getTypeOperand().getAsString(Policy);
1204   } else {
1205     PrintExpr(Node->getExprOperand());
1206   }
1207   OS << ")";
1208 }
1209 
1210 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1211   OS << (Node->getValue() ? "true" : "false");
1212 }
1213 
1214 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1215   OS << "nullptr";
1216 }
1217 
1218 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1219   OS << "this";
1220 }
1221 
1222 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1223   if (Node->getSubExpr() == 0)
1224     OS << "throw";
1225   else {
1226     OS << "throw ";
1227     PrintExpr(Node->getSubExpr());
1228   }
1229 }
1230 
1231 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1232   // Nothing to print: we picked up the default argument
1233 }
1234 
1235 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1236   OS << Node->getType().getAsString(Policy);
1237   OS << "(";
1238   PrintExpr(Node->getSubExpr());
1239   OS << ")";
1240 }
1241 
1242 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1243   PrintExpr(Node->getSubExpr());
1244 }
1245 
1246 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1247   OS << Node->getType().getAsString(Policy);
1248   OS << "(";
1249   for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1250                                          ArgEnd = Node->arg_end();
1251        Arg != ArgEnd; ++Arg) {
1252     if (Arg != Node->arg_begin())
1253       OS << ", ";
1254     PrintExpr(*Arg);
1255   }
1256   OS << ")";
1257 }
1258 
1259 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
1260   if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1261     OS << TSInfo->getType().getAsString(Policy) << "()";
1262   else
1263     OS << Node->getType().getAsString(Policy) << "()";
1264 }
1265 
1266 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1267   if (E->isGlobalNew())
1268     OS << "::";
1269   OS << "new ";
1270   unsigned NumPlace = E->getNumPlacementArgs();
1271   if (NumPlace > 0) {
1272     OS << "(";
1273     PrintExpr(E->getPlacementArg(0));
1274     for (unsigned i = 1; i < NumPlace; ++i) {
1275       OS << ", ";
1276       PrintExpr(E->getPlacementArg(i));
1277     }
1278     OS << ") ";
1279   }
1280   if (E->isParenTypeId())
1281     OS << "(";
1282   std::string TypeS;
1283   if (Expr *Size = E->getArraySize()) {
1284     llvm::raw_string_ostream s(TypeS);
1285     Size->printPretty(s, Context, Helper, Policy);
1286     s.flush();
1287     TypeS = "[" + TypeS + "]";
1288   }
1289   E->getAllocatedType().getAsStringInternal(TypeS, Policy);
1290   OS << TypeS;
1291   if (E->isParenTypeId())
1292     OS << ")";
1293 
1294   if (E->hasInitializer()) {
1295     OS << "(";
1296     unsigned NumCons = E->getNumConstructorArgs();
1297     if (NumCons > 0) {
1298       PrintExpr(E->getConstructorArg(0));
1299       for (unsigned i = 1; i < NumCons; ++i) {
1300         OS << ", ";
1301         PrintExpr(E->getConstructorArg(i));
1302       }
1303     }
1304     OS << ")";
1305   }
1306 }
1307 
1308 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1309   if (E->isGlobalDelete())
1310     OS << "::";
1311   OS << "delete ";
1312   if (E->isArrayForm())
1313     OS << "[] ";
1314   PrintExpr(E->getArgument());
1315 }
1316 
1317 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1318   PrintExpr(E->getBase());
1319   if (E->isArrow())
1320     OS << "->";
1321   else
1322     OS << '.';
1323   if (E->getQualifier())
1324     E->getQualifier()->print(OS, Policy);
1325 
1326   std::string TypeS;
1327   if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1328     OS << II->getName();
1329   else
1330     E->getDestroyedType().getAsStringInternal(TypeS, Policy);
1331   OS << TypeS;
1332 }
1333 
1334 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1335   for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1336     if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1337       // Don't print any defaulted arguments
1338       break;
1339     }
1340 
1341     if (i) OS << ", ";
1342     PrintExpr(E->getArg(i));
1343   }
1344 }
1345 
1346 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
1347   // Just forward to the sub expression.
1348   PrintExpr(E->getSubExpr());
1349 }
1350 
1351 void
1352 StmtPrinter::VisitCXXUnresolvedConstructExpr(
1353                                            CXXUnresolvedConstructExpr *Node) {
1354   OS << Node->getTypeAsWritten().getAsString(Policy);
1355   OS << "(";
1356   for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
1357                                              ArgEnd = Node->arg_end();
1358        Arg != ArgEnd; ++Arg) {
1359     if (Arg != Node->arg_begin())
1360       OS << ", ";
1361     PrintExpr(*Arg);
1362   }
1363   OS << ")";
1364 }
1365 
1366 void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1367                                          CXXDependentScopeMemberExpr *Node) {
1368   if (!Node->isImplicitAccess()) {
1369     PrintExpr(Node->getBase());
1370     OS << (Node->isArrow() ? "->" : ".");
1371   }
1372   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1373     Qualifier->print(OS, Policy);
1374   else if (Node->hasExplicitTemplateArgs())
1375     // FIXME: Track use of "template" keyword explicitly?
1376     OS << "template ";
1377 
1378   OS << Node->getMemberNameInfo();
1379 
1380   if (Node->hasExplicitTemplateArgs()) {
1381     OS << TemplateSpecializationType::PrintTemplateArgumentList(
1382                                                     Node->getTemplateArgs(),
1383                                                     Node->getNumTemplateArgs(),
1384                                                     Policy);
1385   }
1386 }
1387 
1388 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
1389   if (!Node->isImplicitAccess()) {
1390     PrintExpr(Node->getBase());
1391     OS << (Node->isArrow() ? "->" : ".");
1392   }
1393   if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1394     Qualifier->print(OS, Policy);
1395 
1396   // FIXME: this might originally have been written with 'template'
1397 
1398   OS << Node->getMemberNameInfo();
1399 
1400   if (Node->hasExplicitTemplateArgs()) {
1401     OS << TemplateSpecializationType::PrintTemplateArgumentList(
1402                                                     Node->getTemplateArgs(),
1403                                                     Node->getNumTemplateArgs(),
1404                                                     Policy);
1405   }
1406 }
1407 
1408 static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1409   switch (UTT) {
1410   case UTT_HasNothrowAssign:      return "__has_nothrow_assign";
1411   case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1412   case UTT_HasNothrowCopy:          return "__has_nothrow_copy";
1413   case UTT_HasTrivialAssign:      return "__has_trivial_assign";
1414   case UTT_HasTrivialDefaultConstructor: return "__has_trivial_constructor";
1415   case UTT_HasTrivialCopy:          return "__has_trivial_copy";
1416   case UTT_HasTrivialDestructor:  return "__has_trivial_destructor";
1417   case UTT_HasVirtualDestructor:  return "__has_virtual_destructor";
1418   case UTT_IsAbstract:            return "__is_abstract";
1419   case UTT_IsArithmetic:            return "__is_arithmetic";
1420   case UTT_IsArray:                 return "__is_array";
1421   case UTT_IsClass:               return "__is_class";
1422   case UTT_IsCompleteType:          return "__is_complete_type";
1423   case UTT_IsCompound:              return "__is_compound";
1424   case UTT_IsConst:                 return "__is_const";
1425   case UTT_IsEmpty:               return "__is_empty";
1426   case UTT_IsEnum:                return "__is_enum";
1427   case UTT_IsFinal:                 return "__is_final";
1428   case UTT_IsFloatingPoint:         return "__is_floating_point";
1429   case UTT_IsFunction:              return "__is_function";
1430   case UTT_IsFundamental:           return "__is_fundamental";
1431   case UTT_IsIntegral:              return "__is_integral";
1432   case UTT_IsLiteral:               return "__is_literal";
1433   case UTT_IsLvalueReference:       return "__is_lvalue_reference";
1434   case UTT_IsMemberFunctionPointer: return "__is_member_function_pointer";
1435   case UTT_IsMemberObjectPointer:   return "__is_member_object_pointer";
1436   case UTT_IsMemberPointer:         return "__is_member_pointer";
1437   case UTT_IsObject:                return "__is_object";
1438   case UTT_IsPOD:                 return "__is_pod";
1439   case UTT_IsPointer:               return "__is_pointer";
1440   case UTT_IsPolymorphic:         return "__is_polymorphic";
1441   case UTT_IsReference:             return "__is_reference";
1442   case UTT_IsRvalueReference:       return "__is_rvalue_reference";
1443   case UTT_IsScalar:                return "__is_scalar";
1444   case UTT_IsSigned:                return "__is_signed";
1445   case UTT_IsStandardLayout:        return "__is_standard_layout";
1446   case UTT_IsTrivial:               return "__is_trivial";
1447   case UTT_IsTriviallyCopyable:     return "__is_trivially_copyable";
1448   case UTT_IsUnion:               return "__is_union";
1449   case UTT_IsUnsigned:              return "__is_unsigned";
1450   case UTT_IsVoid:                  return "__is_void";
1451   case UTT_IsVolatile:              return "__is_volatile";
1452   }
1453   llvm_unreachable("Type trait not covered by switch statement");
1454 }
1455 
1456 static const char *getTypeTraitName(BinaryTypeTrait BTT) {
1457   switch (BTT) {
1458   case BTT_IsBaseOf:         return "__is_base_of";
1459   case BTT_IsConvertible:    return "__is_convertible";
1460   case BTT_IsSame:           return "__is_same";
1461   case BTT_TypeCompatible:   return "__builtin_types_compatible_p";
1462   case BTT_IsConvertibleTo:  return "__is_convertible_to";
1463   }
1464   llvm_unreachable("Binary type trait not covered by switch");
1465 }
1466 
1467 static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1468   switch (ATT) {
1469   case ATT_ArrayRank:        return "__array_rank";
1470   case ATT_ArrayExtent:      return "__array_extent";
1471   }
1472   llvm_unreachable("Array type trait not covered by switch");
1473 }
1474 
1475 static const char *getExpressionTraitName(ExpressionTrait ET) {
1476   switch (ET) {
1477   case ET_IsLValueExpr:      return "__is_lvalue_expr";
1478   case ET_IsRValueExpr:      return "__is_rvalue_expr";
1479   }
1480   llvm_unreachable("Expression type trait not covered by switch");
1481 }
1482 
1483 void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1484   OS << getTypeTraitName(E->getTrait()) << "("
1485      << E->getQueriedType().getAsString(Policy) << ")";
1486 }
1487 
1488 void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1489   OS << getTypeTraitName(E->getTrait()) << "("
1490      << E->getLhsType().getAsString(Policy) << ","
1491      << E->getRhsType().getAsString(Policy) << ")";
1492 }
1493 
1494 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
1495   OS << getTypeTraitName(E->getTrait()) << "("
1496      << E->getQueriedType().getAsString(Policy) << ")";
1497 }
1498 
1499 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
1500     OS << getExpressionTraitName(E->getTrait()) << "(";
1501     PrintExpr(E->getQueriedExpression());
1502     OS << ")";
1503 }
1504 
1505 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1506   OS << "noexcept(";
1507   PrintExpr(E->getOperand());
1508   OS << ")";
1509 }
1510 
1511 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
1512   PrintExpr(E->getPattern());
1513   OS << "...";
1514 }
1515 
1516 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
1517   OS << "sizeof...(" << E->getPack()->getNameAsString() << ")";
1518 }
1519 
1520 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1521                                        SubstNonTypeTemplateParmPackExpr *Node) {
1522   OS << Node->getParameterPack()->getNameAsString();
1523 }
1524 
1525 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1526                                        SubstNonTypeTemplateParmExpr *Node) {
1527   Visit(Node->getReplacement());
1528 }
1529 
1530 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1531   PrintExpr(Node->GetTemporaryExpr());
1532 }
1533 
1534 // Obj-C
1535 
1536 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1537   OS << "@";
1538   VisitStringLiteral(Node->getString());
1539 }
1540 
1541 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
1542   OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')';
1543 }
1544 
1545 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
1546   OS << "@selector(" << Node->getSelector().getAsString() << ')';
1547 }
1548 
1549 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
1550   OS << "@protocol(" << *Node->getProtocol() << ')';
1551 }
1552 
1553 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1554   OS << "[";
1555   switch (Mess->getReceiverKind()) {
1556   case ObjCMessageExpr::Instance:
1557     PrintExpr(Mess->getInstanceReceiver());
1558     break;
1559 
1560   case ObjCMessageExpr::Class:
1561     OS << Mess->getClassReceiver().getAsString(Policy);
1562     break;
1563 
1564   case ObjCMessageExpr::SuperInstance:
1565   case ObjCMessageExpr::SuperClass:
1566     OS << "Super";
1567     break;
1568   }
1569 
1570   OS << ' ';
1571   Selector selector = Mess->getSelector();
1572   if (selector.isUnarySelector()) {
1573     OS << selector.getNameForSlot(0);
1574   } else {
1575     for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
1576       if (i < selector.getNumArgs()) {
1577         if (i > 0) OS << ' ';
1578         if (selector.getIdentifierInfoForSlot(i))
1579           OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
1580         else
1581            OS << ":";
1582       }
1583       else OS << ", "; // Handle variadic methods.
1584 
1585       PrintExpr(Mess->getArg(i));
1586     }
1587   }
1588   OS << "]";
1589 }
1590 
1591 void
1592 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1593   PrintExpr(E->getSubExpr());
1594 }
1595 
1596 void
1597 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
1598   OS << "(" << E->getBridgeKindName() << E->getType().getAsString(Policy)
1599      << ")";
1600   PrintExpr(E->getSubExpr());
1601 }
1602 
1603 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
1604   BlockDecl *BD = Node->getBlockDecl();
1605   OS << "^";
1606 
1607   const FunctionType *AFT = Node->getFunctionType();
1608 
1609   if (isa<FunctionNoProtoType>(AFT)) {
1610     OS << "()";
1611   } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
1612     OS << '(';
1613     std::string ParamStr;
1614     for (BlockDecl::param_iterator AI = BD->param_begin(),
1615          E = BD->param_end(); AI != E; ++AI) {
1616       if (AI != BD->param_begin()) OS << ", ";
1617       ParamStr = (*AI)->getNameAsString();
1618       (*AI)->getType().getAsStringInternal(ParamStr, Policy);
1619       OS << ParamStr;
1620     }
1621 
1622     const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
1623     if (FT->isVariadic()) {
1624       if (!BD->param_empty()) OS << ", ";
1625       OS << "...";
1626     }
1627     OS << ')';
1628   }
1629 }
1630 
1631 void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
1632   OS << *Node->getDecl();
1633 }
1634 
1635 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
1636   PrintExpr(Node->getSourceExpr());
1637 }
1638 
1639 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1640   OS << "__builtin_astype(";
1641   PrintExpr(Node->getSrcExpr());
1642   OS << ", " << Node->getType().getAsString();
1643   OS << ")";
1644 }
1645 
1646 //===----------------------------------------------------------------------===//
1647 // Stmt method implementations
1648 //===----------------------------------------------------------------------===//
1649 
1650 void Stmt::dumpPretty(ASTContext& Context) const {
1651   printPretty(llvm::errs(), Context, 0,
1652               PrintingPolicy(Context.getLangOptions()));
1653 }
1654 
1655 void Stmt::printPretty(raw_ostream &OS, ASTContext& Context,
1656                        PrinterHelper* Helper,
1657                        const PrintingPolicy &Policy,
1658                        unsigned Indentation) const {
1659   if (this == 0) {
1660     OS << "<NULL>";
1661     return;
1662   }
1663 
1664   if (Policy.Dump && &Context) {
1665     dump(OS, Context.getSourceManager());
1666     return;
1667   }
1668 
1669   StmtPrinter P(OS, Context, Helper, Policy, Indentation);
1670   P.Visit(const_cast<Stmt*>(this));
1671 }
1672 
1673 //===----------------------------------------------------------------------===//
1674 // PrinterHelper
1675 //===----------------------------------------------------------------------===//
1676 
1677 // Implement virtual destructor.
1678 PrinterHelper::~PrinterHelper() {}
1679