blob: 2c8504357ef0d031ffcb9d78522d7f9de3941c9f [file] [log] [blame]
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001//===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnercbe4f772007-08-08 22:51:59 +000010// This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which
11// pretty print the AST back out to C code.
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/StmtVisitor.h"
Douglas Gregorc7acfdf2009-01-06 05:10:23 +000016#include "clang/AST/DeclCXX.h"
Ted Kremenek5c84c012007-10-17 18:36:42 +000017#include "clang/AST/DeclObjC.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000018#include "clang/AST/PrettyPrinter.h"
Ted Kremenek2d470fc2008-09-13 05:16:45 +000019#include "llvm/Support/Format.h"
Douglas Gregor882211c2010-04-28 22:16:22 +000020#include "clang/AST/Expr.h"
Douglas Gregor2b88c112010-09-08 00:15:04 +000021#include "clang/AST/ExprCXX.h"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// StmtPrinter Visitor
26//===----------------------------------------------------------------------===//
27
28namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +000029 class StmtPrinter : public StmtVisitor<StmtPrinter> {
Ted Kremenek2d470fc2008-09-13 05:16:45 +000030 llvm::raw_ostream &OS;
Eli Friedmanef334fd2009-05-30 05:03:24 +000031 ASTContext &Context;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000032 unsigned IndentLevel;
Ted Kremenek04f3cee2007-08-31 21:30:12 +000033 clang::PrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +000034 PrintingPolicy Policy;
35
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000036 public:
Mike Stump11289f42009-09-09 15:08:12 +000037 StmtPrinter(llvm::raw_ostream &os, ASTContext &C, PrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +000038 const PrintingPolicy &Policy,
Douglas Gregor7de59662009-05-29 20:38:28 +000039 unsigned Indentation = 0)
Eli Friedmanef334fd2009-05-30 05:03:24 +000040 : OS(os), Context(C), IndentLevel(Indentation), Helper(helper),
41 Policy(Policy) {}
Mike Stump11289f42009-09-09 15:08:12 +000042
Douglas Gregor7de59662009-05-29 20:38:28 +000043 void PrintStmt(Stmt *S) {
44 PrintStmt(S, Policy.Indentation);
45 }
46
47 void PrintStmt(Stmt *S, int SubIndent) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000048 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000049 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000050 // If this is an expr used in a stmt context, indent and newline it.
51 Indent();
Chris Lattner62249a62007-08-21 04:04:25 +000052 Visit(S);
Chris Lattnerfc068c12007-05-30 17:57:36 +000053 OS << ";\n";
Chris Lattner882f7882006-11-04 18:52:07 +000054 } else if (S) {
Chris Lattner62249a62007-08-21 04:04:25 +000055 Visit(S);
Chris Lattner882f7882006-11-04 18:52:07 +000056 } else {
Chris Lattner2f6ac262007-05-28 01:47:47 +000057 Indent() << "<<<NULL STATEMENT>>>\n";
Chris Lattner882f7882006-11-04 18:52:07 +000058 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000059 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +000060 }
Eli Friedman15ea8802009-05-30 00:19:54 +000061
Chris Lattner073926e2007-05-20 23:04:55 +000062 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +000063 void PrintRawDecl(Decl *D);
Ted Kremenek15e6b402008-10-06 18:39:36 +000064 void PrintRawDeclStmt(DeclStmt *S);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000065 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl9b244a82008-12-22 21:35:02 +000066 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Mike Stump11289f42009-09-09 15:08:12 +000067
Chris Lattner882f7882006-11-04 18:52:07 +000068 void PrintExpr(Expr *E) {
69 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +000070 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000071 else
Chris Lattner882f7882006-11-04 18:52:07 +000072 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000073 }
Mike Stump11289f42009-09-09 15:08:12 +000074
Mike Stump74a76472009-02-10 20:16:46 +000075 llvm::raw_ostream &Indent(int Delta = 0) {
Douglas Gregor7de59662009-05-29 20:38:28 +000076 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
77 OS << " ";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000078 return OS;
79 }
Mike Stump11289f42009-09-09 15:08:12 +000080
Mike Stump11289f42009-09-09 15:08:12 +000081 void Visit(Stmt* S) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +000082 if (Helper && Helper->handledStmt(S,OS))
83 return;
84 else StmtVisitor<StmtPrinter>::Visit(S);
85 }
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000086
Chandler Carruthb7967b92010-10-23 08:21:37 +000087 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000088 Indent() << "<<unknown stmt type>>\n";
89 }
Chandler Carruthb7967b92010-10-23 08:21:37 +000090 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000091 OS << "<<unknown expr type>>";
92 }
93 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Mike Stump11289f42009-09-09 15:08:12 +000094
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000095#define ABSTRACT_STMT(CLASS)
Douglas Gregorbe35ce92008-11-14 12:46:07 +000096#define STMT(CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +000097 void Visit##CLASS(CLASS *Node);
Alexis Hunt656bb312010-05-05 15:24:00 +000098#include "clang/AST/StmtNodes.inc"
Chris Lattner71e23ce2006-11-04 20:18:38 +000099 };
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000100}
101
Chris Lattner71e23ce2006-11-04 20:18:38 +0000102//===----------------------------------------------------------------------===//
103// Stmt printing methods.
104//===----------------------------------------------------------------------===//
105
Chris Lattner073926e2007-05-20 23:04:55 +0000106/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
107/// with no newline after the }.
108void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
109 OS << "{\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000110 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
Chris Lattner882f7882006-11-04 18:52:07 +0000111 I != E; ++I)
112 PrintStmt(*I);
Mike Stump11289f42009-09-09 15:08:12 +0000113
Chris Lattner073926e2007-05-20 23:04:55 +0000114 Indent() << "}";
115}
116
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000117void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000118 D->print(OS, Policy, IndentLevel);
Mike Stump74a76472009-02-10 20:16:46 +0000119}
120
Ted Kremenek15e6b402008-10-06 18:39:36 +0000121void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000122 DeclStmt::decl_iterator Begin = S->decl_begin(), End = S->decl_end();
Eli Friedman79635842009-05-30 04:20:30 +0000123 llvm::SmallVector<Decl*, 2> Decls;
Mike Stump11289f42009-09-09 15:08:12 +0000124 for ( ; Begin != End; ++Begin)
Eli Friedman79635842009-05-30 04:20:30 +0000125 Decls.push_back(*Begin);
Eli Friedman15ea8802009-05-30 00:19:54 +0000126
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000127 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenek15e6b402008-10-06 18:39:36 +0000128}
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000129
130void StmtPrinter::VisitNullStmt(NullStmt *Node) {
131 Indent() << ";\n";
132}
133
134void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000135 Indent();
136 PrintRawDeclStmt(Node);
137 OS << ";\n";
Steve Naroff2a8ad182007-05-29 22:59:26 +0000138}
139
Chris Lattner073926e2007-05-20 23:04:55 +0000140void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
141 Indent();
142 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000143 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000144}
145
Chris Lattner6c0ff132006-11-05 00:19:50 +0000146void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000147 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000148 PrintExpr(Node->getLHS());
149 if (Node->getRHS()) {
150 OS << " ... ";
151 PrintExpr(Node->getRHS());
152 }
153 OS << ":\n";
Mike Stump11289f42009-09-09 15:08:12 +0000154
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000155 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000156}
157
158void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000159 Indent(-1) << "default:\n";
160 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000161}
162
163void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000164 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000165 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000166}
167
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000168void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000169 OS << "if (";
Chris Lattner882f7882006-11-04 18:52:07 +0000170 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000171 OS << ')';
Mike Stump11289f42009-09-09 15:08:12 +0000172
Chris Lattner073926e2007-05-20 23:04:55 +0000173 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
174 OS << ' ';
175 PrintRawCompoundStmt(CS);
176 OS << (If->getElse() ? ' ' : '\n');
177 } else {
178 OS << '\n';
179 PrintStmt(If->getThen());
180 if (If->getElse()) Indent();
181 }
Mike Stump11289f42009-09-09 15:08:12 +0000182
Chris Lattner073926e2007-05-20 23:04:55 +0000183 if (Stmt *Else = If->getElse()) {
184 OS << "else";
Mike Stump11289f42009-09-09 15:08:12 +0000185
Chris Lattner073926e2007-05-20 23:04:55 +0000186 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
187 OS << ' ';
188 PrintRawCompoundStmt(CS);
189 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000190 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
191 OS << ' ';
192 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000193 } else {
194 OS << '\n';
195 PrintStmt(If->getElse());
196 }
Chris Lattner882f7882006-11-04 18:52:07 +0000197 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000198}
199
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000200void StmtPrinter::VisitIfStmt(IfStmt *If) {
201 Indent();
202 PrintRawIfStmt(If);
203}
204
Chris Lattnerf2174b62006-11-04 20:59:27 +0000205void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
206 Indent() << "switch (";
207 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000208 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000209
Chris Lattner073926e2007-05-20 23:04:55 +0000210 // Pretty print compoundstmt bodies (very common).
211 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
212 OS << " ";
213 PrintRawCompoundStmt(CS);
214 OS << "\n";
215 } else {
216 OS << "\n";
217 PrintStmt(Node->getBody());
218 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000219}
220
Anders Carlsson51873c22007-07-22 07:07:56 +0000221void StmtPrinter::VisitSwitchCase(SwitchCase*) {
222 assert(0 && "SwitchCase is an abstract class");
223}
224
Chris Lattner85ed8732006-11-04 20:40:44 +0000225void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
226 Indent() << "while (";
227 PrintExpr(Node->getCond());
228 OS << ")\n";
229 PrintStmt(Node->getBody());
230}
231
232void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000233 Indent() << "do ";
234 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
235 PrintRawCompoundStmt(CS);
236 OS << " ";
237 } else {
238 OS << "\n";
239 PrintStmt(Node->getBody());
240 Indent();
241 }
Mike Stump11289f42009-09-09 15:08:12 +0000242
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000243 OS << "while (";
Chris Lattner85ed8732006-11-04 20:40:44 +0000244 PrintExpr(Node->getCond());
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000245 OS << ");\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000246}
247
Chris Lattner71e23ce2006-11-04 20:18:38 +0000248void StmtPrinter::VisitForStmt(ForStmt *Node) {
249 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000250 if (Node->getInit()) {
251 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000252 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000253 else
254 PrintExpr(cast<Expr>(Node->getInit()));
255 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000256 OS << ";";
257 if (Node->getCond()) {
258 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000259 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000260 }
261 OS << ";";
262 if (Node->getInc()) {
263 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000264 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000265 }
266 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000267
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000268 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
269 PrintRawCompoundStmt(CS);
270 OS << "\n";
271 } else {
272 OS << "\n";
273 PrintStmt(Node->getBody());
274 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000275}
276
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000277void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000278 Indent() << "for (";
279 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000280 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000281 else
282 PrintExpr(cast<Expr>(Node->getElement()));
283 OS << " in ";
284 PrintExpr(Node->getCollection());
285 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000286
Fariborz Jahanian83615522008-01-02 22:54:34 +0000287 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
288 PrintRawCompoundStmt(CS);
289 OS << "\n";
290 } else {
291 OS << "\n";
292 PrintStmt(Node->getBody());
293 }
294}
295
Chris Lattner16976d32006-11-05 01:46:01 +0000296void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000297 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000298}
299
300void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000301 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000302 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000303 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000304}
305
306void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000307 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000308}
309
310void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000311 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000312}
313
314
Chris Lattner882f7882006-11-04 18:52:07 +0000315void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
316 Indent() << "return";
317 if (Node->getRetValue()) {
318 OS << " ";
319 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000320 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000321 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000322}
323
Chris Lattner73c56c02007-10-29 04:04:16 +0000324
325void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000326 Indent() << "asm ";
Mike Stump11289f42009-09-09 15:08:12 +0000327
Anders Carlsson660bdd12007-11-23 23:12:25 +0000328 if (Node->isVolatile())
329 OS << "volatile ";
Mike Stump11289f42009-09-09 15:08:12 +0000330
Anders Carlsson660bdd12007-11-23 23:12:25 +0000331 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000332 VisitStringLiteral(Node->getAsmString());
Mike Stump11289f42009-09-09 15:08:12 +0000333
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000334 // Outputs
335 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
336 Node->getNumClobbers() != 0)
337 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000338
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000339 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
340 if (i != 0)
341 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000342
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000343 if (!Node->getOutputName(i).empty()) {
344 OS << '[';
345 OS << Node->getOutputName(i);
346 OS << "] ";
347 }
Mike Stump11289f42009-09-09 15:08:12 +0000348
Chris Lattner72bbf172009-03-10 04:59:06 +0000349 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000350 OS << " ";
351 Visit(Node->getOutputExpr(i));
352 }
Mike Stump11289f42009-09-09 15:08:12 +0000353
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000354 // Inputs
355 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
356 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000357
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000358 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
359 if (i != 0)
360 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000361
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000362 if (!Node->getInputName(i).empty()) {
363 OS << '[';
364 OS << Node->getInputName(i);
365 OS << "] ";
366 }
Mike Stump11289f42009-09-09 15:08:12 +0000367
Chris Lattner72bbf172009-03-10 04:59:06 +0000368 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000369 OS << " ";
370 Visit(Node->getInputExpr(i));
371 }
Mike Stump11289f42009-09-09 15:08:12 +0000372
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000373 // Clobbers
374 if (Node->getNumClobbers() != 0)
375 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000376
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000377 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
378 if (i != 0)
379 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000380
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000381 VisitStringLiteral(Node->getClobber(i));
382 }
Mike Stump11289f42009-09-09 15:08:12 +0000383
Anders Carlsson81a5a692007-11-20 19:21:03 +0000384 OS << ");\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000385}
386
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000387void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000388 Indent() << "@try";
389 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
390 PrintRawCompoundStmt(TS);
391 OS << "\n";
392 }
Mike Stump11289f42009-09-09 15:08:12 +0000393
Douglas Gregor96c79492010-04-23 22:50:49 +0000394 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
395 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000396 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000397 if (catchStmt->getCatchParamDecl()) {
398 if (Decl *DS = catchStmt->getCatchParamDecl())
399 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000400 }
401 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000402 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
403 PrintRawCompoundStmt(CS);
404 OS << "\n";
405 }
Fariborz Jahanian88157952007-11-02 18:16:07 +0000406 }
Mike Stump11289f42009-09-09 15:08:12 +0000407
408 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
409 Node->getFinallyStmt())) {
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000410 Indent() << "@finally";
411 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000412 OS << "\n";
Mike Stump11289f42009-09-09 15:08:12 +0000413 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000414}
415
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000416void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000417}
418
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000419void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000420 Indent() << "@catch (...) { /* todo */ } \n";
421}
422
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000423void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000424 Indent() << "@throw";
425 if (Node->getThrowExpr()) {
426 OS << " ";
427 PrintExpr(Node->getThrowExpr());
428 }
429 OS << ";\n";
430}
431
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000432void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000433 Indent() << "@synchronized (";
434 PrintExpr(Node->getSynchExpr());
435 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000436 PrintRawCompoundStmt(Node->getSynchBody());
437 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000438}
439
Sebastian Redl9b244a82008-12-22 21:35:02 +0000440void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
441 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000442 if (Decl *ExDecl = Node->getExceptionDecl())
443 PrintRawDecl(ExDecl);
444 else
445 OS << "...";
446 OS << ") ";
447 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000448}
449
450void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
451 Indent();
452 PrintRawCXXCatchStmt(Node);
453 OS << "\n";
454}
455
456void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
457 Indent() << "try ";
458 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000459 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000460 OS << " ";
461 PrintRawCXXCatchStmt(Node->getHandler(i));
462 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000463 OS << "\n";
464}
465
Chris Lattner71e23ce2006-11-04 20:18:38 +0000466//===----------------------------------------------------------------------===//
467// Expr printing methods.
468//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000469
Chris Lattner882f7882006-11-04 18:52:07 +0000470void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000471 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
472 Qualifier->print(OS, Policy);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000473 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000474 if (Node->hasExplicitTemplateArgs())
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000475 OS << TemplateSpecializationType::PrintTemplateArgumentList(
476 Node->getTemplateArgs(),
477 Node->getNumTemplateArgs(),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000478 Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000479}
480
John McCall8cd78132009-11-19 22:55:06 +0000481void StmtPrinter::VisitDependentScopeDeclRefExpr(
482 DependentScopeDeclRefExpr *Node) {
Douglas Gregor7de59662009-05-29 20:38:28 +0000483 Node->getQualifier()->print(OS, Policy);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000484 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000485 if (Node->hasExplicitTemplateArgs())
486 OS << TemplateSpecializationType::PrintTemplateArgumentList(
487 Node->getTemplateArgs(),
488 Node->getNumTemplateArgs(),
489 Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000490}
491
John McCalld14a8642009-11-21 08:51:07 +0000492void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +0000493 if (Node->getQualifier())
494 Node->getQualifier()->print(OS, Policy);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000495 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000496 if (Node->hasExplicitTemplateArgs())
497 OS << TemplateSpecializationType::PrintTemplateArgumentList(
498 Node->getTemplateArgs(),
Douglas Gregora727cb92009-06-30 22:34:41 +0000499 Node->getNumTemplateArgs(),
John McCalle66edc12009-11-24 19:00:30 +0000500 Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +0000501}
502
Steve Naroffe46504b2007-11-12 14:29:37 +0000503void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000504 if (Node->getBase()) {
505 PrintExpr(Node->getBase());
506 OS << (Node->isArrow() ? "->" : ".");
507 }
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000508 OS << Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +0000509}
510
Steve Naroffec944032008-05-30 00:40:33 +0000511void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000512 if (Node->isSuperReceiver())
513 OS << "super.";
514 else if (Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +0000515 PrintExpr(Node->getBase());
516 OS << ".";
517 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000518
John McCallb7bd14f2010-12-02 01:19:52 +0000519 if (Node->isImplicitProperty())
520 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
521 else
522 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000523}
524
Chris Lattner6307f192008-08-10 01:53:14 +0000525void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000526 switch (Node->getIdentType()) {
527 default:
528 assert(0 && "unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000529 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000530 OS << "__func__";
531 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000532 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000533 OS << "__FUNCTION__";
534 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000535 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000536 OS << "__PRETTY_FUNCTION__";
537 break;
538 }
539}
540
Steve Naroffae4143e2007-04-26 20:39:23 +0000541void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000542 unsigned value = Node->getValue();
Chris Lattnera5678cc2008-06-07 22:35:38 +0000543 if (Node->isWide())
544 OS << "L";
Chris Lattner666115c2007-07-13 23:58:20 +0000545 switch (value) {
546 case '\\':
547 OS << "'\\\\'";
548 break;
549 case '\'':
550 OS << "'\\''";
551 break;
552 case '\a':
553 // TODO: K&R: the meaning of '\\a' is different in traditional C
554 OS << "'\\a'";
555 break;
556 case '\b':
557 OS << "'\\b'";
558 break;
559 // Nonstandard escape sequence.
560 /*case '\e':
561 OS << "'\\e'";
562 break;*/
563 case '\f':
564 OS << "'\\f'";
565 break;
566 case '\n':
567 OS << "'\\n'";
568 break;
569 case '\r':
570 OS << "'\\r'";
571 break;
572 case '\t':
573 OS << "'\\t'";
574 break;
575 case '\v':
576 OS << "'\\v'";
577 break;
578 default:
Ted Kremenek652d18e2008-02-23 00:52:04 +0000579 if (value < 256 && isprint(value)) {
Chris Lattner666115c2007-07-13 23:58:20 +0000580 OS << "'" << (char)value << "'";
581 } else if (value < 256) {
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000582 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner666115c2007-07-13 23:58:20 +0000583 } else {
584 // FIXME what to really do here?
585 OS << value;
586 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000587 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000588}
589
Steve Naroffdf7855b2007-02-21 23:46:25 +0000590void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000591 bool isSigned = Node->getType()->isSignedIntegerType();
592 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000593
Chris Lattner06430412007-05-21 05:45:03 +0000594 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +0000595 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
Chris Lattner06430412007-05-21 05:45:03 +0000596 default: assert(0 && "Unexpected type for integer literal!");
597 case BuiltinType::Int: break; // no suffix.
598 case BuiltinType::UInt: OS << 'U'; break;
599 case BuiltinType::Long: OS << 'L'; break;
600 case BuiltinType::ULong: OS << "UL"; break;
601 case BuiltinType::LongLong: OS << "LL"; break;
602 case BuiltinType::ULongLong: OS << "ULL"; break;
603 }
Chris Lattner882f7882006-11-04 18:52:07 +0000604}
Steve Naroffab624882007-02-21 22:05:47 +0000605void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattnerbf2f3862007-08-01 00:23:58 +0000606 // FIXME: print value more precisely.
Chris Lattnera0173132008-06-07 22:13:43 +0000607 OS << Node->getValueAsApproximateDouble();
Chris Lattner882f7882006-11-04 18:52:07 +0000608}
Chris Lattner1c20a172007-08-26 03:42:43 +0000609
610void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
611 PrintExpr(Node->getSubExpr());
612 OS << "i";
613}
614
Steve Naroffdf7855b2007-02-21 23:46:25 +0000615void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner882f7882006-11-04 18:52:07 +0000616 if (Str->isWide()) OS << 'L';
Chris Lattner5d8f4942006-11-04 20:29:31 +0000617 OS << '"';
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000618
Chris Lattner5d8f4942006-11-04 20:29:31 +0000619 // FIXME: this doesn't print wstrings right.
Benjamin Kramer35b077e2010-08-17 12:54:38 +0000620 llvm::StringRef StrData = Str->getString();
621 for (llvm::StringRef::iterator I = StrData.begin(), E = StrData.end();
622 I != E; ++I) {
623 unsigned char Char = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000624
Chris Lattnerbb96cc42009-01-16 19:25:18 +0000625 switch (Char) {
626 default:
627 if (isprint(Char))
628 OS << (char)Char;
629 else // Output anything hard as an octal escape.
630 OS << '\\'
631 << (char)('0'+ ((Char >> 6) & 7))
632 << (char)('0'+ ((Char >> 3) & 7))
633 << (char)('0'+ ((Char >> 0) & 7));
634 break;
635 // Handle some common non-printable cases to make dumps prettier.
Chris Lattner5d8f4942006-11-04 20:29:31 +0000636 case '\\': OS << "\\\\"; break;
637 case '"': OS << "\\\""; break;
638 case '\n': OS << "\\n"; break;
639 case '\t': OS << "\\t"; break;
640 case '\a': OS << "\\a"; break;
641 case '\b': OS << "\\b"; break;
642 }
643 }
644 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000645}
646void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
647 OS << "(";
648 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000649 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000650}
651void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000652 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000653 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +0000654
Eli Friedman1cf25362009-06-14 22:39:26 +0000655 // Print a space if this is an "identifier operator" like __real, or if
656 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +0000657 switch (Node->getOpcode()) {
658 default: break;
John McCalle3027922010-08-25 11:45:40 +0000659 case UO_Real:
660 case UO_Imag:
661 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +0000662 OS << ' ';
663 break;
John McCalle3027922010-08-25 11:45:40 +0000664 case UO_Plus:
665 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +0000666 if (isa<UnaryOperator>(Node->getSubExpr()))
667 OS << ' ';
668 break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000669 }
670 }
Chris Lattner882f7882006-11-04 18:52:07 +0000671 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000672
Chris Lattner15768702006-11-05 23:54:51 +0000673 if (Node->isPostfix())
674 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000675}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000676
Douglas Gregor882211c2010-04-28 22:16:22 +0000677void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
678 OS << "__builtin_offsetof(";
Daniel Dunbar219fa692010-06-30 19:16:48 +0000679 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +0000680 bool PrintedSomething = false;
681 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
682 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
683 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
684 // Array node
685 OS << "[";
686 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
687 OS << "]";
688 PrintedSomething = true;
689 continue;
690 }
Douglas Gregord1702062010-04-29 00:18:15 +0000691
692 // Skip implicit base indirections.
693 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
694 continue;
695
Douglas Gregor882211c2010-04-28 22:16:22 +0000696 // Field or identifier node.
697 IdentifierInfo *Id = ON.getFieldName();
698 if (!Id)
699 continue;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000700
Douglas Gregor882211c2010-04-28 22:16:22 +0000701 if (PrintedSomething)
702 OS << ".";
703 else
704 PrintedSomething = true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000705 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +0000706 }
707 OS << ")";
708}
709
Sebastian Redl6f282892008-11-11 17:56:53 +0000710void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
711 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
712 if (Node->isArgumentType())
Daniel Dunbar219fa692010-06-30 19:16:48 +0000713 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")";
Sebastian Redl6f282892008-11-11 17:56:53 +0000714 else {
715 OS << " ";
716 PrintExpr(Node->getArgumentExpr());
717 }
Chris Lattner882f7882006-11-04 18:52:07 +0000718}
719void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000720 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000721 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000722 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000723 OS << "]";
724}
725
726void StmtPrinter::VisitCallExpr(CallExpr *Call) {
727 PrintExpr(Call->getCallee());
728 OS << "(";
729 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000730 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
731 // Don't print any defaulted arguments
732 break;
733 }
734
Chris Lattner882f7882006-11-04 18:52:07 +0000735 if (i) OS << ", ";
736 PrintExpr(Call->getArg(i));
737 }
738 OS << ")";
739}
740void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +0000741 // FIXME: Suppress printing implicit bases (like "this")
742 PrintExpr(Node->getBase());
Fariborz Jahanian2990c022010-01-11 21:17:32 +0000743 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
744 if (FD->isAnonymousStructOrUnion())
745 return;
Douglas Gregore4b414c2009-01-08 22:45:41 +0000746 OS << (Node->isArrow() ? "->" : ".");
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000747 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
748 Qualifier->print(OS, Policy);
749
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000750 OS << Node->getMemberNameInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000751
John McCallb3774b52010-08-19 23:49:38 +0000752 if (Node->hasExplicitTemplateArgs())
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000753 OS << TemplateSpecializationType::PrintTemplateArgumentList(
754 Node->getTemplateArgs(),
755 Node->getNumTemplateArgs(),
756 Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000757}
Steve Naroffe87026a2009-07-24 17:54:45 +0000758void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
759 PrintExpr(Node->getBase());
760 OS << (Node->isArrow() ? "->isa" : ".isa");
761}
762
Nate Begemance4d7fc2008-04-18 23:10:10 +0000763void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000764 PrintExpr(Node->getBase());
765 OS << ".";
766 OS << Node->getAccessor().getName();
767}
Douglas Gregorf19b2312008-10-28 15:36:24 +0000768void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Fariborz Jahaniane33c1162010-06-30 16:31:08 +0000769 OS << "(" << Node->getType().getAsString(Policy) << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000770 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000771}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000772void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +0000773 OS << "(" << Node->getType().getAsString(Policy) << ")";
Steve Naroff57eb2c52007-07-19 21:32:11 +0000774 PrintExpr(Node->getInitializer());
775}
Steve Naroff7a5af782007-07-13 16:58:59 +0000776void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000777 // No need to print anything, simply forward to the sub expression.
778 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000779}
Chris Lattner882f7882006-11-04 18:52:07 +0000780void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
781 PrintExpr(Node->getLHS());
782 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
783 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000784}
Chris Lattner86928112007-08-25 02:00:02 +0000785void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
786 PrintExpr(Node->getLHS());
787 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
788 PrintExpr(Node->getRHS());
789}
Chris Lattner882f7882006-11-04 18:52:07 +0000790void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
791 PrintExpr(Node->getCond());
Mike Stump11289f42009-09-09 15:08:12 +0000792
Ted Kremenekebb1c0c2007-11-26 18:27:54 +0000793 if (Node->getLHS()) {
794 OS << " ? ";
795 PrintExpr(Node->getLHS());
796 OS << " : ";
797 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000798 else { // Handle GCC extension where LHS can be NULL.
Ted Kremenekebb1c0c2007-11-26 18:27:54 +0000799 OS << " ?: ";
800 }
Mike Stump11289f42009-09-09 15:08:12 +0000801
Chris Lattner882f7882006-11-04 18:52:07 +0000802 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000803}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000804
Chris Lattnereefa10e2007-05-28 06:56:27 +0000805// GNU extensions.
806
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000807void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000808 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000809}
810
Chris Lattner366727f2007-07-24 16:58:17 +0000811void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
812 OS << "(";
813 PrintRawCompoundStmt(E->getSubStmt());
814 OS << ")";
815}
816
Steve Naroff9efdabc2007-08-03 21:21:27 +0000817void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
818 OS << "__builtin_choose_expr(";
819 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000820 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000821 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000822 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000823 PrintExpr(Node->getRHS());
824 OS << ")";
825}
Chris Lattner366727f2007-07-24 16:58:17 +0000826
Douglas Gregor3be4b122008-11-29 04:51:27 +0000827void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
828 OS << "__null";
829}
830
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000831void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
832 OS << "__builtin_shufflevector(";
833 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
834 if (i) OS << ", ";
835 PrintExpr(Node->getExpr(i));
836 }
837 OS << ")";
838}
839
Anders Carlsson4692db02007-08-31 04:56:16 +0000840void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +0000841 if (Node->getSyntacticForm()) {
842 Visit(Node->getSyntacticForm());
843 return;
844 }
845
Anders Carlsson4692db02007-08-31 04:56:16 +0000846 OS << "{ ";
847 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
848 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000849 if (Node->getInit(i))
850 PrintExpr(Node->getInit(i));
851 else
852 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +0000853 }
854 OS << " }";
855}
856
Nate Begeman5ec4b312009-08-10 23:49:36 +0000857void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
858 OS << "( ";
859 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
860 if (i) OS << ", ";
861 PrintExpr(Node->getExpr(i));
862 }
863 OS << " )";
864}
865
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000866void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000867 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
868 DEnd = Node->designators_end();
869 D != DEnd; ++D) {
870 if (D->isFieldDesignator()) {
871 if (D->getDotLoc().isInvalid())
872 OS << D->getFieldName()->getName() << ":";
873 else
874 OS << "." << D->getFieldName()->getName();
875 } else {
876 OS << "[";
877 if (D->isArrayDesignator()) {
878 PrintExpr(Node->getArrayIndex(*D));
879 } else {
880 PrintExpr(Node->getArrayRangeStart(*D));
881 OS << " ... ";
Mike Stump11289f42009-09-09 15:08:12 +0000882 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000883 }
884 OS << "]";
885 }
886 }
887
888 OS << " = ";
889 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000890}
891
Douglas Gregor0202cb42009-01-29 17:44:32 +0000892void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Chris Lattnerc61089a2009-06-30 01:26:17 +0000893 if (Policy.LangOpts.CPlusPlus)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000894 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
895 else {
896 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
897 if (Node->getType()->isRecordType())
898 OS << "{}";
899 else
900 OS << 0;
901 }
Douglas Gregor0202cb42009-01-29 17:44:32 +0000902}
903
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000904void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +0000905 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000906 PrintExpr(Node->getSubExpr());
907 OS << ", ";
Daniel Dunbar219fa692010-06-30 19:16:48 +0000908 OS << Node->getType().getAsString(Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000909 OS << ")";
910}
911
Chris Lattnereefa10e2007-05-28 06:56:27 +0000912// C++
Douglas Gregor993603d2008-11-14 16:09:21 +0000913void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
914 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
915 "",
916#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
917 Spelling,
918#include "clang/Basic/OperatorKinds.def"
919 };
920
921 OverloadedOperatorKind Kind = Node->getOperator();
922 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
923 if (Node->getNumArgs() == 1) {
924 OS << OpStrings[Kind] << ' ';
925 PrintExpr(Node->getArg(0));
926 } else {
927 PrintExpr(Node->getArg(0));
928 OS << ' ' << OpStrings[Kind];
929 }
930 } else if (Kind == OO_Call) {
931 PrintExpr(Node->getArg(0));
932 OS << '(';
933 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
934 if (ArgIdx > 1)
935 OS << ", ";
936 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
937 PrintExpr(Node->getArg(ArgIdx));
938 }
939 OS << ')';
940 } else if (Kind == OO_Subscript) {
941 PrintExpr(Node->getArg(0));
942 OS << '[';
943 PrintExpr(Node->getArg(1));
944 OS << ']';
945 } else if (Node->getNumArgs() == 1) {
946 OS << OpStrings[Kind] << ' ';
947 PrintExpr(Node->getArg(0));
948 } else if (Node->getNumArgs() == 2) {
949 PrintExpr(Node->getArg(0));
950 OS << ' ' << OpStrings[Kind] << ' ';
951 PrintExpr(Node->getArg(1));
952 } else {
953 assert(false && "unknown overloaded operator");
954 }
955}
Chris Lattnereefa10e2007-05-28 06:56:27 +0000956
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000957void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
958 VisitCallExpr(cast<CallExpr>(Node));
959}
960
Douglas Gregore200adc2008-10-27 19:41:14 +0000961void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
962 OS << Node->getCastName() << '<';
Daniel Dunbar219fa692010-06-30 19:16:48 +0000963 OS << Node->getTypeAsWritten().getAsString(Policy) << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +0000964 PrintExpr(Node->getSubExpr());
965 OS << ")";
966}
967
Douglas Gregore200adc2008-10-27 19:41:14 +0000968void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
969 VisitCXXNamedCastExpr(Node);
970}
971
972void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
973 VisitCXXNamedCastExpr(Node);
974}
975
976void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
977 VisitCXXNamedCastExpr(Node);
978}
979
980void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
981 VisitCXXNamedCastExpr(Node);
982}
983
Sebastian Redlc4704762008-11-11 11:37:55 +0000984void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
985 OS << "typeid(";
986 if (Node->isTypeOperand()) {
Daniel Dunbar219fa692010-06-30 19:16:48 +0000987 OS << Node->getTypeOperand().getAsString(Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +0000988 } else {
989 PrintExpr(Node->getExprOperand());
990 }
991 OS << ")";
992}
993
Francois Pichet9f4f2072010-09-08 12:20:18 +0000994void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
995 OS << "__uuidof(";
996 if (Node->isTypeOperand()) {
997 OS << Node->getTypeOperand().getAsString(Policy);
998 } else {
999 PrintExpr(Node->getExprOperand());
1000 }
1001 OS << ")";
1002}
1003
Chris Lattnereefa10e2007-05-28 06:56:27 +00001004void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1005 OS << (Node->getValue() ? "true" : "false");
1006}
1007
Sebastian Redl576fd422009-05-10 18:38:11 +00001008void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1009 OS << "nullptr";
1010}
1011
Douglas Gregor97a9c812008-11-04 14:32:21 +00001012void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1013 OS << "this";
1014}
1015
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001016void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1017 if (Node->getSubExpr() == 0)
1018 OS << "throw";
1019 else {
1020 OS << "throw ";
1021 PrintExpr(Node->getSubExpr());
1022 }
1023}
1024
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001025void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1026 // Nothing to print: we picked up the default argument
1027}
1028
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001029void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001030 OS << Node->getType().getAsString(Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001031 OS << "(";
1032 PrintExpr(Node->getSubExpr());
1033 OS << ")";
1034}
1035
Anders Carlsson993a4b32009-05-30 20:03:25 +00001036void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1037 PrintExpr(Node->getSubExpr());
1038}
1039
Douglas Gregordd04d332009-01-16 18:33:17 +00001040void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001041 OS << Node->getType().getAsString(Policy);
Douglas Gregordd04d332009-01-16 18:33:17 +00001042 OS << "(";
1043 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001044 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001045 Arg != ArgEnd; ++Arg) {
1046 if (Arg != Node->arg_begin())
1047 OS << ", ";
1048 PrintExpr(*Arg);
1049 }
1050 OS << ")";
1051}
1052
Douglas Gregor747eb782010-07-08 06:14:04 +00001053void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00001054 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1055 OS << TSInfo->getType().getAsString(Policy) << "()";
1056 else
1057 OS << Node->getType().getAsString(Policy) << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001058}
1059
Sebastian Redlbd150f42008-11-21 19:14:01 +00001060void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1061 if (E->isGlobalNew())
1062 OS << "::";
1063 OS << "new ";
1064 unsigned NumPlace = E->getNumPlacementArgs();
1065 if (NumPlace > 0) {
1066 OS << "(";
1067 PrintExpr(E->getPlacementArg(0));
1068 for (unsigned i = 1; i < NumPlace; ++i) {
1069 OS << ", ";
1070 PrintExpr(E->getPlacementArg(i));
1071 }
1072 OS << ") ";
1073 }
1074 if (E->isParenTypeId())
1075 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001076 std::string TypeS;
1077 if (Expr *Size = E->getArraySize()) {
1078 llvm::raw_string_ostream s(TypeS);
Eli Friedmanc4fc8392009-05-30 05:32:46 +00001079 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001080 s.flush();
1081 TypeS = "[" + TypeS + "]";
1082 }
Douglas Gregor7de59662009-05-29 20:38:28 +00001083 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001084 OS << TypeS;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001085 if (E->isParenTypeId())
1086 OS << ")";
1087
1088 if (E->hasInitializer()) {
1089 OS << "(";
1090 unsigned NumCons = E->getNumConstructorArgs();
1091 if (NumCons > 0) {
1092 PrintExpr(E->getConstructorArg(0));
1093 for (unsigned i = 1; i < NumCons; ++i) {
1094 OS << ", ";
1095 PrintExpr(E->getConstructorArg(i));
1096 }
1097 }
1098 OS << ")";
1099 }
1100}
1101
1102void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1103 if (E->isGlobalDelete())
1104 OS << "::";
1105 OS << "delete ";
1106 if (E->isArrayForm())
1107 OS << "[] ";
1108 PrintExpr(E->getArgument());
1109}
1110
Douglas Gregorad8a3362009-09-04 17:36:40 +00001111void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1112 PrintExpr(E->getBase());
1113 if (E->isArrow())
1114 OS << "->";
1115 else
1116 OS << '.';
1117 if (E->getQualifier())
1118 E->getQualifier()->print(OS, Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001119
Douglas Gregorad8a3362009-09-04 17:36:40 +00001120 std::string TypeS;
Douglas Gregor678f90d2010-02-25 01:56:36 +00001121 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1122 OS << II->getName();
1123 else
1124 E->getDestroyedType().getAsStringInternal(TypeS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00001125 OS << TypeS;
1126}
1127
Anders Carlsson0781ce72009-04-23 02:32:43 +00001128void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00001129 // FIXME. For now we just print a trivial constructor call expression,
1130 // constructing its first argument object.
1131 if (E->getNumArgs() == 1) {
1132 CXXConstructorDecl *CD = E->getConstructor();
1133 if (CD->isTrivial())
1134 PrintExpr(E->getArg(0));
1135 }
Anders Carlsson0781ce72009-04-23 02:32:43 +00001136 // Nothing to print.
1137}
1138
John McCall5d413782010-12-06 08:20:24 +00001139void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Anders Carlssondefc6442009-04-24 22:47:04 +00001140 // Just forward to the sub expression.
1141 PrintExpr(E->getSubExpr());
1142}
1143
Mike Stump11289f42009-09-09 15:08:12 +00001144void
Douglas Gregorce934142009-05-20 18:46:25 +00001145StmtPrinter::VisitCXXUnresolvedConstructExpr(
1146 CXXUnresolvedConstructExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001147 OS << Node->getTypeAsWritten().getAsString(Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00001148 OS << "(";
1149 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001150 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00001151 Arg != ArgEnd; ++Arg) {
1152 if (Arg != Node->arg_begin())
1153 OS << ", ";
1154 PrintExpr(*Arg);
1155 }
1156 OS << ")";
1157}
1158
John McCall8cd78132009-11-19 22:55:06 +00001159void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1160 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001161 if (!Node->isImplicitAccess()) {
1162 PrintExpr(Node->getBase());
1163 OS << (Node->isArrow() ? "->" : ".");
1164 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00001165 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1166 Qualifier->print(OS, Policy);
John McCall2d74de92009-12-01 22:10:20 +00001167 else if (Node->hasExplicitTemplateArgs())
Douglas Gregor308047d2009-09-09 00:23:06 +00001168 // FIXME: Track use of "template" keyword explicitly?
1169 OS << "template ";
Mike Stump11289f42009-09-09 15:08:12 +00001170
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001171 OS << Node->getMemberNameInfo();
Mike Stump11289f42009-09-09 15:08:12 +00001172
John McCall2d74de92009-12-01 22:10:20 +00001173 if (Node->hasExplicitTemplateArgs()) {
Douglas Gregor308047d2009-09-09 00:23:06 +00001174 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1175 Node->getTemplateArgs(),
1176 Node->getNumTemplateArgs(),
1177 Policy);
1178 }
Douglas Gregora8db9542009-05-22 21:13:27 +00001179}
1180
John McCall10eae182009-11-30 22:42:35 +00001181void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001182 if (!Node->isImplicitAccess()) {
1183 PrintExpr(Node->getBase());
1184 OS << (Node->isArrow() ? "->" : ".");
1185 }
John McCall10eae182009-11-30 22:42:35 +00001186 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1187 Qualifier->print(OS, Policy);
1188
1189 // FIXME: this might originally have been written with 'template'
1190
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001191 OS << Node->getMemberNameInfo();
John McCall10eae182009-11-30 22:42:35 +00001192
1193 if (Node->hasExplicitTemplateArgs()) {
1194 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1195 Node->getTemplateArgs(),
1196 Node->getNumTemplateArgs(),
1197 Policy);
1198 }
1199}
1200
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001201static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1202 switch (UTT) {
Francois Pichet347c4c72010-12-07 00:55:57 +00001203 default: llvm_unreachable("Unknown unary type trait");
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001204 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1205 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1206 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1207 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1208 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1209 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1210 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1211 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1212 case UTT_IsAbstract: return "__is_abstract";
1213 case UTT_IsClass: return "__is_class";
1214 case UTT_IsEmpty: return "__is_empty";
1215 case UTT_IsEnum: return "__is_enum";
1216 case UTT_IsPOD: return "__is_pod";
1217 case UTT_IsPolymorphic: return "__is_polymorphic";
1218 case UTT_IsUnion: return "__is_union";
1219 }
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001220 return "";
1221}
1222
1223static const char *getTypeTraitName(BinaryTypeTrait BTT) {
1224 switch (BTT) {
Francois Pichet347c4c72010-12-07 00:55:57 +00001225 default: llvm_unreachable("Unknown binary type trait");
Francois Pichet34b21132010-12-08 22:35:30 +00001226 case BTT_IsBaseOf: return "__is_base_of";
1227 case BTT_TypeCompatible: return "__builtin_types_compatible_p";
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001228 }
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001229 return "";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001230}
1231
1232void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1233 OS << getTypeTraitName(E->getTrait()) << "("
Daniel Dunbar219fa692010-06-30 19:16:48 +00001234 << E->getQueriedType().getAsString(Policy) << ")";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001235}
1236
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001237void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1238 OS << getTypeTraitName(E->getTrait()) << "("
1239 << E->getLhsType().getAsString(Policy) << ","
1240 << E->getRhsType().getAsString(Policy) << ")";
1241}
1242
Sebastian Redl4202c0f2010-09-10 20:55:43 +00001243void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1244 OS << "noexcept(";
1245 PrintExpr(E->getOperand());
1246 OS << ")";
1247}
1248
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001249void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00001250 PrintExpr(E->getPattern());
1251 OS << "...";
1252}
1253
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001254void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
1255 OS << "sizeof...(" << E->getPack()->getNameAsString() << ")";
1256}
1257
Mike Stump11289f42009-09-09 15:08:12 +00001258// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00001259
1260void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1261 OS << "@";
1262 VisitStringLiteral(Node->getString());
1263}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001264
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001265void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001266 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001267}
1268
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001269void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001270 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001271}
1272
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001273void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001274 OS << "@protocol(" << Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001275}
1276
Steve Naroffd54978b2007-09-18 23:55:05 +00001277void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1278 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00001279 switch (Mess->getReceiverKind()) {
1280 case ObjCMessageExpr::Instance:
1281 PrintExpr(Mess->getInstanceReceiver());
1282 break;
1283
1284 case ObjCMessageExpr::Class:
1285 OS << Mess->getClassReceiver().getAsString(Policy);
1286 break;
1287
1288 case ObjCMessageExpr::SuperInstance:
1289 case ObjCMessageExpr::SuperClass:
1290 OS << "Super";
1291 break;
1292 }
1293
Ted Kremeneka06e7122008-05-02 17:32:38 +00001294 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001295 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001296 if (selector.isUnarySelector()) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001297 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001298 } else {
1299 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001300 if (i < selector.getNumArgs()) {
1301 if (i > 0) OS << ' ';
1302 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001303 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001304 else
1305 OS << ":";
1306 }
1307 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00001308
Steve Naroffc6814ea2007-10-02 20:01:56 +00001309 PrintExpr(Mess->getArg(i));
1310 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001311 }
1312 OS << "]";
1313}
1314
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001315
Steve Naroffc540d662008-09-03 18:15:37 +00001316void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001317 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001318 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00001319
Steve Naroffc540d662008-09-03 18:15:37 +00001320 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001321
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001322 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00001323 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001324 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001325 OS << '(';
1326 std::string ParamStr;
Steve Naroff415d3d52008-10-08 17:01:13 +00001327 for (BlockDecl::param_iterator AI = BD->param_begin(),
1328 E = BD->param_end(); AI != E; ++AI) {
1329 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001330 ParamStr = (*AI)->getNameAsString();
Douglas Gregor7de59662009-05-29 20:38:28 +00001331 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroffc540d662008-09-03 18:15:37 +00001332 OS << ParamStr;
1333 }
Mike Stump11289f42009-09-09 15:08:12 +00001334
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001335 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001336 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001337 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001338 OS << "...";
1339 }
1340 OS << ')';
1341 }
1342}
1343
Steve Naroffc540d662008-09-03 18:15:37 +00001344void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001345 OS << Node->getDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001346}
John McCall8d69a212010-11-15 23:31:06 +00001347
1348void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {}
1349
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001350//===----------------------------------------------------------------------===//
1351// Stmt method implementations
1352//===----------------------------------------------------------------------===//
1353
Eli Friedmanef334fd2009-05-30 05:03:24 +00001354void Stmt::dumpPretty(ASTContext& Context) const {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00001355 printPretty(llvm::errs(), Context, 0,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001356 PrintingPolicy(Context.getLangOptions()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001357}
1358
Eli Friedmanef334fd2009-05-30 05:03:24 +00001359void Stmt::printPretty(llvm::raw_ostream &OS, ASTContext& Context,
1360 PrinterHelper* Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00001361 const PrintingPolicy &Policy,
1362 unsigned Indentation) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001363 if (this == 0) {
1364 OS << "<NULL>";
1365 return;
1366 }
1367
Douglas Gregor8655e882009-10-16 22:46:09 +00001368 if (Policy.Dump && &Context) {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00001369 dump(OS, Context.getSourceManager());
Douglas Gregor278f52e2009-05-30 00:08:05 +00001370 return;
1371 }
Mike Stump11289f42009-09-09 15:08:12 +00001372
Eli Friedmanef334fd2009-05-30 05:03:24 +00001373 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00001374 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001375}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001376
1377//===----------------------------------------------------------------------===//
1378// PrinterHelper
1379//===----------------------------------------------------------------------===//
1380
1381// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001382PrinterHelper::~PrinterHelper() {}