blob: 7ad549100565cc502e5f26ff192106909fb55ad2 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner6000dac2007-08-08 22:51:59 +000010// This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which
11// pretty print the AST back out to C code.
Reid Spencer5f016e22007-07-11 17:01:13 +000012//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramer471c8b42012-07-04 20:19:54 +000015#include "clang/AST/ASTContext.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000016#include "clang/AST/Attr.h"
Douglas Gregor1a49af92009-01-06 05:10:23 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenek91d1d7a2007-10-17 18:36:42 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregorc7793c72011-01-15 01:15:58 +000019#include "clang/AST/DeclTemplate.h"
Douglas Gregor8ecdb652010-04-28 22:16:22 +000020#include "clang/AST/Expr.h"
Douglas Gregorab6677e2010-09-08 00:15:04 +000021#include "clang/AST/ExprCXX.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000022#include "clang/AST/PrettyPrinter.h"
23#include "clang/AST/StmtVisitor.h"
Jordan Rose6d4f7342013-02-08 22:30:27 +000024#include "clang/Basic/CharInfo.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Jordan Rose6d4f7342013-02-08 22:30:27 +000026#include "llvm/Support/Format.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// StmtPrinter Visitor
31//===----------------------------------------------------------------------===//
32
33namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +000034 class StmtPrinter : public StmtVisitor<StmtPrinter> {
Chris Lattner5f9e2722011-07-23 10:55:15 +000035 raw_ostream &OS;
Reid Spencer5f016e22007-07-11 17:01:13 +000036 unsigned IndentLevel;
Ted Kremenek42a509f2007-08-31 21:30:12 +000037 clang::PrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000038 PrintingPolicy Policy;
39
Reid Spencer5f016e22007-07-11 17:01:13 +000040 public:
Richard Smithd1420c62012-08-16 03:56:14 +000041 StmtPrinter(raw_ostream &os, PrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +000042 const PrintingPolicy &Policy,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000043 unsigned Indentation = 0)
Richard Smithd1420c62012-08-16 03:56:14 +000044 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy) {}
Mike Stump1eb44332009-09-09 15:08:12 +000045
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000046 void PrintStmt(Stmt *S) {
47 PrintStmt(S, Policy.Indentation);
48 }
49
50 void PrintStmt(Stmt *S, int SubIndent) {
Reid Spencer5f016e22007-07-11 17:01:13 +000051 IndentLevel += SubIndent;
52 if (S && isa<Expr>(S)) {
53 // If this is an expr used in a stmt context, indent and newline it.
54 Indent();
Chris Lattnerc5598cb2007-08-21 04:04:25 +000055 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000056 OS << ";\n";
57 } else if (S) {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000058 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000059 } else {
60 Indent() << "<<<NULL STATEMENT>>>\n";
61 }
62 IndentLevel -= SubIndent;
63 }
Eli Friedmandb23b152009-05-30 00:19:54 +000064
Reid Spencer5f016e22007-07-11 17:01:13 +000065 void PrintRawCompoundStmt(CompoundStmt *S);
66 void PrintRawDecl(Decl *D);
Eli Friedman915c07d2012-10-16 23:45:15 +000067 void PrintRawDeclStmt(const DeclStmt *S);
Reid Spencer5f016e22007-07-11 17:01:13 +000068 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl8351da02008-12-22 21:35:02 +000069 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Peter Collingbourned64e2372011-02-08 21:17:54 +000070 void PrintCallArgs(CallExpr *E);
John Wiegley28bbe4b2011-04-28 01:08:34 +000071 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
72 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
Stephen Hines651f13c2014-04-23 16:59:28 -070073 void PrintOMPExecutableDirective(OMPExecutableDirective *S);
Mike Stump1eb44332009-09-09 15:08:12 +000074
Reid Spencer5f016e22007-07-11 17:01:13 +000075 void PrintExpr(Expr *E) {
76 if (E)
Chris Lattnerc5598cb2007-08-21 04:04:25 +000077 Visit(E);
Reid Spencer5f016e22007-07-11 17:01:13 +000078 else
79 OS << "<null expr>";
80 }
Mike Stump1eb44332009-09-09 15:08:12 +000081
Chris Lattner5f9e2722011-07-23 10:55:15 +000082 raw_ostream &Indent(int Delta = 0) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000083 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
84 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +000085 return OS;
86 }
Mike Stump1eb44332009-09-09 15:08:12 +000087
Mike Stump1eb44332009-09-09 15:08:12 +000088 void Visit(Stmt* S) {
Ted Kremenek42a509f2007-08-31 21:30:12 +000089 if (Helper && Helper->handledStmt(S,OS))
90 return;
91 else StmtVisitor<StmtPrinter>::Visit(S);
92 }
Stephen Hines651f13c2014-04-23 16:59:28 -070093
Chandler Carruth61e38282010-10-23 08:21:37 +000094 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000095 Indent() << "<<unknown stmt type>>\n";
96 }
Chandler Carruth61e38282010-10-23 08:21:37 +000097 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000098 OS << "<<unknown expr type>>";
99 }
100 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +0000102#define ABSTRACT_STMT(CLASS)
Douglas Gregorf2cad862008-11-14 12:46:07 +0000103#define STMT(CLASS, PARENT) \
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000104 void Visit##CLASS(CLASS *Node);
Sean Hunt4bfe1962010-05-05 15:24:00 +0000105#include "clang/AST/StmtNodes.inc"
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 };
107}
108
109//===----------------------------------------------------------------------===//
110// Stmt printing methods.
111//===----------------------------------------------------------------------===//
112
Reid Spencer5f016e22007-07-11 17:01:13 +0000113/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
114/// with no newline after the }.
115void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
116 OS << "{\n";
Stephen Hines651f13c2014-04-23 16:59:28 -0700117 for (auto *I : Node->body())
118 PrintStmt(I);
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 Indent() << "}";
121}
122
123void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000124 D->print(OS, Policy, IndentLevel);
Mike Stump071e4da2009-02-10 20:16:46 +0000125}
126
Eli Friedman915c07d2012-10-16 23:45:15 +0000127void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700128 SmallVector<Decl*, 2> Decls(S->decls());
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000129 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenekecd64c52008-10-06 18:39:36 +0000130}
Reid Spencer5f016e22007-07-11 17:01:13 +0000131
132void StmtPrinter::VisitNullStmt(NullStmt *Node) {
133 Indent() << ";\n";
134}
135
136void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedmandb23b152009-05-30 00:19:54 +0000137 Indent();
138 PrintRawDeclStmt(Node);
139 OS << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000140}
141
142void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
143 Indent();
144 PrintRawCompoundStmt(Node);
145 OS << "\n";
146}
147
148void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
149 Indent(-1) << "case ";
150 PrintExpr(Node->getLHS());
151 if (Node->getRHS()) {
152 OS << " ... ";
153 PrintExpr(Node->getRHS());
154 }
155 OS << ":\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 PrintStmt(Node->getSubStmt(), 0);
158}
159
160void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
161 Indent(-1) << "default:\n";
162 PrintStmt(Node->getSubStmt(), 0);
163}
164
165void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
166 Indent(-1) << Node->getName() << ":\n";
167 PrintStmt(Node->getSubStmt(), 0);
168}
169
Richard Smith534986f2012-04-14 00:33:13 +0000170void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
171 OS << "[[";
172 bool first = true;
Alexander Kornienko49908902012-07-09 10:04:07 +0000173 for (ArrayRef<const Attr*>::iterator it = Node->getAttrs().begin(),
174 end = Node->getAttrs().end();
175 it != end; ++it) {
Richard Smith534986f2012-04-14 00:33:13 +0000176 if (!first) {
177 OS << ", ";
178 first = false;
179 }
180 // TODO: check this
Richard Smith0dae7292012-08-16 02:43:29 +0000181 (*it)->printPretty(OS, Policy);
Richard Smith534986f2012-04-14 00:33:13 +0000182 }
183 OS << "]] ";
184 PrintStmt(Node->getSubStmt(), 0);
185}
186
Reid Spencer5f016e22007-07-11 17:01:13 +0000187void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000188 OS << "if (";
Eli Friedman915c07d2012-10-16 23:45:15 +0000189 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
190 PrintRawDeclStmt(DS);
191 else
192 PrintExpr(If->getCond());
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000193 OS << ')';
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Reid Spencer5f016e22007-07-11 17:01:13 +0000195 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
196 OS << ' ';
197 PrintRawCompoundStmt(CS);
198 OS << (If->getElse() ? ' ' : '\n');
199 } else {
200 OS << '\n';
201 PrintStmt(If->getThen());
202 if (If->getElse()) Indent();
203 }
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 if (Stmt *Else = If->getElse()) {
206 OS << "else";
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
209 OS << ' ';
210 PrintRawCompoundStmt(CS);
211 OS << '\n';
212 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
213 OS << ' ';
214 PrintRawIfStmt(ElseIf);
215 } else {
216 OS << '\n';
217 PrintStmt(If->getElse());
218 }
219 }
220}
221
222void StmtPrinter::VisitIfStmt(IfStmt *If) {
223 Indent();
224 PrintRawIfStmt(If);
225}
226
227void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
228 Indent() << "switch (";
Eli Friedman915c07d2012-10-16 23:45:15 +0000229 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
230 PrintRawDeclStmt(DS);
231 else
232 PrintExpr(Node->getCond());
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 OS << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 // Pretty print compoundstmt bodies (very common).
236 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
237 OS << " ";
238 PrintRawCompoundStmt(CS);
239 OS << "\n";
240 } else {
241 OS << "\n";
242 PrintStmt(Node->getBody());
243 }
244}
245
246void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
247 Indent() << "while (";
Eli Friedman915c07d2012-10-16 23:45:15 +0000248 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
249 PrintRawDeclStmt(DS);
250 else
251 PrintExpr(Node->getCond());
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 OS << ")\n";
253 PrintStmt(Node->getBody());
254}
255
256void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000257 Indent() << "do ";
258 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
259 PrintRawCompoundStmt(CS);
260 OS << " ";
261 } else {
262 OS << "\n";
263 PrintStmt(Node->getBody());
264 Indent();
265 }
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Eli Friedmanb3e22962009-05-17 01:05:34 +0000267 OS << "while (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000268 PrintExpr(Node->getCond());
Eli Friedmanb3e22962009-05-17 01:05:34 +0000269 OS << ");\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000270}
271
272void StmtPrinter::VisitForStmt(ForStmt *Node) {
273 Indent() << "for (";
274 if (Node->getInit()) {
275 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000276 PrintRawDeclStmt(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 else
278 PrintExpr(cast<Expr>(Node->getInit()));
279 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000280 OS << ";";
281 if (Node->getCond()) {
282 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000284 }
285 OS << ";";
286 if (Node->getInc()) {
287 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000289 }
290 OS << ") ";
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Chris Lattner8bdcc472007-09-15 21:49:37 +0000292 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
293 PrintRawCompoundStmt(CS);
294 OS << "\n";
295 } else {
296 OS << "\n";
297 PrintStmt(Node->getBody());
298 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000299}
300
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000301void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000302 Indent() << "for (";
303 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000304 PrintRawDeclStmt(DS);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000305 else
306 PrintExpr(cast<Expr>(Node->getElement()));
307 OS << " in ";
308 PrintExpr(Node->getCollection());
309 OS << ") ";
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000311 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
312 PrintRawCompoundStmt(CS);
313 OS << "\n";
314 } else {
315 OS << "\n";
316 PrintStmt(Node->getBody());
317 }
318}
319
Richard Smithad762fc2011-04-14 22:09:26 +0000320void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
321 Indent() << "for (";
322 PrintingPolicy SubPolicy(Policy);
323 SubPolicy.SuppressInitializers = true;
324 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
325 OS << " : ";
326 PrintExpr(Node->getRangeInit());
327 OS << ") {\n";
328 PrintStmt(Node->getBody());
Stephen Hines651f13c2014-04-23 16:59:28 -0700329 Indent() << "}";
330 if (Policy.IncludeNewlines) OS << "\n";
Richard Smithad762fc2011-04-14 22:09:26 +0000331}
332
Douglas Gregorba0513d2011-10-25 01:33:02 +0000333void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
334 Indent();
335 if (Node->isIfExists())
336 OS << "__if_exists (";
337 else
338 OS << "__if_not_exists (";
339
340 if (NestedNameSpecifier *Qualifier
341 = Node->getQualifierLoc().getNestedNameSpecifier())
342 Qualifier->print(OS, Policy);
343
344 OS << Node->getNameInfo() << ") ";
345
346 PrintRawCompoundStmt(Node->getSubStmt());
347}
348
Reid Spencer5f016e22007-07-11 17:01:13 +0000349void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700350 Indent() << "goto " << Node->getLabel()->getName() << ";";
351 if (Policy.IncludeNewlines) OS << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000352}
353
354void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
355 Indent() << "goto *";
356 PrintExpr(Node->getTarget());
Stephen Hines651f13c2014-04-23 16:59:28 -0700357 OS << ";";
358 if (Policy.IncludeNewlines) OS << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000359}
360
361void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700362 Indent() << "continue;";
363 if (Policy.IncludeNewlines) OS << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000364}
365
366void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700367 Indent() << "break;";
368 if (Policy.IncludeNewlines) OS << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000369}
370
371
372void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
373 Indent() << "return";
374 if (Node->getRetValue()) {
375 OS << " ";
376 PrintExpr(Node->getRetValue());
377 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700378 OS << ";";
379 if (Policy.IncludeNewlines) OS << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000380}
381
Chris Lattnerfe795952007-10-29 04:04:16 +0000382
Chad Rosierdf5faf52012-08-25 00:11:56 +0000383void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000384 Indent() << "asm ";
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Anders Carlsson39c47b52007-11-23 23:12:25 +0000386 if (Node->isVolatile())
387 OS << "volatile ";
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Anders Carlsson39c47b52007-11-23 23:12:25 +0000389 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000390 VisitStringLiteral(Node->getAsmString());
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Anders Carlssonb235fc22007-11-22 01:36:19 +0000392 // Outputs
393 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
394 Node->getNumClobbers() != 0)
395 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Anders Carlssonb235fc22007-11-22 01:36:19 +0000397 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
398 if (i != 0)
399 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Anders Carlssonb235fc22007-11-22 01:36:19 +0000401 if (!Node->getOutputName(i).empty()) {
402 OS << '[';
403 OS << Node->getOutputName(i);
404 OS << "] ";
405 }
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Chris Lattnerb3277932009-03-10 04:59:06 +0000407 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000408 OS << " ";
409 Visit(Node->getOutputExpr(i));
410 }
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Anders Carlssonb235fc22007-11-22 01:36:19 +0000412 // Inputs
413 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
414 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Anders Carlssonb235fc22007-11-22 01:36:19 +0000416 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
417 if (i != 0)
418 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Anders Carlssonb235fc22007-11-22 01:36:19 +0000420 if (!Node->getInputName(i).empty()) {
421 OS << '[';
422 OS << Node->getInputName(i);
423 OS << "] ";
424 }
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Chris Lattnerb3277932009-03-10 04:59:06 +0000426 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000427 OS << " ";
428 Visit(Node->getInputExpr(i));
429 }
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Anders Carlssonb235fc22007-11-22 01:36:19 +0000431 // Clobbers
432 if (Node->getNumClobbers() != 0)
433 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Anders Carlssonb235fc22007-11-22 01:36:19 +0000435 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
436 if (i != 0)
437 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Chad Rosier5c7f5942012-08-27 23:28:41 +0000439 VisitStringLiteral(Node->getClobberStringLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000440 }
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Stephen Hines651f13c2014-04-23 16:59:28 -0700442 OS << ");";
443 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000444}
445
Chad Rosier8cd64b42012-06-11 20:47:18 +0000446void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
447 // FIXME: Implement MS style inline asm statement printer.
Chad Rosier7bd092b2012-08-15 16:53:30 +0000448 Indent() << "__asm ";
449 if (Node->hasBraces())
450 OS << "{\n";
John McCallaeeacf72013-05-03 00:10:13 +0000451 OS << Node->getAsmString() << "\n";
Chad Rosier7bd092b2012-08-15 16:53:30 +0000452 if (Node->hasBraces())
453 Indent() << "}\n";
Chad Rosier8cd64b42012-06-11 20:47:18 +0000454}
455
Tareq A. Siraj051303c2013-04-16 18:53:08 +0000456void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
Tareq A. Siraj6afcf882013-04-16 19:37:38 +0000457 PrintStmt(Node->getCapturedDecl()->getBody());
Tareq A. Siraj051303c2013-04-16 18:53:08 +0000458}
459
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000460void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000461 Indent() << "@try";
462 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
463 PrintRawCompoundStmt(TS);
464 OS << "\n";
465 }
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000467 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
468 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000469 Indent() << "@catch(";
Steve Naroff7ba138a2009-03-03 19:52:17 +0000470 if (catchStmt->getCatchParamDecl()) {
471 if (Decl *DS = catchStmt->getCatchParamDecl())
472 PrintRawDecl(DS);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000473 }
474 OS << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000475 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
476 PrintRawCompoundStmt(CS);
477 OS << "\n";
478 }
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000479 }
Mike Stump1eb44332009-09-09 15:08:12 +0000480
481 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
482 Node->getFinallyStmt())) {
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000483 Indent() << "@finally";
484 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000485 OS << "\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000486 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000487}
488
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000489void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000490}
491
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000492void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000493 Indent() << "@catch (...) { /* todo */ } \n";
494}
495
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000496void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000497 Indent() << "@throw";
498 if (Node->getThrowExpr()) {
499 OS << " ";
500 PrintExpr(Node->getThrowExpr());
501 }
502 OS << ";\n";
503}
504
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000505void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000506 Indent() << "@synchronized (";
507 PrintExpr(Node->getSynchExpr());
508 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000509 PrintRawCompoundStmt(Node->getSynchBody());
510 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000511}
512
John McCallf85e1932011-06-15 23:02:42 +0000513void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
514 Indent() << "@autoreleasepool";
515 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
516 OS << "\n";
517}
518
Sebastian Redl8351da02008-12-22 21:35:02 +0000519void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
520 OS << "catch (";
Sebastian Redl4b07b292008-12-22 19:15:10 +0000521 if (Decl *ExDecl = Node->getExceptionDecl())
522 PrintRawDecl(ExDecl);
523 else
524 OS << "...";
525 OS << ") ";
526 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl8351da02008-12-22 21:35:02 +0000527}
528
529void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
530 Indent();
531 PrintRawCXXCatchStmt(Node);
532 OS << "\n";
533}
534
535void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
536 Indent() << "try ";
537 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump1eb44332009-09-09 15:08:12 +0000538 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl8351da02008-12-22 21:35:02 +0000539 OS << " ";
540 PrintRawCXXCatchStmt(Node->getHandler(i));
541 }
Sebastian Redl4b07b292008-12-22 19:15:10 +0000542 OS << "\n";
543}
544
John Wiegley28bbe4b2011-04-28 01:08:34 +0000545void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
546 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
547 PrintRawCompoundStmt(Node->getTryBlock());
548 SEHExceptStmt *E = Node->getExceptHandler();
549 SEHFinallyStmt *F = Node->getFinallyHandler();
550 if(E)
551 PrintRawSEHExceptHandler(E);
552 else {
553 assert(F && "Must have a finally block...");
554 PrintRawSEHFinallyStmt(F);
555 }
556 OS << "\n";
557}
558
559void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
560 OS << "__finally ";
561 PrintRawCompoundStmt(Node->getBlock());
562 OS << "\n";
563}
564
565void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
566 OS << "__except (";
567 VisitExpr(Node->getFilterExpr());
Joao Matos568ba872012-09-04 17:49:35 +0000568 OS << ")\n";
John Wiegley28bbe4b2011-04-28 01:08:34 +0000569 PrintRawCompoundStmt(Node->getBlock());
570 OS << "\n";
571}
572
573void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
574 Indent();
575 PrintRawSEHExceptHandler(Node);
576 OS << "\n";
577}
578
579void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
580 Indent();
581 PrintRawSEHFinallyStmt(Node);
582 OS << "\n";
583}
584
Reid Spencer5f016e22007-07-11 17:01:13 +0000585//===----------------------------------------------------------------------===//
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000586// OpenMP clauses printing methods
587//===----------------------------------------------------------------------===//
588
589namespace {
590class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
591 raw_ostream &OS;
Stephen Hines651f13c2014-04-23 16:59:28 -0700592 const PrintingPolicy &Policy;
Alexey Bataev543c4ae2013-09-24 03:17:45 +0000593 /// \brief Process clauses with list of variables.
594 template <typename T>
595 void VisitOMPClauseList(T *Node, char StartSym);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000596public:
Stephen Hines651f13c2014-04-23 16:59:28 -0700597 OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy)
598 : OS(OS), Policy(Policy) { }
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000599#define OPENMP_CLAUSE(Name, Class) \
600 void Visit##Class(Class *S);
601#include "clang/Basic/OpenMPKinds.def"
602};
603
Stephen Hines651f13c2014-04-23 16:59:28 -0700604void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
605 OS << "if(";
606 Node->getCondition()->printPretty(OS, 0, Policy, 0);
607 OS << ")";
608}
609
610void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
611 OS << "num_threads(";
612 Node->getNumThreads()->printPretty(OS, 0, Policy, 0);
613 OS << ")";
614}
615
616void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
617 OS << "safelen(";
618 Node->getSafelen()->printPretty(OS, 0, Policy, 0);
619 OS << ")";
620}
621
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000622void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
623 OS << "default("
624 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
625 << ")";
626}
627
Alexey Bataev543c4ae2013-09-24 03:17:45 +0000628template<typename T>
629void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
630 for (typename T::varlist_iterator I = Node->varlist_begin(),
631 E = Node->varlist_end();
Stephen Hines651f13c2014-04-23 16:59:28 -0700632 I != E; ++I) {
633 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*I)) {
634 OS << (I == Node->varlist_begin() ? StartSym : ',');
635 cast<NamedDecl>(DRE->getDecl())->printQualifiedName(OS);
636 } else {
637 OS << (I == Node->varlist_begin() ? StartSym : ',');
638 (*I)->printPretty(OS, 0, Policy, 0);
639 }
640 }
Alexey Bataev543c4ae2013-09-24 03:17:45 +0000641}
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000642
643void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
644 if (!Node->varlist_empty()) {
645 OS << "private";
Alexey Bataev543c4ae2013-09-24 03:17:45 +0000646 VisitOMPClauseList(Node, '(');
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000647 OS << ")";
648 }
649}
650
Alexey Bataevd195bc32013-10-01 05:32:34 +0000651void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
652 if (!Node->varlist_empty()) {
653 OS << "firstprivate";
654 VisitOMPClauseList(Node, '(');
655 OS << ")";
656 }
657}
658
Alexey Bataev0c018352013-09-06 18:03:48 +0000659void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
660 if (!Node->varlist_empty()) {
661 OS << "shared";
Alexey Bataev543c4ae2013-09-24 03:17:45 +0000662 VisitOMPClauseList(Node, '(');
Alexey Bataev0c018352013-09-06 18:03:48 +0000663 OS << ")";
664 }
665}
666
Stephen Hines651f13c2014-04-23 16:59:28 -0700667void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
668 if (!Node->varlist_empty()) {
669 OS << "copyin";
670 VisitOMPClauseList(Node, '(');
671 OS << ")";
672 }
673}
674
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000675}
676
677//===----------------------------------------------------------------------===//
678// OpenMP directives printing methods
679//===----------------------------------------------------------------------===//
680
Stephen Hines651f13c2014-04-23 16:59:28 -0700681void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) {
682 OMPClausePrinter Printer(OS, Policy);
683 ArrayRef<OMPClause *> Clauses = S->clauses();
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000684 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
685 I != E; ++I)
686 if (*I && !(*I)->isImplicit()) {
687 Printer.Visit(*I);
688 OS << ' ';
689 }
690 OS << "\n";
Stephen Hines651f13c2014-04-23 16:59:28 -0700691 if (S->getAssociatedStmt()) {
692 assert(isa<CapturedStmt>(S->getAssociatedStmt()) &&
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000693 "Expected captured statement!");
Stephen Hines651f13c2014-04-23 16:59:28 -0700694 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt();
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000695 PrintStmt(CS);
696 }
697}
Stephen Hines651f13c2014-04-23 16:59:28 -0700698
699void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
700 Indent() << "#pragma omp parallel ";
701 PrintOMPExecutableDirective(Node);
702}
703
704void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
705 Indent() << "#pragma omp simd ";
706 PrintOMPExecutableDirective(Node);
707}
708
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000709//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000710// Expr printing methods.
711//===----------------------------------------------------------------------===//
712
Reid Spencer5f016e22007-07-11 17:01:13 +0000713void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000714 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
715 Qualifier->print(OS, Policy);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000716 if (Node->hasTemplateKeyword())
717 OS << "template ";
Abramo Bagnara25777432010-08-11 22:01:17 +0000718 OS << Node->getNameInfo();
John McCall096832c2010-08-19 23:49:38 +0000719 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer5eada842013-02-22 15:46:01 +0000720 TemplateSpecializationType::PrintTemplateArgumentList(
721 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregor1a49af92009-01-06 05:10:23 +0000722}
723
John McCall865d4472009-11-19 22:55:06 +0000724void StmtPrinter::VisitDependentScopeDeclRefExpr(
725 DependentScopeDeclRefExpr *Node) {
Axel Naumann274f83c2011-01-24 15:44:00 +0000726 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
727 Qualifier->print(OS, Policy);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000728 if (Node->hasTemplateKeyword())
729 OS << "template ";
Abramo Bagnara25777432010-08-11 22:01:17 +0000730 OS << Node->getNameInfo();
John McCallf7a1a742009-11-24 19:00:30 +0000731 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer5eada842013-02-22 15:46:01 +0000732 TemplateSpecializationType::PrintTemplateArgumentList(
733 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000734}
735
John McCallba135432009-11-21 08:51:07 +0000736void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000737 if (Node->getQualifier())
738 Node->getQualifier()->print(OS, Policy);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000739 if (Node->hasTemplateKeyword())
740 OS << "template ";
Abramo Bagnara25777432010-08-11 22:01:17 +0000741 OS << Node->getNameInfo();
John McCallf7a1a742009-11-24 19:00:30 +0000742 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer5eada842013-02-22 15:46:01 +0000743 TemplateSpecializationType::PrintTemplateArgumentList(
744 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000745}
746
Steve Naroff7779db42007-11-12 14:29:37 +0000747void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000748 if (Node->getBase()) {
749 PrintExpr(Node->getBase());
750 OS << (Node->isArrow() ? "->" : ".");
751 }
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000752 OS << *Node->getDecl();
Steve Naroff7779db42007-11-12 14:29:37 +0000753}
754
Steve Naroffae784072008-05-30 00:40:33 +0000755void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000756 if (Node->isSuperReceiver())
757 OS << "super.";
Stephen Hines651f13c2014-04-23 16:59:28 -0700758 else if (Node->isObjectReceiver() && Node->getBase()) {
Steve Naroffae784072008-05-30 00:40:33 +0000759 PrintExpr(Node->getBase());
760 OS << ".";
Stephen Hines651f13c2014-04-23 16:59:28 -0700761 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
762 OS << Node->getClassReceiver()->getName() << ".";
Steve Naroffae784072008-05-30 00:40:33 +0000763 }
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000764
John McCall12f78a62010-12-02 01:19:52 +0000765 if (Node->isImplicitProperty())
Stephen Hines651f13c2014-04-23 16:59:28 -0700766 Node->getImplicitPropertyGetter()->getSelector().print(OS);
John McCall12f78a62010-12-02 01:19:52 +0000767 else
768 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000769}
770
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000771void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
772
773 PrintExpr(Node->getBaseExpr());
774 OS << "[";
775 PrintExpr(Node->getKeyExpr());
776 OS << "]";
777}
778
Chris Lattnerd9f69102008-08-10 01:53:14 +0000779void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000780 switch (Node->getIdentType()) {
781 default:
David Blaikieb219cfc2011-09-23 05:06:16 +0000782 llvm_unreachable("unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000783 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000784 OS << "__func__";
785 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000786 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000787 OS << "__FUNCTION__";
788 break;
David Majnemerbafa74f2013-11-06 23:31:56 +0000789 case PredefinedExpr::FuncDName:
790 OS << "__FUNCDNAME__";
791 break;
Nico Weber28ad0632012-06-23 02:07:59 +0000792 case PredefinedExpr::LFunction:
793 OS << "L__FUNCTION__";
794 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000795 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000796 OS << "__PRETTY_FUNCTION__";
797 break;
798 }
799}
800
Reid Spencer5f016e22007-07-11 17:01:13 +0000801void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000802 unsigned value = Node->getValue();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000803
804 switch (Node->getKind()) {
805 case CharacterLiteral::Ascii: break; // no prefix.
806 case CharacterLiteral::Wide: OS << 'L'; break;
807 case CharacterLiteral::UTF16: OS << 'u'; break;
808 case CharacterLiteral::UTF32: OS << 'U'; break;
809 }
810
Chris Lattner8bf9f072007-07-13 23:58:20 +0000811 switch (value) {
812 case '\\':
813 OS << "'\\\\'";
814 break;
815 case '\'':
816 OS << "'\\''";
817 break;
818 case '\a':
819 // TODO: K&R: the meaning of '\\a' is different in traditional C
820 OS << "'\\a'";
821 break;
822 case '\b':
823 OS << "'\\b'";
824 break;
825 // Nonstandard escape sequence.
826 /*case '\e':
827 OS << "'\\e'";
828 break;*/
829 case '\f':
830 OS << "'\\f'";
831 break;
832 case '\n':
833 OS << "'\\n'";
834 break;
835 case '\r':
836 OS << "'\\r'";
837 break;
838 case '\t':
839 OS << "'\\t'";
840 break;
841 case '\v':
842 OS << "'\\v'";
843 break;
844 default:
Jordan Rose6d4f7342013-02-08 22:30:27 +0000845 if (value < 256 && isPrintable((unsigned char)value))
Chris Lattner8bf9f072007-07-13 23:58:20 +0000846 OS << "'" << (char)value << "'";
Jordan Rose6d4f7342013-02-08 22:30:27 +0000847 else if (value < 256)
848 OS << "'\\x" << llvm::format("%02x", value) << "'";
849 else if (value <= 0xFFFF)
850 OS << "'\\u" << llvm::format("%04x", value) << "'";
851 else
852 OS << "'\\U" << llvm::format("%08x", value) << "'";
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000853 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000854}
855
856void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
857 bool isSigned = Node->getType()->isSignedIntegerType();
858 OS << Node->getValue().toString(10, isSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Reid Spencer5f016e22007-07-11 17:01:13 +0000860 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall183700f2009-09-21 23:43:11 +0000861 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000862 default: llvm_unreachable("Unexpected type for integer literal!");
Richard Trieu11cbe2a2011-11-07 18:40:31 +0000863 // FIXME: The Short and UShort cases are to handle cases where a short
864 // integeral literal is formed during template instantiation. They should
865 // be removed when template instantiation no longer needs integer literals.
866 case BuiltinType::Short:
867 case BuiltinType::UShort:
Reid Spencer5f016e22007-07-11 17:01:13 +0000868 case BuiltinType::Int: break; // no suffix.
869 case BuiltinType::UInt: OS << 'U'; break;
870 case BuiltinType::Long: OS << 'L'; break;
871 case BuiltinType::ULong: OS << "UL"; break;
872 case BuiltinType::LongLong: OS << "LL"; break;
873 case BuiltinType::ULongLong: OS << "ULL"; break;
Richard Trieu11cbe2a2011-11-07 18:40:31 +0000874 case BuiltinType::Int128: OS << "i128"; break;
875 case BuiltinType::UInt128: OS << "Ui128"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000876 }
877}
Benjamin Kramera0a3c902012-09-20 14:07:17 +0000878
879static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
880 bool PrintSuffix) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000881 SmallString<16> Str;
Eli Friedmanb3909212011-10-05 20:32:03 +0000882 Node->getValue().toString(Str);
883 OS << Str;
Benjamin Kramera0a3c902012-09-20 14:07:17 +0000884 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
885 OS << '.'; // Trailing dot in order to separate from ints.
886
887 if (!PrintSuffix)
888 return;
889
890 // Emit suffixes. Float literals are always a builtin float type.
891 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
892 default: llvm_unreachable("Unexpected type for float literal!");
893 case BuiltinType::Half: break; // FIXME: suffix?
894 case BuiltinType::Double: break; // no suffix.
895 case BuiltinType::Float: OS << 'F'; break;
896 case BuiltinType::LongDouble: OS << 'L'; break;
897 }
898}
899
900void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
901 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000902}
Chris Lattner5d661452007-08-26 03:42:43 +0000903
904void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
905 PrintExpr(Node->getSubExpr());
906 OS << "i";
907}
908
Reid Spencer5f016e22007-07-11 17:01:13 +0000909void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Richard Trieu8ab09da2012-06-13 20:25:24 +0000910 Str->outputString(OS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000911}
912void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
913 OS << "(";
914 PrintExpr(Node->getSubExpr());
915 OS << ")";
916}
917void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000918 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000919 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Eli Friedman7df71ac2009-06-14 22:39:26 +0000921 // Print a space if this is an "identifier operator" like __real, or if
922 // it might be concatenated incorrectly like '+'.
Chris Lattner296bf192007-08-23 21:46:40 +0000923 switch (Node->getOpcode()) {
924 default: break;
John McCall2de56d12010-08-25 11:45:40 +0000925 case UO_Real:
926 case UO_Imag:
927 case UO_Extension:
Chris Lattner296bf192007-08-23 21:46:40 +0000928 OS << ' ';
929 break;
John McCall2de56d12010-08-25 11:45:40 +0000930 case UO_Plus:
931 case UO_Minus:
Eli Friedman7df71ac2009-06-14 22:39:26 +0000932 if (isa<UnaryOperator>(Node->getSubExpr()))
933 OS << ' ';
934 break;
Chris Lattner296bf192007-08-23 21:46:40 +0000935 }
936 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000937 PrintExpr(Node->getSubExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Reid Spencer5f016e22007-07-11 17:01:13 +0000939 if (Node->isPostfix())
940 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000941}
Chris Lattner704fe352007-08-30 17:59:59 +0000942
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000943void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
944 OS << "__builtin_offsetof(";
Benjamin Kramer9d4df462013-02-22 14:19:01 +0000945 Node->getTypeSourceInfo()->getType().print(OS, Policy);
946 OS << ", ";
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000947 bool PrintedSomething = false;
948 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
949 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
950 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
951 // Array node
952 OS << "[";
953 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
954 OS << "]";
955 PrintedSomething = true;
956 continue;
957 }
Douglas Gregorcc8a5d52010-04-29 00:18:15 +0000958
959 // Skip implicit base indirections.
960 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
961 continue;
962
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000963 // Field or identifier node.
964 IdentifierInfo *Id = ON.getFieldName();
965 if (!Id)
966 continue;
Sean Huntc3021132010-05-05 15:23:54 +0000967
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000968 if (PrintedSomething)
969 OS << ".";
970 else
971 PrintedSomething = true;
Sean Huntc3021132010-05-05 15:23:54 +0000972 OS << Id->getName();
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000973 }
974 OS << ")";
975}
976
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000977void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
978 switch(Node->getKind()) {
979 case UETT_SizeOf:
980 OS << "sizeof";
981 break;
982 case UETT_AlignOf:
Jordan Rosef70a8862012-06-30 21:33:57 +0000983 if (Policy.LangOpts.CPlusPlus)
984 OS << "alignof";
985 else if (Policy.LangOpts.C11)
986 OS << "_Alignof";
987 else
988 OS << "__alignof";
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000989 break;
990 case UETT_VecStep:
991 OS << "vec_step";
992 break;
993 }
Benjamin Kramer9d4df462013-02-22 14:19:01 +0000994 if (Node->isArgumentType()) {
995 OS << '(';
996 Node->getArgumentType().print(OS, Policy);
997 OS << ')';
998 } else {
Sebastian Redl05189992008-11-11 17:56:53 +0000999 OS << " ";
1000 PrintExpr(Node->getArgumentExpr());
1001 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001002}
Peter Collingbournef111d932011-04-15 00:35:48 +00001003
1004void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1005 OS << "_Generic(";
1006 PrintExpr(Node->getControllingExpr());
1007 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
1008 OS << ", ";
1009 QualType T = Node->getAssocType(i);
1010 if (T.isNull())
1011 OS << "default";
1012 else
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001013 T.print(OS, Policy);
Peter Collingbournef111d932011-04-15 00:35:48 +00001014 OS << ": ";
1015 PrintExpr(Node->getAssocExpr(i));
1016 }
1017 OS << ")";
1018}
1019
Reid Spencer5f016e22007-07-11 17:01:13 +00001020void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +00001021 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +00001022 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +00001023 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +00001024 OS << "]";
1025}
1026
Peter Collingbourned64e2372011-02-08 21:17:54 +00001027void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001028 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +00001029 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1030 // Don't print any defaulted arguments
1031 break;
1032 }
1033
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 if (i) OS << ", ";
1035 PrintExpr(Call->getArg(i));
1036 }
Peter Collingbourned64e2372011-02-08 21:17:54 +00001037}
1038
1039void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1040 PrintExpr(Call->getCallee());
1041 OS << "(";
1042 PrintCallArgs(Call);
Reid Spencer5f016e22007-07-11 17:01:13 +00001043 OS << ")";
1044}
1045void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +00001046 // FIXME: Suppress printing implicit bases (like "this")
1047 PrintExpr(Node->getBase());
David Blaikie383ec172012-11-12 19:12:12 +00001048
1049 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
David Blaikie731de312012-11-12 19:32:32 +00001050 FieldDecl *ParentDecl = ParentMember
1051 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : NULL;
David Blaikie383ec172012-11-12 19:12:12 +00001052
David Blaikie731de312012-11-12 19:32:32 +00001053 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
David Blaikie383ec172012-11-12 19:12:12 +00001054 OS << (Node->isArrow() ? "->" : ".");
David Blaikie383ec172012-11-12 19:12:12 +00001055
Fariborz Jahanian97fd83a2010-01-11 21:17:32 +00001056 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1057 if (FD->isAnonymousStructOrUnion())
1058 return;
David Blaikie383ec172012-11-12 19:12:12 +00001059
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001060 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1061 Qualifier->print(OS, Policy);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001062 if (Node->hasTemplateKeyword())
1063 OS << "template ";
Abramo Bagnara25777432010-08-11 22:01:17 +00001064 OS << Node->getMemberNameInfo();
John McCall096832c2010-08-19 23:49:38 +00001065 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer5eada842013-02-22 15:46:01 +00001066 TemplateSpecializationType::PrintTemplateArgumentList(
1067 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001068}
Steve Narofff242b1b2009-07-24 17:54:45 +00001069void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1070 PrintExpr(Node->getBase());
1071 OS << (Node->isArrow() ? "->isa" : ".isa");
1072}
1073
Nate Begeman213541a2008-04-18 23:10:10 +00001074void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +00001075 PrintExpr(Node->getBase());
1076 OS << ".";
1077 OS << Node->getAccessor().getName();
1078}
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001079void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001080 OS << '(';
1081 Node->getTypeAsWritten().print(OS, Policy);
1082 OS << ')';
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 PrintExpr(Node->getSubExpr());
1084}
Steve Naroffaff1edd2007-07-19 21:32:11 +00001085void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001086 OS << '(';
1087 Node->getType().print(OS, Policy);
1088 OS << ')';
Steve Naroffaff1edd2007-07-19 21:32:11 +00001089 PrintExpr(Node->getInitializer());
1090}
Steve Naroff49b45262007-07-13 16:58:59 +00001091void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001092 // No need to print anything, simply forward to the subexpression.
Steve Naroff90045e82007-07-13 23:32:42 +00001093 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +00001094}
Reid Spencer5f016e22007-07-11 17:01:13 +00001095void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1096 PrintExpr(Node->getLHS());
1097 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1098 PrintExpr(Node->getRHS());
1099}
Chris Lattnereb14fe82007-08-25 02:00:02 +00001100void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1101 PrintExpr(Node->getLHS());
1102 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1103 PrintExpr(Node->getRHS());
1104}
Reid Spencer5f016e22007-07-11 17:01:13 +00001105void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1106 PrintExpr(Node->getCond());
John McCall56ca35d2011-02-17 10:25:35 +00001107 OS << " ? ";
1108 PrintExpr(Node->getLHS());
1109 OS << " : ";
Reid Spencer5f016e22007-07-11 17:01:13 +00001110 PrintExpr(Node->getRHS());
1111}
1112
1113// GNU extensions.
1114
John McCall56ca35d2011-02-17 10:25:35 +00001115void
1116StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1117 PrintExpr(Node->getCommon());
1118 OS << " ?: ";
1119 PrintExpr(Node->getFalseExpr());
1120}
Chris Lattner6481a572007-08-03 17:31:20 +00001121void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001122 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001123}
1124
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001125void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1126 OS << "(";
1127 PrintRawCompoundStmt(E->getSubStmt());
1128 OS << ")";
1129}
1130
Steve Naroffd04fdd52007-08-03 21:21:27 +00001131void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1132 OS << "__builtin_choose_expr(";
1133 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +00001134 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +00001135 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +00001136 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +00001137 PrintExpr(Node->getRHS());
1138 OS << ")";
1139}
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001140
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001141void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1142 OS << "__null";
1143}
1144
Eli Friedmand38617c2008-05-14 19:38:39 +00001145void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1146 OS << "__builtin_shufflevector(";
1147 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1148 if (i) OS << ", ";
1149 PrintExpr(Node->getExpr(i));
1150 }
1151 OS << ")";
1152}
1153
Hal Finkel414a1bd2013-09-18 03:29:45 +00001154void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1155 OS << "__builtin_convertvector(";
1156 PrintExpr(Node->getSrcExpr());
1157 OS << ", ";
1158 Node->getType().print(OS, Policy);
1159 OS << ")";
1160}
1161
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001162void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor64f65002009-05-30 00:56:08 +00001163 if (Node->getSyntacticForm()) {
1164 Visit(Node->getSyntacticForm());
1165 return;
1166 }
1167
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001168 OS << "{ ";
1169 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1170 if (i) OS << ", ";
Douglas Gregor4c678342009-01-28 21:54:33 +00001171 if (Node->getInit(i))
1172 PrintExpr(Node->getInit(i));
1173 else
1174 OS << "0";
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001175 }
1176 OS << " }";
1177}
1178
Nate Begeman2ef13e52009-08-10 23:49:36 +00001179void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1180 OS << "( ";
1181 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1182 if (i) OS << ", ";
1183 PrintExpr(Node->getExpr(i));
1184 }
1185 OS << " )";
1186}
1187
Douglas Gregor05c13a32009-01-22 00:58:24 +00001188void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor4c678342009-01-28 21:54:33 +00001189 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1190 DEnd = Node->designators_end();
1191 D != DEnd; ++D) {
1192 if (D->isFieldDesignator()) {
1193 if (D->getDotLoc().isInvalid())
1194 OS << D->getFieldName()->getName() << ":";
1195 else
1196 OS << "." << D->getFieldName()->getName();
1197 } else {
1198 OS << "[";
1199 if (D->isArrayDesignator()) {
1200 PrintExpr(Node->getArrayIndex(*D));
1201 } else {
1202 PrintExpr(Node->getArrayRangeStart(*D));
1203 OS << " ... ";
Mike Stump1eb44332009-09-09 15:08:12 +00001204 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor4c678342009-01-28 21:54:33 +00001205 }
1206 OS << "]";
1207 }
1208 }
1209
1210 OS << " = ";
1211 PrintExpr(Node->getInit());
Douglas Gregor05c13a32009-01-22 00:58:24 +00001212}
1213
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001214void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001215 if (Policy.LangOpts.CPlusPlus) {
1216 OS << "/*implicit*/";
1217 Node->getType().print(OS, Policy);
1218 OS << "()";
1219 } else {
1220 OS << "/*implicit*/(";
1221 Node->getType().print(OS, Policy);
1222 OS << ')';
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001223 if (Node->getType()->isRecordType())
1224 OS << "{}";
1225 else
1226 OS << 0;
1227 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001228}
1229
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001230void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman42f42c02009-05-30 04:20:30 +00001231 OS << "__builtin_va_arg(";
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001232 PrintExpr(Node->getSubExpr());
1233 OS << ", ";
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001234 Node->getType().print(OS, Policy);
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001235 OS << ")";
1236}
1237
John McCall4b9c2d22011-11-06 09:01:30 +00001238void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1239 PrintExpr(Node->getSyntacticForm());
1240}
1241
Eli Friedman276b0612011-10-11 02:20:01 +00001242void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Eli Friedman9e3c20b2011-10-11 20:00:47 +00001243 const char *Name = 0;
Eli Friedman276b0612011-10-11 02:20:01 +00001244 switch (Node->getOp()) {
Richard Smithff34d402012-04-12 05:08:17 +00001245#define BUILTIN(ID, TYPE, ATTRS)
1246#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1247 case AtomicExpr::AO ## ID: \
1248 Name = #ID "("; \
1249 break;
1250#include "clang/Basic/Builtins.def"
Eli Friedman276b0612011-10-11 02:20:01 +00001251 }
1252 OS << Name;
Richard Smithff34d402012-04-12 05:08:17 +00001253
1254 // AtomicExpr stores its subexpressions in a permuted order.
Eli Friedman276b0612011-10-11 02:20:01 +00001255 PrintExpr(Node->getPtr());
Richard Smithff34d402012-04-12 05:08:17 +00001256 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1257 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
Eli Friedman276b0612011-10-11 02:20:01 +00001258 OS << ", ";
Richard Smith28fff532013-05-01 19:02:43 +00001259 PrintExpr(Node->getVal1());
Eli Friedman276b0612011-10-11 02:20:01 +00001260 }
Richard Smithff34d402012-04-12 05:08:17 +00001261 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1262 Node->isCmpXChg()) {
Eli Friedman276b0612011-10-11 02:20:01 +00001263 OS << ", ";
Richard Smith28fff532013-05-01 19:02:43 +00001264 PrintExpr(Node->getVal2());
Eli Friedman276b0612011-10-11 02:20:01 +00001265 }
Richard Smithff34d402012-04-12 05:08:17 +00001266 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1267 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
Richard Smithff34d402012-04-12 05:08:17 +00001268 OS << ", ";
Richard Smith28fff532013-05-01 19:02:43 +00001269 PrintExpr(Node->getWeak());
Richard Smithff34d402012-04-12 05:08:17 +00001270 }
Richard Smith28fff532013-05-01 19:02:43 +00001271 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1272 OS << ", ";
David Chisnall7a7ee302012-01-16 17:27:18 +00001273 PrintExpr(Node->getOrder());
Richard Smith28fff532013-05-01 19:02:43 +00001274 }
Eli Friedman276b0612011-10-11 02:20:01 +00001275 if (Node->isCmpXChg()) {
1276 OS << ", ";
1277 PrintExpr(Node->getOrderFail());
1278 }
1279 OS << ")";
1280}
1281
Reid Spencer5f016e22007-07-11 17:01:13 +00001282// C++
Douglas Gregorb4609802008-11-14 16:09:21 +00001283void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1284 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1285 "",
1286#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1287 Spelling,
1288#include "clang/Basic/OperatorKinds.def"
1289 };
1290
1291 OverloadedOperatorKind Kind = Node->getOperator();
1292 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1293 if (Node->getNumArgs() == 1) {
1294 OS << OpStrings[Kind] << ' ';
1295 PrintExpr(Node->getArg(0));
1296 } else {
1297 PrintExpr(Node->getArg(0));
1298 OS << ' ' << OpStrings[Kind];
1299 }
Eli Friedmana143a9d2012-10-12 22:45:14 +00001300 } else if (Kind == OO_Arrow) {
1301 PrintExpr(Node->getArg(0));
Douglas Gregorb4609802008-11-14 16:09:21 +00001302 } else if (Kind == OO_Call) {
1303 PrintExpr(Node->getArg(0));
1304 OS << '(';
1305 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1306 if (ArgIdx > 1)
1307 OS << ", ";
1308 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1309 PrintExpr(Node->getArg(ArgIdx));
1310 }
1311 OS << ')';
1312 } else if (Kind == OO_Subscript) {
1313 PrintExpr(Node->getArg(0));
1314 OS << '[';
1315 PrintExpr(Node->getArg(1));
1316 OS << ']';
1317 } else if (Node->getNumArgs() == 1) {
1318 OS << OpStrings[Kind] << ' ';
1319 PrintExpr(Node->getArg(0));
1320 } else if (Node->getNumArgs() == 2) {
1321 PrintExpr(Node->getArg(0));
1322 OS << ' ' << OpStrings[Kind] << ' ';
1323 PrintExpr(Node->getArg(1));
1324 } else {
David Blaikieb219cfc2011-09-23 05:06:16 +00001325 llvm_unreachable("unknown overloaded operator");
Douglas Gregorb4609802008-11-14 16:09:21 +00001326 }
1327}
Reid Spencer5f016e22007-07-11 17:01:13 +00001328
Douglas Gregor88a35142008-12-22 05:46:06 +00001329void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001330 // If we have a conversion operator call only print the argument.
1331 CXXMethodDecl *MD = Node->getMethodDecl();
1332 if (MD && isa<CXXConversionDecl>(MD)) {
1333 PrintExpr(Node->getImplicitObjectArgument());
1334 return;
1335 }
Douglas Gregor88a35142008-12-22 05:46:06 +00001336 VisitCallExpr(cast<CallExpr>(Node));
1337}
1338
Peter Collingbournee08ce652011-02-09 21:07:24 +00001339void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1340 PrintExpr(Node->getCallee());
1341 OS << "<<<";
1342 PrintCallArgs(Node->getConfig());
1343 OS << ">>>(";
1344 PrintCallArgs(Node);
1345 OS << ")";
1346}
1347
Douglas Gregor49badde2008-10-27 19:41:14 +00001348void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1349 OS << Node->getCastName() << '<';
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001350 Node->getTypeAsWritten().print(OS, Policy);
1351 OS << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +00001352 PrintExpr(Node->getSubExpr());
1353 OS << ")";
1354}
1355
Douglas Gregor49badde2008-10-27 19:41:14 +00001356void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1357 VisitCXXNamedCastExpr(Node);
1358}
1359
1360void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1361 VisitCXXNamedCastExpr(Node);
1362}
1363
1364void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1365 VisitCXXNamedCastExpr(Node);
1366}
1367
1368void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1369 VisitCXXNamedCastExpr(Node);
1370}
1371
Sebastian Redlc42e1182008-11-11 11:37:55 +00001372void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1373 OS << "typeid(";
1374 if (Node->isTypeOperand()) {
David Majnemerfe16aa32013-09-27 07:04:31 +00001375 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001376 } else {
1377 PrintExpr(Node->getExprOperand());
1378 }
1379 OS << ")";
1380}
1381
Francois Pichet01b7c302010-09-08 12:20:18 +00001382void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1383 OS << "__uuidof(";
1384 if (Node->isTypeOperand()) {
David Majnemerfe16aa32013-09-27 07:04:31 +00001385 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Francois Pichet01b7c302010-09-08 12:20:18 +00001386 } else {
1387 PrintExpr(Node->getExprOperand());
1388 }
1389 OS << ")";
1390}
1391
John McCall76da55d2013-04-16 07:28:30 +00001392void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1393 PrintExpr(Node->getBaseExpr());
1394 if (Node->isArrow())
1395 OS << "->";
1396 else
1397 OS << ".";
1398 if (NestedNameSpecifier *Qualifier =
1399 Node->getQualifierLoc().getNestedNameSpecifier())
1400 Qualifier->print(OS, Policy);
1401 OS << Node->getPropertyDecl()->getDeclName();
1402}
1403
Richard Smith9fcce652012-03-07 08:35:16 +00001404void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1405 switch (Node->getLiteralOperatorKind()) {
1406 case UserDefinedLiteral::LOK_Raw:
Richard Smithef9f2982012-03-09 10:10:02 +00001407 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
Richard Smith9fcce652012-03-07 08:35:16 +00001408 break;
1409 case UserDefinedLiteral::LOK_Template: {
Richard Smithef9f2982012-03-09 10:10:02 +00001410 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1411 const TemplateArgumentList *Args =
1412 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1413 assert(Args);
1414 const TemplateArgument &Pack = Args->get(0);
1415 for (TemplateArgument::pack_iterator I = Pack.pack_begin(),
1416 E = Pack.pack_end(); I != E; ++I) {
Benjamin Kramer85524372012-06-07 15:09:51 +00001417 char C = (char)I->getAsIntegral().getZExtValue();
Richard Smith9fcce652012-03-07 08:35:16 +00001418 OS << C;
1419 }
1420 break;
1421 }
Richard Smith91688302012-03-08 09:02:38 +00001422 case UserDefinedLiteral::LOK_Integer: {
1423 // Print integer literal without suffix.
1424 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1425 OS << Int->getValue().toString(10, /*isSigned*/false);
1426 break;
1427 }
Benjamin Kramera0a3c902012-09-20 14:07:17 +00001428 case UserDefinedLiteral::LOK_Floating: {
1429 // Print floating literal without suffix.
1430 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1431 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1432 break;
1433 }
Richard Smith9fcce652012-03-07 08:35:16 +00001434 case UserDefinedLiteral::LOK_String:
1435 case UserDefinedLiteral::LOK_Character:
1436 PrintExpr(Node->getCookedLiteral());
1437 break;
1438 }
1439 OS << Node->getUDSuffix()->getName();
1440}
1441
Reid Spencer5f016e22007-07-11 17:01:13 +00001442void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1443 OS << (Node->getValue() ? "true" : "false");
1444}
1445
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001446void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1447 OS << "nullptr";
1448}
1449
Douglas Gregor796da182008-11-04 14:32:21 +00001450void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1451 OS << "this";
1452}
1453
Chris Lattner50dd2892008-02-26 00:51:44 +00001454void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1455 if (Node->getSubExpr() == 0)
1456 OS << "throw";
1457 else {
1458 OS << "throw ";
1459 PrintExpr(Node->getSubExpr());
1460 }
1461}
1462
Chris Lattner04421082008-04-08 04:40:51 +00001463void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
Richard Smithc3bf52c2013-04-20 22:23:05 +00001464 // Nothing to print: we picked up the default argument.
1465}
1466
1467void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1468 // Nothing to print: we picked up the default initializer.
Chris Lattner04421082008-04-08 04:40:51 +00001469}
1470
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001471void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001472 Node->getType().print(OS, Policy);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001473 OS << "(";
1474 PrintExpr(Node->getSubExpr());
1475 OS << ")";
1476}
1477
Anders Carlssonfceb0a82009-05-30 20:03:25 +00001478void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1479 PrintExpr(Node->getSubExpr());
1480}
1481
Douglas Gregor506ae412009-01-16 18:33:17 +00001482void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001483 Node->getType().print(OS, Policy);
Douglas Gregor506ae412009-01-16 18:33:17 +00001484 OS << "(";
1485 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001486 ArgEnd = Node->arg_end();
Douglas Gregor506ae412009-01-16 18:33:17 +00001487 Arg != ArgEnd; ++Arg) {
Rafael Espindolad3bb9ff2013-05-24 16:11:44 +00001488 if (Arg->isDefaultArgument())
1489 break;
Douglas Gregor506ae412009-01-16 18:33:17 +00001490 if (Arg != Node->arg_begin())
1491 OS << ", ";
1492 PrintExpr(*Arg);
1493 }
1494 OS << ")";
1495}
1496
Douglas Gregor01d08012012-02-07 10:09:13 +00001497void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1498 OS << '[';
1499 bool NeedComma = false;
1500 switch (Node->getCaptureDefault()) {
1501 case LCD_None:
1502 break;
1503
1504 case LCD_ByCopy:
1505 OS << '=';
1506 NeedComma = true;
1507 break;
1508
1509 case LCD_ByRef:
1510 OS << '&';
1511 NeedComma = true;
1512 break;
1513 }
1514 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1515 CEnd = Node->explicit_capture_end();
1516 C != CEnd;
1517 ++C) {
1518 if (NeedComma)
1519 OS << ", ";
1520 NeedComma = true;
1521
1522 switch (C->getCaptureKind()) {
1523 case LCK_This:
1524 OS << "this";
1525 break;
1526
1527 case LCK_ByRef:
Richard Smith04fa7a32013-09-28 04:02:39 +00001528 if (Node->getCaptureDefault() != LCD_ByRef || C->isInitCapture())
Douglas Gregor01d08012012-02-07 10:09:13 +00001529 OS << '&';
1530 OS << C->getCapturedVar()->getName();
1531 break;
1532
1533 case LCK_ByCopy:
Douglas Gregor01d08012012-02-07 10:09:13 +00001534 OS << C->getCapturedVar()->getName();
1535 break;
1536 }
Richard Smith04fa7a32013-09-28 04:02:39 +00001537
1538 if (C->isInitCapture())
1539 PrintExpr(C->getCapturedVar()->getInit());
Douglas Gregor01d08012012-02-07 10:09:13 +00001540 }
1541 OS << ']';
1542
1543 if (Node->hasExplicitParameters()) {
1544 OS << " (";
1545 CXXMethodDecl *Method = Node->getCallOperator();
1546 NeedComma = false;
Stephen Hines651f13c2014-04-23 16:59:28 -07001547 for (auto P : Method->params()) {
Douglas Gregor01d08012012-02-07 10:09:13 +00001548 if (NeedComma) {
1549 OS << ", ";
1550 } else {
1551 NeedComma = true;
1552 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001553 std::string ParamStr = P->getNameAsString();
1554 P->getOriginalType().print(OS, Policy, ParamStr);
Douglas Gregor01d08012012-02-07 10:09:13 +00001555 }
1556 if (Method->isVariadic()) {
1557 if (NeedComma)
1558 OS << ", ";
1559 OS << "...";
1560 }
1561 OS << ')';
1562
1563 if (Node->isMutable())
1564 OS << " mutable";
1565
1566 const FunctionProtoType *Proto
1567 = Method->getType()->getAs<FunctionProtoType>();
Benjamin Kramer5eada842013-02-22 15:46:01 +00001568 Proto->printExceptionSpecification(OS, Policy);
Douglas Gregor01d08012012-02-07 10:09:13 +00001569
Bill Wendlingad017fa2012-12-20 19:22:21 +00001570 // FIXME: Attributes
Douglas Gregor01d08012012-02-07 10:09:13 +00001571
Douglas Gregordfca6f52012-02-13 22:00:16 +00001572 // Print the trailing return type if it was specified in the source.
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001573 if (Node->hasExplicitResultType()) {
1574 OS << " -> ";
Stephen Hines651f13c2014-04-23 16:59:28 -07001575 Proto->getReturnType().print(OS, Policy);
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001576 }
Douglas Gregor01d08012012-02-07 10:09:13 +00001577 }
1578
1579 // Print the body.
1580 CompoundStmt *Body = Node->getBody();
1581 OS << ' ';
1582 PrintStmt(Body);
1583}
1584
Douglas Gregored8abf12010-07-08 06:14:04 +00001585void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregorab6677e2010-09-08 00:15:04 +00001586 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001587 TSInfo->getType().print(OS, Policy);
Douglas Gregorab6677e2010-09-08 00:15:04 +00001588 else
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001589 Node->getType().print(OS, Policy);
1590 OS << "()";
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001591}
1592
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001593void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1594 if (E->isGlobalNew())
1595 OS << "::";
1596 OS << "new ";
1597 unsigned NumPlace = E->getNumPlacementArgs();
Eli Friedmand03ef042012-10-18 20:54:37 +00001598 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001599 OS << "(";
1600 PrintExpr(E->getPlacementArg(0));
1601 for (unsigned i = 1; i < NumPlace; ++i) {
Eli Friedmand03ef042012-10-18 20:54:37 +00001602 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1603 break;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001604 OS << ", ";
1605 PrintExpr(E->getPlacementArg(i));
1606 }
1607 OS << ") ";
1608 }
1609 if (E->isParenTypeId())
1610 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001611 std::string TypeS;
1612 if (Expr *Size = E->getArraySize()) {
1613 llvm::raw_string_ostream s(TypeS);
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001614 s << '[';
Richard Smithd1420c62012-08-16 03:56:14 +00001615 Size->printPretty(s, Helper, Policy);
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001616 s << ']';
Sebastian Redl6fec6482008-12-02 22:08:59 +00001617 }
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001618 E->getAllocatedType().print(OS, Policy, TypeS);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001619 if (E->isParenTypeId())
1620 OS << ")";
1621
Sebastian Redl2aed8b82012-02-16 12:22:20 +00001622 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1623 if (InitStyle) {
1624 if (InitStyle == CXXNewExpr::CallInit)
1625 OS << "(";
1626 PrintExpr(E->getInitializer());
1627 if (InitStyle == CXXNewExpr::CallInit)
1628 OS << ")";
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001629 }
1630}
1631
1632void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1633 if (E->isGlobalDelete())
1634 OS << "::";
1635 OS << "delete ";
1636 if (E->isArrayForm())
1637 OS << "[] ";
1638 PrintExpr(E->getArgument());
1639}
1640
Douglas Gregora71d8192009-09-04 17:36:40 +00001641void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1642 PrintExpr(E->getBase());
1643 if (E->isArrow())
1644 OS << "->";
1645 else
1646 OS << '.';
1647 if (E->getQualifier())
1648 E->getQualifier()->print(OS, Policy);
Eli Friedmana7a38cb2012-10-23 20:26:57 +00001649 OS << "~";
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Douglas Gregora2e7dd22010-02-25 01:56:36 +00001651 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1652 OS << II->getName();
1653 else
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001654 E->getDestroyedType().print(OS, Policy);
Douglas Gregora71d8192009-09-04 17:36:40 +00001655}
1656
Anders Carlssone349bea2009-04-23 02:32:43 +00001657void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Richard Smithc83c2302012-12-19 01:39:02 +00001658 if (E->isListInitialization())
1659 OS << "{ ";
1660
Douglas Gregord0fb3ad2011-01-24 17:25:03 +00001661 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1662 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1663 // Don't print any defaulted arguments
1664 break;
1665 }
1666
1667 if (i) OS << ", ";
1668 PrintExpr(E->getArg(i));
Fariborz Jahanianc75da512010-01-13 21:41:11 +00001669 }
Richard Smithc83c2302012-12-19 01:39:02 +00001670
1671 if (E->isListInitialization())
1672 OS << " }";
Anders Carlssone349bea2009-04-23 02:32:43 +00001673}
1674
Richard Smith7c3e6152013-06-12 22:31:48 +00001675void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
1676 PrintExpr(E->getSubExpr());
1677}
1678
John McCall4765fa02010-12-06 08:20:24 +00001679void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001680 // Just forward to the subexpression.
Anders Carlsson02bbfa32009-04-24 22:47:04 +00001681 PrintExpr(E->getSubExpr());
1682}
1683
Mike Stump1eb44332009-09-09 15:08:12 +00001684void
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001685StmtPrinter::VisitCXXUnresolvedConstructExpr(
1686 CXXUnresolvedConstructExpr *Node) {
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001687 Node->getTypeAsWritten().print(OS, Policy);
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001688 OS << "(";
1689 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001690 ArgEnd = Node->arg_end();
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001691 Arg != ArgEnd; ++Arg) {
1692 if (Arg != Node->arg_begin())
1693 OS << ", ";
1694 PrintExpr(*Arg);
1695 }
1696 OS << ")";
1697}
1698
John McCall865d4472009-11-19 22:55:06 +00001699void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1700 CXXDependentScopeMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001701 if (!Node->isImplicitAccess()) {
1702 PrintExpr(Node->getBase());
1703 OS << (Node->isArrow() ? "->" : ".");
1704 }
Douglas Gregora38c6872009-09-03 16:14:30 +00001705 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1706 Qualifier->print(OS, Policy);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001707 if (Node->hasTemplateKeyword())
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001708 OS << "template ";
Abramo Bagnara25777432010-08-11 22:01:17 +00001709 OS << Node->getMemberNameInfo();
Benjamin Kramer5eada842013-02-22 15:46:01 +00001710 if (Node->hasExplicitTemplateArgs())
1711 TemplateSpecializationType::PrintTemplateArgumentList(
1712 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregor1c0ca592009-05-22 21:13:27 +00001713}
1714
John McCall129e2df2009-11-30 22:42:35 +00001715void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001716 if (!Node->isImplicitAccess()) {
1717 PrintExpr(Node->getBase());
1718 OS << (Node->isArrow() ? "->" : ".");
1719 }
John McCall129e2df2009-11-30 22:42:35 +00001720 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1721 Qualifier->print(OS, Policy);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001722 if (Node->hasTemplateKeyword())
1723 OS << "template ";
Abramo Bagnara25777432010-08-11 22:01:17 +00001724 OS << Node->getMemberNameInfo();
Benjamin Kramer5eada842013-02-22 15:46:01 +00001725 if (Node->hasExplicitTemplateArgs())
1726 TemplateSpecializationType::PrintTemplateArgumentList(
1727 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
John McCall129e2df2009-11-30 22:42:35 +00001728}
1729
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001730static const char *getTypeTraitName(TypeTrait TT) {
1731 switch (TT) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001732#define TYPE_TRAIT_1(Spelling, Name, Key) \
1733case clang::UTT_##Name: return #Spelling;
1734#define TYPE_TRAIT_2(Spelling, Name, Key) \
1735case clang::BTT_##Name: return #Spelling;
1736#define TYPE_TRAIT_N(Spelling, Name, Key) \
1737 case clang::TT_##Name: return #Spelling;
1738#include "clang/Basic/TokenKinds.def"
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001739 }
1740 llvm_unreachable("Type trait not covered by switch");
1741}
1742
John Wiegley21ff2e52011-04-28 00:16:57 +00001743static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1744 switch (ATT) {
1745 case ATT_ArrayRank: return "__array_rank";
1746 case ATT_ArrayExtent: return "__array_extent";
1747 }
Chandler Carrutha280bc82011-05-01 07:23:23 +00001748 llvm_unreachable("Array type trait not covered by switch");
John Wiegley21ff2e52011-04-28 00:16:57 +00001749}
1750
John Wiegley55262202011-04-25 06:54:41 +00001751static const char *getExpressionTraitName(ExpressionTrait ET) {
1752 switch (ET) {
John Wiegley55262202011-04-25 06:54:41 +00001753 case ET_IsLValueExpr: return "__is_lvalue_expr";
1754 case ET_IsRValueExpr: return "__is_rvalue_expr";
1755 }
Chandler Carrutha280bc82011-05-01 07:23:23 +00001756 llvm_unreachable("Expression type trait not covered by switch");
John Wiegley55262202011-04-25 06:54:41 +00001757}
1758
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001759void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1760 OS << getTypeTraitName(E->getTrait()) << "(";
1761 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1762 if (I > 0)
1763 OS << ", ";
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001764 E->getArg(I)->getType().print(OS, Policy);
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001765 }
1766 OS << ")";
1767}
1768
John Wiegley21ff2e52011-04-28 00:16:57 +00001769void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001770 OS << getTypeTraitName(E->getTrait()) << '(';
1771 E->getQueriedType().print(OS, Policy);
1772 OS << ')';
John Wiegley21ff2e52011-04-28 00:16:57 +00001773}
1774
John Wiegley55262202011-04-25 06:54:41 +00001775void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001776 OS << getExpressionTraitName(E->getTrait()) << '(';
1777 PrintExpr(E->getQueriedExpression());
1778 OS << ')';
John Wiegley55262202011-04-25 06:54:41 +00001779}
1780
Sebastian Redl2e156222010-09-10 20:55:43 +00001781void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1782 OS << "noexcept(";
1783 PrintExpr(E->getOperand());
1784 OS << ")";
1785}
1786
Douglas Gregoree8aff02011-01-04 17:33:58 +00001787void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregorbe230c32011-01-03 17:17:50 +00001788 PrintExpr(E->getPattern());
1789 OS << "...";
1790}
1791
Douglas Gregoree8aff02011-01-04 17:33:58 +00001792void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramera59d20b2012-02-07 11:57:57 +00001793 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregoree8aff02011-01-04 17:33:58 +00001794}
1795
Douglas Gregorc7793c72011-01-15 01:15:58 +00001796void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1797 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramera59d20b2012-02-07 11:57:57 +00001798 OS << *Node->getParameterPack();
Douglas Gregorc7793c72011-01-15 01:15:58 +00001799}
1800
John McCall91a57552011-07-15 05:09:51 +00001801void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1802 SubstNonTypeTemplateParmExpr *Node) {
1803 Visit(Node->getReplacement());
1804}
1805
Richard Smith9a4db032012-09-12 00:56:43 +00001806void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
1807 OS << *E->getParameterPack();
1808}
1809
Douglas Gregor03e80032011-06-21 17:03:29 +00001810void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1811 PrintExpr(Node->GetTemporaryExpr());
1812}
1813
Mike Stump1eb44332009-09-09 15:08:12 +00001814// Obj-C
Anders Carlsson55085182007-08-21 17:43:55 +00001815
1816void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1817 OS << "@";
1818 VisitStringLiteral(Node->getString());
1819}
Reid Spencer5f016e22007-07-11 17:01:13 +00001820
Patrick Beardeb382ec2012-04-19 00:25:12 +00001821void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001822 OS << "@";
Patrick Beardeb382ec2012-04-19 00:25:12 +00001823 Visit(E->getSubExpr());
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001824}
1825
1826void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
1827 OS << "@[ ";
1828 StmtRange ch = E->children();
1829 if (ch.first != ch.second) {
1830 while (1) {
1831 Visit(*ch.first);
1832 ++ch.first;
1833 if (ch.first == ch.second) break;
1834 OS << ", ";
1835 }
1836 }
1837 OS << " ]";
1838}
1839
1840void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
1841 OS << "@{ ";
1842 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
1843 if (I > 0)
1844 OS << ", ";
1845
1846 ObjCDictionaryElement Element = E->getKeyValueElement(I);
1847 Visit(Element.Key);
1848 OS << " : ";
1849 Visit(Element.Value);
1850 if (Element.isPackExpansion())
1851 OS << "...";
1852 }
1853 OS << " }";
1854}
1855
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001856void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001857 OS << "@encode(";
1858 Node->getEncodedType().print(OS, Policy);
1859 OS << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001860}
1861
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001862void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001863 OS << "@selector(";
1864 Node->getSelector().print(OS);
1865 OS << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001866}
1867
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001868void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001869 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001870}
1871
Steve Naroff563477d2007-09-18 23:55:05 +00001872void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1873 OS << "[";
Douglas Gregor04badcf2010-04-21 00:45:42 +00001874 switch (Mess->getReceiverKind()) {
1875 case ObjCMessageExpr::Instance:
1876 PrintExpr(Mess->getInstanceReceiver());
1877 break;
1878
1879 case ObjCMessageExpr::Class:
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001880 Mess->getClassReceiver().print(OS, Policy);
Douglas Gregor04badcf2010-04-21 00:45:42 +00001881 break;
1882
1883 case ObjCMessageExpr::SuperInstance:
1884 case ObjCMessageExpr::SuperClass:
1885 OS << "Super";
1886 break;
1887 }
1888
Ted Kremenekc29efd82008-05-02 17:32:38 +00001889 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001890 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001891 if (selector.isUnarySelector()) {
Douglas Gregor813d8342011-02-18 22:29:55 +00001892 OS << selector.getNameForSlot(0);
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001893 } else {
1894 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001895 if (i < selector.getNumArgs()) {
1896 if (i > 0) OS << ' ';
1897 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001898 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001899 else
1900 OS << ":";
1901 }
1902 else OS << ", "; // Handle variadic methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001903
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001904 PrintExpr(Mess->getArg(i));
1905 }
Steve Naroff563477d2007-09-18 23:55:05 +00001906 }
1907 OS << "]";
1908}
1909
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001910void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
1911 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
1912}
1913
John McCallf85e1932011-06-15 23:02:42 +00001914void
1915StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1916 PrintExpr(E->getSubExpr());
1917}
1918
1919void
1920StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001921 OS << '(' << E->getBridgeKindName();
1922 E->getType().print(OS, Policy);
1923 OS << ')';
John McCallf85e1932011-06-15 23:02:42 +00001924 PrintExpr(E->getSubExpr());
1925}
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001926
Steve Naroff4eb206b2008-09-03 18:15:37 +00001927void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001928 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001929 OS << "^";
Mike Stump1eb44332009-09-09 15:08:12 +00001930
Steve Naroff4eb206b2008-09-03 18:15:37 +00001931 const FunctionType *AFT = Node->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Douglas Gregor72564e72009-02-26 23:50:07 +00001933 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001934 OS << "()";
Douglas Gregor72564e72009-02-26 23:50:07 +00001935 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001936 OS << '(';
Steve Naroff56ee6892008-10-08 17:01:13 +00001937 for (BlockDecl::param_iterator AI = BD->param_begin(),
1938 E = BD->param_end(); AI != E; ++AI) {
1939 if (AI != BD->param_begin()) OS << ", ";
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001940 std::string ParamStr = (*AI)->getNameAsString();
1941 (*AI)->getType().print(OS, Policy, ParamStr);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001942 }
Mike Stump1eb44332009-09-09 15:08:12 +00001943
Douglas Gregor72564e72009-02-26 23:50:07 +00001944 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001945 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001946 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001947 OS << "...";
1948 }
1949 OS << ')';
1950 }
Fariborz Jahanian3e2fe862012-12-04 21:15:23 +00001951 OS << "{ }";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001952}
1953
Ted Kremenek381c0662011-11-30 22:08:08 +00001954void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
1955 PrintExpr(Node->getSourceExpr());
1956}
John McCall7cd7d1a2010-11-15 23:31:06 +00001957
Tanya Lattner61eee0c2011-06-04 00:47:47 +00001958void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1959 OS << "__builtin_astype(";
1960 PrintExpr(Node->getSrcExpr());
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001961 OS << ", ";
1962 Node->getType().print(OS, Policy);
Tanya Lattner61eee0c2011-06-04 00:47:47 +00001963 OS << ")";
1964}
1965
Reid Spencer5f016e22007-07-11 17:01:13 +00001966//===----------------------------------------------------------------------===//
1967// Stmt method implementations
1968//===----------------------------------------------------------------------===//
1969
Craig Topper8b4b98b2013-08-22 06:02:26 +00001970void Stmt::dumpPretty(const ASTContext &Context) const {
Richard Smithd1420c62012-08-16 03:56:14 +00001971 printPretty(llvm::errs(), 0, PrintingPolicy(Context.getLangOpts()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001972}
1973
Richard Smithd1420c62012-08-16 03:56:14 +00001974void Stmt::printPretty(raw_ostream &OS,
1975 PrinterHelper *Helper,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001976 const PrintingPolicy &Policy,
1977 unsigned Indentation) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001978 if (this == 0) {
1979 OS << "<NULL>";
1980 return;
1981 }
1982
Richard Smithd1420c62012-08-16 03:56:14 +00001983 StmtPrinter P(OS, Helper, Policy, Indentation);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00001984 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00001985}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001986
1987//===----------------------------------------------------------------------===//
1988// PrinterHelper
1989//===----------------------------------------------------------------------===//
1990
1991// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00001992PrinterHelper::~PrinterHelper() {}