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, '(');
804 //===----------------------------------------------------------------------===//
805 // OpenMP directives printing methods
806 //===----------------------------------------------------------------------===//
808 void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) {
809 OMPClausePrinter Printer(OS, Policy);
810 ArrayRef<OMPClause *> Clauses = S->clauses();
811 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
813 if (*I && !(*I)->isImplicit()) {
818 if (S->hasAssociatedStmt() && S->getAssociatedStmt()) {
819 assert(isa<CapturedStmt>(S->getAssociatedStmt()) &&
820 "Expected captured statement!");
821 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt();
826 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
827 Indent() << "#pragma omp parallel ";
828 PrintOMPExecutableDirective(Node);
831 void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
832 Indent() << "#pragma omp simd ";
833 PrintOMPExecutableDirective(Node);
836 void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
837 Indent() << "#pragma omp for ";
838 PrintOMPExecutableDirective(Node);
841 void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
842 Indent() << "#pragma omp for simd ";
843 PrintOMPExecutableDirective(Node);
846 void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
847 Indent() << "#pragma omp sections ";
848 PrintOMPExecutableDirective(Node);
851 void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
852 Indent() << "#pragma omp section";
853 PrintOMPExecutableDirective(Node);
856 void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
857 Indent() << "#pragma omp single ";
858 PrintOMPExecutableDirective(Node);
861 void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
862 Indent() << "#pragma omp master";
863 PrintOMPExecutableDirective(Node);
866 void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
867 Indent() << "#pragma omp critical";
868 if (Node->getDirectiveName().getName()) {
870 Node->getDirectiveName().printName(OS);
873 PrintOMPExecutableDirective(Node);
876 void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
877 Indent() << "#pragma omp parallel for ";
878 PrintOMPExecutableDirective(Node);
881 void StmtPrinter::VisitOMPParallelForSimdDirective(
882 OMPParallelForSimdDirective *Node) {
883 Indent() << "#pragma omp parallel for simd ";
884 PrintOMPExecutableDirective(Node);
887 void StmtPrinter::VisitOMPParallelSectionsDirective(
888 OMPParallelSectionsDirective *Node) {
889 Indent() << "#pragma omp parallel sections ";
890 PrintOMPExecutableDirective(Node);
893 void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
894 Indent() << "#pragma omp task ";
895 PrintOMPExecutableDirective(Node);
898 void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
899 Indent() << "#pragma omp taskyield";
900 PrintOMPExecutableDirective(Node);
903 void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
904 Indent() << "#pragma omp barrier";
905 PrintOMPExecutableDirective(Node);
908 void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
909 Indent() << "#pragma omp taskwait";
910 PrintOMPExecutableDirective(Node);
913 void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
914 Indent() << "#pragma omp taskgroup";
915 PrintOMPExecutableDirective(Node);
918 void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
919 Indent() << "#pragma omp flush ";
920 PrintOMPExecutableDirective(Node);
923 void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
924 Indent() << "#pragma omp ordered";
925 PrintOMPExecutableDirective(Node);
928 void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
929 Indent() << "#pragma omp atomic ";
930 PrintOMPExecutableDirective(Node);
933 void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
934 Indent() << "#pragma omp target ";
935 PrintOMPExecutableDirective(Node);
938 void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
939 Indent() << "#pragma omp teams ";
940 PrintOMPExecutableDirective(Node);
943 //===----------------------------------------------------------------------===//
944 // Expr printing methods.
945 //===----------------------------------------------------------------------===//
947 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
948 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
949 Qualifier->print(OS, Policy);
950 if (Node->hasTemplateKeyword())
952 OS << Node->getNameInfo();
953 if (Node->hasExplicitTemplateArgs())
954 TemplateSpecializationType::PrintTemplateArgumentList(
955 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
958 void StmtPrinter::VisitDependentScopeDeclRefExpr(
959 DependentScopeDeclRefExpr *Node) {
960 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
961 Qualifier->print(OS, Policy);
962 if (Node->hasTemplateKeyword())
964 OS << Node->getNameInfo();
965 if (Node->hasExplicitTemplateArgs())
966 TemplateSpecializationType::PrintTemplateArgumentList(
967 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
970 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
971 if (Node->getQualifier())
972 Node->getQualifier()->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::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
982 if (Node->getBase()) {
983 PrintExpr(Node->getBase());
984 OS << (Node->isArrow() ? "->" : ".");
986 OS << *Node->getDecl();
989 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
990 if (Node->isSuperReceiver())
992 else if (Node->isObjectReceiver() && Node->getBase()) {
993 PrintExpr(Node->getBase());
995 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
996 OS << Node->getClassReceiver()->getName() << ".";
999 if (Node->isImplicitProperty())
1000 Node->getImplicitPropertyGetter()->getSelector().print(OS);
1002 OS << Node->getExplicitProperty()->getName();
1005 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1007 PrintExpr(Node->getBaseExpr());
1009 PrintExpr(Node->getKeyExpr());
1013 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
1014 OS << PredefinedExpr::getIdentTypeName(Node->getIdentType());
1017 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
1018 unsigned value = Node->getValue();
1020 switch (Node->getKind()) {
1021 case CharacterLiteral::Ascii: break; // no prefix.
1022 case CharacterLiteral::Wide: OS << 'L'; break;
1023 case CharacterLiteral::UTF16: OS << 'u'; break;
1024 case CharacterLiteral::UTF32: OS << 'U'; break;
1035 // TODO: K&R: the meaning of '\\a' is different in traditional C
1041 // Nonstandard escape sequence.
1061 if (value < 256 && isPrintable((unsigned char)value))
1062 OS << "'" << (char)value << "'";
1063 else if (value < 256)
1064 OS << "'\\x" << llvm::format("%02x", value) << "'";
1065 else if (value <= 0xFFFF)
1066 OS << "'\\u" << llvm::format("%04x", value) << "'";
1068 OS << "'\\U" << llvm::format("%08x", value) << "'";
1072 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
1073 bool isSigned = Node->getType()->isSignedIntegerType();
1074 OS << Node->getValue().toString(10, isSigned);
1076 // Emit suffixes. Integer literals are always a builtin integer type.
1077 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1078 default: llvm_unreachable("Unexpected type for integer literal!");
1079 case BuiltinType::Char_S:
1080 case BuiltinType::Char_U: OS << "i8"; break;
1081 case BuiltinType::UChar: OS << "Ui8"; break;
1082 case BuiltinType::Short: OS << "i16"; break;
1083 case BuiltinType::UShort: OS << "Ui16"; break;
1084 case BuiltinType::Int: break; // no suffix.
1085 case BuiltinType::UInt: OS << 'U'; break;
1086 case BuiltinType::Long: OS << 'L'; break;
1087 case BuiltinType::ULong: OS << "UL"; break;
1088 case BuiltinType::LongLong: OS << "LL"; break;
1089 case BuiltinType::ULongLong: OS << "ULL"; break;
1090 case BuiltinType::Int128: OS << "i128"; break;
1091 case BuiltinType::UInt128: OS << "Ui128"; break;
1095 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1097 SmallString<16> Str;
1098 Node->getValue().toString(Str);
1100 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1101 OS << '.'; // Trailing dot in order to separate from ints.
1106 // Emit suffixes. Float literals are always a builtin float type.
1107 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1108 default: llvm_unreachable("Unexpected type for float literal!");
1109 case BuiltinType::Half: break; // FIXME: suffix?
1110 case BuiltinType::Double: break; // no suffix.
1111 case BuiltinType::Float: OS << 'F'; break;
1112 case BuiltinType::LongDouble: OS << 'L'; break;
1116 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1117 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
1120 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1121 PrintExpr(Node->getSubExpr());
1125 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
1126 Str->outputString(OS);
1128 void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1130 PrintExpr(Node->getSubExpr());
1133 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
1134 if (!Node->isPostfix()) {
1135 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1137 // Print a space if this is an "identifier operator" like __real, or if
1138 // it might be concatenated incorrectly like '+'.
1139 switch (Node->getOpcode()) {
1148 if (isa<UnaryOperator>(Node->getSubExpr()))
1153 PrintExpr(Node->getSubExpr());
1155 if (Node->isPostfix())
1156 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
1159 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1160 OS << "__builtin_offsetof(";
1161 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1163 bool PrintedSomething = false;
1164 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1165 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
1166 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
1169 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1171 PrintedSomething = true;
1175 // Skip implicit base indirections.
1176 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
1179 // Field or identifier node.
1180 IdentifierInfo *Id = ON.getFieldName();
1184 if (PrintedSomething)
1187 PrintedSomething = true;
1188 OS << Id->getName();
1193 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
1194 switch(Node->getKind()) {
1199 if (Policy.LangOpts.CPlusPlus)
1201 else if (Policy.LangOpts.C11)
1210 if (Node->isArgumentType()) {
1212 Node->getArgumentType().print(OS, Policy);
1216 PrintExpr(Node->getArgumentExpr());
1220 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1222 PrintExpr(Node->getControllingExpr());
1223 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
1225 QualType T = Node->getAssocType(i);
1229 T.print(OS, Policy);
1231 PrintExpr(Node->getAssocExpr(i));
1236 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
1237 PrintExpr(Node->getLHS());
1239 PrintExpr(Node->getRHS());
1243 void StmtPrinter::PrintCallArgs(CallExpr *Call) {
1244 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
1245 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1246 // Don't print any defaulted arguments
1251 PrintExpr(Call->getArg(i));
1255 void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1256 PrintExpr(Call->getCallee());
1258 PrintCallArgs(Call);
1261 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1262 // FIXME: Suppress printing implicit bases (like "this")
1263 PrintExpr(Node->getBase());
1265 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1266 FieldDecl *ParentDecl = ParentMember
1267 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr;
1269 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1270 OS << (Node->isArrow() ? "->" : ".");
1272 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1273 if (FD->isAnonymousStructOrUnion())
1276 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1277 Qualifier->print(OS, Policy);
1278 if (Node->hasTemplateKeyword())
1280 OS << Node->getMemberNameInfo();
1281 if (Node->hasExplicitTemplateArgs())
1282 TemplateSpecializationType::PrintTemplateArgumentList(
1283 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1285 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1286 PrintExpr(Node->getBase());
1287 OS << (Node->isArrow() ? "->isa" : ".isa");
1290 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1291 PrintExpr(Node->getBase());
1293 OS << Node->getAccessor().getName();
1295 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1297 Node->getTypeAsWritten().print(OS, Policy);
1299 PrintExpr(Node->getSubExpr());
1301 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1303 Node->getType().print(OS, Policy);
1305 PrintExpr(Node->getInitializer());
1307 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1308 // No need to print anything, simply forward to the subexpression.
1309 PrintExpr(Node->getSubExpr());
1311 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1312 PrintExpr(Node->getLHS());
1313 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1314 PrintExpr(Node->getRHS());
1316 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1317 PrintExpr(Node->getLHS());
1318 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1319 PrintExpr(Node->getRHS());
1321 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1322 PrintExpr(Node->getCond());
1324 PrintExpr(Node->getLHS());
1326 PrintExpr(Node->getRHS());
1332 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1333 PrintExpr(Node->getCommon());
1335 PrintExpr(Node->getFalseExpr());
1337 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1338 OS << "&&" << Node->getLabel()->getName();
1341 void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1343 PrintRawCompoundStmt(E->getSubStmt());
1347 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1348 OS << "__builtin_choose_expr(";
1349 PrintExpr(Node->getCond());
1351 PrintExpr(Node->getLHS());
1353 PrintExpr(Node->getRHS());
1357 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1361 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1362 OS << "__builtin_shufflevector(";
1363 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1365 PrintExpr(Node->getExpr(i));
1370 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1371 OS << "__builtin_convertvector(";
1372 PrintExpr(Node->getSrcExpr());
1374 Node->getType().print(OS, Policy);
1378 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1379 if (Node->getSyntacticForm()) {
1380 Visit(Node->getSyntacticForm());
1385 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1387 if (Node->getInit(i))
1388 PrintExpr(Node->getInit(i));
1395 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1397 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1399 PrintExpr(Node->getExpr(i));
1404 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1405 bool NeedsEquals = true;
1406 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1407 DEnd = Node->designators_end();
1409 if (D->isFieldDesignator()) {
1410 if (D->getDotLoc().isInvalid()) {
1411 if (IdentifierInfo *II = D->getFieldName()) {
1412 OS << II->getName() << ":";
1413 NeedsEquals = false;
1416 OS << "." << D->getFieldName()->getName();
1420 if (D->isArrayDesignator()) {
1421 PrintExpr(Node->getArrayIndex(*D));
1423 PrintExpr(Node->getArrayRangeStart(*D));
1425 PrintExpr(Node->getArrayRangeEnd(*D));
1435 PrintExpr(Node->getInit());
1438 void StmtPrinter::VisitDesignatedInitUpdateExpr(
1439 DesignatedInitUpdateExpr *Node) {
1442 PrintExpr(Node->getBase());
1445 OS << "/*updater*/";
1446 PrintExpr(Node->getUpdater());
1450 void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1451 OS << "/*no init*/";
1454 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1455 if (Policy.LangOpts.CPlusPlus) {
1456 OS << "/*implicit*/";
1457 Node->getType().print(OS, Policy);
1460 OS << "/*implicit*/(";
1461 Node->getType().print(OS, Policy);
1463 if (Node->getType()->isRecordType())
1470 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1471 OS << "__builtin_va_arg(";
1472 PrintExpr(Node->getSubExpr());
1474 Node->getType().print(OS, Policy);
1478 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1479 PrintExpr(Node->getSyntacticForm());
1482 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1483 const char *Name = nullptr;
1484 switch (Node->getOp()) {
1485 #define BUILTIN(ID, TYPE, ATTRS)
1486 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1487 case AtomicExpr::AO ## ID: \
1490 #include "clang/Basic/Builtins.def"
1494 // AtomicExpr stores its subexpressions in a permuted order.
1495 PrintExpr(Node->getPtr());
1496 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1497 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
1499 PrintExpr(Node->getVal1());
1501 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1502 Node->isCmpXChg()) {
1504 PrintExpr(Node->getVal2());
1506 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1507 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1509 PrintExpr(Node->getWeak());
1511 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1513 PrintExpr(Node->getOrder());
1515 if (Node->isCmpXChg()) {
1517 PrintExpr(Node->getOrderFail());
1523 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1524 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1526 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1528 #include "clang/Basic/OperatorKinds.def"
1531 OverloadedOperatorKind Kind = Node->getOperator();
1532 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1533 if (Node->getNumArgs() == 1) {
1534 OS << OpStrings[Kind] << ' ';
1535 PrintExpr(Node->getArg(0));
1537 PrintExpr(Node->getArg(0));
1538 OS << ' ' << OpStrings[Kind];
1540 } else if (Kind == OO_Arrow) {
1541 PrintExpr(Node->getArg(0));
1542 } else if (Kind == OO_Call) {
1543 PrintExpr(Node->getArg(0));
1545 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1548 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1549 PrintExpr(Node->getArg(ArgIdx));
1552 } else if (Kind == OO_Subscript) {
1553 PrintExpr(Node->getArg(0));
1555 PrintExpr(Node->getArg(1));
1557 } else if (Node->getNumArgs() == 1) {
1558 OS << OpStrings[Kind] << ' ';
1559 PrintExpr(Node->getArg(0));
1560 } else if (Node->getNumArgs() == 2) {
1561 PrintExpr(Node->getArg(0));
1562 OS << ' ' << OpStrings[Kind] << ' ';
1563 PrintExpr(Node->getArg(1));
1565 llvm_unreachable("unknown overloaded operator");
1569 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1570 // If we have a conversion operator call only print the argument.
1571 CXXMethodDecl *MD = Node->getMethodDecl();
1572 if (MD && isa<CXXConversionDecl>(MD)) {
1573 PrintExpr(Node->getImplicitObjectArgument());
1576 VisitCallExpr(cast<CallExpr>(Node));
1579 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1580 PrintExpr(Node->getCallee());
1582 PrintCallArgs(Node->getConfig());
1584 PrintCallArgs(Node);
1588 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1589 OS << Node->getCastName() << '<';
1590 Node->getTypeAsWritten().print(OS, Policy);
1592 PrintExpr(Node->getSubExpr());
1596 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1597 VisitCXXNamedCastExpr(Node);
1600 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1601 VisitCXXNamedCastExpr(Node);
1604 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1605 VisitCXXNamedCastExpr(Node);
1608 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1609 VisitCXXNamedCastExpr(Node);
1612 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1614 if (Node->isTypeOperand()) {
1615 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1617 PrintExpr(Node->getExprOperand());
1622 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1624 if (Node->isTypeOperand()) {
1625 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1627 PrintExpr(Node->getExprOperand());
1632 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1633 PrintExpr(Node->getBaseExpr());
1634 if (Node->isArrow())
1638 if (NestedNameSpecifier *Qualifier =
1639 Node->getQualifierLoc().getNestedNameSpecifier())
1640 Qualifier->print(OS, Policy);
1641 OS << Node->getPropertyDecl()->getDeclName();
1644 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1645 switch (Node->getLiteralOperatorKind()) {
1646 case UserDefinedLiteral::LOK_Raw:
1647 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
1649 case UserDefinedLiteral::LOK_Template: {
1650 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1651 const TemplateArgumentList *Args =
1652 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1655 if (Args->size() != 1) {
1656 OS << "operator \"\" " << Node->getUDSuffix()->getName();
1657 TemplateSpecializationType::PrintTemplateArgumentList(
1658 OS, Args->data(), Args->size(), Policy);
1663 const TemplateArgument &Pack = Args->get(0);
1664 for (const auto &P : Pack.pack_elements()) {
1665 char C = (char)P.getAsIntegral().getZExtValue();
1670 case UserDefinedLiteral::LOK_Integer: {
1671 // Print integer literal without suffix.
1672 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1673 OS << Int->getValue().toString(10, /*isSigned*/false);
1676 case UserDefinedLiteral::LOK_Floating: {
1677 // Print floating literal without suffix.
1678 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1679 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1682 case UserDefinedLiteral::LOK_String:
1683 case UserDefinedLiteral::LOK_Character:
1684 PrintExpr(Node->getCookedLiteral());
1687 OS << Node->getUDSuffix()->getName();
1690 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1691 OS << (Node->getValue() ? "true" : "false");
1694 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1698 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1702 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1703 if (!Node->getSubExpr())
1707 PrintExpr(Node->getSubExpr());
1711 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1712 // Nothing to print: we picked up the default argument.
1715 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1716 // Nothing to print: we picked up the default initializer.
1719 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1720 Node->getType().print(OS, Policy);
1721 // If there are no parens, this is list-initialization, and the braces are
1722 // part of the syntax of the inner construct.
1723 if (Node->getLParenLoc().isValid())
1725 PrintExpr(Node->getSubExpr());
1726 if (Node->getLParenLoc().isValid())
1730 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1731 PrintExpr(Node->getSubExpr());
1734 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1735 Node->getType().print(OS, Policy);
1736 if (Node->isStdInitListInitialization())
1737 /* Nothing to do; braces are part of creating the std::initializer_list. */;
1738 else if (Node->isListInitialization())
1742 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1743 ArgEnd = Node->arg_end();
1744 Arg != ArgEnd; ++Arg) {
1745 if (Arg->isDefaultArgument())
1747 if (Arg != Node->arg_begin())
1751 if (Node->isStdInitListInitialization())
1753 else if (Node->isListInitialization())
1759 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1761 bool NeedComma = false;
1762 switch (Node->getCaptureDefault()) {
1776 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1777 CEnd = Node->explicit_capture_end();
1784 switch (C->getCaptureKind()) {
1790 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
1792 OS << C->getCapturedVar()->getName();
1796 OS << C->getCapturedVar()->getName();
1799 llvm_unreachable("VLA type in explicit captures.");
1802 if (Node->isInitCapture(C))
1803 PrintExpr(C->getCapturedVar()->getInit());
1807 if (Node->hasExplicitParameters()) {
1809 CXXMethodDecl *Method = Node->getCallOperator();
1811 for (auto P : Method->params()) {
1817 std::string ParamStr = P->getNameAsString();
1818 P->getOriginalType().print(OS, Policy, ParamStr);
1820 if (Method->isVariadic()) {
1827 if (Node->isMutable())
1830 const FunctionProtoType *Proto
1831 = Method->getType()->getAs<FunctionProtoType>();
1832 Proto->printExceptionSpecification(OS, Policy);
1834 // FIXME: Attributes
1836 // Print the trailing return type if it was specified in the source.
1837 if (Node->hasExplicitResultType()) {
1839 Proto->getReturnType().print(OS, Policy);
1844 CompoundStmt *Body = Node->getBody();
1849 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
1850 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1851 TSInfo->getType().print(OS, Policy);
1853 Node->getType().print(OS, Policy);
1857 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1858 if (E->isGlobalNew())
1861 unsigned NumPlace = E->getNumPlacementArgs();
1862 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
1864 PrintExpr(E->getPlacementArg(0));
1865 for (unsigned i = 1; i < NumPlace; ++i) {
1866 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1869 PrintExpr(E->getPlacementArg(i));
1873 if (E->isParenTypeId())
1876 if (Expr *Size = E->getArraySize()) {
1877 llvm::raw_string_ostream s(TypeS);
1879 Size->printPretty(s, Helper, Policy);
1882 E->getAllocatedType().print(OS, Policy, TypeS);
1883 if (E->isParenTypeId())
1886 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1888 if (InitStyle == CXXNewExpr::CallInit)
1890 PrintExpr(E->getInitializer());
1891 if (InitStyle == CXXNewExpr::CallInit)
1896 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1897 if (E->isGlobalDelete())
1900 if (E->isArrayForm())
1902 PrintExpr(E->getArgument());
1905 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1906 PrintExpr(E->getBase());
1911 if (E->getQualifier())
1912 E->getQualifier()->print(OS, Policy);
1915 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1916 OS << II->getName();
1918 E->getDestroyedType().print(OS, Policy);
1921 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1922 if (E->isListInitialization() && !E->isStdInitListInitialization())
1925 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1926 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1927 // Don't print any defaulted arguments
1932 PrintExpr(E->getArg(i));
1935 if (E->isListInitialization() && !E->isStdInitListInitialization())
1939 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
1940 PrintExpr(E->getSubExpr());
1943 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
1944 // Just forward to the subexpression.
1945 PrintExpr(E->getSubExpr());
1949 StmtPrinter::VisitCXXUnresolvedConstructExpr(
1950 CXXUnresolvedConstructExpr *Node) {
1951 Node->getTypeAsWritten().print(OS, Policy);
1953 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
1954 ArgEnd = Node->arg_end();
1955 Arg != ArgEnd; ++Arg) {
1956 if (Arg != Node->arg_begin())
1963 void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1964 CXXDependentScopeMemberExpr *Node) {
1965 if (!Node->isImplicitAccess()) {
1966 PrintExpr(Node->getBase());
1967 OS << (Node->isArrow() ? "->" : ".");
1969 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1970 Qualifier->print(OS, Policy);
1971 if (Node->hasTemplateKeyword())
1973 OS << Node->getMemberNameInfo();
1974 if (Node->hasExplicitTemplateArgs())
1975 TemplateSpecializationType::PrintTemplateArgumentList(
1976 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1979 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
1980 if (!Node->isImplicitAccess()) {
1981 PrintExpr(Node->getBase());
1982 OS << (Node->isArrow() ? "->" : ".");
1984 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1985 Qualifier->print(OS, Policy);
1986 if (Node->hasTemplateKeyword())
1988 OS << Node->getMemberNameInfo();
1989 if (Node->hasExplicitTemplateArgs())
1990 TemplateSpecializationType::PrintTemplateArgumentList(
1991 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1994 static const char *getTypeTraitName(TypeTrait TT) {
1996 #define TYPE_TRAIT_1(Spelling, Name, Key) \
1997 case clang::UTT_##Name: return #Spelling;
1998 #define TYPE_TRAIT_2(Spelling, Name, Key) \
1999 case clang::BTT_##Name: return #Spelling;
2000 #define TYPE_TRAIT_N(Spelling, Name, Key) \
2001 case clang::TT_##Name: return #Spelling;
2002 #include "clang/Basic/TokenKinds.def"
2004 llvm_unreachable("Type trait not covered by switch");
2007 static const char *getTypeTraitName(ArrayTypeTrait ATT) {
2009 case ATT_ArrayRank: return "__array_rank";
2010 case ATT_ArrayExtent: return "__array_extent";
2012 llvm_unreachable("Array type trait not covered by switch");
2015 static const char *getExpressionTraitName(ExpressionTrait ET) {
2017 case ET_IsLValueExpr: return "__is_lvalue_expr";
2018 case ET_IsRValueExpr: return "__is_rvalue_expr";
2020 llvm_unreachable("Expression type trait not covered by switch");
2023 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2024 OS << getTypeTraitName(E->getTrait()) << "(";
2025 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2028 E->getArg(I)->getType().print(OS, Policy);
2033 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
2034 OS << getTypeTraitName(E->getTrait()) << '(';
2035 E->getQueriedType().print(OS, Policy);
2039 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
2040 OS << getExpressionTraitName(E->getTrait()) << '(';
2041 PrintExpr(E->getQueriedExpression());
2045 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2047 PrintExpr(E->getOperand());
2051 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
2052 PrintExpr(E->getPattern());
2056 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2057 OS << "sizeof...(" << *E->getPack() << ")";
2060 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2061 SubstNonTypeTemplateParmPackExpr *Node) {
2062 OS << *Node->getParameterPack();
2065 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2066 SubstNonTypeTemplateParmExpr *Node) {
2067 Visit(Node->getReplacement());
2070 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2071 OS << *E->getParameterPack();
2074 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2075 PrintExpr(Node->GetTemporaryExpr());
2078 void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2081 PrintExpr(E->getLHS());
2082 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2086 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2087 PrintExpr(E->getRHS());
2094 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2096 VisitStringLiteral(Node->getString());
2099 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
2101 Visit(E->getSubExpr());
2104 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2106 StmtRange ch = E->children();
2107 if (ch.first != ch.second) {
2111 if (ch.first == ch.second) break;
2118 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2120 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2124 ObjCDictionaryElement Element = E->getKeyValueElement(I);
2127 Visit(Element.Value);
2128 if (Element.isPackExpansion())
2134 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
2136 Node->getEncodedType().print(OS, Policy);
2140 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
2142 Node->getSelector().print(OS);
2146 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
2147 OS << "@protocol(" << *Node->getProtocol() << ')';
2150 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2152 switch (Mess->getReceiverKind()) {
2153 case ObjCMessageExpr::Instance:
2154 PrintExpr(Mess->getInstanceReceiver());
2157 case ObjCMessageExpr::Class:
2158 Mess->getClassReceiver().print(OS, Policy);
2161 case ObjCMessageExpr::SuperInstance:
2162 case ObjCMessageExpr::SuperClass:
2168 Selector selector = Mess->getSelector();
2169 if (selector.isUnarySelector()) {
2170 OS << selector.getNameForSlot(0);
2172 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
2173 if (i < selector.getNumArgs()) {
2174 if (i > 0) OS << ' ';
2175 if (selector.getIdentifierInfoForSlot(i))
2176 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
2180 else OS << ", "; // Handle variadic methods.
2182 PrintExpr(Mess->getArg(i));
2188 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2189 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2193 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2194 PrintExpr(E->getSubExpr());
2198 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
2199 OS << '(' << E->getBridgeKindName();
2200 E->getType().print(OS, Policy);
2202 PrintExpr(E->getSubExpr());
2205 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
2206 BlockDecl *BD = Node->getBlockDecl();
2209 const FunctionType *AFT = Node->getFunctionType();
2211 if (isa<FunctionNoProtoType>(AFT)) {
2213 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
2215 for (BlockDecl::param_iterator AI = BD->param_begin(),
2216 E = BD->param_end(); AI != E; ++AI) {
2217 if (AI != BD->param_begin()) OS << ", ";
2218 std::string ParamStr = (*AI)->getNameAsString();
2219 (*AI)->getType().print(OS, Policy, ParamStr);
2222 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
2223 if (FT->isVariadic()) {
2224 if (!BD->param_empty()) OS << ", ";
2232 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2233 PrintExpr(Node->getSourceExpr());
2236 void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2237 // TODO: Print something reasonable for a TypoExpr, if necessary.
2238 assert(false && "Cannot print TypoExpr nodes");
2241 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2242 OS << "__builtin_astype(";
2243 PrintExpr(Node->getSrcExpr());
2245 Node->getType().print(OS, Policy);
2249 //===----------------------------------------------------------------------===//
2250 // Stmt method implementations
2251 //===----------------------------------------------------------------------===//
2253 void Stmt::dumpPretty(const ASTContext &Context) const {
2254 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
2257 void Stmt::printPretty(raw_ostream &OS,
2258 PrinterHelper *Helper,
2259 const PrintingPolicy &Policy,
2260 unsigned Indentation) const {
2261 StmtPrinter P(OS, Helper, Policy, Indentation);
2262 P.Visit(const_cast<Stmt*>(this));
2265 //===----------------------------------------------------------------------===//
2267 //===----------------------------------------------------------------------===//
2269 // Implement virtual destructor.
2270 PrinterHelper::~PrinterHelper() {}