blob: 89a185d965d566fe314cafe5907bf1c53349c3a9 [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"
Ted Kremenek91d1d7a2007-10-17 18:36:42 +000016#include "clang/AST/DeclObjC.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000017#include "clang/AST/PrettyPrinter.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "llvm/Support/Compiler.h"
Ted Kremenek51221ec2007-11-26 22:50:46 +000019#include "llvm/Support/Streams.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000020#include "llvm/Support/Format.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// StmtPrinter Visitor
25//===----------------------------------------------------------------------===//
26
27namespace {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000028 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
Ted Kremeneka95d3752008-09-13 05:16:45 +000029 llvm::raw_ostream &OS;
Reid Spencer5f016e22007-07-11 17:01:13 +000030 unsigned IndentLevel;
Ted Kremenek42a509f2007-08-31 21:30:12 +000031 clang::PrinterHelper* Helper;
Reid Spencer5f016e22007-07-11 17:01:13 +000032 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +000033 StmtPrinter(llvm::raw_ostream &os, PrinterHelper* helper) :
Ted Kremenek42a509f2007-08-31 21:30:12 +000034 OS(os), IndentLevel(0), Helper(helper) {}
Reid Spencer5f016e22007-07-11 17:01:13 +000035
36 void PrintStmt(Stmt *S, int SubIndent = 1) {
37 IndentLevel += SubIndent;
38 if (S && isa<Expr>(S)) {
39 // If this is an expr used in a stmt context, indent and newline it.
40 Indent();
Chris Lattnerc5598cb2007-08-21 04:04:25 +000041 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000042 OS << ";\n";
43 } else if (S) {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000044 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000045 } else {
46 Indent() << "<<<NULL STATEMENT>>>\n";
47 }
48 IndentLevel -= SubIndent;
49 }
50
51 void PrintRawCompoundStmt(CompoundStmt *S);
52 void PrintRawDecl(Decl *D);
Ted Kremenekecd64c52008-10-06 18:39:36 +000053 void PrintRawDeclStmt(DeclStmt *S);
Reid Spencer5f016e22007-07-11 17:01:13 +000054 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl8351da02008-12-22 21:35:02 +000055 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Reid Spencer5f016e22007-07-11 17:01:13 +000056
57 void PrintExpr(Expr *E) {
58 if (E)
Chris Lattnerc5598cb2007-08-21 04:04:25 +000059 Visit(E);
Reid Spencer5f016e22007-07-11 17:01:13 +000060 else
61 OS << "<null expr>";
62 }
63
Ted Kremeneka95d3752008-09-13 05:16:45 +000064 llvm::raw_ostream &Indent(int Delta = 0) const {
Reid Spencer5f016e22007-07-11 17:01:13 +000065 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
66 OS << " ";
67 return OS;
68 }
69
Chris Lattner704fe352007-08-30 17:59:59 +000070 bool PrintOffsetOfDesignator(Expr *E);
71 void VisitUnaryOffsetOf(UnaryOperator *Node);
72
Ted Kremenek42a509f2007-08-31 21:30:12 +000073 void Visit(Stmt* S) {
74 if (Helper && Helper->handledStmt(S,OS))
75 return;
76 else StmtVisitor<StmtPrinter>::Visit(S);
77 }
78
Chris Lattnerc5598cb2007-08-21 04:04:25 +000079 void VisitStmt(Stmt *Node);
Douglas Gregorf2cad862008-11-14 12:46:07 +000080#define STMT(CLASS, PARENT) \
Chris Lattnerc5598cb2007-08-21 04:04:25 +000081 void Visit##CLASS(CLASS *Node);
Reid Spencer5f016e22007-07-11 17:01:13 +000082#include "clang/AST/StmtNodes.def"
83 };
84}
85
86//===----------------------------------------------------------------------===//
87// Stmt printing methods.
88//===----------------------------------------------------------------------===//
89
90void StmtPrinter::VisitStmt(Stmt *Node) {
91 Indent() << "<<unknown stmt type>>\n";
92}
93
94/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
95/// with no newline after the }.
96void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
97 OS << "{\n";
98 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
99 I != E; ++I)
100 PrintStmt(*I);
101
102 Indent() << "}";
103}
104
105void StmtPrinter::PrintRawDecl(Decl *D) {
106 // FIXME: Need to complete/beautify this... this code simply shows the
107 // nodes are where they need to be.
108 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
109 OS << "typedef " << localType->getUnderlyingType().getAsString();
Chris Lattner39f34e92008-11-24 04:00:27 +0000110 OS << " " << localType->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
112 // Emit storage class for vardecls.
113 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
114 switch (V->getStorageClass()) {
115 default: assert(0 && "Unknown storage class!");
116 case VarDecl::None: break;
117 case VarDecl::Extern: OS << "extern "; break;
118 case VarDecl::Static: OS << "static "; break;
119 case VarDecl::Auto: OS << "auto "; break;
120 case VarDecl::Register: OS << "register "; break;
121 }
122 }
123
Chris Lattner39f34e92008-11-24 04:00:27 +0000124 std::string Name = VD->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 VD->getType().getAsStringInternal(Name);
126 OS << Name;
127
Chris Lattner24c39902007-07-12 00:36:32 +0000128 // If this is a vardecl with an initializer, emit it.
129 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
130 if (V->getInit()) {
131 OS << " = ";
132 PrintExpr(V->getInit());
133 }
134 }
Steve Naroff91578f32007-11-17 21:21:01 +0000135 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
136 // print a free standing tag decl (e.g. "struct x;").
137 OS << TD->getKindName();
138 OS << " ";
139 if (const IdentifierInfo *II = TD->getIdentifier())
140 OS << II->getName();
141 else
142 OS << "<anonymous>";
143 // FIXME: print tag bodies.
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 assert(0 && "Unexpected decl");
146 }
147}
148
Ted Kremenekecd64c52008-10-06 18:39:36 +0000149void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
150 bool isFirst = false;
151
152 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
153 I != E; ++I) {
154
155 if (!isFirst) OS << ", ";
156 else isFirst = false;
157
158 PrintRawDecl(*I);
159 }
160}
Reid Spencer5f016e22007-07-11 17:01:13 +0000161
162void StmtPrinter::VisitNullStmt(NullStmt *Node) {
163 Indent() << ";\n";
164}
165
166void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Ted Kremenekecd64c52008-10-06 18:39:36 +0000167 for (DeclStmt::decl_iterator I = Node->decl_begin(), E = Node->decl_end();
168 I!=E; ++I) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 Indent();
Ted Kremenekecd64c52008-10-06 18:39:36 +0000170 PrintRawDecl(*I);
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 OS << ";\n";
172 }
173}
174
175void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
176 Indent();
177 PrintRawCompoundStmt(Node);
178 OS << "\n";
179}
180
181void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
182 Indent(-1) << "case ";
183 PrintExpr(Node->getLHS());
184 if (Node->getRHS()) {
185 OS << " ... ";
186 PrintExpr(Node->getRHS());
187 }
188 OS << ":\n";
189
190 PrintStmt(Node->getSubStmt(), 0);
191}
192
193void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
194 Indent(-1) << "default:\n";
195 PrintStmt(Node->getSubStmt(), 0);
196}
197
198void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
199 Indent(-1) << Node->getName() << ":\n";
200 PrintStmt(Node->getSubStmt(), 0);
201}
202
203void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
204 OS << "if ";
205 PrintExpr(If->getCond());
206
207 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
208 OS << ' ';
209 PrintRawCompoundStmt(CS);
210 OS << (If->getElse() ? ' ' : '\n');
211 } else {
212 OS << '\n';
213 PrintStmt(If->getThen());
214 if (If->getElse()) Indent();
215 }
216
217 if (Stmt *Else = If->getElse()) {
218 OS << "else";
219
220 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
221 OS << ' ';
222 PrintRawCompoundStmt(CS);
223 OS << '\n';
224 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
225 OS << ' ';
226 PrintRawIfStmt(ElseIf);
227 } else {
228 OS << '\n';
229 PrintStmt(If->getElse());
230 }
231 }
232}
233
234void StmtPrinter::VisitIfStmt(IfStmt *If) {
235 Indent();
236 PrintRawIfStmt(If);
237}
238
239void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
240 Indent() << "switch (";
241 PrintExpr(Node->getCond());
242 OS << ")";
243
244 // Pretty print compoundstmt bodies (very common).
245 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
246 OS << " ";
247 PrintRawCompoundStmt(CS);
248 OS << "\n";
249 } else {
250 OS << "\n";
251 PrintStmt(Node->getBody());
252 }
253}
254
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000255void StmtPrinter::VisitSwitchCase(SwitchCase*) {
256 assert(0 && "SwitchCase is an abstract class");
257}
258
Reid Spencer5f016e22007-07-11 17:01:13 +0000259void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
260 Indent() << "while (";
261 PrintExpr(Node->getCond());
262 OS << ")\n";
263 PrintStmt(Node->getBody());
264}
265
266void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000267 Indent() << "do ";
268 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
269 PrintRawCompoundStmt(CS);
270 OS << " ";
271 } else {
272 OS << "\n";
273 PrintStmt(Node->getBody());
274 Indent();
275 }
276
277 OS << "while ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 PrintExpr(Node->getCond());
279 OS << ";\n";
280}
281
282void StmtPrinter::VisitForStmt(ForStmt *Node) {
283 Indent() << "for (";
284 if (Node->getInit()) {
285 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000286 PrintRawDeclStmt(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 else
288 PrintExpr(cast<Expr>(Node->getInit()));
289 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000290 OS << ";";
291 if (Node->getCond()) {
292 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000294 }
295 OS << ";";
296 if (Node->getInc()) {
297 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000299 }
300 OS << ") ";
301
302 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
303 PrintRawCompoundStmt(CS);
304 OS << "\n";
305 } else {
306 OS << "\n";
307 PrintStmt(Node->getBody());
308 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000309}
310
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000311void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000312 Indent() << "for (";
313 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000314 PrintRawDeclStmt(DS);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000315 else
316 PrintExpr(cast<Expr>(Node->getElement()));
317 OS << " in ";
318 PrintExpr(Node->getCollection());
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 }
328}
329
Reid Spencer5f016e22007-07-11 17:01:13 +0000330void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
331 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
332}
333
334void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
335 Indent() << "goto *";
336 PrintExpr(Node->getTarget());
337 OS << ";\n";
338}
339
340void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
341 Indent() << "continue;\n";
342}
343
344void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
345 Indent() << "break;\n";
346}
347
348
349void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
350 Indent() << "return";
351 if (Node->getRetValue()) {
352 OS << " ";
353 PrintExpr(Node->getRetValue());
354 }
355 OS << ";\n";
356}
357
Chris Lattnerfe795952007-10-29 04:04:16 +0000358
359void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000360 Indent() << "asm ";
361
362 if (Node->isVolatile())
363 OS << "volatile ";
364
365 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000366 VisitStringLiteral(Node->getAsmString());
Anders Carlssonb235fc22007-11-22 01:36:19 +0000367
368 // Outputs
369 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
370 Node->getNumClobbers() != 0)
371 OS << " : ";
372
373 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
374 if (i != 0)
375 OS << ", ";
376
377 if (!Node->getOutputName(i).empty()) {
378 OS << '[';
379 OS << Node->getOutputName(i);
380 OS << "] ";
381 }
382
383 VisitStringLiteral(Node->getOutputConstraint(i));
384 OS << " ";
385 Visit(Node->getOutputExpr(i));
386 }
387
388 // Inputs
389 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
390 OS << " : ";
391
392 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
393 if (i != 0)
394 OS << ", ";
395
396 if (!Node->getInputName(i).empty()) {
397 OS << '[';
398 OS << Node->getInputName(i);
399 OS << "] ";
400 }
401
402 VisitStringLiteral(Node->getInputConstraint(i));
403 OS << " ";
404 Visit(Node->getInputExpr(i));
405 }
406
407 // Clobbers
408 if (Node->getNumClobbers() != 0)
409 OS << " : ";
410
411 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
412 if (i != 0)
413 OS << ", ";
414
415 VisitStringLiteral(Node->getClobber(i));
416 }
417
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000418 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000419}
420
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000421void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000422 Indent() << "@try";
423 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
424 PrintRawCompoundStmt(TS);
425 OS << "\n";
426 }
427
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000428 for (ObjCAtCatchStmt *catchStmt =
429 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000430 catchStmt;
431 catchStmt =
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000432 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000433 Indent() << "@catch(";
434 if (catchStmt->getCatchParamStmt()) {
435 if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000436 PrintRawDeclStmt(DS);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000437 }
438 OS << ")";
439 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
440 {
441 PrintRawCompoundStmt(CS);
442 OS << "\n";
443 }
444 }
445
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000446 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000447 Node->getFinallyStmt())) {
448 Indent() << "@finally";
449 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000450 OS << "\n";
451 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000452}
453
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000454void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000455}
456
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000457void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000458 Indent() << "@catch (...) { /* todo */ } \n";
459}
460
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000461void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000462 Indent() << "@throw";
463 if (Node->getThrowExpr()) {
464 OS << " ";
465 PrintExpr(Node->getThrowExpr());
466 }
467 OS << ";\n";
468}
469
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000470void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000471 Indent() << "@synchronized (";
472 PrintExpr(Node->getSynchExpr());
473 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000474 PrintRawCompoundStmt(Node->getSynchBody());
475 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000476}
477
Sebastian Redl8351da02008-12-22 21:35:02 +0000478void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
479 OS << "catch (";
Sebastian Redl4b07b292008-12-22 19:15:10 +0000480 if (Decl *ExDecl = Node->getExceptionDecl())
481 PrintRawDecl(ExDecl);
482 else
483 OS << "...";
484 OS << ") ";
485 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl8351da02008-12-22 21:35:02 +0000486}
487
488void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
489 Indent();
490 PrintRawCXXCatchStmt(Node);
491 OS << "\n";
492}
493
494void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
495 Indent() << "try ";
496 PrintRawCompoundStmt(Node->getTryBlock());
497 for(unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
498 OS << " ";
499 PrintRawCXXCatchStmt(Node->getHandler(i));
500 }
Sebastian Redl4b07b292008-12-22 19:15:10 +0000501 OS << "\n";
502}
503
Reid Spencer5f016e22007-07-11 17:01:13 +0000504//===----------------------------------------------------------------------===//
505// Expr printing methods.
506//===----------------------------------------------------------------------===//
507
508void StmtPrinter::VisitExpr(Expr *Node) {
509 OS << "<<unknown expr type>>";
510}
511
512void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +0000513 OS << Node->getDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000514}
515
Steve Naroff7779db42007-11-12 14:29:37 +0000516void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000517 if (Node->getBase()) {
518 PrintExpr(Node->getBase());
519 OS << (Node->isArrow() ? "->" : ".");
520 }
Chris Lattner39f34e92008-11-24 04:00:27 +0000521 OS << Node->getDecl()->getNameAsString();
Steve Naroff7779db42007-11-12 14:29:37 +0000522}
523
Steve Naroffae784072008-05-30 00:40:33 +0000524void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
525 if (Node->getBase()) {
526 PrintExpr(Node->getBase());
527 OS << ".";
528 }
Steve Naroffc77a6362008-12-04 16:24:46 +0000529 OS << Node->getProperty()->getNameAsCString();
Steve Naroffae784072008-05-30 00:40:33 +0000530}
531
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000532void StmtPrinter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
533 if (Node->getBase()) {
534 PrintExpr(Node->getBase());
535 OS << ".";
536 }
537 // FIXME: Setter/Getter names
538}
539
Chris Lattnerd9f69102008-08-10 01:53:14 +0000540void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000541 switch (Node->getIdentType()) {
542 default:
543 assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000544 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000545 OS << "__func__";
546 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000547 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000548 OS << "__FUNCTION__";
549 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000550 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000551 OS << "__PRETTY_FUNCTION__";
552 break;
553 }
554}
555
Reid Spencer5f016e22007-07-11 17:01:13 +0000556void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000557 unsigned value = Node->getValue();
Chris Lattnerc250aae2008-06-07 22:35:38 +0000558 if (Node->isWide())
559 OS << "L";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000560 switch (value) {
561 case '\\':
562 OS << "'\\\\'";
563 break;
564 case '\'':
565 OS << "'\\''";
566 break;
567 case '\a':
568 // TODO: K&R: the meaning of '\\a' is different in traditional C
569 OS << "'\\a'";
570 break;
571 case '\b':
572 OS << "'\\b'";
573 break;
574 // Nonstandard escape sequence.
575 /*case '\e':
576 OS << "'\\e'";
577 break;*/
578 case '\f':
579 OS << "'\\f'";
580 break;
581 case '\n':
582 OS << "'\\n'";
583 break;
584 case '\r':
585 OS << "'\\r'";
586 break;
587 case '\t':
588 OS << "'\\t'";
589 break;
590 case '\v':
591 OS << "'\\v'";
592 break;
593 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000594 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000595 OS << "'" << (char)value << "'";
596 } else if (value < 256) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000597 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000598 } else {
599 // FIXME what to really do here?
600 OS << value;
601 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000602 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000603}
604
605void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
606 bool isSigned = Node->getType()->isSignedIntegerType();
607 OS << Node->getValue().toString(10, isSigned);
608
609 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000610 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 default: assert(0 && "Unexpected type for integer literal!");
612 case BuiltinType::Int: break; // no suffix.
613 case BuiltinType::UInt: OS << 'U'; break;
614 case BuiltinType::Long: OS << 'L'; break;
615 case BuiltinType::ULong: OS << "UL"; break;
616 case BuiltinType::LongLong: OS << "LL"; break;
617 case BuiltinType::ULongLong: OS << "ULL"; break;
618 }
619}
620void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000621 // FIXME: print value more precisely.
Chris Lattnerda8249e2008-06-07 22:13:43 +0000622 OS << Node->getValueAsApproximateDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000623}
Chris Lattner5d661452007-08-26 03:42:43 +0000624
625void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
626 PrintExpr(Node->getSubExpr());
627 OS << "i";
628}
629
Reid Spencer5f016e22007-07-11 17:01:13 +0000630void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
631 if (Str->isWide()) OS << 'L';
632 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000633
Reid Spencer5f016e22007-07-11 17:01:13 +0000634 // FIXME: this doesn't print wstrings right.
635 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
636 switch (Str->getStrData()[i]) {
637 default: OS << Str->getStrData()[i]; break;
638 // Handle some common ones to make dumps prettier.
639 case '\\': OS << "\\\\"; break;
640 case '"': OS << "\\\""; break;
641 case '\n': OS << "\\n"; break;
642 case '\t': OS << "\\t"; break;
643 case '\a': OS << "\\a"; break;
644 case '\b': OS << "\\b"; break;
645 }
646 }
647 OS << '"';
648}
649void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
650 OS << "(";
651 PrintExpr(Node->getSubExpr());
652 OS << ")";
653}
654void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000655 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner296bf192007-08-23 21:46:40 +0000657
Sebastian Redl05189992008-11-11 17:56:53 +0000658 // Print a space if this is an "identifier operator" like __real.
Chris Lattner296bf192007-08-23 21:46:40 +0000659 switch (Node->getOpcode()) {
660 default: break;
Chris Lattner296bf192007-08-23 21:46:40 +0000661 case UnaryOperator::Real:
662 case UnaryOperator::Imag:
663 case UnaryOperator::Extension:
664 OS << ' ';
665 break;
666 }
667 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 PrintExpr(Node->getSubExpr());
669
670 if (Node->isPostfix())
671 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000672}
Chris Lattner704fe352007-08-30 17:59:59 +0000673
674bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
675 if (isa<CompoundLiteralExpr>(E)) {
676 // Base case, print the type and comma.
677 OS << E->getType().getAsString() << ", ";
678 return true;
679 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
680 PrintOffsetOfDesignator(ASE->getLHS());
681 OS << "[";
682 PrintExpr(ASE->getRHS());
683 OS << "]";
684 return false;
685 } else {
686 MemberExpr *ME = cast<MemberExpr>(E);
687 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner39f34e92008-11-24 04:00:27 +0000688 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner704fe352007-08-30 17:59:59 +0000689 return false;
690 }
691}
692
693void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
694 OS << "__builtin_offsetof(";
695 PrintOffsetOfDesignator(Node->getSubExpr());
696 OS << ")";
697}
698
Sebastian Redl05189992008-11-11 17:56:53 +0000699void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
700 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
701 if (Node->isArgumentType())
702 OS << "(" << Node->getArgumentType().getAsString() << ")";
703 else {
704 OS << " ";
705 PrintExpr(Node->getArgumentExpr());
706 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000707}
708void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000709 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000710 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000711 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000712 OS << "]";
713}
714
715void StmtPrinter::VisitCallExpr(CallExpr *Call) {
716 PrintExpr(Call->getCallee());
717 OS << "(";
718 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000719 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
720 // Don't print any defaulted arguments
721 break;
722 }
723
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 if (i) OS << ", ";
725 PrintExpr(Call->getArg(i));
726 }
727 OS << ")";
728}
729void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
730 PrintExpr(Node->getBase());
731 OS << (Node->isArrow() ? "->" : ".");
Douglas Gregor86f19402008-12-20 23:49:58 +0000732 OS << Node->getMemberDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000733}
Nate Begeman213541a2008-04-18 23:10:10 +0000734void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000735 PrintExpr(Node->getBase());
736 OS << ".";
737 OS << Node->getAccessor().getName();
738}
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000739void StmtPrinter::VisitCastExpr(CastExpr *) {
740 assert(0 && "CastExpr is an abstract class");
741}
Douglas Gregor49badde2008-10-27 19:41:14 +0000742void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
743 assert(0 && "ExplicitCastExpr is an abstract class");
744}
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000745void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000746 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000747 PrintExpr(Node->getSubExpr());
748}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000749void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
750 OS << "(" << Node->getType().getAsString() << ")";
751 PrintExpr(Node->getInitializer());
752}
Steve Naroff49b45262007-07-13 16:58:59 +0000753void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000754 // No need to print anything, simply forward to the sub expression.
755 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000756}
Reid Spencer5f016e22007-07-11 17:01:13 +0000757void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
758 PrintExpr(Node->getLHS());
759 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
760 PrintExpr(Node->getRHS());
761}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000762void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
763 PrintExpr(Node->getLHS());
764 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
765 PrintExpr(Node->getRHS());
766}
Reid Spencer5f016e22007-07-11 17:01:13 +0000767void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
768 PrintExpr(Node->getCond());
Ted Kremenek8e911c42007-11-26 18:27:54 +0000769
770 if (Node->getLHS()) {
771 OS << " ? ";
772 PrintExpr(Node->getLHS());
773 OS << " : ";
774 }
775 else { // Handle GCC extention where LHS can be NULL.
776 OS << " ?: ";
777 }
778
Reid Spencer5f016e22007-07-11 17:01:13 +0000779 PrintExpr(Node->getRHS());
780}
781
782// GNU extensions.
783
Chris Lattner6481a572007-08-03 17:31:20 +0000784void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000785 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000786}
787
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000788void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
789 OS << "(";
790 PrintRawCompoundStmt(E->getSubStmt());
791 OS << ")";
792}
793
Steve Naroffd34e9152007-08-01 22:05:33 +0000794void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
795 OS << "__builtin_types_compatible_p(";
796 OS << Node->getArgType1().getAsString() << ",";
797 OS << Node->getArgType2().getAsString() << ")";
798}
799
Steve Naroffd04fdd52007-08-03 21:21:27 +0000800void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
801 OS << "__builtin_choose_expr(";
802 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000803 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000804 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000805 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000806 PrintExpr(Node->getRHS());
807 OS << ")";
808}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000809
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000810void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
811 OS << "__null";
812}
813
Nate Begemane2ce1d92008-01-17 17:46:27 +0000814void StmtPrinter::VisitOverloadExpr(OverloadExpr *Node) {
815 OS << "__builtin_overload(";
Nate Begeman67295d02008-01-30 20:50:20 +0000816 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
Nate Begemane2ce1d92008-01-17 17:46:27 +0000817 if (i) OS << ", ";
Nate Begeman67295d02008-01-30 20:50:20 +0000818 PrintExpr(Node->getExpr(i));
Nate Begemane2ce1d92008-01-17 17:46:27 +0000819 }
820 OS << ")";
821}
822
Eli Friedmand38617c2008-05-14 19:38:39 +0000823void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
824 OS << "__builtin_shufflevector(";
825 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
826 if (i) OS << ", ";
827 PrintExpr(Node->getExpr(i));
828 }
829 OS << ")";
830}
831
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000832void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
833 OS << "{ ";
834 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
835 if (i) OS << ", ";
836 PrintExpr(Node->getInit(i));
837 }
838 OS << " }";
839}
840
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000841void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
842 OS << "va_arg(";
843 PrintExpr(Node->getSubExpr());
844 OS << ", ";
845 OS << Node->getType().getAsString();
846 OS << ")";
847}
848
Reid Spencer5f016e22007-07-11 17:01:13 +0000849// C++
Douglas Gregorb4609802008-11-14 16:09:21 +0000850void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
851 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
852 "",
853#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
854 Spelling,
855#include "clang/Basic/OperatorKinds.def"
856 };
857
858 OverloadedOperatorKind Kind = Node->getOperator();
859 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
860 if (Node->getNumArgs() == 1) {
861 OS << OpStrings[Kind] << ' ';
862 PrintExpr(Node->getArg(0));
863 } else {
864 PrintExpr(Node->getArg(0));
865 OS << ' ' << OpStrings[Kind];
866 }
867 } else if (Kind == OO_Call) {
868 PrintExpr(Node->getArg(0));
869 OS << '(';
870 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
871 if (ArgIdx > 1)
872 OS << ", ";
873 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
874 PrintExpr(Node->getArg(ArgIdx));
875 }
876 OS << ')';
877 } else if (Kind == OO_Subscript) {
878 PrintExpr(Node->getArg(0));
879 OS << '[';
880 PrintExpr(Node->getArg(1));
881 OS << ']';
882 } else if (Node->getNumArgs() == 1) {
883 OS << OpStrings[Kind] << ' ';
884 PrintExpr(Node->getArg(0));
885 } else if (Node->getNumArgs() == 2) {
886 PrintExpr(Node->getArg(0));
887 OS << ' ' << OpStrings[Kind] << ' ';
888 PrintExpr(Node->getArg(1));
889 } else {
890 assert(false && "unknown overloaded operator");
891 }
892}
Reid Spencer5f016e22007-07-11 17:01:13 +0000893
Douglas Gregor88a35142008-12-22 05:46:06 +0000894void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
895 VisitCallExpr(cast<CallExpr>(Node));
896}
897
Douglas Gregor49badde2008-10-27 19:41:14 +0000898void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
899 OS << Node->getCastName() << '<';
900 OS << Node->getTypeAsWritten().getAsString() << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000901 PrintExpr(Node->getSubExpr());
902 OS << ")";
903}
904
Douglas Gregor49badde2008-10-27 19:41:14 +0000905void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
906 VisitCXXNamedCastExpr(Node);
907}
908
909void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
910 VisitCXXNamedCastExpr(Node);
911}
912
913void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
914 VisitCXXNamedCastExpr(Node);
915}
916
917void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
918 VisitCXXNamedCastExpr(Node);
919}
920
Sebastian Redlc42e1182008-11-11 11:37:55 +0000921void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
922 OS << "typeid(";
923 if (Node->isTypeOperand()) {
924 OS << Node->getTypeOperand().getAsString();
925 } else {
926 PrintExpr(Node->getExprOperand());
927 }
928 OS << ")";
929}
930
Reid Spencer5f016e22007-07-11 17:01:13 +0000931void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
932 OS << (Node->getValue() ? "true" : "false");
933}
934
Douglas Gregor796da182008-11-04 14:32:21 +0000935void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
936 OS << "this";
937}
938
Chris Lattner50dd2892008-02-26 00:51:44 +0000939void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
940 if (Node->getSubExpr() == 0)
941 OS << "throw";
942 else {
943 OS << "throw ";
944 PrintExpr(Node->getSubExpr());
945 }
946}
947
Chris Lattner04421082008-04-08 04:40:51 +0000948void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
949 // Nothing to print: we picked up the default argument
950}
951
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000952void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
953 OS << Node->getType().getAsString();
954 OS << "(";
955 PrintExpr(Node->getSubExpr());
956 OS << ")";
957}
958
959void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
960 OS << Node->getType().getAsString() << "()";
961}
962
Argyrios Kyrtzidis9e922b12008-09-09 23:47:53 +0000963void
964StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
965 PrintRawDecl(E->getVarDecl());
966}
967
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000968void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
969 if (E->isGlobalNew())
970 OS << "::";
971 OS << "new ";
972 unsigned NumPlace = E->getNumPlacementArgs();
973 if (NumPlace > 0) {
974 OS << "(";
975 PrintExpr(E->getPlacementArg(0));
976 for (unsigned i = 1; i < NumPlace; ++i) {
977 OS << ", ";
978 PrintExpr(E->getPlacementArg(i));
979 }
980 OS << ") ";
981 }
982 if (E->isParenTypeId())
983 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +0000984 std::string TypeS;
985 if (Expr *Size = E->getArraySize()) {
986 llvm::raw_string_ostream s(TypeS);
987 Size->printPretty(s);
988 s.flush();
989 TypeS = "[" + TypeS + "]";
990 }
991 E->getAllocatedType().getAsStringInternal(TypeS);
992 OS << TypeS;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000993 if (E->isParenTypeId())
994 OS << ")";
995
996 if (E->hasInitializer()) {
997 OS << "(";
998 unsigned NumCons = E->getNumConstructorArgs();
999 if (NumCons > 0) {
1000 PrintExpr(E->getConstructorArg(0));
1001 for (unsigned i = 1; i < NumCons; ++i) {
1002 OS << ", ";
1003 PrintExpr(E->getConstructorArg(i));
1004 }
1005 }
1006 OS << ")";
1007 }
1008}
1009
1010void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1011 if (E->isGlobalDelete())
1012 OS << "::";
1013 OS << "delete ";
1014 if (E->isArrayForm())
1015 OS << "[] ";
1016 PrintExpr(E->getArgument());
1017}
1018
Douglas Gregor5c37de72008-12-06 00:22:45 +00001019void StmtPrinter::VisitCXXDependentNameExpr(CXXDependentNameExpr *E) {
1020 OS << E->getName()->getName();
1021}
1022
Sebastian Redl64b45f72009-01-05 20:52:13 +00001023static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1024 switch (UTT) {
1025 default: assert(false && "Unknown type trait");
1026 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1027 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1028 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1029 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1030 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1031 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1032 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1033 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1034 case UTT_IsAbstract: return "__is_abstract";
1035 case UTT_IsClass: return "__is_class";
1036 case UTT_IsEmpty: return "__is_empty";
1037 case UTT_IsEnum: return "__is_enum";
1038 case UTT_IsPOD: return "__is_pod";
1039 case UTT_IsPolymorphic: return "__is_polymorphic";
1040 case UTT_IsUnion: return "__is_union";
1041 }
1042}
1043
1044void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1045 OS << getTypeTraitName(E->getTrait()) << "("
1046 << E->getQueriedType().getAsString() << ")";
1047}
1048
Anders Carlsson55085182007-08-21 17:43:55 +00001049// Obj-C
1050
1051void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1052 OS << "@";
1053 VisitStringLiteral(Node->getString());
1054}
Reid Spencer5f016e22007-07-11 17:01:13 +00001055
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001056void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001057 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001058}
1059
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001060void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001061 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001062}
1063
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001064void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001065 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001066}
1067
Steve Naroff563477d2007-09-18 23:55:05 +00001068void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1069 OS << "[";
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001070 Expr *receiver = Mess->getReceiver();
1071 if (receiver) PrintExpr(receiver);
1072 else OS << Mess->getClassName()->getName();
Ted Kremenekc29efd82008-05-02 17:32:38 +00001073 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001074 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001075 if (selector.isUnarySelector()) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001076 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001077 } else {
1078 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001079 if (i < selector.getNumArgs()) {
1080 if (i > 0) OS << ' ';
1081 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001082 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001083 else
1084 OS << ":";
1085 }
1086 else OS << ", "; // Handle variadic methods.
1087
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001088 PrintExpr(Mess->getArg(i));
1089 }
Steve Naroff563477d2007-09-18 23:55:05 +00001090 }
1091 OS << "]";
1092}
1093
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001094void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1095 OS << "super";
1096}
1097
Steve Naroff4eb206b2008-09-03 18:15:37 +00001098void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001099 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001100 OS << "^";
1101
1102 const FunctionType *AFT = Node->getFunctionType();
1103
1104 if (isa<FunctionTypeNoProto>(AFT)) {
1105 OS << "()";
Steve Naroff56ee6892008-10-08 17:01:13 +00001106 } else if (!BD->param_empty() || cast<FunctionTypeProto>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001107 OS << '(';
1108 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +00001109 for (BlockDecl::param_iterator AI = BD->param_begin(),
1110 E = BD->param_end(); AI != E; ++AI) {
1111 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner39f34e92008-11-24 04:00:27 +00001112 ParamStr = (*AI)->getNameAsString();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001113 (*AI)->getType().getAsStringInternal(ParamStr);
1114 OS << ParamStr;
1115 }
1116
Steve Naroff56ee6892008-10-08 17:01:13 +00001117 const FunctionTypeProto *FT = cast<FunctionTypeProto>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001118 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001119 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001120 OS << "...";
1121 }
1122 OS << ')';
1123 }
1124}
1125
Steve Naroff4eb206b2008-09-03 18:15:37 +00001126void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001127 OS << Node->getDecl()->getNameAsString();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001128}
Reid Spencer5f016e22007-07-11 17:01:13 +00001129//===----------------------------------------------------------------------===//
1130// Stmt method implementations
1131//===----------------------------------------------------------------------===//
1132
Chris Lattner6000dac2007-08-08 22:51:59 +00001133void Stmt::dumpPretty() const {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001134 printPretty(llvm::errs());
Reid Spencer5f016e22007-07-11 17:01:13 +00001135}
1136
Ted Kremeneka95d3752008-09-13 05:16:45 +00001137void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001138 if (this == 0) {
1139 OS << "<NULL>";
1140 return;
1141 }
1142
Ted Kremenek42a509f2007-08-31 21:30:12 +00001143 StmtPrinter P(OS, Helper);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00001144 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00001145}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001146
1147//===----------------------------------------------------------------------===//
1148// PrinterHelper
1149//===----------------------------------------------------------------------===//
1150
1151// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00001152PrinterHelper::~PrinterHelper() {}