blob: d09358c08a838b4cd82e6e50f4a4caf7ba0eb999 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner95578782007-08-08 22:51:59 +000010// This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which
11// pretty print the AST back out to C code.
Chris Lattner4b009652007-07-25 00:24:17 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/StmtVisitor.h"
Douglas Gregor566782a2009-01-06 05:10:23 +000016#include "clang/AST/DeclCXX.h"
Ted Kremenek10364dd2007-10-17 18:36:42 +000017#include "clang/AST/DeclObjC.h"
Ted Kremenek08176a52007-08-31 21:30:12 +000018#include "clang/AST/PrettyPrinter.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "llvm/Support/Compiler.h"
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000020#include "llvm/Support/Format.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// StmtPrinter Visitor
25//===----------------------------------------------------------------------===//
26
27namespace {
Chris Lattner7ba21fa2007-08-21 04:04:25 +000028 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000029 llvm::raw_ostream &OS;
Eli Friedman4bdae722009-05-30 05:03:24 +000030 ASTContext &Context;
Chris Lattner4b009652007-07-25 00:24:17 +000031 unsigned IndentLevel;
Ted Kremenek08176a52007-08-31 21:30:12 +000032 clang::PrinterHelper* Helper;
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +000033 PrintingPolicy Policy;
34
Chris Lattner4b009652007-07-25 00:24:17 +000035 public:
Eli Friedman4bdae722009-05-30 05:03:24 +000036 StmtPrinter(llvm::raw_ostream &os, ASTContext &C, PrinterHelper* helper,
Chris Lattner7099c782009-06-30 01:26:17 +000037 const PrintingPolicy &Policy,
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +000038 unsigned Indentation = 0)
Eli Friedman4bdae722009-05-30 05:03:24 +000039 : OS(os), Context(C), IndentLevel(Indentation), Helper(helper),
40 Policy(Policy) {}
Chris Lattner4b009652007-07-25 00:24:17 +000041
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +000042 void PrintStmt(Stmt *S) {
43 PrintStmt(S, Policy.Indentation);
44 }
45
46 void PrintStmt(Stmt *S, int SubIndent) {
Chris Lattner4b009652007-07-25 00:24:17 +000047 IndentLevel += SubIndent;
48 if (S && isa<Expr>(S)) {
49 // If this is an expr used in a stmt context, indent and newline it.
50 Indent();
Chris Lattner7ba21fa2007-08-21 04:04:25 +000051 Visit(S);
Chris Lattner4b009652007-07-25 00:24:17 +000052 OS << ";\n";
53 } else if (S) {
Chris Lattner7ba21fa2007-08-21 04:04:25 +000054 Visit(S);
Chris Lattner4b009652007-07-25 00:24:17 +000055 } else {
56 Indent() << "<<<NULL STATEMENT>>>\n";
57 }
58 IndentLevel -= SubIndent;
59 }
Eli Friedmane66ecf52009-05-30 00:19:54 +000060
Chris Lattner4b009652007-07-25 00:24:17 +000061 void PrintRawCompoundStmt(CompoundStmt *S);
62 void PrintRawDecl(Decl *D);
Ted Kremenek388b2842008-10-06 18:39:36 +000063 void PrintRawDeclStmt(DeclStmt *S);
Chris Lattner4b009652007-07-25 00:24:17 +000064 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl237116b2008-12-22 21:35:02 +000065 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Chris Lattner4b009652007-07-25 00:24:17 +000066
67 void PrintExpr(Expr *E) {
68 if (E)
Chris Lattner7ba21fa2007-08-21 04:04:25 +000069 Visit(E);
Chris Lattner4b009652007-07-25 00:24:17 +000070 else
71 OS << "<null expr>";
72 }
73
Mike Stumpc43f4fc2009-02-10 20:16:46 +000074 llvm::raw_ostream &Indent(int Delta = 0) {
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +000075 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
76 OS << " ";
Chris Lattner4b009652007-07-25 00:24:17 +000077 return OS;
78 }
79
Chris Lattner2af6a802007-08-30 17:59:59 +000080 bool PrintOffsetOfDesignator(Expr *E);
81 void VisitUnaryOffsetOf(UnaryOperator *Node);
82
Ted Kremenek08176a52007-08-31 21:30:12 +000083 void Visit(Stmt* S) {
84 if (Helper && Helper->handledStmt(S,OS))
85 return;
86 else StmtVisitor<StmtPrinter>::Visit(S);
87 }
88
Chris Lattner7ba21fa2007-08-21 04:04:25 +000089 void VisitStmt(Stmt *Node);
Douglas Gregor19330152008-11-14 12:46:07 +000090#define STMT(CLASS, PARENT) \
Chris Lattner7ba21fa2007-08-21 04:04:25 +000091 void Visit##CLASS(CLASS *Node);
Chris Lattner4b009652007-07-25 00:24:17 +000092#include "clang/AST/StmtNodes.def"
93 };
94}
95
96//===----------------------------------------------------------------------===//
97// Stmt printing methods.
98//===----------------------------------------------------------------------===//
99
100void StmtPrinter::VisitStmt(Stmt *Node) {
101 Indent() << "<<unknown stmt type>>\n";
102}
103
104/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
105/// with no newline after the }.
106void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
107 OS << "{\n";
108 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
109 I != E; ++I)
110 PrintStmt(*I);
111
112 Indent() << "}";
113}
114
115void StmtPrinter::PrintRawDecl(Decl *D) {
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000116 D->print(OS, Policy, IndentLevel);
Mike Stumpc43f4fc2009-02-10 20:16:46 +0000117}
118
Ted Kremenek388b2842008-10-06 18:39:36 +0000119void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Eli Friedmane66ecf52009-05-30 00:19:54 +0000120 DeclStmt::decl_iterator Begin = S->decl_begin(), End = S->decl_end();
Eli Friedmand73364a2009-05-30 04:20:30 +0000121 llvm::SmallVector<Decl*, 2> Decls;
122 for ( ; Begin != End; ++Begin)
123 Decls.push_back(*Begin);
Eli Friedmane66ecf52009-05-30 00:19:54 +0000124
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000125 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenek388b2842008-10-06 18:39:36 +0000126}
Chris Lattner4b009652007-07-25 00:24:17 +0000127
128void StmtPrinter::VisitNullStmt(NullStmt *Node) {
129 Indent() << ";\n";
130}
131
132void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedmane66ecf52009-05-30 00:19:54 +0000133 Indent();
134 PrintRawDeclStmt(Node);
135 OS << ";\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000136}
137
138void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
139 Indent();
140 PrintRawCompoundStmt(Node);
141 OS << "\n";
142}
143
144void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
145 Indent(-1) << "case ";
146 PrintExpr(Node->getLHS());
147 if (Node->getRHS()) {
148 OS << " ... ";
149 PrintExpr(Node->getRHS());
150 }
151 OS << ":\n";
152
153 PrintStmt(Node->getSubStmt(), 0);
154}
155
156void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
157 Indent(-1) << "default:\n";
158 PrintStmt(Node->getSubStmt(), 0);
159}
160
161void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
162 Indent(-1) << Node->getName() << ":\n";
163 PrintStmt(Node->getSubStmt(), 0);
164}
165
166void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redlfd835a22009-02-07 20:05:48 +0000167 OS << "if (";
Chris Lattner4b009652007-07-25 00:24:17 +0000168 PrintExpr(If->getCond());
Sebastian Redlfd835a22009-02-07 20:05:48 +0000169 OS << ')';
Chris Lattner4b009652007-07-25 00:24:17 +0000170
171 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
172 OS << ' ';
173 PrintRawCompoundStmt(CS);
174 OS << (If->getElse() ? ' ' : '\n');
175 } else {
176 OS << '\n';
177 PrintStmt(If->getThen());
178 if (If->getElse()) Indent();
179 }
180
181 if (Stmt *Else = If->getElse()) {
182 OS << "else";
183
184 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
185 OS << ' ';
186 PrintRawCompoundStmt(CS);
187 OS << '\n';
188 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
189 OS << ' ';
190 PrintRawIfStmt(ElseIf);
191 } else {
192 OS << '\n';
193 PrintStmt(If->getElse());
194 }
195 }
196}
197
198void StmtPrinter::VisitIfStmt(IfStmt *If) {
199 Indent();
200 PrintRawIfStmt(If);
201}
202
203void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
204 Indent() << "switch (";
205 PrintExpr(Node->getCond());
206 OS << ")";
207
208 // Pretty print compoundstmt bodies (very common).
209 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
210 OS << " ";
211 PrintRawCompoundStmt(CS);
212 OS << "\n";
213 } else {
214 OS << "\n";
215 PrintStmt(Node->getBody());
216 }
217}
218
219void StmtPrinter::VisitSwitchCase(SwitchCase*) {
220 assert(0 && "SwitchCase is an abstract class");
221}
222
223void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
224 Indent() << "while (";
225 PrintExpr(Node->getCond());
226 OS << ")\n";
227 PrintStmt(Node->getBody());
228}
229
230void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattnercfe0ff02007-09-15 21:49:37 +0000231 Indent() << "do ";
232 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
233 PrintRawCompoundStmt(CS);
234 OS << " ";
235 } else {
236 OS << "\n";
237 PrintStmt(Node->getBody());
238 Indent();
239 }
240
Eli Friedmanea6a2622009-05-17 01:05:34 +0000241 OS << "while (";
Chris Lattner4b009652007-07-25 00:24:17 +0000242 PrintExpr(Node->getCond());
Eli Friedmanea6a2622009-05-17 01:05:34 +0000243 OS << ");\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000244}
245
246void StmtPrinter::VisitForStmt(ForStmt *Node) {
247 Indent() << "for (";
248 if (Node->getInit()) {
249 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek388b2842008-10-06 18:39:36 +0000250 PrintRawDeclStmt(DS);
Chris Lattner4b009652007-07-25 00:24:17 +0000251 else
252 PrintExpr(cast<Expr>(Node->getInit()));
253 }
Chris Lattnercfe0ff02007-09-15 21:49:37 +0000254 OS << ";";
255 if (Node->getCond()) {
256 OS << " ";
Chris Lattner4b009652007-07-25 00:24:17 +0000257 PrintExpr(Node->getCond());
Chris Lattnercfe0ff02007-09-15 21:49:37 +0000258 }
259 OS << ";";
260 if (Node->getInc()) {
261 OS << " ";
Chris Lattner4b009652007-07-25 00:24:17 +0000262 PrintExpr(Node->getInc());
Chris Lattnercfe0ff02007-09-15 21:49:37 +0000263 }
264 OS << ") ";
265
266 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
267 PrintRawCompoundStmt(CS);
268 OS << "\n";
269 } else {
270 OS << "\n";
271 PrintStmt(Node->getBody());
272 }
Chris Lattner4b009652007-07-25 00:24:17 +0000273}
274
Ted Kremenek42730c52008-01-07 19:49:32 +0000275void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000276 Indent() << "for (";
277 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek388b2842008-10-06 18:39:36 +0000278 PrintRawDeclStmt(DS);
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000279 else
280 PrintExpr(cast<Expr>(Node->getElement()));
281 OS << " in ";
282 PrintExpr(Node->getCollection());
283 OS << ") ";
284
285 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
286 PrintRawCompoundStmt(CS);
287 OS << "\n";
288 } else {
289 OS << "\n";
290 PrintStmt(Node->getBody());
291 }
292}
293
Chris Lattner4b009652007-07-25 00:24:17 +0000294void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
295 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
296}
297
298void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
299 Indent() << "goto *";
300 PrintExpr(Node->getTarget());
301 OS << ";\n";
302}
303
304void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
305 Indent() << "continue;\n";
306}
307
308void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
309 Indent() << "break;\n";
310}
311
312
313void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
314 Indent() << "return";
315 if (Node->getRetValue()) {
316 OS << " ";
317 PrintExpr(Node->getRetValue());
318 }
319 OS << ";\n";
320}
321
Chris Lattner8a40a832007-10-29 04:04:16 +0000322
323void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson759f45d2007-11-23 23:12:25 +0000324 Indent() << "asm ";
325
326 if (Node->isVolatile())
327 OS << "volatile ";
328
329 OS << "(";
Anders Carlsson076c1112007-11-20 19:21:03 +0000330 VisitStringLiteral(Node->getAsmString());
Anders Carlsson965d5202007-11-22 01:36:19 +0000331
332 // Outputs
333 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
334 Node->getNumClobbers() != 0)
335 OS << " : ";
336
337 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
338 if (i != 0)
339 OS << ", ";
340
341 if (!Node->getOutputName(i).empty()) {
342 OS << '[';
343 OS << Node->getOutputName(i);
344 OS << "] ";
345 }
346
Chris Lattnere8625112009-03-10 04:59:06 +0000347 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlsson965d5202007-11-22 01:36:19 +0000348 OS << " ";
349 Visit(Node->getOutputExpr(i));
350 }
351
352 // Inputs
353 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
354 OS << " : ";
355
356 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
357 if (i != 0)
358 OS << ", ";
359
360 if (!Node->getInputName(i).empty()) {
361 OS << '[';
362 OS << Node->getInputName(i);
363 OS << "] ";
364 }
365
Chris Lattnere8625112009-03-10 04:59:06 +0000366 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlsson965d5202007-11-22 01:36:19 +0000367 OS << " ";
368 Visit(Node->getInputExpr(i));
369 }
370
371 // Clobbers
372 if (Node->getNumClobbers() != 0)
373 OS << " : ";
374
375 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
376 if (i != 0)
377 OS << ", ";
378
379 VisitStringLiteral(Node->getClobber(i));
380 }
381
Anders Carlsson076c1112007-11-20 19:21:03 +0000382 OS << ");\n";
Chris Lattner8a40a832007-10-29 04:04:16 +0000383}
384
Ted Kremenek42730c52008-01-07 19:49:32 +0000385void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian59e9e042007-11-02 18:16:07 +0000386 Indent() << "@try";
387 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
388 PrintRawCompoundStmt(TS);
389 OS << "\n";
390 }
391
Ted Kremenek42730c52008-01-07 19:49:32 +0000392 for (ObjCAtCatchStmt *catchStmt =
393 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian59e9e042007-11-02 18:16:07 +0000394 catchStmt;
395 catchStmt =
Ted Kremenek42730c52008-01-07 19:49:32 +0000396 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian59e9e042007-11-02 18:16:07 +0000397 Indent() << "@catch(";
Steve Naroff0e8b96a2009-03-03 19:52:17 +0000398 if (catchStmt->getCatchParamDecl()) {
399 if (Decl *DS = catchStmt->getCatchParamDecl())
400 PrintRawDecl(DS);
Fariborz Jahanian59e9e042007-11-02 18:16:07 +0000401 }
402 OS << ")";
403 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
404 {
405 PrintRawCompoundStmt(CS);
406 OS << "\n";
407 }
408 }
409
Ted Kremenek42730c52008-01-07 19:49:32 +0000410 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahanian50b47922007-11-07 00:46:42 +0000411 Node->getFinallyStmt())) {
412 Indent() << "@finally";
413 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian59e9e042007-11-02 18:16:07 +0000414 OS << "\n";
415 }
Fariborz Jahanian70952482007-11-01 21:12:44 +0000416}
417
Ted Kremenek42730c52008-01-07 19:49:32 +0000418void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian70952482007-11-01 21:12:44 +0000419}
420
Ted Kremenek42730c52008-01-07 19:49:32 +0000421void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian70952482007-11-01 21:12:44 +0000422 Indent() << "@catch (...) { /* todo */ } \n";
423}
424
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +0000425void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000426 Indent() << "@throw";
427 if (Node->getThrowExpr()) {
428 OS << " ";
429 PrintExpr(Node->getThrowExpr());
430 }
431 OS << ";\n";
432}
433
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +0000434void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanian993360a2008-01-29 18:21:32 +0000435 Indent() << "@synchronized (";
436 PrintExpr(Node->getSynchExpr());
437 OS << ")";
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +0000438 PrintRawCompoundStmt(Node->getSynchBody());
439 OS << "\n";
Fariborz Jahanian993360a2008-01-29 18:21:32 +0000440}
441
Sebastian Redl237116b2008-12-22 21:35:02 +0000442void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
443 OS << "catch (";
Sebastian Redl743c8162008-12-22 19:15:10 +0000444 if (Decl *ExDecl = Node->getExceptionDecl())
445 PrintRawDecl(ExDecl);
446 else
447 OS << "...";
448 OS << ") ";
449 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl237116b2008-12-22 21:35:02 +0000450}
451
452void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
453 Indent();
454 PrintRawCXXCatchStmt(Node);
455 OS << "\n";
456}
457
458void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
459 Indent() << "try ";
460 PrintRawCompoundStmt(Node->getTryBlock());
461 for(unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
462 OS << " ";
463 PrintRawCXXCatchStmt(Node->getHandler(i));
464 }
Sebastian Redl743c8162008-12-22 19:15:10 +0000465 OS << "\n";
466}
467
Chris Lattner4b009652007-07-25 00:24:17 +0000468//===----------------------------------------------------------------------===//
469// Expr printing methods.
470//===----------------------------------------------------------------------===//
471
472void StmtPrinter::VisitExpr(Expr *Node) {
473 OS << "<<unknown expr type>>";
474}
475
476void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner6c5ec622008-11-24 04:00:27 +0000477 OS << Node->getDecl()->getNameAsString();
Chris Lattner4b009652007-07-25 00:24:17 +0000478}
479
Douglas Gregor7e508262009-03-19 03:51:16 +0000480void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) {
Douglas Gregor566782a2009-01-06 05:10:23 +0000481 NamedDecl *D = Node->getDecl();
482
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +0000483 Node->getQualifier()->print(OS, Policy);
Douglas Gregor566782a2009-01-06 05:10:23 +0000484 OS << D->getNameAsString();
485}
486
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000487void StmtPrinter::VisitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *Node) {
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +0000488 Node->getQualifier()->print(OS, Policy);
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000489 OS << Node->getDeclName().getAsString();
490}
491
Douglas Gregor28857752009-06-30 22:34:41 +0000492void StmtPrinter::VisitTemplateIdRefExpr(TemplateIdRefExpr *Node) {
493 if (Node->getQualifier())
494 Node->getQualifier()->print(OS, Policy);
495 Node->getTemplateName().print(OS, Policy, true);
496 OS << '<';
497 OS << TemplateSpecializationType::PrintTemplateArgumentList(
498 Node->getTemplateArgs(),
499 Node->getNumTemplateArgs(),
500 Policy);
501 OS << '>';
502}
503
Steve Naroff5eb2a4a2007-11-12 14:29:37 +0000504void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000505 if (Node->getBase()) {
506 PrintExpr(Node->getBase());
507 OS << (Node->isArrow() ? "->" : ".");
508 }
Chris Lattner6c5ec622008-11-24 04:00:27 +0000509 OS << Node->getDecl()->getNameAsString();
Steve Naroff5eb2a4a2007-11-12 14:29:37 +0000510}
511
Steve Naroff05391d22008-05-30 00:40:33 +0000512void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
513 if (Node->getBase()) {
514 PrintExpr(Node->getBase());
515 OS << ".";
516 }
Steve Naroff0e948412008-12-04 16:24:46 +0000517 OS << Node->getProperty()->getNameAsCString();
Steve Naroff05391d22008-05-30 00:40:33 +0000518}
519
Fariborz Jahanian128cdc52009-08-20 17:02:02 +0000520void StmtPrinter::VisitObjCImplicitSetterGetterRefExpr(
521 ObjCImplicitSetterGetterRefExpr *Node) {
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000522 if (Node->getBase()) {
523 PrintExpr(Node->getBase());
524 OS << ".";
525 }
Fariborz Jahanian1c4da452009-08-18 20:50:23 +0000526 if (Node->getGetterMethod())
527 OS << Node->getGetterMethod()->getNameAsString();
528
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000529}
530
Chris Lattner69909292008-08-10 01:53:14 +0000531void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Chris Lattner4b009652007-07-25 00:24:17 +0000532 switch (Node->getIdentType()) {
533 default:
534 assert(0 && "unknown case");
Chris Lattner69909292008-08-10 01:53:14 +0000535 case PredefinedExpr::Func:
Chris Lattner4b009652007-07-25 00:24:17 +0000536 OS << "__func__";
537 break;
Chris Lattner69909292008-08-10 01:53:14 +0000538 case PredefinedExpr::Function:
Chris Lattner4b009652007-07-25 00:24:17 +0000539 OS << "__FUNCTION__";
540 break;
Chris Lattner69909292008-08-10 01:53:14 +0000541 case PredefinedExpr::PrettyFunction:
Chris Lattner4b009652007-07-25 00:24:17 +0000542 OS << "__PRETTY_FUNCTION__";
543 break;
544 }
545}
546
547void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner4b009652007-07-25 00:24:17 +0000548 unsigned value = Node->getValue();
Chris Lattner1aaf71c2008-06-07 22:35:38 +0000549 if (Node->isWide())
550 OS << "L";
Chris Lattner4b009652007-07-25 00:24:17 +0000551 switch (value) {
552 case '\\':
553 OS << "'\\\\'";
554 break;
555 case '\'':
556 OS << "'\\''";
557 break;
558 case '\a':
559 // TODO: K&R: the meaning of '\\a' is different in traditional C
560 OS << "'\\a'";
561 break;
562 case '\b':
563 OS << "'\\b'";
564 break;
565 // Nonstandard escape sequence.
566 /*case '\e':
567 OS << "'\\e'";
568 break;*/
569 case '\f':
570 OS << "'\\f'";
571 break;
572 case '\n':
573 OS << "'\\n'";
574 break;
575 case '\r':
576 OS << "'\\r'";
577 break;
578 case '\t':
579 OS << "'\\t'";
580 break;
581 case '\v':
582 OS << "'\\v'";
583 break;
584 default:
Ted Kremenekf91bc1d2008-02-23 00:52:04 +0000585 if (value < 256 && isprint(value)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000586 OS << "'" << (char)value << "'";
587 } else if (value < 256) {
Ted Kremenek7b6f67b2008-09-13 05:16:45 +0000588 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner4b009652007-07-25 00:24:17 +0000589 } else {
590 // FIXME what to really do here?
591 OS << value;
592 }
593 }
594}
595
596void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
597 bool isSigned = Node->getType()->isSignedIntegerType();
598 OS << Node->getValue().toString(10, isSigned);
599
600 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000601 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000602 default: assert(0 && "Unexpected type for integer literal!");
603 case BuiltinType::Int: break; // no suffix.
604 case BuiltinType::UInt: OS << 'U'; break;
605 case BuiltinType::Long: OS << 'L'; break;
606 case BuiltinType::ULong: OS << "UL"; break;
607 case BuiltinType::LongLong: OS << "LL"; break;
608 case BuiltinType::ULongLong: OS << "ULL"; break;
609 }
610}
611void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner6a390402007-08-01 00:23:58 +0000612 // FIXME: print value more precisely.
Chris Lattnere0391b22008-06-07 22:13:43 +0000613 OS << Node->getValueAsApproximateDouble();
Chris Lattner4b009652007-07-25 00:24:17 +0000614}
Chris Lattner1de66eb2007-08-26 03:42:43 +0000615
616void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
617 PrintExpr(Node->getSubExpr());
618 OS << "i";
619}
620
Chris Lattner4b009652007-07-25 00:24:17 +0000621void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
622 if (Str->isWide()) OS << 'L';
623 OS << '"';
Anders Carlsson55bfe0d2007-10-15 02:50:23 +0000624
Chris Lattner4b009652007-07-25 00:24:17 +0000625 // FIXME: this doesn't print wstrings right.
626 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner5919a852009-01-16 19:25:18 +0000627 unsigned char Char = Str->getStrData()[i];
628
629 switch (Char) {
630 default:
631 if (isprint(Char))
632 OS << (char)Char;
633 else // Output anything hard as an octal escape.
634 OS << '\\'
635 << (char)('0'+ ((Char >> 6) & 7))
636 << (char)('0'+ ((Char >> 3) & 7))
637 << (char)('0'+ ((Char >> 0) & 7));
638 break;
639 // Handle some common non-printable cases to make dumps prettier.
Chris Lattner4b009652007-07-25 00:24:17 +0000640 case '\\': OS << "\\\\"; break;
641 case '"': OS << "\\\""; break;
642 case '\n': OS << "\\n"; break;
643 case '\t': OS << "\\t"; break;
644 case '\a': OS << "\\a"; break;
645 case '\b': OS << "\\b"; break;
646 }
647 }
648 OS << '"';
649}
650void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
651 OS << "(";
652 PrintExpr(Node->getSubExpr());
653 OS << ")";
654}
655void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner2b97b092007-08-23 21:46:40 +0000656 if (!Node->isPostfix()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000657 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner2b97b092007-08-23 21:46:40 +0000658
Eli Friedman34084c22009-06-14 22:39:26 +0000659 // Print a space if this is an "identifier operator" like __real, or if
660 // it might be concatenated incorrectly like '+'.
Chris Lattner2b97b092007-08-23 21:46:40 +0000661 switch (Node->getOpcode()) {
662 default: break;
Chris Lattner2b97b092007-08-23 21:46:40 +0000663 case UnaryOperator::Real:
664 case UnaryOperator::Imag:
665 case UnaryOperator::Extension:
666 OS << ' ';
667 break;
Eli Friedman34084c22009-06-14 22:39:26 +0000668 case UnaryOperator::Plus:
669 case UnaryOperator::Minus:
670 if (isa<UnaryOperator>(Node->getSubExpr()))
671 OS << ' ';
672 break;
Chris Lattner2b97b092007-08-23 21:46:40 +0000673 }
674 }
Chris Lattner4b009652007-07-25 00:24:17 +0000675 PrintExpr(Node->getSubExpr());
676
677 if (Node->isPostfix())
678 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner4b009652007-07-25 00:24:17 +0000679}
Chris Lattner2af6a802007-08-30 17:59:59 +0000680
681bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
Eli Friedman342d9432009-02-27 06:44:11 +0000682 if (isa<UnaryOperator>(E)) {
Chris Lattner2af6a802007-08-30 17:59:59 +0000683 // Base case, print the type and comma.
684 OS << E->getType().getAsString() << ", ";
685 return true;
686 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
687 PrintOffsetOfDesignator(ASE->getLHS());
688 OS << "[";
689 PrintExpr(ASE->getRHS());
690 OS << "]";
691 return false;
692 } else {
693 MemberExpr *ME = cast<MemberExpr>(E);
694 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner6c5ec622008-11-24 04:00:27 +0000695 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner2af6a802007-08-30 17:59:59 +0000696 return false;
697 }
698}
699
700void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
701 OS << "__builtin_offsetof(";
702 PrintOffsetOfDesignator(Node->getSubExpr());
703 OS << ")";
704}
705
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000706void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
707 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
708 if (Node->isArgumentType())
709 OS << "(" << Node->getArgumentType().getAsString() << ")";
710 else {
711 OS << " ";
712 PrintExpr(Node->getArgumentExpr());
713 }
Chris Lattner4b009652007-07-25 00:24:17 +0000714}
715void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000716 PrintExpr(Node->getLHS());
Chris Lattner4b009652007-07-25 00:24:17 +0000717 OS << "[";
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000718 PrintExpr(Node->getRHS());
Chris Lattner4b009652007-07-25 00:24:17 +0000719 OS << "]";
720}
721
722void StmtPrinter::VisitCallExpr(CallExpr *Call) {
723 PrintExpr(Call->getCallee());
724 OS << "(";
725 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner3e254fb2008-04-08 04:40:51 +0000726 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
727 // Don't print any defaulted arguments
728 break;
729 }
730
Chris Lattner4b009652007-07-25 00:24:17 +0000731 if (i) OS << ", ";
732 PrintExpr(Call->getArg(i));
733 }
734 OS << ")";
735}
736void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorbdbbc2d2009-01-08 22:45:41 +0000737 // FIXME: Suppress printing implicit bases (like "this")
738 PrintExpr(Node->getBase());
739 OS << (Node->isArrow() ? "->" : ".");
740 // FIXME: Suppress printing references to unnamed objects
741 // representing anonymous unions/structs
Douglas Gregor82d44772008-12-20 23:49:58 +0000742 OS << Node->getMemberDecl()->getNameAsString();
Chris Lattner4b009652007-07-25 00:24:17 +0000743}
Steve Naroff29d293b2009-07-24 17:54:45 +0000744void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
745 PrintExpr(Node->getBase());
746 OS << (Node->isArrow() ? "->isa" : ".isa");
747}
748
Nate Begemanaf6ed502008-04-18 23:10:10 +0000749void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroffc11705f2007-07-28 23:10:27 +0000750 PrintExpr(Node->getBase());
751 OS << ".";
752 OS << Node->getAccessor().getName();
753}
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +0000754void StmtPrinter::VisitCastExpr(CastExpr *) {
755 assert(0 && "CastExpr is an abstract class");
756}
Douglas Gregor21a04f32008-10-27 19:41:14 +0000757void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
758 assert(0 && "ExplicitCastExpr is an abstract class");
759}
Douglas Gregor035d0882008-10-28 15:36:24 +0000760void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner4b009652007-07-25 00:24:17 +0000761 OS << "(" << Node->getType().getAsString() << ")";
762 PrintExpr(Node->getSubExpr());
763}
764void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
765 OS << "(" << Node->getType().getAsString() << ")";
766 PrintExpr(Node->getInitializer());
767}
768void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
769 // No need to print anything, simply forward to the sub expression.
770 PrintExpr(Node->getSubExpr());
771}
772void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
773 PrintExpr(Node->getLHS());
774 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
775 PrintExpr(Node->getRHS());
776}
Chris Lattner06078d22007-08-25 02:00:02 +0000777void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
778 PrintExpr(Node->getLHS());
779 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
780 PrintExpr(Node->getRHS());
781}
Chris Lattner4b009652007-07-25 00:24:17 +0000782void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
783 PrintExpr(Node->getCond());
Ted Kremenekeecd9e82007-11-26 18:27:54 +0000784
785 if (Node->getLHS()) {
786 OS << " ? ";
787 PrintExpr(Node->getLHS());
788 OS << " : ";
789 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000790 else { // Handle GCC extension where LHS can be NULL.
Ted Kremenekeecd9e82007-11-26 18:27:54 +0000791 OS << " ?: ";
792 }
793
Chris Lattner4b009652007-07-25 00:24:17 +0000794 PrintExpr(Node->getRHS());
795}
796
797// GNU extensions.
798
Chris Lattnera0d03a72007-08-03 17:31:20 +0000799void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattner4b009652007-07-25 00:24:17 +0000800 OS << "&&" << Node->getLabel()->getName();
801}
802
803void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
804 OS << "(";
805 PrintRawCompoundStmt(E->getSubStmt());
806 OS << ")";
807}
808
Steve Naroff63bad2d2007-08-01 22:05:33 +0000809void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
810 OS << "__builtin_types_compatible_p(";
811 OS << Node->getArgType1().getAsString() << ",";
812 OS << Node->getArgType2().getAsString() << ")";
813}
814
Steve Naroff93c53012007-08-03 21:21:27 +0000815void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
816 OS << "__builtin_choose_expr(";
817 PrintExpr(Node->getCond());
Chris Lattner44fcf4f2007-08-04 00:20:15 +0000818 OS << ", ";
Steve Naroff93c53012007-08-03 21:21:27 +0000819 PrintExpr(Node->getLHS());
Chris Lattner44fcf4f2007-08-04 00:20:15 +0000820 OS << ", ";
Steve Naroff93c53012007-08-03 21:21:27 +0000821 PrintExpr(Node->getRHS());
822 OS << ")";
823}
Chris Lattner4b009652007-07-25 00:24:17 +0000824
Douglas Gregorad4b3792008-11-29 04:51:27 +0000825void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
826 OS << "__null";
827}
828
Eli Friedmand0e9d092008-05-14 19:38:39 +0000829void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
830 OS << "__builtin_shufflevector(";
831 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
832 if (i) OS << ", ";
833 PrintExpr(Node->getExpr(i));
834 }
835 OS << ")";
836}
837
Anders Carlsson762b7c72007-08-31 04:56:16 +0000838void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000839 if (Node->getSyntacticForm()) {
840 Visit(Node->getSyntacticForm());
841 return;
842 }
843
Anders Carlsson762b7c72007-08-31 04:56:16 +0000844 OS << "{ ";
845 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
846 if (i) OS << ", ";
Douglas Gregorf603b472009-01-28 21:54:33 +0000847 if (Node->getInit(i))
848 PrintExpr(Node->getInit(i));
849 else
850 OS << "0";
Anders Carlsson762b7c72007-08-31 04:56:16 +0000851 }
852 OS << " }";
853}
854
Nate Begemane85f43d2009-08-10 23:49:36 +0000855void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
856 OS << "( ";
857 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
858 if (i) OS << ", ";
859 PrintExpr(Node->getExpr(i));
860 }
861 OS << " )";
862}
863
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000864void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregorf603b472009-01-28 21:54:33 +0000865 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
866 DEnd = Node->designators_end();
867 D != DEnd; ++D) {
868 if (D->isFieldDesignator()) {
869 if (D->getDotLoc().isInvalid())
870 OS << D->getFieldName()->getName() << ":";
871 else
872 OS << "." << D->getFieldName()->getName();
873 } else {
874 OS << "[";
875 if (D->isArrayDesignator()) {
876 PrintExpr(Node->getArrayIndex(*D));
877 } else {
878 PrintExpr(Node->getArrayRangeStart(*D));
879 OS << " ... ";
880 PrintExpr(Node->getArrayRangeEnd(*D));
881 }
882 OS << "]";
883 }
884 }
885
886 OS << " = ";
887 PrintExpr(Node->getInit());
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000888}
889
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000890void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Chris Lattner7099c782009-06-30 01:26:17 +0000891 if (Policy.LangOpts.CPlusPlus)
Douglas Gregor996677c2009-05-30 00:08:05 +0000892 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
893 else {
894 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
895 if (Node->getType()->isRecordType())
896 OS << "{}";
897 else
898 OS << 0;
899 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000900}
901
Anders Carlsson36760332007-10-15 20:28:48 +0000902void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000903 OS << "__builtin_va_arg(";
Anders Carlsson36760332007-10-15 20:28:48 +0000904 PrintExpr(Node->getSubExpr());
905 OS << ", ";
906 OS << Node->getType().getAsString();
907 OS << ")";
908}
909
Chris Lattner4b009652007-07-25 00:24:17 +0000910// C++
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000911void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
912 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
913 "",
914#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
915 Spelling,
916#include "clang/Basic/OperatorKinds.def"
917 };
918
919 OverloadedOperatorKind Kind = Node->getOperator();
920 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
921 if (Node->getNumArgs() == 1) {
922 OS << OpStrings[Kind] << ' ';
923 PrintExpr(Node->getArg(0));
924 } else {
925 PrintExpr(Node->getArg(0));
926 OS << ' ' << OpStrings[Kind];
927 }
928 } else if (Kind == OO_Call) {
929 PrintExpr(Node->getArg(0));
930 OS << '(';
931 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
932 if (ArgIdx > 1)
933 OS << ", ";
934 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
935 PrintExpr(Node->getArg(ArgIdx));
936 }
937 OS << ')';
938 } else if (Kind == OO_Subscript) {
939 PrintExpr(Node->getArg(0));
940 OS << '[';
941 PrintExpr(Node->getArg(1));
942 OS << ']';
943 } else if (Node->getNumArgs() == 1) {
944 OS << OpStrings[Kind] << ' ';
945 PrintExpr(Node->getArg(0));
946 } else if (Node->getNumArgs() == 2) {
947 PrintExpr(Node->getArg(0));
948 OS << ' ' << OpStrings[Kind] << ' ';
949 PrintExpr(Node->getArg(1));
950 } else {
951 assert(false && "unknown overloaded operator");
952 }
953}
Chris Lattner4b009652007-07-25 00:24:17 +0000954
Douglas Gregor3257fb52008-12-22 05:46:06 +0000955void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
956 VisitCallExpr(cast<CallExpr>(Node));
957}
958
Douglas Gregor21a04f32008-10-27 19:41:14 +0000959void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
960 OS << Node->getCastName() << '<';
961 OS << Node->getTypeAsWritten().getAsString() << ">(";
Chris Lattner4b009652007-07-25 00:24:17 +0000962 PrintExpr(Node->getSubExpr());
963 OS << ")";
964}
965
Douglas Gregor21a04f32008-10-27 19:41:14 +0000966void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
967 VisitCXXNamedCastExpr(Node);
968}
969
970void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
971 VisitCXXNamedCastExpr(Node);
972}
973
974void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
975 VisitCXXNamedCastExpr(Node);
976}
977
978void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
979 VisitCXXNamedCastExpr(Node);
980}
981
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000982void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
983 OS << "typeid(";
984 if (Node->isTypeOperand()) {
985 OS << Node->getTypeOperand().getAsString();
986 } else {
987 PrintExpr(Node->getExprOperand());
988 }
989 OS << ")";
990}
991
Chris Lattner4b009652007-07-25 00:24:17 +0000992void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
993 OS << (Node->getValue() ? "true" : "false");
994}
995
Sebastian Redl5d0ead72009-05-10 18:38:11 +0000996void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
997 OS << "nullptr";
998}
999
Douglas Gregora5b022a2008-11-04 14:32:21 +00001000void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1001 OS << "this";
1002}
1003
Chris Lattnera7447ba2008-02-26 00:51:44 +00001004void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1005 if (Node->getSubExpr() == 0)
1006 OS << "throw";
1007 else {
1008 OS << "throw ";
1009 PrintExpr(Node->getSubExpr());
1010 }
1011}
1012
Chris Lattner3e254fb2008-04-08 04:40:51 +00001013void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1014 // Nothing to print: we picked up the default argument
1015}
1016
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +00001017void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1018 OS << Node->getType().getAsString();
1019 OS << "(";
1020 PrintExpr(Node->getSubExpr());
1021 OS << ")";
1022}
1023
Anders Carlsson873176e2009-05-30 20:03:25 +00001024void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1025 PrintExpr(Node->getSubExpr());
1026}
1027
Douglas Gregor861e7902009-01-16 18:33:17 +00001028void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1029 OS << Node->getType().getAsString();
1030 OS << "(";
1031 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1032 ArgEnd = Node->arg_end();
1033 Arg != ArgEnd; ++Arg) {
1034 if (Arg != Node->arg_begin())
1035 OS << ", ";
1036 PrintExpr(*Arg);
1037 }
1038 OS << ")";
1039}
1040
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +00001041void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1042 OS << Node->getType().getAsString() << "()";
1043}
1044
Argiris Kirtzidisdbce6c12008-09-09 23:47:53 +00001045void
1046StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
1047 PrintRawDecl(E->getVarDecl());
1048}
1049
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001050void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1051 if (E->isGlobalNew())
1052 OS << "::";
1053 OS << "new ";
1054 unsigned NumPlace = E->getNumPlacementArgs();
1055 if (NumPlace > 0) {
1056 OS << "(";
1057 PrintExpr(E->getPlacementArg(0));
1058 for (unsigned i = 1; i < NumPlace; ++i) {
1059 OS << ", ";
1060 PrintExpr(E->getPlacementArg(i));
1061 }
1062 OS << ") ";
1063 }
1064 if (E->isParenTypeId())
1065 OS << "(";
Sebastian Redl516432a2008-12-02 22:08:59 +00001066 std::string TypeS;
1067 if (Expr *Size = E->getArraySize()) {
1068 llvm::raw_string_ostream s(TypeS);
Eli Friedman1fd09bb2009-05-30 05:32:46 +00001069 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redl516432a2008-12-02 22:08:59 +00001070 s.flush();
1071 TypeS = "[" + TypeS + "]";
1072 }
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +00001073 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redl516432a2008-12-02 22:08:59 +00001074 OS << TypeS;
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001075 if (E->isParenTypeId())
1076 OS << ")";
1077
1078 if (E->hasInitializer()) {
1079 OS << "(";
1080 unsigned NumCons = E->getNumConstructorArgs();
1081 if (NumCons > 0) {
1082 PrintExpr(E->getConstructorArg(0));
1083 for (unsigned i = 1; i < NumCons; ++i) {
1084 OS << ", ";
1085 PrintExpr(E->getConstructorArg(i));
1086 }
1087 }
1088 OS << ")";
1089 }
1090}
1091
1092void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1093 if (E->isGlobalDelete())
1094 OS << "::";
1095 OS << "delete ";
1096 if (E->isArrayForm())
1097 OS << "[] ";
1098 PrintExpr(E->getArgument());
1099}
1100
Douglas Gregor4646f9c2009-02-04 15:01:18 +00001101void StmtPrinter::VisitUnresolvedFunctionNameExpr(UnresolvedFunctionNameExpr *E) {
1102 OS << E->getName().getAsString();
Douglas Gregora133e262008-12-06 00:22:45 +00001103}
1104
Anders Carlssondbbab8c2009-04-23 02:32:43 +00001105void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1106 // Nothing to print.
1107}
1108
Anders Carlsson9f4aef72009-05-01 22:18:43 +00001109void StmtPrinter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Anders Carlssonb5d9c472009-04-24 22:47:04 +00001110 // Just forward to the sub expression.
1111 PrintExpr(E->getSubExpr());
1112}
1113
Douglas Gregorf27b7652009-05-20 18:46:25 +00001114void
1115StmtPrinter::VisitCXXUnresolvedConstructExpr(
1116 CXXUnresolvedConstructExpr *Node) {
1117 OS << Node->getTypeAsWritten().getAsString();
1118 OS << "(";
1119 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
1120 ArgEnd = Node->arg_end();
1121 Arg != ArgEnd; ++Arg) {
1122 if (Arg != Node->arg_begin())
1123 OS << ", ";
1124 PrintExpr(*Arg);
1125 }
1126 OS << ")";
1127}
1128
Douglas Gregorefccbec2009-08-31 21:41:48 +00001129void StmtPrinter::VisitCXXAdornedMemberExpr(CXXAdornedMemberExpr *Node) {
Douglas Gregore399ad42009-08-26 22:36:53 +00001130 // FIXME: Suppress printing implicit bases (like "this")
1131 PrintExpr(Node->getBase());
1132 OS << (Node->isArrow() ? "->" : ".");
1133 // FIXME: Suppress printing references to unnamed objects
1134 // representing anonymous unions/structs
1135 Node->getQualifier()->print(OS, Policy);
1136 OS << Node->getMemberDecl()->getNameAsString();
1137}
1138
Douglas Gregor93b8b0f2009-05-22 21:13:27 +00001139void StmtPrinter::VisitCXXUnresolvedMemberExpr(CXXUnresolvedMemberExpr *Node) {
1140 PrintExpr(Node->getBase());
1141 OS << (Node->isArrow() ? "->" : ".");
1142 OS << Node->getMember().getAsString();
1143}
1144
Sebastian Redl39c0f6f2009-01-05 20:52:13 +00001145static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1146 switch (UTT) {
1147 default: assert(false && "Unknown type trait");
1148 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1149 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1150 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1151 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1152 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1153 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1154 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1155 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1156 case UTT_IsAbstract: return "__is_abstract";
1157 case UTT_IsClass: return "__is_class";
1158 case UTT_IsEmpty: return "__is_empty";
1159 case UTT_IsEnum: return "__is_enum";
1160 case UTT_IsPOD: return "__is_pod";
1161 case UTT_IsPolymorphic: return "__is_polymorphic";
1162 case UTT_IsUnion: return "__is_union";
1163 }
1164}
1165
1166void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1167 OS << getTypeTraitName(E->getTrait()) << "("
1168 << E->getQueriedType().getAsString() << ")";
1169}
1170
Anders Carlssona66cad42007-08-21 17:43:55 +00001171// Obj-C
1172
1173void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1174 OS << "@";
1175 VisitStringLiteral(Node->getString());
1176}
Chris Lattner4b009652007-07-25 00:24:17 +00001177
Anders Carlsson8be1d402007-08-22 15:14:15 +00001178void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner6c5ec622008-11-24 04:00:27 +00001179 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlsson8be1d402007-08-22 15:14:15 +00001180}
1181
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001182void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner6c5ec622008-11-24 04:00:27 +00001183 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001184}
1185
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001186void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner6c5ec622008-11-24 04:00:27 +00001187 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001188}
1189
Steve Naroffc39ca262007-09-18 23:55:05 +00001190void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1191 OS << "[";
Steve Narofffa465d12007-10-02 20:01:56 +00001192 Expr *receiver = Mess->getReceiver();
1193 if (receiver) PrintExpr(receiver);
1194 else OS << Mess->getClassName()->getName();
Ted Kremenekbfec9492008-05-02 17:32:38 +00001195 OS << ' ';
Ted Kremenek2287cee2008-04-16 04:30:16 +00001196 Selector selector = Mess->getSelector();
Steve Narofffa465d12007-10-02 20:01:56 +00001197 if (selector.isUnarySelector()) {
Ted Kremenekbfec9492008-05-02 17:32:38 +00001198 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Narofffa465d12007-10-02 20:01:56 +00001199 } else {
1200 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekbfec9492008-05-02 17:32:38 +00001201 if (i < selector.getNumArgs()) {
1202 if (i > 0) OS << ' ';
1203 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner6c5ec622008-11-24 04:00:27 +00001204 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekbfec9492008-05-02 17:32:38 +00001205 else
1206 OS << ":";
1207 }
1208 else OS << ", "; // Handle variadic methods.
1209
Steve Narofffa465d12007-10-02 20:01:56 +00001210 PrintExpr(Mess->getArg(i));
1211 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001212 }
1213 OS << "]";
1214}
1215
Douglas Gregord8606632008-11-04 14:56:14 +00001216void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1217 OS << "super";
1218}
1219
Steve Naroff52a81c02008-09-03 18:15:37 +00001220void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff9ac456d2008-10-08 17:01:13 +00001221 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff52a81c02008-09-03 18:15:37 +00001222 OS << "^";
1223
1224 const FunctionType *AFT = Node->getFunctionType();
1225
Douglas Gregor4fa58902009-02-26 23:50:07 +00001226 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroff52a81c02008-09-03 18:15:37 +00001227 OS << "()";
Douglas Gregor4fa58902009-02-26 23:50:07 +00001228 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroff52a81c02008-09-03 18:15:37 +00001229 OS << '(';
1230 std::string ParamStr;
Steve Naroff9ac456d2008-10-08 17:01:13 +00001231 for (BlockDecl::param_iterator AI = BD->param_begin(),
1232 E = BD->param_end(); AI != E; ++AI) {
1233 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner6c5ec622008-11-24 04:00:27 +00001234 ParamStr = (*AI)->getNameAsString();
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +00001235 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroff52a81c02008-09-03 18:15:37 +00001236 OS << ParamStr;
1237 }
1238
Douglas Gregor4fa58902009-02-26 23:50:07 +00001239 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroff52a81c02008-09-03 18:15:37 +00001240 if (FT->isVariadic()) {
Steve Naroff9ac456d2008-10-08 17:01:13 +00001241 if (!BD->param_empty()) OS << ", ";
Steve Naroff52a81c02008-09-03 18:15:37 +00001242 OS << "...";
1243 }
1244 OS << ')';
1245 }
1246}
1247
Steve Naroff52a81c02008-09-03 18:15:37 +00001248void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner6c5ec622008-11-24 04:00:27 +00001249 OS << Node->getDecl()->getNameAsString();
Steve Naroff52a81c02008-09-03 18:15:37 +00001250}
Chris Lattner4b009652007-07-25 00:24:17 +00001251//===----------------------------------------------------------------------===//
1252// Stmt method implementations
1253//===----------------------------------------------------------------------===//
1254
Eli Friedman4bdae722009-05-30 05:03:24 +00001255void Stmt::dumpPretty(ASTContext& Context) const {
Chris Lattner7099c782009-06-30 01:26:17 +00001256 printPretty(llvm::errs(), Context, 0,
1257 PrintingPolicy(Context.getLangOptions()));
Chris Lattner4b009652007-07-25 00:24:17 +00001258}
1259
Eli Friedman4bdae722009-05-30 05:03:24 +00001260void Stmt::printPretty(llvm::raw_ostream &OS, ASTContext& Context,
1261 PrinterHelper* Helper,
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +00001262 const PrintingPolicy &Policy,
1263 unsigned Indentation) const {
Chris Lattner4b009652007-07-25 00:24:17 +00001264 if (this == 0) {
1265 OS << "<NULL>";
1266 return;
1267 }
1268
Douglas Gregor996677c2009-05-30 00:08:05 +00001269 if (Policy.Dump) {
Argiris Kirtzidisaec06482009-07-14 03:20:38 +00001270 dump(Context.getSourceManager());
Douglas Gregor996677c2009-05-30 00:08:05 +00001271 return;
1272 }
1273
Eli Friedman4bdae722009-05-30 05:03:24 +00001274 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattner7ba21fa2007-08-21 04:04:25 +00001275 P.Visit(const_cast<Stmt*>(this));
Chris Lattner4b009652007-07-25 00:24:17 +00001276}
Ted Kremenek08176a52007-08-31 21:30:12 +00001277
1278//===----------------------------------------------------------------------===//
1279// PrinterHelper
1280//===----------------------------------------------------------------------===//
1281
1282// Implement virtual destructor.
Gabor Greif61ce98c2007-09-11 15:32:40 +00001283PrinterHelper::~PrinterHelper() {}