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