1 //===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which
11 // pretty print the AST back out to C code.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Attr.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/DeclOpenMP.h"
20 #include "clang/AST/DeclTemplate.h"
21 #include "clang/AST/Expr.h"
22 #include "clang/AST/ExprCXX.h"
23 #include "clang/AST/ExprOpenMP.h"
24 #include "clang/AST/PrettyPrinter.h"
25 #include "clang/AST/StmtVisitor.h"
26 #include "clang/Basic/CharInfo.h"
27 #include "llvm/ADT/SmallString.h"
28 #include "llvm/Support/Format.h"
29 using namespace clang;
30
31 //===----------------------------------------------------------------------===//
32 // StmtPrinter Visitor
33 //===----------------------------------------------------------------------===//
34
35 namespace {
36 class StmtPrinter : public StmtVisitor<StmtPrinter> {
37 raw_ostream &OS;
38 unsigned IndentLevel;
39 clang::PrinterHelper* Helper;
40 PrintingPolicy Policy;
41
42 public:
StmtPrinter(raw_ostream & os,PrinterHelper * helper,const PrintingPolicy & Policy,unsigned Indentation=0)43 StmtPrinter(raw_ostream &os, PrinterHelper* helper,
44 const PrintingPolicy &Policy,
45 unsigned Indentation = 0)
46 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy) {}
47
PrintStmt(Stmt * S)48 void PrintStmt(Stmt *S) {
49 PrintStmt(S, Policy.Indentation);
50 }
51
PrintStmt(Stmt * S,int SubIndent)52 void PrintStmt(Stmt *S, int SubIndent) {
53 IndentLevel += SubIndent;
54 if (S && isa<Expr>(S)) {
55 // If this is an expr used in a stmt context, indent and newline it.
56 Indent();
57 Visit(S);
58 OS << ";\n";
59 } else if (S) {
60 Visit(S);
61 } else {
62 Indent() << "<<<NULL STATEMENT>>>\n";
63 }
64 IndentLevel -= SubIndent;
65 }
66
67 void PrintRawCompoundStmt(CompoundStmt *S);
68 void PrintRawDecl(Decl *D);
69 void PrintRawDeclStmt(const DeclStmt *S);
70 void PrintRawIfStmt(IfStmt *If);
71 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
72 void PrintCallArgs(CallExpr *E);
73 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
74 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
75 void PrintOMPExecutableDirective(OMPExecutableDirective *S);
76
PrintExpr(Expr * E)77 void PrintExpr(Expr *E) {
78 if (E)
79 Visit(E);
80 else
81 OS << "<null expr>";
82 }
83
Indent(int Delta=0)84 raw_ostream &Indent(int Delta = 0) {
85 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
86 OS << " ";
87 return OS;
88 }
89
Visit(Stmt * S)90 void Visit(Stmt* S) {
91 if (Helper && Helper->handledStmt(S,OS))
92 return;
93 else StmtVisitor<StmtPrinter>::Visit(S);
94 }
95
VisitStmt(Stmt * Node)96 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
97 Indent() << "<<unknown stmt type>>\n";
98 }
VisitExpr(Expr * Node)99 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
100 OS << "<<unknown expr type>>";
101 }
102 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
103
104 #define ABSTRACT_STMT(CLASS)
105 #define STMT(CLASS, PARENT) \
106 void Visit##CLASS(CLASS *Node);
107 #include "clang/AST/StmtNodes.inc"
108 };
109 }
110
111 //===----------------------------------------------------------------------===//
112 // Stmt printing methods.
113 //===----------------------------------------------------------------------===//
114
115 /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
116 /// with no newline after the }.
PrintRawCompoundStmt(CompoundStmt * Node)117 void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
118 OS << "{\n";
119 for (auto *I : Node->body())
120 PrintStmt(I);
121
122 Indent() << "}";
123 }
124
PrintRawDecl(Decl * D)125 void StmtPrinter::PrintRawDecl(Decl *D) {
126 D->print(OS, Policy, IndentLevel);
127 }
128
PrintRawDeclStmt(const DeclStmt * S)129 void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
130 SmallVector<Decl*, 2> Decls(S->decls());
131 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
132 }
133
VisitNullStmt(NullStmt * Node)134 void StmtPrinter::VisitNullStmt(NullStmt *Node) {
135 Indent() << ";\n";
136 }
137
VisitDeclStmt(DeclStmt * Node)138 void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
139 Indent();
140 PrintRawDeclStmt(Node);
141 OS << ";\n";
142 }
143
VisitCompoundStmt(CompoundStmt * Node)144 void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
145 Indent();
146 PrintRawCompoundStmt(Node);
147 OS << "\n";
148 }
149
VisitCaseStmt(CaseStmt * Node)150 void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
151 Indent(-1) << "case ";
152 PrintExpr(Node->getLHS());
153 if (Node->getRHS()) {
154 OS << " ... ";
155 PrintExpr(Node->getRHS());
156 }
157 OS << ":\n";
158
159 PrintStmt(Node->getSubStmt(), 0);
160 }
161
VisitDefaultStmt(DefaultStmt * Node)162 void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
163 Indent(-1) << "default:\n";
164 PrintStmt(Node->getSubStmt(), 0);
165 }
166
VisitLabelStmt(LabelStmt * Node)167 void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
168 Indent(-1) << Node->getName() << ":\n";
169 PrintStmt(Node->getSubStmt(), 0);
170 }
171
VisitAttributedStmt(AttributedStmt * Node)172 void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
173 for (const auto *Attr : Node->getAttrs()) {
174 Attr->printPretty(OS, Policy);
175 }
176
177 PrintStmt(Node->getSubStmt(), 0);
178 }
179
PrintRawIfStmt(IfStmt * If)180 void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
181 OS << "if (";
182 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
183 PrintRawDeclStmt(DS);
184 else
185 PrintExpr(If->getCond());
186 OS << ')';
187
188 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
189 OS << ' ';
190 PrintRawCompoundStmt(CS);
191 OS << (If->getElse() ? ' ' : '\n');
192 } else {
193 OS << '\n';
194 PrintStmt(If->getThen());
195 if (If->getElse()) Indent();
196 }
197
198 if (Stmt *Else = If->getElse()) {
199 OS << "else";
200
201 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
202 OS << ' ';
203 PrintRawCompoundStmt(CS);
204 OS << '\n';
205 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
206 OS << ' ';
207 PrintRawIfStmt(ElseIf);
208 } else {
209 OS << '\n';
210 PrintStmt(If->getElse());
211 }
212 }
213 }
214
VisitIfStmt(IfStmt * If)215 void StmtPrinter::VisitIfStmt(IfStmt *If) {
216 Indent();
217 PrintRawIfStmt(If);
218 }
219
VisitSwitchStmt(SwitchStmt * Node)220 void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
221 Indent() << "switch (";
222 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
223 PrintRawDeclStmt(DS);
224 else
225 PrintExpr(Node->getCond());
226 OS << ")";
227
228 // Pretty print compoundstmt bodies (very common).
229 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
230 OS << " ";
231 PrintRawCompoundStmt(CS);
232 OS << "\n";
233 } else {
234 OS << "\n";
235 PrintStmt(Node->getBody());
236 }
237 }
238
VisitWhileStmt(WhileStmt * Node)239 void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
240 Indent() << "while (";
241 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
242 PrintRawDeclStmt(DS);
243 else
244 PrintExpr(Node->getCond());
245 OS << ")\n";
246 PrintStmt(Node->getBody());
247 }
248
VisitDoStmt(DoStmt * Node)249 void StmtPrinter::VisitDoStmt(DoStmt *Node) {
250 Indent() << "do ";
251 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
252 PrintRawCompoundStmt(CS);
253 OS << " ";
254 } else {
255 OS << "\n";
256 PrintStmt(Node->getBody());
257 Indent();
258 }
259
260 OS << "while (";
261 PrintExpr(Node->getCond());
262 OS << ");\n";
263 }
264
VisitForStmt(ForStmt * Node)265 void StmtPrinter::VisitForStmt(ForStmt *Node) {
266 Indent() << "for (";
267 if (Node->getInit()) {
268 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
269 PrintRawDeclStmt(DS);
270 else
271 PrintExpr(cast<Expr>(Node->getInit()));
272 }
273 OS << ";";
274 if (Node->getCond()) {
275 OS << " ";
276 PrintExpr(Node->getCond());
277 }
278 OS << ";";
279 if (Node->getInc()) {
280 OS << " ";
281 PrintExpr(Node->getInc());
282 }
283 OS << ") ";
284
285 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
286 PrintRawCompoundStmt(CS);
287 OS << "\n";
288 } else {
289 OS << "\n";
290 PrintStmt(Node->getBody());
291 }
292 }
293
VisitObjCForCollectionStmt(ObjCForCollectionStmt * Node)294 void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
295 Indent() << "for (";
296 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
297 PrintRawDeclStmt(DS);
298 else
299 PrintExpr(cast<Expr>(Node->getElement()));
300 OS << " in ";
301 PrintExpr(Node->getCollection());
302 OS << ") ";
303
304 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
305 PrintRawCompoundStmt(CS);
306 OS << "\n";
307 } else {
308 OS << "\n";
309 PrintStmt(Node->getBody());
310 }
311 }
312
VisitCXXForRangeStmt(CXXForRangeStmt * Node)313 void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
314 Indent() << "for (";
315 PrintingPolicy SubPolicy(Policy);
316 SubPolicy.SuppressInitializers = true;
317 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
318 OS << " : ";
319 PrintExpr(Node->getRangeInit());
320 OS << ") {\n";
321 PrintStmt(Node->getBody());
322 Indent() << "}";
323 if (Policy.IncludeNewlines) OS << "\n";
324 }
325
VisitMSDependentExistsStmt(MSDependentExistsStmt * Node)326 void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
327 Indent();
328 if (Node->isIfExists())
329 OS << "__if_exists (";
330 else
331 OS << "__if_not_exists (";
332
333 if (NestedNameSpecifier *Qualifier
334 = Node->getQualifierLoc().getNestedNameSpecifier())
335 Qualifier->print(OS, Policy);
336
337 OS << Node->getNameInfo() << ") ";
338
339 PrintRawCompoundStmt(Node->getSubStmt());
340 }
341
VisitGotoStmt(GotoStmt * Node)342 void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
343 Indent() << "goto " << Node->getLabel()->getName() << ";";
344 if (Policy.IncludeNewlines) OS << "\n";
345 }
346
VisitIndirectGotoStmt(IndirectGotoStmt * Node)347 void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
348 Indent() << "goto *";
349 PrintExpr(Node->getTarget());
350 OS << ";";
351 if (Policy.IncludeNewlines) OS << "\n";
352 }
353
VisitContinueStmt(ContinueStmt * Node)354 void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
355 Indent() << "continue;";
356 if (Policy.IncludeNewlines) OS << "\n";
357 }
358
VisitBreakStmt(BreakStmt * Node)359 void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
360 Indent() << "break;";
361 if (Policy.IncludeNewlines) OS << "\n";
362 }
363
364
VisitReturnStmt(ReturnStmt * Node)365 void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
366 Indent() << "return";
367 if (Node->getRetValue()) {
368 OS << " ";
369 PrintExpr(Node->getRetValue());
370 }
371 OS << ";";
372 if (Policy.IncludeNewlines) OS << "\n";
373 }
374
375
VisitGCCAsmStmt(GCCAsmStmt * Node)376 void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
377 Indent() << "asm ";
378
379 if (Node->isVolatile())
380 OS << "volatile ";
381
382 OS << "(";
383 VisitStringLiteral(Node->getAsmString());
384
385 // Outputs
386 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
387 Node->getNumClobbers() != 0)
388 OS << " : ";
389
390 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
391 if (i != 0)
392 OS << ", ";
393
394 if (!Node->getOutputName(i).empty()) {
395 OS << '[';
396 OS << Node->getOutputName(i);
397 OS << "] ";
398 }
399
400 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
401 OS << " (";
402 Visit(Node->getOutputExpr(i));
403 OS << ")";
404 }
405
406 // Inputs
407 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
408 OS << " : ";
409
410 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
411 if (i != 0)
412 OS << ", ";
413
414 if (!Node->getInputName(i).empty()) {
415 OS << '[';
416 OS << Node->getInputName(i);
417 OS << "] ";
418 }
419
420 VisitStringLiteral(Node->getInputConstraintLiteral(i));
421 OS << " (";
422 Visit(Node->getInputExpr(i));
423 OS << ")";
424 }
425
426 // Clobbers
427 if (Node->getNumClobbers() != 0)
428 OS << " : ";
429
430 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
431 if (i != 0)
432 OS << ", ";
433
434 VisitStringLiteral(Node->getClobberStringLiteral(i));
435 }
436
437 OS << ");";
438 if (Policy.IncludeNewlines) OS << "\n";
439 }
440
VisitMSAsmStmt(MSAsmStmt * Node)441 void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
442 // FIXME: Implement MS style inline asm statement printer.
443 Indent() << "__asm ";
444 if (Node->hasBraces())
445 OS << "{\n";
446 OS << Node->getAsmString() << "\n";
447 if (Node->hasBraces())
448 Indent() << "}\n";
449 }
450
VisitCapturedStmt(CapturedStmt * Node)451 void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
452 PrintStmt(Node->getCapturedDecl()->getBody());
453 }
454
VisitObjCAtTryStmt(ObjCAtTryStmt * Node)455 void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
456 Indent() << "@try";
457 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
458 PrintRawCompoundStmt(TS);
459 OS << "\n";
460 }
461
462 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
463 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
464 Indent() << "@catch(";
465 if (catchStmt->getCatchParamDecl()) {
466 if (Decl *DS = catchStmt->getCatchParamDecl())
467 PrintRawDecl(DS);
468 }
469 OS << ")";
470 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
471 PrintRawCompoundStmt(CS);
472 OS << "\n";
473 }
474 }
475
476 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
477 Node->getFinallyStmt())) {
478 Indent() << "@finally";
479 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
480 OS << "\n";
481 }
482 }
483
VisitObjCAtFinallyStmt(ObjCAtFinallyStmt * Node)484 void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
485 }
486
VisitObjCAtCatchStmt(ObjCAtCatchStmt * Node)487 void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
488 Indent() << "@catch (...) { /* todo */ } \n";
489 }
490
VisitObjCAtThrowStmt(ObjCAtThrowStmt * Node)491 void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
492 Indent() << "@throw";
493 if (Node->getThrowExpr()) {
494 OS << " ";
495 PrintExpr(Node->getThrowExpr());
496 }
497 OS << ";\n";
498 }
499
VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt * Node)500 void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
501 Indent() << "@synchronized (";
502 PrintExpr(Node->getSynchExpr());
503 OS << ")";
504 PrintRawCompoundStmt(Node->getSynchBody());
505 OS << "\n";
506 }
507
VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt * Node)508 void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
509 Indent() << "@autoreleasepool";
510 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
511 OS << "\n";
512 }
513
PrintRawCXXCatchStmt(CXXCatchStmt * Node)514 void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
515 OS << "catch (";
516 if (Decl *ExDecl = Node->getExceptionDecl())
517 PrintRawDecl(ExDecl);
518 else
519 OS << "...";
520 OS << ") ";
521 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
522 }
523
VisitCXXCatchStmt(CXXCatchStmt * Node)524 void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
525 Indent();
526 PrintRawCXXCatchStmt(Node);
527 OS << "\n";
528 }
529
VisitCXXTryStmt(CXXTryStmt * Node)530 void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
531 Indent() << "try ";
532 PrintRawCompoundStmt(Node->getTryBlock());
533 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
534 OS << " ";
535 PrintRawCXXCatchStmt(Node->getHandler(i));
536 }
537 OS << "\n";
538 }
539
VisitSEHTryStmt(SEHTryStmt * Node)540 void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
541 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
542 PrintRawCompoundStmt(Node->getTryBlock());
543 SEHExceptStmt *E = Node->getExceptHandler();
544 SEHFinallyStmt *F = Node->getFinallyHandler();
545 if(E)
546 PrintRawSEHExceptHandler(E);
547 else {
548 assert(F && "Must have a finally block...");
549 PrintRawSEHFinallyStmt(F);
550 }
551 OS << "\n";
552 }
553
PrintRawSEHFinallyStmt(SEHFinallyStmt * Node)554 void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
555 OS << "__finally ";
556 PrintRawCompoundStmt(Node->getBlock());
557 OS << "\n";
558 }
559
PrintRawSEHExceptHandler(SEHExceptStmt * Node)560 void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
561 OS << "__except (";
562 VisitExpr(Node->getFilterExpr());
563 OS << ")\n";
564 PrintRawCompoundStmt(Node->getBlock());
565 OS << "\n";
566 }
567
VisitSEHExceptStmt(SEHExceptStmt * Node)568 void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
569 Indent();
570 PrintRawSEHExceptHandler(Node);
571 OS << "\n";
572 }
573
VisitSEHFinallyStmt(SEHFinallyStmt * Node)574 void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
575 Indent();
576 PrintRawSEHFinallyStmt(Node);
577 OS << "\n";
578 }
579
VisitSEHLeaveStmt(SEHLeaveStmt * Node)580 void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
581 Indent() << "__leave;";
582 if (Policy.IncludeNewlines) OS << "\n";
583 }
584
585 //===----------------------------------------------------------------------===//
586 // OpenMP clauses printing methods
587 //===----------------------------------------------------------------------===//
588
589 namespace {
590 class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
591 raw_ostream &OS;
592 const PrintingPolicy &Policy;
593 /// \brief Process clauses with list of variables.
594 template <typename T>
595 void VisitOMPClauseList(T *Node, char StartSym);
596 public:
OMPClausePrinter(raw_ostream & OS,const PrintingPolicy & Policy)597 OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy)
598 : OS(OS), Policy(Policy) { }
599 #define OPENMP_CLAUSE(Name, Class) \
600 void Visit##Class(Class *S);
601 #include "clang/Basic/OpenMPKinds.def"
602 };
603
VisitOMPIfClause(OMPIfClause * Node)604 void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
605 OS << "if(";
606 if (Node->getNameModifier() != OMPD_unknown)
607 OS << getOpenMPDirectiveName(Node->getNameModifier()) << ": ";
608 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
609 OS << ")";
610 }
611
VisitOMPFinalClause(OMPFinalClause * Node)612 void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) {
613 OS << "final(";
614 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
615 OS << ")";
616 }
617
VisitOMPNumThreadsClause(OMPNumThreadsClause * Node)618 void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
619 OS << "num_threads(";
620 Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0);
621 OS << ")";
622 }
623
VisitOMPSafelenClause(OMPSafelenClause * Node)624 void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
625 OS << "safelen(";
626 Node->getSafelen()->printPretty(OS, nullptr, Policy, 0);
627 OS << ")";
628 }
629
VisitOMPSimdlenClause(OMPSimdlenClause * Node)630 void OMPClausePrinter::VisitOMPSimdlenClause(OMPSimdlenClause *Node) {
631 OS << "simdlen(";
632 Node->getSimdlen()->printPretty(OS, nullptr, Policy, 0);
633 OS << ")";
634 }
635
VisitOMPCollapseClause(OMPCollapseClause * Node)636 void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) {
637 OS << "collapse(";
638 Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0);
639 OS << ")";
640 }
641
VisitOMPDefaultClause(OMPDefaultClause * Node)642 void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
643 OS << "default("
644 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
645 << ")";
646 }
647
VisitOMPProcBindClause(OMPProcBindClause * Node)648 void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) {
649 OS << "proc_bind("
650 << getOpenMPSimpleClauseTypeName(OMPC_proc_bind, Node->getProcBindKind())
651 << ")";
652 }
653
VisitOMPScheduleClause(OMPScheduleClause * Node)654 void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) {
655 OS << "schedule(";
656 if (Node->getFirstScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
657 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
658 Node->getFirstScheduleModifier());
659 if (Node->getSecondScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
660 OS << ", ";
661 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
662 Node->getSecondScheduleModifier());
663 }
664 OS << ": ";
665 }
666 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind());
667 if (auto *E = Node->getChunkSize()) {
668 OS << ", ";
669 E->printPretty(OS, nullptr, Policy);
670 }
671 OS << ")";
672 }
673
VisitOMPOrderedClause(OMPOrderedClause * Node)674 void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *Node) {
675 OS << "ordered";
676 if (auto *Num = Node->getNumForLoops()) {
677 OS << "(";
678 Num->printPretty(OS, nullptr, Policy, 0);
679 OS << ")";
680 }
681 }
682
VisitOMPNowaitClause(OMPNowaitClause *)683 void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) {
684 OS << "nowait";
685 }
686
VisitOMPUntiedClause(OMPUntiedClause *)687 void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) {
688 OS << "untied";
689 }
690
VisitOMPNogroupClause(OMPNogroupClause *)691 void OMPClausePrinter::VisitOMPNogroupClause(OMPNogroupClause *) {
692 OS << "nogroup";
693 }
694
VisitOMPMergeableClause(OMPMergeableClause *)695 void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) {
696 OS << "mergeable";
697 }
698
VisitOMPReadClause(OMPReadClause *)699 void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; }
700
VisitOMPWriteClause(OMPWriteClause *)701 void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; }
702
VisitOMPUpdateClause(OMPUpdateClause *)703 void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *) {
704 OS << "update";
705 }
706
VisitOMPCaptureClause(OMPCaptureClause *)707 void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) {
708 OS << "capture";
709 }
710
VisitOMPSeqCstClause(OMPSeqCstClause *)711 void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) {
712 OS << "seq_cst";
713 }
714
VisitOMPThreadsClause(OMPThreadsClause *)715 void OMPClausePrinter::VisitOMPThreadsClause(OMPThreadsClause *) {
716 OS << "threads";
717 }
718
VisitOMPSIMDClause(OMPSIMDClause *)719 void OMPClausePrinter::VisitOMPSIMDClause(OMPSIMDClause *) { OS << "simd"; }
720
VisitOMPDeviceClause(OMPDeviceClause * Node)721 void OMPClausePrinter::VisitOMPDeviceClause(OMPDeviceClause *Node) {
722 OS << "device(";
723 Node->getDevice()->printPretty(OS, nullptr, Policy, 0);
724 OS << ")";
725 }
726
VisitOMPNumTeamsClause(OMPNumTeamsClause * Node)727 void OMPClausePrinter::VisitOMPNumTeamsClause(OMPNumTeamsClause *Node) {
728 OS << "num_teams(";
729 Node->getNumTeams()->printPretty(OS, nullptr, Policy, 0);
730 OS << ")";
731 }
732
VisitOMPThreadLimitClause(OMPThreadLimitClause * Node)733 void OMPClausePrinter::VisitOMPThreadLimitClause(OMPThreadLimitClause *Node) {
734 OS << "thread_limit(";
735 Node->getThreadLimit()->printPretty(OS, nullptr, Policy, 0);
736 OS << ")";
737 }
738
VisitOMPPriorityClause(OMPPriorityClause * Node)739 void OMPClausePrinter::VisitOMPPriorityClause(OMPPriorityClause *Node) {
740 OS << "priority(";
741 Node->getPriority()->printPretty(OS, nullptr, Policy, 0);
742 OS << ")";
743 }
744
VisitOMPGrainsizeClause(OMPGrainsizeClause * Node)745 void OMPClausePrinter::VisitOMPGrainsizeClause(OMPGrainsizeClause *Node) {
746 OS << "grainsize(";
747 Node->getGrainsize()->printPretty(OS, nullptr, Policy, 0);
748 OS << ")";
749 }
750
VisitOMPNumTasksClause(OMPNumTasksClause * Node)751 void OMPClausePrinter::VisitOMPNumTasksClause(OMPNumTasksClause *Node) {
752 OS << "num_tasks(";
753 Node->getNumTasks()->printPretty(OS, nullptr, Policy, 0);
754 OS << ")";
755 }
756
VisitOMPHintClause(OMPHintClause * Node)757 void OMPClausePrinter::VisitOMPHintClause(OMPHintClause *Node) {
758 OS << "hint(";
759 Node->getHint()->printPretty(OS, nullptr, Policy, 0);
760 OS << ")";
761 }
762
763 template<typename T>
VisitOMPClauseList(T * Node,char StartSym)764 void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
765 for (typename T::varlist_iterator I = Node->varlist_begin(),
766 E = Node->varlist_end();
767 I != E; ++I) {
768 assert(*I && "Expected non-null Stmt");
769 OS << (I == Node->varlist_begin() ? StartSym : ',');
770 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*I)) {
771 if (isa<OMPCapturedExprDecl>(DRE->getDecl()))
772 DRE->printPretty(OS, nullptr, Policy, 0);
773 else
774 DRE->getDecl()->printQualifiedName(OS);
775 } else
776 (*I)->printPretty(OS, nullptr, Policy, 0);
777 }
778 }
779
VisitOMPPrivateClause(OMPPrivateClause * Node)780 void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
781 if (!Node->varlist_empty()) {
782 OS << "private";
783 VisitOMPClauseList(Node, '(');
784 OS << ")";
785 }
786 }
787
VisitOMPFirstprivateClause(OMPFirstprivateClause * Node)788 void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
789 if (!Node->varlist_empty()) {
790 OS << "firstprivate";
791 VisitOMPClauseList(Node, '(');
792 OS << ")";
793 }
794 }
795
VisitOMPLastprivateClause(OMPLastprivateClause * Node)796 void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) {
797 if (!Node->varlist_empty()) {
798 OS << "lastprivate";
799 VisitOMPClauseList(Node, '(');
800 OS << ")";
801 }
802 }
803
VisitOMPSharedClause(OMPSharedClause * Node)804 void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
805 if (!Node->varlist_empty()) {
806 OS << "shared";
807 VisitOMPClauseList(Node, '(');
808 OS << ")";
809 }
810 }
811
VisitOMPReductionClause(OMPReductionClause * Node)812 void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) {
813 if (!Node->varlist_empty()) {
814 OS << "reduction(";
815 NestedNameSpecifier *QualifierLoc =
816 Node->getQualifierLoc().getNestedNameSpecifier();
817 OverloadedOperatorKind OOK =
818 Node->getNameInfo().getName().getCXXOverloadedOperator();
819 if (QualifierLoc == nullptr && OOK != OO_None) {
820 // Print reduction identifier in C format
821 OS << getOperatorSpelling(OOK);
822 } else {
823 // Use C++ format
824 if (QualifierLoc != nullptr)
825 QualifierLoc->print(OS, Policy);
826 OS << Node->getNameInfo();
827 }
828 OS << ":";
829 VisitOMPClauseList(Node, ' ');
830 OS << ")";
831 }
832 }
833
VisitOMPLinearClause(OMPLinearClause * Node)834 void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) {
835 if (!Node->varlist_empty()) {
836 OS << "linear";
837 if (Node->getModifierLoc().isValid()) {
838 OS << '('
839 << getOpenMPSimpleClauseTypeName(OMPC_linear, Node->getModifier());
840 }
841 VisitOMPClauseList(Node, '(');
842 if (Node->getModifierLoc().isValid())
843 OS << ')';
844 if (Node->getStep() != nullptr) {
845 OS << ": ";
846 Node->getStep()->printPretty(OS, nullptr, Policy, 0);
847 }
848 OS << ")";
849 }
850 }
851
VisitOMPAlignedClause(OMPAlignedClause * Node)852 void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) {
853 if (!Node->varlist_empty()) {
854 OS << "aligned";
855 VisitOMPClauseList(Node, '(');
856 if (Node->getAlignment() != nullptr) {
857 OS << ": ";
858 Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
859 }
860 OS << ")";
861 }
862 }
863
VisitOMPCopyinClause(OMPCopyinClause * Node)864 void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
865 if (!Node->varlist_empty()) {
866 OS << "copyin";
867 VisitOMPClauseList(Node, '(');
868 OS << ")";
869 }
870 }
871
VisitOMPCopyprivateClause(OMPCopyprivateClause * Node)872 void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) {
873 if (!Node->varlist_empty()) {
874 OS << "copyprivate";
875 VisitOMPClauseList(Node, '(');
876 OS << ")";
877 }
878 }
879
VisitOMPFlushClause(OMPFlushClause * Node)880 void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) {
881 if (!Node->varlist_empty()) {
882 VisitOMPClauseList(Node, '(');
883 OS << ")";
884 }
885 }
886
VisitOMPDependClause(OMPDependClause * Node)887 void OMPClausePrinter::VisitOMPDependClause(OMPDependClause *Node) {
888 OS << "depend(";
889 OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
890 Node->getDependencyKind());
891 if (!Node->varlist_empty()) {
892 OS << " :";
893 VisitOMPClauseList(Node, ' ');
894 }
895 OS << ")";
896 }
897
VisitOMPMapClause(OMPMapClause * Node)898 void OMPClausePrinter::VisitOMPMapClause(OMPMapClause *Node) {
899 if (!Node->varlist_empty()) {
900 OS << "map(";
901 if (Node->getMapType() != OMPC_MAP_unknown) {
902 if (Node->getMapTypeModifier() != OMPC_MAP_unknown) {
903 OS << getOpenMPSimpleClauseTypeName(OMPC_map,
904 Node->getMapTypeModifier());
905 OS << ',';
906 }
907 OS << getOpenMPSimpleClauseTypeName(OMPC_map, Node->getMapType());
908 OS << ':';
909 }
910 VisitOMPClauseList(Node, ' ');
911 OS << ")";
912 }
913 }
914
VisitOMPToClause(OMPToClause * Node)915 void OMPClausePrinter::VisitOMPToClause(OMPToClause *Node) {
916 if (!Node->varlist_empty()) {
917 OS << "to";
918 VisitOMPClauseList(Node, '(');
919 OS << ")";
920 }
921 }
922
VisitOMPFromClause(OMPFromClause * Node)923 void OMPClausePrinter::VisitOMPFromClause(OMPFromClause *Node) {
924 if (!Node->varlist_empty()) {
925 OS << "from";
926 VisitOMPClauseList(Node, '(');
927 OS << ")";
928 }
929 }
930
VisitOMPDistScheduleClause(OMPDistScheduleClause * Node)931 void OMPClausePrinter::VisitOMPDistScheduleClause(OMPDistScheduleClause *Node) {
932 OS << "dist_schedule(" << getOpenMPSimpleClauseTypeName(
933 OMPC_dist_schedule, Node->getDistScheduleKind());
934 if (auto *E = Node->getChunkSize()) {
935 OS << ", ";
936 E->printPretty(OS, nullptr, Policy);
937 }
938 OS << ")";
939 }
940
VisitOMPDefaultmapClause(OMPDefaultmapClause * Node)941 void OMPClausePrinter::VisitOMPDefaultmapClause(OMPDefaultmapClause *Node) {
942 OS << "defaultmap(";
943 OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
944 Node->getDefaultmapModifier());
945 OS << ": ";
946 OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
947 Node->getDefaultmapKind());
948 OS << ")";
949 }
950
VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause * Node)951 void OMPClausePrinter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *Node) {
952 if (!Node->varlist_empty()) {
953 OS << "use_device_ptr";
954 VisitOMPClauseList(Node, '(');
955 OS << ")";
956 }
957 }
958
VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause * Node)959 void OMPClausePrinter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *Node) {
960 if (!Node->varlist_empty()) {
961 OS << "is_device_ptr";
962 VisitOMPClauseList(Node, '(');
963 OS << ")";
964 }
965 }
966 }
967
968 //===----------------------------------------------------------------------===//
969 // OpenMP directives printing methods
970 //===----------------------------------------------------------------------===//
971
PrintOMPExecutableDirective(OMPExecutableDirective * S)972 void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) {
973 OMPClausePrinter Printer(OS, Policy);
974 ArrayRef<OMPClause *> Clauses = S->clauses();
975 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
976 I != E; ++I)
977 if (*I && !(*I)->isImplicit()) {
978 Printer.Visit(*I);
979 OS << ' ';
980 }
981 OS << "\n";
982 if (S->hasAssociatedStmt() && S->getAssociatedStmt()) {
983 assert(isa<CapturedStmt>(S->getAssociatedStmt()) &&
984 "Expected captured statement!");
985 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt();
986 PrintStmt(CS);
987 }
988 }
989
VisitOMPParallelDirective(OMPParallelDirective * Node)990 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
991 Indent() << "#pragma omp parallel ";
992 PrintOMPExecutableDirective(Node);
993 }
994
VisitOMPSimdDirective(OMPSimdDirective * Node)995 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
996 Indent() << "#pragma omp simd ";
997 PrintOMPExecutableDirective(Node);
998 }
999
VisitOMPForDirective(OMPForDirective * Node)1000 void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
1001 Indent() << "#pragma omp for ";
1002 PrintOMPExecutableDirective(Node);
1003 }
1004
VisitOMPForSimdDirective(OMPForSimdDirective * Node)1005 void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
1006 Indent() << "#pragma omp for simd ";
1007 PrintOMPExecutableDirective(Node);
1008 }
1009
VisitOMPSectionsDirective(OMPSectionsDirective * Node)1010 void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
1011 Indent() << "#pragma omp sections ";
1012 PrintOMPExecutableDirective(Node);
1013 }
1014
VisitOMPSectionDirective(OMPSectionDirective * Node)1015 void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
1016 Indent() << "#pragma omp section";
1017 PrintOMPExecutableDirective(Node);
1018 }
1019
VisitOMPSingleDirective(OMPSingleDirective * Node)1020 void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
1021 Indent() << "#pragma omp single ";
1022 PrintOMPExecutableDirective(Node);
1023 }
1024
VisitOMPMasterDirective(OMPMasterDirective * Node)1025 void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
1026 Indent() << "#pragma omp master";
1027 PrintOMPExecutableDirective(Node);
1028 }
1029
VisitOMPCriticalDirective(OMPCriticalDirective * Node)1030 void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
1031 Indent() << "#pragma omp critical";
1032 if (Node->getDirectiveName().getName()) {
1033 OS << " (";
1034 Node->getDirectiveName().printName(OS);
1035 OS << ")";
1036 }
1037 OS << " ";
1038 PrintOMPExecutableDirective(Node);
1039 }
1040
VisitOMPParallelForDirective(OMPParallelForDirective * Node)1041 void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
1042 Indent() << "#pragma omp parallel for ";
1043 PrintOMPExecutableDirective(Node);
1044 }
1045
VisitOMPParallelForSimdDirective(OMPParallelForSimdDirective * Node)1046 void StmtPrinter::VisitOMPParallelForSimdDirective(
1047 OMPParallelForSimdDirective *Node) {
1048 Indent() << "#pragma omp parallel for simd ";
1049 PrintOMPExecutableDirective(Node);
1050 }
1051
VisitOMPParallelSectionsDirective(OMPParallelSectionsDirective * Node)1052 void StmtPrinter::VisitOMPParallelSectionsDirective(
1053 OMPParallelSectionsDirective *Node) {
1054 Indent() << "#pragma omp parallel sections ";
1055 PrintOMPExecutableDirective(Node);
1056 }
1057
VisitOMPTaskDirective(OMPTaskDirective * Node)1058 void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
1059 Indent() << "#pragma omp task ";
1060 PrintOMPExecutableDirective(Node);
1061 }
1062
VisitOMPTaskyieldDirective(OMPTaskyieldDirective * Node)1063 void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
1064 Indent() << "#pragma omp taskyield";
1065 PrintOMPExecutableDirective(Node);
1066 }
1067
VisitOMPBarrierDirective(OMPBarrierDirective * Node)1068 void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
1069 Indent() << "#pragma omp barrier";
1070 PrintOMPExecutableDirective(Node);
1071 }
1072
VisitOMPTaskwaitDirective(OMPTaskwaitDirective * Node)1073 void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
1074 Indent() << "#pragma omp taskwait";
1075 PrintOMPExecutableDirective(Node);
1076 }
1077
VisitOMPTaskgroupDirective(OMPTaskgroupDirective * Node)1078 void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
1079 Indent() << "#pragma omp taskgroup";
1080 PrintOMPExecutableDirective(Node);
1081 }
1082
VisitOMPFlushDirective(OMPFlushDirective * Node)1083 void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
1084 Indent() << "#pragma omp flush ";
1085 PrintOMPExecutableDirective(Node);
1086 }
1087
VisitOMPOrderedDirective(OMPOrderedDirective * Node)1088 void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
1089 Indent() << "#pragma omp ordered ";
1090 PrintOMPExecutableDirective(Node);
1091 }
1092
VisitOMPAtomicDirective(OMPAtomicDirective * Node)1093 void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
1094 Indent() << "#pragma omp atomic ";
1095 PrintOMPExecutableDirective(Node);
1096 }
1097
VisitOMPTargetDirective(OMPTargetDirective * Node)1098 void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
1099 Indent() << "#pragma omp target ";
1100 PrintOMPExecutableDirective(Node);
1101 }
1102
VisitOMPTargetDataDirective(OMPTargetDataDirective * Node)1103 void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
1104 Indent() << "#pragma omp target data ";
1105 PrintOMPExecutableDirective(Node);
1106 }
1107
VisitOMPTargetEnterDataDirective(OMPTargetEnterDataDirective * Node)1108 void StmtPrinter::VisitOMPTargetEnterDataDirective(
1109 OMPTargetEnterDataDirective *Node) {
1110 Indent() << "#pragma omp target enter data ";
1111 PrintOMPExecutableDirective(Node);
1112 }
1113
VisitOMPTargetExitDataDirective(OMPTargetExitDataDirective * Node)1114 void StmtPrinter::VisitOMPTargetExitDataDirective(
1115 OMPTargetExitDataDirective *Node) {
1116 Indent() << "#pragma omp target exit data ";
1117 PrintOMPExecutableDirective(Node);
1118 }
1119
VisitOMPTargetParallelDirective(OMPTargetParallelDirective * Node)1120 void StmtPrinter::VisitOMPTargetParallelDirective(
1121 OMPTargetParallelDirective *Node) {
1122 Indent() << "#pragma omp target parallel ";
1123 PrintOMPExecutableDirective(Node);
1124 }
1125
VisitOMPTargetParallelForDirective(OMPTargetParallelForDirective * Node)1126 void StmtPrinter::VisitOMPTargetParallelForDirective(
1127 OMPTargetParallelForDirective *Node) {
1128 Indent() << "#pragma omp target parallel for ";
1129 PrintOMPExecutableDirective(Node);
1130 }
1131
VisitOMPTeamsDirective(OMPTeamsDirective * Node)1132 void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
1133 Indent() << "#pragma omp teams ";
1134 PrintOMPExecutableDirective(Node);
1135 }
1136
VisitOMPCancellationPointDirective(OMPCancellationPointDirective * Node)1137 void StmtPrinter::VisitOMPCancellationPointDirective(
1138 OMPCancellationPointDirective *Node) {
1139 Indent() << "#pragma omp cancellation point "
1140 << getOpenMPDirectiveName(Node->getCancelRegion());
1141 PrintOMPExecutableDirective(Node);
1142 }
1143
VisitOMPCancelDirective(OMPCancelDirective * Node)1144 void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
1145 Indent() << "#pragma omp cancel "
1146 << getOpenMPDirectiveName(Node->getCancelRegion()) << " ";
1147 PrintOMPExecutableDirective(Node);
1148 }
1149
VisitOMPTaskLoopDirective(OMPTaskLoopDirective * Node)1150 void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) {
1151 Indent() << "#pragma omp taskloop ";
1152 PrintOMPExecutableDirective(Node);
1153 }
1154
VisitOMPTaskLoopSimdDirective(OMPTaskLoopSimdDirective * Node)1155 void StmtPrinter::VisitOMPTaskLoopSimdDirective(
1156 OMPTaskLoopSimdDirective *Node) {
1157 Indent() << "#pragma omp taskloop simd ";
1158 PrintOMPExecutableDirective(Node);
1159 }
1160
VisitOMPDistributeDirective(OMPDistributeDirective * Node)1161 void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) {
1162 Indent() << "#pragma omp distribute ";
1163 PrintOMPExecutableDirective(Node);
1164 }
1165
VisitOMPTargetUpdateDirective(OMPTargetUpdateDirective * Node)1166 void StmtPrinter::VisitOMPTargetUpdateDirective(
1167 OMPTargetUpdateDirective *Node) {
1168 Indent() << "#pragma omp target update ";
1169 PrintOMPExecutableDirective(Node);
1170 }
1171
VisitOMPDistributeParallelForDirective(OMPDistributeParallelForDirective * Node)1172 void StmtPrinter::VisitOMPDistributeParallelForDirective(
1173 OMPDistributeParallelForDirective *Node) {
1174 Indent() << "#pragma omp distribute parallel for ";
1175 PrintOMPExecutableDirective(Node);
1176 }
1177
VisitOMPDistributeParallelForSimdDirective(OMPDistributeParallelForSimdDirective * Node)1178 void StmtPrinter::VisitOMPDistributeParallelForSimdDirective(
1179 OMPDistributeParallelForSimdDirective *Node) {
1180 Indent() << "#pragma omp distribute parallel for simd ";
1181 PrintOMPExecutableDirective(Node);
1182 }
1183
VisitOMPDistributeSimdDirective(OMPDistributeSimdDirective * Node)1184 void StmtPrinter::VisitOMPDistributeSimdDirective(
1185 OMPDistributeSimdDirective *Node) {
1186 Indent() << "#pragma omp distribute simd ";
1187 PrintOMPExecutableDirective(Node);
1188 }
1189
VisitOMPTargetParallelForSimdDirective(OMPTargetParallelForSimdDirective * Node)1190 void StmtPrinter::VisitOMPTargetParallelForSimdDirective(
1191 OMPTargetParallelForSimdDirective *Node) {
1192 Indent() << "#pragma omp target parallel for simd ";
1193 PrintOMPExecutableDirective(Node);
1194 }
1195
1196 //===----------------------------------------------------------------------===//
1197 // Expr printing methods.
1198 //===----------------------------------------------------------------------===//
1199
VisitDeclRefExpr(DeclRefExpr * Node)1200 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
1201 if (auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) {
1202 OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy);
1203 return;
1204 }
1205 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1206 Qualifier->print(OS, Policy);
1207 if (Node->hasTemplateKeyword())
1208 OS << "template ";
1209 OS << Node->getNameInfo();
1210 if (Node->hasExplicitTemplateArgs())
1211 TemplateSpecializationType::PrintTemplateArgumentList(
1212 OS, Node->template_arguments(), Policy);
1213 }
1214
VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr * Node)1215 void StmtPrinter::VisitDependentScopeDeclRefExpr(
1216 DependentScopeDeclRefExpr *Node) {
1217 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1218 Qualifier->print(OS, Policy);
1219 if (Node->hasTemplateKeyword())
1220 OS << "template ";
1221 OS << Node->getNameInfo();
1222 if (Node->hasExplicitTemplateArgs())
1223 TemplateSpecializationType::PrintTemplateArgumentList(
1224 OS, Node->template_arguments(), Policy);
1225 }
1226
VisitUnresolvedLookupExpr(UnresolvedLookupExpr * Node)1227 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
1228 if (Node->getQualifier())
1229 Node->getQualifier()->print(OS, Policy);
1230 if (Node->hasTemplateKeyword())
1231 OS << "template ";
1232 OS << Node->getNameInfo();
1233 if (Node->hasExplicitTemplateArgs())
1234 TemplateSpecializationType::PrintTemplateArgumentList(
1235 OS, Node->template_arguments(), Policy);
1236 }
1237
VisitObjCIvarRefExpr(ObjCIvarRefExpr * Node)1238 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
1239 if (Node->getBase()) {
1240 PrintExpr(Node->getBase());
1241 OS << (Node->isArrow() ? "->" : ".");
1242 }
1243 OS << *Node->getDecl();
1244 }
1245
VisitObjCPropertyRefExpr(ObjCPropertyRefExpr * Node)1246 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
1247 if (Node->isSuperReceiver())
1248 OS << "super.";
1249 else if (Node->isObjectReceiver() && Node->getBase()) {
1250 PrintExpr(Node->getBase());
1251 OS << ".";
1252 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1253 OS << Node->getClassReceiver()->getName() << ".";
1254 }
1255
1256 if (Node->isImplicitProperty())
1257 Node->getImplicitPropertyGetter()->getSelector().print(OS);
1258 else
1259 OS << Node->getExplicitProperty()->getName();
1260 }
1261
VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr * Node)1262 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1263
1264 PrintExpr(Node->getBaseExpr());
1265 OS << "[";
1266 PrintExpr(Node->getKeyExpr());
1267 OS << "]";
1268 }
1269
VisitPredefinedExpr(PredefinedExpr * Node)1270 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
1271 OS << PredefinedExpr::getIdentTypeName(Node->getIdentType());
1272 }
1273
VisitCharacterLiteral(CharacterLiteral * Node)1274 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
1275 unsigned value = Node->getValue();
1276
1277 switch (Node->getKind()) {
1278 case CharacterLiteral::Ascii: break; // no prefix.
1279 case CharacterLiteral::Wide: OS << 'L'; break;
1280 case CharacterLiteral::UTF8: OS << "u8"; break;
1281 case CharacterLiteral::UTF16: OS << 'u'; break;
1282 case CharacterLiteral::UTF32: OS << 'U'; break;
1283 }
1284
1285 switch (value) {
1286 case '\\':
1287 OS << "'\\\\'";
1288 break;
1289 case '\'':
1290 OS << "'\\''";
1291 break;
1292 case '\a':
1293 // TODO: K&R: the meaning of '\\a' is different in traditional C
1294 OS << "'\\a'";
1295 break;
1296 case '\b':
1297 OS << "'\\b'";
1298 break;
1299 // Nonstandard escape sequence.
1300 /*case '\e':
1301 OS << "'\\e'";
1302 break;*/
1303 case '\f':
1304 OS << "'\\f'";
1305 break;
1306 case '\n':
1307 OS << "'\\n'";
1308 break;
1309 case '\r':
1310 OS << "'\\r'";
1311 break;
1312 case '\t':
1313 OS << "'\\t'";
1314 break;
1315 case '\v':
1316 OS << "'\\v'";
1317 break;
1318 default:
1319 // A character literal might be sign-extended, which
1320 // would result in an invalid \U escape sequence.
1321 // FIXME: multicharacter literals such as '\xFF\xFF\xFF\xFF'
1322 // are not correctly handled.
1323 if ((value & ~0xFFu) == ~0xFFu && Node->getKind() == CharacterLiteral::Ascii)
1324 value &= 0xFFu;
1325 if (value < 256 && isPrintable((unsigned char)value))
1326 OS << "'" << (char)value << "'";
1327 else if (value < 256)
1328 OS << "'\\x" << llvm::format("%02x", value) << "'";
1329 else if (value <= 0xFFFF)
1330 OS << "'\\u" << llvm::format("%04x", value) << "'";
1331 else
1332 OS << "'\\U" << llvm::format("%08x", value) << "'";
1333 }
1334 }
1335
VisitIntegerLiteral(IntegerLiteral * Node)1336 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
1337 bool isSigned = Node->getType()->isSignedIntegerType();
1338 OS << Node->getValue().toString(10, isSigned);
1339
1340 // Emit suffixes. Integer literals are always a builtin integer type.
1341 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1342 default: llvm_unreachable("Unexpected type for integer literal!");
1343 case BuiltinType::Char_S:
1344 case BuiltinType::Char_U: OS << "i8"; break;
1345 case BuiltinType::UChar: OS << "Ui8"; break;
1346 case BuiltinType::Short: OS << "i16"; break;
1347 case BuiltinType::UShort: OS << "Ui16"; break;
1348 case BuiltinType::Int: break; // no suffix.
1349 case BuiltinType::UInt: OS << 'U'; break;
1350 case BuiltinType::Long: OS << 'L'; break;
1351 case BuiltinType::ULong: OS << "UL"; break;
1352 case BuiltinType::LongLong: OS << "LL"; break;
1353 case BuiltinType::ULongLong: OS << "ULL"; break;
1354 }
1355 }
1356
PrintFloatingLiteral(raw_ostream & OS,FloatingLiteral * Node,bool PrintSuffix)1357 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1358 bool PrintSuffix) {
1359 SmallString<16> Str;
1360 Node->getValue().toString(Str);
1361 OS << Str;
1362 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1363 OS << '.'; // Trailing dot in order to separate from ints.
1364
1365 if (!PrintSuffix)
1366 return;
1367
1368 // Emit suffixes. Float literals are always a builtin float type.
1369 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1370 default: llvm_unreachable("Unexpected type for float literal!");
1371 case BuiltinType::Half: break; // FIXME: suffix?
1372 case BuiltinType::Double: break; // no suffix.
1373 case BuiltinType::Float: OS << 'F'; break;
1374 case BuiltinType::LongDouble: OS << 'L'; break;
1375 case BuiltinType::Float128: OS << 'Q'; break;
1376 }
1377 }
1378
VisitFloatingLiteral(FloatingLiteral * Node)1379 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1380 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
1381 }
1382
VisitImaginaryLiteral(ImaginaryLiteral * Node)1383 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1384 PrintExpr(Node->getSubExpr());
1385 OS << "i";
1386 }
1387
VisitStringLiteral(StringLiteral * Str)1388 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
1389 Str->outputString(OS);
1390 }
VisitParenExpr(ParenExpr * Node)1391 void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1392 OS << "(";
1393 PrintExpr(Node->getSubExpr());
1394 OS << ")";
1395 }
VisitUnaryOperator(UnaryOperator * Node)1396 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
1397 if (!Node->isPostfix()) {
1398 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1399
1400 // Print a space if this is an "identifier operator" like __real, or if
1401 // it might be concatenated incorrectly like '+'.
1402 switch (Node->getOpcode()) {
1403 default: break;
1404 case UO_Real:
1405 case UO_Imag:
1406 case UO_Extension:
1407 OS << ' ';
1408 break;
1409 case UO_Plus:
1410 case UO_Minus:
1411 if (isa<UnaryOperator>(Node->getSubExpr()))
1412 OS << ' ';
1413 break;
1414 }
1415 }
1416 PrintExpr(Node->getSubExpr());
1417
1418 if (Node->isPostfix())
1419 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1420 }
1421
VisitOffsetOfExpr(OffsetOfExpr * Node)1422 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1423 OS << "__builtin_offsetof(";
1424 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1425 OS << ", ";
1426 bool PrintedSomething = false;
1427 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1428 OffsetOfNode ON = Node->getComponent(i);
1429 if (ON.getKind() == OffsetOfNode::Array) {
1430 // Array node
1431 OS << "[";
1432 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1433 OS << "]";
1434 PrintedSomething = true;
1435 continue;
1436 }
1437
1438 // Skip implicit base indirections.
1439 if (ON.getKind() == OffsetOfNode::Base)
1440 continue;
1441
1442 // Field or identifier node.
1443 IdentifierInfo *Id = ON.getFieldName();
1444 if (!Id)
1445 continue;
1446
1447 if (PrintedSomething)
1448 OS << ".";
1449 else
1450 PrintedSomething = true;
1451 OS << Id->getName();
1452 }
1453 OS << ")";
1454 }
1455
VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr * Node)1456 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
1457 switch(Node->getKind()) {
1458 case UETT_SizeOf:
1459 OS << "sizeof";
1460 break;
1461 case UETT_AlignOf:
1462 if (Policy.Alignof)
1463 OS << "alignof";
1464 else if (Policy.UnderscoreAlignof)
1465 OS << "_Alignof";
1466 else
1467 OS << "__alignof";
1468 break;
1469 case UETT_VecStep:
1470 OS << "vec_step";
1471 break;
1472 case UETT_OpenMPRequiredSimdAlign:
1473 OS << "__builtin_omp_required_simd_align";
1474 break;
1475 }
1476 if (Node->isArgumentType()) {
1477 OS << '(';
1478 Node->getArgumentType().print(OS, Policy);
1479 OS << ')';
1480 } else {
1481 OS << " ";
1482 PrintExpr(Node->getArgumentExpr());
1483 }
1484 }
1485
VisitGenericSelectionExpr(GenericSelectionExpr * Node)1486 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1487 OS << "_Generic(";
1488 PrintExpr(Node->getControllingExpr());
1489 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
1490 OS << ", ";
1491 QualType T = Node->getAssocType(i);
1492 if (T.isNull())
1493 OS << "default";
1494 else
1495 T.print(OS, Policy);
1496 OS << ": ";
1497 PrintExpr(Node->getAssocExpr(i));
1498 }
1499 OS << ")";
1500 }
1501
VisitArraySubscriptExpr(ArraySubscriptExpr * Node)1502 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
1503 PrintExpr(Node->getLHS());
1504 OS << "[";
1505 PrintExpr(Node->getRHS());
1506 OS << "]";
1507 }
1508
VisitOMPArraySectionExpr(OMPArraySectionExpr * Node)1509 void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) {
1510 PrintExpr(Node->getBase());
1511 OS << "[";
1512 if (Node->getLowerBound())
1513 PrintExpr(Node->getLowerBound());
1514 if (Node->getColonLoc().isValid()) {
1515 OS << ":";
1516 if (Node->getLength())
1517 PrintExpr(Node->getLength());
1518 }
1519 OS << "]";
1520 }
1521
PrintCallArgs(CallExpr * Call)1522 void StmtPrinter::PrintCallArgs(CallExpr *Call) {
1523 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
1524 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1525 // Don't print any defaulted arguments
1526 break;
1527 }
1528
1529 if (i) OS << ", ";
1530 PrintExpr(Call->getArg(i));
1531 }
1532 }
1533
VisitCallExpr(CallExpr * Call)1534 void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1535 PrintExpr(Call->getCallee());
1536 OS << "(";
1537 PrintCallArgs(Call);
1538 OS << ")";
1539 }
VisitMemberExpr(MemberExpr * Node)1540 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1541 // FIXME: Suppress printing implicit bases (like "this")
1542 PrintExpr(Node->getBase());
1543
1544 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1545 FieldDecl *ParentDecl = ParentMember
1546 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr;
1547
1548 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1549 OS << (Node->isArrow() ? "->" : ".");
1550
1551 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1552 if (FD->isAnonymousStructOrUnion())
1553 return;
1554
1555 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1556 Qualifier->print(OS, Policy);
1557 if (Node->hasTemplateKeyword())
1558 OS << "template ";
1559 OS << Node->getMemberNameInfo();
1560 if (Node->hasExplicitTemplateArgs())
1561 TemplateSpecializationType::PrintTemplateArgumentList(
1562 OS, Node->template_arguments(), Policy);
1563 }
VisitObjCIsaExpr(ObjCIsaExpr * Node)1564 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1565 PrintExpr(Node->getBase());
1566 OS << (Node->isArrow() ? "->isa" : ".isa");
1567 }
1568
VisitExtVectorElementExpr(ExtVectorElementExpr * Node)1569 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1570 PrintExpr(Node->getBase());
1571 OS << ".";
1572 OS << Node->getAccessor().getName();
1573 }
VisitCStyleCastExpr(CStyleCastExpr * Node)1574 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1575 OS << '(';
1576 Node->getTypeAsWritten().print(OS, Policy);
1577 OS << ')';
1578 PrintExpr(Node->getSubExpr());
1579 }
VisitCompoundLiteralExpr(CompoundLiteralExpr * Node)1580 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1581 OS << '(';
1582 Node->getType().print(OS, Policy);
1583 OS << ')';
1584 PrintExpr(Node->getInitializer());
1585 }
VisitImplicitCastExpr(ImplicitCastExpr * Node)1586 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1587 // No need to print anything, simply forward to the subexpression.
1588 PrintExpr(Node->getSubExpr());
1589 }
VisitBinaryOperator(BinaryOperator * Node)1590 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1591 PrintExpr(Node->getLHS());
1592 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1593 PrintExpr(Node->getRHS());
1594 }
VisitCompoundAssignOperator(CompoundAssignOperator * Node)1595 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1596 PrintExpr(Node->getLHS());
1597 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1598 PrintExpr(Node->getRHS());
1599 }
VisitConditionalOperator(ConditionalOperator * Node)1600 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1601 PrintExpr(Node->getCond());
1602 OS << " ? ";
1603 PrintExpr(Node->getLHS());
1604 OS << " : ";
1605 PrintExpr(Node->getRHS());
1606 }
1607
1608 // GNU extensions.
1609
1610 void
VisitBinaryConditionalOperator(BinaryConditionalOperator * Node)1611 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1612 PrintExpr(Node->getCommon());
1613 OS << " ?: ";
1614 PrintExpr(Node->getFalseExpr());
1615 }
VisitAddrLabelExpr(AddrLabelExpr * Node)1616 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1617 OS << "&&" << Node->getLabel()->getName();
1618 }
1619
VisitStmtExpr(StmtExpr * E)1620 void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1621 OS << "(";
1622 PrintRawCompoundStmt(E->getSubStmt());
1623 OS << ")";
1624 }
1625
VisitChooseExpr(ChooseExpr * Node)1626 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1627 OS << "__builtin_choose_expr(";
1628 PrintExpr(Node->getCond());
1629 OS << ", ";
1630 PrintExpr(Node->getLHS());
1631 OS << ", ";
1632 PrintExpr(Node->getRHS());
1633 OS << ")";
1634 }
1635
VisitGNUNullExpr(GNUNullExpr *)1636 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1637 OS << "__null";
1638 }
1639
VisitShuffleVectorExpr(ShuffleVectorExpr * Node)1640 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1641 OS << "__builtin_shufflevector(";
1642 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1643 if (i) OS << ", ";
1644 PrintExpr(Node->getExpr(i));
1645 }
1646 OS << ")";
1647 }
1648
VisitConvertVectorExpr(ConvertVectorExpr * Node)1649 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1650 OS << "__builtin_convertvector(";
1651 PrintExpr(Node->getSrcExpr());
1652 OS << ", ";
1653 Node->getType().print(OS, Policy);
1654 OS << ")";
1655 }
1656
VisitInitListExpr(InitListExpr * Node)1657 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1658 if (Node->getSyntacticForm()) {
1659 Visit(Node->getSyntacticForm());
1660 return;
1661 }
1662
1663 OS << "{";
1664 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1665 if (i) OS << ", ";
1666 if (Node->getInit(i))
1667 PrintExpr(Node->getInit(i));
1668 else
1669 OS << "{}";
1670 }
1671 OS << "}";
1672 }
1673
VisitParenListExpr(ParenListExpr * Node)1674 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1675 OS << "(";
1676 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1677 if (i) OS << ", ";
1678 PrintExpr(Node->getExpr(i));
1679 }
1680 OS << ")";
1681 }
1682
VisitDesignatedInitExpr(DesignatedInitExpr * Node)1683 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1684 bool NeedsEquals = true;
1685 for (const DesignatedInitExpr::Designator &D : Node->designators()) {
1686 if (D.isFieldDesignator()) {
1687 if (D.getDotLoc().isInvalid()) {
1688 if (IdentifierInfo *II = D.getFieldName()) {
1689 OS << II->getName() << ":";
1690 NeedsEquals = false;
1691 }
1692 } else {
1693 OS << "." << D.getFieldName()->getName();
1694 }
1695 } else {
1696 OS << "[";
1697 if (D.isArrayDesignator()) {
1698 PrintExpr(Node->getArrayIndex(D));
1699 } else {
1700 PrintExpr(Node->getArrayRangeStart(D));
1701 OS << " ... ";
1702 PrintExpr(Node->getArrayRangeEnd(D));
1703 }
1704 OS << "]";
1705 }
1706 }
1707
1708 if (NeedsEquals)
1709 OS << " = ";
1710 else
1711 OS << " ";
1712 PrintExpr(Node->getInit());
1713 }
1714
VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr * Node)1715 void StmtPrinter::VisitDesignatedInitUpdateExpr(
1716 DesignatedInitUpdateExpr *Node) {
1717 OS << "{";
1718 OS << "/*base*/";
1719 PrintExpr(Node->getBase());
1720 OS << ", ";
1721
1722 OS << "/*updater*/";
1723 PrintExpr(Node->getUpdater());
1724 OS << "}";
1725 }
1726
VisitNoInitExpr(NoInitExpr * Node)1727 void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1728 OS << "/*no init*/";
1729 }
1730
VisitImplicitValueInitExpr(ImplicitValueInitExpr * Node)1731 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1732 if (Node->getType()->getAsCXXRecordDecl()) {
1733 OS << "/*implicit*/";
1734 Node->getType().print(OS, Policy);
1735 OS << "()";
1736 } else {
1737 OS << "/*implicit*/(";
1738 Node->getType().print(OS, Policy);
1739 OS << ')';
1740 if (Node->getType()->isRecordType())
1741 OS << "{}";
1742 else
1743 OS << 0;
1744 }
1745 }
1746
VisitVAArgExpr(VAArgExpr * Node)1747 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1748 OS << "__builtin_va_arg(";
1749 PrintExpr(Node->getSubExpr());
1750 OS << ", ";
1751 Node->getType().print(OS, Policy);
1752 OS << ")";
1753 }
1754
VisitPseudoObjectExpr(PseudoObjectExpr * Node)1755 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1756 PrintExpr(Node->getSyntacticForm());
1757 }
1758
VisitAtomicExpr(AtomicExpr * Node)1759 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1760 const char *Name = nullptr;
1761 switch (Node->getOp()) {
1762 #define BUILTIN(ID, TYPE, ATTRS)
1763 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1764 case AtomicExpr::AO ## ID: \
1765 Name = #ID "("; \
1766 break;
1767 #include "clang/Basic/Builtins.def"
1768 }
1769 OS << Name;
1770
1771 // AtomicExpr stores its subexpressions in a permuted order.
1772 PrintExpr(Node->getPtr());
1773 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1774 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
1775 OS << ", ";
1776 PrintExpr(Node->getVal1());
1777 }
1778 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1779 Node->isCmpXChg()) {
1780 OS << ", ";
1781 PrintExpr(Node->getVal2());
1782 }
1783 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1784 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1785 OS << ", ";
1786 PrintExpr(Node->getWeak());
1787 }
1788 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1789 OS << ", ";
1790 PrintExpr(Node->getOrder());
1791 }
1792 if (Node->isCmpXChg()) {
1793 OS << ", ";
1794 PrintExpr(Node->getOrderFail());
1795 }
1796 OS << ")";
1797 }
1798
1799 // C++
VisitCXXOperatorCallExpr(CXXOperatorCallExpr * Node)1800 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1801 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1802 "",
1803 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1804 Spelling,
1805 #include "clang/Basic/OperatorKinds.def"
1806 };
1807
1808 OverloadedOperatorKind Kind = Node->getOperator();
1809 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1810 if (Node->getNumArgs() == 1) {
1811 OS << OpStrings[Kind] << ' ';
1812 PrintExpr(Node->getArg(0));
1813 } else {
1814 PrintExpr(Node->getArg(0));
1815 OS << ' ' << OpStrings[Kind];
1816 }
1817 } else if (Kind == OO_Arrow) {
1818 PrintExpr(Node->getArg(0));
1819 } else if (Kind == OO_Call) {
1820 PrintExpr(Node->getArg(0));
1821 OS << '(';
1822 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1823 if (ArgIdx > 1)
1824 OS << ", ";
1825 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1826 PrintExpr(Node->getArg(ArgIdx));
1827 }
1828 OS << ')';
1829 } else if (Kind == OO_Subscript) {
1830 PrintExpr(Node->getArg(0));
1831 OS << '[';
1832 PrintExpr(Node->getArg(1));
1833 OS << ']';
1834 } else if (Node->getNumArgs() == 1) {
1835 OS << OpStrings[Kind] << ' ';
1836 PrintExpr(Node->getArg(0));
1837 } else if (Node->getNumArgs() == 2) {
1838 PrintExpr(Node->getArg(0));
1839 OS << ' ' << OpStrings[Kind] << ' ';
1840 PrintExpr(Node->getArg(1));
1841 } else {
1842 llvm_unreachable("unknown overloaded operator");
1843 }
1844 }
1845
VisitCXXMemberCallExpr(CXXMemberCallExpr * Node)1846 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1847 // If we have a conversion operator call only print the argument.
1848 CXXMethodDecl *MD = Node->getMethodDecl();
1849 if (MD && isa<CXXConversionDecl>(MD)) {
1850 PrintExpr(Node->getImplicitObjectArgument());
1851 return;
1852 }
1853 VisitCallExpr(cast<CallExpr>(Node));
1854 }
1855
VisitCUDAKernelCallExpr(CUDAKernelCallExpr * Node)1856 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1857 PrintExpr(Node->getCallee());
1858 OS << "<<<";
1859 PrintCallArgs(Node->getConfig());
1860 OS << ">>>(";
1861 PrintCallArgs(Node);
1862 OS << ")";
1863 }
1864
VisitCXXNamedCastExpr(CXXNamedCastExpr * Node)1865 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1866 OS << Node->getCastName() << '<';
1867 Node->getTypeAsWritten().print(OS, Policy);
1868 OS << ">(";
1869 PrintExpr(Node->getSubExpr());
1870 OS << ")";
1871 }
1872
VisitCXXStaticCastExpr(CXXStaticCastExpr * Node)1873 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1874 VisitCXXNamedCastExpr(Node);
1875 }
1876
VisitCXXDynamicCastExpr(CXXDynamicCastExpr * Node)1877 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1878 VisitCXXNamedCastExpr(Node);
1879 }
1880
VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr * Node)1881 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1882 VisitCXXNamedCastExpr(Node);
1883 }
1884
VisitCXXConstCastExpr(CXXConstCastExpr * Node)1885 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1886 VisitCXXNamedCastExpr(Node);
1887 }
1888
VisitCXXTypeidExpr(CXXTypeidExpr * Node)1889 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1890 OS << "typeid(";
1891 if (Node->isTypeOperand()) {
1892 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1893 } else {
1894 PrintExpr(Node->getExprOperand());
1895 }
1896 OS << ")";
1897 }
1898
VisitCXXUuidofExpr(CXXUuidofExpr * Node)1899 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1900 OS << "__uuidof(";
1901 if (Node->isTypeOperand()) {
1902 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1903 } else {
1904 PrintExpr(Node->getExprOperand());
1905 }
1906 OS << ")";
1907 }
1908
VisitMSPropertyRefExpr(MSPropertyRefExpr * Node)1909 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1910 PrintExpr(Node->getBaseExpr());
1911 if (Node->isArrow())
1912 OS << "->";
1913 else
1914 OS << ".";
1915 if (NestedNameSpecifier *Qualifier =
1916 Node->getQualifierLoc().getNestedNameSpecifier())
1917 Qualifier->print(OS, Policy);
1918 OS << Node->getPropertyDecl()->getDeclName();
1919 }
1920
VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr * Node)1921 void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
1922 PrintExpr(Node->getBase());
1923 OS << "[";
1924 PrintExpr(Node->getIdx());
1925 OS << "]";
1926 }
1927
VisitUserDefinedLiteral(UserDefinedLiteral * Node)1928 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1929 switch (Node->getLiteralOperatorKind()) {
1930 case UserDefinedLiteral::LOK_Raw:
1931 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
1932 break;
1933 case UserDefinedLiteral::LOK_Template: {
1934 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1935 const TemplateArgumentList *Args =
1936 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1937 assert(Args);
1938
1939 if (Args->size() != 1) {
1940 OS << "operator\"\"" << Node->getUDSuffix()->getName();
1941 TemplateSpecializationType::PrintTemplateArgumentList(
1942 OS, Args->asArray(), Policy);
1943 OS << "()";
1944 return;
1945 }
1946
1947 const TemplateArgument &Pack = Args->get(0);
1948 for (const auto &P : Pack.pack_elements()) {
1949 char C = (char)P.getAsIntegral().getZExtValue();
1950 OS << C;
1951 }
1952 break;
1953 }
1954 case UserDefinedLiteral::LOK_Integer: {
1955 // Print integer literal without suffix.
1956 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1957 OS << Int->getValue().toString(10, /*isSigned*/false);
1958 break;
1959 }
1960 case UserDefinedLiteral::LOK_Floating: {
1961 // Print floating literal without suffix.
1962 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1963 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1964 break;
1965 }
1966 case UserDefinedLiteral::LOK_String:
1967 case UserDefinedLiteral::LOK_Character:
1968 PrintExpr(Node->getCookedLiteral());
1969 break;
1970 }
1971 OS << Node->getUDSuffix()->getName();
1972 }
1973
VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr * Node)1974 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1975 OS << (Node->getValue() ? "true" : "false");
1976 }
1977
VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr * Node)1978 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1979 OS << "nullptr";
1980 }
1981
VisitCXXThisExpr(CXXThisExpr * Node)1982 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1983 OS << "this";
1984 }
1985
VisitCXXThrowExpr(CXXThrowExpr * Node)1986 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1987 if (!Node->getSubExpr())
1988 OS << "throw";
1989 else {
1990 OS << "throw ";
1991 PrintExpr(Node->getSubExpr());
1992 }
1993 }
1994
VisitCXXDefaultArgExpr(CXXDefaultArgExpr * Node)1995 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1996 // Nothing to print: we picked up the default argument.
1997 }
1998
VisitCXXDefaultInitExpr(CXXDefaultInitExpr * Node)1999 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
2000 // Nothing to print: we picked up the default initializer.
2001 }
2002
VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr * Node)2003 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
2004 Node->getType().print(OS, Policy);
2005 // If there are no parens, this is list-initialization, and the braces are
2006 // part of the syntax of the inner construct.
2007 if (Node->getLParenLoc().isValid())
2008 OS << "(";
2009 PrintExpr(Node->getSubExpr());
2010 if (Node->getLParenLoc().isValid())
2011 OS << ")";
2012 }
2013
VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr * Node)2014 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
2015 PrintExpr(Node->getSubExpr());
2016 }
2017
VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr * Node)2018 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
2019 Node->getType().print(OS, Policy);
2020 if (Node->isStdInitListInitialization())
2021 /* Nothing to do; braces are part of creating the std::initializer_list. */;
2022 else if (Node->isListInitialization())
2023 OS << "{";
2024 else
2025 OS << "(";
2026 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
2027 ArgEnd = Node->arg_end();
2028 Arg != ArgEnd; ++Arg) {
2029 if ((*Arg)->isDefaultArgument())
2030 break;
2031 if (Arg != Node->arg_begin())
2032 OS << ", ";
2033 PrintExpr(*Arg);
2034 }
2035 if (Node->isStdInitListInitialization())
2036 /* See above. */;
2037 else if (Node->isListInitialization())
2038 OS << "}";
2039 else
2040 OS << ")";
2041 }
2042
VisitLambdaExpr(LambdaExpr * Node)2043 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
2044 OS << '[';
2045 bool NeedComma = false;
2046 switch (Node->getCaptureDefault()) {
2047 case LCD_None:
2048 break;
2049
2050 case LCD_ByCopy:
2051 OS << '=';
2052 NeedComma = true;
2053 break;
2054
2055 case LCD_ByRef:
2056 OS << '&';
2057 NeedComma = true;
2058 break;
2059 }
2060 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
2061 CEnd = Node->explicit_capture_end();
2062 C != CEnd;
2063 ++C) {
2064 if (NeedComma)
2065 OS << ", ";
2066 NeedComma = true;
2067
2068 switch (C->getCaptureKind()) {
2069 case LCK_This:
2070 OS << "this";
2071 break;
2072 case LCK_StarThis:
2073 OS << "*this";
2074 break;
2075 case LCK_ByRef:
2076 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
2077 OS << '&';
2078 OS << C->getCapturedVar()->getName();
2079 break;
2080
2081 case LCK_ByCopy:
2082 OS << C->getCapturedVar()->getName();
2083 break;
2084 case LCK_VLAType:
2085 llvm_unreachable("VLA type in explicit captures.");
2086 }
2087
2088 if (Node->isInitCapture(C))
2089 PrintExpr(C->getCapturedVar()->getInit());
2090 }
2091 OS << ']';
2092
2093 if (Node->hasExplicitParameters()) {
2094 OS << " (";
2095 CXXMethodDecl *Method = Node->getCallOperator();
2096 NeedComma = false;
2097 for (auto P : Method->parameters()) {
2098 if (NeedComma) {
2099 OS << ", ";
2100 } else {
2101 NeedComma = true;
2102 }
2103 std::string ParamStr = P->getNameAsString();
2104 P->getOriginalType().print(OS, Policy, ParamStr);
2105 }
2106 if (Method->isVariadic()) {
2107 if (NeedComma)
2108 OS << ", ";
2109 OS << "...";
2110 }
2111 OS << ')';
2112
2113 if (Node->isMutable())
2114 OS << " mutable";
2115
2116 const FunctionProtoType *Proto
2117 = Method->getType()->getAs<FunctionProtoType>();
2118 Proto->printExceptionSpecification(OS, Policy);
2119
2120 // FIXME: Attributes
2121
2122 // Print the trailing return type if it was specified in the source.
2123 if (Node->hasExplicitResultType()) {
2124 OS << " -> ";
2125 Proto->getReturnType().print(OS, Policy);
2126 }
2127 }
2128
2129 // Print the body.
2130 CompoundStmt *Body = Node->getBody();
2131 OS << ' ';
2132 PrintStmt(Body);
2133 }
2134
VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr * Node)2135 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
2136 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
2137 TSInfo->getType().print(OS, Policy);
2138 else
2139 Node->getType().print(OS, Policy);
2140 OS << "()";
2141 }
2142
VisitCXXNewExpr(CXXNewExpr * E)2143 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
2144 if (E->isGlobalNew())
2145 OS << "::";
2146 OS << "new ";
2147 unsigned NumPlace = E->getNumPlacementArgs();
2148 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
2149 OS << "(";
2150 PrintExpr(E->getPlacementArg(0));
2151 for (unsigned i = 1; i < NumPlace; ++i) {
2152 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
2153 break;
2154 OS << ", ";
2155 PrintExpr(E->getPlacementArg(i));
2156 }
2157 OS << ") ";
2158 }
2159 if (E->isParenTypeId())
2160 OS << "(";
2161 std::string TypeS;
2162 if (Expr *Size = E->getArraySize()) {
2163 llvm::raw_string_ostream s(TypeS);
2164 s << '[';
2165 Size->printPretty(s, Helper, Policy);
2166 s << ']';
2167 }
2168 E->getAllocatedType().print(OS, Policy, TypeS);
2169 if (E->isParenTypeId())
2170 OS << ")";
2171
2172 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
2173 if (InitStyle) {
2174 if (InitStyle == CXXNewExpr::CallInit)
2175 OS << "(";
2176 PrintExpr(E->getInitializer());
2177 if (InitStyle == CXXNewExpr::CallInit)
2178 OS << ")";
2179 }
2180 }
2181
VisitCXXDeleteExpr(CXXDeleteExpr * E)2182 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2183 if (E->isGlobalDelete())
2184 OS << "::";
2185 OS << "delete ";
2186 if (E->isArrayForm())
2187 OS << "[] ";
2188 PrintExpr(E->getArgument());
2189 }
2190
VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr * E)2191 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
2192 PrintExpr(E->getBase());
2193 if (E->isArrow())
2194 OS << "->";
2195 else
2196 OS << '.';
2197 if (E->getQualifier())
2198 E->getQualifier()->print(OS, Policy);
2199 OS << "~";
2200
2201 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
2202 OS << II->getName();
2203 else
2204 E->getDestroyedType().print(OS, Policy);
2205 }
2206
VisitCXXConstructExpr(CXXConstructExpr * E)2207 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
2208 if (E->isListInitialization() && !E->isStdInitListInitialization())
2209 OS << "{";
2210
2211 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
2212 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
2213 // Don't print any defaulted arguments
2214 break;
2215 }
2216
2217 if (i) OS << ", ";
2218 PrintExpr(E->getArg(i));
2219 }
2220
2221 if (E->isListInitialization() && !E->isStdInitListInitialization())
2222 OS << "}";
2223 }
2224
VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr * E)2225 void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) {
2226 // Parens are printed by the surrounding context.
2227 OS << "<forwarded>";
2228 }
2229
VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr * E)2230 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
2231 PrintExpr(E->getSubExpr());
2232 }
2233
VisitExprWithCleanups(ExprWithCleanups * E)2234 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
2235 // Just forward to the subexpression.
2236 PrintExpr(E->getSubExpr());
2237 }
2238
2239 void
VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr * Node)2240 StmtPrinter::VisitCXXUnresolvedConstructExpr(
2241 CXXUnresolvedConstructExpr *Node) {
2242 Node->getTypeAsWritten().print(OS, Policy);
2243 OS << "(";
2244 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
2245 ArgEnd = Node->arg_end();
2246 Arg != ArgEnd; ++Arg) {
2247 if (Arg != Node->arg_begin())
2248 OS << ", ";
2249 PrintExpr(*Arg);
2250 }
2251 OS << ")";
2252 }
2253
VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr * Node)2254 void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2255 CXXDependentScopeMemberExpr *Node) {
2256 if (!Node->isImplicitAccess()) {
2257 PrintExpr(Node->getBase());
2258 OS << (Node->isArrow() ? "->" : ".");
2259 }
2260 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2261 Qualifier->print(OS, Policy);
2262 if (Node->hasTemplateKeyword())
2263 OS << "template ";
2264 OS << Node->getMemberNameInfo();
2265 if (Node->hasExplicitTemplateArgs())
2266 TemplateSpecializationType::PrintTemplateArgumentList(
2267 OS, Node->template_arguments(), Policy);
2268 }
2269
VisitUnresolvedMemberExpr(UnresolvedMemberExpr * Node)2270 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
2271 if (!Node->isImplicitAccess()) {
2272 PrintExpr(Node->getBase());
2273 OS << (Node->isArrow() ? "->" : ".");
2274 }
2275 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2276 Qualifier->print(OS, Policy);
2277 if (Node->hasTemplateKeyword())
2278 OS << "template ";
2279 OS << Node->getMemberNameInfo();
2280 if (Node->hasExplicitTemplateArgs())
2281 TemplateSpecializationType::PrintTemplateArgumentList(
2282 OS, Node->template_arguments(), Policy);
2283 }
2284
getTypeTraitName(TypeTrait TT)2285 static const char *getTypeTraitName(TypeTrait TT) {
2286 switch (TT) {
2287 #define TYPE_TRAIT_1(Spelling, Name, Key) \
2288 case clang::UTT_##Name: return #Spelling;
2289 #define TYPE_TRAIT_2(Spelling, Name, Key) \
2290 case clang::BTT_##Name: return #Spelling;
2291 #define TYPE_TRAIT_N(Spelling, Name, Key) \
2292 case clang::TT_##Name: return #Spelling;
2293 #include "clang/Basic/TokenKinds.def"
2294 }
2295 llvm_unreachable("Type trait not covered by switch");
2296 }
2297
getTypeTraitName(ArrayTypeTrait ATT)2298 static const char *getTypeTraitName(ArrayTypeTrait ATT) {
2299 switch (ATT) {
2300 case ATT_ArrayRank: return "__array_rank";
2301 case ATT_ArrayExtent: return "__array_extent";
2302 }
2303 llvm_unreachable("Array type trait not covered by switch");
2304 }
2305
getExpressionTraitName(ExpressionTrait ET)2306 static const char *getExpressionTraitName(ExpressionTrait ET) {
2307 switch (ET) {
2308 case ET_IsLValueExpr: return "__is_lvalue_expr";
2309 case ET_IsRValueExpr: return "__is_rvalue_expr";
2310 }
2311 llvm_unreachable("Expression type trait not covered by switch");
2312 }
2313
VisitTypeTraitExpr(TypeTraitExpr * E)2314 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2315 OS << getTypeTraitName(E->getTrait()) << "(";
2316 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2317 if (I > 0)
2318 OS << ", ";
2319 E->getArg(I)->getType().print(OS, Policy);
2320 }
2321 OS << ")";
2322 }
2323
VisitArrayTypeTraitExpr(ArrayTypeTraitExpr * E)2324 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
2325 OS << getTypeTraitName(E->getTrait()) << '(';
2326 E->getQueriedType().print(OS, Policy);
2327 OS << ')';
2328 }
2329
VisitExpressionTraitExpr(ExpressionTraitExpr * E)2330 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
2331 OS << getExpressionTraitName(E->getTrait()) << '(';
2332 PrintExpr(E->getQueriedExpression());
2333 OS << ')';
2334 }
2335
VisitCXXNoexceptExpr(CXXNoexceptExpr * E)2336 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2337 OS << "noexcept(";
2338 PrintExpr(E->getOperand());
2339 OS << ")";
2340 }
2341
VisitPackExpansionExpr(PackExpansionExpr * E)2342 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
2343 PrintExpr(E->getPattern());
2344 OS << "...";
2345 }
2346
VisitSizeOfPackExpr(SizeOfPackExpr * E)2347 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2348 OS << "sizeof...(" << *E->getPack() << ")";
2349 }
2350
VisitSubstNonTypeTemplateParmPackExpr(SubstNonTypeTemplateParmPackExpr * Node)2351 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2352 SubstNonTypeTemplateParmPackExpr *Node) {
2353 OS << *Node->getParameterPack();
2354 }
2355
VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr * Node)2356 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2357 SubstNonTypeTemplateParmExpr *Node) {
2358 Visit(Node->getReplacement());
2359 }
2360
VisitFunctionParmPackExpr(FunctionParmPackExpr * E)2361 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2362 OS << *E->getParameterPack();
2363 }
2364
VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr * Node)2365 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2366 PrintExpr(Node->GetTemporaryExpr());
2367 }
2368
VisitCXXFoldExpr(CXXFoldExpr * E)2369 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2370 OS << "(";
2371 if (E->getLHS()) {
2372 PrintExpr(E->getLHS());
2373 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2374 }
2375 OS << "...";
2376 if (E->getRHS()) {
2377 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2378 PrintExpr(E->getRHS());
2379 }
2380 OS << ")";
2381 }
2382
2383 // C++ Coroutines TS
2384
VisitCoroutineBodyStmt(CoroutineBodyStmt * S)2385 void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
2386 Visit(S->getBody());
2387 }
2388
VisitCoreturnStmt(CoreturnStmt * S)2389 void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
2390 OS << "co_return";
2391 if (S->getOperand()) {
2392 OS << " ";
2393 Visit(S->getOperand());
2394 }
2395 OS << ";";
2396 }
2397
VisitCoawaitExpr(CoawaitExpr * S)2398 void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
2399 OS << "co_await ";
2400 PrintExpr(S->getOperand());
2401 }
2402
VisitCoyieldExpr(CoyieldExpr * S)2403 void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
2404 OS << "co_yield ";
2405 PrintExpr(S->getOperand());
2406 }
2407
2408 // Obj-C
2409
VisitObjCStringLiteral(ObjCStringLiteral * Node)2410 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2411 OS << "@";
2412 VisitStringLiteral(Node->getString());
2413 }
2414
VisitObjCBoxedExpr(ObjCBoxedExpr * E)2415 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
2416 OS << "@";
2417 Visit(E->getSubExpr());
2418 }
2419
VisitObjCArrayLiteral(ObjCArrayLiteral * E)2420 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2421 OS << "@[ ";
2422 ObjCArrayLiteral::child_range Ch = E->children();
2423 for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2424 if (I != Ch.begin())
2425 OS << ", ";
2426 Visit(*I);
2427 }
2428 OS << " ]";
2429 }
2430
VisitObjCDictionaryLiteral(ObjCDictionaryLiteral * E)2431 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2432 OS << "@{ ";
2433 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2434 if (I > 0)
2435 OS << ", ";
2436
2437 ObjCDictionaryElement Element = E->getKeyValueElement(I);
2438 Visit(Element.Key);
2439 OS << " : ";
2440 Visit(Element.Value);
2441 if (Element.isPackExpansion())
2442 OS << "...";
2443 }
2444 OS << " }";
2445 }
2446
VisitObjCEncodeExpr(ObjCEncodeExpr * Node)2447 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
2448 OS << "@encode(";
2449 Node->getEncodedType().print(OS, Policy);
2450 OS << ')';
2451 }
2452
VisitObjCSelectorExpr(ObjCSelectorExpr * Node)2453 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
2454 OS << "@selector(";
2455 Node->getSelector().print(OS);
2456 OS << ')';
2457 }
2458
VisitObjCProtocolExpr(ObjCProtocolExpr * Node)2459 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
2460 OS << "@protocol(" << *Node->getProtocol() << ')';
2461 }
2462
VisitObjCMessageExpr(ObjCMessageExpr * Mess)2463 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2464 OS << "[";
2465 switch (Mess->getReceiverKind()) {
2466 case ObjCMessageExpr::Instance:
2467 PrintExpr(Mess->getInstanceReceiver());
2468 break;
2469
2470 case ObjCMessageExpr::Class:
2471 Mess->getClassReceiver().print(OS, Policy);
2472 break;
2473
2474 case ObjCMessageExpr::SuperInstance:
2475 case ObjCMessageExpr::SuperClass:
2476 OS << "Super";
2477 break;
2478 }
2479
2480 OS << ' ';
2481 Selector selector = Mess->getSelector();
2482 if (selector.isUnarySelector()) {
2483 OS << selector.getNameForSlot(0);
2484 } else {
2485 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
2486 if (i < selector.getNumArgs()) {
2487 if (i > 0) OS << ' ';
2488 if (selector.getIdentifierInfoForSlot(i))
2489 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
2490 else
2491 OS << ":";
2492 }
2493 else OS << ", "; // Handle variadic methods.
2494
2495 PrintExpr(Mess->getArg(i));
2496 }
2497 }
2498 OS << "]";
2499 }
2500
VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr * Node)2501 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2502 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2503 }
2504
2505 void
VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr * E)2506 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2507 PrintExpr(E->getSubExpr());
2508 }
2509
2510 void
VisitObjCBridgedCastExpr(ObjCBridgedCastExpr * E)2511 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
2512 OS << '(' << E->getBridgeKindName();
2513 E->getType().print(OS, Policy);
2514 OS << ')';
2515 PrintExpr(E->getSubExpr());
2516 }
2517
VisitBlockExpr(BlockExpr * Node)2518 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
2519 BlockDecl *BD = Node->getBlockDecl();
2520 OS << "^";
2521
2522 const FunctionType *AFT = Node->getFunctionType();
2523
2524 if (isa<FunctionNoProtoType>(AFT)) {
2525 OS << "()";
2526 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
2527 OS << '(';
2528 for (BlockDecl::param_iterator AI = BD->param_begin(),
2529 E = BD->param_end(); AI != E; ++AI) {
2530 if (AI != BD->param_begin()) OS << ", ";
2531 std::string ParamStr = (*AI)->getNameAsString();
2532 (*AI)->getType().print(OS, Policy, ParamStr);
2533 }
2534
2535 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
2536 if (FT->isVariadic()) {
2537 if (!BD->param_empty()) OS << ", ";
2538 OS << "...";
2539 }
2540 OS << ')';
2541 }
2542 OS << "{ }";
2543 }
2544
VisitOpaqueValueExpr(OpaqueValueExpr * Node)2545 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2546 PrintExpr(Node->getSourceExpr());
2547 }
2548
VisitTypoExpr(TypoExpr * Node)2549 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2550 // TODO: Print something reasonable for a TypoExpr, if necessary.
2551 llvm_unreachable("Cannot print TypoExpr nodes");
2552 }
2553
VisitAsTypeExpr(AsTypeExpr * Node)2554 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2555 OS << "__builtin_astype(";
2556 PrintExpr(Node->getSrcExpr());
2557 OS << ", ";
2558 Node->getType().print(OS, Policy);
2559 OS << ")";
2560 }
2561
2562 //===----------------------------------------------------------------------===//
2563 // Stmt method implementations
2564 //===----------------------------------------------------------------------===//
2565
dumpPretty(const ASTContext & Context) const2566 void Stmt::dumpPretty(const ASTContext &Context) const {
2567 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
2568 }
2569
printPretty(raw_ostream & OS,PrinterHelper * Helper,const PrintingPolicy & Policy,unsigned Indentation) const2570 void Stmt::printPretty(raw_ostream &OS,
2571 PrinterHelper *Helper,
2572 const PrintingPolicy &Policy,
2573 unsigned Indentation) const {
2574 StmtPrinter P(OS, Helper, Policy, Indentation);
2575 P.Visit(const_cast<Stmt*>(this));
2576 }
2577
2578 //===----------------------------------------------------------------------===//
2579 // PrinterHelper
2580 //===----------------------------------------------------------------------===//
2581
2582 // Implement virtual destructor.
~PrinterHelper()2583 PrinterHelper::~PrinterHelper() {}
2584