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