StmtPrinter.cpp revision aeeacf725c9e0ddd64ea9764bd008e5b6873ce51
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"
27using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// StmtPrinter Visitor
31//===----------------------------------------------------------------------===//
32
33namespace  {
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 }.
114void 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
123void StmtPrinter::PrintRawDecl(Decl *D) {
124  D->print(OS, Policy, IndentLevel);
125}
126
127void 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
136void StmtPrinter::VisitNullStmt(NullStmt *Node) {
137  Indent() << ";\n";
138}
139
140void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
141  Indent();
142  PrintRawDeclStmt(Node);
143  OS << ";\n";
144}
145
146void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
147  Indent();
148  PrintRawCompoundStmt(Node);
149  OS << "\n";
150}
151
152void 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
164void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
165  Indent(-1) << "default:\n";
166  PrintStmt(Node->getSubStmt(), 0);
167}
168
169void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
170  Indent(-1) << Node->getName() << ":\n";
171  PrintStmt(Node->getSubStmt(), 0);
172}
173
174void 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
191void 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
226void StmtPrinter::VisitIfStmt(IfStmt *If) {
227  Indent();
228  PrintRawIfStmt(If);
229}
230
231void 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
250void 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
260void 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
276void 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
305void 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
324void 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
336void 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
352void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
353  Indent() << "goto " << Node->getLabel()->getName() << ";\n";
354}
355
356void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
357  Indent() << "goto *";
358  PrintExpr(Node->getTarget());
359  OS << ";\n";
360}
361
362void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
363  Indent() << "continue;\n";
364}
365
366void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
367  Indent() << "break;\n";
368}
369
370
371void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
372  Indent() << "return";
373  if (Node->getRetValue()) {
374    OS << " ";
375    PrintExpr(Node->getRetValue());
376  }
377  OS << ";\n";
378}
379
380
381void 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
443void 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
453void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
454  PrintStmt(Node->getCapturedDecl()->getBody());
455}
456
457void 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
486void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
487}
488
489void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
490  Indent() << "@catch (...) { /* todo */ } \n";
491}
492
493void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
494  Indent() << "@throw";
495  if (Node->getThrowExpr()) {
496    OS << " ";
497    PrintExpr(Node->getThrowExpr());
498  }
499  OS << ";\n";
500}
501
502void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
503  Indent() << "@synchronized (";
504  PrintExpr(Node->getSynchExpr());
505  OS << ")";
506  PrintRawCompoundStmt(Node->getSynchBody());
507  OS << "\n";
508}
509
510void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
511  Indent() << "@autoreleasepool";
512  PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
513  OS << "\n";
514}
515
516void 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
526void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
527  Indent();
528  PrintRawCXXCatchStmt(Node);
529  OS << "\n";
530}
531
532void 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
542void 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
556void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
557  OS << "__finally ";
558  PrintRawCompoundStmt(Node->getBlock());
559  OS << "\n";
560}
561
562void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
563  OS << "__except (";
564  VisitExpr(Node->getFilterExpr());
565  OS << ")\n";
566  PrintRawCompoundStmt(Node->getBlock());
567  OS << "\n";
568}
569
570void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
571  Indent();
572  PrintRawSEHExceptHandler(Node);
573  OS << "\n";
574}
575
576void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
577  Indent();
578  PrintRawSEHFinallyStmt(Node);
579  OS << "\n";
580}
581
582//===----------------------------------------------------------------------===//
583//  Expr printing methods.
584//===----------------------------------------------------------------------===//
585
586void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
587  if (NestedNameSpecifier *Qualifier = Node->getQualifier())
588    Qualifier->print(OS, Policy);
589  if (Node->hasTemplateKeyword())
590    OS << "template ";
591  OS << Node->getNameInfo();
592  if (Node->hasExplicitTemplateArgs())
593    TemplateSpecializationType::PrintTemplateArgumentList(
594        OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
595}
596
597void StmtPrinter::VisitDependentScopeDeclRefExpr(
598                                           DependentScopeDeclRefExpr *Node) {
599  if (NestedNameSpecifier *Qualifier = Node->getQualifier())
600    Qualifier->print(OS, Policy);
601  if (Node->hasTemplateKeyword())
602    OS << "template ";
603  OS << Node->getNameInfo();
604  if (Node->hasExplicitTemplateArgs())
605    TemplateSpecializationType::PrintTemplateArgumentList(
606        OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
607}
608
609void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
610  if (Node->getQualifier())
611    Node->getQualifier()->print(OS, Policy);
612  if (Node->hasTemplateKeyword())
613    OS << "template ";
614  OS << Node->getNameInfo();
615  if (Node->hasExplicitTemplateArgs())
616    TemplateSpecializationType::PrintTemplateArgumentList(
617        OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
618}
619
620void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
621  if (Node->getBase()) {
622    PrintExpr(Node->getBase());
623    OS << (Node->isArrow() ? "->" : ".");
624  }
625  OS << *Node->getDecl();
626}
627
628void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
629  if (Node->isSuperReceiver())
630    OS << "super.";
631  else if (Node->getBase()) {
632    PrintExpr(Node->getBase());
633    OS << ".";
634  }
635
636  if (Node->isImplicitProperty())
637    OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
638  else
639    OS << Node->getExplicitProperty()->getName();
640}
641
642void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
643
644  PrintExpr(Node->getBaseExpr());
645  OS << "[";
646  PrintExpr(Node->getKeyExpr());
647  OS << "]";
648}
649
650void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
651  switch (Node->getIdentType()) {
652    default:
653      llvm_unreachable("unknown case");
654    case PredefinedExpr::Func:
655      OS << "__func__";
656      break;
657    case PredefinedExpr::Function:
658      OS << "__FUNCTION__";
659      break;
660    case PredefinedExpr::LFunction:
661      OS << "L__FUNCTION__";
662      break;
663    case PredefinedExpr::PrettyFunction:
664      OS << "__PRETTY_FUNCTION__";
665      break;
666  }
667}
668
669void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
670  unsigned value = Node->getValue();
671
672  switch (Node->getKind()) {
673  case CharacterLiteral::Ascii: break; // no prefix.
674  case CharacterLiteral::Wide:  OS << 'L'; break;
675  case CharacterLiteral::UTF16: OS << 'u'; break;
676  case CharacterLiteral::UTF32: OS << 'U'; break;
677  }
678
679  switch (value) {
680  case '\\':
681    OS << "'\\\\'";
682    break;
683  case '\'':
684    OS << "'\\''";
685    break;
686  case '\a':
687    // TODO: K&R: the meaning of '\\a' is different in traditional C
688    OS << "'\\a'";
689    break;
690  case '\b':
691    OS << "'\\b'";
692    break;
693  // Nonstandard escape sequence.
694  /*case '\e':
695    OS << "'\\e'";
696    break;*/
697  case '\f':
698    OS << "'\\f'";
699    break;
700  case '\n':
701    OS << "'\\n'";
702    break;
703  case '\r':
704    OS << "'\\r'";
705    break;
706  case '\t':
707    OS << "'\\t'";
708    break;
709  case '\v':
710    OS << "'\\v'";
711    break;
712  default:
713    if (value < 256 && isPrintable((unsigned char)value))
714      OS << "'" << (char)value << "'";
715    else if (value < 256)
716      OS << "'\\x" << llvm::format("%02x", value) << "'";
717    else if (value <= 0xFFFF)
718      OS << "'\\u" << llvm::format("%04x", value) << "'";
719    else
720      OS << "'\\U" << llvm::format("%08x", value) << "'";
721  }
722}
723
724void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
725  bool isSigned = Node->getType()->isSignedIntegerType();
726  OS << Node->getValue().toString(10, isSigned);
727
728  // Emit suffixes.  Integer literals are always a builtin integer type.
729  switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
730  default: llvm_unreachable("Unexpected type for integer literal!");
731  // FIXME: The Short and UShort cases are to handle cases where a short
732  // integeral literal is formed during template instantiation.  They should
733  // be removed when template instantiation no longer needs integer literals.
734  case BuiltinType::Short:
735  case BuiltinType::UShort:
736  case BuiltinType::Int:       break; // no suffix.
737  case BuiltinType::UInt:      OS << 'U'; break;
738  case BuiltinType::Long:      OS << 'L'; break;
739  case BuiltinType::ULong:     OS << "UL"; break;
740  case BuiltinType::LongLong:  OS << "LL"; break;
741  case BuiltinType::ULongLong: OS << "ULL"; break;
742  case BuiltinType::Int128:    OS << "i128"; break;
743  case BuiltinType::UInt128:   OS << "Ui128"; break;
744  }
745}
746
747static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
748                                 bool PrintSuffix) {
749  SmallString<16> Str;
750  Node->getValue().toString(Str);
751  OS << Str;
752  if (Str.find_first_not_of("-0123456789") == StringRef::npos)
753    OS << '.'; // Trailing dot in order to separate from ints.
754
755  if (!PrintSuffix)
756    return;
757
758  // Emit suffixes.  Float literals are always a builtin float type.
759  switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
760  default: llvm_unreachable("Unexpected type for float literal!");
761  case BuiltinType::Half:       break; // FIXME: suffix?
762  case BuiltinType::Double:     break; // no suffix.
763  case BuiltinType::Float:      OS << 'F'; break;
764  case BuiltinType::LongDouble: OS << 'L'; break;
765  }
766}
767
768void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
769  PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
770}
771
772void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
773  PrintExpr(Node->getSubExpr());
774  OS << "i";
775}
776
777void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
778  Str->outputString(OS);
779}
780void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
781  OS << "(";
782  PrintExpr(Node->getSubExpr());
783  OS << ")";
784}
785void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
786  if (!Node->isPostfix()) {
787    OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
788
789    // Print a space if this is an "identifier operator" like __real, or if
790    // it might be concatenated incorrectly like '+'.
791    switch (Node->getOpcode()) {
792    default: break;
793    case UO_Real:
794    case UO_Imag:
795    case UO_Extension:
796      OS << ' ';
797      break;
798    case UO_Plus:
799    case UO_Minus:
800      if (isa<UnaryOperator>(Node->getSubExpr()))
801        OS << ' ';
802      break;
803    }
804  }
805  PrintExpr(Node->getSubExpr());
806
807  if (Node->isPostfix())
808    OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
809}
810
811void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
812  OS << "__builtin_offsetof(";
813  Node->getTypeSourceInfo()->getType().print(OS, Policy);
814  OS << ", ";
815  bool PrintedSomething = false;
816  for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
817    OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
818    if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
819      // Array node
820      OS << "[";
821      PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
822      OS << "]";
823      PrintedSomething = true;
824      continue;
825    }
826
827    // Skip implicit base indirections.
828    if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
829      continue;
830
831    // Field or identifier node.
832    IdentifierInfo *Id = ON.getFieldName();
833    if (!Id)
834      continue;
835
836    if (PrintedSomething)
837      OS << ".";
838    else
839      PrintedSomething = true;
840    OS << Id->getName();
841  }
842  OS << ")";
843}
844
845void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
846  switch(Node->getKind()) {
847  case UETT_SizeOf:
848    OS << "sizeof";
849    break;
850  case UETT_AlignOf:
851    if (Policy.LangOpts.CPlusPlus)
852      OS << "alignof";
853    else if (Policy.LangOpts.C11)
854      OS << "_Alignof";
855    else
856      OS << "__alignof";
857    break;
858  case UETT_VecStep:
859    OS << "vec_step";
860    break;
861  }
862  if (Node->isArgumentType()) {
863    OS << '(';
864    Node->getArgumentType().print(OS, Policy);
865    OS << ')';
866  } else {
867    OS << " ";
868    PrintExpr(Node->getArgumentExpr());
869  }
870}
871
872void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
873  OS << "_Generic(";
874  PrintExpr(Node->getControllingExpr());
875  for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
876    OS << ", ";
877    QualType T = Node->getAssocType(i);
878    if (T.isNull())
879      OS << "default";
880    else
881      T.print(OS, Policy);
882    OS << ": ";
883    PrintExpr(Node->getAssocExpr(i));
884  }
885  OS << ")";
886}
887
888void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
889  PrintExpr(Node->getLHS());
890  OS << "[";
891  PrintExpr(Node->getRHS());
892  OS << "]";
893}
894
895void StmtPrinter::PrintCallArgs(CallExpr *Call) {
896  for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
897    if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
898      // Don't print any defaulted arguments
899      break;
900    }
901
902    if (i) OS << ", ";
903    PrintExpr(Call->getArg(i));
904  }
905}
906
907void StmtPrinter::VisitCallExpr(CallExpr *Call) {
908  PrintExpr(Call->getCallee());
909  OS << "(";
910  PrintCallArgs(Call);
911  OS << ")";
912}
913void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
914  // FIXME: Suppress printing implicit bases (like "this")
915  PrintExpr(Node->getBase());
916
917  MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
918  FieldDecl  *ParentDecl   = ParentMember
919    ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : NULL;
920
921  if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
922    OS << (Node->isArrow() ? "->" : ".");
923
924  if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
925    if (FD->isAnonymousStructOrUnion())
926      return;
927
928  if (NestedNameSpecifier *Qualifier = Node->getQualifier())
929    Qualifier->print(OS, Policy);
930  if (Node->hasTemplateKeyword())
931    OS << "template ";
932  OS << Node->getMemberNameInfo();
933  if (Node->hasExplicitTemplateArgs())
934    TemplateSpecializationType::PrintTemplateArgumentList(
935        OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
936}
937void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
938  PrintExpr(Node->getBase());
939  OS << (Node->isArrow() ? "->isa" : ".isa");
940}
941
942void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
943  PrintExpr(Node->getBase());
944  OS << ".";
945  OS << Node->getAccessor().getName();
946}
947void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
948  OS << '(';
949  Node->getTypeAsWritten().print(OS, Policy);
950  OS << ')';
951  PrintExpr(Node->getSubExpr());
952}
953void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
954  OS << '(';
955  Node->getType().print(OS, Policy);
956  OS << ')';
957  PrintExpr(Node->getInitializer());
958}
959void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
960  // No need to print anything, simply forward to the sub expression.
961  PrintExpr(Node->getSubExpr());
962}
963void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
964  PrintExpr(Node->getLHS());
965  OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
966  PrintExpr(Node->getRHS());
967}
968void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
969  PrintExpr(Node->getLHS());
970  OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
971  PrintExpr(Node->getRHS());
972}
973void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
974  PrintExpr(Node->getCond());
975  OS << " ? ";
976  PrintExpr(Node->getLHS());
977  OS << " : ";
978  PrintExpr(Node->getRHS());
979}
980
981// GNU extensions.
982
983void
984StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
985  PrintExpr(Node->getCommon());
986  OS << " ?: ";
987  PrintExpr(Node->getFalseExpr());
988}
989void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
990  OS << "&&" << Node->getLabel()->getName();
991}
992
993void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
994  OS << "(";
995  PrintRawCompoundStmt(E->getSubStmt());
996  OS << ")";
997}
998
999void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1000  OS << "__builtin_choose_expr(";
1001  PrintExpr(Node->getCond());
1002  OS << ", ";
1003  PrintExpr(Node->getLHS());
1004  OS << ", ";
1005  PrintExpr(Node->getRHS());
1006  OS << ")";
1007}
1008
1009void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1010  OS << "__null";
1011}
1012
1013void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1014  OS << "__builtin_shufflevector(";
1015  for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1016    if (i) OS << ", ";
1017    PrintExpr(Node->getExpr(i));
1018  }
1019  OS << ")";
1020}
1021
1022void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1023  if (Node->getSyntacticForm()) {
1024    Visit(Node->getSyntacticForm());
1025    return;
1026  }
1027
1028  OS << "{ ";
1029  for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1030    if (i) OS << ", ";
1031    if (Node->getInit(i))
1032      PrintExpr(Node->getInit(i));
1033    else
1034      OS << "0";
1035  }
1036  OS << " }";
1037}
1038
1039void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1040  OS << "( ";
1041  for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1042    if (i) OS << ", ";
1043    PrintExpr(Node->getExpr(i));
1044  }
1045  OS << " )";
1046}
1047
1048void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1049  for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1050                      DEnd = Node->designators_end();
1051       D != DEnd; ++D) {
1052    if (D->isFieldDesignator()) {
1053      if (D->getDotLoc().isInvalid())
1054        OS << D->getFieldName()->getName() << ":";
1055      else
1056        OS << "." << D->getFieldName()->getName();
1057    } else {
1058      OS << "[";
1059      if (D->isArrayDesignator()) {
1060        PrintExpr(Node->getArrayIndex(*D));
1061      } else {
1062        PrintExpr(Node->getArrayRangeStart(*D));
1063        OS << " ... ";
1064        PrintExpr(Node->getArrayRangeEnd(*D));
1065      }
1066      OS << "]";
1067    }
1068  }
1069
1070  OS << " = ";
1071  PrintExpr(Node->getInit());
1072}
1073
1074void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1075  if (Policy.LangOpts.CPlusPlus) {
1076    OS << "/*implicit*/";
1077    Node->getType().print(OS, Policy);
1078    OS << "()";
1079  } else {
1080    OS << "/*implicit*/(";
1081    Node->getType().print(OS, Policy);
1082    OS << ')';
1083    if (Node->getType()->isRecordType())
1084      OS << "{}";
1085    else
1086      OS << 0;
1087  }
1088}
1089
1090void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1091  OS << "__builtin_va_arg(";
1092  PrintExpr(Node->getSubExpr());
1093  OS << ", ";
1094  Node->getType().print(OS, Policy);
1095  OS << ")";
1096}
1097
1098void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1099  PrintExpr(Node->getSyntacticForm());
1100}
1101
1102void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1103  const char *Name = 0;
1104  switch (Node->getOp()) {
1105#define BUILTIN(ID, TYPE, ATTRS)
1106#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1107  case AtomicExpr::AO ## ID: \
1108    Name = #ID "("; \
1109    break;
1110#include "clang/Basic/Builtins.def"
1111  }
1112  OS << Name;
1113
1114  // AtomicExpr stores its subexpressions in a permuted order.
1115  PrintExpr(Node->getPtr());
1116  if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1117      Node->getOp() != AtomicExpr::AO__atomic_load_n) {
1118    OS << ", ";
1119    PrintExpr(Node->getVal1());
1120  }
1121  if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1122      Node->isCmpXChg()) {
1123    OS << ", ";
1124    PrintExpr(Node->getVal2());
1125  }
1126  if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1127      Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1128    OS << ", ";
1129    PrintExpr(Node->getWeak());
1130  }
1131  if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1132    OS << ", ";
1133    PrintExpr(Node->getOrder());
1134  }
1135  if (Node->isCmpXChg()) {
1136    OS << ", ";
1137    PrintExpr(Node->getOrderFail());
1138  }
1139  OS << ")";
1140}
1141
1142// C++
1143void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1144  const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1145    "",
1146#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1147    Spelling,
1148#include "clang/Basic/OperatorKinds.def"
1149  };
1150
1151  OverloadedOperatorKind Kind = Node->getOperator();
1152  if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1153    if (Node->getNumArgs() == 1) {
1154      OS << OpStrings[Kind] << ' ';
1155      PrintExpr(Node->getArg(0));
1156    } else {
1157      PrintExpr(Node->getArg(0));
1158      OS << ' ' << OpStrings[Kind];
1159    }
1160  } else if (Kind == OO_Arrow) {
1161    PrintExpr(Node->getArg(0));
1162  } else if (Kind == OO_Call) {
1163    PrintExpr(Node->getArg(0));
1164    OS << '(';
1165    for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1166      if (ArgIdx > 1)
1167        OS << ", ";
1168      if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1169        PrintExpr(Node->getArg(ArgIdx));
1170    }
1171    OS << ')';
1172  } else if (Kind == OO_Subscript) {
1173    PrintExpr(Node->getArg(0));
1174    OS << '[';
1175    PrintExpr(Node->getArg(1));
1176    OS << ']';
1177  } else if (Node->getNumArgs() == 1) {
1178    OS << OpStrings[Kind] << ' ';
1179    PrintExpr(Node->getArg(0));
1180  } else if (Node->getNumArgs() == 2) {
1181    PrintExpr(Node->getArg(0));
1182    OS << ' ' << OpStrings[Kind] << ' ';
1183    PrintExpr(Node->getArg(1));
1184  } else {
1185    llvm_unreachable("unknown overloaded operator");
1186  }
1187}
1188
1189void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1190  VisitCallExpr(cast<CallExpr>(Node));
1191}
1192
1193void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1194  PrintExpr(Node->getCallee());
1195  OS << "<<<";
1196  PrintCallArgs(Node->getConfig());
1197  OS << ">>>(";
1198  PrintCallArgs(Node);
1199  OS << ")";
1200}
1201
1202void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1203  OS << Node->getCastName() << '<';
1204  Node->getTypeAsWritten().print(OS, Policy);
1205  OS << ">(";
1206  PrintExpr(Node->getSubExpr());
1207  OS << ")";
1208}
1209
1210void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1211  VisitCXXNamedCastExpr(Node);
1212}
1213
1214void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1215  VisitCXXNamedCastExpr(Node);
1216}
1217
1218void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1219  VisitCXXNamedCastExpr(Node);
1220}
1221
1222void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1223  VisitCXXNamedCastExpr(Node);
1224}
1225
1226void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1227  OS << "typeid(";
1228  if (Node->isTypeOperand()) {
1229    Node->getTypeOperand().print(OS, Policy);
1230  } else {
1231    PrintExpr(Node->getExprOperand());
1232  }
1233  OS << ")";
1234}
1235
1236void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1237  OS << "__uuidof(";
1238  if (Node->isTypeOperand()) {
1239    Node->getTypeOperand().print(OS, Policy);
1240  } else {
1241    PrintExpr(Node->getExprOperand());
1242  }
1243  OS << ")";
1244}
1245
1246void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1247  PrintExpr(Node->getBaseExpr());
1248  if (Node->isArrow())
1249    OS << "->";
1250  else
1251    OS << ".";
1252  if (NestedNameSpecifier *Qualifier =
1253      Node->getQualifierLoc().getNestedNameSpecifier())
1254    Qualifier->print(OS, Policy);
1255  OS << Node->getPropertyDecl()->getDeclName();
1256}
1257
1258void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1259  switch (Node->getLiteralOperatorKind()) {
1260  case UserDefinedLiteral::LOK_Raw:
1261    OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
1262    break;
1263  case UserDefinedLiteral::LOK_Template: {
1264    DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1265    const TemplateArgumentList *Args =
1266      cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1267    assert(Args);
1268    const TemplateArgument &Pack = Args->get(0);
1269    for (TemplateArgument::pack_iterator I = Pack.pack_begin(),
1270                                         E = Pack.pack_end(); I != E; ++I) {
1271      char C = (char)I->getAsIntegral().getZExtValue();
1272      OS << C;
1273    }
1274    break;
1275  }
1276  case UserDefinedLiteral::LOK_Integer: {
1277    // Print integer literal without suffix.
1278    IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1279    OS << Int->getValue().toString(10, /*isSigned*/false);
1280    break;
1281  }
1282  case UserDefinedLiteral::LOK_Floating: {
1283    // Print floating literal without suffix.
1284    FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1285    PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1286    break;
1287  }
1288  case UserDefinedLiteral::LOK_String:
1289  case UserDefinedLiteral::LOK_Character:
1290    PrintExpr(Node->getCookedLiteral());
1291    break;
1292  }
1293  OS << Node->getUDSuffix()->getName();
1294}
1295
1296void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1297  OS << (Node->getValue() ? "true" : "false");
1298}
1299
1300void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1301  OS << "nullptr";
1302}
1303
1304void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1305  OS << "this";
1306}
1307
1308void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1309  if (Node->getSubExpr() == 0)
1310    OS << "throw";
1311  else {
1312    OS << "throw ";
1313    PrintExpr(Node->getSubExpr());
1314  }
1315}
1316
1317void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1318  // Nothing to print: we picked up the default argument.
1319}
1320
1321void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1322  // Nothing to print: we picked up the default initializer.
1323}
1324
1325void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1326  Node->getType().print(OS, Policy);
1327  OS << "(";
1328  PrintExpr(Node->getSubExpr());
1329  OS << ")";
1330}
1331
1332void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1333  PrintExpr(Node->getSubExpr());
1334}
1335
1336void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1337  Node->getType().print(OS, Policy);
1338  OS << "(";
1339  for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1340                                         ArgEnd = Node->arg_end();
1341       Arg != ArgEnd; ++Arg) {
1342    if (Arg != Node->arg_begin())
1343      OS << ", ";
1344    PrintExpr(*Arg);
1345  }
1346  OS << ")";
1347}
1348
1349void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1350  OS << '[';
1351  bool NeedComma = false;
1352  switch (Node->getCaptureDefault()) {
1353  case LCD_None:
1354    break;
1355
1356  case LCD_ByCopy:
1357    OS << '=';
1358    NeedComma = true;
1359    break;
1360
1361  case LCD_ByRef:
1362    OS << '&';
1363    NeedComma = true;
1364    break;
1365  }
1366  for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1367                                 CEnd = Node->explicit_capture_end();
1368       C != CEnd;
1369       ++C) {
1370    if (NeedComma)
1371      OS << ", ";
1372    NeedComma = true;
1373
1374    switch (C->getCaptureKind()) {
1375    case LCK_This:
1376      OS << "this";
1377      break;
1378
1379    case LCK_ByRef:
1380      if (Node->getCaptureDefault() != LCD_ByRef)
1381        OS << '&';
1382      OS << C->getCapturedVar()->getName();
1383      break;
1384
1385    case LCK_ByCopy:
1386      if (Node->getCaptureDefault() != LCD_ByCopy)
1387        OS << '=';
1388      OS << C->getCapturedVar()->getName();
1389      break;
1390    }
1391  }
1392  OS << ']';
1393
1394  if (Node->hasExplicitParameters()) {
1395    OS << " (";
1396    CXXMethodDecl *Method = Node->getCallOperator();
1397    NeedComma = false;
1398    for (CXXMethodDecl::param_iterator P = Method->param_begin(),
1399                                    PEnd = Method->param_end();
1400         P != PEnd; ++P) {
1401      if (NeedComma) {
1402        OS << ", ";
1403      } else {
1404        NeedComma = true;
1405      }
1406      std::string ParamStr = (*P)->getNameAsString();
1407      (*P)->getOriginalType().print(OS, Policy, ParamStr);
1408    }
1409    if (Method->isVariadic()) {
1410      if (NeedComma)
1411        OS << ", ";
1412      OS << "...";
1413    }
1414    OS << ')';
1415
1416    if (Node->isMutable())
1417      OS << " mutable";
1418
1419    const FunctionProtoType *Proto
1420      = Method->getType()->getAs<FunctionProtoType>();
1421    Proto->printExceptionSpecification(OS, Policy);
1422
1423    // FIXME: Attributes
1424
1425    // Print the trailing return type if it was specified in the source.
1426    if (Node->hasExplicitResultType()) {
1427      OS << " -> ";
1428      Proto->getResultType().print(OS, Policy);
1429    }
1430  }
1431
1432  // Print the body.
1433  CompoundStmt *Body = Node->getBody();
1434  OS << ' ';
1435  PrintStmt(Body);
1436}
1437
1438void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
1439  if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1440    TSInfo->getType().print(OS, Policy);
1441  else
1442    Node->getType().print(OS, Policy);
1443  OS << "()";
1444}
1445
1446void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1447  if (E->isGlobalNew())
1448    OS << "::";
1449  OS << "new ";
1450  unsigned NumPlace = E->getNumPlacementArgs();
1451  if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
1452    OS << "(";
1453    PrintExpr(E->getPlacementArg(0));
1454    for (unsigned i = 1; i < NumPlace; ++i) {
1455      if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1456        break;
1457      OS << ", ";
1458      PrintExpr(E->getPlacementArg(i));
1459    }
1460    OS << ") ";
1461  }
1462  if (E->isParenTypeId())
1463    OS << "(";
1464  std::string TypeS;
1465  if (Expr *Size = E->getArraySize()) {
1466    llvm::raw_string_ostream s(TypeS);
1467    s << '[';
1468    Size->printPretty(s, Helper, Policy);
1469    s << ']';
1470  }
1471  E->getAllocatedType().print(OS, Policy, TypeS);
1472  if (E->isParenTypeId())
1473    OS << ")";
1474
1475  CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1476  if (InitStyle) {
1477    if (InitStyle == CXXNewExpr::CallInit)
1478      OS << "(";
1479    PrintExpr(E->getInitializer());
1480    if (InitStyle == CXXNewExpr::CallInit)
1481      OS << ")";
1482  }
1483}
1484
1485void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1486  if (E->isGlobalDelete())
1487    OS << "::";
1488  OS << "delete ";
1489  if (E->isArrayForm())
1490    OS << "[] ";
1491  PrintExpr(E->getArgument());
1492}
1493
1494void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1495  PrintExpr(E->getBase());
1496  if (E->isArrow())
1497    OS << "->";
1498  else
1499    OS << '.';
1500  if (E->getQualifier())
1501    E->getQualifier()->print(OS, Policy);
1502  OS << "~";
1503
1504  if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1505    OS << II->getName();
1506  else
1507    E->getDestroyedType().print(OS, Policy);
1508}
1509
1510void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1511  if (E->isListInitialization())
1512    OS << "{ ";
1513
1514  for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1515    if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1516      // Don't print any defaulted arguments
1517      break;
1518    }
1519
1520    if (i) OS << ", ";
1521    PrintExpr(E->getArg(i));
1522  }
1523
1524  if (E->isListInitialization())
1525    OS << " }";
1526}
1527
1528void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
1529  // Just forward to the sub expression.
1530  PrintExpr(E->getSubExpr());
1531}
1532
1533void
1534StmtPrinter::VisitCXXUnresolvedConstructExpr(
1535                                           CXXUnresolvedConstructExpr *Node) {
1536  Node->getTypeAsWritten().print(OS, Policy);
1537  OS << "(";
1538  for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
1539                                             ArgEnd = Node->arg_end();
1540       Arg != ArgEnd; ++Arg) {
1541    if (Arg != Node->arg_begin())
1542      OS << ", ";
1543    PrintExpr(*Arg);
1544  }
1545  OS << ")";
1546}
1547
1548void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1549                                         CXXDependentScopeMemberExpr *Node) {
1550  if (!Node->isImplicitAccess()) {
1551    PrintExpr(Node->getBase());
1552    OS << (Node->isArrow() ? "->" : ".");
1553  }
1554  if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1555    Qualifier->print(OS, Policy);
1556  if (Node->hasTemplateKeyword())
1557    OS << "template ";
1558  OS << Node->getMemberNameInfo();
1559  if (Node->hasExplicitTemplateArgs())
1560    TemplateSpecializationType::PrintTemplateArgumentList(
1561        OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1562}
1563
1564void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
1565  if (!Node->isImplicitAccess()) {
1566    PrintExpr(Node->getBase());
1567    OS << (Node->isArrow() ? "->" : ".");
1568  }
1569  if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1570    Qualifier->print(OS, Policy);
1571  if (Node->hasTemplateKeyword())
1572    OS << "template ";
1573  OS << Node->getMemberNameInfo();
1574  if (Node->hasExplicitTemplateArgs())
1575    TemplateSpecializationType::PrintTemplateArgumentList(
1576        OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1577}
1578
1579static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1580  switch (UTT) {
1581  case UTT_HasNothrowAssign:      return "__has_nothrow_assign";
1582  case UTT_HasNothrowMoveAssign:  return "__has_nothrow_move_assign";
1583  case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1584  case UTT_HasNothrowCopy:          return "__has_nothrow_copy";
1585  case UTT_HasTrivialAssign:      return "__has_trivial_assign";
1586  case UTT_HasTrivialMoveAssign:      return "__has_trivial_move_assign";
1587  case UTT_HasTrivialMoveConstructor: return "__has_trivial_move_constructor";
1588  case UTT_HasTrivialDefaultConstructor: return "__has_trivial_constructor";
1589  case UTT_HasTrivialCopy:          return "__has_trivial_copy";
1590  case UTT_HasTrivialDestructor:  return "__has_trivial_destructor";
1591  case UTT_HasVirtualDestructor:  return "__has_virtual_destructor";
1592  case UTT_IsAbstract:            return "__is_abstract";
1593  case UTT_IsArithmetic:            return "__is_arithmetic";
1594  case UTT_IsArray:                 return "__is_array";
1595  case UTT_IsClass:               return "__is_class";
1596  case UTT_IsCompleteType:          return "__is_complete_type";
1597  case UTT_IsCompound:              return "__is_compound";
1598  case UTT_IsConst:                 return "__is_const";
1599  case UTT_IsEmpty:               return "__is_empty";
1600  case UTT_IsEnum:                return "__is_enum";
1601  case UTT_IsFinal:                 return "__is_final";
1602  case UTT_IsFloatingPoint:         return "__is_floating_point";
1603  case UTT_IsFunction:              return "__is_function";
1604  case UTT_IsFundamental:           return "__is_fundamental";
1605  case UTT_IsIntegral:              return "__is_integral";
1606  case UTT_IsInterfaceClass:        return "__is_interface_class";
1607  case UTT_IsLiteral:               return "__is_literal";
1608  case UTT_IsLvalueReference:       return "__is_lvalue_reference";
1609  case UTT_IsMemberFunctionPointer: return "__is_member_function_pointer";
1610  case UTT_IsMemberObjectPointer:   return "__is_member_object_pointer";
1611  case UTT_IsMemberPointer:         return "__is_member_pointer";
1612  case UTT_IsObject:                return "__is_object";
1613  case UTT_IsPOD:                 return "__is_pod";
1614  case UTT_IsPointer:               return "__is_pointer";
1615  case UTT_IsPolymorphic:         return "__is_polymorphic";
1616  case UTT_IsReference:             return "__is_reference";
1617  case UTT_IsRvalueReference:       return "__is_rvalue_reference";
1618  case UTT_IsScalar:                return "__is_scalar";
1619  case UTT_IsSigned:                return "__is_signed";
1620  case UTT_IsStandardLayout:        return "__is_standard_layout";
1621  case UTT_IsTrivial:               return "__is_trivial";
1622  case UTT_IsTriviallyCopyable:     return "__is_trivially_copyable";
1623  case UTT_IsUnion:               return "__is_union";
1624  case UTT_IsUnsigned:              return "__is_unsigned";
1625  case UTT_IsVoid:                  return "__is_void";
1626  case UTT_IsVolatile:              return "__is_volatile";
1627  }
1628  llvm_unreachable("Type trait not covered by switch statement");
1629}
1630
1631static const char *getTypeTraitName(BinaryTypeTrait BTT) {
1632  switch (BTT) {
1633  case BTT_IsBaseOf:              return "__is_base_of";
1634  case BTT_IsConvertible:         return "__is_convertible";
1635  case BTT_IsSame:                return "__is_same";
1636  case BTT_TypeCompatible:        return "__builtin_types_compatible_p";
1637  case BTT_IsConvertibleTo:       return "__is_convertible_to";
1638  case BTT_IsTriviallyAssignable: return "__is_trivially_assignable";
1639  }
1640  llvm_unreachable("Binary type trait not covered by switch");
1641}
1642
1643static const char *getTypeTraitName(TypeTrait TT) {
1644  switch (TT) {
1645  case clang::TT_IsTriviallyConstructible:return "__is_trivially_constructible";
1646  }
1647  llvm_unreachable("Type trait not covered by switch");
1648}
1649
1650static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1651  switch (ATT) {
1652  case ATT_ArrayRank:        return "__array_rank";
1653  case ATT_ArrayExtent:      return "__array_extent";
1654  }
1655  llvm_unreachable("Array type trait not covered by switch");
1656}
1657
1658static const char *getExpressionTraitName(ExpressionTrait ET) {
1659  switch (ET) {
1660  case ET_IsLValueExpr:      return "__is_lvalue_expr";
1661  case ET_IsRValueExpr:      return "__is_rvalue_expr";
1662  }
1663  llvm_unreachable("Expression type trait not covered by switch");
1664}
1665
1666void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1667  OS << getTypeTraitName(E->getTrait()) << '(';
1668  E->getQueriedType().print(OS, Policy);
1669  OS << ')';
1670}
1671
1672void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1673  OS << getTypeTraitName(E->getTrait()) << '(';
1674  E->getLhsType().print(OS, Policy);
1675  OS << ',';
1676  E->getRhsType().print(OS, Policy);
1677  OS << ')';
1678}
1679
1680void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1681  OS << getTypeTraitName(E->getTrait()) << "(";
1682  for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1683    if (I > 0)
1684      OS << ", ";
1685    E->getArg(I)->getType().print(OS, Policy);
1686  }
1687  OS << ")";
1688}
1689
1690void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
1691  OS << getTypeTraitName(E->getTrait()) << '(';
1692  E->getQueriedType().print(OS, Policy);
1693  OS << ')';
1694}
1695
1696void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
1697  OS << getExpressionTraitName(E->getTrait()) << '(';
1698  PrintExpr(E->getQueriedExpression());
1699  OS << ')';
1700}
1701
1702void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1703  OS << "noexcept(";
1704  PrintExpr(E->getOperand());
1705  OS << ")";
1706}
1707
1708void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
1709  PrintExpr(E->getPattern());
1710  OS << "...";
1711}
1712
1713void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
1714  OS << "sizeof...(" << *E->getPack() << ")";
1715}
1716
1717void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1718                                       SubstNonTypeTemplateParmPackExpr *Node) {
1719  OS << *Node->getParameterPack();
1720}
1721
1722void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1723                                       SubstNonTypeTemplateParmExpr *Node) {
1724  Visit(Node->getReplacement());
1725}
1726
1727void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
1728  OS << *E->getParameterPack();
1729}
1730
1731void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1732  PrintExpr(Node->GetTemporaryExpr());
1733}
1734
1735// Obj-C
1736
1737void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1738  OS << "@";
1739  VisitStringLiteral(Node->getString());
1740}
1741
1742void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
1743  OS << "@";
1744  Visit(E->getSubExpr());
1745}
1746
1747void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
1748  OS << "@[ ";
1749  StmtRange ch = E->children();
1750  if (ch.first != ch.second) {
1751    while (1) {
1752      Visit(*ch.first);
1753      ++ch.first;
1754      if (ch.first == ch.second) break;
1755      OS << ", ";
1756    }
1757  }
1758  OS << " ]";
1759}
1760
1761void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
1762  OS << "@{ ";
1763  for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
1764    if (I > 0)
1765      OS << ", ";
1766
1767    ObjCDictionaryElement Element = E->getKeyValueElement(I);
1768    Visit(Element.Key);
1769    OS << " : ";
1770    Visit(Element.Value);
1771    if (Element.isPackExpansion())
1772      OS << "...";
1773  }
1774  OS << " }";
1775}
1776
1777void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
1778  OS << "@encode(";
1779  Node->getEncodedType().print(OS, Policy);
1780  OS << ')';
1781}
1782
1783void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
1784  OS << "@selector(" << Node->getSelector().getAsString() << ')';
1785}
1786
1787void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
1788  OS << "@protocol(" << *Node->getProtocol() << ')';
1789}
1790
1791void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1792  OS << "[";
1793  switch (Mess->getReceiverKind()) {
1794  case ObjCMessageExpr::Instance:
1795    PrintExpr(Mess->getInstanceReceiver());
1796    break;
1797
1798  case ObjCMessageExpr::Class:
1799    Mess->getClassReceiver().print(OS, Policy);
1800    break;
1801
1802  case ObjCMessageExpr::SuperInstance:
1803  case ObjCMessageExpr::SuperClass:
1804    OS << "Super";
1805    break;
1806  }
1807
1808  OS << ' ';
1809  Selector selector = Mess->getSelector();
1810  if (selector.isUnarySelector()) {
1811    OS << selector.getNameForSlot(0);
1812  } else {
1813    for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
1814      if (i < selector.getNumArgs()) {
1815        if (i > 0) OS << ' ';
1816        if (selector.getIdentifierInfoForSlot(i))
1817          OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
1818        else
1819           OS << ":";
1820      }
1821      else OS << ", "; // Handle variadic methods.
1822
1823      PrintExpr(Mess->getArg(i));
1824    }
1825  }
1826  OS << "]";
1827}
1828
1829void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
1830  OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
1831}
1832
1833void
1834StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1835  PrintExpr(E->getSubExpr());
1836}
1837
1838void
1839StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
1840  OS << '(' << E->getBridgeKindName();
1841  E->getType().print(OS, Policy);
1842  OS << ')';
1843  PrintExpr(E->getSubExpr());
1844}
1845
1846void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
1847  BlockDecl *BD = Node->getBlockDecl();
1848  OS << "^";
1849
1850  const FunctionType *AFT = Node->getFunctionType();
1851
1852  if (isa<FunctionNoProtoType>(AFT)) {
1853    OS << "()";
1854  } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
1855    OS << '(';
1856    for (BlockDecl::param_iterator AI = BD->param_begin(),
1857         E = BD->param_end(); AI != E; ++AI) {
1858      if (AI != BD->param_begin()) OS << ", ";
1859      std::string ParamStr = (*AI)->getNameAsString();
1860      (*AI)->getType().print(OS, Policy, ParamStr);
1861    }
1862
1863    const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
1864    if (FT->isVariadic()) {
1865      if (!BD->param_empty()) OS << ", ";
1866      OS << "...";
1867    }
1868    OS << ')';
1869  }
1870  OS << "{ }";
1871}
1872
1873void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
1874  PrintExpr(Node->getSourceExpr());
1875}
1876
1877void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1878  OS << "__builtin_astype(";
1879  PrintExpr(Node->getSrcExpr());
1880  OS << ", ";
1881  Node->getType().print(OS, Policy);
1882  OS << ")";
1883}
1884
1885//===----------------------------------------------------------------------===//
1886// Stmt method implementations
1887//===----------------------------------------------------------------------===//
1888
1889void Stmt::dumpPretty(ASTContext &Context) const {
1890  printPretty(llvm::errs(), 0, PrintingPolicy(Context.getLangOpts()));
1891}
1892
1893void Stmt::printPretty(raw_ostream &OS,
1894                       PrinterHelper *Helper,
1895                       const PrintingPolicy &Policy,
1896                       unsigned Indentation) const {
1897  if (this == 0) {
1898    OS << "<NULL>";
1899    return;
1900  }
1901
1902  StmtPrinter P(OS, Helper, Policy, Indentation);
1903  P.Visit(const_cast<Stmt*>(this));
1904}
1905
1906//===----------------------------------------------------------------------===//
1907// PrinterHelper
1908//===----------------------------------------------------------------------===//
1909
1910// Implement virtual destructor.
1911PrinterHelper::~PrinterHelper() {}
1912