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