blob: 4f500441166d5bddbee327f4c8e60f1b24902023 [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
Chris Lattner16976d32006-11-05 01:46:01 +0000294void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000295 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000296}
297
298void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000299 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000300 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000301 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000302}
303
304void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000305 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000306}
307
308void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000309 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000310}
311
312
Chris Lattner882f7882006-11-04 18:52:07 +0000313void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
314 Indent() << "return";
315 if (Node->getRetValue()) {
316 OS << " ";
317 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000318 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000319 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000320}
321
Chris Lattner73c56c02007-10-29 04:04:16 +0000322
323void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000324 Indent() << "asm ";
Mike Stump11289f42009-09-09 15:08:12 +0000325
Anders Carlsson660bdd12007-11-23 23:12:25 +0000326 if (Node->isVolatile())
327 OS << "volatile ";
Mike Stump11289f42009-09-09 15:08:12 +0000328
Anders Carlsson660bdd12007-11-23 23:12:25 +0000329 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000330 VisitStringLiteral(Node->getAsmString());
Mike Stump11289f42009-09-09 15:08:12 +0000331
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000332 // Outputs
333 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
334 Node->getNumClobbers() != 0)
335 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000336
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000337 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
338 if (i != 0)
339 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000340
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000341 if (!Node->getOutputName(i).empty()) {
342 OS << '[';
343 OS << Node->getOutputName(i);
344 OS << "] ";
345 }
Mike Stump11289f42009-09-09 15:08:12 +0000346
Chris Lattner72bbf172009-03-10 04:59:06 +0000347 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000348 OS << " ";
349 Visit(Node->getOutputExpr(i));
350 }
Mike Stump11289f42009-09-09 15:08:12 +0000351
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000352 // Inputs
353 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
354 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000355
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000356 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
357 if (i != 0)
358 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000359
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000360 if (!Node->getInputName(i).empty()) {
361 OS << '[';
362 OS << Node->getInputName(i);
363 OS << "] ";
364 }
Mike Stump11289f42009-09-09 15:08:12 +0000365
Chris Lattner72bbf172009-03-10 04:59:06 +0000366 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000367 OS << " ";
368 Visit(Node->getInputExpr(i));
369 }
Mike Stump11289f42009-09-09 15:08:12 +0000370
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000371 // Clobbers
372 if (Node->getNumClobbers() != 0)
373 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000374
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000375 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
376 if (i != 0)
377 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000378
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000379 VisitStringLiteral(Node->getClobber(i));
380 }
Mike Stump11289f42009-09-09 15:08:12 +0000381
Anders Carlsson81a5a692007-11-20 19:21:03 +0000382 OS << ");\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000383}
384
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000385void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000386 Indent() << "@try";
387 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
388 PrintRawCompoundStmt(TS);
389 OS << "\n";
390 }
Mike Stump11289f42009-09-09 15:08:12 +0000391
Douglas Gregor96c79492010-04-23 22:50:49 +0000392 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
393 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000394 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000395 if (catchStmt->getCatchParamDecl()) {
396 if (Decl *DS = catchStmt->getCatchParamDecl())
397 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000398 }
399 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000400 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
401 PrintRawCompoundStmt(CS);
402 OS << "\n";
403 }
Fariborz Jahanian88157952007-11-02 18:16:07 +0000404 }
Mike Stump11289f42009-09-09 15:08:12 +0000405
406 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
407 Node->getFinallyStmt())) {
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000408 Indent() << "@finally";
409 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000410 OS << "\n";
Mike Stump11289f42009-09-09 15:08:12 +0000411 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000412}
413
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000414void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000415}
416
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000417void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000418 Indent() << "@catch (...) { /* todo */ } \n";
419}
420
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000421void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000422 Indent() << "@throw";
423 if (Node->getThrowExpr()) {
424 OS << " ";
425 PrintExpr(Node->getThrowExpr());
426 }
427 OS << ";\n";
428}
429
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000430void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000431 Indent() << "@synchronized (";
432 PrintExpr(Node->getSynchExpr());
433 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000434 PrintRawCompoundStmt(Node->getSynchBody());
435 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000436}
437
Sebastian Redl9b244a82008-12-22 21:35:02 +0000438void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
439 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000440 if (Decl *ExDecl = Node->getExceptionDecl())
441 PrintRawDecl(ExDecl);
442 else
443 OS << "...";
444 OS << ") ";
445 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000446}
447
448void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
449 Indent();
450 PrintRawCXXCatchStmt(Node);
451 OS << "\n";
452}
453
454void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
455 Indent() << "try ";
456 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000457 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000458 OS << " ";
459 PrintRawCXXCatchStmt(Node->getHandler(i));
460 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000461 OS << "\n";
462}
463
Chris Lattner71e23ce2006-11-04 20:18:38 +0000464//===----------------------------------------------------------------------===//
465// Expr printing methods.
466//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000467
Chris Lattner882f7882006-11-04 18:52:07 +0000468void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000469 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
470 Qualifier->print(OS, Policy);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000471 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000472 if (Node->hasExplicitTemplateArgs())
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000473 OS << TemplateSpecializationType::PrintTemplateArgumentList(
474 Node->getTemplateArgs(),
475 Node->getNumTemplateArgs(),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000476 Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000477}
478
John McCall8cd78132009-11-19 22:55:06 +0000479void StmtPrinter::VisitDependentScopeDeclRefExpr(
480 DependentScopeDeclRefExpr *Node) {
Axel Naumann20b27862011-01-24 15:44:00 +0000481 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
482 Qualifier->print(OS, Policy);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000483 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000484 if (Node->hasExplicitTemplateArgs())
485 OS << TemplateSpecializationType::PrintTemplateArgumentList(
486 Node->getTemplateArgs(),
487 Node->getNumTemplateArgs(),
488 Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000489}
490
John McCalld14a8642009-11-21 08:51:07 +0000491void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +0000492 if (Node->getQualifier())
493 Node->getQualifier()->print(OS, Policy);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000494 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000495 if (Node->hasExplicitTemplateArgs())
496 OS << TemplateSpecializationType::PrintTemplateArgumentList(
497 Node->getTemplateArgs(),
Douglas Gregora727cb92009-06-30 22:34:41 +0000498 Node->getNumTemplateArgs(),
John McCalle66edc12009-11-24 19:00:30 +0000499 Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +0000500}
501
Steve Naroffe46504b2007-11-12 14:29:37 +0000502void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000503 if (Node->getBase()) {
504 PrintExpr(Node->getBase());
505 OS << (Node->isArrow() ? "->" : ".");
506 }
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000507 OS << Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +0000508}
509
Steve Naroffec944032008-05-30 00:40:33 +0000510void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000511 if (Node->isSuperReceiver())
512 OS << "super.";
513 else if (Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +0000514 PrintExpr(Node->getBase());
515 OS << ".";
516 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000517
John McCallb7bd14f2010-12-02 01:19:52 +0000518 if (Node->isImplicitProperty())
519 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
520 else
521 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000522}
523
Chris Lattner6307f192008-08-10 01:53:14 +0000524void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000525 switch (Node->getIdentType()) {
526 default:
527 assert(0 && "unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000528 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000529 OS << "__func__";
530 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000531 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000532 OS << "__FUNCTION__";
533 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000534 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000535 OS << "__PRETTY_FUNCTION__";
536 break;
537 }
538}
539
Steve Naroffae4143e2007-04-26 20:39:23 +0000540void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000541 unsigned value = Node->getValue();
Chris Lattnera5678cc2008-06-07 22:35:38 +0000542 if (Node->isWide())
543 OS << "L";
Chris Lattner666115c2007-07-13 23:58:20 +0000544 switch (value) {
545 case '\\':
546 OS << "'\\\\'";
547 break;
548 case '\'':
549 OS << "'\\''";
550 break;
551 case '\a':
552 // TODO: K&R: the meaning of '\\a' is different in traditional C
553 OS << "'\\a'";
554 break;
555 case '\b':
556 OS << "'\\b'";
557 break;
558 // Nonstandard escape sequence.
559 /*case '\e':
560 OS << "'\\e'";
561 break;*/
562 case '\f':
563 OS << "'\\f'";
564 break;
565 case '\n':
566 OS << "'\\n'";
567 break;
568 case '\r':
569 OS << "'\\r'";
570 break;
571 case '\t':
572 OS << "'\\t'";
573 break;
574 case '\v':
575 OS << "'\\v'";
576 break;
577 default:
Ted Kremenek652d18e2008-02-23 00:52:04 +0000578 if (value < 256 && isprint(value)) {
Chris Lattner666115c2007-07-13 23:58:20 +0000579 OS << "'" << (char)value << "'";
580 } else if (value < 256) {
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000581 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner666115c2007-07-13 23:58:20 +0000582 } else {
583 // FIXME what to really do here?
584 OS << value;
585 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000586 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000587}
588
Steve Naroffdf7855b2007-02-21 23:46:25 +0000589void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000590 bool isSigned = Node->getType()->isSignedIntegerType();
591 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000592
Chris Lattner06430412007-05-21 05:45:03 +0000593 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +0000594 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
Chris Lattner06430412007-05-21 05:45:03 +0000595 default: assert(0 && "Unexpected type for integer literal!");
596 case BuiltinType::Int: break; // no suffix.
597 case BuiltinType::UInt: OS << 'U'; break;
598 case BuiltinType::Long: OS << 'L'; break;
599 case BuiltinType::ULong: OS << "UL"; break;
600 case BuiltinType::LongLong: OS << "LL"; break;
601 case BuiltinType::ULongLong: OS << "ULL"; break;
602 }
Chris Lattner882f7882006-11-04 18:52:07 +0000603}
Steve Naroffab624882007-02-21 22:05:47 +0000604void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattnerbf2f3862007-08-01 00:23:58 +0000605 // FIXME: print value more precisely.
Chris Lattnera0173132008-06-07 22:13:43 +0000606 OS << Node->getValueAsApproximateDouble();
Chris Lattner882f7882006-11-04 18:52:07 +0000607}
Chris Lattner1c20a172007-08-26 03:42:43 +0000608
609void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
610 PrintExpr(Node->getSubExpr());
611 OS << "i";
612}
613
Steve Naroffdf7855b2007-02-21 23:46:25 +0000614void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner882f7882006-11-04 18:52:07 +0000615 if (Str->isWide()) OS << 'L';
Chris Lattner5d8f4942006-11-04 20:29:31 +0000616 OS << '"';
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000617
Chris Lattner5d8f4942006-11-04 20:29:31 +0000618 // FIXME: this doesn't print wstrings right.
Benjamin Kramer35b077e2010-08-17 12:54:38 +0000619 llvm::StringRef StrData = Str->getString();
620 for (llvm::StringRef::iterator I = StrData.begin(), E = StrData.end();
621 I != E; ++I) {
622 unsigned char Char = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000623
Chris Lattnerbb96cc42009-01-16 19:25:18 +0000624 switch (Char) {
625 default:
626 if (isprint(Char))
627 OS << (char)Char;
628 else // Output anything hard as an octal escape.
629 OS << '\\'
630 << (char)('0'+ ((Char >> 6) & 7))
631 << (char)('0'+ ((Char >> 3) & 7))
632 << (char)('0'+ ((Char >> 0) & 7));
633 break;
634 // Handle some common non-printable cases to make dumps prettier.
Chris Lattner5d8f4942006-11-04 20:29:31 +0000635 case '\\': OS << "\\\\"; break;
636 case '"': OS << "\\\""; break;
637 case '\n': OS << "\\n"; break;
638 case '\t': OS << "\\t"; break;
639 case '\a': OS << "\\a"; break;
640 case '\b': OS << "\\b"; break;
641 }
642 }
643 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000644}
645void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
646 OS << "(";
647 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000648 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000649}
650void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000651 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000652 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +0000653
Eli Friedman1cf25362009-06-14 22:39:26 +0000654 // Print a space if this is an "identifier operator" like __real, or if
655 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +0000656 switch (Node->getOpcode()) {
657 default: break;
John McCalle3027922010-08-25 11:45:40 +0000658 case UO_Real:
659 case UO_Imag:
660 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +0000661 OS << ' ';
662 break;
John McCalle3027922010-08-25 11:45:40 +0000663 case UO_Plus:
664 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +0000665 if (isa<UnaryOperator>(Node->getSubExpr()))
666 OS << ' ';
667 break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000668 }
669 }
Chris Lattner882f7882006-11-04 18:52:07 +0000670 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000671
Chris Lattner15768702006-11-05 23:54:51 +0000672 if (Node->isPostfix())
673 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000674}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000675
Douglas Gregor882211c2010-04-28 22:16:22 +0000676void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
677 OS << "__builtin_offsetof(";
Daniel Dunbar219fa692010-06-30 19:16:48 +0000678 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +0000679 bool PrintedSomething = false;
680 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
681 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
682 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
683 // Array node
684 OS << "[";
685 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
686 OS << "]";
687 PrintedSomething = true;
688 continue;
689 }
Douglas Gregord1702062010-04-29 00:18:15 +0000690
691 // Skip implicit base indirections.
692 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
693 continue;
694
Douglas Gregor882211c2010-04-28 22:16:22 +0000695 // Field or identifier node.
696 IdentifierInfo *Id = ON.getFieldName();
697 if (!Id)
698 continue;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000699
Douglas Gregor882211c2010-04-28 22:16:22 +0000700 if (PrintedSomething)
701 OS << ".";
702 else
703 PrintedSomething = true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000704 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +0000705 }
706 OS << ")";
707}
708
Peter Collingbournee190dee2011-03-11 19:24:49 +0000709void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
710 switch(Node->getKind()) {
711 case UETT_SizeOf:
712 OS << "sizeof";
713 break;
714 case UETT_AlignOf:
715 OS << "__alignof";
716 break;
717 case UETT_VecStep:
718 OS << "vec_step";
719 break;
720 }
Sebastian Redl6f282892008-11-11 17:56:53 +0000721 if (Node->isArgumentType())
Daniel Dunbar219fa692010-06-30 19:16:48 +0000722 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")";
Sebastian Redl6f282892008-11-11 17:56:53 +0000723 else {
724 OS << " ";
725 PrintExpr(Node->getArgumentExpr());
726 }
Chris Lattner882f7882006-11-04 18:52:07 +0000727}
728void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000729 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000730 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000731 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000732 OS << "]";
733}
734
Peter Collingbourne4b279a02011-02-08 21:17:54 +0000735void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Chris Lattner882f7882006-11-04 18:52:07 +0000736 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000737 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
738 // Don't print any defaulted arguments
739 break;
740 }
741
Chris Lattner882f7882006-11-04 18:52:07 +0000742 if (i) OS << ", ";
743 PrintExpr(Call->getArg(i));
744 }
Peter Collingbourne4b279a02011-02-08 21:17:54 +0000745}
746
747void StmtPrinter::VisitCallExpr(CallExpr *Call) {
748 PrintExpr(Call->getCallee());
749 OS << "(";
750 PrintCallArgs(Call);
Chris Lattner882f7882006-11-04 18:52:07 +0000751 OS << ")";
752}
753void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +0000754 // FIXME: Suppress printing implicit bases (like "this")
755 PrintExpr(Node->getBase());
Fariborz Jahanian2990c022010-01-11 21:17:32 +0000756 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
757 if (FD->isAnonymousStructOrUnion())
758 return;
Douglas Gregore4b414c2009-01-08 22:45:41 +0000759 OS << (Node->isArrow() ? "->" : ".");
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000760 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
761 Qualifier->print(OS, Policy);
762
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000763 OS << Node->getMemberNameInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000764
John McCallb3774b52010-08-19 23:49:38 +0000765 if (Node->hasExplicitTemplateArgs())
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000766 OS << TemplateSpecializationType::PrintTemplateArgumentList(
767 Node->getTemplateArgs(),
768 Node->getNumTemplateArgs(),
769 Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000770}
Steve Naroffe87026a2009-07-24 17:54:45 +0000771void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
772 PrintExpr(Node->getBase());
773 OS << (Node->isArrow() ? "->isa" : ".isa");
774}
775
Nate Begemance4d7fc2008-04-18 23:10:10 +0000776void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000777 PrintExpr(Node->getBase());
778 OS << ".";
779 OS << Node->getAccessor().getName();
780}
Douglas Gregorf19b2312008-10-28 15:36:24 +0000781void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Fariborz Jahaniane33c1162010-06-30 16:31:08 +0000782 OS << "(" << Node->getType().getAsString(Policy) << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000783 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000784}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000785void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +0000786 OS << "(" << Node->getType().getAsString(Policy) << ")";
Steve Naroff57eb2c52007-07-19 21:32:11 +0000787 PrintExpr(Node->getInitializer());
788}
Steve Naroff7a5af782007-07-13 16:58:59 +0000789void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000790 // No need to print anything, simply forward to the sub expression.
791 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000792}
Chris Lattner882f7882006-11-04 18:52:07 +0000793void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
794 PrintExpr(Node->getLHS());
795 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
796 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000797}
Chris Lattner86928112007-08-25 02:00:02 +0000798void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
799 PrintExpr(Node->getLHS());
800 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
801 PrintExpr(Node->getRHS());
802}
Chris Lattner882f7882006-11-04 18:52:07 +0000803void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
804 PrintExpr(Node->getCond());
John McCallc07a0c72011-02-17 10:25:35 +0000805 OS << " ? ";
806 PrintExpr(Node->getLHS());
807 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +0000808 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000809}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000810
Chris Lattnereefa10e2007-05-28 06:56:27 +0000811// GNU extensions.
812
John McCallc07a0c72011-02-17 10:25:35 +0000813void
814StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
815 PrintExpr(Node->getCommon());
816 OS << " ?: ";
817 PrintExpr(Node->getFalseExpr());
818}
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000819void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000820 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000821}
822
Chris Lattner366727f2007-07-24 16:58:17 +0000823void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
824 OS << "(";
825 PrintRawCompoundStmt(E->getSubStmt());
826 OS << ")";
827}
828
Steve Naroff9efdabc2007-08-03 21:21:27 +0000829void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
830 OS << "__builtin_choose_expr(";
831 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000832 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000833 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000834 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000835 PrintExpr(Node->getRHS());
836 OS << ")";
837}
Chris Lattner366727f2007-07-24 16:58:17 +0000838
Douglas Gregor3be4b122008-11-29 04:51:27 +0000839void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
840 OS << "__null";
841}
842
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000843void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
844 OS << "__builtin_shufflevector(";
845 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
846 if (i) OS << ", ";
847 PrintExpr(Node->getExpr(i));
848 }
849 OS << ")";
850}
851
Anders Carlsson4692db02007-08-31 04:56:16 +0000852void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +0000853 if (Node->getSyntacticForm()) {
854 Visit(Node->getSyntacticForm());
855 return;
856 }
857
Anders Carlsson4692db02007-08-31 04:56:16 +0000858 OS << "{ ";
859 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
860 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000861 if (Node->getInit(i))
862 PrintExpr(Node->getInit(i));
863 else
864 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +0000865 }
866 OS << " }";
867}
868
Nate Begeman5ec4b312009-08-10 23:49:36 +0000869void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
870 OS << "( ";
871 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
872 if (i) OS << ", ";
873 PrintExpr(Node->getExpr(i));
874 }
875 OS << " )";
876}
877
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000878void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000879 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
880 DEnd = Node->designators_end();
881 D != DEnd; ++D) {
882 if (D->isFieldDesignator()) {
883 if (D->getDotLoc().isInvalid())
884 OS << D->getFieldName()->getName() << ":";
885 else
886 OS << "." << D->getFieldName()->getName();
887 } else {
888 OS << "[";
889 if (D->isArrayDesignator()) {
890 PrintExpr(Node->getArrayIndex(*D));
891 } else {
892 PrintExpr(Node->getArrayRangeStart(*D));
893 OS << " ... ";
Mike Stump11289f42009-09-09 15:08:12 +0000894 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000895 }
896 OS << "]";
897 }
898 }
899
900 OS << " = ";
901 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000902}
903
Douglas Gregor0202cb42009-01-29 17:44:32 +0000904void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Chris Lattnerc61089a2009-06-30 01:26:17 +0000905 if (Policy.LangOpts.CPlusPlus)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000906 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
907 else {
908 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
909 if (Node->getType()->isRecordType())
910 OS << "{}";
911 else
912 OS << 0;
913 }
Douglas Gregor0202cb42009-01-29 17:44:32 +0000914}
915
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000916void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +0000917 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000918 PrintExpr(Node->getSubExpr());
919 OS << ", ";
Daniel Dunbar219fa692010-06-30 19:16:48 +0000920 OS << Node->getType().getAsString(Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000921 OS << ")";
922}
923
Chris Lattnereefa10e2007-05-28 06:56:27 +0000924// C++
Douglas Gregor993603d2008-11-14 16:09:21 +0000925void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
926 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
927 "",
928#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
929 Spelling,
930#include "clang/Basic/OperatorKinds.def"
931 };
932
933 OverloadedOperatorKind Kind = Node->getOperator();
934 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
935 if (Node->getNumArgs() == 1) {
936 OS << OpStrings[Kind] << ' ';
937 PrintExpr(Node->getArg(0));
938 } else {
939 PrintExpr(Node->getArg(0));
940 OS << ' ' << OpStrings[Kind];
941 }
942 } else if (Kind == OO_Call) {
943 PrintExpr(Node->getArg(0));
944 OS << '(';
945 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
946 if (ArgIdx > 1)
947 OS << ", ";
948 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
949 PrintExpr(Node->getArg(ArgIdx));
950 }
951 OS << ')';
952 } else if (Kind == OO_Subscript) {
953 PrintExpr(Node->getArg(0));
954 OS << '[';
955 PrintExpr(Node->getArg(1));
956 OS << ']';
957 } else if (Node->getNumArgs() == 1) {
958 OS << OpStrings[Kind] << ' ';
959 PrintExpr(Node->getArg(0));
960 } else if (Node->getNumArgs() == 2) {
961 PrintExpr(Node->getArg(0));
962 OS << ' ' << OpStrings[Kind] << ' ';
963 PrintExpr(Node->getArg(1));
964 } else {
965 assert(false && "unknown overloaded operator");
966 }
967}
Chris Lattnereefa10e2007-05-28 06:56:27 +0000968
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000969void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
970 VisitCallExpr(cast<CallExpr>(Node));
971}
972
Peter Collingbourne41f85462011-02-09 21:07:24 +0000973void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
974 PrintExpr(Node->getCallee());
975 OS << "<<<";
976 PrintCallArgs(Node->getConfig());
977 OS << ">>>(";
978 PrintCallArgs(Node);
979 OS << ")";
980}
981
Douglas Gregore200adc2008-10-27 19:41:14 +0000982void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
983 OS << Node->getCastName() << '<';
Daniel Dunbar219fa692010-06-30 19:16:48 +0000984 OS << Node->getTypeAsWritten().getAsString(Policy) << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +0000985 PrintExpr(Node->getSubExpr());
986 OS << ")";
987}
988
Douglas Gregore200adc2008-10-27 19:41:14 +0000989void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
990 VisitCXXNamedCastExpr(Node);
991}
992
993void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
994 VisitCXXNamedCastExpr(Node);
995}
996
997void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
998 VisitCXXNamedCastExpr(Node);
999}
1000
1001void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1002 VisitCXXNamedCastExpr(Node);
1003}
1004
Sebastian Redlc4704762008-11-11 11:37:55 +00001005void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1006 OS << "typeid(";
1007 if (Node->isTypeOperand()) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001008 OS << Node->getTypeOperand().getAsString(Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00001009 } else {
1010 PrintExpr(Node->getExprOperand());
1011 }
1012 OS << ")";
1013}
1014
Francois Pichet9f4f2072010-09-08 12:20:18 +00001015void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1016 OS << "__uuidof(";
1017 if (Node->isTypeOperand()) {
1018 OS << Node->getTypeOperand().getAsString(Policy);
1019 } else {
1020 PrintExpr(Node->getExprOperand());
1021 }
1022 OS << ")";
1023}
1024
Chris Lattnereefa10e2007-05-28 06:56:27 +00001025void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1026 OS << (Node->getValue() ? "true" : "false");
1027}
1028
Sebastian Redl576fd422009-05-10 18:38:11 +00001029void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1030 OS << "nullptr";
1031}
1032
Douglas Gregor97a9c812008-11-04 14:32:21 +00001033void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1034 OS << "this";
1035}
1036
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001037void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1038 if (Node->getSubExpr() == 0)
1039 OS << "throw";
1040 else {
1041 OS << "throw ";
1042 PrintExpr(Node->getSubExpr());
1043 }
1044}
1045
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001046void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1047 // Nothing to print: we picked up the default argument
1048}
1049
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001050void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001051 OS << Node->getType().getAsString(Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001052 OS << "(";
1053 PrintExpr(Node->getSubExpr());
1054 OS << ")";
1055}
1056
Anders Carlsson993a4b32009-05-30 20:03:25 +00001057void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1058 PrintExpr(Node->getSubExpr());
1059}
1060
Douglas Gregordd04d332009-01-16 18:33:17 +00001061void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001062 OS << Node->getType().getAsString(Policy);
Douglas Gregordd04d332009-01-16 18:33:17 +00001063 OS << "(";
1064 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001065 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001066 Arg != ArgEnd; ++Arg) {
1067 if (Arg != Node->arg_begin())
1068 OS << ", ";
1069 PrintExpr(*Arg);
1070 }
1071 OS << ")";
1072}
1073
Douglas Gregor747eb782010-07-08 06:14:04 +00001074void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00001075 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1076 OS << TSInfo->getType().getAsString(Policy) << "()";
1077 else
1078 OS << Node->getType().getAsString(Policy) << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001079}
1080
Sebastian Redlbd150f42008-11-21 19:14:01 +00001081void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1082 if (E->isGlobalNew())
1083 OS << "::";
1084 OS << "new ";
1085 unsigned NumPlace = E->getNumPlacementArgs();
1086 if (NumPlace > 0) {
1087 OS << "(";
1088 PrintExpr(E->getPlacementArg(0));
1089 for (unsigned i = 1; i < NumPlace; ++i) {
1090 OS << ", ";
1091 PrintExpr(E->getPlacementArg(i));
1092 }
1093 OS << ") ";
1094 }
1095 if (E->isParenTypeId())
1096 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001097 std::string TypeS;
1098 if (Expr *Size = E->getArraySize()) {
1099 llvm::raw_string_ostream s(TypeS);
Eli Friedmanc4fc8392009-05-30 05:32:46 +00001100 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001101 s.flush();
1102 TypeS = "[" + TypeS + "]";
1103 }
Douglas Gregor7de59662009-05-29 20:38:28 +00001104 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001105 OS << TypeS;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001106 if (E->isParenTypeId())
1107 OS << ")";
1108
1109 if (E->hasInitializer()) {
1110 OS << "(";
1111 unsigned NumCons = E->getNumConstructorArgs();
1112 if (NumCons > 0) {
1113 PrintExpr(E->getConstructorArg(0));
1114 for (unsigned i = 1; i < NumCons; ++i) {
1115 OS << ", ";
1116 PrintExpr(E->getConstructorArg(i));
1117 }
1118 }
1119 OS << ")";
1120 }
1121}
1122
1123void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1124 if (E->isGlobalDelete())
1125 OS << "::";
1126 OS << "delete ";
1127 if (E->isArrayForm())
1128 OS << "[] ";
1129 PrintExpr(E->getArgument());
1130}
1131
Douglas Gregorad8a3362009-09-04 17:36:40 +00001132void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1133 PrintExpr(E->getBase());
1134 if (E->isArrow())
1135 OS << "->";
1136 else
1137 OS << '.';
1138 if (E->getQualifier())
1139 E->getQualifier()->print(OS, Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001140
Douglas Gregorad8a3362009-09-04 17:36:40 +00001141 std::string TypeS;
Douglas Gregor678f90d2010-02-25 01:56:36 +00001142 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1143 OS << II->getName();
1144 else
1145 E->getDestroyedType().getAsStringInternal(TypeS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00001146 OS << TypeS;
1147}
1148
Anders Carlsson0781ce72009-04-23 02:32:43 +00001149void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Douglas Gregorbaba85d2011-01-24 17:25:03 +00001150 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1151 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1152 // Don't print any defaulted arguments
1153 break;
1154 }
1155
1156 if (i) OS << ", ";
1157 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00001158 }
Anders Carlsson0781ce72009-04-23 02:32:43 +00001159}
1160
John McCall5d413782010-12-06 08:20:24 +00001161void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Anders Carlssondefc6442009-04-24 22:47:04 +00001162 // Just forward to the sub expression.
1163 PrintExpr(E->getSubExpr());
1164}
1165
Mike Stump11289f42009-09-09 15:08:12 +00001166void
Douglas Gregorce934142009-05-20 18:46:25 +00001167StmtPrinter::VisitCXXUnresolvedConstructExpr(
1168 CXXUnresolvedConstructExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001169 OS << Node->getTypeAsWritten().getAsString(Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00001170 OS << "(";
1171 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001172 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00001173 Arg != ArgEnd; ++Arg) {
1174 if (Arg != Node->arg_begin())
1175 OS << ", ";
1176 PrintExpr(*Arg);
1177 }
1178 OS << ")";
1179}
1180
John McCall8cd78132009-11-19 22:55:06 +00001181void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1182 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001183 if (!Node->isImplicitAccess()) {
1184 PrintExpr(Node->getBase());
1185 OS << (Node->isArrow() ? "->" : ".");
1186 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00001187 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1188 Qualifier->print(OS, Policy);
John McCall2d74de92009-12-01 22:10:20 +00001189 else if (Node->hasExplicitTemplateArgs())
Douglas Gregor308047d2009-09-09 00:23:06 +00001190 // FIXME: Track use of "template" keyword explicitly?
1191 OS << "template ";
Mike Stump11289f42009-09-09 15:08:12 +00001192
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001193 OS << Node->getMemberNameInfo();
Mike Stump11289f42009-09-09 15:08:12 +00001194
John McCall2d74de92009-12-01 22:10:20 +00001195 if (Node->hasExplicitTemplateArgs()) {
Douglas Gregor308047d2009-09-09 00:23:06 +00001196 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1197 Node->getTemplateArgs(),
1198 Node->getNumTemplateArgs(),
1199 Policy);
1200 }
Douglas Gregora8db9542009-05-22 21:13:27 +00001201}
1202
John McCall10eae182009-11-30 22:42:35 +00001203void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001204 if (!Node->isImplicitAccess()) {
1205 PrintExpr(Node->getBase());
1206 OS << (Node->isArrow() ? "->" : ".");
1207 }
John McCall10eae182009-11-30 22:42:35 +00001208 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1209 Qualifier->print(OS, Policy);
1210
1211 // FIXME: this might originally have been written with 'template'
1212
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001213 OS << Node->getMemberNameInfo();
John McCall10eae182009-11-30 22:42:35 +00001214
1215 if (Node->hasExplicitTemplateArgs()) {
1216 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1217 Node->getTemplateArgs(),
1218 Node->getNumTemplateArgs(),
1219 Policy);
1220 }
1221}
1222
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001223static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1224 switch (UTT) {
Francois Pichet347c4c72010-12-07 00:55:57 +00001225 default: llvm_unreachable("Unknown unary type trait");
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001226 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1227 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1228 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1229 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1230 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1231 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1232 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1233 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1234 case UTT_IsAbstract: return "__is_abstract";
1235 case UTT_IsClass: return "__is_class";
1236 case UTT_IsEmpty: return "__is_empty";
1237 case UTT_IsEnum: return "__is_enum";
1238 case UTT_IsPOD: return "__is_pod";
1239 case UTT_IsPolymorphic: return "__is_polymorphic";
1240 case UTT_IsUnion: return "__is_union";
1241 }
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001242 return "";
1243}
1244
1245static const char *getTypeTraitName(BinaryTypeTrait BTT) {
1246 switch (BTT) {
Francois Pichet34b21132010-12-08 22:35:30 +00001247 case BTT_IsBaseOf: return "__is_base_of";
1248 case BTT_TypeCompatible: return "__builtin_types_compatible_p";
Douglas Gregor8006e762011-01-27 20:28:01 +00001249 case BTT_IsConvertibleTo: return "__is_convertible_to";
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001250 }
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001251 return "";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001252}
1253
1254void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1255 OS << getTypeTraitName(E->getTrait()) << "("
Daniel Dunbar219fa692010-06-30 19:16:48 +00001256 << E->getQueriedType().getAsString(Policy) << ")";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001257}
1258
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001259void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1260 OS << getTypeTraitName(E->getTrait()) << "("
1261 << E->getLhsType().getAsString(Policy) << ","
1262 << E->getRhsType().getAsString(Policy) << ")";
1263}
1264
Sebastian Redl4202c0f2010-09-10 20:55:43 +00001265void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1266 OS << "noexcept(";
1267 PrintExpr(E->getOperand());
1268 OS << ")";
1269}
1270
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001271void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00001272 PrintExpr(E->getPattern());
1273 OS << "...";
1274}
1275
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001276void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
1277 OS << "sizeof...(" << E->getPack()->getNameAsString() << ")";
1278}
1279
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001280void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1281 SubstNonTypeTemplateParmPackExpr *Node) {
1282 OS << Node->getParameterPack()->getNameAsString();
1283}
1284
Mike Stump11289f42009-09-09 15:08:12 +00001285// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00001286
1287void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1288 OS << "@";
1289 VisitStringLiteral(Node->getString());
1290}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001291
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001292void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001293 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001294}
1295
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001296void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001297 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001298}
1299
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001300void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001301 OS << "@protocol(" << Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001302}
1303
Steve Naroffd54978b2007-09-18 23:55:05 +00001304void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1305 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00001306 switch (Mess->getReceiverKind()) {
1307 case ObjCMessageExpr::Instance:
1308 PrintExpr(Mess->getInstanceReceiver());
1309 break;
1310
1311 case ObjCMessageExpr::Class:
1312 OS << Mess->getClassReceiver().getAsString(Policy);
1313 break;
1314
1315 case ObjCMessageExpr::SuperInstance:
1316 case ObjCMessageExpr::SuperClass:
1317 OS << "Super";
1318 break;
1319 }
1320
Ted Kremeneka06e7122008-05-02 17:32:38 +00001321 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001322 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001323 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00001324 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00001325 } else {
1326 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001327 if (i < selector.getNumArgs()) {
1328 if (i > 0) OS << ' ';
1329 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001330 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001331 else
1332 OS << ":";
1333 }
1334 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00001335
Steve Naroffc6814ea2007-10-02 20:01:56 +00001336 PrintExpr(Mess->getArg(i));
1337 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001338 }
1339 OS << "]";
1340}
1341
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001342
Steve Naroffc540d662008-09-03 18:15:37 +00001343void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001344 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001345 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00001346
Steve Naroffc540d662008-09-03 18:15:37 +00001347 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001348
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001349 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00001350 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001351 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001352 OS << '(';
1353 std::string ParamStr;
Steve Naroff415d3d52008-10-08 17:01:13 +00001354 for (BlockDecl::param_iterator AI = BD->param_begin(),
1355 E = BD->param_end(); AI != E; ++AI) {
1356 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001357 ParamStr = (*AI)->getNameAsString();
Douglas Gregor7de59662009-05-29 20:38:28 +00001358 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroffc540d662008-09-03 18:15:37 +00001359 OS << ParamStr;
1360 }
Mike Stump11289f42009-09-09 15:08:12 +00001361
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001362 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001363 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001364 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001365 OS << "...";
1366 }
1367 OS << ')';
1368 }
1369}
1370
Steve Naroffc540d662008-09-03 18:15:37 +00001371void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001372 OS << Node->getDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001373}
John McCall8d69a212010-11-15 23:31:06 +00001374
1375void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {}
1376
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001377//===----------------------------------------------------------------------===//
1378// Stmt method implementations
1379//===----------------------------------------------------------------------===//
1380
Eli Friedmanef334fd2009-05-30 05:03:24 +00001381void Stmt::dumpPretty(ASTContext& Context) const {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00001382 printPretty(llvm::errs(), Context, 0,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001383 PrintingPolicy(Context.getLangOptions()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001384}
1385
Eli Friedmanef334fd2009-05-30 05:03:24 +00001386void Stmt::printPretty(llvm::raw_ostream &OS, ASTContext& Context,
1387 PrinterHelper* Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00001388 const PrintingPolicy &Policy,
1389 unsigned Indentation) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001390 if (this == 0) {
1391 OS << "<NULL>";
1392 return;
1393 }
1394
Douglas Gregor8655e882009-10-16 22:46:09 +00001395 if (Policy.Dump && &Context) {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00001396 dump(OS, Context.getSourceManager());
Douglas Gregor278f52e2009-05-30 00:08:05 +00001397 return;
1398 }
Mike Stump11289f42009-09-09 15:08:12 +00001399
Eli Friedmanef334fd2009-05-30 05:03:24 +00001400 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00001401 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001402}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001403
1404//===----------------------------------------------------------------------===//
1405// PrinterHelper
1406//===----------------------------------------------------------------------===//
1407
1408// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001409PrinterHelper::~PrinterHelper() {}