blob: fc9fdf975d0a404091a7c24b5dff750afcc7c86e [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner6000dac2007-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.
Reid Spencer5f016e22007-07-11 17:01:13 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/StmtVisitor.h"
Douglas Gregor1a49af92009-01-06 05:10:23 +000016#include "clang/AST/DeclCXX.h"
Ted Kremenek91d1d7a2007-10-17 18:36:42 +000017#include "clang/AST/DeclObjC.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000018#include "clang/AST/PrettyPrinter.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/Support/Compiler.h"
Ted Kremenek51221ec2007-11-26 22:50:46 +000020#include "llvm/Support/Streams.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000021#include "llvm/Support/Format.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// StmtPrinter Visitor
26//===----------------------------------------------------------------------===//
27
28namespace {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000029 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
Ted Kremeneka95d3752008-09-13 05:16:45 +000030 llvm::raw_ostream &OS;
Reid Spencer5f016e22007-07-11 17:01:13 +000031 unsigned IndentLevel;
Ted Kremenek42a509f2007-08-31 21:30:12 +000032 clang::PrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000033 PrintingPolicy Policy;
34
Reid Spencer5f016e22007-07-11 17:01:13 +000035 public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000036 StmtPrinter(llvm::raw_ostream &os, PrinterHelper* helper,
37 const PrintingPolicy &Policy = PrintingPolicy(),
38 unsigned Indentation = 0)
39 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy) {}
Reid Spencer5f016e22007-07-11 17:01:13 +000040
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000041 void PrintStmt(Stmt *S) {
42 PrintStmt(S, Policy.Indentation);
43 }
44
45 void PrintStmt(Stmt *S, int SubIndent) {
Reid Spencer5f016e22007-07-11 17:01:13 +000046 IndentLevel += SubIndent;
47 if (S && isa<Expr>(S)) {
48 // If this is an expr used in a stmt context, indent and newline it.
49 Indent();
Chris Lattnerc5598cb2007-08-21 04:04:25 +000050 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000051 OS << ";\n";
52 } else if (S) {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000053 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000054 } else {
55 Indent() << "<<<NULL STATEMENT>>>\n";
56 }
57 IndentLevel -= SubIndent;
58 }
59
60 void PrintRawCompoundStmt(CompoundStmt *S);
61 void PrintRawDecl(Decl *D);
Ted Kremenekecd64c52008-10-06 18:39:36 +000062 void PrintRawDeclStmt(DeclStmt *S);
Mike Stump071e4da2009-02-10 20:16:46 +000063 void PrintFieldDecl(FieldDecl *FD);
Reid Spencer5f016e22007-07-11 17:01:13 +000064 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl8351da02008-12-22 21:35:02 +000065 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Reid Spencer5f016e22007-07-11 17:01:13 +000066
67 void PrintExpr(Expr *E) {
68 if (E)
Chris Lattnerc5598cb2007-08-21 04:04:25 +000069 Visit(E);
Reid Spencer5f016e22007-07-11 17:01:13 +000070 else
71 OS << "<null expr>";
72 }
73
Mike Stump071e4da2009-02-10 20:16:46 +000074 llvm::raw_ostream &Indent(int Delta = 0) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000075 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
76 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +000077 return OS;
78 }
79
Chris Lattner704fe352007-08-30 17:59:59 +000080 bool PrintOffsetOfDesignator(Expr *E);
81 void VisitUnaryOffsetOf(UnaryOperator *Node);
82
Ted Kremenek42a509f2007-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 Lattnerc5598cb2007-08-21 04:04:25 +000089 void VisitStmt(Stmt *Node);
Douglas Gregorf2cad862008-11-14 12:46:07 +000090#define STMT(CLASS, PARENT) \
Chris Lattnerc5598cb2007-08-21 04:04:25 +000091 void Visit##CLASS(CLASS *Node);
Reid Spencer5f016e22007-07-11 17:01:13 +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) {
116 // FIXME: Need to complete/beautify this... this code simply shows the
117 // nodes are where they need to be.
118 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
119 OS << "typedef " << localType->getUnderlyingType().getAsString();
Chris Lattner39f34e92008-11-24 04:00:27 +0000120 OS << " " << localType->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
122 // Emit storage class for vardecls.
123 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000124 if (V->getStorageClass() != VarDecl::None)
125 OS << VarDecl::getStorageClassSpecifierString(V->getStorageClass())
126 << ' ';
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 }
128
Chris Lattner39f34e92008-11-24 04:00:27 +0000129 std::string Name = VD->getNameAsString();
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000130 VD->getType().getAsStringInternal(Name, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 OS << Name;
132
Chris Lattner24c39902007-07-12 00:36:32 +0000133 // If this is a vardecl with an initializer, emit it.
134 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
135 if (V->getInit()) {
136 OS << " = ";
137 PrintExpr(V->getInit());
138 }
139 }
Steve Naroff91578f32007-11-17 21:21:01 +0000140 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
141 // print a free standing tag decl (e.g. "struct x;").
142 OS << TD->getKindName();
143 OS << " ";
144 if (const IdentifierInfo *II = TD->getIdentifier())
145 OS << II->getName();
Mike Stump071e4da2009-02-10 20:16:46 +0000146 if (RecordDecl *RD = dyn_cast<RecordDecl>(TD)) {
147 OS << "{\n";
148 IndentLevel += 1;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000149 // FIXME: The context passed to field_begin/field_end should
150 // never be NULL!
151 ASTContext *Context = 0;
152 for (RecordDecl::field_iterator i = RD->field_begin(*Context);
153 i != RD->field_end(*Context); ++i) {
Mike Stump071e4da2009-02-10 20:16:46 +0000154 PrintFieldDecl(*i);
155 IndentLevel -= 1;
156 }
157 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 assert(0 && "Unexpected decl");
160 }
161}
162
Mike Stump071e4da2009-02-10 20:16:46 +0000163void StmtPrinter::PrintFieldDecl(FieldDecl *FD) {
164 Indent() << FD->getNameAsString() << "\n";
165}
166
Ted Kremenekecd64c52008-10-06 18:39:36 +0000167void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Mike Stump071e4da2009-02-10 20:16:46 +0000168 bool isFirst = true;
Ted Kremenekecd64c52008-10-06 18:39:36 +0000169
170 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
171 I != E; ++I) {
172
173 if (!isFirst) OS << ", ";
174 else isFirst = false;
175
176 PrintRawDecl(*I);
177 }
178}
Reid Spencer5f016e22007-07-11 17:01:13 +0000179
180void StmtPrinter::VisitNullStmt(NullStmt *Node) {
181 Indent() << ";\n";
182}
183
184void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Ted Kremenekecd64c52008-10-06 18:39:36 +0000185 for (DeclStmt::decl_iterator I = Node->decl_begin(), E = Node->decl_end();
186 I!=E; ++I) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 Indent();
Ted Kremenekecd64c52008-10-06 18:39:36 +0000188 PrintRawDecl(*I);
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 OS << ";\n";
190 }
191}
192
193void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
194 Indent();
195 PrintRawCompoundStmt(Node);
196 OS << "\n";
197}
198
199void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
200 Indent(-1) << "case ";
201 PrintExpr(Node->getLHS());
202 if (Node->getRHS()) {
203 OS << " ... ";
204 PrintExpr(Node->getRHS());
205 }
206 OS << ":\n";
207
208 PrintStmt(Node->getSubStmt(), 0);
209}
210
211void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
212 Indent(-1) << "default:\n";
213 PrintStmt(Node->getSubStmt(), 0);
214}
215
216void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
217 Indent(-1) << Node->getName() << ":\n";
218 PrintStmt(Node->getSubStmt(), 0);
219}
220
221void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000222 OS << "if (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 PrintExpr(If->getCond());
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000224 OS << ')';
Reid Spencer5f016e22007-07-11 17:01:13 +0000225
226 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
227 OS << ' ';
228 PrintRawCompoundStmt(CS);
229 OS << (If->getElse() ? ' ' : '\n');
230 } else {
231 OS << '\n';
232 PrintStmt(If->getThen());
233 if (If->getElse()) Indent();
234 }
235
236 if (Stmt *Else = If->getElse()) {
237 OS << "else";
238
239 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
240 OS << ' ';
241 PrintRawCompoundStmt(CS);
242 OS << '\n';
243 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
244 OS << ' ';
245 PrintRawIfStmt(ElseIf);
246 } else {
247 OS << '\n';
248 PrintStmt(If->getElse());
249 }
250 }
251}
252
253void StmtPrinter::VisitIfStmt(IfStmt *If) {
254 Indent();
255 PrintRawIfStmt(If);
256}
257
258void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
259 Indent() << "switch (";
260 PrintExpr(Node->getCond());
261 OS << ")";
262
263 // Pretty print compoundstmt bodies (very common).
264 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
265 OS << " ";
266 PrintRawCompoundStmt(CS);
267 OS << "\n";
268 } else {
269 OS << "\n";
270 PrintStmt(Node->getBody());
271 }
272}
273
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000274void StmtPrinter::VisitSwitchCase(SwitchCase*) {
275 assert(0 && "SwitchCase is an abstract class");
276}
277
Reid Spencer5f016e22007-07-11 17:01:13 +0000278void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
279 Indent() << "while (";
280 PrintExpr(Node->getCond());
281 OS << ")\n";
282 PrintStmt(Node->getBody());
283}
284
285void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000286 Indent() << "do ";
287 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
288 PrintRawCompoundStmt(CS);
289 OS << " ";
290 } else {
291 OS << "\n";
292 PrintStmt(Node->getBody());
293 Indent();
294 }
295
Eli Friedmanb3e22962009-05-17 01:05:34 +0000296 OS << "while (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 PrintExpr(Node->getCond());
Eli Friedmanb3e22962009-05-17 01:05:34 +0000298 OS << ");\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000299}
300
301void StmtPrinter::VisitForStmt(ForStmt *Node) {
302 Indent() << "for (";
303 if (Node->getInit()) {
304 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000305 PrintRawDeclStmt(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 else
307 PrintExpr(cast<Expr>(Node->getInit()));
308 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000309 OS << ";";
310 if (Node->getCond()) {
311 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000313 }
314 OS << ";";
315 if (Node->getInc()) {
316 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000318 }
319 OS << ") ";
320
321 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
322 PrintRawCompoundStmt(CS);
323 OS << "\n";
324 } else {
325 OS << "\n";
326 PrintStmt(Node->getBody());
327 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000328}
329
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000330void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000331 Indent() << "for (";
332 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000333 PrintRawDeclStmt(DS);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000334 else
335 PrintExpr(cast<Expr>(Node->getElement()));
336 OS << " in ";
337 PrintExpr(Node->getCollection());
338 OS << ") ";
339
340 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
341 PrintRawCompoundStmt(CS);
342 OS << "\n";
343 } else {
344 OS << "\n";
345 PrintStmt(Node->getBody());
346 }
347}
348
Reid Spencer5f016e22007-07-11 17:01:13 +0000349void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
350 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
351}
352
353void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
354 Indent() << "goto *";
355 PrintExpr(Node->getTarget());
356 OS << ";\n";
357}
358
359void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
360 Indent() << "continue;\n";
361}
362
363void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
364 Indent() << "break;\n";
365}
366
367
368void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
369 Indent() << "return";
370 if (Node->getRetValue()) {
371 OS << " ";
372 PrintExpr(Node->getRetValue());
373 }
374 OS << ";\n";
375}
376
Chris Lattnerfe795952007-10-29 04:04:16 +0000377
378void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000379 Indent() << "asm ";
380
381 if (Node->isVolatile())
382 OS << "volatile ";
383
384 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000385 VisitStringLiteral(Node->getAsmString());
Anders Carlssonb235fc22007-11-22 01:36:19 +0000386
387 // Outputs
388 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
389 Node->getNumClobbers() != 0)
390 OS << " : ";
391
392 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
393 if (i != 0)
394 OS << ", ";
395
396 if (!Node->getOutputName(i).empty()) {
397 OS << '[';
398 OS << Node->getOutputName(i);
399 OS << "] ";
400 }
401
Chris Lattnerb3277932009-03-10 04:59:06 +0000402 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000403 OS << " ";
404 Visit(Node->getOutputExpr(i));
405 }
406
407 // Inputs
408 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
409 OS << " : ";
410
411 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
412 if (i != 0)
413 OS << ", ";
414
415 if (!Node->getInputName(i).empty()) {
416 OS << '[';
417 OS << Node->getInputName(i);
418 OS << "] ";
419 }
420
Chris Lattnerb3277932009-03-10 04:59:06 +0000421 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000422 OS << " ";
423 Visit(Node->getInputExpr(i));
424 }
425
426 // Clobbers
427 if (Node->getNumClobbers() != 0)
428 OS << " : ";
429
430 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
431 if (i != 0)
432 OS << ", ";
433
434 VisitStringLiteral(Node->getClobber(i));
435 }
436
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000437 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000438}
439
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000440void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000441 Indent() << "@try";
442 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
443 PrintRawCompoundStmt(TS);
444 OS << "\n";
445 }
446
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000447 for (ObjCAtCatchStmt *catchStmt =
448 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000449 catchStmt;
450 catchStmt =
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000451 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000452 Indent() << "@catch(";
Steve Naroff7ba138a2009-03-03 19:52:17 +0000453 if (catchStmt->getCatchParamDecl()) {
454 if (Decl *DS = catchStmt->getCatchParamDecl())
455 PrintRawDecl(DS);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000456 }
457 OS << ")";
458 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
459 {
460 PrintRawCompoundStmt(CS);
461 OS << "\n";
462 }
463 }
464
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000465 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000466 Node->getFinallyStmt())) {
467 Indent() << "@finally";
468 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000469 OS << "\n";
470 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000471}
472
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000473void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000474}
475
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000476void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000477 Indent() << "@catch (...) { /* todo */ } \n";
478}
479
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000480void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000481 Indent() << "@throw";
482 if (Node->getThrowExpr()) {
483 OS << " ";
484 PrintExpr(Node->getThrowExpr());
485 }
486 OS << ";\n";
487}
488
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000489void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000490 Indent() << "@synchronized (";
491 PrintExpr(Node->getSynchExpr());
492 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000493 PrintRawCompoundStmt(Node->getSynchBody());
494 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000495}
496
Sebastian Redl8351da02008-12-22 21:35:02 +0000497void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
498 OS << "catch (";
Sebastian Redl4b07b292008-12-22 19:15:10 +0000499 if (Decl *ExDecl = Node->getExceptionDecl())
500 PrintRawDecl(ExDecl);
501 else
502 OS << "...";
503 OS << ") ";
504 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl8351da02008-12-22 21:35:02 +0000505}
506
507void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
508 Indent();
509 PrintRawCXXCatchStmt(Node);
510 OS << "\n";
511}
512
513void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
514 Indent() << "try ";
515 PrintRawCompoundStmt(Node->getTryBlock());
516 for(unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
517 OS << " ";
518 PrintRawCXXCatchStmt(Node->getHandler(i));
519 }
Sebastian Redl4b07b292008-12-22 19:15:10 +0000520 OS << "\n";
521}
522
Reid Spencer5f016e22007-07-11 17:01:13 +0000523//===----------------------------------------------------------------------===//
524// Expr printing methods.
525//===----------------------------------------------------------------------===//
526
527void StmtPrinter::VisitExpr(Expr *Node) {
528 OS << "<<unknown expr type>>";
529}
530
531void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +0000532 OS << Node->getDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000533}
534
Douglas Gregorbad35182009-03-19 03:51:16 +0000535void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) {
Douglas Gregor1a49af92009-01-06 05:10:23 +0000536 NamedDecl *D = Node->getDecl();
537
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000538 Node->getQualifier()->print(OS, Policy);
Douglas Gregor1a49af92009-01-06 05:10:23 +0000539 OS << D->getNameAsString();
540}
541
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000542void StmtPrinter::VisitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *Node) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000543 Node->getQualifier()->print(OS, Policy);
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000544 OS << Node->getDeclName().getAsString();
545}
546
Steve Naroff7779db42007-11-12 14:29:37 +0000547void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000548 if (Node->getBase()) {
549 PrintExpr(Node->getBase());
550 OS << (Node->isArrow() ? "->" : ".");
551 }
Chris Lattner39f34e92008-11-24 04:00:27 +0000552 OS << Node->getDecl()->getNameAsString();
Steve Naroff7779db42007-11-12 14:29:37 +0000553}
554
Steve Naroffae784072008-05-30 00:40:33 +0000555void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
556 if (Node->getBase()) {
557 PrintExpr(Node->getBase());
558 OS << ".";
559 }
Steve Naroffc77a6362008-12-04 16:24:46 +0000560 OS << Node->getProperty()->getNameAsCString();
Steve Naroffae784072008-05-30 00:40:33 +0000561}
562
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000563void StmtPrinter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
564 if (Node->getBase()) {
565 PrintExpr(Node->getBase());
566 OS << ".";
567 }
568 // FIXME: Setter/Getter names
569}
570
Chris Lattnerd9f69102008-08-10 01:53:14 +0000571void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000572 switch (Node->getIdentType()) {
573 default:
574 assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000575 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000576 OS << "__func__";
577 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000578 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000579 OS << "__FUNCTION__";
580 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000581 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000582 OS << "__PRETTY_FUNCTION__";
583 break;
584 }
585}
586
Reid Spencer5f016e22007-07-11 17:01:13 +0000587void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000588 unsigned value = Node->getValue();
Chris Lattnerc250aae2008-06-07 22:35:38 +0000589 if (Node->isWide())
590 OS << "L";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000591 switch (value) {
592 case '\\':
593 OS << "'\\\\'";
594 break;
595 case '\'':
596 OS << "'\\''";
597 break;
598 case '\a':
599 // TODO: K&R: the meaning of '\\a' is different in traditional C
600 OS << "'\\a'";
601 break;
602 case '\b':
603 OS << "'\\b'";
604 break;
605 // Nonstandard escape sequence.
606 /*case '\e':
607 OS << "'\\e'";
608 break;*/
609 case '\f':
610 OS << "'\\f'";
611 break;
612 case '\n':
613 OS << "'\\n'";
614 break;
615 case '\r':
616 OS << "'\\r'";
617 break;
618 case '\t':
619 OS << "'\\t'";
620 break;
621 case '\v':
622 OS << "'\\v'";
623 break;
624 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000625 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000626 OS << "'" << (char)value << "'";
627 } else if (value < 256) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000628 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000629 } else {
630 // FIXME what to really do here?
631 OS << value;
632 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000633 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000634}
635
636void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
637 bool isSigned = Node->getType()->isSignedIntegerType();
638 OS << Node->getValue().toString(10, isSigned);
639
640 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000641 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000642 default: assert(0 && "Unexpected type for integer literal!");
643 case BuiltinType::Int: break; // no suffix.
644 case BuiltinType::UInt: OS << 'U'; break;
645 case BuiltinType::Long: OS << 'L'; break;
646 case BuiltinType::ULong: OS << "UL"; break;
647 case BuiltinType::LongLong: OS << "LL"; break;
648 case BuiltinType::ULongLong: OS << "ULL"; break;
649 }
650}
651void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000652 // FIXME: print value more precisely.
Chris Lattnerda8249e2008-06-07 22:13:43 +0000653 OS << Node->getValueAsApproximateDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000654}
Chris Lattner5d661452007-08-26 03:42:43 +0000655
656void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
657 PrintExpr(Node->getSubExpr());
658 OS << "i";
659}
660
Reid Spencer5f016e22007-07-11 17:01:13 +0000661void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
662 if (Str->isWide()) OS << 'L';
663 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000664
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 // FIXME: this doesn't print wstrings right.
666 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner9a81c872009-01-16 19:25:18 +0000667 unsigned char Char = Str->getStrData()[i];
668
669 switch (Char) {
670 default:
671 if (isprint(Char))
672 OS << (char)Char;
673 else // Output anything hard as an octal escape.
674 OS << '\\'
675 << (char)('0'+ ((Char >> 6) & 7))
676 << (char)('0'+ ((Char >> 3) & 7))
677 << (char)('0'+ ((Char >> 0) & 7));
678 break;
679 // Handle some common non-printable cases to make dumps prettier.
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 case '\\': OS << "\\\\"; break;
681 case '"': OS << "\\\""; break;
682 case '\n': OS << "\\n"; break;
683 case '\t': OS << "\\t"; break;
684 case '\a': OS << "\\a"; break;
685 case '\b': OS << "\\b"; break;
686 }
687 }
688 OS << '"';
689}
690void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
691 OS << "(";
692 PrintExpr(Node->getSubExpr());
693 OS << ")";
694}
695void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000696 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner296bf192007-08-23 21:46:40 +0000698
Sebastian Redl05189992008-11-11 17:56:53 +0000699 // Print a space if this is an "identifier operator" like __real.
Chris Lattner296bf192007-08-23 21:46:40 +0000700 switch (Node->getOpcode()) {
701 default: break;
Chris Lattner296bf192007-08-23 21:46:40 +0000702 case UnaryOperator::Real:
703 case UnaryOperator::Imag:
704 case UnaryOperator::Extension:
705 OS << ' ';
706 break;
707 }
708 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 PrintExpr(Node->getSubExpr());
710
711 if (Node->isPostfix())
712 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000713}
Chris Lattner704fe352007-08-30 17:59:59 +0000714
715bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
Eli Friedman35183ac2009-02-27 06:44:11 +0000716 if (isa<UnaryOperator>(E)) {
Chris Lattner704fe352007-08-30 17:59:59 +0000717 // Base case, print the type and comma.
718 OS << E->getType().getAsString() << ", ";
719 return true;
720 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
721 PrintOffsetOfDesignator(ASE->getLHS());
722 OS << "[";
723 PrintExpr(ASE->getRHS());
724 OS << "]";
725 return false;
726 } else {
727 MemberExpr *ME = cast<MemberExpr>(E);
728 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner39f34e92008-11-24 04:00:27 +0000729 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner704fe352007-08-30 17:59:59 +0000730 return false;
731 }
732}
733
734void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
735 OS << "__builtin_offsetof(";
736 PrintOffsetOfDesignator(Node->getSubExpr());
737 OS << ")";
738}
739
Sebastian Redl05189992008-11-11 17:56:53 +0000740void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
741 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
742 if (Node->isArgumentType())
743 OS << "(" << Node->getArgumentType().getAsString() << ")";
744 else {
745 OS << " ";
746 PrintExpr(Node->getArgumentExpr());
747 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000748}
749void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000750 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000752 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 OS << "]";
754}
755
756void StmtPrinter::VisitCallExpr(CallExpr *Call) {
757 PrintExpr(Call->getCallee());
758 OS << "(";
759 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000760 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
761 // Don't print any defaulted arguments
762 break;
763 }
764
Reid Spencer5f016e22007-07-11 17:01:13 +0000765 if (i) OS << ", ";
766 PrintExpr(Call->getArg(i));
767 }
768 OS << ")";
769}
770void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +0000771 // FIXME: Suppress printing implicit bases (like "this")
772 PrintExpr(Node->getBase());
773 OS << (Node->isArrow() ? "->" : ".");
774 // FIXME: Suppress printing references to unnamed objects
775 // representing anonymous unions/structs
Douglas Gregor86f19402008-12-20 23:49:58 +0000776 OS << Node->getMemberDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000777}
Nate Begeman213541a2008-04-18 23:10:10 +0000778void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000779 PrintExpr(Node->getBase());
780 OS << ".";
781 OS << Node->getAccessor().getName();
782}
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000783void StmtPrinter::VisitCastExpr(CastExpr *) {
784 assert(0 && "CastExpr is an abstract class");
785}
Douglas Gregor49badde2008-10-27 19:41:14 +0000786void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
787 assert(0 && "ExplicitCastExpr is an abstract class");
788}
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000789void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000790 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000791 PrintExpr(Node->getSubExpr());
792}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000793void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
794 OS << "(" << Node->getType().getAsString() << ")";
795 PrintExpr(Node->getInitializer());
796}
Steve Naroff49b45262007-07-13 16:58:59 +0000797void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000798 // No need to print anything, simply forward to the sub expression.
799 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000800}
Reid Spencer5f016e22007-07-11 17:01:13 +0000801void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
802 PrintExpr(Node->getLHS());
803 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
804 PrintExpr(Node->getRHS());
805}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000806void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
807 PrintExpr(Node->getLHS());
808 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
809 PrintExpr(Node->getRHS());
810}
Reid Spencer5f016e22007-07-11 17:01:13 +0000811void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
812 PrintExpr(Node->getCond());
Ted Kremenek8e911c42007-11-26 18:27:54 +0000813
814 if (Node->getLHS()) {
815 OS << " ? ";
816 PrintExpr(Node->getLHS());
817 OS << " : ";
818 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000819 else { // Handle GCC extension where LHS can be NULL.
Ted Kremenek8e911c42007-11-26 18:27:54 +0000820 OS << " ?: ";
821 }
822
Reid Spencer5f016e22007-07-11 17:01:13 +0000823 PrintExpr(Node->getRHS());
824}
825
826// GNU extensions.
827
Chris Lattner6481a572007-08-03 17:31:20 +0000828void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000830}
831
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000832void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
833 OS << "(";
834 PrintRawCompoundStmt(E->getSubStmt());
835 OS << ")";
836}
837
Steve Naroffd34e9152007-08-01 22:05:33 +0000838void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
839 OS << "__builtin_types_compatible_p(";
840 OS << Node->getArgType1().getAsString() << ",";
841 OS << Node->getArgType2().getAsString() << ")";
842}
843
Steve Naroffd04fdd52007-08-03 21:21:27 +0000844void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
845 OS << "__builtin_choose_expr(";
846 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000847 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000848 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000849 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000850 PrintExpr(Node->getRHS());
851 OS << ")";
852}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000853
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000854void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
855 OS << "__null";
856}
857
Eli Friedmand38617c2008-05-14 19:38:39 +0000858void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
859 OS << "__builtin_shufflevector(";
860 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
861 if (i) OS << ", ";
862 PrintExpr(Node->getExpr(i));
863 }
864 OS << ")";
865}
866
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000867void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
868 OS << "{ ";
869 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
870 if (i) OS << ", ";
Douglas Gregor4c678342009-01-28 21:54:33 +0000871 if (Node->getInit(i))
872 PrintExpr(Node->getInit(i));
873 else
874 OS << "0";
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000875 }
876 OS << " }";
877}
878
Douglas Gregor05c13a32009-01-22 00:58:24 +0000879void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000880 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
881 DEnd = Node->designators_end();
882 D != DEnd; ++D) {
883 if (D->isFieldDesignator()) {
884 if (D->getDotLoc().isInvalid())
885 OS << D->getFieldName()->getName() << ":";
886 else
887 OS << "." << D->getFieldName()->getName();
888 } else {
889 OS << "[";
890 if (D->isArrayDesignator()) {
891 PrintExpr(Node->getArrayIndex(*D));
892 } else {
893 PrintExpr(Node->getArrayRangeStart(*D));
894 OS << " ... ";
895 PrintExpr(Node->getArrayRangeEnd(*D));
896 }
897 OS << "]";
898 }
899 }
900
901 OS << " = ";
902 PrintExpr(Node->getInit());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000903}
904
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000905void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000906 if (Policy.CPlusPlus)
907 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
908 else {
909 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
910 if (Node->getType()->isRecordType())
911 OS << "{}";
912 else
913 OS << 0;
914 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000915}
916
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000917void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
918 OS << "va_arg(";
919 PrintExpr(Node->getSubExpr());
920 OS << ", ";
921 OS << Node->getType().getAsString();
922 OS << ")";
923}
924
Reid Spencer5f016e22007-07-11 17:01:13 +0000925// C++
Douglas Gregorb4609802008-11-14 16:09:21 +0000926void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
927 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
928 "",
929#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
930 Spelling,
931#include "clang/Basic/OperatorKinds.def"
932 };
933
934 OverloadedOperatorKind Kind = Node->getOperator();
935 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
936 if (Node->getNumArgs() == 1) {
937 OS << OpStrings[Kind] << ' ';
938 PrintExpr(Node->getArg(0));
939 } else {
940 PrintExpr(Node->getArg(0));
941 OS << ' ' << OpStrings[Kind];
942 }
943 } else if (Kind == OO_Call) {
944 PrintExpr(Node->getArg(0));
945 OS << '(';
946 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
947 if (ArgIdx > 1)
948 OS << ", ";
949 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
950 PrintExpr(Node->getArg(ArgIdx));
951 }
952 OS << ')';
953 } else if (Kind == OO_Subscript) {
954 PrintExpr(Node->getArg(0));
955 OS << '[';
956 PrintExpr(Node->getArg(1));
957 OS << ']';
958 } else if (Node->getNumArgs() == 1) {
959 OS << OpStrings[Kind] << ' ';
960 PrintExpr(Node->getArg(0));
961 } else if (Node->getNumArgs() == 2) {
962 PrintExpr(Node->getArg(0));
963 OS << ' ' << OpStrings[Kind] << ' ';
964 PrintExpr(Node->getArg(1));
965 } else {
966 assert(false && "unknown overloaded operator");
967 }
968}
Reid Spencer5f016e22007-07-11 17:01:13 +0000969
Douglas Gregor88a35142008-12-22 05:46:06 +0000970void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
971 VisitCallExpr(cast<CallExpr>(Node));
972}
973
Douglas Gregor49badde2008-10-27 19:41:14 +0000974void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
975 OS << Node->getCastName() << '<';
976 OS << Node->getTypeAsWritten().getAsString() << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000977 PrintExpr(Node->getSubExpr());
978 OS << ")";
979}
980
Douglas Gregor49badde2008-10-27 19:41:14 +0000981void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
982 VisitCXXNamedCastExpr(Node);
983}
984
985void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
986 VisitCXXNamedCastExpr(Node);
987}
988
989void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
990 VisitCXXNamedCastExpr(Node);
991}
992
993void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
994 VisitCXXNamedCastExpr(Node);
995}
996
Sebastian Redlc42e1182008-11-11 11:37:55 +0000997void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
998 OS << "typeid(";
999 if (Node->isTypeOperand()) {
1000 OS << Node->getTypeOperand().getAsString();
1001 } else {
1002 PrintExpr(Node->getExprOperand());
1003 }
1004 OS << ")";
1005}
1006
Reid Spencer5f016e22007-07-11 17:01:13 +00001007void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1008 OS << (Node->getValue() ? "true" : "false");
1009}
1010
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001011void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1012 OS << "nullptr";
1013}
1014
Douglas Gregor796da182008-11-04 14:32:21 +00001015void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1016 OS << "this";
1017}
1018
Chris Lattner50dd2892008-02-26 00:51:44 +00001019void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1020 if (Node->getSubExpr() == 0)
1021 OS << "throw";
1022 else {
1023 OS << "throw ";
1024 PrintExpr(Node->getSubExpr());
1025 }
1026}
1027
Chris Lattner04421082008-04-08 04:40:51 +00001028void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1029 // Nothing to print: we picked up the default argument
1030}
1031
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001032void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1033 OS << Node->getType().getAsString();
1034 OS << "(";
1035 PrintExpr(Node->getSubExpr());
1036 OS << ")";
1037}
1038
Douglas Gregor506ae412009-01-16 18:33:17 +00001039void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1040 OS << Node->getType().getAsString();
1041 OS << "(";
1042 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1043 ArgEnd = Node->arg_end();
1044 Arg != ArgEnd; ++Arg) {
1045 if (Arg != Node->arg_begin())
1046 OS << ", ";
1047 PrintExpr(*Arg);
1048 }
1049 OS << ")";
1050}
1051
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001052void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1053 OS << Node->getType().getAsString() << "()";
1054}
1055
Argyrios Kyrtzidis9e922b12008-09-09 23:47:53 +00001056void
1057StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
1058 PrintRawDecl(E->getVarDecl());
1059}
1060
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001061void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1062 if (E->isGlobalNew())
1063 OS << "::";
1064 OS << "new ";
1065 unsigned NumPlace = E->getNumPlacementArgs();
1066 if (NumPlace > 0) {
1067 OS << "(";
1068 PrintExpr(E->getPlacementArg(0));
1069 for (unsigned i = 1; i < NumPlace; ++i) {
1070 OS << ", ";
1071 PrintExpr(E->getPlacementArg(i));
1072 }
1073 OS << ") ";
1074 }
1075 if (E->isParenTypeId())
1076 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001077 std::string TypeS;
1078 if (Expr *Size = E->getArraySize()) {
1079 llvm::raw_string_ostream s(TypeS);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001080 Size->printPretty(s, Helper, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001081 s.flush();
1082 TypeS = "[" + TypeS + "]";
1083 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001084 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001085 OS << TypeS;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001086 if (E->isParenTypeId())
1087 OS << ")";
1088
1089 if (E->hasInitializer()) {
1090 OS << "(";
1091 unsigned NumCons = E->getNumConstructorArgs();
1092 if (NumCons > 0) {
1093 PrintExpr(E->getConstructorArg(0));
1094 for (unsigned i = 1; i < NumCons; ++i) {
1095 OS << ", ";
1096 PrintExpr(E->getConstructorArg(i));
1097 }
1098 }
1099 OS << ")";
1100 }
1101}
1102
1103void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1104 if (E->isGlobalDelete())
1105 OS << "::";
1106 OS << "delete ";
1107 if (E->isArrayForm())
1108 OS << "[] ";
1109 PrintExpr(E->getArgument());
1110}
1111
Douglas Gregor17330012009-02-04 15:01:18 +00001112void StmtPrinter::VisitUnresolvedFunctionNameExpr(UnresolvedFunctionNameExpr *E) {
1113 OS << E->getName().getAsString();
Douglas Gregor5c37de72008-12-06 00:22:45 +00001114}
1115
Anders Carlssone349bea2009-04-23 02:32:43 +00001116void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1117 // Nothing to print.
1118}
1119
Anders Carlsson2d44e8a2009-05-01 22:18:43 +00001120void StmtPrinter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Anders Carlsson02bbfa32009-04-24 22:47:04 +00001121 // Just forward to the sub expression.
1122 PrintExpr(E->getSubExpr());
1123}
1124
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001125void
1126StmtPrinter::VisitCXXUnresolvedConstructExpr(
1127 CXXUnresolvedConstructExpr *Node) {
1128 OS << Node->getTypeAsWritten().getAsString();
1129 OS << "(";
1130 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
1131 ArgEnd = Node->arg_end();
1132 Arg != ArgEnd; ++Arg) {
1133 if (Arg != Node->arg_begin())
1134 OS << ", ";
1135 PrintExpr(*Arg);
1136 }
1137 OS << ")";
1138}
1139
Douglas Gregor1c0ca592009-05-22 21:13:27 +00001140void StmtPrinter::VisitCXXUnresolvedMemberExpr(CXXUnresolvedMemberExpr *Node) {
1141 PrintExpr(Node->getBase());
1142 OS << (Node->isArrow() ? "->" : ".");
1143 OS << Node->getMember().getAsString();
1144}
1145
Sebastian Redl64b45f72009-01-05 20:52:13 +00001146static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1147 switch (UTT) {
1148 default: assert(false && "Unknown type trait");
1149 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1150 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1151 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1152 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1153 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1154 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1155 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1156 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1157 case UTT_IsAbstract: return "__is_abstract";
1158 case UTT_IsClass: return "__is_class";
1159 case UTT_IsEmpty: return "__is_empty";
1160 case UTT_IsEnum: return "__is_enum";
1161 case UTT_IsPOD: return "__is_pod";
1162 case UTT_IsPolymorphic: return "__is_polymorphic";
1163 case UTT_IsUnion: return "__is_union";
1164 }
1165}
1166
1167void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1168 OS << getTypeTraitName(E->getTrait()) << "("
1169 << E->getQueriedType().getAsString() << ")";
1170}
1171
Anders Carlsson55085182007-08-21 17:43:55 +00001172// Obj-C
1173
1174void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1175 OS << "@";
1176 VisitStringLiteral(Node->getString());
1177}
Reid Spencer5f016e22007-07-11 17:01:13 +00001178
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001179void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001180 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001181}
1182
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001183void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001184 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001185}
1186
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001187void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001188 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001189}
1190
Steve Naroff563477d2007-09-18 23:55:05 +00001191void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1192 OS << "[";
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001193 Expr *receiver = Mess->getReceiver();
1194 if (receiver) PrintExpr(receiver);
1195 else OS << Mess->getClassName()->getName();
Ted Kremenekc29efd82008-05-02 17:32:38 +00001196 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001197 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001198 if (selector.isUnarySelector()) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001199 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001200 } else {
1201 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001202 if (i < selector.getNumArgs()) {
1203 if (i > 0) OS << ' ';
1204 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001205 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001206 else
1207 OS << ":";
1208 }
1209 else OS << ", "; // Handle variadic methods.
1210
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001211 PrintExpr(Mess->getArg(i));
1212 }
Steve Naroff563477d2007-09-18 23:55:05 +00001213 }
1214 OS << "]";
1215}
1216
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001217void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1218 OS << "super";
1219}
1220
Steve Naroff4eb206b2008-09-03 18:15:37 +00001221void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001222 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001223 OS << "^";
1224
1225 const FunctionType *AFT = Node->getFunctionType();
1226
Douglas Gregor72564e72009-02-26 23:50:07 +00001227 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001228 OS << "()";
Douglas Gregor72564e72009-02-26 23:50:07 +00001229 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001230 OS << '(';
1231 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +00001232 for (BlockDecl::param_iterator AI = BD->param_begin(),
1233 E = BD->param_end(); AI != E; ++AI) {
1234 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner39f34e92008-11-24 04:00:27 +00001235 ParamStr = (*AI)->getNameAsString();
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001236 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001237 OS << ParamStr;
1238 }
1239
Douglas Gregor72564e72009-02-26 23:50:07 +00001240 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001241 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001242 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001243 OS << "...";
1244 }
1245 OS << ')';
1246 }
1247}
1248
Steve Naroff4eb206b2008-09-03 18:15:37 +00001249void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001250 OS << Node->getDecl()->getNameAsString();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001251}
Reid Spencer5f016e22007-07-11 17:01:13 +00001252//===----------------------------------------------------------------------===//
1253// Stmt method implementations
1254//===----------------------------------------------------------------------===//
1255
Chris Lattner6000dac2007-08-08 22:51:59 +00001256void Stmt::dumpPretty() const {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001257 printPretty(llvm::errs(), 0, PrintingPolicy());
Reid Spencer5f016e22007-07-11 17:01:13 +00001258}
1259
Mike Stump071e4da2009-02-10 20:16:46 +00001260void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001261 const PrintingPolicy &Policy,
1262 unsigned Indentation) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 if (this == 0) {
1264 OS << "<NULL>";
1265 return;
1266 }
1267
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001268 if (Policy.Dump) {
1269 dump();
1270 return;
1271 }
1272
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001273 StmtPrinter P(OS, Helper, Policy, Indentation);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00001274 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00001275}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001276
1277//===----------------------------------------------------------------------===//
1278// PrinterHelper
1279//===----------------------------------------------------------------------===//
1280
1281// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00001282PrinterHelper::~PrinterHelper() {}