StmtPrinter.cpp revision 35183aca180a2b9b2c637cd625a40a7e147d6a32
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/PrettyPrinter.h"
19#include "llvm/Support/Compiler.h"
20#include "llvm/Support/Streams.h"
21#include "llvm/Support/Format.h"
22using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// StmtPrinter Visitor
26//===----------------------------------------------------------------------===//
27
28namespace  {
29  class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
30    llvm::raw_ostream &OS;
31    unsigned IndentLevel;
32    bool NoIndent;
33    clang::PrinterHelper* Helper;
34  public:
35    StmtPrinter(llvm::raw_ostream &os, PrinterHelper* helper, unsigned I=0,
36                bool noIndent=false) :
37      OS(os), IndentLevel(I), NoIndent(noIndent), Helper(helper) {}
38
39    void PrintStmt(Stmt *S, int SubIndent = 1) {
40      IndentLevel += SubIndent;
41      if (S && isa<Expr>(S)) {
42        // If this is an expr used in a stmt context, indent and newline it.
43        Indent();
44        Visit(S);
45        OS << ";\n";
46      } else if (S) {
47        Visit(S);
48      } else {
49        Indent() << "<<<NULL STATEMENT>>>\n";
50      }
51      IndentLevel -= SubIndent;
52    }
53
54    void PrintRawCompoundStmt(CompoundStmt *S);
55    void PrintRawDecl(Decl *D);
56    void PrintRawDeclStmt(DeclStmt *S);
57    void PrintFieldDecl(FieldDecl *FD);
58    void PrintRawIfStmt(IfStmt *If);
59    void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
60
61    void PrintExpr(Expr *E) {
62      if (E)
63        Visit(E);
64      else
65        OS << "<null expr>";
66    }
67
68    llvm::raw_ostream &Indent(int Delta = 0) {
69      if (!NoIndent) {
70        for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
71          OS << "  ";
72      } else NoIndent = false;
73      return OS;
74    }
75
76    bool PrintOffsetOfDesignator(Expr *E);
77    void VisitUnaryOffsetOf(UnaryOperator *Node);
78
79    void Visit(Stmt* S) {
80      if (Helper && Helper->handledStmt(S,OS))
81          return;
82      else StmtVisitor<StmtPrinter>::Visit(S);
83    }
84
85    void VisitStmt(Stmt *Node);
86#define STMT(CLASS, PARENT) \
87    void Visit##CLASS(CLASS *Node);
88#include "clang/AST/StmtNodes.def"
89  };
90}
91
92//===----------------------------------------------------------------------===//
93//  Stmt printing methods.
94//===----------------------------------------------------------------------===//
95
96void StmtPrinter::VisitStmt(Stmt *Node) {
97  Indent() << "<<unknown stmt type>>\n";
98}
99
100/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
101/// with no newline after the }.
102void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
103  OS << "{\n";
104  for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
105       I != E; ++I)
106    PrintStmt(*I);
107
108  Indent() << "}";
109}
110
111void StmtPrinter::PrintRawDecl(Decl *D) {
112  // FIXME: Need to complete/beautify this... this code simply shows the
113  // nodes are where they need to be.
114  if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
115    OS << "typedef " << localType->getUnderlyingType().getAsString();
116    OS << " " << localType->getNameAsString();
117  } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
118    // Emit storage class for vardecls.
119    if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
120      switch (V->getStorageClass()) {
121        default: assert(0 && "Unknown storage class!");
122        case VarDecl::None:          break;
123        case VarDecl::Extern:        OS << "extern "; break;
124        case VarDecl::Static:        OS << "static "; break;
125        case VarDecl::Auto:          OS << "auto "; break;
126        case VarDecl::Register:      OS << "register "; break;
127        case VarDecl::PrivateExtern: OS << "__private_extern "; break;
128      }
129    }
130
131    std::string Name = VD->getNameAsString();
132    VD->getType().getAsStringInternal(Name);
133    OS << Name;
134
135    // If this is a vardecl with an initializer, emit it.
136    if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
137      if (V->getInit()) {
138        OS << " = ";
139        PrintExpr(V->getInit());
140      }
141    }
142  } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
143    // print a free standing tag decl (e.g. "struct x;").
144    OS << TD->getKindName();
145    OS << " ";
146    if (const IdentifierInfo *II = TD->getIdentifier())
147      OS << II->getName();
148    if (RecordDecl *RD = dyn_cast<RecordDecl>(TD)) {
149      OS << "{\n";
150      IndentLevel += 1;
151      for (RecordDecl::field_iterator i = RD->field_begin(); i != RD->field_end(); ++i) {
152        PrintFieldDecl(*i);
153      IndentLevel -= 1;
154      }
155    }
156  } else {
157    assert(0 && "Unexpected decl");
158  }
159}
160
161void StmtPrinter::PrintFieldDecl(FieldDecl *FD) {
162  Indent() << FD->getNameAsString() << "\n";
163}
164
165void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
166  bool isFirst = true;
167
168  for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
169       I != E; ++I) {
170
171    if (!isFirst) OS << ", ";
172    else isFirst = false;
173
174    PrintRawDecl(*I);
175  }
176}
177
178void StmtPrinter::VisitNullStmt(NullStmt *Node) {
179  Indent() << ";\n";
180}
181
182void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
183  for (DeclStmt::decl_iterator I = Node->decl_begin(), E = Node->decl_end();
184       I!=E; ++I) {
185    Indent();
186    PrintRawDecl(*I);
187    OS << ";\n";
188  }
189}
190
191void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
192  Indent();
193  PrintRawCompoundStmt(Node);
194  OS << "\n";
195}
196
197void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
198  Indent(-1) << "case ";
199  PrintExpr(Node->getLHS());
200  if (Node->getRHS()) {
201    OS << " ... ";
202    PrintExpr(Node->getRHS());
203  }
204  OS << ":\n";
205
206  PrintStmt(Node->getSubStmt(), 0);
207}
208
209void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
210  Indent(-1) << "default:\n";
211  PrintStmt(Node->getSubStmt(), 0);
212}
213
214void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
215  Indent(-1) << Node->getName() << ":\n";
216  PrintStmt(Node->getSubStmt(), 0);
217}
218
219void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
220  OS << "if (";
221  PrintExpr(If->getCond());
222  OS << ')';
223
224  if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
225    OS << ' ';
226    PrintRawCompoundStmt(CS);
227    OS << (If->getElse() ? ' ' : '\n');
228  } else {
229    OS << '\n';
230    PrintStmt(If->getThen());
231    if (If->getElse()) Indent();
232  }
233
234  if (Stmt *Else = If->getElse()) {
235    OS << "else";
236
237    if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
238      OS << ' ';
239      PrintRawCompoundStmt(CS);
240      OS << '\n';
241    } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
242      OS << ' ';
243      PrintRawIfStmt(ElseIf);
244    } else {
245      OS << '\n';
246      PrintStmt(If->getElse());
247    }
248  }
249}
250
251void StmtPrinter::VisitIfStmt(IfStmt *If) {
252  Indent();
253  PrintRawIfStmt(If);
254}
255
256void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
257  Indent() << "switch (";
258  PrintExpr(Node->getCond());
259  OS << ")";
260
261  // Pretty print compoundstmt bodies (very common).
262  if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
263    OS << " ";
264    PrintRawCompoundStmt(CS);
265    OS << "\n";
266  } else {
267    OS << "\n";
268    PrintStmt(Node->getBody());
269  }
270}
271
272void StmtPrinter::VisitSwitchCase(SwitchCase*) {
273  assert(0 && "SwitchCase is an abstract class");
274}
275
276void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
277  Indent() << "while (";
278  PrintExpr(Node->getCond());
279  OS << ")\n";
280  PrintStmt(Node->getBody());
281}
282
283void StmtPrinter::VisitDoStmt(DoStmt *Node) {
284  Indent() << "do ";
285  if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
286    PrintRawCompoundStmt(CS);
287    OS << " ";
288  } else {
289    OS << "\n";
290    PrintStmt(Node->getBody());
291    Indent();
292  }
293
294  OS << "while ";
295  PrintExpr(Node->getCond());
296  OS << ";\n";
297}
298
299void StmtPrinter::VisitForStmt(ForStmt *Node) {
300  Indent() << "for (";
301  if (Node->getInit()) {
302    if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
303      PrintRawDeclStmt(DS);
304    else
305      PrintExpr(cast<Expr>(Node->getInit()));
306  }
307  OS << ";";
308  if (Node->getCond()) {
309    OS << " ";
310    PrintExpr(Node->getCond());
311  }
312  OS << ";";
313  if (Node->getInc()) {
314    OS << " ";
315    PrintExpr(Node->getInc());
316  }
317  OS << ") ";
318
319  if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
320    PrintRawCompoundStmt(CS);
321    OS << "\n";
322  } else {
323    OS << "\n";
324    PrintStmt(Node->getBody());
325  }
326}
327
328void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
329  Indent() << "for (";
330  if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
331    PrintRawDeclStmt(DS);
332  else
333    PrintExpr(cast<Expr>(Node->getElement()));
334  OS << " in ";
335  PrintExpr(Node->getCollection());
336  OS << ") ";
337
338  if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
339    PrintRawCompoundStmt(CS);
340    OS << "\n";
341  } else {
342    OS << "\n";
343    PrintStmt(Node->getBody());
344  }
345}
346
347void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
348  Indent() << "goto " << Node->getLabel()->getName() << ";\n";
349}
350
351void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
352  Indent() << "goto *";
353  PrintExpr(Node->getTarget());
354  OS << ";\n";
355}
356
357void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
358  Indent() << "continue;\n";
359}
360
361void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
362  Indent() << "break;\n";
363}
364
365
366void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
367  Indent() << "return";
368  if (Node->getRetValue()) {
369    OS << " ";
370    PrintExpr(Node->getRetValue());
371  }
372  OS << ";\n";
373}
374
375
376void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
377  Indent() << "asm ";
378
379  if (Node->isVolatile())
380    OS << "volatile ";
381
382  OS << "(";
383  VisitStringLiteral(Node->getAsmString());
384
385  // Outputs
386  if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
387      Node->getNumClobbers() != 0)
388    OS << " : ";
389
390  for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
391    if (i != 0)
392      OS << ", ";
393
394    if (!Node->getOutputName(i).empty()) {
395      OS << '[';
396      OS << Node->getOutputName(i);
397      OS << "] ";
398    }
399
400    VisitStringLiteral(Node->getOutputConstraint(i));
401    OS << " ";
402    Visit(Node->getOutputExpr(i));
403  }
404
405  // Inputs
406  if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
407    OS << " : ";
408
409  for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
410    if (i != 0)
411      OS << ", ";
412
413    if (!Node->getInputName(i).empty()) {
414      OS << '[';
415      OS << Node->getInputName(i);
416      OS << "] ";
417    }
418
419    VisitStringLiteral(Node->getInputConstraint(i));
420    OS << " ";
421    Visit(Node->getInputExpr(i));
422  }
423
424  // Clobbers
425  if (Node->getNumClobbers() != 0)
426    OS << " : ";
427
428  for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
429    if (i != 0)
430      OS << ", ";
431
432    VisitStringLiteral(Node->getClobber(i));
433  }
434
435  OS << ");\n";
436}
437
438void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
439  Indent() << "@try";
440  if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
441    PrintRawCompoundStmt(TS);
442    OS << "\n";
443  }
444
445  for (ObjCAtCatchStmt *catchStmt =
446         static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
447       catchStmt;
448       catchStmt =
449         static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
450    Indent() << "@catch(";
451    if (catchStmt->getCatchParamStmt()) {
452      if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt()))
453        PrintRawDeclStmt(DS);
454    }
455    OS << ")";
456    if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
457      {
458        PrintRawCompoundStmt(CS);
459        OS << "\n";
460      }
461  }
462
463  if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
464          Node->getFinallyStmt())) {
465    Indent() << "@finally";
466    PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
467    OS << "\n";
468  }
469}
470
471void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
472}
473
474void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
475  Indent() << "@catch (...) { /* todo */ } \n";
476}
477
478void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
479  Indent() << "@throw";
480  if (Node->getThrowExpr()) {
481    OS << " ";
482    PrintExpr(Node->getThrowExpr());
483  }
484  OS << ";\n";
485}
486
487void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
488  Indent() << "@synchronized (";
489  PrintExpr(Node->getSynchExpr());
490  OS << ")";
491  PrintRawCompoundStmt(Node->getSynchBody());
492  OS << "\n";
493}
494
495void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
496  OS << "catch (";
497  if (Decl *ExDecl = Node->getExceptionDecl())
498    PrintRawDecl(ExDecl);
499  else
500    OS << "...";
501  OS << ") ";
502  PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
503}
504
505void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
506  Indent();
507  PrintRawCXXCatchStmt(Node);
508  OS << "\n";
509}
510
511void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
512  Indent() << "try ";
513  PrintRawCompoundStmt(Node->getTryBlock());
514  for(unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
515    OS << " ";
516    PrintRawCXXCatchStmt(Node->getHandler(i));
517  }
518  OS << "\n";
519}
520
521//===----------------------------------------------------------------------===//
522//  Expr printing methods.
523//===----------------------------------------------------------------------===//
524
525void StmtPrinter::VisitExpr(Expr *Node) {
526  OS << "<<unknown expr type>>";
527}
528
529void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
530  OS << Node->getDecl()->getNameAsString();
531}
532
533void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) {
534  // FIXME: Should we keep enough information in QualifiedDeclRefExpr
535  // to produce the same qualification that the user wrote?
536  llvm::SmallVector<DeclContext *, 4> Contexts;
537
538  NamedDecl *D = Node->getDecl();
539
540  // Build up a stack of contexts.
541  DeclContext *Ctx = D->getDeclContext();
542  for (; Ctx; Ctx = Ctx->getParent())
543    if (!Ctx->isTransparentContext())
544      Contexts.push_back(Ctx);
545
546  while (!Contexts.empty()) {
547    DeclContext *Ctx = Contexts.back();
548    if (isa<TranslationUnitDecl>(Ctx))
549      OS << "::";
550    else if (NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
551      OS << ND->getNameAsString() << "::";
552    Contexts.pop_back();
553  }
554
555  OS << D->getNameAsString();
556}
557
558void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
559  if (Node->getBase()) {
560    PrintExpr(Node->getBase());
561    OS << (Node->isArrow() ? "->" : ".");
562  }
563  OS << Node->getDecl()->getNameAsString();
564}
565
566void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
567  if (Node->getBase()) {
568    PrintExpr(Node->getBase());
569    OS << ".";
570  }
571  OS << Node->getProperty()->getNameAsCString();
572}
573
574void StmtPrinter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
575  if (Node->getBase()) {
576    PrintExpr(Node->getBase());
577    OS << ".";
578  }
579  // FIXME: Setter/Getter names
580}
581
582void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
583  switch (Node->getIdentType()) {
584    default:
585      assert(0 && "unknown case");
586    case PredefinedExpr::Func:
587      OS << "__func__";
588      break;
589    case PredefinedExpr::Function:
590      OS << "__FUNCTION__";
591      break;
592    case PredefinedExpr::PrettyFunction:
593      OS << "__PRETTY_FUNCTION__";
594      break;
595  }
596}
597
598void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
599  unsigned value = Node->getValue();
600  if (Node->isWide())
601    OS << "L";
602  switch (value) {
603  case '\\':
604    OS << "'\\\\'";
605    break;
606  case '\'':
607    OS << "'\\''";
608    break;
609  case '\a':
610    // TODO: K&R: the meaning of '\\a' is different in traditional C
611    OS << "'\\a'";
612    break;
613  case '\b':
614    OS << "'\\b'";
615    break;
616  // Nonstandard escape sequence.
617  /*case '\e':
618    OS << "'\\e'";
619    break;*/
620  case '\f':
621    OS << "'\\f'";
622    break;
623  case '\n':
624    OS << "'\\n'";
625    break;
626  case '\r':
627    OS << "'\\r'";
628    break;
629  case '\t':
630    OS << "'\\t'";
631    break;
632  case '\v':
633    OS << "'\\v'";
634    break;
635  default:
636    if (value < 256 && isprint(value)) {
637      OS << "'" << (char)value << "'";
638    } else if (value < 256) {
639      OS << "'\\x" << llvm::format("%x", value) << "'";
640    } else {
641      // FIXME what to really do here?
642      OS << value;
643    }
644  }
645}
646
647void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
648  bool isSigned = Node->getType()->isSignedIntegerType();
649  OS << Node->getValue().toString(10, isSigned);
650
651  // Emit suffixes.  Integer literals are always a builtin integer type.
652  switch (Node->getType()->getAsBuiltinType()->getKind()) {
653  default: assert(0 && "Unexpected type for integer literal!");
654  case BuiltinType::Int:       break; // no suffix.
655  case BuiltinType::UInt:      OS << 'U'; break;
656  case BuiltinType::Long:      OS << 'L'; break;
657  case BuiltinType::ULong:     OS << "UL"; break;
658  case BuiltinType::LongLong:  OS << "LL"; break;
659  case BuiltinType::ULongLong: OS << "ULL"; break;
660  }
661}
662void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
663  // FIXME: print value more precisely.
664  OS << Node->getValueAsApproximateDouble();
665}
666
667void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
668  PrintExpr(Node->getSubExpr());
669  OS << "i";
670}
671
672void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
673  if (Str->isWide()) OS << 'L';
674  OS << '"';
675
676  // FIXME: this doesn't print wstrings right.
677  for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
678    unsigned char Char = Str->getStrData()[i];
679
680    switch (Char) {
681    default:
682      if (isprint(Char))
683        OS << (char)Char;
684      else  // Output anything hard as an octal escape.
685        OS << '\\'
686        << (char)('0'+ ((Char >> 6) & 7))
687        << (char)('0'+ ((Char >> 3) & 7))
688        << (char)('0'+ ((Char >> 0) & 7));
689      break;
690    // Handle some common non-printable cases to make dumps prettier.
691    case '\\': OS << "\\\\"; break;
692    case '"': OS << "\\\""; break;
693    case '\n': OS << "\\n"; break;
694    case '\t': OS << "\\t"; break;
695    case '\a': OS << "\\a"; break;
696    case '\b': OS << "\\b"; break;
697    }
698  }
699  OS << '"';
700}
701void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
702  OS << "(";
703  PrintExpr(Node->getSubExpr());
704  OS << ")";
705}
706void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
707  if (!Node->isPostfix()) {
708    OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
709
710    // Print a space if this is an "identifier operator" like __real.
711    switch (Node->getOpcode()) {
712    default: break;
713    case UnaryOperator::Real:
714    case UnaryOperator::Imag:
715    case UnaryOperator::Extension:
716      OS << ' ';
717      break;
718    }
719  }
720  PrintExpr(Node->getSubExpr());
721
722  if (Node->isPostfix())
723    OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
724}
725
726bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
727  if (isa<UnaryOperator>(E)) {
728    // Base case, print the type and comma.
729    OS << E->getType().getAsString() << ", ";
730    return true;
731  } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
732    PrintOffsetOfDesignator(ASE->getLHS());
733    OS << "[";
734    PrintExpr(ASE->getRHS());
735    OS << "]";
736    return false;
737  } else {
738    MemberExpr *ME = cast<MemberExpr>(E);
739    bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
740    OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
741    return false;
742  }
743}
744
745void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
746  OS << "__builtin_offsetof(";
747  PrintOffsetOfDesignator(Node->getSubExpr());
748  OS << ")";
749}
750
751void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
752  OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
753  if (Node->isArgumentType())
754    OS << "(" << Node->getArgumentType().getAsString() << ")";
755  else {
756    OS << " ";
757    PrintExpr(Node->getArgumentExpr());
758  }
759}
760void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
761  PrintExpr(Node->getLHS());
762  OS << "[";
763  PrintExpr(Node->getRHS());
764  OS << "]";
765}
766
767void StmtPrinter::VisitCallExpr(CallExpr *Call) {
768  PrintExpr(Call->getCallee());
769  OS << "(";
770  for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
771    if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
772      // Don't print any defaulted arguments
773      break;
774    }
775
776    if (i) OS << ", ";
777    PrintExpr(Call->getArg(i));
778  }
779  OS << ")";
780}
781void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
782  // FIXME: Suppress printing implicit bases (like "this")
783  PrintExpr(Node->getBase());
784  OS << (Node->isArrow() ? "->" : ".");
785  // FIXME: Suppress printing references to unnamed objects
786  // representing anonymous unions/structs
787  OS << Node->getMemberDecl()->getNameAsString();
788}
789void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
790  PrintExpr(Node->getBase());
791  OS << ".";
792  OS << Node->getAccessor().getName();
793}
794void StmtPrinter::VisitCastExpr(CastExpr *) {
795  assert(0 && "CastExpr is an abstract class");
796}
797void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
798  assert(0 && "ExplicitCastExpr is an abstract class");
799}
800void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
801  OS << "(" << Node->getType().getAsString() << ")";
802  PrintExpr(Node->getSubExpr());
803}
804void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
805  OS << "(" << Node->getType().getAsString() << ")";
806  PrintExpr(Node->getInitializer());
807}
808void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
809  // No need to print anything, simply forward to the sub expression.
810  PrintExpr(Node->getSubExpr());
811}
812void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
813  PrintExpr(Node->getLHS());
814  OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
815  PrintExpr(Node->getRHS());
816}
817void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
818  PrintExpr(Node->getLHS());
819  OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
820  PrintExpr(Node->getRHS());
821}
822void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
823  PrintExpr(Node->getCond());
824
825  if (Node->getLHS()) {
826    OS << " ? ";
827    PrintExpr(Node->getLHS());
828    OS << " : ";
829  }
830  else { // Handle GCC extention where LHS can be NULL.
831    OS << " ?: ";
832  }
833
834  PrintExpr(Node->getRHS());
835}
836
837// GNU extensions.
838
839void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
840  OS << "&&" << Node->getLabel()->getName();
841}
842
843void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
844  OS << "(";
845  PrintRawCompoundStmt(E->getSubStmt());
846  OS << ")";
847}
848
849void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
850  OS << "__builtin_types_compatible_p(";
851  OS << Node->getArgType1().getAsString() << ",";
852  OS << Node->getArgType2().getAsString() << ")";
853}
854
855void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
856  OS << "__builtin_choose_expr(";
857  PrintExpr(Node->getCond());
858  OS << ", ";
859  PrintExpr(Node->getLHS());
860  OS << ", ";
861  PrintExpr(Node->getRHS());
862  OS << ")";
863}
864
865void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
866  OS << "__null";
867}
868
869void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
870  OS << "__builtin_shufflevector(";
871  for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
872    if (i) OS << ", ";
873    PrintExpr(Node->getExpr(i));
874  }
875  OS << ")";
876}
877
878void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
879  OS << "{ ";
880  for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
881    if (i) OS << ", ";
882    if (Node->getInit(i))
883      PrintExpr(Node->getInit(i));
884    else
885      OS << "0";
886  }
887  OS << " }";
888}
889
890void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
891  for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
892                      DEnd = Node->designators_end();
893       D != DEnd; ++D) {
894    if (D->isFieldDesignator()) {
895      if (D->getDotLoc().isInvalid())
896        OS << D->getFieldName()->getName() << ":";
897      else
898        OS << "." << D->getFieldName()->getName();
899    } else {
900      OS << "[";
901      if (D->isArrayDesignator()) {
902        PrintExpr(Node->getArrayIndex(*D));
903      } else {
904        PrintExpr(Node->getArrayRangeStart(*D));
905        OS << " ... ";
906        PrintExpr(Node->getArrayRangeEnd(*D));
907      }
908      OS << "]";
909    }
910  }
911
912  OS << " = ";
913  PrintExpr(Node->getInit());
914}
915
916void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
917  OS << "/*implicit*/" << Node->getType().getAsString() << "()";
918}
919
920void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
921  OS << "va_arg(";
922  PrintExpr(Node->getSubExpr());
923  OS << ", ";
924  OS << Node->getType().getAsString();
925  OS << ")";
926}
927
928// C++
929void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
930  const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
931    "",
932#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
933    Spelling,
934#include "clang/Basic/OperatorKinds.def"
935  };
936
937  OverloadedOperatorKind Kind = Node->getOperator();
938  if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
939    if (Node->getNumArgs() == 1) {
940      OS << OpStrings[Kind] << ' ';
941      PrintExpr(Node->getArg(0));
942    } else {
943      PrintExpr(Node->getArg(0));
944      OS << ' ' << OpStrings[Kind];
945    }
946  } else if (Kind == OO_Call) {
947    PrintExpr(Node->getArg(0));
948    OS << '(';
949    for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
950      if (ArgIdx > 1)
951        OS << ", ";
952      if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
953        PrintExpr(Node->getArg(ArgIdx));
954    }
955    OS << ')';
956  } else if (Kind == OO_Subscript) {
957    PrintExpr(Node->getArg(0));
958    OS << '[';
959    PrintExpr(Node->getArg(1));
960    OS << ']';
961  } else if (Node->getNumArgs() == 1) {
962    OS << OpStrings[Kind] << ' ';
963    PrintExpr(Node->getArg(0));
964  } else if (Node->getNumArgs() == 2) {
965    PrintExpr(Node->getArg(0));
966    OS << ' ' << OpStrings[Kind] << ' ';
967    PrintExpr(Node->getArg(1));
968  } else {
969    assert(false && "unknown overloaded operator");
970  }
971}
972
973void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
974  VisitCallExpr(cast<CallExpr>(Node));
975}
976
977void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
978  OS << Node->getCastName() << '<';
979  OS << Node->getTypeAsWritten().getAsString() << ">(";
980  PrintExpr(Node->getSubExpr());
981  OS << ")";
982}
983
984void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
985  VisitCXXNamedCastExpr(Node);
986}
987
988void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
989  VisitCXXNamedCastExpr(Node);
990}
991
992void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
993  VisitCXXNamedCastExpr(Node);
994}
995
996void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
997  VisitCXXNamedCastExpr(Node);
998}
999
1000void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1001  OS << "typeid(";
1002  if (Node->isTypeOperand()) {
1003    OS << Node->getTypeOperand().getAsString();
1004  } else {
1005    PrintExpr(Node->getExprOperand());
1006  }
1007  OS << ")";
1008}
1009
1010void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1011  OS << (Node->getValue() ? "true" : "false");
1012}
1013
1014void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1015  OS << "this";
1016}
1017
1018void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1019  if (Node->getSubExpr() == 0)
1020    OS << "throw";
1021  else {
1022    OS << "throw ";
1023    PrintExpr(Node->getSubExpr());
1024  }
1025}
1026
1027void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1028  // Nothing to print: we picked up the default argument
1029}
1030
1031void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1032  OS << Node->getType().getAsString();
1033  OS << "(";
1034  PrintExpr(Node->getSubExpr());
1035  OS << ")";
1036}
1037
1038void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1039  OS << Node->getType().getAsString();
1040  OS << "(";
1041  for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1042                                         ArgEnd = Node->arg_end();
1043       Arg != ArgEnd; ++Arg) {
1044    if (Arg != Node->arg_begin())
1045      OS << ", ";
1046    PrintExpr(*Arg);
1047  }
1048  OS << ")";
1049}
1050
1051void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1052  OS << Node->getType().getAsString() << "()";
1053}
1054
1055void
1056StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
1057  PrintRawDecl(E->getVarDecl());
1058}
1059
1060void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1061  if (E->isGlobalNew())
1062    OS << "::";
1063  OS << "new ";
1064  unsigned NumPlace = E->getNumPlacementArgs();
1065  if (NumPlace > 0) {
1066    OS << "(";
1067    PrintExpr(E->getPlacementArg(0));
1068    for (unsigned i = 1; i < NumPlace; ++i) {
1069      OS << ", ";
1070      PrintExpr(E->getPlacementArg(i));
1071    }
1072    OS << ") ";
1073  }
1074  if (E->isParenTypeId())
1075    OS << "(";
1076  std::string TypeS;
1077  if (Expr *Size = E->getArraySize()) {
1078    llvm::raw_string_ostream s(TypeS);
1079    Size->printPretty(s);
1080    s.flush();
1081    TypeS = "[" + TypeS + "]";
1082  }
1083  E->getAllocatedType().getAsStringInternal(TypeS);
1084  OS << TypeS;
1085  if (E->isParenTypeId())
1086    OS << ")";
1087
1088  if (E->hasInitializer()) {
1089    OS << "(";
1090    unsigned NumCons = E->getNumConstructorArgs();
1091    if (NumCons > 0) {
1092      PrintExpr(E->getConstructorArg(0));
1093      for (unsigned i = 1; i < NumCons; ++i) {
1094        OS << ", ";
1095        PrintExpr(E->getConstructorArg(i));
1096      }
1097    }
1098    OS << ")";
1099  }
1100}
1101
1102void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1103  if (E->isGlobalDelete())
1104    OS << "::";
1105  OS << "delete ";
1106  if (E->isArrayForm())
1107    OS << "[] ";
1108  PrintExpr(E->getArgument());
1109}
1110
1111void StmtPrinter::VisitUnresolvedFunctionNameExpr(UnresolvedFunctionNameExpr *E) {
1112  OS << E->getName().getAsString();
1113}
1114
1115static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1116  switch (UTT) {
1117  default: assert(false && "Unknown type trait");
1118  case UTT_HasNothrowAssign:      return "__has_nothrow_assign";
1119  case UTT_HasNothrowCopy:        return "__has_nothrow_copy";
1120  case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1121  case UTT_HasTrivialAssign:      return "__has_trivial_assign";
1122  case UTT_HasTrivialCopy:        return "__has_trivial_copy";
1123  case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1124  case UTT_HasTrivialDestructor:  return "__has_trivial_destructor";
1125  case UTT_HasVirtualDestructor:  return "__has_virtual_destructor";
1126  case UTT_IsAbstract:            return "__is_abstract";
1127  case UTT_IsClass:               return "__is_class";
1128  case UTT_IsEmpty:               return "__is_empty";
1129  case UTT_IsEnum:                return "__is_enum";
1130  case UTT_IsPOD:                 return "__is_pod";
1131  case UTT_IsPolymorphic:         return "__is_polymorphic";
1132  case UTT_IsUnion:               return "__is_union";
1133  }
1134}
1135
1136void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1137  OS << getTypeTraitName(E->getTrait()) << "("
1138     << E->getQueriedType().getAsString() << ")";
1139}
1140
1141// Obj-C
1142
1143void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1144  OS << "@";
1145  VisitStringLiteral(Node->getString());
1146}
1147
1148void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
1149  OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
1150}
1151
1152void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
1153  OS << "@selector(" << Node->getSelector().getAsString() << ')';
1154}
1155
1156void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
1157  OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
1158}
1159
1160void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1161  OS << "[";
1162  Expr *receiver = Mess->getReceiver();
1163  if (receiver) PrintExpr(receiver);
1164  else OS << Mess->getClassName()->getName();
1165  OS << ' ';
1166  Selector selector = Mess->getSelector();
1167  if (selector.isUnarySelector()) {
1168    OS << selector.getIdentifierInfoForSlot(0)->getName();
1169  } else {
1170    for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
1171      if (i < selector.getNumArgs()) {
1172        if (i > 0) OS << ' ';
1173        if (selector.getIdentifierInfoForSlot(i))
1174          OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
1175        else
1176           OS << ":";
1177      }
1178      else OS << ", "; // Handle variadic methods.
1179
1180      PrintExpr(Mess->getArg(i));
1181    }
1182  }
1183  OS << "]";
1184}
1185
1186void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1187  OS << "super";
1188}
1189
1190void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
1191  BlockDecl *BD = Node->getBlockDecl();
1192  OS << "^";
1193
1194  const FunctionType *AFT = Node->getFunctionType();
1195
1196  if (isa<FunctionNoProtoType>(AFT)) {
1197    OS << "()";
1198  } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
1199    OS << '(';
1200    std::string ParamStr;
1201    for (BlockDecl::param_iterator AI = BD->param_begin(),
1202         E = BD->param_end(); AI != E; ++AI) {
1203      if (AI != BD->param_begin()) OS << ", ";
1204      ParamStr = (*AI)->getNameAsString();
1205      (*AI)->getType().getAsStringInternal(ParamStr);
1206      OS << ParamStr;
1207    }
1208
1209    const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
1210    if (FT->isVariadic()) {
1211      if (!BD->param_empty()) OS << ", ";
1212      OS << "...";
1213    }
1214    OS << ')';
1215  }
1216}
1217
1218void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
1219  OS << Node->getDecl()->getNameAsString();
1220}
1221//===----------------------------------------------------------------------===//
1222// Stmt method implementations
1223//===----------------------------------------------------------------------===//
1224
1225void Stmt::dumpPretty() const {
1226  llvm::raw_ostream &OS = llvm::errs();
1227  printPretty(OS);
1228  OS.flush();
1229}
1230
1231void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper,
1232                       unsigned I, bool NoIndent) const {
1233  if (this == 0) {
1234    OS << "<NULL>";
1235    return;
1236  }
1237
1238  StmtPrinter P(OS, Helper, I, NoIndent);
1239  P.Visit(const_cast<Stmt*>(this));
1240}
1241
1242//===----------------------------------------------------------------------===//
1243// PrinterHelper
1244//===----------------------------------------------------------------------===//
1245
1246// Implement virtual destructor.
1247PrinterHelper::~PrinterHelper() {}
1248