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/StringRef.h"
51 #include "llvm/Support/Casting.h"
52 #include "llvm/Support/Compiler.h"
53 #include "llvm/Support/ErrorHandling.h"
54 #include "llvm/Support/Format.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,
79 const ASTContext *Context = nullptr)
80 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy),
81 NL(NL), Context(Context) {}
83 void PrintStmt(Stmt *S) {
84 PrintStmt(S, Policy.Indentation);
87 void PrintStmt(Stmt *S, int SubIndent) {
88 IndentLevel += SubIndent;
89 if (S && isa<Expr>(S)) {
90 // If this is an expr used in a stmt context, indent and newline it.
97 Indent() << "<<<NULL STATEMENT>>>" << NL;
99 IndentLevel -= SubIndent;
102 void PrintInitStmt(Stmt *S, unsigned PrefixWidth) {
103 // FIXME: Cope better with odd prefix widths.
104 IndentLevel += (PrefixWidth + 1) / 2;
105 if (auto *DS = dyn_cast<DeclStmt>(S))
106 PrintRawDeclStmt(DS);
108 PrintExpr(cast<Expr>(S));
110 IndentLevel -= (PrefixWidth + 1) / 2;
113 void PrintControlledStmt(Stmt *S) {
114 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
116 PrintRawCompoundStmt(CS);
124 void PrintRawCompoundStmt(CompoundStmt *S);
125 void PrintRawDecl(Decl *D);
126 void PrintRawDeclStmt(const DeclStmt *S);
127 void PrintRawIfStmt(IfStmt *If);
128 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
129 void PrintCallArgs(CallExpr *E);
130 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
131 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
132 void PrintOMPExecutableDirective(OMPExecutableDirective *S,
133 bool ForceNoStmt = false);
135 void PrintExpr(Expr *E) {
142 raw_ostream &Indent(int Delta = 0) {
143 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
148 void Visit(Stmt* S) {
149 if (Helper && Helper->handledStmt(S,OS))
151 else StmtVisitor<StmtPrinter>::Visit(S);
154 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
155 Indent() << "<<unknown stmt type>>" << NL;
158 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
159 OS << "<<unknown expr type>>";
162 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
164 #define ABSTRACT_STMT(CLASS)
165 #define STMT(CLASS, PARENT) \
166 void Visit##CLASS(CLASS *Node);
167 #include "clang/AST/StmtNodes.inc"
172 //===----------------------------------------------------------------------===//
173 // Stmt printing methods.
174 //===----------------------------------------------------------------------===//
176 /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
177 /// with no newline after the }.
178 void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
180 for (auto *I : Node->body())
186 void StmtPrinter::PrintRawDecl(Decl *D) {
187 D->print(OS, Policy, IndentLevel);
190 void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
191 SmallVector<Decl *, 2> Decls(S->decls());
192 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
195 void StmtPrinter::VisitNullStmt(NullStmt *Node) {
196 Indent() << ";" << NL;
199 void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
201 PrintRawDeclStmt(Node);
205 void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
207 PrintRawCompoundStmt(Node);
211 void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
212 Indent(-1) << "case ";
213 PrintExpr(Node->getLHS());
214 if (Node->getRHS()) {
216 PrintExpr(Node->getRHS());
220 PrintStmt(Node->getSubStmt(), 0);
223 void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
224 Indent(-1) << "default:" << NL;
225 PrintStmt(Node->getSubStmt(), 0);
228 void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
229 Indent(-1) << Node->getName() << ":" << NL;
230 PrintStmt(Node->getSubStmt(), 0);
233 void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
234 for (const auto *Attr : Node->getAttrs()) {
235 Attr->printPretty(OS, Policy);
238 PrintStmt(Node->getSubStmt(), 0);
241 void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
244 PrintInitStmt(If->getInit(), 4);
245 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
246 PrintRawDeclStmt(DS);
248 PrintExpr(If->getCond());
251 if (auto *CS = dyn_cast<CompoundStmt>(If->getThen())) {
253 PrintRawCompoundStmt(CS);
254 OS << (If->getElse() ? " " : NL);
257 PrintStmt(If->getThen());
258 if (If->getElse()) Indent();
261 if (Stmt *Else = If->getElse()) {
264 if (auto *CS = dyn_cast<CompoundStmt>(Else)) {
266 PrintRawCompoundStmt(CS);
268 } else if (auto *ElseIf = dyn_cast<IfStmt>(Else)) {
270 PrintRawIfStmt(ElseIf);
273 PrintStmt(If->getElse());
278 void StmtPrinter::VisitIfStmt(IfStmt *If) {
283 void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
284 Indent() << "switch (";
286 PrintInitStmt(Node->getInit(), 8);
287 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
288 PrintRawDeclStmt(DS);
290 PrintExpr(Node->getCond());
292 PrintControlledStmt(Node->getBody());
295 void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
296 Indent() << "while (";
297 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
298 PrintRawDeclStmt(DS);
300 PrintExpr(Node->getCond());
302 PrintStmt(Node->getBody());
305 void StmtPrinter::VisitDoStmt(DoStmt *Node) {
307 if (auto *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
308 PrintRawCompoundStmt(CS);
312 PrintStmt(Node->getBody());
317 PrintExpr(Node->getCond());
321 void StmtPrinter::VisitForStmt(ForStmt *Node) {
324 PrintInitStmt(Node->getInit(), 5);
326 OS << (Node->getCond() ? "; " : ";");
328 PrintExpr(Node->getCond());
330 if (Node->getInc()) {
332 PrintExpr(Node->getInc());
335 PrintControlledStmt(Node->getBody());
338 void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
340 if (auto *DS = dyn_cast<DeclStmt>(Node->getElement()))
341 PrintRawDeclStmt(DS);
343 PrintExpr(cast<Expr>(Node->getElement()));
345 PrintExpr(Node->getCollection());
347 PrintControlledStmt(Node->getBody());
350 void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
353 PrintInitStmt(Node->getInit(), 5);
354 PrintingPolicy SubPolicy(Policy);
355 SubPolicy.SuppressInitializers = true;
356 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
358 PrintExpr(Node->getRangeInit());
360 PrintControlledStmt(Node->getBody());
363 void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
365 if (Node->isIfExists())
366 OS << "__if_exists (";
368 OS << "__if_not_exists (";
370 if (NestedNameSpecifier *Qualifier
371 = Node->getQualifierLoc().getNestedNameSpecifier())
372 Qualifier->print(OS, Policy);
374 OS << Node->getNameInfo() << ") ";
376 PrintRawCompoundStmt(Node->getSubStmt());
379 void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
380 Indent() << "goto " << Node->getLabel()->getName() << ";";
381 if (Policy.IncludeNewlines) OS << NL;
384 void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
385 Indent() << "goto *";
386 PrintExpr(Node->getTarget());
388 if (Policy.IncludeNewlines) OS << NL;
391 void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
392 Indent() << "continue;";
393 if (Policy.IncludeNewlines) OS << NL;
396 void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
397 Indent() << "break;";
398 if (Policy.IncludeNewlines) OS << NL;
401 void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
402 Indent() << "return";
403 if (Node->getRetValue()) {
405 PrintExpr(Node->getRetValue());
408 if (Policy.IncludeNewlines) OS << NL;
411 void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
414 if (Node->isVolatile())
417 if (Node->isAsmGoto())
421 VisitStringLiteral(Node->getAsmString());
424 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
425 Node->getNumClobbers() != 0 || Node->getNumLabels() != 0)
428 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
432 if (!Node->getOutputName(i).empty()) {
434 OS << Node->getOutputName(i);
438 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
440 Visit(Node->getOutputExpr(i));
445 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0 ||
446 Node->getNumLabels() != 0)
449 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
453 if (!Node->getInputName(i).empty()) {
455 OS << Node->getInputName(i);
459 VisitStringLiteral(Node->getInputConstraintLiteral(i));
461 Visit(Node->getInputExpr(i));
466 if (Node->getNumClobbers() != 0 || Node->getNumLabels())
469 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
473 VisitStringLiteral(Node->getClobberStringLiteral(i));
477 if (Node->getNumLabels() != 0)
480 for (unsigned i = 0, e = Node->getNumLabels(); i != e; ++i) {
483 OS << Node->getLabelName(i);
487 if (Policy.IncludeNewlines) OS << NL;
490 void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
491 // FIXME: Implement MS style inline asm statement printer.
492 Indent() << "__asm ";
493 if (Node->hasBraces())
495 OS << Node->getAsmString() << NL;
496 if (Node->hasBraces())
497 Indent() << "}" << NL;
500 void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
501 PrintStmt(Node->getCapturedDecl()->getBody());
504 void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
506 if (auto *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
507 PrintRawCompoundStmt(TS);
511 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
512 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
513 Indent() << "@catch(";
514 if (catchStmt->getCatchParamDecl()) {
515 if (Decl *DS = catchStmt->getCatchParamDecl())
519 if (auto *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
520 PrintRawCompoundStmt(CS);
525 if (auto *FS = static_cast<ObjCAtFinallyStmt *>(Node->getFinallyStmt())) {
526 Indent() << "@finally";
527 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
532 void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
535 void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
536 Indent() << "@catch (...) { /* todo */ } " << NL;
539 void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
540 Indent() << "@throw";
541 if (Node->getThrowExpr()) {
543 PrintExpr(Node->getThrowExpr());
548 void StmtPrinter::VisitObjCAvailabilityCheckExpr(
549 ObjCAvailabilityCheckExpr *Node) {
550 OS << "@available(...)";
553 void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
554 Indent() << "@synchronized (";
555 PrintExpr(Node->getSynchExpr());
557 PrintRawCompoundStmt(Node->getSynchBody());
561 void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
562 Indent() << "@autoreleasepool";
563 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
567 void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
569 if (Decl *ExDecl = Node->getExceptionDecl())
570 PrintRawDecl(ExDecl);
574 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
577 void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
579 PrintRawCXXCatchStmt(Node);
583 void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
585 PrintRawCompoundStmt(Node->getTryBlock());
586 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
588 PrintRawCXXCatchStmt(Node->getHandler(i));
593 void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
594 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
595 PrintRawCompoundStmt(Node->getTryBlock());
596 SEHExceptStmt *E = Node->getExceptHandler();
597 SEHFinallyStmt *F = Node->getFinallyHandler();
599 PrintRawSEHExceptHandler(E);
601 assert(F && "Must have a finally block...");
602 PrintRawSEHFinallyStmt(F);
607 void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
609 PrintRawCompoundStmt(Node->getBlock());
613 void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
615 VisitExpr(Node->getFilterExpr());
617 PrintRawCompoundStmt(Node->getBlock());
621 void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
623 PrintRawSEHExceptHandler(Node);
627 void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
629 PrintRawSEHFinallyStmt(Node);
633 void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
634 Indent() << "__leave;";
635 if (Policy.IncludeNewlines) OS << NL;
638 //===----------------------------------------------------------------------===//
639 // OpenMP directives printing methods
640 //===----------------------------------------------------------------------===//
642 void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S,
644 OMPClausePrinter Printer(OS, Policy);
645 ArrayRef<OMPClause *> Clauses = S->clauses();
646 for (auto *Clause : Clauses)
647 if (Clause && !Clause->isImplicit()) {
649 Printer.Visit(Clause);
652 if (!ForceNoStmt && S->hasAssociatedStmt())
653 PrintStmt(S->getInnermostCapturedStmt()->getCapturedStmt());
656 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
657 Indent() << "#pragma omp parallel";
658 PrintOMPExecutableDirective(Node);
661 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
662 Indent() << "#pragma omp simd";
663 PrintOMPExecutableDirective(Node);
666 void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
667 Indent() << "#pragma omp for";
668 PrintOMPExecutableDirective(Node);
671 void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
672 Indent() << "#pragma omp for simd";
673 PrintOMPExecutableDirective(Node);
676 void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
677 Indent() << "#pragma omp sections";
678 PrintOMPExecutableDirective(Node);
681 void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
682 Indent() << "#pragma omp section";
683 PrintOMPExecutableDirective(Node);
686 void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
687 Indent() << "#pragma omp single";
688 PrintOMPExecutableDirective(Node);
691 void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
692 Indent() << "#pragma omp master";
693 PrintOMPExecutableDirective(Node);
696 void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
697 Indent() << "#pragma omp critical";
698 if (Node->getDirectiveName().getName()) {
700 Node->getDirectiveName().printName(OS, Policy);
703 PrintOMPExecutableDirective(Node);
706 void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
707 Indent() << "#pragma omp parallel for";
708 PrintOMPExecutableDirective(Node);
711 void StmtPrinter::VisitOMPParallelForSimdDirective(
712 OMPParallelForSimdDirective *Node) {
713 Indent() << "#pragma omp parallel for simd";
714 PrintOMPExecutableDirective(Node);
717 void StmtPrinter::VisitOMPParallelMasterDirective(
718 OMPParallelMasterDirective *Node) {
719 Indent() << "#pragma omp parallel master";
720 PrintOMPExecutableDirective(Node);
723 void StmtPrinter::VisitOMPParallelSectionsDirective(
724 OMPParallelSectionsDirective *Node) {
725 Indent() << "#pragma omp parallel sections";
726 PrintOMPExecutableDirective(Node);
729 void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
730 Indent() << "#pragma omp task";
731 PrintOMPExecutableDirective(Node);
734 void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
735 Indent() << "#pragma omp taskyield";
736 PrintOMPExecutableDirective(Node);
739 void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
740 Indent() << "#pragma omp barrier";
741 PrintOMPExecutableDirective(Node);
744 void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
745 Indent() << "#pragma omp taskwait";
746 PrintOMPExecutableDirective(Node);
749 void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
750 Indent() << "#pragma omp taskgroup";
751 PrintOMPExecutableDirective(Node);
754 void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
755 Indent() << "#pragma omp flush";
756 PrintOMPExecutableDirective(Node);
759 void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
760 Indent() << "#pragma omp ordered";
761 PrintOMPExecutableDirective(Node, Node->hasClausesOfKind<OMPDependClause>());
764 void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
765 Indent() << "#pragma omp atomic";
766 PrintOMPExecutableDirective(Node);
769 void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
770 Indent() << "#pragma omp target";
771 PrintOMPExecutableDirective(Node);
774 void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
775 Indent() << "#pragma omp target data";
776 PrintOMPExecutableDirective(Node);
779 void StmtPrinter::VisitOMPTargetEnterDataDirective(
780 OMPTargetEnterDataDirective *Node) {
781 Indent() << "#pragma omp target enter data";
782 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
785 void StmtPrinter::VisitOMPTargetExitDataDirective(
786 OMPTargetExitDataDirective *Node) {
787 Indent() << "#pragma omp target exit data";
788 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
791 void StmtPrinter::VisitOMPTargetParallelDirective(
792 OMPTargetParallelDirective *Node) {
793 Indent() << "#pragma omp target parallel";
794 PrintOMPExecutableDirective(Node);
797 void StmtPrinter::VisitOMPTargetParallelForDirective(
798 OMPTargetParallelForDirective *Node) {
799 Indent() << "#pragma omp target parallel for";
800 PrintOMPExecutableDirective(Node);
803 void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
804 Indent() << "#pragma omp teams";
805 PrintOMPExecutableDirective(Node);
808 void StmtPrinter::VisitOMPCancellationPointDirective(
809 OMPCancellationPointDirective *Node) {
810 Indent() << "#pragma omp cancellation point "
811 << getOpenMPDirectiveName(Node->getCancelRegion());
812 PrintOMPExecutableDirective(Node);
815 void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
816 Indent() << "#pragma omp cancel "
817 << getOpenMPDirectiveName(Node->getCancelRegion());
818 PrintOMPExecutableDirective(Node);
821 void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) {
822 Indent() << "#pragma omp taskloop";
823 PrintOMPExecutableDirective(Node);
826 void StmtPrinter::VisitOMPTaskLoopSimdDirective(
827 OMPTaskLoopSimdDirective *Node) {
828 Indent() << "#pragma omp taskloop simd";
829 PrintOMPExecutableDirective(Node);
832 void StmtPrinter::VisitOMPMasterTaskLoopDirective(
833 OMPMasterTaskLoopDirective *Node) {
834 Indent() << "#pragma omp master taskloop";
835 PrintOMPExecutableDirective(Node);
838 void StmtPrinter::VisitOMPMasterTaskLoopSimdDirective(
839 OMPMasterTaskLoopSimdDirective *Node) {
840 Indent() << "#pragma omp master taskloop simd";
841 PrintOMPExecutableDirective(Node);
844 void StmtPrinter::VisitOMPParallelMasterTaskLoopDirective(
845 OMPParallelMasterTaskLoopDirective *Node) {
846 Indent() << "#pragma omp parallel master taskloop";
847 PrintOMPExecutableDirective(Node);
850 void StmtPrinter::VisitOMPParallelMasterTaskLoopSimdDirective(
851 OMPParallelMasterTaskLoopSimdDirective *Node) {
852 Indent() << "#pragma omp parallel master taskloop simd";
853 PrintOMPExecutableDirective(Node);
856 void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) {
857 Indent() << "#pragma omp distribute";
858 PrintOMPExecutableDirective(Node);
861 void StmtPrinter::VisitOMPTargetUpdateDirective(
862 OMPTargetUpdateDirective *Node) {
863 Indent() << "#pragma omp target update";
864 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
867 void StmtPrinter::VisitOMPDistributeParallelForDirective(
868 OMPDistributeParallelForDirective *Node) {
869 Indent() << "#pragma omp distribute parallel for";
870 PrintOMPExecutableDirective(Node);
873 void StmtPrinter::VisitOMPDistributeParallelForSimdDirective(
874 OMPDistributeParallelForSimdDirective *Node) {
875 Indent() << "#pragma omp distribute parallel for simd";
876 PrintOMPExecutableDirective(Node);
879 void StmtPrinter::VisitOMPDistributeSimdDirective(
880 OMPDistributeSimdDirective *Node) {
881 Indent() << "#pragma omp distribute simd";
882 PrintOMPExecutableDirective(Node);
885 void StmtPrinter::VisitOMPTargetParallelForSimdDirective(
886 OMPTargetParallelForSimdDirective *Node) {
887 Indent() << "#pragma omp target parallel for simd";
888 PrintOMPExecutableDirective(Node);
891 void StmtPrinter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective *Node) {
892 Indent() << "#pragma omp target simd";
893 PrintOMPExecutableDirective(Node);
896 void StmtPrinter::VisitOMPTeamsDistributeDirective(
897 OMPTeamsDistributeDirective *Node) {
898 Indent() << "#pragma omp teams distribute";
899 PrintOMPExecutableDirective(Node);
902 void StmtPrinter::VisitOMPTeamsDistributeSimdDirective(
903 OMPTeamsDistributeSimdDirective *Node) {
904 Indent() << "#pragma omp teams distribute simd";
905 PrintOMPExecutableDirective(Node);
908 void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective(
909 OMPTeamsDistributeParallelForSimdDirective *Node) {
910 Indent() << "#pragma omp teams distribute parallel for simd";
911 PrintOMPExecutableDirective(Node);
914 void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective(
915 OMPTeamsDistributeParallelForDirective *Node) {
916 Indent() << "#pragma omp teams distribute parallel for";
917 PrintOMPExecutableDirective(Node);
920 void StmtPrinter::VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective *Node) {
921 Indent() << "#pragma omp target teams";
922 PrintOMPExecutableDirective(Node);
925 void StmtPrinter::VisitOMPTargetTeamsDistributeDirective(
926 OMPTargetTeamsDistributeDirective *Node) {
927 Indent() << "#pragma omp target teams distribute";
928 PrintOMPExecutableDirective(Node);
931 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective(
932 OMPTargetTeamsDistributeParallelForDirective *Node) {
933 Indent() << "#pragma omp target teams distribute parallel for";
934 PrintOMPExecutableDirective(Node);
937 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
938 OMPTargetTeamsDistributeParallelForSimdDirective *Node) {
939 Indent() << "#pragma omp target teams distribute parallel for simd";
940 PrintOMPExecutableDirective(Node);
943 void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective(
944 OMPTargetTeamsDistributeSimdDirective *Node) {
945 Indent() << "#pragma omp target teams distribute simd";
946 PrintOMPExecutableDirective(Node);
949 //===----------------------------------------------------------------------===//
950 // Expr printing methods.
951 //===----------------------------------------------------------------------===//
953 void StmtPrinter::VisitSourceLocExpr(SourceLocExpr *Node) {
954 OS << Node->getBuiltinStr() << "()";
957 void StmtPrinter::VisitConstantExpr(ConstantExpr *Node) {
958 PrintExpr(Node->getSubExpr());
961 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
962 if (const auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) {
963 OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy);
966 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
967 Qualifier->print(OS, Policy);
968 if (Node->hasTemplateKeyword())
970 OS << Node->getNameInfo();
971 if (Node->hasExplicitTemplateArgs())
972 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
975 void StmtPrinter::VisitDependentScopeDeclRefExpr(
976 DependentScopeDeclRefExpr *Node) {
977 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
978 Qualifier->print(OS, Policy);
979 if (Node->hasTemplateKeyword())
981 OS << Node->getNameInfo();
982 if (Node->hasExplicitTemplateArgs())
983 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
986 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
987 if (Node->getQualifier())
988 Node->getQualifier()->print(OS, Policy);
989 if (Node->hasTemplateKeyword())
991 OS << Node->getNameInfo();
992 if (Node->hasExplicitTemplateArgs())
993 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
996 static bool isImplicitSelf(const Expr *E) {
997 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
998 if (const auto *PD = dyn_cast<ImplicitParamDecl>(DRE->getDecl())) {
999 if (PD->getParameterKind() == ImplicitParamDecl::ObjCSelf &&
1000 DRE->getBeginLoc().isInvalid())
1007 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
1008 if (Node->getBase()) {
1009 if (!Policy.SuppressImplicitBase ||
1010 !isImplicitSelf(Node->getBase()->IgnoreImpCasts())) {
1011 PrintExpr(Node->getBase());
1012 OS << (Node->isArrow() ? "->" : ".");
1015 OS << *Node->getDecl();
1018 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
1019 if (Node->isSuperReceiver())
1021 else if (Node->isObjectReceiver() && Node->getBase()) {
1022 PrintExpr(Node->getBase());
1024 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1025 OS << Node->getClassReceiver()->getName() << ".";
1028 if (Node->isImplicitProperty()) {
1029 if (const auto *Getter = Node->getImplicitPropertyGetter())
1030 Getter->getSelector().print(OS);
1032 OS << SelectorTable::getPropertyNameFromSetterSelector(
1033 Node->getImplicitPropertySetter()->getSelector());
1035 OS << Node->getExplicitProperty()->getName();
1038 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1039 PrintExpr(Node->getBaseExpr());
1041 PrintExpr(Node->getKeyExpr());
1045 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
1046 OS << PredefinedExpr::getIdentKindName(Node->getIdentKind());
1049 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
1050 unsigned value = Node->getValue();
1052 switch (Node->getKind()) {
1053 case CharacterLiteral::Ascii: break; // no prefix.
1054 case CharacterLiteral::Wide: OS << 'L'; break;
1055 case CharacterLiteral::UTF8: OS << "u8"; break;
1056 case CharacterLiteral::UTF16: OS << 'u'; break;
1057 case CharacterLiteral::UTF32: OS << 'U'; break;
1068 // TODO: K&R: the meaning of '\\a' is different in traditional C
1074 // Nonstandard escape sequence.
1094 // A character literal might be sign-extended, which
1095 // would result in an invalid \U escape sequence.
1096 // FIXME: multicharacter literals such as '\xFF\xFF\xFF\xFF'
1097 // are not correctly handled.
1098 if ((value & ~0xFFu) == ~0xFFu && Node->getKind() == CharacterLiteral::Ascii)
1100 if (value < 256 && isPrintable((unsigned char)value))
1101 OS << "'" << (char)value << "'";
1102 else if (value < 256)
1103 OS << "'\\x" << llvm::format("%02x", value) << "'";
1104 else if (value <= 0xFFFF)
1105 OS << "'\\u" << llvm::format("%04x", value) << "'";
1107 OS << "'\\U" << llvm::format("%08x", value) << "'";
1111 /// Prints the given expression using the original source text. Returns true on
1112 /// success, false otherwise.
1113 static bool printExprAsWritten(raw_ostream &OS, Expr *E,
1114 const ASTContext *Context) {
1117 bool Invalid = false;
1118 StringRef Source = Lexer::getSourceText(
1119 CharSourceRange::getTokenRange(E->getSourceRange()),
1120 Context->getSourceManager(), Context->getLangOpts(), &Invalid);
1128 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
1129 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1131 bool isSigned = Node->getType()->isSignedIntegerType();
1132 OS << Node->getValue().toString(10, isSigned);
1134 // Emit suffixes. Integer literals are always a builtin integer type.
1135 switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1136 default: llvm_unreachable("Unexpected type for integer literal!");
1137 case BuiltinType::Char_S:
1138 case BuiltinType::Char_U: OS << "i8"; break;
1139 case BuiltinType::UChar: OS << "Ui8"; break;
1140 case BuiltinType::Short: OS << "i16"; break;
1141 case BuiltinType::UShort: OS << "Ui16"; break;
1142 case BuiltinType::Int: break; // no suffix.
1143 case BuiltinType::UInt: OS << 'U'; break;
1144 case BuiltinType::Long: OS << 'L'; break;
1145 case BuiltinType::ULong: OS << "UL"; break;
1146 case BuiltinType::LongLong: OS << "LL"; break;
1147 case BuiltinType::ULongLong: OS << "ULL"; break;
1151 void StmtPrinter::VisitFixedPointLiteral(FixedPointLiteral *Node) {
1152 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1154 OS << Node->getValueAsString(/*Radix=*/10);
1156 switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1157 default: llvm_unreachable("Unexpected type for fixed point literal!");
1158 case BuiltinType::ShortFract: OS << "hr"; break;
1159 case BuiltinType::ShortAccum: OS << "hk"; break;
1160 case BuiltinType::UShortFract: OS << "uhr"; break;
1161 case BuiltinType::UShortAccum: OS << "uhk"; break;
1162 case BuiltinType::Fract: OS << "r"; break;
1163 case BuiltinType::Accum: OS << "k"; break;
1164 case BuiltinType::UFract: OS << "ur"; break;
1165 case BuiltinType::UAccum: OS << "uk"; break;
1166 case BuiltinType::LongFract: OS << "lr"; break;
1167 case BuiltinType::LongAccum: OS << "lk"; break;
1168 case BuiltinType::ULongFract: OS << "ulr"; break;
1169 case BuiltinType::ULongAccum: OS << "ulk"; break;
1173 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1175 SmallString<16> Str;
1176 Node->getValue().toString(Str);
1178 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1179 OS << '.'; // Trailing dot in order to separate from ints.
1184 // Emit suffixes. Float literals are always a builtin float type.
1185 switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
1186 default: llvm_unreachable("Unexpected type for float literal!");
1187 case BuiltinType::Half: break; // FIXME: suffix?
1188 case BuiltinType::Double: break; // no suffix.
1189 case BuiltinType::Float16: OS << "F16"; break;
1190 case BuiltinType::Float: OS << 'F'; break;
1191 case BuiltinType::LongDouble: OS << 'L'; break;
1192 case BuiltinType::Float128: OS << 'Q'; break;
1196 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1197 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1199 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
1202 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1203 PrintExpr(Node->getSubExpr());
1207 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
1208 Str->outputString(OS);
1211 void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1213 PrintExpr(Node->getSubExpr());
1217 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
1218 if (!Node->isPostfix()) {
1219 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1221 // Print a space if this is an "identifier operator" like __real, or if
1222 // it might be concatenated incorrectly like '+'.
1223 switch (Node->getOpcode()) {
1232 if (isa<UnaryOperator>(Node->getSubExpr()))
1237 PrintExpr(Node->getSubExpr());
1239 if (Node->isPostfix())
1240 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1243 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1244 OS << "__builtin_offsetof(";
1245 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1247 bool PrintedSomething = false;
1248 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1249 OffsetOfNode ON = Node->getComponent(i);
1250 if (ON.getKind() == OffsetOfNode::Array) {
1253 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1255 PrintedSomething = true;
1259 // Skip implicit base indirections.
1260 if (ON.getKind() == OffsetOfNode::Base)
1263 // Field or identifier node.
1264 IdentifierInfo *Id = ON.getFieldName();
1268 if (PrintedSomething)
1271 PrintedSomething = true;
1272 OS << Id->getName();
1277 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
1278 switch(Node->getKind()) {
1285 else if (Policy.UnderscoreAlignof)
1290 case UETT_PreferredAlignOf:
1296 case UETT_OpenMPRequiredSimdAlign:
1297 OS << "__builtin_omp_required_simd_align";
1300 if (Node->isArgumentType()) {
1302 Node->getArgumentType().print(OS, Policy);
1306 PrintExpr(Node->getArgumentExpr());
1310 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1312 PrintExpr(Node->getControllingExpr());
1313 for (const GenericSelectionExpr::Association Assoc : Node->associations()) {
1315 QualType T = Assoc.getType();
1319 T.print(OS, Policy);
1321 PrintExpr(Assoc.getAssociationExpr());
1326 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
1327 PrintExpr(Node->getLHS());
1329 PrintExpr(Node->getRHS());
1333 void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) {
1334 PrintExpr(Node->getBase());
1336 if (Node->getLowerBound())
1337 PrintExpr(Node->getLowerBound());
1338 if (Node->getColonLoc().isValid()) {
1340 if (Node->getLength())
1341 PrintExpr(Node->getLength());
1346 void StmtPrinter::PrintCallArgs(CallExpr *Call) {
1347 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
1348 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1349 // Don't print any defaulted arguments
1354 PrintExpr(Call->getArg(i));
1358 void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1359 PrintExpr(Call->getCallee());
1361 PrintCallArgs(Call);
1365 static bool isImplicitThis(const Expr *E) {
1366 if (const auto *TE = dyn_cast<CXXThisExpr>(E))
1367 return TE->isImplicit();
1371 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1372 if (!Policy.SuppressImplicitBase || !isImplicitThis(Node->getBase())) {
1373 PrintExpr(Node->getBase());
1375 auto *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1376 FieldDecl *ParentDecl =
1377 ParentMember ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl())
1380 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1381 OS << (Node->isArrow() ? "->" : ".");
1384 if (auto *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1385 if (FD->isAnonymousStructOrUnion())
1388 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1389 Qualifier->print(OS, Policy);
1390 if (Node->hasTemplateKeyword())
1392 OS << Node->getMemberNameInfo();
1393 if (Node->hasExplicitTemplateArgs())
1394 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
1397 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1398 PrintExpr(Node->getBase());
1399 OS << (Node->isArrow() ? "->isa" : ".isa");
1402 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1403 PrintExpr(Node->getBase());
1405 OS << Node->getAccessor().getName();
1408 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1410 Node->getTypeAsWritten().print(OS, Policy);
1412 PrintExpr(Node->getSubExpr());
1415 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1417 Node->getType().print(OS, Policy);
1419 PrintExpr(Node->getInitializer());
1422 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1423 // No need to print anything, simply forward to the subexpression.
1424 PrintExpr(Node->getSubExpr());
1427 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1428 PrintExpr(Node->getLHS());
1429 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1430 PrintExpr(Node->getRHS());
1433 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1434 PrintExpr(Node->getLHS());
1435 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1436 PrintExpr(Node->getRHS());
1439 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1440 PrintExpr(Node->getCond());
1442 PrintExpr(Node->getLHS());
1444 PrintExpr(Node->getRHS());
1450 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1451 PrintExpr(Node->getCommon());
1453 PrintExpr(Node->getFalseExpr());
1456 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1457 OS << "&&" << Node->getLabel()->getName();
1460 void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1462 PrintRawCompoundStmt(E->getSubStmt());
1466 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1467 OS << "__builtin_choose_expr(";
1468 PrintExpr(Node->getCond());
1470 PrintExpr(Node->getLHS());
1472 PrintExpr(Node->getRHS());
1476 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1480 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1481 OS << "__builtin_shufflevector(";
1482 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1484 PrintExpr(Node->getExpr(i));
1489 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1490 OS << "__builtin_convertvector(";
1491 PrintExpr(Node->getSrcExpr());
1493 Node->getType().print(OS, Policy);
1497 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1498 if (Node->getSyntacticForm()) {
1499 Visit(Node->getSyntacticForm());
1504 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1506 if (Node->getInit(i))
1507 PrintExpr(Node->getInit(i));
1514 void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) {
1515 // There's no way to express this expression in any of our supported
1516 // languages, so just emit something terse and (hopefully) clear.
1518 PrintExpr(Node->getSubExpr());
1522 void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) {
1526 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1528 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1530 PrintExpr(Node->getExpr(i));
1535 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1536 bool NeedsEquals = true;
1537 for (const DesignatedInitExpr::Designator &D : Node->designators()) {
1538 if (D.isFieldDesignator()) {
1539 if (D.getDotLoc().isInvalid()) {
1540 if (IdentifierInfo *II = D.getFieldName()) {
1541 OS << II->getName() << ":";
1542 NeedsEquals = false;
1545 OS << "." << D.getFieldName()->getName();
1549 if (D.isArrayDesignator()) {
1550 PrintExpr(Node->getArrayIndex(D));
1552 PrintExpr(Node->getArrayRangeStart(D));
1554 PrintExpr(Node->getArrayRangeEnd(D));
1564 PrintExpr(Node->getInit());
1567 void StmtPrinter::VisitDesignatedInitUpdateExpr(
1568 DesignatedInitUpdateExpr *Node) {
1571 PrintExpr(Node->getBase());
1574 OS << "/*updater*/";
1575 PrintExpr(Node->getUpdater());
1579 void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1580 OS << "/*no init*/";
1583 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1584 if (Node->getType()->getAsCXXRecordDecl()) {
1585 OS << "/*implicit*/";
1586 Node->getType().print(OS, Policy);
1589 OS << "/*implicit*/(";
1590 Node->getType().print(OS, Policy);
1592 if (Node->getType()->isRecordType())
1599 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1600 OS << "__builtin_va_arg(";
1601 PrintExpr(Node->getSubExpr());
1603 Node->getType().print(OS, Policy);
1607 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1608 PrintExpr(Node->getSyntacticForm());
1611 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1612 const char *Name = nullptr;
1613 switch (Node->getOp()) {
1614 #define BUILTIN(ID, TYPE, ATTRS)
1615 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1616 case AtomicExpr::AO ## ID: \
1619 #include "clang/Basic/Builtins.def"
1623 // AtomicExpr stores its subexpressions in a permuted order.
1624 PrintExpr(Node->getPtr());
1625 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1626 Node->getOp() != AtomicExpr::AO__atomic_load_n &&
1627 Node->getOp() != AtomicExpr::AO__opencl_atomic_load) {
1629 PrintExpr(Node->getVal1());
1631 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1632 Node->isCmpXChg()) {
1634 PrintExpr(Node->getVal2());
1636 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1637 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1639 PrintExpr(Node->getWeak());
1641 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init &&
1642 Node->getOp() != AtomicExpr::AO__opencl_atomic_init) {
1644 PrintExpr(Node->getOrder());
1646 if (Node->isCmpXChg()) {
1648 PrintExpr(Node->getOrderFail());
1654 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1655 OverloadedOperatorKind Kind = Node->getOperator();
1656 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1657 if (Node->getNumArgs() == 1) {
1658 OS << getOperatorSpelling(Kind) << ' ';
1659 PrintExpr(Node->getArg(0));
1661 PrintExpr(Node->getArg(0));
1662 OS << ' ' << getOperatorSpelling(Kind);
1664 } else if (Kind == OO_Arrow) {
1665 PrintExpr(Node->getArg(0));
1666 } else if (Kind == OO_Call) {
1667 PrintExpr(Node->getArg(0));
1669 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1672 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1673 PrintExpr(Node->getArg(ArgIdx));
1676 } else if (Kind == OO_Subscript) {
1677 PrintExpr(Node->getArg(0));
1679 PrintExpr(Node->getArg(1));
1681 } else if (Node->getNumArgs() == 1) {
1682 OS << getOperatorSpelling(Kind) << ' ';
1683 PrintExpr(Node->getArg(0));
1684 } else if (Node->getNumArgs() == 2) {
1685 PrintExpr(Node->getArg(0));
1686 OS << ' ' << getOperatorSpelling(Kind) << ' ';
1687 PrintExpr(Node->getArg(1));
1689 llvm_unreachable("unknown overloaded operator");
1693 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1694 // If we have a conversion operator call only print the argument.
1695 CXXMethodDecl *MD = Node->getMethodDecl();
1696 if (MD && isa<CXXConversionDecl>(MD)) {
1697 PrintExpr(Node->getImplicitObjectArgument());
1700 VisitCallExpr(cast<CallExpr>(Node));
1703 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1704 PrintExpr(Node->getCallee());
1706 PrintCallArgs(Node->getConfig());
1708 PrintCallArgs(Node);
1712 void StmtPrinter::VisitCXXRewrittenBinaryOperator(
1713 CXXRewrittenBinaryOperator *Node) {
1714 CXXRewrittenBinaryOperator::DecomposedForm Decomposed =
1715 Node->getDecomposedForm();
1716 PrintExpr(const_cast<Expr*>(Decomposed.LHS));
1717 OS << ' ' << BinaryOperator::getOpcodeStr(Decomposed.Opcode) << ' ';
1718 PrintExpr(const_cast<Expr*>(Decomposed.RHS));
1721 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1722 OS << Node->getCastName() << '<';
1723 Node->getTypeAsWritten().print(OS, Policy);
1725 PrintExpr(Node->getSubExpr());
1729 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1730 VisitCXXNamedCastExpr(Node);
1733 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1734 VisitCXXNamedCastExpr(Node);
1737 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1738 VisitCXXNamedCastExpr(Node);
1741 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1742 VisitCXXNamedCastExpr(Node);
1745 void StmtPrinter::VisitBuiltinBitCastExpr(BuiltinBitCastExpr *Node) {
1746 OS << "__builtin_bit_cast(";
1747 Node->getTypeInfoAsWritten()->getType().print(OS, Policy);
1749 PrintExpr(Node->getSubExpr());
1753 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1755 if (Node->isTypeOperand()) {
1756 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1758 PrintExpr(Node->getExprOperand());
1763 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1765 if (Node->isTypeOperand()) {
1766 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1768 PrintExpr(Node->getExprOperand());
1773 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1774 PrintExpr(Node->getBaseExpr());
1775 if (Node->isArrow())
1779 if (NestedNameSpecifier *Qualifier =
1780 Node->getQualifierLoc().getNestedNameSpecifier())
1781 Qualifier->print(OS, Policy);
1782 OS << Node->getPropertyDecl()->getDeclName();
1785 void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
1786 PrintExpr(Node->getBase());
1788 PrintExpr(Node->getIdx());
1792 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1793 switch (Node->getLiteralOperatorKind()) {
1794 case UserDefinedLiteral::LOK_Raw:
1795 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
1797 case UserDefinedLiteral::LOK_Template: {
1798 const auto *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1799 const TemplateArgumentList *Args =
1800 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1803 if (Args->size() != 1) {
1804 OS << "operator\"\"" << Node->getUDSuffix()->getName();
1805 printTemplateArgumentList(OS, Args->asArray(), Policy);
1810 const TemplateArgument &Pack = Args->get(0);
1811 for (const auto &P : Pack.pack_elements()) {
1812 char C = (char)P.getAsIntegral().getZExtValue();
1817 case UserDefinedLiteral::LOK_Integer: {
1818 // Print integer literal without suffix.
1819 const auto *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1820 OS << Int->getValue().toString(10, /*isSigned*/false);
1823 case UserDefinedLiteral::LOK_Floating: {
1824 // Print floating literal without suffix.
1825 auto *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1826 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1829 case UserDefinedLiteral::LOK_String:
1830 case UserDefinedLiteral::LOK_Character:
1831 PrintExpr(Node->getCookedLiteral());
1834 OS << Node->getUDSuffix()->getName();
1837 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1838 OS << (Node->getValue() ? "true" : "false");
1841 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1845 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1849 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1850 if (!Node->getSubExpr())
1854 PrintExpr(Node->getSubExpr());
1858 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1859 // Nothing to print: we picked up the default argument.
1862 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1863 // Nothing to print: we picked up the default initializer.
1866 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1867 Node->getType().print(OS, Policy);
1868 // If there are no parens, this is list-initialization, and the braces are
1869 // part of the syntax of the inner construct.
1870 if (Node->getLParenLoc().isValid())
1872 PrintExpr(Node->getSubExpr());
1873 if (Node->getLParenLoc().isValid())
1877 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1878 PrintExpr(Node->getSubExpr());
1881 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1882 Node->getType().print(OS, Policy);
1883 if (Node->isStdInitListInitialization())
1884 /* Nothing to do; braces are part of creating the std::initializer_list. */;
1885 else if (Node->isListInitialization())
1889 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1890 ArgEnd = Node->arg_end();
1891 Arg != ArgEnd; ++Arg) {
1892 if ((*Arg)->isDefaultArgument())
1894 if (Arg != Node->arg_begin())
1898 if (Node->isStdInitListInitialization())
1900 else if (Node->isListInitialization())
1906 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1908 bool NeedComma = false;
1909 switch (Node->getCaptureDefault()) {
1923 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1924 CEnd = Node->explicit_capture_end();
1927 if (C->capturesVLAType())
1934 switch (C->getCaptureKind()) {
1944 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
1946 OS << C->getCapturedVar()->getName();
1950 OS << C->getCapturedVar()->getName();
1954 llvm_unreachable("VLA type in explicit captures.");
1957 if (C->isPackExpansion())
1960 if (Node->isInitCapture(C))
1961 PrintExpr(C->getCapturedVar()->getInit());
1965 if (!Node->getExplicitTemplateParameters().empty()) {
1966 Node->getTemplateParameterList()->print(
1967 OS, Node->getLambdaClass()->getASTContext(),
1968 /*OmitTemplateKW*/true);
1971 if (Node->hasExplicitParameters()) {
1973 CXXMethodDecl *Method = Node->getCallOperator();
1975 for (const auto *P : Method->parameters()) {
1981 std::string ParamStr = P->getNameAsString();
1982 P->getOriginalType().print(OS, Policy, ParamStr);
1984 if (Method->isVariadic()) {
1991 if (Node->isMutable())
1994 auto *Proto = Method->getType()->castAs<FunctionProtoType>();
1995 Proto->printExceptionSpecification(OS, Policy);
1997 // FIXME: Attributes
1999 // Print the trailing return type if it was specified in the source.
2000 if (Node->hasExplicitResultType()) {
2002 Proto->getReturnType().print(OS, Policy);
2008 if (Policy.TerseOutput)
2011 PrintRawCompoundStmt(Node->getBody());
2014 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
2015 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
2016 TSInfo->getType().print(OS, Policy);
2018 Node->getType().print(OS, Policy);
2022 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
2023 if (E->isGlobalNew())
2026 unsigned NumPlace = E->getNumPlacementArgs();
2027 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
2029 PrintExpr(E->getPlacementArg(0));
2030 for (unsigned i = 1; i < NumPlace; ++i) {
2031 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
2034 PrintExpr(E->getPlacementArg(i));
2038 if (E->isParenTypeId())
2041 if (Optional<Expr *> Size = E->getArraySize()) {
2042 llvm::raw_string_ostream s(TypeS);
2045 (*Size)->printPretty(s, Helper, Policy);
2048 E->getAllocatedType().print(OS, Policy, TypeS);
2049 if (E->isParenTypeId())
2052 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
2054 if (InitStyle == CXXNewExpr::CallInit)
2056 PrintExpr(E->getInitializer());
2057 if (InitStyle == CXXNewExpr::CallInit)
2062 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2063 if (E->isGlobalDelete())
2066 if (E->isArrayForm())
2068 PrintExpr(E->getArgument());
2071 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
2072 PrintExpr(E->getBase());
2077 if (E->getQualifier())
2078 E->getQualifier()->print(OS, Policy);
2081 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
2082 OS << II->getName();
2084 E->getDestroyedType().print(OS, Policy);
2087 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
2088 if (E->isListInitialization() && !E->isStdInitListInitialization())
2091 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
2092 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
2093 // Don't print any defaulted arguments
2098 PrintExpr(E->getArg(i));
2101 if (E->isListInitialization() && !E->isStdInitListInitialization())
2105 void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) {
2106 // Parens are printed by the surrounding context.
2107 OS << "<forwarded>";
2110 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
2111 PrintExpr(E->getSubExpr());
2114 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
2115 // Just forward to the subexpression.
2116 PrintExpr(E->getSubExpr());
2120 StmtPrinter::VisitCXXUnresolvedConstructExpr(
2121 CXXUnresolvedConstructExpr *Node) {
2122 Node->getTypeAsWritten().print(OS, Policy);
2124 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
2125 ArgEnd = Node->arg_end();
2126 Arg != ArgEnd; ++Arg) {
2127 if (Arg != Node->arg_begin())
2134 void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2135 CXXDependentScopeMemberExpr *Node) {
2136 if (!Node->isImplicitAccess()) {
2137 PrintExpr(Node->getBase());
2138 OS << (Node->isArrow() ? "->" : ".");
2140 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2141 Qualifier->print(OS, Policy);
2142 if (Node->hasTemplateKeyword())
2144 OS << Node->getMemberNameInfo();
2145 if (Node->hasExplicitTemplateArgs())
2146 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
2149 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
2150 if (!Node->isImplicitAccess()) {
2151 PrintExpr(Node->getBase());
2152 OS << (Node->isArrow() ? "->" : ".");
2154 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2155 Qualifier->print(OS, Policy);
2156 if (Node->hasTemplateKeyword())
2158 OS << Node->getMemberNameInfo();
2159 if (Node->hasExplicitTemplateArgs())
2160 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
2163 static const char *getTypeTraitName(TypeTrait TT) {
2165 #define TYPE_TRAIT_1(Spelling, Name, Key) \
2166 case clang::UTT_##Name: return #Spelling;
2167 #define TYPE_TRAIT_2(Spelling, Name, Key) \
2168 case clang::BTT_##Name: return #Spelling;
2169 #define TYPE_TRAIT_N(Spelling, Name, Key) \
2170 case clang::TT_##Name: return #Spelling;
2171 #include "clang/Basic/TokenKinds.def"
2173 llvm_unreachable("Type trait not covered by switch");
2176 static const char *getTypeTraitName(ArrayTypeTrait ATT) {
2178 case ATT_ArrayRank: return "__array_rank";
2179 case ATT_ArrayExtent: return "__array_extent";
2181 llvm_unreachable("Array type trait not covered by switch");
2184 static const char *getExpressionTraitName(ExpressionTrait ET) {
2186 case ET_IsLValueExpr: return "__is_lvalue_expr";
2187 case ET_IsRValueExpr: return "__is_rvalue_expr";
2189 llvm_unreachable("Expression type trait not covered by switch");
2192 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2193 OS << getTypeTraitName(E->getTrait()) << "(";
2194 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2197 E->getArg(I)->getType().print(OS, Policy);
2202 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
2203 OS << getTypeTraitName(E->getTrait()) << '(';
2204 E->getQueriedType().print(OS, Policy);
2208 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
2209 OS << getExpressionTraitName(E->getTrait()) << '(';
2210 PrintExpr(E->getQueriedExpression());
2214 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2216 PrintExpr(E->getOperand());
2220 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
2221 PrintExpr(E->getPattern());
2225 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2226 OS << "sizeof...(" << *E->getPack() << ")";
2229 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2230 SubstNonTypeTemplateParmPackExpr *Node) {
2231 OS << *Node->getParameterPack();
2234 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2235 SubstNonTypeTemplateParmExpr *Node) {
2236 Visit(Node->getReplacement());
2239 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2240 OS << *E->getParameterPack();
2243 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2244 PrintExpr(Node->getSubExpr());
2247 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2250 PrintExpr(E->getLHS());
2251 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2255 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2256 PrintExpr(E->getRHS());
2261 void StmtPrinter::VisitConceptSpecializationExpr(ConceptSpecializationExpr *E) {
2262 NestedNameSpecifierLoc NNS = E->getNestedNameSpecifierLoc();
2264 NNS.getNestedNameSpecifier()->print(OS, Policy);
2265 if (E->getTemplateKWLoc().isValid())
2267 OS << E->getFoundDecl()->getName();
2268 printTemplateArgumentList(OS, E->getTemplateArgsAsWritten()->arguments(),
2272 void StmtPrinter::VisitRequiresExpr(RequiresExpr *E) {
2274 auto LocalParameters = E->getLocalParameters();
2275 if (!LocalParameters.empty()) {
2277 for (ParmVarDecl *LocalParam : LocalParameters) {
2278 PrintRawDecl(LocalParam);
2279 if (LocalParam != LocalParameters.back())
2286 auto Requirements = E->getRequirements();
2287 for (concepts::Requirement *Req : Requirements) {
2288 if (auto *TypeReq = dyn_cast<concepts::TypeRequirement>(Req)) {
2289 if (TypeReq->isSubstitutionFailure())
2290 OS << "<<error-type>>";
2292 TypeReq->getType()->getType().print(OS, Policy);
2293 } else if (auto *ExprReq = dyn_cast<concepts::ExprRequirement>(Req)) {
2294 if (ExprReq->isCompound())
2296 if (ExprReq->isExprSubstitutionFailure())
2297 OS << "<<error-expression>>";
2299 PrintExpr(ExprReq->getExpr());
2300 if (ExprReq->isCompound()) {
2302 if (ExprReq->getNoexceptLoc().isValid())
2304 const auto &RetReq = ExprReq->getReturnTypeRequirement();
2305 if (!RetReq.isEmpty()) {
2307 if (RetReq.isSubstitutionFailure())
2308 OS << "<<error-type>>";
2309 else if (RetReq.isTypeConstraint())
2310 RetReq.getTypeConstraint()->print(OS, Policy);
2314 auto *NestedReq = cast<concepts::NestedRequirement>(Req);
2316 if (NestedReq->isSubstitutionFailure())
2317 OS << "<<error-expression>>";
2319 PrintExpr(NestedReq->getConstraintExpr());
2326 // C++ Coroutines TS
2328 void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
2329 Visit(S->getBody());
2332 void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
2334 if (S->getOperand()) {
2336 Visit(S->getOperand());
2341 void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
2343 PrintExpr(S->getOperand());
2346 void StmtPrinter::VisitDependentCoawaitExpr(DependentCoawaitExpr *S) {
2348 PrintExpr(S->getOperand());
2351 void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
2353 PrintExpr(S->getOperand());
2358 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2360 VisitStringLiteral(Node->getString());
2363 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
2365 Visit(E->getSubExpr());
2368 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2370 ObjCArrayLiteral::child_range Ch = E->children();
2371 for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2372 if (I != Ch.begin())
2379 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2381 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2385 ObjCDictionaryElement Element = E->getKeyValueElement(I);
2388 Visit(Element.Value);
2389 if (Element.isPackExpansion())
2395 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
2397 Node->getEncodedType().print(OS, Policy);
2401 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
2403 Node->getSelector().print(OS);
2407 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
2408 OS << "@protocol(" << *Node->getProtocol() << ')';
2411 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2413 switch (Mess->getReceiverKind()) {
2414 case ObjCMessageExpr::Instance:
2415 PrintExpr(Mess->getInstanceReceiver());
2418 case ObjCMessageExpr::Class:
2419 Mess->getClassReceiver().print(OS, Policy);
2422 case ObjCMessageExpr::SuperInstance:
2423 case ObjCMessageExpr::SuperClass:
2429 Selector selector = Mess->getSelector();
2430 if (selector.isUnarySelector()) {
2431 OS << selector.getNameForSlot(0);
2433 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
2434 if (i < selector.getNumArgs()) {
2435 if (i > 0) OS << ' ';
2436 if (selector.getIdentifierInfoForSlot(i))
2437 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
2441 else OS << ", "; // Handle variadic methods.
2443 PrintExpr(Mess->getArg(i));
2449 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2450 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2454 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2455 PrintExpr(E->getSubExpr());
2459 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
2460 OS << '(' << E->getBridgeKindName();
2461 E->getType().print(OS, Policy);
2463 PrintExpr(E->getSubExpr());
2466 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
2467 BlockDecl *BD = Node->getBlockDecl();
2470 const FunctionType *AFT = Node->getFunctionType();
2472 if (isa<FunctionNoProtoType>(AFT)) {
2474 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
2476 for (BlockDecl::param_iterator AI = BD->param_begin(),
2477 E = BD->param_end(); AI != E; ++AI) {
2478 if (AI != BD->param_begin()) OS << ", ";
2479 std::string ParamStr = (*AI)->getNameAsString();
2480 (*AI)->getType().print(OS, Policy, ParamStr);
2483 const auto *FT = cast<FunctionProtoType>(AFT);
2484 if (FT->isVariadic()) {
2485 if (!BD->param_empty()) OS << ", ";
2493 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2494 PrintExpr(Node->getSourceExpr());
2497 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2498 // TODO: Print something reasonable for a TypoExpr, if necessary.
2499 llvm_unreachable("Cannot print TypoExpr nodes");
2502 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2503 OS << "__builtin_astype(";
2504 PrintExpr(Node->getSrcExpr());
2506 Node->getType().print(OS, Policy);
2510 //===----------------------------------------------------------------------===//
2511 // Stmt method implementations
2512 //===----------------------------------------------------------------------===//
2514 void Stmt::dumpPretty(const ASTContext &Context) const {
2515 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
2518 void Stmt::printPretty(raw_ostream &Out, PrinterHelper *Helper,
2519 const PrintingPolicy &Policy, unsigned Indentation,
2520 StringRef NL, const ASTContext *Context) const {
2521 StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context);
2522 P.Visit(const_cast<Stmt *>(this));
2525 void Stmt::printJson(raw_ostream &Out, PrinterHelper *Helper,
2526 const PrintingPolicy &Policy, bool AddQuotes) const {
2528 llvm::raw_string_ostream TempOut(Buf);
2530 printPretty(TempOut, Helper, Policy);
2532 Out << JsonFormat(TempOut.str(), AddQuotes);
2535 //===----------------------------------------------------------------------===//
2537 //===----------------------------------------------------------------------===//
2539 // Implement virtual destructor.
2540 PrinterHelper::~PrinterHelper() = default;