blob: 37010a0358d6b4915cfd3f7bcd40eacd33969a9a [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"
Douglas Gregorcdbc5392011-01-15 01:15:58 +000018#include "clang/AST/DeclTemplate.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Ted Kremenek2d470fc2008-09-13 05:16:45 +000020#include "llvm/Support/Format.h"
Douglas Gregor882211c2010-04-28 22:16:22 +000021#include "clang/AST/Expr.h"
Douglas Gregor2b88c112010-09-08 00:15:04 +000022#include "clang/AST/ExprCXX.h"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// StmtPrinter Visitor
27//===----------------------------------------------------------------------===//
28
29namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +000030 class StmtPrinter : public StmtVisitor<StmtPrinter> {
Ted Kremenek2d470fc2008-09-13 05:16:45 +000031 llvm::raw_ostream &OS;
Eli Friedmanef334fd2009-05-30 05:03:24 +000032 ASTContext &Context;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000033 unsigned IndentLevel;
Ted Kremenek04f3cee2007-08-31 21:30:12 +000034 clang::PrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +000035 PrintingPolicy Policy;
36
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000037 public:
Mike Stump11289f42009-09-09 15:08:12 +000038 StmtPrinter(llvm::raw_ostream &os, ASTContext &C, PrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +000039 const PrintingPolicy &Policy,
Douglas Gregor7de59662009-05-29 20:38:28 +000040 unsigned Indentation = 0)
Eli Friedmanef334fd2009-05-30 05:03:24 +000041 : OS(os), Context(C), IndentLevel(Indentation), Helper(helper),
42 Policy(Policy) {}
Mike Stump11289f42009-09-09 15:08:12 +000043
Douglas Gregor7de59662009-05-29 20:38:28 +000044 void PrintStmt(Stmt *S) {
45 PrintStmt(S, Policy.Indentation);
46 }
47
48 void PrintStmt(Stmt *S, int SubIndent) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000049 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000050 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000051 // If this is an expr used in a stmt context, indent and newline it.
52 Indent();
Chris Lattner62249a62007-08-21 04:04:25 +000053 Visit(S);
Chris Lattnerfc068c12007-05-30 17:57:36 +000054 OS << ";\n";
Chris Lattner882f7882006-11-04 18:52:07 +000055 } else if (S) {
Chris Lattner62249a62007-08-21 04:04:25 +000056 Visit(S);
Chris Lattner882f7882006-11-04 18:52:07 +000057 } else {
Chris Lattner2f6ac262007-05-28 01:47:47 +000058 Indent() << "<<<NULL STATEMENT>>>\n";
Chris Lattner882f7882006-11-04 18:52:07 +000059 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000060 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +000061 }
Eli Friedman15ea8802009-05-30 00:19:54 +000062
Chris Lattner073926e2007-05-20 23:04:55 +000063 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +000064 void PrintRawDecl(Decl *D);
Ted Kremenek15e6b402008-10-06 18:39:36 +000065 void PrintRawDeclStmt(DeclStmt *S);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000066 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl9b244a82008-12-22 21:35:02 +000067 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Peter Collingbourne4b279a02011-02-08 21:17:54 +000068 void PrintCallArgs(CallExpr *E);
Mike Stump11289f42009-09-09 15:08:12 +000069
Chris Lattner882f7882006-11-04 18:52:07 +000070 void PrintExpr(Expr *E) {
71 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +000072 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000073 else
Chris Lattner882f7882006-11-04 18:52:07 +000074 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000075 }
Mike Stump11289f42009-09-09 15:08:12 +000076
Mike Stump74a76472009-02-10 20:16:46 +000077 llvm::raw_ostream &Indent(int Delta = 0) {
Douglas Gregor7de59662009-05-29 20:38:28 +000078 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
79 OS << " ";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000080 return OS;
81 }
Mike Stump11289f42009-09-09 15:08:12 +000082
Mike Stump11289f42009-09-09 15:08:12 +000083 void Visit(Stmt* S) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +000084 if (Helper && Helper->handledStmt(S,OS))
85 return;
86 else StmtVisitor<StmtPrinter>::Visit(S);
87 }
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000088
Chandler Carruthb7967b92010-10-23 08:21:37 +000089 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000090 Indent() << "<<unknown stmt type>>\n";
91 }
Chandler Carruthb7967b92010-10-23 08:21:37 +000092 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000093 OS << "<<unknown expr type>>";
94 }
95 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Mike Stump11289f42009-09-09 15:08:12 +000096
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000097#define ABSTRACT_STMT(CLASS)
Douglas Gregorbe35ce92008-11-14 12:46:07 +000098#define STMT(CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +000099 void Visit##CLASS(CLASS *Node);
Alexis Hunt656bb312010-05-05 15:24:00 +0000100#include "clang/AST/StmtNodes.inc"
Chris Lattner71e23ce2006-11-04 20:18:38 +0000101 };
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000102}
103
Chris Lattner71e23ce2006-11-04 20:18:38 +0000104//===----------------------------------------------------------------------===//
105// Stmt printing methods.
106//===----------------------------------------------------------------------===//
107
Chris Lattner073926e2007-05-20 23:04:55 +0000108/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
109/// with no newline after the }.
110void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
111 OS << "{\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000112 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
Chris Lattner882f7882006-11-04 18:52:07 +0000113 I != E; ++I)
114 PrintStmt(*I);
Mike Stump11289f42009-09-09 15:08:12 +0000115
Chris Lattner073926e2007-05-20 23:04:55 +0000116 Indent() << "}";
117}
118
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000119void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000120 D->print(OS, Policy, IndentLevel);
Mike Stump74a76472009-02-10 20:16:46 +0000121}
122
Ted Kremenek15e6b402008-10-06 18:39:36 +0000123void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000124 DeclStmt::decl_iterator Begin = S->decl_begin(), End = S->decl_end();
Eli Friedman79635842009-05-30 04:20:30 +0000125 llvm::SmallVector<Decl*, 2> Decls;
Mike Stump11289f42009-09-09 15:08:12 +0000126 for ( ; Begin != End; ++Begin)
Eli Friedman79635842009-05-30 04:20:30 +0000127 Decls.push_back(*Begin);
Eli Friedman15ea8802009-05-30 00:19:54 +0000128
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000129 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenek15e6b402008-10-06 18:39:36 +0000130}
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000131
132void StmtPrinter::VisitNullStmt(NullStmt *Node) {
133 Indent() << ";\n";
134}
135
136void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000137 Indent();
138 PrintRawDeclStmt(Node);
139 OS << ";\n";
Steve Naroff2a8ad182007-05-29 22:59:26 +0000140}
141
Chris Lattner073926e2007-05-20 23:04:55 +0000142void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
143 Indent();
144 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000145 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000146}
147
Chris Lattner6c0ff132006-11-05 00:19:50 +0000148void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000149 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000150 PrintExpr(Node->getLHS());
151 if (Node->getRHS()) {
152 OS << " ... ";
153 PrintExpr(Node->getRHS());
154 }
155 OS << ":\n";
Mike Stump11289f42009-09-09 15:08:12 +0000156
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000157 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000158}
159
160void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000161 Indent(-1) << "default:\n";
162 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000163}
164
165void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000166 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000167 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000168}
169
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000170void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000171 OS << "if (";
Chris Lattner882f7882006-11-04 18:52:07 +0000172 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000173 OS << ')';
Mike Stump11289f42009-09-09 15:08:12 +0000174
Chris Lattner073926e2007-05-20 23:04:55 +0000175 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
176 OS << ' ';
177 PrintRawCompoundStmt(CS);
178 OS << (If->getElse() ? ' ' : '\n');
179 } else {
180 OS << '\n';
181 PrintStmt(If->getThen());
182 if (If->getElse()) Indent();
183 }
Mike Stump11289f42009-09-09 15:08:12 +0000184
Chris Lattner073926e2007-05-20 23:04:55 +0000185 if (Stmt *Else = If->getElse()) {
186 OS << "else";
Mike Stump11289f42009-09-09 15:08:12 +0000187
Chris Lattner073926e2007-05-20 23:04:55 +0000188 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
189 OS << ' ';
190 PrintRawCompoundStmt(CS);
191 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000192 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
193 OS << ' ';
194 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000195 } else {
196 OS << '\n';
197 PrintStmt(If->getElse());
198 }
Chris Lattner882f7882006-11-04 18:52:07 +0000199 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000200}
201
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000202void StmtPrinter::VisitIfStmt(IfStmt *If) {
203 Indent();
204 PrintRawIfStmt(If);
205}
206
Chris Lattnerf2174b62006-11-04 20:59:27 +0000207void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
208 Indent() << "switch (";
209 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000210 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000211
Chris Lattner073926e2007-05-20 23:04:55 +0000212 // Pretty print compoundstmt bodies (very common).
213 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
214 OS << " ";
215 PrintRawCompoundStmt(CS);
216 OS << "\n";
217 } else {
218 OS << "\n";
219 PrintStmt(Node->getBody());
220 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000221}
222
Chris Lattner85ed8732006-11-04 20:40:44 +0000223void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
224 Indent() << "while (";
225 PrintExpr(Node->getCond());
226 OS << ")\n";
227 PrintStmt(Node->getBody());
228}
229
230void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000231 Indent() << "do ";
232 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
233 PrintRawCompoundStmt(CS);
234 OS << " ";
235 } else {
236 OS << "\n";
237 PrintStmt(Node->getBody());
238 Indent();
239 }
Mike Stump11289f42009-09-09 15:08:12 +0000240
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000241 OS << "while (";
Chris Lattner85ed8732006-11-04 20:40:44 +0000242 PrintExpr(Node->getCond());
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000243 OS << ");\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000244}
245
Chris Lattner71e23ce2006-11-04 20:18:38 +0000246void StmtPrinter::VisitForStmt(ForStmt *Node) {
247 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000248 if (Node->getInit()) {
249 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000250 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000251 else
252 PrintExpr(cast<Expr>(Node->getInit()));
253 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000254 OS << ";";
255 if (Node->getCond()) {
256 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000257 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000258 }
259 OS << ";";
260 if (Node->getInc()) {
261 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000262 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000263 }
264 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000265
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000266 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
267 PrintRawCompoundStmt(CS);
268 OS << "\n";
269 } else {
270 OS << "\n";
271 PrintStmt(Node->getBody());
272 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000273}
274
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000275void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000276 Indent() << "for (";
277 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000278 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000279 else
280 PrintExpr(cast<Expr>(Node->getElement()));
281 OS << " in ";
282 PrintExpr(Node->getCollection());
283 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000284
Fariborz Jahanian83615522008-01-02 22:54:34 +0000285 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
286 PrintRawCompoundStmt(CS);
287 OS << "\n";
288 } else {
289 OS << "\n";
290 PrintStmt(Node->getBody());
291 }
292}
293
Richard Smith02e85f32011-04-14 22:09:26 +0000294void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
295 Indent() << "for (";
296 PrintingPolicy SubPolicy(Policy);
297 SubPolicy.SuppressInitializers = true;
298 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
299 OS << " : ";
300 PrintExpr(Node->getRangeInit());
301 OS << ") {\n";
302 PrintStmt(Node->getBody());
303 Indent() << "}\n";
304}
305
Chris Lattner16976d32006-11-05 01:46:01 +0000306void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000307 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000308}
309
310void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000311 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000312 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000313 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000314}
315
316void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000317 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000318}
319
320void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000321 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000322}
323
324
Chris Lattner882f7882006-11-04 18:52:07 +0000325void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
326 Indent() << "return";
327 if (Node->getRetValue()) {
328 OS << " ";
329 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000330 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000331 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000332}
333
Chris Lattner73c56c02007-10-29 04:04:16 +0000334
335void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000336 Indent() << "asm ";
Mike Stump11289f42009-09-09 15:08:12 +0000337
Anders Carlsson660bdd12007-11-23 23:12:25 +0000338 if (Node->isVolatile())
339 OS << "volatile ";
Mike Stump11289f42009-09-09 15:08:12 +0000340
Anders Carlsson660bdd12007-11-23 23:12:25 +0000341 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000342 VisitStringLiteral(Node->getAsmString());
Mike Stump11289f42009-09-09 15:08:12 +0000343
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000344 // Outputs
345 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
346 Node->getNumClobbers() != 0)
347 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000348
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000349 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
350 if (i != 0)
351 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000352
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000353 if (!Node->getOutputName(i).empty()) {
354 OS << '[';
355 OS << Node->getOutputName(i);
356 OS << "] ";
357 }
Mike Stump11289f42009-09-09 15:08:12 +0000358
Chris Lattner72bbf172009-03-10 04:59:06 +0000359 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000360 OS << " ";
361 Visit(Node->getOutputExpr(i));
362 }
Mike Stump11289f42009-09-09 15:08:12 +0000363
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000364 // Inputs
365 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
366 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000367
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000368 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
369 if (i != 0)
370 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000371
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000372 if (!Node->getInputName(i).empty()) {
373 OS << '[';
374 OS << Node->getInputName(i);
375 OS << "] ";
376 }
Mike Stump11289f42009-09-09 15:08:12 +0000377
Chris Lattner72bbf172009-03-10 04:59:06 +0000378 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000379 OS << " ";
380 Visit(Node->getInputExpr(i));
381 }
Mike Stump11289f42009-09-09 15:08:12 +0000382
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000383 // Clobbers
384 if (Node->getNumClobbers() != 0)
385 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000386
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000387 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
388 if (i != 0)
389 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000390
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000391 VisitStringLiteral(Node->getClobber(i));
392 }
Mike Stump11289f42009-09-09 15:08:12 +0000393
Anders Carlsson81a5a692007-11-20 19:21:03 +0000394 OS << ");\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000395}
396
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000397void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000398 Indent() << "@try";
399 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
400 PrintRawCompoundStmt(TS);
401 OS << "\n";
402 }
Mike Stump11289f42009-09-09 15:08:12 +0000403
Douglas Gregor96c79492010-04-23 22:50:49 +0000404 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
405 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000406 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000407 if (catchStmt->getCatchParamDecl()) {
408 if (Decl *DS = catchStmt->getCatchParamDecl())
409 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000410 }
411 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000412 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
413 PrintRawCompoundStmt(CS);
414 OS << "\n";
415 }
Fariborz Jahanian88157952007-11-02 18:16:07 +0000416 }
Mike Stump11289f42009-09-09 15:08:12 +0000417
418 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
419 Node->getFinallyStmt())) {
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000420 Indent() << "@finally";
421 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000422 OS << "\n";
Mike Stump11289f42009-09-09 15:08:12 +0000423 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000424}
425
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000426void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000427}
428
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000429void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000430 Indent() << "@catch (...) { /* todo */ } \n";
431}
432
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000433void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000434 Indent() << "@throw";
435 if (Node->getThrowExpr()) {
436 OS << " ";
437 PrintExpr(Node->getThrowExpr());
438 }
439 OS << ";\n";
440}
441
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000442void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000443 Indent() << "@synchronized (";
444 PrintExpr(Node->getSynchExpr());
445 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000446 PrintRawCompoundStmt(Node->getSynchBody());
447 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000448}
449
Sebastian Redl9b244a82008-12-22 21:35:02 +0000450void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
451 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000452 if (Decl *ExDecl = Node->getExceptionDecl())
453 PrintRawDecl(ExDecl);
454 else
455 OS << "...";
456 OS << ") ";
457 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000458}
459
460void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
461 Indent();
462 PrintRawCXXCatchStmt(Node);
463 OS << "\n";
464}
465
466void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
467 Indent() << "try ";
468 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000469 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000470 OS << " ";
471 PrintRawCXXCatchStmt(Node->getHandler(i));
472 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000473 OS << "\n";
474}
475
Chris Lattner71e23ce2006-11-04 20:18:38 +0000476//===----------------------------------------------------------------------===//
477// Expr printing methods.
478//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000479
Chris Lattner882f7882006-11-04 18:52:07 +0000480void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000481 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
482 Qualifier->print(OS, Policy);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000483 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000484 if (Node->hasExplicitTemplateArgs())
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000485 OS << TemplateSpecializationType::PrintTemplateArgumentList(
486 Node->getTemplateArgs(),
487 Node->getNumTemplateArgs(),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000488 Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000489}
490
John McCall8cd78132009-11-19 22:55:06 +0000491void StmtPrinter::VisitDependentScopeDeclRefExpr(
492 DependentScopeDeclRefExpr *Node) {
Axel Naumann20b27862011-01-24 15:44:00 +0000493 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
494 Qualifier->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(),
499 Node->getNumTemplateArgs(),
500 Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000501}
502
John McCalld14a8642009-11-21 08:51:07 +0000503void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +0000504 if (Node->getQualifier())
505 Node->getQualifier()->print(OS, Policy);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000506 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000507 if (Node->hasExplicitTemplateArgs())
508 OS << TemplateSpecializationType::PrintTemplateArgumentList(
509 Node->getTemplateArgs(),
Douglas Gregora727cb92009-06-30 22:34:41 +0000510 Node->getNumTemplateArgs(),
John McCalle66edc12009-11-24 19:00:30 +0000511 Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +0000512}
513
Steve Naroffe46504b2007-11-12 14:29:37 +0000514void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000515 if (Node->getBase()) {
516 PrintExpr(Node->getBase());
517 OS << (Node->isArrow() ? "->" : ".");
518 }
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000519 OS << Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +0000520}
521
Steve Naroffec944032008-05-30 00:40:33 +0000522void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000523 if (Node->isSuperReceiver())
524 OS << "super.";
525 else if (Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +0000526 PrintExpr(Node->getBase());
527 OS << ".";
528 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000529
John McCallb7bd14f2010-12-02 01:19:52 +0000530 if (Node->isImplicitProperty())
531 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
532 else
533 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000534}
535
Chris Lattner6307f192008-08-10 01:53:14 +0000536void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000537 switch (Node->getIdentType()) {
538 default:
539 assert(0 && "unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000540 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000541 OS << "__func__";
542 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000543 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000544 OS << "__FUNCTION__";
545 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000546 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000547 OS << "__PRETTY_FUNCTION__";
548 break;
549 }
550}
551
Steve Naroffae4143e2007-04-26 20:39:23 +0000552void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000553 unsigned value = Node->getValue();
Chris Lattnera5678cc2008-06-07 22:35:38 +0000554 if (Node->isWide())
555 OS << "L";
Chris Lattner666115c2007-07-13 23:58:20 +0000556 switch (value) {
557 case '\\':
558 OS << "'\\\\'";
559 break;
560 case '\'':
561 OS << "'\\''";
562 break;
563 case '\a':
564 // TODO: K&R: the meaning of '\\a' is different in traditional C
565 OS << "'\\a'";
566 break;
567 case '\b':
568 OS << "'\\b'";
569 break;
570 // Nonstandard escape sequence.
571 /*case '\e':
572 OS << "'\\e'";
573 break;*/
574 case '\f':
575 OS << "'\\f'";
576 break;
577 case '\n':
578 OS << "'\\n'";
579 break;
580 case '\r':
581 OS << "'\\r'";
582 break;
583 case '\t':
584 OS << "'\\t'";
585 break;
586 case '\v':
587 OS << "'\\v'";
588 break;
589 default:
Ted Kremenek652d18e2008-02-23 00:52:04 +0000590 if (value < 256 && isprint(value)) {
Chris Lattner666115c2007-07-13 23:58:20 +0000591 OS << "'" << (char)value << "'";
592 } else if (value < 256) {
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000593 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner666115c2007-07-13 23:58:20 +0000594 } else {
595 // FIXME what to really do here?
596 OS << value;
597 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000598 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000599}
600
Steve Naroffdf7855b2007-02-21 23:46:25 +0000601void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000602 bool isSigned = Node->getType()->isSignedIntegerType();
603 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000604
Chris Lattner06430412007-05-21 05:45:03 +0000605 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +0000606 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
Chris Lattner06430412007-05-21 05:45:03 +0000607 default: assert(0 && "Unexpected type for integer literal!");
608 case BuiltinType::Int: break; // no suffix.
609 case BuiltinType::UInt: OS << 'U'; break;
610 case BuiltinType::Long: OS << 'L'; break;
611 case BuiltinType::ULong: OS << "UL"; break;
612 case BuiltinType::LongLong: OS << "LL"; break;
613 case BuiltinType::ULongLong: OS << "ULL"; break;
614 }
Chris Lattner882f7882006-11-04 18:52:07 +0000615}
Steve Naroffab624882007-02-21 22:05:47 +0000616void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattnerbf2f3862007-08-01 00:23:58 +0000617 // FIXME: print value more precisely.
Chris Lattnera0173132008-06-07 22:13:43 +0000618 OS << Node->getValueAsApproximateDouble();
Chris Lattner882f7882006-11-04 18:52:07 +0000619}
Chris Lattner1c20a172007-08-26 03:42:43 +0000620
621void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
622 PrintExpr(Node->getSubExpr());
623 OS << "i";
624}
625
Steve Naroffdf7855b2007-02-21 23:46:25 +0000626void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner882f7882006-11-04 18:52:07 +0000627 if (Str->isWide()) OS << 'L';
Chris Lattner5d8f4942006-11-04 20:29:31 +0000628 OS << '"';
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000629
Chris Lattner5d8f4942006-11-04 20:29:31 +0000630 // FIXME: this doesn't print wstrings right.
Benjamin Kramer35b077e2010-08-17 12:54:38 +0000631 llvm::StringRef StrData = Str->getString();
632 for (llvm::StringRef::iterator I = StrData.begin(), E = StrData.end();
633 I != E; ++I) {
634 unsigned char Char = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000635
Chris Lattnerbb96cc42009-01-16 19:25:18 +0000636 switch (Char) {
637 default:
638 if (isprint(Char))
639 OS << (char)Char;
640 else // Output anything hard as an octal escape.
641 OS << '\\'
642 << (char)('0'+ ((Char >> 6) & 7))
643 << (char)('0'+ ((Char >> 3) & 7))
644 << (char)('0'+ ((Char >> 0) & 7));
645 break;
646 // Handle some common non-printable cases to make dumps prettier.
Chris Lattner5d8f4942006-11-04 20:29:31 +0000647 case '\\': OS << "\\\\"; break;
648 case '"': OS << "\\\""; break;
649 case '\n': OS << "\\n"; break;
650 case '\t': OS << "\\t"; break;
651 case '\a': OS << "\\a"; break;
652 case '\b': OS << "\\b"; break;
653 }
654 }
655 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000656}
657void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
658 OS << "(";
659 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000660 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000661}
662void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000663 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000664 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +0000665
Eli Friedman1cf25362009-06-14 22:39:26 +0000666 // Print a space if this is an "identifier operator" like __real, or if
667 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +0000668 switch (Node->getOpcode()) {
669 default: break;
John McCalle3027922010-08-25 11:45:40 +0000670 case UO_Real:
671 case UO_Imag:
672 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +0000673 OS << ' ';
674 break;
John McCalle3027922010-08-25 11:45:40 +0000675 case UO_Plus:
676 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +0000677 if (isa<UnaryOperator>(Node->getSubExpr()))
678 OS << ' ';
679 break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000680 }
681 }
Chris Lattner882f7882006-11-04 18:52:07 +0000682 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000683
Chris Lattner15768702006-11-05 23:54:51 +0000684 if (Node->isPostfix())
685 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000686}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000687
Douglas Gregor882211c2010-04-28 22:16:22 +0000688void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
689 OS << "__builtin_offsetof(";
Daniel Dunbar219fa692010-06-30 19:16:48 +0000690 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +0000691 bool PrintedSomething = false;
692 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
693 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
694 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
695 // Array node
696 OS << "[";
697 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
698 OS << "]";
699 PrintedSomething = true;
700 continue;
701 }
Douglas Gregord1702062010-04-29 00:18:15 +0000702
703 // Skip implicit base indirections.
704 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
705 continue;
706
Douglas Gregor882211c2010-04-28 22:16:22 +0000707 // Field or identifier node.
708 IdentifierInfo *Id = ON.getFieldName();
709 if (!Id)
710 continue;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000711
Douglas Gregor882211c2010-04-28 22:16:22 +0000712 if (PrintedSomething)
713 OS << ".";
714 else
715 PrintedSomething = true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000716 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +0000717 }
718 OS << ")";
719}
720
Peter Collingbournee190dee2011-03-11 19:24:49 +0000721void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
722 switch(Node->getKind()) {
723 case UETT_SizeOf:
724 OS << "sizeof";
725 break;
726 case UETT_AlignOf:
727 OS << "__alignof";
728 break;
729 case UETT_VecStep:
730 OS << "vec_step";
731 break;
732 }
Sebastian Redl6f282892008-11-11 17:56:53 +0000733 if (Node->isArgumentType())
Daniel Dunbar219fa692010-06-30 19:16:48 +0000734 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")";
Sebastian Redl6f282892008-11-11 17:56:53 +0000735 else {
736 OS << " ";
737 PrintExpr(Node->getArgumentExpr());
738 }
Chris Lattner882f7882006-11-04 18:52:07 +0000739}
740void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000741 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000742 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000743 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000744 OS << "]";
745}
746
Peter Collingbourne4b279a02011-02-08 21:17:54 +0000747void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Chris Lattner882f7882006-11-04 18:52:07 +0000748 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000749 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
750 // Don't print any defaulted arguments
751 break;
752 }
753
Chris Lattner882f7882006-11-04 18:52:07 +0000754 if (i) OS << ", ";
755 PrintExpr(Call->getArg(i));
756 }
Peter Collingbourne4b279a02011-02-08 21:17:54 +0000757}
758
759void StmtPrinter::VisitCallExpr(CallExpr *Call) {
760 PrintExpr(Call->getCallee());
761 OS << "(";
762 PrintCallArgs(Call);
Chris Lattner882f7882006-11-04 18:52:07 +0000763 OS << ")";
764}
765void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +0000766 // FIXME: Suppress printing implicit bases (like "this")
767 PrintExpr(Node->getBase());
Fariborz Jahanian2990c022010-01-11 21:17:32 +0000768 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
769 if (FD->isAnonymousStructOrUnion())
770 return;
Douglas Gregore4b414c2009-01-08 22:45:41 +0000771 OS << (Node->isArrow() ? "->" : ".");
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000772 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
773 Qualifier->print(OS, Policy);
774
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000775 OS << Node->getMemberNameInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000776
John McCallb3774b52010-08-19 23:49:38 +0000777 if (Node->hasExplicitTemplateArgs())
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000778 OS << TemplateSpecializationType::PrintTemplateArgumentList(
779 Node->getTemplateArgs(),
780 Node->getNumTemplateArgs(),
781 Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000782}
Steve Naroffe87026a2009-07-24 17:54:45 +0000783void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
784 PrintExpr(Node->getBase());
785 OS << (Node->isArrow() ? "->isa" : ".isa");
786}
787
Nate Begemance4d7fc2008-04-18 23:10:10 +0000788void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000789 PrintExpr(Node->getBase());
790 OS << ".";
791 OS << Node->getAccessor().getName();
792}
Douglas Gregorf19b2312008-10-28 15:36:24 +0000793void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Fariborz Jahaniane33c1162010-06-30 16:31:08 +0000794 OS << "(" << Node->getType().getAsString(Policy) << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000795 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000796}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000797void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +0000798 OS << "(" << Node->getType().getAsString(Policy) << ")";
Steve Naroff57eb2c52007-07-19 21:32:11 +0000799 PrintExpr(Node->getInitializer());
800}
Steve Naroff7a5af782007-07-13 16:58:59 +0000801void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000802 // No need to print anything, simply forward to the sub expression.
803 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000804}
Chris Lattner882f7882006-11-04 18:52:07 +0000805void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
806 PrintExpr(Node->getLHS());
807 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
808 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000809}
Chris Lattner86928112007-08-25 02:00:02 +0000810void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
811 PrintExpr(Node->getLHS());
812 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
813 PrintExpr(Node->getRHS());
814}
Chris Lattner882f7882006-11-04 18:52:07 +0000815void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
816 PrintExpr(Node->getCond());
John McCallc07a0c72011-02-17 10:25:35 +0000817 OS << " ? ";
818 PrintExpr(Node->getLHS());
819 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +0000820 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000821}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000822
Chris Lattnereefa10e2007-05-28 06:56:27 +0000823// GNU extensions.
824
John McCallc07a0c72011-02-17 10:25:35 +0000825void
826StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
827 PrintExpr(Node->getCommon());
828 OS << " ?: ";
829 PrintExpr(Node->getFalseExpr());
830}
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000831void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000832 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000833}
834
Chris Lattner366727f2007-07-24 16:58:17 +0000835void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
836 OS << "(";
837 PrintRawCompoundStmt(E->getSubStmt());
838 OS << ")";
839}
840
Steve Naroff9efdabc2007-08-03 21:21:27 +0000841void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
842 OS << "__builtin_choose_expr(";
843 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000844 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000845 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000846 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000847 PrintExpr(Node->getRHS());
848 OS << ")";
849}
Chris Lattner366727f2007-07-24 16:58:17 +0000850
Douglas Gregor3be4b122008-11-29 04:51:27 +0000851void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
852 OS << "__null";
853}
854
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000855void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
856 OS << "__builtin_shufflevector(";
857 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
858 if (i) OS << ", ";
859 PrintExpr(Node->getExpr(i));
860 }
861 OS << ")";
862}
863
Anders Carlsson4692db02007-08-31 04:56:16 +0000864void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +0000865 if (Node->getSyntacticForm()) {
866 Visit(Node->getSyntacticForm());
867 return;
868 }
869
Anders Carlsson4692db02007-08-31 04:56:16 +0000870 OS << "{ ";
871 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
872 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000873 if (Node->getInit(i))
874 PrintExpr(Node->getInit(i));
875 else
876 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +0000877 }
878 OS << " }";
879}
880
Nate Begeman5ec4b312009-08-10 23:49:36 +0000881void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
882 OS << "( ";
883 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
884 if (i) OS << ", ";
885 PrintExpr(Node->getExpr(i));
886 }
887 OS << " )";
888}
889
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000890void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000891 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
892 DEnd = Node->designators_end();
893 D != DEnd; ++D) {
894 if (D->isFieldDesignator()) {
895 if (D->getDotLoc().isInvalid())
896 OS << D->getFieldName()->getName() << ":";
897 else
898 OS << "." << D->getFieldName()->getName();
899 } else {
900 OS << "[";
901 if (D->isArrayDesignator()) {
902 PrintExpr(Node->getArrayIndex(*D));
903 } else {
904 PrintExpr(Node->getArrayRangeStart(*D));
905 OS << " ... ";
Mike Stump11289f42009-09-09 15:08:12 +0000906 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000907 }
908 OS << "]";
909 }
910 }
911
912 OS << " = ";
913 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000914}
915
Douglas Gregor0202cb42009-01-29 17:44:32 +0000916void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Chris Lattnerc61089a2009-06-30 01:26:17 +0000917 if (Policy.LangOpts.CPlusPlus)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000918 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
919 else {
920 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
921 if (Node->getType()->isRecordType())
922 OS << "{}";
923 else
924 OS << 0;
925 }
Douglas Gregor0202cb42009-01-29 17:44:32 +0000926}
927
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000928void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +0000929 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000930 PrintExpr(Node->getSubExpr());
931 OS << ", ";
Daniel Dunbar219fa692010-06-30 19:16:48 +0000932 OS << Node->getType().getAsString(Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000933 OS << ")";
934}
935
Chris Lattnereefa10e2007-05-28 06:56:27 +0000936// C++
Douglas Gregor993603d2008-11-14 16:09:21 +0000937void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
938 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
939 "",
940#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
941 Spelling,
942#include "clang/Basic/OperatorKinds.def"
943 };
944
945 OverloadedOperatorKind Kind = Node->getOperator();
946 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
947 if (Node->getNumArgs() == 1) {
948 OS << OpStrings[Kind] << ' ';
949 PrintExpr(Node->getArg(0));
950 } else {
951 PrintExpr(Node->getArg(0));
952 OS << ' ' << OpStrings[Kind];
953 }
954 } else if (Kind == OO_Call) {
955 PrintExpr(Node->getArg(0));
956 OS << '(';
957 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
958 if (ArgIdx > 1)
959 OS << ", ";
960 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
961 PrintExpr(Node->getArg(ArgIdx));
962 }
963 OS << ')';
964 } else if (Kind == OO_Subscript) {
965 PrintExpr(Node->getArg(0));
966 OS << '[';
967 PrintExpr(Node->getArg(1));
968 OS << ']';
969 } else if (Node->getNumArgs() == 1) {
970 OS << OpStrings[Kind] << ' ';
971 PrintExpr(Node->getArg(0));
972 } else if (Node->getNumArgs() == 2) {
973 PrintExpr(Node->getArg(0));
974 OS << ' ' << OpStrings[Kind] << ' ';
975 PrintExpr(Node->getArg(1));
976 } else {
977 assert(false && "unknown overloaded operator");
978 }
979}
Chris Lattnereefa10e2007-05-28 06:56:27 +0000980
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000981void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
982 VisitCallExpr(cast<CallExpr>(Node));
983}
984
Peter Collingbourne41f85462011-02-09 21:07:24 +0000985void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
986 PrintExpr(Node->getCallee());
987 OS << "<<<";
988 PrintCallArgs(Node->getConfig());
989 OS << ">>>(";
990 PrintCallArgs(Node);
991 OS << ")";
992}
993
Douglas Gregore200adc2008-10-27 19:41:14 +0000994void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
995 OS << Node->getCastName() << '<';
Daniel Dunbar219fa692010-06-30 19:16:48 +0000996 OS << Node->getTypeAsWritten().getAsString(Policy) << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +0000997 PrintExpr(Node->getSubExpr());
998 OS << ")";
999}
1000
Douglas Gregore200adc2008-10-27 19:41:14 +00001001void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1002 VisitCXXNamedCastExpr(Node);
1003}
1004
1005void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1006 VisitCXXNamedCastExpr(Node);
1007}
1008
1009void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1010 VisitCXXNamedCastExpr(Node);
1011}
1012
1013void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1014 VisitCXXNamedCastExpr(Node);
1015}
1016
Sebastian Redlc4704762008-11-11 11:37:55 +00001017void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1018 OS << "typeid(";
1019 if (Node->isTypeOperand()) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001020 OS << Node->getTypeOperand().getAsString(Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00001021 } else {
1022 PrintExpr(Node->getExprOperand());
1023 }
1024 OS << ")";
1025}
1026
Francois Pichet9f4f2072010-09-08 12:20:18 +00001027void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1028 OS << "__uuidof(";
1029 if (Node->isTypeOperand()) {
1030 OS << Node->getTypeOperand().getAsString(Policy);
1031 } else {
1032 PrintExpr(Node->getExprOperand());
1033 }
1034 OS << ")";
1035}
1036
Chris Lattnereefa10e2007-05-28 06:56:27 +00001037void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1038 OS << (Node->getValue() ? "true" : "false");
1039}
1040
Sebastian Redl576fd422009-05-10 18:38:11 +00001041void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1042 OS << "nullptr";
1043}
1044
Douglas Gregor97a9c812008-11-04 14:32:21 +00001045void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1046 OS << "this";
1047}
1048
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001049void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1050 if (Node->getSubExpr() == 0)
1051 OS << "throw";
1052 else {
1053 OS << "throw ";
1054 PrintExpr(Node->getSubExpr());
1055 }
1056}
1057
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001058void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1059 // Nothing to print: we picked up the default argument
1060}
1061
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001062void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001063 OS << Node->getType().getAsString(Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001064 OS << "(";
1065 PrintExpr(Node->getSubExpr());
1066 OS << ")";
1067}
1068
Anders Carlsson993a4b32009-05-30 20:03:25 +00001069void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1070 PrintExpr(Node->getSubExpr());
1071}
1072
Douglas Gregordd04d332009-01-16 18:33:17 +00001073void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001074 OS << Node->getType().getAsString(Policy);
Douglas Gregordd04d332009-01-16 18:33:17 +00001075 OS << "(";
1076 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001077 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001078 Arg != ArgEnd; ++Arg) {
1079 if (Arg != Node->arg_begin())
1080 OS << ", ";
1081 PrintExpr(*Arg);
1082 }
1083 OS << ")";
1084}
1085
Douglas Gregor747eb782010-07-08 06:14:04 +00001086void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00001087 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1088 OS << TSInfo->getType().getAsString(Policy) << "()";
1089 else
1090 OS << Node->getType().getAsString(Policy) << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001091}
1092
Sebastian Redlbd150f42008-11-21 19:14:01 +00001093void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1094 if (E->isGlobalNew())
1095 OS << "::";
1096 OS << "new ";
1097 unsigned NumPlace = E->getNumPlacementArgs();
1098 if (NumPlace > 0) {
1099 OS << "(";
1100 PrintExpr(E->getPlacementArg(0));
1101 for (unsigned i = 1; i < NumPlace; ++i) {
1102 OS << ", ";
1103 PrintExpr(E->getPlacementArg(i));
1104 }
1105 OS << ") ";
1106 }
1107 if (E->isParenTypeId())
1108 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001109 std::string TypeS;
1110 if (Expr *Size = E->getArraySize()) {
1111 llvm::raw_string_ostream s(TypeS);
Eli Friedmanc4fc8392009-05-30 05:32:46 +00001112 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001113 s.flush();
1114 TypeS = "[" + TypeS + "]";
1115 }
Douglas Gregor7de59662009-05-29 20:38:28 +00001116 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001117 OS << TypeS;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001118 if (E->isParenTypeId())
1119 OS << ")";
1120
1121 if (E->hasInitializer()) {
1122 OS << "(";
1123 unsigned NumCons = E->getNumConstructorArgs();
1124 if (NumCons > 0) {
1125 PrintExpr(E->getConstructorArg(0));
1126 for (unsigned i = 1; i < NumCons; ++i) {
1127 OS << ", ";
1128 PrintExpr(E->getConstructorArg(i));
1129 }
1130 }
1131 OS << ")";
1132 }
1133}
1134
1135void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1136 if (E->isGlobalDelete())
1137 OS << "::";
1138 OS << "delete ";
1139 if (E->isArrayForm())
1140 OS << "[] ";
1141 PrintExpr(E->getArgument());
1142}
1143
Douglas Gregorad8a3362009-09-04 17:36:40 +00001144void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1145 PrintExpr(E->getBase());
1146 if (E->isArrow())
1147 OS << "->";
1148 else
1149 OS << '.';
1150 if (E->getQualifier())
1151 E->getQualifier()->print(OS, Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001152
Douglas Gregorad8a3362009-09-04 17:36:40 +00001153 std::string TypeS;
Douglas Gregor678f90d2010-02-25 01:56:36 +00001154 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1155 OS << II->getName();
1156 else
1157 E->getDestroyedType().getAsStringInternal(TypeS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00001158 OS << TypeS;
1159}
1160
Anders Carlsson0781ce72009-04-23 02:32:43 +00001161void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Douglas Gregorbaba85d2011-01-24 17:25:03 +00001162 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1163 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1164 // Don't print any defaulted arguments
1165 break;
1166 }
1167
1168 if (i) OS << ", ";
1169 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00001170 }
Anders Carlsson0781ce72009-04-23 02:32:43 +00001171}
1172
John McCall5d413782010-12-06 08:20:24 +00001173void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Anders Carlssondefc6442009-04-24 22:47:04 +00001174 // Just forward to the sub expression.
1175 PrintExpr(E->getSubExpr());
1176}
1177
Mike Stump11289f42009-09-09 15:08:12 +00001178void
Douglas Gregorce934142009-05-20 18:46:25 +00001179StmtPrinter::VisitCXXUnresolvedConstructExpr(
1180 CXXUnresolvedConstructExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001181 OS << Node->getTypeAsWritten().getAsString(Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00001182 OS << "(";
1183 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001184 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00001185 Arg != ArgEnd; ++Arg) {
1186 if (Arg != Node->arg_begin())
1187 OS << ", ";
1188 PrintExpr(*Arg);
1189 }
1190 OS << ")";
1191}
1192
John McCall8cd78132009-11-19 22:55:06 +00001193void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1194 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001195 if (!Node->isImplicitAccess()) {
1196 PrintExpr(Node->getBase());
1197 OS << (Node->isArrow() ? "->" : ".");
1198 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00001199 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1200 Qualifier->print(OS, Policy);
John McCall2d74de92009-12-01 22:10:20 +00001201 else if (Node->hasExplicitTemplateArgs())
Douglas Gregor308047d2009-09-09 00:23:06 +00001202 // FIXME: Track use of "template" keyword explicitly?
1203 OS << "template ";
Mike Stump11289f42009-09-09 15:08:12 +00001204
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001205 OS << Node->getMemberNameInfo();
Mike Stump11289f42009-09-09 15:08:12 +00001206
John McCall2d74de92009-12-01 22:10:20 +00001207 if (Node->hasExplicitTemplateArgs()) {
Douglas Gregor308047d2009-09-09 00:23:06 +00001208 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1209 Node->getTemplateArgs(),
1210 Node->getNumTemplateArgs(),
1211 Policy);
1212 }
Douglas Gregora8db9542009-05-22 21:13:27 +00001213}
1214
John McCall10eae182009-11-30 22:42:35 +00001215void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001216 if (!Node->isImplicitAccess()) {
1217 PrintExpr(Node->getBase());
1218 OS << (Node->isArrow() ? "->" : ".");
1219 }
John McCall10eae182009-11-30 22:42:35 +00001220 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1221 Qualifier->print(OS, Policy);
1222
1223 // FIXME: this might originally have been written with 'template'
1224
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001225 OS << Node->getMemberNameInfo();
John McCall10eae182009-11-30 22:42:35 +00001226
1227 if (Node->hasExplicitTemplateArgs()) {
1228 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1229 Node->getTemplateArgs(),
1230 Node->getNumTemplateArgs(),
1231 Policy);
1232 }
1233}
1234
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001235static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1236 switch (UTT) {
Francois Pichet347c4c72010-12-07 00:55:57 +00001237 default: llvm_unreachable("Unknown unary type trait");
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001238 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1239 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1240 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1241 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1242 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1243 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1244 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1245 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1246 case UTT_IsAbstract: return "__is_abstract";
1247 case UTT_IsClass: return "__is_class";
1248 case UTT_IsEmpty: return "__is_empty";
1249 case UTT_IsEnum: return "__is_enum";
1250 case UTT_IsPOD: return "__is_pod";
1251 case UTT_IsPolymorphic: return "__is_polymorphic";
1252 case UTT_IsUnion: return "__is_union";
1253 }
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001254 return "";
1255}
1256
1257static const char *getTypeTraitName(BinaryTypeTrait BTT) {
1258 switch (BTT) {
Francois Pichet34b21132010-12-08 22:35:30 +00001259 case BTT_IsBaseOf: return "__is_base_of";
1260 case BTT_TypeCompatible: return "__builtin_types_compatible_p";
Douglas Gregor8006e762011-01-27 20:28:01 +00001261 case BTT_IsConvertibleTo: return "__is_convertible_to";
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001262 }
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001263 return "";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001264}
1265
1266void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1267 OS << getTypeTraitName(E->getTrait()) << "("
Daniel Dunbar219fa692010-06-30 19:16:48 +00001268 << E->getQueriedType().getAsString(Policy) << ")";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001269}
1270
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001271void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1272 OS << getTypeTraitName(E->getTrait()) << "("
1273 << E->getLhsType().getAsString(Policy) << ","
1274 << E->getRhsType().getAsString(Policy) << ")";
1275}
1276
Sebastian Redl4202c0f2010-09-10 20:55:43 +00001277void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1278 OS << "noexcept(";
1279 PrintExpr(E->getOperand());
1280 OS << ")";
1281}
1282
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001283void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00001284 PrintExpr(E->getPattern());
1285 OS << "...";
1286}
1287
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001288void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
1289 OS << "sizeof...(" << E->getPack()->getNameAsString() << ")";
1290}
1291
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001292void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1293 SubstNonTypeTemplateParmPackExpr *Node) {
1294 OS << Node->getParameterPack()->getNameAsString();
1295}
1296
Mike Stump11289f42009-09-09 15:08:12 +00001297// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00001298
1299void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1300 OS << "@";
1301 VisitStringLiteral(Node->getString());
1302}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001303
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001304void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001305 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001306}
1307
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001308void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001309 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001310}
1311
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001312void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001313 OS << "@protocol(" << Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001314}
1315
Steve Naroffd54978b2007-09-18 23:55:05 +00001316void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1317 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00001318 switch (Mess->getReceiverKind()) {
1319 case ObjCMessageExpr::Instance:
1320 PrintExpr(Mess->getInstanceReceiver());
1321 break;
1322
1323 case ObjCMessageExpr::Class:
1324 OS << Mess->getClassReceiver().getAsString(Policy);
1325 break;
1326
1327 case ObjCMessageExpr::SuperInstance:
1328 case ObjCMessageExpr::SuperClass:
1329 OS << "Super";
1330 break;
1331 }
1332
Ted Kremeneka06e7122008-05-02 17:32:38 +00001333 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001334 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001335 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00001336 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00001337 } else {
1338 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001339 if (i < selector.getNumArgs()) {
1340 if (i > 0) OS << ' ';
1341 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001342 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001343 else
1344 OS << ":";
1345 }
1346 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00001347
Steve Naroffc6814ea2007-10-02 20:01:56 +00001348 PrintExpr(Mess->getArg(i));
1349 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001350 }
1351 OS << "]";
1352}
1353
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001354
Steve Naroffc540d662008-09-03 18:15:37 +00001355void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001356 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001357 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00001358
Steve Naroffc540d662008-09-03 18:15:37 +00001359 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001360
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001361 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00001362 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001363 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001364 OS << '(';
1365 std::string ParamStr;
Steve Naroff415d3d52008-10-08 17:01:13 +00001366 for (BlockDecl::param_iterator AI = BD->param_begin(),
1367 E = BD->param_end(); AI != E; ++AI) {
1368 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001369 ParamStr = (*AI)->getNameAsString();
Douglas Gregor7de59662009-05-29 20:38:28 +00001370 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroffc540d662008-09-03 18:15:37 +00001371 OS << ParamStr;
1372 }
Mike Stump11289f42009-09-09 15:08:12 +00001373
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001374 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001375 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001376 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001377 OS << "...";
1378 }
1379 OS << ')';
1380 }
1381}
1382
Steve Naroffc540d662008-09-03 18:15:37 +00001383void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001384 OS << Node->getDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001385}
John McCall8d69a212010-11-15 23:31:06 +00001386
1387void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {}
1388
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001389//===----------------------------------------------------------------------===//
1390// Stmt method implementations
1391//===----------------------------------------------------------------------===//
1392
Eli Friedmanef334fd2009-05-30 05:03:24 +00001393void Stmt::dumpPretty(ASTContext& Context) const {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00001394 printPretty(llvm::errs(), Context, 0,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001395 PrintingPolicy(Context.getLangOptions()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001396}
1397
Eli Friedmanef334fd2009-05-30 05:03:24 +00001398void Stmt::printPretty(llvm::raw_ostream &OS, ASTContext& Context,
1399 PrinterHelper* Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00001400 const PrintingPolicy &Policy,
1401 unsigned Indentation) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001402 if (this == 0) {
1403 OS << "<NULL>";
1404 return;
1405 }
1406
Douglas Gregor8655e882009-10-16 22:46:09 +00001407 if (Policy.Dump && &Context) {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00001408 dump(OS, Context.getSourceManager());
Douglas Gregor278f52e2009-05-30 00:08:05 +00001409 return;
1410 }
Mike Stump11289f42009-09-09 15:08:12 +00001411
Eli Friedmanef334fd2009-05-30 05:03:24 +00001412 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00001413 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001414}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001415
1416//===----------------------------------------------------------------------===//
1417// PrinterHelper
1418//===----------------------------------------------------------------------===//
1419
1420// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001421PrinterHelper::~PrinterHelper() {}