1 //===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which
11 // pretty print the AST back out to C code.
13 //===----------------------------------------------------------------------===//
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Attr.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/DeclOpenMP.h"
20 #include "clang/AST/DeclTemplate.h"
21 #include "clang/AST/Expr.h"
22 #include "clang/AST/ExprCXX.h"
23 #include "clang/AST/ExprOpenMP.h"
24 #include "clang/AST/PrettyPrinter.h"
25 #include "clang/AST/StmtVisitor.h"
26 #include "clang/Basic/CharInfo.h"
27 #include "llvm/ADT/SmallString.h"
28 #include "llvm/Support/Format.h"
29 using namespace clang;
31 //===----------------------------------------------------------------------===//
32 // StmtPrinter Visitor
33 //===----------------------------------------------------------------------===//
36 class StmtPrinter : public StmtVisitor<StmtPrinter> {
39 clang::PrinterHelper* Helper;
40 PrintingPolicy Policy;
43 StmtPrinter(raw_ostream &os, PrinterHelper* helper,
44 const PrintingPolicy &Policy,
45 unsigned Indentation = 0)
46 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy) {}
48 void PrintStmt(Stmt *S) {
49 PrintStmt(S, Policy.Indentation);
52 void PrintStmt(Stmt *S, int SubIndent) {
53 IndentLevel += SubIndent;
54 if (S && isa<Expr>(S)) {
55 // If this is an expr used in a stmt context, indent and newline it.
62 Indent() << "<<<NULL STATEMENT>>>\n";
64 IndentLevel -= SubIndent;
67 void PrintRawCompoundStmt(CompoundStmt *S);
68 void PrintRawDecl(Decl *D);
69 void PrintRawDeclStmt(const DeclStmt *S);
70 void PrintRawIfStmt(IfStmt *If);
71 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
72 void PrintCallArgs(CallExpr *E);
73 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
74 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
75 void PrintOMPExecutableDirective(OMPExecutableDirective *S);
77 void PrintExpr(Expr *E) {
84 raw_ostream &Indent(int Delta = 0) {
85 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
91 if (Helper && Helper->handledStmt(S,OS))
93 else StmtVisitor<StmtPrinter>::Visit(S);
96 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
97 Indent() << "<<unknown stmt type>>\n";
99 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
100 OS << "<<unknown expr type>>";
102 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
104 #define ABSTRACT_STMT(CLASS)
105 #define STMT(CLASS, PARENT) \
106 void Visit##CLASS(CLASS *Node);
107 #include "clang/AST/StmtNodes.inc"
111 //===----------------------------------------------------------------------===//
112 // Stmt printing methods.
113 //===----------------------------------------------------------------------===//
115 /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
116 /// with no newline after the }.
117 void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
119 for (auto *I : Node->body())
125 void StmtPrinter::PrintRawDecl(Decl *D) {
126 D->print(OS, Policy, IndentLevel);
129 void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
130 SmallVector<Decl*, 2> Decls(S->decls());
131 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
134 void StmtPrinter::VisitNullStmt(NullStmt *Node) {
138 void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
140 PrintRawDeclStmt(Node);
144 void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
146 PrintRawCompoundStmt(Node);
150 void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
151 Indent(-1) << "case ";
152 PrintExpr(Node->getLHS());
153 if (Node->getRHS()) {
155 PrintExpr(Node->getRHS());
159 PrintStmt(Node->getSubStmt(), 0);
162 void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
163 Indent(-1) << "default:\n";
164 PrintStmt(Node->getSubStmt(), 0);
167 void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
168 Indent(-1) << Node->getName() << ":\n";
169 PrintStmt(Node->getSubStmt(), 0);
172 void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
173 for (const auto *Attr : Node->getAttrs()) {
174 Attr->printPretty(OS, Policy);
177 PrintStmt(Node->getSubStmt(), 0);
180 void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
182 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
183 PrintRawDeclStmt(DS);
185 PrintExpr(If->getCond());
188 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
190 PrintRawCompoundStmt(CS);
191 OS << (If->getElse() ? ' ' : '\n');
194 PrintStmt(If->getThen());
195 if (If->getElse()) Indent();
198 if (Stmt *Else = If->getElse()) {
201 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
203 PrintRawCompoundStmt(CS);
205 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
207 PrintRawIfStmt(ElseIf);
210 PrintStmt(If->getElse());
215 void StmtPrinter::VisitIfStmt(IfStmt *If) {
220 void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
221 Indent() << "switch (";
222 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
223 PrintRawDeclStmt(DS);
225 PrintExpr(Node->getCond());
228 // Pretty print compoundstmt bodies (very common).
229 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
231 PrintRawCompoundStmt(CS);
235 PrintStmt(Node->getBody());
239 void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
240 Indent() << "while (";
241 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
242 PrintRawDeclStmt(DS);
244 PrintExpr(Node->getCond());
246 PrintStmt(Node->getBody());
249 void StmtPrinter::VisitDoStmt(DoStmt *Node) {
251 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
252 PrintRawCompoundStmt(CS);
256 PrintStmt(Node->getBody());
261 PrintExpr(Node->getCond());
265 void StmtPrinter::VisitForStmt(ForStmt *Node) {
267 if (Node->getInit()) {
268 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
269 PrintRawDeclStmt(DS);
271 PrintExpr(cast<Expr>(Node->getInit()));
274 if (Node->getCond()) {
276 PrintExpr(Node->getCond());
279 if (Node->getInc()) {
281 PrintExpr(Node->getInc());
285 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
286 PrintRawCompoundStmt(CS);
290 PrintStmt(Node->getBody());
294 void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
296 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
297 PrintRawDeclStmt(DS);
299 PrintExpr(cast<Expr>(Node->getElement()));
301 PrintExpr(Node->getCollection());
304 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
305 PrintRawCompoundStmt(CS);
309 PrintStmt(Node->getBody());
313 void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
315 PrintingPolicy SubPolicy(Policy);
316 SubPolicy.SuppressInitializers = true;
317 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
319 PrintExpr(Node->getRangeInit());
321 PrintStmt(Node->getBody());
323 if (Policy.IncludeNewlines) OS << "\n";
326 void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
328 if (Node->isIfExists())
329 OS << "__if_exists (";
331 OS << "__if_not_exists (";
333 if (NestedNameSpecifier *Qualifier
334 = Node->getQualifierLoc().getNestedNameSpecifier())
335 Qualifier->print(OS, Policy);
337 OS << Node->getNameInfo() << ") ";
339 PrintRawCompoundStmt(Node->getSubStmt());
342 void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
343 Indent() << "goto " << Node->getLabel()->getName() << ";";
344 if (Policy.IncludeNewlines) OS << "\n";
347 void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
348 Indent() << "goto *";
349 PrintExpr(Node->getTarget());
351 if (Policy.IncludeNewlines) OS << "\n";
354 void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
355 Indent() << "continue;";
356 if (Policy.IncludeNewlines) OS << "\n";
359 void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
360 Indent() << "break;";
361 if (Policy.IncludeNewlines) OS << "\n";
365 void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
366 Indent() << "return";
367 if (Node->getRetValue()) {
369 PrintExpr(Node->getRetValue());
372 if (Policy.IncludeNewlines) OS << "\n";
376 void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
379 if (Node->isVolatile())
383 VisitStringLiteral(Node->getAsmString());
386 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
387 Node->getNumClobbers() != 0)
390 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
394 if (!Node->getOutputName(i).empty()) {
396 OS << Node->getOutputName(i);
400 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
402 Visit(Node->getOutputExpr(i));
407 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
410 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
414 if (!Node->getInputName(i).empty()) {
416 OS << Node->getInputName(i);
420 VisitStringLiteral(Node->getInputConstraintLiteral(i));
422 Visit(Node->getInputExpr(i));
427 if (Node->getNumClobbers() != 0)
430 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
434 VisitStringLiteral(Node->getClobberStringLiteral(i));
438 if (Policy.IncludeNewlines) OS << "\n";
441 void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
442 // FIXME: Implement MS style inline asm statement printer.
443 Indent() << "__asm ";
444 if (Node->hasBraces())
446 OS << Node->getAsmString() << "\n";
447 if (Node->hasBraces())
451 void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
452 PrintStmt(Node->getCapturedDecl()->getBody());
455 void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
457 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
458 PrintRawCompoundStmt(TS);
462 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
463 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
464 Indent() << "@catch(";
465 if (catchStmt->getCatchParamDecl()) {
466 if (Decl *DS = catchStmt->getCatchParamDecl())
470 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
471 PrintRawCompoundStmt(CS);
476 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
477 Node->getFinallyStmt())) {
478 Indent() << "@finally";
479 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
484 void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
487 void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
488 Indent() << "@catch (...) { /* todo */ } \n";
491 void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
492 Indent() << "@throw";
493 if (Node->getThrowExpr()) {
495 PrintExpr(Node->getThrowExpr());
500 void StmtPrinter::VisitObjCAvailabilityCheckExpr(
501 ObjCAvailabilityCheckExpr *Node) {
502 OS << "@available(...)";
505 void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
506 Indent() << "@synchronized (";
507 PrintExpr(Node->getSynchExpr());
509 PrintRawCompoundStmt(Node->getSynchBody());
513 void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
514 Indent() << "@autoreleasepool";
515 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
519 void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
521 if (Decl *ExDecl = Node->getExceptionDecl())
522 PrintRawDecl(ExDecl);
526 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
529 void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
531 PrintRawCXXCatchStmt(Node);
535 void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
537 PrintRawCompoundStmt(Node->getTryBlock());
538 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
540 PrintRawCXXCatchStmt(Node->getHandler(i));
545 void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
546 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
547 PrintRawCompoundStmt(Node->getTryBlock());
548 SEHExceptStmt *E = Node->getExceptHandler();
549 SEHFinallyStmt *F = Node->getFinallyHandler();
551 PrintRawSEHExceptHandler(E);
553 assert(F && "Must have a finally block...");
554 PrintRawSEHFinallyStmt(F);
559 void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
561 PrintRawCompoundStmt(Node->getBlock());
565 void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
567 VisitExpr(Node->getFilterExpr());
569 PrintRawCompoundStmt(Node->getBlock());
573 void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
575 PrintRawSEHExceptHandler(Node);
579 void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
581 PrintRawSEHFinallyStmt(Node);
585 void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
586 Indent() << "__leave;";
587 if (Policy.IncludeNewlines) OS << "\n";
590 //===----------------------------------------------------------------------===//
591 // OpenMP clauses printing methods
592 //===----------------------------------------------------------------------===//
595 class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
597 const PrintingPolicy &Policy;
598 /// \brief Process clauses with list of variables.
599 template <typename T>
600 void VisitOMPClauseList(T *Node, char StartSym);
602 OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy)
603 : OS(OS), Policy(Policy) { }
604 #define OPENMP_CLAUSE(Name, Class) \
605 void Visit##Class(Class *S);
606 #include "clang/Basic/OpenMPKinds.def"
609 void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
611 if (Node->getNameModifier() != OMPD_unknown)
612 OS << getOpenMPDirectiveName(Node->getNameModifier()) << ": ";
613 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
617 void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) {
619 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
623 void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
624 OS << "num_threads(";
625 Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0);
629 void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
631 Node->getSafelen()->printPretty(OS, nullptr, Policy, 0);
635 void OMPClausePrinter::VisitOMPSimdlenClause(OMPSimdlenClause *Node) {
637 Node->getSimdlen()->printPretty(OS, nullptr, Policy, 0);
641 void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) {
643 Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0);
647 void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
649 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
653 void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) {
655 << getOpenMPSimpleClauseTypeName(OMPC_proc_bind, Node->getProcBindKind())
659 void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) {
661 if (Node->getFirstScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
662 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
663 Node->getFirstScheduleModifier());
664 if (Node->getSecondScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
666 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
667 Node->getSecondScheduleModifier());
671 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind());
672 if (auto *E = Node->getChunkSize()) {
674 E->printPretty(OS, nullptr, Policy);
679 void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *Node) {
681 if (auto *Num = Node->getNumForLoops()) {
683 Num->printPretty(OS, nullptr, Policy, 0);
688 void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) {
692 void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) {
696 void OMPClausePrinter::VisitOMPNogroupClause(OMPNogroupClause *) {
700 void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) {
704 void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; }
706 void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; }
708 void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *) {
712 void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) {
716 void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) {
720 void OMPClausePrinter::VisitOMPThreadsClause(OMPThreadsClause *) {
724 void OMPClausePrinter::VisitOMPSIMDClause(OMPSIMDClause *) { OS << "simd"; }
726 void OMPClausePrinter::VisitOMPDeviceClause(OMPDeviceClause *Node) {
728 Node->getDevice()->printPretty(OS, nullptr, Policy, 0);
732 void OMPClausePrinter::VisitOMPNumTeamsClause(OMPNumTeamsClause *Node) {
734 Node->getNumTeams()->printPretty(OS, nullptr, Policy, 0);
738 void OMPClausePrinter::VisitOMPThreadLimitClause(OMPThreadLimitClause *Node) {
739 OS << "thread_limit(";
740 Node->getThreadLimit()->printPretty(OS, nullptr, Policy, 0);
744 void OMPClausePrinter::VisitOMPPriorityClause(OMPPriorityClause *Node) {
746 Node->getPriority()->printPretty(OS, nullptr, Policy, 0);
750 void OMPClausePrinter::VisitOMPGrainsizeClause(OMPGrainsizeClause *Node) {
752 Node->getGrainsize()->printPretty(OS, nullptr, Policy, 0);
756 void OMPClausePrinter::VisitOMPNumTasksClause(OMPNumTasksClause *Node) {
758 Node->getNumTasks()->printPretty(OS, nullptr, Policy, 0);
762 void OMPClausePrinter::VisitOMPHintClause(OMPHintClause *Node) {
764 Node->getHint()->printPretty(OS, nullptr, Policy, 0);
769 void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
770 for (typename T::varlist_iterator I = Node->varlist_begin(),
771 E = Node->varlist_end();
773 assert(*I && "Expected non-null Stmt");
774 OS << (I == Node->varlist_begin() ? StartSym : ',');
775 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*I)) {
776 if (isa<OMPCapturedExprDecl>(DRE->getDecl()))
777 DRE->printPretty(OS, nullptr, Policy, 0);
779 DRE->getDecl()->printQualifiedName(OS);
781 (*I)->printPretty(OS, nullptr, Policy, 0);
785 void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
786 if (!Node->varlist_empty()) {
788 VisitOMPClauseList(Node, '(');
793 void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
794 if (!Node->varlist_empty()) {
795 OS << "firstprivate";
796 VisitOMPClauseList(Node, '(');
801 void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) {
802 if (!Node->varlist_empty()) {
804 VisitOMPClauseList(Node, '(');
809 void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
810 if (!Node->varlist_empty()) {
812 VisitOMPClauseList(Node, '(');
817 void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) {
818 if (!Node->varlist_empty()) {
820 NestedNameSpecifier *QualifierLoc =
821 Node->getQualifierLoc().getNestedNameSpecifier();
822 OverloadedOperatorKind OOK =
823 Node->getNameInfo().getName().getCXXOverloadedOperator();
824 if (QualifierLoc == nullptr && OOK != OO_None) {
825 // Print reduction identifier in C format
826 OS << getOperatorSpelling(OOK);
829 if (QualifierLoc != nullptr)
830 QualifierLoc->print(OS, Policy);
831 OS << Node->getNameInfo();
834 VisitOMPClauseList(Node, ' ');
839 void OMPClausePrinter::VisitOMPTaskReductionClause(
840 OMPTaskReductionClause *Node) {
841 if (!Node->varlist_empty()) {
842 OS << "task_reduction(";
843 NestedNameSpecifier *QualifierLoc =
844 Node->getQualifierLoc().getNestedNameSpecifier();
845 OverloadedOperatorKind OOK =
846 Node->getNameInfo().getName().getCXXOverloadedOperator();
847 if (QualifierLoc == nullptr && OOK != OO_None) {
848 // Print reduction identifier in C format
849 OS << getOperatorSpelling(OOK);
852 if (QualifierLoc != nullptr)
853 QualifierLoc->print(OS, Policy);
854 OS << Node->getNameInfo();
857 VisitOMPClauseList(Node, ' ');
862 void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) {
863 if (!Node->varlist_empty()) {
865 if (Node->getModifierLoc().isValid()) {
867 << getOpenMPSimpleClauseTypeName(OMPC_linear, Node->getModifier());
869 VisitOMPClauseList(Node, '(');
870 if (Node->getModifierLoc().isValid())
872 if (Node->getStep() != nullptr) {
874 Node->getStep()->printPretty(OS, nullptr, Policy, 0);
880 void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) {
881 if (!Node->varlist_empty()) {
883 VisitOMPClauseList(Node, '(');
884 if (Node->getAlignment() != nullptr) {
886 Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
892 void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
893 if (!Node->varlist_empty()) {
895 VisitOMPClauseList(Node, '(');
900 void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) {
901 if (!Node->varlist_empty()) {
903 VisitOMPClauseList(Node, '(');
908 void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) {
909 if (!Node->varlist_empty()) {
910 VisitOMPClauseList(Node, '(');
915 void OMPClausePrinter::VisitOMPDependClause(OMPDependClause *Node) {
917 OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
918 Node->getDependencyKind());
919 if (!Node->varlist_empty()) {
921 VisitOMPClauseList(Node, ' ');
926 void OMPClausePrinter::VisitOMPMapClause(OMPMapClause *Node) {
927 if (!Node->varlist_empty()) {
929 if (Node->getMapType() != OMPC_MAP_unknown) {
930 if (Node->getMapTypeModifier() != OMPC_MAP_unknown) {
931 OS << getOpenMPSimpleClauseTypeName(OMPC_map,
932 Node->getMapTypeModifier());
935 OS << getOpenMPSimpleClauseTypeName(OMPC_map, Node->getMapType());
938 VisitOMPClauseList(Node, ' ');
943 void OMPClausePrinter::VisitOMPToClause(OMPToClause *Node) {
944 if (!Node->varlist_empty()) {
946 VisitOMPClauseList(Node, '(');
951 void OMPClausePrinter::VisitOMPFromClause(OMPFromClause *Node) {
952 if (!Node->varlist_empty()) {
954 VisitOMPClauseList(Node, '(');
959 void OMPClausePrinter::VisitOMPDistScheduleClause(OMPDistScheduleClause *Node) {
960 OS << "dist_schedule(" << getOpenMPSimpleClauseTypeName(
961 OMPC_dist_schedule, Node->getDistScheduleKind());
962 if (auto *E = Node->getChunkSize()) {
964 E->printPretty(OS, nullptr, Policy);
969 void OMPClausePrinter::VisitOMPDefaultmapClause(OMPDefaultmapClause *Node) {
971 OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
972 Node->getDefaultmapModifier());
974 OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
975 Node->getDefaultmapKind());
979 void OMPClausePrinter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *Node) {
980 if (!Node->varlist_empty()) {
981 OS << "use_device_ptr";
982 VisitOMPClauseList(Node, '(');
987 void OMPClausePrinter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *Node) {
988 if (!Node->varlist_empty()) {
989 OS << "is_device_ptr";
990 VisitOMPClauseList(Node, '(');
996 //===----------------------------------------------------------------------===//
997 // OpenMP directives printing methods
998 //===----------------------------------------------------------------------===//
1000 void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) {
1001 OMPClausePrinter Printer(OS, Policy);
1002 ArrayRef<OMPClause *> Clauses = S->clauses();
1003 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
1005 if (*I && !(*I)->isImplicit()) {
1010 if (S->hasAssociatedStmt() && S->getAssociatedStmt()) {
1011 assert(isa<CapturedStmt>(S->getAssociatedStmt()) &&
1012 "Expected captured statement!");
1013 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt();
1018 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
1019 Indent() << "#pragma omp parallel ";
1020 PrintOMPExecutableDirective(Node);
1023 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
1024 Indent() << "#pragma omp simd ";
1025 PrintOMPExecutableDirective(Node);
1028 void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
1029 Indent() << "#pragma omp for ";
1030 PrintOMPExecutableDirective(Node);
1033 void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
1034 Indent() << "#pragma omp for simd ";
1035 PrintOMPExecutableDirective(Node);
1038 void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
1039 Indent() << "#pragma omp sections ";
1040 PrintOMPExecutableDirective(Node);
1043 void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
1044 Indent() << "#pragma omp section";
1045 PrintOMPExecutableDirective(Node);
1048 void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
1049 Indent() << "#pragma omp single ";
1050 PrintOMPExecutableDirective(Node);
1053 void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
1054 Indent() << "#pragma omp master";
1055 PrintOMPExecutableDirective(Node);
1058 void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
1059 Indent() << "#pragma omp critical";
1060 if (Node->getDirectiveName().getName()) {
1062 Node->getDirectiveName().printName(OS);
1066 PrintOMPExecutableDirective(Node);
1069 void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
1070 Indent() << "#pragma omp parallel for ";
1071 PrintOMPExecutableDirective(Node);
1074 void StmtPrinter::VisitOMPParallelForSimdDirective(
1075 OMPParallelForSimdDirective *Node) {
1076 Indent() << "#pragma omp parallel for simd ";
1077 PrintOMPExecutableDirective(Node);
1080 void StmtPrinter::VisitOMPParallelSectionsDirective(
1081 OMPParallelSectionsDirective *Node) {
1082 Indent() << "#pragma omp parallel sections ";
1083 PrintOMPExecutableDirective(Node);
1086 void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
1087 Indent() << "#pragma omp task ";
1088 PrintOMPExecutableDirective(Node);
1091 void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
1092 Indent() << "#pragma omp taskyield";
1093 PrintOMPExecutableDirective(Node);
1096 void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
1097 Indent() << "#pragma omp barrier";
1098 PrintOMPExecutableDirective(Node);
1101 void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
1102 Indent() << "#pragma omp taskwait";
1103 PrintOMPExecutableDirective(Node);
1106 void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
1107 Indent() << "#pragma omp taskgroup ";
1108 PrintOMPExecutableDirective(Node);
1111 void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
1112 Indent() << "#pragma omp flush ";
1113 PrintOMPExecutableDirective(Node);
1116 void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
1117 Indent() << "#pragma omp ordered ";
1118 PrintOMPExecutableDirective(Node);
1121 void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
1122 Indent() << "#pragma omp atomic ";
1123 PrintOMPExecutableDirective(Node);
1126 void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
1127 Indent() << "#pragma omp target ";
1128 PrintOMPExecutableDirective(Node);
1131 void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
1132 Indent() << "#pragma omp target data ";
1133 PrintOMPExecutableDirective(Node);
1136 void StmtPrinter::VisitOMPTargetEnterDataDirective(
1137 OMPTargetEnterDataDirective *Node) {
1138 Indent() << "#pragma omp target enter data ";
1139 PrintOMPExecutableDirective(Node);
1142 void StmtPrinter::VisitOMPTargetExitDataDirective(
1143 OMPTargetExitDataDirective *Node) {
1144 Indent() << "#pragma omp target exit data ";
1145 PrintOMPExecutableDirective(Node);
1148 void StmtPrinter::VisitOMPTargetParallelDirective(
1149 OMPTargetParallelDirective *Node) {
1150 Indent() << "#pragma omp target parallel ";
1151 PrintOMPExecutableDirective(Node);
1154 void StmtPrinter::VisitOMPTargetParallelForDirective(
1155 OMPTargetParallelForDirective *Node) {
1156 Indent() << "#pragma omp target parallel for ";
1157 PrintOMPExecutableDirective(Node);
1160 void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
1161 Indent() << "#pragma omp teams ";
1162 PrintOMPExecutableDirective(Node);
1165 void StmtPrinter::VisitOMPCancellationPointDirective(
1166 OMPCancellationPointDirective *Node) {
1167 Indent() << "#pragma omp cancellation point "
1168 << getOpenMPDirectiveName(Node->getCancelRegion());
1169 PrintOMPExecutableDirective(Node);
1172 void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
1173 Indent() << "#pragma omp cancel "
1174 << getOpenMPDirectiveName(Node->getCancelRegion()) << " ";
1175 PrintOMPExecutableDirective(Node);
1178 void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) {
1179 Indent() << "#pragma omp taskloop ";
1180 PrintOMPExecutableDirective(Node);
1183 void StmtPrinter::VisitOMPTaskLoopSimdDirective(
1184 OMPTaskLoopSimdDirective *Node) {
1185 Indent() << "#pragma omp taskloop simd ";
1186 PrintOMPExecutableDirective(Node);
1189 void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) {
1190 Indent() << "#pragma omp distribute ";
1191 PrintOMPExecutableDirective(Node);
1194 void StmtPrinter::VisitOMPTargetUpdateDirective(
1195 OMPTargetUpdateDirective *Node) {
1196 Indent() << "#pragma omp target update ";
1197 PrintOMPExecutableDirective(Node);
1200 void StmtPrinter::VisitOMPDistributeParallelForDirective(
1201 OMPDistributeParallelForDirective *Node) {
1202 Indent() << "#pragma omp distribute parallel for ";
1203 PrintOMPExecutableDirective(Node);
1206 void StmtPrinter::VisitOMPDistributeParallelForSimdDirective(
1207 OMPDistributeParallelForSimdDirective *Node) {
1208 Indent() << "#pragma omp distribute parallel for simd ";
1209 PrintOMPExecutableDirective(Node);
1212 void StmtPrinter::VisitOMPDistributeSimdDirective(
1213 OMPDistributeSimdDirective *Node) {
1214 Indent() << "#pragma omp distribute simd ";
1215 PrintOMPExecutableDirective(Node);
1218 void StmtPrinter::VisitOMPTargetParallelForSimdDirective(
1219 OMPTargetParallelForSimdDirective *Node) {
1220 Indent() << "#pragma omp target parallel for simd ";
1221 PrintOMPExecutableDirective(Node);
1224 void StmtPrinter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective *Node) {
1225 Indent() << "#pragma omp target simd ";
1226 PrintOMPExecutableDirective(Node);
1229 void StmtPrinter::VisitOMPTeamsDistributeDirective(
1230 OMPTeamsDistributeDirective *Node) {
1231 Indent() << "#pragma omp teams distribute ";
1232 PrintOMPExecutableDirective(Node);
1235 void StmtPrinter::VisitOMPTeamsDistributeSimdDirective(
1236 OMPTeamsDistributeSimdDirective *Node) {
1237 Indent() << "#pragma omp teams distribute simd ";
1238 PrintOMPExecutableDirective(Node);
1241 void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective(
1242 OMPTeamsDistributeParallelForSimdDirective *Node) {
1243 Indent() << "#pragma omp teams distribute parallel for simd ";
1244 PrintOMPExecutableDirective(Node);
1247 void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective(
1248 OMPTeamsDistributeParallelForDirective *Node) {
1249 Indent() << "#pragma omp teams distribute parallel for ";
1250 PrintOMPExecutableDirective(Node);
1253 void StmtPrinter::VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective *Node) {
1254 Indent() << "#pragma omp target teams ";
1255 PrintOMPExecutableDirective(Node);
1258 void StmtPrinter::VisitOMPTargetTeamsDistributeDirective(
1259 OMPTargetTeamsDistributeDirective *Node) {
1260 Indent() << "#pragma omp target teams distribute ";
1261 PrintOMPExecutableDirective(Node);
1264 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective(
1265 OMPTargetTeamsDistributeParallelForDirective *Node) {
1266 Indent() << "#pragma omp target teams distribute parallel for ";
1267 PrintOMPExecutableDirective(Node);
1270 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
1271 OMPTargetTeamsDistributeParallelForSimdDirective *Node) {
1272 Indent() << "#pragma omp target teams distribute parallel for simd ";
1273 PrintOMPExecutableDirective(Node);
1276 void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective(
1277 OMPTargetTeamsDistributeSimdDirective *Node) {
1278 Indent() << "#pragma omp target teams distribute simd ";
1279 PrintOMPExecutableDirective(Node);
1282 //===----------------------------------------------------------------------===//
1283 // Expr printing methods.
1284 //===----------------------------------------------------------------------===//
1286 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
1287 if (auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) {
1288 OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy);
1291 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1292 Qualifier->print(OS, Policy);
1293 if (Node->hasTemplateKeyword())
1295 OS << Node->getNameInfo();
1296 if (Node->hasExplicitTemplateArgs())
1297 TemplateSpecializationType::PrintTemplateArgumentList(
1298 OS, Node->template_arguments(), Policy);
1301 void StmtPrinter::VisitDependentScopeDeclRefExpr(
1302 DependentScopeDeclRefExpr *Node) {
1303 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1304 Qualifier->print(OS, Policy);
1305 if (Node->hasTemplateKeyword())
1307 OS << Node->getNameInfo();
1308 if (Node->hasExplicitTemplateArgs())
1309 TemplateSpecializationType::PrintTemplateArgumentList(
1310 OS, Node->template_arguments(), Policy);
1313 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
1314 if (Node->getQualifier())
1315 Node->getQualifier()->print(OS, Policy);
1316 if (Node->hasTemplateKeyword())
1318 OS << Node->getNameInfo();
1319 if (Node->hasExplicitTemplateArgs())
1320 TemplateSpecializationType::PrintTemplateArgumentList(
1321 OS, Node->template_arguments(), Policy);
1324 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
1325 if (Node->getBase()) {
1326 PrintExpr(Node->getBase());
1327 OS << (Node->isArrow() ? "->" : ".");
1329 OS << *Node->getDecl();
1332 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
1333 if (Node->isSuperReceiver())
1335 else if (Node->isObjectReceiver() && Node->getBase()) {
1336 PrintExpr(Node->getBase());
1338 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1339 OS << Node->getClassReceiver()->getName() << ".";
1342 if (Node->isImplicitProperty())
1343 Node->getImplicitPropertyGetter()->getSelector().print(OS);
1345 OS << Node->getExplicitProperty()->getName();
1348 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1350 PrintExpr(Node->getBaseExpr());
1352 PrintExpr(Node->getKeyExpr());
1356 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
1357 OS << PredefinedExpr::getIdentTypeName(Node->getIdentType());
1360 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
1361 unsigned value = Node->getValue();
1363 switch (Node->getKind()) {
1364 case CharacterLiteral::Ascii: break; // no prefix.
1365 case CharacterLiteral::Wide: OS << 'L'; break;
1366 case CharacterLiteral::UTF8: OS << "u8"; break;
1367 case CharacterLiteral::UTF16: OS << 'u'; break;
1368 case CharacterLiteral::UTF32: OS << 'U'; break;
1379 // TODO: K&R: the meaning of '\\a' is different in traditional C
1385 // Nonstandard escape sequence.
1405 // A character literal might be sign-extended, which
1406 // would result in an invalid \U escape sequence.
1407 // FIXME: multicharacter literals such as '\xFF\xFF\xFF\xFF'
1408 // are not correctly handled.
1409 if ((value & ~0xFFu) == ~0xFFu && Node->getKind() == CharacterLiteral::Ascii)
1411 if (value < 256 && isPrintable((unsigned char)value))
1412 OS << "'" << (char)value << "'";
1413 else if (value < 256)
1414 OS << "'\\x" << llvm::format("%02x", value) << "'";
1415 else if (value <= 0xFFFF)
1416 OS << "'\\u" << llvm::format("%04x", value) << "'";
1418 OS << "'\\U" << llvm::format("%08x", value) << "'";
1422 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
1423 bool isSigned = Node->getType()->isSignedIntegerType();
1424 OS << Node->getValue().toString(10, isSigned);
1426 // Emit suffixes. Integer literals are always a builtin integer type.
1427 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1428 default: llvm_unreachable("Unexpected type for integer literal!");
1429 case BuiltinType::Char_S:
1430 case BuiltinType::Char_U: OS << "i8"; break;
1431 case BuiltinType::UChar: OS << "Ui8"; break;
1432 case BuiltinType::Short: OS << "i16"; break;
1433 case BuiltinType::UShort: OS << "Ui16"; break;
1434 case BuiltinType::Int: break; // no suffix.
1435 case BuiltinType::UInt: OS << 'U'; break;
1436 case BuiltinType::Long: OS << 'L'; break;
1437 case BuiltinType::ULong: OS << "UL"; break;
1438 case BuiltinType::LongLong: OS << "LL"; break;
1439 case BuiltinType::ULongLong: OS << "ULL"; break;
1443 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1445 SmallString<16> Str;
1446 Node->getValue().toString(Str);
1448 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1449 OS << '.'; // Trailing dot in order to separate from ints.
1454 // Emit suffixes. Float literals are always a builtin float type.
1455 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1456 default: llvm_unreachable("Unexpected type for float literal!");
1457 case BuiltinType::Half: break; // FIXME: suffix?
1458 case BuiltinType::Double: break; // no suffix.
1459 case BuiltinType::Float: OS << 'F'; break;
1460 case BuiltinType::LongDouble: OS << 'L'; break;
1461 case BuiltinType::Float128: OS << 'Q'; break;
1465 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1466 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
1469 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1470 PrintExpr(Node->getSubExpr());
1474 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
1475 Str->outputString(OS);
1477 void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1479 PrintExpr(Node->getSubExpr());
1482 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
1483 if (!Node->isPostfix()) {
1484 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1486 // Print a space if this is an "identifier operator" like __real, or if
1487 // it might be concatenated incorrectly like '+'.
1488 switch (Node->getOpcode()) {
1497 if (isa<UnaryOperator>(Node->getSubExpr()))
1502 PrintExpr(Node->getSubExpr());
1504 if (Node->isPostfix())
1505 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1508 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1509 OS << "__builtin_offsetof(";
1510 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1512 bool PrintedSomething = false;
1513 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1514 OffsetOfNode ON = Node->getComponent(i);
1515 if (ON.getKind() == OffsetOfNode::Array) {
1518 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1520 PrintedSomething = true;
1524 // Skip implicit base indirections.
1525 if (ON.getKind() == OffsetOfNode::Base)
1528 // Field or identifier node.
1529 IdentifierInfo *Id = ON.getFieldName();
1533 if (PrintedSomething)
1536 PrintedSomething = true;
1537 OS << Id->getName();
1542 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
1543 switch(Node->getKind()) {
1550 else if (Policy.UnderscoreAlignof)
1558 case UETT_OpenMPRequiredSimdAlign:
1559 OS << "__builtin_omp_required_simd_align";
1562 if (Node->isArgumentType()) {
1564 Node->getArgumentType().print(OS, Policy);
1568 PrintExpr(Node->getArgumentExpr());
1572 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1574 PrintExpr(Node->getControllingExpr());
1575 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
1577 QualType T = Node->getAssocType(i);
1581 T.print(OS, Policy);
1583 PrintExpr(Node->getAssocExpr(i));
1588 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
1589 PrintExpr(Node->getLHS());
1591 PrintExpr(Node->getRHS());
1595 void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) {
1596 PrintExpr(Node->getBase());
1598 if (Node->getLowerBound())
1599 PrintExpr(Node->getLowerBound());
1600 if (Node->getColonLoc().isValid()) {
1602 if (Node->getLength())
1603 PrintExpr(Node->getLength());
1608 void StmtPrinter::PrintCallArgs(CallExpr *Call) {
1609 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
1610 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1611 // Don't print any defaulted arguments
1616 PrintExpr(Call->getArg(i));
1620 void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1621 PrintExpr(Call->getCallee());
1623 PrintCallArgs(Call);
1626 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1627 // FIXME: Suppress printing implicit bases (like "this")
1628 PrintExpr(Node->getBase());
1630 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1631 FieldDecl *ParentDecl = ParentMember
1632 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr;
1634 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1635 OS << (Node->isArrow() ? "->" : ".");
1637 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1638 if (FD->isAnonymousStructOrUnion())
1641 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1642 Qualifier->print(OS, Policy);
1643 if (Node->hasTemplateKeyword())
1645 OS << Node->getMemberNameInfo();
1646 if (Node->hasExplicitTemplateArgs())
1647 TemplateSpecializationType::PrintTemplateArgumentList(
1648 OS, Node->template_arguments(), Policy);
1650 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1651 PrintExpr(Node->getBase());
1652 OS << (Node->isArrow() ? "->isa" : ".isa");
1655 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1656 PrintExpr(Node->getBase());
1658 OS << Node->getAccessor().getName();
1660 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1662 Node->getTypeAsWritten().print(OS, Policy);
1664 PrintExpr(Node->getSubExpr());
1666 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1668 Node->getType().print(OS, Policy);
1670 PrintExpr(Node->getInitializer());
1672 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1673 // No need to print anything, simply forward to the subexpression.
1674 PrintExpr(Node->getSubExpr());
1676 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1677 PrintExpr(Node->getLHS());
1678 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1679 PrintExpr(Node->getRHS());
1681 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1682 PrintExpr(Node->getLHS());
1683 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1684 PrintExpr(Node->getRHS());
1686 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1687 PrintExpr(Node->getCond());
1689 PrintExpr(Node->getLHS());
1691 PrintExpr(Node->getRHS());
1697 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1698 PrintExpr(Node->getCommon());
1700 PrintExpr(Node->getFalseExpr());
1702 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1703 OS << "&&" << Node->getLabel()->getName();
1706 void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1708 PrintRawCompoundStmt(E->getSubStmt());
1712 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1713 OS << "__builtin_choose_expr(";
1714 PrintExpr(Node->getCond());
1716 PrintExpr(Node->getLHS());
1718 PrintExpr(Node->getRHS());
1722 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1726 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1727 OS << "__builtin_shufflevector(";
1728 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1730 PrintExpr(Node->getExpr(i));
1735 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1736 OS << "__builtin_convertvector(";
1737 PrintExpr(Node->getSrcExpr());
1739 Node->getType().print(OS, Policy);
1743 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1744 if (Node->getSyntacticForm()) {
1745 Visit(Node->getSyntacticForm());
1750 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1752 if (Node->getInit(i))
1753 PrintExpr(Node->getInit(i));
1760 void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) {
1761 // There's no way to express this expression in any of our supported
1762 // languages, so just emit something terse and (hopefully) clear.
1764 PrintExpr(Node->getSubExpr());
1768 void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) {
1772 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1774 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1776 PrintExpr(Node->getExpr(i));
1781 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1782 bool NeedsEquals = true;
1783 for (const DesignatedInitExpr::Designator &D : Node->designators()) {
1784 if (D.isFieldDesignator()) {
1785 if (D.getDotLoc().isInvalid()) {
1786 if (IdentifierInfo *II = D.getFieldName()) {
1787 OS << II->getName() << ":";
1788 NeedsEquals = false;
1791 OS << "." << D.getFieldName()->getName();
1795 if (D.isArrayDesignator()) {
1796 PrintExpr(Node->getArrayIndex(D));
1798 PrintExpr(Node->getArrayRangeStart(D));
1800 PrintExpr(Node->getArrayRangeEnd(D));
1810 PrintExpr(Node->getInit());
1813 void StmtPrinter::VisitDesignatedInitUpdateExpr(
1814 DesignatedInitUpdateExpr *Node) {
1817 PrintExpr(Node->getBase());
1820 OS << "/*updater*/";
1821 PrintExpr(Node->getUpdater());
1825 void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1826 OS << "/*no init*/";
1829 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1830 if (Node->getType()->getAsCXXRecordDecl()) {
1831 OS << "/*implicit*/";
1832 Node->getType().print(OS, Policy);
1835 OS << "/*implicit*/(";
1836 Node->getType().print(OS, Policy);
1838 if (Node->getType()->isRecordType())
1845 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1846 OS << "__builtin_va_arg(";
1847 PrintExpr(Node->getSubExpr());
1849 Node->getType().print(OS, Policy);
1853 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1854 PrintExpr(Node->getSyntacticForm());
1857 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1858 const char *Name = nullptr;
1859 switch (Node->getOp()) {
1860 #define BUILTIN(ID, TYPE, ATTRS)
1861 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1862 case AtomicExpr::AO ## ID: \
1865 #include "clang/Basic/Builtins.def"
1869 // AtomicExpr stores its subexpressions in a permuted order.
1870 PrintExpr(Node->getPtr());
1871 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1872 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
1874 PrintExpr(Node->getVal1());
1876 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1877 Node->isCmpXChg()) {
1879 PrintExpr(Node->getVal2());
1881 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1882 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1884 PrintExpr(Node->getWeak());
1886 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1888 PrintExpr(Node->getOrder());
1890 if (Node->isCmpXChg()) {
1892 PrintExpr(Node->getOrderFail());
1898 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1899 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1901 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1903 #include "clang/Basic/OperatorKinds.def"
1906 OverloadedOperatorKind Kind = Node->getOperator();
1907 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1908 if (Node->getNumArgs() == 1) {
1909 OS << OpStrings[Kind] << ' ';
1910 PrintExpr(Node->getArg(0));
1912 PrintExpr(Node->getArg(0));
1913 OS << ' ' << OpStrings[Kind];
1915 } else if (Kind == OO_Arrow) {
1916 PrintExpr(Node->getArg(0));
1917 } else if (Kind == OO_Call) {
1918 PrintExpr(Node->getArg(0));
1920 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1923 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1924 PrintExpr(Node->getArg(ArgIdx));
1927 } else if (Kind == OO_Subscript) {
1928 PrintExpr(Node->getArg(0));
1930 PrintExpr(Node->getArg(1));
1932 } else if (Node->getNumArgs() == 1) {
1933 OS << OpStrings[Kind] << ' ';
1934 PrintExpr(Node->getArg(0));
1935 } else if (Node->getNumArgs() == 2) {
1936 PrintExpr(Node->getArg(0));
1937 OS << ' ' << OpStrings[Kind] << ' ';
1938 PrintExpr(Node->getArg(1));
1940 llvm_unreachable("unknown overloaded operator");
1944 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1945 // If we have a conversion operator call only print the argument.
1946 CXXMethodDecl *MD = Node->getMethodDecl();
1947 if (MD && isa<CXXConversionDecl>(MD)) {
1948 PrintExpr(Node->getImplicitObjectArgument());
1951 VisitCallExpr(cast<CallExpr>(Node));
1954 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1955 PrintExpr(Node->getCallee());
1957 PrintCallArgs(Node->getConfig());
1959 PrintCallArgs(Node);
1963 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1964 OS << Node->getCastName() << '<';
1965 Node->getTypeAsWritten().print(OS, Policy);
1967 PrintExpr(Node->getSubExpr());
1971 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1972 VisitCXXNamedCastExpr(Node);
1975 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1976 VisitCXXNamedCastExpr(Node);
1979 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1980 VisitCXXNamedCastExpr(Node);
1983 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1984 VisitCXXNamedCastExpr(Node);
1987 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1989 if (Node->isTypeOperand()) {
1990 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1992 PrintExpr(Node->getExprOperand());
1997 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1999 if (Node->isTypeOperand()) {
2000 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
2002 PrintExpr(Node->getExprOperand());
2007 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
2008 PrintExpr(Node->getBaseExpr());
2009 if (Node->isArrow())
2013 if (NestedNameSpecifier *Qualifier =
2014 Node->getQualifierLoc().getNestedNameSpecifier())
2015 Qualifier->print(OS, Policy);
2016 OS << Node->getPropertyDecl()->getDeclName();
2019 void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
2020 PrintExpr(Node->getBase());
2022 PrintExpr(Node->getIdx());
2026 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
2027 switch (Node->getLiteralOperatorKind()) {
2028 case UserDefinedLiteral::LOK_Raw:
2029 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
2031 case UserDefinedLiteral::LOK_Template: {
2032 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
2033 const TemplateArgumentList *Args =
2034 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
2037 if (Args->size() != 1) {
2038 OS << "operator\"\"" << Node->getUDSuffix()->getName();
2039 TemplateSpecializationType::PrintTemplateArgumentList(
2040 OS, Args->asArray(), Policy);
2045 const TemplateArgument &Pack = Args->get(0);
2046 for (const auto &P : Pack.pack_elements()) {
2047 char C = (char)P.getAsIntegral().getZExtValue();
2052 case UserDefinedLiteral::LOK_Integer: {
2053 // Print integer literal without suffix.
2054 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
2055 OS << Int->getValue().toString(10, /*isSigned*/false);
2058 case UserDefinedLiteral::LOK_Floating: {
2059 // Print floating literal without suffix.
2060 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
2061 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
2064 case UserDefinedLiteral::LOK_String:
2065 case UserDefinedLiteral::LOK_Character:
2066 PrintExpr(Node->getCookedLiteral());
2069 OS << Node->getUDSuffix()->getName();
2072 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
2073 OS << (Node->getValue() ? "true" : "false");
2076 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
2080 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
2084 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
2085 if (!Node->getSubExpr())
2089 PrintExpr(Node->getSubExpr());
2093 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
2094 // Nothing to print: we picked up the default argument.
2097 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
2098 // Nothing to print: we picked up the default initializer.
2101 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
2102 Node->getType().print(OS, Policy);
2103 // If there are no parens, this is list-initialization, and the braces are
2104 // part of the syntax of the inner construct.
2105 if (Node->getLParenLoc().isValid())
2107 PrintExpr(Node->getSubExpr());
2108 if (Node->getLParenLoc().isValid())
2112 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
2113 PrintExpr(Node->getSubExpr());
2116 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
2117 Node->getType().print(OS, Policy);
2118 if (Node->isStdInitListInitialization())
2119 /* Nothing to do; braces are part of creating the std::initializer_list. */;
2120 else if (Node->isListInitialization())
2124 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
2125 ArgEnd = Node->arg_end();
2126 Arg != ArgEnd; ++Arg) {
2127 if ((*Arg)->isDefaultArgument())
2129 if (Arg != Node->arg_begin())
2133 if (Node->isStdInitListInitialization())
2135 else if (Node->isListInitialization())
2141 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
2143 bool NeedComma = false;
2144 switch (Node->getCaptureDefault()) {
2158 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
2159 CEnd = Node->explicit_capture_end();
2166 switch (C->getCaptureKind()) {
2174 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
2176 OS << C->getCapturedVar()->getName();
2180 OS << C->getCapturedVar()->getName();
2183 llvm_unreachable("VLA type in explicit captures.");
2186 if (Node->isInitCapture(C))
2187 PrintExpr(C->getCapturedVar()->getInit());
2191 if (Node->hasExplicitParameters()) {
2193 CXXMethodDecl *Method = Node->getCallOperator();
2195 for (auto P : Method->parameters()) {
2201 std::string ParamStr = P->getNameAsString();
2202 P->getOriginalType().print(OS, Policy, ParamStr);
2204 if (Method->isVariadic()) {
2211 if (Node->isMutable())
2214 const FunctionProtoType *Proto
2215 = Method->getType()->getAs<FunctionProtoType>();
2216 Proto->printExceptionSpecification(OS, Policy);
2218 // FIXME: Attributes
2220 // Print the trailing return type if it was specified in the source.
2221 if (Node->hasExplicitResultType()) {
2223 Proto->getReturnType().print(OS, Policy);
2228 CompoundStmt *Body = Node->getBody();
2233 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
2234 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
2235 TSInfo->getType().print(OS, Policy);
2237 Node->getType().print(OS, Policy);
2241 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
2242 if (E->isGlobalNew())
2245 unsigned NumPlace = E->getNumPlacementArgs();
2246 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
2248 PrintExpr(E->getPlacementArg(0));
2249 for (unsigned i = 1; i < NumPlace; ++i) {
2250 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
2253 PrintExpr(E->getPlacementArg(i));
2257 if (E->isParenTypeId())
2260 if (Expr *Size = E->getArraySize()) {
2261 llvm::raw_string_ostream s(TypeS);
2263 Size->printPretty(s, Helper, Policy);
2266 E->getAllocatedType().print(OS, Policy, TypeS);
2267 if (E->isParenTypeId())
2270 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
2272 if (InitStyle == CXXNewExpr::CallInit)
2274 PrintExpr(E->getInitializer());
2275 if (InitStyle == CXXNewExpr::CallInit)
2280 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2281 if (E->isGlobalDelete())
2284 if (E->isArrayForm())
2286 PrintExpr(E->getArgument());
2289 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
2290 PrintExpr(E->getBase());
2295 if (E->getQualifier())
2296 E->getQualifier()->print(OS, Policy);
2299 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
2300 OS << II->getName();
2302 E->getDestroyedType().print(OS, Policy);
2305 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
2306 if (E->isListInitialization() && !E->isStdInitListInitialization())
2309 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
2310 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
2311 // Don't print any defaulted arguments
2316 PrintExpr(E->getArg(i));
2319 if (E->isListInitialization() && !E->isStdInitListInitialization())
2323 void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) {
2324 // Parens are printed by the surrounding context.
2325 OS << "<forwarded>";
2328 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
2329 PrintExpr(E->getSubExpr());
2332 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
2333 // Just forward to the subexpression.
2334 PrintExpr(E->getSubExpr());
2338 StmtPrinter::VisitCXXUnresolvedConstructExpr(
2339 CXXUnresolvedConstructExpr *Node) {
2340 Node->getTypeAsWritten().print(OS, Policy);
2342 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
2343 ArgEnd = Node->arg_end();
2344 Arg != ArgEnd; ++Arg) {
2345 if (Arg != Node->arg_begin())
2352 void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2353 CXXDependentScopeMemberExpr *Node) {
2354 if (!Node->isImplicitAccess()) {
2355 PrintExpr(Node->getBase());
2356 OS << (Node->isArrow() ? "->" : ".");
2358 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2359 Qualifier->print(OS, Policy);
2360 if (Node->hasTemplateKeyword())
2362 OS << Node->getMemberNameInfo();
2363 if (Node->hasExplicitTemplateArgs())
2364 TemplateSpecializationType::PrintTemplateArgumentList(
2365 OS, Node->template_arguments(), Policy);
2368 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
2369 if (!Node->isImplicitAccess()) {
2370 PrintExpr(Node->getBase());
2371 OS << (Node->isArrow() ? "->" : ".");
2373 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2374 Qualifier->print(OS, Policy);
2375 if (Node->hasTemplateKeyword())
2377 OS << Node->getMemberNameInfo();
2378 if (Node->hasExplicitTemplateArgs())
2379 TemplateSpecializationType::PrintTemplateArgumentList(
2380 OS, Node->template_arguments(), Policy);
2383 static const char *getTypeTraitName(TypeTrait TT) {
2385 #define TYPE_TRAIT_1(Spelling, Name, Key) \
2386 case clang::UTT_##Name: return #Spelling;
2387 #define TYPE_TRAIT_2(Spelling, Name, Key) \
2388 case clang::BTT_##Name: return #Spelling;
2389 #define TYPE_TRAIT_N(Spelling, Name, Key) \
2390 case clang::TT_##Name: return #Spelling;
2391 #include "clang/Basic/TokenKinds.def"
2393 llvm_unreachable("Type trait not covered by switch");
2396 static const char *getTypeTraitName(ArrayTypeTrait ATT) {
2398 case ATT_ArrayRank: return "__array_rank";
2399 case ATT_ArrayExtent: return "__array_extent";
2401 llvm_unreachable("Array type trait not covered by switch");
2404 static const char *getExpressionTraitName(ExpressionTrait ET) {
2406 case ET_IsLValueExpr: return "__is_lvalue_expr";
2407 case ET_IsRValueExpr: return "__is_rvalue_expr";
2409 llvm_unreachable("Expression type trait not covered by switch");
2412 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2413 OS << getTypeTraitName(E->getTrait()) << "(";
2414 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2417 E->getArg(I)->getType().print(OS, Policy);
2422 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
2423 OS << getTypeTraitName(E->getTrait()) << '(';
2424 E->getQueriedType().print(OS, Policy);
2428 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
2429 OS << getExpressionTraitName(E->getTrait()) << '(';
2430 PrintExpr(E->getQueriedExpression());
2434 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2436 PrintExpr(E->getOperand());
2440 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
2441 PrintExpr(E->getPattern());
2445 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2446 OS << "sizeof...(" << *E->getPack() << ")";
2449 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2450 SubstNonTypeTemplateParmPackExpr *Node) {
2451 OS << *Node->getParameterPack();
2454 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2455 SubstNonTypeTemplateParmExpr *Node) {
2456 Visit(Node->getReplacement());
2459 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2460 OS << *E->getParameterPack();
2463 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2464 PrintExpr(Node->GetTemporaryExpr());
2467 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2470 PrintExpr(E->getLHS());
2471 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2475 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2476 PrintExpr(E->getRHS());
2481 // C++ Coroutines TS
2483 void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
2484 Visit(S->getBody());
2487 void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
2489 if (S->getOperand()) {
2491 Visit(S->getOperand());
2496 void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
2498 PrintExpr(S->getOperand());
2502 void StmtPrinter::VisitDependentCoawaitExpr(DependentCoawaitExpr *S) {
2504 PrintExpr(S->getOperand());
2508 void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
2510 PrintExpr(S->getOperand());
2515 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2517 VisitStringLiteral(Node->getString());
2520 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
2522 Visit(E->getSubExpr());
2525 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2527 ObjCArrayLiteral::child_range Ch = E->children();
2528 for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2529 if (I != Ch.begin())
2536 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2538 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2542 ObjCDictionaryElement Element = E->getKeyValueElement(I);
2545 Visit(Element.Value);
2546 if (Element.isPackExpansion())
2552 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
2554 Node->getEncodedType().print(OS, Policy);
2558 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
2560 Node->getSelector().print(OS);
2564 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
2565 OS << "@protocol(" << *Node->getProtocol() << ')';
2568 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2570 switch (Mess->getReceiverKind()) {
2571 case ObjCMessageExpr::Instance:
2572 PrintExpr(Mess->getInstanceReceiver());
2575 case ObjCMessageExpr::Class:
2576 Mess->getClassReceiver().print(OS, Policy);
2579 case ObjCMessageExpr::SuperInstance:
2580 case ObjCMessageExpr::SuperClass:
2586 Selector selector = Mess->getSelector();
2587 if (selector.isUnarySelector()) {
2588 OS << selector.getNameForSlot(0);
2590 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
2591 if (i < selector.getNumArgs()) {
2592 if (i > 0) OS << ' ';
2593 if (selector.getIdentifierInfoForSlot(i))
2594 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
2598 else OS << ", "; // Handle variadic methods.
2600 PrintExpr(Mess->getArg(i));
2606 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2607 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2611 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2612 PrintExpr(E->getSubExpr());
2616 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
2617 OS << '(' << E->getBridgeKindName();
2618 E->getType().print(OS, Policy);
2620 PrintExpr(E->getSubExpr());
2623 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
2624 BlockDecl *BD = Node->getBlockDecl();
2627 const FunctionType *AFT = Node->getFunctionType();
2629 if (isa<FunctionNoProtoType>(AFT)) {
2631 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
2633 for (BlockDecl::param_iterator AI = BD->param_begin(),
2634 E = BD->param_end(); AI != E; ++AI) {
2635 if (AI != BD->param_begin()) OS << ", ";
2636 std::string ParamStr = (*AI)->getNameAsString();
2637 (*AI)->getType().print(OS, Policy, ParamStr);
2640 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
2641 if (FT->isVariadic()) {
2642 if (!BD->param_empty()) OS << ", ";
2650 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2651 PrintExpr(Node->getSourceExpr());
2654 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2655 // TODO: Print something reasonable for a TypoExpr, if necessary.
2656 llvm_unreachable("Cannot print TypoExpr nodes");
2659 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2660 OS << "__builtin_astype(";
2661 PrintExpr(Node->getSrcExpr());
2663 Node->getType().print(OS, Policy);
2667 //===----------------------------------------------------------------------===//
2668 // Stmt method implementations
2669 //===----------------------------------------------------------------------===//
2671 void Stmt::dumpPretty(const ASTContext &Context) const {
2672 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
2675 void Stmt::printPretty(raw_ostream &OS,
2676 PrinterHelper *Helper,
2677 const PrintingPolicy &Policy,
2678 unsigned Indentation) const {
2679 StmtPrinter P(OS, Helper, Policy, Indentation);
2680 P.Visit(const_cast<Stmt*>(this));
2683 //===----------------------------------------------------------------------===//
2685 //===----------------------------------------------------------------------===//
2687 // Implement virtual destructor.
2688 PrinterHelper::~PrinterHelper() {}