1193326Sed//===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10193326Sed// This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which
11193326Sed// pretty print the AST back out to C code.
12193326Sed//
13193326Sed//===----------------------------------------------------------------------===//
14193326Sed
15239462Sdim#include "clang/AST/ASTContext.h"
16249423Sdim#include "clang/AST/Attr.h"
17193326Sed#include "clang/AST/DeclCXX.h"
18193326Sed#include "clang/AST/DeclObjC.h"
19218893Sdim#include "clang/AST/DeclTemplate.h"
20207619Srdivacky#include "clang/AST/Expr.h"
21218893Sdim#include "clang/AST/ExprCXX.h"
22249423Sdim#include "clang/AST/PrettyPrinter.h"
23249423Sdim#include "clang/AST/StmtVisitor.h"
24249423Sdim#include "clang/Basic/CharInfo.h"
25234353Sdim#include "llvm/ADT/SmallString.h"
26249423Sdim#include "llvm/Support/Format.h"
27193326Sedusing namespace clang;
28193326Sed
29193326Sed//===----------------------------------------------------------------------===//
30193326Sed// StmtPrinter Visitor
31193326Sed//===----------------------------------------------------------------------===//
32193326Sed
33193326Sednamespace  {
34199990Srdivacky  class StmtPrinter : public StmtVisitor<StmtPrinter> {
35226633Sdim    raw_ostream &OS;
36193326Sed    unsigned IndentLevel;
37193326Sed    clang::PrinterHelper* Helper;
38193326Sed    PrintingPolicy Policy;
39193326Sed
40193326Sed  public:
41239462Sdim    StmtPrinter(raw_ostream &os, PrinterHelper* helper,
42195341Sed                const PrintingPolicy &Policy,
43193326Sed                unsigned Indentation = 0)
44239462Sdim      : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy) {}
45198092Srdivacky
46193326Sed    void PrintStmt(Stmt *S) {
47193326Sed      PrintStmt(S, Policy.Indentation);
48193326Sed    }
49193326Sed
50193326Sed    void PrintStmt(Stmt *S, int SubIndent) {
51193326Sed      IndentLevel += SubIndent;
52193326Sed      if (S && isa<Expr>(S)) {
53193326Sed        // If this is an expr used in a stmt context, indent and newline it.
54193326Sed        Indent();
55193326Sed        Visit(S);
56193326Sed        OS << ";\n";
57193326Sed      } else if (S) {
58193326Sed        Visit(S);
59193326Sed      } else {
60193326Sed        Indent() << "<<<NULL STATEMENT>>>\n";
61193326Sed      }
62193326Sed      IndentLevel -= SubIndent;
63193326Sed    }
64193326Sed
65193326Sed    void PrintRawCompoundStmt(CompoundStmt *S);
66193326Sed    void PrintRawDecl(Decl *D);
67243830Sdim    void PrintRawDeclStmt(const DeclStmt *S);
68193326Sed    void PrintRawIfStmt(IfStmt *If);
69193326Sed    void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
70218893Sdim    void PrintCallArgs(CallExpr *E);
71221345Sdim    void PrintRawSEHExceptHandler(SEHExceptStmt *S);
72221345Sdim    void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
73198092Srdivacky
74193326Sed    void PrintExpr(Expr *E) {
75193326Sed      if (E)
76193326Sed        Visit(E);
77193326Sed      else
78193326Sed        OS << "<null expr>";
79193326Sed    }
80198092Srdivacky
81226633Sdim    raw_ostream &Indent(int Delta = 0) {
82193326Sed      for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
83193326Sed        OS << "  ";
84193326Sed      return OS;
85193326Sed    }
86198092Srdivacky
87198092Srdivacky    void Visit(Stmt* S) {
88193326Sed      if (Helper && Helper->handledStmt(S,OS))
89193326Sed          return;
90193326Sed      else StmtVisitor<StmtPrinter>::Visit(S);
91193326Sed    }
92212904Sdim
93218893Sdim    void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
94212904Sdim      Indent() << "<<unknown stmt type>>\n";
95212904Sdim    }
96218893Sdim    void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
97212904Sdim      OS << "<<unknown expr type>>";
98212904Sdim    }
99212904Sdim    void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
100198092Srdivacky
101212904Sdim#define ABSTRACT_STMT(CLASS)
102193326Sed#define STMT(CLASS, PARENT) \
103193326Sed    void Visit##CLASS(CLASS *Node);
104208600Srdivacky#include "clang/AST/StmtNodes.inc"
105193326Sed  };
106193326Sed}
107193326Sed
108193326Sed//===----------------------------------------------------------------------===//
109193326Sed//  Stmt printing methods.
110193326Sed//===----------------------------------------------------------------------===//
111193326Sed
112193326Sed/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
113193326Sed/// with no newline after the }.
114193326Sedvoid StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
115193326Sed  OS << "{\n";
116193326Sed  for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
117193326Sed       I != E; ++I)
118193326Sed    PrintStmt(*I);
119198092Srdivacky
120193326Sed  Indent() << "}";
121193326Sed}
122193326Sed
123193326Sedvoid StmtPrinter::PrintRawDecl(Decl *D) {
124195341Sed  D->print(OS, Policy, IndentLevel);
125193326Sed}
126193326Sed
127243830Sdimvoid StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
128243830Sdim  DeclStmt::const_decl_iterator Begin = S->decl_begin(), End = S->decl_end();
129226633Sdim  SmallVector<Decl*, 2> Decls;
130198092Srdivacky  for ( ; Begin != End; ++Begin)
131193326Sed    Decls.push_back(*Begin);
132193326Sed
133195341Sed  Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
134193326Sed}
135193326Sed
136193326Sedvoid StmtPrinter::VisitNullStmt(NullStmt *Node) {
137193326Sed  Indent() << ";\n";
138193326Sed}
139193326Sed
140193326Sedvoid StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
141193326Sed  Indent();
142193326Sed  PrintRawDeclStmt(Node);
143193326Sed  OS << ";\n";
144193326Sed}
145193326Sed
146193326Sedvoid StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
147193326Sed  Indent();
148193326Sed  PrintRawCompoundStmt(Node);
149193326Sed  OS << "\n";
150193326Sed}
151193326Sed
152193326Sedvoid StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
153193326Sed  Indent(-1) << "case ";
154193326Sed  PrintExpr(Node->getLHS());
155193326Sed  if (Node->getRHS()) {
156193326Sed    OS << " ... ";
157193326Sed    PrintExpr(Node->getRHS());
158193326Sed  }
159193326Sed  OS << ":\n";
160198092Srdivacky
161193326Sed  PrintStmt(Node->getSubStmt(), 0);
162193326Sed}
163193326Sed
164193326Sedvoid StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
165193326Sed  Indent(-1) << "default:\n";
166193326Sed  PrintStmt(Node->getSubStmt(), 0);
167193326Sed}
168193326Sed
169193326Sedvoid StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
170193326Sed  Indent(-1) << Node->getName() << ":\n";
171193326Sed  PrintStmt(Node->getSubStmt(), 0);
172193326Sed}
173193326Sed
174234982Sdimvoid StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
175234982Sdim  OS << "[[";
176234982Sdim  bool first = true;
177239462Sdim  for (ArrayRef<const Attr*>::iterator it = Node->getAttrs().begin(),
178239462Sdim                                       end = Node->getAttrs().end();
179239462Sdim                                       it != end; ++it) {
180234982Sdim    if (!first) {
181234982Sdim      OS << ", ";
182234982Sdim      first = false;
183234982Sdim    }
184234982Sdim    // TODO: check this
185239462Sdim    (*it)->printPretty(OS, Policy);
186234982Sdim  }
187234982Sdim  OS << "]] ";
188234982Sdim  PrintStmt(Node->getSubStmt(), 0);
189234982Sdim}
190234982Sdim
191193326Sedvoid StmtPrinter::PrintRawIfStmt(IfStmt *If) {
192193326Sed  OS << "if (";
193243830Sdim  if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
194243830Sdim    PrintRawDeclStmt(DS);
195243830Sdim  else
196243830Sdim    PrintExpr(If->getCond());
197193326Sed  OS << ')';
198198092Srdivacky
199193326Sed  if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
200193326Sed    OS << ' ';
201193326Sed    PrintRawCompoundStmt(CS);
202193326Sed    OS << (If->getElse() ? ' ' : '\n');
203193326Sed  } else {
204193326Sed    OS << '\n';
205193326Sed    PrintStmt(If->getThen());
206193326Sed    if (If->getElse()) Indent();
207193326Sed  }
208198092Srdivacky
209193326Sed  if (Stmt *Else = If->getElse()) {
210193326Sed    OS << "else";
211198092Srdivacky
212193326Sed    if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
213193326Sed      OS << ' ';
214193326Sed      PrintRawCompoundStmt(CS);
215193326Sed      OS << '\n';
216193326Sed    } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
217193326Sed      OS << ' ';
218193326Sed      PrintRawIfStmt(ElseIf);
219193326Sed    } else {
220193326Sed      OS << '\n';
221193326Sed      PrintStmt(If->getElse());
222193326Sed    }
223193326Sed  }
224193326Sed}
225193326Sed
226193326Sedvoid StmtPrinter::VisitIfStmt(IfStmt *If) {
227193326Sed  Indent();
228193326Sed  PrintRawIfStmt(If);
229193326Sed}
230193326Sed
231193326Sedvoid StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
232193326Sed  Indent() << "switch (";
233243830Sdim  if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
234243830Sdim    PrintRawDeclStmt(DS);
235243830Sdim  else
236243830Sdim    PrintExpr(Node->getCond());
237193326Sed  OS << ")";
238198092Srdivacky
239193326Sed  // Pretty print compoundstmt bodies (very common).
240193326Sed  if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
241193326Sed    OS << " ";
242193326Sed    PrintRawCompoundStmt(CS);
243193326Sed    OS << "\n";
244193326Sed  } else {
245193326Sed    OS << "\n";
246193326Sed    PrintStmt(Node->getBody());
247193326Sed  }
248193326Sed}
249193326Sed
250193326Sedvoid StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
251193326Sed  Indent() << "while (";
252243830Sdim  if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
253243830Sdim    PrintRawDeclStmt(DS);
254243830Sdim  else
255243830Sdim    PrintExpr(Node->getCond());
256193326Sed  OS << ")\n";
257193326Sed  PrintStmt(Node->getBody());
258193326Sed}
259193326Sed
260193326Sedvoid StmtPrinter::VisitDoStmt(DoStmt *Node) {
261193326Sed  Indent() << "do ";
262193326Sed  if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
263193326Sed    PrintRawCompoundStmt(CS);
264193326Sed    OS << " ";
265193326Sed  } else {
266193326Sed    OS << "\n";
267193326Sed    PrintStmt(Node->getBody());
268193326Sed    Indent();
269193326Sed  }
270198092Srdivacky
271193326Sed  OS << "while (";
272193326Sed  PrintExpr(Node->getCond());
273193326Sed  OS << ");\n";
274193326Sed}
275193326Sed
276193326Sedvoid StmtPrinter::VisitForStmt(ForStmt *Node) {
277193326Sed  Indent() << "for (";
278193326Sed  if (Node->getInit()) {
279193326Sed    if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
280193326Sed      PrintRawDeclStmt(DS);
281193326Sed    else
282193326Sed      PrintExpr(cast<Expr>(Node->getInit()));
283193326Sed  }
284193326Sed  OS << ";";
285193326Sed  if (Node->getCond()) {
286193326Sed    OS << " ";
287193326Sed    PrintExpr(Node->getCond());
288193326Sed  }
289193326Sed  OS << ";";
290193326Sed  if (Node->getInc()) {
291193326Sed    OS << " ";
292193326Sed    PrintExpr(Node->getInc());
293193326Sed  }
294193326Sed  OS << ") ";
295198092Srdivacky
296193326Sed  if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
297193326Sed    PrintRawCompoundStmt(CS);
298193326Sed    OS << "\n";
299193326Sed  } else {
300193326Sed    OS << "\n";
301193326Sed    PrintStmt(Node->getBody());
302193326Sed  }
303193326Sed}
304193326Sed
305193326Sedvoid StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
306193326Sed  Indent() << "for (";
307193326Sed  if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
308193326Sed    PrintRawDeclStmt(DS);
309193326Sed  else
310193326Sed    PrintExpr(cast<Expr>(Node->getElement()));
311193326Sed  OS << " in ";
312193326Sed  PrintExpr(Node->getCollection());
313193326Sed  OS << ") ";
314198092Srdivacky
315193326Sed  if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
316193326Sed    PrintRawCompoundStmt(CS);
317193326Sed    OS << "\n";
318193326Sed  } else {
319193326Sed    OS << "\n";
320193326Sed    PrintStmt(Node->getBody());
321193326Sed  }
322193326Sed}
323193326Sed
324221345Sdimvoid StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
325221345Sdim  Indent() << "for (";
326221345Sdim  PrintingPolicy SubPolicy(Policy);
327221345Sdim  SubPolicy.SuppressInitializers = true;
328221345Sdim  Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
329221345Sdim  OS << " : ";
330221345Sdim  PrintExpr(Node->getRangeInit());
331221345Sdim  OS << ") {\n";
332221345Sdim  PrintStmt(Node->getBody());
333221345Sdim  Indent() << "}\n";
334221345Sdim}
335221345Sdim
336234353Sdimvoid StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
337234353Sdim  Indent();
338234353Sdim  if (Node->isIfExists())
339234353Sdim    OS << "__if_exists (";
340234353Sdim  else
341234353Sdim    OS << "__if_not_exists (";
342234353Sdim
343234353Sdim  if (NestedNameSpecifier *Qualifier
344234353Sdim        = Node->getQualifierLoc().getNestedNameSpecifier())
345234353Sdim    Qualifier->print(OS, Policy);
346234353Sdim
347234353Sdim  OS << Node->getNameInfo() << ") ";
348234353Sdim
349234353Sdim  PrintRawCompoundStmt(Node->getSubStmt());
350234353Sdim}
351234353Sdim
352193326Sedvoid StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
353193326Sed  Indent() << "goto " << Node->getLabel()->getName() << ";\n";
354193326Sed}
355193326Sed
356193326Sedvoid StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
357193326Sed  Indent() << "goto *";
358193326Sed  PrintExpr(Node->getTarget());
359193326Sed  OS << ";\n";
360193326Sed}
361193326Sed
362193326Sedvoid StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
363193326Sed  Indent() << "continue;\n";
364193326Sed}
365193326Sed
366193326Sedvoid StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
367193326Sed  Indent() << "break;\n";
368193326Sed}
369193326Sed
370193326Sed
371193326Sedvoid StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
372193326Sed  Indent() << "return";
373193326Sed  if (Node->getRetValue()) {
374193326Sed    OS << " ";
375193326Sed    PrintExpr(Node->getRetValue());
376193326Sed  }
377193326Sed  OS << ";\n";
378193326Sed}
379193326Sed
380193326Sed
381243830Sdimvoid StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
382193326Sed  Indent() << "asm ";
383198092Srdivacky
384193326Sed  if (Node->isVolatile())
385193326Sed    OS << "volatile ";
386198092Srdivacky
387193326Sed  OS << "(";
388193326Sed  VisitStringLiteral(Node->getAsmString());
389198092Srdivacky
390193326Sed  // Outputs
391193326Sed  if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
392193326Sed      Node->getNumClobbers() != 0)
393193326Sed    OS << " : ";
394198092Srdivacky
395193326Sed  for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
396193326Sed    if (i != 0)
397193326Sed      OS << ", ";
398198092Srdivacky
399193326Sed    if (!Node->getOutputName(i).empty()) {
400193326Sed      OS << '[';
401193326Sed      OS << Node->getOutputName(i);
402193326Sed      OS << "] ";
403193326Sed    }
404198092Srdivacky
405193326Sed    VisitStringLiteral(Node->getOutputConstraintLiteral(i));
406193326Sed    OS << " ";
407193326Sed    Visit(Node->getOutputExpr(i));
408193326Sed  }
409198092Srdivacky
410193326Sed  // Inputs
411193326Sed  if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
412193326Sed    OS << " : ";
413198092Srdivacky
414193326Sed  for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
415193326Sed    if (i != 0)
416193326Sed      OS << ", ";
417198092Srdivacky
418193326Sed    if (!Node->getInputName(i).empty()) {
419193326Sed      OS << '[';
420193326Sed      OS << Node->getInputName(i);
421193326Sed      OS << "] ";
422193326Sed    }
423198092Srdivacky
424193326Sed    VisitStringLiteral(Node->getInputConstraintLiteral(i));
425193326Sed    OS << " ";
426193326Sed    Visit(Node->getInputExpr(i));
427193326Sed  }
428198092Srdivacky
429193326Sed  // Clobbers
430193326Sed  if (Node->getNumClobbers() != 0)
431193326Sed    OS << " : ";
432198092Srdivacky
433193326Sed  for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
434193326Sed    if (i != 0)
435193326Sed      OS << ", ";
436198092Srdivacky
437243830Sdim    VisitStringLiteral(Node->getClobberStringLiteral(i));
438193326Sed  }
439198092Srdivacky
440193326Sed  OS << ");\n";
441193326Sed}
442193326Sed
443239462Sdimvoid StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
444239462Sdim  // FIXME: Implement MS style inline asm statement printer.
445239462Sdim  Indent() << "__asm ";
446239462Sdim  if (Node->hasBraces())
447239462Sdim    OS << "{\n";
448251662Sdim  OS << Node->getAsmString() << "\n";
449239462Sdim  if (Node->hasBraces())
450239462Sdim    Indent() << "}\n";
451239462Sdim}
452239462Sdim
453251662Sdimvoid StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
454251662Sdim  PrintStmt(Node->getCapturedDecl()->getBody());
455251662Sdim}
456251662Sdim
457193326Sedvoid StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
458193326Sed  Indent() << "@try";
459193326Sed  if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
460193326Sed    PrintRawCompoundStmt(TS);
461193326Sed    OS << "\n";
462193326Sed  }
463198092Srdivacky
464207619Srdivacky  for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
465207619Srdivacky    ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
466193326Sed    Indent() << "@catch(";
467193326Sed    if (catchStmt->getCatchParamDecl()) {
468193326Sed      if (Decl *DS = catchStmt->getCatchParamDecl())
469193326Sed        PrintRawDecl(DS);
470193326Sed    }
471193326Sed    OS << ")";
472198092Srdivacky    if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
473198092Srdivacky      PrintRawCompoundStmt(CS);
474198092Srdivacky      OS << "\n";
475198092Srdivacky    }
476193326Sed  }
477198092Srdivacky
478198092Srdivacky  if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
479198092Srdivacky        Node->getFinallyStmt())) {
480193326Sed    Indent() << "@finally";
481193326Sed    PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
482193326Sed    OS << "\n";
483198092Srdivacky  }
484193326Sed}
485193326Sed
486193326Sedvoid StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
487193326Sed}
488193326Sed
489193326Sedvoid StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
490193326Sed  Indent() << "@catch (...) { /* todo */ } \n";
491193326Sed}
492193326Sed
493193326Sedvoid StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
494193326Sed  Indent() << "@throw";
495193326Sed  if (Node->getThrowExpr()) {
496193326Sed    OS << " ";
497193326Sed    PrintExpr(Node->getThrowExpr());
498193326Sed  }
499193326Sed  OS << ";\n";
500193326Sed}
501193326Sed
502193326Sedvoid StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
503193326Sed  Indent() << "@synchronized (";
504193326Sed  PrintExpr(Node->getSynchExpr());
505193326Sed  OS << ")";
506193326Sed  PrintRawCompoundStmt(Node->getSynchBody());
507193326Sed  OS << "\n";
508193326Sed}
509193326Sed
510224145Sdimvoid StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
511224145Sdim  Indent() << "@autoreleasepool";
512224145Sdim  PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
513224145Sdim  OS << "\n";
514224145Sdim}
515224145Sdim
516193326Sedvoid StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
517193326Sed  OS << "catch (";
518193326Sed  if (Decl *ExDecl = Node->getExceptionDecl())
519193326Sed    PrintRawDecl(ExDecl);
520193326Sed  else
521193326Sed    OS << "...";
522193326Sed  OS << ") ";
523193326Sed  PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
524193326Sed}
525193326Sed
526193326Sedvoid StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
527193326Sed  Indent();
528193326Sed  PrintRawCXXCatchStmt(Node);
529193326Sed  OS << "\n";
530193326Sed}
531193326Sed
532193326Sedvoid StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
533193326Sed  Indent() << "try ";
534193326Sed  PrintRawCompoundStmt(Node->getTryBlock());
535198092Srdivacky  for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
536193326Sed    OS << " ";
537193326Sed    PrintRawCXXCatchStmt(Node->getHandler(i));
538193326Sed  }
539193326Sed  OS << "\n";
540193326Sed}
541193326Sed
542221345Sdimvoid StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
543221345Sdim  Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
544221345Sdim  PrintRawCompoundStmt(Node->getTryBlock());
545221345Sdim  SEHExceptStmt *E = Node->getExceptHandler();
546221345Sdim  SEHFinallyStmt *F = Node->getFinallyHandler();
547221345Sdim  if(E)
548221345Sdim    PrintRawSEHExceptHandler(E);
549221345Sdim  else {
550221345Sdim    assert(F && "Must have a finally block...");
551221345Sdim    PrintRawSEHFinallyStmt(F);
552221345Sdim  }
553221345Sdim  OS << "\n";
554221345Sdim}
555221345Sdim
556221345Sdimvoid StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
557221345Sdim  OS << "__finally ";
558221345Sdim  PrintRawCompoundStmt(Node->getBlock());
559221345Sdim  OS << "\n";
560221345Sdim}
561221345Sdim
562221345Sdimvoid StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
563221345Sdim  OS << "__except (";
564221345Sdim  VisitExpr(Node->getFilterExpr());
565221345Sdim  OS << ")\n";
566221345Sdim  PrintRawCompoundStmt(Node->getBlock());
567221345Sdim  OS << "\n";
568221345Sdim}
569221345Sdim
570221345Sdimvoid StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
571221345Sdim  Indent();
572221345Sdim  PrintRawSEHExceptHandler(Node);
573221345Sdim  OS << "\n";
574221345Sdim}
575221345Sdim
576221345Sdimvoid StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
577221345Sdim  Indent();
578221345Sdim  PrintRawSEHFinallyStmt(Node);
579221345Sdim  OS << "\n";
580221345Sdim}
581221345Sdim
582193326Sed//===----------------------------------------------------------------------===//
583263508Sdim//  OpenMP clauses printing methods
584263508Sdim//===----------------------------------------------------------------------===//
585263508Sdim
586263508Sdimnamespace {
587263508Sdimclass OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
588263508Sdim  raw_ostream &OS;
589263508Sdim  /// \brief Process clauses with list of variables.
590263508Sdim  template <typename T>
591263508Sdim  void VisitOMPClauseList(T *Node, char StartSym);
592263508Sdimpublic:
593263508Sdim  OMPClausePrinter(raw_ostream &OS) : OS(OS) { }
594263508Sdim#define OPENMP_CLAUSE(Name, Class)                              \
595263508Sdim  void Visit##Class(Class *S);
596263508Sdim#include "clang/Basic/OpenMPKinds.def"
597263508Sdim};
598263508Sdim
599263508Sdimvoid OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
600263508Sdim  OS << "default("
601263508Sdim     << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
602263508Sdim     << ")";
603263508Sdim}
604263508Sdim
605263508Sdimtemplate<typename T>
606263508Sdimvoid OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
607263508Sdim  for (typename T::varlist_iterator I = Node->varlist_begin(),
608263508Sdim                                    E = Node->varlist_end();
609263508Sdim         I != E; ++I)
610263508Sdim    OS << (I == Node->varlist_begin() ? StartSym : ',')
611263508Sdim       << *cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
612263508Sdim}
613263508Sdim
614263508Sdimvoid OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
615263508Sdim  if (!Node->varlist_empty()) {
616263508Sdim    OS << "private";
617263508Sdim    VisitOMPClauseList(Node, '(');
618263508Sdim    OS << ")";
619263508Sdim  }
620263508Sdim}
621263508Sdim
622263508Sdimvoid OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
623263508Sdim  if (!Node->varlist_empty()) {
624263508Sdim    OS << "firstprivate";
625263508Sdim    VisitOMPClauseList(Node, '(');
626263508Sdim    OS << ")";
627263508Sdim  }
628263508Sdim}
629263508Sdim
630263508Sdimvoid OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
631263508Sdim  if (!Node->varlist_empty()) {
632263508Sdim    OS << "shared";
633263508Sdim    VisitOMPClauseList(Node, '(');
634263508Sdim    OS << ")";
635263508Sdim  }
636263508Sdim}
637263508Sdim
638263508Sdim}
639263508Sdim
640263508Sdim//===----------------------------------------------------------------------===//
641263508Sdim//  OpenMP directives printing methods
642263508Sdim//===----------------------------------------------------------------------===//
643263508Sdim
644263508Sdimvoid StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
645263508Sdim  Indent() << "#pragma omp parallel ";
646263508Sdim
647263508Sdim  OMPClausePrinter Printer(OS);
648263508Sdim  ArrayRef<OMPClause *> Clauses = Node->clauses();
649263508Sdim  for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
650263508Sdim       I != E; ++I)
651263508Sdim    if (*I && !(*I)->isImplicit()) {
652263508Sdim      Printer.Visit(*I);
653263508Sdim      OS << ' ';
654263508Sdim    }
655263508Sdim  OS << "\n";
656263508Sdim  if (Node->getAssociatedStmt()) {
657263508Sdim    assert(isa<CapturedStmt>(Node->getAssociatedStmt()) &&
658263508Sdim           "Expected captured statement!");
659263508Sdim    Stmt *CS = cast<CapturedStmt>(Node->getAssociatedStmt())->getCapturedStmt();
660263508Sdim    PrintStmt(CS);
661263508Sdim  }
662263508Sdim}
663263508Sdim//===----------------------------------------------------------------------===//
664193326Sed//  Expr printing methods.
665193326Sed//===----------------------------------------------------------------------===//
666193326Sed
667193326Sedvoid StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
668198893Srdivacky  if (NestedNameSpecifier *Qualifier = Node->getQualifier())
669198893Srdivacky    Qualifier->print(OS, Policy);
670234353Sdim  if (Node->hasTemplateKeyword())
671234353Sdim    OS << "template ";
672212904Sdim  OS << Node->getNameInfo();
673212904Sdim  if (Node->hasExplicitTemplateArgs())
674249423Sdim    TemplateSpecializationType::PrintTemplateArgumentList(
675249423Sdim        OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
676193326Sed}
677193326Sed
678199990Srdivackyvoid StmtPrinter::VisitDependentScopeDeclRefExpr(
679199990Srdivacky                                           DependentScopeDeclRefExpr *Node) {
680218893Sdim  if (NestedNameSpecifier *Qualifier = Node->getQualifier())
681218893Sdim    Qualifier->print(OS, Policy);
682234353Sdim  if (Node->hasTemplateKeyword())
683234353Sdim    OS << "template ";
684212904Sdim  OS << Node->getNameInfo();
685199990Srdivacky  if (Node->hasExplicitTemplateArgs())
686249423Sdim    TemplateSpecializationType::PrintTemplateArgumentList(
687249423Sdim        OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
688193326Sed}
689193326Sed
690199990Srdivackyvoid StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
691195341Sed  if (Node->getQualifier())
692195341Sed    Node->getQualifier()->print(OS, Policy);
693234353Sdim  if (Node->hasTemplateKeyword())
694234353Sdim    OS << "template ";
695212904Sdim  OS << Node->getNameInfo();
696199990Srdivacky  if (Node->hasExplicitTemplateArgs())
697249423Sdim    TemplateSpecializationType::PrintTemplateArgumentList(
698249423Sdim        OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
699195341Sed}
700195341Sed
701193326Sedvoid StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
702193326Sed  if (Node->getBase()) {
703193326Sed    PrintExpr(Node->getBase());
704193326Sed    OS << (Node->isArrow() ? "->" : ".");
705193326Sed  }
706226633Sdim  OS << *Node->getDecl();
707193326Sed}
708193326Sed
709193326Sedvoid StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
710218893Sdim  if (Node->isSuperReceiver())
711218893Sdim    OS << "super.";
712266715Sdim  else if (Node->isObjectReceiver() && Node->getBase()) {
713193326Sed    PrintExpr(Node->getBase());
714193326Sed    OS << ".";
715266715Sdim  } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
716266715Sdim    OS << Node->getClassReceiver()->getName() << ".";
717193326Sed  }
718193326Sed
719218893Sdim  if (Node->isImplicitProperty())
720218893Sdim    OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
721218893Sdim  else
722218893Sdim    OS << Node->getExplicitProperty()->getName();
723193326Sed}
724193326Sed
725234353Sdimvoid StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
726234353Sdim
727234353Sdim  PrintExpr(Node->getBaseExpr());
728234353Sdim  OS << "[";
729234353Sdim  PrintExpr(Node->getKeyExpr());
730234353Sdim  OS << "]";
731234353Sdim}
732234353Sdim
733193326Sedvoid StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
734193326Sed  switch (Node->getIdentType()) {
735193326Sed    default:
736226633Sdim      llvm_unreachable("unknown case");
737193326Sed    case PredefinedExpr::Func:
738193326Sed      OS << "__func__";
739193326Sed      break;
740193326Sed    case PredefinedExpr::Function:
741193326Sed      OS << "__FUNCTION__";
742193326Sed      break;
743263508Sdim    case PredefinedExpr::FuncDName:
744263508Sdim      OS << "__FUNCDNAME__";
745263508Sdim      break;
746239462Sdim    case PredefinedExpr::LFunction:
747239462Sdim      OS << "L__FUNCTION__";
748239462Sdim      break;
749193326Sed    case PredefinedExpr::PrettyFunction:
750193326Sed      OS << "__PRETTY_FUNCTION__";
751193326Sed      break;
752193326Sed  }
753193326Sed}
754193326Sed
755193326Sedvoid StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
756193326Sed  unsigned value = Node->getValue();
757226633Sdim
758226633Sdim  switch (Node->getKind()) {
759226633Sdim  case CharacterLiteral::Ascii: break; // no prefix.
760226633Sdim  case CharacterLiteral::Wide:  OS << 'L'; break;
761226633Sdim  case CharacterLiteral::UTF16: OS << 'u'; break;
762226633Sdim  case CharacterLiteral::UTF32: OS << 'U'; break;
763226633Sdim  }
764226633Sdim
765193326Sed  switch (value) {
766193326Sed  case '\\':
767193326Sed    OS << "'\\\\'";
768193326Sed    break;
769193326Sed  case '\'':
770193326Sed    OS << "'\\''";
771193326Sed    break;
772193326Sed  case '\a':
773193326Sed    // TODO: K&R: the meaning of '\\a' is different in traditional C
774193326Sed    OS << "'\\a'";
775193326Sed    break;
776193326Sed  case '\b':
777193326Sed    OS << "'\\b'";
778193326Sed    break;
779193326Sed  // Nonstandard escape sequence.
780193326Sed  /*case '\e':
781193326Sed    OS << "'\\e'";
782193326Sed    break;*/
783193326Sed  case '\f':
784193326Sed    OS << "'\\f'";
785193326Sed    break;
786193326Sed  case '\n':
787193326Sed    OS << "'\\n'";
788193326Sed    break;
789193326Sed  case '\r':
790193326Sed    OS << "'\\r'";
791193326Sed    break;
792193326Sed  case '\t':
793193326Sed    OS << "'\\t'";
794193326Sed    break;
795193326Sed  case '\v':
796193326Sed    OS << "'\\v'";
797193326Sed    break;
798193326Sed  default:
799249423Sdim    if (value < 256 && isPrintable((unsigned char)value))
800193326Sed      OS << "'" << (char)value << "'";
801249423Sdim    else if (value < 256)
802249423Sdim      OS << "'\\x" << llvm::format("%02x", value) << "'";
803249423Sdim    else if (value <= 0xFFFF)
804249423Sdim      OS << "'\\u" << llvm::format("%04x", value) << "'";
805249423Sdim    else
806249423Sdim      OS << "'\\U" << llvm::format("%08x", value) << "'";
807193326Sed  }
808193326Sed}
809193326Sed
810193326Sedvoid StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
811193326Sed  bool isSigned = Node->getType()->isSignedIntegerType();
812193326Sed  OS << Node->getValue().toString(10, isSigned);
813198092Srdivacky
814193326Sed  // Emit suffixes.  Integer literals are always a builtin integer type.
815198092Srdivacky  switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
816226633Sdim  default: llvm_unreachable("Unexpected type for integer literal!");
817234353Sdim  // FIXME: The Short and UShort cases are to handle cases where a short
818234353Sdim  // integeral literal is formed during template instantiation.  They should
819234353Sdim  // be removed when template instantiation no longer needs integer literals.
820234353Sdim  case BuiltinType::Short:
821234353Sdim  case BuiltinType::UShort:
822193326Sed  case BuiltinType::Int:       break; // no suffix.
823193326Sed  case BuiltinType::UInt:      OS << 'U'; break;
824193326Sed  case BuiltinType::Long:      OS << 'L'; break;
825193326Sed  case BuiltinType::ULong:     OS << "UL"; break;
826193326Sed  case BuiltinType::LongLong:  OS << "LL"; break;
827193326Sed  case BuiltinType::ULongLong: OS << "ULL"; break;
828234353Sdim  case BuiltinType::Int128:    OS << "i128"; break;
829234353Sdim  case BuiltinType::UInt128:   OS << "Ui128"; break;
830193326Sed  }
831193326Sed}
832243830Sdim
833243830Sdimstatic void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
834243830Sdim                                 bool PrintSuffix) {
835234353Sdim  SmallString<16> Str;
836226633Sdim  Node->getValue().toString(Str);
837226633Sdim  OS << Str;
838243830Sdim  if (Str.find_first_not_of("-0123456789") == StringRef::npos)
839243830Sdim    OS << '.'; // Trailing dot in order to separate from ints.
840243830Sdim
841243830Sdim  if (!PrintSuffix)
842243830Sdim    return;
843243830Sdim
844243830Sdim  // Emit suffixes.  Float literals are always a builtin float type.
845243830Sdim  switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
846243830Sdim  default: llvm_unreachable("Unexpected type for float literal!");
847243830Sdim  case BuiltinType::Half:       break; // FIXME: suffix?
848243830Sdim  case BuiltinType::Double:     break; // no suffix.
849243830Sdim  case BuiltinType::Float:      OS << 'F'; break;
850243830Sdim  case BuiltinType::LongDouble: OS << 'L'; break;
851243830Sdim  }
852193326Sed}
853193326Sed
854243830Sdimvoid StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
855243830Sdim  PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
856243830Sdim}
857243830Sdim
858193326Sedvoid StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
859193326Sed  PrintExpr(Node->getSubExpr());
860193326Sed  OS << "i";
861193326Sed}
862193326Sed
863193326Sedvoid StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
864239462Sdim  Str->outputString(OS);
865193326Sed}
866193326Sedvoid StmtPrinter::VisitParenExpr(ParenExpr *Node) {
867193326Sed  OS << "(";
868193326Sed  PrintExpr(Node->getSubExpr());
869193326Sed  OS << ")";
870193326Sed}
871193326Sedvoid StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
872193326Sed  if (!Node->isPostfix()) {
873193326Sed    OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
874198092Srdivacky
875194613Sed    // Print a space if this is an "identifier operator" like __real, or if
876194613Sed    // it might be concatenated incorrectly like '+'.
877193326Sed    switch (Node->getOpcode()) {
878193326Sed    default: break;
879212904Sdim    case UO_Real:
880212904Sdim    case UO_Imag:
881212904Sdim    case UO_Extension:
882193326Sed      OS << ' ';
883193326Sed      break;
884212904Sdim    case UO_Plus:
885212904Sdim    case UO_Minus:
886194613Sed      if (isa<UnaryOperator>(Node->getSubExpr()))
887194613Sed        OS << ' ';
888194613Sed      break;
889193326Sed    }
890193326Sed  }
891193326Sed  PrintExpr(Node->getSubExpr());
892198092Srdivacky
893193326Sed  if (Node->isPostfix())
894193326Sed    OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
895193326Sed}
896193326Sed
897207619Srdivackyvoid StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
898207619Srdivacky  OS << "__builtin_offsetof(";
899249423Sdim  Node->getTypeSourceInfo()->getType().print(OS, Policy);
900249423Sdim  OS << ", ";
901207619Srdivacky  bool PrintedSomething = false;
902207619Srdivacky  for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
903207619Srdivacky    OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
904207619Srdivacky    if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
905207619Srdivacky      // Array node
906207619Srdivacky      OS << "[";
907207619Srdivacky      PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
908207619Srdivacky      OS << "]";
909207619Srdivacky      PrintedSomething = true;
910207619Srdivacky      continue;
911207619Srdivacky    }
912207619Srdivacky
913207619Srdivacky    // Skip implicit base indirections.
914207619Srdivacky    if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
915207619Srdivacky      continue;
916207619Srdivacky
917207619Srdivacky    // Field or identifier node.
918207619Srdivacky    IdentifierInfo *Id = ON.getFieldName();
919207619Srdivacky    if (!Id)
920207619Srdivacky      continue;
921207619Srdivacky
922207619Srdivacky    if (PrintedSomething)
923207619Srdivacky      OS << ".";
924207619Srdivacky    else
925207619Srdivacky      PrintedSomething = true;
926207619Srdivacky    OS << Id->getName();
927207619Srdivacky  }
928207619Srdivacky  OS << ")";
929207619Srdivacky}
930207619Srdivacky
931221345Sdimvoid StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
932221345Sdim  switch(Node->getKind()) {
933221345Sdim  case UETT_SizeOf:
934221345Sdim    OS << "sizeof";
935221345Sdim    break;
936221345Sdim  case UETT_AlignOf:
937239462Sdim    if (Policy.LangOpts.CPlusPlus)
938239462Sdim      OS << "alignof";
939239462Sdim    else if (Policy.LangOpts.C11)
940239462Sdim      OS << "_Alignof";
941239462Sdim    else
942239462Sdim      OS << "__alignof";
943221345Sdim    break;
944221345Sdim  case UETT_VecStep:
945221345Sdim    OS << "vec_step";
946221345Sdim    break;
947221345Sdim  }
948249423Sdim  if (Node->isArgumentType()) {
949249423Sdim    OS << '(';
950249423Sdim    Node->getArgumentType().print(OS, Policy);
951249423Sdim    OS << ')';
952249423Sdim  } else {
953193326Sed    OS << " ";
954193326Sed    PrintExpr(Node->getArgumentExpr());
955193326Sed  }
956193326Sed}
957221345Sdim
958221345Sdimvoid StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
959221345Sdim  OS << "_Generic(";
960221345Sdim  PrintExpr(Node->getControllingExpr());
961221345Sdim  for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
962221345Sdim    OS << ", ";
963221345Sdim    QualType T = Node->getAssocType(i);
964221345Sdim    if (T.isNull())
965221345Sdim      OS << "default";
966221345Sdim    else
967249423Sdim      T.print(OS, Policy);
968221345Sdim    OS << ": ";
969221345Sdim    PrintExpr(Node->getAssocExpr(i));
970221345Sdim  }
971221345Sdim  OS << ")";
972221345Sdim}
973221345Sdim
974193326Sedvoid StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
975193326Sed  PrintExpr(Node->getLHS());
976193326Sed  OS << "[";
977193326Sed  PrintExpr(Node->getRHS());
978193326Sed  OS << "]";
979193326Sed}
980193326Sed
981218893Sdimvoid StmtPrinter::PrintCallArgs(CallExpr *Call) {
982193326Sed  for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
983193326Sed    if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
984193326Sed      // Don't print any defaulted arguments
985193326Sed      break;
986193326Sed    }
987193326Sed
988193326Sed    if (i) OS << ", ";
989193326Sed    PrintExpr(Call->getArg(i));
990193326Sed  }
991218893Sdim}
992218893Sdim
993218893Sdimvoid StmtPrinter::VisitCallExpr(CallExpr *Call) {
994218893Sdim  PrintExpr(Call->getCallee());
995218893Sdim  OS << "(";
996218893Sdim  PrintCallArgs(Call);
997193326Sed  OS << ")";
998193326Sed}
999193326Sedvoid StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1000193326Sed  // FIXME: Suppress printing implicit bases (like "this")
1001193326Sed  PrintExpr(Node->getBase());
1002249423Sdim
1003249423Sdim  MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1004249423Sdim  FieldDecl  *ParentDecl   = ParentMember
1005249423Sdim    ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : NULL;
1006249423Sdim
1007249423Sdim  if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1008249423Sdim    OS << (Node->isArrow() ? "->" : ".");
1009249423Sdim
1010202379Srdivacky  if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1011202379Srdivacky    if (FD->isAnonymousStructOrUnion())
1012202379Srdivacky      return;
1013249423Sdim
1014198092Srdivacky  if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1015198092Srdivacky    Qualifier->print(OS, Policy);
1016234353Sdim  if (Node->hasTemplateKeyword())
1017234353Sdim    OS << "template ";
1018212904Sdim  OS << Node->getMemberNameInfo();
1019212904Sdim  if (Node->hasExplicitTemplateArgs())
1020249423Sdim    TemplateSpecializationType::PrintTemplateArgumentList(
1021249423Sdim        OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1022193326Sed}
1023198092Srdivackyvoid StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1024198092Srdivacky  PrintExpr(Node->getBase());
1025198092Srdivacky  OS << (Node->isArrow() ? "->isa" : ".isa");
1026198092Srdivacky}
1027198092Srdivacky
1028193326Sedvoid StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1029193326Sed  PrintExpr(Node->getBase());
1030193326Sed  OS << ".";
1031193326Sed  OS << Node->getAccessor().getName();
1032193326Sed}
1033193326Sedvoid StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1034249423Sdim  OS << '(';
1035249423Sdim  Node->getTypeAsWritten().print(OS, Policy);
1036249423Sdim  OS << ')';
1037193326Sed  PrintExpr(Node->getSubExpr());
1038193326Sed}
1039193326Sedvoid StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1040249423Sdim  OS << '(';
1041249423Sdim  Node->getType().print(OS, Policy);
1042249423Sdim  OS << ')';
1043193326Sed  PrintExpr(Node->getInitializer());
1044193326Sed}
1045193326Sedvoid StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1046193326Sed  // No need to print anything, simply forward to the sub expression.
1047193326Sed  PrintExpr(Node->getSubExpr());
1048193326Sed}
1049193326Sedvoid StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1050193326Sed  PrintExpr(Node->getLHS());
1051193326Sed  OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1052193326Sed  PrintExpr(Node->getRHS());
1053193326Sed}
1054193326Sedvoid StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1055193326Sed  PrintExpr(Node->getLHS());
1056193326Sed  OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1057193326Sed  PrintExpr(Node->getRHS());
1058193326Sed}
1059193326Sedvoid StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1060193326Sed  PrintExpr(Node->getCond());
1061218893Sdim  OS << " ? ";
1062218893Sdim  PrintExpr(Node->getLHS());
1063218893Sdim  OS << " : ";
1064193326Sed  PrintExpr(Node->getRHS());
1065193326Sed}
1066193326Sed
1067193326Sed// GNU extensions.
1068193326Sed
1069218893Sdimvoid
1070218893SdimStmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1071218893Sdim  PrintExpr(Node->getCommon());
1072218893Sdim  OS << " ?: ";
1073218893Sdim  PrintExpr(Node->getFalseExpr());
1074218893Sdim}
1075193326Sedvoid StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1076193326Sed  OS << "&&" << Node->getLabel()->getName();
1077193326Sed}
1078193326Sed
1079193326Sedvoid StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1080193326Sed  OS << "(";
1081193326Sed  PrintRawCompoundStmt(E->getSubStmt());
1082193326Sed  OS << ")";
1083193326Sed}
1084193326Sed
1085193326Sedvoid StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1086193326Sed  OS << "__builtin_choose_expr(";
1087193326Sed  PrintExpr(Node->getCond());
1088193326Sed  OS << ", ";
1089193326Sed  PrintExpr(Node->getLHS());
1090193326Sed  OS << ", ";
1091193326Sed  PrintExpr(Node->getRHS());
1092193326Sed  OS << ")";
1093193326Sed}
1094193326Sed
1095193326Sedvoid StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1096193326Sed  OS << "__null";
1097193326Sed}
1098193326Sed
1099193326Sedvoid StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1100193326Sed  OS << "__builtin_shufflevector(";
1101193326Sed  for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1102193326Sed    if (i) OS << ", ";
1103193326Sed    PrintExpr(Node->getExpr(i));
1104193326Sed  }
1105193326Sed  OS << ")";
1106193326Sed}
1107193326Sed
1108263508Sdimvoid StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1109263508Sdim  OS << "__builtin_convertvector(";
1110263508Sdim  PrintExpr(Node->getSrcExpr());
1111263508Sdim  OS << ", ";
1112263508Sdim  Node->getType().print(OS, Policy);
1113263508Sdim  OS << ")";
1114263508Sdim}
1115263508Sdim
1116193326Sedvoid StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1117193326Sed  if (Node->getSyntacticForm()) {
1118193326Sed    Visit(Node->getSyntacticForm());
1119193326Sed    return;
1120193326Sed  }
1121193326Sed
1122193326Sed  OS << "{ ";
1123193326Sed  for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1124193326Sed    if (i) OS << ", ";
1125193326Sed    if (Node->getInit(i))
1126193326Sed      PrintExpr(Node->getInit(i));
1127193326Sed    else
1128193326Sed      OS << "0";
1129193326Sed  }
1130193326Sed  OS << " }";
1131193326Sed}
1132193326Sed
1133198092Srdivackyvoid StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1134198092Srdivacky  OS << "( ";
1135198092Srdivacky  for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1136198092Srdivacky    if (i) OS << ", ";
1137198092Srdivacky    PrintExpr(Node->getExpr(i));
1138198092Srdivacky  }
1139198092Srdivacky  OS << " )";
1140198092Srdivacky}
1141198092Srdivacky
1142193326Sedvoid StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1143193326Sed  for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1144193326Sed                      DEnd = Node->designators_end();
1145193326Sed       D != DEnd; ++D) {
1146193326Sed    if (D->isFieldDesignator()) {
1147193326Sed      if (D->getDotLoc().isInvalid())
1148193326Sed        OS << D->getFieldName()->getName() << ":";
1149193326Sed      else
1150193326Sed        OS << "." << D->getFieldName()->getName();
1151193326Sed    } else {
1152193326Sed      OS << "[";
1153193326Sed      if (D->isArrayDesignator()) {
1154193326Sed        PrintExpr(Node->getArrayIndex(*D));
1155193326Sed      } else {
1156193326Sed        PrintExpr(Node->getArrayRangeStart(*D));
1157193326Sed        OS << " ... ";
1158198092Srdivacky        PrintExpr(Node->getArrayRangeEnd(*D));
1159193326Sed      }
1160193326Sed      OS << "]";
1161193326Sed    }
1162193326Sed  }
1163193326Sed
1164193326Sed  OS << " = ";
1165193326Sed  PrintExpr(Node->getInit());
1166193326Sed}
1167193326Sed
1168193326Sedvoid StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1169249423Sdim  if (Policy.LangOpts.CPlusPlus) {
1170249423Sdim    OS << "/*implicit*/";
1171249423Sdim    Node->getType().print(OS, Policy);
1172249423Sdim    OS << "()";
1173249423Sdim  } else {
1174249423Sdim    OS << "/*implicit*/(";
1175249423Sdim    Node->getType().print(OS, Policy);
1176249423Sdim    OS << ')';
1177193326Sed    if (Node->getType()->isRecordType())
1178193326Sed      OS << "{}";
1179193326Sed    else
1180193326Sed      OS << 0;
1181193326Sed  }
1182193326Sed}
1183193326Sed
1184193326Sedvoid StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1185193326Sed  OS << "__builtin_va_arg(";
1186193326Sed  PrintExpr(Node->getSubExpr());
1187193326Sed  OS << ", ";
1188249423Sdim  Node->getType().print(OS, Policy);
1189193326Sed  OS << ")";
1190193326Sed}
1191193326Sed
1192234353Sdimvoid StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1193234353Sdim  PrintExpr(Node->getSyntacticForm());
1194234353Sdim}
1195234353Sdim
1196226633Sdimvoid StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1197226633Sdim  const char *Name = 0;
1198226633Sdim  switch (Node->getOp()) {
1199234353Sdim#define BUILTIN(ID, TYPE, ATTRS)
1200234353Sdim#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1201234353Sdim  case AtomicExpr::AO ## ID: \
1202234353Sdim    Name = #ID "("; \
1203234353Sdim    break;
1204234353Sdim#include "clang/Basic/Builtins.def"
1205226633Sdim  }
1206226633Sdim  OS << Name;
1207234353Sdim
1208234353Sdim  // AtomicExpr stores its subexpressions in a permuted order.
1209226633Sdim  PrintExpr(Node->getPtr());
1210234353Sdim  if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1211234353Sdim      Node->getOp() != AtomicExpr::AO__atomic_load_n) {
1212251662Sdim    OS << ", ";
1213226633Sdim    PrintExpr(Node->getVal1());
1214226633Sdim  }
1215234353Sdim  if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1216234353Sdim      Node->isCmpXChg()) {
1217251662Sdim    OS << ", ";
1218226633Sdim    PrintExpr(Node->getVal2());
1219226633Sdim  }
1220234353Sdim  if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1221234353Sdim      Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1222251662Sdim    OS << ", ";
1223234353Sdim    PrintExpr(Node->getWeak());
1224251662Sdim  }
1225251662Sdim  if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1226234353Sdim    OS << ", ";
1227251662Sdim    PrintExpr(Node->getOrder());
1228234353Sdim  }
1229226633Sdim  if (Node->isCmpXChg()) {
1230226633Sdim    OS << ", ";
1231226633Sdim    PrintExpr(Node->getOrderFail());
1232226633Sdim  }
1233226633Sdim  OS << ")";
1234226633Sdim}
1235226633Sdim
1236193326Sed// C++
1237193326Sedvoid StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1238193326Sed  const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1239193326Sed    "",
1240193326Sed#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1241193326Sed    Spelling,
1242193326Sed#include "clang/Basic/OperatorKinds.def"
1243193326Sed  };
1244193326Sed
1245193326Sed  OverloadedOperatorKind Kind = Node->getOperator();
1246193326Sed  if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1247193326Sed    if (Node->getNumArgs() == 1) {
1248193326Sed      OS << OpStrings[Kind] << ' ';
1249193326Sed      PrintExpr(Node->getArg(0));
1250193326Sed    } else {
1251193326Sed      PrintExpr(Node->getArg(0));
1252193326Sed      OS << ' ' << OpStrings[Kind];
1253193326Sed    }
1254243830Sdim  } else if (Kind == OO_Arrow) {
1255243830Sdim    PrintExpr(Node->getArg(0));
1256193326Sed  } else if (Kind == OO_Call) {
1257193326Sed    PrintExpr(Node->getArg(0));
1258193326Sed    OS << '(';
1259193326Sed    for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1260193326Sed      if (ArgIdx > 1)
1261193326Sed        OS << ", ";
1262193326Sed      if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1263193326Sed        PrintExpr(Node->getArg(ArgIdx));
1264193326Sed    }
1265193326Sed    OS << ')';
1266193326Sed  } else if (Kind == OO_Subscript) {
1267193326Sed    PrintExpr(Node->getArg(0));
1268193326Sed    OS << '[';
1269193326Sed    PrintExpr(Node->getArg(1));
1270193326Sed    OS << ']';
1271193326Sed  } else if (Node->getNumArgs() == 1) {
1272193326Sed    OS << OpStrings[Kind] << ' ';
1273193326Sed    PrintExpr(Node->getArg(0));
1274193326Sed  } else if (Node->getNumArgs() == 2) {
1275193326Sed    PrintExpr(Node->getArg(0));
1276193326Sed    OS << ' ' << OpStrings[Kind] << ' ';
1277193326Sed    PrintExpr(Node->getArg(1));
1278193326Sed  } else {
1279226633Sdim    llvm_unreachable("unknown overloaded operator");
1280193326Sed  }
1281193326Sed}
1282193326Sed
1283193326Sedvoid StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1284193326Sed  VisitCallExpr(cast<CallExpr>(Node));
1285193326Sed}
1286193326Sed
1287218893Sdimvoid StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1288218893Sdim  PrintExpr(Node->getCallee());
1289218893Sdim  OS << "<<<";
1290218893Sdim  PrintCallArgs(Node->getConfig());
1291218893Sdim  OS << ">>>(";
1292218893Sdim  PrintCallArgs(Node);
1293218893Sdim  OS << ")";
1294218893Sdim}
1295218893Sdim
1296193326Sedvoid StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1297193326Sed  OS << Node->getCastName() << '<';
1298249423Sdim  Node->getTypeAsWritten().print(OS, Policy);
1299249423Sdim  OS << ">(";
1300193326Sed  PrintExpr(Node->getSubExpr());
1301193326Sed  OS << ")";
1302193326Sed}
1303193326Sed
1304193326Sedvoid StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1305193326Sed  VisitCXXNamedCastExpr(Node);
1306193326Sed}
1307193326Sed
1308193326Sedvoid StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1309193326Sed  VisitCXXNamedCastExpr(Node);
1310193326Sed}
1311193326Sed
1312193326Sedvoid StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1313193326Sed  VisitCXXNamedCastExpr(Node);
1314193326Sed}
1315193326Sed
1316193326Sedvoid StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1317193326Sed  VisitCXXNamedCastExpr(Node);
1318193326Sed}
1319193326Sed
1320193326Sedvoid StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1321193326Sed  OS << "typeid(";
1322193326Sed  if (Node->isTypeOperand()) {
1323263508Sdim    Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1324193326Sed  } else {
1325193326Sed    PrintExpr(Node->getExprOperand());
1326193326Sed  }
1327193326Sed  OS << ")";
1328193326Sed}
1329193326Sed
1330218893Sdimvoid StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1331218893Sdim  OS << "__uuidof(";
1332218893Sdim  if (Node->isTypeOperand()) {
1333263508Sdim    Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1334218893Sdim  } else {
1335218893Sdim    PrintExpr(Node->getExprOperand());
1336218893Sdim  }
1337218893Sdim  OS << ")";
1338218893Sdim}
1339218893Sdim
1340251662Sdimvoid StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1341251662Sdim  PrintExpr(Node->getBaseExpr());
1342251662Sdim  if (Node->isArrow())
1343251662Sdim    OS << "->";
1344251662Sdim  else
1345251662Sdim    OS << ".";
1346251662Sdim  if (NestedNameSpecifier *Qualifier =
1347251662Sdim      Node->getQualifierLoc().getNestedNameSpecifier())
1348251662Sdim    Qualifier->print(OS, Policy);
1349251662Sdim  OS << Node->getPropertyDecl()->getDeclName();
1350251662Sdim}
1351251662Sdim
1352234353Sdimvoid StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1353234353Sdim  switch (Node->getLiteralOperatorKind()) {
1354234353Sdim  case UserDefinedLiteral::LOK_Raw:
1355234353Sdim    OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
1356234353Sdim    break;
1357234353Sdim  case UserDefinedLiteral::LOK_Template: {
1358234353Sdim    DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1359234353Sdim    const TemplateArgumentList *Args =
1360234353Sdim      cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1361234353Sdim    assert(Args);
1362234353Sdim    const TemplateArgument &Pack = Args->get(0);
1363234353Sdim    for (TemplateArgument::pack_iterator I = Pack.pack_begin(),
1364234353Sdim                                         E = Pack.pack_end(); I != E; ++I) {
1365239462Sdim      char C = (char)I->getAsIntegral().getZExtValue();
1366234353Sdim      OS << C;
1367234353Sdim    }
1368234353Sdim    break;
1369234353Sdim  }
1370234353Sdim  case UserDefinedLiteral::LOK_Integer: {
1371234353Sdim    // Print integer literal without suffix.
1372234353Sdim    IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1373234353Sdim    OS << Int->getValue().toString(10, /*isSigned*/false);
1374234353Sdim    break;
1375234353Sdim  }
1376243830Sdim  case UserDefinedLiteral::LOK_Floating: {
1377243830Sdim    // Print floating literal without suffix.
1378243830Sdim    FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1379243830Sdim    PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1380243830Sdim    break;
1381243830Sdim  }
1382234353Sdim  case UserDefinedLiteral::LOK_String:
1383234353Sdim  case UserDefinedLiteral::LOK_Character:
1384234353Sdim    PrintExpr(Node->getCookedLiteral());
1385234353Sdim    break;
1386234353Sdim  }
1387234353Sdim  OS << Node->getUDSuffix()->getName();
1388234353Sdim}
1389234353Sdim
1390193326Sedvoid StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1391193326Sed  OS << (Node->getValue() ? "true" : "false");
1392193326Sed}
1393193326Sed
1394193326Sedvoid StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1395193326Sed  OS << "nullptr";
1396193326Sed}
1397193326Sed
1398193326Sedvoid StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1399193326Sed  OS << "this";
1400193326Sed}
1401193326Sed
1402193326Sedvoid StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1403193326Sed  if (Node->getSubExpr() == 0)
1404193326Sed    OS << "throw";
1405193326Sed  else {
1406193326Sed    OS << "throw ";
1407193326Sed    PrintExpr(Node->getSubExpr());
1408193326Sed  }
1409193326Sed}
1410193326Sed
1411193326Sedvoid StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1412251662Sdim  // Nothing to print: we picked up the default argument.
1413193326Sed}
1414193326Sed
1415251662Sdimvoid StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1416251662Sdim  // Nothing to print: we picked up the default initializer.
1417251662Sdim}
1418251662Sdim
1419193326Sedvoid StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1420249423Sdim  Node->getType().print(OS, Policy);
1421193326Sed  OS << "(";
1422193326Sed  PrintExpr(Node->getSubExpr());
1423193326Sed  OS << ")";
1424193326Sed}
1425193326Sed
1426193326Sedvoid StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1427193326Sed  PrintExpr(Node->getSubExpr());
1428193326Sed}
1429193326Sed
1430193326Sedvoid StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1431249423Sdim  Node->getType().print(OS, Policy);
1432193326Sed  OS << "(";
1433193326Sed  for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1434198092Srdivacky                                         ArgEnd = Node->arg_end();
1435193326Sed       Arg != ArgEnd; ++Arg) {
1436263508Sdim    if (Arg->isDefaultArgument())
1437263508Sdim      break;
1438193326Sed    if (Arg != Node->arg_begin())
1439193326Sed      OS << ", ";
1440193326Sed    PrintExpr(*Arg);
1441193326Sed  }
1442193326Sed  OS << ")";
1443193326Sed}
1444193326Sed
1445234353Sdimvoid StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1446234353Sdim  OS << '[';
1447234353Sdim  bool NeedComma = false;
1448234353Sdim  switch (Node->getCaptureDefault()) {
1449234353Sdim  case LCD_None:
1450234353Sdim    break;
1451234353Sdim
1452234353Sdim  case LCD_ByCopy:
1453234353Sdim    OS << '=';
1454234353Sdim    NeedComma = true;
1455234353Sdim    break;
1456234353Sdim
1457234353Sdim  case LCD_ByRef:
1458234353Sdim    OS << '&';
1459234353Sdim    NeedComma = true;
1460234353Sdim    break;
1461234353Sdim  }
1462234353Sdim  for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1463234353Sdim                                 CEnd = Node->explicit_capture_end();
1464234353Sdim       C != CEnd;
1465234353Sdim       ++C) {
1466234353Sdim    if (NeedComma)
1467234353Sdim      OS << ", ";
1468234353Sdim    NeedComma = true;
1469234353Sdim
1470234353Sdim    switch (C->getCaptureKind()) {
1471234353Sdim    case LCK_This:
1472234353Sdim      OS << "this";
1473234353Sdim      break;
1474234353Sdim
1475234353Sdim    case LCK_ByRef:
1476263508Sdim      if (Node->getCaptureDefault() != LCD_ByRef || C->isInitCapture())
1477234353Sdim        OS << '&';
1478234353Sdim      OS << C->getCapturedVar()->getName();
1479234353Sdim      break;
1480234353Sdim
1481234353Sdim    case LCK_ByCopy:
1482234353Sdim      OS << C->getCapturedVar()->getName();
1483234353Sdim      break;
1484234353Sdim    }
1485263508Sdim
1486263508Sdim    if (C->isInitCapture())
1487263508Sdim      PrintExpr(C->getCapturedVar()->getInit());
1488234353Sdim  }
1489234353Sdim  OS << ']';
1490234353Sdim
1491234353Sdim  if (Node->hasExplicitParameters()) {
1492234353Sdim    OS << " (";
1493234353Sdim    CXXMethodDecl *Method = Node->getCallOperator();
1494234353Sdim    NeedComma = false;
1495234353Sdim    for (CXXMethodDecl::param_iterator P = Method->param_begin(),
1496234353Sdim                                    PEnd = Method->param_end();
1497234353Sdim         P != PEnd; ++P) {
1498234353Sdim      if (NeedComma) {
1499234353Sdim        OS << ", ";
1500234353Sdim      } else {
1501234353Sdim        NeedComma = true;
1502234353Sdim      }
1503234353Sdim      std::string ParamStr = (*P)->getNameAsString();
1504249423Sdim      (*P)->getOriginalType().print(OS, Policy, ParamStr);
1505234353Sdim    }
1506234353Sdim    if (Method->isVariadic()) {
1507234353Sdim      if (NeedComma)
1508234353Sdim        OS << ", ";
1509234353Sdim      OS << "...";
1510234353Sdim    }
1511234353Sdim    OS << ')';
1512234353Sdim
1513234353Sdim    if (Node->isMutable())
1514234353Sdim      OS << " mutable";
1515234353Sdim
1516234353Sdim    const FunctionProtoType *Proto
1517234353Sdim      = Method->getType()->getAs<FunctionProtoType>();
1518249423Sdim    Proto->printExceptionSpecification(OS, Policy);
1519234353Sdim
1520234353Sdim    // FIXME: Attributes
1521234353Sdim
1522234353Sdim    // Print the trailing return type if it was specified in the source.
1523249423Sdim    if (Node->hasExplicitResultType()) {
1524249423Sdim      OS << " -> ";
1525249423Sdim      Proto->getResultType().print(OS, Policy);
1526249423Sdim    }
1527234353Sdim  }
1528234353Sdim
1529234353Sdim  // Print the body.
1530234353Sdim  CompoundStmt *Body = Node->getBody();
1531234353Sdim  OS << ' ';
1532234353Sdim  PrintStmt(Body);
1533234353Sdim}
1534234353Sdim
1535210299Sedvoid StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
1536218893Sdim  if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1537249423Sdim    TSInfo->getType().print(OS, Policy);
1538218893Sdim  else
1539249423Sdim    Node->getType().print(OS, Policy);
1540249423Sdim  OS << "()";
1541193326Sed}
1542193326Sed
1543193326Sedvoid StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1544193326Sed  if (E->isGlobalNew())
1545193326Sed    OS << "::";
1546193326Sed  OS << "new ";
1547193326Sed  unsigned NumPlace = E->getNumPlacementArgs();
1548243830Sdim  if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
1549193326Sed    OS << "(";
1550193326Sed    PrintExpr(E->getPlacementArg(0));
1551193326Sed    for (unsigned i = 1; i < NumPlace; ++i) {
1552243830Sdim      if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1553243830Sdim        break;
1554193326Sed      OS << ", ";
1555193326Sed      PrintExpr(E->getPlacementArg(i));
1556193326Sed    }
1557193326Sed    OS << ") ";
1558193326Sed  }
1559193326Sed  if (E->isParenTypeId())
1560193326Sed    OS << "(";
1561193326Sed  std::string TypeS;
1562193326Sed  if (Expr *Size = E->getArraySize()) {
1563193326Sed    llvm::raw_string_ostream s(TypeS);
1564249423Sdim    s << '[';
1565239462Sdim    Size->printPretty(s, Helper, Policy);
1566249423Sdim    s << ']';
1567193326Sed  }
1568249423Sdim  E->getAllocatedType().print(OS, Policy, TypeS);
1569193326Sed  if (E->isParenTypeId())
1570193326Sed    OS << ")";
1571193326Sed
1572234353Sdim  CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1573234353Sdim  if (InitStyle) {
1574234353Sdim    if (InitStyle == CXXNewExpr::CallInit)
1575234353Sdim      OS << "(";
1576234353Sdim    PrintExpr(E->getInitializer());
1577234353Sdim    if (InitStyle == CXXNewExpr::CallInit)
1578234353Sdim      OS << ")";
1579193326Sed  }
1580193326Sed}
1581193326Sed
1582193326Sedvoid StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1583193326Sed  if (E->isGlobalDelete())
1584193326Sed    OS << "::";
1585193326Sed  OS << "delete ";
1586193326Sed  if (E->isArrayForm())
1587193326Sed    OS << "[] ";
1588193326Sed  PrintExpr(E->getArgument());
1589193326Sed}
1590193326Sed
1591198092Srdivackyvoid StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1592198092Srdivacky  PrintExpr(E->getBase());
1593198092Srdivacky  if (E->isArrow())
1594198092Srdivacky    OS << "->";
1595198092Srdivacky  else
1596198092Srdivacky    OS << '.';
1597198092Srdivacky  if (E->getQualifier())
1598198092Srdivacky    E->getQualifier()->print(OS, Policy);
1599243830Sdim  OS << "~";
1600198092Srdivacky
1601204643Srdivacky  if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1602204643Srdivacky    OS << II->getName();
1603204643Srdivacky  else
1604249423Sdim    E->getDestroyedType().print(OS, Policy);
1605198092Srdivacky}
1606198092Srdivacky
1607193326Sedvoid StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1608249423Sdim  if (E->isListInitialization())
1609249423Sdim    OS << "{ ";
1610249423Sdim
1611218893Sdim  for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1612218893Sdim    if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1613218893Sdim      // Don't print any defaulted arguments
1614218893Sdim      break;
1615218893Sdim    }
1616218893Sdim
1617218893Sdim    if (i) OS << ", ";
1618218893Sdim    PrintExpr(E->getArg(i));
1619202379Srdivacky  }
1620249423Sdim
1621249423Sdim  if (E->isListInitialization())
1622249423Sdim    OS << " }";
1623193326Sed}
1624193326Sed
1625263508Sdimvoid StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
1626263508Sdim  PrintExpr(E->getSubExpr());
1627263508Sdim}
1628263508Sdim
1629218893Sdimvoid StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
1630193326Sed  // Just forward to the sub expression.
1631193326Sed  PrintExpr(E->getSubExpr());
1632193326Sed}
1633193326Sed
1634198092Srdivackyvoid
1635193326SedStmtPrinter::VisitCXXUnresolvedConstructExpr(
1636193326Sed                                           CXXUnresolvedConstructExpr *Node) {
1637249423Sdim  Node->getTypeAsWritten().print(OS, Policy);
1638193326Sed  OS << "(";
1639193326Sed  for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
1640198092Srdivacky                                             ArgEnd = Node->arg_end();
1641193326Sed       Arg != ArgEnd; ++Arg) {
1642193326Sed    if (Arg != Node->arg_begin())
1643193326Sed      OS << ", ";
1644193326Sed    PrintExpr(*Arg);
1645193326Sed  }
1646193326Sed  OS << ")";
1647193326Sed}
1648193326Sed
1649199990Srdivackyvoid StmtPrinter::VisitCXXDependentScopeMemberExpr(
1650199990Srdivacky                                         CXXDependentScopeMemberExpr *Node) {
1651200583Srdivacky  if (!Node->isImplicitAccess()) {
1652200583Srdivacky    PrintExpr(Node->getBase());
1653200583Srdivacky    OS << (Node->isArrow() ? "->" : ".");
1654200583Srdivacky  }
1655198092Srdivacky  if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1656198092Srdivacky    Qualifier->print(OS, Policy);
1657234353Sdim  if (Node->hasTemplateKeyword())
1658198092Srdivacky    OS << "template ";
1659212904Sdim  OS << Node->getMemberNameInfo();
1660249423Sdim  if (Node->hasExplicitTemplateArgs())
1661249423Sdim    TemplateSpecializationType::PrintTemplateArgumentList(
1662249423Sdim        OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1663193326Sed}
1664193326Sed
1665199990Srdivackyvoid StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
1666200583Srdivacky  if (!Node->isImplicitAccess()) {
1667200583Srdivacky    PrintExpr(Node->getBase());
1668200583Srdivacky    OS << (Node->isArrow() ? "->" : ".");
1669200583Srdivacky  }
1670199990Srdivacky  if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1671199990Srdivacky    Qualifier->print(OS, Policy);
1672234353Sdim  if (Node->hasTemplateKeyword())
1673234353Sdim    OS << "template ";
1674212904Sdim  OS << Node->getMemberNameInfo();
1675249423Sdim  if (Node->hasExplicitTemplateArgs())
1676249423Sdim    TemplateSpecializationType::PrintTemplateArgumentList(
1677249423Sdim        OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1678199990Srdivacky}
1679199990Srdivacky
1680193326Sedstatic const char *getTypeTraitName(UnaryTypeTrait UTT) {
1681193326Sed  switch (UTT) {
1682193326Sed  case UTT_HasNothrowAssign:      return "__has_nothrow_assign";
1683249423Sdim  case UTT_HasNothrowMoveAssign:  return "__has_nothrow_move_assign";
1684193326Sed  case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1685221345Sdim  case UTT_HasNothrowCopy:          return "__has_nothrow_copy";
1686193326Sed  case UTT_HasTrivialAssign:      return "__has_trivial_assign";
1687249423Sdim  case UTT_HasTrivialMoveAssign:      return "__has_trivial_move_assign";
1688249423Sdim  case UTT_HasTrivialMoveConstructor: return "__has_trivial_move_constructor";
1689223017Sdim  case UTT_HasTrivialDefaultConstructor: return "__has_trivial_constructor";
1690221345Sdim  case UTT_HasTrivialCopy:          return "__has_trivial_copy";
1691193326Sed  case UTT_HasTrivialDestructor:  return "__has_trivial_destructor";
1692193326Sed  case UTT_HasVirtualDestructor:  return "__has_virtual_destructor";
1693193326Sed  case UTT_IsAbstract:            return "__is_abstract";
1694221345Sdim  case UTT_IsArithmetic:            return "__is_arithmetic";
1695221345Sdim  case UTT_IsArray:                 return "__is_array";
1696193326Sed  case UTT_IsClass:               return "__is_class";
1697221345Sdim  case UTT_IsCompleteType:          return "__is_complete_type";
1698221345Sdim  case UTT_IsCompound:              return "__is_compound";
1699221345Sdim  case UTT_IsConst:                 return "__is_const";
1700193326Sed  case UTT_IsEmpty:               return "__is_empty";
1701193326Sed  case UTT_IsEnum:                return "__is_enum";
1702234353Sdim  case UTT_IsFinal:                 return "__is_final";
1703221345Sdim  case UTT_IsFloatingPoint:         return "__is_floating_point";
1704221345Sdim  case UTT_IsFunction:              return "__is_function";
1705221345Sdim  case UTT_IsFundamental:           return "__is_fundamental";
1706221345Sdim  case UTT_IsIntegral:              return "__is_integral";
1707243830Sdim  case UTT_IsInterfaceClass:        return "__is_interface_class";
1708221345Sdim  case UTT_IsLiteral:               return "__is_literal";
1709221345Sdim  case UTT_IsLvalueReference:       return "__is_lvalue_reference";
1710221345Sdim  case UTT_IsMemberFunctionPointer: return "__is_member_function_pointer";
1711221345Sdim  case UTT_IsMemberObjectPointer:   return "__is_member_object_pointer";
1712221345Sdim  case UTT_IsMemberPointer:         return "__is_member_pointer";
1713221345Sdim  case UTT_IsObject:                return "__is_object";
1714193326Sed  case UTT_IsPOD:                 return "__is_pod";
1715221345Sdim  case UTT_IsPointer:               return "__is_pointer";
1716193326Sed  case UTT_IsPolymorphic:         return "__is_polymorphic";
1717221345Sdim  case UTT_IsReference:             return "__is_reference";
1718221345Sdim  case UTT_IsRvalueReference:       return "__is_rvalue_reference";
1719221345Sdim  case UTT_IsScalar:                return "__is_scalar";
1720263508Sdim  case UTT_IsSealed:                return "__is_sealed";
1721221345Sdim  case UTT_IsSigned:                return "__is_signed";
1722221345Sdim  case UTT_IsStandardLayout:        return "__is_standard_layout";
1723221345Sdim  case UTT_IsTrivial:               return "__is_trivial";
1724223017Sdim  case UTT_IsTriviallyCopyable:     return "__is_trivially_copyable";
1725193326Sed  case UTT_IsUnion:               return "__is_union";
1726221345Sdim  case UTT_IsUnsigned:              return "__is_unsigned";
1727221345Sdim  case UTT_IsVoid:                  return "__is_void";
1728221345Sdim  case UTT_IsVolatile:              return "__is_volatile";
1729193326Sed  }
1730221345Sdim  llvm_unreachable("Type trait not covered by switch statement");
1731193326Sed}
1732193326Sed
1733218893Sdimstatic const char *getTypeTraitName(BinaryTypeTrait BTT) {
1734218893Sdim  switch (BTT) {
1735234353Sdim  case BTT_IsBaseOf:              return "__is_base_of";
1736234353Sdim  case BTT_IsConvertible:         return "__is_convertible";
1737234353Sdim  case BTT_IsSame:                return "__is_same";
1738234353Sdim  case BTT_TypeCompatible:        return "__builtin_types_compatible_p";
1739234353Sdim  case BTT_IsConvertibleTo:       return "__is_convertible_to";
1740234353Sdim  case BTT_IsTriviallyAssignable: return "__is_trivially_assignable";
1741218893Sdim  }
1742221345Sdim  llvm_unreachable("Binary type trait not covered by switch");
1743218893Sdim}
1744218893Sdim
1745234353Sdimstatic const char *getTypeTraitName(TypeTrait TT) {
1746234353Sdim  switch (TT) {
1747234353Sdim  case clang::TT_IsTriviallyConstructible:return "__is_trivially_constructible";
1748234353Sdim  }
1749234353Sdim  llvm_unreachable("Type trait not covered by switch");
1750234353Sdim}
1751234353Sdim
1752221345Sdimstatic const char *getTypeTraitName(ArrayTypeTrait ATT) {
1753221345Sdim  switch (ATT) {
1754221345Sdim  case ATT_ArrayRank:        return "__array_rank";
1755221345Sdim  case ATT_ArrayExtent:      return "__array_extent";
1756221345Sdim  }
1757221345Sdim  llvm_unreachable("Array type trait not covered by switch");
1758221345Sdim}
1759221345Sdim
1760221345Sdimstatic const char *getExpressionTraitName(ExpressionTrait ET) {
1761221345Sdim  switch (ET) {
1762221345Sdim  case ET_IsLValueExpr:      return "__is_lvalue_expr";
1763221345Sdim  case ET_IsRValueExpr:      return "__is_rvalue_expr";
1764221345Sdim  }
1765221345Sdim  llvm_unreachable("Expression type trait not covered by switch");
1766221345Sdim}
1767221345Sdim
1768193326Sedvoid StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1769249423Sdim  OS << getTypeTraitName(E->getTrait()) << '(';
1770249423Sdim  E->getQueriedType().print(OS, Policy);
1771249423Sdim  OS << ')';
1772193326Sed}
1773193326Sed
1774218893Sdimvoid StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1775249423Sdim  OS << getTypeTraitName(E->getTrait()) << '(';
1776249423Sdim  E->getLhsType().print(OS, Policy);
1777249423Sdim  OS << ',';
1778249423Sdim  E->getRhsType().print(OS, Policy);
1779249423Sdim  OS << ')';
1780218893Sdim}
1781218893Sdim
1782234353Sdimvoid StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1783234353Sdim  OS << getTypeTraitName(E->getTrait()) << "(";
1784234353Sdim  for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1785234353Sdim    if (I > 0)
1786234353Sdim      OS << ", ";
1787249423Sdim    E->getArg(I)->getType().print(OS, Policy);
1788234353Sdim  }
1789234353Sdim  OS << ")";
1790234353Sdim}
1791234353Sdim
1792221345Sdimvoid StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
1793249423Sdim  OS << getTypeTraitName(E->getTrait()) << '(';
1794249423Sdim  E->getQueriedType().print(OS, Policy);
1795249423Sdim  OS << ')';
1796221345Sdim}
1797221345Sdim
1798221345Sdimvoid StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
1799249423Sdim  OS << getExpressionTraitName(E->getTrait()) << '(';
1800249423Sdim  PrintExpr(E->getQueriedExpression());
1801249423Sdim  OS << ')';
1802221345Sdim}
1803221345Sdim
1804218893Sdimvoid StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1805218893Sdim  OS << "noexcept(";
1806218893Sdim  PrintExpr(E->getOperand());
1807218893Sdim  OS << ")";
1808218893Sdim}
1809218893Sdim
1810218893Sdimvoid StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
1811218893Sdim  PrintExpr(E->getPattern());
1812218893Sdim  OS << "...";
1813218893Sdim}
1814218893Sdim
1815218893Sdimvoid StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
1816234353Sdim  OS << "sizeof...(" << *E->getPack() << ")";
1817218893Sdim}
1818218893Sdim
1819218893Sdimvoid StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1820218893Sdim                                       SubstNonTypeTemplateParmPackExpr *Node) {
1821234353Sdim  OS << *Node->getParameterPack();
1822218893Sdim}
1823218893Sdim
1824224145Sdimvoid StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1825224145Sdim                                       SubstNonTypeTemplateParmExpr *Node) {
1826224145Sdim  Visit(Node->getReplacement());
1827224145Sdim}
1828224145Sdim
1829243830Sdimvoid StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
1830243830Sdim  OS << *E->getParameterPack();
1831243830Sdim}
1832243830Sdim
1833224145Sdimvoid StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1834224145Sdim  PrintExpr(Node->GetTemporaryExpr());
1835224145Sdim}
1836224145Sdim
1837198092Srdivacky// Obj-C
1838193326Sed
1839193326Sedvoid StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1840193326Sed  OS << "@";
1841193326Sed  VisitStringLiteral(Node->getString());
1842193326Sed}
1843193326Sed
1844239462Sdimvoid StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
1845234353Sdim  OS << "@";
1846239462Sdim  Visit(E->getSubExpr());
1847234353Sdim}
1848234353Sdim
1849234353Sdimvoid StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
1850234353Sdim  OS << "@[ ";
1851234353Sdim  StmtRange ch = E->children();
1852234353Sdim  if (ch.first != ch.second) {
1853234353Sdim    while (1) {
1854234353Sdim      Visit(*ch.first);
1855234353Sdim      ++ch.first;
1856234353Sdim      if (ch.first == ch.second) break;
1857234353Sdim      OS << ", ";
1858234353Sdim    }
1859234353Sdim  }
1860234353Sdim  OS << " ]";
1861234353Sdim}
1862234353Sdim
1863234353Sdimvoid StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
1864234353Sdim  OS << "@{ ";
1865234353Sdim  for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
1866234353Sdim    if (I > 0)
1867234353Sdim      OS << ", ";
1868234353Sdim
1869234353Sdim    ObjCDictionaryElement Element = E->getKeyValueElement(I);
1870234353Sdim    Visit(Element.Key);
1871234353Sdim    OS << " : ";
1872234353Sdim    Visit(Element.Value);
1873234353Sdim    if (Element.isPackExpansion())
1874234353Sdim      OS << "...";
1875234353Sdim  }
1876234353Sdim  OS << " }";
1877234353Sdim}
1878234353Sdim
1879193326Sedvoid StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
1880249423Sdim  OS << "@encode(";
1881249423Sdim  Node->getEncodedType().print(OS, Policy);
1882249423Sdim  OS << ')';
1883193326Sed}
1884193326Sed
1885193326Sedvoid StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
1886193326Sed  OS << "@selector(" << Node->getSelector().getAsString() << ')';
1887193326Sed}
1888193326Sed
1889193326Sedvoid StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
1890226633Sdim  OS << "@protocol(" << *Node->getProtocol() << ')';
1891193326Sed}
1892193326Sed
1893193326Sedvoid StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1894193326Sed  OS << "[";
1895207619Srdivacky  switch (Mess->getReceiverKind()) {
1896207619Srdivacky  case ObjCMessageExpr::Instance:
1897207619Srdivacky    PrintExpr(Mess->getInstanceReceiver());
1898207619Srdivacky    break;
1899207619Srdivacky
1900207619Srdivacky  case ObjCMessageExpr::Class:
1901249423Sdim    Mess->getClassReceiver().print(OS, Policy);
1902207619Srdivacky    break;
1903207619Srdivacky
1904207619Srdivacky  case ObjCMessageExpr::SuperInstance:
1905207619Srdivacky  case ObjCMessageExpr::SuperClass:
1906207619Srdivacky    OS << "Super";
1907207619Srdivacky    break;
1908207619Srdivacky  }
1909207619Srdivacky
1910193326Sed  OS << ' ';
1911193326Sed  Selector selector = Mess->getSelector();
1912193326Sed  if (selector.isUnarySelector()) {
1913218893Sdim    OS << selector.getNameForSlot(0);
1914193326Sed  } else {
1915193326Sed    for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
1916193326Sed      if (i < selector.getNumArgs()) {
1917193326Sed        if (i > 0) OS << ' ';
1918193326Sed        if (selector.getIdentifierInfoForSlot(i))
1919193326Sed          OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
1920193326Sed        else
1921193326Sed           OS << ":";
1922193326Sed      }
1923193326Sed      else OS << ", "; // Handle variadic methods.
1924198092Srdivacky
1925193326Sed      PrintExpr(Mess->getArg(i));
1926193326Sed    }
1927193326Sed  }
1928193326Sed  OS << "]";
1929193326Sed}
1930193326Sed
1931234353Sdimvoid StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
1932234353Sdim  OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
1933234353Sdim}
1934234353Sdim
1935224145Sdimvoid
1936224145SdimStmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1937224145Sdim  PrintExpr(E->getSubExpr());
1938224145Sdim}
1939193326Sed
1940224145Sdimvoid
1941224145SdimStmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
1942249423Sdim  OS << '(' << E->getBridgeKindName();
1943249423Sdim  E->getType().print(OS, Policy);
1944249423Sdim  OS << ')';
1945224145Sdim  PrintExpr(E->getSubExpr());
1946224145Sdim}
1947224145Sdim
1948193326Sedvoid StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
1949193326Sed  BlockDecl *BD = Node->getBlockDecl();
1950193326Sed  OS << "^";
1951198092Srdivacky
1952193326Sed  const FunctionType *AFT = Node->getFunctionType();
1953198092Srdivacky
1954193326Sed  if (isa<FunctionNoProtoType>(AFT)) {
1955193326Sed    OS << "()";
1956193326Sed  } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
1957193326Sed    OS << '(';
1958193326Sed    for (BlockDecl::param_iterator AI = BD->param_begin(),
1959193326Sed         E = BD->param_end(); AI != E; ++AI) {
1960193326Sed      if (AI != BD->param_begin()) OS << ", ";
1961249423Sdim      std::string ParamStr = (*AI)->getNameAsString();
1962249423Sdim      (*AI)->getType().print(OS, Policy, ParamStr);
1963193326Sed    }
1964198092Srdivacky
1965193326Sed    const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
1966193326Sed    if (FT->isVariadic()) {
1967193326Sed      if (!BD->param_empty()) OS << ", ";
1968193326Sed      OS << "...";
1969193326Sed    }
1970193326Sed    OS << ')';
1971193326Sed  }
1972249423Sdim  OS << "{ }";
1973193326Sed}
1974193326Sed
1975234353Sdimvoid StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
1976234353Sdim  PrintExpr(Node->getSourceExpr());
1977193326Sed}
1978218893Sdim
1979223017Sdimvoid StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1980223017Sdim  OS << "__builtin_astype(";
1981223017Sdim  PrintExpr(Node->getSrcExpr());
1982249423Sdim  OS << ", ";
1983249423Sdim  Node->getType().print(OS, Policy);
1984223017Sdim  OS << ")";
1985223017Sdim}
1986223017Sdim
1987193326Sed//===----------------------------------------------------------------------===//
1988193326Sed// Stmt method implementations
1989193326Sed//===----------------------------------------------------------------------===//
1990193326Sed
1991263508Sdimvoid Stmt::dumpPretty(const ASTContext &Context) const {
1992239462Sdim  printPretty(llvm::errs(), 0, PrintingPolicy(Context.getLangOpts()));
1993193326Sed}
1994193326Sed
1995239462Sdimvoid Stmt::printPretty(raw_ostream &OS,
1996239462Sdim                       PrinterHelper *Helper,
1997193326Sed                       const PrintingPolicy &Policy,
1998193326Sed                       unsigned Indentation) const {
1999193326Sed  if (this == 0) {
2000193326Sed    OS << "<NULL>";
2001193326Sed    return;
2002193326Sed  }
2003193326Sed
2004239462Sdim  StmtPrinter P(OS, Helper, Policy, Indentation);
2005193326Sed  P.Visit(const_cast<Stmt*>(this));
2006193326Sed}
2007193326Sed
2008193326Sed//===----------------------------------------------------------------------===//
2009193326Sed// PrinterHelper
2010193326Sed//===----------------------------------------------------------------------===//
2011193326Sed
2012193326Sed// Implement virtual destructor.
2013193326SedPrinterHelper::~PrinterHelper() {}
2014