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/DeclTemplate.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/ExprCXX.h"
22 #include "clang/AST/PrettyPrinter.h"
23 #include "clang/AST/StmtVisitor.h"
24 #include "clang/Basic/CharInfo.h"
25 #include "llvm/ADT/SmallString.h"
26 #include "llvm/Support/Format.h"
27 using namespace clang;
29 //===----------------------------------------------------------------------===//
30 // StmtPrinter Visitor
31 //===----------------------------------------------------------------------===//
34 class StmtPrinter : public StmtVisitor<StmtPrinter> {
37 clang::PrinterHelper* Helper;
38 PrintingPolicy Policy;
41 StmtPrinter(raw_ostream &os, PrinterHelper* helper,
42 const PrintingPolicy &Policy,
43 unsigned Indentation = 0)
44 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy) {}
46 void PrintStmt(Stmt *S) {
47 PrintStmt(S, Policy.Indentation);
50 void PrintStmt(Stmt *S, int SubIndent) {
51 IndentLevel += SubIndent;
52 if (S && isa<Expr>(S)) {
53 // If this is an expr used in a stmt context, indent and newline it.
60 Indent() << "<<<NULL STATEMENT>>>\n";
62 IndentLevel -= SubIndent;
65 void PrintRawCompoundStmt(CompoundStmt *S);
66 void PrintRawDecl(Decl *D);
67 void PrintRawDeclStmt(const DeclStmt *S);
68 void PrintRawIfStmt(IfStmt *If);
69 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
70 void PrintCallArgs(CallExpr *E);
71 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
72 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
73 void PrintOMPExecutableDirective(OMPExecutableDirective *S);
75 void PrintExpr(Expr *E) {
82 raw_ostream &Indent(int Delta = 0) {
83 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
89 if (Helper && Helper->handledStmt(S,OS))
91 else StmtVisitor<StmtPrinter>::Visit(S);
94 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
95 Indent() << "<<unknown stmt type>>\n";
97 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
98 OS << "<<unknown expr type>>";
100 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
102 #define ABSTRACT_STMT(CLASS)
103 #define STMT(CLASS, PARENT) \
104 void Visit##CLASS(CLASS *Node);
105 #include "clang/AST/StmtNodes.inc"
109 //===----------------------------------------------------------------------===//
110 // Stmt printing methods.
111 //===----------------------------------------------------------------------===//
113 /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
114 /// with no newline after the }.
115 void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
117 for (auto *I : Node->body())
123 void StmtPrinter::PrintRawDecl(Decl *D) {
124 D->print(OS, Policy, IndentLevel);
127 void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
128 SmallVector<Decl*, 2> Decls(S->decls());
129 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
132 void StmtPrinter::VisitNullStmt(NullStmt *Node) {
136 void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
138 PrintRawDeclStmt(Node);
142 void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
144 PrintRawCompoundStmt(Node);
148 void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
149 Indent(-1) << "case ";
150 PrintExpr(Node->getLHS());
151 if (Node->getRHS()) {
153 PrintExpr(Node->getRHS());
157 PrintStmt(Node->getSubStmt(), 0);
160 void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
161 Indent(-1) << "default:\n";
162 PrintStmt(Node->getSubStmt(), 0);
165 void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
166 Indent(-1) << Node->getName() << ":\n";
167 PrintStmt(Node->getSubStmt(), 0);
170 void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
171 for (const auto *Attr : Node->getAttrs()) {
172 Attr->printPretty(OS, Policy);
175 PrintStmt(Node->getSubStmt(), 0);
178 void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
180 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
181 PrintRawDeclStmt(DS);
183 PrintExpr(If->getCond());
186 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
188 PrintRawCompoundStmt(CS);
189 OS << (If->getElse() ? ' ' : '\n');
192 PrintStmt(If->getThen());
193 if (If->getElse()) Indent();
196 if (Stmt *Else = If->getElse()) {
199 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
201 PrintRawCompoundStmt(CS);
203 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
205 PrintRawIfStmt(ElseIf);
208 PrintStmt(If->getElse());
213 void StmtPrinter::VisitIfStmt(IfStmt *If) {
218 void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
219 Indent() << "switch (";
220 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
221 PrintRawDeclStmt(DS);
223 PrintExpr(Node->getCond());
226 // Pretty print compoundstmt bodies (very common).
227 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
229 PrintRawCompoundStmt(CS);
233 PrintStmt(Node->getBody());
237 void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
238 Indent() << "while (";
239 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
240 PrintRawDeclStmt(DS);
242 PrintExpr(Node->getCond());
244 PrintStmt(Node->getBody());
247 void StmtPrinter::VisitDoStmt(DoStmt *Node) {
249 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
250 PrintRawCompoundStmt(CS);
254 PrintStmt(Node->getBody());
259 PrintExpr(Node->getCond());
263 void StmtPrinter::VisitForStmt(ForStmt *Node) {
265 if (Node->getInit()) {
266 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
267 PrintRawDeclStmt(DS);
269 PrintExpr(cast<Expr>(Node->getInit()));
272 if (Node->getCond()) {
274 PrintExpr(Node->getCond());
277 if (Node->getInc()) {
279 PrintExpr(Node->getInc());
283 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
284 PrintRawCompoundStmt(CS);
288 PrintStmt(Node->getBody());
292 void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
294 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
295 PrintRawDeclStmt(DS);
297 PrintExpr(cast<Expr>(Node->getElement()));
299 PrintExpr(Node->getCollection());
302 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
303 PrintRawCompoundStmt(CS);
307 PrintStmt(Node->getBody());
311 void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
313 PrintingPolicy SubPolicy(Policy);
314 SubPolicy.SuppressInitializers = true;
315 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
317 PrintExpr(Node->getRangeInit());
319 PrintStmt(Node->getBody());
321 if (Policy.IncludeNewlines) OS << "\n";
324 void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
326 if (Node->isIfExists())
327 OS << "__if_exists (";
329 OS << "__if_not_exists (";
331 if (NestedNameSpecifier *Qualifier
332 = Node->getQualifierLoc().getNestedNameSpecifier())
333 Qualifier->print(OS, Policy);
335 OS << Node->getNameInfo() << ") ";
337 PrintRawCompoundStmt(Node->getSubStmt());
340 void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
341 Indent() << "goto " << Node->getLabel()->getName() << ";";
342 if (Policy.IncludeNewlines) OS << "\n";
345 void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
346 Indent() << "goto *";
347 PrintExpr(Node->getTarget());
349 if (Policy.IncludeNewlines) OS << "\n";
352 void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
353 Indent() << "continue;";
354 if (Policy.IncludeNewlines) OS << "\n";
357 void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
358 Indent() << "break;";
359 if (Policy.IncludeNewlines) OS << "\n";
363 void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
364 Indent() << "return";
365 if (Node->getRetValue()) {
367 PrintExpr(Node->getRetValue());
370 if (Policy.IncludeNewlines) OS << "\n";
374 void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
377 if (Node->isVolatile())
381 VisitStringLiteral(Node->getAsmString());
384 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
385 Node->getNumClobbers() != 0)
388 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
392 if (!Node->getOutputName(i).empty()) {
394 OS << Node->getOutputName(i);
398 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
400 Visit(Node->getOutputExpr(i));
405 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
408 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
412 if (!Node->getInputName(i).empty()) {
414 OS << Node->getInputName(i);
418 VisitStringLiteral(Node->getInputConstraintLiteral(i));
420 Visit(Node->getInputExpr(i));
425 if (Node->getNumClobbers() != 0)
428 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
432 VisitStringLiteral(Node->getClobberStringLiteral(i));
436 if (Policy.IncludeNewlines) OS << "\n";
439 void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
440 // FIXME: Implement MS style inline asm statement printer.
441 Indent() << "__asm ";
442 if (Node->hasBraces())
444 OS << Node->getAsmString() << "\n";
445 if (Node->hasBraces())
449 void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
450 PrintStmt(Node->getCapturedDecl()->getBody());
453 void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
455 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
456 PrintRawCompoundStmt(TS);
460 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
461 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
462 Indent() << "@catch(";
463 if (catchStmt->getCatchParamDecl()) {
464 if (Decl *DS = catchStmt->getCatchParamDecl())
468 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
469 PrintRawCompoundStmt(CS);
474 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
475 Node->getFinallyStmt())) {
476 Indent() << "@finally";
477 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
482 void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
485 void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
486 Indent() << "@catch (...) { /* todo */ } \n";
489 void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
490 Indent() << "@throw";
491 if (Node->getThrowExpr()) {
493 PrintExpr(Node->getThrowExpr());
498 void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
499 Indent() << "@synchronized (";
500 PrintExpr(Node->getSynchExpr());
502 PrintRawCompoundStmt(Node->getSynchBody());
506 void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
507 Indent() << "@autoreleasepool";
508 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
512 void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
514 if (Decl *ExDecl = Node->getExceptionDecl())
515 PrintRawDecl(ExDecl);
519 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
522 void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
524 PrintRawCXXCatchStmt(Node);
528 void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
530 PrintRawCompoundStmt(Node->getTryBlock());
531 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
533 PrintRawCXXCatchStmt(Node->getHandler(i));
538 void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
539 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
540 PrintRawCompoundStmt(Node->getTryBlock());
541 SEHExceptStmt *E = Node->getExceptHandler();
542 SEHFinallyStmt *F = Node->getFinallyHandler();
544 PrintRawSEHExceptHandler(E);
546 assert(F && "Must have a finally block...");
547 PrintRawSEHFinallyStmt(F);
552 void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
554 PrintRawCompoundStmt(Node->getBlock());
558 void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
560 VisitExpr(Node->getFilterExpr());
562 PrintRawCompoundStmt(Node->getBlock());
566 void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
568 PrintRawSEHExceptHandler(Node);
572 void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
574 PrintRawSEHFinallyStmt(Node);
578 void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
579 Indent() << "__leave;";
580 if (Policy.IncludeNewlines) OS << "\n";
583 //===----------------------------------------------------------------------===//
584 // OpenMP clauses printing methods
585 //===----------------------------------------------------------------------===//
588 class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
590 const PrintingPolicy &Policy;
591 /// \brief Process clauses with list of variables.
592 template <typename T>
593 void VisitOMPClauseList(T *Node, char StartSym);
595 OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy)
596 : OS(OS), Policy(Policy) { }
597 #define OPENMP_CLAUSE(Name, Class) \
598 void Visit##Class(Class *S);
599 #include "clang/Basic/OpenMPKinds.def"
602 void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
604 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
608 void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) {
610 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
614 void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
615 OS << "num_threads(";
616 Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0);
620 void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
622 Node->getSafelen()->printPretty(OS, nullptr, Policy, 0);
626 void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) {
628 Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0);
632 void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
634 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
638 void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) {
640 << getOpenMPSimpleClauseTypeName(OMPC_proc_bind, Node->getProcBindKind())
644 void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) {
646 << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind());
647 if (Node->getChunkSize()) {
649 Node->getChunkSize()->printPretty(OS, nullptr, Policy);
654 void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *) {
658 void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) {
662 void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) {
666 void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) {
670 void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; }
672 void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; }
674 void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *) {
678 void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) {
682 void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) {
687 void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
688 for (typename T::varlist_iterator I = Node->varlist_begin(),
689 E = Node->varlist_end();
691 assert(*I && "Expected non-null Stmt");
692 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*I)) {
693 OS << (I == Node->varlist_begin() ? StartSym : ',');
694 cast<NamedDecl>(DRE->getDecl())->printQualifiedName(OS);
696 OS << (I == Node->varlist_begin() ? StartSym : ',');
697 (*I)->printPretty(OS, nullptr, Policy, 0);
702 void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
703 if (!Node->varlist_empty()) {
705 VisitOMPClauseList(Node, '(');
710 void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
711 if (!Node->varlist_empty()) {
712 OS << "firstprivate";
713 VisitOMPClauseList(Node, '(');
718 void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) {
719 if (!Node->varlist_empty()) {
721 VisitOMPClauseList(Node, '(');
726 void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
727 if (!Node->varlist_empty()) {
729 VisitOMPClauseList(Node, '(');
734 void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) {
735 if (!Node->varlist_empty()) {
737 NestedNameSpecifier *QualifierLoc =
738 Node->getQualifierLoc().getNestedNameSpecifier();
739 OverloadedOperatorKind OOK =
740 Node->getNameInfo().getName().getCXXOverloadedOperator();
741 if (QualifierLoc == nullptr && OOK != OO_None) {
742 // Print reduction identifier in C format
743 OS << getOperatorSpelling(OOK);
746 if (QualifierLoc != nullptr)
747 QualifierLoc->print(OS, Policy);
748 OS << Node->getNameInfo();
751 VisitOMPClauseList(Node, ' ');
756 void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) {
757 if (!Node->varlist_empty()) {
759 VisitOMPClauseList(Node, '(');
760 if (Node->getStep() != nullptr) {
762 Node->getStep()->printPretty(OS, nullptr, Policy, 0);
768 void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) {
769 if (!Node->varlist_empty()) {
771 VisitOMPClauseList(Node, '(');
772 if (Node->getAlignment() != nullptr) {
774 Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
780 void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
781 if (!Node->varlist_empty()) {
783 VisitOMPClauseList(Node, '(');
788 void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) {
789 if (!Node->varlist_empty()) {
791 VisitOMPClauseList(Node, '(');
796 void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) {
797 if (!Node->varlist_empty()) {
798 VisitOMPClauseList(Node, '(');
803 void OMPClausePrinter::VisitOMPDependClause(OMPDependClause *Node) {
804 if (!Node->varlist_empty()) {
806 OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
807 Node->getDependencyKind())
809 VisitOMPClauseList(Node, ' ');
815 //===----------------------------------------------------------------------===//
816 // OpenMP directives printing methods
817 //===----------------------------------------------------------------------===//
819 void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) {
820 OMPClausePrinter Printer(OS, Policy);
821 ArrayRef<OMPClause *> Clauses = S->clauses();
822 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
824 if (*I && !(*I)->isImplicit()) {
829 if (S->hasAssociatedStmt() && S->getAssociatedStmt()) {
830 assert(isa<CapturedStmt>(S->getAssociatedStmt()) &&
831 "Expected captured statement!");
832 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt();
837 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
838 Indent() << "#pragma omp parallel ";
839 PrintOMPExecutableDirective(Node);
842 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
843 Indent() << "#pragma omp simd ";
844 PrintOMPExecutableDirective(Node);
847 void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
848 Indent() << "#pragma omp for ";
849 PrintOMPExecutableDirective(Node);
852 void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
853 Indent() << "#pragma omp for simd ";
854 PrintOMPExecutableDirective(Node);
857 void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
858 Indent() << "#pragma omp sections ";
859 PrintOMPExecutableDirective(Node);
862 void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
863 Indent() << "#pragma omp section";
864 PrintOMPExecutableDirective(Node);
867 void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
868 Indent() << "#pragma omp single ";
869 PrintOMPExecutableDirective(Node);
872 void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
873 Indent() << "#pragma omp master";
874 PrintOMPExecutableDirective(Node);
877 void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
878 Indent() << "#pragma omp critical";
879 if (Node->getDirectiveName().getName()) {
881 Node->getDirectiveName().printName(OS);
884 PrintOMPExecutableDirective(Node);
887 void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
888 Indent() << "#pragma omp parallel for ";
889 PrintOMPExecutableDirective(Node);
892 void StmtPrinter::VisitOMPParallelForSimdDirective(
893 OMPParallelForSimdDirective *Node) {
894 Indent() << "#pragma omp parallel for simd ";
895 PrintOMPExecutableDirective(Node);
898 void StmtPrinter::VisitOMPParallelSectionsDirective(
899 OMPParallelSectionsDirective *Node) {
900 Indent() << "#pragma omp parallel sections ";
901 PrintOMPExecutableDirective(Node);
904 void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
905 Indent() << "#pragma omp task ";
906 PrintOMPExecutableDirective(Node);
909 void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
910 Indent() << "#pragma omp taskyield";
911 PrintOMPExecutableDirective(Node);
914 void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
915 Indent() << "#pragma omp barrier";
916 PrintOMPExecutableDirective(Node);
919 void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
920 Indent() << "#pragma omp taskwait";
921 PrintOMPExecutableDirective(Node);
924 void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
925 Indent() << "#pragma omp taskgroup";
926 PrintOMPExecutableDirective(Node);
929 void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
930 Indent() << "#pragma omp flush ";
931 PrintOMPExecutableDirective(Node);
934 void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
935 Indent() << "#pragma omp ordered";
936 PrintOMPExecutableDirective(Node);
939 void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
940 Indent() << "#pragma omp atomic ";
941 PrintOMPExecutableDirective(Node);
944 void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
945 Indent() << "#pragma omp target ";
946 PrintOMPExecutableDirective(Node);
949 void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
950 Indent() << "#pragma omp teams ";
951 PrintOMPExecutableDirective(Node);
954 void StmtPrinter::VisitOMPCancellationPointDirective(
955 OMPCancellationPointDirective *Node) {
956 Indent() << "#pragma omp cancellation point "
957 << getOpenMPDirectiveName(Node->getCancelRegion());
958 PrintOMPExecutableDirective(Node);
961 void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
962 Indent() << "#pragma omp cancel "
963 << getOpenMPDirectiveName(Node->getCancelRegion());
964 PrintOMPExecutableDirective(Node);
966 //===----------------------------------------------------------------------===//
967 // Expr printing methods.
968 //===----------------------------------------------------------------------===//
970 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
971 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
972 Qualifier->print(OS, Policy);
973 if (Node->hasTemplateKeyword())
975 OS << Node->getNameInfo();
976 if (Node->hasExplicitTemplateArgs())
977 TemplateSpecializationType::PrintTemplateArgumentList(
978 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
981 void StmtPrinter::VisitDependentScopeDeclRefExpr(
982 DependentScopeDeclRefExpr *Node) {
983 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
984 Qualifier->print(OS, Policy);
985 if (Node->hasTemplateKeyword())
987 OS << Node->getNameInfo();
988 if (Node->hasExplicitTemplateArgs())
989 TemplateSpecializationType::PrintTemplateArgumentList(
990 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
993 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
994 if (Node->getQualifier())
995 Node->getQualifier()->print(OS, Policy);
996 if (Node->hasTemplateKeyword())
998 OS << Node->getNameInfo();
999 if (Node->hasExplicitTemplateArgs())
1000 TemplateSpecializationType::PrintTemplateArgumentList(
1001 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1004 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
1005 if (Node->getBase()) {
1006 PrintExpr(Node->getBase());
1007 OS << (Node->isArrow() ? "->" : ".");
1009 OS << *Node->getDecl();
1012 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
1013 if (Node->isSuperReceiver())
1015 else if (Node->isObjectReceiver() && Node->getBase()) {
1016 PrintExpr(Node->getBase());
1018 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1019 OS << Node->getClassReceiver()->getName() << ".";
1022 if (Node->isImplicitProperty())
1023 Node->getImplicitPropertyGetter()->getSelector().print(OS);
1025 OS << Node->getExplicitProperty()->getName();
1028 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1030 PrintExpr(Node->getBaseExpr());
1032 PrintExpr(Node->getKeyExpr());
1036 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
1037 OS << PredefinedExpr::getIdentTypeName(Node->getIdentType());
1040 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
1041 unsigned value = Node->getValue();
1043 switch (Node->getKind()) {
1044 case CharacterLiteral::Ascii: break; // no prefix.
1045 case CharacterLiteral::Wide: OS << 'L'; break;
1046 case CharacterLiteral::UTF16: OS << 'u'; break;
1047 case CharacterLiteral::UTF32: OS << 'U'; break;
1058 // TODO: K&R: the meaning of '\\a' is different in traditional C
1064 // Nonstandard escape sequence.
1084 if (value < 256 && isPrintable((unsigned char)value))
1085 OS << "'" << (char)value << "'";
1086 else if (value < 256)
1087 OS << "'\\x" << llvm::format("%02x", value) << "'";
1088 else if (value <= 0xFFFF)
1089 OS << "'\\u" << llvm::format("%04x", value) << "'";
1091 OS << "'\\U" << llvm::format("%08x", value) << "'";
1095 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
1096 bool isSigned = Node->getType()->isSignedIntegerType();
1097 OS << Node->getValue().toString(10, isSigned);
1099 // Emit suffixes. Integer literals are always a builtin integer type.
1100 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1101 default: llvm_unreachable("Unexpected type for integer literal!");
1102 case BuiltinType::Char_S:
1103 case BuiltinType::Char_U: OS << "i8"; break;
1104 case BuiltinType::UChar: OS << "Ui8"; break;
1105 case BuiltinType::Short: OS << "i16"; break;
1106 case BuiltinType::UShort: OS << "Ui16"; break;
1107 case BuiltinType::Int: break; // no suffix.
1108 case BuiltinType::UInt: OS << 'U'; break;
1109 case BuiltinType::Long: OS << 'L'; break;
1110 case BuiltinType::ULong: OS << "UL"; break;
1111 case BuiltinType::LongLong: OS << "LL"; break;
1112 case BuiltinType::ULongLong: OS << "ULL"; break;
1113 case BuiltinType::Int128: OS << "i128"; break;
1114 case BuiltinType::UInt128: OS << "Ui128"; break;
1118 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1120 SmallString<16> Str;
1121 Node->getValue().toString(Str);
1123 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1124 OS << '.'; // Trailing dot in order to separate from ints.
1129 // Emit suffixes. Float literals are always a builtin float type.
1130 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1131 default: llvm_unreachable("Unexpected type for float literal!");
1132 case BuiltinType::Half: break; // FIXME: suffix?
1133 case BuiltinType::Double: break; // no suffix.
1134 case BuiltinType::Float: OS << 'F'; break;
1135 case BuiltinType::LongDouble: OS << 'L'; break;
1139 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1140 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
1143 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1144 PrintExpr(Node->getSubExpr());
1148 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
1149 Str->outputString(OS);
1151 void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1153 PrintExpr(Node->getSubExpr());
1156 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
1157 if (!Node->isPostfix()) {
1158 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1160 // Print a space if this is an "identifier operator" like __real, or if
1161 // it might be concatenated incorrectly like '+'.
1162 switch (Node->getOpcode()) {
1171 if (isa<UnaryOperator>(Node->getSubExpr()))
1176 PrintExpr(Node->getSubExpr());
1178 if (Node->isPostfix())
1179 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1182 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1183 OS << "__builtin_offsetof(";
1184 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1186 bool PrintedSomething = false;
1187 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1188 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
1189 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
1192 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1194 PrintedSomething = true;
1198 // Skip implicit base indirections.
1199 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
1202 // Field or identifier node.
1203 IdentifierInfo *Id = ON.getFieldName();
1207 if (PrintedSomething)
1210 PrintedSomething = true;
1211 OS << Id->getName();
1216 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
1217 switch(Node->getKind()) {
1222 if (Policy.LangOpts.CPlusPlus)
1224 else if (Policy.LangOpts.C11)
1232 case UETT_OpenMPRequiredSimdAlign:
1233 OS << "__builtin_omp_required_simd_align";
1236 if (Node->isArgumentType()) {
1238 Node->getArgumentType().print(OS, Policy);
1242 PrintExpr(Node->getArgumentExpr());
1246 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1248 PrintExpr(Node->getControllingExpr());
1249 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
1251 QualType T = Node->getAssocType(i);
1255 T.print(OS, Policy);
1257 PrintExpr(Node->getAssocExpr(i));
1262 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
1263 PrintExpr(Node->getLHS());
1265 PrintExpr(Node->getRHS());
1269 void StmtPrinter::PrintCallArgs(CallExpr *Call) {
1270 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
1271 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1272 // Don't print any defaulted arguments
1277 PrintExpr(Call->getArg(i));
1281 void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1282 PrintExpr(Call->getCallee());
1284 PrintCallArgs(Call);
1287 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1288 // FIXME: Suppress printing implicit bases (like "this")
1289 PrintExpr(Node->getBase());
1291 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1292 FieldDecl *ParentDecl = ParentMember
1293 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr;
1295 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1296 OS << (Node->isArrow() ? "->" : ".");
1298 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1299 if (FD->isAnonymousStructOrUnion())
1302 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1303 Qualifier->print(OS, Policy);
1304 if (Node->hasTemplateKeyword())
1306 OS << Node->getMemberNameInfo();
1307 if (Node->hasExplicitTemplateArgs())
1308 TemplateSpecializationType::PrintTemplateArgumentList(
1309 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1311 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1312 PrintExpr(Node->getBase());
1313 OS << (Node->isArrow() ? "->isa" : ".isa");
1316 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1317 PrintExpr(Node->getBase());
1319 OS << Node->getAccessor().getName();
1321 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1323 Node->getTypeAsWritten().print(OS, Policy);
1325 PrintExpr(Node->getSubExpr());
1327 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1329 Node->getType().print(OS, Policy);
1331 PrintExpr(Node->getInitializer());
1333 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1334 // No need to print anything, simply forward to the subexpression.
1335 PrintExpr(Node->getSubExpr());
1337 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1338 PrintExpr(Node->getLHS());
1339 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1340 PrintExpr(Node->getRHS());
1342 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1343 PrintExpr(Node->getLHS());
1344 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1345 PrintExpr(Node->getRHS());
1347 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1348 PrintExpr(Node->getCond());
1350 PrintExpr(Node->getLHS());
1352 PrintExpr(Node->getRHS());
1358 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1359 PrintExpr(Node->getCommon());
1361 PrintExpr(Node->getFalseExpr());
1363 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1364 OS << "&&" << Node->getLabel()->getName();
1367 void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1369 PrintRawCompoundStmt(E->getSubStmt());
1373 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1374 OS << "__builtin_choose_expr(";
1375 PrintExpr(Node->getCond());
1377 PrintExpr(Node->getLHS());
1379 PrintExpr(Node->getRHS());
1383 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1387 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1388 OS << "__builtin_shufflevector(";
1389 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1391 PrintExpr(Node->getExpr(i));
1396 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1397 OS << "__builtin_convertvector(";
1398 PrintExpr(Node->getSrcExpr());
1400 Node->getType().print(OS, Policy);
1404 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1405 if (Node->getSyntacticForm()) {
1406 Visit(Node->getSyntacticForm());
1411 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1413 if (Node->getInit(i))
1414 PrintExpr(Node->getInit(i));
1421 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1423 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1425 PrintExpr(Node->getExpr(i));
1430 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1431 bool NeedsEquals = true;
1432 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1433 DEnd = Node->designators_end();
1435 if (D->isFieldDesignator()) {
1436 if (D->getDotLoc().isInvalid()) {
1437 if (IdentifierInfo *II = D->getFieldName()) {
1438 OS << II->getName() << ":";
1439 NeedsEquals = false;
1442 OS << "." << D->getFieldName()->getName();
1446 if (D->isArrayDesignator()) {
1447 PrintExpr(Node->getArrayIndex(*D));
1449 PrintExpr(Node->getArrayRangeStart(*D));
1451 PrintExpr(Node->getArrayRangeEnd(*D));
1461 PrintExpr(Node->getInit());
1464 void StmtPrinter::VisitDesignatedInitUpdateExpr(
1465 DesignatedInitUpdateExpr *Node) {
1468 PrintExpr(Node->getBase());
1471 OS << "/*updater*/";
1472 PrintExpr(Node->getUpdater());
1476 void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1477 OS << "/*no init*/";
1480 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1481 if (Policy.LangOpts.CPlusPlus) {
1482 OS << "/*implicit*/";
1483 Node->getType().print(OS, Policy);
1486 OS << "/*implicit*/(";
1487 Node->getType().print(OS, Policy);
1489 if (Node->getType()->isRecordType())
1496 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1497 OS << "__builtin_va_arg(";
1498 PrintExpr(Node->getSubExpr());
1500 Node->getType().print(OS, Policy);
1504 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1505 PrintExpr(Node->getSyntacticForm());
1508 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1509 const char *Name = nullptr;
1510 switch (Node->getOp()) {
1511 #define BUILTIN(ID, TYPE, ATTRS)
1512 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1513 case AtomicExpr::AO ## ID: \
1516 #include "clang/Basic/Builtins.def"
1520 // AtomicExpr stores its subexpressions in a permuted order.
1521 PrintExpr(Node->getPtr());
1522 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1523 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
1525 PrintExpr(Node->getVal1());
1527 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1528 Node->isCmpXChg()) {
1530 PrintExpr(Node->getVal2());
1532 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1533 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1535 PrintExpr(Node->getWeak());
1537 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1539 PrintExpr(Node->getOrder());
1541 if (Node->isCmpXChg()) {
1543 PrintExpr(Node->getOrderFail());
1549 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1550 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1552 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1554 #include "clang/Basic/OperatorKinds.def"
1557 OverloadedOperatorKind Kind = Node->getOperator();
1558 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1559 if (Node->getNumArgs() == 1) {
1560 OS << OpStrings[Kind] << ' ';
1561 PrintExpr(Node->getArg(0));
1563 PrintExpr(Node->getArg(0));
1564 OS << ' ' << OpStrings[Kind];
1566 } else if (Kind == OO_Arrow) {
1567 PrintExpr(Node->getArg(0));
1568 } else if (Kind == OO_Call) {
1569 PrintExpr(Node->getArg(0));
1571 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1574 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1575 PrintExpr(Node->getArg(ArgIdx));
1578 } else if (Kind == OO_Subscript) {
1579 PrintExpr(Node->getArg(0));
1581 PrintExpr(Node->getArg(1));
1583 } else if (Node->getNumArgs() == 1) {
1584 OS << OpStrings[Kind] << ' ';
1585 PrintExpr(Node->getArg(0));
1586 } else if (Node->getNumArgs() == 2) {
1587 PrintExpr(Node->getArg(0));
1588 OS << ' ' << OpStrings[Kind] << ' ';
1589 PrintExpr(Node->getArg(1));
1591 llvm_unreachable("unknown overloaded operator");
1595 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1596 // If we have a conversion operator call only print the argument.
1597 CXXMethodDecl *MD = Node->getMethodDecl();
1598 if (MD && isa<CXXConversionDecl>(MD)) {
1599 PrintExpr(Node->getImplicitObjectArgument());
1602 VisitCallExpr(cast<CallExpr>(Node));
1605 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1606 PrintExpr(Node->getCallee());
1608 PrintCallArgs(Node->getConfig());
1610 PrintCallArgs(Node);
1614 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1615 OS << Node->getCastName() << '<';
1616 Node->getTypeAsWritten().print(OS, Policy);
1618 PrintExpr(Node->getSubExpr());
1622 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1623 VisitCXXNamedCastExpr(Node);
1626 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1627 VisitCXXNamedCastExpr(Node);
1630 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1631 VisitCXXNamedCastExpr(Node);
1634 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1635 VisitCXXNamedCastExpr(Node);
1638 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1640 if (Node->isTypeOperand()) {
1641 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1643 PrintExpr(Node->getExprOperand());
1648 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1650 if (Node->isTypeOperand()) {
1651 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1653 PrintExpr(Node->getExprOperand());
1658 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1659 PrintExpr(Node->getBaseExpr());
1660 if (Node->isArrow())
1664 if (NestedNameSpecifier *Qualifier =
1665 Node->getQualifierLoc().getNestedNameSpecifier())
1666 Qualifier->print(OS, Policy);
1667 OS << Node->getPropertyDecl()->getDeclName();
1670 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1671 switch (Node->getLiteralOperatorKind()) {
1672 case UserDefinedLiteral::LOK_Raw:
1673 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
1675 case UserDefinedLiteral::LOK_Template: {
1676 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1677 const TemplateArgumentList *Args =
1678 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1681 if (Args->size() != 1) {
1682 OS << "operator \"\" " << Node->getUDSuffix()->getName();
1683 TemplateSpecializationType::PrintTemplateArgumentList(
1684 OS, Args->data(), Args->size(), Policy);
1689 const TemplateArgument &Pack = Args->get(0);
1690 for (const auto &P : Pack.pack_elements()) {
1691 char C = (char)P.getAsIntegral().getZExtValue();
1696 case UserDefinedLiteral::LOK_Integer: {
1697 // Print integer literal without suffix.
1698 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1699 OS << Int->getValue().toString(10, /*isSigned*/false);
1702 case UserDefinedLiteral::LOK_Floating: {
1703 // Print floating literal without suffix.
1704 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1705 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1708 case UserDefinedLiteral::LOK_String:
1709 case UserDefinedLiteral::LOK_Character:
1710 PrintExpr(Node->getCookedLiteral());
1713 OS << Node->getUDSuffix()->getName();
1716 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1717 OS << (Node->getValue() ? "true" : "false");
1720 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1724 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1728 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1729 if (!Node->getSubExpr())
1733 PrintExpr(Node->getSubExpr());
1737 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1738 // Nothing to print: we picked up the default argument.
1741 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1742 // Nothing to print: we picked up the default initializer.
1745 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1746 Node->getType().print(OS, Policy);
1747 // If there are no parens, this is list-initialization, and the braces are
1748 // part of the syntax of the inner construct.
1749 if (Node->getLParenLoc().isValid())
1751 PrintExpr(Node->getSubExpr());
1752 if (Node->getLParenLoc().isValid())
1756 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1757 PrintExpr(Node->getSubExpr());
1760 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1761 Node->getType().print(OS, Policy);
1762 if (Node->isStdInitListInitialization())
1763 /* Nothing to do; braces are part of creating the std::initializer_list. */;
1764 else if (Node->isListInitialization())
1768 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1769 ArgEnd = Node->arg_end();
1770 Arg != ArgEnd; ++Arg) {
1771 if (Arg->isDefaultArgument())
1773 if (Arg != Node->arg_begin())
1777 if (Node->isStdInitListInitialization())
1779 else if (Node->isListInitialization())
1785 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1787 bool NeedComma = false;
1788 switch (Node->getCaptureDefault()) {
1802 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1803 CEnd = Node->explicit_capture_end();
1810 switch (C->getCaptureKind()) {
1816 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
1818 OS << C->getCapturedVar()->getName();
1822 OS << C->getCapturedVar()->getName();
1825 llvm_unreachable("VLA type in explicit captures.");
1828 if (Node->isInitCapture(C))
1829 PrintExpr(C->getCapturedVar()->getInit());
1833 if (Node->hasExplicitParameters()) {
1835 CXXMethodDecl *Method = Node->getCallOperator();
1837 for (auto P : Method->params()) {
1843 std::string ParamStr = P->getNameAsString();
1844 P->getOriginalType().print(OS, Policy, ParamStr);
1846 if (Method->isVariadic()) {
1853 if (Node->isMutable())
1856 const FunctionProtoType *Proto
1857 = Method->getType()->getAs<FunctionProtoType>();
1858 Proto->printExceptionSpecification(OS, Policy);
1860 // FIXME: Attributes
1862 // Print the trailing return type if it was specified in the source.
1863 if (Node->hasExplicitResultType()) {
1865 Proto->getReturnType().print(OS, Policy);
1870 CompoundStmt *Body = Node->getBody();
1875 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
1876 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1877 TSInfo->getType().print(OS, Policy);
1879 Node->getType().print(OS, Policy);
1883 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1884 if (E->isGlobalNew())
1887 unsigned NumPlace = E->getNumPlacementArgs();
1888 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
1890 PrintExpr(E->getPlacementArg(0));
1891 for (unsigned i = 1; i < NumPlace; ++i) {
1892 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1895 PrintExpr(E->getPlacementArg(i));
1899 if (E->isParenTypeId())
1902 if (Expr *Size = E->getArraySize()) {
1903 llvm::raw_string_ostream s(TypeS);
1905 Size->printPretty(s, Helper, Policy);
1908 E->getAllocatedType().print(OS, Policy, TypeS);
1909 if (E->isParenTypeId())
1912 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1914 if (InitStyle == CXXNewExpr::CallInit)
1916 PrintExpr(E->getInitializer());
1917 if (InitStyle == CXXNewExpr::CallInit)
1922 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1923 if (E->isGlobalDelete())
1926 if (E->isArrayForm())
1928 PrintExpr(E->getArgument());
1931 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1932 PrintExpr(E->getBase());
1937 if (E->getQualifier())
1938 E->getQualifier()->print(OS, Policy);
1941 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1942 OS << II->getName();
1944 E->getDestroyedType().print(OS, Policy);
1947 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1948 if (E->isListInitialization() && !E->isStdInitListInitialization())
1951 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1952 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1953 // Don't print any defaulted arguments
1958 PrintExpr(E->getArg(i));
1961 if (E->isListInitialization() && !E->isStdInitListInitialization())
1965 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
1966 PrintExpr(E->getSubExpr());
1969 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
1970 // Just forward to the subexpression.
1971 PrintExpr(E->getSubExpr());
1975 StmtPrinter::VisitCXXUnresolvedConstructExpr(
1976 CXXUnresolvedConstructExpr *Node) {
1977 Node->getTypeAsWritten().print(OS, Policy);
1979 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
1980 ArgEnd = Node->arg_end();
1981 Arg != ArgEnd; ++Arg) {
1982 if (Arg != Node->arg_begin())
1989 void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1990 CXXDependentScopeMemberExpr *Node) {
1991 if (!Node->isImplicitAccess()) {
1992 PrintExpr(Node->getBase());
1993 OS << (Node->isArrow() ? "->" : ".");
1995 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1996 Qualifier->print(OS, Policy);
1997 if (Node->hasTemplateKeyword())
1999 OS << Node->getMemberNameInfo();
2000 if (Node->hasExplicitTemplateArgs())
2001 TemplateSpecializationType::PrintTemplateArgumentList(
2002 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
2005 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
2006 if (!Node->isImplicitAccess()) {
2007 PrintExpr(Node->getBase());
2008 OS << (Node->isArrow() ? "->" : ".");
2010 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2011 Qualifier->print(OS, Policy);
2012 if (Node->hasTemplateKeyword())
2014 OS << Node->getMemberNameInfo();
2015 if (Node->hasExplicitTemplateArgs())
2016 TemplateSpecializationType::PrintTemplateArgumentList(
2017 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
2020 static const char *getTypeTraitName(TypeTrait TT) {
2022 #define TYPE_TRAIT_1(Spelling, Name, Key) \
2023 case clang::UTT_##Name: return #Spelling;
2024 #define TYPE_TRAIT_2(Spelling, Name, Key) \
2025 case clang::BTT_##Name: return #Spelling;
2026 #define TYPE_TRAIT_N(Spelling, Name, Key) \
2027 case clang::TT_##Name: return #Spelling;
2028 #include "clang/Basic/TokenKinds.def"
2030 llvm_unreachable("Type trait not covered by switch");
2033 static const char *getTypeTraitName(ArrayTypeTrait ATT) {
2035 case ATT_ArrayRank: return "__array_rank";
2036 case ATT_ArrayExtent: return "__array_extent";
2038 llvm_unreachable("Array type trait not covered by switch");
2041 static const char *getExpressionTraitName(ExpressionTrait ET) {
2043 case ET_IsLValueExpr: return "__is_lvalue_expr";
2044 case ET_IsRValueExpr: return "__is_rvalue_expr";
2046 llvm_unreachable("Expression type trait not covered by switch");
2049 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2050 OS << getTypeTraitName(E->getTrait()) << "(";
2051 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2054 E->getArg(I)->getType().print(OS, Policy);
2059 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
2060 OS << getTypeTraitName(E->getTrait()) << '(';
2061 E->getQueriedType().print(OS, Policy);
2065 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
2066 OS << getExpressionTraitName(E->getTrait()) << '(';
2067 PrintExpr(E->getQueriedExpression());
2071 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2073 PrintExpr(E->getOperand());
2077 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
2078 PrintExpr(E->getPattern());
2082 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2083 OS << "sizeof...(" << *E->getPack() << ")";
2086 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2087 SubstNonTypeTemplateParmPackExpr *Node) {
2088 OS << *Node->getParameterPack();
2091 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2092 SubstNonTypeTemplateParmExpr *Node) {
2093 Visit(Node->getReplacement());
2096 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2097 OS << *E->getParameterPack();
2100 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2101 PrintExpr(Node->GetTemporaryExpr());
2104 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2107 PrintExpr(E->getLHS());
2108 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2112 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2113 PrintExpr(E->getRHS());
2120 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2122 VisitStringLiteral(Node->getString());
2125 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
2127 Visit(E->getSubExpr());
2130 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2132 StmtRange ch = E->children();
2133 if (ch.first != ch.second) {
2137 if (ch.first == ch.second) break;
2144 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2146 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2150 ObjCDictionaryElement Element = E->getKeyValueElement(I);
2153 Visit(Element.Value);
2154 if (Element.isPackExpansion())
2160 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
2162 Node->getEncodedType().print(OS, Policy);
2166 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
2168 Node->getSelector().print(OS);
2172 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
2173 OS << "@protocol(" << *Node->getProtocol() << ')';
2176 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2178 switch (Mess->getReceiverKind()) {
2179 case ObjCMessageExpr::Instance:
2180 PrintExpr(Mess->getInstanceReceiver());
2183 case ObjCMessageExpr::Class:
2184 Mess->getClassReceiver().print(OS, Policy);
2187 case ObjCMessageExpr::SuperInstance:
2188 case ObjCMessageExpr::SuperClass:
2194 Selector selector = Mess->getSelector();
2195 if (selector.isUnarySelector()) {
2196 OS << selector.getNameForSlot(0);
2198 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
2199 if (i < selector.getNumArgs()) {
2200 if (i > 0) OS << ' ';
2201 if (selector.getIdentifierInfoForSlot(i))
2202 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
2206 else OS << ", "; // Handle variadic methods.
2208 PrintExpr(Mess->getArg(i));
2214 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2215 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2219 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2220 PrintExpr(E->getSubExpr());
2224 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
2225 OS << '(' << E->getBridgeKindName();
2226 E->getType().print(OS, Policy);
2228 PrintExpr(E->getSubExpr());
2231 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
2232 BlockDecl *BD = Node->getBlockDecl();
2235 const FunctionType *AFT = Node->getFunctionType();
2237 if (isa<FunctionNoProtoType>(AFT)) {
2239 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
2241 for (BlockDecl::param_iterator AI = BD->param_begin(),
2242 E = BD->param_end(); AI != E; ++AI) {
2243 if (AI != BD->param_begin()) OS << ", ";
2244 std::string ParamStr = (*AI)->getNameAsString();
2245 (*AI)->getType().print(OS, Policy, ParamStr);
2248 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
2249 if (FT->isVariadic()) {
2250 if (!BD->param_empty()) OS << ", ";
2258 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2259 PrintExpr(Node->getSourceExpr());
2262 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2263 // TODO: Print something reasonable for a TypoExpr, if necessary.
2264 assert(false && "Cannot print TypoExpr nodes");
2267 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2268 OS << "__builtin_astype(";
2269 PrintExpr(Node->getSrcExpr());
2271 Node->getType().print(OS, Policy);
2275 //===----------------------------------------------------------------------===//
2276 // Stmt method implementations
2277 //===----------------------------------------------------------------------===//
2279 void Stmt::dumpPretty(const ASTContext &Context) const {
2280 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
2283 void Stmt::printPretty(raw_ostream &OS,
2284 PrinterHelper *Helper,
2285 const PrintingPolicy &Policy,
2286 unsigned Indentation) const {
2287 StmtPrinter P(OS, Helper, Policy, Indentation);
2288 P.Visit(const_cast<Stmt*>(this));
2291 //===----------------------------------------------------------------------===//
2293 //===----------------------------------------------------------------------===//
2295 // Implement virtual destructor.
2296 PrinterHelper::~PrinterHelper() {}