blob: 927a679244b5c3e959b75b13a07e303089cf4409 [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) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700171 for (const auto *Attr : Node->getAttrs()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700172 Attr->printPretty(OS, Policy);
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700173 }
174
Richard Smith534986f2012-04-14 00:33:13 +0000175 PrintStmt(Node->getSubStmt(), 0);
176}
177
Reid Spencer5f016e22007-07-11 17:01:13 +0000178void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000179 OS << "if (";
Eli Friedman915c07d2012-10-16 23:45:15 +0000180 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
181 PrintRawDeclStmt(DS);
182 else
183 PrintExpr(If->getCond());
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000184 OS << ')';
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
187 OS << ' ';
188 PrintRawCompoundStmt(CS);
189 OS << (If->getElse() ? ' ' : '\n');
190 } else {
191 OS << '\n';
192 PrintStmt(If->getThen());
193 if (If->getElse()) Indent();
194 }
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 if (Stmt *Else = If->getElse()) {
197 OS << "else";
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
200 OS << ' ';
201 PrintRawCompoundStmt(CS);
202 OS << '\n';
203 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
204 OS << ' ';
205 PrintRawIfStmt(ElseIf);
206 } else {
207 OS << '\n';
208 PrintStmt(If->getElse());
209 }
210 }
211}
212
213void StmtPrinter::VisitIfStmt(IfStmt *If) {
214 Indent();
215 PrintRawIfStmt(If);
216}
217
218void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
219 Indent() << "switch (";
Eli Friedman915c07d2012-10-16 23:45:15 +0000220 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
221 PrintRawDeclStmt(DS);
222 else
223 PrintExpr(Node->getCond());
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 OS << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 // Pretty print compoundstmt bodies (very common).
227 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
228 OS << " ";
229 PrintRawCompoundStmt(CS);
230 OS << "\n";
231 } else {
232 OS << "\n";
233 PrintStmt(Node->getBody());
234 }
235}
236
237void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
238 Indent() << "while (";
Eli Friedman915c07d2012-10-16 23:45:15 +0000239 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
240 PrintRawDeclStmt(DS);
241 else
242 PrintExpr(Node->getCond());
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 OS << ")\n";
244 PrintStmt(Node->getBody());
245}
246
247void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000248 Indent() << "do ";
249 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
250 PrintRawCompoundStmt(CS);
251 OS << " ";
252 } else {
253 OS << "\n";
254 PrintStmt(Node->getBody());
255 Indent();
256 }
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Eli Friedmanb3e22962009-05-17 01:05:34 +0000258 OS << "while (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000259 PrintExpr(Node->getCond());
Eli Friedmanb3e22962009-05-17 01:05:34 +0000260 OS << ");\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000261}
262
263void StmtPrinter::VisitForStmt(ForStmt *Node) {
264 Indent() << "for (";
265 if (Node->getInit()) {
266 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000267 PrintRawDeclStmt(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000268 else
269 PrintExpr(cast<Expr>(Node->getInit()));
270 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000271 OS << ";";
272 if (Node->getCond()) {
273 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000275 }
276 OS << ";";
277 if (Node->getInc()) {
278 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000280 }
281 OS << ") ";
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Chris Lattner8bdcc472007-09-15 21:49:37 +0000283 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
284 PrintRawCompoundStmt(CS);
285 OS << "\n";
286 } else {
287 OS << "\n";
288 PrintStmt(Node->getBody());
289 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000290}
291
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000292void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000293 Indent() << "for (";
294 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000295 PrintRawDeclStmt(DS);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000296 else
297 PrintExpr(cast<Expr>(Node->getElement()));
298 OS << " in ";
299 PrintExpr(Node->getCollection());
300 OS << ") ";
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000302 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
303 PrintRawCompoundStmt(CS);
304 OS << "\n";
305 } else {
306 OS << "\n";
307 PrintStmt(Node->getBody());
308 }
309}
310
Richard Smithad762fc2011-04-14 22:09:26 +0000311void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
312 Indent() << "for (";
313 PrintingPolicy SubPolicy(Policy);
314 SubPolicy.SuppressInitializers = true;
315 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
316 OS << " : ";
317 PrintExpr(Node->getRangeInit());
318 OS << ") {\n";
319 PrintStmt(Node->getBody());
Stephen Hines651f13c2014-04-23 16:59:28 -0700320 Indent() << "}";
321 if (Policy.IncludeNewlines) OS << "\n";
Richard Smithad762fc2011-04-14 22:09:26 +0000322}
323
Douglas Gregorba0513d2011-10-25 01:33:02 +0000324void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
325 Indent();
326 if (Node->isIfExists())
327 OS << "__if_exists (";
328 else
329 OS << "__if_not_exists (";
330
331 if (NestedNameSpecifier *Qualifier
332 = Node->getQualifierLoc().getNestedNameSpecifier())
333 Qualifier->print(OS, Policy);
334
335 OS << Node->getNameInfo() << ") ";
336
337 PrintRawCompoundStmt(Node->getSubStmt());
338}
339
Reid Spencer5f016e22007-07-11 17:01:13 +0000340void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700341 Indent() << "goto " << Node->getLabel()->getName() << ";";
342 if (Policy.IncludeNewlines) OS << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000343}
344
345void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
346 Indent() << "goto *";
347 PrintExpr(Node->getTarget());
Stephen Hines651f13c2014-04-23 16:59:28 -0700348 OS << ";";
349 if (Policy.IncludeNewlines) OS << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000350}
351
352void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700353 Indent() << "continue;";
354 if (Policy.IncludeNewlines) OS << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000355}
356
357void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700358 Indent() << "break;";
359 if (Policy.IncludeNewlines) OS << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000360}
361
362
363void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
364 Indent() << "return";
365 if (Node->getRetValue()) {
366 OS << " ";
367 PrintExpr(Node->getRetValue());
368 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700369 OS << ";";
370 if (Policy.IncludeNewlines) OS << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000371}
372
Chris Lattnerfe795952007-10-29 04:04:16 +0000373
Chad Rosierdf5faf52012-08-25 00:11:56 +0000374void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000375 Indent() << "asm ";
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Anders Carlsson39c47b52007-11-23 23:12:25 +0000377 if (Node->isVolatile())
378 OS << "volatile ";
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Anders Carlsson39c47b52007-11-23 23:12:25 +0000380 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000381 VisitStringLiteral(Node->getAsmString());
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Anders Carlssonb235fc22007-11-22 01:36:19 +0000383 // Outputs
384 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
385 Node->getNumClobbers() != 0)
386 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Anders Carlssonb235fc22007-11-22 01:36:19 +0000388 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
389 if (i != 0)
390 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Anders Carlssonb235fc22007-11-22 01:36:19 +0000392 if (!Node->getOutputName(i).empty()) {
393 OS << '[';
394 OS << Node->getOutputName(i);
395 OS << "] ";
396 }
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Chris Lattnerb3277932009-03-10 04:59:06 +0000398 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000399 OS << " ";
400 Visit(Node->getOutputExpr(i));
401 }
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Anders Carlssonb235fc22007-11-22 01:36:19 +0000403 // Inputs
404 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
405 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Anders Carlssonb235fc22007-11-22 01:36:19 +0000407 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
408 if (i != 0)
409 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Anders Carlssonb235fc22007-11-22 01:36:19 +0000411 if (!Node->getInputName(i).empty()) {
412 OS << '[';
413 OS << Node->getInputName(i);
414 OS << "] ";
415 }
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Chris Lattnerb3277932009-03-10 04:59:06 +0000417 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000418 OS << " ";
419 Visit(Node->getInputExpr(i));
420 }
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Anders Carlssonb235fc22007-11-22 01:36:19 +0000422 // Clobbers
423 if (Node->getNumClobbers() != 0)
424 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Anders Carlssonb235fc22007-11-22 01:36:19 +0000426 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
427 if (i != 0)
428 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Chad Rosier5c7f5942012-08-27 23:28:41 +0000430 VisitStringLiteral(Node->getClobberStringLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000431 }
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Stephen Hines651f13c2014-04-23 16:59:28 -0700433 OS << ");";
434 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000435}
436
Chad Rosier8cd64b42012-06-11 20:47:18 +0000437void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
438 // FIXME: Implement MS style inline asm statement printer.
Chad Rosier7bd092b2012-08-15 16:53:30 +0000439 Indent() << "__asm ";
440 if (Node->hasBraces())
441 OS << "{\n";
John McCallaeeacf72013-05-03 00:10:13 +0000442 OS << Node->getAsmString() << "\n";
Chad Rosier7bd092b2012-08-15 16:53:30 +0000443 if (Node->hasBraces())
444 Indent() << "}\n";
Chad Rosier8cd64b42012-06-11 20:47:18 +0000445}
446
Tareq A. Siraj051303c2013-04-16 18:53:08 +0000447void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
Tareq A. Siraj6afcf882013-04-16 19:37:38 +0000448 PrintStmt(Node->getCapturedDecl()->getBody());
Tareq A. Siraj051303c2013-04-16 18:53:08 +0000449}
450
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000451void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000452 Indent() << "@try";
453 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
454 PrintRawCompoundStmt(TS);
455 OS << "\n";
456 }
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000458 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
459 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000460 Indent() << "@catch(";
Steve Naroff7ba138a2009-03-03 19:52:17 +0000461 if (catchStmt->getCatchParamDecl()) {
462 if (Decl *DS = catchStmt->getCatchParamDecl())
463 PrintRawDecl(DS);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000464 }
465 OS << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000466 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
467 PrintRawCompoundStmt(CS);
468 OS << "\n";
469 }
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000470 }
Mike Stump1eb44332009-09-09 15:08:12 +0000471
472 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
473 Node->getFinallyStmt())) {
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000474 Indent() << "@finally";
475 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000476 OS << "\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000477 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000478}
479
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000480void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000481}
482
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000483void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000484 Indent() << "@catch (...) { /* todo */ } \n";
485}
486
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000487void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000488 Indent() << "@throw";
489 if (Node->getThrowExpr()) {
490 OS << " ";
491 PrintExpr(Node->getThrowExpr());
492 }
493 OS << ";\n";
494}
495
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000496void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000497 Indent() << "@synchronized (";
498 PrintExpr(Node->getSynchExpr());
499 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000500 PrintRawCompoundStmt(Node->getSynchBody());
501 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000502}
503
John McCallf85e1932011-06-15 23:02:42 +0000504void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
505 Indent() << "@autoreleasepool";
506 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
507 OS << "\n";
508}
509
Sebastian Redl8351da02008-12-22 21:35:02 +0000510void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
511 OS << "catch (";
Sebastian Redl4b07b292008-12-22 19:15:10 +0000512 if (Decl *ExDecl = Node->getExceptionDecl())
513 PrintRawDecl(ExDecl);
514 else
515 OS << "...";
516 OS << ") ";
517 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl8351da02008-12-22 21:35:02 +0000518}
519
520void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
521 Indent();
522 PrintRawCXXCatchStmt(Node);
523 OS << "\n";
524}
525
526void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
527 Indent() << "try ";
528 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump1eb44332009-09-09 15:08:12 +0000529 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl8351da02008-12-22 21:35:02 +0000530 OS << " ";
531 PrintRawCXXCatchStmt(Node->getHandler(i));
532 }
Sebastian Redl4b07b292008-12-22 19:15:10 +0000533 OS << "\n";
534}
535
John Wiegley28bbe4b2011-04-28 01:08:34 +0000536void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
537 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
538 PrintRawCompoundStmt(Node->getTryBlock());
539 SEHExceptStmt *E = Node->getExceptHandler();
540 SEHFinallyStmt *F = Node->getFinallyHandler();
541 if(E)
542 PrintRawSEHExceptHandler(E);
543 else {
544 assert(F && "Must have a finally block...");
545 PrintRawSEHFinallyStmt(F);
546 }
547 OS << "\n";
548}
549
550void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
551 OS << "__finally ";
552 PrintRawCompoundStmt(Node->getBlock());
553 OS << "\n";
554}
555
556void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
557 OS << "__except (";
558 VisitExpr(Node->getFilterExpr());
Joao Matos568ba872012-09-04 17:49:35 +0000559 OS << ")\n";
John Wiegley28bbe4b2011-04-28 01:08:34 +0000560 PrintRawCompoundStmt(Node->getBlock());
561 OS << "\n";
562}
563
564void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
565 Indent();
566 PrintRawSEHExceptHandler(Node);
567 OS << "\n";
568}
569
570void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
571 Indent();
572 PrintRawSEHFinallyStmt(Node);
573 OS << "\n";
574}
575
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700576void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
577 Indent() << "__leave;";
578 if (Policy.IncludeNewlines) OS << "\n";
579}
580
Reid Spencer5f016e22007-07-11 17:01:13 +0000581//===----------------------------------------------------------------------===//
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000582// OpenMP clauses printing methods
583//===----------------------------------------------------------------------===//
584
585namespace {
586class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
587 raw_ostream &OS;
Stephen Hines651f13c2014-04-23 16:59:28 -0700588 const PrintingPolicy &Policy;
Alexey Bataev543c4ae2013-09-24 03:17:45 +0000589 /// \brief Process clauses with list of variables.
590 template <typename T>
591 void VisitOMPClauseList(T *Node, char StartSym);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000592public:
Stephen Hines651f13c2014-04-23 16:59:28 -0700593 OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy)
594 : OS(OS), Policy(Policy) { }
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000595#define OPENMP_CLAUSE(Name, Class) \
596 void Visit##Class(Class *S);
597#include "clang/Basic/OpenMPKinds.def"
598};
599
Stephen Hines651f13c2014-04-23 16:59:28 -0700600void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
601 OS << "if(";
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700602 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
Stephen Hines651f13c2014-04-23 16:59:28 -0700603 OS << ")";
604}
605
Stephen Hines176edba2014-12-01 14:53:08 -0800606void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) {
607 OS << "final(";
608 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
609 OS << ")";
610}
611
Stephen Hines651f13c2014-04-23 16:59:28 -0700612void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
613 OS << "num_threads(";
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700614 Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0);
Stephen Hines651f13c2014-04-23 16:59:28 -0700615 OS << ")";
616}
617
618void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
619 OS << "safelen(";
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700620 Node->getSafelen()->printPretty(OS, nullptr, Policy, 0);
621 OS << ")";
622}
623
624void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) {
625 OS << "collapse(";
626 Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0);
Stephen Hines651f13c2014-04-23 16:59:28 -0700627 OS << ")";
628}
629
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000630void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
631 OS << "default("
632 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
633 << ")";
634}
635
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700636void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) {
637 OS << "proc_bind("
638 << getOpenMPSimpleClauseTypeName(OMPC_proc_bind, Node->getProcBindKind())
639 << ")";
640}
641
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700642void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) {
643 OS << "schedule("
644 << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind());
645 if (Node->getChunkSize()) {
646 OS << ", ";
647 Node->getChunkSize()->printPretty(OS, nullptr, Policy);
648 }
649 OS << ")";
650}
651
652void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *) {
653 OS << "ordered";
654}
655
656void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) {
657 OS << "nowait";
658}
659
Stephen Hines176edba2014-12-01 14:53:08 -0800660void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) {
661 OS << "untied";
662}
663
664void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) {
665 OS << "mergeable";
666}
667
668void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; }
669
670void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; }
671
672void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *) {
673 OS << "update";
674}
675
676void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) {
677 OS << "capture";
678}
679
680void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) {
681 OS << "seq_cst";
682}
683
Alexey Bataev543c4ae2013-09-24 03:17:45 +0000684template<typename T>
685void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
686 for (typename T::varlist_iterator I = Node->varlist_begin(),
687 E = Node->varlist_end();
Stephen Hines651f13c2014-04-23 16:59:28 -0700688 I != E; ++I) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700689 assert(*I && "Expected non-null Stmt");
Stephen Hines651f13c2014-04-23 16:59:28 -0700690 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*I)) {
691 OS << (I == Node->varlist_begin() ? StartSym : ',');
692 cast<NamedDecl>(DRE->getDecl())->printQualifiedName(OS);
693 } else {
694 OS << (I == Node->varlist_begin() ? StartSym : ',');
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700695 (*I)->printPretty(OS, nullptr, Policy, 0);
Stephen Hines651f13c2014-04-23 16:59:28 -0700696 }
697 }
Alexey Bataev543c4ae2013-09-24 03:17:45 +0000698}
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000699
700void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
701 if (!Node->varlist_empty()) {
702 OS << "private";
Alexey Bataev543c4ae2013-09-24 03:17:45 +0000703 VisitOMPClauseList(Node, '(');
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000704 OS << ")";
705 }
706}
707
Alexey Bataevd195bc32013-10-01 05:32:34 +0000708void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
709 if (!Node->varlist_empty()) {
710 OS << "firstprivate";
711 VisitOMPClauseList(Node, '(');
712 OS << ")";
713 }
714}
715
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700716void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) {
717 if (!Node->varlist_empty()) {
718 OS << "lastprivate";
719 VisitOMPClauseList(Node, '(');
720 OS << ")";
721 }
722}
723
Alexey Bataev0c018352013-09-06 18:03:48 +0000724void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
725 if (!Node->varlist_empty()) {
726 OS << "shared";
Alexey Bataev543c4ae2013-09-24 03:17:45 +0000727 VisitOMPClauseList(Node, '(');
Alexey Bataev0c018352013-09-06 18:03:48 +0000728 OS << ")";
729 }
730}
731
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700732void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) {
733 if (!Node->varlist_empty()) {
734 OS << "reduction(";
735 NestedNameSpecifier *QualifierLoc =
736 Node->getQualifierLoc().getNestedNameSpecifier();
737 OverloadedOperatorKind OOK =
738 Node->getNameInfo().getName().getCXXOverloadedOperator();
739 if (QualifierLoc == nullptr && OOK != OO_None) {
740 // Print reduction identifier in C format
741 OS << getOperatorSpelling(OOK);
742 } else {
743 // Use C++ format
744 if (QualifierLoc != nullptr)
745 QualifierLoc->print(OS, Policy);
746 OS << Node->getNameInfo();
747 }
748 OS << ":";
749 VisitOMPClauseList(Node, ' ');
750 OS << ")";
751 }
752}
753
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700754void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) {
755 if (!Node->varlist_empty()) {
756 OS << "linear";
757 VisitOMPClauseList(Node, '(');
758 if (Node->getStep() != nullptr) {
759 OS << ": ";
760 Node->getStep()->printPretty(OS, nullptr, Policy, 0);
761 }
762 OS << ")";
763 }
764}
765
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700766void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) {
767 if (!Node->varlist_empty()) {
768 OS << "aligned";
769 VisitOMPClauseList(Node, '(');
770 if (Node->getAlignment() != nullptr) {
771 OS << ": ";
772 Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
773 }
774 OS << ")";
775 }
776}
777
Stephen Hines651f13c2014-04-23 16:59:28 -0700778void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
779 if (!Node->varlist_empty()) {
780 OS << "copyin";
781 VisitOMPClauseList(Node, '(');
782 OS << ")";
783 }
784}
785
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700786void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) {
787 if (!Node->varlist_empty()) {
788 OS << "copyprivate";
789 VisitOMPClauseList(Node, '(');
790 OS << ")";
791 }
792}
793
Stephen Hines176edba2014-12-01 14:53:08 -0800794void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) {
795 if (!Node->varlist_empty()) {
796 VisitOMPClauseList(Node, '(');
797 OS << ")";
798 }
799}
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000800}
801
802//===----------------------------------------------------------------------===//
803// OpenMP directives printing methods
804//===----------------------------------------------------------------------===//
805
Stephen Hines651f13c2014-04-23 16:59:28 -0700806void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) {
807 OMPClausePrinter Printer(OS, Policy);
808 ArrayRef<OMPClause *> Clauses = S->clauses();
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000809 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
810 I != E; ++I)
811 if (*I && !(*I)->isImplicit()) {
812 Printer.Visit(*I);
813 OS << ' ';
814 }
815 OS << "\n";
Stephen Hines176edba2014-12-01 14:53:08 -0800816 if (S->hasAssociatedStmt() && S->getAssociatedStmt()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700817 assert(isa<CapturedStmt>(S->getAssociatedStmt()) &&
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000818 "Expected captured statement!");
Stephen Hines651f13c2014-04-23 16:59:28 -0700819 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt();
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000820 PrintStmt(CS);
821 }
822}
Stephen Hines651f13c2014-04-23 16:59:28 -0700823
824void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
825 Indent() << "#pragma omp parallel ";
826 PrintOMPExecutableDirective(Node);
827}
828
829void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
830 Indent() << "#pragma omp simd ";
831 PrintOMPExecutableDirective(Node);
832}
833
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700834void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
835 Indent() << "#pragma omp for ";
836 PrintOMPExecutableDirective(Node);
837}
838
Stephen Hines176edba2014-12-01 14:53:08 -0800839void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
840 Indent() << "#pragma omp for simd ";
841 PrintOMPExecutableDirective(Node);
842}
843
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700844void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
845 Indent() << "#pragma omp sections ";
846 PrintOMPExecutableDirective(Node);
847}
848
849void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
850 Indent() << "#pragma omp section";
851 PrintOMPExecutableDirective(Node);
852}
853
854void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
855 Indent() << "#pragma omp single ";
856 PrintOMPExecutableDirective(Node);
857}
858
Stephen Hines176edba2014-12-01 14:53:08 -0800859void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
860 Indent() << "#pragma omp master";
861 PrintOMPExecutableDirective(Node);
862}
863
864void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
865 Indent() << "#pragma omp critical";
866 if (Node->getDirectiveName().getName()) {
867 OS << " (";
868 Node->getDirectiveName().printName(OS);
869 OS << ")";
870 }
871 PrintOMPExecutableDirective(Node);
872}
873
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700874void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
875 Indent() << "#pragma omp parallel for ";
876 PrintOMPExecutableDirective(Node);
877}
878
Stephen Hines176edba2014-12-01 14:53:08 -0800879void StmtPrinter::VisitOMPParallelForSimdDirective(
880 OMPParallelForSimdDirective *Node) {
881 Indent() << "#pragma omp parallel for simd ";
882 PrintOMPExecutableDirective(Node);
883}
884
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700885void StmtPrinter::VisitOMPParallelSectionsDirective(
886 OMPParallelSectionsDirective *Node) {
887 Indent() << "#pragma omp parallel sections ";
888 PrintOMPExecutableDirective(Node);
889}
890
Stephen Hines176edba2014-12-01 14:53:08 -0800891void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
892 Indent() << "#pragma omp task ";
893 PrintOMPExecutableDirective(Node);
894}
895
896void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
897 Indent() << "#pragma omp taskyield";
898 PrintOMPExecutableDirective(Node);
899}
900
901void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
902 Indent() << "#pragma omp barrier";
903 PrintOMPExecutableDirective(Node);
904}
905
906void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
907 Indent() << "#pragma omp taskwait";
908 PrintOMPExecutableDirective(Node);
909}
910
911void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
912 Indent() << "#pragma omp flush ";
913 PrintOMPExecutableDirective(Node);
914}
915
916void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
917 Indent() << "#pragma omp ordered";
918 PrintOMPExecutableDirective(Node);
919}
920
921void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
922 Indent() << "#pragma omp atomic ";
923 PrintOMPExecutableDirective(Node);
924}
925
926void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
927 Indent() << "#pragma omp target ";
928 PrintOMPExecutableDirective(Node);
929}
930
931void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
932 Indent() << "#pragma omp teams ";
933 PrintOMPExecutableDirective(Node);
934}
935
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000936//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000937// Expr printing methods.
938//===----------------------------------------------------------------------===//
939
Reid Spencer5f016e22007-07-11 17:01:13 +0000940void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000941 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
942 Qualifier->print(OS, Policy);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000943 if (Node->hasTemplateKeyword())
944 OS << "template ";
Abramo Bagnara25777432010-08-11 22:01:17 +0000945 OS << Node->getNameInfo();
John McCall096832c2010-08-19 23:49:38 +0000946 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer5eada842013-02-22 15:46:01 +0000947 TemplateSpecializationType::PrintTemplateArgumentList(
948 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregor1a49af92009-01-06 05:10:23 +0000949}
950
John McCall865d4472009-11-19 22:55:06 +0000951void StmtPrinter::VisitDependentScopeDeclRefExpr(
952 DependentScopeDeclRefExpr *Node) {
Axel Naumann274f83c2011-01-24 15:44:00 +0000953 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
954 Qualifier->print(OS, Policy);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000955 if (Node->hasTemplateKeyword())
956 OS << "template ";
Abramo Bagnara25777432010-08-11 22:01:17 +0000957 OS << Node->getNameInfo();
John McCallf7a1a742009-11-24 19:00:30 +0000958 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer5eada842013-02-22 15:46:01 +0000959 TemplateSpecializationType::PrintTemplateArgumentList(
960 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000961}
962
John McCallba135432009-11-21 08:51:07 +0000963void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000964 if (Node->getQualifier())
965 Node->getQualifier()->print(OS, Policy);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000966 if (Node->hasTemplateKeyword())
967 OS << "template ";
Abramo Bagnara25777432010-08-11 22:01:17 +0000968 OS << Node->getNameInfo();
John McCallf7a1a742009-11-24 19:00:30 +0000969 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer5eada842013-02-22 15:46:01 +0000970 TemplateSpecializationType::PrintTemplateArgumentList(
971 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000972}
973
Steve Naroff7779db42007-11-12 14:29:37 +0000974void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000975 if (Node->getBase()) {
976 PrintExpr(Node->getBase());
977 OS << (Node->isArrow() ? "->" : ".");
978 }
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000979 OS << *Node->getDecl();
Steve Naroff7779db42007-11-12 14:29:37 +0000980}
981
Steve Naroffae784072008-05-30 00:40:33 +0000982void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000983 if (Node->isSuperReceiver())
984 OS << "super.";
Stephen Hines651f13c2014-04-23 16:59:28 -0700985 else if (Node->isObjectReceiver() && Node->getBase()) {
Steve Naroffae784072008-05-30 00:40:33 +0000986 PrintExpr(Node->getBase());
987 OS << ".";
Stephen Hines651f13c2014-04-23 16:59:28 -0700988 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
989 OS << Node->getClassReceiver()->getName() << ".";
Steve Naroffae784072008-05-30 00:40:33 +0000990 }
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000991
John McCall12f78a62010-12-02 01:19:52 +0000992 if (Node->isImplicitProperty())
Stephen Hines651f13c2014-04-23 16:59:28 -0700993 Node->getImplicitPropertyGetter()->getSelector().print(OS);
John McCall12f78a62010-12-02 01:19:52 +0000994 else
995 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000996}
997
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000998void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
999
1000 PrintExpr(Node->getBaseExpr());
1001 OS << "[";
1002 PrintExpr(Node->getKeyExpr());
1003 OS << "]";
1004}
1005
Chris Lattnerd9f69102008-08-10 01:53:14 +00001006void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Stephen Hines176edba2014-12-01 14:53:08 -08001007 OS << PredefinedExpr::getIdentTypeName(Node->getIdentType());
Anders Carlsson22742662007-07-21 05:21:51 +00001008}
1009
Reid Spencer5f016e22007-07-11 17:01:13 +00001010void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +00001011 unsigned value = Node->getValue();
Douglas Gregor5cee1192011-07-27 05:40:30 +00001012
1013 switch (Node->getKind()) {
1014 case CharacterLiteral::Ascii: break; // no prefix.
1015 case CharacterLiteral::Wide: OS << 'L'; break;
1016 case CharacterLiteral::UTF16: OS << 'u'; break;
1017 case CharacterLiteral::UTF32: OS << 'U'; break;
1018 }
1019
Chris Lattner8bf9f072007-07-13 23:58:20 +00001020 switch (value) {
1021 case '\\':
1022 OS << "'\\\\'";
1023 break;
1024 case '\'':
1025 OS << "'\\''";
1026 break;
1027 case '\a':
1028 // TODO: K&R: the meaning of '\\a' is different in traditional C
1029 OS << "'\\a'";
1030 break;
1031 case '\b':
1032 OS << "'\\b'";
1033 break;
1034 // Nonstandard escape sequence.
1035 /*case '\e':
1036 OS << "'\\e'";
1037 break;*/
1038 case '\f':
1039 OS << "'\\f'";
1040 break;
1041 case '\n':
1042 OS << "'\\n'";
1043 break;
1044 case '\r':
1045 OS << "'\\r'";
1046 break;
1047 case '\t':
1048 OS << "'\\t'";
1049 break;
1050 case '\v':
1051 OS << "'\\v'";
1052 break;
1053 default:
Jordan Rose6d4f7342013-02-08 22:30:27 +00001054 if (value < 256 && isPrintable((unsigned char)value))
Chris Lattner8bf9f072007-07-13 23:58:20 +00001055 OS << "'" << (char)value << "'";
Jordan Rose6d4f7342013-02-08 22:30:27 +00001056 else if (value < 256)
1057 OS << "'\\x" << llvm::format("%02x", value) << "'";
1058 else if (value <= 0xFFFF)
1059 OS << "'\\u" << llvm::format("%04x", value) << "'";
1060 else
1061 OS << "'\\U" << llvm::format("%08x", value) << "'";
Chris Lattnerb0a721a2007-07-13 05:18:11 +00001062 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001063}
1064
1065void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
1066 bool isSigned = Node->getType()->isSignedIntegerType();
1067 OS << Node->getValue().toString(10, isSigned);
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall183700f2009-09-21 23:43:11 +00001070 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001071 default: llvm_unreachable("Unexpected type for integer literal!");
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001072 case BuiltinType::SChar: OS << "i8"; break;
1073 case BuiltinType::UChar: OS << "Ui8"; break;
1074 case BuiltinType::Short: OS << "i16"; break;
1075 case BuiltinType::UShort: OS << "Ui16"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001076 case BuiltinType::Int: break; // no suffix.
1077 case BuiltinType::UInt: OS << 'U'; break;
1078 case BuiltinType::Long: OS << 'L'; break;
1079 case BuiltinType::ULong: OS << "UL"; break;
1080 case BuiltinType::LongLong: OS << "LL"; break;
1081 case BuiltinType::ULongLong: OS << "ULL"; break;
Richard Trieu11cbe2a2011-11-07 18:40:31 +00001082 case BuiltinType::Int128: OS << "i128"; break;
1083 case BuiltinType::UInt128: OS << "Ui128"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001084 }
1085}
Benjamin Kramera0a3c902012-09-20 14:07:17 +00001086
1087static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1088 bool PrintSuffix) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001089 SmallString<16> Str;
Eli Friedmanb3909212011-10-05 20:32:03 +00001090 Node->getValue().toString(Str);
1091 OS << Str;
Benjamin Kramera0a3c902012-09-20 14:07:17 +00001092 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1093 OS << '.'; // Trailing dot in order to separate from ints.
1094
1095 if (!PrintSuffix)
1096 return;
1097
1098 // Emit suffixes. Float literals are always a builtin float type.
1099 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1100 default: llvm_unreachable("Unexpected type for float literal!");
1101 case BuiltinType::Half: break; // FIXME: suffix?
1102 case BuiltinType::Double: break; // no suffix.
1103 case BuiltinType::Float: OS << 'F'; break;
1104 case BuiltinType::LongDouble: OS << 'L'; break;
1105 }
1106}
1107
1108void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1109 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001110}
Chris Lattner5d661452007-08-26 03:42:43 +00001111
1112void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1113 PrintExpr(Node->getSubExpr());
1114 OS << "i";
1115}
1116
Reid Spencer5f016e22007-07-11 17:01:13 +00001117void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Richard Trieu8ab09da2012-06-13 20:25:24 +00001118 Str->outputString(OS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001119}
1120void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1121 OS << "(";
1122 PrintExpr(Node->getSubExpr());
1123 OS << ")";
1124}
1125void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +00001126 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001127 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Eli Friedman7df71ac2009-06-14 22:39:26 +00001129 // Print a space if this is an "identifier operator" like __real, or if
1130 // it might be concatenated incorrectly like '+'.
Chris Lattner296bf192007-08-23 21:46:40 +00001131 switch (Node->getOpcode()) {
1132 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001133 case UO_Real:
1134 case UO_Imag:
1135 case UO_Extension:
Chris Lattner296bf192007-08-23 21:46:40 +00001136 OS << ' ';
1137 break;
John McCall2de56d12010-08-25 11:45:40 +00001138 case UO_Plus:
1139 case UO_Minus:
Eli Friedman7df71ac2009-06-14 22:39:26 +00001140 if (isa<UnaryOperator>(Node->getSubExpr()))
1141 OS << ' ';
1142 break;
Chris Lattner296bf192007-08-23 21:46:40 +00001143 }
1144 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 PrintExpr(Node->getSubExpr());
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 if (Node->isPostfix())
1148 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +00001149}
Chris Lattner704fe352007-08-30 17:59:59 +00001150
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001151void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1152 OS << "__builtin_offsetof(";
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001153 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1154 OS << ", ";
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001155 bool PrintedSomething = false;
1156 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1157 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
1158 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
1159 // Array node
1160 OS << "[";
1161 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1162 OS << "]";
1163 PrintedSomething = true;
1164 continue;
1165 }
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00001166
1167 // Skip implicit base indirections.
1168 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
1169 continue;
1170
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001171 // Field or identifier node.
1172 IdentifierInfo *Id = ON.getFieldName();
1173 if (!Id)
1174 continue;
Sean Huntc3021132010-05-05 15:23:54 +00001175
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001176 if (PrintedSomething)
1177 OS << ".";
1178 else
1179 PrintedSomething = true;
Sean Huntc3021132010-05-05 15:23:54 +00001180 OS << Id->getName();
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001181 }
1182 OS << ")";
1183}
1184
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001185void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
1186 switch(Node->getKind()) {
1187 case UETT_SizeOf:
1188 OS << "sizeof";
1189 break;
1190 case UETT_AlignOf:
Jordan Rosef70a8862012-06-30 21:33:57 +00001191 if (Policy.LangOpts.CPlusPlus)
1192 OS << "alignof";
1193 else if (Policy.LangOpts.C11)
1194 OS << "_Alignof";
1195 else
1196 OS << "__alignof";
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001197 break;
1198 case UETT_VecStep:
1199 OS << "vec_step";
1200 break;
1201 }
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001202 if (Node->isArgumentType()) {
1203 OS << '(';
1204 Node->getArgumentType().print(OS, Policy);
1205 OS << ')';
1206 } else {
Sebastian Redl05189992008-11-11 17:56:53 +00001207 OS << " ";
1208 PrintExpr(Node->getArgumentExpr());
1209 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001210}
Peter Collingbournef111d932011-04-15 00:35:48 +00001211
1212void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1213 OS << "_Generic(";
1214 PrintExpr(Node->getControllingExpr());
1215 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
1216 OS << ", ";
1217 QualType T = Node->getAssocType(i);
1218 if (T.isNull())
1219 OS << "default";
1220 else
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001221 T.print(OS, Policy);
Peter Collingbournef111d932011-04-15 00:35:48 +00001222 OS << ": ";
1223 PrintExpr(Node->getAssocExpr(i));
1224 }
1225 OS << ")";
1226}
1227
Reid Spencer5f016e22007-07-11 17:01:13 +00001228void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +00001229 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +00001231 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 OS << "]";
1233}
1234
Peter Collingbourned64e2372011-02-08 21:17:54 +00001235void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +00001237 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1238 // Don't print any defaulted arguments
1239 break;
1240 }
1241
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 if (i) OS << ", ";
1243 PrintExpr(Call->getArg(i));
1244 }
Peter Collingbourned64e2372011-02-08 21:17:54 +00001245}
1246
1247void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1248 PrintExpr(Call->getCallee());
1249 OS << "(";
1250 PrintCallArgs(Call);
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 OS << ")";
1252}
1253void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +00001254 // FIXME: Suppress printing implicit bases (like "this")
1255 PrintExpr(Node->getBase());
David Blaikie383ec172012-11-12 19:12:12 +00001256
1257 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
David Blaikie731de312012-11-12 19:32:32 +00001258 FieldDecl *ParentDecl = ParentMember
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001259 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr;
David Blaikie383ec172012-11-12 19:12:12 +00001260
David Blaikie731de312012-11-12 19:32:32 +00001261 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
David Blaikie383ec172012-11-12 19:12:12 +00001262 OS << (Node->isArrow() ? "->" : ".");
David Blaikie383ec172012-11-12 19:12:12 +00001263
Fariborz Jahanian97fd83a2010-01-11 21:17:32 +00001264 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1265 if (FD->isAnonymousStructOrUnion())
1266 return;
David Blaikie383ec172012-11-12 19:12:12 +00001267
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001268 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1269 Qualifier->print(OS, Policy);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001270 if (Node->hasTemplateKeyword())
1271 OS << "template ";
Abramo Bagnara25777432010-08-11 22:01:17 +00001272 OS << Node->getMemberNameInfo();
John McCall096832c2010-08-19 23:49:38 +00001273 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer5eada842013-02-22 15:46:01 +00001274 TemplateSpecializationType::PrintTemplateArgumentList(
1275 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001276}
Steve Narofff242b1b2009-07-24 17:54:45 +00001277void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1278 PrintExpr(Node->getBase());
1279 OS << (Node->isArrow() ? "->isa" : ".isa");
1280}
1281
Nate Begeman213541a2008-04-18 23:10:10 +00001282void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +00001283 PrintExpr(Node->getBase());
1284 OS << ".";
1285 OS << Node->getAccessor().getName();
1286}
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001287void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001288 OS << '(';
1289 Node->getTypeAsWritten().print(OS, Policy);
1290 OS << ')';
Reid Spencer5f016e22007-07-11 17:01:13 +00001291 PrintExpr(Node->getSubExpr());
1292}
Steve Naroffaff1edd2007-07-19 21:32:11 +00001293void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001294 OS << '(';
1295 Node->getType().print(OS, Policy);
1296 OS << ')';
Steve Naroffaff1edd2007-07-19 21:32:11 +00001297 PrintExpr(Node->getInitializer());
1298}
Steve Naroff49b45262007-07-13 16:58:59 +00001299void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001300 // No need to print anything, simply forward to the subexpression.
Steve Naroff90045e82007-07-13 23:32:42 +00001301 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +00001302}
Reid Spencer5f016e22007-07-11 17:01:13 +00001303void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1304 PrintExpr(Node->getLHS());
1305 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1306 PrintExpr(Node->getRHS());
1307}
Chris Lattnereb14fe82007-08-25 02:00:02 +00001308void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1309 PrintExpr(Node->getLHS());
1310 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1311 PrintExpr(Node->getRHS());
1312}
Reid Spencer5f016e22007-07-11 17:01:13 +00001313void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1314 PrintExpr(Node->getCond());
John McCall56ca35d2011-02-17 10:25:35 +00001315 OS << " ? ";
1316 PrintExpr(Node->getLHS());
1317 OS << " : ";
Reid Spencer5f016e22007-07-11 17:01:13 +00001318 PrintExpr(Node->getRHS());
1319}
1320
1321// GNU extensions.
1322
John McCall56ca35d2011-02-17 10:25:35 +00001323void
1324StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1325 PrintExpr(Node->getCommon());
1326 OS << " ?: ";
1327 PrintExpr(Node->getFalseExpr());
1328}
Chris Lattner6481a572007-08-03 17:31:20 +00001329void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001330 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001331}
1332
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001333void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1334 OS << "(";
1335 PrintRawCompoundStmt(E->getSubStmt());
1336 OS << ")";
1337}
1338
Steve Naroffd04fdd52007-08-03 21:21:27 +00001339void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1340 OS << "__builtin_choose_expr(";
1341 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +00001342 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +00001343 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +00001344 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +00001345 PrintExpr(Node->getRHS());
1346 OS << ")";
1347}
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001348
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001349void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1350 OS << "__null";
1351}
1352
Eli Friedmand38617c2008-05-14 19:38:39 +00001353void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1354 OS << "__builtin_shufflevector(";
1355 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1356 if (i) OS << ", ";
1357 PrintExpr(Node->getExpr(i));
1358 }
1359 OS << ")";
1360}
1361
Hal Finkel414a1bd2013-09-18 03:29:45 +00001362void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1363 OS << "__builtin_convertvector(";
1364 PrintExpr(Node->getSrcExpr());
1365 OS << ", ";
1366 Node->getType().print(OS, Policy);
1367 OS << ")";
1368}
1369
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001370void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor64f65002009-05-30 00:56:08 +00001371 if (Node->getSyntacticForm()) {
1372 Visit(Node->getSyntacticForm());
1373 return;
1374 }
1375
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001376 OS << "{ ";
1377 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1378 if (i) OS << ", ";
Douglas Gregor4c678342009-01-28 21:54:33 +00001379 if (Node->getInit(i))
1380 PrintExpr(Node->getInit(i));
1381 else
1382 OS << "0";
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001383 }
1384 OS << " }";
1385}
1386
Nate Begeman2ef13e52009-08-10 23:49:36 +00001387void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1388 OS << "( ";
1389 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1390 if (i) OS << ", ";
1391 PrintExpr(Node->getExpr(i));
1392 }
1393 OS << " )";
1394}
1395
Douglas Gregor05c13a32009-01-22 00:58:24 +00001396void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor4c678342009-01-28 21:54:33 +00001397 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1398 DEnd = Node->designators_end();
1399 D != DEnd; ++D) {
1400 if (D->isFieldDesignator()) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001401 if (D->getDotLoc().isInvalid()) {
1402 if (IdentifierInfo *II = D->getFieldName())
1403 OS << II->getName() << ":";
1404 } else {
Douglas Gregor4c678342009-01-28 21:54:33 +00001405 OS << "." << D->getFieldName()->getName();
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001406 }
Douglas Gregor4c678342009-01-28 21:54:33 +00001407 } else {
1408 OS << "[";
1409 if (D->isArrayDesignator()) {
1410 PrintExpr(Node->getArrayIndex(*D));
1411 } else {
1412 PrintExpr(Node->getArrayRangeStart(*D));
1413 OS << " ... ";
Mike Stump1eb44332009-09-09 15:08:12 +00001414 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor4c678342009-01-28 21:54:33 +00001415 }
1416 OS << "]";
1417 }
1418 }
1419
1420 OS << " = ";
1421 PrintExpr(Node->getInit());
Douglas Gregor05c13a32009-01-22 00:58:24 +00001422}
1423
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001424void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001425 if (Policy.LangOpts.CPlusPlus) {
1426 OS << "/*implicit*/";
1427 Node->getType().print(OS, Policy);
1428 OS << "()";
1429 } else {
1430 OS << "/*implicit*/(";
1431 Node->getType().print(OS, Policy);
1432 OS << ')';
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001433 if (Node->getType()->isRecordType())
1434 OS << "{}";
1435 else
1436 OS << 0;
1437 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001438}
1439
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001440void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman42f42c02009-05-30 04:20:30 +00001441 OS << "__builtin_va_arg(";
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001442 PrintExpr(Node->getSubExpr());
1443 OS << ", ";
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001444 Node->getType().print(OS, Policy);
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001445 OS << ")";
1446}
1447
John McCall4b9c2d22011-11-06 09:01:30 +00001448void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1449 PrintExpr(Node->getSyntacticForm());
1450}
1451
Eli Friedman276b0612011-10-11 02:20:01 +00001452void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001453 const char *Name = nullptr;
Eli Friedman276b0612011-10-11 02:20:01 +00001454 switch (Node->getOp()) {
Richard Smithff34d402012-04-12 05:08:17 +00001455#define BUILTIN(ID, TYPE, ATTRS)
1456#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1457 case AtomicExpr::AO ## ID: \
1458 Name = #ID "("; \
1459 break;
1460#include "clang/Basic/Builtins.def"
Eli Friedman276b0612011-10-11 02:20:01 +00001461 }
1462 OS << Name;
Richard Smithff34d402012-04-12 05:08:17 +00001463
1464 // AtomicExpr stores its subexpressions in a permuted order.
Eli Friedman276b0612011-10-11 02:20:01 +00001465 PrintExpr(Node->getPtr());
Richard Smithff34d402012-04-12 05:08:17 +00001466 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1467 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
Eli Friedman276b0612011-10-11 02:20:01 +00001468 OS << ", ";
Richard Smith28fff532013-05-01 19:02:43 +00001469 PrintExpr(Node->getVal1());
Eli Friedman276b0612011-10-11 02:20:01 +00001470 }
Richard Smithff34d402012-04-12 05:08:17 +00001471 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1472 Node->isCmpXChg()) {
Eli Friedman276b0612011-10-11 02:20:01 +00001473 OS << ", ";
Richard Smith28fff532013-05-01 19:02:43 +00001474 PrintExpr(Node->getVal2());
Eli Friedman276b0612011-10-11 02:20:01 +00001475 }
Richard Smithff34d402012-04-12 05:08:17 +00001476 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1477 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
Richard Smithff34d402012-04-12 05:08:17 +00001478 OS << ", ";
Richard Smith28fff532013-05-01 19:02:43 +00001479 PrintExpr(Node->getWeak());
Richard Smithff34d402012-04-12 05:08:17 +00001480 }
Richard Smith28fff532013-05-01 19:02:43 +00001481 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1482 OS << ", ";
David Chisnall7a7ee302012-01-16 17:27:18 +00001483 PrintExpr(Node->getOrder());
Richard Smith28fff532013-05-01 19:02:43 +00001484 }
Eli Friedman276b0612011-10-11 02:20:01 +00001485 if (Node->isCmpXChg()) {
1486 OS << ", ";
1487 PrintExpr(Node->getOrderFail());
1488 }
1489 OS << ")";
1490}
1491
Reid Spencer5f016e22007-07-11 17:01:13 +00001492// C++
Douglas Gregorb4609802008-11-14 16:09:21 +00001493void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1494 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1495 "",
1496#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1497 Spelling,
1498#include "clang/Basic/OperatorKinds.def"
1499 };
1500
1501 OverloadedOperatorKind Kind = Node->getOperator();
1502 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1503 if (Node->getNumArgs() == 1) {
1504 OS << OpStrings[Kind] << ' ';
1505 PrintExpr(Node->getArg(0));
1506 } else {
1507 PrintExpr(Node->getArg(0));
1508 OS << ' ' << OpStrings[Kind];
1509 }
Eli Friedmana143a9d2012-10-12 22:45:14 +00001510 } else if (Kind == OO_Arrow) {
1511 PrintExpr(Node->getArg(0));
Douglas Gregorb4609802008-11-14 16:09:21 +00001512 } else if (Kind == OO_Call) {
1513 PrintExpr(Node->getArg(0));
1514 OS << '(';
1515 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1516 if (ArgIdx > 1)
1517 OS << ", ";
1518 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1519 PrintExpr(Node->getArg(ArgIdx));
1520 }
1521 OS << ')';
1522 } else if (Kind == OO_Subscript) {
1523 PrintExpr(Node->getArg(0));
1524 OS << '[';
1525 PrintExpr(Node->getArg(1));
1526 OS << ']';
1527 } else if (Node->getNumArgs() == 1) {
1528 OS << OpStrings[Kind] << ' ';
1529 PrintExpr(Node->getArg(0));
1530 } else if (Node->getNumArgs() == 2) {
1531 PrintExpr(Node->getArg(0));
1532 OS << ' ' << OpStrings[Kind] << ' ';
1533 PrintExpr(Node->getArg(1));
1534 } else {
David Blaikieb219cfc2011-09-23 05:06:16 +00001535 llvm_unreachable("unknown overloaded operator");
Douglas Gregorb4609802008-11-14 16:09:21 +00001536 }
1537}
Reid Spencer5f016e22007-07-11 17:01:13 +00001538
Douglas Gregor88a35142008-12-22 05:46:06 +00001539void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001540 // If we have a conversion operator call only print the argument.
1541 CXXMethodDecl *MD = Node->getMethodDecl();
1542 if (MD && isa<CXXConversionDecl>(MD)) {
1543 PrintExpr(Node->getImplicitObjectArgument());
1544 return;
1545 }
Douglas Gregor88a35142008-12-22 05:46:06 +00001546 VisitCallExpr(cast<CallExpr>(Node));
1547}
1548
Peter Collingbournee08ce652011-02-09 21:07:24 +00001549void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1550 PrintExpr(Node->getCallee());
1551 OS << "<<<";
1552 PrintCallArgs(Node->getConfig());
1553 OS << ">>>(";
1554 PrintCallArgs(Node);
1555 OS << ")";
1556}
1557
Douglas Gregor49badde2008-10-27 19:41:14 +00001558void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1559 OS << Node->getCastName() << '<';
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001560 Node->getTypeAsWritten().print(OS, Policy);
1561 OS << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +00001562 PrintExpr(Node->getSubExpr());
1563 OS << ")";
1564}
1565
Douglas Gregor49badde2008-10-27 19:41:14 +00001566void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1567 VisitCXXNamedCastExpr(Node);
1568}
1569
1570void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1571 VisitCXXNamedCastExpr(Node);
1572}
1573
1574void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1575 VisitCXXNamedCastExpr(Node);
1576}
1577
1578void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1579 VisitCXXNamedCastExpr(Node);
1580}
1581
Sebastian Redlc42e1182008-11-11 11:37:55 +00001582void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1583 OS << "typeid(";
1584 if (Node->isTypeOperand()) {
David Majnemerfe16aa32013-09-27 07:04:31 +00001585 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001586 } else {
1587 PrintExpr(Node->getExprOperand());
1588 }
1589 OS << ")";
1590}
1591
Francois Pichet01b7c302010-09-08 12:20:18 +00001592void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1593 OS << "__uuidof(";
1594 if (Node->isTypeOperand()) {
David Majnemerfe16aa32013-09-27 07:04:31 +00001595 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Francois Pichet01b7c302010-09-08 12:20:18 +00001596 } else {
1597 PrintExpr(Node->getExprOperand());
1598 }
1599 OS << ")";
1600}
1601
John McCall76da55d2013-04-16 07:28:30 +00001602void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1603 PrintExpr(Node->getBaseExpr());
1604 if (Node->isArrow())
1605 OS << "->";
1606 else
1607 OS << ".";
1608 if (NestedNameSpecifier *Qualifier =
1609 Node->getQualifierLoc().getNestedNameSpecifier())
1610 Qualifier->print(OS, Policy);
1611 OS << Node->getPropertyDecl()->getDeclName();
1612}
1613
Richard Smith9fcce652012-03-07 08:35:16 +00001614void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1615 switch (Node->getLiteralOperatorKind()) {
1616 case UserDefinedLiteral::LOK_Raw:
Richard Smithef9f2982012-03-09 10:10:02 +00001617 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
Richard Smith9fcce652012-03-07 08:35:16 +00001618 break;
1619 case UserDefinedLiteral::LOK_Template: {
Richard Smithef9f2982012-03-09 10:10:02 +00001620 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1621 const TemplateArgumentList *Args =
1622 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1623 assert(Args);
1624 const TemplateArgument &Pack = Args->get(0);
Stephen Hines176edba2014-12-01 14:53:08 -08001625 for (const auto &P : Pack.pack_elements()) {
1626 char C = (char)P.getAsIntegral().getZExtValue();
Richard Smith9fcce652012-03-07 08:35:16 +00001627 OS << C;
1628 }
1629 break;
1630 }
Richard Smith91688302012-03-08 09:02:38 +00001631 case UserDefinedLiteral::LOK_Integer: {
1632 // Print integer literal without suffix.
1633 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1634 OS << Int->getValue().toString(10, /*isSigned*/false);
1635 break;
1636 }
Benjamin Kramera0a3c902012-09-20 14:07:17 +00001637 case UserDefinedLiteral::LOK_Floating: {
1638 // Print floating literal without suffix.
1639 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1640 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1641 break;
1642 }
Richard Smith9fcce652012-03-07 08:35:16 +00001643 case UserDefinedLiteral::LOK_String:
1644 case UserDefinedLiteral::LOK_Character:
1645 PrintExpr(Node->getCookedLiteral());
1646 break;
1647 }
1648 OS << Node->getUDSuffix()->getName();
1649}
1650
Reid Spencer5f016e22007-07-11 17:01:13 +00001651void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1652 OS << (Node->getValue() ? "true" : "false");
1653}
1654
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001655void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1656 OS << "nullptr";
1657}
1658
Douglas Gregor796da182008-11-04 14:32:21 +00001659void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1660 OS << "this";
1661}
1662
Chris Lattner50dd2892008-02-26 00:51:44 +00001663void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001664 if (!Node->getSubExpr())
Chris Lattner50dd2892008-02-26 00:51:44 +00001665 OS << "throw";
1666 else {
1667 OS << "throw ";
1668 PrintExpr(Node->getSubExpr());
1669 }
1670}
1671
Chris Lattner04421082008-04-08 04:40:51 +00001672void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
Richard Smithc3bf52c2013-04-20 22:23:05 +00001673 // Nothing to print: we picked up the default argument.
1674}
1675
1676void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1677 // Nothing to print: we picked up the default initializer.
Chris Lattner04421082008-04-08 04:40:51 +00001678}
1679
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001680void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001681 Node->getType().print(OS, Policy);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001682 OS << "(";
1683 PrintExpr(Node->getSubExpr());
1684 OS << ")";
1685}
1686
Anders Carlssonfceb0a82009-05-30 20:03:25 +00001687void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1688 PrintExpr(Node->getSubExpr());
1689}
1690
Douglas Gregor506ae412009-01-16 18:33:17 +00001691void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001692 Node->getType().print(OS, Policy);
Douglas Gregor506ae412009-01-16 18:33:17 +00001693 OS << "(";
1694 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001695 ArgEnd = Node->arg_end();
Douglas Gregor506ae412009-01-16 18:33:17 +00001696 Arg != ArgEnd; ++Arg) {
Rafael Espindolad3bb9ff2013-05-24 16:11:44 +00001697 if (Arg->isDefaultArgument())
1698 break;
Douglas Gregor506ae412009-01-16 18:33:17 +00001699 if (Arg != Node->arg_begin())
1700 OS << ", ";
1701 PrintExpr(*Arg);
1702 }
1703 OS << ")";
1704}
1705
Douglas Gregor01d08012012-02-07 10:09:13 +00001706void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1707 OS << '[';
1708 bool NeedComma = false;
1709 switch (Node->getCaptureDefault()) {
1710 case LCD_None:
1711 break;
1712
1713 case LCD_ByCopy:
1714 OS << '=';
1715 NeedComma = true;
1716 break;
1717
1718 case LCD_ByRef:
1719 OS << '&';
1720 NeedComma = true;
1721 break;
1722 }
1723 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1724 CEnd = Node->explicit_capture_end();
1725 C != CEnd;
1726 ++C) {
1727 if (NeedComma)
1728 OS << ", ";
1729 NeedComma = true;
1730
1731 switch (C->getCaptureKind()) {
1732 case LCK_This:
1733 OS << "this";
1734 break;
1735
1736 case LCK_ByRef:
Richard Smith04fa7a32013-09-28 04:02:39 +00001737 if (Node->getCaptureDefault() != LCD_ByRef || C->isInitCapture())
Douglas Gregor01d08012012-02-07 10:09:13 +00001738 OS << '&';
1739 OS << C->getCapturedVar()->getName();
1740 break;
1741
1742 case LCK_ByCopy:
Douglas Gregor01d08012012-02-07 10:09:13 +00001743 OS << C->getCapturedVar()->getName();
1744 break;
Stephen Hines176edba2014-12-01 14:53:08 -08001745 case LCK_VLAType:
1746 llvm_unreachable("VLA type in explicit captures.");
Douglas Gregor01d08012012-02-07 10:09:13 +00001747 }
Richard Smith04fa7a32013-09-28 04:02:39 +00001748
1749 if (C->isInitCapture())
1750 PrintExpr(C->getCapturedVar()->getInit());
Douglas Gregor01d08012012-02-07 10:09:13 +00001751 }
1752 OS << ']';
1753
1754 if (Node->hasExplicitParameters()) {
1755 OS << " (";
1756 CXXMethodDecl *Method = Node->getCallOperator();
1757 NeedComma = false;
Stephen Hines651f13c2014-04-23 16:59:28 -07001758 for (auto P : Method->params()) {
Douglas Gregor01d08012012-02-07 10:09:13 +00001759 if (NeedComma) {
1760 OS << ", ";
1761 } else {
1762 NeedComma = true;
1763 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001764 std::string ParamStr = P->getNameAsString();
1765 P->getOriginalType().print(OS, Policy, ParamStr);
Douglas Gregor01d08012012-02-07 10:09:13 +00001766 }
1767 if (Method->isVariadic()) {
1768 if (NeedComma)
1769 OS << ", ";
1770 OS << "...";
1771 }
1772 OS << ')';
1773
1774 if (Node->isMutable())
1775 OS << " mutable";
1776
1777 const FunctionProtoType *Proto
1778 = Method->getType()->getAs<FunctionProtoType>();
Benjamin Kramer5eada842013-02-22 15:46:01 +00001779 Proto->printExceptionSpecification(OS, Policy);
Douglas Gregor01d08012012-02-07 10:09:13 +00001780
Bill Wendlingad017fa2012-12-20 19:22:21 +00001781 // FIXME: Attributes
Douglas Gregor01d08012012-02-07 10:09:13 +00001782
Douglas Gregordfca6f52012-02-13 22:00:16 +00001783 // Print the trailing return type if it was specified in the source.
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001784 if (Node->hasExplicitResultType()) {
1785 OS << " -> ";
Stephen Hines651f13c2014-04-23 16:59:28 -07001786 Proto->getReturnType().print(OS, Policy);
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001787 }
Douglas Gregor01d08012012-02-07 10:09:13 +00001788 }
1789
1790 // Print the body.
1791 CompoundStmt *Body = Node->getBody();
1792 OS << ' ';
1793 PrintStmt(Body);
1794}
1795
Douglas Gregored8abf12010-07-08 06:14:04 +00001796void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregorab6677e2010-09-08 00:15:04 +00001797 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001798 TSInfo->getType().print(OS, Policy);
Douglas Gregorab6677e2010-09-08 00:15:04 +00001799 else
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001800 Node->getType().print(OS, Policy);
1801 OS << "()";
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001802}
1803
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001804void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1805 if (E->isGlobalNew())
1806 OS << "::";
1807 OS << "new ";
1808 unsigned NumPlace = E->getNumPlacementArgs();
Eli Friedmand03ef042012-10-18 20:54:37 +00001809 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001810 OS << "(";
1811 PrintExpr(E->getPlacementArg(0));
1812 for (unsigned i = 1; i < NumPlace; ++i) {
Eli Friedmand03ef042012-10-18 20:54:37 +00001813 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1814 break;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001815 OS << ", ";
1816 PrintExpr(E->getPlacementArg(i));
1817 }
1818 OS << ") ";
1819 }
1820 if (E->isParenTypeId())
1821 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001822 std::string TypeS;
1823 if (Expr *Size = E->getArraySize()) {
1824 llvm::raw_string_ostream s(TypeS);
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001825 s << '[';
Richard Smithd1420c62012-08-16 03:56:14 +00001826 Size->printPretty(s, Helper, Policy);
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001827 s << ']';
Sebastian Redl6fec6482008-12-02 22:08:59 +00001828 }
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001829 E->getAllocatedType().print(OS, Policy, TypeS);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001830 if (E->isParenTypeId())
1831 OS << ")";
1832
Sebastian Redl2aed8b82012-02-16 12:22:20 +00001833 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1834 if (InitStyle) {
1835 if (InitStyle == CXXNewExpr::CallInit)
1836 OS << "(";
1837 PrintExpr(E->getInitializer());
1838 if (InitStyle == CXXNewExpr::CallInit)
1839 OS << ")";
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001840 }
1841}
1842
1843void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1844 if (E->isGlobalDelete())
1845 OS << "::";
1846 OS << "delete ";
1847 if (E->isArrayForm())
1848 OS << "[] ";
1849 PrintExpr(E->getArgument());
1850}
1851
Douglas Gregora71d8192009-09-04 17:36:40 +00001852void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1853 PrintExpr(E->getBase());
1854 if (E->isArrow())
1855 OS << "->";
1856 else
1857 OS << '.';
1858 if (E->getQualifier())
1859 E->getQualifier()->print(OS, Policy);
Eli Friedmana7a38cb2012-10-23 20:26:57 +00001860 OS << "~";
Mike Stump1eb44332009-09-09 15:08:12 +00001861
Douglas Gregora2e7dd22010-02-25 01:56:36 +00001862 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1863 OS << II->getName();
1864 else
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001865 E->getDestroyedType().print(OS, Policy);
Douglas Gregora71d8192009-09-04 17:36:40 +00001866}
1867
Anders Carlssone349bea2009-04-23 02:32:43 +00001868void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Richard Smithc83c2302012-12-19 01:39:02 +00001869 if (E->isListInitialization())
1870 OS << "{ ";
1871
Douglas Gregord0fb3ad2011-01-24 17:25:03 +00001872 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1873 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1874 // Don't print any defaulted arguments
1875 break;
1876 }
1877
1878 if (i) OS << ", ";
1879 PrintExpr(E->getArg(i));
Fariborz Jahanianc75da512010-01-13 21:41:11 +00001880 }
Richard Smithc83c2302012-12-19 01:39:02 +00001881
1882 if (E->isListInitialization())
1883 OS << " }";
Anders Carlssone349bea2009-04-23 02:32:43 +00001884}
1885
Richard Smith7c3e6152013-06-12 22:31:48 +00001886void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
1887 PrintExpr(E->getSubExpr());
1888}
1889
John McCall4765fa02010-12-06 08:20:24 +00001890void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001891 // Just forward to the subexpression.
Anders Carlsson02bbfa32009-04-24 22:47:04 +00001892 PrintExpr(E->getSubExpr());
1893}
1894
Mike Stump1eb44332009-09-09 15:08:12 +00001895void
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001896StmtPrinter::VisitCXXUnresolvedConstructExpr(
1897 CXXUnresolvedConstructExpr *Node) {
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001898 Node->getTypeAsWritten().print(OS, Policy);
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001899 OS << "(";
1900 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001901 ArgEnd = Node->arg_end();
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001902 Arg != ArgEnd; ++Arg) {
1903 if (Arg != Node->arg_begin())
1904 OS << ", ";
1905 PrintExpr(*Arg);
1906 }
1907 OS << ")";
1908}
1909
John McCall865d4472009-11-19 22:55:06 +00001910void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1911 CXXDependentScopeMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001912 if (!Node->isImplicitAccess()) {
1913 PrintExpr(Node->getBase());
1914 OS << (Node->isArrow() ? "->" : ".");
1915 }
Douglas Gregora38c6872009-09-03 16:14:30 +00001916 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1917 Qualifier->print(OS, Policy);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001918 if (Node->hasTemplateKeyword())
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001919 OS << "template ";
Abramo Bagnara25777432010-08-11 22:01:17 +00001920 OS << Node->getMemberNameInfo();
Benjamin Kramer5eada842013-02-22 15:46:01 +00001921 if (Node->hasExplicitTemplateArgs())
1922 TemplateSpecializationType::PrintTemplateArgumentList(
1923 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregor1c0ca592009-05-22 21:13:27 +00001924}
1925
John McCall129e2df2009-11-30 22:42:35 +00001926void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001927 if (!Node->isImplicitAccess()) {
1928 PrintExpr(Node->getBase());
1929 OS << (Node->isArrow() ? "->" : ".");
1930 }
John McCall129e2df2009-11-30 22:42:35 +00001931 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1932 Qualifier->print(OS, Policy);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001933 if (Node->hasTemplateKeyword())
1934 OS << "template ";
Abramo Bagnara25777432010-08-11 22:01:17 +00001935 OS << Node->getMemberNameInfo();
Benjamin Kramer5eada842013-02-22 15:46:01 +00001936 if (Node->hasExplicitTemplateArgs())
1937 TemplateSpecializationType::PrintTemplateArgumentList(
1938 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
John McCall129e2df2009-11-30 22:42:35 +00001939}
1940
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001941static const char *getTypeTraitName(TypeTrait TT) {
1942 switch (TT) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001943#define TYPE_TRAIT_1(Spelling, Name, Key) \
1944case clang::UTT_##Name: return #Spelling;
1945#define TYPE_TRAIT_2(Spelling, Name, Key) \
1946case clang::BTT_##Name: return #Spelling;
1947#define TYPE_TRAIT_N(Spelling, Name, Key) \
1948 case clang::TT_##Name: return #Spelling;
1949#include "clang/Basic/TokenKinds.def"
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001950 }
1951 llvm_unreachable("Type trait not covered by switch");
1952}
1953
John Wiegley21ff2e52011-04-28 00:16:57 +00001954static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1955 switch (ATT) {
1956 case ATT_ArrayRank: return "__array_rank";
1957 case ATT_ArrayExtent: return "__array_extent";
1958 }
Chandler Carrutha280bc82011-05-01 07:23:23 +00001959 llvm_unreachable("Array type trait not covered by switch");
John Wiegley21ff2e52011-04-28 00:16:57 +00001960}
1961
John Wiegley55262202011-04-25 06:54:41 +00001962static const char *getExpressionTraitName(ExpressionTrait ET) {
1963 switch (ET) {
John Wiegley55262202011-04-25 06:54:41 +00001964 case ET_IsLValueExpr: return "__is_lvalue_expr";
1965 case ET_IsRValueExpr: return "__is_rvalue_expr";
1966 }
Chandler Carrutha280bc82011-05-01 07:23:23 +00001967 llvm_unreachable("Expression type trait not covered by switch");
John Wiegley55262202011-04-25 06:54:41 +00001968}
1969
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001970void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1971 OS << getTypeTraitName(E->getTrait()) << "(";
1972 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1973 if (I > 0)
1974 OS << ", ";
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001975 E->getArg(I)->getType().print(OS, Policy);
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001976 }
1977 OS << ")";
1978}
1979
John Wiegley21ff2e52011-04-28 00:16:57 +00001980void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001981 OS << getTypeTraitName(E->getTrait()) << '(';
1982 E->getQueriedType().print(OS, Policy);
1983 OS << ')';
John Wiegley21ff2e52011-04-28 00:16:57 +00001984}
1985
John Wiegley55262202011-04-25 06:54:41 +00001986void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
Benjamin Kramer9d4df462013-02-22 14:19:01 +00001987 OS << getExpressionTraitName(E->getTrait()) << '(';
1988 PrintExpr(E->getQueriedExpression());
1989 OS << ')';
John Wiegley55262202011-04-25 06:54:41 +00001990}
1991
Sebastian Redl2e156222010-09-10 20:55:43 +00001992void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1993 OS << "noexcept(";
1994 PrintExpr(E->getOperand());
1995 OS << ")";
1996}
1997
Douglas Gregoree8aff02011-01-04 17:33:58 +00001998void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregorbe230c32011-01-03 17:17:50 +00001999 PrintExpr(E->getPattern());
2000 OS << "...";
2001}
2002
Douglas Gregoree8aff02011-01-04 17:33:58 +00002003void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramera59d20b2012-02-07 11:57:57 +00002004 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregoree8aff02011-01-04 17:33:58 +00002005}
2006
Douglas Gregorc7793c72011-01-15 01:15:58 +00002007void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2008 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramera59d20b2012-02-07 11:57:57 +00002009 OS << *Node->getParameterPack();
Douglas Gregorc7793c72011-01-15 01:15:58 +00002010}
2011
John McCall91a57552011-07-15 05:09:51 +00002012void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2013 SubstNonTypeTemplateParmExpr *Node) {
2014 Visit(Node->getReplacement());
2015}
2016
Richard Smith9a4db032012-09-12 00:56:43 +00002017void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2018 OS << *E->getParameterPack();
2019}
2020
Douglas Gregor03e80032011-06-21 17:03:29 +00002021void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2022 PrintExpr(Node->GetTemporaryExpr());
2023}
2024
Stephen Hines176edba2014-12-01 14:53:08 -08002025void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2026 OS << "(";
2027 if (E->getLHS()) {
2028 PrintExpr(E->getLHS());
2029 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2030 }
2031 OS << "...";
2032 if (E->getRHS()) {
2033 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2034 PrintExpr(E->getRHS());
2035 }
2036 OS << ")";
2037}
2038
Mike Stump1eb44332009-09-09 15:08:12 +00002039// Obj-C
Anders Carlsson55085182007-08-21 17:43:55 +00002040
2041void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2042 OS << "@";
2043 VisitStringLiteral(Node->getString());
2044}
Reid Spencer5f016e22007-07-11 17:01:13 +00002045
Patrick Beardeb382ec2012-04-19 00:25:12 +00002046void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002047 OS << "@";
Patrick Beardeb382ec2012-04-19 00:25:12 +00002048 Visit(E->getSubExpr());
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002049}
2050
2051void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2052 OS << "@[ ";
2053 StmtRange ch = E->children();
2054 if (ch.first != ch.second) {
2055 while (1) {
2056 Visit(*ch.first);
2057 ++ch.first;
2058 if (ch.first == ch.second) break;
2059 OS << ", ";
2060 }
2061 }
2062 OS << " ]";
2063}
2064
2065void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2066 OS << "@{ ";
2067 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2068 if (I > 0)
2069 OS << ", ";
2070
2071 ObjCDictionaryElement Element = E->getKeyValueElement(I);
2072 Visit(Element.Key);
2073 OS << " : ";
2074 Visit(Element.Value);
2075 if (Element.isPackExpansion())
2076 OS << "...";
2077 }
2078 OS << " }";
2079}
2080
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002081void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Benjamin Kramer9d4df462013-02-22 14:19:01 +00002082 OS << "@encode(";
2083 Node->getEncodedType().print(OS, Policy);
2084 OS << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002085}
2086
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002087void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002088 OS << "@selector(";
2089 Node->getSelector().print(OS);
2090 OS << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002091}
2092
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002093void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +00002094 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002095}
2096
Steve Naroff563477d2007-09-18 23:55:05 +00002097void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2098 OS << "[";
Douglas Gregor04badcf2010-04-21 00:45:42 +00002099 switch (Mess->getReceiverKind()) {
2100 case ObjCMessageExpr::Instance:
2101 PrintExpr(Mess->getInstanceReceiver());
2102 break;
2103
2104 case ObjCMessageExpr::Class:
Benjamin Kramer9d4df462013-02-22 14:19:01 +00002105 Mess->getClassReceiver().print(OS, Policy);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002106 break;
2107
2108 case ObjCMessageExpr::SuperInstance:
2109 case ObjCMessageExpr::SuperClass:
2110 OS << "Super";
2111 break;
2112 }
2113
Ted Kremenekc29efd82008-05-02 17:32:38 +00002114 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00002115 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00002116 if (selector.isUnarySelector()) {
Douglas Gregor813d8342011-02-18 22:29:55 +00002117 OS << selector.getNameForSlot(0);
Steve Naroff6a8a9a42007-10-02 20:01:56 +00002118 } else {
2119 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00002120 if (i < selector.getNumArgs()) {
2121 if (i > 0) OS << ' ';
2122 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00002123 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00002124 else
2125 OS << ":";
2126 }
2127 else OS << ", "; // Handle variadic methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002128
Steve Naroff6a8a9a42007-10-02 20:01:56 +00002129 PrintExpr(Mess->getArg(i));
2130 }
Steve Naroff563477d2007-09-18 23:55:05 +00002131 }
2132 OS << "]";
2133}
2134
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002135void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2136 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2137}
2138
John McCallf85e1932011-06-15 23:02:42 +00002139void
2140StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2141 PrintExpr(E->getSubExpr());
2142}
2143
2144void
2145StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
Benjamin Kramer9d4df462013-02-22 14:19:01 +00002146 OS << '(' << E->getBridgeKindName();
2147 E->getType().print(OS, Policy);
2148 OS << ')';
John McCallf85e1932011-06-15 23:02:42 +00002149 PrintExpr(E->getSubExpr());
2150}
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00002151
Steve Naroff4eb206b2008-09-03 18:15:37 +00002152void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00002153 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00002154 OS << "^";
Mike Stump1eb44332009-09-09 15:08:12 +00002155
Steve Naroff4eb206b2008-09-03 18:15:37 +00002156 const FunctionType *AFT = Node->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00002157
Douglas Gregor72564e72009-02-26 23:50:07 +00002158 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00002159 OS << "()";
Douglas Gregor72564e72009-02-26 23:50:07 +00002160 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00002161 OS << '(';
Steve Naroff56ee6892008-10-08 17:01:13 +00002162 for (BlockDecl::param_iterator AI = BD->param_begin(),
2163 E = BD->param_end(); AI != E; ++AI) {
2164 if (AI != BD->param_begin()) OS << ", ";
Benjamin Kramer9d4df462013-02-22 14:19:01 +00002165 std::string ParamStr = (*AI)->getNameAsString();
2166 (*AI)->getType().print(OS, Policy, ParamStr);
Steve Naroff4eb206b2008-09-03 18:15:37 +00002167 }
Mike Stump1eb44332009-09-09 15:08:12 +00002168
Douglas Gregor72564e72009-02-26 23:50:07 +00002169 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00002170 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00002171 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00002172 OS << "...";
2173 }
2174 OS << ')';
2175 }
Fariborz Jahanian3e2fe862012-12-04 21:15:23 +00002176 OS << "{ }";
Steve Naroff4eb206b2008-09-03 18:15:37 +00002177}
2178
Ted Kremenek381c0662011-11-30 22:08:08 +00002179void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2180 PrintExpr(Node->getSourceExpr());
2181}
John McCall7cd7d1a2010-11-15 23:31:06 +00002182
Stephen Hines176edba2014-12-01 14:53:08 -08002183void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2184 // TODO: Print something reasonable for a TypoExpr, if necessary.
2185 assert(false && "Cannot print TypoExpr nodes");
2186}
2187
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002188void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2189 OS << "__builtin_astype(";
2190 PrintExpr(Node->getSrcExpr());
Benjamin Kramer9d4df462013-02-22 14:19:01 +00002191 OS << ", ";
2192 Node->getType().print(OS, Policy);
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002193 OS << ")";
2194}
2195
Reid Spencer5f016e22007-07-11 17:01:13 +00002196//===----------------------------------------------------------------------===//
2197// Stmt method implementations
2198//===----------------------------------------------------------------------===//
2199
Craig Topper8b4b98b2013-08-22 06:02:26 +00002200void Stmt::dumpPretty(const ASTContext &Context) const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002201 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
Reid Spencer5f016e22007-07-11 17:01:13 +00002202}
2203
Richard Smithd1420c62012-08-16 03:56:14 +00002204void Stmt::printPretty(raw_ostream &OS,
2205 PrinterHelper *Helper,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002206 const PrintingPolicy &Policy,
2207 unsigned Indentation) const {
Richard Smithd1420c62012-08-16 03:56:14 +00002208 StmtPrinter P(OS, Helper, Policy, Indentation);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00002209 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00002210}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002211
2212//===----------------------------------------------------------------------===//
2213// PrinterHelper
2214//===----------------------------------------------------------------------===//
2215
2216// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00002217PrinterHelper::~PrinterHelper() {}