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::VisitOMPLinearClause(OMPLinearClause *Node) {
840 if (!Node->varlist_empty()) {
842 if (Node->getModifierLoc().isValid()) {
844 << getOpenMPSimpleClauseTypeName(OMPC_linear, Node->getModifier());
846 VisitOMPClauseList(Node, '(');
847 if (Node->getModifierLoc().isValid())
849 if (Node->getStep() != nullptr) {
851 Node->getStep()->printPretty(OS, nullptr, Policy, 0);
857 void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) {
858 if (!Node->varlist_empty()) {
860 VisitOMPClauseList(Node, '(');
861 if (Node->getAlignment() != nullptr) {
863 Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
869 void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
870 if (!Node->varlist_empty()) {
872 VisitOMPClauseList(Node, '(');
877 void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) {
878 if (!Node->varlist_empty()) {
880 VisitOMPClauseList(Node, '(');
885 void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) {
886 if (!Node->varlist_empty()) {
887 VisitOMPClauseList(Node, '(');
892 void OMPClausePrinter::VisitOMPDependClause(OMPDependClause *Node) {
894 OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
895 Node->getDependencyKind());
896 if (!Node->varlist_empty()) {
898 VisitOMPClauseList(Node, ' ');
903 void OMPClausePrinter::VisitOMPMapClause(OMPMapClause *Node) {
904 if (!Node->varlist_empty()) {
906 if (Node->getMapType() != OMPC_MAP_unknown) {
907 if (Node->getMapTypeModifier() != OMPC_MAP_unknown) {
908 OS << getOpenMPSimpleClauseTypeName(OMPC_map,
909 Node->getMapTypeModifier());
912 OS << getOpenMPSimpleClauseTypeName(OMPC_map, Node->getMapType());
915 VisitOMPClauseList(Node, ' ');
920 void OMPClausePrinter::VisitOMPToClause(OMPToClause *Node) {
921 if (!Node->varlist_empty()) {
923 VisitOMPClauseList(Node, '(');
928 void OMPClausePrinter::VisitOMPFromClause(OMPFromClause *Node) {
929 if (!Node->varlist_empty()) {
931 VisitOMPClauseList(Node, '(');
936 void OMPClausePrinter::VisitOMPDistScheduleClause(OMPDistScheduleClause *Node) {
937 OS << "dist_schedule(" << getOpenMPSimpleClauseTypeName(
938 OMPC_dist_schedule, Node->getDistScheduleKind());
939 if (auto *E = Node->getChunkSize()) {
941 E->printPretty(OS, nullptr, Policy);
946 void OMPClausePrinter::VisitOMPDefaultmapClause(OMPDefaultmapClause *Node) {
948 OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
949 Node->getDefaultmapModifier());
951 OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
952 Node->getDefaultmapKind());
956 void OMPClausePrinter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *Node) {
957 if (!Node->varlist_empty()) {
958 OS << "use_device_ptr";
959 VisitOMPClauseList(Node, '(');
964 void OMPClausePrinter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *Node) {
965 if (!Node->varlist_empty()) {
966 OS << "is_device_ptr";
967 VisitOMPClauseList(Node, '(');
973 //===----------------------------------------------------------------------===//
974 // OpenMP directives printing methods
975 //===----------------------------------------------------------------------===//
977 void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) {
978 OMPClausePrinter Printer(OS, Policy);
979 ArrayRef<OMPClause *> Clauses = S->clauses();
980 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
982 if (*I && !(*I)->isImplicit()) {
987 if (S->hasAssociatedStmt() && S->getAssociatedStmt()) {
988 assert(isa<CapturedStmt>(S->getAssociatedStmt()) &&
989 "Expected captured statement!");
990 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt();
995 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
996 Indent() << "#pragma omp parallel ";
997 PrintOMPExecutableDirective(Node);
1000 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
1001 Indent() << "#pragma omp simd ";
1002 PrintOMPExecutableDirective(Node);
1005 void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
1006 Indent() << "#pragma omp for ";
1007 PrintOMPExecutableDirective(Node);
1010 void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
1011 Indent() << "#pragma omp for simd ";
1012 PrintOMPExecutableDirective(Node);
1015 void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
1016 Indent() << "#pragma omp sections ";
1017 PrintOMPExecutableDirective(Node);
1020 void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
1021 Indent() << "#pragma omp section";
1022 PrintOMPExecutableDirective(Node);
1025 void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
1026 Indent() << "#pragma omp single ";
1027 PrintOMPExecutableDirective(Node);
1030 void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
1031 Indent() << "#pragma omp master";
1032 PrintOMPExecutableDirective(Node);
1035 void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
1036 Indent() << "#pragma omp critical";
1037 if (Node->getDirectiveName().getName()) {
1039 Node->getDirectiveName().printName(OS);
1043 PrintOMPExecutableDirective(Node);
1046 void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
1047 Indent() << "#pragma omp parallel for ";
1048 PrintOMPExecutableDirective(Node);
1051 void StmtPrinter::VisitOMPParallelForSimdDirective(
1052 OMPParallelForSimdDirective *Node) {
1053 Indent() << "#pragma omp parallel for simd ";
1054 PrintOMPExecutableDirective(Node);
1057 void StmtPrinter::VisitOMPParallelSectionsDirective(
1058 OMPParallelSectionsDirective *Node) {
1059 Indent() << "#pragma omp parallel sections ";
1060 PrintOMPExecutableDirective(Node);
1063 void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
1064 Indent() << "#pragma omp task ";
1065 PrintOMPExecutableDirective(Node);
1068 void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
1069 Indent() << "#pragma omp taskyield";
1070 PrintOMPExecutableDirective(Node);
1073 void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
1074 Indent() << "#pragma omp barrier";
1075 PrintOMPExecutableDirective(Node);
1078 void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
1079 Indent() << "#pragma omp taskwait";
1080 PrintOMPExecutableDirective(Node);
1083 void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
1084 Indent() << "#pragma omp taskgroup";
1085 PrintOMPExecutableDirective(Node);
1088 void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
1089 Indent() << "#pragma omp flush ";
1090 PrintOMPExecutableDirective(Node);
1093 void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
1094 Indent() << "#pragma omp ordered ";
1095 PrintOMPExecutableDirective(Node);
1098 void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
1099 Indent() << "#pragma omp atomic ";
1100 PrintOMPExecutableDirective(Node);
1103 void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
1104 Indent() << "#pragma omp target ";
1105 PrintOMPExecutableDirective(Node);
1108 void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
1109 Indent() << "#pragma omp target data ";
1110 PrintOMPExecutableDirective(Node);
1113 void StmtPrinter::VisitOMPTargetEnterDataDirective(
1114 OMPTargetEnterDataDirective *Node) {
1115 Indent() << "#pragma omp target enter data ";
1116 PrintOMPExecutableDirective(Node);
1119 void StmtPrinter::VisitOMPTargetExitDataDirective(
1120 OMPTargetExitDataDirective *Node) {
1121 Indent() << "#pragma omp target exit data ";
1122 PrintOMPExecutableDirective(Node);
1125 void StmtPrinter::VisitOMPTargetParallelDirective(
1126 OMPTargetParallelDirective *Node) {
1127 Indent() << "#pragma omp target parallel ";
1128 PrintOMPExecutableDirective(Node);
1131 void StmtPrinter::VisitOMPTargetParallelForDirective(
1132 OMPTargetParallelForDirective *Node) {
1133 Indent() << "#pragma omp target parallel for ";
1134 PrintOMPExecutableDirective(Node);
1137 void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
1138 Indent() << "#pragma omp teams ";
1139 PrintOMPExecutableDirective(Node);
1142 void StmtPrinter::VisitOMPCancellationPointDirective(
1143 OMPCancellationPointDirective *Node) {
1144 Indent() << "#pragma omp cancellation point "
1145 << getOpenMPDirectiveName(Node->getCancelRegion());
1146 PrintOMPExecutableDirective(Node);
1149 void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
1150 Indent() << "#pragma omp cancel "
1151 << getOpenMPDirectiveName(Node->getCancelRegion()) << " ";
1152 PrintOMPExecutableDirective(Node);
1155 void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) {
1156 Indent() << "#pragma omp taskloop ";
1157 PrintOMPExecutableDirective(Node);
1160 void StmtPrinter::VisitOMPTaskLoopSimdDirective(
1161 OMPTaskLoopSimdDirective *Node) {
1162 Indent() << "#pragma omp taskloop simd ";
1163 PrintOMPExecutableDirective(Node);
1166 void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) {
1167 Indent() << "#pragma omp distribute ";
1168 PrintOMPExecutableDirective(Node);
1171 void StmtPrinter::VisitOMPTargetUpdateDirective(
1172 OMPTargetUpdateDirective *Node) {
1173 Indent() << "#pragma omp target update ";
1174 PrintOMPExecutableDirective(Node);
1177 void StmtPrinter::VisitOMPDistributeParallelForDirective(
1178 OMPDistributeParallelForDirective *Node) {
1179 Indent() << "#pragma omp distribute parallel for ";
1180 PrintOMPExecutableDirective(Node);
1183 void StmtPrinter::VisitOMPDistributeParallelForSimdDirective(
1184 OMPDistributeParallelForSimdDirective *Node) {
1185 Indent() << "#pragma omp distribute parallel for simd ";
1186 PrintOMPExecutableDirective(Node);
1189 void StmtPrinter::VisitOMPDistributeSimdDirective(
1190 OMPDistributeSimdDirective *Node) {
1191 Indent() << "#pragma omp distribute simd ";
1192 PrintOMPExecutableDirective(Node);
1195 void StmtPrinter::VisitOMPTargetParallelForSimdDirective(
1196 OMPTargetParallelForSimdDirective *Node) {
1197 Indent() << "#pragma omp target parallel for simd ";
1198 PrintOMPExecutableDirective(Node);
1201 void StmtPrinter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective *Node) {
1202 Indent() << "#pragma omp target simd ";
1203 PrintOMPExecutableDirective(Node);
1206 void StmtPrinter::VisitOMPTeamsDistributeDirective(
1207 OMPTeamsDistributeDirective *Node) {
1208 Indent() << "#pragma omp teams distribute ";
1209 PrintOMPExecutableDirective(Node);
1212 void StmtPrinter::VisitOMPTeamsDistributeSimdDirective(
1213 OMPTeamsDistributeSimdDirective *Node) {
1214 Indent() << "#pragma omp teams distribute simd ";
1215 PrintOMPExecutableDirective(Node);
1218 void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective(
1219 OMPTeamsDistributeParallelForSimdDirective *Node) {
1220 Indent() << "#pragma omp teams distribute parallel for simd ";
1221 PrintOMPExecutableDirective(Node);
1224 void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective(
1225 OMPTeamsDistributeParallelForDirective *Node) {
1226 Indent() << "#pragma omp teams distribute parallel for ";
1227 PrintOMPExecutableDirective(Node);
1230 void StmtPrinter::VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective *Node) {
1231 Indent() << "#pragma omp target teams ";
1232 PrintOMPExecutableDirective(Node);
1235 void StmtPrinter::VisitOMPTargetTeamsDistributeDirective(
1236 OMPTargetTeamsDistributeDirective *Node) {
1237 Indent() << "#pragma omp target teams distribute ";
1238 PrintOMPExecutableDirective(Node);
1241 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective(
1242 OMPTargetTeamsDistributeParallelForDirective *Node) {
1243 Indent() << "#pragma omp target teams distribute parallel for ";
1244 PrintOMPExecutableDirective(Node);
1247 void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
1248 OMPTargetTeamsDistributeParallelForSimdDirective *Node) {
1249 Indent() << "#pragma omp target teams distribute parallel for simd ";
1250 PrintOMPExecutableDirective(Node);
1253 void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective(
1254 OMPTargetTeamsDistributeSimdDirective *Node) {
1255 Indent() << "#pragma omp target teams distribute simd ";
1256 PrintOMPExecutableDirective(Node);
1259 //===----------------------------------------------------------------------===//
1260 // Expr printing methods.
1261 //===----------------------------------------------------------------------===//
1263 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
1264 if (auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) {
1265 OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy);
1268 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1269 Qualifier->print(OS, Policy);
1270 if (Node->hasTemplateKeyword())
1272 OS << Node->getNameInfo();
1273 if (Node->hasExplicitTemplateArgs())
1274 TemplateSpecializationType::PrintTemplateArgumentList(
1275 OS, Node->template_arguments(), Policy);
1278 void StmtPrinter::VisitDependentScopeDeclRefExpr(
1279 DependentScopeDeclRefExpr *Node) {
1280 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1281 Qualifier->print(OS, Policy);
1282 if (Node->hasTemplateKeyword())
1284 OS << Node->getNameInfo();
1285 if (Node->hasExplicitTemplateArgs())
1286 TemplateSpecializationType::PrintTemplateArgumentList(
1287 OS, Node->template_arguments(), Policy);
1290 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
1291 if (Node->getQualifier())
1292 Node->getQualifier()->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::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
1302 if (Node->getBase()) {
1303 PrintExpr(Node->getBase());
1304 OS << (Node->isArrow() ? "->" : ".");
1306 OS << *Node->getDecl();
1309 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
1310 if (Node->isSuperReceiver())
1312 else if (Node->isObjectReceiver() && Node->getBase()) {
1313 PrintExpr(Node->getBase());
1315 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1316 OS << Node->getClassReceiver()->getName() << ".";
1319 if (Node->isImplicitProperty())
1320 Node->getImplicitPropertyGetter()->getSelector().print(OS);
1322 OS << Node->getExplicitProperty()->getName();
1325 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1327 PrintExpr(Node->getBaseExpr());
1329 PrintExpr(Node->getKeyExpr());
1333 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
1334 OS << PredefinedExpr::getIdentTypeName(Node->getIdentType());
1337 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
1338 unsigned value = Node->getValue();
1340 switch (Node->getKind()) {
1341 case CharacterLiteral::Ascii: break; // no prefix.
1342 case CharacterLiteral::Wide: OS << 'L'; break;
1343 case CharacterLiteral::UTF8: OS << "u8"; break;
1344 case CharacterLiteral::UTF16: OS << 'u'; break;
1345 case CharacterLiteral::UTF32: OS << 'U'; break;
1356 // TODO: K&R: the meaning of '\\a' is different in traditional C
1362 // Nonstandard escape sequence.
1382 // A character literal might be sign-extended, which
1383 // would result in an invalid \U escape sequence.
1384 // FIXME: multicharacter literals such as '\xFF\xFF\xFF\xFF'
1385 // are not correctly handled.
1386 if ((value & ~0xFFu) == ~0xFFu && Node->getKind() == CharacterLiteral::Ascii)
1388 if (value < 256 && isPrintable((unsigned char)value))
1389 OS << "'" << (char)value << "'";
1390 else if (value < 256)
1391 OS << "'\\x" << llvm::format("%02x", value) << "'";
1392 else if (value <= 0xFFFF)
1393 OS << "'\\u" << llvm::format("%04x", value) << "'";
1395 OS << "'\\U" << llvm::format("%08x", value) << "'";
1399 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
1400 bool isSigned = Node->getType()->isSignedIntegerType();
1401 OS << Node->getValue().toString(10, isSigned);
1403 // Emit suffixes. Integer literals are always a builtin integer type.
1404 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1405 default: llvm_unreachable("Unexpected type for integer literal!");
1406 case BuiltinType::Char_S:
1407 case BuiltinType::Char_U: OS << "i8"; break;
1408 case BuiltinType::UChar: OS << "Ui8"; break;
1409 case BuiltinType::Short: OS << "i16"; break;
1410 case BuiltinType::UShort: OS << "Ui16"; break;
1411 case BuiltinType::Int: break; // no suffix.
1412 case BuiltinType::UInt: OS << 'U'; break;
1413 case BuiltinType::Long: OS << 'L'; break;
1414 case BuiltinType::ULong: OS << "UL"; break;
1415 case BuiltinType::LongLong: OS << "LL"; break;
1416 case BuiltinType::ULongLong: OS << "ULL"; break;
1420 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1422 SmallString<16> Str;
1423 Node->getValue().toString(Str);
1425 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1426 OS << '.'; // Trailing dot in order to separate from ints.
1431 // Emit suffixes. Float literals are always a builtin float type.
1432 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1433 default: llvm_unreachable("Unexpected type for float literal!");
1434 case BuiltinType::Half: break; // FIXME: suffix?
1435 case BuiltinType::Double: break; // no suffix.
1436 case BuiltinType::Float: OS << 'F'; break;
1437 case BuiltinType::LongDouble: OS << 'L'; break;
1438 case BuiltinType::Float128: OS << 'Q'; break;
1442 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1443 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
1446 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1447 PrintExpr(Node->getSubExpr());
1451 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
1452 Str->outputString(OS);
1454 void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1456 PrintExpr(Node->getSubExpr());
1459 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
1460 if (!Node->isPostfix()) {
1461 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1463 // Print a space if this is an "identifier operator" like __real, or if
1464 // it might be concatenated incorrectly like '+'.
1465 switch (Node->getOpcode()) {
1474 if (isa<UnaryOperator>(Node->getSubExpr()))
1479 PrintExpr(Node->getSubExpr());
1481 if (Node->isPostfix())
1482 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1485 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1486 OS << "__builtin_offsetof(";
1487 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1489 bool PrintedSomething = false;
1490 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1491 OffsetOfNode ON = Node->getComponent(i);
1492 if (ON.getKind() == OffsetOfNode::Array) {
1495 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1497 PrintedSomething = true;
1501 // Skip implicit base indirections.
1502 if (ON.getKind() == OffsetOfNode::Base)
1505 // Field or identifier node.
1506 IdentifierInfo *Id = ON.getFieldName();
1510 if (PrintedSomething)
1513 PrintedSomething = true;
1514 OS << Id->getName();
1519 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
1520 switch(Node->getKind()) {
1527 else if (Policy.UnderscoreAlignof)
1535 case UETT_OpenMPRequiredSimdAlign:
1536 OS << "__builtin_omp_required_simd_align";
1539 if (Node->isArgumentType()) {
1541 Node->getArgumentType().print(OS, Policy);
1545 PrintExpr(Node->getArgumentExpr());
1549 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1551 PrintExpr(Node->getControllingExpr());
1552 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
1554 QualType T = Node->getAssocType(i);
1558 T.print(OS, Policy);
1560 PrintExpr(Node->getAssocExpr(i));
1565 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
1566 PrintExpr(Node->getLHS());
1568 PrintExpr(Node->getRHS());
1572 void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) {
1573 PrintExpr(Node->getBase());
1575 if (Node->getLowerBound())
1576 PrintExpr(Node->getLowerBound());
1577 if (Node->getColonLoc().isValid()) {
1579 if (Node->getLength())
1580 PrintExpr(Node->getLength());
1585 void StmtPrinter::PrintCallArgs(CallExpr *Call) {
1586 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
1587 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1588 // Don't print any defaulted arguments
1593 PrintExpr(Call->getArg(i));
1597 void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1598 PrintExpr(Call->getCallee());
1600 PrintCallArgs(Call);
1603 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1604 // FIXME: Suppress printing implicit bases (like "this")
1605 PrintExpr(Node->getBase());
1607 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1608 FieldDecl *ParentDecl = ParentMember
1609 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr;
1611 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1612 OS << (Node->isArrow() ? "->" : ".");
1614 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1615 if (FD->isAnonymousStructOrUnion())
1618 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1619 Qualifier->print(OS, Policy);
1620 if (Node->hasTemplateKeyword())
1622 OS << Node->getMemberNameInfo();
1623 if (Node->hasExplicitTemplateArgs())
1624 TemplateSpecializationType::PrintTemplateArgumentList(
1625 OS, Node->template_arguments(), Policy);
1627 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1628 PrintExpr(Node->getBase());
1629 OS << (Node->isArrow() ? "->isa" : ".isa");
1632 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1633 PrintExpr(Node->getBase());
1635 OS << Node->getAccessor().getName();
1637 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1639 Node->getTypeAsWritten().print(OS, Policy);
1641 PrintExpr(Node->getSubExpr());
1643 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1645 Node->getType().print(OS, Policy);
1647 PrintExpr(Node->getInitializer());
1649 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1650 // No need to print anything, simply forward to the subexpression.
1651 PrintExpr(Node->getSubExpr());
1653 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1654 PrintExpr(Node->getLHS());
1655 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1656 PrintExpr(Node->getRHS());
1658 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1659 PrintExpr(Node->getLHS());
1660 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1661 PrintExpr(Node->getRHS());
1663 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1664 PrintExpr(Node->getCond());
1666 PrintExpr(Node->getLHS());
1668 PrintExpr(Node->getRHS());
1674 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1675 PrintExpr(Node->getCommon());
1677 PrintExpr(Node->getFalseExpr());
1679 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1680 OS << "&&" << Node->getLabel()->getName();
1683 void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1685 PrintRawCompoundStmt(E->getSubStmt());
1689 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1690 OS << "__builtin_choose_expr(";
1691 PrintExpr(Node->getCond());
1693 PrintExpr(Node->getLHS());
1695 PrintExpr(Node->getRHS());
1699 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1703 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1704 OS << "__builtin_shufflevector(";
1705 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1707 PrintExpr(Node->getExpr(i));
1712 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1713 OS << "__builtin_convertvector(";
1714 PrintExpr(Node->getSrcExpr());
1716 Node->getType().print(OS, Policy);
1720 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1721 if (Node->getSyntacticForm()) {
1722 Visit(Node->getSyntacticForm());
1727 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1729 if (Node->getInit(i))
1730 PrintExpr(Node->getInit(i));
1737 void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) {
1738 // There's no way to express this expression in any of our supported
1739 // languages, so just emit something terse and (hopefully) clear.
1741 PrintExpr(Node->getSubExpr());
1745 void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) {
1749 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1751 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1753 PrintExpr(Node->getExpr(i));
1758 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1759 bool NeedsEquals = true;
1760 for (const DesignatedInitExpr::Designator &D : Node->designators()) {
1761 if (D.isFieldDesignator()) {
1762 if (D.getDotLoc().isInvalid()) {
1763 if (IdentifierInfo *II = D.getFieldName()) {
1764 OS << II->getName() << ":";
1765 NeedsEquals = false;
1768 OS << "." << D.getFieldName()->getName();
1772 if (D.isArrayDesignator()) {
1773 PrintExpr(Node->getArrayIndex(D));
1775 PrintExpr(Node->getArrayRangeStart(D));
1777 PrintExpr(Node->getArrayRangeEnd(D));
1787 PrintExpr(Node->getInit());
1790 void StmtPrinter::VisitDesignatedInitUpdateExpr(
1791 DesignatedInitUpdateExpr *Node) {
1794 PrintExpr(Node->getBase());
1797 OS << "/*updater*/";
1798 PrintExpr(Node->getUpdater());
1802 void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1803 OS << "/*no init*/";
1806 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1807 if (Node->getType()->getAsCXXRecordDecl()) {
1808 OS << "/*implicit*/";
1809 Node->getType().print(OS, Policy);
1812 OS << "/*implicit*/(";
1813 Node->getType().print(OS, Policy);
1815 if (Node->getType()->isRecordType())
1822 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1823 OS << "__builtin_va_arg(";
1824 PrintExpr(Node->getSubExpr());
1826 Node->getType().print(OS, Policy);
1830 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1831 PrintExpr(Node->getSyntacticForm());
1834 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1835 const char *Name = nullptr;
1836 switch (Node->getOp()) {
1837 #define BUILTIN(ID, TYPE, ATTRS)
1838 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1839 case AtomicExpr::AO ## ID: \
1842 #include "clang/Basic/Builtins.def"
1846 // AtomicExpr stores its subexpressions in a permuted order.
1847 PrintExpr(Node->getPtr());
1848 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1849 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
1851 PrintExpr(Node->getVal1());
1853 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1854 Node->isCmpXChg()) {
1856 PrintExpr(Node->getVal2());
1858 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1859 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1861 PrintExpr(Node->getWeak());
1863 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1865 PrintExpr(Node->getOrder());
1867 if (Node->isCmpXChg()) {
1869 PrintExpr(Node->getOrderFail());
1875 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1876 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1878 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1880 #include "clang/Basic/OperatorKinds.def"
1883 OverloadedOperatorKind Kind = Node->getOperator();
1884 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1885 if (Node->getNumArgs() == 1) {
1886 OS << OpStrings[Kind] << ' ';
1887 PrintExpr(Node->getArg(0));
1889 PrintExpr(Node->getArg(0));
1890 OS << ' ' << OpStrings[Kind];
1892 } else if (Kind == OO_Arrow) {
1893 PrintExpr(Node->getArg(0));
1894 } else if (Kind == OO_Call) {
1895 PrintExpr(Node->getArg(0));
1897 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1900 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1901 PrintExpr(Node->getArg(ArgIdx));
1904 } else if (Kind == OO_Subscript) {
1905 PrintExpr(Node->getArg(0));
1907 PrintExpr(Node->getArg(1));
1909 } else if (Node->getNumArgs() == 1) {
1910 OS << OpStrings[Kind] << ' ';
1911 PrintExpr(Node->getArg(0));
1912 } else if (Node->getNumArgs() == 2) {
1913 PrintExpr(Node->getArg(0));
1914 OS << ' ' << OpStrings[Kind] << ' ';
1915 PrintExpr(Node->getArg(1));
1917 llvm_unreachable("unknown overloaded operator");
1921 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1922 // If we have a conversion operator call only print the argument.
1923 CXXMethodDecl *MD = Node->getMethodDecl();
1924 if (MD && isa<CXXConversionDecl>(MD)) {
1925 PrintExpr(Node->getImplicitObjectArgument());
1928 VisitCallExpr(cast<CallExpr>(Node));
1931 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1932 PrintExpr(Node->getCallee());
1934 PrintCallArgs(Node->getConfig());
1936 PrintCallArgs(Node);
1940 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1941 OS << Node->getCastName() << '<';
1942 Node->getTypeAsWritten().print(OS, Policy);
1944 PrintExpr(Node->getSubExpr());
1948 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1949 VisitCXXNamedCastExpr(Node);
1952 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1953 VisitCXXNamedCastExpr(Node);
1956 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1957 VisitCXXNamedCastExpr(Node);
1960 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1961 VisitCXXNamedCastExpr(Node);
1964 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1966 if (Node->isTypeOperand()) {
1967 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1969 PrintExpr(Node->getExprOperand());
1974 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1976 if (Node->isTypeOperand()) {
1977 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1979 PrintExpr(Node->getExprOperand());
1984 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1985 PrintExpr(Node->getBaseExpr());
1986 if (Node->isArrow())
1990 if (NestedNameSpecifier *Qualifier =
1991 Node->getQualifierLoc().getNestedNameSpecifier())
1992 Qualifier->print(OS, Policy);
1993 OS << Node->getPropertyDecl()->getDeclName();
1996 void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
1997 PrintExpr(Node->getBase());
1999 PrintExpr(Node->getIdx());
2003 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
2004 switch (Node->getLiteralOperatorKind()) {
2005 case UserDefinedLiteral::LOK_Raw:
2006 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
2008 case UserDefinedLiteral::LOK_Template: {
2009 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
2010 const TemplateArgumentList *Args =
2011 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
2014 if (Args->size() != 1) {
2015 OS << "operator\"\"" << Node->getUDSuffix()->getName();
2016 TemplateSpecializationType::PrintTemplateArgumentList(
2017 OS, Args->asArray(), Policy);
2022 const TemplateArgument &Pack = Args->get(0);
2023 for (const auto &P : Pack.pack_elements()) {
2024 char C = (char)P.getAsIntegral().getZExtValue();
2029 case UserDefinedLiteral::LOK_Integer: {
2030 // Print integer literal without suffix.
2031 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
2032 OS << Int->getValue().toString(10, /*isSigned*/false);
2035 case UserDefinedLiteral::LOK_Floating: {
2036 // Print floating literal without suffix.
2037 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
2038 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
2041 case UserDefinedLiteral::LOK_String:
2042 case UserDefinedLiteral::LOK_Character:
2043 PrintExpr(Node->getCookedLiteral());
2046 OS << Node->getUDSuffix()->getName();
2049 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
2050 OS << (Node->getValue() ? "true" : "false");
2053 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
2057 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
2061 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
2062 if (!Node->getSubExpr())
2066 PrintExpr(Node->getSubExpr());
2070 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
2071 // Nothing to print: we picked up the default argument.
2074 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
2075 // Nothing to print: we picked up the default initializer.
2078 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
2079 Node->getType().print(OS, Policy);
2080 // If there are no parens, this is list-initialization, and the braces are
2081 // part of the syntax of the inner construct.
2082 if (Node->getLParenLoc().isValid())
2084 PrintExpr(Node->getSubExpr());
2085 if (Node->getLParenLoc().isValid())
2089 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
2090 PrintExpr(Node->getSubExpr());
2093 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
2094 Node->getType().print(OS, Policy);
2095 if (Node->isStdInitListInitialization())
2096 /* Nothing to do; braces are part of creating the std::initializer_list. */;
2097 else if (Node->isListInitialization())
2101 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
2102 ArgEnd = Node->arg_end();
2103 Arg != ArgEnd; ++Arg) {
2104 if ((*Arg)->isDefaultArgument())
2106 if (Arg != Node->arg_begin())
2110 if (Node->isStdInitListInitialization())
2112 else if (Node->isListInitialization())
2118 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
2120 bool NeedComma = false;
2121 switch (Node->getCaptureDefault()) {
2135 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
2136 CEnd = Node->explicit_capture_end();
2143 switch (C->getCaptureKind()) {
2151 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
2153 OS << C->getCapturedVar()->getName();
2157 OS << C->getCapturedVar()->getName();
2160 llvm_unreachable("VLA type in explicit captures.");
2163 if (Node->isInitCapture(C))
2164 PrintExpr(C->getCapturedVar()->getInit());
2168 if (Node->hasExplicitParameters()) {
2170 CXXMethodDecl *Method = Node->getCallOperator();
2172 for (auto P : Method->parameters()) {
2178 std::string ParamStr = P->getNameAsString();
2179 P->getOriginalType().print(OS, Policy, ParamStr);
2181 if (Method->isVariadic()) {
2188 if (Node->isMutable())
2191 const FunctionProtoType *Proto
2192 = Method->getType()->getAs<FunctionProtoType>();
2193 Proto->printExceptionSpecification(OS, Policy);
2195 // FIXME: Attributes
2197 // Print the trailing return type if it was specified in the source.
2198 if (Node->hasExplicitResultType()) {
2200 Proto->getReturnType().print(OS, Policy);
2205 CompoundStmt *Body = Node->getBody();
2210 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
2211 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
2212 TSInfo->getType().print(OS, Policy);
2214 Node->getType().print(OS, Policy);
2218 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
2219 if (E->isGlobalNew())
2222 unsigned NumPlace = E->getNumPlacementArgs();
2223 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
2225 PrintExpr(E->getPlacementArg(0));
2226 for (unsigned i = 1; i < NumPlace; ++i) {
2227 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
2230 PrintExpr(E->getPlacementArg(i));
2234 if (E->isParenTypeId())
2237 if (Expr *Size = E->getArraySize()) {
2238 llvm::raw_string_ostream s(TypeS);
2240 Size->printPretty(s, Helper, Policy);
2243 E->getAllocatedType().print(OS, Policy, TypeS);
2244 if (E->isParenTypeId())
2247 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
2249 if (InitStyle == CXXNewExpr::CallInit)
2251 PrintExpr(E->getInitializer());
2252 if (InitStyle == CXXNewExpr::CallInit)
2257 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2258 if (E->isGlobalDelete())
2261 if (E->isArrayForm())
2263 PrintExpr(E->getArgument());
2266 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
2267 PrintExpr(E->getBase());
2272 if (E->getQualifier())
2273 E->getQualifier()->print(OS, Policy);
2276 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
2277 OS << II->getName();
2279 E->getDestroyedType().print(OS, Policy);
2282 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
2283 if (E->isListInitialization() && !E->isStdInitListInitialization())
2286 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
2287 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
2288 // Don't print any defaulted arguments
2293 PrintExpr(E->getArg(i));
2296 if (E->isListInitialization() && !E->isStdInitListInitialization())
2300 void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) {
2301 // Parens are printed by the surrounding context.
2302 OS << "<forwarded>";
2305 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
2306 PrintExpr(E->getSubExpr());
2309 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
2310 // Just forward to the subexpression.
2311 PrintExpr(E->getSubExpr());
2315 StmtPrinter::VisitCXXUnresolvedConstructExpr(
2316 CXXUnresolvedConstructExpr *Node) {
2317 Node->getTypeAsWritten().print(OS, Policy);
2319 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
2320 ArgEnd = Node->arg_end();
2321 Arg != ArgEnd; ++Arg) {
2322 if (Arg != Node->arg_begin())
2329 void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2330 CXXDependentScopeMemberExpr *Node) {
2331 if (!Node->isImplicitAccess()) {
2332 PrintExpr(Node->getBase());
2333 OS << (Node->isArrow() ? "->" : ".");
2335 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2336 Qualifier->print(OS, Policy);
2337 if (Node->hasTemplateKeyword())
2339 OS << Node->getMemberNameInfo();
2340 if (Node->hasExplicitTemplateArgs())
2341 TemplateSpecializationType::PrintTemplateArgumentList(
2342 OS, Node->template_arguments(), Policy);
2345 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
2346 if (!Node->isImplicitAccess()) {
2347 PrintExpr(Node->getBase());
2348 OS << (Node->isArrow() ? "->" : ".");
2350 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2351 Qualifier->print(OS, Policy);
2352 if (Node->hasTemplateKeyword())
2354 OS << Node->getMemberNameInfo();
2355 if (Node->hasExplicitTemplateArgs())
2356 TemplateSpecializationType::PrintTemplateArgumentList(
2357 OS, Node->template_arguments(), Policy);
2360 static const char *getTypeTraitName(TypeTrait TT) {
2362 #define TYPE_TRAIT_1(Spelling, Name, Key) \
2363 case clang::UTT_##Name: return #Spelling;
2364 #define TYPE_TRAIT_2(Spelling, Name, Key) \
2365 case clang::BTT_##Name: return #Spelling;
2366 #define TYPE_TRAIT_N(Spelling, Name, Key) \
2367 case clang::TT_##Name: return #Spelling;
2368 #include "clang/Basic/TokenKinds.def"
2370 llvm_unreachable("Type trait not covered by switch");
2373 static const char *getTypeTraitName(ArrayTypeTrait ATT) {
2375 case ATT_ArrayRank: return "__array_rank";
2376 case ATT_ArrayExtent: return "__array_extent";
2378 llvm_unreachable("Array type trait not covered by switch");
2381 static const char *getExpressionTraitName(ExpressionTrait ET) {
2383 case ET_IsLValueExpr: return "__is_lvalue_expr";
2384 case ET_IsRValueExpr: return "__is_rvalue_expr";
2386 llvm_unreachable("Expression type trait not covered by switch");
2389 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2390 OS << getTypeTraitName(E->getTrait()) << "(";
2391 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2394 E->getArg(I)->getType().print(OS, Policy);
2399 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
2400 OS << getTypeTraitName(E->getTrait()) << '(';
2401 E->getQueriedType().print(OS, Policy);
2405 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
2406 OS << getExpressionTraitName(E->getTrait()) << '(';
2407 PrintExpr(E->getQueriedExpression());
2411 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2413 PrintExpr(E->getOperand());
2417 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
2418 PrintExpr(E->getPattern());
2422 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2423 OS << "sizeof...(" << *E->getPack() << ")";
2426 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2427 SubstNonTypeTemplateParmPackExpr *Node) {
2428 OS << *Node->getParameterPack();
2431 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2432 SubstNonTypeTemplateParmExpr *Node) {
2433 Visit(Node->getReplacement());
2436 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2437 OS << *E->getParameterPack();
2440 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2441 PrintExpr(Node->GetTemporaryExpr());
2444 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2447 PrintExpr(E->getLHS());
2448 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2452 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2453 PrintExpr(E->getRHS());
2458 // C++ Coroutines TS
2460 void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
2461 Visit(S->getBody());
2464 void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
2466 if (S->getOperand()) {
2468 Visit(S->getOperand());
2473 void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
2475 PrintExpr(S->getOperand());
2478 void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
2480 PrintExpr(S->getOperand());
2485 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2487 VisitStringLiteral(Node->getString());
2490 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
2492 Visit(E->getSubExpr());
2495 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2497 ObjCArrayLiteral::child_range Ch = E->children();
2498 for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2499 if (I != Ch.begin())
2506 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2508 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2512 ObjCDictionaryElement Element = E->getKeyValueElement(I);
2515 Visit(Element.Value);
2516 if (Element.isPackExpansion())
2522 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
2524 Node->getEncodedType().print(OS, Policy);
2528 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
2530 Node->getSelector().print(OS);
2534 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
2535 OS << "@protocol(" << *Node->getProtocol() << ')';
2538 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2540 switch (Mess->getReceiverKind()) {
2541 case ObjCMessageExpr::Instance:
2542 PrintExpr(Mess->getInstanceReceiver());
2545 case ObjCMessageExpr::Class:
2546 Mess->getClassReceiver().print(OS, Policy);
2549 case ObjCMessageExpr::SuperInstance:
2550 case ObjCMessageExpr::SuperClass:
2556 Selector selector = Mess->getSelector();
2557 if (selector.isUnarySelector()) {
2558 OS << selector.getNameForSlot(0);
2560 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
2561 if (i < selector.getNumArgs()) {
2562 if (i > 0) OS << ' ';
2563 if (selector.getIdentifierInfoForSlot(i))
2564 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
2568 else OS << ", "; // Handle variadic methods.
2570 PrintExpr(Mess->getArg(i));
2576 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2577 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2581 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2582 PrintExpr(E->getSubExpr());
2586 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
2587 OS << '(' << E->getBridgeKindName();
2588 E->getType().print(OS, Policy);
2590 PrintExpr(E->getSubExpr());
2593 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
2594 BlockDecl *BD = Node->getBlockDecl();
2597 const FunctionType *AFT = Node->getFunctionType();
2599 if (isa<FunctionNoProtoType>(AFT)) {
2601 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
2603 for (BlockDecl::param_iterator AI = BD->param_begin(),
2604 E = BD->param_end(); AI != E; ++AI) {
2605 if (AI != BD->param_begin()) OS << ", ";
2606 std::string ParamStr = (*AI)->getNameAsString();
2607 (*AI)->getType().print(OS, Policy, ParamStr);
2610 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
2611 if (FT->isVariadic()) {
2612 if (!BD->param_empty()) OS << ", ";
2620 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2621 PrintExpr(Node->getSourceExpr());
2624 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2625 // TODO: Print something reasonable for a TypoExpr, if necessary.
2626 llvm_unreachable("Cannot print TypoExpr nodes");
2629 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2630 OS << "__builtin_astype(";
2631 PrintExpr(Node->getSrcExpr());
2633 Node->getType().print(OS, Policy);
2637 //===----------------------------------------------------------------------===//
2638 // Stmt method implementations
2639 //===----------------------------------------------------------------------===//
2641 void Stmt::dumpPretty(const ASTContext &Context) const {
2642 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
2645 void Stmt::printPretty(raw_ostream &OS,
2646 PrinterHelper *Helper,
2647 const PrintingPolicy &Policy,
2648 unsigned Indentation) const {
2649 StmtPrinter P(OS, Helper, Policy, Indentation);
2650 P.Visit(const_cast<Stmt*>(this));
2653 //===----------------------------------------------------------------------===//
2655 //===----------------------------------------------------------------------===//
2657 // Implement virtual destructor.
2658 PrinterHelper::~PrinterHelper() {}