1 //===- StmtPrinter.cpp - Printing implementation for Stmt ASTs ------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which
10 // pretty print the AST back out to C code.
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/Attr.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclBase.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/DeclObjC.h"
20 #include "clang/AST/DeclOpenMP.h"
21 #include "clang/AST/DeclTemplate.h"
22 #include "clang/AST/Expr.h"
23 #include "clang/AST/ExprCXX.h"
24 #include "clang/AST/ExprObjC.h"
25 #include "clang/AST/ExprOpenMP.h"
26 #include "clang/AST/NestedNameSpecifier.h"
27 #include "clang/AST/OpenMPClause.h"
28 #include "clang/AST/PrettyPrinter.h"
29 #include "clang/AST/Stmt.h"
30 #include "clang/AST/StmtCXX.h"
31 #include "clang/AST/StmtObjC.h"
32 #include "clang/AST/StmtOpenMP.h"
33 #include "clang/AST/StmtVisitor.h"
34 #include "clang/AST/TemplateBase.h"
35 #include "clang/AST/Type.h"
36 #include "clang/Basic/CharInfo.h"
37 #include "clang/Basic/ExpressionTraits.h"
38 #include "clang/Basic/IdentifierTable.h"
39 #include "clang/Basic/JsonSupport.h"
40 #include "clang/Basic/LLVM.h"
41 #include "clang/Basic/Lambda.h"
42 #include "clang/Basic/OpenMPKinds.h"
43 #include "clang/Basic/OperatorKinds.h"
44 #include "clang/Basic/SourceLocation.h"
45 #include "clang/Basic/TypeTraits.h"
46 #include "clang/Lex/Lexer.h"
47 #include "llvm/ADT/ArrayRef.h"
48 #include "llvm/ADT/SmallString.h"
49 #include "llvm/ADT/SmallVector.h"
50 #include "llvm/ADT/StringExtras.h"
51 #include "llvm/ADT/StringRef.h"
52 #include "llvm/Support/Casting.h"
53 #include "llvm/Support/Compiler.h"
54 #include "llvm/Support/ErrorHandling.h"
55 #include "llvm/Support/raw_ostream.h"
60 using namespace clang
;
62 //===----------------------------------------------------------------------===//
63 // StmtPrinter Visitor
64 //===----------------------------------------------------------------------===//
68 class StmtPrinter
: public StmtVisitor
<StmtPrinter
> {
71 PrinterHelper
* Helper
;
72 PrintingPolicy Policy
;
74 const ASTContext
*Context
;
77 StmtPrinter(raw_ostream
&os
, PrinterHelper
*helper
,
78 const PrintingPolicy
&Policy
, unsigned Indentation
= 0,
79 StringRef NL
= "\n", const ASTContext
*Context
= nullptr)
80 : OS(os
), IndentLevel(Indentation
), Helper(helper
), Policy(Policy
),
81 NL(NL
), Context(Context
) {}
83 void PrintStmt(Stmt
*S
) { PrintStmt(S
, Policy
.Indentation
); }
85 void PrintStmt(Stmt
*S
, int SubIndent
) {
86 IndentLevel
+= SubIndent
;
87 if (S
&& isa
<Expr
>(S
)) {
88 // If this is an expr used in a stmt context, indent and newline it.
95 Indent() << "<<<NULL STATEMENT>>>" << NL
;
97 IndentLevel
-= SubIndent
;
100 void PrintInitStmt(Stmt
*S
, unsigned PrefixWidth
) {
101 // FIXME: Cope better with odd prefix widths.
102 IndentLevel
+= (PrefixWidth
+ 1) / 2;
103 if (auto *DS
= dyn_cast
<DeclStmt
>(S
))
104 PrintRawDeclStmt(DS
);
106 PrintExpr(cast
<Expr
>(S
));
108 IndentLevel
-= (PrefixWidth
+ 1) / 2;
111 void PrintControlledStmt(Stmt
*S
) {
112 if (auto *CS
= dyn_cast
<CompoundStmt
>(S
)) {
114 PrintRawCompoundStmt(CS
);
122 void PrintRawCompoundStmt(CompoundStmt
*S
);
123 void PrintRawDecl(Decl
*D
);
124 void PrintRawDeclStmt(const DeclStmt
*S
);
125 void PrintRawIfStmt(IfStmt
*If
);
126 void PrintRawCXXCatchStmt(CXXCatchStmt
*Catch
);
127 void PrintCallArgs(CallExpr
*E
);
128 void PrintRawSEHExceptHandler(SEHExceptStmt
*S
);
129 void PrintRawSEHFinallyStmt(SEHFinallyStmt
*S
);
130 void PrintOMPExecutableDirective(OMPExecutableDirective
*S
,
131 bool ForceNoStmt
= false);
132 void PrintFPPragmas(CompoundStmt
*S
);
134 void PrintExpr(Expr
*E
) {
141 raw_ostream
&Indent(int Delta
= 0) {
142 for (int i
= 0, e
= IndentLevel
+Delta
; i
< e
; ++i
)
147 void Visit(Stmt
* S
) {
148 if (Helper
&& Helper
->handledStmt(S
,OS
))
150 else StmtVisitor
<StmtPrinter
>::Visit(S
);
153 void VisitStmt(Stmt
*Node
) LLVM_ATTRIBUTE_UNUSED
{
154 Indent() << "<<unknown stmt type>>" << NL
;
157 void VisitExpr(Expr
*Node
) LLVM_ATTRIBUTE_UNUSED
{
158 OS
<< "<<unknown expr type>>";
161 void VisitCXXNamedCastExpr(CXXNamedCastExpr
*Node
);
163 #define ABSTRACT_STMT(CLASS)
164 #define STMT(CLASS, PARENT) \
165 void Visit##CLASS(CLASS *Node);
166 #include "clang/AST/StmtNodes.inc"
171 //===----------------------------------------------------------------------===//
172 // Stmt printing methods.
173 //===----------------------------------------------------------------------===//
175 /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
176 /// with no newline after the }.
177 void StmtPrinter::PrintRawCompoundStmt(CompoundStmt
*Node
) {
178 assert(Node
&& "Compound statement cannot be null");
180 PrintFPPragmas(Node
);
181 for (auto *I
: Node
->body())
187 void StmtPrinter::PrintFPPragmas(CompoundStmt
*S
) {
188 if (!S
->hasStoredFPFeatures())
190 FPOptionsOverride FPO
= S
->getStoredFPFeatures();
191 bool FEnvAccess
= false;
192 if (FPO
.hasAllowFEnvAccessOverride()) {
193 FEnvAccess
= FPO
.getAllowFEnvAccessOverride();
194 Indent() << "#pragma STDC FENV_ACCESS " << (FEnvAccess
? "ON" : "OFF")
197 if (FPO
.hasSpecifiedExceptionModeOverride()) {
198 LangOptions::FPExceptionModeKind EM
=
199 FPO
.getSpecifiedExceptionModeOverride();
200 if (!FEnvAccess
|| EM
!= LangOptions::FPE_Strict
) {
201 Indent() << "#pragma clang fp exceptions(";
202 switch (FPO
.getSpecifiedExceptionModeOverride()) {
205 case LangOptions::FPE_Ignore
:
208 case LangOptions::FPE_MayTrap
:
211 case LangOptions::FPE_Strict
:
218 if (FPO
.hasConstRoundingModeOverride()) {
219 LangOptions::RoundingMode RM
= FPO
.getConstRoundingModeOverride();
220 Indent() << "#pragma STDC FENV_ROUND ";
222 case llvm::RoundingMode::TowardZero
:
223 OS
<< "FE_TOWARDZERO";
225 case llvm::RoundingMode::NearestTiesToEven
:
226 OS
<< "FE_TONEAREST";
228 case llvm::RoundingMode::TowardPositive
:
231 case llvm::RoundingMode::TowardNegative
:
234 case llvm::RoundingMode::NearestTiesToAway
:
235 OS
<< "FE_TONEARESTFROMZERO";
237 case llvm::RoundingMode::Dynamic
:
241 llvm_unreachable("Invalid rounding mode");
247 void StmtPrinter::PrintRawDecl(Decl
*D
) {
248 D
->print(OS
, Policy
, IndentLevel
);
251 void StmtPrinter::PrintRawDeclStmt(const DeclStmt
*S
) {
252 SmallVector
<Decl
*, 2> Decls(S
->decls());
253 Decl::printGroup(Decls
.data(), Decls
.size(), OS
, Policy
, IndentLevel
);
256 void StmtPrinter::VisitNullStmt(NullStmt
*Node
) {
257 Indent() << ";" << NL
;
260 void StmtPrinter::VisitDeclStmt(DeclStmt
*Node
) {
262 PrintRawDeclStmt(Node
);
266 void StmtPrinter::VisitCompoundStmt(CompoundStmt
*Node
) {
268 PrintRawCompoundStmt(Node
);
272 void StmtPrinter::VisitCaseStmt(CaseStmt
*Node
) {
273 Indent(-1) << "case ";
274 PrintExpr(Node
->getLHS());
275 if (Node
->getRHS()) {
277 PrintExpr(Node
->getRHS());
281 PrintStmt(Node
->getSubStmt(), 0);
284 void StmtPrinter::VisitDefaultStmt(DefaultStmt
*Node
) {
285 Indent(-1) << "default:" << NL
;
286 PrintStmt(Node
->getSubStmt(), 0);
289 void StmtPrinter::VisitLabelStmt(LabelStmt
*Node
) {
290 Indent(-1) << Node
->getName() << ":" << NL
;
291 PrintStmt(Node
->getSubStmt(), 0);
294 void StmtPrinter::VisitAttributedStmt(AttributedStmt
*Node
) {
295 llvm::ArrayRef
<const Attr
*> Attrs
= Node
->getAttrs();
296 for (const auto *Attr
: Attrs
) {
297 Attr
->printPretty(OS
, Policy
);
298 if (Attr
!= Attrs
.back())
302 PrintStmt(Node
->getSubStmt(), 0);
305 void StmtPrinter::PrintRawIfStmt(IfStmt
*If
) {
306 if (If
->isConsteval()) {
308 if (If
->isNegatedConsteval())
312 PrintStmt(If
->getThen());
313 if (Stmt
*Else
= If
->getElse()) {
324 PrintInitStmt(If
->getInit(), 4);
325 if (const DeclStmt
*DS
= If
->getConditionVariableDeclStmt())
326 PrintRawDeclStmt(DS
);
328 PrintExpr(If
->getCond());
331 if (auto *CS
= dyn_cast
<CompoundStmt
>(If
->getThen())) {
333 PrintRawCompoundStmt(CS
);
334 OS
<< (If
->getElse() ? " " : NL
);
337 PrintStmt(If
->getThen());
338 if (If
->getElse()) Indent();
341 if (Stmt
*Else
= If
->getElse()) {
344 if (auto *CS
= dyn_cast
<CompoundStmt
>(Else
)) {
346 PrintRawCompoundStmt(CS
);
348 } else if (auto *ElseIf
= dyn_cast
<IfStmt
>(Else
)) {
350 PrintRawIfStmt(ElseIf
);
353 PrintStmt(If
->getElse());
358 void StmtPrinter::VisitIfStmt(IfStmt
*If
) {
363 void StmtPrinter::VisitSwitchStmt(SwitchStmt
*Node
) {
364 Indent() << "switch (";
366 PrintInitStmt(Node
->getInit(), 8);
367 if (const DeclStmt
*DS
= Node
->getConditionVariableDeclStmt())
368 PrintRawDeclStmt(DS
);
370 PrintExpr(Node
->getCond());
372 PrintControlledStmt(Node
->getBody());
375 void StmtPrinter::VisitWhileStmt(WhileStmt
*Node
) {
376 Indent() << "while (";
377 if (const DeclStmt
*DS
= Node
->getConditionVariableDeclStmt())
378 PrintRawDeclStmt(DS
);
380 PrintExpr(Node
->getCond());
382 PrintStmt(Node
->getBody());
385 void StmtPrinter::VisitDoStmt(DoStmt
*Node
) {
387 if (auto *CS
= dyn_cast
<CompoundStmt
>(Node
->getBody())) {
388 PrintRawCompoundStmt(CS
);
392 PrintStmt(Node
->getBody());
397 PrintExpr(Node
->getCond());
401 void StmtPrinter::VisitForStmt(ForStmt
*Node
) {
404 PrintInitStmt(Node
->getInit(), 5);
406 OS
<< (Node
->getCond() ? "; " : ";");
407 if (const DeclStmt
*DS
= Node
->getConditionVariableDeclStmt())
408 PrintRawDeclStmt(DS
);
409 else if (Node
->getCond())
410 PrintExpr(Node
->getCond());
412 if (Node
->getInc()) {
414 PrintExpr(Node
->getInc());
417 PrintControlledStmt(Node
->getBody());
420 void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt
*Node
) {
422 if (auto *DS
= dyn_cast
<DeclStmt
>(Node
->getElement()))
423 PrintRawDeclStmt(DS
);
425 PrintExpr(cast
<Expr
>(Node
->getElement()));
427 PrintExpr(Node
->getCollection());
429 PrintControlledStmt(Node
->getBody());
432 void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt
*Node
) {
435 PrintInitStmt(Node
->getInit(), 5);
436 PrintingPolicy
SubPolicy(Policy
);
437 SubPolicy
.SuppressInitializers
= true;
438 Node
->getLoopVariable()->print(OS
, SubPolicy
, IndentLevel
);
440 PrintExpr(Node
->getRangeInit());
442 PrintControlledStmt(Node
->getBody());
445 void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt
*Node
) {
447 if (Node
->isIfExists())
448 OS
<< "__if_exists (";
450 OS
<< "__if_not_exists (";
452 if (NestedNameSpecifier
*Qualifier
453 = Node
->getQualifierLoc().getNestedNameSpecifier())
454 Qualifier
->print(OS
, Policy
);
456 OS
<< Node
->getNameInfo() << ") ";
458 PrintRawCompoundStmt(Node
->getSubStmt());
461 void StmtPrinter::VisitGotoStmt(GotoStmt
*Node
) {
462 Indent() << "goto " << Node
->getLabel()->getName() << ";";
463 if (Policy
.IncludeNewlines
) OS
<< NL
;
466 void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt
*Node
) {
467 Indent() << "goto *";
468 PrintExpr(Node
->getTarget());
470 if (Policy
.IncludeNewlines
) OS
<< NL
;
473 void StmtPrinter::VisitContinueStmt(ContinueStmt
*Node
) {
474 Indent() << "continue;";
475 if (Policy
.IncludeNewlines
) OS
<< NL
;
478 void StmtPrinter::VisitBreakStmt(BreakStmt
*Node
) {
479 Indent() << "break;";
480 if (Policy
.IncludeNewlines
) OS
<< NL
;
483 void StmtPrinter::VisitReturnStmt(ReturnStmt
*Node
) {
484 Indent() << "return";
485 if (Node
->getRetValue()) {
487 PrintExpr(Node
->getRetValue());
490 if (Policy
.IncludeNewlines
) OS
<< NL
;
493 void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt
*Node
) {
496 if (Node
->isVolatile())
499 if (Node
->isAsmGoto())
503 VisitStringLiteral(Node
->getAsmString());
506 if (Node
->getNumOutputs() != 0 || Node
->getNumInputs() != 0 ||
507 Node
->getNumClobbers() != 0 || Node
->getNumLabels() != 0)
510 for (unsigned i
= 0, e
= Node
->getNumOutputs(); i
!= e
; ++i
) {
514 if (!Node
->getOutputName(i
).empty()) {
516 OS
<< Node
->getOutputName(i
);
520 VisitStringLiteral(Node
->getOutputConstraintLiteral(i
));
522 Visit(Node
->getOutputExpr(i
));
527 if (Node
->getNumInputs() != 0 || Node
->getNumClobbers() != 0 ||
528 Node
->getNumLabels() != 0)
531 for (unsigned i
= 0, e
= Node
->getNumInputs(); i
!= e
; ++i
) {
535 if (!Node
->getInputName(i
).empty()) {
537 OS
<< Node
->getInputName(i
);
541 VisitStringLiteral(Node
->getInputConstraintLiteral(i
));
543 Visit(Node
->getInputExpr(i
));
548 if (Node
->getNumClobbers() != 0 || Node
->getNumLabels())
551 for (unsigned i
= 0, e
= Node
->getNumClobbers(); i
!= e
; ++i
) {
555 VisitStringLiteral(Node
->getClobberStringLiteral(i
));
559 if (Node
->getNumLabels() != 0)
562 for (unsigned i
= 0, e
= Node
->getNumLabels(); i
!= e
; ++i
) {
565 OS
<< Node
->getLabelName(i
);
569 if (Policy
.IncludeNewlines
) OS
<< NL
;
572 void StmtPrinter::VisitMSAsmStmt(MSAsmStmt
*Node
) {
573 // FIXME: Implement MS style inline asm statement printer.
574 Indent() << "__asm ";
575 if (Node
->hasBraces())
577 OS
<< Node
->getAsmString() << NL
;
578 if (Node
->hasBraces())
579 Indent() << "}" << NL
;
582 void StmtPrinter::VisitCapturedStmt(CapturedStmt
*Node
) {
583 PrintStmt(Node
->getCapturedDecl()->getBody());
586 void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt
*Node
) {
588 if (auto *TS
= dyn_cast
<CompoundStmt
>(Node
->getTryBody())) {
589 PrintRawCompoundStmt(TS
);
593 for (ObjCAtCatchStmt
*catchStmt
: Node
->catch_stmts()) {
594 Indent() << "@catch(";
595 if (Decl
*DS
= catchStmt
->getCatchParamDecl())
598 if (auto *CS
= dyn_cast
<CompoundStmt
>(catchStmt
->getCatchBody())) {
599 PrintRawCompoundStmt(CS
);
604 if (auto *FS
= static_cast<ObjCAtFinallyStmt
*>(Node
->getFinallyStmt())) {
605 Indent() << "@finally";
606 if (auto *CS
= dyn_cast
<CompoundStmt
>(FS
->getFinallyBody())) {
607 PrintRawCompoundStmt(CS
);
613 void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt
*Node
) {
616 void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt
*Node
) {
617 Indent() << "@catch (...) { /* todo */ } " << NL
;
620 void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt
*Node
) {
621 Indent() << "@throw";
622 if (Node
->getThrowExpr()) {
624 PrintExpr(Node
->getThrowExpr());
629 void StmtPrinter::VisitObjCAvailabilityCheckExpr(
630 ObjCAvailabilityCheckExpr
*Node
) {
631 OS
<< "@available(...)";
634 void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt
*Node
) {
635 Indent() << "@synchronized (";
636 PrintExpr(Node
->getSynchExpr());
638 PrintRawCompoundStmt(Node
->getSynchBody());
642 void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt
*Node
) {
643 Indent() << "@autoreleasepool";
644 PrintRawCompoundStmt(cast
<CompoundStmt
>(Node
->getSubStmt()));
648 void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt
*Node
) {
650 if (Decl
*ExDecl
= Node
->getExceptionDecl())
651 PrintRawDecl(ExDecl
);
655 PrintRawCompoundStmt(cast
<CompoundStmt
>(Node
->getHandlerBlock()));
658 void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt
*Node
) {
660 PrintRawCXXCatchStmt(Node
);
664 void StmtPrinter::VisitCXXTryStmt(CXXTryStmt
*Node
) {
666 PrintRawCompoundStmt(Node
->getTryBlock());
667 for (unsigned i
= 0, e
= Node
->getNumHandlers(); i
< e
; ++i
) {
669 PrintRawCXXCatchStmt(Node
->getHandler(i
));
674 void StmtPrinter::VisitSEHTryStmt(SEHTryStmt
*Node
) {
675 Indent() << (Node
->getIsCXXTry() ? "try " : "__try ");
676 PrintRawCompoundStmt(Node
->getTryBlock());
677 SEHExceptStmt
*E
= Node
->getExceptHandler();
678 SEHFinallyStmt
*F
= Node
->getFinallyHandler();
680 PrintRawSEHExceptHandler(E
);
682 assert(F
&& "Must have a finally block...");
683 PrintRawSEHFinallyStmt(F
);
688 void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt
*Node
) {
690 PrintRawCompoundStmt(Node
->getBlock());
694 void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt
*Node
) {
696 VisitExpr(Node
->getFilterExpr());
698 PrintRawCompoundStmt(Node
->getBlock());
702 void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt
*Node
) {
704 PrintRawSEHExceptHandler(Node
);
708 void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt
*Node
) {
710 PrintRawSEHFinallyStmt(Node
);
714 void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt
*Node
) {
715 Indent() << "__leave;";
716 if (Policy
.IncludeNewlines
) OS
<< NL
;
719 //===----------------------------------------------------------------------===//
720 // OpenMP directives printing methods
721 //===----------------------------------------------------------------------===//
723 void StmtPrinter::VisitOMPCanonicalLoop(OMPCanonicalLoop
*Node
) {
724 PrintStmt(Node
->getLoopStmt());
727 void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective
*S
,
729 OMPClausePrinter
Printer(OS
, Policy
);
730 ArrayRef
<OMPClause
*> Clauses
= S
->clauses();
731 for (auto *Clause
: Clauses
)
732 if (Clause
&& !Clause
->isImplicit()) {
734 Printer
.Visit(Clause
);
737 if (!ForceNoStmt
&& S
->hasAssociatedStmt())
738 PrintStmt(S
->getRawStmt());
741 void StmtPrinter::VisitOMPMetaDirective(OMPMetaDirective
*Node
) {
742 Indent() << "#pragma omp metadirective";
743 PrintOMPExecutableDirective(Node
);
746 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective
*Node
) {
747 Indent() << "#pragma omp parallel";
748 PrintOMPExecutableDirective(Node
);
751 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective
*Node
) {
752 Indent() << "#pragma omp simd";
753 PrintOMPExecutableDirective(Node
);
756 void StmtPrinter::VisitOMPTileDirective(OMPTileDirective
*Node
) {
757 Indent() << "#pragma omp tile";
758 PrintOMPExecutableDirective(Node
);
761 void StmtPrinter::VisitOMPUnrollDirective(OMPUnrollDirective
*Node
) {
762 Indent() << "#pragma omp unroll";
763 PrintOMPExecutableDirective(Node
);
766 void StmtPrinter::VisitOMPForDirective(OMPForDirective
*Node
) {
767 Indent() << "#pragma omp for";
768 PrintOMPExecutableDirective(Node
);
771 void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective
*Node
) {
772 Indent() << "#pragma omp for simd";
773 PrintOMPExecutableDirective(Node
);
776 void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective
*Node
) {
777 Indent() << "#pragma omp sections";
778 PrintOMPExecutableDirective(Node
);
781 void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective
*Node
) {
782 Indent() << "#pragma omp section";
783 PrintOMPExecutableDirective(Node
);
786 void StmtPrinter::VisitOMPScopeDirective(OMPScopeDirective
*Node
) {
787 Indent() << "#pragma omp scope";
788 PrintOMPExecutableDirective(Node
);
791 void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective
*Node
) {
792 Indent() << "#pragma omp single";
793 PrintOMPExecutableDirective(Node
);
796 void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective
*Node
) {
797 Indent() << "#pragma omp master";
798 PrintOMPExecutableDirective(Node
);
801 void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective
*Node
) {
802 Indent() << "#pragma omp critical";
803 if (Node
->getDirectiveName().getName()) {
805 Node
->getDirectiveName().printName(OS
, Policy
);
808 PrintOMPExecutableDirective(Node
);
811 void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective
*Node
) {
812 Indent() << "#pragma omp parallel for";
813 PrintOMPExecutableDirective(Node
);
816 void StmtPrinter::VisitOMPParallelForSimdDirective(
817 OMPParallelForSimdDirective
*Node
) {
818 Indent() << "#pragma omp parallel for simd";
819 PrintOMPExecutableDirective(Node
);
822 void StmtPrinter::VisitOMPParallelMasterDirective(
823 OMPParallelMasterDirective
*Node
) {
824 Indent() << "#pragma omp parallel master";
825 PrintOMPExecutableDirective(Node
);
828 void StmtPrinter::VisitOMPParallelMaskedDirective(
829 OMPParallelMaskedDirective
*Node
) {
830 Indent() << "#pragma omp parallel masked";
831 PrintOMPExecutableDirective(Node
);
834 void StmtPrinter::VisitOMPParallelSectionsDirective(
835 OMPParallelSectionsDirective
*Node
) {
836 Indent() << "#pragma omp parallel sections";
837 PrintOMPExecutableDirective(Node
);
840 void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective
*Node
) {
841 Indent() << "#pragma omp task";
842 PrintOMPExecutableDirective(Node
);
845 void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective
*Node
) {
846 Indent() << "#pragma omp taskyield";
847 PrintOMPExecutableDirective(Node
);
850 void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective
*Node
) {
851 Indent() << "#pragma omp barrier";
852 PrintOMPExecutableDirective(Node
);
855 void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective
*Node
) {
856 Indent() << "#pragma omp taskwait";
857 PrintOMPExecutableDirective(Node
);
860 void StmtPrinter::VisitOMPErrorDirective(OMPErrorDirective
*Node
) {
861 Indent() << "#pragma omp error";
862 PrintOMPExecutableDirective(Node
);
865 void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective
*Node
) {
866 Indent() << "#pragma omp taskgroup";
867 PrintOMPExecutableDirective(Node
);
870 void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective
*Node
) {
871 Indent() << "#pragma omp flush";
872 PrintOMPExecutableDirective(Node
);
875 void StmtPrinter::VisitOMPDepobjDirective(OMPDepobjDirective
*Node
) {
876 Indent() << "#pragma omp depobj";
877 PrintOMPExecutableDirective(Node
);
880 void StmtPrinter::VisitOMPScanDirective(OMPScanDirective
*Node
) {
881 Indent() << "#pragma omp scan";
882 PrintOMPExecutableDirective(Node
);
885 void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective
*Node
) {
886 Indent() << "#pragma omp ordered";
887 PrintOMPExecutableDirective(Node
, Node
->hasClausesOfKind
<OMPDependClause
>());
890 void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective
*Node
) {
891 Indent() << "#pragma omp atomic";
892 PrintOMPExecutableDirective(Node
);
895 void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective
*Node
) {
896 Indent() << "#pragma omp target";
897 PrintOMPExecutableDirective(Node
);
900 void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective
*Node
) {
901 Indent() << "#pragma omp target data";
902 PrintOMPExecutableDirective(Node
);
905 void StmtPrinter::VisitOMPTargetEnterDataDirective(
906 OMPTargetEnterDataDirective
*Node
) {
907 Indent() << "#pragma omp target enter data";
908 PrintOMPExecutableDirective(Node
, /*ForceNoStmt=*/true);
911 void StmtPrinter::VisitOMPTargetExitDataDirective(
912 OMPTargetExitDataDirective
*Node
) {
913 Indent() << "#pragma omp target exit data";
914 PrintOMPExecutableDirective(Node
, /*ForceNoStmt=*/true);
917 void StmtPrinter::VisitOMPTargetParallelDirective(
918 OMPTargetParallelDirective
*Node
) {
919 Indent() << "#pragma omp target parallel";
920 PrintOMPExecutableDirective(Node
);
923 void StmtPrinter::VisitOMPTargetParallelForDirective(
924 OMPTargetParallelForDirective
*Node
) {
925 Indent() << "#pragma omp target parallel for";
926 PrintOMPExecutableDirective(Node
);
929 void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective
*Node
) {
930 Indent() << "#pragma omp teams";
931 PrintOMPExecutableDirective(Node
);
934 void StmtPrinter::VisitOMPCancellationPointDirective(
935 OMPCancellationPointDirective
*Node
) {
936 Indent() << "#pragma omp cancellation point "
937 << getOpenMPDirectiveName(Node
->getCancelRegion());
938 PrintOMPExecutableDirective(Node
);
941 void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective
*Node
) {
942 Indent() << "#pragma omp cancel "
943 << getOpenMPDirectiveName(Node
->getCancelRegion());
944 PrintOMPExecutableDirective(Node
);
947 void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective
*Node
) {
948 Indent() << "#pragma omp taskloop";
949 PrintOMPExecutableDirective(Node
);
952 void StmtPrinter::VisitOMPTaskLoopSimdDirective(
953 OMPTaskLoopSimdDirective
*Node
) {
954 Indent() << "#pragma omp taskloop simd";
955 PrintOMPExecutableDirective(Node
);
958 void StmtPrinter::VisitOMPMasterTaskLoopDirective(
959 OMPMasterTaskLoopDirective
*Node
) {
960 Indent() << "#pragma omp master taskloop";
961 PrintOMPExecutableDirective(Node
);
964 void StmtPrinter::VisitOMPMaskedTaskLoopDirective(
965 OMPMaskedTaskLoopDirective
*Node
) {
966 Indent() << "#pragma omp masked taskloop";
967 PrintOMPExecutableDirective(Node
);
970 void StmtPrinter::VisitOMPMasterTaskLoopSimdDirective(
971 OMPMasterTaskLoopSimdDirective
*Node
) {
972 Indent() << "#pragma omp master taskloop simd";
973 PrintOMPExecutableDirective(Node
);
976 void StmtPrinter::VisitOMPMaskedTaskLoopSimdDirective(
977 OMPMaskedTaskLoopSimdDirective
*Node
) {
978 Indent() << "#pragma omp masked taskloop simd";
979 PrintOMPExecutableDirective(Node
);
982 void StmtPrinter::VisitOMPParallelMasterTaskLoopDirective(
983 OMPParallelMasterTaskLoopDirective
*Node
) {
984 Indent() << "#pragma omp parallel master taskloop";
985 PrintOMPExecutableDirective(Node
);
988 void StmtPrinter::VisitOMPParallelMaskedTaskLoopDirective(
989 OMPParallelMaskedTaskLoopDirective
*Node
) {
990 Indent() << "#pragma omp parallel masked taskloop";
991 PrintOMPExecutableDirective(Node
);
994 void StmtPrinter::VisitOMPParallelMasterTaskLoopSimdDirective(
995 OMPParallelMasterTaskLoopSimdDirective
*Node
) {
996 Indent() << "#pragma omp parallel master taskloop simd";
997 PrintOMPExecutableDirective(Node
);
1000 void StmtPrinter::VisitOMPParallelMaskedTaskLoopSimdDirective(
1001 OMPParallelMaskedTaskLoopSimdDirective
*Node
) {
1002 Indent() << "#pragma omp parallel masked taskloop simd";
1003 PrintOMPExecutableDirective(Node
);
1006 void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective
*Node
) {
1007 Indent() << "#pragma omp distribute";
1008 PrintOMPExecutableDirective(Node
);
1011 void StmtPrinter::VisitOMPTargetUpdateDirective(
1012 OMPTargetUpdateDirective
*Node
) {
1013 Indent() << "#pragma omp target update";
1014 PrintOMPExecutableDirective(Node
, /*ForceNoStmt=*/true);
1017 void StmtPrinter::VisitOMPDistributeParallelForDirective(
1018 OMPDistributeParallelForDirective
*Node
) {
1019 Indent() << "#pragma omp distribute parallel for";
1020 PrintOMPExecutableDirective(Node
);
1023 void StmtPrinter::VisitOMPDistributeParallelForSimdDirective(
1024 OMPDistributeParallelForSimdDirective
*Node
) {
1025 Indent() << "#pragma omp distribute parallel for simd";
1026 PrintOMPExecutableDirective(Node
);
1029 void StmtPrinter::VisitOMPDistributeSimdDirective(
1030 OMPDistributeSimdDirective
*Node
) {
1031 Indent() << "#pragma omp distribute simd";
1032 PrintOMPExecutableDirective(Node
);
1035 void StmtPrinter::VisitOMPTargetParallelForSimdDirective(
1036 OMPTargetParallelForSimdDirective
*Node
) {
1037 Indent() << "#pragma omp target parallel for simd";
1038 PrintOMPExecutableDirective(Node
);
1041 void StmtPrinter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective
*Node
) {
1042 Indent() << "#pragma omp target simd";
1043 PrintOMPExecutableDirective(Node
);
1046 void StmtPrinter::VisitOMPTeamsDistributeDirective(
1047 OMPTeamsDistributeDirective
*Node
) {
1048 Indent() << "#pragma omp teams distribute";
1049 PrintOMPExecutableDirective(Node
);
1052 void StmtPrinter::VisitOMPTeamsDistributeSimdDirective(
1053 OMPTeamsDistributeSimdDirective
*Node
) {
1054 Indent() << "#pragma omp teams distribute simd";
1055 PrintOMPExecutableDirective(Node
);
1058 void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective(
1059 OMPTeamsDistributeParallelForSimdDirective
*Node
) {
1060 Indent() << "#pragma omp teams distribute parallel for simd";
1061 PrintOMPExecutableDirective(Node
);
1064 void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective(
1065 OMPTeamsDistributeParallelForDirective
*Node
) {
1066 Indent() << "#pragma omp teams distribute parallel for";
1067 PrintOMPExecutableDirective(Node
);
1070 void StmtPrinter::VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective
*Node
) {
1071 Indent() << "#pragma omp target teams";
1072 PrintOMPExecutableDirective(Node
);
1075 void StmtPrinter::VisitOMPTargetTeamsDistributeDirective(
1076 OMPTargetTeamsDistributeDirective
*Node
) {
1077 Indent() << "#pragma omp target teams distribute";
1078 PrintOMPExecutableDirective(Node
);
1081 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective(
1082 OMPTargetTeamsDistributeParallelForDirective
*Node
) {
1083 Indent() << "#pragma omp target teams distribute parallel for";
1084 PrintOMPExecutableDirective(Node
);
1087 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
1088 OMPTargetTeamsDistributeParallelForSimdDirective
*Node
) {
1089 Indent() << "#pragma omp target teams distribute parallel for simd";
1090 PrintOMPExecutableDirective(Node
);
1093 void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective(
1094 OMPTargetTeamsDistributeSimdDirective
*Node
) {
1095 Indent() << "#pragma omp target teams distribute simd";
1096 PrintOMPExecutableDirective(Node
);
1099 void StmtPrinter::VisitOMPInteropDirective(OMPInteropDirective
*Node
) {
1100 Indent() << "#pragma omp interop";
1101 PrintOMPExecutableDirective(Node
);
1104 void StmtPrinter::VisitOMPDispatchDirective(OMPDispatchDirective
*Node
) {
1105 Indent() << "#pragma omp dispatch";
1106 PrintOMPExecutableDirective(Node
);
1109 void StmtPrinter::VisitOMPMaskedDirective(OMPMaskedDirective
*Node
) {
1110 Indent() << "#pragma omp masked";
1111 PrintOMPExecutableDirective(Node
);
1114 void StmtPrinter::VisitOMPGenericLoopDirective(OMPGenericLoopDirective
*Node
) {
1115 Indent() << "#pragma omp loop";
1116 PrintOMPExecutableDirective(Node
);
1119 void StmtPrinter::VisitOMPTeamsGenericLoopDirective(
1120 OMPTeamsGenericLoopDirective
*Node
) {
1121 Indent() << "#pragma omp teams loop";
1122 PrintOMPExecutableDirective(Node
);
1125 void StmtPrinter::VisitOMPTargetTeamsGenericLoopDirective(
1126 OMPTargetTeamsGenericLoopDirective
*Node
) {
1127 Indent() << "#pragma omp target teams loop";
1128 PrintOMPExecutableDirective(Node
);
1131 void StmtPrinter::VisitOMPParallelGenericLoopDirective(
1132 OMPParallelGenericLoopDirective
*Node
) {
1133 Indent() << "#pragma omp parallel loop";
1134 PrintOMPExecutableDirective(Node
);
1137 void StmtPrinter::VisitOMPTargetParallelGenericLoopDirective(
1138 OMPTargetParallelGenericLoopDirective
*Node
) {
1139 Indent() << "#pragma omp target parallel loop";
1140 PrintOMPExecutableDirective(Node
);
1143 //===----------------------------------------------------------------------===//
1144 // OpenACC construct printing methods
1145 //===----------------------------------------------------------------------===//
1146 void StmtPrinter::VisitOpenACCComputeConstruct(OpenACCComputeConstruct
*S
) {
1147 Indent() << "#pragma acc " << S
->getDirectiveKind();
1149 if (!S
->clauses().empty()) {
1151 OpenACCClausePrinter
Printer(OS
);
1152 Printer
.VisitClauseList(S
->clauses());
1155 PrintStmt(S
->getStructuredBlock());
1158 //===----------------------------------------------------------------------===//
1159 // Expr printing methods.
1160 //===----------------------------------------------------------------------===//
1162 void StmtPrinter::VisitSourceLocExpr(SourceLocExpr
*Node
) {
1163 OS
<< Node
->getBuiltinStr() << "()";
1166 void StmtPrinter::VisitConstantExpr(ConstantExpr
*Node
) {
1167 PrintExpr(Node
->getSubExpr());
1170 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr
*Node
) {
1171 if (const auto *OCED
= dyn_cast
<OMPCapturedExprDecl
>(Node
->getDecl())) {
1172 OCED
->getInit()->IgnoreImpCasts()->printPretty(OS
, nullptr, Policy
);
1175 if (const auto *TPOD
= dyn_cast
<TemplateParamObjectDecl
>(Node
->getDecl())) {
1176 TPOD
->printAsExpr(OS
, Policy
);
1179 if (NestedNameSpecifier
*Qualifier
= Node
->getQualifier())
1180 Qualifier
->print(OS
, Policy
);
1181 if (Node
->hasTemplateKeyword())
1183 if (Policy
.CleanUglifiedParameters
&&
1184 isa
<ParmVarDecl
, NonTypeTemplateParmDecl
>(Node
->getDecl()) &&
1185 Node
->getDecl()->getIdentifier())
1186 OS
<< Node
->getDecl()->getIdentifier()->deuglifiedName();
1188 Node
->getNameInfo().printName(OS
, Policy
);
1189 if (Node
->hasExplicitTemplateArgs()) {
1190 const TemplateParameterList
*TPL
= nullptr;
1191 if (!Node
->hadMultipleCandidates())
1192 if (auto *TD
= dyn_cast
<TemplateDecl
>(Node
->getDecl()))
1193 TPL
= TD
->getTemplateParameters();
1194 printTemplateArgumentList(OS
, Node
->template_arguments(), Policy
, TPL
);
1198 void StmtPrinter::VisitDependentScopeDeclRefExpr(
1199 DependentScopeDeclRefExpr
*Node
) {
1200 if (NestedNameSpecifier
*Qualifier
= Node
->getQualifier())
1201 Qualifier
->print(OS
, Policy
);
1202 if (Node
->hasTemplateKeyword())
1204 OS
<< Node
->getNameInfo();
1205 if (Node
->hasExplicitTemplateArgs())
1206 printTemplateArgumentList(OS
, Node
->template_arguments(), Policy
);
1209 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr
*Node
) {
1210 if (Node
->getQualifier())
1211 Node
->getQualifier()->print(OS
, Policy
);
1212 if (Node
->hasTemplateKeyword())
1214 OS
<< Node
->getNameInfo();
1215 if (Node
->hasExplicitTemplateArgs())
1216 printTemplateArgumentList(OS
, Node
->template_arguments(), Policy
);
1219 static bool isImplicitSelf(const Expr
*E
) {
1220 if (const auto *DRE
= dyn_cast
<DeclRefExpr
>(E
)) {
1221 if (const auto *PD
= dyn_cast
<ImplicitParamDecl
>(DRE
->getDecl())) {
1222 if (PD
->getParameterKind() == ImplicitParamKind::ObjCSelf
&&
1223 DRE
->getBeginLoc().isInvalid())
1230 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr
*Node
) {
1231 if (Node
->getBase()) {
1232 if (!Policy
.SuppressImplicitBase
||
1233 !isImplicitSelf(Node
->getBase()->IgnoreImpCasts())) {
1234 PrintExpr(Node
->getBase());
1235 OS
<< (Node
->isArrow() ? "->" : ".");
1238 OS
<< *Node
->getDecl();
1241 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr
*Node
) {
1242 if (Node
->isSuperReceiver())
1244 else if (Node
->isObjectReceiver() && Node
->getBase()) {
1245 PrintExpr(Node
->getBase());
1247 } else if (Node
->isClassReceiver() && Node
->getClassReceiver()) {
1248 OS
<< Node
->getClassReceiver()->getName() << ".";
1251 if (Node
->isImplicitProperty()) {
1252 if (const auto *Getter
= Node
->getImplicitPropertyGetter())
1253 Getter
->getSelector().print(OS
);
1255 OS
<< SelectorTable::getPropertyNameFromSetterSelector(
1256 Node
->getImplicitPropertySetter()->getSelector());
1258 OS
<< Node
->getExplicitProperty()->getName();
1261 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr
*Node
) {
1262 PrintExpr(Node
->getBaseExpr());
1264 PrintExpr(Node
->getKeyExpr());
1268 void StmtPrinter::VisitSYCLUniqueStableNameExpr(
1269 SYCLUniqueStableNameExpr
*Node
) {
1270 OS
<< "__builtin_sycl_unique_stable_name(";
1271 Node
->getTypeSourceInfo()->getType().print(OS
, Policy
);
1275 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr
*Node
) {
1276 OS
<< PredefinedExpr::getIdentKindName(Node
->getIdentKind());
1279 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral
*Node
) {
1280 CharacterLiteral::print(Node
->getValue(), Node
->getKind(), OS
);
1283 /// Prints the given expression using the original source text. Returns true on
1284 /// success, false otherwise.
1285 static bool printExprAsWritten(raw_ostream
&OS
, Expr
*E
,
1286 const ASTContext
*Context
) {
1289 bool Invalid
= false;
1290 StringRef Source
= Lexer::getSourceText(
1291 CharSourceRange::getTokenRange(E
->getSourceRange()),
1292 Context
->getSourceManager(), Context
->getLangOpts(), &Invalid
);
1300 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral
*Node
) {
1301 if (Policy
.ConstantsAsWritten
&& printExprAsWritten(OS
, Node
, Context
))
1303 bool isSigned
= Node
->getType()->isSignedIntegerType();
1304 OS
<< toString(Node
->getValue(), 10, isSigned
);
1306 if (isa
<BitIntType
>(Node
->getType())) {
1307 OS
<< (isSigned
? "wb" : "uwb");
1311 // Emit suffixes. Integer literals are always a builtin integer type.
1312 switch (Node
->getType()->castAs
<BuiltinType
>()->getKind()) {
1313 default: llvm_unreachable("Unexpected type for integer literal!");
1314 case BuiltinType::Char_S
:
1315 case BuiltinType::Char_U
: OS
<< "i8"; break;
1316 case BuiltinType::UChar
: OS
<< "Ui8"; break;
1317 case BuiltinType::SChar
: OS
<< "i8"; break;
1318 case BuiltinType::Short
: OS
<< "i16"; break;
1319 case BuiltinType::UShort
: OS
<< "Ui16"; break;
1320 case BuiltinType::Int
: break; // no suffix.
1321 case BuiltinType::UInt
: OS
<< 'U'; break;
1322 case BuiltinType::Long
: OS
<< 'L'; break;
1323 case BuiltinType::ULong
: OS
<< "UL"; break;
1324 case BuiltinType::LongLong
: OS
<< "LL"; break;
1325 case BuiltinType::ULongLong
: OS
<< "ULL"; break;
1326 case BuiltinType::Int128
:
1327 break; // no suffix.
1328 case BuiltinType::UInt128
:
1329 break; // no suffix.
1330 case BuiltinType::WChar_S
:
1331 case BuiltinType::WChar_U
:
1336 void StmtPrinter::VisitFixedPointLiteral(FixedPointLiteral
*Node
) {
1337 if (Policy
.ConstantsAsWritten
&& printExprAsWritten(OS
, Node
, Context
))
1339 OS
<< Node
->getValueAsString(/*Radix=*/10);
1341 switch (Node
->getType()->castAs
<BuiltinType
>()->getKind()) {
1342 default: llvm_unreachable("Unexpected type for fixed point literal!");
1343 case BuiltinType::ShortFract
: OS
<< "hr"; break;
1344 case BuiltinType::ShortAccum
: OS
<< "hk"; break;
1345 case BuiltinType::UShortFract
: OS
<< "uhr"; break;
1346 case BuiltinType::UShortAccum
: OS
<< "uhk"; break;
1347 case BuiltinType::Fract
: OS
<< "r"; break;
1348 case BuiltinType::Accum
: OS
<< "k"; break;
1349 case BuiltinType::UFract
: OS
<< "ur"; break;
1350 case BuiltinType::UAccum
: OS
<< "uk"; break;
1351 case BuiltinType::LongFract
: OS
<< "lr"; break;
1352 case BuiltinType::LongAccum
: OS
<< "lk"; break;
1353 case BuiltinType::ULongFract
: OS
<< "ulr"; break;
1354 case BuiltinType::ULongAccum
: OS
<< "ulk"; break;
1358 static void PrintFloatingLiteral(raw_ostream
&OS
, FloatingLiteral
*Node
,
1360 SmallString
<16> Str
;
1361 Node
->getValue().toString(Str
);
1363 if (Str
.find_first_not_of("-0123456789") == StringRef::npos
)
1364 OS
<< '.'; // Trailing dot in order to separate from ints.
1369 // Emit suffixes. Float literals are always a builtin float type.
1370 switch (Node
->getType()->castAs
<BuiltinType
>()->getKind()) {
1371 default: llvm_unreachable("Unexpected type for float literal!");
1372 case BuiltinType::Half
: break; // FIXME: suffix?
1373 case BuiltinType::Ibm128
: break; // FIXME: No suffix for ibm128 literal
1374 case BuiltinType::Double
: break; // no suffix.
1375 case BuiltinType::Float16
: OS
<< "F16"; break;
1376 case BuiltinType::Float
: OS
<< 'F'; break;
1377 case BuiltinType::LongDouble
: OS
<< 'L'; break;
1378 case BuiltinType::Float128
: OS
<< 'Q'; break;
1382 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral
*Node
) {
1383 if (Policy
.ConstantsAsWritten
&& printExprAsWritten(OS
, Node
, Context
))
1385 PrintFloatingLiteral(OS
, Node
, /*PrintSuffix=*/true);
1388 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral
*Node
) {
1389 PrintExpr(Node
->getSubExpr());
1393 void StmtPrinter::VisitStringLiteral(StringLiteral
*Str
) {
1394 Str
->outputString(OS
);
1397 void StmtPrinter::VisitParenExpr(ParenExpr
*Node
) {
1399 PrintExpr(Node
->getSubExpr());
1403 void StmtPrinter::VisitUnaryOperator(UnaryOperator
*Node
) {
1404 if (!Node
->isPostfix()) {
1405 OS
<< UnaryOperator::getOpcodeStr(Node
->getOpcode());
1407 // Print a space if this is an "identifier operator" like __real, or if
1408 // it might be concatenated incorrectly like '+'.
1409 switch (Node
->getOpcode()) {
1418 if (isa
<UnaryOperator
>(Node
->getSubExpr()))
1423 PrintExpr(Node
->getSubExpr());
1425 if (Node
->isPostfix())
1426 OS
<< UnaryOperator::getOpcodeStr(Node
->getOpcode());
1429 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr
*Node
) {
1430 OS
<< "__builtin_offsetof(";
1431 Node
->getTypeSourceInfo()->getType().print(OS
, Policy
);
1433 bool PrintedSomething
= false;
1434 for (unsigned i
= 0, n
= Node
->getNumComponents(); i
< n
; ++i
) {
1435 OffsetOfNode ON
= Node
->getComponent(i
);
1436 if (ON
.getKind() == OffsetOfNode::Array
) {
1439 PrintExpr(Node
->getIndexExpr(ON
.getArrayExprIndex()));
1441 PrintedSomething
= true;
1445 // Skip implicit base indirections.
1446 if (ON
.getKind() == OffsetOfNode::Base
)
1449 // Field or identifier node.
1450 IdentifierInfo
*Id
= ON
.getFieldName();
1454 if (PrintedSomething
)
1457 PrintedSomething
= true;
1458 OS
<< Id
->getName();
1463 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(
1464 UnaryExprOrTypeTraitExpr
*Node
) {
1465 const char *Spelling
= getTraitSpelling(Node
->getKind());
1466 if (Node
->getKind() == UETT_AlignOf
) {
1468 Spelling
= "alignof";
1469 else if (Policy
.UnderscoreAlignof
)
1470 Spelling
= "_Alignof";
1472 Spelling
= "__alignof";
1477 if (Node
->isArgumentType()) {
1479 Node
->getArgumentType().print(OS
, Policy
);
1483 PrintExpr(Node
->getArgumentExpr());
1487 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr
*Node
) {
1489 if (Node
->isExprPredicate())
1490 PrintExpr(Node
->getControllingExpr());
1492 Node
->getControllingType()->getType().print(OS
, Policy
);
1494 for (const GenericSelectionExpr::Association
&Assoc
: Node
->associations()) {
1496 QualType T
= Assoc
.getType();
1500 T
.print(OS
, Policy
);
1502 PrintExpr(Assoc
.getAssociationExpr());
1507 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr
*Node
) {
1508 PrintExpr(Node
->getLHS());
1510 PrintExpr(Node
->getRHS());
1514 void StmtPrinter::VisitMatrixSubscriptExpr(MatrixSubscriptExpr
*Node
) {
1515 PrintExpr(Node
->getBase());
1517 PrintExpr(Node
->getRowIdx());
1520 PrintExpr(Node
->getColumnIdx());
1524 void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr
*Node
) {
1525 PrintExpr(Node
->getBase());
1527 if (Node
->getLowerBound())
1528 PrintExpr(Node
->getLowerBound());
1529 if (Node
->getColonLocFirst().isValid()) {
1531 if (Node
->getLength())
1532 PrintExpr(Node
->getLength());
1534 if (Node
->getColonLocSecond().isValid()) {
1536 if (Node
->getStride())
1537 PrintExpr(Node
->getStride());
1542 void StmtPrinter::VisitOMPArrayShapingExpr(OMPArrayShapingExpr
*Node
) {
1544 for (Expr
*E
: Node
->getDimensions()) {
1550 PrintExpr(Node
->getBase());
1553 void StmtPrinter::VisitOMPIteratorExpr(OMPIteratorExpr
*Node
) {
1555 for (unsigned I
= 0, E
= Node
->numOfIterators(); I
< E
; ++I
) {
1556 auto *VD
= cast
<ValueDecl
>(Node
->getIteratorDecl(I
));
1557 VD
->getType().print(OS
, Policy
);
1558 const OMPIteratorExpr::IteratorRange Range
= Node
->getIteratorRange(I
);
1559 OS
<< " " << VD
->getName() << " = ";
1560 PrintExpr(Range
.Begin
);
1562 PrintExpr(Range
.End
);
1565 PrintExpr(Range
.Step
);
1573 void StmtPrinter::PrintCallArgs(CallExpr
*Call
) {
1574 for (unsigned i
= 0, e
= Call
->getNumArgs(); i
!= e
; ++i
) {
1575 if (isa
<CXXDefaultArgExpr
>(Call
->getArg(i
))) {
1576 // Don't print any defaulted arguments
1581 PrintExpr(Call
->getArg(i
));
1585 void StmtPrinter::VisitCallExpr(CallExpr
*Call
) {
1586 PrintExpr(Call
->getCallee());
1588 PrintCallArgs(Call
);
1592 static bool isImplicitThis(const Expr
*E
) {
1593 if (const auto *TE
= dyn_cast
<CXXThisExpr
>(E
))
1594 return TE
->isImplicit();
1598 void StmtPrinter::VisitMemberExpr(MemberExpr
*Node
) {
1599 if (!Policy
.SuppressImplicitBase
|| !isImplicitThis(Node
->getBase())) {
1600 PrintExpr(Node
->getBase());
1602 auto *ParentMember
= dyn_cast
<MemberExpr
>(Node
->getBase());
1603 FieldDecl
*ParentDecl
=
1604 ParentMember
? dyn_cast
<FieldDecl
>(ParentMember
->getMemberDecl())
1607 if (!ParentDecl
|| !ParentDecl
->isAnonymousStructOrUnion())
1608 OS
<< (Node
->isArrow() ? "->" : ".");
1611 if (auto *FD
= dyn_cast
<FieldDecl
>(Node
->getMemberDecl()))
1612 if (FD
->isAnonymousStructOrUnion())
1615 if (NestedNameSpecifier
*Qualifier
= Node
->getQualifier())
1616 Qualifier
->print(OS
, Policy
);
1617 if (Node
->hasTemplateKeyword())
1619 OS
<< Node
->getMemberNameInfo();
1620 const TemplateParameterList
*TPL
= nullptr;
1621 if (auto *FD
= dyn_cast
<FunctionDecl
>(Node
->getMemberDecl())) {
1622 if (!Node
->hadMultipleCandidates())
1623 if (auto *FTD
= FD
->getPrimaryTemplate())
1624 TPL
= FTD
->getTemplateParameters();
1625 } else if (auto *VTSD
=
1626 dyn_cast
<VarTemplateSpecializationDecl
>(Node
->getMemberDecl()))
1627 TPL
= VTSD
->getSpecializedTemplate()->getTemplateParameters();
1628 if (Node
->hasExplicitTemplateArgs())
1629 printTemplateArgumentList(OS
, Node
->template_arguments(), Policy
, TPL
);
1632 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr
*Node
) {
1633 PrintExpr(Node
->getBase());
1634 OS
<< (Node
->isArrow() ? "->isa" : ".isa");
1637 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr
*Node
) {
1638 PrintExpr(Node
->getBase());
1640 OS
<< Node
->getAccessor().getName();
1643 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr
*Node
) {
1645 Node
->getTypeAsWritten().print(OS
, Policy
);
1647 PrintExpr(Node
->getSubExpr());
1650 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr
*Node
) {
1652 Node
->getType().print(OS
, Policy
);
1654 PrintExpr(Node
->getInitializer());
1657 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr
*Node
) {
1658 // No need to print anything, simply forward to the subexpression.
1659 PrintExpr(Node
->getSubExpr());
1662 void StmtPrinter::VisitBinaryOperator(BinaryOperator
*Node
) {
1663 PrintExpr(Node
->getLHS());
1664 OS
<< " " << BinaryOperator::getOpcodeStr(Node
->getOpcode()) << " ";
1665 PrintExpr(Node
->getRHS());
1668 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator
*Node
) {
1669 PrintExpr(Node
->getLHS());
1670 OS
<< " " << BinaryOperator::getOpcodeStr(Node
->getOpcode()) << " ";
1671 PrintExpr(Node
->getRHS());
1674 void StmtPrinter::VisitConditionalOperator(ConditionalOperator
*Node
) {
1675 PrintExpr(Node
->getCond());
1677 PrintExpr(Node
->getLHS());
1679 PrintExpr(Node
->getRHS());
1685 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator
*Node
) {
1686 PrintExpr(Node
->getCommon());
1688 PrintExpr(Node
->getFalseExpr());
1691 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr
*Node
) {
1692 OS
<< "&&" << Node
->getLabel()->getName();
1695 void StmtPrinter::VisitStmtExpr(StmtExpr
*E
) {
1697 PrintRawCompoundStmt(E
->getSubStmt());
1701 void StmtPrinter::VisitChooseExpr(ChooseExpr
*Node
) {
1702 OS
<< "__builtin_choose_expr(";
1703 PrintExpr(Node
->getCond());
1705 PrintExpr(Node
->getLHS());
1707 PrintExpr(Node
->getRHS());
1711 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr
*) {
1715 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr
*Node
) {
1716 OS
<< "__builtin_shufflevector(";
1717 for (unsigned i
= 0, e
= Node
->getNumSubExprs(); i
!= e
; ++i
) {
1719 PrintExpr(Node
->getExpr(i
));
1724 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr
*Node
) {
1725 OS
<< "__builtin_convertvector(";
1726 PrintExpr(Node
->getSrcExpr());
1728 Node
->getType().print(OS
, Policy
);
1732 void StmtPrinter::VisitInitListExpr(InitListExpr
* Node
) {
1733 if (Node
->getSyntacticForm()) {
1734 Visit(Node
->getSyntacticForm());
1739 for (unsigned i
= 0, e
= Node
->getNumInits(); i
!= e
; ++i
) {
1741 if (Node
->getInit(i
))
1742 PrintExpr(Node
->getInit(i
));
1749 void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr
*Node
) {
1750 // There's no way to express this expression in any of our supported
1751 // languages, so just emit something terse and (hopefully) clear.
1753 PrintExpr(Node
->getSubExpr());
1757 void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr
*Node
) {
1761 void StmtPrinter::VisitParenListExpr(ParenListExpr
* Node
) {
1763 for (unsigned i
= 0, e
= Node
->getNumExprs(); i
!= e
; ++i
) {
1765 PrintExpr(Node
->getExpr(i
));
1770 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr
*Node
) {
1771 bool NeedsEquals
= true;
1772 for (const DesignatedInitExpr::Designator
&D
: Node
->designators()) {
1773 if (D
.isFieldDesignator()) {
1774 if (D
.getDotLoc().isInvalid()) {
1775 if (const IdentifierInfo
*II
= D
.getFieldName()) {
1776 OS
<< II
->getName() << ":";
1777 NeedsEquals
= false;
1780 OS
<< "." << D
.getFieldName()->getName();
1784 if (D
.isArrayDesignator()) {
1785 PrintExpr(Node
->getArrayIndex(D
));
1787 PrintExpr(Node
->getArrayRangeStart(D
));
1789 PrintExpr(Node
->getArrayRangeEnd(D
));
1799 PrintExpr(Node
->getInit());
1802 void StmtPrinter::VisitDesignatedInitUpdateExpr(
1803 DesignatedInitUpdateExpr
*Node
) {
1806 PrintExpr(Node
->getBase());
1809 OS
<< "/*updater*/";
1810 PrintExpr(Node
->getUpdater());
1814 void StmtPrinter::VisitNoInitExpr(NoInitExpr
*Node
) {
1815 OS
<< "/*no init*/";
1818 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr
*Node
) {
1819 if (Node
->getType()->getAsCXXRecordDecl()) {
1820 OS
<< "/*implicit*/";
1821 Node
->getType().print(OS
, Policy
);
1824 OS
<< "/*implicit*/(";
1825 Node
->getType().print(OS
, Policy
);
1827 if (Node
->getType()->isRecordType())
1834 void StmtPrinter::VisitVAArgExpr(VAArgExpr
*Node
) {
1835 OS
<< "__builtin_va_arg(";
1836 PrintExpr(Node
->getSubExpr());
1838 Node
->getType().print(OS
, Policy
);
1842 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr
*Node
) {
1843 PrintExpr(Node
->getSyntacticForm());
1846 void StmtPrinter::VisitAtomicExpr(AtomicExpr
*Node
) {
1847 const char *Name
= nullptr;
1848 switch (Node
->getOp()) {
1849 #define BUILTIN(ID, TYPE, ATTRS)
1850 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1851 case AtomicExpr::AO ## ID: \
1854 #include "clang/Basic/Builtins.inc"
1858 // AtomicExpr stores its subexpressions in a permuted order.
1859 PrintExpr(Node
->getPtr());
1860 if (Node
->getOp() != AtomicExpr::AO__c11_atomic_load
&&
1861 Node
->getOp() != AtomicExpr::AO__atomic_load_n
&&
1862 Node
->getOp() != AtomicExpr::AO__scoped_atomic_load_n
&&
1863 Node
->getOp() != AtomicExpr::AO__opencl_atomic_load
&&
1864 Node
->getOp() != AtomicExpr::AO__hip_atomic_load
) {
1866 PrintExpr(Node
->getVal1());
1868 if (Node
->getOp() == AtomicExpr::AO__atomic_exchange
||
1869 Node
->isCmpXChg()) {
1871 PrintExpr(Node
->getVal2());
1873 if (Node
->getOp() == AtomicExpr::AO__atomic_compare_exchange
||
1874 Node
->getOp() == AtomicExpr::AO__atomic_compare_exchange_n
) {
1876 PrintExpr(Node
->getWeak());
1878 if (Node
->getOp() != AtomicExpr::AO__c11_atomic_init
&&
1879 Node
->getOp() != AtomicExpr::AO__opencl_atomic_init
) {
1881 PrintExpr(Node
->getOrder());
1883 if (Node
->isCmpXChg()) {
1885 PrintExpr(Node
->getOrderFail());
1891 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr
*Node
) {
1892 OverloadedOperatorKind Kind
= Node
->getOperator();
1893 if (Kind
== OO_PlusPlus
|| Kind
== OO_MinusMinus
) {
1894 if (Node
->getNumArgs() == 1) {
1895 OS
<< getOperatorSpelling(Kind
) << ' ';
1896 PrintExpr(Node
->getArg(0));
1898 PrintExpr(Node
->getArg(0));
1899 OS
<< ' ' << getOperatorSpelling(Kind
);
1901 } else if (Kind
== OO_Arrow
) {
1902 PrintExpr(Node
->getArg(0));
1903 } else if (Kind
== OO_Call
|| Kind
== OO_Subscript
) {
1904 PrintExpr(Node
->getArg(0));
1905 OS
<< (Kind
== OO_Call
? '(' : '[');
1906 for (unsigned ArgIdx
= 1; ArgIdx
< Node
->getNumArgs(); ++ArgIdx
) {
1909 if (!isa
<CXXDefaultArgExpr
>(Node
->getArg(ArgIdx
)))
1910 PrintExpr(Node
->getArg(ArgIdx
));
1912 OS
<< (Kind
== OO_Call
? ')' : ']');
1913 } else if (Node
->getNumArgs() == 1) {
1914 OS
<< getOperatorSpelling(Kind
) << ' ';
1915 PrintExpr(Node
->getArg(0));
1916 } else if (Node
->getNumArgs() == 2) {
1917 PrintExpr(Node
->getArg(0));
1918 OS
<< ' ' << getOperatorSpelling(Kind
) << ' ';
1919 PrintExpr(Node
->getArg(1));
1921 llvm_unreachable("unknown overloaded operator");
1925 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr
*Node
) {
1926 // If we have a conversion operator call only print the argument.
1927 CXXMethodDecl
*MD
= Node
->getMethodDecl();
1928 if (MD
&& isa
<CXXConversionDecl
>(MD
)) {
1929 PrintExpr(Node
->getImplicitObjectArgument());
1932 VisitCallExpr(cast
<CallExpr
>(Node
));
1935 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr
*Node
) {
1936 PrintExpr(Node
->getCallee());
1938 PrintCallArgs(Node
->getConfig());
1940 PrintCallArgs(Node
);
1944 void StmtPrinter::VisitCXXRewrittenBinaryOperator(
1945 CXXRewrittenBinaryOperator
*Node
) {
1946 CXXRewrittenBinaryOperator::DecomposedForm Decomposed
=
1947 Node
->getDecomposedForm();
1948 PrintExpr(const_cast<Expr
*>(Decomposed
.LHS
));
1949 OS
<< ' ' << BinaryOperator::getOpcodeStr(Decomposed
.Opcode
) << ' ';
1950 PrintExpr(const_cast<Expr
*>(Decomposed
.RHS
));
1953 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr
*Node
) {
1954 OS
<< Node
->getCastName() << '<';
1955 Node
->getTypeAsWritten().print(OS
, Policy
);
1957 PrintExpr(Node
->getSubExpr());
1961 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr
*Node
) {
1962 VisitCXXNamedCastExpr(Node
);
1965 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr
*Node
) {
1966 VisitCXXNamedCastExpr(Node
);
1969 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr
*Node
) {
1970 VisitCXXNamedCastExpr(Node
);
1973 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr
*Node
) {
1974 VisitCXXNamedCastExpr(Node
);
1977 void StmtPrinter::VisitBuiltinBitCastExpr(BuiltinBitCastExpr
*Node
) {
1978 OS
<< "__builtin_bit_cast(";
1979 Node
->getTypeInfoAsWritten()->getType().print(OS
, Policy
);
1981 PrintExpr(Node
->getSubExpr());
1985 void StmtPrinter::VisitCXXAddrspaceCastExpr(CXXAddrspaceCastExpr
*Node
) {
1986 VisitCXXNamedCastExpr(Node
);
1989 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr
*Node
) {
1991 if (Node
->isTypeOperand()) {
1992 Node
->getTypeOperandSourceInfo()->getType().print(OS
, Policy
);
1994 PrintExpr(Node
->getExprOperand());
1999 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr
*Node
) {
2001 if (Node
->isTypeOperand()) {
2002 Node
->getTypeOperandSourceInfo()->getType().print(OS
, Policy
);
2004 PrintExpr(Node
->getExprOperand());
2009 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr
*Node
) {
2010 PrintExpr(Node
->getBaseExpr());
2011 if (Node
->isArrow())
2015 if (NestedNameSpecifier
*Qualifier
=
2016 Node
->getQualifierLoc().getNestedNameSpecifier())
2017 Qualifier
->print(OS
, Policy
);
2018 OS
<< Node
->getPropertyDecl()->getDeclName();
2021 void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr
*Node
) {
2022 PrintExpr(Node
->getBase());
2024 PrintExpr(Node
->getIdx());
2028 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral
*Node
) {
2029 switch (Node
->getLiteralOperatorKind()) {
2030 case UserDefinedLiteral::LOK_Raw
:
2031 OS
<< cast
<StringLiteral
>(Node
->getArg(0)->IgnoreImpCasts())->getString();
2033 case UserDefinedLiteral::LOK_Template
: {
2034 const auto *DRE
= cast
<DeclRefExpr
>(Node
->getCallee()->IgnoreImpCasts());
2035 const TemplateArgumentList
*Args
=
2036 cast
<FunctionDecl
>(DRE
->getDecl())->getTemplateSpecializationArgs();
2039 if (Args
->size() != 1 || Args
->get(0).getKind() != TemplateArgument::Pack
) {
2040 const TemplateParameterList
*TPL
= nullptr;
2041 if (!DRE
->hadMultipleCandidates())
2042 if (const auto *TD
= dyn_cast
<TemplateDecl
>(DRE
->getDecl()))
2043 TPL
= TD
->getTemplateParameters();
2044 OS
<< "operator\"\"" << Node
->getUDSuffix()->getName();
2045 printTemplateArgumentList(OS
, Args
->asArray(), Policy
, TPL
);
2050 const TemplateArgument
&Pack
= Args
->get(0);
2051 for (const auto &P
: Pack
.pack_elements()) {
2052 char C
= (char)P
.getAsIntegral().getZExtValue();
2057 case UserDefinedLiteral::LOK_Integer
: {
2058 // Print integer literal without suffix.
2059 const auto *Int
= cast
<IntegerLiteral
>(Node
->getCookedLiteral());
2060 OS
<< toString(Int
->getValue(), 10, /*isSigned*/false);
2063 case UserDefinedLiteral::LOK_Floating
: {
2064 // Print floating literal without suffix.
2065 auto *Float
= cast
<FloatingLiteral
>(Node
->getCookedLiteral());
2066 PrintFloatingLiteral(OS
, Float
, /*PrintSuffix=*/false);
2069 case UserDefinedLiteral::LOK_String
:
2070 case UserDefinedLiteral::LOK_Character
:
2071 PrintExpr(Node
->getCookedLiteral());
2074 OS
<< Node
->getUDSuffix()->getName();
2077 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr
*Node
) {
2078 OS
<< (Node
->getValue() ? "true" : "false");
2081 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr
*Node
) {
2085 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr
*Node
) {
2089 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr
*Node
) {
2090 if (!Node
->getSubExpr())
2094 PrintExpr(Node
->getSubExpr());
2098 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr
*Node
) {
2099 // Nothing to print: we picked up the default argument.
2102 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr
*Node
) {
2103 // Nothing to print: we picked up the default initializer.
2106 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr
*Node
) {
2107 auto TargetType
= Node
->getType();
2108 auto *Auto
= TargetType
->getContainedDeducedType();
2109 bool Bare
= Auto
&& Auto
->isDeduced();
2111 // Parenthesize deduced casts.
2114 TargetType
.print(OS
, Policy
);
2118 // No extra braces surrounding the inner construct.
2119 if (!Node
->isListInitialization())
2121 PrintExpr(Node
->getSubExpr());
2122 if (!Node
->isListInitialization())
2126 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr
*Node
) {
2127 PrintExpr(Node
->getSubExpr());
2130 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr
*Node
) {
2131 Node
->getType().print(OS
, Policy
);
2132 if (Node
->isStdInitListInitialization())
2133 /* Nothing to do; braces are part of creating the std::initializer_list. */;
2134 else if (Node
->isListInitialization())
2138 for (CXXTemporaryObjectExpr::arg_iterator Arg
= Node
->arg_begin(),
2139 ArgEnd
= Node
->arg_end();
2140 Arg
!= ArgEnd
; ++Arg
) {
2141 if ((*Arg
)->isDefaultArgument())
2143 if (Arg
!= Node
->arg_begin())
2147 if (Node
->isStdInitListInitialization())
2149 else if (Node
->isListInitialization())
2155 void StmtPrinter::VisitLambdaExpr(LambdaExpr
*Node
) {
2157 bool NeedComma
= false;
2158 switch (Node
->getCaptureDefault()) {
2172 for (LambdaExpr::capture_iterator C
= Node
->explicit_capture_begin(),
2173 CEnd
= Node
->explicit_capture_end();
2176 if (C
->capturesVLAType())
2183 switch (C
->getCaptureKind()) {
2193 if (Node
->getCaptureDefault() != LCD_ByRef
|| Node
->isInitCapture(C
))
2195 OS
<< C
->getCapturedVar()->getName();
2199 OS
<< C
->getCapturedVar()->getName();
2203 llvm_unreachable("VLA type in explicit captures.");
2206 if (C
->isPackExpansion())
2209 if (Node
->isInitCapture(C
)) {
2210 // Init captures are always VarDecl.
2211 auto *D
= cast
<VarDecl
>(C
->getCapturedVar());
2213 llvm::StringRef Pre
;
2214 llvm::StringRef Post
;
2215 if (D
->getInitStyle() == VarDecl::CallInit
&&
2216 !isa
<ParenListExpr
>(D
->getInit())) {
2219 } else if (D
->getInitStyle() == VarDecl::CInit
) {
2224 PrintExpr(D
->getInit());
2230 if (!Node
->getExplicitTemplateParameters().empty()) {
2231 Node
->getTemplateParameterList()->print(
2232 OS
, Node
->getLambdaClass()->getASTContext(),
2233 /*OmitTemplateKW*/true);
2236 if (Node
->hasExplicitParameters()) {
2238 CXXMethodDecl
*Method
= Node
->getCallOperator();
2240 for (const auto *P
: Method
->parameters()) {
2246 std::string ParamStr
=
2247 (Policy
.CleanUglifiedParameters
&& P
->getIdentifier())
2248 ? P
->getIdentifier()->deuglifiedName().str()
2249 : P
->getNameAsString();
2250 P
->getOriginalType().print(OS
, Policy
, ParamStr
);
2252 if (Method
->isVariadic()) {
2259 if (Node
->isMutable())
2262 auto *Proto
= Method
->getType()->castAs
<FunctionProtoType
>();
2263 Proto
->printExceptionSpecification(OS
, Policy
);
2265 // FIXME: Attributes
2267 // Print the trailing return type if it was specified in the source.
2268 if (Node
->hasExplicitResultType()) {
2270 Proto
->getReturnType().print(OS
, Policy
);
2276 if (Policy
.TerseOutput
)
2279 PrintRawCompoundStmt(Node
->getCompoundStmtBody());
2282 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr
*Node
) {
2283 if (TypeSourceInfo
*TSInfo
= Node
->getTypeSourceInfo())
2284 TSInfo
->getType().print(OS
, Policy
);
2286 Node
->getType().print(OS
, Policy
);
2290 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr
*E
) {
2291 if (E
->isGlobalNew())
2294 unsigned NumPlace
= E
->getNumPlacementArgs();
2295 if (NumPlace
> 0 && !isa
<CXXDefaultArgExpr
>(E
->getPlacementArg(0))) {
2297 PrintExpr(E
->getPlacementArg(0));
2298 for (unsigned i
= 1; i
< NumPlace
; ++i
) {
2299 if (isa
<CXXDefaultArgExpr
>(E
->getPlacementArg(i
)))
2302 PrintExpr(E
->getPlacementArg(i
));
2306 if (E
->isParenTypeId())
2310 llvm::raw_string_ostream
s(TypeS
);
2312 if (std::optional
<Expr
*> Size
= E
->getArraySize())
2313 (*Size
)->printPretty(s
, Helper
, Policy
);
2316 E
->getAllocatedType().print(OS
, Policy
, TypeS
);
2317 if (E
->isParenTypeId())
2320 CXXNewInitializationStyle InitStyle
= E
->getInitializationStyle();
2321 if (InitStyle
!= CXXNewInitializationStyle::None
) {
2322 bool Bare
= InitStyle
== CXXNewInitializationStyle::Parens
&&
2323 !isa
<ParenListExpr
>(E
->getInitializer());
2326 PrintExpr(E
->getInitializer());
2332 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr
*E
) {
2333 if (E
->isGlobalDelete())
2336 if (E
->isArrayForm())
2338 PrintExpr(E
->getArgument());
2341 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr
*E
) {
2342 PrintExpr(E
->getBase());
2347 if (E
->getQualifier())
2348 E
->getQualifier()->print(OS
, Policy
);
2351 if (IdentifierInfo
*II
= E
->getDestroyedTypeIdentifier())
2352 OS
<< II
->getName();
2354 E
->getDestroyedType().print(OS
, Policy
);
2357 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr
*E
) {
2358 if (E
->isListInitialization() && !E
->isStdInitListInitialization())
2361 for (unsigned i
= 0, e
= E
->getNumArgs(); i
!= e
; ++i
) {
2362 if (isa
<CXXDefaultArgExpr
>(E
->getArg(i
))) {
2363 // Don't print any defaulted arguments
2368 PrintExpr(E
->getArg(i
));
2371 if (E
->isListInitialization() && !E
->isStdInitListInitialization())
2375 void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr
*E
) {
2376 // Parens are printed by the surrounding context.
2377 OS
<< "<forwarded>";
2380 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr
*E
) {
2381 PrintExpr(E
->getSubExpr());
2384 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups
*E
) {
2385 // Just forward to the subexpression.
2386 PrintExpr(E
->getSubExpr());
2389 void StmtPrinter::VisitCXXUnresolvedConstructExpr(
2390 CXXUnresolvedConstructExpr
*Node
) {
2391 Node
->getTypeAsWritten().print(OS
, Policy
);
2392 if (!Node
->isListInitialization())
2394 for (auto Arg
= Node
->arg_begin(), ArgEnd
= Node
->arg_end(); Arg
!= ArgEnd
;
2396 if (Arg
!= Node
->arg_begin())
2400 if (!Node
->isListInitialization())
2404 void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2405 CXXDependentScopeMemberExpr
*Node
) {
2406 if (!Node
->isImplicitAccess()) {
2407 PrintExpr(Node
->getBase());
2408 OS
<< (Node
->isArrow() ? "->" : ".");
2410 if (NestedNameSpecifier
*Qualifier
= Node
->getQualifier())
2411 Qualifier
->print(OS
, Policy
);
2412 if (Node
->hasTemplateKeyword())
2414 OS
<< Node
->getMemberNameInfo();
2415 if (Node
->hasExplicitTemplateArgs())
2416 printTemplateArgumentList(OS
, Node
->template_arguments(), Policy
);
2419 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr
*Node
) {
2420 if (!Node
->isImplicitAccess()) {
2421 PrintExpr(Node
->getBase());
2422 OS
<< (Node
->isArrow() ? "->" : ".");
2424 if (NestedNameSpecifier
*Qualifier
= Node
->getQualifier())
2425 Qualifier
->print(OS
, Policy
);
2426 if (Node
->hasTemplateKeyword())
2428 OS
<< Node
->getMemberNameInfo();
2429 if (Node
->hasExplicitTemplateArgs())
2430 printTemplateArgumentList(OS
, Node
->template_arguments(), Policy
);
2433 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr
*E
) {
2434 OS
<< getTraitSpelling(E
->getTrait()) << "(";
2435 for (unsigned I
= 0, N
= E
->getNumArgs(); I
!= N
; ++I
) {
2438 E
->getArg(I
)->getType().print(OS
, Policy
);
2443 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr
*E
) {
2444 OS
<< getTraitSpelling(E
->getTrait()) << '(';
2445 E
->getQueriedType().print(OS
, Policy
);
2449 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr
*E
) {
2450 OS
<< getTraitSpelling(E
->getTrait()) << '(';
2451 PrintExpr(E
->getQueriedExpression());
2455 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr
*E
) {
2457 PrintExpr(E
->getOperand());
2461 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr
*E
) {
2462 PrintExpr(E
->getPattern());
2466 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr
*E
) {
2467 OS
<< "sizeof...(" << *E
->getPack() << ")";
2470 void StmtPrinter::VisitPackIndexingExpr(PackIndexingExpr
*E
) {
2471 OS
<< E
->getPackIdExpression() << "...[" << E
->getIndexExpr() << "]";
2474 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2475 SubstNonTypeTemplateParmPackExpr
*Node
) {
2476 OS
<< *Node
->getParameterPack();
2479 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2480 SubstNonTypeTemplateParmExpr
*Node
) {
2481 Visit(Node
->getReplacement());
2484 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr
*E
) {
2485 OS
<< *E
->getParameterPack();
2488 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr
*Node
){
2489 PrintExpr(Node
->getSubExpr());
2492 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr
*E
) {
2495 PrintExpr(E
->getLHS());
2496 OS
<< " " << BinaryOperator::getOpcodeStr(E
->getOperator()) << " ";
2500 OS
<< " " << BinaryOperator::getOpcodeStr(E
->getOperator()) << " ";
2501 PrintExpr(E
->getRHS());
2506 void StmtPrinter::VisitCXXParenListInitExpr(CXXParenListInitExpr
*Node
) {
2508 llvm::interleaveComma(Node
->getInitExprs(), OS
,
2509 [&](Expr
*E
) { PrintExpr(E
); });
2513 void StmtPrinter::VisitConceptSpecializationExpr(ConceptSpecializationExpr
*E
) {
2514 NestedNameSpecifierLoc NNS
= E
->getNestedNameSpecifierLoc();
2516 NNS
.getNestedNameSpecifier()->print(OS
, Policy
);
2517 if (E
->getTemplateKWLoc().isValid())
2519 OS
<< E
->getFoundDecl()->getName();
2520 printTemplateArgumentList(OS
, E
->getTemplateArgsAsWritten()->arguments(),
2522 E
->getNamedConcept()->getTemplateParameters());
2525 void StmtPrinter::VisitRequiresExpr(RequiresExpr
*E
) {
2527 auto LocalParameters
= E
->getLocalParameters();
2528 if (!LocalParameters
.empty()) {
2530 for (ParmVarDecl
*LocalParam
: LocalParameters
) {
2531 PrintRawDecl(LocalParam
);
2532 if (LocalParam
!= LocalParameters
.back())
2539 auto Requirements
= E
->getRequirements();
2540 for (concepts::Requirement
*Req
: Requirements
) {
2541 if (auto *TypeReq
= dyn_cast
<concepts::TypeRequirement
>(Req
)) {
2542 if (TypeReq
->isSubstitutionFailure())
2543 OS
<< "<<error-type>>";
2545 TypeReq
->getType()->getType().print(OS
, Policy
);
2546 } else if (auto *ExprReq
= dyn_cast
<concepts::ExprRequirement
>(Req
)) {
2547 if (ExprReq
->isCompound())
2549 if (ExprReq
->isExprSubstitutionFailure())
2550 OS
<< "<<error-expression>>";
2552 PrintExpr(ExprReq
->getExpr());
2553 if (ExprReq
->isCompound()) {
2555 if (ExprReq
->getNoexceptLoc().isValid())
2557 const auto &RetReq
= ExprReq
->getReturnTypeRequirement();
2558 if (!RetReq
.isEmpty()) {
2560 if (RetReq
.isSubstitutionFailure())
2561 OS
<< "<<error-type>>";
2562 else if (RetReq
.isTypeConstraint())
2563 RetReq
.getTypeConstraint()->print(OS
, Policy
);
2567 auto *NestedReq
= cast
<concepts::NestedRequirement
>(Req
);
2569 if (NestedReq
->hasInvalidConstraint())
2570 OS
<< "<<error-expression>>";
2572 PrintExpr(NestedReq
->getConstraintExpr());
2581 void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt
*S
) {
2582 Visit(S
->getBody());
2585 void StmtPrinter::VisitCoreturnStmt(CoreturnStmt
*S
) {
2587 if (S
->getOperand()) {
2589 Visit(S
->getOperand());
2594 void StmtPrinter::VisitCoawaitExpr(CoawaitExpr
*S
) {
2596 PrintExpr(S
->getOperand());
2599 void StmtPrinter::VisitDependentCoawaitExpr(DependentCoawaitExpr
*S
) {
2601 PrintExpr(S
->getOperand());
2604 void StmtPrinter::VisitCoyieldExpr(CoyieldExpr
*S
) {
2606 PrintExpr(S
->getOperand());
2611 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral
*Node
) {
2613 VisitStringLiteral(Node
->getString());
2616 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr
*E
) {
2618 Visit(E
->getSubExpr());
2621 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral
*E
) {
2623 ObjCArrayLiteral::child_range Ch
= E
->children();
2624 for (auto I
= Ch
.begin(), E
= Ch
.end(); I
!= E
; ++I
) {
2625 if (I
!= Ch
.begin())
2632 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral
*E
) {
2634 for (unsigned I
= 0, N
= E
->getNumElements(); I
!= N
; ++I
) {
2638 ObjCDictionaryElement Element
= E
->getKeyValueElement(I
);
2641 Visit(Element
.Value
);
2642 if (Element
.isPackExpansion())
2648 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr
*Node
) {
2650 Node
->getEncodedType().print(OS
, Policy
);
2654 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr
*Node
) {
2656 Node
->getSelector().print(OS
);
2660 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr
*Node
) {
2661 OS
<< "@protocol(" << *Node
->getProtocol() << ')';
2664 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr
*Mess
) {
2666 switch (Mess
->getReceiverKind()) {
2667 case ObjCMessageExpr::Instance
:
2668 PrintExpr(Mess
->getInstanceReceiver());
2671 case ObjCMessageExpr::Class
:
2672 Mess
->getClassReceiver().print(OS
, Policy
);
2675 case ObjCMessageExpr::SuperInstance
:
2676 case ObjCMessageExpr::SuperClass
:
2682 Selector selector
= Mess
->getSelector();
2683 if (selector
.isUnarySelector()) {
2684 OS
<< selector
.getNameForSlot(0);
2686 for (unsigned i
= 0, e
= Mess
->getNumArgs(); i
!= e
; ++i
) {
2687 if (i
< selector
.getNumArgs()) {
2688 if (i
> 0) OS
<< ' ';
2689 if (selector
.getIdentifierInfoForSlot(i
))
2690 OS
<< selector
.getIdentifierInfoForSlot(i
)->getName() << ':';
2694 else OS
<< ", "; // Handle variadic methods.
2696 PrintExpr(Mess
->getArg(i
));
2702 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr
*Node
) {
2703 OS
<< (Node
->getValue() ? "__objc_yes" : "__objc_no");
2707 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr
*E
) {
2708 PrintExpr(E
->getSubExpr());
2712 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr
*E
) {
2713 OS
<< '(' << E
->getBridgeKindName();
2714 E
->getType().print(OS
, Policy
);
2716 PrintExpr(E
->getSubExpr());
2719 void StmtPrinter::VisitBlockExpr(BlockExpr
*Node
) {
2720 BlockDecl
*BD
= Node
->getBlockDecl();
2723 const FunctionType
*AFT
= Node
->getFunctionType();
2725 if (isa
<FunctionNoProtoType
>(AFT
)) {
2727 } else if (!BD
->param_empty() || cast
<FunctionProtoType
>(AFT
)->isVariadic()) {
2729 for (BlockDecl::param_iterator AI
= BD
->param_begin(),
2730 E
= BD
->param_end(); AI
!= E
; ++AI
) {
2731 if (AI
!= BD
->param_begin()) OS
<< ", ";
2732 std::string ParamStr
= (*AI
)->getNameAsString();
2733 (*AI
)->getType().print(OS
, Policy
, ParamStr
);
2736 const auto *FT
= cast
<FunctionProtoType
>(AFT
);
2737 if (FT
->isVariadic()) {
2738 if (!BD
->param_empty()) OS
<< ", ";
2746 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr
*Node
) {
2747 PrintExpr(Node
->getSourceExpr());
2750 void StmtPrinter::VisitTypoExpr(TypoExpr
*Node
) {
2751 // TODO: Print something reasonable for a TypoExpr, if necessary.
2752 llvm_unreachable("Cannot print TypoExpr nodes");
2755 void StmtPrinter::VisitRecoveryExpr(RecoveryExpr
*Node
) {
2756 OS
<< "<recovery-expr>(";
2757 const char *Sep
= "";
2758 for (Expr
*E
: Node
->subExpressions()) {
2766 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr
*Node
) {
2767 OS
<< "__builtin_astype(";
2768 PrintExpr(Node
->getSrcExpr());
2770 Node
->getType().print(OS
, Policy
);
2774 //===----------------------------------------------------------------------===//
2775 // Stmt method implementations
2776 //===----------------------------------------------------------------------===//
2778 void Stmt::dumpPretty(const ASTContext
&Context
) const {
2779 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context
.getLangOpts()));
2782 void Stmt::printPretty(raw_ostream
&Out
, PrinterHelper
*Helper
,
2783 const PrintingPolicy
&Policy
, unsigned Indentation
,
2784 StringRef NL
, const ASTContext
*Context
) const {
2785 StmtPrinter
P(Out
, Helper
, Policy
, Indentation
, NL
, Context
);
2786 P
.Visit(const_cast<Stmt
*>(this));
2789 void Stmt::printPrettyControlled(raw_ostream
&Out
, PrinterHelper
*Helper
,
2790 const PrintingPolicy
&Policy
,
2791 unsigned Indentation
, StringRef NL
,
2792 const ASTContext
*Context
) const {
2793 StmtPrinter
P(Out
, Helper
, Policy
, Indentation
, NL
, Context
);
2794 P
.PrintControlledStmt(const_cast<Stmt
*>(this));
2797 void Stmt::printJson(raw_ostream
&Out
, PrinterHelper
*Helper
,
2798 const PrintingPolicy
&Policy
, bool AddQuotes
) const {
2800 llvm::raw_string_ostream
TempOut(Buf
);
2802 printPretty(TempOut
, Helper
, Policy
);
2804 Out
<< JsonFormat(TempOut
.str(), AddQuotes
);
2807 //===----------------------------------------------------------------------===//
2809 //===----------------------------------------------------------------------===//
2811 // Implement virtual destructor.
2812 PrinterHelper::~PrinterHelper() = default;