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"
59 using namespace clang;
61 //===----------------------------------------------------------------------===//
62 // StmtPrinter Visitor
63 //===----------------------------------------------------------------------===//
67 class StmtPrinter : public StmtVisitor<StmtPrinter> {
70 PrinterHelper* Helper;
71 PrintingPolicy Policy;
73 const ASTContext *Context;
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) {}
82 void PrintStmt(Stmt *S) { PrintStmt(S, Policy.Indentation); }
84 void PrintStmt(Stmt *S, int SubIndent) {
85 IndentLevel += SubIndent;
86 if (S && isa<Expr>(S)) {
87 // If this is an expr used in a stmt context, indent and newline it.
94 Indent() << "<<<NULL STATEMENT>>>" << NL;
96 IndentLevel -= SubIndent;
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>(S))
103 PrintRawDeclStmt(DS);
105 PrintExpr(cast<Expr>(S));
107 IndentLevel -= (PrefixWidth + 1) / 2;
110 void PrintControlledStmt(Stmt *S) {
111 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
113 PrintRawCompoundStmt(CS);
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);
133 void PrintExpr(Expr *E) {
140 raw_ostream &Indent(int Delta = 0) {
141 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
146 void Visit(Stmt* S) {
147 if (Helper && Helper->handledStmt(S,OS))
149 else StmtVisitor<StmtPrinter>::Visit(S);
152 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
153 Indent() << "<<unknown stmt type>>" << NL;
156 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
157 OS << "<<unknown expr type>>";
160 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
162 #define ABSTRACT_STMT(CLASS)
163 #define STMT(CLASS, PARENT) \
164 void Visit##CLASS(CLASS *Node);
165 #include "clang/AST/StmtNodes.inc"
170 //===----------------------------------------------------------------------===//
171 // Stmt printing methods.
172 //===----------------------------------------------------------------------===//
174 /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
175 /// with no newline after the }.
176 void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
178 PrintFPPragmas(Node);
179 for (auto *I : Node->body())
185 void StmtPrinter::PrintFPPragmas(CompoundStmt *S) {
186 if (!S->hasStoredFPFeatures())
188 FPOptionsOverride FPO = S->getStoredFPFeatures();
189 bool FEnvAccess = false;
190 if (FPO.hasAllowFEnvAccessOverride()) {
191 FEnvAccess = FPO.getAllowFEnvAccessOverride();
192 Indent() << "#pragma STDC FENV_ACCESS " << (FEnvAccess ? "ON" : "OFF")
195 if (FPO.hasSpecifiedExceptionModeOverride()) {
196 LangOptions::FPExceptionModeKind EM =
197 FPO.getSpecifiedExceptionModeOverride();
198 if (!FEnvAccess || EM != LangOptions::FPE_Strict) {
199 Indent() << "#pragma clang fp exceptions(";
200 switch (FPO.getSpecifiedExceptionModeOverride()) {
203 case LangOptions::FPE_Ignore:
206 case LangOptions::FPE_MayTrap:
209 case LangOptions::FPE_Strict:
216 if (FPO.hasConstRoundingModeOverride()) {
217 LangOptions::RoundingMode RM = FPO.getConstRoundingModeOverride();
218 Indent() << "#pragma STDC FENV_ROUND ";
220 case llvm::RoundingMode::TowardZero:
221 OS << "FE_TOWARDZERO";
223 case llvm::RoundingMode::NearestTiesToEven:
224 OS << "FE_TONEAREST";
226 case llvm::RoundingMode::TowardPositive:
229 case llvm::RoundingMode::TowardNegative:
232 case llvm::RoundingMode::NearestTiesToAway:
233 OS << "FE_TONEARESTFROMZERO";
235 case llvm::RoundingMode::Dynamic:
239 llvm_unreachable("Invalid rounding mode");
245 void StmtPrinter::PrintRawDecl(Decl *D) {
246 D->print(OS, Policy, IndentLevel);
249 void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
250 SmallVector<Decl *, 2> Decls(S->decls());
251 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
254 void StmtPrinter::VisitNullStmt(NullStmt *Node) {
255 Indent() << ";" << NL;
258 void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
260 PrintRawDeclStmt(Node);
264 void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
266 PrintRawCompoundStmt(Node);
270 void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
271 Indent(-1) << "case ";
272 PrintExpr(Node->getLHS());
273 if (Node->getRHS()) {
275 PrintExpr(Node->getRHS());
279 PrintStmt(Node->getSubStmt(), 0);
282 void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
283 Indent(-1) << "default:" << NL;
284 PrintStmt(Node->getSubStmt(), 0);
287 void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
288 Indent(-1) << Node->getName() << ":" << NL;
289 PrintStmt(Node->getSubStmt(), 0);
292 void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
293 for (const auto *Attr : Node->getAttrs()) {
294 Attr->printPretty(OS, Policy);
297 PrintStmt(Node->getSubStmt(), 0);
300 void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
301 if (If->isConsteval()) {
303 if (If->isNegatedConsteval())
307 PrintStmt(If->getThen());
308 if (Stmt *Else = If->getElse()) {
319 PrintInitStmt(If->getInit(), 4);
320 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
321 PrintRawDeclStmt(DS);
323 PrintExpr(If->getCond());
326 if (auto *CS = dyn_cast<CompoundStmt>(If->getThen())) {
328 PrintRawCompoundStmt(CS);
329 OS << (If->getElse() ? " " : NL);
332 PrintStmt(If->getThen());
333 if (If->getElse()) Indent();
336 if (Stmt *Else = If->getElse()) {
339 if (auto *CS = dyn_cast<CompoundStmt>(Else)) {
341 PrintRawCompoundStmt(CS);
343 } else if (auto *ElseIf = dyn_cast<IfStmt>(Else)) {
345 PrintRawIfStmt(ElseIf);
348 PrintStmt(If->getElse());
353 void StmtPrinter::VisitIfStmt(IfStmt *If) {
358 void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
359 Indent() << "switch (";
361 PrintInitStmt(Node->getInit(), 8);
362 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
363 PrintRawDeclStmt(DS);
365 PrintExpr(Node->getCond());
367 PrintControlledStmt(Node->getBody());
370 void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
371 Indent() << "while (";
372 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
373 PrintRawDeclStmt(DS);
375 PrintExpr(Node->getCond());
377 PrintStmt(Node->getBody());
380 void StmtPrinter::VisitDoStmt(DoStmt *Node) {
382 if (auto *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
383 PrintRawCompoundStmt(CS);
387 PrintStmt(Node->getBody());
392 PrintExpr(Node->getCond());
396 void StmtPrinter::VisitForStmt(ForStmt *Node) {
399 PrintInitStmt(Node->getInit(), 5);
401 OS << (Node->getCond() ? "; " : ";");
403 PrintExpr(Node->getCond());
405 if (Node->getInc()) {
407 PrintExpr(Node->getInc());
410 PrintControlledStmt(Node->getBody());
413 void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
415 if (auto *DS = dyn_cast<DeclStmt>(Node->getElement()))
416 PrintRawDeclStmt(DS);
418 PrintExpr(cast<Expr>(Node->getElement()));
420 PrintExpr(Node->getCollection());
422 PrintControlledStmt(Node->getBody());
425 void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
428 PrintInitStmt(Node->getInit(), 5);
429 PrintingPolicy SubPolicy(Policy);
430 SubPolicy.SuppressInitializers = true;
431 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
433 PrintExpr(Node->getRangeInit());
435 PrintControlledStmt(Node->getBody());
438 void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
440 if (Node->isIfExists())
441 OS << "__if_exists (";
443 OS << "__if_not_exists (";
445 if (NestedNameSpecifier *Qualifier
446 = Node->getQualifierLoc().getNestedNameSpecifier())
447 Qualifier->print(OS, Policy);
449 OS << Node->getNameInfo() << ") ";
451 PrintRawCompoundStmt(Node->getSubStmt());
454 void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
455 Indent() << "goto " << Node->getLabel()->getName() << ";";
456 if (Policy.IncludeNewlines) OS << NL;
459 void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
460 Indent() << "goto *";
461 PrintExpr(Node->getTarget());
463 if (Policy.IncludeNewlines) OS << NL;
466 void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
467 Indent() << "continue;";
468 if (Policy.IncludeNewlines) OS << NL;
471 void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
472 Indent() << "break;";
473 if (Policy.IncludeNewlines) OS << NL;
476 void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
477 Indent() << "return";
478 if (Node->getRetValue()) {
480 PrintExpr(Node->getRetValue());
483 if (Policy.IncludeNewlines) OS << NL;
486 void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
489 if (Node->isVolatile())
492 if (Node->isAsmGoto())
496 VisitStringLiteral(Node->getAsmString());
499 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
500 Node->getNumClobbers() != 0 || Node->getNumLabels() != 0)
503 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
507 if (!Node->getOutputName(i).empty()) {
509 OS << Node->getOutputName(i);
513 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
515 Visit(Node->getOutputExpr(i));
520 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0 ||
521 Node->getNumLabels() != 0)
524 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
528 if (!Node->getInputName(i).empty()) {
530 OS << Node->getInputName(i);
534 VisitStringLiteral(Node->getInputConstraintLiteral(i));
536 Visit(Node->getInputExpr(i));
541 if (Node->getNumClobbers() != 0 || Node->getNumLabels())
544 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
548 VisitStringLiteral(Node->getClobberStringLiteral(i));
552 if (Node->getNumLabels() != 0)
555 for (unsigned i = 0, e = Node->getNumLabels(); i != e; ++i) {
558 OS << Node->getLabelName(i);
562 if (Policy.IncludeNewlines) OS << NL;
565 void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
566 // FIXME: Implement MS style inline asm statement printer.
567 Indent() << "__asm ";
568 if (Node->hasBraces())
570 OS << Node->getAsmString() << NL;
571 if (Node->hasBraces())
572 Indent() << "}" << NL;
575 void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
576 PrintStmt(Node->getCapturedDecl()->getBody());
579 void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
581 if (auto *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
582 PrintRawCompoundStmt(TS);
586 for (ObjCAtCatchStmt *catchStmt : Node->catch_stmts()) {
587 Indent() << "@catch(";
588 if (Decl *DS = catchStmt->getCatchParamDecl())
591 if (auto *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
592 PrintRawCompoundStmt(CS);
597 if (auto *FS = static_cast<ObjCAtFinallyStmt *>(Node->getFinallyStmt())) {
598 Indent() << "@finally";
599 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
604 void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
607 void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
608 Indent() << "@catch (...) { /* todo */ } " << NL;
611 void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
612 Indent() << "@throw";
613 if (Node->getThrowExpr()) {
615 PrintExpr(Node->getThrowExpr());
620 void StmtPrinter::VisitObjCAvailabilityCheckExpr(
621 ObjCAvailabilityCheckExpr *Node) {
622 OS << "@available(...)";
625 void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
626 Indent() << "@synchronized (";
627 PrintExpr(Node->getSynchExpr());
629 PrintRawCompoundStmt(Node->getSynchBody());
633 void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
634 Indent() << "@autoreleasepool";
635 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
639 void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
641 if (Decl *ExDecl = Node->getExceptionDecl())
642 PrintRawDecl(ExDecl);
646 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
649 void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
651 PrintRawCXXCatchStmt(Node);
655 void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
657 PrintRawCompoundStmt(Node->getTryBlock());
658 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
660 PrintRawCXXCatchStmt(Node->getHandler(i));
665 void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
666 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
667 PrintRawCompoundStmt(Node->getTryBlock());
668 SEHExceptStmt *E = Node->getExceptHandler();
669 SEHFinallyStmt *F = Node->getFinallyHandler();
671 PrintRawSEHExceptHandler(E);
673 assert(F && "Must have a finally block...");
674 PrintRawSEHFinallyStmt(F);
679 void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
681 PrintRawCompoundStmt(Node->getBlock());
685 void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
687 VisitExpr(Node->getFilterExpr());
689 PrintRawCompoundStmt(Node->getBlock());
693 void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
695 PrintRawSEHExceptHandler(Node);
699 void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
701 PrintRawSEHFinallyStmt(Node);
705 void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
706 Indent() << "__leave;";
707 if (Policy.IncludeNewlines) OS << NL;
710 //===----------------------------------------------------------------------===//
711 // OpenMP directives printing methods
712 //===----------------------------------------------------------------------===//
714 void StmtPrinter::VisitOMPCanonicalLoop(OMPCanonicalLoop *Node) {
715 PrintStmt(Node->getLoopStmt());
718 void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S,
720 OMPClausePrinter Printer(OS, Policy);
721 ArrayRef<OMPClause *> Clauses = S->clauses();
722 for (auto *Clause : Clauses)
723 if (Clause && !Clause->isImplicit()) {
725 Printer.Visit(Clause);
728 if (!ForceNoStmt && S->hasAssociatedStmt())
729 PrintStmt(S->getRawStmt());
732 void StmtPrinter::VisitOMPMetaDirective(OMPMetaDirective *Node) {
733 Indent() << "#pragma omp metadirective";
734 PrintOMPExecutableDirective(Node);
737 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
738 Indent() << "#pragma omp parallel";
739 PrintOMPExecutableDirective(Node);
742 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
743 Indent() << "#pragma omp simd";
744 PrintOMPExecutableDirective(Node);
747 void StmtPrinter::VisitOMPTileDirective(OMPTileDirective *Node) {
748 Indent() << "#pragma omp tile";
749 PrintOMPExecutableDirective(Node);
752 void StmtPrinter::VisitOMPUnrollDirective(OMPUnrollDirective *Node) {
753 Indent() << "#pragma omp unroll";
754 PrintOMPExecutableDirective(Node);
757 void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
758 Indent() << "#pragma omp for";
759 PrintOMPExecutableDirective(Node);
762 void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
763 Indent() << "#pragma omp for simd";
764 PrintOMPExecutableDirective(Node);
767 void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
768 Indent() << "#pragma omp sections";
769 PrintOMPExecutableDirective(Node);
772 void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
773 Indent() << "#pragma omp section";
774 PrintOMPExecutableDirective(Node);
777 void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
778 Indent() << "#pragma omp single";
779 PrintOMPExecutableDirective(Node);
782 void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
783 Indent() << "#pragma omp master";
784 PrintOMPExecutableDirective(Node);
787 void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
788 Indent() << "#pragma omp critical";
789 if (Node->getDirectiveName().getName()) {
791 Node->getDirectiveName().printName(OS, Policy);
794 PrintOMPExecutableDirective(Node);
797 void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
798 Indent() << "#pragma omp parallel for";
799 PrintOMPExecutableDirective(Node);
802 void StmtPrinter::VisitOMPParallelForSimdDirective(
803 OMPParallelForSimdDirective *Node) {
804 Indent() << "#pragma omp parallel for simd";
805 PrintOMPExecutableDirective(Node);
808 void StmtPrinter::VisitOMPParallelMasterDirective(
809 OMPParallelMasterDirective *Node) {
810 Indent() << "#pragma omp parallel master";
811 PrintOMPExecutableDirective(Node);
814 void StmtPrinter::VisitOMPParallelMaskedDirective(
815 OMPParallelMaskedDirective *Node) {
816 Indent() << "#pragma omp parallel masked";
817 PrintOMPExecutableDirective(Node);
820 void StmtPrinter::VisitOMPParallelSectionsDirective(
821 OMPParallelSectionsDirective *Node) {
822 Indent() << "#pragma omp parallel sections";
823 PrintOMPExecutableDirective(Node);
826 void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
827 Indent() << "#pragma omp task";
828 PrintOMPExecutableDirective(Node);
831 void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
832 Indent() << "#pragma omp taskyield";
833 PrintOMPExecutableDirective(Node);
836 void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
837 Indent() << "#pragma omp barrier";
838 PrintOMPExecutableDirective(Node);
841 void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
842 Indent() << "#pragma omp taskwait";
843 PrintOMPExecutableDirective(Node);
846 void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
847 Indent() << "#pragma omp taskgroup";
848 PrintOMPExecutableDirective(Node);
851 void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
852 Indent() << "#pragma omp flush";
853 PrintOMPExecutableDirective(Node);
856 void StmtPrinter::VisitOMPDepobjDirective(OMPDepobjDirective *Node) {
857 Indent() << "#pragma omp depobj";
858 PrintOMPExecutableDirective(Node);
861 void StmtPrinter::VisitOMPScanDirective(OMPScanDirective *Node) {
862 Indent() << "#pragma omp scan";
863 PrintOMPExecutableDirective(Node);
866 void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
867 Indent() << "#pragma omp ordered";
868 PrintOMPExecutableDirective(Node, Node->hasClausesOfKind<OMPDependClause>());
871 void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
872 Indent() << "#pragma omp atomic";
873 PrintOMPExecutableDirective(Node);
876 void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
877 Indent() << "#pragma omp target";
878 PrintOMPExecutableDirective(Node);
881 void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
882 Indent() << "#pragma omp target data";
883 PrintOMPExecutableDirective(Node);
886 void StmtPrinter::VisitOMPTargetEnterDataDirective(
887 OMPTargetEnterDataDirective *Node) {
888 Indent() << "#pragma omp target enter data";
889 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
892 void StmtPrinter::VisitOMPTargetExitDataDirective(
893 OMPTargetExitDataDirective *Node) {
894 Indent() << "#pragma omp target exit data";
895 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
898 void StmtPrinter::VisitOMPTargetParallelDirective(
899 OMPTargetParallelDirective *Node) {
900 Indent() << "#pragma omp target parallel";
901 PrintOMPExecutableDirective(Node);
904 void StmtPrinter::VisitOMPTargetParallelForDirective(
905 OMPTargetParallelForDirective *Node) {
906 Indent() << "#pragma omp target parallel for";
907 PrintOMPExecutableDirective(Node);
910 void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
911 Indent() << "#pragma omp teams";
912 PrintOMPExecutableDirective(Node);
915 void StmtPrinter::VisitOMPCancellationPointDirective(
916 OMPCancellationPointDirective *Node) {
917 Indent() << "#pragma omp cancellation point "
918 << getOpenMPDirectiveName(Node->getCancelRegion());
919 PrintOMPExecutableDirective(Node);
922 void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
923 Indent() << "#pragma omp cancel "
924 << getOpenMPDirectiveName(Node->getCancelRegion());
925 PrintOMPExecutableDirective(Node);
928 void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) {
929 Indent() << "#pragma omp taskloop";
930 PrintOMPExecutableDirective(Node);
933 void StmtPrinter::VisitOMPTaskLoopSimdDirective(
934 OMPTaskLoopSimdDirective *Node) {
935 Indent() << "#pragma omp taskloop simd";
936 PrintOMPExecutableDirective(Node);
939 void StmtPrinter::VisitOMPMasterTaskLoopDirective(
940 OMPMasterTaskLoopDirective *Node) {
941 Indent() << "#pragma omp master taskloop";
942 PrintOMPExecutableDirective(Node);
945 void StmtPrinter::VisitOMPMaskedTaskLoopDirective(
946 OMPMaskedTaskLoopDirective *Node) {
947 Indent() << "#pragma omp masked taskloop";
948 PrintOMPExecutableDirective(Node);
951 void StmtPrinter::VisitOMPMasterTaskLoopSimdDirective(
952 OMPMasterTaskLoopSimdDirective *Node) {
953 Indent() << "#pragma omp master taskloop simd";
954 PrintOMPExecutableDirective(Node);
957 void StmtPrinter::VisitOMPMaskedTaskLoopSimdDirective(
958 OMPMaskedTaskLoopSimdDirective *Node) {
959 Indent() << "#pragma omp masked taskloop simd";
960 PrintOMPExecutableDirective(Node);
963 void StmtPrinter::VisitOMPParallelMasterTaskLoopDirective(
964 OMPParallelMasterTaskLoopDirective *Node) {
965 Indent() << "#pragma omp parallel master taskloop";
966 PrintOMPExecutableDirective(Node);
969 void StmtPrinter::VisitOMPParallelMaskedTaskLoopDirective(
970 OMPParallelMaskedTaskLoopDirective *Node) {
971 Indent() << "#pragma omp parallel masked taskloop";
972 PrintOMPExecutableDirective(Node);
975 void StmtPrinter::VisitOMPParallelMasterTaskLoopSimdDirective(
976 OMPParallelMasterTaskLoopSimdDirective *Node) {
977 Indent() << "#pragma omp parallel master taskloop simd";
978 PrintOMPExecutableDirective(Node);
981 void StmtPrinter::VisitOMPParallelMaskedTaskLoopSimdDirective(
982 OMPParallelMaskedTaskLoopSimdDirective *Node) {
983 Indent() << "#pragma omp parallel masked taskloop simd";
984 PrintOMPExecutableDirective(Node);
987 void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) {
988 Indent() << "#pragma omp distribute";
989 PrintOMPExecutableDirective(Node);
992 void StmtPrinter::VisitOMPTargetUpdateDirective(
993 OMPTargetUpdateDirective *Node) {
994 Indent() << "#pragma omp target update";
995 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
998 void StmtPrinter::VisitOMPDistributeParallelForDirective(
999 OMPDistributeParallelForDirective *Node) {
1000 Indent() << "#pragma omp distribute parallel for";
1001 PrintOMPExecutableDirective(Node);
1004 void StmtPrinter::VisitOMPDistributeParallelForSimdDirective(
1005 OMPDistributeParallelForSimdDirective *Node) {
1006 Indent() << "#pragma omp distribute parallel for simd";
1007 PrintOMPExecutableDirective(Node);
1010 void StmtPrinter::VisitOMPDistributeSimdDirective(
1011 OMPDistributeSimdDirective *Node) {
1012 Indent() << "#pragma omp distribute simd";
1013 PrintOMPExecutableDirective(Node);
1016 void StmtPrinter::VisitOMPTargetParallelForSimdDirective(
1017 OMPTargetParallelForSimdDirective *Node) {
1018 Indent() << "#pragma omp target parallel for simd";
1019 PrintOMPExecutableDirective(Node);
1022 void StmtPrinter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective *Node) {
1023 Indent() << "#pragma omp target simd";
1024 PrintOMPExecutableDirective(Node);
1027 void StmtPrinter::VisitOMPTeamsDistributeDirective(
1028 OMPTeamsDistributeDirective *Node) {
1029 Indent() << "#pragma omp teams distribute";
1030 PrintOMPExecutableDirective(Node);
1033 void StmtPrinter::VisitOMPTeamsDistributeSimdDirective(
1034 OMPTeamsDistributeSimdDirective *Node) {
1035 Indent() << "#pragma omp teams distribute simd";
1036 PrintOMPExecutableDirective(Node);
1039 void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective(
1040 OMPTeamsDistributeParallelForSimdDirective *Node) {
1041 Indent() << "#pragma omp teams distribute parallel for simd";
1042 PrintOMPExecutableDirective(Node);
1045 void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective(
1046 OMPTeamsDistributeParallelForDirective *Node) {
1047 Indent() << "#pragma omp teams distribute parallel for";
1048 PrintOMPExecutableDirective(Node);
1051 void StmtPrinter::VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective *Node) {
1052 Indent() << "#pragma omp target teams";
1053 PrintOMPExecutableDirective(Node);
1056 void StmtPrinter::VisitOMPTargetTeamsDistributeDirective(
1057 OMPTargetTeamsDistributeDirective *Node) {
1058 Indent() << "#pragma omp target teams distribute";
1059 PrintOMPExecutableDirective(Node);
1062 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective(
1063 OMPTargetTeamsDistributeParallelForDirective *Node) {
1064 Indent() << "#pragma omp target teams distribute parallel for";
1065 PrintOMPExecutableDirective(Node);
1068 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
1069 OMPTargetTeamsDistributeParallelForSimdDirective *Node) {
1070 Indent() << "#pragma omp target teams distribute parallel for simd";
1071 PrintOMPExecutableDirective(Node);
1074 void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective(
1075 OMPTargetTeamsDistributeSimdDirective *Node) {
1076 Indent() << "#pragma omp target teams distribute simd";
1077 PrintOMPExecutableDirective(Node);
1080 void StmtPrinter::VisitOMPInteropDirective(OMPInteropDirective *Node) {
1081 Indent() << "#pragma omp interop";
1082 PrintOMPExecutableDirective(Node);
1085 void StmtPrinter::VisitOMPDispatchDirective(OMPDispatchDirective *Node) {
1086 Indent() << "#pragma omp dispatch";
1087 PrintOMPExecutableDirective(Node);
1090 void StmtPrinter::VisitOMPMaskedDirective(OMPMaskedDirective *Node) {
1091 Indent() << "#pragma omp masked";
1092 PrintOMPExecutableDirective(Node);
1095 void StmtPrinter::VisitOMPGenericLoopDirective(OMPGenericLoopDirective *Node) {
1096 Indent() << "#pragma omp loop";
1097 PrintOMPExecutableDirective(Node);
1100 void StmtPrinter::VisitOMPTeamsGenericLoopDirective(
1101 OMPTeamsGenericLoopDirective *Node) {
1102 Indent() << "#pragma omp teams loop";
1103 PrintOMPExecutableDirective(Node);
1106 void StmtPrinter::VisitOMPTargetTeamsGenericLoopDirective(
1107 OMPTargetTeamsGenericLoopDirective *Node) {
1108 Indent() << "#pragma omp target teams loop";
1109 PrintOMPExecutableDirective(Node);
1112 void StmtPrinter::VisitOMPParallelGenericLoopDirective(
1113 OMPParallelGenericLoopDirective *Node) {
1114 Indent() << "#pragma omp parallel loop";
1115 PrintOMPExecutableDirective(Node);
1118 void StmtPrinter::VisitOMPTargetParallelGenericLoopDirective(
1119 OMPTargetParallelGenericLoopDirective *Node) {
1120 Indent() << "#pragma omp target parallel loop";
1121 PrintOMPExecutableDirective(Node);
1124 //===----------------------------------------------------------------------===//
1125 // Expr printing methods.
1126 //===----------------------------------------------------------------------===//
1128 void StmtPrinter::VisitSourceLocExpr(SourceLocExpr *Node) {
1129 OS << Node->getBuiltinStr() << "()";
1132 void StmtPrinter::VisitConstantExpr(ConstantExpr *Node) {
1133 PrintExpr(Node->getSubExpr());
1136 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
1137 if (const auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) {
1138 OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy);
1141 if (const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(Node->getDecl())) {
1142 TPOD->printAsExpr(OS, Policy);
1145 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1146 Qualifier->print(OS, Policy);
1147 if (Node->hasTemplateKeyword())
1149 if (Policy.CleanUglifiedParameters &&
1150 isa<ParmVarDecl, NonTypeTemplateParmDecl>(Node->getDecl()) &&
1151 Node->getDecl()->getIdentifier())
1152 OS << Node->getDecl()->getIdentifier()->deuglifiedName();
1154 Node->getNameInfo().printName(OS, Policy);
1155 if (Node->hasExplicitTemplateArgs()) {
1156 const TemplateParameterList *TPL = nullptr;
1157 if (!Node->hadMultipleCandidates())
1158 if (auto *TD = dyn_cast<TemplateDecl>(Node->getDecl()))
1159 TPL = TD->getTemplateParameters();
1160 printTemplateArgumentList(OS, Node->template_arguments(), Policy, TPL);
1164 void StmtPrinter::VisitDependentScopeDeclRefExpr(
1165 DependentScopeDeclRefExpr *Node) {
1166 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1167 Qualifier->print(OS, Policy);
1168 if (Node->hasTemplateKeyword())
1170 OS << Node->getNameInfo();
1171 if (Node->hasExplicitTemplateArgs())
1172 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1175 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
1176 if (Node->getQualifier())
1177 Node->getQualifier()->print(OS, Policy);
1178 if (Node->hasTemplateKeyword())
1180 OS << Node->getNameInfo();
1181 if (Node->hasExplicitTemplateArgs())
1182 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1185 static bool isImplicitSelf(const Expr *E) {
1186 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
1187 if (const auto *PD = dyn_cast<ImplicitParamDecl>(DRE->getDecl())) {
1188 if (PD->getParameterKind() == ImplicitParamDecl::ObjCSelf &&
1189 DRE->getBeginLoc().isInvalid())
1196 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
1197 if (Node->getBase()) {
1198 if (!Policy.SuppressImplicitBase ||
1199 !isImplicitSelf(Node->getBase()->IgnoreImpCasts())) {
1200 PrintExpr(Node->getBase());
1201 OS << (Node->isArrow() ? "->" : ".");
1204 OS << *Node->getDecl();
1207 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
1208 if (Node->isSuperReceiver())
1210 else if (Node->isObjectReceiver() && Node->getBase()) {
1211 PrintExpr(Node->getBase());
1213 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1214 OS << Node->getClassReceiver()->getName() << ".";
1217 if (Node->isImplicitProperty()) {
1218 if (const auto *Getter = Node->getImplicitPropertyGetter())
1219 Getter->getSelector().print(OS);
1221 OS << SelectorTable::getPropertyNameFromSetterSelector(
1222 Node->getImplicitPropertySetter()->getSelector());
1224 OS << Node->getExplicitProperty()->getName();
1227 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1228 PrintExpr(Node->getBaseExpr());
1230 PrintExpr(Node->getKeyExpr());
1234 void StmtPrinter::VisitSYCLUniqueStableNameExpr(
1235 SYCLUniqueStableNameExpr *Node) {
1236 OS << "__builtin_sycl_unique_stable_name(";
1237 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1241 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
1242 OS << PredefinedExpr::getIdentKindName(Node->getIdentKind());
1245 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
1246 CharacterLiteral::print(Node->getValue(), Node->getKind(), OS);
1249 /// Prints the given expression using the original source text. Returns true on
1250 /// success, false otherwise.
1251 static bool printExprAsWritten(raw_ostream &OS, Expr *E,
1252 const ASTContext *Context) {
1255 bool Invalid = false;
1256 StringRef Source = Lexer::getSourceText(
1257 CharSourceRange::getTokenRange(E->getSourceRange()),
1258 Context->getSourceManager(), Context->getLangOpts(), &Invalid);
1266 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
1267 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1269 bool isSigned = Node->getType()->isSignedIntegerType();
1270 OS << toString(Node->getValue(), 10, isSigned);
1272 if (isa<BitIntType>(Node->getType())) {
1273 OS << (isSigned ? "wb" : "uwb");
1277 // Emit suffixes. Integer literals are always a builtin integer type.
1278 switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1279 default: llvm_unreachable("Unexpected type for integer literal!");
1280 case BuiltinType::Char_S:
1281 case BuiltinType::Char_U: OS << "i8"; break;
1282 case BuiltinType::UChar: OS << "Ui8"; break;
1283 case BuiltinType::Short: OS << "i16"; break;
1284 case BuiltinType::UShort: OS << "Ui16"; break;
1285 case BuiltinType::Int: break; // no suffix.
1286 case BuiltinType::UInt: OS << 'U'; break;
1287 case BuiltinType::Long: OS << 'L'; break;
1288 case BuiltinType::ULong: OS << "UL"; break;
1289 case BuiltinType::LongLong: OS << "LL"; break;
1290 case BuiltinType::ULongLong: OS << "ULL"; break;
1291 case BuiltinType::Int128:
1292 break; // no suffix.
1293 case BuiltinType::UInt128:
1294 break; // no suffix.
1298 void StmtPrinter::VisitFixedPointLiteral(FixedPointLiteral *Node) {
1299 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1301 OS << Node->getValueAsString(/*Radix=*/10);
1303 switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1304 default: llvm_unreachable("Unexpected type for fixed point literal!");
1305 case BuiltinType::ShortFract: OS << "hr"; break;
1306 case BuiltinType::ShortAccum: OS << "hk"; break;
1307 case BuiltinType::UShortFract: OS << "uhr"; break;
1308 case BuiltinType::UShortAccum: OS << "uhk"; break;
1309 case BuiltinType::Fract: OS << "r"; break;
1310 case BuiltinType::Accum: OS << "k"; break;
1311 case BuiltinType::UFract: OS << "ur"; break;
1312 case BuiltinType::UAccum: OS << "uk"; break;
1313 case BuiltinType::LongFract: OS << "lr"; break;
1314 case BuiltinType::LongAccum: OS << "lk"; break;
1315 case BuiltinType::ULongFract: OS << "ulr"; break;
1316 case BuiltinType::ULongAccum: OS << "ulk"; break;
1320 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1322 SmallString<16> Str;
1323 Node->getValue().toString(Str);
1325 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1326 OS << '.'; // Trailing dot in order to separate from ints.
1331 // Emit suffixes. Float literals are always a builtin float type.
1332 switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1333 default: llvm_unreachable("Unexpected type for float literal!");
1334 case BuiltinType::Half: break; // FIXME: suffix?
1335 case BuiltinType::Ibm128: break; // FIXME: No suffix for ibm128 literal
1336 case BuiltinType::Double: break; // no suffix.
1337 case BuiltinType::Float16: OS << "F16"; break;
1338 case BuiltinType::Float: OS << 'F'; break;
1339 case BuiltinType::LongDouble: OS << 'L'; break;
1340 case BuiltinType::Float128: OS << 'Q'; break;
1344 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1345 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1347 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
1350 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1351 PrintExpr(Node->getSubExpr());
1355 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
1356 Str->outputString(OS);
1359 void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1361 PrintExpr(Node->getSubExpr());
1365 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
1366 if (!Node->isPostfix()) {
1367 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1369 // Print a space if this is an "identifier operator" like __real, or if
1370 // it might be concatenated incorrectly like '+'.
1371 switch (Node->getOpcode()) {
1380 if (isa<UnaryOperator>(Node->getSubExpr()))
1385 PrintExpr(Node->getSubExpr());
1387 if (Node->isPostfix())
1388 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1391 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1392 OS << "__builtin_offsetof(";
1393 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1395 bool PrintedSomething = false;
1396 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1397 OffsetOfNode ON = Node->getComponent(i);
1398 if (ON.getKind() == OffsetOfNode::Array) {
1401 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1403 PrintedSomething = true;
1407 // Skip implicit base indirections.
1408 if (ON.getKind() == OffsetOfNode::Base)
1411 // Field or identifier node.
1412 IdentifierInfo *Id = ON.getFieldName();
1416 if (PrintedSomething)
1419 PrintedSomething = true;
1420 OS << Id->getName();
1425 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(
1426 UnaryExprOrTypeTraitExpr *Node) {
1427 const char *Spelling = getTraitSpelling(Node->getKind());
1428 if (Node->getKind() == UETT_AlignOf) {
1430 Spelling = "alignof";
1431 else if (Policy.UnderscoreAlignof)
1432 Spelling = "_Alignof";
1434 Spelling = "__alignof";
1439 if (Node->isArgumentType()) {
1441 Node->getArgumentType().print(OS, Policy);
1445 PrintExpr(Node->getArgumentExpr());
1449 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1451 PrintExpr(Node->getControllingExpr());
1452 for (const GenericSelectionExpr::Association Assoc : Node->associations()) {
1454 QualType T = Assoc.getType();
1458 T.print(OS, Policy);
1460 PrintExpr(Assoc.getAssociationExpr());
1465 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
1466 PrintExpr(Node->getLHS());
1468 PrintExpr(Node->getRHS());
1472 void StmtPrinter::VisitMatrixSubscriptExpr(MatrixSubscriptExpr *Node) {
1473 PrintExpr(Node->getBase());
1475 PrintExpr(Node->getRowIdx());
1478 PrintExpr(Node->getColumnIdx());
1482 void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) {
1483 PrintExpr(Node->getBase());
1485 if (Node->getLowerBound())
1486 PrintExpr(Node->getLowerBound());
1487 if (Node->getColonLocFirst().isValid()) {
1489 if (Node->getLength())
1490 PrintExpr(Node->getLength());
1492 if (Node->getColonLocSecond().isValid()) {
1494 if (Node->getStride())
1495 PrintExpr(Node->getStride());
1500 void StmtPrinter::VisitOMPArrayShapingExpr(OMPArrayShapingExpr *Node) {
1502 for (Expr *E : Node->getDimensions()) {
1508 PrintExpr(Node->getBase());
1511 void StmtPrinter::VisitOMPIteratorExpr(OMPIteratorExpr *Node) {
1513 for (unsigned I = 0, E = Node->numOfIterators(); I < E; ++I) {
1514 auto *VD = cast<ValueDecl>(Node->getIteratorDecl(I));
1515 VD->getType().print(OS, Policy);
1516 const OMPIteratorExpr::IteratorRange Range = Node->getIteratorRange(I);
1517 OS << " " << VD->getName() << " = ";
1518 PrintExpr(Range.Begin);
1520 PrintExpr(Range.End);
1523 PrintExpr(Range.Step);
1531 void StmtPrinter::PrintCallArgs(CallExpr *Call) {
1532 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
1533 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1534 // Don't print any defaulted arguments
1539 PrintExpr(Call->getArg(i));
1543 void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1544 PrintExpr(Call->getCallee());
1546 PrintCallArgs(Call);
1550 static bool isImplicitThis(const Expr *E) {
1551 if (const auto *TE = dyn_cast<CXXThisExpr>(E))
1552 return TE->isImplicit();
1556 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1557 if (!Policy.SuppressImplicitBase || !isImplicitThis(Node->getBase())) {
1558 PrintExpr(Node->getBase());
1560 auto *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1561 FieldDecl *ParentDecl =
1562 ParentMember ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl())
1565 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1566 OS << (Node->isArrow() ? "->" : ".");
1569 if (auto *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1570 if (FD->isAnonymousStructOrUnion())
1573 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1574 Qualifier->print(OS, Policy);
1575 if (Node->hasTemplateKeyword())
1577 OS << Node->getMemberNameInfo();
1578 const TemplateParameterList *TPL = nullptr;
1579 if (auto *FD = dyn_cast<FunctionDecl>(Node->getMemberDecl())) {
1580 if (!Node->hadMultipleCandidates())
1581 if (auto *FTD = FD->getPrimaryTemplate())
1582 TPL = FTD->getTemplateParameters();
1583 } else if (auto *VTSD =
1584 dyn_cast<VarTemplateSpecializationDecl>(Node->getMemberDecl()))
1585 TPL = VTSD->getSpecializedTemplate()->getTemplateParameters();
1586 if (Node->hasExplicitTemplateArgs())
1587 printTemplateArgumentList(OS, Node->template_arguments(), Policy, TPL);
1590 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1591 PrintExpr(Node->getBase());
1592 OS << (Node->isArrow() ? "->isa" : ".isa");
1595 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1596 PrintExpr(Node->getBase());
1598 OS << Node->getAccessor().getName();
1601 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1603 Node->getTypeAsWritten().print(OS, Policy);
1605 PrintExpr(Node->getSubExpr());
1608 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1610 Node->getType().print(OS, Policy);
1612 PrintExpr(Node->getInitializer());
1615 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1616 // No need to print anything, simply forward to the subexpression.
1617 PrintExpr(Node->getSubExpr());
1620 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1621 PrintExpr(Node->getLHS());
1622 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1623 PrintExpr(Node->getRHS());
1626 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1627 PrintExpr(Node->getLHS());
1628 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1629 PrintExpr(Node->getRHS());
1632 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1633 PrintExpr(Node->getCond());
1635 PrintExpr(Node->getLHS());
1637 PrintExpr(Node->getRHS());
1643 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1644 PrintExpr(Node->getCommon());
1646 PrintExpr(Node->getFalseExpr());
1649 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1650 OS << "&&" << Node->getLabel()->getName();
1653 void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1655 PrintRawCompoundStmt(E->getSubStmt());
1659 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1660 OS << "__builtin_choose_expr(";
1661 PrintExpr(Node->getCond());
1663 PrintExpr(Node->getLHS());
1665 PrintExpr(Node->getRHS());
1669 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1673 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1674 OS << "__builtin_shufflevector(";
1675 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1677 PrintExpr(Node->getExpr(i));
1682 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1683 OS << "__builtin_convertvector(";
1684 PrintExpr(Node->getSrcExpr());
1686 Node->getType().print(OS, Policy);
1690 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1691 if (Node->getSyntacticForm()) {
1692 Visit(Node->getSyntacticForm());
1697 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1699 if (Node->getInit(i))
1700 PrintExpr(Node->getInit(i));
1707 void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) {
1708 // There's no way to express this expression in any of our supported
1709 // languages, so just emit something terse and (hopefully) clear.
1711 PrintExpr(Node->getSubExpr());
1715 void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) {
1719 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1721 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1723 PrintExpr(Node->getExpr(i));
1728 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1729 bool NeedsEquals = true;
1730 for (const DesignatedInitExpr::Designator &D : Node->designators()) {
1731 if (D.isFieldDesignator()) {
1732 if (D.getDotLoc().isInvalid()) {
1733 if (IdentifierInfo *II = D.getFieldName()) {
1734 OS << II->getName() << ":";
1735 NeedsEquals = false;
1738 OS << "." << D.getFieldName()->getName();
1742 if (D.isArrayDesignator()) {
1743 PrintExpr(Node->getArrayIndex(D));
1745 PrintExpr(Node->getArrayRangeStart(D));
1747 PrintExpr(Node->getArrayRangeEnd(D));
1757 PrintExpr(Node->getInit());
1760 void StmtPrinter::VisitDesignatedInitUpdateExpr(
1761 DesignatedInitUpdateExpr *Node) {
1764 PrintExpr(Node->getBase());
1767 OS << "/*updater*/";
1768 PrintExpr(Node->getUpdater());
1772 void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1773 OS << "/*no init*/";
1776 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1777 if (Node->getType()->getAsCXXRecordDecl()) {
1778 OS << "/*implicit*/";
1779 Node->getType().print(OS, Policy);
1782 OS << "/*implicit*/(";
1783 Node->getType().print(OS, Policy);
1785 if (Node->getType()->isRecordType())
1792 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1793 OS << "__builtin_va_arg(";
1794 PrintExpr(Node->getSubExpr());
1796 Node->getType().print(OS, Policy);
1800 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1801 PrintExpr(Node->getSyntacticForm());
1804 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1805 const char *Name = nullptr;
1806 switch (Node->getOp()) {
1807 #define BUILTIN(ID, TYPE, ATTRS)
1808 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1809 case AtomicExpr::AO ## ID: \
1812 #include "clang/Basic/Builtins.def"
1816 // AtomicExpr stores its subexpressions in a permuted order.
1817 PrintExpr(Node->getPtr());
1818 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1819 Node->getOp() != AtomicExpr::AO__atomic_load_n &&
1820 Node->getOp() != AtomicExpr::AO__opencl_atomic_load &&
1821 Node->getOp() != AtomicExpr::AO__hip_atomic_load) {
1823 PrintExpr(Node->getVal1());
1825 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1826 Node->isCmpXChg()) {
1828 PrintExpr(Node->getVal2());
1830 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1831 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1833 PrintExpr(Node->getWeak());
1835 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init &&
1836 Node->getOp() != AtomicExpr::AO__opencl_atomic_init) {
1838 PrintExpr(Node->getOrder());
1840 if (Node->isCmpXChg()) {
1842 PrintExpr(Node->getOrderFail());
1848 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1849 OverloadedOperatorKind Kind = Node->getOperator();
1850 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1851 if (Node->getNumArgs() == 1) {
1852 OS << getOperatorSpelling(Kind) << ' ';
1853 PrintExpr(Node->getArg(0));
1855 PrintExpr(Node->getArg(0));
1856 OS << ' ' << getOperatorSpelling(Kind);
1858 } else if (Kind == OO_Arrow) {
1859 PrintExpr(Node->getArg(0));
1860 } else if (Kind == OO_Call || Kind == OO_Subscript) {
1861 PrintExpr(Node->getArg(0));
1862 OS << (Kind == OO_Call ? '(' : '[');
1863 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1866 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1867 PrintExpr(Node->getArg(ArgIdx));
1869 OS << (Kind == OO_Call ? ')' : ']');
1870 } else if (Node->getNumArgs() == 1) {
1871 OS << getOperatorSpelling(Kind) << ' ';
1872 PrintExpr(Node->getArg(0));
1873 } else if (Node->getNumArgs() == 2) {
1874 PrintExpr(Node->getArg(0));
1875 OS << ' ' << getOperatorSpelling(Kind) << ' ';
1876 PrintExpr(Node->getArg(1));
1878 llvm_unreachable("unknown overloaded operator");
1882 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1883 // If we have a conversion operator call only print the argument.
1884 CXXMethodDecl *MD = Node->getMethodDecl();
1885 if (MD && isa<CXXConversionDecl>(MD)) {
1886 PrintExpr(Node->getImplicitObjectArgument());
1889 VisitCallExpr(cast<CallExpr>(Node));
1892 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1893 PrintExpr(Node->getCallee());
1895 PrintCallArgs(Node->getConfig());
1897 PrintCallArgs(Node);
1901 void StmtPrinter::VisitCXXRewrittenBinaryOperator(
1902 CXXRewrittenBinaryOperator *Node) {
1903 CXXRewrittenBinaryOperator::DecomposedForm Decomposed =
1904 Node->getDecomposedForm();
1905 PrintExpr(const_cast<Expr*>(Decomposed.LHS));
1906 OS << ' ' << BinaryOperator::getOpcodeStr(Decomposed.Opcode) << ' ';
1907 PrintExpr(const_cast<Expr*>(Decomposed.RHS));
1910 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1911 OS << Node->getCastName() << '<';
1912 Node->getTypeAsWritten().print(OS, Policy);
1914 PrintExpr(Node->getSubExpr());
1918 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1919 VisitCXXNamedCastExpr(Node);
1922 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1923 VisitCXXNamedCastExpr(Node);
1926 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1927 VisitCXXNamedCastExpr(Node);
1930 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1931 VisitCXXNamedCastExpr(Node);
1934 void StmtPrinter::VisitBuiltinBitCastExpr(BuiltinBitCastExpr *Node) {
1935 OS << "__builtin_bit_cast(";
1936 Node->getTypeInfoAsWritten()->getType().print(OS, Policy);
1938 PrintExpr(Node->getSubExpr());
1942 void StmtPrinter::VisitCXXAddrspaceCastExpr(CXXAddrspaceCastExpr *Node) {
1943 VisitCXXNamedCastExpr(Node);
1946 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1948 if (Node->isTypeOperand()) {
1949 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1951 PrintExpr(Node->getExprOperand());
1956 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1958 if (Node->isTypeOperand()) {
1959 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1961 PrintExpr(Node->getExprOperand());
1966 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1967 PrintExpr(Node->getBaseExpr());
1968 if (Node->isArrow())
1972 if (NestedNameSpecifier *Qualifier =
1973 Node->getQualifierLoc().getNestedNameSpecifier())
1974 Qualifier->print(OS, Policy);
1975 OS << Node->getPropertyDecl()->getDeclName();
1978 void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
1979 PrintExpr(Node->getBase());
1981 PrintExpr(Node->getIdx());
1985 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1986 switch (Node->getLiteralOperatorKind()) {
1987 case UserDefinedLiteral::LOK_Raw:
1988 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
1990 case UserDefinedLiteral::LOK_Template: {
1991 const auto *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1992 const TemplateArgumentList *Args =
1993 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1996 if (Args->size() != 1) {
1997 const TemplateParameterList *TPL = nullptr;
1998 if (!DRE->hadMultipleCandidates())
1999 if (const auto *TD = dyn_cast<TemplateDecl>(DRE->getDecl()))
2000 TPL = TD->getTemplateParameters();
2001 OS << "operator\"\"" << Node->getUDSuffix()->getName();
2002 printTemplateArgumentList(OS, Args->asArray(), Policy, TPL);
2007 const TemplateArgument &Pack = Args->get(0);
2008 for (const auto &P : Pack.pack_elements()) {
2009 char C = (char)P.getAsIntegral().getZExtValue();
2014 case UserDefinedLiteral::LOK_Integer: {
2015 // Print integer literal without suffix.
2016 const auto *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
2017 OS << toString(Int->getValue(), 10, /*isSigned*/false);
2020 case UserDefinedLiteral::LOK_Floating: {
2021 // Print floating literal without suffix.
2022 auto *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
2023 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
2026 case UserDefinedLiteral::LOK_String:
2027 case UserDefinedLiteral::LOK_Character:
2028 PrintExpr(Node->getCookedLiteral());
2031 OS << Node->getUDSuffix()->getName();
2034 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
2035 OS << (Node->getValue() ? "true" : "false");
2038 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
2042 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
2046 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
2047 if (!Node->getSubExpr())
2051 PrintExpr(Node->getSubExpr());
2055 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
2056 // Nothing to print: we picked up the default argument.
2059 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
2060 // Nothing to print: we picked up the default initializer.
2063 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
2064 auto TargetType = Node->getType();
2065 auto *Auto = TargetType->getContainedDeducedType();
2066 bool Bare = Auto && Auto->isDeduced();
2068 // Parenthesize deduced casts.
2071 TargetType.print(OS, Policy);
2075 // No extra braces surrounding the inner construct.
2076 if (!Node->isListInitialization())
2078 PrintExpr(Node->getSubExpr());
2079 if (!Node->isListInitialization())
2083 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
2084 PrintExpr(Node->getSubExpr());
2087 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
2088 Node->getType().print(OS, Policy);
2089 if (Node->isStdInitListInitialization())
2090 /* Nothing to do; braces are part of creating the std::initializer_list. */;
2091 else if (Node->isListInitialization())
2095 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
2096 ArgEnd = Node->arg_end();
2097 Arg != ArgEnd; ++Arg) {
2098 if ((*Arg)->isDefaultArgument())
2100 if (Arg != Node->arg_begin())
2104 if (Node->isStdInitListInitialization())
2106 else if (Node->isListInitialization())
2112 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
2114 bool NeedComma = false;
2115 switch (Node->getCaptureDefault()) {
2129 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
2130 CEnd = Node->explicit_capture_end();
2133 if (C->capturesVLAType())
2140 switch (C->getCaptureKind()) {
2150 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
2152 OS << C->getCapturedVar()->getName();
2156 OS << C->getCapturedVar()->getName();
2160 llvm_unreachable("VLA type in explicit captures.");
2163 if (C->isPackExpansion())
2166 if (Node->isInitCapture(C)) {
2167 VarDecl *D = C->getCapturedVar();
2169 llvm::StringRef Pre;
2170 llvm::StringRef Post;
2171 if (D->getInitStyle() == VarDecl::CallInit &&
2172 !isa<ParenListExpr>(D->getInit())) {
2175 } else if (D->getInitStyle() == VarDecl::CInit) {
2180 PrintExpr(D->getInit());
2186 if (!Node->getExplicitTemplateParameters().empty()) {
2187 Node->getTemplateParameterList()->print(
2188 OS, Node->getLambdaClass()->getASTContext(),
2189 /*OmitTemplateKW*/true);
2192 if (Node->hasExplicitParameters()) {
2194 CXXMethodDecl *Method = Node->getCallOperator();
2196 for (const auto *P : Method->parameters()) {
2202 std::string ParamStr =
2203 (Policy.CleanUglifiedParameters && P->getIdentifier())
2204 ? P->getIdentifier()->deuglifiedName().str()
2205 : P->getNameAsString();
2206 P->getOriginalType().print(OS, Policy, ParamStr);
2208 if (Method->isVariadic()) {
2215 if (Node->isMutable())
2218 auto *Proto = Method->getType()->castAs<FunctionProtoType>();
2219 Proto->printExceptionSpecification(OS, Policy);
2221 // FIXME: Attributes
2223 // Print the trailing return type if it was specified in the source.
2224 if (Node->hasExplicitResultType()) {
2226 Proto->getReturnType().print(OS, Policy);
2232 if (Policy.TerseOutput)
2235 PrintRawCompoundStmt(Node->getCompoundStmtBody());
2238 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
2239 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
2240 TSInfo->getType().print(OS, Policy);
2242 Node->getType().print(OS, Policy);
2246 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
2247 if (E->isGlobalNew())
2250 unsigned NumPlace = E->getNumPlacementArgs();
2251 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
2253 PrintExpr(E->getPlacementArg(0));
2254 for (unsigned i = 1; i < NumPlace; ++i) {
2255 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
2258 PrintExpr(E->getPlacementArg(i));
2262 if (E->isParenTypeId())
2266 llvm::raw_string_ostream s(TypeS);
2268 if (Optional<Expr *> Size = E->getArraySize())
2269 (*Size)->printPretty(s, Helper, Policy);
2272 E->getAllocatedType().print(OS, Policy, TypeS);
2273 if (E->isParenTypeId())
2276 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
2277 if (InitStyle != CXXNewExpr::NoInit) {
2278 bool Bare = InitStyle == CXXNewExpr::CallInit &&
2279 !isa<ParenListExpr>(E->getInitializer());
2282 PrintExpr(E->getInitializer());
2288 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2289 if (E->isGlobalDelete())
2292 if (E->isArrayForm())
2294 PrintExpr(E->getArgument());
2297 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
2298 PrintExpr(E->getBase());
2303 if (E->getQualifier())
2304 E->getQualifier()->print(OS, Policy);
2307 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
2308 OS << II->getName();
2310 E->getDestroyedType().print(OS, Policy);
2313 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
2314 if (E->isListInitialization() && !E->isStdInitListInitialization())
2317 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
2318 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
2319 // Don't print any defaulted arguments
2324 PrintExpr(E->getArg(i));
2327 if (E->isListInitialization() && !E->isStdInitListInitialization())
2331 void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) {
2332 // Parens are printed by the surrounding context.
2333 OS << "<forwarded>";
2336 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
2337 PrintExpr(E->getSubExpr());
2340 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
2341 // Just forward to the subexpression.
2342 PrintExpr(E->getSubExpr());
2345 void StmtPrinter::VisitCXXUnresolvedConstructExpr(
2346 CXXUnresolvedConstructExpr *Node) {
2347 Node->getTypeAsWritten().print(OS, Policy);
2348 if (!Node->isListInitialization())
2350 for (auto Arg = Node->arg_begin(), ArgEnd = Node->arg_end(); Arg != ArgEnd;
2352 if (Arg != Node->arg_begin())
2356 if (!Node->isListInitialization())
2360 void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2361 CXXDependentScopeMemberExpr *Node) {
2362 if (!Node->isImplicitAccess()) {
2363 PrintExpr(Node->getBase());
2364 OS << (Node->isArrow() ? "->" : ".");
2366 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2367 Qualifier->print(OS, Policy);
2368 if (Node->hasTemplateKeyword())
2370 OS << Node->getMemberNameInfo();
2371 if (Node->hasExplicitTemplateArgs())
2372 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
2375 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
2376 if (!Node->isImplicitAccess()) {
2377 PrintExpr(Node->getBase());
2378 OS << (Node->isArrow() ? "->" : ".");
2380 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2381 Qualifier->print(OS, Policy);
2382 if (Node->hasTemplateKeyword())
2384 OS << Node->getMemberNameInfo();
2385 if (Node->hasExplicitTemplateArgs())
2386 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
2389 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2390 OS << getTraitSpelling(E->getTrait()) << "(";
2391 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2394 E->getArg(I)->getType().print(OS, Policy);
2399 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
2400 OS << getTraitSpelling(E->getTrait()) << '(';
2401 E->getQueriedType().print(OS, Policy);
2405 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
2406 OS << getTraitSpelling(E->getTrait()) << '(';
2407 PrintExpr(E->getQueriedExpression());
2411 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2413 PrintExpr(E->getOperand());
2417 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
2418 PrintExpr(E->getPattern());
2422 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2423 OS << "sizeof...(" << *E->getPack() << ")";
2426 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2427 SubstNonTypeTemplateParmPackExpr *Node) {
2428 OS << *Node->getParameterPack();
2431 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2432 SubstNonTypeTemplateParmExpr *Node) {
2433 Visit(Node->getReplacement());
2436 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2437 OS << *E->getParameterPack();
2440 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2441 PrintExpr(Node->getSubExpr());
2444 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2447 PrintExpr(E->getLHS());
2448 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2452 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2453 PrintExpr(E->getRHS());
2458 void StmtPrinter::VisitConceptSpecializationExpr(ConceptSpecializationExpr *E) {
2459 NestedNameSpecifierLoc NNS = E->getNestedNameSpecifierLoc();
2461 NNS.getNestedNameSpecifier()->print(OS, Policy);
2462 if (E->getTemplateKWLoc().isValid())
2464 OS << E->getFoundDecl()->getName();
2465 printTemplateArgumentList(OS, E->getTemplateArgsAsWritten()->arguments(),
2467 E->getNamedConcept()->getTemplateParameters());
2470 void StmtPrinter::VisitRequiresExpr(RequiresExpr *E) {
2472 auto LocalParameters = E->getLocalParameters();
2473 if (!LocalParameters.empty()) {
2475 for (ParmVarDecl *LocalParam : LocalParameters) {
2476 PrintRawDecl(LocalParam);
2477 if (LocalParam != LocalParameters.back())
2484 auto Requirements = E->getRequirements();
2485 for (concepts::Requirement *Req : Requirements) {
2486 if (auto *TypeReq = dyn_cast<concepts::TypeRequirement>(Req)) {
2487 if (TypeReq->isSubstitutionFailure())
2488 OS << "<<error-type>>";
2490 TypeReq->getType()->getType().print(OS, Policy);
2491 } else if (auto *ExprReq = dyn_cast<concepts::ExprRequirement>(Req)) {
2492 if (ExprReq->isCompound())
2494 if (ExprReq->isExprSubstitutionFailure())
2495 OS << "<<error-expression>>";
2497 PrintExpr(ExprReq->getExpr());
2498 if (ExprReq->isCompound()) {
2500 if (ExprReq->getNoexceptLoc().isValid())
2502 const auto &RetReq = ExprReq->getReturnTypeRequirement();
2503 if (!RetReq.isEmpty()) {
2505 if (RetReq.isSubstitutionFailure())
2506 OS << "<<error-type>>";
2507 else if (RetReq.isTypeConstraint())
2508 RetReq.getTypeConstraint()->print(OS, Policy);
2512 auto *NestedReq = cast<concepts::NestedRequirement>(Req);
2514 if (NestedReq->isSubstitutionFailure())
2515 OS << "<<error-expression>>";
2517 PrintExpr(NestedReq->getConstraintExpr());
2524 // C++ Coroutines TS
2526 void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
2527 Visit(S->getBody());
2530 void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
2532 if (S->getOperand()) {
2534 Visit(S->getOperand());
2539 void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
2541 PrintExpr(S->getOperand());
2544 void StmtPrinter::VisitDependentCoawaitExpr(DependentCoawaitExpr *S) {
2546 PrintExpr(S->getOperand());
2549 void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
2551 PrintExpr(S->getOperand());
2556 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2558 VisitStringLiteral(Node->getString());
2561 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
2563 Visit(E->getSubExpr());
2566 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2568 ObjCArrayLiteral::child_range Ch = E->children();
2569 for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2570 if (I != Ch.begin())
2577 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2579 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2583 ObjCDictionaryElement Element = E->getKeyValueElement(I);
2586 Visit(Element.Value);
2587 if (Element.isPackExpansion())
2593 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
2595 Node->getEncodedType().print(OS, Policy);
2599 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
2601 Node->getSelector().print(OS);
2605 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
2606 OS << "@protocol(" << *Node->getProtocol() << ')';
2609 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2611 switch (Mess->getReceiverKind()) {
2612 case ObjCMessageExpr::Instance:
2613 PrintExpr(Mess->getInstanceReceiver());
2616 case ObjCMessageExpr::Class:
2617 Mess->getClassReceiver().print(OS, Policy);
2620 case ObjCMessageExpr::SuperInstance:
2621 case ObjCMessageExpr::SuperClass:
2627 Selector selector = Mess->getSelector();
2628 if (selector.isUnarySelector()) {
2629 OS << selector.getNameForSlot(0);
2631 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
2632 if (i < selector.getNumArgs()) {
2633 if (i > 0) OS << ' ';
2634 if (selector.getIdentifierInfoForSlot(i))
2635 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
2639 else OS << ", "; // Handle variadic methods.
2641 PrintExpr(Mess->getArg(i));
2647 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2648 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2652 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2653 PrintExpr(E->getSubExpr());
2657 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
2658 OS << '(' << E->getBridgeKindName();
2659 E->getType().print(OS, Policy);
2661 PrintExpr(E->getSubExpr());
2664 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
2665 BlockDecl *BD = Node->getBlockDecl();
2668 const FunctionType *AFT = Node->getFunctionType();
2670 if (isa<FunctionNoProtoType>(AFT)) {
2672 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
2674 for (BlockDecl::param_iterator AI = BD->param_begin(),
2675 E = BD->param_end(); AI != E; ++AI) {
2676 if (AI != BD->param_begin()) OS << ", ";
2677 std::string ParamStr = (*AI)->getNameAsString();
2678 (*AI)->getType().print(OS, Policy, ParamStr);
2681 const auto *FT = cast<FunctionProtoType>(AFT);
2682 if (FT->isVariadic()) {
2683 if (!BD->param_empty()) OS << ", ";
2691 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2692 PrintExpr(Node->getSourceExpr());
2695 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2696 // TODO: Print something reasonable for a TypoExpr, if necessary.
2697 llvm_unreachable("Cannot print TypoExpr nodes");
2700 void StmtPrinter::VisitRecoveryExpr(RecoveryExpr *Node) {
2701 OS << "<recovery-expr>(";
2702 const char *Sep = "";
2703 for (Expr *E : Node->subExpressions()) {
2711 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2712 OS << "__builtin_astype(";
2713 PrintExpr(Node->getSrcExpr());
2715 Node->getType().print(OS, Policy);
2719 //===----------------------------------------------------------------------===//
2720 // Stmt method implementations
2721 //===----------------------------------------------------------------------===//
2723 void Stmt::dumpPretty(const ASTContext &Context) const {
2724 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
2727 void Stmt::printPretty(raw_ostream &Out, PrinterHelper *Helper,
2728 const PrintingPolicy &Policy, unsigned Indentation,
2729 StringRef NL, const ASTContext *Context) const {
2730 StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context);
2731 P.Visit(const_cast<Stmt *>(this));
2734 void Stmt::printPrettyControlled(raw_ostream &Out, PrinterHelper *Helper,
2735 const PrintingPolicy &Policy,
2736 unsigned Indentation, StringRef NL,
2737 const ASTContext *Context) const {
2738 StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context);
2739 P.PrintControlledStmt(const_cast<Stmt *>(this));
2742 void Stmt::printJson(raw_ostream &Out, PrinterHelper *Helper,
2743 const PrintingPolicy &Policy, bool AddQuotes) const {
2745 llvm::raw_string_ostream TempOut(Buf);
2747 printPretty(TempOut, Helper, Policy);
2749 Out << JsonFormat(TempOut.str(), AddQuotes);
2752 //===----------------------------------------------------------------------===//
2754 //===----------------------------------------------------------------------===//
2756 // Implement virtual destructor.
2757 PrinterHelper::~PrinterHelper() = default;