1 //===- StmtPrinter.cpp - Printing implementation for Stmt ASTs ------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which
10 // pretty print the AST back out to C code.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/Attr.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclBase.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/DeclObjC.h"
20 #include "clang/AST/DeclOpenMP.h"
21 #include "clang/AST/DeclTemplate.h"
22 #include "clang/AST/Expr.h"
23 #include "clang/AST/ExprCXX.h"
24 #include "clang/AST/ExprObjC.h"
25 #include "clang/AST/ExprOpenMP.h"
26 #include "clang/AST/NestedNameSpecifier.h"
27 #include "clang/AST/OpenMPClause.h"
28 #include "clang/AST/PrettyPrinter.h"
29 #include "clang/AST/Stmt.h"
30 #include "clang/AST/StmtCXX.h"
31 #include "clang/AST/StmtObjC.h"
32 #include "clang/AST/StmtOpenMP.h"
33 #include "clang/AST/StmtVisitor.h"
34 #include "clang/AST/TemplateBase.h"
35 #include "clang/AST/Type.h"
36 #include "clang/Basic/CharInfo.h"
37 #include "clang/Basic/ExpressionTraits.h"
38 #include "clang/Basic/IdentifierTable.h"
39 #include "clang/Basic/JsonSupport.h"
40 #include "clang/Basic/LLVM.h"
41 #include "clang/Basic/Lambda.h"
42 #include "clang/Basic/OpenMPKinds.h"
43 #include "clang/Basic/OperatorKinds.h"
44 #include "clang/Basic/SourceLocation.h"
45 #include "clang/Basic/TypeTraits.h"
46 #include "clang/Lex/Lexer.h"
47 #include "llvm/ADT/ArrayRef.h"
48 #include "llvm/ADT/SmallString.h"
49 #include "llvm/ADT/SmallVector.h"
50 #include "llvm/ADT/StringExtras.h"
51 #include "llvm/ADT/StringRef.h"
52 #include "llvm/Support/Casting.h"
53 #include "llvm/Support/Compiler.h"
54 #include "llvm/Support/ErrorHandling.h"
55 #include "llvm/Support/raw_ostream.h"
56 #include <cassert>
57 #include <optional>
58 #include <string>
59
60 using namespace clang;
61
62 //===----------------------------------------------------------------------===//
63 // StmtPrinter Visitor
64 //===----------------------------------------------------------------------===//
65
66 namespace {
67
68 class StmtPrinter : public StmtVisitor<StmtPrinter> {
69 raw_ostream &OS;
70 unsigned IndentLevel;
71 PrinterHelper* Helper;
72 PrintingPolicy Policy;
73 std::string NL;
74 const ASTContext *Context;
75
76 public:
StmtPrinter(raw_ostream & os,PrinterHelper * helper,const PrintingPolicy & Policy,unsigned Indentation=0,StringRef NL="\\n",const ASTContext * Context=nullptr)77 StmtPrinter(raw_ostream &os, PrinterHelper *helper,
78 const PrintingPolicy &Policy, unsigned Indentation = 0,
79 StringRef NL = "\n", const ASTContext *Context = nullptr)
80 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy),
81 NL(NL), Context(Context) {}
82
PrintStmt(Stmt * S)83 void PrintStmt(Stmt *S) { PrintStmt(S, Policy.Indentation); }
84
PrintStmt(Stmt * S,int SubIndent)85 void PrintStmt(Stmt *S, int SubIndent) {
86 IndentLevel += SubIndent;
87 if (S && isa<Expr>(S)) {
88 // If this is an expr used in a stmt context, indent and newline it.
89 Indent();
90 Visit(S);
91 OS << ";" << NL;
92 } else if (S) {
93 Visit(S);
94 } else {
95 Indent() << "<<<NULL STATEMENT>>>" << NL;
96 }
97 IndentLevel -= SubIndent;
98 }
99
PrintInitStmt(Stmt * S,unsigned PrefixWidth)100 void PrintInitStmt(Stmt *S, unsigned PrefixWidth) {
101 // FIXME: Cope better with odd prefix widths.
102 IndentLevel += (PrefixWidth + 1) / 2;
103 if (auto *DS = dyn_cast<DeclStmt>(S))
104 PrintRawDeclStmt(DS);
105 else
106 PrintExpr(cast<Expr>(S));
107 OS << "; ";
108 IndentLevel -= (PrefixWidth + 1) / 2;
109 }
110
PrintControlledStmt(Stmt * S)111 void PrintControlledStmt(Stmt *S) {
112 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
113 OS << " ";
114 PrintRawCompoundStmt(CS);
115 OS << NL;
116 } else {
117 OS << NL;
118 PrintStmt(S);
119 }
120 }
121
122 void PrintRawCompoundStmt(CompoundStmt *S);
123 void PrintRawDecl(Decl *D);
124 void PrintRawDeclStmt(const DeclStmt *S);
125 void PrintRawIfStmt(IfStmt *If);
126 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
127 void PrintCallArgs(CallExpr *E);
128 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
129 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
130 void PrintOMPExecutableDirective(OMPExecutableDirective *S,
131 bool ForceNoStmt = false);
132 void PrintFPPragmas(CompoundStmt *S);
133
PrintExpr(Expr * E)134 void PrintExpr(Expr *E) {
135 if (E)
136 Visit(E);
137 else
138 OS << "<null expr>";
139 }
140
Indent(int Delta=0)141 raw_ostream &Indent(int Delta = 0) {
142 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
143 OS << " ";
144 return OS;
145 }
146
Visit(Stmt * S)147 void Visit(Stmt* S) {
148 if (Helper && Helper->handledStmt(S,OS))
149 return;
150 else StmtVisitor<StmtPrinter>::Visit(S);
151 }
152
VisitStmt(Stmt * Node)153 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
154 Indent() << "<<unknown stmt type>>" << NL;
155 }
156
VisitExpr(Expr * Node)157 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
158 OS << "<<unknown expr type>>";
159 }
160
161 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
162
163 #define ABSTRACT_STMT(CLASS)
164 #define STMT(CLASS, PARENT) \
165 void Visit##CLASS(CLASS *Node);
166 #include "clang/AST/StmtNodes.inc"
167 };
168
169 } // namespace
170
171 //===----------------------------------------------------------------------===//
172 // Stmt printing methods.
173 //===----------------------------------------------------------------------===//
174
175 /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
176 /// with no newline after the }.
PrintRawCompoundStmt(CompoundStmt * Node)177 void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
178 assert(Node && "Compound statement cannot be null");
179 OS << "{" << NL;
180 PrintFPPragmas(Node);
181 for (auto *I : Node->body())
182 PrintStmt(I);
183
184 Indent() << "}";
185 }
186
PrintFPPragmas(CompoundStmt * S)187 void StmtPrinter::PrintFPPragmas(CompoundStmt *S) {
188 if (!S->hasStoredFPFeatures())
189 return;
190 FPOptionsOverride FPO = S->getStoredFPFeatures();
191 bool FEnvAccess = false;
192 if (FPO.hasAllowFEnvAccessOverride()) {
193 FEnvAccess = FPO.getAllowFEnvAccessOverride();
194 Indent() << "#pragma STDC FENV_ACCESS " << (FEnvAccess ? "ON" : "OFF")
195 << NL;
196 }
197 if (FPO.hasSpecifiedExceptionModeOverride()) {
198 LangOptions::FPExceptionModeKind EM =
199 FPO.getSpecifiedExceptionModeOverride();
200 if (!FEnvAccess || EM != LangOptions::FPE_Strict) {
201 Indent() << "#pragma clang fp exceptions(";
202 switch (FPO.getSpecifiedExceptionModeOverride()) {
203 default:
204 break;
205 case LangOptions::FPE_Ignore:
206 OS << "ignore";
207 break;
208 case LangOptions::FPE_MayTrap:
209 OS << "maytrap";
210 break;
211 case LangOptions::FPE_Strict:
212 OS << "strict";
213 break;
214 }
215 OS << ")\n";
216 }
217 }
218 if (FPO.hasConstRoundingModeOverride()) {
219 LangOptions::RoundingMode RM = FPO.getConstRoundingModeOverride();
220 Indent() << "#pragma STDC FENV_ROUND ";
221 switch (RM) {
222 case llvm::RoundingMode::TowardZero:
223 OS << "FE_TOWARDZERO";
224 break;
225 case llvm::RoundingMode::NearestTiesToEven:
226 OS << "FE_TONEAREST";
227 break;
228 case llvm::RoundingMode::TowardPositive:
229 OS << "FE_UPWARD";
230 break;
231 case llvm::RoundingMode::TowardNegative:
232 OS << "FE_DOWNWARD";
233 break;
234 case llvm::RoundingMode::NearestTiesToAway:
235 OS << "FE_TONEARESTFROMZERO";
236 break;
237 case llvm::RoundingMode::Dynamic:
238 OS << "FE_DYNAMIC";
239 break;
240 default:
241 llvm_unreachable("Invalid rounding mode");
242 }
243 OS << NL;
244 }
245 }
246
PrintRawDecl(Decl * D)247 void StmtPrinter::PrintRawDecl(Decl *D) {
248 D->print(OS, Policy, IndentLevel);
249 }
250
PrintRawDeclStmt(const DeclStmt * S)251 void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
252 SmallVector<Decl *, 2> Decls(S->decls());
253 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
254 }
255
VisitNullStmt(NullStmt * Node)256 void StmtPrinter::VisitNullStmt(NullStmt *Node) {
257 Indent() << ";" << NL;
258 }
259
VisitDeclStmt(DeclStmt * Node)260 void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
261 Indent();
262 PrintRawDeclStmt(Node);
263 OS << ";" << NL;
264 }
265
VisitCompoundStmt(CompoundStmt * Node)266 void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
267 Indent();
268 PrintRawCompoundStmt(Node);
269 OS << "" << NL;
270 }
271
VisitCaseStmt(CaseStmt * Node)272 void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
273 Indent(-1) << "case ";
274 PrintExpr(Node->getLHS());
275 if (Node->getRHS()) {
276 OS << " ... ";
277 PrintExpr(Node->getRHS());
278 }
279 OS << ":" << NL;
280
281 PrintStmt(Node->getSubStmt(), 0);
282 }
283
VisitDefaultStmt(DefaultStmt * Node)284 void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
285 Indent(-1) << "default:" << NL;
286 PrintStmt(Node->getSubStmt(), 0);
287 }
288
VisitLabelStmt(LabelStmt * Node)289 void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
290 Indent(-1) << Node->getName() << ":" << NL;
291 PrintStmt(Node->getSubStmt(), 0);
292 }
293
VisitAttributedStmt(AttributedStmt * Node)294 void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
295 for (const auto *Attr : Node->getAttrs()) {
296 Attr->printPretty(OS, Policy);
297 }
298
299 PrintStmt(Node->getSubStmt(), 0);
300 }
301
PrintRawIfStmt(IfStmt * If)302 void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
303 if (If->isConsteval()) {
304 OS << "if ";
305 if (If->isNegatedConsteval())
306 OS << "!";
307 OS << "consteval";
308 OS << NL;
309 PrintStmt(If->getThen());
310 if (Stmt *Else = If->getElse()) {
311 Indent();
312 OS << "else";
313 PrintStmt(Else);
314 OS << NL;
315 }
316 return;
317 }
318
319 OS << "if (";
320 if (If->getInit())
321 PrintInitStmt(If->getInit(), 4);
322 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
323 PrintRawDeclStmt(DS);
324 else
325 PrintExpr(If->getCond());
326 OS << ')';
327
328 if (auto *CS = dyn_cast<CompoundStmt>(If->getThen())) {
329 OS << ' ';
330 PrintRawCompoundStmt(CS);
331 OS << (If->getElse() ? " " : NL);
332 } else {
333 OS << NL;
334 PrintStmt(If->getThen());
335 if (If->getElse()) Indent();
336 }
337
338 if (Stmt *Else = If->getElse()) {
339 OS << "else";
340
341 if (auto *CS = dyn_cast<CompoundStmt>(Else)) {
342 OS << ' ';
343 PrintRawCompoundStmt(CS);
344 OS << NL;
345 } else if (auto *ElseIf = dyn_cast<IfStmt>(Else)) {
346 OS << ' ';
347 PrintRawIfStmt(ElseIf);
348 } else {
349 OS << NL;
350 PrintStmt(If->getElse());
351 }
352 }
353 }
354
VisitIfStmt(IfStmt * If)355 void StmtPrinter::VisitIfStmt(IfStmt *If) {
356 Indent();
357 PrintRawIfStmt(If);
358 }
359
VisitSwitchStmt(SwitchStmt * Node)360 void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
361 Indent() << "switch (";
362 if (Node->getInit())
363 PrintInitStmt(Node->getInit(), 8);
364 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
365 PrintRawDeclStmt(DS);
366 else
367 PrintExpr(Node->getCond());
368 OS << ")";
369 PrintControlledStmt(Node->getBody());
370 }
371
VisitWhileStmt(WhileStmt * Node)372 void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
373 Indent() << "while (";
374 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
375 PrintRawDeclStmt(DS);
376 else
377 PrintExpr(Node->getCond());
378 OS << ")" << NL;
379 PrintStmt(Node->getBody());
380 }
381
VisitDoStmt(DoStmt * Node)382 void StmtPrinter::VisitDoStmt(DoStmt *Node) {
383 Indent() << "do ";
384 if (auto *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
385 PrintRawCompoundStmt(CS);
386 OS << " ";
387 } else {
388 OS << NL;
389 PrintStmt(Node->getBody());
390 Indent();
391 }
392
393 OS << "while (";
394 PrintExpr(Node->getCond());
395 OS << ");" << NL;
396 }
397
VisitForStmt(ForStmt * Node)398 void StmtPrinter::VisitForStmt(ForStmt *Node) {
399 Indent() << "for (";
400 if (Node->getInit())
401 PrintInitStmt(Node->getInit(), 5);
402 else
403 OS << (Node->getCond() ? "; " : ";");
404 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
405 PrintRawDeclStmt(DS);
406 else if (Node->getCond())
407 PrintExpr(Node->getCond());
408 OS << ";";
409 if (Node->getInc()) {
410 OS << " ";
411 PrintExpr(Node->getInc());
412 }
413 OS << ")";
414 PrintControlledStmt(Node->getBody());
415 }
416
VisitObjCForCollectionStmt(ObjCForCollectionStmt * Node)417 void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
418 Indent() << "for (";
419 if (auto *DS = dyn_cast<DeclStmt>(Node->getElement()))
420 PrintRawDeclStmt(DS);
421 else
422 PrintExpr(cast<Expr>(Node->getElement()));
423 OS << " in ";
424 PrintExpr(Node->getCollection());
425 OS << ")";
426 PrintControlledStmt(Node->getBody());
427 }
428
VisitCXXForRangeStmt(CXXForRangeStmt * Node)429 void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
430 Indent() << "for (";
431 if (Node->getInit())
432 PrintInitStmt(Node->getInit(), 5);
433 PrintingPolicy SubPolicy(Policy);
434 SubPolicy.SuppressInitializers = true;
435 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
436 OS << " : ";
437 PrintExpr(Node->getRangeInit());
438 OS << ")";
439 PrintControlledStmt(Node->getBody());
440 }
441
VisitMSDependentExistsStmt(MSDependentExistsStmt * Node)442 void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
443 Indent();
444 if (Node->isIfExists())
445 OS << "__if_exists (";
446 else
447 OS << "__if_not_exists (";
448
449 if (NestedNameSpecifier *Qualifier
450 = Node->getQualifierLoc().getNestedNameSpecifier())
451 Qualifier->print(OS, Policy);
452
453 OS << Node->getNameInfo() << ") ";
454
455 PrintRawCompoundStmt(Node->getSubStmt());
456 }
457
VisitGotoStmt(GotoStmt * Node)458 void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
459 Indent() << "goto " << Node->getLabel()->getName() << ";";
460 if (Policy.IncludeNewlines) OS << NL;
461 }
462
VisitIndirectGotoStmt(IndirectGotoStmt * Node)463 void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
464 Indent() << "goto *";
465 PrintExpr(Node->getTarget());
466 OS << ";";
467 if (Policy.IncludeNewlines) OS << NL;
468 }
469
VisitContinueStmt(ContinueStmt * Node)470 void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
471 Indent() << "continue;";
472 if (Policy.IncludeNewlines) OS << NL;
473 }
474
VisitBreakStmt(BreakStmt * Node)475 void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
476 Indent() << "break;";
477 if (Policy.IncludeNewlines) OS << NL;
478 }
479
VisitReturnStmt(ReturnStmt * Node)480 void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
481 Indent() << "return";
482 if (Node->getRetValue()) {
483 OS << " ";
484 PrintExpr(Node->getRetValue());
485 }
486 OS << ";";
487 if (Policy.IncludeNewlines) OS << NL;
488 }
489
VisitGCCAsmStmt(GCCAsmStmt * Node)490 void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
491 Indent() << "asm ";
492
493 if (Node->isVolatile())
494 OS << "volatile ";
495
496 if (Node->isAsmGoto())
497 OS << "goto ";
498
499 OS << "(";
500 VisitStringLiteral(Node->getAsmString());
501
502 // Outputs
503 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
504 Node->getNumClobbers() != 0 || Node->getNumLabels() != 0)
505 OS << " : ";
506
507 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
508 if (i != 0)
509 OS << ", ";
510
511 if (!Node->getOutputName(i).empty()) {
512 OS << '[';
513 OS << Node->getOutputName(i);
514 OS << "] ";
515 }
516
517 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
518 OS << " (";
519 Visit(Node->getOutputExpr(i));
520 OS << ")";
521 }
522
523 // Inputs
524 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0 ||
525 Node->getNumLabels() != 0)
526 OS << " : ";
527
528 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
529 if (i != 0)
530 OS << ", ";
531
532 if (!Node->getInputName(i).empty()) {
533 OS << '[';
534 OS << Node->getInputName(i);
535 OS << "] ";
536 }
537
538 VisitStringLiteral(Node->getInputConstraintLiteral(i));
539 OS << " (";
540 Visit(Node->getInputExpr(i));
541 OS << ")";
542 }
543
544 // Clobbers
545 if (Node->getNumClobbers() != 0 || Node->getNumLabels())
546 OS << " : ";
547
548 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
549 if (i != 0)
550 OS << ", ";
551
552 VisitStringLiteral(Node->getClobberStringLiteral(i));
553 }
554
555 // Labels
556 if (Node->getNumLabels() != 0)
557 OS << " : ";
558
559 for (unsigned i = 0, e = Node->getNumLabels(); i != e; ++i) {
560 if (i != 0)
561 OS << ", ";
562 OS << Node->getLabelName(i);
563 }
564
565 OS << ");";
566 if (Policy.IncludeNewlines) OS << NL;
567 }
568
VisitMSAsmStmt(MSAsmStmt * Node)569 void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
570 // FIXME: Implement MS style inline asm statement printer.
571 Indent() << "__asm ";
572 if (Node->hasBraces())
573 OS << "{" << NL;
574 OS << Node->getAsmString() << NL;
575 if (Node->hasBraces())
576 Indent() << "}" << NL;
577 }
578
VisitCapturedStmt(CapturedStmt * Node)579 void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
580 PrintStmt(Node->getCapturedDecl()->getBody());
581 }
582
VisitObjCAtTryStmt(ObjCAtTryStmt * Node)583 void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
584 Indent() << "@try";
585 if (auto *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
586 PrintRawCompoundStmt(TS);
587 OS << NL;
588 }
589
590 for (ObjCAtCatchStmt *catchStmt : Node->catch_stmts()) {
591 Indent() << "@catch(";
592 if (Decl *DS = catchStmt->getCatchParamDecl())
593 PrintRawDecl(DS);
594 OS << ")";
595 if (auto *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
596 PrintRawCompoundStmt(CS);
597 OS << NL;
598 }
599 }
600
601 if (auto *FS = static_cast<ObjCAtFinallyStmt *>(Node->getFinallyStmt())) {
602 Indent() << "@finally";
603 if (auto *CS = dyn_cast<CompoundStmt>(FS->getFinallyBody())) {
604 PrintRawCompoundStmt(CS);
605 OS << NL;
606 }
607 }
608 }
609
VisitObjCAtFinallyStmt(ObjCAtFinallyStmt * Node)610 void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
611 }
612
VisitObjCAtCatchStmt(ObjCAtCatchStmt * Node)613 void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
614 Indent() << "@catch (...) { /* todo */ } " << NL;
615 }
616
VisitObjCAtThrowStmt(ObjCAtThrowStmt * Node)617 void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
618 Indent() << "@throw";
619 if (Node->getThrowExpr()) {
620 OS << " ";
621 PrintExpr(Node->getThrowExpr());
622 }
623 OS << ";" << NL;
624 }
625
VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr * Node)626 void StmtPrinter::VisitObjCAvailabilityCheckExpr(
627 ObjCAvailabilityCheckExpr *Node) {
628 OS << "@available(...)";
629 }
630
VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt * Node)631 void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
632 Indent() << "@synchronized (";
633 PrintExpr(Node->getSynchExpr());
634 OS << ")";
635 PrintRawCompoundStmt(Node->getSynchBody());
636 OS << NL;
637 }
638
VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt * Node)639 void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
640 Indent() << "@autoreleasepool";
641 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getSubStmt()));
642 OS << NL;
643 }
644
PrintRawCXXCatchStmt(CXXCatchStmt * Node)645 void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
646 OS << "catch (";
647 if (Decl *ExDecl = Node->getExceptionDecl())
648 PrintRawDecl(ExDecl);
649 else
650 OS << "...";
651 OS << ") ";
652 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
653 }
654
VisitCXXCatchStmt(CXXCatchStmt * Node)655 void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
656 Indent();
657 PrintRawCXXCatchStmt(Node);
658 OS << NL;
659 }
660
VisitCXXTryStmt(CXXTryStmt * Node)661 void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
662 Indent() << "try ";
663 PrintRawCompoundStmt(Node->getTryBlock());
664 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
665 OS << " ";
666 PrintRawCXXCatchStmt(Node->getHandler(i));
667 }
668 OS << NL;
669 }
670
VisitSEHTryStmt(SEHTryStmt * Node)671 void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
672 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
673 PrintRawCompoundStmt(Node->getTryBlock());
674 SEHExceptStmt *E = Node->getExceptHandler();
675 SEHFinallyStmt *F = Node->getFinallyHandler();
676 if(E)
677 PrintRawSEHExceptHandler(E);
678 else {
679 assert(F && "Must have a finally block...");
680 PrintRawSEHFinallyStmt(F);
681 }
682 OS << NL;
683 }
684
PrintRawSEHFinallyStmt(SEHFinallyStmt * Node)685 void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
686 OS << "__finally ";
687 PrintRawCompoundStmt(Node->getBlock());
688 OS << NL;
689 }
690
PrintRawSEHExceptHandler(SEHExceptStmt * Node)691 void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
692 OS << "__except (";
693 VisitExpr(Node->getFilterExpr());
694 OS << ")" << NL;
695 PrintRawCompoundStmt(Node->getBlock());
696 OS << NL;
697 }
698
VisitSEHExceptStmt(SEHExceptStmt * Node)699 void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
700 Indent();
701 PrintRawSEHExceptHandler(Node);
702 OS << NL;
703 }
704
VisitSEHFinallyStmt(SEHFinallyStmt * Node)705 void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
706 Indent();
707 PrintRawSEHFinallyStmt(Node);
708 OS << NL;
709 }
710
VisitSEHLeaveStmt(SEHLeaveStmt * Node)711 void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
712 Indent() << "__leave;";
713 if (Policy.IncludeNewlines) OS << NL;
714 }
715
716 //===----------------------------------------------------------------------===//
717 // OpenMP directives printing methods
718 //===----------------------------------------------------------------------===//
719
VisitOMPCanonicalLoop(OMPCanonicalLoop * Node)720 void StmtPrinter::VisitOMPCanonicalLoop(OMPCanonicalLoop *Node) {
721 PrintStmt(Node->getLoopStmt());
722 }
723
PrintOMPExecutableDirective(OMPExecutableDirective * S,bool ForceNoStmt)724 void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S,
725 bool ForceNoStmt) {
726 OMPClausePrinter Printer(OS, Policy);
727 ArrayRef<OMPClause *> Clauses = S->clauses();
728 for (auto *Clause : Clauses)
729 if (Clause && !Clause->isImplicit()) {
730 OS << ' ';
731 Printer.Visit(Clause);
732 }
733 OS << NL;
734 if (!ForceNoStmt && S->hasAssociatedStmt())
735 PrintStmt(S->getRawStmt());
736 }
737
VisitOMPMetaDirective(OMPMetaDirective * Node)738 void StmtPrinter::VisitOMPMetaDirective(OMPMetaDirective *Node) {
739 Indent() << "#pragma omp metadirective";
740 PrintOMPExecutableDirective(Node);
741 }
742
VisitOMPParallelDirective(OMPParallelDirective * Node)743 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
744 Indent() << "#pragma omp parallel";
745 PrintOMPExecutableDirective(Node);
746 }
747
VisitOMPSimdDirective(OMPSimdDirective * Node)748 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
749 Indent() << "#pragma omp simd";
750 PrintOMPExecutableDirective(Node);
751 }
752
VisitOMPTileDirective(OMPTileDirective * Node)753 void StmtPrinter::VisitOMPTileDirective(OMPTileDirective *Node) {
754 Indent() << "#pragma omp tile";
755 PrintOMPExecutableDirective(Node);
756 }
757
VisitOMPUnrollDirective(OMPUnrollDirective * Node)758 void StmtPrinter::VisitOMPUnrollDirective(OMPUnrollDirective *Node) {
759 Indent() << "#pragma omp unroll";
760 PrintOMPExecutableDirective(Node);
761 }
762
VisitOMPForDirective(OMPForDirective * Node)763 void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
764 Indent() << "#pragma omp for";
765 PrintOMPExecutableDirective(Node);
766 }
767
VisitOMPForSimdDirective(OMPForSimdDirective * Node)768 void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
769 Indent() << "#pragma omp for simd";
770 PrintOMPExecutableDirective(Node);
771 }
772
VisitOMPSectionsDirective(OMPSectionsDirective * Node)773 void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
774 Indent() << "#pragma omp sections";
775 PrintOMPExecutableDirective(Node);
776 }
777
VisitOMPSectionDirective(OMPSectionDirective * Node)778 void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
779 Indent() << "#pragma omp section";
780 PrintOMPExecutableDirective(Node);
781 }
782
VisitOMPScopeDirective(OMPScopeDirective * Node)783 void StmtPrinter::VisitOMPScopeDirective(OMPScopeDirective *Node) {
784 Indent() << "#pragma omp scope";
785 PrintOMPExecutableDirective(Node);
786 }
787
VisitOMPSingleDirective(OMPSingleDirective * Node)788 void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
789 Indent() << "#pragma omp single";
790 PrintOMPExecutableDirective(Node);
791 }
792
VisitOMPMasterDirective(OMPMasterDirective * Node)793 void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
794 Indent() << "#pragma omp master";
795 PrintOMPExecutableDirective(Node);
796 }
797
VisitOMPCriticalDirective(OMPCriticalDirective * Node)798 void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
799 Indent() << "#pragma omp critical";
800 if (Node->getDirectiveName().getName()) {
801 OS << " (";
802 Node->getDirectiveName().printName(OS, Policy);
803 OS << ")";
804 }
805 PrintOMPExecutableDirective(Node);
806 }
807
VisitOMPParallelForDirective(OMPParallelForDirective * Node)808 void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
809 Indent() << "#pragma omp parallel for";
810 PrintOMPExecutableDirective(Node);
811 }
812
VisitOMPParallelForSimdDirective(OMPParallelForSimdDirective * Node)813 void StmtPrinter::VisitOMPParallelForSimdDirective(
814 OMPParallelForSimdDirective *Node) {
815 Indent() << "#pragma omp parallel for simd";
816 PrintOMPExecutableDirective(Node);
817 }
818
VisitOMPParallelMasterDirective(OMPParallelMasterDirective * Node)819 void StmtPrinter::VisitOMPParallelMasterDirective(
820 OMPParallelMasterDirective *Node) {
821 Indent() << "#pragma omp parallel master";
822 PrintOMPExecutableDirective(Node);
823 }
824
VisitOMPParallelMaskedDirective(OMPParallelMaskedDirective * Node)825 void StmtPrinter::VisitOMPParallelMaskedDirective(
826 OMPParallelMaskedDirective *Node) {
827 Indent() << "#pragma omp parallel masked";
828 PrintOMPExecutableDirective(Node);
829 }
830
VisitOMPParallelSectionsDirective(OMPParallelSectionsDirective * Node)831 void StmtPrinter::VisitOMPParallelSectionsDirective(
832 OMPParallelSectionsDirective *Node) {
833 Indent() << "#pragma omp parallel sections";
834 PrintOMPExecutableDirective(Node);
835 }
836
VisitOMPTaskDirective(OMPTaskDirective * Node)837 void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
838 Indent() << "#pragma omp task";
839 PrintOMPExecutableDirective(Node);
840 }
841
VisitOMPTaskyieldDirective(OMPTaskyieldDirective * Node)842 void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
843 Indent() << "#pragma omp taskyield";
844 PrintOMPExecutableDirective(Node);
845 }
846
VisitOMPBarrierDirective(OMPBarrierDirective * Node)847 void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
848 Indent() << "#pragma omp barrier";
849 PrintOMPExecutableDirective(Node);
850 }
851
VisitOMPTaskwaitDirective(OMPTaskwaitDirective * Node)852 void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
853 Indent() << "#pragma omp taskwait";
854 PrintOMPExecutableDirective(Node);
855 }
856
VisitOMPErrorDirective(OMPErrorDirective * Node)857 void StmtPrinter::VisitOMPErrorDirective(OMPErrorDirective *Node) {
858 Indent() << "#pragma omp error";
859 PrintOMPExecutableDirective(Node);
860 }
861
VisitOMPTaskgroupDirective(OMPTaskgroupDirective * Node)862 void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
863 Indent() << "#pragma omp taskgroup";
864 PrintOMPExecutableDirective(Node);
865 }
866
VisitOMPFlushDirective(OMPFlushDirective * Node)867 void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
868 Indent() << "#pragma omp flush";
869 PrintOMPExecutableDirective(Node);
870 }
871
VisitOMPDepobjDirective(OMPDepobjDirective * Node)872 void StmtPrinter::VisitOMPDepobjDirective(OMPDepobjDirective *Node) {
873 Indent() << "#pragma omp depobj";
874 PrintOMPExecutableDirective(Node);
875 }
876
VisitOMPScanDirective(OMPScanDirective * Node)877 void StmtPrinter::VisitOMPScanDirective(OMPScanDirective *Node) {
878 Indent() << "#pragma omp scan";
879 PrintOMPExecutableDirective(Node);
880 }
881
VisitOMPOrderedDirective(OMPOrderedDirective * Node)882 void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
883 Indent() << "#pragma omp ordered";
884 PrintOMPExecutableDirective(Node, Node->hasClausesOfKind<OMPDependClause>());
885 }
886
VisitOMPAtomicDirective(OMPAtomicDirective * Node)887 void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
888 Indent() << "#pragma omp atomic";
889 PrintOMPExecutableDirective(Node);
890 }
891
VisitOMPTargetDirective(OMPTargetDirective * Node)892 void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
893 Indent() << "#pragma omp target";
894 PrintOMPExecutableDirective(Node);
895 }
896
VisitOMPTargetDataDirective(OMPTargetDataDirective * Node)897 void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
898 Indent() << "#pragma omp target data";
899 PrintOMPExecutableDirective(Node);
900 }
901
VisitOMPTargetEnterDataDirective(OMPTargetEnterDataDirective * Node)902 void StmtPrinter::VisitOMPTargetEnterDataDirective(
903 OMPTargetEnterDataDirective *Node) {
904 Indent() << "#pragma omp target enter data";
905 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
906 }
907
VisitOMPTargetExitDataDirective(OMPTargetExitDataDirective * Node)908 void StmtPrinter::VisitOMPTargetExitDataDirective(
909 OMPTargetExitDataDirective *Node) {
910 Indent() << "#pragma omp target exit data";
911 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
912 }
913
VisitOMPTargetParallelDirective(OMPTargetParallelDirective * Node)914 void StmtPrinter::VisitOMPTargetParallelDirective(
915 OMPTargetParallelDirective *Node) {
916 Indent() << "#pragma omp target parallel";
917 PrintOMPExecutableDirective(Node);
918 }
919
VisitOMPTargetParallelForDirective(OMPTargetParallelForDirective * Node)920 void StmtPrinter::VisitOMPTargetParallelForDirective(
921 OMPTargetParallelForDirective *Node) {
922 Indent() << "#pragma omp target parallel for";
923 PrintOMPExecutableDirective(Node);
924 }
925
VisitOMPTeamsDirective(OMPTeamsDirective * Node)926 void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
927 Indent() << "#pragma omp teams";
928 PrintOMPExecutableDirective(Node);
929 }
930
VisitOMPCancellationPointDirective(OMPCancellationPointDirective * Node)931 void StmtPrinter::VisitOMPCancellationPointDirective(
932 OMPCancellationPointDirective *Node) {
933 Indent() << "#pragma omp cancellation point "
934 << getOpenMPDirectiveName(Node->getCancelRegion());
935 PrintOMPExecutableDirective(Node);
936 }
937
VisitOMPCancelDirective(OMPCancelDirective * Node)938 void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
939 Indent() << "#pragma omp cancel "
940 << getOpenMPDirectiveName(Node->getCancelRegion());
941 PrintOMPExecutableDirective(Node);
942 }
943
VisitOMPTaskLoopDirective(OMPTaskLoopDirective * Node)944 void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) {
945 Indent() << "#pragma omp taskloop";
946 PrintOMPExecutableDirective(Node);
947 }
948
VisitOMPTaskLoopSimdDirective(OMPTaskLoopSimdDirective * Node)949 void StmtPrinter::VisitOMPTaskLoopSimdDirective(
950 OMPTaskLoopSimdDirective *Node) {
951 Indent() << "#pragma omp taskloop simd";
952 PrintOMPExecutableDirective(Node);
953 }
954
VisitOMPMasterTaskLoopDirective(OMPMasterTaskLoopDirective * Node)955 void StmtPrinter::VisitOMPMasterTaskLoopDirective(
956 OMPMasterTaskLoopDirective *Node) {
957 Indent() << "#pragma omp master taskloop";
958 PrintOMPExecutableDirective(Node);
959 }
960
VisitOMPMaskedTaskLoopDirective(OMPMaskedTaskLoopDirective * Node)961 void StmtPrinter::VisitOMPMaskedTaskLoopDirective(
962 OMPMaskedTaskLoopDirective *Node) {
963 Indent() << "#pragma omp masked taskloop";
964 PrintOMPExecutableDirective(Node);
965 }
966
VisitOMPMasterTaskLoopSimdDirective(OMPMasterTaskLoopSimdDirective * Node)967 void StmtPrinter::VisitOMPMasterTaskLoopSimdDirective(
968 OMPMasterTaskLoopSimdDirective *Node) {
969 Indent() << "#pragma omp master taskloop simd";
970 PrintOMPExecutableDirective(Node);
971 }
972
VisitOMPMaskedTaskLoopSimdDirective(OMPMaskedTaskLoopSimdDirective * Node)973 void StmtPrinter::VisitOMPMaskedTaskLoopSimdDirective(
974 OMPMaskedTaskLoopSimdDirective *Node) {
975 Indent() << "#pragma omp masked taskloop simd";
976 PrintOMPExecutableDirective(Node);
977 }
978
VisitOMPParallelMasterTaskLoopDirective(OMPParallelMasterTaskLoopDirective * Node)979 void StmtPrinter::VisitOMPParallelMasterTaskLoopDirective(
980 OMPParallelMasterTaskLoopDirective *Node) {
981 Indent() << "#pragma omp parallel master taskloop";
982 PrintOMPExecutableDirective(Node);
983 }
984
VisitOMPParallelMaskedTaskLoopDirective(OMPParallelMaskedTaskLoopDirective * Node)985 void StmtPrinter::VisitOMPParallelMaskedTaskLoopDirective(
986 OMPParallelMaskedTaskLoopDirective *Node) {
987 Indent() << "#pragma omp parallel masked taskloop";
988 PrintOMPExecutableDirective(Node);
989 }
990
VisitOMPParallelMasterTaskLoopSimdDirective(OMPParallelMasterTaskLoopSimdDirective * Node)991 void StmtPrinter::VisitOMPParallelMasterTaskLoopSimdDirective(
992 OMPParallelMasterTaskLoopSimdDirective *Node) {
993 Indent() << "#pragma omp parallel master taskloop simd";
994 PrintOMPExecutableDirective(Node);
995 }
996
VisitOMPParallelMaskedTaskLoopSimdDirective(OMPParallelMaskedTaskLoopSimdDirective * Node)997 void StmtPrinter::VisitOMPParallelMaskedTaskLoopSimdDirective(
998 OMPParallelMaskedTaskLoopSimdDirective *Node) {
999 Indent() << "#pragma omp parallel masked taskloop simd";
1000 PrintOMPExecutableDirective(Node);
1001 }
1002
VisitOMPDistributeDirective(OMPDistributeDirective * Node)1003 void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) {
1004 Indent() << "#pragma omp distribute";
1005 PrintOMPExecutableDirective(Node);
1006 }
1007
VisitOMPTargetUpdateDirective(OMPTargetUpdateDirective * Node)1008 void StmtPrinter::VisitOMPTargetUpdateDirective(
1009 OMPTargetUpdateDirective *Node) {
1010 Indent() << "#pragma omp target update";
1011 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
1012 }
1013
VisitOMPDistributeParallelForDirective(OMPDistributeParallelForDirective * Node)1014 void StmtPrinter::VisitOMPDistributeParallelForDirective(
1015 OMPDistributeParallelForDirective *Node) {
1016 Indent() << "#pragma omp distribute parallel for";
1017 PrintOMPExecutableDirective(Node);
1018 }
1019
VisitOMPDistributeParallelForSimdDirective(OMPDistributeParallelForSimdDirective * Node)1020 void StmtPrinter::VisitOMPDistributeParallelForSimdDirective(
1021 OMPDistributeParallelForSimdDirective *Node) {
1022 Indent() << "#pragma omp distribute parallel for simd";
1023 PrintOMPExecutableDirective(Node);
1024 }
1025
VisitOMPDistributeSimdDirective(OMPDistributeSimdDirective * Node)1026 void StmtPrinter::VisitOMPDistributeSimdDirective(
1027 OMPDistributeSimdDirective *Node) {
1028 Indent() << "#pragma omp distribute simd";
1029 PrintOMPExecutableDirective(Node);
1030 }
1031
VisitOMPTargetParallelForSimdDirective(OMPTargetParallelForSimdDirective * Node)1032 void StmtPrinter::VisitOMPTargetParallelForSimdDirective(
1033 OMPTargetParallelForSimdDirective *Node) {
1034 Indent() << "#pragma omp target parallel for simd";
1035 PrintOMPExecutableDirective(Node);
1036 }
1037
VisitOMPTargetSimdDirective(OMPTargetSimdDirective * Node)1038 void StmtPrinter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective *Node) {
1039 Indent() << "#pragma omp target simd";
1040 PrintOMPExecutableDirective(Node);
1041 }
1042
VisitOMPTeamsDistributeDirective(OMPTeamsDistributeDirective * Node)1043 void StmtPrinter::VisitOMPTeamsDistributeDirective(
1044 OMPTeamsDistributeDirective *Node) {
1045 Indent() << "#pragma omp teams distribute";
1046 PrintOMPExecutableDirective(Node);
1047 }
1048
VisitOMPTeamsDistributeSimdDirective(OMPTeamsDistributeSimdDirective * Node)1049 void StmtPrinter::VisitOMPTeamsDistributeSimdDirective(
1050 OMPTeamsDistributeSimdDirective *Node) {
1051 Indent() << "#pragma omp teams distribute simd";
1052 PrintOMPExecutableDirective(Node);
1053 }
1054
VisitOMPTeamsDistributeParallelForSimdDirective(OMPTeamsDistributeParallelForSimdDirective * Node)1055 void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective(
1056 OMPTeamsDistributeParallelForSimdDirective *Node) {
1057 Indent() << "#pragma omp teams distribute parallel for simd";
1058 PrintOMPExecutableDirective(Node);
1059 }
1060
VisitOMPTeamsDistributeParallelForDirective(OMPTeamsDistributeParallelForDirective * Node)1061 void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective(
1062 OMPTeamsDistributeParallelForDirective *Node) {
1063 Indent() << "#pragma omp teams distribute parallel for";
1064 PrintOMPExecutableDirective(Node);
1065 }
1066
VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective * Node)1067 void StmtPrinter::VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective *Node) {
1068 Indent() << "#pragma omp target teams";
1069 PrintOMPExecutableDirective(Node);
1070 }
1071
VisitOMPTargetTeamsDistributeDirective(OMPTargetTeamsDistributeDirective * Node)1072 void StmtPrinter::VisitOMPTargetTeamsDistributeDirective(
1073 OMPTargetTeamsDistributeDirective *Node) {
1074 Indent() << "#pragma omp target teams distribute";
1075 PrintOMPExecutableDirective(Node);
1076 }
1077
VisitOMPTargetTeamsDistributeParallelForDirective(OMPTargetTeamsDistributeParallelForDirective * Node)1078 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective(
1079 OMPTargetTeamsDistributeParallelForDirective *Node) {
1080 Indent() << "#pragma omp target teams distribute parallel for";
1081 PrintOMPExecutableDirective(Node);
1082 }
1083
VisitOMPTargetTeamsDistributeParallelForSimdDirective(OMPTargetTeamsDistributeParallelForSimdDirective * Node)1084 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
1085 OMPTargetTeamsDistributeParallelForSimdDirective *Node) {
1086 Indent() << "#pragma omp target teams distribute parallel for simd";
1087 PrintOMPExecutableDirective(Node);
1088 }
1089
VisitOMPTargetTeamsDistributeSimdDirective(OMPTargetTeamsDistributeSimdDirective * Node)1090 void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective(
1091 OMPTargetTeamsDistributeSimdDirective *Node) {
1092 Indent() << "#pragma omp target teams distribute simd";
1093 PrintOMPExecutableDirective(Node);
1094 }
1095
VisitOMPInteropDirective(OMPInteropDirective * Node)1096 void StmtPrinter::VisitOMPInteropDirective(OMPInteropDirective *Node) {
1097 Indent() << "#pragma omp interop";
1098 PrintOMPExecutableDirective(Node);
1099 }
1100
VisitOMPDispatchDirective(OMPDispatchDirective * Node)1101 void StmtPrinter::VisitOMPDispatchDirective(OMPDispatchDirective *Node) {
1102 Indent() << "#pragma omp dispatch";
1103 PrintOMPExecutableDirective(Node);
1104 }
1105
VisitOMPMaskedDirective(OMPMaskedDirective * Node)1106 void StmtPrinter::VisitOMPMaskedDirective(OMPMaskedDirective *Node) {
1107 Indent() << "#pragma omp masked";
1108 PrintOMPExecutableDirective(Node);
1109 }
1110
VisitOMPGenericLoopDirective(OMPGenericLoopDirective * Node)1111 void StmtPrinter::VisitOMPGenericLoopDirective(OMPGenericLoopDirective *Node) {
1112 Indent() << "#pragma omp loop";
1113 PrintOMPExecutableDirective(Node);
1114 }
1115
VisitOMPTeamsGenericLoopDirective(OMPTeamsGenericLoopDirective * Node)1116 void StmtPrinter::VisitOMPTeamsGenericLoopDirective(
1117 OMPTeamsGenericLoopDirective *Node) {
1118 Indent() << "#pragma omp teams loop";
1119 PrintOMPExecutableDirective(Node);
1120 }
1121
VisitOMPTargetTeamsGenericLoopDirective(OMPTargetTeamsGenericLoopDirective * Node)1122 void StmtPrinter::VisitOMPTargetTeamsGenericLoopDirective(
1123 OMPTargetTeamsGenericLoopDirective *Node) {
1124 Indent() << "#pragma omp target teams loop";
1125 PrintOMPExecutableDirective(Node);
1126 }
1127
VisitOMPParallelGenericLoopDirective(OMPParallelGenericLoopDirective * Node)1128 void StmtPrinter::VisitOMPParallelGenericLoopDirective(
1129 OMPParallelGenericLoopDirective *Node) {
1130 Indent() << "#pragma omp parallel loop";
1131 PrintOMPExecutableDirective(Node);
1132 }
1133
VisitOMPTargetParallelGenericLoopDirective(OMPTargetParallelGenericLoopDirective * Node)1134 void StmtPrinter::VisitOMPTargetParallelGenericLoopDirective(
1135 OMPTargetParallelGenericLoopDirective *Node) {
1136 Indent() << "#pragma omp target parallel loop";
1137 PrintOMPExecutableDirective(Node);
1138 }
1139
1140 //===----------------------------------------------------------------------===//
1141 // Expr printing methods.
1142 //===----------------------------------------------------------------------===//
1143
VisitSourceLocExpr(SourceLocExpr * Node)1144 void StmtPrinter::VisitSourceLocExpr(SourceLocExpr *Node) {
1145 OS << Node->getBuiltinStr() << "()";
1146 }
1147
VisitConstantExpr(ConstantExpr * Node)1148 void StmtPrinter::VisitConstantExpr(ConstantExpr *Node) {
1149 PrintExpr(Node->getSubExpr());
1150 }
1151
VisitDeclRefExpr(DeclRefExpr * Node)1152 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
1153 if (const auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) {
1154 OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy);
1155 return;
1156 }
1157 if (const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(Node->getDecl())) {
1158 TPOD->printAsExpr(OS, Policy);
1159 return;
1160 }
1161 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1162 Qualifier->print(OS, Policy);
1163 if (Node->hasTemplateKeyword())
1164 OS << "template ";
1165 if (Policy.CleanUglifiedParameters &&
1166 isa<ParmVarDecl, NonTypeTemplateParmDecl>(Node->getDecl()) &&
1167 Node->getDecl()->getIdentifier())
1168 OS << Node->getDecl()->getIdentifier()->deuglifiedName();
1169 else
1170 Node->getNameInfo().printName(OS, Policy);
1171 if (Node->hasExplicitTemplateArgs()) {
1172 const TemplateParameterList *TPL = nullptr;
1173 if (!Node->hadMultipleCandidates())
1174 if (auto *TD = dyn_cast<TemplateDecl>(Node->getDecl()))
1175 TPL = TD->getTemplateParameters();
1176 printTemplateArgumentList(OS, Node->template_arguments(), Policy, TPL);
1177 }
1178 }
1179
VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr * Node)1180 void StmtPrinter::VisitDependentScopeDeclRefExpr(
1181 DependentScopeDeclRefExpr *Node) {
1182 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1183 Qualifier->print(OS, Policy);
1184 if (Node->hasTemplateKeyword())
1185 OS << "template ";
1186 OS << Node->getNameInfo();
1187 if (Node->hasExplicitTemplateArgs())
1188 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1189 }
1190
VisitUnresolvedLookupExpr(UnresolvedLookupExpr * Node)1191 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
1192 if (Node->getQualifier())
1193 Node->getQualifier()->print(OS, Policy);
1194 if (Node->hasTemplateKeyword())
1195 OS << "template ";
1196 OS << Node->getNameInfo();
1197 if (Node->hasExplicitTemplateArgs())
1198 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1199 }
1200
isImplicitSelf(const Expr * E)1201 static bool isImplicitSelf(const Expr *E) {
1202 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
1203 if (const auto *PD = dyn_cast<ImplicitParamDecl>(DRE->getDecl())) {
1204 if (PD->getParameterKind() == ImplicitParamKind::ObjCSelf &&
1205 DRE->getBeginLoc().isInvalid())
1206 return true;
1207 }
1208 }
1209 return false;
1210 }
1211
VisitObjCIvarRefExpr(ObjCIvarRefExpr * Node)1212 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
1213 if (Node->getBase()) {
1214 if (!Policy.SuppressImplicitBase ||
1215 !isImplicitSelf(Node->getBase()->IgnoreImpCasts())) {
1216 PrintExpr(Node->getBase());
1217 OS << (Node->isArrow() ? "->" : ".");
1218 }
1219 }
1220 OS << *Node->getDecl();
1221 }
1222
VisitObjCPropertyRefExpr(ObjCPropertyRefExpr * Node)1223 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
1224 if (Node->isSuperReceiver())
1225 OS << "super.";
1226 else if (Node->isObjectReceiver() && Node->getBase()) {
1227 PrintExpr(Node->getBase());
1228 OS << ".";
1229 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1230 OS << Node->getClassReceiver()->getName() << ".";
1231 }
1232
1233 if (Node->isImplicitProperty()) {
1234 if (const auto *Getter = Node->getImplicitPropertyGetter())
1235 Getter->getSelector().print(OS);
1236 else
1237 OS << SelectorTable::getPropertyNameFromSetterSelector(
1238 Node->getImplicitPropertySetter()->getSelector());
1239 } else
1240 OS << Node->getExplicitProperty()->getName();
1241 }
1242
VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr * Node)1243 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1244 PrintExpr(Node->getBaseExpr());
1245 OS << "[";
1246 PrintExpr(Node->getKeyExpr());
1247 OS << "]";
1248 }
1249
VisitSYCLUniqueStableNameExpr(SYCLUniqueStableNameExpr * Node)1250 void StmtPrinter::VisitSYCLUniqueStableNameExpr(
1251 SYCLUniqueStableNameExpr *Node) {
1252 OS << "__builtin_sycl_unique_stable_name(";
1253 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1254 OS << ")";
1255 }
1256
VisitPredefinedExpr(PredefinedExpr * Node)1257 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
1258 OS << PredefinedExpr::getIdentKindName(Node->getIdentKind());
1259 }
1260
VisitCharacterLiteral(CharacterLiteral * Node)1261 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
1262 CharacterLiteral::print(Node->getValue(), Node->getKind(), OS);
1263 }
1264
1265 /// Prints the given expression using the original source text. Returns true on
1266 /// success, false otherwise.
printExprAsWritten(raw_ostream & OS,Expr * E,const ASTContext * Context)1267 static bool printExprAsWritten(raw_ostream &OS, Expr *E,
1268 const ASTContext *Context) {
1269 if (!Context)
1270 return false;
1271 bool Invalid = false;
1272 StringRef Source = Lexer::getSourceText(
1273 CharSourceRange::getTokenRange(E->getSourceRange()),
1274 Context->getSourceManager(), Context->getLangOpts(), &Invalid);
1275 if (!Invalid) {
1276 OS << Source;
1277 return true;
1278 }
1279 return false;
1280 }
1281
VisitIntegerLiteral(IntegerLiteral * Node)1282 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
1283 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1284 return;
1285 bool isSigned = Node->getType()->isSignedIntegerType();
1286 OS << toString(Node->getValue(), 10, isSigned);
1287
1288 if (isa<BitIntType>(Node->getType())) {
1289 OS << (isSigned ? "wb" : "uwb");
1290 return;
1291 }
1292
1293 // Emit suffixes. Integer literals are always a builtin integer type.
1294 switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1295 default: llvm_unreachable("Unexpected type for integer literal!");
1296 case BuiltinType::Char_S:
1297 case BuiltinType::Char_U: OS << "i8"; break;
1298 case BuiltinType::UChar: OS << "Ui8"; break;
1299 case BuiltinType::SChar: OS << "i8"; break;
1300 case BuiltinType::Short: OS << "i16"; break;
1301 case BuiltinType::UShort: OS << "Ui16"; break;
1302 case BuiltinType::Int: break; // no suffix.
1303 case BuiltinType::UInt: OS << 'U'; break;
1304 case BuiltinType::Long: OS << 'L'; break;
1305 case BuiltinType::ULong: OS << "UL"; break;
1306 case BuiltinType::LongLong: OS << "LL"; break;
1307 case BuiltinType::ULongLong: OS << "ULL"; break;
1308 case BuiltinType::Int128:
1309 break; // no suffix.
1310 case BuiltinType::UInt128:
1311 break; // no suffix.
1312 case BuiltinType::WChar_S:
1313 case BuiltinType::WChar_U:
1314 break; // no suffix
1315 }
1316 }
1317
VisitFixedPointLiteral(FixedPointLiteral * Node)1318 void StmtPrinter::VisitFixedPointLiteral(FixedPointLiteral *Node) {
1319 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1320 return;
1321 OS << Node->getValueAsString(/*Radix=*/10);
1322
1323 switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1324 default: llvm_unreachable("Unexpected type for fixed point literal!");
1325 case BuiltinType::ShortFract: OS << "hr"; break;
1326 case BuiltinType::ShortAccum: OS << "hk"; break;
1327 case BuiltinType::UShortFract: OS << "uhr"; break;
1328 case BuiltinType::UShortAccum: OS << "uhk"; break;
1329 case BuiltinType::Fract: OS << "r"; break;
1330 case BuiltinType::Accum: OS << "k"; break;
1331 case BuiltinType::UFract: OS << "ur"; break;
1332 case BuiltinType::UAccum: OS << "uk"; break;
1333 case BuiltinType::LongFract: OS << "lr"; break;
1334 case BuiltinType::LongAccum: OS << "lk"; break;
1335 case BuiltinType::ULongFract: OS << "ulr"; break;
1336 case BuiltinType::ULongAccum: OS << "ulk"; break;
1337 }
1338 }
1339
PrintFloatingLiteral(raw_ostream & OS,FloatingLiteral * Node,bool PrintSuffix)1340 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1341 bool PrintSuffix) {
1342 SmallString<16> Str;
1343 Node->getValue().toString(Str);
1344 OS << Str;
1345 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1346 OS << '.'; // Trailing dot in order to separate from ints.
1347
1348 if (!PrintSuffix)
1349 return;
1350
1351 // Emit suffixes. Float literals are always a builtin float type.
1352 switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1353 default: llvm_unreachable("Unexpected type for float literal!");
1354 case BuiltinType::Half: break; // FIXME: suffix?
1355 case BuiltinType::Ibm128: break; // FIXME: No suffix for ibm128 literal
1356 case BuiltinType::Double: break; // no suffix.
1357 case BuiltinType::Float16: OS << "F16"; break;
1358 case BuiltinType::Float: OS << 'F'; break;
1359 case BuiltinType::LongDouble: OS << 'L'; break;
1360 case BuiltinType::Float128: OS << 'Q'; break;
1361 }
1362 }
1363
VisitFloatingLiteral(FloatingLiteral * Node)1364 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1365 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1366 return;
1367 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
1368 }
1369
VisitImaginaryLiteral(ImaginaryLiteral * Node)1370 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1371 PrintExpr(Node->getSubExpr());
1372 OS << "i";
1373 }
1374
VisitStringLiteral(StringLiteral * Str)1375 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
1376 Str->outputString(OS);
1377 }
1378
VisitParenExpr(ParenExpr * Node)1379 void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1380 OS << "(";
1381 PrintExpr(Node->getSubExpr());
1382 OS << ")";
1383 }
1384
VisitUnaryOperator(UnaryOperator * Node)1385 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
1386 if (!Node->isPostfix()) {
1387 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1388
1389 // Print a space if this is an "identifier operator" like __real, or if
1390 // it might be concatenated incorrectly like '+'.
1391 switch (Node->getOpcode()) {
1392 default: break;
1393 case UO_Real:
1394 case UO_Imag:
1395 case UO_Extension:
1396 OS << ' ';
1397 break;
1398 case UO_Plus:
1399 case UO_Minus:
1400 if (isa<UnaryOperator>(Node->getSubExpr()))
1401 OS << ' ';
1402 break;
1403 }
1404 }
1405 PrintExpr(Node->getSubExpr());
1406
1407 if (Node->isPostfix())
1408 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1409 }
1410
VisitOffsetOfExpr(OffsetOfExpr * Node)1411 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1412 OS << "__builtin_offsetof(";
1413 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1414 OS << ", ";
1415 bool PrintedSomething = false;
1416 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1417 OffsetOfNode ON = Node->getComponent(i);
1418 if (ON.getKind() == OffsetOfNode::Array) {
1419 // Array node
1420 OS << "[";
1421 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1422 OS << "]";
1423 PrintedSomething = true;
1424 continue;
1425 }
1426
1427 // Skip implicit base indirections.
1428 if (ON.getKind() == OffsetOfNode::Base)
1429 continue;
1430
1431 // Field or identifier node.
1432 IdentifierInfo *Id = ON.getFieldName();
1433 if (!Id)
1434 continue;
1435
1436 if (PrintedSomething)
1437 OS << ".";
1438 else
1439 PrintedSomething = true;
1440 OS << Id->getName();
1441 }
1442 OS << ")";
1443 }
1444
VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr * Node)1445 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(
1446 UnaryExprOrTypeTraitExpr *Node) {
1447 const char *Spelling = getTraitSpelling(Node->getKind());
1448 if (Node->getKind() == UETT_AlignOf) {
1449 if (Policy.Alignof)
1450 Spelling = "alignof";
1451 else if (Policy.UnderscoreAlignof)
1452 Spelling = "_Alignof";
1453 else
1454 Spelling = "__alignof";
1455 }
1456
1457 OS << Spelling;
1458
1459 if (Node->isArgumentType()) {
1460 OS << '(';
1461 Node->getArgumentType().print(OS, Policy);
1462 OS << ')';
1463 } else {
1464 OS << " ";
1465 PrintExpr(Node->getArgumentExpr());
1466 }
1467 }
1468
VisitGenericSelectionExpr(GenericSelectionExpr * Node)1469 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1470 OS << "_Generic(";
1471 if (Node->isExprPredicate())
1472 PrintExpr(Node->getControllingExpr());
1473 else
1474 Node->getControllingType()->getType().print(OS, Policy);
1475
1476 for (const GenericSelectionExpr::Association &Assoc : Node->associations()) {
1477 OS << ", ";
1478 QualType T = Assoc.getType();
1479 if (T.isNull())
1480 OS << "default";
1481 else
1482 T.print(OS, Policy);
1483 OS << ": ";
1484 PrintExpr(Assoc.getAssociationExpr());
1485 }
1486 OS << ")";
1487 }
1488
VisitArraySubscriptExpr(ArraySubscriptExpr * Node)1489 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
1490 PrintExpr(Node->getLHS());
1491 OS << "[";
1492 PrintExpr(Node->getRHS());
1493 OS << "]";
1494 }
1495
VisitMatrixSubscriptExpr(MatrixSubscriptExpr * Node)1496 void StmtPrinter::VisitMatrixSubscriptExpr(MatrixSubscriptExpr *Node) {
1497 PrintExpr(Node->getBase());
1498 OS << "[";
1499 PrintExpr(Node->getRowIdx());
1500 OS << "]";
1501 OS << "[";
1502 PrintExpr(Node->getColumnIdx());
1503 OS << "]";
1504 }
1505
VisitOMPArraySectionExpr(OMPArraySectionExpr * Node)1506 void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) {
1507 PrintExpr(Node->getBase());
1508 OS << "[";
1509 if (Node->getLowerBound())
1510 PrintExpr(Node->getLowerBound());
1511 if (Node->getColonLocFirst().isValid()) {
1512 OS << ":";
1513 if (Node->getLength())
1514 PrintExpr(Node->getLength());
1515 }
1516 if (Node->getColonLocSecond().isValid()) {
1517 OS << ":";
1518 if (Node->getStride())
1519 PrintExpr(Node->getStride());
1520 }
1521 OS << "]";
1522 }
1523
VisitOMPArrayShapingExpr(OMPArrayShapingExpr * Node)1524 void StmtPrinter::VisitOMPArrayShapingExpr(OMPArrayShapingExpr *Node) {
1525 OS << "(";
1526 for (Expr *E : Node->getDimensions()) {
1527 OS << "[";
1528 PrintExpr(E);
1529 OS << "]";
1530 }
1531 OS << ")";
1532 PrintExpr(Node->getBase());
1533 }
1534
VisitOMPIteratorExpr(OMPIteratorExpr * Node)1535 void StmtPrinter::VisitOMPIteratorExpr(OMPIteratorExpr *Node) {
1536 OS << "iterator(";
1537 for (unsigned I = 0, E = Node->numOfIterators(); I < E; ++I) {
1538 auto *VD = cast<ValueDecl>(Node->getIteratorDecl(I));
1539 VD->getType().print(OS, Policy);
1540 const OMPIteratorExpr::IteratorRange Range = Node->getIteratorRange(I);
1541 OS << " " << VD->getName() << " = ";
1542 PrintExpr(Range.Begin);
1543 OS << ":";
1544 PrintExpr(Range.End);
1545 if (Range.Step) {
1546 OS << ":";
1547 PrintExpr(Range.Step);
1548 }
1549 if (I < E - 1)
1550 OS << ", ";
1551 }
1552 OS << ")";
1553 }
1554
PrintCallArgs(CallExpr * Call)1555 void StmtPrinter::PrintCallArgs(CallExpr *Call) {
1556 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
1557 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1558 // Don't print any defaulted arguments
1559 break;
1560 }
1561
1562 if (i) OS << ", ";
1563 PrintExpr(Call->getArg(i));
1564 }
1565 }
1566
VisitCallExpr(CallExpr * Call)1567 void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1568 PrintExpr(Call->getCallee());
1569 OS << "(";
1570 PrintCallArgs(Call);
1571 OS << ")";
1572 }
1573
isImplicitThis(const Expr * E)1574 static bool isImplicitThis(const Expr *E) {
1575 if (const auto *TE = dyn_cast<CXXThisExpr>(E))
1576 return TE->isImplicit();
1577 return false;
1578 }
1579
VisitMemberExpr(MemberExpr * Node)1580 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1581 if (!Policy.SuppressImplicitBase || !isImplicitThis(Node->getBase())) {
1582 PrintExpr(Node->getBase());
1583
1584 auto *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1585 FieldDecl *ParentDecl =
1586 ParentMember ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl())
1587 : nullptr;
1588
1589 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1590 OS << (Node->isArrow() ? "->" : ".");
1591 }
1592
1593 if (auto *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1594 if (FD->isAnonymousStructOrUnion())
1595 return;
1596
1597 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1598 Qualifier->print(OS, Policy);
1599 if (Node->hasTemplateKeyword())
1600 OS << "template ";
1601 OS << Node->getMemberNameInfo();
1602 const TemplateParameterList *TPL = nullptr;
1603 if (auto *FD = dyn_cast<FunctionDecl>(Node->getMemberDecl())) {
1604 if (!Node->hadMultipleCandidates())
1605 if (auto *FTD = FD->getPrimaryTemplate())
1606 TPL = FTD->getTemplateParameters();
1607 } else if (auto *VTSD =
1608 dyn_cast<VarTemplateSpecializationDecl>(Node->getMemberDecl()))
1609 TPL = VTSD->getSpecializedTemplate()->getTemplateParameters();
1610 if (Node->hasExplicitTemplateArgs())
1611 printTemplateArgumentList(OS, Node->template_arguments(), Policy, TPL);
1612 }
1613
VisitObjCIsaExpr(ObjCIsaExpr * Node)1614 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1615 PrintExpr(Node->getBase());
1616 OS << (Node->isArrow() ? "->isa" : ".isa");
1617 }
1618
VisitExtVectorElementExpr(ExtVectorElementExpr * Node)1619 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1620 PrintExpr(Node->getBase());
1621 OS << ".";
1622 OS << Node->getAccessor().getName();
1623 }
1624
VisitCStyleCastExpr(CStyleCastExpr * Node)1625 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1626 OS << '(';
1627 Node->getTypeAsWritten().print(OS, Policy);
1628 OS << ')';
1629 PrintExpr(Node->getSubExpr());
1630 }
1631
VisitCompoundLiteralExpr(CompoundLiteralExpr * Node)1632 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1633 OS << '(';
1634 Node->getType().print(OS, Policy);
1635 OS << ')';
1636 PrintExpr(Node->getInitializer());
1637 }
1638
VisitImplicitCastExpr(ImplicitCastExpr * Node)1639 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1640 // No need to print anything, simply forward to the subexpression.
1641 PrintExpr(Node->getSubExpr());
1642 }
1643
VisitBinaryOperator(BinaryOperator * Node)1644 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1645 PrintExpr(Node->getLHS());
1646 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1647 PrintExpr(Node->getRHS());
1648 }
1649
VisitCompoundAssignOperator(CompoundAssignOperator * Node)1650 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1651 PrintExpr(Node->getLHS());
1652 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1653 PrintExpr(Node->getRHS());
1654 }
1655
VisitConditionalOperator(ConditionalOperator * Node)1656 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1657 PrintExpr(Node->getCond());
1658 OS << " ? ";
1659 PrintExpr(Node->getLHS());
1660 OS << " : ";
1661 PrintExpr(Node->getRHS());
1662 }
1663
1664 // GNU extensions.
1665
1666 void
VisitBinaryConditionalOperator(BinaryConditionalOperator * Node)1667 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1668 PrintExpr(Node->getCommon());
1669 OS << " ?: ";
1670 PrintExpr(Node->getFalseExpr());
1671 }
1672
VisitAddrLabelExpr(AddrLabelExpr * Node)1673 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1674 OS << "&&" << Node->getLabel()->getName();
1675 }
1676
VisitStmtExpr(StmtExpr * E)1677 void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1678 OS << "(";
1679 PrintRawCompoundStmt(E->getSubStmt());
1680 OS << ")";
1681 }
1682
VisitChooseExpr(ChooseExpr * Node)1683 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1684 OS << "__builtin_choose_expr(";
1685 PrintExpr(Node->getCond());
1686 OS << ", ";
1687 PrintExpr(Node->getLHS());
1688 OS << ", ";
1689 PrintExpr(Node->getRHS());
1690 OS << ")";
1691 }
1692
VisitGNUNullExpr(GNUNullExpr *)1693 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1694 OS << "__null";
1695 }
1696
VisitShuffleVectorExpr(ShuffleVectorExpr * Node)1697 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1698 OS << "__builtin_shufflevector(";
1699 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1700 if (i) OS << ", ";
1701 PrintExpr(Node->getExpr(i));
1702 }
1703 OS << ")";
1704 }
1705
VisitConvertVectorExpr(ConvertVectorExpr * Node)1706 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1707 OS << "__builtin_convertvector(";
1708 PrintExpr(Node->getSrcExpr());
1709 OS << ", ";
1710 Node->getType().print(OS, Policy);
1711 OS << ")";
1712 }
1713
VisitInitListExpr(InitListExpr * Node)1714 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1715 if (Node->getSyntacticForm()) {
1716 Visit(Node->getSyntacticForm());
1717 return;
1718 }
1719
1720 OS << "{";
1721 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1722 if (i) OS << ", ";
1723 if (Node->getInit(i))
1724 PrintExpr(Node->getInit(i));
1725 else
1726 OS << "{}";
1727 }
1728 OS << "}";
1729 }
1730
VisitArrayInitLoopExpr(ArrayInitLoopExpr * Node)1731 void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) {
1732 // There's no way to express this expression in any of our supported
1733 // languages, so just emit something terse and (hopefully) clear.
1734 OS << "{";
1735 PrintExpr(Node->getSubExpr());
1736 OS << "}";
1737 }
1738
VisitArrayInitIndexExpr(ArrayInitIndexExpr * Node)1739 void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) {
1740 OS << "*";
1741 }
1742
VisitParenListExpr(ParenListExpr * Node)1743 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1744 OS << "(";
1745 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1746 if (i) OS << ", ";
1747 PrintExpr(Node->getExpr(i));
1748 }
1749 OS << ")";
1750 }
1751
VisitDesignatedInitExpr(DesignatedInitExpr * Node)1752 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1753 bool NeedsEquals = true;
1754 for (const DesignatedInitExpr::Designator &D : Node->designators()) {
1755 if (D.isFieldDesignator()) {
1756 if (D.getDotLoc().isInvalid()) {
1757 if (const IdentifierInfo *II = D.getFieldName()) {
1758 OS << II->getName() << ":";
1759 NeedsEquals = false;
1760 }
1761 } else {
1762 OS << "." << D.getFieldName()->getName();
1763 }
1764 } else {
1765 OS << "[";
1766 if (D.isArrayDesignator()) {
1767 PrintExpr(Node->getArrayIndex(D));
1768 } else {
1769 PrintExpr(Node->getArrayRangeStart(D));
1770 OS << " ... ";
1771 PrintExpr(Node->getArrayRangeEnd(D));
1772 }
1773 OS << "]";
1774 }
1775 }
1776
1777 if (NeedsEquals)
1778 OS << " = ";
1779 else
1780 OS << " ";
1781 PrintExpr(Node->getInit());
1782 }
1783
VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr * Node)1784 void StmtPrinter::VisitDesignatedInitUpdateExpr(
1785 DesignatedInitUpdateExpr *Node) {
1786 OS << "{";
1787 OS << "/*base*/";
1788 PrintExpr(Node->getBase());
1789 OS << ", ";
1790
1791 OS << "/*updater*/";
1792 PrintExpr(Node->getUpdater());
1793 OS << "}";
1794 }
1795
VisitNoInitExpr(NoInitExpr * Node)1796 void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1797 OS << "/*no init*/";
1798 }
1799
VisitImplicitValueInitExpr(ImplicitValueInitExpr * Node)1800 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1801 if (Node->getType()->getAsCXXRecordDecl()) {
1802 OS << "/*implicit*/";
1803 Node->getType().print(OS, Policy);
1804 OS << "()";
1805 } else {
1806 OS << "/*implicit*/(";
1807 Node->getType().print(OS, Policy);
1808 OS << ')';
1809 if (Node->getType()->isRecordType())
1810 OS << "{}";
1811 else
1812 OS << 0;
1813 }
1814 }
1815
VisitVAArgExpr(VAArgExpr * Node)1816 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1817 OS << "__builtin_va_arg(";
1818 PrintExpr(Node->getSubExpr());
1819 OS << ", ";
1820 Node->getType().print(OS, Policy);
1821 OS << ")";
1822 }
1823
VisitPseudoObjectExpr(PseudoObjectExpr * Node)1824 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1825 PrintExpr(Node->getSyntacticForm());
1826 }
1827
VisitAtomicExpr(AtomicExpr * Node)1828 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1829 const char *Name = nullptr;
1830 switch (Node->getOp()) {
1831 #define BUILTIN(ID, TYPE, ATTRS)
1832 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1833 case AtomicExpr::AO ## ID: \
1834 Name = #ID "("; \
1835 break;
1836 #include "clang/Basic/Builtins.def"
1837 }
1838 OS << Name;
1839
1840 // AtomicExpr stores its subexpressions in a permuted order.
1841 PrintExpr(Node->getPtr());
1842 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1843 Node->getOp() != AtomicExpr::AO__atomic_load_n &&
1844 Node->getOp() != AtomicExpr::AO__scoped_atomic_load_n &&
1845 Node->getOp() != AtomicExpr::AO__opencl_atomic_load &&
1846 Node->getOp() != AtomicExpr::AO__hip_atomic_load) {
1847 OS << ", ";
1848 PrintExpr(Node->getVal1());
1849 }
1850 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1851 Node->isCmpXChg()) {
1852 OS << ", ";
1853 PrintExpr(Node->getVal2());
1854 }
1855 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1856 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1857 OS << ", ";
1858 PrintExpr(Node->getWeak());
1859 }
1860 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init &&
1861 Node->getOp() != AtomicExpr::AO__opencl_atomic_init) {
1862 OS << ", ";
1863 PrintExpr(Node->getOrder());
1864 }
1865 if (Node->isCmpXChg()) {
1866 OS << ", ";
1867 PrintExpr(Node->getOrderFail());
1868 }
1869 OS << ")";
1870 }
1871
1872 // C++
VisitCXXOperatorCallExpr(CXXOperatorCallExpr * Node)1873 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1874 OverloadedOperatorKind Kind = Node->getOperator();
1875 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1876 if (Node->getNumArgs() == 1) {
1877 OS << getOperatorSpelling(Kind) << ' ';
1878 PrintExpr(Node->getArg(0));
1879 } else {
1880 PrintExpr(Node->getArg(0));
1881 OS << ' ' << getOperatorSpelling(Kind);
1882 }
1883 } else if (Kind == OO_Arrow) {
1884 PrintExpr(Node->getArg(0));
1885 } else if (Kind == OO_Call || Kind == OO_Subscript) {
1886 PrintExpr(Node->getArg(0));
1887 OS << (Kind == OO_Call ? '(' : '[');
1888 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1889 if (ArgIdx > 1)
1890 OS << ", ";
1891 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1892 PrintExpr(Node->getArg(ArgIdx));
1893 }
1894 OS << (Kind == OO_Call ? ')' : ']');
1895 } else if (Node->getNumArgs() == 1) {
1896 OS << getOperatorSpelling(Kind) << ' ';
1897 PrintExpr(Node->getArg(0));
1898 } else if (Node->getNumArgs() == 2) {
1899 PrintExpr(Node->getArg(0));
1900 OS << ' ' << getOperatorSpelling(Kind) << ' ';
1901 PrintExpr(Node->getArg(1));
1902 } else {
1903 llvm_unreachable("unknown overloaded operator");
1904 }
1905 }
1906
VisitCXXMemberCallExpr(CXXMemberCallExpr * Node)1907 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1908 // If we have a conversion operator call only print the argument.
1909 CXXMethodDecl *MD = Node->getMethodDecl();
1910 if (MD && isa<CXXConversionDecl>(MD)) {
1911 PrintExpr(Node->getImplicitObjectArgument());
1912 return;
1913 }
1914 VisitCallExpr(cast<CallExpr>(Node));
1915 }
1916
VisitCUDAKernelCallExpr(CUDAKernelCallExpr * Node)1917 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1918 PrintExpr(Node->getCallee());
1919 OS << "<<<";
1920 PrintCallArgs(Node->getConfig());
1921 OS << ">>>(";
1922 PrintCallArgs(Node);
1923 OS << ")";
1924 }
1925
VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator * Node)1926 void StmtPrinter::VisitCXXRewrittenBinaryOperator(
1927 CXXRewrittenBinaryOperator *Node) {
1928 CXXRewrittenBinaryOperator::DecomposedForm Decomposed =
1929 Node->getDecomposedForm();
1930 PrintExpr(const_cast<Expr*>(Decomposed.LHS));
1931 OS << ' ' << BinaryOperator::getOpcodeStr(Decomposed.Opcode) << ' ';
1932 PrintExpr(const_cast<Expr*>(Decomposed.RHS));
1933 }
1934
VisitCXXNamedCastExpr(CXXNamedCastExpr * Node)1935 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1936 OS << Node->getCastName() << '<';
1937 Node->getTypeAsWritten().print(OS, Policy);
1938 OS << ">(";
1939 PrintExpr(Node->getSubExpr());
1940 OS << ")";
1941 }
1942
VisitCXXStaticCastExpr(CXXStaticCastExpr * Node)1943 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1944 VisitCXXNamedCastExpr(Node);
1945 }
1946
VisitCXXDynamicCastExpr(CXXDynamicCastExpr * Node)1947 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1948 VisitCXXNamedCastExpr(Node);
1949 }
1950
VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr * Node)1951 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1952 VisitCXXNamedCastExpr(Node);
1953 }
1954
VisitCXXConstCastExpr(CXXConstCastExpr * Node)1955 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1956 VisitCXXNamedCastExpr(Node);
1957 }
1958
VisitBuiltinBitCastExpr(BuiltinBitCastExpr * Node)1959 void StmtPrinter::VisitBuiltinBitCastExpr(BuiltinBitCastExpr *Node) {
1960 OS << "__builtin_bit_cast(";
1961 Node->getTypeInfoAsWritten()->getType().print(OS, Policy);
1962 OS << ", ";
1963 PrintExpr(Node->getSubExpr());
1964 OS << ")";
1965 }
1966
VisitCXXAddrspaceCastExpr(CXXAddrspaceCastExpr * Node)1967 void StmtPrinter::VisitCXXAddrspaceCastExpr(CXXAddrspaceCastExpr *Node) {
1968 VisitCXXNamedCastExpr(Node);
1969 }
1970
VisitCXXTypeidExpr(CXXTypeidExpr * Node)1971 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1972 OS << "typeid(";
1973 if (Node->isTypeOperand()) {
1974 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1975 } else {
1976 PrintExpr(Node->getExprOperand());
1977 }
1978 OS << ")";
1979 }
1980
VisitCXXUuidofExpr(CXXUuidofExpr * Node)1981 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1982 OS << "__uuidof(";
1983 if (Node->isTypeOperand()) {
1984 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1985 } else {
1986 PrintExpr(Node->getExprOperand());
1987 }
1988 OS << ")";
1989 }
1990
VisitMSPropertyRefExpr(MSPropertyRefExpr * Node)1991 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1992 PrintExpr(Node->getBaseExpr());
1993 if (Node->isArrow())
1994 OS << "->";
1995 else
1996 OS << ".";
1997 if (NestedNameSpecifier *Qualifier =
1998 Node->getQualifierLoc().getNestedNameSpecifier())
1999 Qualifier->print(OS, Policy);
2000 OS << Node->getPropertyDecl()->getDeclName();
2001 }
2002
VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr * Node)2003 void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
2004 PrintExpr(Node->getBase());
2005 OS << "[";
2006 PrintExpr(Node->getIdx());
2007 OS << "]";
2008 }
2009
VisitUserDefinedLiteral(UserDefinedLiteral * Node)2010 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
2011 switch (Node->getLiteralOperatorKind()) {
2012 case UserDefinedLiteral::LOK_Raw:
2013 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
2014 break;
2015 case UserDefinedLiteral::LOK_Template: {
2016 const auto *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
2017 const TemplateArgumentList *Args =
2018 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
2019 assert(Args);
2020
2021 if (Args->size() != 1 || Args->get(0).getKind() != TemplateArgument::Pack) {
2022 const TemplateParameterList *TPL = nullptr;
2023 if (!DRE->hadMultipleCandidates())
2024 if (const auto *TD = dyn_cast<TemplateDecl>(DRE->getDecl()))
2025 TPL = TD->getTemplateParameters();
2026 OS << "operator\"\"" << Node->getUDSuffix()->getName();
2027 printTemplateArgumentList(OS, Args->asArray(), Policy, TPL);
2028 OS << "()";
2029 return;
2030 }
2031
2032 const TemplateArgument &Pack = Args->get(0);
2033 for (const auto &P : Pack.pack_elements()) {
2034 char C = (char)P.getAsIntegral().getZExtValue();
2035 OS << C;
2036 }
2037 break;
2038 }
2039 case UserDefinedLiteral::LOK_Integer: {
2040 // Print integer literal without suffix.
2041 const auto *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
2042 OS << toString(Int->getValue(), 10, /*isSigned*/false);
2043 break;
2044 }
2045 case UserDefinedLiteral::LOK_Floating: {
2046 // Print floating literal without suffix.
2047 auto *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
2048 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
2049 break;
2050 }
2051 case UserDefinedLiteral::LOK_String:
2052 case UserDefinedLiteral::LOK_Character:
2053 PrintExpr(Node->getCookedLiteral());
2054 break;
2055 }
2056 OS << Node->getUDSuffix()->getName();
2057 }
2058
VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr * Node)2059 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
2060 OS << (Node->getValue() ? "true" : "false");
2061 }
2062
VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr * Node)2063 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
2064 OS << "nullptr";
2065 }
2066
VisitCXXThisExpr(CXXThisExpr * Node)2067 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
2068 OS << "this";
2069 }
2070
VisitCXXThrowExpr(CXXThrowExpr * Node)2071 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
2072 if (!Node->getSubExpr())
2073 OS << "throw";
2074 else {
2075 OS << "throw ";
2076 PrintExpr(Node->getSubExpr());
2077 }
2078 }
2079
VisitCXXDefaultArgExpr(CXXDefaultArgExpr * Node)2080 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
2081 // Nothing to print: we picked up the default argument.
2082 }
2083
VisitCXXDefaultInitExpr(CXXDefaultInitExpr * Node)2084 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
2085 // Nothing to print: we picked up the default initializer.
2086 }
2087
VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr * Node)2088 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
2089 auto TargetType = Node->getType();
2090 auto *Auto = TargetType->getContainedDeducedType();
2091 bool Bare = Auto && Auto->isDeduced();
2092
2093 // Parenthesize deduced casts.
2094 if (Bare)
2095 OS << '(';
2096 TargetType.print(OS, Policy);
2097 if (Bare)
2098 OS << ')';
2099
2100 // No extra braces surrounding the inner construct.
2101 if (!Node->isListInitialization())
2102 OS << '(';
2103 PrintExpr(Node->getSubExpr());
2104 if (!Node->isListInitialization())
2105 OS << ')';
2106 }
2107
VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr * Node)2108 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
2109 PrintExpr(Node->getSubExpr());
2110 }
2111
VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr * Node)2112 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
2113 Node->getType().print(OS, Policy);
2114 if (Node->isStdInitListInitialization())
2115 /* Nothing to do; braces are part of creating the std::initializer_list. */;
2116 else if (Node->isListInitialization())
2117 OS << "{";
2118 else
2119 OS << "(";
2120 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
2121 ArgEnd = Node->arg_end();
2122 Arg != ArgEnd; ++Arg) {
2123 if ((*Arg)->isDefaultArgument())
2124 break;
2125 if (Arg != Node->arg_begin())
2126 OS << ", ";
2127 PrintExpr(*Arg);
2128 }
2129 if (Node->isStdInitListInitialization())
2130 /* See above. */;
2131 else if (Node->isListInitialization())
2132 OS << "}";
2133 else
2134 OS << ")";
2135 }
2136
VisitLambdaExpr(LambdaExpr * Node)2137 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
2138 OS << '[';
2139 bool NeedComma = false;
2140 switch (Node->getCaptureDefault()) {
2141 case LCD_None:
2142 break;
2143
2144 case LCD_ByCopy:
2145 OS << '=';
2146 NeedComma = true;
2147 break;
2148
2149 case LCD_ByRef:
2150 OS << '&';
2151 NeedComma = true;
2152 break;
2153 }
2154 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
2155 CEnd = Node->explicit_capture_end();
2156 C != CEnd;
2157 ++C) {
2158 if (C->capturesVLAType())
2159 continue;
2160
2161 if (NeedComma)
2162 OS << ", ";
2163 NeedComma = true;
2164
2165 switch (C->getCaptureKind()) {
2166 case LCK_This:
2167 OS << "this";
2168 break;
2169
2170 case LCK_StarThis:
2171 OS << "*this";
2172 break;
2173
2174 case LCK_ByRef:
2175 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
2176 OS << '&';
2177 OS << C->getCapturedVar()->getName();
2178 break;
2179
2180 case LCK_ByCopy:
2181 OS << C->getCapturedVar()->getName();
2182 break;
2183
2184 case LCK_VLAType:
2185 llvm_unreachable("VLA type in explicit captures.");
2186 }
2187
2188 if (C->isPackExpansion())
2189 OS << "...";
2190
2191 if (Node->isInitCapture(C)) {
2192 // Init captures are always VarDecl.
2193 auto *D = cast<VarDecl>(C->getCapturedVar());
2194
2195 llvm::StringRef Pre;
2196 llvm::StringRef Post;
2197 if (D->getInitStyle() == VarDecl::CallInit &&
2198 !isa<ParenListExpr>(D->getInit())) {
2199 Pre = "(";
2200 Post = ")";
2201 } else if (D->getInitStyle() == VarDecl::CInit) {
2202 Pre = " = ";
2203 }
2204
2205 OS << Pre;
2206 PrintExpr(D->getInit());
2207 OS << Post;
2208 }
2209 }
2210 OS << ']';
2211
2212 if (!Node->getExplicitTemplateParameters().empty()) {
2213 Node->getTemplateParameterList()->print(
2214 OS, Node->getLambdaClass()->getASTContext(),
2215 /*OmitTemplateKW*/true);
2216 }
2217
2218 if (Node->hasExplicitParameters()) {
2219 OS << '(';
2220 CXXMethodDecl *Method = Node->getCallOperator();
2221 NeedComma = false;
2222 for (const auto *P : Method->parameters()) {
2223 if (NeedComma) {
2224 OS << ", ";
2225 } else {
2226 NeedComma = true;
2227 }
2228 std::string ParamStr =
2229 (Policy.CleanUglifiedParameters && P->getIdentifier())
2230 ? P->getIdentifier()->deuglifiedName().str()
2231 : P->getNameAsString();
2232 P->getOriginalType().print(OS, Policy, ParamStr);
2233 }
2234 if (Method->isVariadic()) {
2235 if (NeedComma)
2236 OS << ", ";
2237 OS << "...";
2238 }
2239 OS << ')';
2240
2241 if (Node->isMutable())
2242 OS << " mutable";
2243
2244 auto *Proto = Method->getType()->castAs<FunctionProtoType>();
2245 Proto->printExceptionSpecification(OS, Policy);
2246
2247 // FIXME: Attributes
2248
2249 // Print the trailing return type if it was specified in the source.
2250 if (Node->hasExplicitResultType()) {
2251 OS << " -> ";
2252 Proto->getReturnType().print(OS, Policy);
2253 }
2254 }
2255
2256 // Print the body.
2257 OS << ' ';
2258 if (Policy.TerseOutput)
2259 OS << "{}";
2260 else
2261 PrintRawCompoundStmt(Node->getCompoundStmtBody());
2262 }
2263
VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr * Node)2264 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
2265 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
2266 TSInfo->getType().print(OS, Policy);
2267 else
2268 Node->getType().print(OS, Policy);
2269 OS << "()";
2270 }
2271
VisitCXXNewExpr(CXXNewExpr * E)2272 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
2273 if (E->isGlobalNew())
2274 OS << "::";
2275 OS << "new ";
2276 unsigned NumPlace = E->getNumPlacementArgs();
2277 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
2278 OS << "(";
2279 PrintExpr(E->getPlacementArg(0));
2280 for (unsigned i = 1; i < NumPlace; ++i) {
2281 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
2282 break;
2283 OS << ", ";
2284 PrintExpr(E->getPlacementArg(i));
2285 }
2286 OS << ") ";
2287 }
2288 if (E->isParenTypeId())
2289 OS << "(";
2290 std::string TypeS;
2291 if (E->isArray()) {
2292 llvm::raw_string_ostream s(TypeS);
2293 s << '[';
2294 if (std::optional<Expr *> Size = E->getArraySize())
2295 (*Size)->printPretty(s, Helper, Policy);
2296 s << ']';
2297 }
2298 E->getAllocatedType().print(OS, Policy, TypeS);
2299 if (E->isParenTypeId())
2300 OS << ")";
2301
2302 CXXNewInitializationStyle InitStyle = E->getInitializationStyle();
2303 if (InitStyle != CXXNewInitializationStyle::None) {
2304 bool Bare = InitStyle == CXXNewInitializationStyle::Parens &&
2305 !isa<ParenListExpr>(E->getInitializer());
2306 if (Bare)
2307 OS << "(";
2308 PrintExpr(E->getInitializer());
2309 if (Bare)
2310 OS << ")";
2311 }
2312 }
2313
VisitCXXDeleteExpr(CXXDeleteExpr * E)2314 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2315 if (E->isGlobalDelete())
2316 OS << "::";
2317 OS << "delete ";
2318 if (E->isArrayForm())
2319 OS << "[] ";
2320 PrintExpr(E->getArgument());
2321 }
2322
VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr * E)2323 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
2324 PrintExpr(E->getBase());
2325 if (E->isArrow())
2326 OS << "->";
2327 else
2328 OS << '.';
2329 if (E->getQualifier())
2330 E->getQualifier()->print(OS, Policy);
2331 OS << "~";
2332
2333 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
2334 OS << II->getName();
2335 else
2336 E->getDestroyedType().print(OS, Policy);
2337 }
2338
VisitCXXConstructExpr(CXXConstructExpr * E)2339 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
2340 if (E->isListInitialization() && !E->isStdInitListInitialization())
2341 OS << "{";
2342
2343 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
2344 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
2345 // Don't print any defaulted arguments
2346 break;
2347 }
2348
2349 if (i) OS << ", ";
2350 PrintExpr(E->getArg(i));
2351 }
2352
2353 if (E->isListInitialization() && !E->isStdInitListInitialization())
2354 OS << "}";
2355 }
2356
VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr * E)2357 void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) {
2358 // Parens are printed by the surrounding context.
2359 OS << "<forwarded>";
2360 }
2361
VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr * E)2362 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
2363 PrintExpr(E->getSubExpr());
2364 }
2365
VisitExprWithCleanups(ExprWithCleanups * E)2366 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
2367 // Just forward to the subexpression.
2368 PrintExpr(E->getSubExpr());
2369 }
2370
VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr * Node)2371 void StmtPrinter::VisitCXXUnresolvedConstructExpr(
2372 CXXUnresolvedConstructExpr *Node) {
2373 Node->getTypeAsWritten().print(OS, Policy);
2374 if (!Node->isListInitialization())
2375 OS << '(';
2376 for (auto Arg = Node->arg_begin(), ArgEnd = Node->arg_end(); Arg != ArgEnd;
2377 ++Arg) {
2378 if (Arg != Node->arg_begin())
2379 OS << ", ";
2380 PrintExpr(*Arg);
2381 }
2382 if (!Node->isListInitialization())
2383 OS << ')';
2384 }
2385
VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr * Node)2386 void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2387 CXXDependentScopeMemberExpr *Node) {
2388 if (!Node->isImplicitAccess()) {
2389 PrintExpr(Node->getBase());
2390 OS << (Node->isArrow() ? "->" : ".");
2391 }
2392 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2393 Qualifier->print(OS, Policy);
2394 if (Node->hasTemplateKeyword())
2395 OS << "template ";
2396 OS << Node->getMemberNameInfo();
2397 if (Node->hasExplicitTemplateArgs())
2398 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
2399 }
2400
VisitUnresolvedMemberExpr(UnresolvedMemberExpr * Node)2401 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
2402 if (!Node->isImplicitAccess()) {
2403 PrintExpr(Node->getBase());
2404 OS << (Node->isArrow() ? "->" : ".");
2405 }
2406 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2407 Qualifier->print(OS, Policy);
2408 if (Node->hasTemplateKeyword())
2409 OS << "template ";
2410 OS << Node->getMemberNameInfo();
2411 if (Node->hasExplicitTemplateArgs())
2412 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
2413 }
2414
VisitTypeTraitExpr(TypeTraitExpr * E)2415 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2416 OS << getTraitSpelling(E->getTrait()) << "(";
2417 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2418 if (I > 0)
2419 OS << ", ";
2420 E->getArg(I)->getType().print(OS, Policy);
2421 }
2422 OS << ")";
2423 }
2424
VisitArrayTypeTraitExpr(ArrayTypeTraitExpr * E)2425 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
2426 OS << getTraitSpelling(E->getTrait()) << '(';
2427 E->getQueriedType().print(OS, Policy);
2428 OS << ')';
2429 }
2430
VisitExpressionTraitExpr(ExpressionTraitExpr * E)2431 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
2432 OS << getTraitSpelling(E->getTrait()) << '(';
2433 PrintExpr(E->getQueriedExpression());
2434 OS << ')';
2435 }
2436
VisitCXXNoexceptExpr(CXXNoexceptExpr * E)2437 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2438 OS << "noexcept(";
2439 PrintExpr(E->getOperand());
2440 OS << ")";
2441 }
2442
VisitPackExpansionExpr(PackExpansionExpr * E)2443 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
2444 PrintExpr(E->getPattern());
2445 OS << "...";
2446 }
2447
VisitSizeOfPackExpr(SizeOfPackExpr * E)2448 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2449 OS << "sizeof...(" << *E->getPack() << ")";
2450 }
2451
VisitSubstNonTypeTemplateParmPackExpr(SubstNonTypeTemplateParmPackExpr * Node)2452 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2453 SubstNonTypeTemplateParmPackExpr *Node) {
2454 OS << *Node->getParameterPack();
2455 }
2456
VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr * Node)2457 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2458 SubstNonTypeTemplateParmExpr *Node) {
2459 Visit(Node->getReplacement());
2460 }
2461
VisitFunctionParmPackExpr(FunctionParmPackExpr * E)2462 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2463 OS << *E->getParameterPack();
2464 }
2465
VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr * Node)2466 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2467 PrintExpr(Node->getSubExpr());
2468 }
2469
VisitCXXFoldExpr(CXXFoldExpr * E)2470 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2471 OS << "(";
2472 if (E->getLHS()) {
2473 PrintExpr(E->getLHS());
2474 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2475 }
2476 OS << "...";
2477 if (E->getRHS()) {
2478 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2479 PrintExpr(E->getRHS());
2480 }
2481 OS << ")";
2482 }
2483
VisitCXXParenListInitExpr(CXXParenListInitExpr * Node)2484 void StmtPrinter::VisitCXXParenListInitExpr(CXXParenListInitExpr *Node) {
2485 OS << "(";
2486 llvm::interleaveComma(Node->getInitExprs(), OS,
2487 [&](Expr *E) { PrintExpr(E); });
2488 OS << ")";
2489 }
2490
VisitConceptSpecializationExpr(ConceptSpecializationExpr * E)2491 void StmtPrinter::VisitConceptSpecializationExpr(ConceptSpecializationExpr *E) {
2492 NestedNameSpecifierLoc NNS = E->getNestedNameSpecifierLoc();
2493 if (NNS)
2494 NNS.getNestedNameSpecifier()->print(OS, Policy);
2495 if (E->getTemplateKWLoc().isValid())
2496 OS << "template ";
2497 OS << E->getFoundDecl()->getName();
2498 printTemplateArgumentList(OS, E->getTemplateArgsAsWritten()->arguments(),
2499 Policy,
2500 E->getNamedConcept()->getTemplateParameters());
2501 }
2502
VisitRequiresExpr(RequiresExpr * E)2503 void StmtPrinter::VisitRequiresExpr(RequiresExpr *E) {
2504 OS << "requires ";
2505 auto LocalParameters = E->getLocalParameters();
2506 if (!LocalParameters.empty()) {
2507 OS << "(";
2508 for (ParmVarDecl *LocalParam : LocalParameters) {
2509 PrintRawDecl(LocalParam);
2510 if (LocalParam != LocalParameters.back())
2511 OS << ", ";
2512 }
2513
2514 OS << ") ";
2515 }
2516 OS << "{ ";
2517 auto Requirements = E->getRequirements();
2518 for (concepts::Requirement *Req : Requirements) {
2519 if (auto *TypeReq = dyn_cast<concepts::TypeRequirement>(Req)) {
2520 if (TypeReq->isSubstitutionFailure())
2521 OS << "<<error-type>>";
2522 else
2523 TypeReq->getType()->getType().print(OS, Policy);
2524 } else if (auto *ExprReq = dyn_cast<concepts::ExprRequirement>(Req)) {
2525 if (ExprReq->isCompound())
2526 OS << "{ ";
2527 if (ExprReq->isExprSubstitutionFailure())
2528 OS << "<<error-expression>>";
2529 else
2530 PrintExpr(ExprReq->getExpr());
2531 if (ExprReq->isCompound()) {
2532 OS << " }";
2533 if (ExprReq->getNoexceptLoc().isValid())
2534 OS << " noexcept";
2535 const auto &RetReq = ExprReq->getReturnTypeRequirement();
2536 if (!RetReq.isEmpty()) {
2537 OS << " -> ";
2538 if (RetReq.isSubstitutionFailure())
2539 OS << "<<error-type>>";
2540 else if (RetReq.isTypeConstraint())
2541 RetReq.getTypeConstraint()->print(OS, Policy);
2542 }
2543 }
2544 } else {
2545 auto *NestedReq = cast<concepts::NestedRequirement>(Req);
2546 OS << "requires ";
2547 if (NestedReq->hasInvalidConstraint())
2548 OS << "<<error-expression>>";
2549 else
2550 PrintExpr(NestedReq->getConstraintExpr());
2551 }
2552 OS << "; ";
2553 }
2554 OS << "}";
2555 }
2556
2557 // C++ Coroutines
2558
VisitCoroutineBodyStmt(CoroutineBodyStmt * S)2559 void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
2560 Visit(S->getBody());
2561 }
2562
VisitCoreturnStmt(CoreturnStmt * S)2563 void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
2564 OS << "co_return";
2565 if (S->getOperand()) {
2566 OS << " ";
2567 Visit(S->getOperand());
2568 }
2569 OS << ";";
2570 }
2571
VisitCoawaitExpr(CoawaitExpr * S)2572 void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
2573 OS << "co_await ";
2574 PrintExpr(S->getOperand());
2575 }
2576
VisitDependentCoawaitExpr(DependentCoawaitExpr * S)2577 void StmtPrinter::VisitDependentCoawaitExpr(DependentCoawaitExpr *S) {
2578 OS << "co_await ";
2579 PrintExpr(S->getOperand());
2580 }
2581
VisitCoyieldExpr(CoyieldExpr * S)2582 void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
2583 OS << "co_yield ";
2584 PrintExpr(S->getOperand());
2585 }
2586
2587 // Obj-C
2588
VisitObjCStringLiteral(ObjCStringLiteral * Node)2589 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2590 OS << "@";
2591 VisitStringLiteral(Node->getString());
2592 }
2593
VisitObjCBoxedExpr(ObjCBoxedExpr * E)2594 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
2595 OS << "@";
2596 Visit(E->getSubExpr());
2597 }
2598
VisitObjCArrayLiteral(ObjCArrayLiteral * E)2599 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2600 OS << "@[ ";
2601 ObjCArrayLiteral::child_range Ch = E->children();
2602 for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2603 if (I != Ch.begin())
2604 OS << ", ";
2605 Visit(*I);
2606 }
2607 OS << " ]";
2608 }
2609
VisitObjCDictionaryLiteral(ObjCDictionaryLiteral * E)2610 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2611 OS << "@{ ";
2612 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2613 if (I > 0)
2614 OS << ", ";
2615
2616 ObjCDictionaryElement Element = E->getKeyValueElement(I);
2617 Visit(Element.Key);
2618 OS << " : ";
2619 Visit(Element.Value);
2620 if (Element.isPackExpansion())
2621 OS << "...";
2622 }
2623 OS << " }";
2624 }
2625
VisitObjCEncodeExpr(ObjCEncodeExpr * Node)2626 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
2627 OS << "@encode(";
2628 Node->getEncodedType().print(OS, Policy);
2629 OS << ')';
2630 }
2631
VisitObjCSelectorExpr(ObjCSelectorExpr * Node)2632 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
2633 OS << "@selector(";
2634 Node->getSelector().print(OS);
2635 OS << ')';
2636 }
2637
VisitObjCProtocolExpr(ObjCProtocolExpr * Node)2638 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
2639 OS << "@protocol(" << *Node->getProtocol() << ')';
2640 }
2641
VisitObjCMessageExpr(ObjCMessageExpr * Mess)2642 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2643 OS << "[";
2644 switch (Mess->getReceiverKind()) {
2645 case ObjCMessageExpr::Instance:
2646 PrintExpr(Mess->getInstanceReceiver());
2647 break;
2648
2649 case ObjCMessageExpr::Class:
2650 Mess->getClassReceiver().print(OS, Policy);
2651 break;
2652
2653 case ObjCMessageExpr::SuperInstance:
2654 case ObjCMessageExpr::SuperClass:
2655 OS << "Super";
2656 break;
2657 }
2658
2659 OS << ' ';
2660 Selector selector = Mess->getSelector();
2661 if (selector.isUnarySelector()) {
2662 OS << selector.getNameForSlot(0);
2663 } else {
2664 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
2665 if (i < selector.getNumArgs()) {
2666 if (i > 0) OS << ' ';
2667 if (selector.getIdentifierInfoForSlot(i))
2668 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
2669 else
2670 OS << ":";
2671 }
2672 else OS << ", "; // Handle variadic methods.
2673
2674 PrintExpr(Mess->getArg(i));
2675 }
2676 }
2677 OS << "]";
2678 }
2679
VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr * Node)2680 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2681 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2682 }
2683
2684 void
VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr * E)2685 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2686 PrintExpr(E->getSubExpr());
2687 }
2688
2689 void
VisitObjCBridgedCastExpr(ObjCBridgedCastExpr * E)2690 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
2691 OS << '(' << E->getBridgeKindName();
2692 E->getType().print(OS, Policy);
2693 OS << ')';
2694 PrintExpr(E->getSubExpr());
2695 }
2696
VisitBlockExpr(BlockExpr * Node)2697 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
2698 BlockDecl *BD = Node->getBlockDecl();
2699 OS << "^";
2700
2701 const FunctionType *AFT = Node->getFunctionType();
2702
2703 if (isa<FunctionNoProtoType>(AFT)) {
2704 OS << "()";
2705 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
2706 OS << '(';
2707 for (BlockDecl::param_iterator AI = BD->param_begin(),
2708 E = BD->param_end(); AI != E; ++AI) {
2709 if (AI != BD->param_begin()) OS << ", ";
2710 std::string ParamStr = (*AI)->getNameAsString();
2711 (*AI)->getType().print(OS, Policy, ParamStr);
2712 }
2713
2714 const auto *FT = cast<FunctionProtoType>(AFT);
2715 if (FT->isVariadic()) {
2716 if (!BD->param_empty()) OS << ", ";
2717 OS << "...";
2718 }
2719 OS << ')';
2720 }
2721 OS << "{ }";
2722 }
2723
VisitOpaqueValueExpr(OpaqueValueExpr * Node)2724 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2725 PrintExpr(Node->getSourceExpr());
2726 }
2727
VisitTypoExpr(TypoExpr * Node)2728 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2729 // TODO: Print something reasonable for a TypoExpr, if necessary.
2730 llvm_unreachable("Cannot print TypoExpr nodes");
2731 }
2732
VisitRecoveryExpr(RecoveryExpr * Node)2733 void StmtPrinter::VisitRecoveryExpr(RecoveryExpr *Node) {
2734 OS << "<recovery-expr>(";
2735 const char *Sep = "";
2736 for (Expr *E : Node->subExpressions()) {
2737 OS << Sep;
2738 PrintExpr(E);
2739 Sep = ", ";
2740 }
2741 OS << ')';
2742 }
2743
VisitAsTypeExpr(AsTypeExpr * Node)2744 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2745 OS << "__builtin_astype(";
2746 PrintExpr(Node->getSrcExpr());
2747 OS << ", ";
2748 Node->getType().print(OS, Policy);
2749 OS << ")";
2750 }
2751
2752 //===----------------------------------------------------------------------===//
2753 // Stmt method implementations
2754 //===----------------------------------------------------------------------===//
2755
dumpPretty(const ASTContext & Context) const2756 void Stmt::dumpPretty(const ASTContext &Context) const {
2757 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
2758 }
2759
printPretty(raw_ostream & Out,PrinterHelper * Helper,const PrintingPolicy & Policy,unsigned Indentation,StringRef NL,const ASTContext * Context) const2760 void Stmt::printPretty(raw_ostream &Out, PrinterHelper *Helper,
2761 const PrintingPolicy &Policy, unsigned Indentation,
2762 StringRef NL, const ASTContext *Context) const {
2763 StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context);
2764 P.Visit(const_cast<Stmt *>(this));
2765 }
2766
printPrettyControlled(raw_ostream & Out,PrinterHelper * Helper,const PrintingPolicy & Policy,unsigned Indentation,StringRef NL,const ASTContext * Context) const2767 void Stmt::printPrettyControlled(raw_ostream &Out, PrinterHelper *Helper,
2768 const PrintingPolicy &Policy,
2769 unsigned Indentation, StringRef NL,
2770 const ASTContext *Context) const {
2771 StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context);
2772 P.PrintControlledStmt(const_cast<Stmt *>(this));
2773 }
2774
printJson(raw_ostream & Out,PrinterHelper * Helper,const PrintingPolicy & Policy,bool AddQuotes) const2775 void Stmt::printJson(raw_ostream &Out, PrinterHelper *Helper,
2776 const PrintingPolicy &Policy, bool AddQuotes) const {
2777 std::string Buf;
2778 llvm::raw_string_ostream TempOut(Buf);
2779
2780 printPretty(TempOut, Helper, Policy);
2781
2782 Out << JsonFormat(TempOut.str(), AddQuotes);
2783 }
2784
2785 //===----------------------------------------------------------------------===//
2786 // PrinterHelper
2787 //===----------------------------------------------------------------------===//
2788
2789 // Implement virtual destructor.
2790 PrinterHelper::~PrinterHelper() = default;
2791